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