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