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