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