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