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: ObjC generics and protocol qualifiers are ok too.
1979 if (FormatTok->is(Kind: tok::less)) {
1980 nextToken();
1981 parseBracedList(/*IsAngleBracket=*/true);
1982 }
1983 // Return types: pointers are ok too.
1984 while (FormatTok->is(Kind: tok::star))
1985 nextToken();
1986 }
1987 // Block argument list.
1988 if (FormatTok->is(Kind: tok::l_paren))
1989 parseParens();
1990 // Block body.
1991 if (FormatTok->is(Kind: tok::l_brace))
1992 parseChildBlock();
1993 break;
1994 }
1995 case tok::l_brace:
1996 if (InRequiresExpression)
1997 FormatTok->setFinalizedType(TT_BracedListLBrace);
1998 if (!tryToParsePropertyAccessor() && !tryToParseBracedList()) {
1999 IsDecltypeAutoFunction = Line->SeenDecltypeAuto;
2000 // A block outside of parentheses must be the last part of a
2001 // structural element.
2002 // FIXME: Figure out cases where this is not true, and add projections
2003 // for them (the one we know is missing are lambdas).
2004 if (Style.isJava() &&
2005 Line->Tokens.front().Tok->is(II: Keywords.kw_synchronized)) {
2006 // If necessary, we could set the type to something different than
2007 // TT_FunctionLBrace.
2008 if (Style.BraceWrapping.AfterControlStatement ==
2009 FormatStyle::BWACS_Always) {
2010 addUnwrappedLine();
2011 }
2012 } else if (Style.BraceWrapping.AfterFunction) {
2013 addUnwrappedLine();
2014 }
2015 if (!Previous || Previous->isNot(Kind: TT_TypeDeclarationParen))
2016 FormatTok->setFinalizedType(TT_FunctionLBrace);
2017 parseBlock();
2018 IsDecltypeAutoFunction = false;
2019 addUnwrappedLine();
2020 return;
2021 }
2022 // Otherwise this was a braced init list, and the structural
2023 // element continues.
2024 break;
2025 case tok::kw_try:
2026 if (Style.isJavaScript() && Line->MustBeDeclaration) {
2027 // field/method declaration.
2028 nextToken();
2029 break;
2030 }
2031 // We arrive here when parsing function-try blocks.
2032 if (Style.BraceWrapping.AfterFunction)
2033 addUnwrappedLine();
2034 parseTryCatch();
2035 return;
2036 case tok::identifier: {
2037 if (Style.isCSharp() && FormatTok->is(II: Keywords.kw_where) &&
2038 Line->MustBeDeclaration) {
2039 addUnwrappedLine();
2040 parseCSharpGenericTypeConstraint();
2041 break;
2042 }
2043 if (FormatTok->is(TT: TT_MacroBlockEnd)) {
2044 addUnwrappedLine();
2045 return;
2046 }
2047
2048 // Function declarations (as opposed to function expressions) are parsed
2049 // on their own unwrapped line by continuing this loop. Function
2050 // expressions (functions that are not on their own line) must not create
2051 // a new unwrapped line, so they are special cased below.
2052 size_t TokenCount = Line->Tokens.size();
2053 if (Style.isJavaScript() && FormatTok->is(II: Keywords.kw_function) &&
2054 (TokenCount > 1 ||
2055 (TokenCount == 1 &&
2056 Line->Tokens.front().Tok->isNot(Kind: Keywords.kw_async)))) {
2057 tryToParseJSFunction();
2058 break;
2059 }
2060 if ((Style.isJavaScript() || Style.isJava()) &&
2061 FormatTok->is(II: Keywords.kw_interface)) {
2062 if (Style.isJavaScript()) {
2063 // In JavaScript/TypeScript, "interface" can be used as a standalone
2064 // identifier, e.g. in `var interface = 1;`. If "interface" is
2065 // followed by another identifier, it is very like to be an actual
2066 // interface declaration.
2067 unsigned StoredPosition = Tokens->getPosition();
2068 FormatToken *Next = Tokens->getNextToken();
2069 FormatTok = Tokens->setPosition(StoredPosition);
2070 if (!mustBeJSIdent(Keywords, FormatTok: Next)) {
2071 nextToken();
2072 break;
2073 }
2074 }
2075 parseRecord();
2076 addUnwrappedLine();
2077 return;
2078 }
2079
2080 if (Style.isVerilog()) {
2081 if (FormatTok->is(II: Keywords.kw_table)) {
2082 parseVerilogTable();
2083 return;
2084 }
2085 if (Keywords.isVerilogBegin(Tok: *FormatTok) ||
2086 Keywords.isVerilogHierarchy(Tok: *FormatTok)) {
2087 parseBlock();
2088 addUnwrappedLine();
2089 return;
2090 }
2091 }
2092
2093 if (!IsCpp && FormatTok->is(II: Keywords.kw_interface)) {
2094 if (parseStructLike())
2095 return;
2096 break;
2097 }
2098
2099 if (IsCpp && FormatTok->is(TT: TT_StatementMacro)) {
2100 parseStatementMacro();
2101 return;
2102 }
2103
2104 // See if the following token should start a new unwrapped line.
2105 StringRef Text = FormatTok->TokenText;
2106
2107 FormatToken *PreviousToken = FormatTok;
2108 nextToken();
2109
2110 // JS doesn't have macros, and within classes colons indicate fields, not
2111 // labels.
2112 if (Style.isJavaScript())
2113 break;
2114
2115 auto OneTokenSoFar = [&]() {
2116 auto I = Line->Tokens.begin(), E = Line->Tokens.end();
2117 while (I != E && I->Tok->is(Kind: tok::comment))
2118 ++I;
2119 if (Style.isVerilog())
2120 while (I != E && I->Tok->is(Kind: tok::hash))
2121 ++I;
2122 return I != E && (++I == E);
2123 };
2124 if (OneTokenSoFar()) {
2125 // Recognize function-like macro usages without trailing semicolon as
2126 // well as free-standing macros like Q_OBJECT.
2127 bool FunctionLike = FormatTok->is(Kind: tok::l_paren);
2128 if (FunctionLike)
2129 parseParens();
2130
2131 bool FollowedByNewline =
2132 CommentsBeforeNextToken.empty()
2133 ? FormatTok->NewlinesBefore > 0
2134 : CommentsBeforeNextToken.front()->NewlinesBefore > 0;
2135
2136 if (FollowedByNewline &&
2137 (Text.size() >= 5 ||
2138 (FunctionLike && FormatTok->isNot(Kind: tok::l_paren))) &&
2139 tokenCanStartNewLine(Tok: *FormatTok) && Text == Text.upper()) {
2140 if (PreviousToken->isNot(Kind: TT_UntouchableMacroFunc))
2141 PreviousToken->setFinalizedType(TT_FunctionLikeOrFreestandingMacro);
2142 addUnwrappedLine();
2143 return;
2144 }
2145 }
2146 break;
2147 }
2148 case tok::equal:
2149 if ((Style.isJavaScript() || Style.isCSharp()) &&
2150 FormatTok->is(TT: TT_FatArrow)) {
2151 tryToParseChildBlock();
2152 break;
2153 }
2154
2155 SeenEqual = true;
2156 nextToken();
2157 if (FormatTok->is(Kind: tok::l_brace)) {
2158 // C# needs this change to ensure that array initialisers and object
2159 // initialisers are indented the same way. In TypeScript, the brace
2160 // can also be an object type definition.
2161 if (!Style.isJavaScript())
2162 FormatTok->setBlockKind(BK_BracedInit);
2163 // TableGen's defset statement has syntax of the form,
2164 // `defset <type> <name> = { <statement>... }`
2165 if (Style.isTableGen() &&
2166 Line->Tokens.begin()->Tok->is(II: Keywords.kw_defset)) {
2167 FormatTok->setFinalizedType(TT_FunctionLBrace);
2168 parseBlock(/*MustBeDeclaration=*/false, /*AddLevels=*/1u,
2169 /*MunchSemi=*/false);
2170 addUnwrappedLine();
2171 break;
2172 }
2173 nextToken();
2174 parseBracedList();
2175 } else if (Style.Language == FormatStyle::LK_Proto &&
2176 FormatTok->is(Kind: tok::less)) {
2177 nextToken();
2178 parseBracedList(/*IsAngleBracket=*/true);
2179 }
2180 break;
2181 case tok::l_square:
2182 parseSquare();
2183 break;
2184 case tok::kw_new:
2185 if (Style.isCSharp() &&
2186 (Tokens->peekNextToken()->isAccessSpecifierKeyword() ||
2187 (Previous && Previous->isAccessSpecifierKeyword()))) {
2188 nextToken();
2189 } else {
2190 parseNew();
2191 }
2192 break;
2193 case tok::kw_switch:
2194 if (Style.isJava())
2195 parseSwitch(/*IsExpr=*/true);
2196 else
2197 nextToken();
2198 break;
2199 case tok::kw_case:
2200 // Proto: there are no switch/case statements.
2201 if (Style.Language == FormatStyle::LK_Proto) {
2202 nextToken();
2203 return;
2204 }
2205 // In Verilog switch is called case.
2206 if (Style.isVerilog()) {
2207 parseBlock();
2208 addUnwrappedLine();
2209 return;
2210 }
2211 if (Style.isJavaScript() && Line->MustBeDeclaration) {
2212 // 'case: string' field declaration.
2213 nextToken();
2214 break;
2215 }
2216 parseCaseLabel();
2217 break;
2218 case tok::kw_default:
2219 nextToken();
2220 if (Style.isVerilog()) {
2221 if (FormatTok->is(Kind: tok::colon)) {
2222 // The label will be handled in the next iteration.
2223 break;
2224 }
2225 if (FormatTok->is(II: Keywords.kw_clocking)) {
2226 // A default clocking block.
2227 parseBlock();
2228 addUnwrappedLine();
2229 return;
2230 }
2231 parseVerilogCaseLabel();
2232 return;
2233 }
2234 break;
2235 case tok::colon:
2236 nextToken();
2237 if (Style.isVerilog()) {
2238 parseVerilogCaseLabel();
2239 return;
2240 }
2241 break;
2242 case tok::greater:
2243 nextToken();
2244 if (FormatTok->is(Kind: tok::l_brace))
2245 FormatTok->Previous->setFinalizedType(TT_TemplateCloser);
2246 break;
2247 default:
2248 nextToken();
2249 break;
2250 }
2251 }
2252}
2253
2254bool UnwrappedLineParser::tryToParsePropertyAccessor() {
2255 assert(FormatTok->is(tok::l_brace));
2256 if (!Style.isCSharp())
2257 return false;
2258 // See if it's a property accessor.
2259 if (!FormatTok->Previous || FormatTok->Previous->isNot(Kind: tok::identifier))
2260 return false;
2261
2262 // See if we are inside a property accessor.
2263 //
2264 // Record the current tokenPosition so that we can advance and
2265 // reset the current token. `Next` is not set yet so we need
2266 // another way to advance along the token stream.
2267 unsigned int StoredPosition = Tokens->getPosition();
2268 FormatToken *Tok = Tokens->getNextToken();
2269
2270 // A trivial property accessor is of the form:
2271 // { [ACCESS_SPECIFIER] [get]; [ACCESS_SPECIFIER] [set|init] }
2272 // Track these as they do not require line breaks to be introduced.
2273 bool HasSpecialAccessor = false;
2274 bool IsTrivialPropertyAccessor = true;
2275 bool HasAttribute = false;
2276 while (!eof()) {
2277 if (const bool IsAccessorKeyword =
2278 Tok->isOneOf(K1: Keywords.kw_get, K2: Keywords.kw_init, Ks: Keywords.kw_set);
2279 IsAccessorKeyword || Tok->isAccessSpecifierKeyword() ||
2280 Tok->isOneOf(K1: tok::l_square, K2: tok::semi, Ks: Keywords.kw_internal)) {
2281 if (IsAccessorKeyword)
2282 HasSpecialAccessor = true;
2283 else if (Tok->is(Kind: tok::l_square))
2284 HasAttribute = true;
2285 Tok = Tokens->getNextToken();
2286 continue;
2287 }
2288 if (Tok->isNot(Kind: tok::r_brace))
2289 IsTrivialPropertyAccessor = false;
2290 break;
2291 }
2292
2293 if (!HasSpecialAccessor || HasAttribute) {
2294 Tokens->setPosition(StoredPosition);
2295 return false;
2296 }
2297
2298 // Try to parse the property accessor:
2299 // https://docs.microsoft.com/en-us/dotnet/csharp/programming-guide/classes-and-structs/properties
2300 Tokens->setPosition(StoredPosition);
2301 if (!IsTrivialPropertyAccessor && Style.BraceWrapping.AfterFunction)
2302 addUnwrappedLine();
2303 nextToken();
2304 do {
2305 switch (FormatTok->Tok.getKind()) {
2306 case tok::r_brace:
2307 nextToken();
2308 if (FormatTok->is(Kind: tok::equal)) {
2309 while (!eof() && FormatTok->isNot(Kind: tok::semi))
2310 nextToken();
2311 nextToken();
2312 }
2313 addUnwrappedLine();
2314 return true;
2315 case tok::l_brace:
2316 ++Line->Level;
2317 parseBlock(/*MustBeDeclaration=*/true);
2318 addUnwrappedLine();
2319 --Line->Level;
2320 break;
2321 case tok::equal:
2322 if (FormatTok->is(TT: TT_FatArrow)) {
2323 ++Line->Level;
2324 do {
2325 nextToken();
2326 } while (!eof() && FormatTok->isNot(Kind: tok::semi));
2327 nextToken();
2328 addUnwrappedLine();
2329 --Line->Level;
2330 break;
2331 }
2332 nextToken();
2333 break;
2334 default:
2335 if (FormatTok->isOneOf(K1: Keywords.kw_get, K2: Keywords.kw_init,
2336 Ks: Keywords.kw_set) &&
2337 !IsTrivialPropertyAccessor) {
2338 // Non-trivial get/set needs to be on its own line.
2339 addUnwrappedLine();
2340 }
2341 nextToken();
2342 }
2343 } while (!eof());
2344
2345 // Unreachable for well-formed code (paired '{' and '}').
2346 return true;
2347}
2348
2349bool UnwrappedLineParser::tryToParseLambda() {
2350 assert(FormatTok->is(tok::l_square));
2351 if (!IsCpp) {
2352 nextToken();
2353 return false;
2354 }
2355 FormatToken &LSquare = *FormatTok;
2356 if (!tryToParseLambdaIntroducer())
2357 return false;
2358
2359 FormatToken *Arrow = nullptr;
2360 bool InTemplateParameterList = false;
2361
2362 while (FormatTok->isNot(Kind: tok::l_brace)) {
2363 if (FormatTok->isTypeName(LangOpts) || FormatTok->isAttribute()) {
2364 nextToken();
2365 continue;
2366 }
2367 switch (FormatTok->Tok.getKind()) {
2368 case tok::l_brace:
2369 break;
2370 case tok::l_paren:
2371 parseParens(/*AmpAmpTokenType=*/StarAndAmpTokenType: TT_PointerOrReference);
2372 break;
2373 case tok::l_square:
2374 parseSquare();
2375 break;
2376 case tok::less:
2377 assert(FormatTok->Previous);
2378 if (FormatTok->Previous->is(Kind: tok::r_square))
2379 InTemplateParameterList = true;
2380 nextToken();
2381 break;
2382 case tok::kw_auto:
2383 case tok::kw_class:
2384 case tok::kw_struct:
2385 case tok::kw_union:
2386 case tok::kw_template:
2387 case tok::kw_typename:
2388 case tok::amp:
2389 case tok::star:
2390 case tok::kw_const:
2391 case tok::kw_constexpr:
2392 case tok::kw_consteval:
2393 case tok::comma:
2394 case tok::greater:
2395 case tok::identifier:
2396 case tok::numeric_constant:
2397 case tok::coloncolon:
2398 case tok::kw_mutable:
2399 case tok::kw_noexcept:
2400 case tok::kw_static:
2401 nextToken();
2402 break;
2403 // Specialization of a template with an integer parameter can contain
2404 // arithmetic, logical, comparison and ternary operators.
2405 //
2406 // FIXME: This also accepts sequences of operators that are not in the scope
2407 // of a template argument list.
2408 //
2409 // In a C++ lambda a template type can only occur after an arrow. We use
2410 // this as an heuristic to distinguish between Objective-C expressions
2411 // followed by an `a->b` expression, such as:
2412 // ([obj func:arg] + a->b)
2413 // Otherwise the code below would parse as a lambda.
2414 case tok::plus:
2415 case tok::minus:
2416 case tok::exclaim:
2417 case tok::tilde:
2418 case tok::slash:
2419 case tok::percent:
2420 case tok::lessless:
2421 case tok::pipe:
2422 case tok::pipepipe:
2423 case tok::ampamp:
2424 case tok::caret:
2425 case tok::equalequal:
2426 case tok::exclaimequal:
2427 case tok::greaterequal:
2428 case tok::lessequal:
2429 case tok::question:
2430 case tok::colon:
2431 case tok::ellipsis:
2432 case tok::kw_true:
2433 case tok::kw_false:
2434 if (Arrow || InTemplateParameterList) {
2435 nextToken();
2436 break;
2437 }
2438 return true;
2439 case tok::arrow:
2440 Arrow = FormatTok;
2441 nextToken();
2442 break;
2443 case tok::kw_requires:
2444 parseRequiresClause();
2445 break;
2446 case tok::equal:
2447 if (!InTemplateParameterList)
2448 return true;
2449 nextToken();
2450 break;
2451 default:
2452 return true;
2453 }
2454 }
2455
2456 FormatTok->setFinalizedType(TT_LambdaLBrace);
2457 LSquare.setFinalizedType(TT_LambdaLSquare);
2458
2459 if (Arrow)
2460 Arrow->setFinalizedType(TT_LambdaArrow);
2461
2462 NestedLambdas.push_back(Elt: Line->SeenDecltypeAuto);
2463 parseChildBlock();
2464 assert(!NestedLambdas.empty());
2465 NestedLambdas.pop_back();
2466
2467 return true;
2468}
2469
2470bool UnwrappedLineParser::tryToParseLambdaIntroducer() {
2471 const FormatToken *Previous = FormatTok->Previous;
2472 const FormatToken *LeftSquare = FormatTok;
2473 nextToken();
2474 if (Previous) {
2475 const auto *PrevPrev = Previous->getPreviousNonComment();
2476 if (Previous->is(Kind: tok::star) && PrevPrev && PrevPrev->isTypeName(LangOpts))
2477 return false;
2478 if (Previous->closesScope()) {
2479 // Not a potential C-style cast.
2480 if (Previous->isNot(Kind: tok::r_paren))
2481 return false;
2482 // Lambdas can be cast to function types only, e.g. `std::function<int()>`
2483 // and `int (*)()`.
2484 if (!PrevPrev || PrevPrev->isNoneOf(Ks: tok::greater, Ks: tok::r_paren))
2485 return false;
2486 }
2487 if (Previous && Previous->Tok.getIdentifierInfo() &&
2488 Previous->isNoneOf(Ks: tok::kw_return, Ks: tok::kw_co_await, Ks: tok::kw_co_yield,
2489 Ks: tok::kw_co_return)) {
2490 return false;
2491 }
2492 }
2493 if (LeftSquare->isCppStructuredBinding(IsCpp))
2494 return false;
2495 if (FormatTok->is(Kind: tok::l_square) || tok::isLiteral(K: FormatTok->Tok.getKind()))
2496 return false;
2497 if (FormatTok->is(Kind: tok::r_square)) {
2498 const FormatToken *Next = Tokens->peekNextToken(/*SkipComment=*/true);
2499 if (Next->is(Kind: tok::greater))
2500 return false;
2501 }
2502 parseSquare(/*LambdaIntroducer=*/true);
2503 return true;
2504}
2505
2506void UnwrappedLineParser::tryToParseJSFunction() {
2507 assert(FormatTok->is(Keywords.kw_function));
2508 if (FormatTok->is(II: Keywords.kw_async))
2509 nextToken();
2510 // Consume "function".
2511 nextToken();
2512
2513 // Consume * (generator function). Treat it like C++'s overloaded operators.
2514 if (FormatTok->is(Kind: tok::star)) {
2515 FormatTok->setFinalizedType(TT_OverloadedOperator);
2516 nextToken();
2517 }
2518
2519 // Consume function name.
2520 if (FormatTok->is(Kind: tok::identifier))
2521 nextToken();
2522
2523 if (FormatTok->isNot(Kind: tok::l_paren))
2524 return;
2525
2526 // Parse formal parameter list.
2527 parseParens();
2528
2529 if (FormatTok->is(Kind: tok::colon)) {
2530 // Parse a type definition.
2531 nextToken();
2532
2533 // Eat the type declaration. For braced inline object types, balance braces,
2534 // otherwise just parse until finding an l_brace for the function body.
2535 if (FormatTok->is(Kind: tok::l_brace))
2536 tryToParseBracedList();
2537 else
2538 while (FormatTok->isNoneOf(Ks: tok::l_brace, Ks: tok::semi) && !eof())
2539 nextToken();
2540 }
2541
2542 if (FormatTok->is(Kind: tok::semi))
2543 return;
2544
2545 parseChildBlock();
2546}
2547
2548bool UnwrappedLineParser::tryToParseBracedList() {
2549 if (FormatTok->is(BBK: BK_Unknown))
2550 calculateBraceTypes();
2551 assert(FormatTok->isNot(BK_Unknown));
2552 if (FormatTok->is(BBK: BK_Block))
2553 return false;
2554 nextToken();
2555 parseBracedList();
2556 return true;
2557}
2558
2559bool UnwrappedLineParser::tryToParseChildBlock() {
2560 assert(Style.isJavaScript() || Style.isCSharp());
2561 assert(FormatTok->is(TT_FatArrow));
2562 // Fat arrows (=>) have tok::TokenKind tok::equal but TokenType TT_FatArrow.
2563 // They always start an expression or a child block if followed by a curly
2564 // brace.
2565 nextToken();
2566 if (FormatTok->isNot(Kind: tok::l_brace))
2567 return false;
2568 parseChildBlock();
2569 return true;
2570}
2571
2572bool UnwrappedLineParser::parseBracedList(bool IsAngleBracket, bool IsEnum) {
2573 assert(!IsAngleBracket || !IsEnum);
2574 bool HasError = false;
2575
2576 // FIXME: Once we have an expression parser in the UnwrappedLineParser,
2577 // replace this by using parseAssignmentExpression() inside.
2578 do {
2579 if (Style.isCSharp() && FormatTok->is(TT: TT_FatArrow) &&
2580 tryToParseChildBlock()) {
2581 continue;
2582 }
2583 if (Style.isJavaScript()) {
2584 if (FormatTok->is(II: Keywords.kw_function)) {
2585 tryToParseJSFunction();
2586 continue;
2587 }
2588 if (FormatTok->is(Kind: tok::l_brace)) {
2589 // Could be a method inside of a braced list `{a() { return 1; }}`.
2590 if (tryToParseBracedList())
2591 continue;
2592 parseChildBlock();
2593 }
2594 }
2595 if (FormatTok->is(Kind: IsAngleBracket ? tok::greater : tok::r_brace)) {
2596 if (IsEnum) {
2597 FormatTok->setBlockKind(BK_Block);
2598 if (!Style.AllowShortEnumsOnASingleLine)
2599 addUnwrappedLine();
2600 }
2601 nextToken();
2602 return !HasError;
2603 }
2604 switch (FormatTok->Tok.getKind()) {
2605 case tok::l_square:
2606 if (Style.isCSharp())
2607 parseSquare();
2608 else
2609 tryToParseLambda();
2610 break;
2611 case tok::l_paren:
2612 parseParens();
2613 // JavaScript can just have free standing methods and getters/setters in
2614 // object literals. Detect them by a "{" following ")".
2615 if (Style.isJavaScript()) {
2616 if (FormatTok->is(Kind: tok::l_brace))
2617 parseChildBlock();
2618 break;
2619 }
2620 break;
2621 case tok::l_brace:
2622 // Assume there are no blocks inside a braced init list apart
2623 // from the ones we explicitly parse out (like lambdas).
2624 FormatTok->setBlockKind(BK_BracedInit);
2625 if (!IsAngleBracket) {
2626 auto *Prev = FormatTok->Previous;
2627 if (Prev && Prev->is(Kind: tok::greater))
2628 Prev->setFinalizedType(TT_TemplateCloser);
2629 }
2630 nextToken();
2631 parseBracedList();
2632 break;
2633 case tok::less:
2634 nextToken();
2635 if (IsAngleBracket)
2636 parseBracedList(/*IsAngleBracket=*/true);
2637 break;
2638 case tok::semi:
2639 // JavaScript (or more precisely TypeScript) can have semicolons in braced
2640 // lists (in so-called TypeMemberLists). Thus, the semicolon cannot be
2641 // used for error recovery if we have otherwise determined that this is
2642 // a braced list.
2643 if (Style.isJavaScript()) {
2644 nextToken();
2645 break;
2646 }
2647 HasError = true;
2648 if (!IsEnum)
2649 return false;
2650 nextToken();
2651 break;
2652 case tok::comma:
2653 nextToken();
2654 if (IsEnum && !Style.AllowShortEnumsOnASingleLine)
2655 addUnwrappedLine();
2656 break;
2657 case tok::kw_requires:
2658 parseRequiresExpression();
2659 break;
2660 default:
2661 nextToken();
2662 break;
2663 }
2664 } while (!eof());
2665 return false;
2666}
2667
2668/// Parses a pair of parentheses (and everything between them).
2669/// \param StarAndAmpTokenType If different than TT_Unknown sets this type for
2670/// all (double) ampersands and stars. This applies for all nested scopes as
2671/// well, this is disabled within a (potential) template argument <>, and thus
2672/// also if we find only a <.
2673///
2674/// Returns whether there is a `=` token between the parentheses.
2675bool UnwrappedLineParser::parseParens(TokenType StarAndAmpTokenType,
2676 bool InMacroCall) {
2677 assert(FormatTok->is(tok::l_paren) && "'(' expected.");
2678 auto *LParen = FormatTok;
2679 auto *Prev = FormatTok->Previous;
2680 bool SeenComma = false;
2681 bool SeenEqual = false;
2682 bool MightBeFoldExpr = false;
2683 unsigned ExcessLess = 0;
2684 nextToken();
2685 const bool MightBeStmtExpr = FormatTok->is(Kind: tok::l_brace);
2686 if (!InMacroCall && Prev && Prev->is(TT: TT_FunctionLikeMacro))
2687 InMacroCall = true;
2688 do {
2689 switch (FormatTok->Tok.getKind()) {
2690 case tok::l_paren:
2691 if (parseParens(StarAndAmpTokenType: ExcessLess == 0 ? StarAndAmpTokenType : TT_Unknown,
2692 InMacroCall)) {
2693 SeenEqual = true;
2694 }
2695 if (Style.isJava() && FormatTok->is(Kind: tok::l_brace))
2696 parseChildBlock();
2697 break;
2698 case tok::r_paren: {
2699 auto *RParen = FormatTok;
2700 nextToken();
2701 if (Prev) {
2702 auto OptionalParens = [&] {
2703 if (Style.RemoveParentheses == FormatStyle::RPS_Leave ||
2704 MightBeStmtExpr || MightBeFoldExpr || SeenComma || InMacroCall ||
2705 Line->InMacroBody || RParen->getPreviousNonComment() == LParen) {
2706 return false;
2707 }
2708 const bool DoubleParens =
2709 Prev->is(Kind: tok::l_paren) && FormatTok->is(Kind: tok::r_paren);
2710 if (DoubleParens) {
2711 const auto *PrevPrev = Prev->getPreviousNonComment();
2712 const bool Excluded =
2713 PrevPrev &&
2714 (PrevPrev->isOneOf(K1: tok::kw___attribute, K2: tok::kw_decltype) ||
2715 (SeenEqual &&
2716 (PrevPrev->isOneOf(K1: tok::kw_if, K2: tok::kw_while) ||
2717 PrevPrev->endsSequence(K1: tok::kw_constexpr, Tokens: tok::kw_if))));
2718 if (!Excluded)
2719 return true;
2720 } else {
2721 const bool CommaSeparated =
2722 Prev->isOneOf(K1: tok::l_paren, K2: tok::comma) &&
2723 FormatTok->isOneOf(K1: tok::comma, K2: tok::r_paren);
2724 if (CommaSeparated &&
2725 // LParen is not preceded by ellipsis, comma.
2726 !Prev->endsSequence(K1: tok::comma, Tokens: tok::ellipsis) &&
2727 // RParen is not followed by comma, ellipsis.
2728 !(FormatTok->is(Kind: tok::comma) &&
2729 Tokens->peekNextToken()->is(Kind: tok::ellipsis))) {
2730 return true;
2731 }
2732 const bool ReturnParens =
2733 Style.RemoveParentheses == FormatStyle::RPS_ReturnStatement &&
2734 ((NestedLambdas.empty() && !IsDecltypeAutoFunction) ||
2735 (!NestedLambdas.empty() && !NestedLambdas.back())) &&
2736 Prev->isOneOf(K1: tok::kw_return, K2: tok::kw_co_return) &&
2737 FormatTok->is(Kind: tok::semi);
2738 if (ReturnParens)
2739 return true;
2740 }
2741 return false;
2742 };
2743 if (OptionalParens()) {
2744 LParen->Optional = true;
2745 RParen->Optional = true;
2746 } else if (Prev->is(TT: TT_TypenameMacro)) {
2747 LParen->setFinalizedType(TT_TypeDeclarationParen);
2748 RParen->setFinalizedType(TT_TypeDeclarationParen);
2749 } else if (Prev->is(Kind: tok::greater) && RParen->Previous == LParen) {
2750 Prev->setFinalizedType(TT_TemplateCloser);
2751 } else if (FormatTok->is(Kind: tok::l_brace) && Prev->is(Kind: tok::amp) &&
2752 !Prev->Previous) {
2753 FormatTok->setBlockKind(BK_BracedInit);
2754 }
2755 }
2756 return SeenEqual;
2757 }
2758 case tok::r_brace:
2759 // A "}" inside parenthesis is an error if there wasn't a matching "{".
2760 return SeenEqual;
2761 case tok::l_square:
2762 tryToParseLambda();
2763 break;
2764 case tok::l_brace:
2765 if (!tryToParseBracedList())
2766 parseChildBlock();
2767 break;
2768 case tok::at:
2769 nextToken();
2770 if (FormatTok->is(Kind: tok::l_brace)) {
2771 nextToken();
2772 parseBracedList();
2773 }
2774 break;
2775 case tok::comma:
2776 SeenComma = true;
2777 nextToken();
2778 break;
2779 case tok::ellipsis:
2780 MightBeFoldExpr = true;
2781 nextToken();
2782 break;
2783 case tok::equal:
2784 SeenEqual = true;
2785 if (Style.isCSharp() && FormatTok->is(TT: TT_FatArrow))
2786 tryToParseChildBlock();
2787 else
2788 nextToken();
2789 break;
2790 case tok::kw_class:
2791 if (Style.isJavaScript())
2792 parseRecord(/*ParseAsExpr=*/true);
2793 else
2794 nextToken();
2795 break;
2796 case tok::identifier:
2797 if (Style.isJavaScript() && (FormatTok->is(II: Keywords.kw_function)))
2798 tryToParseJSFunction();
2799 else
2800 nextToken();
2801 break;
2802 case tok::kw_switch:
2803 if (Style.isJava())
2804 parseSwitch(/*IsExpr=*/true);
2805 else
2806 nextToken();
2807 break;
2808 case tok::kw_requires:
2809 parseRequiresExpression();
2810 break;
2811 case tok::less:
2812 // We have here no clue whether this is a less, or a template opener, opt
2813 // out of the predefined StarAndAmpTokenType.
2814 ++ExcessLess;
2815 nextToken();
2816 break;
2817 case tok::greater:
2818 if (ExcessLess > 0)
2819 --ExcessLess;
2820 nextToken();
2821 break;
2822 case tok::star:
2823 case tok::amp:
2824 case tok::ampamp:
2825 if (StarAndAmpTokenType != TT_Unknown && ExcessLess == 0)
2826 FormatTok->setFinalizedType(StarAndAmpTokenType);
2827 [[fallthrough]];
2828 default:
2829 nextToken();
2830 break;
2831 }
2832 } while (!eof());
2833 return SeenEqual;
2834}
2835
2836void UnwrappedLineParser::parseSquare(bool LambdaIntroducer) {
2837 if (!LambdaIntroducer) {
2838 assert(FormatTok->is(tok::l_square) && "'[' expected.");
2839 if (tryToParseLambda())
2840 return;
2841 }
2842 do {
2843 switch (FormatTok->Tok.getKind()) {
2844 case tok::l_paren:
2845 parseParens();
2846 break;
2847 case tok::r_square:
2848 nextToken();
2849 return;
2850 case tok::r_brace:
2851 // A "}" inside parenthesis is an error if there wasn't a matching "{".
2852 return;
2853 case tok::l_square:
2854 parseSquare();
2855 break;
2856 case tok::l_brace: {
2857 if (!tryToParseBracedList())
2858 parseChildBlock();
2859 break;
2860 }
2861 case tok::at:
2862 case tok::colon:
2863 nextToken();
2864 if (FormatTok->is(Kind: tok::l_brace)) {
2865 nextToken();
2866 parseBracedList();
2867 }
2868 break;
2869 default:
2870 nextToken();
2871 break;
2872 }
2873 } while (!eof());
2874}
2875
2876void UnwrappedLineParser::keepAncestorBraces() {
2877 if (!Style.RemoveBracesLLVM)
2878 return;
2879
2880 const int MaxNestingLevels = 2;
2881 const int Size = NestedTooDeep.size();
2882 if (Size >= MaxNestingLevels)
2883 NestedTooDeep[Size - MaxNestingLevels] = true;
2884 NestedTooDeep.push_back(Elt: false);
2885}
2886
2887static FormatToken *getLastNonComment(const UnwrappedLine &Line) {
2888 for (const auto &Token : llvm::reverse(C: Line.Tokens))
2889 if (Token.Tok->isNot(Kind: tok::comment))
2890 return Token.Tok;
2891
2892 return nullptr;
2893}
2894
2895void UnwrappedLineParser::parseUnbracedBody(bool CheckEOF) {
2896 FormatToken *Tok = nullptr;
2897
2898 if (Style.InsertBraces && !Line->InPPDirective && !Line->Tokens.empty() &&
2899 PreprocessorDirectives.empty() && FormatTok->isNot(Kind: tok::semi)) {
2900 Tok = Style.BraceWrapping.AfterControlStatement == FormatStyle::BWACS_Never
2901 ? getLastNonComment(Line: *Line)
2902 : Line->Tokens.back().Tok;
2903 assert(Tok);
2904 if (Tok->BraceCount < 0) {
2905 assert(Tok->BraceCount == -1);
2906 Tok = nullptr;
2907 } else {
2908 Tok->BraceCount = -1;
2909 }
2910 }
2911
2912 addUnwrappedLine();
2913 ++Line->Level;
2914 ++Line->UnbracedBodyLevel;
2915 parseStructuralElement();
2916 --Line->UnbracedBodyLevel;
2917
2918 if (Tok) {
2919 assert(!Line->InPPDirective);
2920 Tok = nullptr;
2921 for (const auto &L : llvm::reverse(C&: *CurrentLines)) {
2922 if (!L.InPPDirective && getLastNonComment(Line: L)) {
2923 Tok = L.Tokens.back().Tok;
2924 break;
2925 }
2926 }
2927 assert(Tok);
2928 ++Tok->BraceCount;
2929 }
2930
2931 if (CheckEOF && eof())
2932 addUnwrappedLine();
2933
2934 --Line->Level;
2935}
2936
2937static void markOptionalBraces(FormatToken *LeftBrace) {
2938 if (!LeftBrace)
2939 return;
2940
2941 assert(LeftBrace->is(tok::l_brace));
2942
2943 FormatToken *RightBrace = LeftBrace->MatchingParen;
2944 if (!RightBrace) {
2945 assert(!LeftBrace->Optional);
2946 return;
2947 }
2948
2949 assert(RightBrace->is(tok::r_brace));
2950 assert(RightBrace->MatchingParen == LeftBrace);
2951 assert(LeftBrace->Optional == RightBrace->Optional);
2952
2953 LeftBrace->Optional = true;
2954 RightBrace->Optional = true;
2955}
2956
2957void UnwrappedLineParser::handleAttributes() {
2958 // Handle AttributeMacro, e.g. `if (x) UNLIKELY`.
2959 if (FormatTok->isAttribute())
2960 nextToken();
2961 else if (FormatTok->is(Kind: tok::l_square))
2962 handleCppAttributes();
2963}
2964
2965bool UnwrappedLineParser::handleCppAttributes() {
2966 // Handle [[likely]] / [[unlikely]] attributes.
2967 assert(FormatTok->is(tok::l_square));
2968 if (!tryToParseSimpleAttribute())
2969 return false;
2970 parseSquare();
2971 return true;
2972}
2973
2974/// Returns whether \c Tok begins a block.
2975bool UnwrappedLineParser::isBlockBegin(const FormatToken &Tok) const {
2976 // FIXME: rename the function or make
2977 // Tok.isOneOf(tok::l_brace, TT_MacroBlockBegin) work.
2978 return Style.isVerilog() ? Keywords.isVerilogBegin(Tok)
2979 : Tok.is(Kind: tok::l_brace);
2980}
2981
2982FormatToken *UnwrappedLineParser::parseIfThenElse(IfStmtKind *IfKind,
2983 bool KeepBraces,
2984 bool IsVerilogAssert) {
2985 assert((FormatTok->is(tok::kw_if) ||
2986 (Style.isVerilog() &&
2987 FormatTok->isOneOf(tok::kw_restrict, Keywords.kw_assert,
2988 Keywords.kw_assume, Keywords.kw_cover))) &&
2989 "'if' expected");
2990 nextToken();
2991
2992 if (IsVerilogAssert) {
2993 // Handle `assert #0` and `assert final`.
2994 if (FormatTok->is(II: Keywords.kw_verilogHash)) {
2995 nextToken();
2996 if (FormatTok->is(Kind: tok::numeric_constant))
2997 nextToken();
2998 } else if (FormatTok->isOneOf(K1: Keywords.kw_final, K2: Keywords.kw_property,
2999 Ks: Keywords.kw_sequence)) {
3000 nextToken();
3001 }
3002 }
3003
3004 // TableGen's if statement has the form of `if <cond> then { ... }`.
3005 if (Style.isTableGen()) {
3006 while (!eof() && FormatTok->isNot(Kind: Keywords.kw_then)) {
3007 // Simply skip until then. This range only contains a value.
3008 nextToken();
3009 }
3010 }
3011
3012 // Handle `if !consteval`.
3013 if (FormatTok->is(Kind: tok::exclaim))
3014 nextToken();
3015
3016 bool KeepIfBraces = true;
3017 if (FormatTok->is(Kind: tok::kw_consteval)) {
3018 nextToken();
3019 } else {
3020 KeepIfBraces = !Style.RemoveBracesLLVM || KeepBraces;
3021 if (FormatTok->isOneOf(K1: tok::kw_constexpr, K2: tok::identifier))
3022 nextToken();
3023 if (FormatTok->is(Kind: tok::l_paren)) {
3024 FormatTok->setFinalizedType(TT_ConditionLParen);
3025 parseParens();
3026 }
3027 }
3028 handleAttributes();
3029 // The then action is optional in Verilog assert statements.
3030 if (IsVerilogAssert && FormatTok->is(Kind: tok::semi)) {
3031 nextToken();
3032 addUnwrappedLine();
3033 return nullptr;
3034 }
3035
3036 bool NeedsUnwrappedLine = false;
3037 keepAncestorBraces();
3038
3039 FormatToken *IfLeftBrace = nullptr;
3040 IfStmtKind IfBlockKind = IfStmtKind::NotIf;
3041
3042 if (isBlockBegin(Tok: *FormatTok)) {
3043 FormatTok->setFinalizedType(TT_ControlStatementLBrace);
3044 IfLeftBrace = FormatTok;
3045 CompoundStatementIndenter Indenter(this, Style, Line->Level);
3046 parseBlock(/*MustBeDeclaration=*/false, /*AddLevels=*/1u,
3047 /*MunchSemi=*/true, KeepBraces: KeepIfBraces, IfKind: &IfBlockKind);
3048 setPreviousRBraceType(TT_ControlStatementRBrace);
3049 if (Style.BraceWrapping.BeforeElse)
3050 addUnwrappedLine();
3051 else
3052 NeedsUnwrappedLine = true;
3053 } else if (IsVerilogAssert && FormatTok->is(Kind: tok::kw_else)) {
3054 addUnwrappedLine();
3055 } else {
3056 parseUnbracedBody();
3057 }
3058
3059 if (Style.RemoveBracesLLVM) {
3060 assert(!NestedTooDeep.empty());
3061 KeepIfBraces = KeepIfBraces ||
3062 (IfLeftBrace && !IfLeftBrace->MatchingParen) ||
3063 NestedTooDeep.back() || IfBlockKind == IfStmtKind::IfOnly ||
3064 IfBlockKind == IfStmtKind::IfElseIf;
3065 }
3066
3067 bool KeepElseBraces = KeepIfBraces;
3068 FormatToken *ElseLeftBrace = nullptr;
3069 IfStmtKind Kind = IfStmtKind::IfOnly;
3070
3071 if (FormatTok->is(Kind: tok::kw_else)) {
3072 if (Style.RemoveBracesLLVM) {
3073 NestedTooDeep.back() = false;
3074 Kind = IfStmtKind::IfElse;
3075 }
3076 nextToken();
3077 handleAttributes();
3078 if (isBlockBegin(Tok: *FormatTok)) {
3079 const bool FollowedByIf = Tokens->peekNextToken()->is(Kind: tok::kw_if);
3080 FormatTok->setFinalizedType(TT_ElseLBrace);
3081 ElseLeftBrace = FormatTok;
3082 CompoundStatementIndenter Indenter(this, Style, Line->Level);
3083 IfStmtKind ElseBlockKind = IfStmtKind::NotIf;
3084 FormatToken *IfLBrace =
3085 parseBlock(/*MustBeDeclaration=*/false, /*AddLevels=*/1u,
3086 /*MunchSemi=*/true, KeepBraces: KeepElseBraces, IfKind: &ElseBlockKind);
3087 setPreviousRBraceType(TT_ElseRBrace);
3088 if (FormatTok->is(Kind: tok::kw_else)) {
3089 KeepElseBraces = KeepElseBraces ||
3090 ElseBlockKind == IfStmtKind::IfOnly ||
3091 ElseBlockKind == IfStmtKind::IfElseIf;
3092 } else if (FollowedByIf && IfLBrace && !IfLBrace->Optional) {
3093 KeepElseBraces = true;
3094 assert(ElseLeftBrace->MatchingParen);
3095 markOptionalBraces(LeftBrace: ElseLeftBrace);
3096 }
3097 addUnwrappedLine();
3098 } else if (!IsVerilogAssert && FormatTok->is(Kind: tok::kw_if)) {
3099 const FormatToken *Previous = Tokens->getPreviousToken();
3100 assert(Previous);
3101 const bool IsPrecededByComment = Previous->is(Kind: tok::comment);
3102 if (IsPrecededByComment) {
3103 addUnwrappedLine();
3104 ++Line->Level;
3105 }
3106 bool TooDeep = true;
3107 if (Style.RemoveBracesLLVM) {
3108 Kind = IfStmtKind::IfElseIf;
3109 TooDeep = NestedTooDeep.pop_back_val();
3110 }
3111 ElseLeftBrace = parseIfThenElse(/*IfKind=*/nullptr, KeepBraces: KeepIfBraces);
3112 if (Style.RemoveBracesLLVM)
3113 NestedTooDeep.push_back(Elt: TooDeep);
3114 if (IsPrecededByComment)
3115 --Line->Level;
3116 } else {
3117 parseUnbracedBody(/*CheckEOF=*/true);
3118 }
3119 } else {
3120 KeepIfBraces = KeepIfBraces || IfBlockKind == IfStmtKind::IfElse;
3121 if (NeedsUnwrappedLine)
3122 addUnwrappedLine();
3123 }
3124
3125 if (!Style.RemoveBracesLLVM)
3126 return nullptr;
3127
3128 assert(!NestedTooDeep.empty());
3129 KeepElseBraces = KeepElseBraces ||
3130 (ElseLeftBrace && !ElseLeftBrace->MatchingParen) ||
3131 NestedTooDeep.back();
3132
3133 NestedTooDeep.pop_back();
3134
3135 if (!KeepIfBraces && !KeepElseBraces) {
3136 markOptionalBraces(LeftBrace: IfLeftBrace);
3137 markOptionalBraces(LeftBrace: ElseLeftBrace);
3138 } else if (IfLeftBrace) {
3139 FormatToken *IfRightBrace = IfLeftBrace->MatchingParen;
3140 if (IfRightBrace) {
3141 assert(IfRightBrace->MatchingParen == IfLeftBrace);
3142 assert(!IfLeftBrace->Optional);
3143 assert(!IfRightBrace->Optional);
3144 IfLeftBrace->MatchingParen = nullptr;
3145 IfRightBrace->MatchingParen = nullptr;
3146 }
3147 }
3148
3149 if (IfKind)
3150 *IfKind = Kind;
3151
3152 return IfLeftBrace;
3153}
3154
3155void UnwrappedLineParser::parseTryCatch() {
3156 assert(FormatTok->isOneOf(tok::kw_try, tok::kw___try) && "'try' expected");
3157 nextToken();
3158 bool NeedsUnwrappedLine = false;
3159 bool HasCtorInitializer = false;
3160 if (FormatTok->is(Kind: tok::colon)) {
3161 auto *Colon = FormatTok;
3162 // We are in a function try block, what comes is an initializer list.
3163 nextToken();
3164 if (FormatTok->is(Kind: tok::identifier)) {
3165 HasCtorInitializer = true;
3166 Colon->setFinalizedType(TT_CtorInitializerColon);
3167 }
3168
3169 // In case identifiers were removed by clang-tidy, what might follow is
3170 // multiple commas in sequence - before the first identifier.
3171 while (FormatTok->is(Kind: tok::comma))
3172 nextToken();
3173
3174 while (FormatTok->is(Kind: tok::identifier)) {
3175 nextToken();
3176 if (FormatTok->is(Kind: tok::l_paren)) {
3177 parseParens();
3178 } else if (FormatTok->is(Kind: tok::l_brace)) {
3179 nextToken();
3180 parseBracedList();
3181 }
3182
3183 // In case identifiers were removed by clang-tidy, what might follow is
3184 // multiple commas in sequence - after the first identifier.
3185 while (FormatTok->is(Kind: tok::comma))
3186 nextToken();
3187 }
3188 }
3189 // Parse try with resource.
3190 if (Style.isJava() && FormatTok->is(Kind: tok::l_paren))
3191 parseParens();
3192
3193 keepAncestorBraces();
3194
3195 if (FormatTok->is(Kind: tok::l_brace)) {
3196 if (HasCtorInitializer)
3197 FormatTok->setFinalizedType(TT_FunctionLBrace);
3198 CompoundStatementIndenter Indenter(this, Style, Line->Level);
3199 parseBlock();
3200 if (Style.BraceWrapping.BeforeCatch)
3201 addUnwrappedLine();
3202 else
3203 NeedsUnwrappedLine = true;
3204 } else if (FormatTok->isNot(Kind: tok::kw_catch)) {
3205 // The C++ standard requires a compound-statement after a try.
3206 // If there's none, we try to assume there's a structuralElement
3207 // and try to continue.
3208 addUnwrappedLine();
3209 ++Line->Level;
3210 parseStructuralElement();
3211 --Line->Level;
3212 }
3213 for (bool SeenCatch = false;;) {
3214 if (FormatTok->is(Kind: tok::at))
3215 nextToken();
3216 if (FormatTok->isNoneOf(Ks: tok::kw_catch, Ks: Keywords.kw___except,
3217 Ks: tok::kw___finally, Ks: tok::objc_catch,
3218 Ks: tok::objc_finally) &&
3219 !((Style.isJava() || Style.isJavaScript()) &&
3220 FormatTok->is(II: Keywords.kw_finally))) {
3221 break;
3222 }
3223 if (FormatTok->is(Kind: tok::kw_catch))
3224 SeenCatch = true;
3225 nextToken();
3226 while (FormatTok->isNot(Kind: tok::l_brace)) {
3227 if (FormatTok->is(Kind: tok::l_paren)) {
3228 parseParens();
3229 continue;
3230 }
3231 if (FormatTok->isOneOf(K1: tok::semi, K2: tok::r_brace) || eof()) {
3232 if (Style.RemoveBracesLLVM)
3233 NestedTooDeep.pop_back();
3234 return;
3235 }
3236 nextToken();
3237 }
3238 if (SeenCatch) {
3239 FormatTok->setFinalizedType(TT_ControlStatementLBrace);
3240 SeenCatch = false;
3241 }
3242 NeedsUnwrappedLine = false;
3243 Line->MustBeDeclaration = false;
3244 CompoundStatementIndenter Indenter(this, Style, Line->Level);
3245 parseBlock();
3246 if (Style.BraceWrapping.BeforeCatch)
3247 addUnwrappedLine();
3248 else
3249 NeedsUnwrappedLine = true;
3250 }
3251
3252 if (Style.RemoveBracesLLVM)
3253 NestedTooDeep.pop_back();
3254
3255 if (NeedsUnwrappedLine)
3256 addUnwrappedLine();
3257}
3258
3259void UnwrappedLineParser::parseNamespaceOrExportBlock(unsigned AddLevels) {
3260 bool ManageWhitesmithsBraces =
3261 AddLevels == 0u && Style.BreakBeforeBraces == FormatStyle::BS_Whitesmiths;
3262
3263 // If we're in Whitesmiths mode, indent the brace if we're not indenting
3264 // the whole block.
3265 if (ManageWhitesmithsBraces)
3266 ++Line->Level;
3267
3268 // Munch the semicolon after the block. This is more common than one would
3269 // think. Putting the semicolon into its own line is very ugly.
3270 parseBlock(/*MustBeDeclaration=*/true, AddLevels, /*MunchSemi=*/true,
3271 /*KeepBraces=*/true, /*IfKind=*/nullptr, UnindentWhitesmithsBraces: ManageWhitesmithsBraces);
3272
3273 addUnwrappedLine(AdjustLevel: AddLevels > 0 ? LineLevel::Remove : LineLevel::Keep);
3274
3275 if (ManageWhitesmithsBraces)
3276 --Line->Level;
3277}
3278
3279void UnwrappedLineParser::parseNamespace() {
3280 assert(FormatTok->isOneOf(tok::kw_namespace, TT_NamespaceMacro) &&
3281 "'namespace' expected");
3282
3283 const FormatToken &InitialToken = *FormatTok;
3284 nextToken();
3285 if (InitialToken.is(TT: TT_NamespaceMacro)) {
3286 parseParens();
3287 } else {
3288 while (FormatTok->isOneOf(K1: tok::identifier, K2: tok::coloncolon, Ks: tok::kw_inline,
3289 Ks: tok::l_square, Ks: tok::period, Ks: tok::l_paren) ||
3290 (Style.isCSharp() && FormatTok->is(Kind: tok::kw_union))) {
3291 if (FormatTok->is(Kind: tok::l_square))
3292 parseSquare();
3293 else if (FormatTok->is(Kind: tok::l_paren))
3294 parseParens();
3295 else
3296 nextToken();
3297 }
3298 }
3299 if (FormatTok->is(Kind: tok::l_brace)) {
3300 FormatTok->setFinalizedType(TT_NamespaceLBrace);
3301
3302 if (ShouldBreakBeforeBrace(Style, InitialToken,
3303 IsEmptyBlock: Tokens->peekNextToken()->is(Kind: tok::r_brace))) {
3304 addUnwrappedLine();
3305 }
3306
3307 unsigned AddLevels =
3308 Style.NamespaceIndentation == FormatStyle::NI_All ||
3309 (Style.NamespaceIndentation == FormatStyle::NI_Inner &&
3310 DeclarationScopeStack.size() > 1)
3311 ? 1u
3312 : 0u;
3313 parseNamespaceOrExportBlock(AddLevels);
3314 }
3315 // FIXME: Add error handling.
3316}
3317
3318void UnwrappedLineParser::parseCppExportBlock() {
3319 if (FormatTok->is(Kind: tok::l_brace)) {
3320 FormatTok->setFinalizedType(TT_ExportLBrace);
3321 if (Style.BraceWrapping.AfterExportBlock)
3322 addUnwrappedLine();
3323 }
3324 parseNamespaceOrExportBlock(/*AddLevels=*/Style.IndentExportBlock ? 1 : 0);
3325}
3326
3327void UnwrappedLineParser::parseNew() {
3328 assert(FormatTok->is(tok::kw_new) && "'new' expected");
3329 nextToken();
3330
3331 if (Style.isCSharp()) {
3332 do {
3333 // Handle constructor invocation, e.g. `new(field: value)`.
3334 if (FormatTok->is(Kind: tok::l_paren))
3335 parseParens();
3336
3337 // Handle array initialization syntax, e.g. `new[] {10, 20, 30}`.
3338 if (FormatTok->is(Kind: tok::l_brace))
3339 parseBracedList();
3340
3341 if (FormatTok->isOneOf(K1: tok::semi, K2: tok::comma))
3342 return;
3343
3344 nextToken();
3345 } while (!eof());
3346 }
3347
3348 if (!Style.isJava())
3349 return;
3350
3351 // In Java, we can parse everything up to the parens, which aren't optional.
3352 do {
3353 // There should not be a ;, { or } before the new's open paren.
3354 if (FormatTok->isOneOf(K1: tok::semi, K2: tok::l_brace, Ks: tok::r_brace))
3355 return;
3356
3357 // Consume the parens.
3358 if (FormatTok->is(Kind: tok::l_paren)) {
3359 parseParens();
3360
3361 // If there is a class body of an anonymous class, consume that as child.
3362 if (FormatTok->is(Kind: tok::l_brace))
3363 parseChildBlock();
3364 return;
3365 }
3366 nextToken();
3367 } while (!eof());
3368}
3369
3370void UnwrappedLineParser::parseLoopBody(bool KeepBraces, bool WrapRightBrace) {
3371 keepAncestorBraces();
3372
3373 if (isBlockBegin(Tok: *FormatTok)) {
3374 FormatTok->setFinalizedType(TT_ControlStatementLBrace);
3375 FormatToken *LeftBrace = FormatTok;
3376 CompoundStatementIndenter Indenter(this, Style, Line->Level);
3377 parseBlock(/*MustBeDeclaration=*/false, /*AddLevels=*/1u,
3378 /*MunchSemi=*/true, KeepBraces);
3379 setPreviousRBraceType(TT_ControlStatementRBrace);
3380 if (!KeepBraces) {
3381 assert(!NestedTooDeep.empty());
3382 if (!NestedTooDeep.back())
3383 markOptionalBraces(LeftBrace);
3384 }
3385 if (WrapRightBrace)
3386 addUnwrappedLine();
3387 } else {
3388 parseUnbracedBody();
3389 }
3390
3391 if (!KeepBraces)
3392 NestedTooDeep.pop_back();
3393}
3394
3395void UnwrappedLineParser::parseForOrWhileLoop(bool HasParens) {
3396 assert((FormatTok->isOneOf(tok::kw_for, tok::kw_while, TT_ForEachMacro) ||
3397 (Style.isVerilog() &&
3398 FormatTok->isOneOf(Keywords.kw_always, Keywords.kw_always_comb,
3399 Keywords.kw_always_ff, Keywords.kw_always_latch,
3400 Keywords.kw_final, Keywords.kw_initial,
3401 Keywords.kw_foreach, Keywords.kw_forever,
3402 Keywords.kw_repeat))) &&
3403 "'for', 'while' or foreach macro expected");
3404 const bool KeepBraces = !Style.RemoveBracesLLVM ||
3405 FormatTok->isNoneOf(Ks: tok::kw_for, Ks: tok::kw_while);
3406
3407 nextToken();
3408 // JS' for await ( ...
3409 if (Style.isJavaScript() && FormatTok->is(II: Keywords.kw_await))
3410 nextToken();
3411 if (IsCpp && FormatTok->is(Kind: tok::kw_co_await))
3412 nextToken();
3413 if (HasParens && FormatTok->is(Kind: tok::l_paren)) {
3414 // The type is only set for Verilog basically because we were afraid to
3415 // change the existing behavior for loops. See the discussion on D121756 for
3416 // details.
3417 if (Style.isVerilog())
3418 FormatTok->setFinalizedType(TT_ConditionLParen);
3419 parseParens();
3420 }
3421
3422 if (Style.isVerilog()) {
3423 // Event control.
3424 parseVerilogSensitivityList();
3425 } else if (Style.AllowShortLoopsOnASingleLine && FormatTok->is(Kind: tok::semi) &&
3426 Tokens->getPreviousToken()->is(Kind: tok::r_paren)) {
3427 nextToken();
3428 addUnwrappedLine();
3429 return;
3430 }
3431
3432 handleAttributes();
3433 parseLoopBody(KeepBraces, /*WrapRightBrace=*/true);
3434}
3435
3436void UnwrappedLineParser::parseDoWhile() {
3437 assert(FormatTok->is(tok::kw_do) && "'do' expected");
3438 nextToken();
3439
3440 parseLoopBody(/*KeepBraces=*/true, WrapRightBrace: Style.BraceWrapping.BeforeWhile);
3441
3442 // FIXME: Add error handling.
3443 if (FormatTok->isNot(Kind: tok::kw_while)) {
3444 addUnwrappedLine();
3445 return;
3446 }
3447
3448 FormatTok->setFinalizedType(TT_DoWhile);
3449
3450 // If in Whitesmiths mode, the line with the while() needs to be indented
3451 // to the same level as the block.
3452 if (Style.BreakBeforeBraces == FormatStyle::BS_Whitesmiths)
3453 ++Line->Level;
3454
3455 nextToken();
3456 parseStructuralElement();
3457}
3458
3459void UnwrappedLineParser::parseLabel(bool IsGotoLabel) {
3460 nextToken();
3461
3462 const auto IndentGotoLabel = Style.IndentGotoLabels;
3463 const auto OldLineLevel = Line->Level;
3464 auto &Level = Line->Level;
3465
3466 if (IsGotoLabel && IndentGotoLabel == FormatStyle::IGLS_NoIndent)
3467 Level = 0;
3468
3469 if (!IsGotoLabel || IndentGotoLabel == FormatStyle::IGLS_OuterIndent) {
3470 if (OldLineLevel > 1 || (!Line->InPPDirective && OldLineLevel > 0))
3471 --Level;
3472 }
3473
3474 if (!IsGotoLabel && !Style.IndentCaseBlocks &&
3475 CommentsBeforeNextToken.empty() && FormatTok->is(Kind: tok::l_brace)) {
3476 CompoundStatementIndenter Indenter(this, Level,
3477 Style.BraceWrapping.AfterCaseLabel,
3478 Style.BraceWrapping.IndentBraces);
3479 parseBlock();
3480 if (FormatTok->is(Kind: tok::kw_break)) {
3481 if (Style.BraceWrapping.AfterControlStatement ==
3482 FormatStyle::BWACS_Always) {
3483 addUnwrappedLine();
3484 if (!Style.IndentCaseBlocks &&
3485 Style.BreakBeforeBraces == FormatStyle::BS_Whitesmiths) {
3486 ++Level;
3487 }
3488 }
3489 parseStructuralElement();
3490 }
3491 addUnwrappedLine();
3492 } else {
3493 if (FormatTok->is(Kind: tok::semi))
3494 nextToken();
3495 addUnwrappedLine();
3496 }
3497
3498 Level = OldLineLevel;
3499
3500 if (FormatTok->isNot(Kind: tok::l_brace)) {
3501 parseStructuralElement();
3502 addUnwrappedLine();
3503 }
3504}
3505
3506void UnwrappedLineParser::parseCaseLabel() {
3507 assert(FormatTok->is(tok::kw_case) && "'case' expected");
3508 auto *Case = FormatTok;
3509
3510 // FIXME: fix handling of complex expressions here.
3511 do {
3512 nextToken();
3513 if (FormatTok->is(Kind: tok::colon)) {
3514 FormatTok->setFinalizedType(TT_CaseLabelColon);
3515 break;
3516 }
3517 if (Style.isJava() && FormatTok->is(Kind: tok::arrow)) {
3518 FormatTok->setFinalizedType(TT_CaseLabelArrow);
3519 Case->setFinalizedType(TT_SwitchExpressionLabel);
3520 break;
3521 }
3522 } while (!eof());
3523 parseLabel();
3524}
3525
3526void UnwrappedLineParser::parseSwitch(bool IsExpr) {
3527 assert(FormatTok->is(tok::kw_switch) && "'switch' expected");
3528 nextToken();
3529 if (FormatTok->is(Kind: tok::l_paren))
3530 parseParens();
3531
3532 keepAncestorBraces();
3533
3534 if (FormatTok->is(Kind: tok::l_brace)) {
3535 CompoundStatementIndenter Indenter(this, Style, Line->Level);
3536 FormatTok->setFinalizedType(IsExpr ? TT_SwitchExpressionLBrace
3537 : TT_ControlStatementLBrace);
3538 if (IsExpr)
3539 parseChildBlock();
3540 else
3541 parseBlock();
3542 setPreviousRBraceType(TT_ControlStatementRBrace);
3543 if (!IsExpr)
3544 addUnwrappedLine();
3545 } else {
3546 addUnwrappedLine();
3547 ++Line->Level;
3548 parseStructuralElement();
3549 --Line->Level;
3550 }
3551
3552 if (Style.RemoveBracesLLVM)
3553 NestedTooDeep.pop_back();
3554}
3555
3556void UnwrappedLineParser::parseAccessSpecifier() {
3557 nextToken();
3558 // Understand Qt's slots.
3559 if (FormatTok->isOneOf(K1: Keywords.kw_slots, K2: Keywords.kw_qslots))
3560 nextToken();
3561 // Otherwise, we don't know what it is, and we'd better keep the next token.
3562 if (FormatTok->is(Kind: tok::colon))
3563 nextToken();
3564 addUnwrappedLine();
3565}
3566
3567/// Parses a requires, decides if it is a clause or an expression.
3568/// \pre The current token has to be the requires keyword.
3569/// \returns true if it parsed a clause.
3570bool UnwrappedLineParser::parseRequires(bool SeenEqual) {
3571 assert(FormatTok->is(tok::kw_requires) && "'requires' expected");
3572
3573 // We try to guess if it is a requires clause, or a requires expression. For
3574 // that we first check the next token.
3575 switch (Tokens->peekNextToken(/*SkipComment=*/true)->Tok.getKind()) {
3576 case tok::l_brace:
3577 // This can only be an expression, never a clause.
3578 parseRequiresExpression();
3579 return false;
3580 case tok::l_paren:
3581 // Clauses and expression can start with a paren, it's unclear what we have.
3582 break;
3583 default:
3584 // All other tokens can only be a clause.
3585 parseRequiresClause();
3586 return true;
3587 }
3588
3589 // Looking forward we would have to decide if there are function declaration
3590 // like arguments to the requires expression:
3591 // requires (T t) {
3592 // Or there is a constraint expression for the requires clause:
3593 // requires (C<T> && ...
3594
3595 // But first let's look behind.
3596 auto *PreviousNonComment = FormatTok->getPreviousNonComment();
3597
3598 if (!PreviousNonComment ||
3599 PreviousNonComment->is(TT: TT_RequiresExpressionLBrace)) {
3600 // If there is no token, or an expression left brace, we are a requires
3601 // clause within a requires expression.
3602 parseRequiresClause();
3603 return true;
3604 }
3605
3606 switch (PreviousNonComment->Tok.getKind()) {
3607 case tok::greater:
3608 case tok::r_paren:
3609 case tok::kw_noexcept:
3610 case tok::kw_const:
3611 case tok::star:
3612 case tok::amp:
3613 // This is a requires clause.
3614 parseRequiresClause();
3615 return true;
3616 case tok::ampamp: {
3617 // This can be either:
3618 // if (... && requires (T t) ...)
3619 // Or
3620 // void member(...) && requires (C<T> ...
3621 // We check the one token before that for a const:
3622 // void member(...) const && requires (C<T> ...
3623 auto PrevPrev = PreviousNonComment->getPreviousNonComment();
3624 if ((PrevPrev && PrevPrev->is(Kind: tok::kw_const)) || !SeenEqual) {
3625 parseRequiresClause();
3626 return true;
3627 }
3628 break;
3629 }
3630 default:
3631 if (PreviousNonComment->isTypeOrIdentifier(LangOpts)) {
3632 // This is a requires clause.
3633 parseRequiresClause();
3634 return true;
3635 }
3636 // It's an expression.
3637 parseRequiresExpression();
3638 return false;
3639 }
3640
3641 // Now we look forward and try to check if the paren content is a parameter
3642 // list. The parameters can be cv-qualified and contain references or
3643 // pointers.
3644 // So we want basically to check for TYPE NAME, but TYPE can contain all kinds
3645 // of stuff: typename, const, *, &, &&, ::, identifiers.
3646
3647 unsigned StoredPosition = Tokens->getPosition();
3648 FormatToken *NextToken = Tokens->getNextToken();
3649 int Lookahead = 0;
3650 auto PeekNext = [&Lookahead, &NextToken, this] {
3651 ++Lookahead;
3652 NextToken = Tokens->getNextToken();
3653 };
3654
3655 bool FoundType = false;
3656 bool LastWasColonColon = false;
3657 int OpenAngles = 0;
3658
3659 for (; Lookahead < 50; PeekNext()) {
3660 switch (NextToken->Tok.getKind()) {
3661 case tok::kw_volatile:
3662 case tok::kw_const:
3663 case tok::comma:
3664 if (OpenAngles == 0) {
3665 FormatTok = Tokens->setPosition(StoredPosition);
3666 parseRequiresExpression();
3667 return false;
3668 }
3669 break;
3670 case tok::eof:
3671 // Break out of the loop.
3672 Lookahead = 50;
3673 break;
3674 case tok::coloncolon:
3675 LastWasColonColon = true;
3676 break;
3677 case tok::kw_decltype:
3678 case tok::identifier:
3679 if (FoundType && !LastWasColonColon && OpenAngles == 0) {
3680 FormatTok = Tokens->setPosition(StoredPosition);
3681 parseRequiresExpression();
3682 return false;
3683 }
3684 FoundType = true;
3685 LastWasColonColon = false;
3686 break;
3687 case tok::less:
3688 ++OpenAngles;
3689 break;
3690 case tok::greater:
3691 --OpenAngles;
3692 break;
3693 default:
3694 if (NextToken->isTypeName(LangOpts)) {
3695 FormatTok = Tokens->setPosition(StoredPosition);
3696 parseRequiresExpression();
3697 return false;
3698 }
3699 break;
3700 }
3701 }
3702 // This seems to be a complicated expression, just assume it's a clause.
3703 FormatTok = Tokens->setPosition(StoredPosition);
3704 parseRequiresClause();
3705 return true;
3706}
3707
3708/// Parses a requires clause.
3709/// \sa parseRequiresExpression
3710///
3711/// Returns if it either has finished parsing the clause, or it detects, that
3712/// the clause is incorrect.
3713void UnwrappedLineParser::parseRequiresClause() {
3714 assert(FormatTok->is(tok::kw_requires) && "'requires' expected");
3715
3716 // If there is no previous token, we are within a requires expression,
3717 // otherwise we will always have the template or function declaration in front
3718 // of it.
3719 bool InRequiresExpression =
3720 !FormatTok->Previous ||
3721 FormatTok->Previous->is(TT: TT_RequiresExpressionLBrace);
3722
3723 FormatTok->setFinalizedType(InRequiresExpression
3724 ? TT_RequiresClauseInARequiresExpression
3725 : TT_RequiresClause);
3726 nextToken();
3727
3728 // NOTE: parseConstraintExpression is only ever called from this function.
3729 // It could be inlined into here.
3730 parseConstraintExpression();
3731
3732 if (!InRequiresExpression && FormatTok->Previous)
3733 FormatTok->Previous->ClosesRequiresClause = true;
3734}
3735
3736/// Parses a requires expression.
3737/// \sa parseRequiresClause
3738///
3739/// Returns if it either has finished parsing the expression, or it detects,
3740/// that the expression is incorrect.
3741void UnwrappedLineParser::parseRequiresExpression() {
3742 assert(FormatTok->is(tok::kw_requires) && "'requires' expected");
3743
3744 FormatTok->setFinalizedType(TT_RequiresExpression);
3745 nextToken();
3746
3747 if (FormatTok->is(Kind: tok::l_paren)) {
3748 FormatTok->setFinalizedType(TT_RequiresExpressionLParen);
3749 parseParens();
3750 }
3751
3752 if (FormatTok->is(Kind: tok::l_brace)) {
3753 FormatTok->setFinalizedType(TT_RequiresExpressionLBrace);
3754 parseChildBlock();
3755 }
3756}
3757
3758/// Parses a constraint expression.
3759///
3760/// This is the body of a requires clause. It returns, when the parsing is
3761/// complete, or the expression is incorrect.
3762void UnwrappedLineParser::parseConstraintExpression() {
3763 // The special handling for lambdas is needed since tryToParseLambda() eats a
3764 // token and if a requires expression is the last part of a requires clause
3765 // and followed by an attribute like [[nodiscard]] the ClosesRequiresClause is
3766 // not set on the correct token. Thus we need to be aware if we even expect a
3767 // lambda to be possible.
3768 // template <typename T> requires requires { ... } [[nodiscard]] ...;
3769 bool LambdaNextTimeAllowed = true;
3770
3771 // Within lambda declarations, it is permitted to put a requires clause after
3772 // its template parameter list, which would place the requires clause right
3773 // before the parentheses of the parameters of the lambda declaration. Thus,
3774 // we track if we expect to see grouping parentheses at all.
3775 // Without this check, `requires foo<T> (T t)` in the below example would be
3776 // seen as the whole requires clause, accidentally eating the parameters of
3777 // the lambda.
3778 // [&]<typename T> requires foo<T> (T t) { ... };
3779 bool TopLevelParensAllowed = true;
3780
3781 do {
3782 bool LambdaThisTimeAllowed = std::exchange(obj&: LambdaNextTimeAllowed, new_val: false);
3783
3784 switch (FormatTok->Tok.getKind()) {
3785 case tok::kw_requires:
3786 parseRequiresExpression();
3787 break;
3788
3789 case tok::l_paren:
3790 if (!TopLevelParensAllowed)
3791 return;
3792 parseParens(/*AmpAmpTokenType=*/StarAndAmpTokenType: TT_BinaryOperator);
3793 TopLevelParensAllowed = false;
3794 break;
3795
3796 case tok::l_square:
3797 if (!LambdaThisTimeAllowed || !tryToParseLambda())
3798 return;
3799 break;
3800
3801 case tok::kw_const:
3802 case tok::semi:
3803 case tok::kw_class:
3804 case tok::kw_struct:
3805 case tok::kw_union:
3806 return;
3807
3808 case tok::l_brace:
3809 // Potential function body.
3810 return;
3811
3812 case tok::ampamp:
3813 case tok::pipepipe:
3814 FormatTok->setFinalizedType(TT_BinaryOperator);
3815 nextToken();
3816 LambdaNextTimeAllowed = true;
3817 TopLevelParensAllowed = true;
3818 break;
3819
3820 case tok::comma:
3821 case tok::comment:
3822 LambdaNextTimeAllowed = LambdaThisTimeAllowed;
3823 nextToken();
3824 break;
3825
3826 case tok::kw_sizeof:
3827 case tok::greater:
3828 case tok::greaterequal:
3829 case tok::greatergreater:
3830 case tok::less:
3831 case tok::lessequal:
3832 case tok::lessless:
3833 case tok::equalequal:
3834 case tok::exclaim:
3835 case tok::exclaimequal:
3836 case tok::plus:
3837 case tok::minus:
3838 case tok::star:
3839 case tok::slash:
3840 LambdaNextTimeAllowed = true;
3841 TopLevelParensAllowed = true;
3842 // Just eat them.
3843 nextToken();
3844 break;
3845
3846 case tok::numeric_constant:
3847 case tok::coloncolon:
3848 case tok::kw_true:
3849 case tok::kw_false:
3850 TopLevelParensAllowed = false;
3851 // Just eat them.
3852 nextToken();
3853 break;
3854
3855 case tok::kw_static_cast:
3856 case tok::kw_const_cast:
3857 case tok::kw_reinterpret_cast:
3858 case tok::kw_dynamic_cast:
3859 nextToken();
3860 if (FormatTok->isNot(Kind: tok::less))
3861 return;
3862
3863 nextToken();
3864 parseBracedList(/*IsAngleBracket=*/true);
3865 break;
3866
3867 default:
3868 if (!FormatTok->Tok.getIdentifierInfo()) {
3869 // Identifiers are part of the default case, we check for more then
3870 // tok::identifier to handle builtin type traits.
3871 return;
3872 }
3873
3874 // We need to differentiate identifiers for a template deduction guide,
3875 // variables, or function return types (the constraint expression has
3876 // ended before that), and basically all other cases. But it's easier to
3877 // check the other way around.
3878 assert(FormatTok->Previous);
3879 switch (FormatTok->Previous->Tok.getKind()) {
3880 case tok::coloncolon: // Nested identifier.
3881 case tok::ampamp: // Start of a function or variable for the
3882 case tok::pipepipe: // constraint expression. (binary)
3883 case tok::exclaim: // The same as above, but unary.
3884 case tok::kw_requires: // Initial identifier of a requires clause.
3885 case tok::equal: // Initial identifier of a concept declaration.
3886 case tok::kw_template: // A dependent template.
3887 break;
3888 default:
3889 return;
3890 }
3891
3892 // Read identifier with optional template declaration.
3893 nextToken();
3894 if (FormatTok->is(Kind: tok::less)) {
3895 nextToken();
3896 parseBracedList(/*IsAngleBracket=*/true);
3897 }
3898 TopLevelParensAllowed = false;
3899 break;
3900 }
3901 } while (!eof());
3902}
3903
3904bool UnwrappedLineParser::parseEnum() {
3905 const FormatToken &InitialToken = *FormatTok;
3906
3907 // Won't be 'enum' for NS_ENUMs.
3908 if (FormatTok->is(Kind: tok::kw_enum))
3909 nextToken();
3910
3911 // In TypeScript, "enum" can also be used as property name, e.g. in interface
3912 // declarations. An "enum" keyword followed by a colon would be a syntax
3913 // error and thus assume it is just an identifier.
3914 if (Style.isJavaScript() && FormatTok->isOneOf(K1: tok::colon, K2: tok::question))
3915 return false;
3916
3917 // In protobuf, "enum" can be used as a field name.
3918 if (Style.Language == FormatStyle::LK_Proto && FormatTok->is(Kind: tok::equal))
3919 return false;
3920
3921 if (IsCpp) {
3922 // Eat up enum class ...
3923 if (FormatTok->isOneOf(K1: tok::kw_class, K2: tok::kw_struct))
3924 nextToken();
3925 while (FormatTok->is(Kind: tok::l_square))
3926 if (!handleCppAttributes())
3927 return false;
3928 }
3929
3930 while (FormatTok->Tok.getIdentifierInfo() ||
3931 FormatTok->isOneOf(K1: tok::colon, K2: tok::coloncolon, Ks: tok::less,
3932 Ks: tok::greater, Ks: tok::comma, Ks: tok::question,
3933 Ks: tok::l_square)) {
3934 if (FormatTok->is(Kind: tok::colon))
3935 FormatTok->setFinalizedType(TT_EnumUnderlyingTypeColon);
3936 if (Style.isVerilog()) {
3937 FormatTok->setFinalizedType(TT_VerilogDimensionedTypeName);
3938 nextToken();
3939 // In Verilog the base type can have dimensions.
3940 while (FormatTok->is(Kind: tok::l_square))
3941 parseSquare();
3942 } else {
3943 nextToken();
3944 }
3945 // We can have macros or attributes in between 'enum' and the enum name.
3946 if (FormatTok->is(Kind: tok::l_paren))
3947 parseParens();
3948 if (FormatTok->is(Kind: tok::identifier)) {
3949 nextToken();
3950 // If there are two identifiers in a row, this is likely an elaborate
3951 // return type. In Java, this can be "implements", etc.
3952 if (IsCpp && FormatTok->is(Kind: tok::identifier))
3953 return false;
3954 }
3955 }
3956
3957 // Just a declaration or something is wrong.
3958 if (FormatTok->isNot(Kind: tok::l_brace))
3959 return true;
3960 FormatTok->setFinalizedType(TT_EnumLBrace);
3961 FormatTok->setBlockKind(BK_Block);
3962
3963 if (Style.isJava()) {
3964 // Java enums are different.
3965 parseJavaEnumBody();
3966 return true;
3967 }
3968 if (Style.Language == FormatStyle::LK_Proto) {
3969 parseBlock(/*MustBeDeclaration=*/true);
3970 return true;
3971 }
3972
3973 const bool ManageWhitesmithsBraces =
3974 Style.BreakBeforeBraces == FormatStyle::BS_Whitesmiths;
3975
3976 if (!Style.AllowShortEnumsOnASingleLine &&
3977 ShouldBreakBeforeBrace(Style, InitialToken,
3978 IsEmptyBlock: Tokens->peekNextToken()->is(Kind: tok::r_brace))) {
3979 addUnwrappedLine();
3980
3981 // If we're in Whitesmiths mode, indent the brace if we're not indenting
3982 // the whole block.
3983 if (ManageWhitesmithsBraces)
3984 ++Line->Level;
3985 }
3986 // Parse enum body.
3987 nextToken();
3988 if (!Style.AllowShortEnumsOnASingleLine) {
3989 addUnwrappedLine();
3990 if (!ManageWhitesmithsBraces)
3991 ++Line->Level;
3992 }
3993 const auto OpeningLineIndex = CurrentLines->empty()
3994 ? UnwrappedLine::kInvalidIndex
3995 : CurrentLines->size() - 1;
3996 bool HasError = !parseBracedList(/*IsAngleBracket=*/false, /*IsEnum=*/true);
3997 if (!Style.AllowShortEnumsOnASingleLine && !ManageWhitesmithsBraces)
3998 --Line->Level;
3999 if (HasError) {
4000 if (FormatTok->is(Kind: tok::semi))
4001 nextToken();
4002 addUnwrappedLine();
4003 }
4004 setPreviousRBraceType(TT_EnumRBrace);
4005 if (ManageWhitesmithsBraces)
4006 Line->MatchingOpeningBlockLineIndex = OpeningLineIndex;
4007 return true;
4008
4009 // There is no addUnwrappedLine() here so that we fall through to parsing a
4010 // structural element afterwards. Thus, in "enum A {} n, m;",
4011 // "} n, m;" will end up in one unwrapped line.
4012}
4013
4014bool UnwrappedLineParser::parseStructLike() {
4015 // parseRecord falls through and does not yet add an unwrapped line as a
4016 // record declaration or definition can start a structural element.
4017 parseRecord();
4018 // This does not apply to Java, JavaScript and C#.
4019 if (Style.isJava() || Style.isJavaScript() || Style.isCSharp()) {
4020 if (FormatTok->is(Kind: tok::semi))
4021 nextToken();
4022 addUnwrappedLine();
4023 return true;
4024 }
4025 return false;
4026}
4027
4028namespace {
4029// A class used to set and restore the Token position when peeking
4030// ahead in the token source.
4031class ScopedTokenPosition {
4032 unsigned StoredPosition;
4033 FormatTokenSource *Tokens;
4034
4035public:
4036 ScopedTokenPosition(FormatTokenSource *Tokens) : Tokens(Tokens) {
4037 assert(Tokens && "Tokens expected to not be null");
4038 StoredPosition = Tokens->getPosition();
4039 }
4040
4041 ~ScopedTokenPosition() { Tokens->setPosition(StoredPosition); }
4042};
4043} // namespace
4044
4045// Look to see if we have [[ by looking ahead, if
4046// its not then rewind to the original position.
4047bool UnwrappedLineParser::tryToParseSimpleAttribute() {
4048 ScopedTokenPosition AutoPosition(Tokens);
4049 FormatToken *Tok = Tokens->getNextToken();
4050 // We already read the first [ check for the second.
4051 if (Tok->isNot(Kind: tok::l_square))
4052 return false;
4053 // Double check that the attribute is just something
4054 // fairly simple.
4055 while (Tok->isNot(Kind: tok::eof)) {
4056 if (Tok->is(Kind: tok::r_square))
4057 break;
4058 Tok = Tokens->getNextToken();
4059 }
4060 if (Tok->is(Kind: tok::eof))
4061 return false;
4062 Tok = Tokens->getNextToken();
4063 if (Tok->isNot(Kind: tok::r_square))
4064 return false;
4065 Tok = Tokens->getNextToken();
4066 if (Tok->is(Kind: tok::semi))
4067 return false;
4068 return true;
4069}
4070
4071void UnwrappedLineParser::parseJavaEnumBody() {
4072 assert(FormatTok->is(tok::l_brace));
4073 const FormatToken *OpeningBrace = FormatTok;
4074
4075 // Determine whether the enum is simple, i.e. does not have a semicolon or
4076 // constants with class bodies. Simple enums can be formatted like braced
4077 // lists, contracted to a single line, etc.
4078 unsigned StoredPosition = Tokens->getPosition();
4079 bool IsSimple = true;
4080 FormatToken *Tok = Tokens->getNextToken();
4081 while (Tok->isNot(Kind: tok::eof)) {
4082 if (Tok->is(Kind: tok::r_brace))
4083 break;
4084 if (Tok->isOneOf(K1: tok::l_brace, K2: tok::semi)) {
4085 IsSimple = false;
4086 break;
4087 }
4088 // FIXME: This will also mark enums with braces in the arguments to enum
4089 // constants as "not simple". This is probably fine in practice, though.
4090 Tok = Tokens->getNextToken();
4091 }
4092 FormatTok = Tokens->setPosition(StoredPosition);
4093
4094 if (IsSimple) {
4095 nextToken();
4096 parseBracedList();
4097 addUnwrappedLine();
4098 return;
4099 }
4100
4101 // Parse the body of a more complex enum.
4102 // First add a line for everything up to the "{".
4103 nextToken();
4104 addUnwrappedLine();
4105 ++Line->Level;
4106
4107 // Parse the enum constants.
4108 while (!eof()) {
4109 if (FormatTok->is(Kind: tok::l_brace)) {
4110 // Parse the constant's class body.
4111 parseBlock(/*MustBeDeclaration=*/true, /*AddLevels=*/1u,
4112 /*MunchSemi=*/false);
4113 } else if (FormatTok->is(Kind: tok::l_paren)) {
4114 parseParens();
4115 } else if (FormatTok->is(Kind: tok::comma)) {
4116 nextToken();
4117 addUnwrappedLine();
4118 } else if (FormatTok->is(Kind: tok::semi)) {
4119 nextToken();
4120 addUnwrappedLine();
4121 break;
4122 } else if (FormatTok->is(Kind: tok::r_brace)) {
4123 addUnwrappedLine();
4124 break;
4125 } else {
4126 nextToken();
4127 }
4128 }
4129
4130 // Parse the class body after the enum's ";" if any.
4131 parseLevel(OpeningBrace);
4132 nextToken();
4133 --Line->Level;
4134 addUnwrappedLine();
4135}
4136
4137void UnwrappedLineParser::parseRecord(bool ParseAsExpr, bool IsJavaRecord) {
4138 assert(!IsJavaRecord || FormatTok->is(Keywords.kw_record));
4139 const FormatToken &InitialToken = *FormatTok;
4140 nextToken();
4141
4142 FormatToken *ClassName =
4143 IsJavaRecord && FormatTok->is(Kind: tok::identifier) ? FormatTok : nullptr;
4144 bool IsDerived = false;
4145 auto IsNonMacroIdentifier = [](const FormatToken *Tok) {
4146 return Tok->is(Kind: tok::identifier) && Tok->TokenText != Tok->TokenText.upper();
4147 };
4148 // JavaScript/TypeScript supports anonymous classes like:
4149 // a = class extends foo { }
4150 bool JSPastExtendsOrImplements = false;
4151 // The actual identifier can be a nested name specifier, and in macros
4152 // it is often token-pasted.
4153 // An [[attribute]] can be before the identifier.
4154 while (FormatTok->isOneOf(K1: tok::identifier, K2: tok::coloncolon, Ks: tok::hashhash,
4155 Ks: tok::kw_alignas, Ks: tok::l_square) ||
4156 FormatTok->isAttribute() ||
4157 ((Style.isJava() || Style.isJavaScript()) &&
4158 FormatTok->isOneOf(K1: tok::period, K2: tok::comma)) ||
4159 (Style.isVerilog() &&
4160 FormatTok->isOneOf(K1: tok::kw_signed, K2: tok::kw_unsigned))) {
4161 if (Style.isJavaScript() &&
4162 FormatTok->isOneOf(K1: Keywords.kw_extends, K2: Keywords.kw_implements)) {
4163 JSPastExtendsOrImplements = true;
4164 // JavaScript/TypeScript supports inline object types in
4165 // extends/implements positions:
4166 // class Foo implements {bar: number} { }
4167 nextToken();
4168 if (FormatTok->is(Kind: tok::l_brace)) {
4169 tryToParseBracedList();
4170 continue;
4171 }
4172 }
4173 if (FormatTok->is(Kind: tok::l_square) && handleCppAttributes())
4174 continue;
4175 auto *Previous = FormatTok;
4176 nextToken();
4177 switch (FormatTok->Tok.getKind()) {
4178 case tok::l_paren:
4179 // We can have macros in between 'class' and the class name.
4180 if (IsJavaRecord || !IsNonMacroIdentifier(Previous) ||
4181 // e.g. `struct macro(a) S { int i; };`
4182 Previous->Previous == &InitialToken) {
4183 parseParens();
4184 }
4185 break;
4186 case tok::coloncolon:
4187 case tok::hashhash:
4188 break;
4189 default:
4190 if (JSPastExtendsOrImplements || ClassName ||
4191 Previous->isNot(Kind: tok::identifier) || Previous->is(TT: TT_AttributeMacro)) {
4192 break;
4193 }
4194 if (const auto Text = Previous->TokenText;
4195 Text.size() == 1 || Text != Text.upper()) {
4196 ClassName = Previous;
4197 }
4198 }
4199 }
4200
4201 auto IsListInitialization = [&] {
4202 if (!ClassName || IsDerived || JSPastExtendsOrImplements)
4203 return false;
4204 assert(FormatTok->is(tok::l_brace));
4205 const auto *Prev = FormatTok->getPreviousNonComment();
4206 assert(Prev);
4207 return Prev != ClassName && Prev->is(Kind: tok::identifier) &&
4208 Prev->isNot(Kind: Keywords.kw_final) && tryToParseBracedList();
4209 };
4210
4211 if (FormatTok->isOneOf(K1: tok::colon, K2: tok::less)) {
4212 int AngleNestingLevel = 0;
4213 do {
4214 if (FormatTok->is(Kind: tok::less))
4215 ++AngleNestingLevel;
4216 else if (FormatTok->is(Kind: tok::greater))
4217 --AngleNestingLevel;
4218
4219 if (AngleNestingLevel == 0) {
4220 if (FormatTok->is(Kind: tok::colon)) {
4221 IsDerived = true;
4222 } else if (!IsDerived && FormatTok->is(Kind: tok::identifier) &&
4223 FormatTok->Previous->is(Kind: tok::coloncolon)) {
4224 ClassName = FormatTok;
4225 } else if (FormatTok->is(Kind: tok::l_paren) &&
4226 IsNonMacroIdentifier(FormatTok->Previous)) {
4227 break;
4228 }
4229 }
4230 if (FormatTok->is(Kind: tok::l_brace)) {
4231 if (AngleNestingLevel == 0 && IsListInitialization())
4232 return;
4233 calculateBraceTypes(/*ExpectClassBody=*/true);
4234 if (!tryToParseBracedList())
4235 break;
4236 }
4237 if (FormatTok->is(Kind: tok::l_square)) {
4238 FormatToken *Previous = FormatTok->Previous;
4239 if (!Previous || (Previous->isNot(Kind: tok::r_paren) &&
4240 !Previous->isTypeOrIdentifier(LangOpts))) {
4241 // Don't try parsing a lambda if we had a closing parenthesis before,
4242 // it was probably a pointer to an array: int (*)[].
4243 if (!tryToParseLambda())
4244 continue;
4245 } else {
4246 parseSquare();
4247 continue;
4248 }
4249 }
4250 if (FormatTok->is(Kind: tok::semi))
4251 return;
4252 if (Style.isCSharp() && FormatTok->is(II: Keywords.kw_where)) {
4253 addUnwrappedLine();
4254 nextToken();
4255 parseCSharpGenericTypeConstraint();
4256 break;
4257 }
4258 nextToken();
4259 } while (!eof());
4260 }
4261
4262 auto GetBraceTypes =
4263 [](const FormatToken &RecordTok) -> std::pair<TokenType, TokenType> {
4264 switch (RecordTok.Tok.getKind()) {
4265 case tok::kw_class:
4266 return {TT_ClassLBrace, TT_ClassRBrace};
4267 case tok::kw_struct:
4268 return {TT_StructLBrace, TT_StructRBrace};
4269 case tok::kw_union:
4270 return {TT_UnionLBrace, TT_UnionRBrace};
4271 default:
4272 // Useful for e.g. interface.
4273 return {TT_RecordLBrace, TT_RecordRBrace};
4274 }
4275 };
4276 if (FormatTok->is(Kind: tok::l_brace)) {
4277 if (IsListInitialization())
4278 return;
4279 if (ClassName)
4280 ClassName->setFinalizedType(TT_ClassHeadName);
4281 auto [OpenBraceType, ClosingBraceType] = GetBraceTypes(InitialToken);
4282 FormatTok->setFinalizedType(OpenBraceType);
4283 if (ParseAsExpr) {
4284 parseChildBlock();
4285 } else {
4286 if (ShouldBreakBeforeBrace(Style, InitialToken,
4287 IsEmptyBlock: Tokens->peekNextToken()->is(Kind: tok::r_brace),
4288 IsJavaRecord)) {
4289 addUnwrappedLine();
4290 }
4291
4292 unsigned AddLevels = Style.IndentAccessModifiers ? 2u : 1u;
4293 parseBlock(/*MustBeDeclaration=*/true, AddLevels, /*MunchSemi=*/false);
4294 }
4295 setPreviousRBraceType(ClosingBraceType);
4296 }
4297 // There is no addUnwrappedLine() here so that we fall through to parsing a
4298 // structural element afterwards. Thus, in "class A {} n, m;",
4299 // "} n, m;" will end up in one unwrapped line.
4300}
4301
4302void UnwrappedLineParser::parseObjCMethod() {
4303 assert(FormatTok->isOneOf(tok::l_paren, tok::identifier) &&
4304 "'(' or identifier expected.");
4305 do {
4306 if (FormatTok->is(Kind: tok::semi)) {
4307 nextToken();
4308 addUnwrappedLine();
4309 return;
4310 } else if (FormatTok->is(Kind: tok::l_brace)) {
4311 if (Style.BraceWrapping.AfterFunction)
4312 addUnwrappedLine();
4313 parseBlock();
4314 addUnwrappedLine();
4315 return;
4316 } else {
4317 nextToken();
4318 }
4319 } while (!eof());
4320}
4321
4322void UnwrappedLineParser::parseObjCProtocolList() {
4323 assert(FormatTok->is(tok::less) && "'<' expected.");
4324 do {
4325 nextToken();
4326 // Early exit in case someone forgot a close angle.
4327 if (FormatTok->isOneOf(K1: tok::semi, K2: tok::l_brace, Ks: tok::objc_end))
4328 return;
4329 } while (!eof() && FormatTok->isNot(Kind: tok::greater));
4330 nextToken(); // Skip '>'.
4331}
4332
4333void UnwrappedLineParser::parseObjCUntilAtEnd() {
4334 do {
4335 if (FormatTok->is(Kind: tok::objc_end)) {
4336 nextToken();
4337 addUnwrappedLine();
4338 break;
4339 }
4340 if (FormatTok->is(Kind: tok::l_brace)) {
4341 parseBlock();
4342 // In ObjC interfaces, nothing should be following the "}".
4343 addUnwrappedLine();
4344 } else if (FormatTok->is(Kind: tok::r_brace)) {
4345 // Ignore stray "}". parseStructuralElement doesn't consume them.
4346 nextToken();
4347 addUnwrappedLine();
4348 } else if (FormatTok->isOneOf(K1: tok::minus, K2: tok::plus)) {
4349 nextToken();
4350 if (FormatTok->isOneOf(K1: tok::l_paren, K2: tok::identifier))
4351 parseObjCMethod();
4352 } else {
4353 parseStructuralElement();
4354 }
4355 } while (!eof());
4356}
4357
4358void UnwrappedLineParser::parseObjCInterfaceOrImplementation() {
4359 assert(FormatTok->isOneOf(tok::objc_interface, tok::objc_implementation));
4360 nextToken();
4361 nextToken(); // interface name
4362
4363 // @interface can be followed by a lightweight generic
4364 // specialization list, then either a base class or a category.
4365 if (FormatTok->is(Kind: tok::less))
4366 parseObjCLightweightGenerics();
4367 if (FormatTok->is(Kind: tok::colon)) {
4368 nextToken();
4369 nextToken(); // base class name
4370 // The base class can also have lightweight generics applied to it.
4371 if (FormatTok->is(Kind: tok::less))
4372 parseObjCLightweightGenerics();
4373 } else if (FormatTok->is(Kind: tok::l_paren)) {
4374 // Skip category, if present.
4375 parseParens();
4376 }
4377
4378 if (FormatTok->is(Kind: tok::less))
4379 parseObjCProtocolList();
4380
4381 if (FormatTok->is(Kind: tok::l_brace)) {
4382 if (Style.BraceWrapping.AfterObjCDeclaration)
4383 addUnwrappedLine();
4384 parseBlock(/*MustBeDeclaration=*/true);
4385 }
4386
4387 // With instance variables, this puts '}' on its own line. Without instance
4388 // variables, this ends the @interface line.
4389 addUnwrappedLine();
4390
4391 parseObjCUntilAtEnd();
4392}
4393
4394void UnwrappedLineParser::parseObjCLightweightGenerics() {
4395 assert(FormatTok->is(tok::less));
4396 // Unlike protocol lists, generic parameterizations support
4397 // nested angles:
4398 //
4399 // @interface Foo<ValueType : id <NSCopying, NSSecureCoding>> :
4400 // NSObject <NSCopying, NSSecureCoding>
4401 //
4402 // so we need to count how many open angles we have left.
4403 unsigned NumOpenAngles = 1;
4404 do {
4405 nextToken();
4406 // Early exit in case someone forgot a close angle.
4407 if (FormatTok->isOneOf(K1: tok::semi, K2: tok::l_brace, Ks: tok::objc_end))
4408 break;
4409 if (FormatTok->is(Kind: tok::less)) {
4410 ++NumOpenAngles;
4411 } else if (FormatTok->is(Kind: tok::greater)) {
4412 assert(NumOpenAngles > 0 && "'>' makes NumOpenAngles negative");
4413 --NumOpenAngles;
4414 }
4415 } while (!eof() && NumOpenAngles != 0);
4416 nextToken(); // Skip '>'.
4417}
4418
4419// Returns true for the declaration/definition form of @protocol,
4420// false for the expression form.
4421bool UnwrappedLineParser::parseObjCProtocol() {
4422 assert(FormatTok->is(tok::objc_protocol));
4423 nextToken();
4424
4425 if (FormatTok->is(Kind: tok::l_paren)) {
4426 // The expression form of @protocol, e.g. "Protocol* p = @protocol(foo);".
4427 return false;
4428 }
4429
4430 // The definition/declaration form,
4431 // @protocol Foo
4432 // - (int)someMethod;
4433 // @end
4434
4435 nextToken(); // protocol name
4436
4437 if (FormatTok->is(Kind: tok::less))
4438 parseObjCProtocolList();
4439
4440 // Check for protocol declaration.
4441 if (FormatTok->is(Kind: tok::semi)) {
4442 nextToken();
4443 addUnwrappedLine();
4444 return true;
4445 }
4446
4447 addUnwrappedLine();
4448 parseObjCUntilAtEnd();
4449 return true;
4450}
4451
4452void UnwrappedLineParser::parseJavaScriptEs6ImportExport() {
4453 bool IsImport = FormatTok->is(II: Keywords.kw_import);
4454 assert(IsImport || FormatTok->is(tok::kw_export));
4455 nextToken();
4456
4457 // Consume the "default" in "export default class/function".
4458 if (FormatTok->is(Kind: tok::kw_default))
4459 nextToken();
4460
4461 // Consume "async function", "function" and "default function", so that these
4462 // get parsed as free-standing JS functions, i.e. do not require a trailing
4463 // semicolon.
4464 if (FormatTok->is(II: Keywords.kw_async))
4465 nextToken();
4466 if (FormatTok->is(II: Keywords.kw_function)) {
4467 nextToken();
4468 return;
4469 }
4470
4471 // For imports, `export *`, `export {...}`, consume the rest of the line up
4472 // to the terminating `;`. For everything else, just return and continue
4473 // parsing the structural element, i.e. the declaration or expression for
4474 // `export default`.
4475 if (!IsImport && FormatTok->isNoneOf(Ks: tok::l_brace, Ks: tok::star) &&
4476 !FormatTok->isStringLiteral() &&
4477 !(FormatTok->is(II: Keywords.kw_type) &&
4478 Tokens->peekNextToken()->isOneOf(K1: tok::l_brace, K2: tok::star))) {
4479 return;
4480 }
4481
4482 while (!eof()) {
4483 if (FormatTok->is(Kind: tok::semi))
4484 return;
4485 if (Line->Tokens.empty()) {
4486 // Common issue: Automatic Semicolon Insertion wrapped the line, so the
4487 // import statement should terminate.
4488 return;
4489 }
4490 if (FormatTok->is(Kind: tok::l_brace)) {
4491 FormatTok->setBlockKind(BK_Block);
4492 nextToken();
4493 parseBracedList();
4494 } else {
4495 nextToken();
4496 }
4497 }
4498}
4499
4500void UnwrappedLineParser::parseStatementMacro() {
4501 nextToken();
4502 if (FormatTok->is(Kind: tok::l_paren))
4503 parseParens();
4504 if (FormatTok->is(Kind: tok::semi))
4505 nextToken();
4506 addUnwrappedLine();
4507}
4508
4509void UnwrappedLineParser::parseVerilogHierarchyIdentifier() {
4510 // consume things like a::`b.c[d:e] or a::*
4511 while (true) {
4512 if (FormatTok->isOneOf(K1: tok::star, K2: tok::period, Ks: tok::periodstar,
4513 Ks: tok::coloncolon, Ks: tok::hash) ||
4514 Keywords.isVerilogIdentifier(Tok: *FormatTok)) {
4515 nextToken();
4516 } else if (FormatTok->is(Kind: tok::l_square)) {
4517 parseSquare();
4518 } else {
4519 break;
4520 }
4521 }
4522}
4523
4524void UnwrappedLineParser::parseVerilogSensitivityList() {
4525 if (FormatTok->isNot(Kind: tok::at))
4526 return;
4527 nextToken();
4528 // A block event expression has 2 at signs.
4529 if (FormatTok->is(Kind: tok::at))
4530 nextToken();
4531 switch (FormatTok->Tok.getKind()) {
4532 case tok::star:
4533 nextToken();
4534 break;
4535 case tok::l_paren:
4536 parseParens();
4537 break;
4538 default:
4539 parseVerilogHierarchyIdentifier();
4540 break;
4541 }
4542}
4543
4544unsigned UnwrappedLineParser::parseVerilogHierarchyHeader() {
4545 unsigned AddLevels = 0;
4546
4547 if (FormatTok->is(II: Keywords.kw_clocking)) {
4548 nextToken();
4549 if (Keywords.isVerilogIdentifier(Tok: *FormatTok))
4550 nextToken();
4551 parseVerilogSensitivityList();
4552 if (FormatTok->is(Kind: tok::semi))
4553 nextToken();
4554 } else if (FormatTok->isOneOf(K1: tok::kw_case, K2: Keywords.kw_casex,
4555 Ks: Keywords.kw_casez, Ks: Keywords.kw_randcase,
4556 Ks: Keywords.kw_randsequence)) {
4557 if (Style.IndentCaseLabels)
4558 AddLevels++;
4559 nextToken();
4560 if (FormatTok->is(Kind: tok::l_paren)) {
4561 FormatTok->setFinalizedType(TT_ConditionLParen);
4562 parseParens();
4563 }
4564 if (FormatTok->isOneOf(K1: Keywords.kw_inside, K2: Keywords.kw_matches))
4565 nextToken();
4566 // The case header has no semicolon.
4567 } else {
4568 // "module" etc.
4569 nextToken();
4570 // all the words like the name of the module and specifiers like
4571 // "automatic" and the width of function return type
4572 while (true) {
4573 if (FormatTok->is(Kind: tok::l_square)) {
4574 auto Prev = FormatTok->getPreviousNonComment();
4575 if (Prev && Keywords.isVerilogIdentifier(Tok: *Prev))
4576 Prev->setFinalizedType(TT_VerilogDimensionedTypeName);
4577 parseSquare();
4578 } else if (Keywords.isVerilogIdentifier(Tok: *FormatTok) ||
4579 FormatTok->isOneOf(K1: tok::hash, K2: tok::hashhash, Ks: tok::coloncolon,
4580 Ks: Keywords.kw_automatic, Ks: tok::kw_static)) {
4581 nextToken();
4582 } else {
4583 break;
4584 }
4585 }
4586
4587 auto NewLine = [this]() {
4588 addUnwrappedLine();
4589 Line->IsContinuation = true;
4590 };
4591
4592 // package imports
4593 while (FormatTok->is(II: Keywords.kw_import)) {
4594 NewLine();
4595 nextToken();
4596 parseVerilogHierarchyIdentifier();
4597 if (FormatTok->is(Kind: tok::semi))
4598 nextToken();
4599 }
4600
4601 // parameters and ports
4602 if (FormatTok->is(II: Keywords.kw_verilogHash)) {
4603 NewLine();
4604 nextToken();
4605 if (FormatTok->is(Kind: tok::l_paren)) {
4606 FormatTok->setFinalizedType(TT_VerilogMultiLineListLParen);
4607 parseParens();
4608 }
4609 }
4610 if (FormatTok->is(Kind: tok::l_paren)) {
4611 NewLine();
4612 FormatTok->setFinalizedType(TT_VerilogMultiLineListLParen);
4613 parseParens();
4614 }
4615
4616 // extends and implements
4617 if (FormatTok->is(II: Keywords.kw_extends)) {
4618 NewLine();
4619 nextToken();
4620 parseVerilogHierarchyIdentifier();
4621 if (FormatTok->is(Kind: tok::l_paren))
4622 parseParens();
4623 }
4624 if (FormatTok->is(II: Keywords.kw_implements)) {
4625 NewLine();
4626 do {
4627 nextToken();
4628 parseVerilogHierarchyIdentifier();
4629 } while (FormatTok->is(Kind: tok::comma));
4630 }
4631
4632 // Coverage event for cover groups.
4633 if (FormatTok->is(Kind: tok::at)) {
4634 NewLine();
4635 parseVerilogSensitivityList();
4636 }
4637
4638 if (FormatTok->is(Kind: tok::semi))
4639 nextToken(/*LevelDifference=*/1);
4640 addUnwrappedLine();
4641 }
4642
4643 return AddLevels;
4644}
4645
4646void UnwrappedLineParser::parseVerilogTable() {
4647 assert(FormatTok->is(Keywords.kw_table));
4648 nextToken(/*LevelDifference=*/1);
4649 addUnwrappedLine();
4650
4651 auto InitialLevel = Line->Level++;
4652 while (!eof() && !Keywords.isVerilogEnd(Tok: *FormatTok)) {
4653 FormatToken *Tok = FormatTok;
4654 nextToken();
4655 if (Tok->is(Kind: tok::semi))
4656 addUnwrappedLine();
4657 else if (Tok->isOneOf(K1: tok::star, K2: tok::colon, Ks: tok::question, Ks: tok::minus))
4658 Tok->setFinalizedType(TT_VerilogTableItem);
4659 }
4660 Line->Level = InitialLevel;
4661 nextToken(/*LevelDifference=*/-1);
4662 addUnwrappedLine();
4663}
4664
4665void UnwrappedLineParser::parseVerilogCaseLabel() {
4666 // The label will get unindented in AnnotatingParser. If there are no leading
4667 // spaces, indent the rest here so that things inside the block will be
4668 // indented relative to things outside. We don't use parseLabel because we
4669 // don't know whether this colon is a label or a ternary expression at this
4670 // point.
4671 auto OrigLevel = Line->Level;
4672 auto FirstLine = CurrentLines->size();
4673 if (Line->Level == 0 || (Line->InPPDirective && Line->Level <= 1))
4674 ++Line->Level;
4675 else if (!Style.IndentCaseBlocks && Keywords.isVerilogBegin(Tok: *FormatTok))
4676 --Line->Level;
4677 parseStructuralElement();
4678 // Restore the indentation in both the new line and the line that has the
4679 // label.
4680 if (CurrentLines->size() > FirstLine)
4681 (*CurrentLines)[FirstLine].Level = OrigLevel;
4682 Line->Level = OrigLevel;
4683}
4684
4685void UnwrappedLineParser::parseVerilogExtern() {
4686 assert(
4687 FormatTok->isOneOf(tok::kw_extern, tok::kw_export, Keywords.kw_import));
4688 nextToken();
4689 // "DPI-C"
4690 if (FormatTok->is(Kind: tok::string_literal))
4691 nextToken();
4692 skipVerilogQualifiers();
4693 if (Keywords.isVerilogIdentifier(Tok: *FormatTok))
4694 nextToken();
4695 if (FormatTok->is(Kind: tok::equal))
4696 nextToken();
4697 if (Keywords.isVerilogHierarchy(Tok: *FormatTok))
4698 parseVerilogHierarchyHeader();
4699}
4700
4701void UnwrappedLineParser::skipVerilogQualifiers() {
4702 while (FormatTok->isOneOf(K1: tok::kw_protected, K2: tok::kw_virtual, Ks: tok::kw_static,
4703 Ks: Keywords.kw_rand, Ks: Keywords.kw_context,
4704 Ks: Keywords.kw_pure, Ks: Keywords.kw_randc,
4705 Ks: Keywords.kw_local)) {
4706 nextToken();
4707 }
4708}
4709
4710bool UnwrappedLineParser::containsExpansion(const UnwrappedLine &Line) const {
4711 for (const auto &N : Line.Tokens) {
4712 if (N.Tok->MacroCtx)
4713 return true;
4714 for (const UnwrappedLine &Child : N.Children)
4715 if (containsExpansion(Line: Child))
4716 return true;
4717 }
4718 return false;
4719}
4720
4721void UnwrappedLineParser::addUnwrappedLine(LineLevel AdjustLevel) {
4722 if (Line->Tokens.empty())
4723 return;
4724 LLVM_DEBUG({
4725 if (!parsingPPDirective()) {
4726 llvm::dbgs() << "Adding unwrapped line:\n";
4727 printDebugInfo(*Line);
4728 }
4729 });
4730
4731 // If this line closes a block when in Whitesmiths mode, remember that
4732 // information so that the level can be decreased after the line is added.
4733 // This has to happen after the addition of the line since the line itself
4734 // needs to be indented.
4735 bool ClosesWhitesmithsBlock =
4736 Line->MatchingOpeningBlockLineIndex != UnwrappedLine::kInvalidIndex &&
4737 Style.BreakBeforeBraces == FormatStyle::BS_Whitesmiths;
4738
4739 // If the current line was expanded from a macro call, we use it to
4740 // reconstruct an unwrapped line from the structure of the expanded unwrapped
4741 // line and the unexpanded token stream.
4742 if (!parsingPPDirective() && !InExpansion && containsExpansion(Line: *Line)) {
4743 if (!Reconstruct)
4744 Reconstruct.emplace(args&: Line->Level, args&: Unexpanded);
4745 Reconstruct->addLine(Line: *Line);
4746
4747 // While the reconstructed unexpanded lines are stored in the normal
4748 // flow of lines, the expanded lines are stored on the side to be analyzed
4749 // in an extra step.
4750 CurrentExpandedLines.push_back(Elt: std::move(*Line));
4751
4752 if (Reconstruct->finished()) {
4753 UnwrappedLine Reconstructed = std::move(*Reconstruct).takeResult();
4754 assert(!Reconstructed.Tokens.empty() &&
4755 "Reconstructed must at least contain the macro identifier.");
4756 assert(!parsingPPDirective());
4757 LLVM_DEBUG({
4758 llvm::dbgs() << "Adding unexpanded line:\n";
4759 printDebugInfo(Reconstructed);
4760 });
4761 ExpandedLines[Reconstructed.Tokens.begin()->Tok] = CurrentExpandedLines;
4762 Lines.push_back(Elt: std::move(Reconstructed));
4763 CurrentExpandedLines.clear();
4764 Reconstruct.reset();
4765 }
4766 } else {
4767 // At the top level we only get here when no unexpansion is going on, or
4768 // when conditional formatting led to unfinished macro reconstructions.
4769 assert(!Reconstruct || (CurrentLines != &Lines) || !PPStack.empty());
4770 CurrentLines->push_back(Elt: std::move(*Line));
4771 }
4772 Line->Tokens.clear();
4773 Line->MatchingOpeningBlockLineIndex = UnwrappedLine::kInvalidIndex;
4774 Line->FirstStartColumn = 0;
4775 Line->IsContinuation = false;
4776 Line->SeenDecltypeAuto = false;
4777 Line->IsModuleOrImportDecl = false;
4778
4779 if (ClosesWhitesmithsBlock && AdjustLevel == LineLevel::Remove)
4780 --Line->Level;
4781 if (!parsingPPDirective() && !PreprocessorDirectives.empty()) {
4782 CurrentLines->append(
4783 in_start: std::make_move_iterator(i: PreprocessorDirectives.begin()),
4784 in_end: std::make_move_iterator(i: PreprocessorDirectives.end()));
4785 PreprocessorDirectives.clear();
4786 }
4787 // Disconnect the current token from the last token on the previous line.
4788 FormatTok->Previous = nullptr;
4789}
4790
4791bool UnwrappedLineParser::eof() const { return FormatTok->is(Kind: tok::eof); }
4792
4793bool UnwrappedLineParser::isOnNewLine(const FormatToken &FormatTok) {
4794 return (Line->InPPDirective || FormatTok.HasUnescapedNewline) &&
4795 FormatTok.NewlinesBefore > 0;
4796}
4797
4798// Checks if \p FormatTok is a line comment that continues the line comment
4799// section on \p Line.
4800static bool
4801continuesLineCommentSection(const FormatToken &FormatTok,
4802 const UnwrappedLine &Line, const FormatStyle &Style,
4803 const llvm::Regex &CommentPragmasRegex) {
4804 if (Line.Tokens.empty() || Style.ReflowComments != FormatStyle::RCS_Always)
4805 return false;
4806
4807 StringRef IndentContent = FormatTok.TokenText;
4808 if (FormatTok.TokenText.starts_with(Prefix: "//") ||
4809 FormatTok.TokenText.starts_with(Prefix: "/*")) {
4810 IndentContent = FormatTok.TokenText.substr(Start: 2);
4811 }
4812 if (CommentPragmasRegex.match(String: IndentContent))
4813 return false;
4814
4815 // If Line starts with a line comment, then FormatTok continues the comment
4816 // section if its original column is greater or equal to the original start
4817 // column of the line.
4818 //
4819 // Define the min column token of a line as follows: if a line ends in '{' or
4820 // contains a '{' followed by a line comment, then the min column token is
4821 // that '{'. Otherwise, the min column token of the line is the first token of
4822 // the line.
4823 //
4824 // If Line starts with a token other than a line comment, then FormatTok
4825 // continues the comment section if its original column is greater than the
4826 // original start column of the min column token of the line.
4827 //
4828 // For example, the second line comment continues the first in these cases:
4829 //
4830 // // first line
4831 // // second line
4832 //
4833 // and:
4834 //
4835 // // first line
4836 // // second line
4837 //
4838 // and:
4839 //
4840 // int i; // first line
4841 // // second line
4842 //
4843 // and:
4844 //
4845 // do { // first line
4846 // // second line
4847 // int i;
4848 // } while (true);
4849 //
4850 // and:
4851 //
4852 // enum {
4853 // a, // first line
4854 // // second line
4855 // b
4856 // };
4857 //
4858 // The second line comment doesn't continue the first in these cases:
4859 //
4860 // // first line
4861 // // second line
4862 //
4863 // and:
4864 //
4865 // int i; // first line
4866 // // second line
4867 //
4868 // and:
4869 //
4870 // do { // first line
4871 // // second line
4872 // int i;
4873 // } while (true);
4874 //
4875 // and:
4876 //
4877 // enum {
4878 // a, // first line
4879 // // second line
4880 // };
4881 const FormatToken *MinColumnToken = Line.Tokens.front().Tok;
4882
4883 // Scan for '{//'. If found, use the column of '{' as a min column for line
4884 // comment section continuation.
4885 const FormatToken *PreviousToken = nullptr;
4886 for (const UnwrappedLineNode &Node : Line.Tokens) {
4887 if (PreviousToken && PreviousToken->is(Kind: tok::l_brace) &&
4888 isLineComment(FormatTok: *Node.Tok)) {
4889 MinColumnToken = PreviousToken;
4890 break;
4891 }
4892 PreviousToken = Node.Tok;
4893
4894 // Grab the last newline preceding a token in this unwrapped line.
4895 if (Node.Tok->NewlinesBefore > 0)
4896 MinColumnToken = Node.Tok;
4897 }
4898 if (PreviousToken && PreviousToken->is(Kind: tok::l_brace))
4899 MinColumnToken = PreviousToken;
4900
4901 return continuesLineComment(FormatTok, /*Previous=*/Line.Tokens.back().Tok,
4902 MinColumnToken);
4903}
4904
4905void UnwrappedLineParser::flushComments(bool NewlineBeforeNext) {
4906 bool JustComments = Line->Tokens.empty();
4907 for (FormatToken *Tok : CommentsBeforeNextToken) {
4908 // Line comments that belong to the same line comment section are put on the
4909 // same line since later we might want to reflow content between them.
4910 // Additional fine-grained breaking of line comment sections is controlled
4911 // by the class BreakableLineCommentSection in case it is desirable to keep
4912 // several line comment sections in the same unwrapped line.
4913 //
4914 // FIXME: Consider putting separate line comment sections as children to the
4915 // unwrapped line instead.
4916 Tok->ContinuesLineCommentSection =
4917 continuesLineCommentSection(FormatTok: *Tok, Line: *Line, Style, CommentPragmasRegex);
4918 if (isOnNewLine(FormatTok: *Tok) && JustComments && !Tok->ContinuesLineCommentSection)
4919 addUnwrappedLine();
4920 pushToken(Tok);
4921 }
4922 if (NewlineBeforeNext && JustComments)
4923 addUnwrappedLine();
4924 CommentsBeforeNextToken.clear();
4925}
4926
4927void UnwrappedLineParser::nextToken(int LevelDifference) {
4928 if (eof())
4929 return;
4930 flushComments(NewlineBeforeNext: isOnNewLine(FormatTok: *FormatTok));
4931 pushToken(Tok: FormatTok);
4932 FormatToken *Previous = FormatTok;
4933 if (!Style.isJavaScript())
4934 readToken(LevelDifference);
4935 else
4936 readTokenWithJavaScriptASI();
4937 FormatTok->Previous = Previous;
4938 if (Style.isVerilog()) {
4939 // Blocks in Verilog can have `begin` and `end` instead of braces. For
4940 // keywords like `begin`, we can't treat them the same as left braces
4941 // because some contexts require one of them. For example structs use
4942 // braces and if blocks use keywords, and a left brace can occur in an if
4943 // statement, but it is not a block. For keywords like `end`, we simply
4944 // treat them the same as right braces.
4945 if (Keywords.isVerilogEnd(Tok: *FormatTok))
4946 FormatTok->Tok.setKind(tok::r_brace);
4947 }
4948}
4949
4950void UnwrappedLineParser::distributeComments(
4951 const ArrayRef<FormatToken *> &Comments, const FormatToken *NextTok) {
4952 // Whether or not a line comment token continues a line is controlled by
4953 // the method continuesLineCommentSection, with the following caveat:
4954 //
4955 // Define a trail of Comments to be a nonempty proper postfix of Comments such
4956 // that each comment line from the trail is aligned with the next token, if
4957 // the next token exists. If a trail exists, the beginning of the maximal
4958 // trail is marked as a start of a new comment section.
4959 //
4960 // For example in this code:
4961 //
4962 // int a; // line about a
4963 // // line 1 about b
4964 // // line 2 about b
4965 // int b;
4966 //
4967 // the two lines about b form a maximal trail, so there are two sections, the
4968 // first one consisting of the single comment "// line about a" and the
4969 // second one consisting of the next two comments.
4970 if (Comments.empty())
4971 return;
4972 bool ShouldPushCommentsInCurrentLine = true;
4973 bool HasTrailAlignedWithNextToken = false;
4974 unsigned StartOfTrailAlignedWithNextToken = 0;
4975 if (NextTok) {
4976 // We are skipping the first element intentionally.
4977 for (unsigned i = Comments.size() - 1; i > 0; --i) {
4978 if (Comments[i]->OriginalColumn == NextTok->OriginalColumn) {
4979 HasTrailAlignedWithNextToken = true;
4980 StartOfTrailAlignedWithNextToken = i;
4981 }
4982 }
4983 }
4984 for (unsigned i = 0, e = Comments.size(); i < e; ++i) {
4985 FormatToken *FormatTok = Comments[i];
4986 if (HasTrailAlignedWithNextToken && i == StartOfTrailAlignedWithNextToken) {
4987 FormatTok->ContinuesLineCommentSection = false;
4988 } else {
4989 FormatTok->ContinuesLineCommentSection = continuesLineCommentSection(
4990 FormatTok: *FormatTok, Line: *Line, Style, CommentPragmasRegex);
4991 }
4992 if (!FormatTok->ContinuesLineCommentSection &&
4993 (isOnNewLine(FormatTok: *FormatTok) || FormatTok->IsFirst)) {
4994 ShouldPushCommentsInCurrentLine = false;
4995 }
4996 if (ShouldPushCommentsInCurrentLine)
4997 pushToken(Tok: FormatTok);
4998 else
4999 CommentsBeforeNextToken.push_back(Elt: FormatTok);
5000 }
5001}
5002
5003void UnwrappedLineParser::readToken(int LevelDifference) {
5004 SmallVector<FormatToken *, 1> Comments;
5005 bool PreviousWasComment = false;
5006 bool FirstNonCommentOnLine = false;
5007 do {
5008 FormatTok = Tokens->getNextToken();
5009 assert(FormatTok);
5010 while (FormatTok->isOneOf(K1: TT_ConflictStart, K2: TT_ConflictEnd,
5011 Ks: TT_ConflictAlternative)) {
5012 if (FormatTok->is(TT: TT_ConflictStart))
5013 conditionalCompilationStart(/*Unreachable=*/false);
5014 else if (FormatTok->is(TT: TT_ConflictAlternative))
5015 conditionalCompilationAlternative();
5016 else if (FormatTok->is(TT: TT_ConflictEnd))
5017 conditionalCompilationEnd();
5018 FormatTok = Tokens->getNextToken();
5019 FormatTok->MustBreakBefore = true;
5020 FormatTok->MustBreakBeforeFinalized = true;
5021 }
5022
5023 auto IsFirstNonCommentOnLine = [](bool FirstNonCommentOnLine,
5024 const FormatToken &Tok,
5025 bool PreviousWasComment) {
5026 auto IsFirstOnLine = [](const FormatToken &Tok) {
5027 return Tok.HasUnescapedNewline || Tok.IsFirst;
5028 };
5029
5030 // Consider preprocessor directives preceded by block comments as first
5031 // on line.
5032 if (PreviousWasComment)
5033 return FirstNonCommentOnLine || IsFirstOnLine(Tok);
5034 return IsFirstOnLine(Tok);
5035 };
5036
5037 FirstNonCommentOnLine = IsFirstNonCommentOnLine(
5038 FirstNonCommentOnLine, *FormatTok, PreviousWasComment);
5039 PreviousWasComment = FormatTok->is(Kind: tok::comment);
5040
5041 while (!Line->InPPDirective && FormatTok->is(Kind: tok::hash) &&
5042 FirstNonCommentOnLine) {
5043 // In Verilog, the backtick is used for macro invocations. In TableGen,
5044 // the single hash is used for the paste operator.
5045 const auto *Next = Tokens->peekNextToken();
5046 if ((Style.isVerilog() && !Keywords.isVerilogPPDirective(Tok: *Next)) ||
5047 (Style.isTableGen() &&
5048 Next->isNoneOf(Ks: tok::kw_else, Ks: tok::pp_define, Ks: tok::pp_ifdef,
5049 Ks: tok::pp_ifndef, Ks: tok::pp_endif))) {
5050 break;
5051 }
5052 distributeComments(Comments, NextTok: FormatTok);
5053 Comments.clear();
5054 // If there is an unfinished unwrapped line, we flush the preprocessor
5055 // directives only after that unwrapped line was finished later.
5056 bool SwitchToPreprocessorLines = !Line->Tokens.empty();
5057 ScopedLineState BlockState(*this, SwitchToPreprocessorLines);
5058 assert((LevelDifference >= 0 ||
5059 static_cast<unsigned>(-LevelDifference) <= Line->Level) &&
5060 "LevelDifference makes Line->Level negative");
5061 Line->Level += LevelDifference;
5062 // Comments stored before the preprocessor directive need to be output
5063 // before the preprocessor directive, at the same level as the
5064 // preprocessor directive, as we consider them to apply to the directive.
5065 if (Style.IndentPPDirectives == FormatStyle::PPDIS_BeforeHash &&
5066 PPBranchLevel > 0) {
5067 Line->Level += PPBranchLevel;
5068 }
5069 assert(Line->Level >= Line->UnbracedBodyLevel);
5070 Line->Level -= Line->UnbracedBodyLevel;
5071 flushComments(NewlineBeforeNext: isOnNewLine(FormatTok: *FormatTok));
5072 const bool IsEndIf = Tokens->peekNextToken()->is(Kind: tok::pp_endif);
5073 parsePPDirective();
5074 PreviousWasComment = FormatTok->is(Kind: tok::comment);
5075 FirstNonCommentOnLine = IsFirstNonCommentOnLine(
5076 FirstNonCommentOnLine, *FormatTok, PreviousWasComment);
5077 // If the #endif of a potential include guard is the last thing in the
5078 // file, then we found an include guard.
5079 if (IsEndIf && IncludeGuard == IG_Defined && PPBranchLevel == -1 &&
5080 getIncludeGuardState(Style: Style.IndentPPDirectives) == IG_Inited &&
5081 (eof() ||
5082 (PreviousWasComment &&
5083 Tokens->peekNextToken(/*SkipComment=*/true)->is(Kind: tok::eof)))) {
5084 IncludeGuard = IG_Found;
5085 }
5086 }
5087
5088 if (!PPStack.empty() && (PPStack.back().Kind == PP_Unreachable) &&
5089 !Line->InPPDirective) {
5090 continue;
5091 }
5092
5093 if (FormatTok->is(Kind: tok::identifier) &&
5094 Macros.defined(Name: FormatTok->TokenText) &&
5095 // FIXME: Allow expanding macros in preprocessor directives.
5096 !Line->InPPDirective) {
5097 FormatToken *ID = FormatTok;
5098 unsigned Position = Tokens->getPosition();
5099
5100 // To correctly parse the code, we need to replace the tokens of the macro
5101 // call with its expansion.
5102 auto PreCall = std::move(Line);
5103 Line.reset(p: new UnwrappedLine);
5104 bool OldInExpansion = InExpansion;
5105 InExpansion = true;
5106 // We parse the macro call into a new line.
5107 auto Args = parseMacroCall();
5108 InExpansion = OldInExpansion;
5109 assert(Line->Tokens.front().Tok == ID);
5110 // And remember the unexpanded macro call tokens.
5111 auto UnexpandedLine = std::move(Line);
5112 // Reset to the old line.
5113 Line = std::move(PreCall);
5114
5115 LLVM_DEBUG({
5116 llvm::dbgs() << "Macro call: " << ID->TokenText << "(";
5117 if (Args) {
5118 llvm::dbgs() << "(";
5119 for (const auto &Arg : Args.value())
5120 for (const auto &T : Arg)
5121 llvm::dbgs() << T->TokenText << " ";
5122 llvm::dbgs() << ")";
5123 }
5124 llvm::dbgs() << "\n";
5125 });
5126 if (Macros.objectLike(Name: ID->TokenText) && Args &&
5127 !Macros.hasArity(Name: ID->TokenText, Arity: Args->size())) {
5128 // The macro is either
5129 // - object-like, but we got argumnets, or
5130 // - overloaded to be both object-like and function-like, but none of
5131 // the function-like arities match the number of arguments.
5132 // Thus, expand as object-like macro.
5133 LLVM_DEBUG(llvm::dbgs()
5134 << "Macro \"" << ID->TokenText
5135 << "\" not overloaded for arity " << Args->size()
5136 << "or not function-like, using object-like overload.");
5137 Args.reset();
5138 UnexpandedLine->Tokens.resize(new_size: 1);
5139 Tokens->setPosition(Position);
5140 // Not nextToken(), which would push the stale FormatTok onto the line.
5141 FormatTok = Tokens->getNextToken();
5142 assert(!Args && Macros.objectLike(ID->TokenText));
5143 }
5144 if ((!Args && Macros.objectLike(Name: ID->TokenText)) ||
5145 (Args && Macros.hasArity(Name: ID->TokenText, Arity: Args->size()))) {
5146 // Next, we insert the expanded tokens in the token stream at the
5147 // current position, and continue parsing.
5148 Unexpanded[ID] = std::move(UnexpandedLine);
5149 SmallVector<FormatToken *, 8> Expansion =
5150 Macros.expand(ID, OptionalArgs: std::move(Args));
5151 if (!Expansion.empty())
5152 FormatTok = Tokens->insertTokens(Tokens: Expansion);
5153
5154 LLVM_DEBUG({
5155 llvm::dbgs() << "Expanded: ";
5156 for (const auto &T : Expansion)
5157 llvm::dbgs() << T->TokenText << " ";
5158 llvm::dbgs() << "\n";
5159 });
5160 } else {
5161 LLVM_DEBUG({
5162 llvm::dbgs() << "Did not expand macro \"" << ID->TokenText
5163 << "\", because it was used ";
5164 if (Args)
5165 llvm::dbgs() << "with " << Args->size();
5166 else
5167 llvm::dbgs() << "without";
5168 llvm::dbgs() << " arguments, which doesn't match any definition.\n";
5169 });
5170 Tokens->setPosition(Position);
5171 FormatTok = ID;
5172 }
5173 }
5174
5175 if (FormatTok->isNot(Kind: tok::comment)) {
5176 distributeComments(Comments, NextTok: FormatTok);
5177 Comments.clear();
5178 return;
5179 }
5180
5181 Comments.push_back(Elt: FormatTok);
5182 } while (!eof());
5183
5184 distributeComments(Comments, NextTok: nullptr);
5185 Comments.clear();
5186}
5187
5188namespace {
5189template <typename Iterator>
5190void pushTokens(Iterator Begin, Iterator End,
5191 SmallVectorImpl<FormatToken *> &Into) {
5192 for (auto I = Begin; I != End; ++I) {
5193 Into.push_back(Elt: I->Tok);
5194 for (const auto &Child : I->Children)
5195 pushTokens(Child.Tokens.begin(), Child.Tokens.end(), Into);
5196 }
5197}
5198} // namespace
5199
5200std::optional<llvm::SmallVector<llvm::SmallVector<FormatToken *, 8>, 1>>
5201UnwrappedLineParser::parseMacroCall() {
5202 std::optional<llvm::SmallVector<llvm::SmallVector<FormatToken *, 8>, 1>> Args;
5203 assert(Line->Tokens.empty());
5204 // Not nextToken(), which would already expand a directly following macro
5205 // call before the expansion of this one is inserted.
5206 auto ConsumeLastTokenOfCall = [this] {
5207 flushComments(NewlineBeforeNext: isOnNewLine(FormatTok: *FormatTok));
5208 pushToken(Tok: FormatTok);
5209 FormatTok = Tokens->getNextToken();
5210 };
5211 if (Tokens->peekNextToken(/*SkipComment=*/true)->isNot(Kind: tok::l_paren)) {
5212 ConsumeLastTokenOfCall();
5213 return Args;
5214 }
5215 nextToken();
5216 assert(FormatTok->is(tok::l_paren));
5217 unsigned Position = Tokens->getPosition();
5218 FormatToken *Tok = FormatTok;
5219 nextToken();
5220 Args.emplace();
5221 auto ArgStart = std::prev(x: Line->Tokens.end());
5222
5223 int Parens = 0;
5224 do {
5225 switch (FormatTok->Tok.getKind()) {
5226 case tok::l_paren:
5227 ++Parens;
5228 nextToken();
5229 break;
5230 case tok::r_paren: {
5231 if (Parens > 0) {
5232 --Parens;
5233 nextToken();
5234 break;
5235 }
5236 Args->push_back(Elt: {});
5237 pushTokens(Begin: std::next(x: ArgStart), End: Line->Tokens.end(), Into&: Args->back());
5238 ConsumeLastTokenOfCall();
5239 return Args;
5240 }
5241 case tok::comma: {
5242 if (Parens > 0) {
5243 nextToken();
5244 break;
5245 }
5246 Args->push_back(Elt: {});
5247 pushTokens(Begin: std::next(x: ArgStart), End: Line->Tokens.end(), Into&: Args->back());
5248 nextToken();
5249 ArgStart = std::prev(x: Line->Tokens.end());
5250 break;
5251 }
5252 default:
5253 nextToken();
5254 break;
5255 }
5256 } while (!eof());
5257 Line->Tokens.resize(new_size: 1);
5258 Tokens->setPosition(Position);
5259 FormatTok = Tok;
5260 return {};
5261}
5262
5263void UnwrappedLineParser::pushToken(FormatToken *Tok) {
5264 Line->Tokens.push_back(x: UnwrappedLineNode(Tok));
5265 if (AtEndOfPPLine) {
5266 auto &Tok = *Line->Tokens.back().Tok;
5267 Tok.MustBreakBefore = true;
5268 Tok.MustBreakBeforeFinalized = true;
5269 Tok.FirstAfterPPLine = true;
5270 AtEndOfPPLine = false;
5271 }
5272}
5273
5274} // end namespace format
5275} // end namespace clang
5276