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