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