clang 23.0.0git
UnwrappedLineParser.cpp
Go to the documentation of this file.
1//===--- UnwrappedLineParser.cpp - Format C++ code ------------------------===//
2//
3// Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions.
4// See https://llvm.org/LICENSE.txt for license information.
5// SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception
6//
7//===----------------------------------------------------------------------===//
8///
9/// \file
10/// This file contains the implementation of the UnwrappedLineParser,
11/// which turns a stream of tokens into UnwrappedLines.
12///
13//===----------------------------------------------------------------------===//
14
15#include "UnwrappedLineParser.h"
16#include "FormatToken.h"
17#include "FormatTokenSource.h"
18#include "Macros.h"
19#include "TokenAnnotator.h"
21#include "llvm/ADT/STLExtras.h"
22#include "llvm/ADT/StringRef.h"
23#include "llvm/Support/Debug.h"
24#include "llvm/Support/raw_os_ostream.h"
25#include "llvm/Support/raw_ostream.h"
26
27#include <utility>
28
29#define DEBUG_TYPE "format-parser"
30
31namespace clang {
32namespace format {
33
34namespace {
35
36void printLine(llvm::raw_ostream &OS, const UnwrappedLine &Line,
37 StringRef Prefix = "", bool PrintText = false) {
38 OS << Prefix << "Line(" << Line.Level << ", FSC=" << Line.FirstStartColumn
39 << ")" << (Line.InPPDirective ? " MACRO" : "") << ": ";
40 bool NewLine = false;
41 for (std::list<UnwrappedLineNode>::const_iterator I = Line.Tokens.begin(),
42 E = Line.Tokens.end();
43 I != E; ++I) {
44 if (NewLine) {
45 OS << Prefix;
46 NewLine = false;
47 }
48 OS << I->Tok->Tok.getName() << "["
49 << "T=" << (unsigned)I->Tok->getType()
50 << ", OC=" << I->Tok->OriginalColumn << ", \"" << I->Tok->TokenText
51 << "\"] ";
52 for (const auto *CI = I->Children.begin(), *CE = I->Children.end();
53 CI != CE; ++CI) {
54 OS << "\n";
55 printLine(OS, *CI, (Prefix + " ").str());
56 NewLine = true;
57 }
58 }
59 if (!NewLine)
60 OS << "\n";
61}
62
63[[maybe_unused]] static void printDebugInfo(const UnwrappedLine &Line) {
64 printLine(llvm::dbgs(), Line);
65}
66
67class ScopedDeclarationState {
68public:
69 ScopedDeclarationState(UnwrappedLine &Line, llvm::BitVector &Stack,
70 bool MustBeDeclaration)
71 : Line(Line), Stack(Stack) {
72 Line.MustBeDeclaration = MustBeDeclaration;
73 Stack.push_back(MustBeDeclaration);
74 }
75 ~ScopedDeclarationState() {
76 Stack.pop_back();
77 if (!Stack.empty())
78 Line.MustBeDeclaration = Stack.back();
79 else
80 Line.MustBeDeclaration = true;
81 }
82
83private:
84 UnwrappedLine &Line;
85 llvm::BitVector &Stack;
86};
87
88} // end anonymous namespace
89
90std::ostream &operator<<(std::ostream &Stream, const UnwrappedLine &Line) {
91 llvm::raw_os_ostream OS(Stream);
92 printLine(OS, Line);
93 return Stream;
94}
95
97public:
99 bool SwitchToPreprocessorLines = false)
100 : Parser(Parser), OriginalLines(Parser.CurrentLines) {
101 if (SwitchToPreprocessorLines)
102 Parser.CurrentLines = &Parser.PreprocessorDirectives;
103 else if (!Parser.Line->Tokens.empty())
104 Parser.CurrentLines = &Parser.Line->Tokens.back().Children;
105 PreBlockLine = std::move(Parser.Line);
106 Parser.Line = std::make_unique<UnwrappedLine>();
107 Parser.Line->Level = PreBlockLine->Level;
108 Parser.Line->PPLevel = PreBlockLine->PPLevel;
109 Parser.Line->InPPDirective = PreBlockLine->InPPDirective;
110 Parser.Line->InMacroBody = PreBlockLine->InMacroBody;
111 Parser.Line->UnbracedBodyLevel = PreBlockLine->UnbracedBodyLevel;
112 }
113
115 if (!Parser.Line->Tokens.empty())
116 Parser.addUnwrappedLine();
117 assert(Parser.Line->Tokens.empty());
118 Parser.Line = std::move(PreBlockLine);
119 if (Parser.CurrentLines == &Parser.PreprocessorDirectives)
120 Parser.AtEndOfPPLine = true;
121 Parser.CurrentLines = OriginalLines;
122 }
123
124private:
126
127 std::unique_ptr<UnwrappedLine> PreBlockLine;
128 SmallVectorImpl<UnwrappedLine> *OriginalLines;
129};
130
132public:
134 const FormatStyle &Style, unsigned &LineLevel)
136 Style.BraceWrapping.AfterControlStatement ==
137 FormatStyle::BWACS_Always,
138 Style.BraceWrapping.IndentBraces) {}
140 bool WrapBrace, bool IndentBrace)
141 : LineLevel(LineLevel), OldLineLevel(LineLevel) {
142 if (WrapBrace)
143 Parser->addUnwrappedLine();
144 if (IndentBrace)
145 ++LineLevel;
146 }
147 ~CompoundStatementIndenter() { LineLevel = OldLineLevel; }
148
149private:
150 unsigned &LineLevel;
151 unsigned OldLineLevel;
152};
153
155 SourceManager &SourceMgr, const FormatStyle &Style,
156 const AdditionalKeywords &Keywords, unsigned FirstStartColumn,
158 llvm::SpecificBumpPtrAllocator<FormatToken> &Allocator,
159 IdentifierTable &IdentTable)
160 : Line(new UnwrappedLine), AtEndOfPPLine(false), CurrentLines(&Lines),
161 Style(Style), IsCpp(Style.isCpp()),
162 LangOpts(getFormattingLangOpts(Style)), Keywords(Keywords),
163 CommentPragmasRegex(Style.CommentPragmas), Tokens(nullptr),
164 Callback(Callback), AllTokens(Tokens), PPBranchLevel(-1),
165 IncludeGuard(getIncludeGuardState(Style.IndentPPDirectives)),
166 IncludeGuardToken(nullptr), FirstStartColumn(FirstStartColumn),
167 Macros(Style.Macros, SourceMgr, Style, Allocator, IdentTable) {}
168
169void UnwrappedLineParser::reset() {
170 PPBranchLevel = -1;
171 IncludeGuard = getIncludeGuardState(Style.IndentPPDirectives);
172 IncludeGuardToken = nullptr;
173 Line.reset(new UnwrappedLine);
174 CommentsBeforeNextToken.clear();
175 FormatTok = nullptr;
176 AtEndOfPPLine = false;
177 IsDecltypeAutoFunction = false;
178 PreprocessorDirectives.clear();
179 CurrentLines = &Lines;
180 DeclarationScopeStack.clear();
181 NestedTooDeep.clear();
182 NestedLambdas.clear();
183 PPStack.clear();
184 Line->FirstStartColumn = FirstStartColumn;
185
186 if (!Unexpanded.empty())
187 for (FormatToken *Token : AllTokens)
188 Token->MacroCtx.reset();
189 CurrentExpandedLines.clear();
190 ExpandedLines.clear();
191 Unexpanded.clear();
192 InExpansion = false;
193 Reconstruct.reset();
194}
195
197 IndexedTokenSource TokenSource(AllTokens);
198 Line->FirstStartColumn = FirstStartColumn;
199 do {
200 LLVM_DEBUG(llvm::dbgs() << "----\n");
201 reset();
202 Tokens = &TokenSource;
203 TokenSource.reset();
204
205 readToken();
206 parseFile();
207
208 // If we found an include guard then all preprocessor directives (other than
209 // the guard) are over-indented by one.
210 if (IncludeGuard == IG_Found) {
211 for (auto &Line : Lines)
212 if (Line.InPPDirective && Line.Level > 0)
213 --Line.Level;
214 }
215
216 // Create line with eof token.
217 assert(eof());
218 pushToken(FormatTok);
219 addUnwrappedLine();
220
221 // In a first run, format everything with the lines containing macro calls
222 // replaced by the expansion.
223 if (!ExpandedLines.empty()) {
224 LLVM_DEBUG(llvm::dbgs() << "Expanded lines:\n");
225 for (const auto &Line : Lines) {
226 if (!Line.Tokens.empty()) {
227 auto it = ExpandedLines.find(Line.Tokens.begin()->Tok);
228 if (it != ExpandedLines.end()) {
229 for (const auto &Expanded : it->second) {
230 LLVM_DEBUG(printDebugInfo(Expanded));
231 Callback.consumeUnwrappedLine(Expanded);
232 }
233 continue;
234 }
235 }
236 LLVM_DEBUG(printDebugInfo(Line));
237 Callback.consumeUnwrappedLine(Line);
238 }
239 Callback.finishRun();
240 }
241
242 LLVM_DEBUG(llvm::dbgs() << "Unwrapped lines:\n");
243 for (const UnwrappedLine &Line : Lines) {
244 LLVM_DEBUG(printDebugInfo(Line));
245 Callback.consumeUnwrappedLine(Line);
246 }
247 Callback.finishRun();
248 Lines.clear();
249 while (!PPLevelBranchIndex.empty() &&
250 PPLevelBranchIndex.back() + 1 >= PPLevelBranchCount.back()) {
251 PPLevelBranchIndex.resize(PPLevelBranchIndex.size() - 1);
252 PPLevelBranchCount.resize(PPLevelBranchCount.size() - 1);
253 }
254 if (!PPLevelBranchIndex.empty()) {
255 ++PPLevelBranchIndex.back();
256 assert(PPLevelBranchIndex.size() == PPLevelBranchCount.size());
257 assert(PPLevelBranchIndex.back() <= PPLevelBranchCount.back());
258 }
259 } while (!PPLevelBranchIndex.empty());
260}
261
262void UnwrappedLineParser::parseFile() {
263 // The top-level context in a file always has declarations, except for pre-
264 // processor directives and JavaScript files.
265 bool MustBeDeclaration = !Line->InPPDirective && !Style.isJavaScript();
266 ScopedDeclarationState DeclarationState(*Line, DeclarationScopeStack,
267 MustBeDeclaration);
268 if (Style.isTextProto() || (Style.isJson() && FormatTok->IsFirst))
269 parseBracedList();
270 else
271 parseLevel();
272 // Make sure to format the remaining tokens.
273 //
274 // LK_TextProto is special since its top-level is parsed as the body of a
275 // braced list, which does not necessarily have natural line separators such
276 // as a semicolon. Comments after the last entry that have been determined to
277 // not belong to that line, as in:
278 // key: value
279 // // endfile comment
280 // do not have a chance to be put on a line of their own until this point.
281 // Here we add this newline before end-of-file comments.
282 if (Style.isTextProto() && !CommentsBeforeNextToken.empty())
283 addUnwrappedLine();
284 flushComments(true);
285 addUnwrappedLine();
286}
287
288void UnwrappedLineParser::parseCSharpGenericTypeConstraint() {
289 do {
290 switch (FormatTok->Tok.getKind()) {
291 case tok::l_brace:
292 case tok::semi:
293 return;
294 default:
295 if (FormatTok->is(Keywords.kw_where)) {
296 addUnwrappedLine();
297 nextToken();
298 parseCSharpGenericTypeConstraint();
299 break;
300 }
301 nextToken();
302 break;
303 }
304 } while (!eof());
305}
306
307void UnwrappedLineParser::parseCSharpAttribute() {
308 int UnpairedSquareBrackets = 1;
309 do {
310 switch (FormatTok->Tok.getKind()) {
311 case tok::r_square:
312 nextToken();
313 --UnpairedSquareBrackets;
314 if (UnpairedSquareBrackets == 0) {
315 addUnwrappedLine();
316 return;
317 }
318 break;
319 case tok::l_square:
320 ++UnpairedSquareBrackets;
321 nextToken();
322 break;
323 default:
324 nextToken();
325 break;
326 }
327 } while (!eof());
328}
329
330bool UnwrappedLineParser::precededByCommentOrPPDirective() const {
331 if (!Lines.empty() && Lines.back().InPPDirective)
332 return true;
333
334 const FormatToken *Previous = Tokens->getPreviousToken();
335 return Previous && Previous->is(tok::comment) &&
336 (Previous->IsMultiline || Previous->NewlinesBefore > 0);
337}
338
339/// Parses a level, that is ???.
340/// \param OpeningBrace Opening brace (\p nullptr if absent) of that level.
341/// \param IfKind The \p if statement kind in the level.
342/// \param IfLeftBrace The left brace of the \p if block in the level.
343/// \returns true if a simple block of if/else/for/while, or false otherwise.
344/// (A simple block has a single statement.)
345bool UnwrappedLineParser::parseLevel(const FormatToken *OpeningBrace,
346 IfStmtKind *IfKind,
347 FormatToken **IfLeftBrace) {
348 const bool InRequiresExpression =
349 OpeningBrace && OpeningBrace->is(TT_RequiresExpressionLBrace);
350 const bool IsPrecededByCommentOrPPDirective =
351 !Style.RemoveBracesLLVM || precededByCommentOrPPDirective();
352 FormatToken *IfLBrace = nullptr;
353 bool HasDoWhile = false;
354 bool HasLabel = false;
355 unsigned StatementCount = 0;
356 bool SwitchLabelEncountered = false;
357
358 do {
359 if (FormatTok->isAttribute()) {
360 nextToken();
361 if (FormatTok->is(tok::l_paren))
362 parseParens();
363 continue;
364 }
365 tok::TokenKind Kind = FormatTok->Tok.getKind();
366 if (FormatTok->is(TT_MacroBlockBegin))
367 Kind = tok::l_brace;
368 else if (FormatTok->is(TT_MacroBlockEnd))
369 Kind = tok::r_brace;
370
371 auto ParseDefault = [this, OpeningBrace, IfKind, &IfLBrace, &HasDoWhile,
372 &HasLabel, &StatementCount] {
373 parseStructuralElement(OpeningBrace, IfKind, &IfLBrace,
374 HasDoWhile ? nullptr : &HasDoWhile,
375 HasLabel ? nullptr : &HasLabel);
376 ++StatementCount;
377 assert(StatementCount > 0 && "StatementCount overflow!");
378 };
379
380 switch (Kind) {
381 case tok::comment:
382 nextToken();
383 addUnwrappedLine();
384 break;
385 case tok::l_brace:
386 if (InRequiresExpression) {
387 FormatTok->setFinalizedType(TT_CompoundRequirementLBrace);
388 } else if (FormatTok->Previous &&
389 FormatTok->Previous->ClosesRequiresClause) {
390 // We need the 'default' case here to correctly parse a function
391 // l_brace.
392 ParseDefault();
393 continue;
394 }
395 if (!InRequiresExpression && FormatTok->isNot(TT_MacroBlockBegin)) {
396 if (tryToParseBracedList())
397 continue;
398 FormatTok->setFinalizedType(TT_BlockLBrace);
399 }
400 parseBlock();
401 ++StatementCount;
402 assert(StatementCount > 0 && "StatementCount overflow!");
403 addUnwrappedLine();
404 break;
405 case tok::r_brace:
406 if (OpeningBrace) {
407 if (!Style.RemoveBracesLLVM || Line->InPPDirective ||
408 OpeningBrace->isNoneOf(TT_ControlStatementLBrace, TT_ElseLBrace)) {
409 return false;
410 }
411 if (FormatTok->isNot(tok::r_brace) || StatementCount != 1 || HasLabel ||
412 HasDoWhile || IsPrecededByCommentOrPPDirective ||
413 precededByCommentOrPPDirective()) {
414 return false;
415 }
416 const FormatToken *Next = Tokens->peekNextToken();
417 if (Next->is(tok::comment) && Next->NewlinesBefore == 0)
418 return false;
419 if (IfLeftBrace)
420 *IfLeftBrace = IfLBrace;
421 return true;
422 }
423 nextToken();
424 addUnwrappedLine();
425 break;
426 case tok::kw_default: {
427 unsigned StoredPosition = Tokens->getPosition();
428 auto *Next = Tokens->getNextNonComment();
429 FormatTok = Tokens->setPosition(StoredPosition);
430 if (Next->isNoneOf(tok::colon, tok::arrow)) {
431 // default not followed by `:` or `->` is not a case label; treat it
432 // like an identifier.
433 parseStructuralElement();
434 break;
435 }
436 // Else, if it is 'default:', fall through to the case handling.
437 [[fallthrough]];
438 }
439 case tok::kw_case:
440 if (Style.Language == FormatStyle::LK_Proto || Style.isVerilog() ||
441 (Style.isJavaScript() && Line->MustBeDeclaration)) {
442 // Proto: there are no switch/case statements
443 // Verilog: Case labels don't have this word. We handle case
444 // labels including default in TokenAnnotator.
445 // JavaScript: A 'case: string' style field declaration.
446 ParseDefault();
447 break;
448 }
449 if (!SwitchLabelEncountered &&
450 (Style.IndentCaseLabels ||
451 (OpeningBrace && OpeningBrace->is(TT_SwitchExpressionLBrace)) ||
452 (Line->InPPDirective && Line->Level == 1))) {
453 ++Line->Level;
454 }
455 SwitchLabelEncountered = true;
456 parseStructuralElement();
457 break;
458 case tok::l_square:
459 if (Style.isCSharp()) {
460 nextToken();
461 parseCSharpAttribute();
462 break;
463 }
464 if (handleCppAttributes())
465 break;
466 [[fallthrough]];
467 default:
468 ParseDefault();
469 break;
470 }
471 } while (!eof());
472
473 return false;
474}
475
476void UnwrappedLineParser::calculateBraceTypes(bool ExpectClassBody) {
477 // We'll parse forward through the tokens until we hit
478 // a closing brace or eof - note that getNextToken() will
479 // parse macros, so this will magically work inside macro
480 // definitions, too.
481 unsigned StoredPosition = Tokens->getPosition();
482 FormatToken *Tok = FormatTok;
483 const FormatToken *PrevTok = Tok->Previous;
484 // Keep a stack of positions of lbrace tokens. We will
485 // update information about whether an lbrace starts a
486 // braced init list or a different block during the loop.
487 struct StackEntry {
489 const FormatToken *PrevTok;
490 };
491 SmallVector<StackEntry, 8> LBraceStack;
492 assert(Tok->is(tok::l_brace));
493
494 do {
495 auto *NextTok = Tokens->getNextNonComment();
496
497 if (!Line->InMacroBody && !Style.isTableGen()) {
498 // Skip PPDirective lines (except macro definitions) and comments.
499 while (NextTok->is(tok::hash)) {
500 NextTok = Tokens->getNextToken();
501 if (NextTok->isOneOf(tok::pp_not_keyword, tok::pp_define))
502 break;
503 do {
504 NextTok = Tokens->getNextToken();
505 } while (!NextTok->HasUnescapedNewline && NextTok->isNot(tok::eof));
506
507 while (NextTok->is(tok::comment))
508 NextTok = Tokens->getNextToken();
509 }
510 }
511
512 switch (Tok->Tok.getKind()) {
513 case tok::l_brace:
514 if (Style.isJavaScript() && PrevTok) {
515 if (PrevTok->isOneOf(tok::colon, tok::less)) {
516 // A ':' indicates this code is in a type, or a braced list
517 // following a label in an object literal ({a: {b: 1}}).
518 // A '<' could be an object used in a comparison, but that is nonsense
519 // code (can never return true), so more likely it is a generic type
520 // argument (`X<{a: string; b: number}>`).
521 // The code below could be confused by semicolons between the
522 // individual members in a type member list, which would normally
523 // trigger BK_Block. In both cases, this must be parsed as an inline
524 // braced init.
525 Tok->setBlockKind(BK_BracedInit);
526 } else if (PrevTok->is(tok::r_paren)) {
527 // `) { }` can only occur in function or method declarations in JS.
528 Tok->setBlockKind(BK_Block);
529 }
530 } else if (Style.isJava() && PrevTok && PrevTok->is(tok::arrow)) {
531 Tok->setBlockKind(BK_Block);
532 } else {
533 Tok->setBlockKind(BK_Unknown);
534 }
535 LBraceStack.push_back({Tok, PrevTok});
536 break;
537 case tok::r_brace:
538 if (LBraceStack.empty())
539 break;
540 if (auto *LBrace = LBraceStack.back().Tok; LBrace->is(BK_Unknown)) {
541 bool ProbablyBracedList = false;
542 if (Style.Language == FormatStyle::LK_Proto) {
543 ProbablyBracedList = NextTok->isOneOf(tok::comma, tok::r_square);
544 } else if (LBrace->isNot(TT_EnumLBrace)) {
545 // Using OriginalColumn to distinguish between ObjC methods and
546 // binary operators is a bit hacky.
547 bool NextIsObjCMethod = NextTok->isOneOf(tok::plus, tok::minus) &&
548 NextTok->OriginalColumn == 0;
549
550 // Try to detect a braced list. Note that regardless how we mark inner
551 // braces here, we will overwrite the BlockKind later if we parse a
552 // braced list (where all blocks inside are by default braced lists),
553 // or when we explicitly detect blocks (for example while parsing
554 // lambdas).
555
556 // If we already marked the opening brace as braced list, the closing
557 // must also be part of it.
558 ProbablyBracedList = LBrace->is(TT_BracedListLBrace);
559
560 ProbablyBracedList = ProbablyBracedList ||
561 (Style.isJavaScript() &&
562 NextTok->isOneOf(Keywords.kw_of, Keywords.kw_in,
563 Keywords.kw_as));
564 ProbablyBracedList =
565 ProbablyBracedList ||
566 (IsCpp && (PrevTok->Tok.isLiteral() ||
567 NextTok->isOneOf(tok::l_paren, tok::arrow)));
568
569 // If there is a comma, or right paren after the closing brace, we
570 // assume this is a braced initializer list.
571 // FIXME: Some of these do not apply to JS, e.g. "} {" can never be a
572 // braced list in JS.
573 ProbablyBracedList =
574 ProbablyBracedList ||
575 NextTok->isOneOf(tok::comma, tok::period, tok::colon,
576 tok::r_paren, tok::r_square, tok::ellipsis);
577
578 // Distinguish between braced list in a constructor initializer list
579 // followed by constructor body, or just adjacent blocks.
580 ProbablyBracedList =
581 ProbablyBracedList ||
582 (NextTok->is(tok::l_brace) && LBraceStack.back().PrevTok &&
583 LBraceStack.back().PrevTok->isOneOf(tok::identifier,
584 tok::greater));
585
586 ProbablyBracedList =
587 ProbablyBracedList ||
588 (NextTok->is(tok::identifier) &&
589 PrevTok->isNoneOf(tok::semi, tok::r_brace, tok::l_brace));
590
591 ProbablyBracedList = ProbablyBracedList ||
592 (NextTok->is(tok::semi) &&
593 (!ExpectClassBody || LBraceStack.size() != 1));
594
595 ProbablyBracedList =
596 ProbablyBracedList ||
597 (NextTok->isBinaryOperator() && !NextIsObjCMethod);
598
599 if (!Style.isCSharp() && NextTok->is(tok::l_square)) {
600 // We can have an array subscript after a braced init
601 // list, but C++11 attributes are expected after blocks.
602 NextTok = Tokens->getNextToken();
603 ProbablyBracedList = NextTok->isNot(tok::l_square);
604 }
605
606 // Cpp macro definition body that is a nonempty braced list or block:
607 if (IsCpp && Line->InMacroBody && PrevTok != FormatTok &&
608 !FormatTok->Previous && NextTok->is(tok::eof) &&
609 // A statement can end with only `;` (simple statement), a block
610 // closing brace (compound statement), or `:` (label statement).
611 // If PrevTok is a block opening brace, Tok ends an empty block.
612 PrevTok->isNoneOf(tok::semi, BK_Block, tok::colon)) {
613 ProbablyBracedList = true;
614 }
615 }
616 const auto BlockKind = ProbablyBracedList ? BK_BracedInit : BK_Block;
617 Tok->setBlockKind(BlockKind);
618 LBrace->setBlockKind(BlockKind);
619 }
620 LBraceStack.pop_back();
621 break;
622 case tok::identifier:
623 if (Tok->isNot(TT_StatementMacro))
624 break;
625 [[fallthrough]];
626 case tok::at:
627 case tok::semi:
628 case tok::kw_if:
629 case tok::kw_while:
630 case tok::kw_for:
631 case tok::kw_switch:
632 case tok::kw_try:
633 case tok::kw___try:
634 if (!LBraceStack.empty() && LBraceStack.back().Tok->is(BK_Unknown))
635 LBraceStack.back().Tok->setBlockKind(BK_Block);
636 break;
637 default:
638 break;
639 }
640
641 PrevTok = Tok;
642 Tok = NextTok;
643 } while (Tok->isNot(tok::eof) && !LBraceStack.empty());
644
645 // Assume other blocks for all unclosed opening braces.
646 for (const auto &Entry : LBraceStack)
647 if (Entry.Tok->is(BK_Unknown))
648 Entry.Tok->setBlockKind(BK_Block);
649
650 FormatTok = Tokens->setPosition(StoredPosition);
651}
652
653// Sets the token type of the directly previous right brace.
654void UnwrappedLineParser::setPreviousRBraceType(TokenType Type) {
655 if (auto Prev = FormatTok->getPreviousNonComment();
656 Prev && Prev->is(tok::r_brace)) {
657 Prev->setFinalizedType(Type);
658 }
659}
660
661template <class T>
662static inline void hash_combine(std::size_t &seed, const T &v) {
663 std::hash<T> hasher;
664 seed ^= hasher(v) + 0x9e3779b9 + (seed << 6) + (seed >> 2);
665}
666
667size_t UnwrappedLineParser::computePPHash() const {
668 size_t h = 0;
669 for (const auto &i : PPStack) {
670 hash_combine(h, size_t(i.Kind));
671 hash_combine(h, i.Line);
672 }
673 return h;
674}
675
676// Checks whether \p ParsedLine might fit on a single line. If \p OpeningBrace
677// is not null, subtracts its length (plus the preceding space) when computing
678// the length of \p ParsedLine. We must clone the tokens of \p ParsedLine before
679// running the token annotator on it so that we can restore them afterward.
680bool UnwrappedLineParser::mightFitOnOneLine(
681 UnwrappedLine &ParsedLine, const FormatToken *OpeningBrace) const {
682 const auto ColumnLimit = Style.ColumnLimit;
683 if (ColumnLimit == 0)
684 return true;
685
686 auto &Tokens = ParsedLine.Tokens;
687 assert(!Tokens.empty());
688
689 const auto *LastToken = Tokens.back().Tok;
690 assert(LastToken);
691
692 SmallVector<UnwrappedLineNode> SavedTokens(Tokens.size());
693
694 int Index = 0;
695 for (const auto &Token : Tokens) {
696 assert(Token.Tok);
697 auto &SavedToken = SavedTokens[Index++];
698 SavedToken.Tok = new FormatToken;
699 SavedToken.Tok->copyFrom(*Token.Tok);
700 SavedToken.Children = std::move(Token.Children);
701 }
702
703 AnnotatedLine Line(ParsedLine);
704 assert(Line.Last == LastToken);
705
706 TokenAnnotator Annotator(Style, Keywords);
707 Annotator.annotate(Line);
708 Annotator.calculateFormattingInformation(Line);
709
710 auto Length = LastToken->TotalLength;
711 if (OpeningBrace) {
712 assert(OpeningBrace != Tokens.front().Tok);
713 if (auto Prev = OpeningBrace->Previous;
714 Prev && Prev->TotalLength + ColumnLimit == OpeningBrace->TotalLength) {
715 Length -= ColumnLimit;
716 }
717 Length -= OpeningBrace->TokenText.size() + 1;
718 }
719
720 if (const auto *FirstToken = Line.First; FirstToken->is(tok::r_brace)) {
721 assert(!OpeningBrace || OpeningBrace->is(TT_ControlStatementLBrace));
722 Length -= FirstToken->TokenText.size() + 1;
723 }
724
725 Index = 0;
726 for (auto &Token : Tokens) {
727 const auto &SavedToken = SavedTokens[Index++];
728 Token.Tok->copyFrom(*SavedToken.Tok);
729 Token.Children = std::move(SavedToken.Children);
730 delete SavedToken.Tok;
731 }
732
733 // If these change PPLevel needs to be used for get correct indentation.
734 assert(!Line.InMacroBody);
735 assert(!Line.InPPDirective);
736 return Line.Level * Style.IndentWidth + Length <= ColumnLimit;
737}
738
739FormatToken *UnwrappedLineParser::parseBlock(bool MustBeDeclaration,
740 unsigned AddLevels, bool MunchSemi,
741 bool KeepBraces,
742 IfStmtKind *IfKind,
743 bool UnindentWhitesmithsBraces) {
744 auto HandleVerilogBlockLabel = [this]() {
745 // ":" name
746 if (Style.isVerilog() && FormatTok->is(tok::colon)) {
747 nextToken();
748 if (Keywords.isVerilogIdentifier(*FormatTok))
749 nextToken();
750 }
751 };
752
753 // Whether this is a Verilog-specific block that has a special header like a
754 // module.
755 const bool VerilogHierarchy =
756 Style.isVerilog() && Keywords.isVerilogHierarchy(*FormatTok);
757 assert((FormatTok->isOneOf(tok::l_brace, TT_MacroBlockBegin) ||
758 (Style.isVerilog() &&
759 (Keywords.isVerilogBegin(*FormatTok) || VerilogHierarchy))) &&
760 "'{' or macro block token expected");
761 FormatToken *Tok = FormatTok;
762 const bool FollowedByComment = Tokens->peekNextToken()->is(tok::comment);
763 auto Index = CurrentLines->size();
764 const bool MacroBlock = FormatTok->is(TT_MacroBlockBegin);
765 FormatTok->setBlockKind(BK_Block);
766
767 const bool IsWhitesmiths =
768 Style.BreakBeforeBraces == FormatStyle::BS_Whitesmiths;
769
770 // For Whitesmiths mode, jump to the next level prior to skipping over the
771 // braces.
772 if (!VerilogHierarchy && AddLevels > 0 && IsWhitesmiths)
773 ++Line->Level;
774
775 size_t PPStartHash = computePPHash();
776
777 const unsigned InitialLevel = Line->Level;
778 if (VerilogHierarchy) {
779 AddLevels += parseVerilogHierarchyHeader();
780 } else {
781 nextToken(/*LevelDifference=*/AddLevels);
782 HandleVerilogBlockLabel();
783 }
784
785 // Bail out if there are too many levels. Otherwise, the stack might overflow.
786 if (Line->Level > 300)
787 return nullptr;
788
789 if (MacroBlock && FormatTok->is(tok::l_paren))
790 parseParens();
791
792 size_t NbPreprocessorDirectives =
793 !parsingPPDirective() ? PreprocessorDirectives.size() : 0;
794 addUnwrappedLine();
795 size_t OpeningLineIndex =
796 CurrentLines->empty()
798 : (CurrentLines->size() - 1 - NbPreprocessorDirectives);
799
800 // Whitesmiths is weird here. The brace needs to be indented for the namespace
801 // block, but the block itself may not be indented depending on the style
802 // settings. This allows the format to back up one level in those cases.
803 if (UnindentWhitesmithsBraces)
804 --Line->Level;
805
806 ScopedDeclarationState DeclarationState(*Line, DeclarationScopeStack,
807 MustBeDeclaration);
808
809 // Whitesmiths logic has already added a level by this point, so avoid
810 // adding it twice.
811 if (AddLevels > 0u)
812 Line->Level += AddLevels - (IsWhitesmiths ? 1 : 0);
813
814 FormatToken *IfLBrace = nullptr;
815 const bool SimpleBlock = parseLevel(Tok, IfKind, &IfLBrace);
816
817 if (eof())
818 return IfLBrace;
819
820 if (MacroBlock ? FormatTok->isNot(TT_MacroBlockEnd)
821 : FormatTok->isNot(tok::r_brace)) {
822 Line->Level = InitialLevel;
823 FormatTok->setBlockKind(BK_Block);
824 return IfLBrace;
825 }
826
827 if (FormatTok->is(tok::r_brace)) {
828 FormatTok->setBlockKind(BK_Block);
829 if (Tok->is(TT_NamespaceLBrace))
830 FormatTok->setFinalizedType(TT_NamespaceRBrace);
831 }
832
833 const bool IsFunctionRBrace =
834 FormatTok->is(tok::r_brace) && Tok->is(TT_FunctionLBrace);
835
836 auto RemoveBraces = [=]() mutable {
837 if (!SimpleBlock)
838 return false;
839 assert(Tok->isOneOf(TT_ControlStatementLBrace, TT_ElseLBrace));
840 assert(FormatTok->is(tok::r_brace));
841 const bool WrappedOpeningBrace = !Tok->Previous;
842 if (WrappedOpeningBrace && FollowedByComment)
843 return false;
844 const bool HasRequiredIfBraces = IfLBrace && !IfLBrace->Optional;
845 if (KeepBraces && !HasRequiredIfBraces)
846 return false;
847 if (Tok->isNot(TT_ElseLBrace) || !HasRequiredIfBraces) {
848 const FormatToken *Previous = Tokens->getPreviousToken();
849 assert(Previous);
850 if (Previous->is(tok::r_brace) && !Previous->Optional)
851 return false;
852 }
853 assert(!CurrentLines->empty());
854 auto &LastLine = CurrentLines->back();
855 if (LastLine.Level == InitialLevel + 1 && !mightFitOnOneLine(LastLine))
856 return false;
857 if (Tok->is(TT_ElseLBrace))
858 return true;
859 if (WrappedOpeningBrace) {
860 assert(Index > 0);
861 --Index; // The line above the wrapped l_brace.
862 Tok = nullptr;
863 }
864 return mightFitOnOneLine((*CurrentLines)[Index], Tok);
865 };
866 if (RemoveBraces()) {
867 Tok->MatchingParen = FormatTok;
868 FormatTok->MatchingParen = Tok;
869 }
870
871 size_t PPEndHash = computePPHash();
872
873 // Munch the closing brace.
874 nextToken(/*LevelDifference=*/-AddLevels);
875
876 // When this is a function block and there is an unnecessary semicolon
877 // afterwards then mark it as optional (so the RemoveSemi pass can get rid of
878 // it later).
879 if (Style.RemoveSemicolon && IsFunctionRBrace) {
880 while (FormatTok->is(tok::semi)) {
881 FormatTok->Optional = true;
882 nextToken();
883 }
884 }
885
886 HandleVerilogBlockLabel();
887
888 if (MacroBlock && FormatTok->is(tok::l_paren))
889 parseParens();
890
891 Line->Level = InitialLevel;
892
893 if (FormatTok->is(tok::kw_noexcept)) {
894 // A noexcept in a requires expression.
895 nextToken();
896 }
897
898 if (FormatTok->is(tok::arrow)) {
899 // Following the } or noexcept we can find a trailing return type arrow
900 // as part of an implicit conversion constraint.
901 nextToken();
902 parseStructuralElement();
903 }
904
905 if (MunchSemi && FormatTok->is(tok::semi))
906 nextToken();
907
908 if (PPStartHash == PPEndHash) {
909 Line->MatchingOpeningBlockLineIndex = OpeningLineIndex;
910 if (OpeningLineIndex != UnwrappedLine::kInvalidIndex) {
911 // Update the opening line to add the forward reference as well
912 (*CurrentLines)[OpeningLineIndex].MatchingClosingBlockLineIndex =
913 CurrentLines->size() - 1;
914 }
915 }
916
917 return IfLBrace;
918}
919
920static bool isGoogScope(const UnwrappedLine &Line) {
921 // FIXME: Closure-library specific stuff should not be hard-coded but be
922 // configurable.
923 if (Line.Tokens.size() < 4)
924 return false;
925 auto I = Line.Tokens.begin();
926 if (I->Tok->TokenText != "goog")
927 return false;
928 ++I;
929 if (I->Tok->isNot(tok::period))
930 return false;
931 ++I;
932 if (I->Tok->TokenText != "scope")
933 return false;
934 ++I;
935 return I->Tok->is(tok::l_paren);
936}
937
938static bool isIIFE(const UnwrappedLine &Line,
939 const AdditionalKeywords &Keywords) {
940 // Look for the start of an immediately invoked anonymous function.
941 // https://en.wikipedia.org/wiki/Immediately-invoked_function_expression
942 // This is commonly done in JavaScript to create a new, anonymous scope.
943 // Example: (function() { ... })()
944 if (Line.Tokens.size() < 3)
945 return false;
946 auto I = Line.Tokens.begin();
947 if (I->Tok->isNot(tok::l_paren))
948 return false;
949 ++I;
950 if (I->Tok->isNot(Keywords.kw_function))
951 return false;
952 ++I;
953 return I->Tok->is(tok::l_paren);
954}
955
956static bool ShouldBreakBeforeBrace(const FormatStyle &Style,
957 const FormatToken &InitialToken,
958 bool IsEmptyBlock,
959 bool IsJavaRecord = false) {
960 if (IsJavaRecord)
961 return Style.BraceWrapping.AfterClass;
962
963 tok::TokenKind Kind = InitialToken.Tok.getKind();
964 if (InitialToken.is(TT_NamespaceMacro))
965 Kind = tok::kw_namespace;
966
967 const bool WrapRecordAllowed =
968 !IsEmptyBlock ||
969 Style.AllowShortRecordOnASingleLine < FormatStyle::SRS_Empty ||
970 Style.BraceWrapping.SplitEmptyRecord;
971
972 switch (Kind) {
973 case tok::kw_namespace:
974 return Style.BraceWrapping.AfterNamespace;
975 case tok::kw_class:
976 return Style.BraceWrapping.AfterClass && WrapRecordAllowed;
977 case tok::kw_union:
978 return Style.BraceWrapping.AfterUnion && WrapRecordAllowed;
979 case tok::kw_struct:
980 return Style.BraceWrapping.AfterStruct && WrapRecordAllowed;
981 case tok::kw_enum:
982 return Style.BraceWrapping.AfterEnum;
983 default:
984 return false;
985 }
986}
987
988void UnwrappedLineParser::parseChildBlock() {
989 assert(FormatTok->is(tok::l_brace));
990 FormatTok->setBlockKind(BK_Block);
991 const FormatToken *OpeningBrace = FormatTok;
992 nextToken();
993 {
994 bool SkipIndent = (Style.isJavaScript() &&
995 (isGoogScope(*Line) || isIIFE(*Line, Keywords)));
996 ScopedLineState LineState(*this);
997 ScopedDeclarationState DeclarationState(*Line, DeclarationScopeStack,
998 /*MustBeDeclaration=*/false);
999 Line->Level += SkipIndent ? 0 : 1;
1000 parseLevel(OpeningBrace);
1001 flushComments(isOnNewLine(*FormatTok));
1002 Line->Level -= SkipIndent ? 0 : 1;
1003 }
1004 nextToken();
1005}
1006
1007void UnwrappedLineParser::parsePPDirective() {
1008 assert(FormatTok->is(tok::hash) && "'#' expected");
1009 ScopedMacroState MacroState(*Line, Tokens, FormatTok);
1010
1011 nextToken();
1012
1013 if (!FormatTok->Tok.getIdentifierInfo()) {
1014 parsePPUnknown();
1015 return;
1016 }
1017
1018 switch (FormatTok->Tok.getIdentifierInfo()->getPPKeywordID()) {
1019 case tok::pp_define:
1020 parsePPDefine();
1021 return;
1022 case tok::pp_if:
1023 parsePPIf(/*IfDef=*/false);
1024 break;
1025 case tok::pp_ifdef:
1026 case tok::pp_ifndef:
1027 parsePPIf(/*IfDef=*/true);
1028 break;
1029 case tok::pp_else:
1030 case tok::pp_elifdef:
1031 case tok::pp_elifndef:
1032 case tok::pp_elif:
1033 parsePPElse();
1034 break;
1035 case tok::pp_endif:
1036 parsePPEndIf();
1037 break;
1038 case tok::pp_pragma:
1039 parsePPPragma();
1040 break;
1041 case tok::pp_error:
1042 case tok::pp_warning:
1043 nextToken();
1044 if (!eof() && Style.isCpp())
1045 FormatTok->setFinalizedType(TT_AfterPPDirective);
1046 [[fallthrough]];
1047 default:
1048 parsePPUnknown();
1049 break;
1050 }
1051}
1052
1053void UnwrappedLineParser::conditionalCompilationCondition(bool Unreachable) {
1054 size_t Line = CurrentLines->size();
1055 if (CurrentLines == &PreprocessorDirectives)
1056 Line += Lines.size();
1057
1058 if (Unreachable ||
1059 (!PPStack.empty() && PPStack.back().Kind == PP_Unreachable)) {
1060 PPStack.push_back({PP_Unreachable, Line});
1061 } else {
1062 PPStack.push_back({PP_Conditional, Line});
1063 }
1064}
1065
1066void UnwrappedLineParser::conditionalCompilationStart(bool Unreachable) {
1067 ++PPBranchLevel;
1068 assert(PPBranchLevel >= 0 && PPBranchLevel <= (int)PPLevelBranchIndex.size());
1069 if (PPBranchLevel == (int)PPLevelBranchIndex.size()) {
1070 PPLevelBranchIndex.push_back(0);
1071 PPLevelBranchCount.push_back(0);
1072 }
1073 PPChainBranchIndex.push(Unreachable ? -1 : 0);
1074 bool Skip = PPLevelBranchIndex[PPBranchLevel] > 0;
1075 conditionalCompilationCondition(Unreachable || Skip);
1076}
1077
1078void UnwrappedLineParser::conditionalCompilationAlternative() {
1079 if (!PPStack.empty())
1080 PPStack.pop_back();
1081 assert(PPBranchLevel < (int)PPLevelBranchIndex.size());
1082 if (!PPChainBranchIndex.empty())
1083 ++PPChainBranchIndex.top();
1084 conditionalCompilationCondition(
1085 PPBranchLevel >= 0 && !PPChainBranchIndex.empty() &&
1086 PPLevelBranchIndex[PPBranchLevel] != PPChainBranchIndex.top());
1087}
1088
1089void UnwrappedLineParser::conditionalCompilationEnd() {
1090 assert(PPBranchLevel < (int)PPLevelBranchIndex.size());
1091 if (PPBranchLevel >= 0 && !PPChainBranchIndex.empty()) {
1092 if (PPChainBranchIndex.top() + 1 > PPLevelBranchCount[PPBranchLevel])
1093 PPLevelBranchCount[PPBranchLevel] = PPChainBranchIndex.top() + 1;
1094 }
1095 // Guard against #endif's without #if.
1096 if (PPBranchLevel > -1)
1097 --PPBranchLevel;
1098 if (!PPChainBranchIndex.empty())
1099 PPChainBranchIndex.pop();
1100 if (!PPStack.empty())
1101 PPStack.pop_back();
1102}
1103
1104void UnwrappedLineParser::parsePPIf(bool IfDef) {
1105 bool IfNDef = FormatTok->is(tok::pp_ifndef);
1106 nextToken();
1107 bool Unreachable = false;
1108 if (!IfDef && (FormatTok->is(tok::kw_false) || FormatTok->TokenText == "0"))
1109 Unreachable = true;
1110 if (IfDef && !IfNDef && FormatTok->TokenText == "SWIG")
1111 Unreachable = true;
1112 conditionalCompilationStart(Unreachable);
1113 FormatToken *IfCondition = FormatTok;
1114 // If there's a #ifndef on the first line, and the only lines before it are
1115 // comments, it could be an include guard.
1116 bool MaybeIncludeGuard = IfNDef;
1117 if (IncludeGuard == IG_Inited && MaybeIncludeGuard) {
1118 for (auto &Line : Lines) {
1119 if (Line.Tokens.front().Tok->isNot(tok::comment)) {
1120 MaybeIncludeGuard = false;
1121 IncludeGuard = IG_Rejected;
1122 break;
1123 }
1124 }
1125 }
1126 --PPBranchLevel;
1127 parsePPUnknown();
1128 ++PPBranchLevel;
1129 if (IncludeGuard == IG_Inited && MaybeIncludeGuard) {
1130 IncludeGuard = IG_IfNdefed;
1131 IncludeGuardToken = IfCondition;
1132 }
1133}
1134
1135void UnwrappedLineParser::parsePPElse() {
1136 // If a potential include guard has an #else, it's not an include guard.
1137 if (IncludeGuard == IG_Defined && PPBranchLevel == 0)
1138 IncludeGuard = IG_Rejected;
1139 // Don't crash when there is an #else without an #if.
1140 assert(PPBranchLevel >= -1);
1141 if (PPBranchLevel == -1)
1142 conditionalCompilationStart(/*Unreachable=*/true);
1143 conditionalCompilationAlternative();
1144 --PPBranchLevel;
1145 parsePPUnknown();
1146 ++PPBranchLevel;
1147}
1148
1149void UnwrappedLineParser::parsePPEndIf() {
1150 conditionalCompilationEnd();
1151 parsePPUnknown();
1152}
1153
1154void UnwrappedLineParser::parsePPDefine() {
1155 nextToken();
1156
1157 if (!FormatTok->Tok.getIdentifierInfo()) {
1158 IncludeGuard = IG_Rejected;
1159 IncludeGuardToken = nullptr;
1160 parsePPUnknown();
1161 return;
1162 }
1163
1164 bool MaybeIncludeGuard = false;
1165 if (IncludeGuard == IG_IfNdefed &&
1166 IncludeGuardToken->TokenText == FormatTok->TokenText) {
1167 IncludeGuard = IG_Defined;
1168 IncludeGuardToken = nullptr;
1169 for (auto &Line : Lines) {
1170 if (Line.Tokens.front().Tok->isNoneOf(tok::comment, tok::hash)) {
1171 IncludeGuard = IG_Rejected;
1172 break;
1173 }
1174 }
1175 MaybeIncludeGuard = IncludeGuard == IG_Defined;
1176 }
1177
1178 // In the context of a define, even keywords should be treated as normal
1179 // identifiers. Setting the kind to identifier is not enough, because we need
1180 // to treat additional keywords like __except as well, which are already
1181 // identifiers. Setting the identifier info to null interferes with include
1182 // guard processing above, and changes preprocessing nesting.
1183 FormatTok->Tok.setKind(tok::identifier);
1184 FormatTok->Tok.setIdentifierInfo(Keywords.kw_internal_ident_after_define);
1185 nextToken();
1186
1187 // IncludeGuard can't have a non-empty macro definition.
1188 if (MaybeIncludeGuard && !eof())
1189 IncludeGuard = IG_Rejected;
1190
1191 if (FormatTok->is(tok::l_paren) && !FormatTok->hasWhitespaceBefore())
1192 parseParens();
1193 if (Style.IndentPPDirectives != FormatStyle::PPDIS_None)
1194 Line->Level += PPBranchLevel + 1;
1195 addUnwrappedLine();
1196 ++Line->Level;
1197
1198 Line->PPLevel = PPBranchLevel + (IncludeGuard == IG_Defined ? 0 : 1);
1199 assert((int)Line->PPLevel >= 0);
1200
1201 if (eof())
1202 return;
1203
1204 Line->InMacroBody = true;
1205
1206 if (!Style.SkipMacroDefinitionBody) {
1207 // Errors during a preprocessor directive can only affect the layout of the
1208 // preprocessor directive, and thus we ignore them. An alternative approach
1209 // would be to use the same approach we use on the file level (no
1210 // re-indentation if there was a structural error) within the macro
1211 // definition.
1212 parseFile();
1213 return;
1214 }
1215
1216 for (auto *Comment : CommentsBeforeNextToken)
1217 Comment->Finalized = true;
1218
1219 do {
1220 FormatTok->Finalized = true;
1221 FormatTok = Tokens->getNextToken();
1222 } while (!eof());
1223
1224 addUnwrappedLine();
1225}
1226
1227void UnwrappedLineParser::parsePPPragma() {
1228 Line->InPragmaDirective = true;
1229 parsePPUnknown();
1230}
1231
1232void UnwrappedLineParser::parsePPUnknown() {
1233 while (!eof())
1234 nextToken();
1235 if (Style.IndentPPDirectives != FormatStyle::PPDIS_None)
1236 Line->Level += PPBranchLevel + 1;
1237 addUnwrappedLine();
1238}
1239
1240// Here we exclude certain tokens that are not usually the first token in an
1241// unwrapped line. This is used in attempt to distinguish macro calls without
1242// trailing semicolons from other constructs split to several lines.
1244 // Semicolon can be a null-statement, l_square can be a start of a macro or
1245 // a C++11 attribute, but this doesn't seem to be common.
1246 return Tok.isNoneOf(tok::semi, tok::l_brace,
1247 // Tokens that can only be used as binary operators and a
1248 // part of overloaded operator names.
1249 tok::period, tok::periodstar, tok::arrow, tok::arrowstar,
1250 tok::less, tok::greater, tok::slash, tok::percent,
1251 tok::lessless, tok::greatergreater, tok::equal,
1252 tok::plusequal, tok::minusequal, tok::starequal,
1253 tok::slashequal, tok::percentequal, tok::ampequal,
1254 tok::pipeequal, tok::caretequal, tok::greatergreaterequal,
1255 tok::lesslessequal,
1256 // Colon is used in labels, base class lists, initializer
1257 // lists, range-based for loops, ternary operator, but
1258 // should never be the first token in an unwrapped line.
1259 tok::colon,
1260 // 'noexcept' is a trailing annotation.
1261 tok::kw_noexcept);
1262}
1263
1264static bool mustBeJSIdent(const AdditionalKeywords &Keywords,
1265 const FormatToken *FormatTok) {
1266 // FIXME: This returns true for C/C++ keywords like 'struct'.
1267 return FormatTok->is(tok::identifier) &&
1268 (!FormatTok->Tok.getIdentifierInfo() ||
1269 FormatTok->isNoneOf(
1270 Keywords.kw_in, Keywords.kw_of, Keywords.kw_as, Keywords.kw_async,
1271 Keywords.kw_await, Keywords.kw_yield, Keywords.kw_finally,
1272 Keywords.kw_function, Keywords.kw_import, Keywords.kw_is,
1273 Keywords.kw_let, Keywords.kw_var, tok::kw_const,
1274 Keywords.kw_abstract, Keywords.kw_extends, Keywords.kw_implements,
1275 Keywords.kw_instanceof, Keywords.kw_interface,
1276 Keywords.kw_override, Keywords.kw_throws, Keywords.kw_from));
1277}
1278
1279static bool mustBeJSIdentOrValue(const AdditionalKeywords &Keywords,
1280 const FormatToken *FormatTok) {
1281 return FormatTok->Tok.isLiteral() ||
1282 FormatTok->isOneOf(tok::kw_true, tok::kw_false) ||
1283 mustBeJSIdent(Keywords, FormatTok);
1284}
1285
1286// isJSDeclOrStmt returns true if |FormatTok| starts a declaration or statement
1287// when encountered after a value (see mustBeJSIdentOrValue).
1288static bool isJSDeclOrStmt(const AdditionalKeywords &Keywords,
1289 const FormatToken *FormatTok) {
1290 return FormatTok->isOneOf(
1291 tok::kw_return, Keywords.kw_yield,
1292 // conditionals
1293 tok::kw_if, tok::kw_else,
1294 // loops
1295 tok::kw_for, tok::kw_while, tok::kw_do, tok::kw_continue, tok::kw_break,
1296 // switch/case
1297 tok::kw_switch, tok::kw_case,
1298 // exceptions
1299 tok::kw_throw, tok::kw_try, tok::kw_catch, Keywords.kw_finally,
1300 // declaration
1301 tok::kw_const, tok::kw_class, Keywords.kw_var, Keywords.kw_let,
1302 Keywords.kw_async, Keywords.kw_function,
1303 // import/export
1304 Keywords.kw_import, tok::kw_export);
1305}
1306
1307// Checks whether a token is a type in K&R C (aka C78).
1308static bool isC78Type(const FormatToken &Tok) {
1309 return Tok.isOneOf(tok::kw_char, tok::kw_short, tok::kw_int, tok::kw_long,
1310 tok::kw_unsigned, tok::kw_float, tok::kw_double,
1311 tok::identifier);
1312}
1313
1314// This function checks whether a token starts the first parameter declaration
1315// in a K&R C (aka C78) function definition, e.g.:
1316// int f(a, b)
1317// short a, b;
1318// {
1319// return a + b;
1320// }
1322 const FormatToken *FuncName) {
1323 assert(Tok);
1324 assert(Next);
1325 assert(FuncName);
1326
1327 if (FuncName->isNot(tok::identifier))
1328 return false;
1329
1330 const FormatToken *Prev = FuncName->Previous;
1331 if (!Prev || (Prev->isNot(tok::star) && !isC78Type(*Prev)))
1332 return false;
1333
1334 if (!isC78Type(*Tok) &&
1335 Tok->isNoneOf(tok::kw_register, tok::kw_struct, tok::kw_union)) {
1336 return false;
1337 }
1338
1339 if (Next->isNot(tok::star) && !Next->Tok.getIdentifierInfo())
1340 return false;
1341
1342 Tok = Tok->Previous;
1343 if (!Tok || Tok->isNot(tok::r_paren))
1344 return false;
1345
1346 Tok = Tok->Previous;
1347 if (!Tok || Tok->isNot(tok::identifier))
1348 return false;
1349
1350 return Tok->Previous && Tok->Previous->isOneOf(tok::l_paren, tok::comma);
1351}
1352
1353bool UnwrappedLineParser::parseModuleDecl() {
1354 assert(IsCpp);
1355 assert(FormatTok->is(Keywords.kw_module));
1356
1357 if (Style.Language == FormatStyle::LK_C ||
1358 Style.Standard < FormatStyle::LS_Cpp20) {
1359 return false;
1360 }
1361
1362 nextToken();
1363 if (FormatTok->isNot(tok::identifier))
1364 return false;
1365
1366 for (nextToken(); FormatTok->isNoneOf(tok::semi, tok::eof); nextToken())
1367 if (FormatTok->is(tok::colon))
1368 FormatTok->setFinalizedType(TT_ModulePartitionColon);
1369
1370 nextToken();
1371 Line->IsModuleOrImportDecl = true;
1372 addUnwrappedLine();
1373 return true;
1374}
1375
1376bool UnwrappedLineParser::parseImportDecl() {
1377 assert(IsCpp);
1378 assert(FormatTok->is(Keywords.kw_import) && "'import' expected");
1379
1380 if (Style.Language == FormatStyle::LK_C ||
1381 Style.Standard < FormatStyle::LS_Cpp20) {
1382 return false;
1383 }
1384
1385 nextToken();
1386 if (FormatTok->is(tok::colon)) {
1387 FormatTok->setFinalizedType(TT_ModulePartitionColon);
1388 nextToken();
1389 }
1390 if (FormatTok->isNoneOf(tok::identifier, tok::less, tok::string_literal))
1391 return false;
1392
1393 for (; FormatTok->isNoneOf(tok::semi, tok::eof); nextToken()) {
1394 // Handle import <foo/bar.h> as we would an include statement.
1395 if (FormatTok->is(tok::less)) {
1396 for (nextToken(); FormatTok->isNoneOf(tok::greater, tok::semi, tok::eof);
1397 nextToken()) {
1398 // Mark tokens as implicit string literals, so that import <A/Foo> will
1399 // neither be broken nor have a space added.
1400 FormatTok->setFinalizedType(TT_ImplicitStringLiteral);
1401 }
1402 }
1403 }
1404
1405 nextToken();
1406 Line->IsModuleOrImportDecl = true;
1407 addUnwrappedLine();
1408 return true;
1409}
1410
1411// readTokenWithJavaScriptASI reads the next token and terminates the current
1412// line if JavaScript Automatic Semicolon Insertion must
1413// happen between the current token and the next token.
1414//
1415// This method is conservative - it cannot cover all edge cases of JavaScript,
1416// but only aims to correctly handle certain well known cases. It *must not*
1417// return true in speculative cases.
1418void UnwrappedLineParser::readTokenWithJavaScriptASI() {
1419 FormatToken *Previous = FormatTok;
1420 readToken();
1421 FormatToken *Next = FormatTok;
1422
1423 bool IsOnSameLine =
1424 CommentsBeforeNextToken.empty()
1425 ? Next->NewlinesBefore == 0
1426 : CommentsBeforeNextToken.front()->NewlinesBefore == 0;
1427 if (IsOnSameLine)
1428 return;
1429
1430 bool PreviousMustBeValue = mustBeJSIdentOrValue(Keywords, Previous);
1431 bool PreviousStartsTemplateExpr =
1432 Previous->is(TT_TemplateString) && Previous->TokenText.ends_with("${");
1433 if (PreviousMustBeValue || Previous->is(tok::r_paren)) {
1434 // If the line contains an '@' sign, the previous token might be an
1435 // annotation, which can precede another identifier/value.
1436 bool HasAt = llvm::any_of(Line->Tokens, [](UnwrappedLineNode &LineNode) {
1437 return LineNode.Tok->is(tok::at);
1438 });
1439 if (HasAt)
1440 return;
1441 }
1442 if (Next->is(tok::exclaim) && PreviousMustBeValue)
1443 return addUnwrappedLine();
1444 bool NextMustBeValue = mustBeJSIdentOrValue(Keywords, Next);
1445 bool NextEndsTemplateExpr =
1446 Next->is(TT_TemplateString) && Next->TokenText.starts_with("}");
1447 if (NextMustBeValue && !NextEndsTemplateExpr && !PreviousStartsTemplateExpr &&
1448 (PreviousMustBeValue ||
1449 Previous->isOneOf(tok::r_square, tok::r_paren, tok::plusplus,
1450 tok::minusminus))) {
1451 return addUnwrappedLine();
1452 }
1453 if ((PreviousMustBeValue || Previous->is(tok::r_paren)) &&
1454 isJSDeclOrStmt(Keywords, Next)) {
1455 return addUnwrappedLine();
1456 }
1457}
1458
1459void UnwrappedLineParser::parseStructuralElement(
1460 const FormatToken *OpeningBrace, IfStmtKind *IfKind,
1461 FormatToken **IfLeftBrace, bool *HasDoWhile, bool *HasLabel) {
1462 if (Style.isTableGen() && FormatTok->is(tok::pp_include)) {
1463 nextToken();
1464 if (FormatTok->is(tok::string_literal))
1465 nextToken();
1466 addUnwrappedLine();
1467 return;
1468 }
1469
1470 if (IsCpp) {
1471 while (FormatTok->is(tok::l_square) && handleCppAttributes()) {
1472 }
1473 } else if (Style.isVerilog()) {
1474 // Skip attributes.
1475 while (FormatTok->is(tok::l_paren) &&
1476 Tokens->peekNextToken()->is(tok::star)) {
1477 parseParens();
1478 }
1479 skipVerilogQualifiers();
1480 // Skip things that can exist before keywords like 'if' and 'case'.
1481 if (FormatTok->isOneOf(Keywords.kw_priority, Keywords.kw_unique,
1482 Keywords.kw_unique0)) {
1483 nextToken();
1484 }
1485
1486 if (Keywords.isVerilogStructuredProcedure(*FormatTok)) {
1487 parseForOrWhileLoop(/*HasParens=*/false);
1488 return;
1489 }
1490 if (FormatTok->isOneOf(Keywords.kw_foreach, Keywords.kw_repeat)) {
1491 parseForOrWhileLoop();
1492 return;
1493 }
1494 if (FormatTok->isOneOf(tok::kw_restrict, Keywords.kw_assert,
1495 Keywords.kw_assume, Keywords.kw_cover)) {
1496 parseIfThenElse(IfKind, /*KeepBraces=*/false, /*IsVerilogAssert=*/true);
1497 return;
1498 }
1499 }
1500
1501 // Tokens that only make sense at the beginning of a line.
1502 if (FormatTok->isAccessSpecifierKeyword()) {
1503 if (Style.isJava() || Style.isJavaScript() || Style.isCSharp())
1504 nextToken();
1505 else
1506 parseAccessSpecifier();
1507 return;
1508 }
1509 switch (FormatTok->Tok.getKind()) {
1510 case tok::kw_asm:
1511 nextToken();
1512 if (FormatTok->is(tok::l_brace)) {
1513 FormatTok->setFinalizedType(TT_InlineASMBrace);
1514 nextToken();
1515 while (FormatTok && !eof()) {
1516 if (FormatTok->is(tok::r_brace)) {
1517 FormatTok->setFinalizedType(TT_InlineASMBrace);
1518 nextToken();
1519 addUnwrappedLine();
1520 break;
1521 }
1522 FormatTok->Finalized = true;
1523 nextToken();
1524 }
1525 }
1526 break;
1527 case tok::kw_namespace:
1528 parseNamespace();
1529 return;
1530 case tok::kw_if: {
1531 if (Style.isJavaScript() && Line->MustBeDeclaration) {
1532 // field/method declaration.
1533 break;
1534 }
1535 FormatToken *Tok = parseIfThenElse(IfKind);
1536 if (IfLeftBrace)
1537 *IfLeftBrace = Tok;
1538 return;
1539 }
1540 case tok::kw_for:
1541 case tok::kw_while:
1542 if (Style.isJavaScript() && Line->MustBeDeclaration) {
1543 // field/method declaration.
1544 break;
1545 }
1546 parseForOrWhileLoop();
1547 return;
1548 case tok::kw_do:
1549 if (Style.isJavaScript() && Line->MustBeDeclaration) {
1550 // field/method declaration.
1551 break;
1552 }
1553 parseDoWhile();
1554 if (HasDoWhile)
1555 *HasDoWhile = true;
1556 return;
1557 case tok::kw_switch:
1558 if (Style.isJavaScript() && Line->MustBeDeclaration) {
1559 // 'switch: string' field declaration.
1560 break;
1561 }
1562 parseSwitch(/*IsExpr=*/false);
1563 return;
1564 case tok::kw_default: {
1565 // In Verilog default along with other labels are handled in the next loop.
1566 if (Style.isVerilog())
1567 break;
1568 if (Style.isJavaScript() && Line->MustBeDeclaration) {
1569 // 'default: string' field declaration.
1570 break;
1571 }
1572 auto *Default = FormatTok;
1573 nextToken();
1574 if (FormatTok->is(tok::colon)) {
1575 FormatTok->setFinalizedType(TT_CaseLabelColon);
1576 parseLabel();
1577 return;
1578 }
1579 if (FormatTok->is(tok::arrow)) {
1580 FormatTok->setFinalizedType(TT_CaseLabelArrow);
1581 Default->setFinalizedType(TT_SwitchExpressionLabel);
1582 parseLabel();
1583 return;
1584 }
1585 // e.g. "default void f() {}" in a Java interface.
1586 break;
1587 }
1588 case tok::kw_case:
1589 // Proto: there are no switch/case statements.
1590 if (Style.Language == FormatStyle::LK_Proto) {
1591 nextToken();
1592 return;
1593 }
1594 if (Style.isVerilog()) {
1595 parseBlock();
1596 addUnwrappedLine();
1597 return;
1598 }
1599 if (Style.isJavaScript() && Line->MustBeDeclaration) {
1600 // 'case: string' field declaration.
1601 nextToken();
1602 break;
1603 }
1604 parseCaseLabel();
1605 return;
1606 case tok::kw_goto:
1607 nextToken();
1608 if (FormatTok->is(tok::kw_case))
1609 nextToken();
1610 break;
1611 case tok::kw_try:
1612 case tok::kw___try:
1613 if (Style.isJavaScript() && Line->MustBeDeclaration) {
1614 // field/method declaration.
1615 break;
1616 }
1617 parseTryCatch();
1618 return;
1619 case tok::kw_extern:
1620 if (Style.isVerilog()) {
1621 // In Verilog an extern module declaration looks like a start of module.
1622 // But there is no body and endmodule. So we handle it separately.
1623 parseVerilogExtern();
1624 return;
1625 }
1626 nextToken();
1627 if (FormatTok->is(tok::string_literal)) {
1628 nextToken();
1629 if (FormatTok->is(tok::l_brace)) {
1630 if (Style.BraceWrapping.AfterExternBlock)
1631 addUnwrappedLine();
1632 // Either we indent or for backwards compatibility we follow the
1633 // AfterExternBlock style.
1634 unsigned AddLevels =
1635 (Style.IndentExternBlock == FormatStyle::IEBS_Indent) ||
1636 (Style.BraceWrapping.AfterExternBlock &&
1637 Style.IndentExternBlock ==
1638 FormatStyle::IEBS_AfterExternBlock)
1639 ? 1u
1640 : 0u;
1641 parseBlock(/*MustBeDeclaration=*/true, AddLevels);
1642 addUnwrappedLine();
1643 return;
1644 }
1645 }
1646 break;
1647 case tok::kw_export:
1648 if (IsCpp) {
1649 nextToken();
1650 if (FormatTok->is(tok::kw_namespace)) {
1651 parseNamespace();
1652 return;
1653 }
1654 if (FormatTok->is(tok::l_brace)) {
1655 parseCppExportBlock();
1656 return;
1657 }
1658 if (FormatTok->is(Keywords.kw_module) && parseModuleDecl())
1659 return;
1660 if (FormatTok->is(Keywords.kw_import) && parseImportDecl())
1661 return;
1662 break;
1663 }
1664 if (Style.isJavaScript()) {
1665 parseJavaScriptEs6ImportExport();
1666 return;
1667 }
1668 if (Style.isVerilog()) {
1669 parseVerilogExtern();
1670 return;
1671 }
1672 break;
1673 case tok::kw_inline:
1674 nextToken();
1675 if (FormatTok->is(tok::kw_namespace)) {
1676 parseNamespace();
1677 return;
1678 }
1679 break;
1680 case tok::identifier:
1681 if (FormatTok->is(TT_ForEachMacro)) {
1682 parseForOrWhileLoop();
1683 return;
1684 }
1685 if (FormatTok->is(TT_MacroBlockBegin)) {
1686 parseBlock(/*MustBeDeclaration=*/false, /*AddLevels=*/1u,
1687 /*MunchSemi=*/false);
1688 return;
1689 }
1690 if (FormatTok->is(Keywords.kw_import)) {
1691 if (IsCpp && parseImportDecl())
1692 return;
1693 if (Style.isJavaScript()) {
1694 parseJavaScriptEs6ImportExport();
1695 return;
1696 }
1697 if (Style.Language == FormatStyle::LK_Proto) {
1698 nextToken();
1699 if (FormatTok->is(tok::kw_public))
1700 nextToken();
1701 if (FormatTok->isNot(tok::string_literal))
1702 return;
1703 nextToken();
1704 if (FormatTok->is(tok::semi))
1705 nextToken();
1706 addUnwrappedLine();
1707 return;
1708 }
1709 if (Style.isVerilog()) {
1710 parseVerilogExtern();
1711 return;
1712 }
1713 }
1714 if (IsCpp) {
1715 if (FormatTok->is(Keywords.kw_module) && parseModuleDecl())
1716 return;
1717 if (FormatTok->isOneOf(Keywords.kw_signals, Keywords.kw_qsignals,
1718 Keywords.kw_slots, Keywords.kw_qslots)) {
1719 nextToken();
1720 if (FormatTok->is(tok::colon)) {
1721 nextToken();
1722 addUnwrappedLine();
1723 return;
1724 }
1725 }
1726 if (FormatTok->is(TT_StatementMacro)) {
1727 parseStatementMacro();
1728 return;
1729 }
1730 if (FormatTok->is(TT_NamespaceMacro)) {
1731 parseNamespace();
1732 return;
1733 }
1734 }
1735 // In Verilog labels can be any expression, so we don't do them here.
1736 // JS doesn't have macros, and within classes colons indicate fields, not
1737 // labels.
1738 // TableGen doesn't have labels.
1739 if (!Style.isJavaScript() && !Style.isVerilog() && !Style.isTableGen() &&
1740 Tokens->peekNextToken()->is(tok::colon) && !Line->MustBeDeclaration) {
1741 nextToken();
1742 if (!Line->InMacroBody || CurrentLines->size() > 1)
1743 Line->Tokens.begin()->Tok->MustBreakBefore = true;
1744 FormatTok->setFinalizedType(TT_GotoLabelColon);
1745 parseLabel(Style.IndentGotoLabels);
1746 if (HasLabel)
1747 *HasLabel = true;
1748 return;
1749 }
1750 if (Style.isJava() && FormatTok->is(Keywords.kw_record)) {
1751 parseRecord(/*ParseAsExpr=*/false, /*IsJavaRecord=*/true);
1752 addUnwrappedLine();
1753 return;
1754 }
1755 // In all other cases, parse the declaration.
1756 break;
1757 default:
1758 break;
1759 }
1760
1761 bool SeenEqual = false;
1762 for (const bool InRequiresExpression =
1763 OpeningBrace && OpeningBrace->isOneOf(TT_RequiresExpressionLBrace,
1764 TT_CompoundRequirementLBrace);
1765 !eof();) {
1766 const FormatToken *Previous = FormatTok->Previous;
1767 switch (FormatTok->Tok.getKind()) {
1768 case tok::at:
1769 nextToken();
1770 if (FormatTok->is(tok::l_brace)) {
1771 nextToken();
1772 parseBracedList();
1773 break;
1774 }
1775 if (Style.isJava() && FormatTok->is(Keywords.kw_interface)) {
1776 nextToken();
1777 break;
1778 }
1779 switch (bool IsAutoRelease = false; FormatTok->Tok.getObjCKeywordID()) {
1780 case tok::objc_public:
1781 case tok::objc_protected:
1782 case tok::objc_package:
1783 case tok::objc_private:
1784 return parseAccessSpecifier();
1785 case tok::objc_interface:
1786 case tok::objc_implementation:
1787 return parseObjCInterfaceOrImplementation();
1788 case tok::objc_protocol:
1789 if (parseObjCProtocol())
1790 return;
1791 break;
1792 case tok::objc_end:
1793 return; // Handled by the caller.
1794 case tok::objc_optional:
1795 case tok::objc_required:
1796 nextToken();
1797 addUnwrappedLine();
1798 return;
1799 case tok::objc_autoreleasepool:
1800 IsAutoRelease = true;
1801 [[fallthrough]];
1802 case tok::objc_synchronized:
1803 nextToken();
1804 if (!IsAutoRelease && FormatTok->is(tok::l_paren)) {
1805 // Skip synchronization object
1806 parseParens();
1807 }
1808 if (FormatTok->is(tok::l_brace)) {
1809 if (Style.BraceWrapping.AfterControlStatement ==
1810 FormatStyle::BWACS_Always) {
1811 addUnwrappedLine();
1812 }
1813 parseBlock();
1814 }
1815 addUnwrappedLine();
1816 return;
1817 case tok::objc_try:
1818 // This branch isn't strictly necessary (the kw_try case below would
1819 // do this too after the tok::at is parsed above). But be explicit.
1820 parseTryCatch();
1821 return;
1822 default:
1823 break;
1824 }
1825 break;
1826 case tok::kw_requires: {
1827 if (IsCpp) {
1828 bool ParsedClause = parseRequires(SeenEqual);
1829 if (ParsedClause)
1830 return;
1831 } else {
1832 nextToken();
1833 }
1834 break;
1835 }
1836 case tok::kw_enum:
1837 // Ignore if this is part of "template <enum ..." or "... -> enum" or
1838 // "template <..., enum ...>".
1839 if (Previous && Previous->isOneOf(tok::less, tok::arrow, tok::comma)) {
1840 nextToken();
1841 break;
1842 }
1843
1844 // parseEnum falls through and does not yet add an unwrapped line as an
1845 // enum definition can start a structural element.
1846 if (!parseEnum())
1847 break;
1848 // This only applies to C++ and Verilog.
1849 if (!IsCpp && !Style.isVerilog()) {
1850 addUnwrappedLine();
1851 return;
1852 }
1853 break;
1854 case tok::kw_typedef:
1855 nextToken();
1856 if (FormatTok->isOneOf(Keywords.kw_NS_ENUM, Keywords.kw_NS_OPTIONS,
1857 Keywords.kw_CF_ENUM, Keywords.kw_CF_OPTIONS,
1858 Keywords.kw_CF_CLOSED_ENUM,
1859 Keywords.kw_NS_CLOSED_ENUM)) {
1860 parseEnum();
1861 }
1862 break;
1863 case tok::kw_class:
1864 if (Style.isVerilog()) {
1865 parseBlock();
1866 addUnwrappedLine();
1867 return;
1868 }
1869 if (Style.isTableGen()) {
1870 // Do nothing special. In this case the l_brace becomes FunctionLBrace.
1871 // This is same as def and so on.
1872 nextToken();
1873 break;
1874 }
1875 [[fallthrough]];
1876 case tok::kw_struct:
1877 case tok::kw_union:
1878 if (parseStructLike())
1879 return;
1880 break;
1881 case tok::kw_decltype:
1882 nextToken();
1883 if (FormatTok->is(tok::l_paren)) {
1884 parseParens();
1885 if (FormatTok->Previous &&
1886 FormatTok->Previous->endsSequence(tok::r_paren, tok::kw_auto,
1887 tok::l_paren)) {
1888 Line->SeenDecltypeAuto = true;
1889 }
1890 }
1891 break;
1892 case tok::period:
1893 nextToken();
1894 // In Java, classes have an implicit static member "class".
1895 if (Style.isJava() && FormatTok && FormatTok->is(tok::kw_class))
1896 nextToken();
1897 if (Style.isJavaScript() && FormatTok &&
1898 FormatTok->Tok.getIdentifierInfo()) {
1899 // JavaScript only has pseudo keywords, all keywords are allowed to
1900 // appear in "IdentifierName" positions. See http://es5.github.io/#x7.6
1901 nextToken();
1902 }
1903 break;
1904 case tok::semi:
1905 nextToken();
1906 addUnwrappedLine();
1907 return;
1908 case tok::r_brace:
1909 addUnwrappedLine();
1910 return;
1911 case tok::string_literal:
1912 if (Style.isVerilog() && FormatTok->is(TT_VerilogProtected)) {
1913 FormatTok->Finalized = true;
1914 nextToken();
1915 addUnwrappedLine();
1916 return;
1917 }
1918 nextToken();
1919 break;
1920 case tok::l_paren: {
1921 parseParens();
1922 // Break the unwrapped line if a K&R C function definition has a parameter
1923 // declaration.
1924 if (OpeningBrace || !IsCpp || !Previous || eof())
1925 break;
1926 if (isC78ParameterDecl(FormatTok,
1927 Tokens->peekNextToken(/*SkipComment=*/true),
1928 Previous)) {
1929 addUnwrappedLine();
1930 return;
1931 }
1932 break;
1933 }
1934 case tok::kw_operator:
1935 nextToken();
1936 if (FormatTok->isBinaryOperator())
1937 nextToken();
1938 break;
1939 case tok::caret: {
1940 const auto *Prev = FormatTok->getPreviousNonComment();
1941 nextToken();
1942 if (Prev && Prev->is(tok::identifier))
1943 break;
1944 // Block return type.
1945 if (FormatTok->Tok.isAnyIdentifier() || FormatTok->isTypeName(LangOpts)) {
1946 nextToken();
1947 // Return types: pointers are ok too.
1948 while (FormatTok->is(tok::star))
1949 nextToken();
1950 }
1951 // Block argument list.
1952 if (FormatTok->is(tok::l_paren))
1953 parseParens();
1954 // Block body.
1955 if (FormatTok->is(tok::l_brace))
1956 parseChildBlock();
1957 break;
1958 }
1959 case tok::l_brace:
1960 if (InRequiresExpression)
1961 FormatTok->setFinalizedType(TT_BracedListLBrace);
1962 if (!tryToParsePropertyAccessor() && !tryToParseBracedList()) {
1963 IsDecltypeAutoFunction = Line->SeenDecltypeAuto;
1964 // A block outside of parentheses must be the last part of a
1965 // structural element.
1966 // FIXME: Figure out cases where this is not true, and add projections
1967 // for them (the one we know is missing are lambdas).
1968 if (Style.isJava() &&
1969 Line->Tokens.front().Tok->is(Keywords.kw_synchronized)) {
1970 // If necessary, we could set the type to something different than
1971 // TT_FunctionLBrace.
1972 if (Style.BraceWrapping.AfterControlStatement ==
1973 FormatStyle::BWACS_Always) {
1974 addUnwrappedLine();
1975 }
1976 } else if (Style.BraceWrapping.AfterFunction) {
1977 addUnwrappedLine();
1978 }
1979 if (!Previous || Previous->isNot(TT_TypeDeclarationParen))
1980 FormatTok->setFinalizedType(TT_FunctionLBrace);
1981 parseBlock();
1982 IsDecltypeAutoFunction = false;
1983 addUnwrappedLine();
1984 return;
1985 }
1986 // Otherwise this was a braced init list, and the structural
1987 // element continues.
1988 break;
1989 case tok::kw_try:
1990 if (Style.isJavaScript() && Line->MustBeDeclaration) {
1991 // field/method declaration.
1992 nextToken();
1993 break;
1994 }
1995 // We arrive here when parsing function-try blocks.
1996 if (Style.BraceWrapping.AfterFunction)
1997 addUnwrappedLine();
1998 parseTryCatch();
1999 return;
2000 case tok::identifier: {
2001 if (Style.isCSharp() && FormatTok->is(Keywords.kw_where) &&
2002 Line->MustBeDeclaration) {
2003 addUnwrappedLine();
2004 parseCSharpGenericTypeConstraint();
2005 break;
2006 }
2007 if (FormatTok->is(TT_MacroBlockEnd)) {
2008 addUnwrappedLine();
2009 return;
2010 }
2011
2012 // Function declarations (as opposed to function expressions) are parsed
2013 // on their own unwrapped line by continuing this loop. Function
2014 // expressions (functions that are not on their own line) must not create
2015 // a new unwrapped line, so they are special cased below.
2016 size_t TokenCount = Line->Tokens.size();
2017 if (Style.isJavaScript() && FormatTok->is(Keywords.kw_function) &&
2018 (TokenCount > 1 ||
2019 (TokenCount == 1 &&
2020 Line->Tokens.front().Tok->isNot(Keywords.kw_async)))) {
2021 tryToParseJSFunction();
2022 break;
2023 }
2024 if ((Style.isJavaScript() || Style.isJava()) &&
2025 FormatTok->is(Keywords.kw_interface)) {
2026 if (Style.isJavaScript()) {
2027 // In JavaScript/TypeScript, "interface" can be used as a standalone
2028 // identifier, e.g. in `var interface = 1;`. If "interface" is
2029 // followed by another identifier, it is very like to be an actual
2030 // interface declaration.
2031 unsigned StoredPosition = Tokens->getPosition();
2032 FormatToken *Next = Tokens->getNextToken();
2033 FormatTok = Tokens->setPosition(StoredPosition);
2034 if (!mustBeJSIdent(Keywords, Next)) {
2035 nextToken();
2036 break;
2037 }
2038 }
2039 parseRecord();
2040 addUnwrappedLine();
2041 return;
2042 }
2043
2044 if (Style.isVerilog()) {
2045 if (FormatTok->is(Keywords.kw_table)) {
2046 parseVerilogTable();
2047 return;
2048 }
2049 if (Keywords.isVerilogBegin(*FormatTok) ||
2050 Keywords.isVerilogHierarchy(*FormatTok)) {
2051 parseBlock();
2052 addUnwrappedLine();
2053 return;
2054 }
2055 }
2056
2057 if (!IsCpp && FormatTok->is(Keywords.kw_interface)) {
2058 if (parseStructLike())
2059 return;
2060 break;
2061 }
2062
2063 if (IsCpp && FormatTok->is(TT_StatementMacro)) {
2064 parseStatementMacro();
2065 return;
2066 }
2067
2068 // See if the following token should start a new unwrapped line.
2069 StringRef Text = FormatTok->TokenText;
2070
2071 FormatToken *PreviousToken = FormatTok;
2072 nextToken();
2073
2074 // JS doesn't have macros, and within classes colons indicate fields, not
2075 // labels.
2076 if (Style.isJavaScript())
2077 break;
2078
2079 auto OneTokenSoFar = [&]() {
2080 auto I = Line->Tokens.begin(), E = Line->Tokens.end();
2081 while (I != E && I->Tok->is(tok::comment))
2082 ++I;
2083 if (Style.isVerilog())
2084 while (I != E && I->Tok->is(tok::hash))
2085 ++I;
2086 return I != E && (++I == E);
2087 };
2088 if (OneTokenSoFar()) {
2089 // Recognize function-like macro usages without trailing semicolon as
2090 // well as free-standing macros like Q_OBJECT.
2091 bool FunctionLike = FormatTok->is(tok::l_paren);
2092 if (FunctionLike)
2093 parseParens();
2094
2095 bool FollowedByNewline =
2096 CommentsBeforeNextToken.empty()
2097 ? FormatTok->NewlinesBefore > 0
2098 : CommentsBeforeNextToken.front()->NewlinesBefore > 0;
2099
2100 if (FollowedByNewline &&
2101 (Text.size() >= 5 ||
2102 (FunctionLike && FormatTok->isNot(tok::l_paren))) &&
2103 tokenCanStartNewLine(*FormatTok) && Text == Text.upper()) {
2104 if (PreviousToken->isNot(TT_UntouchableMacroFunc))
2105 PreviousToken->setFinalizedType(TT_FunctionLikeOrFreestandingMacro);
2106 addUnwrappedLine();
2107 return;
2108 }
2109 }
2110 break;
2111 }
2112 case tok::equal:
2113 if ((Style.isJavaScript() || Style.isCSharp()) &&
2114 FormatTok->is(TT_FatArrow)) {
2115 tryToParseChildBlock();
2116 break;
2117 }
2118
2119 SeenEqual = true;
2120 nextToken();
2121 if (FormatTok->is(tok::l_brace)) {
2122 // C# needs this change to ensure that array initialisers and object
2123 // initialisers are indented the same way. In TypeScript, the brace
2124 // can also be an object type definition.
2125 if (!Style.isJavaScript())
2126 FormatTok->setBlockKind(BK_BracedInit);
2127 // TableGen's defset statement has syntax of the form,
2128 // `defset <type> <name> = { <statement>... }`
2129 if (Style.isTableGen() &&
2130 Line->Tokens.begin()->Tok->is(Keywords.kw_defset)) {
2131 FormatTok->setFinalizedType(TT_FunctionLBrace);
2132 parseBlock(/*MustBeDeclaration=*/false, /*AddLevels=*/1u,
2133 /*MunchSemi=*/false);
2134 addUnwrappedLine();
2135 break;
2136 }
2137 nextToken();
2138 parseBracedList();
2139 } else if (Style.Language == FormatStyle::LK_Proto &&
2140 FormatTok->is(tok::less)) {
2141 nextToken();
2142 parseBracedList(/*IsAngleBracket=*/true);
2143 }
2144 break;
2145 case tok::l_square:
2146 parseSquare();
2147 break;
2148 case tok::kw_new:
2149 if (Style.isCSharp() &&
2150 (Tokens->peekNextToken()->isAccessSpecifierKeyword() ||
2151 (Previous && Previous->isAccessSpecifierKeyword()))) {
2152 nextToken();
2153 } else {
2154 parseNew();
2155 }
2156 break;
2157 case tok::kw_switch:
2158 if (Style.isJava())
2159 parseSwitch(/*IsExpr=*/true);
2160 else
2161 nextToken();
2162 break;
2163 case tok::kw_case:
2164 // Proto: there are no switch/case statements.
2165 if (Style.Language == FormatStyle::LK_Proto) {
2166 nextToken();
2167 return;
2168 }
2169 // In Verilog switch is called case.
2170 if (Style.isVerilog()) {
2171 parseBlock();
2172 addUnwrappedLine();
2173 return;
2174 }
2175 if (Style.isJavaScript() && Line->MustBeDeclaration) {
2176 // 'case: string' field declaration.
2177 nextToken();
2178 break;
2179 }
2180 parseCaseLabel();
2181 break;
2182 case tok::kw_default:
2183 nextToken();
2184 if (Style.isVerilog()) {
2185 if (FormatTok->is(tok::colon)) {
2186 // The label will be handled in the next iteration.
2187 break;
2188 }
2189 if (FormatTok->is(Keywords.kw_clocking)) {
2190 // A default clocking block.
2191 parseBlock();
2192 addUnwrappedLine();
2193 return;
2194 }
2195 parseVerilogCaseLabel();
2196 return;
2197 }
2198 break;
2199 case tok::colon:
2200 nextToken();
2201 if (Style.isVerilog()) {
2202 parseVerilogCaseLabel();
2203 return;
2204 }
2205 break;
2206 case tok::greater:
2207 nextToken();
2208 if (FormatTok->is(tok::l_brace))
2209 FormatTok->Previous->setFinalizedType(TT_TemplateCloser);
2210 break;
2211 default:
2212 nextToken();
2213 break;
2214 }
2215 }
2216}
2217
2218bool UnwrappedLineParser::tryToParsePropertyAccessor() {
2219 assert(FormatTok->is(tok::l_brace));
2220 if (!Style.isCSharp())
2221 return false;
2222 // See if it's a property accessor.
2223 if (!FormatTok->Previous || FormatTok->Previous->isNot(tok::identifier))
2224 return false;
2225
2226 // See if we are inside a property accessor.
2227 //
2228 // Record the current tokenPosition so that we can advance and
2229 // reset the current token. `Next` is not set yet so we need
2230 // another way to advance along the token stream.
2231 unsigned int StoredPosition = Tokens->getPosition();
2232 FormatToken *Tok = Tokens->getNextToken();
2233
2234 // A trivial property accessor is of the form:
2235 // { [ACCESS_SPECIFIER] [get]; [ACCESS_SPECIFIER] [set|init] }
2236 // Track these as they do not require line breaks to be introduced.
2237 bool HasSpecialAccessor = false;
2238 bool IsTrivialPropertyAccessor = true;
2239 bool HasAttribute = false;
2240 while (!eof()) {
2241 if (const bool IsAccessorKeyword =
2242 Tok->isOneOf(Keywords.kw_get, Keywords.kw_init, Keywords.kw_set);
2243 IsAccessorKeyword || Tok->isAccessSpecifierKeyword() ||
2244 Tok->isOneOf(tok::l_square, tok::semi, Keywords.kw_internal)) {
2245 if (IsAccessorKeyword)
2246 HasSpecialAccessor = true;
2247 else if (Tok->is(tok::l_square))
2248 HasAttribute = true;
2249 Tok = Tokens->getNextToken();
2250 continue;
2251 }
2252 if (Tok->isNot(tok::r_brace))
2253 IsTrivialPropertyAccessor = false;
2254 break;
2255 }
2256
2257 if (!HasSpecialAccessor || HasAttribute) {
2258 Tokens->setPosition(StoredPosition);
2259 return false;
2260 }
2261
2262 // Try to parse the property accessor:
2263 // https://docs.microsoft.com/en-us/dotnet/csharp/programming-guide/classes-and-structs/properties
2264 Tokens->setPosition(StoredPosition);
2265 if (!IsTrivialPropertyAccessor && Style.BraceWrapping.AfterFunction)
2266 addUnwrappedLine();
2267 nextToken();
2268 do {
2269 switch (FormatTok->Tok.getKind()) {
2270 case tok::r_brace:
2271 nextToken();
2272 if (FormatTok->is(tok::equal)) {
2273 while (!eof() && FormatTok->isNot(tok::semi))
2274 nextToken();
2275 nextToken();
2276 }
2277 addUnwrappedLine();
2278 return true;
2279 case tok::l_brace:
2280 ++Line->Level;
2281 parseBlock(/*MustBeDeclaration=*/true);
2282 addUnwrappedLine();
2283 --Line->Level;
2284 break;
2285 case tok::equal:
2286 if (FormatTok->is(TT_FatArrow)) {
2287 ++Line->Level;
2288 do {
2289 nextToken();
2290 } while (!eof() && FormatTok->isNot(tok::semi));
2291 nextToken();
2292 addUnwrappedLine();
2293 --Line->Level;
2294 break;
2295 }
2296 nextToken();
2297 break;
2298 default:
2299 if (FormatTok->isOneOf(Keywords.kw_get, Keywords.kw_init,
2300 Keywords.kw_set) &&
2301 !IsTrivialPropertyAccessor) {
2302 // Non-trivial get/set needs to be on its own line.
2303 addUnwrappedLine();
2304 }
2305 nextToken();
2306 }
2307 } while (!eof());
2308
2309 // Unreachable for well-formed code (paired '{' and '}').
2310 return true;
2311}
2312
2313bool UnwrappedLineParser::tryToParseLambda() {
2314 assert(FormatTok->is(tok::l_square));
2315 if (!IsCpp) {
2316 nextToken();
2317 return false;
2318 }
2319 FormatToken &LSquare = *FormatTok;
2320 if (!tryToParseLambdaIntroducer())
2321 return false;
2322
2323 FormatToken *Arrow = nullptr;
2324 bool InTemplateParameterList = false;
2325
2326 while (FormatTok->isNot(tok::l_brace)) {
2327 if (FormatTok->isTypeName(LangOpts) || FormatTok->isAttribute()) {
2328 nextToken();
2329 continue;
2330 }
2331 switch (FormatTok->Tok.getKind()) {
2332 case tok::l_brace:
2333 break;
2334 case tok::l_paren:
2335 parseParens(/*AmpAmpTokenType=*/TT_PointerOrReference);
2336 break;
2337 case tok::l_square:
2338 parseSquare();
2339 break;
2340 case tok::less:
2341 assert(FormatTok->Previous);
2342 if (FormatTok->Previous->is(tok::r_square))
2343 InTemplateParameterList = true;
2344 nextToken();
2345 break;
2346 case tok::kw_auto:
2347 case tok::kw_class:
2348 case tok::kw_struct:
2349 case tok::kw_union:
2350 case tok::kw_template:
2351 case tok::kw_typename:
2352 case tok::amp:
2353 case tok::star:
2354 case tok::kw_const:
2355 case tok::kw_constexpr:
2356 case tok::kw_consteval:
2357 case tok::comma:
2358 case tok::greater:
2359 case tok::identifier:
2360 case tok::numeric_constant:
2361 case tok::coloncolon:
2362 case tok::kw_mutable:
2363 case tok::kw_noexcept:
2364 case tok::kw_static:
2365 nextToken();
2366 break;
2367 // Specialization of a template with an integer parameter can contain
2368 // arithmetic, logical, comparison and ternary operators.
2369 //
2370 // FIXME: This also accepts sequences of operators that are not in the scope
2371 // of a template argument list.
2372 //
2373 // In a C++ lambda a template type can only occur after an arrow. We use
2374 // this as an heuristic to distinguish between Objective-C expressions
2375 // followed by an `a->b` expression, such as:
2376 // ([obj func:arg] + a->b)
2377 // Otherwise the code below would parse as a lambda.
2378 case tok::plus:
2379 case tok::minus:
2380 case tok::exclaim:
2381 case tok::tilde:
2382 case tok::slash:
2383 case tok::percent:
2384 case tok::lessless:
2385 case tok::pipe:
2386 case tok::pipepipe:
2387 case tok::ampamp:
2388 case tok::caret:
2389 case tok::equalequal:
2390 case tok::exclaimequal:
2391 case tok::greaterequal:
2392 case tok::lessequal:
2393 case tok::question:
2394 case tok::colon:
2395 case tok::ellipsis:
2396 case tok::kw_true:
2397 case tok::kw_false:
2398 if (Arrow || InTemplateParameterList) {
2399 nextToken();
2400 break;
2401 }
2402 return true;
2403 case tok::arrow:
2404 Arrow = FormatTok;
2405 nextToken();
2406 break;
2407 case tok::kw_requires:
2408 parseRequiresClause();
2409 break;
2410 case tok::equal:
2411 if (!InTemplateParameterList)
2412 return true;
2413 nextToken();
2414 break;
2415 default:
2416 return true;
2417 }
2418 }
2419
2420 FormatTok->setFinalizedType(TT_LambdaLBrace);
2421 LSquare.setFinalizedType(TT_LambdaLSquare);
2422
2423 if (Arrow)
2424 Arrow->setFinalizedType(TT_LambdaArrow);
2425
2426 NestedLambdas.push_back(Line->SeenDecltypeAuto);
2427 parseChildBlock();
2428 assert(!NestedLambdas.empty());
2429 NestedLambdas.pop_back();
2430
2431 return true;
2432}
2433
2434bool UnwrappedLineParser::tryToParseLambdaIntroducer() {
2435 const FormatToken *Previous = FormatTok->Previous;
2436 const FormatToken *LeftSquare = FormatTok;
2437 nextToken();
2438 if (Previous) {
2439 const auto *PrevPrev = Previous->getPreviousNonComment();
2440 if (Previous->is(tok::star) && PrevPrev && PrevPrev->isTypeName(LangOpts))
2441 return false;
2442 if (Previous->closesScope()) {
2443 // Not a potential C-style cast.
2444 if (Previous->isNot(tok::r_paren))
2445 return false;
2446 // Lambdas can be cast to function types only, e.g. `std::function<int()>`
2447 // and `int (*)()`.
2448 if (!PrevPrev || PrevPrev->isNoneOf(tok::greater, tok::r_paren))
2449 return false;
2450 }
2451 if (Previous && Previous->Tok.getIdentifierInfo() &&
2452 Previous->isNoneOf(tok::kw_return, tok::kw_co_await, tok::kw_co_yield,
2453 tok::kw_co_return)) {
2454 return false;
2455 }
2456 }
2457 if (LeftSquare->isCppStructuredBinding(IsCpp))
2458 return false;
2459 if (FormatTok->is(tok::l_square) || tok::isLiteral(FormatTok->Tok.getKind()))
2460 return false;
2461 if (FormatTok->is(tok::r_square)) {
2462 const FormatToken *Next = Tokens->peekNextToken(/*SkipComment=*/true);
2463 if (Next->is(tok::greater))
2464 return false;
2465 }
2466 parseSquare(/*LambdaIntroducer=*/true);
2467 return true;
2468}
2469
2470void UnwrappedLineParser::tryToParseJSFunction() {
2471 assert(FormatTok->is(Keywords.kw_function));
2472 if (FormatTok->is(Keywords.kw_async))
2473 nextToken();
2474 // Consume "function".
2475 nextToken();
2476
2477 // Consume * (generator function). Treat it like C++'s overloaded operators.
2478 if (FormatTok->is(tok::star)) {
2479 FormatTok->setFinalizedType(TT_OverloadedOperator);
2480 nextToken();
2481 }
2482
2483 // Consume function name.
2484 if (FormatTok->is(tok::identifier))
2485 nextToken();
2486
2487 if (FormatTok->isNot(tok::l_paren))
2488 return;
2489
2490 // Parse formal parameter list.
2491 parseParens();
2492
2493 if (FormatTok->is(tok::colon)) {
2494 // Parse a type definition.
2495 nextToken();
2496
2497 // Eat the type declaration. For braced inline object types, balance braces,
2498 // otherwise just parse until finding an l_brace for the function body.
2499 if (FormatTok->is(tok::l_brace))
2500 tryToParseBracedList();
2501 else
2502 while (FormatTok->isNoneOf(tok::l_brace, tok::semi) && !eof())
2503 nextToken();
2504 }
2505
2506 if (FormatTok->is(tok::semi))
2507 return;
2508
2509 parseChildBlock();
2510}
2511
2512bool UnwrappedLineParser::tryToParseBracedList() {
2513 if (FormatTok->is(BK_Unknown))
2514 calculateBraceTypes();
2515 assert(FormatTok->isNot(BK_Unknown));
2516 if (FormatTok->is(BK_Block))
2517 return false;
2518 nextToken();
2519 parseBracedList();
2520 return true;
2521}
2522
2523bool UnwrappedLineParser::tryToParseChildBlock() {
2524 assert(Style.isJavaScript() || Style.isCSharp());
2525 assert(FormatTok->is(TT_FatArrow));
2526 // Fat arrows (=>) have tok::TokenKind tok::equal but TokenType TT_FatArrow.
2527 // They always start an expression or a child block if followed by a curly
2528 // brace.
2529 nextToken();
2530 if (FormatTok->isNot(tok::l_brace))
2531 return false;
2532 parseChildBlock();
2533 return true;
2534}
2535
2536bool UnwrappedLineParser::parseBracedList(bool IsAngleBracket, bool IsEnum) {
2537 assert(!IsAngleBracket || !IsEnum);
2538 bool HasError = false;
2539
2540 // FIXME: Once we have an expression parser in the UnwrappedLineParser,
2541 // replace this by using parseAssignmentExpression() inside.
2542 do {
2543 if (Style.isCSharp() && FormatTok->is(TT_FatArrow) &&
2544 tryToParseChildBlock()) {
2545 continue;
2546 }
2547 if (Style.isJavaScript()) {
2548 if (FormatTok->is(Keywords.kw_function)) {
2549 tryToParseJSFunction();
2550 continue;
2551 }
2552 if (FormatTok->is(tok::l_brace)) {
2553 // Could be a method inside of a braced list `{a() { return 1; }}`.
2554 if (tryToParseBracedList())
2555 continue;
2556 parseChildBlock();
2557 }
2558 }
2559 if (FormatTok->is(IsAngleBracket ? tok::greater : tok::r_brace)) {
2560 if (IsEnum) {
2561 FormatTok->setBlockKind(BK_Block);
2562 if (!Style.AllowShortEnumsOnASingleLine)
2563 addUnwrappedLine();
2564 }
2565 nextToken();
2566 return !HasError;
2567 }
2568 switch (FormatTok->Tok.getKind()) {
2569 case tok::l_square:
2570 if (Style.isCSharp())
2571 parseSquare();
2572 else
2573 tryToParseLambda();
2574 break;
2575 case tok::l_paren:
2576 parseParens();
2577 // JavaScript can just have free standing methods and getters/setters in
2578 // object literals. Detect them by a "{" following ")".
2579 if (Style.isJavaScript()) {
2580 if (FormatTok->is(tok::l_brace))
2581 parseChildBlock();
2582 break;
2583 }
2584 break;
2585 case tok::l_brace:
2586 // Assume there are no blocks inside a braced init list apart
2587 // from the ones we explicitly parse out (like lambdas).
2588 FormatTok->setBlockKind(BK_BracedInit);
2589 if (!IsAngleBracket) {
2590 auto *Prev = FormatTok->Previous;
2591 if (Prev && Prev->is(tok::greater))
2592 Prev->setFinalizedType(TT_TemplateCloser);
2593 }
2594 nextToken();
2595 parseBracedList();
2596 break;
2597 case tok::less:
2598 nextToken();
2599 if (IsAngleBracket)
2600 parseBracedList(/*IsAngleBracket=*/true);
2601 break;
2602 case tok::semi:
2603 // JavaScript (or more precisely TypeScript) can have semicolons in braced
2604 // lists (in so-called TypeMemberLists). Thus, the semicolon cannot be
2605 // used for error recovery if we have otherwise determined that this is
2606 // a braced list.
2607 if (Style.isJavaScript()) {
2608 nextToken();
2609 break;
2610 }
2611 HasError = true;
2612 if (!IsEnum)
2613 return false;
2614 nextToken();
2615 break;
2616 case tok::comma:
2617 nextToken();
2618 if (IsEnum && !Style.AllowShortEnumsOnASingleLine)
2619 addUnwrappedLine();
2620 break;
2621 case tok::kw_requires:
2622 parseRequiresExpression();
2623 break;
2624 default:
2625 nextToken();
2626 break;
2627 }
2628 } while (!eof());
2629 return false;
2630}
2631
2632/// Parses a pair of parentheses (and everything between them).
2633/// \param StarAndAmpTokenType If different than TT_Unknown sets this type for
2634/// all (double) ampersands and stars. This applies for all nested scopes as
2635/// well.
2636///
2637/// Returns whether there is a `=` token between the parentheses.
2638bool UnwrappedLineParser::parseParens(TokenType StarAndAmpTokenType,
2639 bool InMacroCall) {
2640 assert(FormatTok->is(tok::l_paren) && "'(' expected.");
2641 auto *LParen = FormatTok;
2642 auto *Prev = FormatTok->Previous;
2643 bool SeenComma = false;
2644 bool SeenEqual = false;
2645 bool MightBeFoldExpr = false;
2646 nextToken();
2647 const bool MightBeStmtExpr = FormatTok->is(tok::l_brace);
2648 if (!InMacroCall && Prev && Prev->is(TT_FunctionLikeMacro))
2649 InMacroCall = true;
2650 do {
2651 switch (FormatTok->Tok.getKind()) {
2652 case tok::l_paren:
2653 if (parseParens(StarAndAmpTokenType, InMacroCall))
2654 SeenEqual = true;
2655 if (Style.isJava() && FormatTok->is(tok::l_brace))
2656 parseChildBlock();
2657 break;
2658 case tok::r_paren: {
2659 auto *RParen = FormatTok;
2660 nextToken();
2661 if (Prev) {
2662 auto OptionalParens = [&] {
2663 if (Style.RemoveParentheses == FormatStyle::RPS_Leave ||
2664 MightBeStmtExpr || MightBeFoldExpr || SeenComma || InMacroCall ||
2665 Line->InMacroBody || RParen->getPreviousNonComment() == LParen) {
2666 return false;
2667 }
2668 const bool DoubleParens =
2669 Prev->is(tok::l_paren) && FormatTok->is(tok::r_paren);
2670 if (DoubleParens) {
2671 const auto *PrevPrev = Prev->getPreviousNonComment();
2672 const bool Excluded =
2673 PrevPrev &&
2674 (PrevPrev->isOneOf(tok::kw___attribute, tok::kw_decltype) ||
2675 (SeenEqual &&
2676 (PrevPrev->isOneOf(tok::kw_if, tok::kw_while) ||
2677 PrevPrev->endsSequence(tok::kw_constexpr, tok::kw_if))));
2678 if (!Excluded)
2679 return true;
2680 } else {
2681 const bool CommaSeparated =
2682 Prev->isOneOf(tok::l_paren, tok::comma) &&
2683 FormatTok->isOneOf(tok::comma, tok::r_paren);
2684 if (CommaSeparated &&
2685 // LParen is not preceded by ellipsis, comma.
2686 !Prev->endsSequence(tok::comma, tok::ellipsis) &&
2687 // RParen is not followed by comma, ellipsis.
2688 !(FormatTok->is(tok::comma) &&
2689 Tokens->peekNextToken()->is(tok::ellipsis))) {
2690 return true;
2691 }
2692 const bool ReturnParens =
2693 Style.RemoveParentheses == FormatStyle::RPS_ReturnStatement &&
2694 ((NestedLambdas.empty() && !IsDecltypeAutoFunction) ||
2695 (!NestedLambdas.empty() && !NestedLambdas.back())) &&
2696 Prev->isOneOf(tok::kw_return, tok::kw_co_return) &&
2697 FormatTok->is(tok::semi);
2698 if (ReturnParens)
2699 return true;
2700 }
2701 return false;
2702 };
2703 if (OptionalParens()) {
2704 LParen->Optional = true;
2705 RParen->Optional = true;
2706 } else if (Prev->is(TT_TypenameMacro)) {
2707 LParen->setFinalizedType(TT_TypeDeclarationParen);
2708 RParen->setFinalizedType(TT_TypeDeclarationParen);
2709 } else if (Prev->is(tok::greater) && RParen->Previous == LParen) {
2710 Prev->setFinalizedType(TT_TemplateCloser);
2711 } else if (FormatTok->is(tok::l_brace) && Prev->is(tok::amp) &&
2712 !Prev->Previous) {
2713 FormatTok->setBlockKind(BK_BracedInit);
2714 }
2715 }
2716 return SeenEqual;
2717 }
2718 case tok::r_brace:
2719 // A "}" inside parenthesis is an error if there wasn't a matching "{".
2720 return SeenEqual;
2721 case tok::l_square:
2722 tryToParseLambda();
2723 break;
2724 case tok::l_brace:
2725 if (!tryToParseBracedList())
2726 parseChildBlock();
2727 break;
2728 case tok::at:
2729 nextToken();
2730 if (FormatTok->is(tok::l_brace)) {
2731 nextToken();
2732 parseBracedList();
2733 }
2734 break;
2735 case tok::comma:
2736 SeenComma = true;
2737 nextToken();
2738 break;
2739 case tok::ellipsis:
2740 MightBeFoldExpr = true;
2741 nextToken();
2742 break;
2743 case tok::equal:
2744 SeenEqual = true;
2745 if (Style.isCSharp() && FormatTok->is(TT_FatArrow))
2746 tryToParseChildBlock();
2747 else
2748 nextToken();
2749 break;
2750 case tok::kw_class:
2751 if (Style.isJavaScript())
2752 parseRecord(/*ParseAsExpr=*/true);
2753 else
2754 nextToken();
2755 break;
2756 case tok::identifier:
2757 if (Style.isJavaScript() && (FormatTok->is(Keywords.kw_function)))
2758 tryToParseJSFunction();
2759 else
2760 nextToken();
2761 break;
2762 case tok::kw_switch:
2763 if (Style.isJava())
2764 parseSwitch(/*IsExpr=*/true);
2765 else
2766 nextToken();
2767 break;
2768 case tok::kw_requires:
2769 parseRequiresExpression();
2770 break;
2771 case tok::star:
2772 case tok::amp:
2773 case tok::ampamp:
2774 if (StarAndAmpTokenType != TT_Unknown)
2775 FormatTok->setFinalizedType(StarAndAmpTokenType);
2776 [[fallthrough]];
2777 default:
2778 nextToken();
2779 break;
2780 }
2781 } while (!eof());
2782 return SeenEqual;
2783}
2784
2785void UnwrappedLineParser::parseSquare(bool LambdaIntroducer) {
2786 if (!LambdaIntroducer) {
2787 assert(FormatTok->is(tok::l_square) && "'[' expected.");
2788 if (tryToParseLambda())
2789 return;
2790 }
2791 do {
2792 switch (FormatTok->Tok.getKind()) {
2793 case tok::l_paren:
2794 parseParens();
2795 break;
2796 case tok::r_square:
2797 nextToken();
2798 return;
2799 case tok::r_brace:
2800 // A "}" inside parenthesis is an error if there wasn't a matching "{".
2801 return;
2802 case tok::l_square:
2803 parseSquare();
2804 break;
2805 case tok::l_brace: {
2806 if (!tryToParseBracedList())
2807 parseChildBlock();
2808 break;
2809 }
2810 case tok::at:
2811 case tok::colon:
2812 nextToken();
2813 if (FormatTok->is(tok::l_brace)) {
2814 nextToken();
2815 parseBracedList();
2816 }
2817 break;
2818 default:
2819 nextToken();
2820 break;
2821 }
2822 } while (!eof());
2823}
2824
2825void UnwrappedLineParser::keepAncestorBraces() {
2826 if (!Style.RemoveBracesLLVM)
2827 return;
2828
2829 const int MaxNestingLevels = 2;
2830 const int Size = NestedTooDeep.size();
2831 if (Size >= MaxNestingLevels)
2832 NestedTooDeep[Size - MaxNestingLevels] = true;
2833 NestedTooDeep.push_back(false);
2834}
2835
2837 for (const auto &Token : llvm::reverse(Line.Tokens))
2838 if (Token.Tok->isNot(tok::comment))
2839 return Token.Tok;
2840
2841 return nullptr;
2842}
2843
2844void UnwrappedLineParser::parseUnbracedBody(bool CheckEOF) {
2845 FormatToken *Tok = nullptr;
2846
2847 if (Style.InsertBraces && !Line->InPPDirective && !Line->Tokens.empty() &&
2848 PreprocessorDirectives.empty() && FormatTok->isNot(tok::semi)) {
2849 Tok = Style.BraceWrapping.AfterControlStatement == FormatStyle::BWACS_Never
2850 ? getLastNonComment(*Line)
2851 : Line->Tokens.back().Tok;
2852 assert(Tok);
2853 if (Tok->BraceCount < 0) {
2854 assert(Tok->BraceCount == -1);
2855 Tok = nullptr;
2856 } else {
2857 Tok->BraceCount = -1;
2858 }
2859 }
2860
2861 addUnwrappedLine();
2862 ++Line->Level;
2863 ++Line->UnbracedBodyLevel;
2864 parseStructuralElement();
2865 --Line->UnbracedBodyLevel;
2866
2867 if (Tok) {
2868 assert(!Line->InPPDirective);
2869 Tok = nullptr;
2870 for (const auto &L : llvm::reverse(*CurrentLines)) {
2871 if (!L.InPPDirective && getLastNonComment(L)) {
2872 Tok = L.Tokens.back().Tok;
2873 break;
2874 }
2875 }
2876 assert(Tok);
2877 ++Tok->BraceCount;
2878 }
2879
2880 if (CheckEOF && eof())
2881 addUnwrappedLine();
2882
2883 --Line->Level;
2884}
2885
2886static void markOptionalBraces(FormatToken *LeftBrace) {
2887 if (!LeftBrace)
2888 return;
2889
2890 assert(LeftBrace->is(tok::l_brace));
2891
2892 FormatToken *RightBrace = LeftBrace->MatchingParen;
2893 if (!RightBrace) {
2894 assert(!LeftBrace->Optional);
2895 return;
2896 }
2897
2898 assert(RightBrace->is(tok::r_brace));
2899 assert(RightBrace->MatchingParen == LeftBrace);
2900 assert(LeftBrace->Optional == RightBrace->Optional);
2901
2902 LeftBrace->Optional = true;
2903 RightBrace->Optional = true;
2904}
2905
2906void UnwrappedLineParser::handleAttributes() {
2907 // Handle AttributeMacro, e.g. `if (x) UNLIKELY`.
2908 if (FormatTok->isAttribute())
2909 nextToken();
2910 else if (FormatTok->is(tok::l_square))
2911 handleCppAttributes();
2912}
2913
2914bool UnwrappedLineParser::handleCppAttributes() {
2915 // Handle [[likely]] / [[unlikely]] attributes.
2916 assert(FormatTok->is(tok::l_square));
2917 if (!tryToParseSimpleAttribute())
2918 return false;
2919 parseSquare();
2920 return true;
2921}
2922
2923/// Returns whether \c Tok begins a block.
2924bool UnwrappedLineParser::isBlockBegin(const FormatToken &Tok) const {
2925 // FIXME: rename the function or make
2926 // Tok.isOneOf(tok::l_brace, TT_MacroBlockBegin) work.
2927 return Style.isVerilog() ? Keywords.isVerilogBegin(Tok)
2928 : Tok.is(tok::l_brace);
2929}
2930
2931FormatToken *UnwrappedLineParser::parseIfThenElse(IfStmtKind *IfKind,
2932 bool KeepBraces,
2933 bool IsVerilogAssert) {
2934 assert((FormatTok->is(tok::kw_if) ||
2935 (Style.isVerilog() &&
2936 FormatTok->isOneOf(tok::kw_restrict, Keywords.kw_assert,
2937 Keywords.kw_assume, Keywords.kw_cover))) &&
2938 "'if' expected");
2939 nextToken();
2940
2941 if (IsVerilogAssert) {
2942 // Handle `assert #0` and `assert final`.
2943 if (FormatTok->is(Keywords.kw_verilogHash)) {
2944 nextToken();
2945 if (FormatTok->is(tok::numeric_constant))
2946 nextToken();
2947 } else if (FormatTok->isOneOf(Keywords.kw_final, Keywords.kw_property,
2948 Keywords.kw_sequence)) {
2949 nextToken();
2950 }
2951 }
2952
2953 // TableGen's if statement has the form of `if <cond> then { ... }`.
2954 if (Style.isTableGen()) {
2955 while (!eof() && FormatTok->isNot(Keywords.kw_then)) {
2956 // Simply skip until then. This range only contains a value.
2957 nextToken();
2958 }
2959 }
2960
2961 // Handle `if !consteval`.
2962 if (FormatTok->is(tok::exclaim))
2963 nextToken();
2964
2965 bool KeepIfBraces = true;
2966 if (FormatTok->is(tok::kw_consteval)) {
2967 nextToken();
2968 } else {
2969 KeepIfBraces = !Style.RemoveBracesLLVM || KeepBraces;
2970 if (FormatTok->isOneOf(tok::kw_constexpr, tok::identifier))
2971 nextToken();
2972 if (FormatTok->is(tok::l_paren)) {
2973 FormatTok->setFinalizedType(TT_ConditionLParen);
2974 parseParens();
2975 }
2976 }
2977 handleAttributes();
2978 // The then action is optional in Verilog assert statements.
2979 if (IsVerilogAssert && FormatTok->is(tok::semi)) {
2980 nextToken();
2981 addUnwrappedLine();
2982 return nullptr;
2983 }
2984
2985 bool NeedsUnwrappedLine = false;
2986 keepAncestorBraces();
2987
2988 FormatToken *IfLeftBrace = nullptr;
2989 IfStmtKind IfBlockKind = IfStmtKind::NotIf;
2990
2991 if (isBlockBegin(*FormatTok)) {
2992 FormatTok->setFinalizedType(TT_ControlStatementLBrace);
2993 IfLeftBrace = FormatTok;
2994 CompoundStatementIndenter Indenter(this, Style, Line->Level);
2995 parseBlock(/*MustBeDeclaration=*/false, /*AddLevels=*/1u,
2996 /*MunchSemi=*/true, KeepIfBraces, &IfBlockKind);
2997 setPreviousRBraceType(TT_ControlStatementRBrace);
2998 if (Style.BraceWrapping.BeforeElse)
2999 addUnwrappedLine();
3000 else
3001 NeedsUnwrappedLine = true;
3002 } else if (IsVerilogAssert && FormatTok->is(tok::kw_else)) {
3003 addUnwrappedLine();
3004 } else {
3005 parseUnbracedBody();
3006 }
3007
3008 if (Style.RemoveBracesLLVM) {
3009 assert(!NestedTooDeep.empty());
3010 KeepIfBraces = KeepIfBraces ||
3011 (IfLeftBrace && !IfLeftBrace->MatchingParen) ||
3012 NestedTooDeep.back() || IfBlockKind == IfStmtKind::IfOnly ||
3013 IfBlockKind == IfStmtKind::IfElseIf;
3014 }
3015
3016 bool KeepElseBraces = KeepIfBraces;
3017 FormatToken *ElseLeftBrace = nullptr;
3018 IfStmtKind Kind = IfStmtKind::IfOnly;
3019
3020 if (FormatTok->is(tok::kw_else)) {
3021 if (Style.RemoveBracesLLVM) {
3022 NestedTooDeep.back() = false;
3023 Kind = IfStmtKind::IfElse;
3024 }
3025 nextToken();
3026 handleAttributes();
3027 if (isBlockBegin(*FormatTok)) {
3028 const bool FollowedByIf = Tokens->peekNextToken()->is(tok::kw_if);
3029 FormatTok->setFinalizedType(TT_ElseLBrace);
3030 ElseLeftBrace = FormatTok;
3031 CompoundStatementIndenter Indenter(this, Style, Line->Level);
3032 IfStmtKind ElseBlockKind = IfStmtKind::NotIf;
3033 FormatToken *IfLBrace =
3034 parseBlock(/*MustBeDeclaration=*/false, /*AddLevels=*/1u,
3035 /*MunchSemi=*/true, KeepElseBraces, &ElseBlockKind);
3036 setPreviousRBraceType(TT_ElseRBrace);
3037 if (FormatTok->is(tok::kw_else)) {
3038 KeepElseBraces = KeepElseBraces ||
3039 ElseBlockKind == IfStmtKind::IfOnly ||
3040 ElseBlockKind == IfStmtKind::IfElseIf;
3041 } else if (FollowedByIf && IfLBrace && !IfLBrace->Optional) {
3042 KeepElseBraces = true;
3043 assert(ElseLeftBrace->MatchingParen);
3044 markOptionalBraces(ElseLeftBrace);
3045 }
3046 addUnwrappedLine();
3047 } else if (!IsVerilogAssert && FormatTok->is(tok::kw_if)) {
3048 const FormatToken *Previous = Tokens->getPreviousToken();
3049 assert(Previous);
3050 const bool IsPrecededByComment = Previous->is(tok::comment);
3051 if (IsPrecededByComment) {
3052 addUnwrappedLine();
3053 ++Line->Level;
3054 }
3055 bool TooDeep = true;
3056 if (Style.RemoveBracesLLVM) {
3057 Kind = IfStmtKind::IfElseIf;
3058 TooDeep = NestedTooDeep.pop_back_val();
3059 }
3060 ElseLeftBrace = parseIfThenElse(/*IfKind=*/nullptr, KeepIfBraces);
3061 if (Style.RemoveBracesLLVM)
3062 NestedTooDeep.push_back(TooDeep);
3063 if (IsPrecededByComment)
3064 --Line->Level;
3065 } else {
3066 parseUnbracedBody(/*CheckEOF=*/true);
3067 }
3068 } else {
3069 KeepIfBraces = KeepIfBraces || IfBlockKind == IfStmtKind::IfElse;
3070 if (NeedsUnwrappedLine)
3071 addUnwrappedLine();
3072 }
3073
3074 if (!Style.RemoveBracesLLVM)
3075 return nullptr;
3076
3077 assert(!NestedTooDeep.empty());
3078 KeepElseBraces = KeepElseBraces ||
3079 (ElseLeftBrace && !ElseLeftBrace->MatchingParen) ||
3080 NestedTooDeep.back();
3081
3082 NestedTooDeep.pop_back();
3083
3084 if (!KeepIfBraces && !KeepElseBraces) {
3085 markOptionalBraces(IfLeftBrace);
3086 markOptionalBraces(ElseLeftBrace);
3087 } else if (IfLeftBrace) {
3088 FormatToken *IfRightBrace = IfLeftBrace->MatchingParen;
3089 if (IfRightBrace) {
3090 assert(IfRightBrace->MatchingParen == IfLeftBrace);
3091 assert(!IfLeftBrace->Optional);
3092 assert(!IfRightBrace->Optional);
3093 IfLeftBrace->MatchingParen = nullptr;
3094 IfRightBrace->MatchingParen = nullptr;
3095 }
3096 }
3097
3098 if (IfKind)
3099 *IfKind = Kind;
3100
3101 return IfLeftBrace;
3102}
3103
3104void UnwrappedLineParser::parseTryCatch() {
3105 assert(FormatTok->isOneOf(tok::kw_try, tok::kw___try) && "'try' expected");
3106 nextToken();
3107 bool NeedsUnwrappedLine = false;
3108 bool HasCtorInitializer = false;
3109 if (FormatTok->is(tok::colon)) {
3110 auto *Colon = FormatTok;
3111 // We are in a function try block, what comes is an initializer list.
3112 nextToken();
3113 if (FormatTok->is(tok::identifier)) {
3114 HasCtorInitializer = true;
3115 Colon->setFinalizedType(TT_CtorInitializerColon);
3116 }
3117
3118 // In case identifiers were removed by clang-tidy, what might follow is
3119 // multiple commas in sequence - before the first identifier.
3120 while (FormatTok->is(tok::comma))
3121 nextToken();
3122
3123 while (FormatTok->is(tok::identifier)) {
3124 nextToken();
3125 if (FormatTok->is(tok::l_paren)) {
3126 parseParens();
3127 } else if (FormatTok->is(tok::l_brace)) {
3128 nextToken();
3129 parseBracedList();
3130 }
3131
3132 // In case identifiers were removed by clang-tidy, what might follow is
3133 // multiple commas in sequence - after the first identifier.
3134 while (FormatTok->is(tok::comma))
3135 nextToken();
3136 }
3137 }
3138 // Parse try with resource.
3139 if (Style.isJava() && FormatTok->is(tok::l_paren))
3140 parseParens();
3141
3142 keepAncestorBraces();
3143
3144 if (FormatTok->is(tok::l_brace)) {
3145 if (HasCtorInitializer)
3146 FormatTok->setFinalizedType(TT_FunctionLBrace);
3147 CompoundStatementIndenter Indenter(this, Style, Line->Level);
3148 parseBlock();
3149 if (Style.BraceWrapping.BeforeCatch)
3150 addUnwrappedLine();
3151 else
3152 NeedsUnwrappedLine = true;
3153 } else if (FormatTok->isNot(tok::kw_catch)) {
3154 // The C++ standard requires a compound-statement after a try.
3155 // If there's none, we try to assume there's a structuralElement
3156 // and try to continue.
3157 addUnwrappedLine();
3158 ++Line->Level;
3159 parseStructuralElement();
3160 --Line->Level;
3161 }
3162 for (bool SeenCatch = false;;) {
3163 if (FormatTok->is(tok::at))
3164 nextToken();
3165 if (FormatTok->isNoneOf(tok::kw_catch, Keywords.kw___except,
3166 tok::kw___finally, tok::objc_catch,
3167 tok::objc_finally) &&
3168 !((Style.isJava() || Style.isJavaScript()) &&
3169 FormatTok->is(Keywords.kw_finally))) {
3170 break;
3171 }
3172 if (FormatTok->is(tok::kw_catch))
3173 SeenCatch = true;
3174 nextToken();
3175 while (FormatTok->isNot(tok::l_brace)) {
3176 if (FormatTok->is(tok::l_paren)) {
3177 parseParens();
3178 continue;
3179 }
3180 if (FormatTok->isOneOf(tok::semi, tok::r_brace) || eof()) {
3181 if (Style.RemoveBracesLLVM)
3182 NestedTooDeep.pop_back();
3183 return;
3184 }
3185 nextToken();
3186 }
3187 if (SeenCatch) {
3188 FormatTok->setFinalizedType(TT_ControlStatementLBrace);
3189 SeenCatch = false;
3190 }
3191 NeedsUnwrappedLine = false;
3192 Line->MustBeDeclaration = false;
3193 CompoundStatementIndenter Indenter(this, Style, Line->Level);
3194 parseBlock();
3195 if (Style.BraceWrapping.BeforeCatch)
3196 addUnwrappedLine();
3197 else
3198 NeedsUnwrappedLine = true;
3199 }
3200
3201 if (Style.RemoveBracesLLVM)
3202 NestedTooDeep.pop_back();
3203
3204 if (NeedsUnwrappedLine)
3205 addUnwrappedLine();
3206}
3207
3208void UnwrappedLineParser::parseNamespaceOrExportBlock(unsigned AddLevels) {
3209 bool ManageWhitesmithsBraces =
3210 AddLevels == 0u && Style.BreakBeforeBraces == FormatStyle::BS_Whitesmiths;
3211
3212 // If we're in Whitesmiths mode, indent the brace if we're not indenting
3213 // the whole block.
3214 if (ManageWhitesmithsBraces)
3215 ++Line->Level;
3216
3217 // Munch the semicolon after the block. This is more common than one would
3218 // think. Putting the semicolon into its own line is very ugly.
3219 parseBlock(/*MustBeDeclaration=*/true, AddLevels, /*MunchSemi=*/true,
3220 /*KeepBraces=*/true, /*IfKind=*/nullptr, ManageWhitesmithsBraces);
3221
3222 addUnwrappedLine(AddLevels > 0 ? LineLevel::Remove : LineLevel::Keep);
3223
3224 if (ManageWhitesmithsBraces)
3225 --Line->Level;
3226}
3227
3228void UnwrappedLineParser::parseNamespace() {
3229 assert(FormatTok->isOneOf(tok::kw_namespace, TT_NamespaceMacro) &&
3230 "'namespace' expected");
3231
3232 const FormatToken &InitialToken = *FormatTok;
3233 nextToken();
3234 if (InitialToken.is(TT_NamespaceMacro)) {
3235 parseParens();
3236 } else {
3237 while (FormatTok->isOneOf(tok::identifier, tok::coloncolon, tok::kw_inline,
3238 tok::l_square, tok::period, tok::l_paren) ||
3239 (Style.isCSharp() && FormatTok->is(tok::kw_union))) {
3240 if (FormatTok->is(tok::l_square))
3241 parseSquare();
3242 else if (FormatTok->is(tok::l_paren))
3243 parseParens();
3244 else
3245 nextToken();
3246 }
3247 }
3248 if (FormatTok->is(tok::l_brace)) {
3249 FormatTok->setFinalizedType(TT_NamespaceLBrace);
3250
3251 if (ShouldBreakBeforeBrace(Style, InitialToken,
3252 Tokens->peekNextToken()->is(tok::r_brace))) {
3253 addUnwrappedLine();
3254 }
3255
3256 unsigned AddLevels =
3257 Style.NamespaceIndentation == FormatStyle::NI_All ||
3258 (Style.NamespaceIndentation == FormatStyle::NI_Inner &&
3259 DeclarationScopeStack.size() > 1)
3260 ? 1u
3261 : 0u;
3262 parseNamespaceOrExportBlock(AddLevels);
3263 }
3264 // FIXME: Add error handling.
3265}
3266
3267void UnwrappedLineParser::parseCppExportBlock() {
3268 parseNamespaceOrExportBlock(/*AddLevels=*/Style.IndentExportBlock ? 1 : 0);
3269}
3270
3271void UnwrappedLineParser::parseNew() {
3272 assert(FormatTok->is(tok::kw_new) && "'new' expected");
3273 nextToken();
3274
3275 if (Style.isCSharp()) {
3276 do {
3277 // Handle constructor invocation, e.g. `new(field: value)`.
3278 if (FormatTok->is(tok::l_paren))
3279 parseParens();
3280
3281 // Handle array initialization syntax, e.g. `new[] {10, 20, 30}`.
3282 if (FormatTok->is(tok::l_brace))
3283 parseBracedList();
3284
3285 if (FormatTok->isOneOf(tok::semi, tok::comma))
3286 return;
3287
3288 nextToken();
3289 } while (!eof());
3290 }
3291
3292 if (!Style.isJava())
3293 return;
3294
3295 // In Java, we can parse everything up to the parens, which aren't optional.
3296 do {
3297 // There should not be a ;, { or } before the new's open paren.
3298 if (FormatTok->isOneOf(tok::semi, tok::l_brace, tok::r_brace))
3299 return;
3300
3301 // Consume the parens.
3302 if (FormatTok->is(tok::l_paren)) {
3303 parseParens();
3304
3305 // If there is a class body of an anonymous class, consume that as child.
3306 if (FormatTok->is(tok::l_brace))
3307 parseChildBlock();
3308 return;
3309 }
3310 nextToken();
3311 } while (!eof());
3312}
3313
3314void UnwrappedLineParser::parseLoopBody(bool KeepBraces, bool WrapRightBrace) {
3315 keepAncestorBraces();
3316
3317 if (isBlockBegin(*FormatTok)) {
3318 FormatTok->setFinalizedType(TT_ControlStatementLBrace);
3319 FormatToken *LeftBrace = FormatTok;
3320 CompoundStatementIndenter Indenter(this, Style, Line->Level);
3321 parseBlock(/*MustBeDeclaration=*/false, /*AddLevels=*/1u,
3322 /*MunchSemi=*/true, KeepBraces);
3323 setPreviousRBraceType(TT_ControlStatementRBrace);
3324 if (!KeepBraces) {
3325 assert(!NestedTooDeep.empty());
3326 if (!NestedTooDeep.back())
3327 markOptionalBraces(LeftBrace);
3328 }
3329 if (WrapRightBrace)
3330 addUnwrappedLine();
3331 } else {
3332 parseUnbracedBody();
3333 }
3334
3335 if (!KeepBraces)
3336 NestedTooDeep.pop_back();
3337}
3338
3339void UnwrappedLineParser::parseForOrWhileLoop(bool HasParens) {
3340 assert((FormatTok->isOneOf(tok::kw_for, tok::kw_while, TT_ForEachMacro) ||
3341 (Style.isVerilog() &&
3342 FormatTok->isOneOf(Keywords.kw_always, Keywords.kw_always_comb,
3343 Keywords.kw_always_ff, Keywords.kw_always_latch,
3344 Keywords.kw_final, Keywords.kw_initial,
3345 Keywords.kw_foreach, Keywords.kw_forever,
3346 Keywords.kw_repeat))) &&
3347 "'for', 'while' or foreach macro expected");
3348 const bool KeepBraces = !Style.RemoveBracesLLVM ||
3349 FormatTok->isNoneOf(tok::kw_for, tok::kw_while);
3350
3351 nextToken();
3352 // JS' for await ( ...
3353 if (Style.isJavaScript() && FormatTok->is(Keywords.kw_await))
3354 nextToken();
3355 if (IsCpp && FormatTok->is(tok::kw_co_await))
3356 nextToken();
3357 if (HasParens && FormatTok->is(tok::l_paren)) {
3358 // The type is only set for Verilog basically because we were afraid to
3359 // change the existing behavior for loops. See the discussion on D121756 for
3360 // details.
3361 if (Style.isVerilog())
3362 FormatTok->setFinalizedType(TT_ConditionLParen);
3363 parseParens();
3364 }
3365
3366 if (Style.isVerilog()) {
3367 // Event control.
3368 parseVerilogSensitivityList();
3369 } else if (Style.AllowShortLoopsOnASingleLine && FormatTok->is(tok::semi) &&
3370 Tokens->getPreviousToken()->is(tok::r_paren)) {
3371 nextToken();
3372 addUnwrappedLine();
3373 return;
3374 }
3375
3376 handleAttributes();
3377 parseLoopBody(KeepBraces, /*WrapRightBrace=*/true);
3378}
3379
3380void UnwrappedLineParser::parseDoWhile() {
3381 assert(FormatTok->is(tok::kw_do) && "'do' expected");
3382 nextToken();
3383
3384 parseLoopBody(/*KeepBraces=*/true, Style.BraceWrapping.BeforeWhile);
3385
3386 // FIXME: Add error handling.
3387 if (FormatTok->isNot(tok::kw_while)) {
3388 addUnwrappedLine();
3389 return;
3390 }
3391
3392 FormatTok->setFinalizedType(TT_DoWhile);
3393
3394 // If in Whitesmiths mode, the line with the while() needs to be indented
3395 // to the same level as the block.
3396 if (Style.BreakBeforeBraces == FormatStyle::BS_Whitesmiths)
3397 ++Line->Level;
3398
3399 nextToken();
3400 parseStructuralElement();
3401}
3402
3403void UnwrappedLineParser::parseLabel(
3404 FormatStyle::IndentGotoLabelStyle IndentGotoLabels) {
3405 const bool IsGotoLabel = FormatTok->is(TT_GotoLabelColon);
3406 nextToken();
3407 unsigned OldLineLevel = Line->Level;
3408
3409 switch (IndentGotoLabels) {
3410 case FormatStyle::IGLS_NoIndent:
3411 Line->Level = 0;
3412 break;
3413 case FormatStyle::IGLS_OuterIndent:
3414 if (Line->Level > 1 || (!Line->InPPDirective && Line->Level > 0))
3415 --Line->Level;
3416 break;
3417 case FormatStyle::IGLS_HalfIndent:
3418 case FormatStyle::IGLS_InnerIndent:
3419 break;
3420 }
3421
3422 if (!IsGotoLabel && !Style.IndentCaseBlocks &&
3423 CommentsBeforeNextToken.empty() && FormatTok->is(tok::l_brace)) {
3424 CompoundStatementIndenter Indenter(this, Line->Level,
3425 Style.BraceWrapping.AfterCaseLabel,
3426 Style.BraceWrapping.IndentBraces);
3427 parseBlock();
3428 if (FormatTok->is(tok::kw_break)) {
3429 if (Style.BraceWrapping.AfterControlStatement ==
3430 FormatStyle::BWACS_Always) {
3431 addUnwrappedLine();
3432 if (!Style.IndentCaseBlocks &&
3433 Style.BreakBeforeBraces == FormatStyle::BS_Whitesmiths) {
3434 ++Line->Level;
3435 }
3436 }
3437 parseStructuralElement();
3438 }
3439 addUnwrappedLine();
3440 } else {
3441 if (FormatTok->is(tok::semi))
3442 nextToken();
3443 addUnwrappedLine();
3444 }
3445 Line->Level = OldLineLevel;
3446 if (FormatTok->isNot(tok::l_brace)) {
3447 parseStructuralElement();
3448 addUnwrappedLine();
3449 }
3450}
3451
3452void UnwrappedLineParser::parseCaseLabel() {
3453 assert(FormatTok->is(tok::kw_case) && "'case' expected");
3454 auto *Case = FormatTok;
3455
3456 // FIXME: fix handling of complex expressions here.
3457 do {
3458 nextToken();
3459 if (FormatTok->is(tok::colon)) {
3460 FormatTok->setFinalizedType(TT_CaseLabelColon);
3461 break;
3462 }
3463 if (Style.isJava() && FormatTok->is(tok::arrow)) {
3464 FormatTok->setFinalizedType(TT_CaseLabelArrow);
3465 Case->setFinalizedType(TT_SwitchExpressionLabel);
3466 break;
3467 }
3468 } while (!eof());
3469 parseLabel();
3470}
3471
3472void UnwrappedLineParser::parseSwitch(bool IsExpr) {
3473 assert(FormatTok->is(tok::kw_switch) && "'switch' expected");
3474 nextToken();
3475 if (FormatTok->is(tok::l_paren))
3476 parseParens();
3477
3478 keepAncestorBraces();
3479
3480 if (FormatTok->is(tok::l_brace)) {
3481 CompoundStatementIndenter Indenter(this, Style, Line->Level);
3482 FormatTok->setFinalizedType(IsExpr ? TT_SwitchExpressionLBrace
3483 : TT_ControlStatementLBrace);
3484 if (IsExpr)
3485 parseChildBlock();
3486 else
3487 parseBlock();
3488 setPreviousRBraceType(TT_ControlStatementRBrace);
3489 if (!IsExpr)
3490 addUnwrappedLine();
3491 } else {
3492 addUnwrappedLine();
3493 ++Line->Level;
3494 parseStructuralElement();
3495 --Line->Level;
3496 }
3497
3498 if (Style.RemoveBracesLLVM)
3499 NestedTooDeep.pop_back();
3500}
3501
3502void UnwrappedLineParser::parseAccessSpecifier() {
3503 nextToken();
3504 // Understand Qt's slots.
3505 if (FormatTok->isOneOf(Keywords.kw_slots, Keywords.kw_qslots))
3506 nextToken();
3507 // Otherwise, we don't know what it is, and we'd better keep the next token.
3508 if (FormatTok->is(tok::colon))
3509 nextToken();
3510 addUnwrappedLine();
3511}
3512
3513/// Parses a requires, decides if it is a clause or an expression.
3514/// \pre The current token has to be the requires keyword.
3515/// \returns true if it parsed a clause.
3516bool UnwrappedLineParser::parseRequires(bool SeenEqual) {
3517 assert(FormatTok->is(tok::kw_requires) && "'requires' expected");
3518
3519 // We try to guess if it is a requires clause, or a requires expression. For
3520 // that we first check the next token.
3521 switch (Tokens->peekNextToken(/*SkipComment=*/true)->Tok.getKind()) {
3522 case tok::l_brace:
3523 // This can only be an expression, never a clause.
3524 parseRequiresExpression();
3525 return false;
3526 case tok::l_paren:
3527 // Clauses and expression can start with a paren, it's unclear what we have.
3528 break;
3529 default:
3530 // All other tokens can only be a clause.
3531 parseRequiresClause();
3532 return true;
3533 }
3534
3535 // Looking forward we would have to decide if there are function declaration
3536 // like arguments to the requires expression:
3537 // requires (T t) {
3538 // Or there is a constraint expression for the requires clause:
3539 // requires (C<T> && ...
3540
3541 // But first let's look behind.
3542 auto *PreviousNonComment = FormatTok->getPreviousNonComment();
3543
3544 if (!PreviousNonComment ||
3545 PreviousNonComment->is(TT_RequiresExpressionLBrace)) {
3546 // If there is no token, or an expression left brace, we are a requires
3547 // clause within a requires expression.
3548 parseRequiresClause();
3549 return true;
3550 }
3551
3552 switch (PreviousNonComment->Tok.getKind()) {
3553 case tok::greater:
3554 case tok::r_paren:
3555 case tok::kw_noexcept:
3556 case tok::kw_const:
3557 case tok::star:
3558 case tok::amp:
3559 // This is a requires clause.
3560 parseRequiresClause();
3561 return true;
3562 case tok::ampamp: {
3563 // This can be either:
3564 // if (... && requires (T t) ...)
3565 // Or
3566 // void member(...) && requires (C<T> ...
3567 // We check the one token before that for a const:
3568 // void member(...) const && requires (C<T> ...
3569 auto PrevPrev = PreviousNonComment->getPreviousNonComment();
3570 if ((PrevPrev && PrevPrev->is(tok::kw_const)) || !SeenEqual) {
3571 parseRequiresClause();
3572 return true;
3573 }
3574 break;
3575 }
3576 default:
3577 if (PreviousNonComment->isTypeOrIdentifier(LangOpts)) {
3578 // This is a requires clause.
3579 parseRequiresClause();
3580 return true;
3581 }
3582 // It's an expression.
3583 parseRequiresExpression();
3584 return false;
3585 }
3586
3587 // Now we look forward and try to check if the paren content is a parameter
3588 // list. The parameters can be cv-qualified and contain references or
3589 // pointers.
3590 // So we want basically to check for TYPE NAME, but TYPE can contain all kinds
3591 // of stuff: typename, const, *, &, &&, ::, identifiers.
3592
3593 unsigned StoredPosition = Tokens->getPosition();
3594 FormatToken *NextToken = Tokens->getNextToken();
3595 int Lookahead = 0;
3596 auto PeekNext = [&Lookahead, &NextToken, this] {
3597 ++Lookahead;
3598 NextToken = Tokens->getNextToken();
3599 };
3600
3601 bool FoundType = false;
3602 bool LastWasColonColon = false;
3603 int OpenAngles = 0;
3604
3605 for (; Lookahead < 50; PeekNext()) {
3606 switch (NextToken->Tok.getKind()) {
3607 case tok::kw_volatile:
3608 case tok::kw_const:
3609 case tok::comma:
3610 if (OpenAngles == 0) {
3611 FormatTok = Tokens->setPosition(StoredPosition);
3612 parseRequiresExpression();
3613 return false;
3614 }
3615 break;
3616 case tok::eof:
3617 // Break out of the loop.
3618 Lookahead = 50;
3619 break;
3620 case tok::coloncolon:
3621 LastWasColonColon = true;
3622 break;
3623 case tok::kw_decltype:
3624 case tok::identifier:
3625 if (FoundType && !LastWasColonColon && OpenAngles == 0) {
3626 FormatTok = Tokens->setPosition(StoredPosition);
3627 parseRequiresExpression();
3628 return false;
3629 }
3630 FoundType = true;
3631 LastWasColonColon = false;
3632 break;
3633 case tok::less:
3634 ++OpenAngles;
3635 break;
3636 case tok::greater:
3637 --OpenAngles;
3638 break;
3639 default:
3640 if (NextToken->isTypeName(LangOpts)) {
3641 FormatTok = Tokens->setPosition(StoredPosition);
3642 parseRequiresExpression();
3643 return false;
3644 }
3645 break;
3646 }
3647 }
3648 // This seems to be a complicated expression, just assume it's a clause.
3649 FormatTok = Tokens->setPosition(StoredPosition);
3650 parseRequiresClause();
3651 return true;
3652}
3653
3654/// Parses a requires clause.
3655/// \sa parseRequiresExpression
3656///
3657/// Returns if it either has finished parsing the clause, or it detects, that
3658/// the clause is incorrect.
3659void UnwrappedLineParser::parseRequiresClause() {
3660 assert(FormatTok->is(tok::kw_requires) && "'requires' expected");
3661
3662 // If there is no previous token, we are within a requires expression,
3663 // otherwise we will always have the template or function declaration in front
3664 // of it.
3665 bool InRequiresExpression =
3666 !FormatTok->Previous ||
3667 FormatTok->Previous->is(TT_RequiresExpressionLBrace);
3668
3669 FormatTok->setFinalizedType(InRequiresExpression
3670 ? TT_RequiresClauseInARequiresExpression
3671 : TT_RequiresClause);
3672 nextToken();
3673
3674 // NOTE: parseConstraintExpression is only ever called from this function.
3675 // It could be inlined into here.
3676 parseConstraintExpression();
3677
3678 if (!InRequiresExpression && FormatTok->Previous)
3679 FormatTok->Previous->ClosesRequiresClause = true;
3680}
3681
3682/// Parses a requires expression.
3683/// \sa parseRequiresClause
3684///
3685/// Returns if it either has finished parsing the expression, or it detects,
3686/// that the expression is incorrect.
3687void UnwrappedLineParser::parseRequiresExpression() {
3688 assert(FormatTok->is(tok::kw_requires) && "'requires' expected");
3689
3690 FormatTok->setFinalizedType(TT_RequiresExpression);
3691 nextToken();
3692
3693 if (FormatTok->is(tok::l_paren)) {
3694 FormatTok->setFinalizedType(TT_RequiresExpressionLParen);
3695 parseParens();
3696 }
3697
3698 if (FormatTok->is(tok::l_brace)) {
3699 FormatTok->setFinalizedType(TT_RequiresExpressionLBrace);
3700 parseChildBlock();
3701 }
3702}
3703
3704/// Parses a constraint expression.
3705///
3706/// This is the body of a requires clause. It returns, when the parsing is
3707/// complete, or the expression is incorrect.
3708void UnwrappedLineParser::parseConstraintExpression() {
3709 // The special handling for lambdas is needed since tryToParseLambda() eats a
3710 // token and if a requires expression is the last part of a requires clause
3711 // and followed by an attribute like [[nodiscard]] the ClosesRequiresClause is
3712 // not set on the correct token. Thus we need to be aware if we even expect a
3713 // lambda to be possible.
3714 // template <typename T> requires requires { ... } [[nodiscard]] ...;
3715 bool LambdaNextTimeAllowed = true;
3716
3717 // Within lambda declarations, it is permitted to put a requires clause after
3718 // its template parameter list, which would place the requires clause right
3719 // before the parentheses of the parameters of the lambda declaration. Thus,
3720 // we track if we expect to see grouping parentheses at all.
3721 // Without this check, `requires foo<T> (T t)` in the below example would be
3722 // seen as the whole requires clause, accidentally eating the parameters of
3723 // the lambda.
3724 // [&]<typename T> requires foo<T> (T t) { ... };
3725 bool TopLevelParensAllowed = true;
3726
3727 do {
3728 bool LambdaThisTimeAllowed = std::exchange(LambdaNextTimeAllowed, false);
3729
3730 switch (FormatTok->Tok.getKind()) {
3731 case tok::kw_requires:
3732 parseRequiresExpression();
3733 break;
3734
3735 case tok::l_paren:
3736 if (!TopLevelParensAllowed)
3737 return;
3738 parseParens(/*AmpAmpTokenType=*/TT_BinaryOperator);
3739 TopLevelParensAllowed = false;
3740 break;
3741
3742 case tok::l_square:
3743 if (!LambdaThisTimeAllowed || !tryToParseLambda())
3744 return;
3745 break;
3746
3747 case tok::kw_const:
3748 case tok::semi:
3749 case tok::kw_class:
3750 case tok::kw_struct:
3751 case tok::kw_union:
3752 return;
3753
3754 case tok::l_brace:
3755 // Potential function body.
3756 return;
3757
3758 case tok::ampamp:
3759 case tok::pipepipe:
3760 FormatTok->setFinalizedType(TT_BinaryOperator);
3761 nextToken();
3762 LambdaNextTimeAllowed = true;
3763 TopLevelParensAllowed = true;
3764 break;
3765
3766 case tok::comma:
3767 case tok::comment:
3768 LambdaNextTimeAllowed = LambdaThisTimeAllowed;
3769 nextToken();
3770 break;
3771
3772 case tok::kw_sizeof:
3773 case tok::greater:
3774 case tok::greaterequal:
3775 case tok::greatergreater:
3776 case tok::less:
3777 case tok::lessequal:
3778 case tok::lessless:
3779 case tok::equalequal:
3780 case tok::exclaim:
3781 case tok::exclaimequal:
3782 case tok::plus:
3783 case tok::minus:
3784 case tok::star:
3785 case tok::slash:
3786 LambdaNextTimeAllowed = true;
3787 TopLevelParensAllowed = true;
3788 // Just eat them.
3789 nextToken();
3790 break;
3791
3792 case tok::numeric_constant:
3793 case tok::coloncolon:
3794 case tok::kw_true:
3795 case tok::kw_false:
3796 TopLevelParensAllowed = false;
3797 // Just eat them.
3798 nextToken();
3799 break;
3800
3801 case tok::kw_static_cast:
3802 case tok::kw_const_cast:
3803 case tok::kw_reinterpret_cast:
3804 case tok::kw_dynamic_cast:
3805 nextToken();
3806 if (FormatTok->isNot(tok::less))
3807 return;
3808
3809 nextToken();
3810 parseBracedList(/*IsAngleBracket=*/true);
3811 break;
3812
3813 default:
3814 if (!FormatTok->Tok.getIdentifierInfo()) {
3815 // Identifiers are part of the default case, we check for more then
3816 // tok::identifier to handle builtin type traits.
3817 return;
3818 }
3819
3820 // We need to differentiate identifiers for a template deduction guide,
3821 // variables, or function return types (the constraint expression has
3822 // ended before that), and basically all other cases. But it's easier to
3823 // check the other way around.
3824 assert(FormatTok->Previous);
3825 switch (FormatTok->Previous->Tok.getKind()) {
3826 case tok::coloncolon: // Nested identifier.
3827 case tok::ampamp: // Start of a function or variable for the
3828 case tok::pipepipe: // constraint expression. (binary)
3829 case tok::exclaim: // The same as above, but unary.
3830 case tok::kw_requires: // Initial identifier of a requires clause.
3831 case tok::equal: // Initial identifier of a concept declaration.
3832 break;
3833 default:
3834 return;
3835 }
3836
3837 // Read identifier with optional template declaration.
3838 nextToken();
3839 if (FormatTok->is(tok::less)) {
3840 nextToken();
3841 parseBracedList(/*IsAngleBracket=*/true);
3842 }
3843 TopLevelParensAllowed = false;
3844 break;
3845 }
3846 } while (!eof());
3847}
3848
3849bool UnwrappedLineParser::parseEnum() {
3850 const FormatToken &InitialToken = *FormatTok;
3851
3852 // Won't be 'enum' for NS_ENUMs.
3853 if (FormatTok->is(tok::kw_enum))
3854 nextToken();
3855
3856 // In TypeScript, "enum" can also be used as property name, e.g. in interface
3857 // declarations. An "enum" keyword followed by a colon would be a syntax
3858 // error and thus assume it is just an identifier.
3859 if (Style.isJavaScript() && FormatTok->isOneOf(tok::colon, tok::question))
3860 return false;
3861
3862 // In protobuf, "enum" can be used as a field name.
3863 if (Style.Language == FormatStyle::LK_Proto && FormatTok->is(tok::equal))
3864 return false;
3865
3866 if (IsCpp) {
3867 // Eat up enum class ...
3868 if (FormatTok->isOneOf(tok::kw_class, tok::kw_struct))
3869 nextToken();
3870 while (FormatTok->is(tok::l_square))
3871 if (!handleCppAttributes())
3872 return false;
3873 }
3874
3875 while (FormatTok->Tok.getIdentifierInfo() ||
3876 FormatTok->isOneOf(tok::colon, tok::coloncolon, tok::less,
3877 tok::greater, tok::comma, tok::question,
3878 tok::l_square)) {
3879 if (FormatTok->is(tok::colon))
3880 FormatTok->setFinalizedType(TT_EnumUnderlyingTypeColon);
3881 if (Style.isVerilog()) {
3882 FormatTok->setFinalizedType(TT_VerilogDimensionedTypeName);
3883 nextToken();
3884 // In Verilog the base type can have dimensions.
3885 while (FormatTok->is(tok::l_square))
3886 parseSquare();
3887 } else {
3888 nextToken();
3889 }
3890 // We can have macros or attributes in between 'enum' and the enum name.
3891 if (FormatTok->is(tok::l_paren))
3892 parseParens();
3893 if (FormatTok->is(tok::identifier)) {
3894 nextToken();
3895 // If there are two identifiers in a row, this is likely an elaborate
3896 // return type. In Java, this can be "implements", etc.
3897 if (IsCpp && FormatTok->is(tok::identifier))
3898 return false;
3899 }
3900 }
3901
3902 // Just a declaration or something is wrong.
3903 if (FormatTok->isNot(tok::l_brace))
3904 return true;
3905 FormatTok->setFinalizedType(TT_EnumLBrace);
3906 FormatTok->setBlockKind(BK_Block);
3907
3908 if (Style.isJava()) {
3909 // Java enums are different.
3910 parseJavaEnumBody();
3911 return true;
3912 }
3913 if (Style.Language == FormatStyle::LK_Proto) {
3914 parseBlock(/*MustBeDeclaration=*/true);
3915 return true;
3916 }
3917
3918 const bool ManageWhitesmithsBraces =
3919 Style.BreakBeforeBraces == FormatStyle::BS_Whitesmiths;
3920
3921 if (!Style.AllowShortEnumsOnASingleLine &&
3922 ShouldBreakBeforeBrace(Style, InitialToken,
3923 Tokens->peekNextToken()->is(tok::r_brace))) {
3924 addUnwrappedLine();
3925
3926 // If we're in Whitesmiths mode, indent the brace if we're not indenting
3927 // the whole block.
3928 if (ManageWhitesmithsBraces)
3929 ++Line->Level;
3930 }
3931 // Parse enum body.
3932 nextToken();
3933 if (!Style.AllowShortEnumsOnASingleLine) {
3934 addUnwrappedLine();
3935 if (!ManageWhitesmithsBraces)
3936 ++Line->Level;
3937 }
3938 const auto OpeningLineIndex = CurrentLines->empty()
3939 ? UnwrappedLine::kInvalidIndex
3940 : CurrentLines->size() - 1;
3941 bool HasError = !parseBracedList(/*IsAngleBracket=*/false, /*IsEnum=*/true);
3942 if (!Style.AllowShortEnumsOnASingleLine && !ManageWhitesmithsBraces)
3943 --Line->Level;
3944 if (HasError) {
3945 if (FormatTok->is(tok::semi))
3946 nextToken();
3947 addUnwrappedLine();
3948 }
3949 setPreviousRBraceType(TT_EnumRBrace);
3950 if (ManageWhitesmithsBraces)
3951 Line->MatchingOpeningBlockLineIndex = OpeningLineIndex;
3952 return true;
3953
3954 // There is no addUnwrappedLine() here so that we fall through to parsing a
3955 // structural element afterwards. Thus, in "enum A {} n, m;",
3956 // "} n, m;" will end up in one unwrapped line.
3957}
3958
3959bool UnwrappedLineParser::parseStructLike() {
3960 // parseRecord falls through and does not yet add an unwrapped line as a
3961 // record declaration or definition can start a structural element.
3962 parseRecord();
3963 // This does not apply to Java, JavaScript and C#.
3964 if (Style.isJava() || Style.isJavaScript() || Style.isCSharp()) {
3965 if (FormatTok->is(tok::semi))
3966 nextToken();
3967 addUnwrappedLine();
3968 return true;
3969 }
3970 return false;
3971}
3972
3973namespace {
3974// A class used to set and restore the Token position when peeking
3975// ahead in the token source.
3976class ScopedTokenPosition {
3977 unsigned StoredPosition;
3978 FormatTokenSource *Tokens;
3979
3980public:
3981 ScopedTokenPosition(FormatTokenSource *Tokens) : Tokens(Tokens) {
3982 assert(Tokens && "Tokens expected to not be null");
3983 StoredPosition = Tokens->getPosition();
3984 }
3985
3986 ~ScopedTokenPosition() { Tokens->setPosition(StoredPosition); }
3987};
3988} // namespace
3989
3990// Look to see if we have [[ by looking ahead, if
3991// its not then rewind to the original position.
3992bool UnwrappedLineParser::tryToParseSimpleAttribute() {
3993 ScopedTokenPosition AutoPosition(Tokens);
3994 FormatToken *Tok = Tokens->getNextToken();
3995 // We already read the first [ check for the second.
3996 if (Tok->isNot(tok::l_square))
3997 return false;
3998 // Double check that the attribute is just something
3999 // fairly simple.
4000 while (Tok->isNot(tok::eof)) {
4001 if (Tok->is(tok::r_square))
4002 break;
4003 Tok = Tokens->getNextToken();
4004 }
4005 if (Tok->is(tok::eof))
4006 return false;
4007 Tok = Tokens->getNextToken();
4008 if (Tok->isNot(tok::r_square))
4009 return false;
4010 Tok = Tokens->getNextToken();
4011 if (Tok->is(tok::semi))
4012 return false;
4013 return true;
4014}
4015
4016void UnwrappedLineParser::parseJavaEnumBody() {
4017 assert(FormatTok->is(tok::l_brace));
4018 const FormatToken *OpeningBrace = FormatTok;
4019
4020 // Determine whether the enum is simple, i.e. does not have a semicolon or
4021 // constants with class bodies. Simple enums can be formatted like braced
4022 // lists, contracted to a single line, etc.
4023 unsigned StoredPosition = Tokens->getPosition();
4024 bool IsSimple = true;
4025 FormatToken *Tok = Tokens->getNextToken();
4026 while (Tok->isNot(tok::eof)) {
4027 if (Tok->is(tok::r_brace))
4028 break;
4029 if (Tok->isOneOf(tok::l_brace, tok::semi)) {
4030 IsSimple = false;
4031 break;
4032 }
4033 // FIXME: This will also mark enums with braces in the arguments to enum
4034 // constants as "not simple". This is probably fine in practice, though.
4035 Tok = Tokens->getNextToken();
4036 }
4037 FormatTok = Tokens->setPosition(StoredPosition);
4038
4039 if (IsSimple) {
4040 nextToken();
4041 parseBracedList();
4042 addUnwrappedLine();
4043 return;
4044 }
4045
4046 // Parse the body of a more complex enum.
4047 // First add a line for everything up to the "{".
4048 nextToken();
4049 addUnwrappedLine();
4050 ++Line->Level;
4051
4052 // Parse the enum constants.
4053 while (!eof()) {
4054 if (FormatTok->is(tok::l_brace)) {
4055 // Parse the constant's class body.
4056 parseBlock(/*MustBeDeclaration=*/true, /*AddLevels=*/1u,
4057 /*MunchSemi=*/false);
4058 } else if (FormatTok->is(tok::l_paren)) {
4059 parseParens();
4060 } else if (FormatTok->is(tok::comma)) {
4061 nextToken();
4062 addUnwrappedLine();
4063 } else if (FormatTok->is(tok::semi)) {
4064 nextToken();
4065 addUnwrappedLine();
4066 break;
4067 } else if (FormatTok->is(tok::r_brace)) {
4068 addUnwrappedLine();
4069 break;
4070 } else {
4071 nextToken();
4072 }
4073 }
4074
4075 // Parse the class body after the enum's ";" if any.
4076 parseLevel(OpeningBrace);
4077 nextToken();
4078 --Line->Level;
4079 addUnwrappedLine();
4080}
4081
4082void UnwrappedLineParser::parseRecord(bool ParseAsExpr, bool IsJavaRecord) {
4083 assert(!IsJavaRecord || FormatTok->is(Keywords.kw_record));
4084 const FormatToken &InitialToken = *FormatTok;
4085 nextToken();
4086
4087 FormatToken *ClassName =
4088 IsJavaRecord && FormatTok->is(tok::identifier) ? FormatTok : nullptr;
4089 bool IsDerived = false;
4090 auto IsNonMacroIdentifier = [](const FormatToken *Tok) {
4091 return Tok->is(tok::identifier) && Tok->TokenText != Tok->TokenText.upper();
4092 };
4093 // JavaScript/TypeScript supports anonymous classes like:
4094 // a = class extends foo { }
4095 bool JSPastExtendsOrImplements = false;
4096 // The actual identifier can be a nested name specifier, and in macros
4097 // it is often token-pasted.
4098 // An [[attribute]] can be before the identifier.
4099 while (FormatTok->isOneOf(tok::identifier, tok::coloncolon, tok::hashhash,
4100 tok::kw_alignas, tok::l_square) ||
4101 FormatTok->isAttribute() ||
4102 ((Style.isJava() || Style.isJavaScript()) &&
4103 FormatTok->isOneOf(tok::period, tok::comma))) {
4104 if (Style.isJavaScript() &&
4105 FormatTok->isOneOf(Keywords.kw_extends, Keywords.kw_implements)) {
4106 JSPastExtendsOrImplements = true;
4107 // JavaScript/TypeScript supports inline object types in
4108 // extends/implements positions:
4109 // class Foo implements {bar: number} { }
4110 nextToken();
4111 if (FormatTok->is(tok::l_brace)) {
4112 tryToParseBracedList();
4113 continue;
4114 }
4115 }
4116 if (FormatTok->is(tok::l_square) && handleCppAttributes())
4117 continue;
4118 auto *Previous = FormatTok;
4119 nextToken();
4120 switch (FormatTok->Tok.getKind()) {
4121 case tok::l_paren:
4122 // We can have macros in between 'class' and the class name.
4123 if (IsJavaRecord || !IsNonMacroIdentifier(Previous) ||
4124 // e.g. `struct macro(a) S { int i; };`
4125 Previous->Previous == &InitialToken) {
4126 parseParens();
4127 }
4128 break;
4129 case tok::coloncolon:
4130 case tok::hashhash:
4131 break;
4132 default:
4133 if (JSPastExtendsOrImplements || ClassName ||
4134 Previous->isNot(tok::identifier) || Previous->is(TT_AttributeMacro)) {
4135 break;
4136 }
4137 if (const auto Text = Previous->TokenText;
4138 Text.size() == 1 || Text != Text.upper()) {
4139 ClassName = Previous;
4140 }
4141 }
4142 }
4143
4144 auto IsListInitialization = [&] {
4145 if (!ClassName || IsDerived || JSPastExtendsOrImplements)
4146 return false;
4147 assert(FormatTok->is(tok::l_brace));
4148 const auto *Prev = FormatTok->getPreviousNonComment();
4149 assert(Prev);
4150 return Prev != ClassName && Prev->is(tok::identifier) &&
4151 Prev->isNot(Keywords.kw_final) && tryToParseBracedList();
4152 };
4153
4154 if (FormatTok->isOneOf(tok::colon, tok::less)) {
4155 int AngleNestingLevel = 0;
4156 do {
4157 if (FormatTok->is(tok::less))
4158 ++AngleNestingLevel;
4159 else if (FormatTok->is(tok::greater))
4160 --AngleNestingLevel;
4161
4162 if (AngleNestingLevel == 0) {
4163 if (FormatTok->is(tok::colon)) {
4164 IsDerived = true;
4165 } else if (!IsDerived && FormatTok->is(tok::identifier) &&
4166 FormatTok->Previous->is(tok::coloncolon)) {
4167 ClassName = FormatTok;
4168 } else if (FormatTok->is(tok::l_paren) &&
4169 IsNonMacroIdentifier(FormatTok->Previous)) {
4170 break;
4171 }
4172 }
4173 if (FormatTok->is(tok::l_brace)) {
4174 if (AngleNestingLevel == 0 && IsListInitialization())
4175 return;
4176 calculateBraceTypes(/*ExpectClassBody=*/true);
4177 if (!tryToParseBracedList())
4178 break;
4179 }
4180 if (FormatTok->is(tok::l_square)) {
4181 FormatToken *Previous = FormatTok->Previous;
4182 if (!Previous || (Previous->isNot(tok::r_paren) &&
4183 !Previous->isTypeOrIdentifier(LangOpts))) {
4184 // Don't try parsing a lambda if we had a closing parenthesis before,
4185 // it was probably a pointer to an array: int (*)[].
4186 if (!tryToParseLambda())
4187 continue;
4188 } else {
4189 parseSquare();
4190 continue;
4191 }
4192 }
4193 if (FormatTok->is(tok::semi))
4194 return;
4195 if (Style.isCSharp() && FormatTok->is(Keywords.kw_where)) {
4196 addUnwrappedLine();
4197 nextToken();
4198 parseCSharpGenericTypeConstraint();
4199 break;
4200 }
4201 nextToken();
4202 } while (!eof());
4203 }
4204
4205 auto GetBraceTypes =
4206 [](const FormatToken &RecordTok) -> std::pair<TokenType, TokenType> {
4207 switch (RecordTok.Tok.getKind()) {
4208 case tok::kw_class:
4209 return {TT_ClassLBrace, TT_ClassRBrace};
4210 case tok::kw_struct:
4211 return {TT_StructLBrace, TT_StructRBrace};
4212 case tok::kw_union:
4213 return {TT_UnionLBrace, TT_UnionRBrace};
4214 default:
4215 // Useful for e.g. interface.
4216 return {TT_RecordLBrace, TT_RecordRBrace};
4217 }
4218 };
4219 if (FormatTok->is(tok::l_brace)) {
4220 if (IsListInitialization())
4221 return;
4222 if (ClassName)
4223 ClassName->setFinalizedType(TT_ClassHeadName);
4224 auto [OpenBraceType, ClosingBraceType] = GetBraceTypes(InitialToken);
4225 FormatTok->setFinalizedType(OpenBraceType);
4226 if (ParseAsExpr) {
4227 parseChildBlock();
4228 } else {
4229 if (ShouldBreakBeforeBrace(Style, InitialToken,
4230 Tokens->peekNextToken()->is(tok::r_brace),
4231 IsJavaRecord)) {
4232 addUnwrappedLine();
4233 }
4234
4235 unsigned AddLevels = Style.IndentAccessModifiers ? 2u : 1u;
4236 parseBlock(/*MustBeDeclaration=*/true, AddLevels, /*MunchSemi=*/false);
4237 }
4238 setPreviousRBraceType(ClosingBraceType);
4239 }
4240 // There is no addUnwrappedLine() here so that we fall through to parsing a
4241 // structural element afterwards. Thus, in "class A {} n, m;",
4242 // "} n, m;" will end up in one unwrapped line.
4243}
4244
4245void UnwrappedLineParser::parseObjCMethod() {
4246 assert(FormatTok->isOneOf(tok::l_paren, tok::identifier) &&
4247 "'(' or identifier expected.");
4248 do {
4249 if (FormatTok->is(tok::semi)) {
4250 nextToken();
4251 addUnwrappedLine();
4252 return;
4253 } else if (FormatTok->is(tok::l_brace)) {
4254 if (Style.BraceWrapping.AfterFunction)
4255 addUnwrappedLine();
4256 parseBlock();
4257 addUnwrappedLine();
4258 return;
4259 } else {
4260 nextToken();
4261 }
4262 } while (!eof());
4263}
4264
4265void UnwrappedLineParser::parseObjCProtocolList() {
4266 assert(FormatTok->is(tok::less) && "'<' expected.");
4267 do {
4268 nextToken();
4269 // Early exit in case someone forgot a close angle.
4270 if (FormatTok->isOneOf(tok::semi, tok::l_brace, tok::objc_end))
4271 return;
4272 } while (!eof() && FormatTok->isNot(tok::greater));
4273 nextToken(); // Skip '>'.
4274}
4275
4276void UnwrappedLineParser::parseObjCUntilAtEnd() {
4277 do {
4278 if (FormatTok->is(tok::objc_end)) {
4279 nextToken();
4280 addUnwrappedLine();
4281 break;
4282 }
4283 if (FormatTok->is(tok::l_brace)) {
4284 parseBlock();
4285 // In ObjC interfaces, nothing should be following the "}".
4286 addUnwrappedLine();
4287 } else if (FormatTok->is(tok::r_brace)) {
4288 // Ignore stray "}". parseStructuralElement doesn't consume them.
4289 nextToken();
4290 addUnwrappedLine();
4291 } else if (FormatTok->isOneOf(tok::minus, tok::plus)) {
4292 nextToken();
4293 if (FormatTok->isOneOf(tok::l_paren, tok::identifier))
4294 parseObjCMethod();
4295 } else {
4296 parseStructuralElement();
4297 }
4298 } while (!eof());
4299}
4300
4301void UnwrappedLineParser::parseObjCInterfaceOrImplementation() {
4302 assert(FormatTok->isOneOf(tok::objc_interface, tok::objc_implementation));
4303 nextToken();
4304 nextToken(); // interface name
4305
4306 // @interface can be followed by a lightweight generic
4307 // specialization list, then either a base class or a category.
4308 if (FormatTok->is(tok::less))
4309 parseObjCLightweightGenerics();
4310 if (FormatTok->is(tok::colon)) {
4311 nextToken();
4312 nextToken(); // base class name
4313 // The base class can also have lightweight generics applied to it.
4314 if (FormatTok->is(tok::less))
4315 parseObjCLightweightGenerics();
4316 } else if (FormatTok->is(tok::l_paren)) {
4317 // Skip category, if present.
4318 parseParens();
4319 }
4320
4321 if (FormatTok->is(tok::less))
4322 parseObjCProtocolList();
4323
4324 if (FormatTok->is(tok::l_brace)) {
4325 if (Style.BraceWrapping.AfterObjCDeclaration)
4326 addUnwrappedLine();
4327 parseBlock(/*MustBeDeclaration=*/true);
4328 }
4329
4330 // With instance variables, this puts '}' on its own line. Without instance
4331 // variables, this ends the @interface line.
4332 addUnwrappedLine();
4333
4334 parseObjCUntilAtEnd();
4335}
4336
4337void UnwrappedLineParser::parseObjCLightweightGenerics() {
4338 assert(FormatTok->is(tok::less));
4339 // Unlike protocol lists, generic parameterizations support
4340 // nested angles:
4341 //
4342 // @interface Foo<ValueType : id <NSCopying, NSSecureCoding>> :
4343 // NSObject <NSCopying, NSSecureCoding>
4344 //
4345 // so we need to count how many open angles we have left.
4346 unsigned NumOpenAngles = 1;
4347 do {
4348 nextToken();
4349 // Early exit in case someone forgot a close angle.
4350 if (FormatTok->isOneOf(tok::semi, tok::l_brace, tok::objc_end))
4351 break;
4352 if (FormatTok->is(tok::less)) {
4353 ++NumOpenAngles;
4354 } else if (FormatTok->is(tok::greater)) {
4355 assert(NumOpenAngles > 0 && "'>' makes NumOpenAngles negative");
4356 --NumOpenAngles;
4357 }
4358 } while (!eof() && NumOpenAngles != 0);
4359 nextToken(); // Skip '>'.
4360}
4361
4362// Returns true for the declaration/definition form of @protocol,
4363// false for the expression form.
4364bool UnwrappedLineParser::parseObjCProtocol() {
4365 assert(FormatTok->is(tok::objc_protocol));
4366 nextToken();
4367
4368 if (FormatTok->is(tok::l_paren)) {
4369 // The expression form of @protocol, e.g. "Protocol* p = @protocol(foo);".
4370 return false;
4371 }
4372
4373 // The definition/declaration form,
4374 // @protocol Foo
4375 // - (int)someMethod;
4376 // @end
4377
4378 nextToken(); // protocol name
4379
4380 if (FormatTok->is(tok::less))
4381 parseObjCProtocolList();
4382
4383 // Check for protocol declaration.
4384 if (FormatTok->is(tok::semi)) {
4385 nextToken();
4386 addUnwrappedLine();
4387 return true;
4388 }
4389
4390 addUnwrappedLine();
4391 parseObjCUntilAtEnd();
4392 return true;
4393}
4394
4395void UnwrappedLineParser::parseJavaScriptEs6ImportExport() {
4396 bool IsImport = FormatTok->is(Keywords.kw_import);
4397 assert(IsImport || FormatTok->is(tok::kw_export));
4398 nextToken();
4399
4400 // Consume the "default" in "export default class/function".
4401 if (FormatTok->is(tok::kw_default))
4402 nextToken();
4403
4404 // Consume "async function", "function" and "default function", so that these
4405 // get parsed as free-standing JS functions, i.e. do not require a trailing
4406 // semicolon.
4407 if (FormatTok->is(Keywords.kw_async))
4408 nextToken();
4409 if (FormatTok->is(Keywords.kw_function)) {
4410 nextToken();
4411 return;
4412 }
4413
4414 // For imports, `export *`, `export {...}`, consume the rest of the line up
4415 // to the terminating `;`. For everything else, just return and continue
4416 // parsing the structural element, i.e. the declaration or expression for
4417 // `export default`.
4418 if (!IsImport && FormatTok->isNoneOf(tok::l_brace, tok::star) &&
4419 !FormatTok->isStringLiteral() &&
4420 !(FormatTok->is(Keywords.kw_type) &&
4421 Tokens->peekNextToken()->isOneOf(tok::l_brace, tok::star))) {
4422 return;
4423 }
4424
4425 while (!eof()) {
4426 if (FormatTok->is(tok::semi))
4427 return;
4428 if (Line->Tokens.empty()) {
4429 // Common issue: Automatic Semicolon Insertion wrapped the line, so the
4430 // import statement should terminate.
4431 return;
4432 }
4433 if (FormatTok->is(tok::l_brace)) {
4434 FormatTok->setBlockKind(BK_Block);
4435 nextToken();
4436 parseBracedList();
4437 } else {
4438 nextToken();
4439 }
4440 }
4441}
4442
4443void UnwrappedLineParser::parseStatementMacro() {
4444 nextToken();
4445 if (FormatTok->is(tok::l_paren))
4446 parseParens();
4447 if (FormatTok->is(tok::semi))
4448 nextToken();
4449 addUnwrappedLine();
4450}
4451
4452void UnwrappedLineParser::parseVerilogHierarchyIdentifier() {
4453 // consume things like a::`b.c[d:e] or a::*
4454 while (true) {
4455 if (FormatTok->isOneOf(tok::star, tok::period, tok::periodstar,
4456 tok::coloncolon, tok::hash) ||
4457 Keywords.isVerilogIdentifier(*FormatTok)) {
4458 nextToken();
4459 } else if (FormatTok->is(tok::l_square)) {
4460 parseSquare();
4461 } else {
4462 break;
4463 }
4464 }
4465}
4466
4467void UnwrappedLineParser::parseVerilogSensitivityList() {
4468 if (FormatTok->isNot(tok::at))
4469 return;
4470 nextToken();
4471 // A block event expression has 2 at signs.
4472 if (FormatTok->is(tok::at))
4473 nextToken();
4474 switch (FormatTok->Tok.getKind()) {
4475 case tok::star:
4476 nextToken();
4477 break;
4478 case tok::l_paren:
4479 parseParens();
4480 break;
4481 default:
4482 parseVerilogHierarchyIdentifier();
4483 break;
4484 }
4485}
4486
4487unsigned UnwrappedLineParser::parseVerilogHierarchyHeader() {
4488 unsigned AddLevels = 0;
4489
4490 if (FormatTok->is(Keywords.kw_clocking)) {
4491 nextToken();
4492 if (Keywords.isVerilogIdentifier(*FormatTok))
4493 nextToken();
4494 parseVerilogSensitivityList();
4495 if (FormatTok->is(tok::semi))
4496 nextToken();
4497 } else if (FormatTok->isOneOf(tok::kw_case, Keywords.kw_casex,
4498 Keywords.kw_casez, Keywords.kw_randcase,
4499 Keywords.kw_randsequence)) {
4500 if (Style.IndentCaseLabels)
4501 AddLevels++;
4502 nextToken();
4503 if (FormatTok->is(tok::l_paren)) {
4504 FormatTok->setFinalizedType(TT_ConditionLParen);
4505 parseParens();
4506 }
4507 if (FormatTok->isOneOf(Keywords.kw_inside, Keywords.kw_matches))
4508 nextToken();
4509 // The case header has no semicolon.
4510 } else {
4511 // "module" etc.
4512 nextToken();
4513 // all the words like the name of the module and specifiers like
4514 // "automatic" and the width of function return type
4515 while (true) {
4516 if (FormatTok->is(tok::l_square)) {
4517 auto Prev = FormatTok->getPreviousNonComment();
4518 if (Prev && Keywords.isVerilogIdentifier(*Prev))
4519 Prev->setFinalizedType(TT_VerilogDimensionedTypeName);
4520 parseSquare();
4521 } else if (Keywords.isVerilogIdentifier(*FormatTok) ||
4522 FormatTok->isOneOf(tok::hash, tok::hashhash, tok::coloncolon,
4523 Keywords.kw_automatic, tok::kw_static)) {
4524 nextToken();
4525 } else {
4526 break;
4527 }
4528 }
4529
4530 auto NewLine = [this]() {
4531 addUnwrappedLine();
4532 Line->IsContinuation = true;
4533 };
4534
4535 // package imports
4536 while (FormatTok->is(Keywords.kw_import)) {
4537 NewLine();
4538 nextToken();
4539 parseVerilogHierarchyIdentifier();
4540 if (FormatTok->is(tok::semi))
4541 nextToken();
4542 }
4543
4544 // parameters and ports
4545 if (FormatTok->is(Keywords.kw_verilogHash)) {
4546 NewLine();
4547 nextToken();
4548 if (FormatTok->is(tok::l_paren)) {
4549 FormatTok->setFinalizedType(TT_VerilogMultiLineListLParen);
4550 parseParens();
4551 }
4552 }
4553 if (FormatTok->is(tok::l_paren)) {
4554 NewLine();
4555 FormatTok->setFinalizedType(TT_VerilogMultiLineListLParen);
4556 parseParens();
4557 }
4558
4559 // extends and implements
4560 if (FormatTok->is(Keywords.kw_extends)) {
4561 NewLine();
4562 nextToken();
4563 parseVerilogHierarchyIdentifier();
4564 if (FormatTok->is(tok::l_paren))
4565 parseParens();
4566 }
4567 if (FormatTok->is(Keywords.kw_implements)) {
4568 NewLine();
4569 do {
4570 nextToken();
4571 parseVerilogHierarchyIdentifier();
4572 } while (FormatTok->is(tok::comma));
4573 }
4574
4575 // Coverage event for cover groups.
4576 if (FormatTok->is(tok::at)) {
4577 NewLine();
4578 parseVerilogSensitivityList();
4579 }
4580
4581 if (FormatTok->is(tok::semi))
4582 nextToken(/*LevelDifference=*/1);
4583 addUnwrappedLine();
4584 }
4585
4586 return AddLevels;
4587}
4588
4589void UnwrappedLineParser::parseVerilogTable() {
4590 assert(FormatTok->is(Keywords.kw_table));
4591 nextToken(/*LevelDifference=*/1);
4592 addUnwrappedLine();
4593
4594 auto InitialLevel = Line->Level++;
4595 while (!eof() && !Keywords.isVerilogEnd(*FormatTok)) {
4596 FormatToken *Tok = FormatTok;
4597 nextToken();
4598 if (Tok->is(tok::semi))
4599 addUnwrappedLine();
4600 else if (Tok->isOneOf(tok::star, tok::colon, tok::question, tok::minus))
4601 Tok->setFinalizedType(TT_VerilogTableItem);
4602 }
4603 Line->Level = InitialLevel;
4604 nextToken(/*LevelDifference=*/-1);
4605 addUnwrappedLine();
4606}
4607
4608void UnwrappedLineParser::parseVerilogCaseLabel() {
4609 // The label will get unindented in AnnotatingParser. If there are no leading
4610 // spaces, indent the rest here so that things inside the block will be
4611 // indented relative to things outside. We don't use parseLabel because we
4612 // don't know whether this colon is a label or a ternary expression at this
4613 // point.
4614 auto OrigLevel = Line->Level;
4615 auto FirstLine = CurrentLines->size();
4616 if (Line->Level == 0 || (Line->InPPDirective && Line->Level <= 1))
4617 ++Line->Level;
4618 else if (!Style.IndentCaseBlocks && Keywords.isVerilogBegin(*FormatTok))
4619 --Line->Level;
4620 parseStructuralElement();
4621 // Restore the indentation in both the new line and the line that has the
4622 // label.
4623 if (CurrentLines->size() > FirstLine)
4624 (*CurrentLines)[FirstLine].Level = OrigLevel;
4625 Line->Level = OrigLevel;
4626}
4627
4628void UnwrappedLineParser::parseVerilogExtern() {
4629 assert(
4630 FormatTok->isOneOf(tok::kw_extern, tok::kw_export, Keywords.kw_import));
4631 nextToken();
4632 // "DPI-C"
4633 if (FormatTok->is(tok::string_literal))
4634 nextToken();
4635 skipVerilogQualifiers();
4636 if (Keywords.isVerilogIdentifier(*FormatTok))
4637 nextToken();
4638 if (FormatTok->is(tok::equal))
4639 nextToken();
4640 if (Keywords.isVerilogHierarchy(*FormatTok))
4641 parseVerilogHierarchyHeader();
4642}
4643
4644void UnwrappedLineParser::skipVerilogQualifiers() {
4645 while (FormatTok->isOneOf(tok::kw_protected, tok::kw_virtual, tok::kw_static,
4646 Keywords.kw_rand, Keywords.kw_context,
4647 Keywords.kw_pure, Keywords.kw_randc,
4648 Keywords.kw_local)) {
4649 nextToken();
4650 }
4651}
4652
4653bool UnwrappedLineParser::containsExpansion(const UnwrappedLine &Line) const {
4654 for (const auto &N : Line.Tokens) {
4655 if (N.Tok->MacroCtx)
4656 return true;
4657 for (const UnwrappedLine &Child : N.Children)
4658 if (containsExpansion(Child))
4659 return true;
4660 }
4661 return false;
4662}
4663
4664void UnwrappedLineParser::addUnwrappedLine(LineLevel AdjustLevel) {
4665 if (Line->Tokens.empty())
4666 return;
4667 LLVM_DEBUG({
4668 if (!parsingPPDirective()) {
4669 llvm::dbgs() << "Adding unwrapped line:\n";
4670 printDebugInfo(*Line);
4671 }
4672 });
4673
4674 // If this line closes a block when in Whitesmiths mode, remember that
4675 // information so that the level can be decreased after the line is added.
4676 // This has to happen after the addition of the line since the line itself
4677 // needs to be indented.
4678 bool ClosesWhitesmithsBlock =
4679 Line->MatchingOpeningBlockLineIndex != UnwrappedLine::kInvalidIndex &&
4680 Style.BreakBeforeBraces == FormatStyle::BS_Whitesmiths;
4681
4682 // If the current line was expanded from a macro call, we use it to
4683 // reconstruct an unwrapped line from the structure of the expanded unwrapped
4684 // line and the unexpanded token stream.
4685 if (!parsingPPDirective() && !InExpansion && containsExpansion(*Line)) {
4686 if (!Reconstruct)
4687 Reconstruct.emplace(Line->Level, Unexpanded);
4688 Reconstruct->addLine(*Line);
4689
4690 // While the reconstructed unexpanded lines are stored in the normal
4691 // flow of lines, the expanded lines are stored on the side to be analyzed
4692 // in an extra step.
4693 CurrentExpandedLines.push_back(std::move(*Line));
4694
4695 if (Reconstruct->finished()) {
4696 UnwrappedLine Reconstructed = std::move(*Reconstruct).takeResult();
4697 assert(!Reconstructed.Tokens.empty() &&
4698 "Reconstructed must at least contain the macro identifier.");
4699 assert(!parsingPPDirective());
4700 LLVM_DEBUG({
4701 llvm::dbgs() << "Adding unexpanded line:\n";
4702 printDebugInfo(Reconstructed);
4703 });
4704 ExpandedLines[Reconstructed.Tokens.begin()->Tok] = CurrentExpandedLines;
4705 Lines.push_back(std::move(Reconstructed));
4706 CurrentExpandedLines.clear();
4707 Reconstruct.reset();
4708 }
4709 } else {
4710 // At the top level we only get here when no unexpansion is going on, or
4711 // when conditional formatting led to unfinished macro reconstructions.
4712 assert(!Reconstruct || (CurrentLines != &Lines) || !PPStack.empty());
4713 CurrentLines->push_back(std::move(*Line));
4714 }
4715 Line->Tokens.clear();
4716 Line->MatchingOpeningBlockLineIndex = UnwrappedLine::kInvalidIndex;
4717 Line->FirstStartColumn = 0;
4718 Line->IsContinuation = false;
4719 Line->SeenDecltypeAuto = false;
4720 Line->IsModuleOrImportDecl = false;
4721
4722 if (ClosesWhitesmithsBlock && AdjustLevel == LineLevel::Remove)
4723 --Line->Level;
4724 if (!parsingPPDirective() && !PreprocessorDirectives.empty()) {
4725 CurrentLines->append(
4726 std::make_move_iterator(PreprocessorDirectives.begin()),
4727 std::make_move_iterator(PreprocessorDirectives.end()));
4728 PreprocessorDirectives.clear();
4729 }
4730 // Disconnect the current token from the last token on the previous line.
4731 FormatTok->Previous = nullptr;
4732}
4733
4734bool UnwrappedLineParser::eof() const { return FormatTok->is(tok::eof); }
4735
4736bool UnwrappedLineParser::isOnNewLine(const FormatToken &FormatTok) {
4737 return (Line->InPPDirective || FormatTok.HasUnescapedNewline) &&
4738 FormatTok.NewlinesBefore > 0;
4739}
4740
4741// Checks if \p FormatTok is a line comment that continues the line comment
4742// section on \p Line.
4743static bool
4745 const UnwrappedLine &Line, const FormatStyle &Style,
4746 const llvm::Regex &CommentPragmasRegex) {
4747 if (Line.Tokens.empty() || Style.ReflowComments != FormatStyle::RCS_Always)
4748 return false;
4749
4750 StringRef IndentContent = FormatTok.TokenText;
4751 if (FormatTok.TokenText.starts_with("//") ||
4752 FormatTok.TokenText.starts_with("/*")) {
4753 IndentContent = FormatTok.TokenText.substr(2);
4754 }
4755 if (CommentPragmasRegex.match(IndentContent))
4756 return false;
4757
4758 // If Line starts with a line comment, then FormatTok continues the comment
4759 // section if its original column is greater or equal to the original start
4760 // column of the line.
4761 //
4762 // Define the min column token of a line as follows: if a line ends in '{' or
4763 // contains a '{' followed by a line comment, then the min column token is
4764 // that '{'. Otherwise, the min column token of the line is the first token of
4765 // the line.
4766 //
4767 // If Line starts with a token other than a line comment, then FormatTok
4768 // continues the comment section if its original column is greater than the
4769 // original start column of the min column token of the line.
4770 //
4771 // For example, the second line comment continues the first in these cases:
4772 //
4773 // // first line
4774 // // second line
4775 //
4776 // and:
4777 //
4778 // // first line
4779 // // second line
4780 //
4781 // and:
4782 //
4783 // int i; // first line
4784 // // second line
4785 //
4786 // and:
4787 //
4788 // do { // first line
4789 // // second line
4790 // int i;
4791 // } while (true);
4792 //
4793 // and:
4794 //
4795 // enum {
4796 // a, // first line
4797 // // second line
4798 // b
4799 // };
4800 //
4801 // The second line comment doesn't continue the first in these cases:
4802 //
4803 // // first line
4804 // // second line
4805 //
4806 // and:
4807 //
4808 // int i; // first line
4809 // // second line
4810 //
4811 // and:
4812 //
4813 // do { // first line
4814 // // second line
4815 // int i;
4816 // } while (true);
4817 //
4818 // and:
4819 //
4820 // enum {
4821 // a, // first line
4822 // // second line
4823 // };
4824 const FormatToken *MinColumnToken = Line.Tokens.front().Tok;
4825
4826 // Scan for '{//'. If found, use the column of '{' as a min column for line
4827 // comment section continuation.
4828 const FormatToken *PreviousToken = nullptr;
4829 for (const UnwrappedLineNode &Node : Line.Tokens) {
4830 if (PreviousToken && PreviousToken->is(tok::l_brace) &&
4831 isLineComment(*Node.Tok)) {
4832 MinColumnToken = PreviousToken;
4833 break;
4834 }
4835 PreviousToken = Node.Tok;
4836
4837 // Grab the last newline preceding a token in this unwrapped line.
4838 if (Node.Tok->NewlinesBefore > 0)
4839 MinColumnToken = Node.Tok;
4840 }
4841 if (PreviousToken && PreviousToken->is(tok::l_brace))
4842 MinColumnToken = PreviousToken;
4843
4844 return continuesLineComment(FormatTok, /*Previous=*/Line.Tokens.back().Tok,
4845 MinColumnToken);
4846}
4847
4848void UnwrappedLineParser::flushComments(bool NewlineBeforeNext) {
4849 bool JustComments = Line->Tokens.empty();
4850 for (FormatToken *Tok : CommentsBeforeNextToken) {
4851 // Line comments that belong to the same line comment section are put on the
4852 // same line since later we might want to reflow content between them.
4853 // Additional fine-grained breaking of line comment sections is controlled
4854 // by the class BreakableLineCommentSection in case it is desirable to keep
4855 // several line comment sections in the same unwrapped line.
4856 //
4857 // FIXME: Consider putting separate line comment sections as children to the
4858 // unwrapped line instead.
4859 Tok->ContinuesLineCommentSection =
4860 continuesLineCommentSection(*Tok, *Line, Style, CommentPragmasRegex);
4861 if (isOnNewLine(*Tok) && JustComments && !Tok->ContinuesLineCommentSection)
4862 addUnwrappedLine();
4863 pushToken(Tok);
4864 }
4865 if (NewlineBeforeNext && JustComments)
4866 addUnwrappedLine();
4867 CommentsBeforeNextToken.clear();
4868}
4869
4870void UnwrappedLineParser::nextToken(int LevelDifference) {
4871 if (eof())
4872 return;
4873 flushComments(isOnNewLine(*FormatTok));
4874 pushToken(FormatTok);
4875 FormatToken *Previous = FormatTok;
4876 if (!Style.isJavaScript())
4877 readToken(LevelDifference);
4878 else
4879 readTokenWithJavaScriptASI();
4880 FormatTok->Previous = Previous;
4881 if (Style.isVerilog()) {
4882 // Blocks in Verilog can have `begin` and `end` instead of braces. For
4883 // keywords like `begin`, we can't treat them the same as left braces
4884 // because some contexts require one of them. For example structs use
4885 // braces and if blocks use keywords, and a left brace can occur in an if
4886 // statement, but it is not a block. For keywords like `end`, we simply
4887 // treat them the same as right braces.
4888 if (Keywords.isVerilogEnd(*FormatTok))
4889 FormatTok->Tok.setKind(tok::r_brace);
4890 }
4891}
4892
4893void UnwrappedLineParser::distributeComments(
4894 const ArrayRef<FormatToken *> &Comments, const FormatToken *NextTok) {
4895 // Whether or not a line comment token continues a line is controlled by
4896 // the method continuesLineCommentSection, with the following caveat:
4897 //
4898 // Define a trail of Comments to be a nonempty proper postfix of Comments such
4899 // that each comment line from the trail is aligned with the next token, if
4900 // the next token exists. If a trail exists, the beginning of the maximal
4901 // trail is marked as a start of a new comment section.
4902 //
4903 // For example in this code:
4904 //
4905 // int a; // line about a
4906 // // line 1 about b
4907 // // line 2 about b
4908 // int b;
4909 //
4910 // the two lines about b form a maximal trail, so there are two sections, the
4911 // first one consisting of the single comment "// line about a" and the
4912 // second one consisting of the next two comments.
4913 if (Comments.empty())
4914 return;
4915 bool ShouldPushCommentsInCurrentLine = true;
4916 bool HasTrailAlignedWithNextToken = false;
4917 unsigned StartOfTrailAlignedWithNextToken = 0;
4918 if (NextTok) {
4919 // We are skipping the first element intentionally.
4920 for (unsigned i = Comments.size() - 1; i > 0; --i) {
4921 if (Comments[i]->OriginalColumn == NextTok->OriginalColumn) {
4922 HasTrailAlignedWithNextToken = true;
4923 StartOfTrailAlignedWithNextToken = i;
4924 }
4925 }
4926 }
4927 for (unsigned i = 0, e = Comments.size(); i < e; ++i) {
4928 FormatToken *FormatTok = Comments[i];
4929 if (HasTrailAlignedWithNextToken && i == StartOfTrailAlignedWithNextToken) {
4930 FormatTok->ContinuesLineCommentSection = false;
4931 } else {
4932 FormatTok->ContinuesLineCommentSection = continuesLineCommentSection(
4933 *FormatTok, *Line, Style, CommentPragmasRegex);
4934 }
4935 if (!FormatTok->ContinuesLineCommentSection &&
4936 (isOnNewLine(*FormatTok) || FormatTok->IsFirst)) {
4937 ShouldPushCommentsInCurrentLine = false;
4938 }
4939 if (ShouldPushCommentsInCurrentLine)
4940 pushToken(FormatTok);
4941 else
4942 CommentsBeforeNextToken.push_back(FormatTok);
4943 }
4944}
4945
4946void UnwrappedLineParser::readToken(int LevelDifference) {
4948 bool PreviousWasComment = false;
4949 bool FirstNonCommentOnLine = false;
4950 do {
4951 FormatTok = Tokens->getNextToken();
4952 assert(FormatTok);
4953 while (FormatTok->isOneOf(TT_ConflictStart, TT_ConflictEnd,
4954 TT_ConflictAlternative)) {
4955 if (FormatTok->is(TT_ConflictStart))
4956 conditionalCompilationStart(/*Unreachable=*/false);
4957 else if (FormatTok->is(TT_ConflictAlternative))
4958 conditionalCompilationAlternative();
4959 else if (FormatTok->is(TT_ConflictEnd))
4960 conditionalCompilationEnd();
4961 FormatTok = Tokens->getNextToken();
4962 FormatTok->MustBreakBefore = true;
4963 FormatTok->MustBreakBeforeFinalized = true;
4964 }
4965
4966 auto IsFirstNonCommentOnLine = [](bool FirstNonCommentOnLine,
4967 const FormatToken &Tok,
4968 bool PreviousWasComment) {
4969 auto IsFirstOnLine = [](const FormatToken &Tok) {
4970 return Tok.HasUnescapedNewline || Tok.IsFirst;
4971 };
4972
4973 // Consider preprocessor directives preceded by block comments as first
4974 // on line.
4975 if (PreviousWasComment)
4976 return FirstNonCommentOnLine || IsFirstOnLine(Tok);
4977 return IsFirstOnLine(Tok);
4978 };
4979
4980 FirstNonCommentOnLine = IsFirstNonCommentOnLine(
4981 FirstNonCommentOnLine, *FormatTok, PreviousWasComment);
4982 PreviousWasComment = FormatTok->is(tok::comment);
4983
4984 while (!Line->InPPDirective && FormatTok->is(tok::hash) &&
4985 FirstNonCommentOnLine) {
4986 // In Verilog, the backtick is used for macro invocations. In TableGen,
4987 // the single hash is used for the paste operator.
4988 const auto *Next = Tokens->peekNextToken();
4989 if ((Style.isVerilog() && !Keywords.isVerilogPPDirective(*Next)) ||
4990 (Style.isTableGen() &&
4991 Next->isNoneOf(tok::kw_else, tok::pp_define, tok::pp_ifdef,
4992 tok::pp_ifndef, tok::pp_endif))) {
4993 break;
4994 }
4995 distributeComments(Comments, FormatTok);
4996 Comments.clear();
4997 // If there is an unfinished unwrapped line, we flush the preprocessor
4998 // directives only after that unwrapped line was finished later.
4999 bool SwitchToPreprocessorLines = !Line->Tokens.empty();
5000 ScopedLineState BlockState(*this, SwitchToPreprocessorLines);
5001 assert((LevelDifference >= 0 ||
5002 static_cast<unsigned>(-LevelDifference) <= Line->Level) &&
5003 "LevelDifference makes Line->Level negative");
5004 Line->Level += LevelDifference;
5005 // Comments stored before the preprocessor directive need to be output
5006 // before the preprocessor directive, at the same level as the
5007 // preprocessor directive, as we consider them to apply to the directive.
5008 if (Style.IndentPPDirectives == FormatStyle::PPDIS_BeforeHash &&
5009 PPBranchLevel > 0) {
5010 Line->Level += PPBranchLevel;
5011 }
5012 assert(Line->Level >= Line->UnbracedBodyLevel);
5013 Line->Level -= Line->UnbracedBodyLevel;
5014 flushComments(isOnNewLine(*FormatTok));
5015 const bool IsEndIf = Tokens->peekNextToken()->is(tok::pp_endif);
5016 parsePPDirective();
5017 PreviousWasComment = FormatTok->is(tok::comment);
5018 FirstNonCommentOnLine = IsFirstNonCommentOnLine(
5019 FirstNonCommentOnLine, *FormatTok, PreviousWasComment);
5020 // If the #endif of a potential include guard is the last thing in the
5021 // file, then we found an include guard.
5022 if (IsEndIf && IncludeGuard == IG_Defined && PPBranchLevel == -1 &&
5023 getIncludeGuardState(Style.IndentPPDirectives) == IG_Inited &&
5024 (eof() ||
5025 (PreviousWasComment &&
5026 Tokens->peekNextToken(/*SkipComment=*/true)->is(tok::eof)))) {
5027 IncludeGuard = IG_Found;
5028 }
5029 }
5030
5031 if (!PPStack.empty() && (PPStack.back().Kind == PP_Unreachable) &&
5032 !Line->InPPDirective) {
5033 continue;
5034 }
5035
5036 if (FormatTok->is(tok::identifier) &&
5037 Macros.defined(FormatTok->TokenText) &&
5038 // FIXME: Allow expanding macros in preprocessor directives.
5039 !Line->InPPDirective) {
5040 FormatToken *ID = FormatTok;
5041 unsigned Position = Tokens->getPosition();
5042
5043 // To correctly parse the code, we need to replace the tokens of the macro
5044 // call with its expansion.
5045 auto PreCall = std::move(Line);
5046 Line.reset(new UnwrappedLine);
5047 bool OldInExpansion = InExpansion;
5048 InExpansion = true;
5049 // We parse the macro call into a new line.
5050 auto Args = parseMacroCall();
5051 InExpansion = OldInExpansion;
5052 assert(Line->Tokens.front().Tok == ID);
5053 // And remember the unexpanded macro call tokens.
5054 auto UnexpandedLine = std::move(Line);
5055 // Reset to the old line.
5056 Line = std::move(PreCall);
5057
5058 LLVM_DEBUG({
5059 llvm::dbgs() << "Macro call: " << ID->TokenText << "(";
5060 if (Args) {
5061 llvm::dbgs() << "(";
5062 for (const auto &Arg : Args.value())
5063 for (const auto &T : Arg)
5064 llvm::dbgs() << T->TokenText << " ";
5065 llvm::dbgs() << ")";
5066 }
5067 llvm::dbgs() << "\n";
5068 });
5069 if (Macros.objectLike(ID->TokenText) && Args &&
5070 !Macros.hasArity(ID->TokenText, Args->size())) {
5071 // The macro is either
5072 // - object-like, but we got argumnets, or
5073 // - overloaded to be both object-like and function-like, but none of
5074 // the function-like arities match the number of arguments.
5075 // Thus, expand as object-like macro.
5076 LLVM_DEBUG(llvm::dbgs()
5077 << "Macro \"" << ID->TokenText
5078 << "\" not overloaded for arity " << Args->size()
5079 << "or not function-like, using object-like overload.");
5080 Args.reset();
5081 UnexpandedLine->Tokens.resize(1);
5082 Tokens->setPosition(Position);
5083 nextToken();
5084 assert(!Args && Macros.objectLike(ID->TokenText));
5085 }
5086 if ((!Args && Macros.objectLike(ID->TokenText)) ||
5087 (Args && Macros.hasArity(ID->TokenText, Args->size()))) {
5088 // Next, we insert the expanded tokens in the token stream at the
5089 // current position, and continue parsing.
5090 Unexpanded[ID] = std::move(UnexpandedLine);
5092 Macros.expand(ID, std::move(Args));
5093 if (!Expansion.empty())
5094 FormatTok = Tokens->insertTokens(Expansion);
5095
5096 LLVM_DEBUG({
5097 llvm::dbgs() << "Expanded: ";
5098 for (const auto &T : Expansion)
5099 llvm::dbgs() << T->TokenText << " ";
5100 llvm::dbgs() << "\n";
5101 });
5102 } else {
5103 LLVM_DEBUG({
5104 llvm::dbgs() << "Did not expand macro \"" << ID->TokenText
5105 << "\", because it was used ";
5106 if (Args)
5107 llvm::dbgs() << "with " << Args->size();
5108 else
5109 llvm::dbgs() << "without";
5110 llvm::dbgs() << " arguments, which doesn't match any definition.\n";
5111 });
5112 Tokens->setPosition(Position);
5113 FormatTok = ID;
5114 }
5115 }
5116
5117 if (FormatTok->isNot(tok::comment)) {
5118 distributeComments(Comments, FormatTok);
5119 Comments.clear();
5120 return;
5121 }
5122
5123 Comments.push_back(FormatTok);
5124 } while (!eof());
5125
5126 distributeComments(Comments, nullptr);
5127 Comments.clear();
5128}
5129
5130namespace {
5131template <typename Iterator>
5132void pushTokens(Iterator Begin, Iterator End,
5134 for (auto I = Begin; I != End; ++I) {
5135 Into.push_back(I->Tok);
5136 for (const auto &Child : I->Children)
5137 pushTokens(Child.Tokens.begin(), Child.Tokens.end(), Into);
5138 }
5139}
5140} // namespace
5141
5142std::optional<llvm::SmallVector<llvm::SmallVector<FormatToken *, 8>, 1>>
5143UnwrappedLineParser::parseMacroCall() {
5144 std::optional<llvm::SmallVector<llvm::SmallVector<FormatToken *, 8>, 1>> Args;
5145 assert(Line->Tokens.empty());
5146 nextToken();
5147 if (FormatTok->isNot(tok::l_paren))
5148 return Args;
5149 unsigned Position = Tokens->getPosition();
5150 FormatToken *Tok = FormatTok;
5151 nextToken();
5152 Args.emplace();
5153 auto ArgStart = std::prev(Line->Tokens.end());
5154
5155 int Parens = 0;
5156 do {
5157 switch (FormatTok->Tok.getKind()) {
5158 case tok::l_paren:
5159 ++Parens;
5160 nextToken();
5161 break;
5162 case tok::r_paren: {
5163 if (Parens > 0) {
5164 --Parens;
5165 nextToken();
5166 break;
5167 }
5168 Args->push_back({});
5169 pushTokens(std::next(ArgStart), Line->Tokens.end(), Args->back());
5170 nextToken();
5171 return Args;
5172 }
5173 case tok::comma: {
5174 if (Parens > 0) {
5175 nextToken();
5176 break;
5177 }
5178 Args->push_back({});
5179 pushTokens(std::next(ArgStart), Line->Tokens.end(), Args->back());
5180 nextToken();
5181 ArgStart = std::prev(Line->Tokens.end());
5182 break;
5183 }
5184 default:
5185 nextToken();
5186 break;
5187 }
5188 } while (!eof());
5189 Line->Tokens.resize(1);
5190 Tokens->setPosition(Position);
5191 FormatTok = Tok;
5192 return {};
5193}
5194
5195void UnwrappedLineParser::pushToken(FormatToken *Tok) {
5196 Line->Tokens.push_back(UnwrappedLineNode(Tok));
5197 if (AtEndOfPPLine) {
5198 auto &Tok = *Line->Tokens.back().Tok;
5199 Tok.MustBreakBefore = true;
5200 Tok.MustBreakBeforeFinalized = true;
5201 Tok.FirstAfterPPLine = true;
5202 AtEndOfPPLine = false;
5203 }
5204}
5205
5206} // end namespace format
5207} // end namespace clang
This file defines the FormatTokenSource interface, which provides a token stream as well as the abili...
This file contains the declaration of the FormatToken, a wrapper around Token with additional informa...
FormatToken()
Token Tok
The Token.
unsigned OriginalColumn
The original 0-based column of this token, including expanded tabs.
FormatToken * Previous
The previous token in the unwrapped line.
FormatToken * Next
The next token in the unwrapped line.
This file contains the main building blocks of macro support in clang-format.
static bool HasAttribute(const QualType &T)
This file implements a token annotator, i.e.
Defines the clang::TokenKind enum and support functions.
This file contains the declaration of the UnwrappedLineParser, which turns a stream of tokens into Un...
Implements an efficient mapping from strings to IdentifierInfo nodes.
Parser - This implements a parser for the C family of languages.
Definition Parser.h:256
This class handles loading and caching of source files into memory.
Token - This structure provides full information about a lexed token.
Definition Token.h:36
IdentifierInfo * getIdentifierInfo() const
Definition Token.h:197
bool isLiteral() const
Return true if this is a "literal", like a numeric constant, string, etc.
Definition Token.h:126
bool is(tok::TokenKind K) const
is/isNot - Predicates to check if this token is a specific kind, as in "if (Tok.is(tok::l_brace)) {....
Definition Token.h:104
tok::TokenKind getKind() const
Definition Token.h:99
bool isOneOf(Ts... Ks) const
Definition Token.h:105
bool isNot(tok::TokenKind K) const
Definition Token.h:111
CompoundStatementIndenter(UnwrappedLineParser *Parser, const FormatStyle &Style, unsigned &LineLevel)
CompoundStatementIndenter(UnwrappedLineParser *Parser, unsigned &LineLevel, bool WrapBrace, bool IndentBrace)
ScopedLineState(UnwrappedLineParser &Parser, bool SwitchToPreprocessorLines=false)
Interface for users of the UnwrappedLineParser to receive the parsed lines.
UnwrappedLineParser(SourceManager &SourceMgr, const FormatStyle &Style, const AdditionalKeywords &Keywords, unsigned FirstStartColumn, ArrayRef< FormatToken * > Tokens, UnwrappedLineConsumer &Callback, llvm::SpecificBumpPtrAllocator< FormatToken > &Allocator, IdentifierTable &IdentTable)
static void hash_combine(std::size_t &seed, const T &v)
static bool mustBeJSIdentOrValue(const AdditionalKeywords &Keywords, const FormatToken *FormatTok)
std::ostream & operator<<(std::ostream &Stream, const UnwrappedLine &Line)
static bool tokenCanStartNewLine(const FormatToken &Tok)
static bool continuesLineCommentSection(const FormatToken &FormatTok, const UnwrappedLine &Line, const FormatStyle &Style, const llvm::Regex &CommentPragmasRegex)
static bool isC78Type(const FormatToken &Tok)
static bool isJSDeclOrStmt(const AdditionalKeywords &Keywords, const FormatToken *FormatTok)
static void markOptionalBraces(FormatToken *LeftBrace)
static bool mustBeJSIdent(const AdditionalKeywords &Keywords, const FormatToken *FormatTok)
static bool isIIFE(const UnwrappedLine &Line, const AdditionalKeywords &Keywords)
static bool isC78ParameterDecl(const FormatToken *Tok, const FormatToken *Next, const FormatToken *FuncName)
static bool isGoogScope(const UnwrappedLine &Line)
static FormatToken * getLastNonComment(const UnwrappedLine &Line)
TokenType
Determines the semantic type of a syntactic token, e.g.
static bool ShouldBreakBeforeBrace(const FormatStyle &Style, const FormatToken &InitialToken, bool IsEmptyBlock, bool IsJavaRecord=false)
LangOptions getFormattingLangOpts(const FormatStyle &Style)
Definition Format.cpp:4458
TokenKind
Provides a simple uniform namespace for tokens from all C languages.
Definition TokenKinds.h:25
bool isLiteral(TokenKind K)
Return true if this is a "literal" kind, like a numeric constant, string, etc.
Definition TokenKinds.h:101
The JSON file list parser is used to communicate input to InstallAPI.
bool isLineComment(const FormatToken &FormatTok)
if(T->getSizeExpr()) TRY_TO(TraverseStmt(const_cast< Expr * >(T -> getSizeExpr())))
std::vector< std::string > Macros
A list of macros of the form <definition>=<expansion> .
Definition Format.h:3951
nullptr
This class represents a compute construct, representing a 'Kind' of ‘parallel’, 'serial',...
bool isCpp() const
Definition Format.h:3843
@ Default
Set to the current date and time.
@ Type
The name was classified as a type.
Definition Sema.h:564
bool continuesLineComment(const FormatToken &FormatTok, const FormatToken *Previous, const FormatToken *MinColumnToken)
@ Parens
New-expression has a C++98 paren-delimited initializer.
Definition ExprCXX.h:2249
#define false
Definition stdbool.h:26
Encapsulates keywords that are context sensitive or for languages not properly supported by Clang's l...
IdentifierInfo * kw_instanceof
IdentifierInfo * kw_implements
IdentifierInfo * kw_override
IdentifierInfo * kw_await
IdentifierInfo * kw_extends
IdentifierInfo * kw_async
IdentifierInfo * kw_from
IdentifierInfo * kw_abstract
IdentifierInfo * kw_var
IdentifierInfo * kw_interface
IdentifierInfo * kw_function
IdentifierInfo * kw_yield
IdentifierInfo * kw_where
IdentifierInfo * kw_throws
IdentifierInfo * kw_let
IdentifierInfo * kw_import
IdentifierInfo * kw_finally
Represents a complete lambda introducer.
Definition DeclSpec.h:2884
A wrapper around a Token storing information about the whitespace characters preceding it.
bool Optional
Is optional and can be removed.
bool isNot(T Kind) const
StringRef TokenText
The raw text of the token.
bool isNoneOf(Ts... Ks) const
unsigned NewlinesBefore
The number of newlines immediately before the Token.
bool is(tok::TokenKind Kind) const
bool isOneOf(A K1, B K2) const
unsigned IsFirst
Indicates that this is the first token of the file.
FormatToken * MatchingParen
If this is a bracket, this points to the matching one.
FormatToken * Previous
The previous token in the unwrapped line.
An unwrapped line is a sequence of Token, that we would like to put on a single line if there was no ...