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