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