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