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