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