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