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 // C# needs this change to ensure that array initialisers and object
2097 // initialisers are indented the same way. In TypeScript, the brace
2098 // can also be an object type definition.
2099 if (!Style.isJavaScript())
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 StarAndAmpTokenType If different than TT_Unknown sets this type for
2608/// all (double) ampersands and stars. This applies for all nested scopes as
2609/// well.
2610///
2611/// Returns whether there is a `=` token between the parentheses.
2612bool UnwrappedLineParser::parseParens(TokenType StarAndAmpTokenType,
2613 bool InMacroCall) {
2614 assert(FormatTok->is(tok::l_paren) && "'(' expected.");
2615 auto *LParen = FormatTok;
2616 auto *Prev = FormatTok->Previous;
2617 bool SeenComma = false;
2618 bool SeenEqual = false;
2619 bool MightBeFoldExpr = false;
2620 nextToken();
2621 const bool MightBeStmtExpr = FormatTok->is(tok::l_brace);
2622 if (!InMacroCall && Prev && Prev->is(TT_FunctionLikeMacro))
2623 InMacroCall = true;
2624 do {
2625 switch (FormatTok->Tok.getKind()) {
2626 case tok::l_paren:
2627 if (parseParens(StarAndAmpTokenType, InMacroCall))
2628 SeenEqual = true;
2629 if (Style.isJava() && FormatTok->is(tok::l_brace))
2630 parseChildBlock();
2631 break;
2632 case tok::r_paren: {
2633 auto *RParen = FormatTok;
2634 nextToken();
2635 if (Prev) {
2636 auto OptionalParens = [&] {
2637 if (Style.RemoveParentheses == FormatStyle::RPS_Leave ||
2638 MightBeStmtExpr || MightBeFoldExpr || SeenComma || InMacroCall ||
2639 Line->InMacroBody || RParen->getPreviousNonComment() == LParen) {
2640 return false;
2641 }
2642 const bool DoubleParens =
2643 Prev->is(tok::l_paren) && FormatTok->is(tok::r_paren);
2644 if (DoubleParens) {
2645 const auto *PrevPrev = Prev->getPreviousNonComment();
2646 const bool Excluded =
2647 PrevPrev &&
2648 (PrevPrev->isOneOf(tok::kw___attribute, tok::kw_decltype) ||
2649 (SeenEqual &&
2650 (PrevPrev->isOneOf(tok::kw_if, tok::kw_while) ||
2651 PrevPrev->endsSequence(tok::kw_constexpr, tok::kw_if))));
2652 if (!Excluded)
2653 return true;
2654 } else {
2655 const bool CommaSeparated =
2656 Prev->isOneOf(tok::l_paren, tok::comma) &&
2657 FormatTok->isOneOf(tok::comma, tok::r_paren);
2658 if (CommaSeparated &&
2659 // LParen is not preceded by ellipsis, comma.
2660 !Prev->endsSequence(tok::comma, tok::ellipsis) &&
2661 // RParen is not followed by comma, ellipsis.
2662 !(FormatTok->is(tok::comma) &&
2663 Tokens->peekNextToken()->is(tok::ellipsis))) {
2664 return true;
2665 }
2666 const bool ReturnParens =
2667 Style.RemoveParentheses == FormatStyle::RPS_ReturnStatement &&
2668 ((NestedLambdas.empty() && !IsDecltypeAutoFunction) ||
2669 (!NestedLambdas.empty() && !NestedLambdas.back())) &&
2670 Prev->isOneOf(tok::kw_return, tok::kw_co_return) &&
2671 FormatTok->is(tok::semi);
2672 if (ReturnParens)
2673 return true;
2674 }
2675 return false;
2676 };
2677 if (OptionalParens()) {
2678 LParen->Optional = true;
2679 RParen->Optional = true;
2680 } else if (Prev->is(TT_TypenameMacro)) {
2681 LParen->setFinalizedType(TT_TypeDeclarationParen);
2682 RParen->setFinalizedType(TT_TypeDeclarationParen);
2683 } else if (Prev->is(tok::greater) && RParen->Previous == LParen) {
2684 Prev->setFinalizedType(TT_TemplateCloser);
2685 } else if (FormatTok->is(tok::l_brace) && Prev->is(tok::amp) &&
2686 !Prev->Previous) {
2687 FormatTok->setBlockKind(BK_BracedInit);
2688 }
2689 }
2690 return SeenEqual;
2691 }
2692 case tok::r_brace:
2693 // A "}" inside parenthesis is an error if there wasn't a matching "{".
2694 return SeenEqual;
2695 case tok::l_square:
2696 tryToParseLambda();
2697 break;
2698 case tok::l_brace:
2699 if (!tryToParseBracedList())
2700 parseChildBlock();
2701 break;
2702 case tok::at:
2703 nextToken();
2704 if (FormatTok->is(tok::l_brace)) {
2705 nextToken();
2706 parseBracedList();
2707 }
2708 break;
2709 case tok::comma:
2710 SeenComma = true;
2711 nextToken();
2712 break;
2713 case tok::ellipsis:
2714 MightBeFoldExpr = true;
2715 nextToken();
2716 break;
2717 case tok::equal:
2718 SeenEqual = true;
2719 if (Style.isCSharp() && FormatTok->is(TT_FatArrow))
2720 tryToParseChildBlock();
2721 else
2722 nextToken();
2723 break;
2724 case tok::kw_class:
2725 if (Style.isJavaScript())
2726 parseRecord(/*ParseAsExpr=*/true);
2727 else
2728 nextToken();
2729 break;
2730 case tok::identifier:
2731 if (Style.isJavaScript() && (FormatTok->is(Keywords.kw_function)))
2732 tryToParseJSFunction();
2733 else
2734 nextToken();
2735 break;
2736 case tok::kw_switch:
2737 if (Style.isJava())
2738 parseSwitch(/*IsExpr=*/true);
2739 else
2740 nextToken();
2741 break;
2742 case tok::kw_requires:
2743 parseRequiresExpression();
2744 break;
2745 case tok::star:
2746 case tok::amp:
2747 case tok::ampamp:
2748 if (StarAndAmpTokenType != TT_Unknown)
2749 FormatTok->setFinalizedType(StarAndAmpTokenType);
2750 [[fallthrough]];
2751 default:
2752 nextToken();
2753 break;
2754 }
2755 } while (!eof());
2756 return SeenEqual;
2757}
2758
2759void UnwrappedLineParser::parseSquare(bool LambdaIntroducer) {
2760 if (!LambdaIntroducer) {
2761 assert(FormatTok->is(tok::l_square) && "'[' expected.");
2762 if (tryToParseLambda())
2763 return;
2764 }
2765 do {
2766 switch (FormatTok->Tok.getKind()) {
2767 case tok::l_paren:
2768 parseParens();
2769 break;
2770 case tok::r_square:
2771 nextToken();
2772 return;
2773 case tok::r_brace:
2774 // A "}" inside parenthesis is an error if there wasn't a matching "{".
2775 return;
2776 case tok::l_square:
2777 parseSquare();
2778 break;
2779 case tok::l_brace: {
2780 if (!tryToParseBracedList())
2781 parseChildBlock();
2782 break;
2783 }
2784 case tok::at:
2785 case tok::colon:
2786 nextToken();
2787 if (FormatTok->is(tok::l_brace)) {
2788 nextToken();
2789 parseBracedList();
2790 }
2791 break;
2792 default:
2793 nextToken();
2794 break;
2795 }
2796 } while (!eof());
2797}
2798
2799void UnwrappedLineParser::keepAncestorBraces() {
2800 if (!Style.RemoveBracesLLVM)
2801 return;
2802
2803 const int MaxNestingLevels = 2;
2804 const int Size = NestedTooDeep.size();
2805 if (Size >= MaxNestingLevels)
2806 NestedTooDeep[Size - MaxNestingLevels] = true;
2807 NestedTooDeep.push_back(false);
2808}
2809
2811 for (const auto &Token : llvm::reverse(Line.Tokens))
2812 if (Token.Tok->isNot(tok::comment))
2813 return Token.Tok;
2814
2815 return nullptr;
2816}
2817
2818void UnwrappedLineParser::parseUnbracedBody(bool CheckEOF) {
2819 FormatToken *Tok = nullptr;
2820
2821 if (Style.InsertBraces && !Line->InPPDirective && !Line->Tokens.empty() &&
2822 PreprocessorDirectives.empty() && FormatTok->isNot(tok::semi)) {
2823 Tok = Style.BraceWrapping.AfterControlStatement == FormatStyle::BWACS_Never
2824 ? getLastNonComment(*Line)
2825 : Line->Tokens.back().Tok;
2826 assert(Tok);
2827 if (Tok->BraceCount < 0) {
2828 assert(Tok->BraceCount == -1);
2829 Tok = nullptr;
2830 } else {
2831 Tok->BraceCount = -1;
2832 }
2833 }
2834
2835 addUnwrappedLine();
2836 ++Line->Level;
2837 ++Line->UnbracedBodyLevel;
2838 parseStructuralElement();
2839 --Line->UnbracedBodyLevel;
2840
2841 if (Tok) {
2842 assert(!Line->InPPDirective);
2843 Tok = nullptr;
2844 for (const auto &L : llvm::reverse(*CurrentLines)) {
2845 if (!L.InPPDirective && getLastNonComment(L)) {
2846 Tok = L.Tokens.back().Tok;
2847 break;
2848 }
2849 }
2850 assert(Tok);
2851 ++Tok->BraceCount;
2852 }
2853
2854 if (CheckEOF && eof())
2855 addUnwrappedLine();
2856
2857 --Line->Level;
2858}
2859
2860static void markOptionalBraces(FormatToken *LeftBrace) {
2861 if (!LeftBrace)
2862 return;
2863
2864 assert(LeftBrace->is(tok::l_brace));
2865
2866 FormatToken *RightBrace = LeftBrace->MatchingParen;
2867 if (!RightBrace) {
2868 assert(!LeftBrace->Optional);
2869 return;
2870 }
2871
2872 assert(RightBrace->is(tok::r_brace));
2873 assert(RightBrace->MatchingParen == LeftBrace);
2874 assert(LeftBrace->Optional == RightBrace->Optional);
2875
2876 LeftBrace->Optional = true;
2877 RightBrace->Optional = true;
2878}
2879
2880void UnwrappedLineParser::handleAttributes() {
2881 // Handle AttributeMacro, e.g. `if (x) UNLIKELY`.
2882 if (FormatTok->isAttribute())
2883 nextToken();
2884 else if (FormatTok->is(tok::l_square))
2885 handleCppAttributes();
2886}
2887
2888bool UnwrappedLineParser::handleCppAttributes() {
2889 // Handle [[likely]] / [[unlikely]] attributes.
2890 assert(FormatTok->is(tok::l_square));
2891 if (!tryToParseSimpleAttribute())
2892 return false;
2893 parseSquare();
2894 return true;
2895}
2896
2897/// Returns whether \c Tok begins a block.
2898bool UnwrappedLineParser::isBlockBegin(const FormatToken &Tok) const {
2899 // FIXME: rename the function or make
2900 // Tok.isOneOf(tok::l_brace, TT_MacroBlockBegin) work.
2901 return Style.isVerilog() ? Keywords.isVerilogBegin(Tok)
2902 : Tok.is(tok::l_brace);
2903}
2904
2905FormatToken *UnwrappedLineParser::parseIfThenElse(IfStmtKind *IfKind,
2906 bool KeepBraces,
2907 bool IsVerilogAssert) {
2908 assert((FormatTok->is(tok::kw_if) ||
2909 (Style.isVerilog() &&
2910 FormatTok->isOneOf(tok::kw_restrict, Keywords.kw_assert,
2911 Keywords.kw_assume, Keywords.kw_cover))) &&
2912 "'if' expected");
2913 nextToken();
2914
2915 if (IsVerilogAssert) {
2916 // Handle `assert #0` and `assert final`.
2917 if (FormatTok->is(Keywords.kw_verilogHash)) {
2918 nextToken();
2919 if (FormatTok->is(tok::numeric_constant))
2920 nextToken();
2921 } else if (FormatTok->isOneOf(Keywords.kw_final, Keywords.kw_property,
2922 Keywords.kw_sequence)) {
2923 nextToken();
2924 }
2925 }
2926
2927 // TableGen's if statement has the form of `if <cond> then { ... }`.
2928 if (Style.isTableGen()) {
2929 while (!eof() && FormatTok->isNot(Keywords.kw_then)) {
2930 // Simply skip until then. This range only contains a value.
2931 nextToken();
2932 }
2933 }
2934
2935 // Handle `if !consteval`.
2936 if (FormatTok->is(tok::exclaim))
2937 nextToken();
2938
2939 bool KeepIfBraces = true;
2940 if (FormatTok->is(tok::kw_consteval)) {
2941 nextToken();
2942 } else {
2943 KeepIfBraces = !Style.RemoveBracesLLVM || KeepBraces;
2944 if (FormatTok->isOneOf(tok::kw_constexpr, tok::identifier))
2945 nextToken();
2946 if (FormatTok->is(tok::l_paren)) {
2947 FormatTok->setFinalizedType(TT_ConditionLParen);
2948 parseParens();
2949 }
2950 }
2951 handleAttributes();
2952 // The then action is optional in Verilog assert statements.
2953 if (IsVerilogAssert && FormatTok->is(tok::semi)) {
2954 nextToken();
2955 addUnwrappedLine();
2956 return nullptr;
2957 }
2958
2959 bool NeedsUnwrappedLine = false;
2960 keepAncestorBraces();
2961
2962 FormatToken *IfLeftBrace = nullptr;
2963 IfStmtKind IfBlockKind = IfStmtKind::NotIf;
2964
2965 if (isBlockBegin(*FormatTok)) {
2966 FormatTok->setFinalizedType(TT_ControlStatementLBrace);
2967 IfLeftBrace = FormatTok;
2968 CompoundStatementIndenter Indenter(this, Style, Line->Level);
2969 parseBlock(/*MustBeDeclaration=*/false, /*AddLevels=*/1u,
2970 /*MunchSemi=*/true, KeepIfBraces, &IfBlockKind);
2971 setPreviousRBraceType(TT_ControlStatementRBrace);
2972 if (Style.BraceWrapping.BeforeElse)
2973 addUnwrappedLine();
2974 else
2975 NeedsUnwrappedLine = true;
2976 } else if (IsVerilogAssert && FormatTok->is(tok::kw_else)) {
2977 addUnwrappedLine();
2978 } else {
2979 parseUnbracedBody();
2980 }
2981
2982 if (Style.RemoveBracesLLVM) {
2983 assert(!NestedTooDeep.empty());
2984 KeepIfBraces = KeepIfBraces ||
2985 (IfLeftBrace && !IfLeftBrace->MatchingParen) ||
2986 NestedTooDeep.back() || IfBlockKind == IfStmtKind::IfOnly ||
2987 IfBlockKind == IfStmtKind::IfElseIf;
2988 }
2989
2990 bool KeepElseBraces = KeepIfBraces;
2991 FormatToken *ElseLeftBrace = nullptr;
2992 IfStmtKind Kind = IfStmtKind::IfOnly;
2993
2994 if (FormatTok->is(tok::kw_else)) {
2995 if (Style.RemoveBracesLLVM) {
2996 NestedTooDeep.back() = false;
2997 Kind = IfStmtKind::IfElse;
2998 }
2999 nextToken();
3000 handleAttributes();
3001 if (isBlockBegin(*FormatTok)) {
3002 const bool FollowedByIf = Tokens->peekNextToken()->is(tok::kw_if);
3003 FormatTok->setFinalizedType(TT_ElseLBrace);
3004 ElseLeftBrace = FormatTok;
3005 CompoundStatementIndenter Indenter(this, Style, Line->Level);
3006 IfStmtKind ElseBlockKind = IfStmtKind::NotIf;
3007 FormatToken *IfLBrace =
3008 parseBlock(/*MustBeDeclaration=*/false, /*AddLevels=*/1u,
3009 /*MunchSemi=*/true, KeepElseBraces, &ElseBlockKind);
3010 setPreviousRBraceType(TT_ElseRBrace);
3011 if (FormatTok->is(tok::kw_else)) {
3012 KeepElseBraces = KeepElseBraces ||
3013 ElseBlockKind == IfStmtKind::IfOnly ||
3014 ElseBlockKind == IfStmtKind::IfElseIf;
3015 } else if (FollowedByIf && IfLBrace && !IfLBrace->Optional) {
3016 KeepElseBraces = true;
3017 assert(ElseLeftBrace->MatchingParen);
3018 markOptionalBraces(ElseLeftBrace);
3019 }
3020 addUnwrappedLine();
3021 } else if (!IsVerilogAssert && FormatTok->is(tok::kw_if)) {
3022 const FormatToken *Previous = Tokens->getPreviousToken();
3023 assert(Previous);
3024 const bool IsPrecededByComment = Previous->is(tok::comment);
3025 if (IsPrecededByComment) {
3026 addUnwrappedLine();
3027 ++Line->Level;
3028 }
3029 bool TooDeep = true;
3030 if (Style.RemoveBracesLLVM) {
3031 Kind = IfStmtKind::IfElseIf;
3032 TooDeep = NestedTooDeep.pop_back_val();
3033 }
3034 ElseLeftBrace = parseIfThenElse(/*IfKind=*/nullptr, KeepIfBraces);
3035 if (Style.RemoveBracesLLVM)
3036 NestedTooDeep.push_back(TooDeep);
3037 if (IsPrecededByComment)
3038 --Line->Level;
3039 } else {
3040 parseUnbracedBody(/*CheckEOF=*/true);
3041 }
3042 } else {
3043 KeepIfBraces = KeepIfBraces || IfBlockKind == IfStmtKind::IfElse;
3044 if (NeedsUnwrappedLine)
3045 addUnwrappedLine();
3046 }
3047
3048 if (!Style.RemoveBracesLLVM)
3049 return nullptr;
3050
3051 assert(!NestedTooDeep.empty());
3052 KeepElseBraces = KeepElseBraces ||
3053 (ElseLeftBrace && !ElseLeftBrace->MatchingParen) ||
3054 NestedTooDeep.back();
3055
3056 NestedTooDeep.pop_back();
3057
3058 if (!KeepIfBraces && !KeepElseBraces) {
3059 markOptionalBraces(IfLeftBrace);
3060 markOptionalBraces(ElseLeftBrace);
3061 } else if (IfLeftBrace) {
3062 FormatToken *IfRightBrace = IfLeftBrace->MatchingParen;
3063 if (IfRightBrace) {
3064 assert(IfRightBrace->MatchingParen == IfLeftBrace);
3065 assert(!IfLeftBrace->Optional);
3066 assert(!IfRightBrace->Optional);
3067 IfLeftBrace->MatchingParen = nullptr;
3068 IfRightBrace->MatchingParen = nullptr;
3069 }
3070 }
3071
3072 if (IfKind)
3073 *IfKind = Kind;
3074
3075 return IfLeftBrace;
3076}
3077
3078void UnwrappedLineParser::parseTryCatch() {
3079 assert(FormatTok->isOneOf(tok::kw_try, tok::kw___try) && "'try' expected");
3080 nextToken();
3081 bool NeedsUnwrappedLine = false;
3082 bool HasCtorInitializer = false;
3083 if (FormatTok->is(tok::colon)) {
3084 auto *Colon = FormatTok;
3085 // We are in a function try block, what comes is an initializer list.
3086 nextToken();
3087 if (FormatTok->is(tok::identifier)) {
3088 HasCtorInitializer = true;
3089 Colon->setFinalizedType(TT_CtorInitializerColon);
3090 }
3091
3092 // In case identifiers were removed by clang-tidy, what might follow is
3093 // multiple commas in sequence - before the first identifier.
3094 while (FormatTok->is(tok::comma))
3095 nextToken();
3096
3097 while (FormatTok->is(tok::identifier)) {
3098 nextToken();
3099 if (FormatTok->is(tok::l_paren)) {
3100 parseParens();
3101 } else if (FormatTok->is(tok::l_brace)) {
3102 nextToken();
3103 parseBracedList();
3104 }
3105
3106 // In case identifiers were removed by clang-tidy, what might follow is
3107 // multiple commas in sequence - after the first identifier.
3108 while (FormatTok->is(tok::comma))
3109 nextToken();
3110 }
3111 }
3112 // Parse try with resource.
3113 if (Style.isJava() && FormatTok->is(tok::l_paren))
3114 parseParens();
3115
3116 keepAncestorBraces();
3117
3118 if (FormatTok->is(tok::l_brace)) {
3119 if (HasCtorInitializer)
3120 FormatTok->setFinalizedType(TT_FunctionLBrace);
3121 CompoundStatementIndenter Indenter(this, Style, Line->Level);
3122 parseBlock();
3123 if (Style.BraceWrapping.BeforeCatch)
3124 addUnwrappedLine();
3125 else
3126 NeedsUnwrappedLine = true;
3127 } else if (FormatTok->isNot(tok::kw_catch)) {
3128 // The C++ standard requires a compound-statement after a try.
3129 // If there's none, we try to assume there's a structuralElement
3130 // and try to continue.
3131 addUnwrappedLine();
3132 ++Line->Level;
3133 parseStructuralElement();
3134 --Line->Level;
3135 }
3136 for (bool SeenCatch = false;;) {
3137 if (FormatTok->is(tok::at))
3138 nextToken();
3139 if (FormatTok->isNoneOf(tok::kw_catch, Keywords.kw___except,
3140 tok::kw___finally, tok::objc_catch,
3141 tok::objc_finally) &&
3142 !((Style.isJava() || Style.isJavaScript()) &&
3143 FormatTok->is(Keywords.kw_finally))) {
3144 break;
3145 }
3146 if (FormatTok->is(tok::kw_catch))
3147 SeenCatch = true;
3148 nextToken();
3149 while (FormatTok->isNot(tok::l_brace)) {
3150 if (FormatTok->is(tok::l_paren)) {
3151 parseParens();
3152 continue;
3153 }
3154 if (FormatTok->isOneOf(tok::semi, tok::r_brace) || eof()) {
3155 if (Style.RemoveBracesLLVM)
3156 NestedTooDeep.pop_back();
3157 return;
3158 }
3159 nextToken();
3160 }
3161 if (SeenCatch) {
3162 FormatTok->setFinalizedType(TT_ControlStatementLBrace);
3163 SeenCatch = false;
3164 }
3165 NeedsUnwrappedLine = false;
3166 Line->MustBeDeclaration = false;
3167 CompoundStatementIndenter Indenter(this, Style, Line->Level);
3168 parseBlock();
3169 if (Style.BraceWrapping.BeforeCatch)
3170 addUnwrappedLine();
3171 else
3172 NeedsUnwrappedLine = true;
3173 }
3174
3175 if (Style.RemoveBracesLLVM)
3176 NestedTooDeep.pop_back();
3177
3178 if (NeedsUnwrappedLine)
3179 addUnwrappedLine();
3180}
3181
3182void UnwrappedLineParser::parseNamespaceOrExportBlock(unsigned AddLevels) {
3183 bool ManageWhitesmithsBraces =
3184 AddLevels == 0u && Style.BreakBeforeBraces == FormatStyle::BS_Whitesmiths;
3185
3186 // If we're in Whitesmiths mode, indent the brace if we're not indenting
3187 // the whole block.
3188 if (ManageWhitesmithsBraces)
3189 ++Line->Level;
3190
3191 // Munch the semicolon after the block. This is more common than one would
3192 // think. Putting the semicolon into its own line is very ugly.
3193 parseBlock(/*MustBeDeclaration=*/true, AddLevels, /*MunchSemi=*/true,
3194 /*KeepBraces=*/true, /*IfKind=*/nullptr, ManageWhitesmithsBraces);
3195
3196 addUnwrappedLine(AddLevels > 0 ? LineLevel::Remove : LineLevel::Keep);
3197
3198 if (ManageWhitesmithsBraces)
3199 --Line->Level;
3200}
3201
3202void UnwrappedLineParser::parseNamespace() {
3203 assert(FormatTok->isOneOf(tok::kw_namespace, TT_NamespaceMacro) &&
3204 "'namespace' expected");
3205
3206 const FormatToken &InitialToken = *FormatTok;
3207 nextToken();
3208 if (InitialToken.is(TT_NamespaceMacro)) {
3209 parseParens();
3210 } else {
3211 while (FormatTok->isOneOf(tok::identifier, tok::coloncolon, tok::kw_inline,
3212 tok::l_square, tok::period, tok::l_paren) ||
3213 (Style.isCSharp() && FormatTok->is(tok::kw_union))) {
3214 if (FormatTok->is(tok::l_square))
3215 parseSquare();
3216 else if (FormatTok->is(tok::l_paren))
3217 parseParens();
3218 else
3219 nextToken();
3220 }
3221 }
3222 if (FormatTok->is(tok::l_brace)) {
3223 FormatTok->setFinalizedType(TT_NamespaceLBrace);
3224
3225 if (ShouldBreakBeforeBrace(Style, InitialToken,
3226 Tokens->peekNextToken()->is(tok::r_brace))) {
3227 addUnwrappedLine();
3228 }
3229
3230 unsigned AddLevels =
3231 Style.NamespaceIndentation == FormatStyle::NI_All ||
3232 (Style.NamespaceIndentation == FormatStyle::NI_Inner &&
3233 DeclarationScopeStack.size() > 1)
3234 ? 1u
3235 : 0u;
3236 parseNamespaceOrExportBlock(AddLevels);
3237 }
3238 // FIXME: Add error handling.
3239}
3240
3241void UnwrappedLineParser::parseCppExportBlock() {
3242 parseNamespaceOrExportBlock(/*AddLevels=*/Style.IndentExportBlock ? 1 : 0);
3243}
3244
3245void UnwrappedLineParser::parseNew() {
3246 assert(FormatTok->is(tok::kw_new) && "'new' expected");
3247 nextToken();
3248
3249 if (Style.isCSharp()) {
3250 do {
3251 // Handle constructor invocation, e.g. `new(field: value)`.
3252 if (FormatTok->is(tok::l_paren))
3253 parseParens();
3254
3255 // Handle array initialization syntax, e.g. `new[] {10, 20, 30}`.
3256 if (FormatTok->is(tok::l_brace))
3257 parseBracedList();
3258
3259 if (FormatTok->isOneOf(tok::semi, tok::comma))
3260 return;
3261
3262 nextToken();
3263 } while (!eof());
3264 }
3265
3266 if (!Style.isJava())
3267 return;
3268
3269 // In Java, we can parse everything up to the parens, which aren't optional.
3270 do {
3271 // There should not be a ;, { or } before the new's open paren.
3272 if (FormatTok->isOneOf(tok::semi, tok::l_brace, tok::r_brace))
3273 return;
3274
3275 // Consume the parens.
3276 if (FormatTok->is(tok::l_paren)) {
3277 parseParens();
3278
3279 // If there is a class body of an anonymous class, consume that as child.
3280 if (FormatTok->is(tok::l_brace))
3281 parseChildBlock();
3282 return;
3283 }
3284 nextToken();
3285 } while (!eof());
3286}
3287
3288void UnwrappedLineParser::parseLoopBody(bool KeepBraces, bool WrapRightBrace) {
3289 keepAncestorBraces();
3290
3291 if (isBlockBegin(*FormatTok)) {
3292 FormatTok->setFinalizedType(TT_ControlStatementLBrace);
3293 FormatToken *LeftBrace = FormatTok;
3294 CompoundStatementIndenter Indenter(this, Style, Line->Level);
3295 parseBlock(/*MustBeDeclaration=*/false, /*AddLevels=*/1u,
3296 /*MunchSemi=*/true, KeepBraces);
3297 setPreviousRBraceType(TT_ControlStatementRBrace);
3298 if (!KeepBraces) {
3299 assert(!NestedTooDeep.empty());
3300 if (!NestedTooDeep.back())
3301 markOptionalBraces(LeftBrace);
3302 }
3303 if (WrapRightBrace)
3304 addUnwrappedLine();
3305 } else {
3306 parseUnbracedBody();
3307 }
3308
3309 if (!KeepBraces)
3310 NestedTooDeep.pop_back();
3311}
3312
3313void UnwrappedLineParser::parseForOrWhileLoop(bool HasParens) {
3314 assert((FormatTok->isOneOf(tok::kw_for, tok::kw_while, TT_ForEachMacro) ||
3315 (Style.isVerilog() &&
3316 FormatTok->isOneOf(Keywords.kw_always, Keywords.kw_always_comb,
3317 Keywords.kw_always_ff, Keywords.kw_always_latch,
3318 Keywords.kw_final, Keywords.kw_initial,
3319 Keywords.kw_foreach, Keywords.kw_forever,
3320 Keywords.kw_repeat))) &&
3321 "'for', 'while' or foreach macro expected");
3322 const bool KeepBraces = !Style.RemoveBracesLLVM ||
3323 FormatTok->isNoneOf(tok::kw_for, tok::kw_while);
3324
3325 nextToken();
3326 // JS' for await ( ...
3327 if (Style.isJavaScript() && FormatTok->is(Keywords.kw_await))
3328 nextToken();
3329 if (IsCpp && FormatTok->is(tok::kw_co_await))
3330 nextToken();
3331 if (HasParens && FormatTok->is(tok::l_paren)) {
3332 // The type is only set for Verilog basically because we were afraid to
3333 // change the existing behavior for loops. See the discussion on D121756 for
3334 // details.
3335 if (Style.isVerilog())
3336 FormatTok->setFinalizedType(TT_ConditionLParen);
3337 parseParens();
3338 }
3339
3340 if (Style.isVerilog()) {
3341 // Event control.
3342 parseVerilogSensitivityList();
3343 } else if (Style.AllowShortLoopsOnASingleLine && FormatTok->is(tok::semi) &&
3344 Tokens->getPreviousToken()->is(tok::r_paren)) {
3345 nextToken();
3346 addUnwrappedLine();
3347 return;
3348 }
3349
3350 handleAttributes();
3351 parseLoopBody(KeepBraces, /*WrapRightBrace=*/true);
3352}
3353
3354void UnwrappedLineParser::parseDoWhile() {
3355 assert(FormatTok->is(tok::kw_do) && "'do' expected");
3356 nextToken();
3357
3358 parseLoopBody(/*KeepBraces=*/true, Style.BraceWrapping.BeforeWhile);
3359
3360 // FIXME: Add error handling.
3361 if (FormatTok->isNot(tok::kw_while)) {
3362 addUnwrappedLine();
3363 return;
3364 }
3365
3366 FormatTok->setFinalizedType(TT_DoWhile);
3367
3368 // If in Whitesmiths mode, the line with the while() needs to be indented
3369 // to the same level as the block.
3370 if (Style.BreakBeforeBraces == FormatStyle::BS_Whitesmiths)
3371 ++Line->Level;
3372
3373 nextToken();
3374 parseStructuralElement();
3375}
3376
3377void UnwrappedLineParser::parseLabel(
3378 FormatStyle::IndentGotoLabelStyle IndentGotoLabels) {
3379 nextToken();
3380 unsigned OldLineLevel = Line->Level;
3381
3382 switch (IndentGotoLabels) {
3383 case FormatStyle::IGLS_NoIndent:
3384 Line->Level = 0;
3385 break;
3386 case FormatStyle::IGLS_OuterIndent:
3387 if (Line->Level > 1 || (!Line->InPPDirective && Line->Level > 0))
3388 --Line->Level;
3389 break;
3390 case FormatStyle::IGLS_HalfIndent:
3391 case FormatStyle::IGLS_InnerIndent:
3392 break;
3393 }
3394
3395 if (!Style.IndentCaseBlocks && CommentsBeforeNextToken.empty() &&
3396 FormatTok->is(tok::l_brace)) {
3397
3398 CompoundStatementIndenter Indenter(this, Line->Level,
3399 Style.BraceWrapping.AfterCaseLabel,
3400 Style.BraceWrapping.IndentBraces);
3401 parseBlock();
3402 if (FormatTok->is(tok::kw_break)) {
3403 if (Style.BraceWrapping.AfterControlStatement ==
3404 FormatStyle::BWACS_Always) {
3405 addUnwrappedLine();
3406 if (!Style.IndentCaseBlocks &&
3407 Style.BreakBeforeBraces == FormatStyle::BS_Whitesmiths) {
3408 ++Line->Level;
3409 }
3410 }
3411 parseStructuralElement();
3412 }
3413 addUnwrappedLine();
3414 } else {
3415 if (FormatTok->is(tok::semi))
3416 nextToken();
3417 addUnwrappedLine();
3418 }
3419 Line->Level = OldLineLevel;
3420 if (FormatTok->isNot(tok::l_brace)) {
3421 parseStructuralElement();
3422 addUnwrappedLine();
3423 }
3424}
3425
3426void UnwrappedLineParser::parseCaseLabel() {
3427 assert(FormatTok->is(tok::kw_case) && "'case' expected");
3428 auto *Case = FormatTok;
3429
3430 // FIXME: fix handling of complex expressions here.
3431 do {
3432 nextToken();
3433 if (FormatTok->is(tok::colon)) {
3434 FormatTok->setFinalizedType(TT_CaseLabelColon);
3435 break;
3436 }
3437 if (Style.isJava() && FormatTok->is(tok::arrow)) {
3438 FormatTok->setFinalizedType(TT_CaseLabelArrow);
3439 Case->setFinalizedType(TT_SwitchExpressionLabel);
3440 break;
3441 }
3442 } while (!eof());
3443 parseLabel();
3444}
3445
3446void UnwrappedLineParser::parseSwitch(bool IsExpr) {
3447 assert(FormatTok->is(tok::kw_switch) && "'switch' expected");
3448 nextToken();
3449 if (FormatTok->is(tok::l_paren))
3450 parseParens();
3451
3452 keepAncestorBraces();
3453
3454 if (FormatTok->is(tok::l_brace)) {
3455 CompoundStatementIndenter Indenter(this, Style, Line->Level);
3456 FormatTok->setFinalizedType(IsExpr ? TT_SwitchExpressionLBrace
3457 : TT_ControlStatementLBrace);
3458 if (IsExpr)
3459 parseChildBlock();
3460 else
3461 parseBlock();
3462 setPreviousRBraceType(TT_ControlStatementRBrace);
3463 if (!IsExpr)
3464 addUnwrappedLine();
3465 } else {
3466 addUnwrappedLine();
3467 ++Line->Level;
3468 parseStructuralElement();
3469 --Line->Level;
3470 }
3471
3472 if (Style.RemoveBracesLLVM)
3473 NestedTooDeep.pop_back();
3474}
3475
3476void UnwrappedLineParser::parseAccessSpecifier() {
3477 nextToken();
3478 // Understand Qt's slots.
3479 if (FormatTok->isOneOf(Keywords.kw_slots, Keywords.kw_qslots))
3480 nextToken();
3481 // Otherwise, we don't know what it is, and we'd better keep the next token.
3482 if (FormatTok->is(tok::colon))
3483 nextToken();
3484 addUnwrappedLine();
3485}
3486
3487/// Parses a requires, decides if it is a clause or an expression.
3488/// \pre The current token has to be the requires keyword.
3489/// \returns true if it parsed a clause.
3490bool UnwrappedLineParser::parseRequires(bool SeenEqual) {
3491 assert(FormatTok->is(tok::kw_requires) && "'requires' expected");
3492
3493 // We try to guess if it is a requires clause, or a requires expression. For
3494 // that we first check the next token.
3495 switch (Tokens->peekNextToken(/*SkipComment=*/true)->Tok.getKind()) {
3496 case tok::l_brace:
3497 // This can only be an expression, never a clause.
3498 parseRequiresExpression();
3499 return false;
3500 case tok::l_paren:
3501 // Clauses and expression can start with a paren, it's unclear what we have.
3502 break;
3503 default:
3504 // All other tokens can only be a clause.
3505 parseRequiresClause();
3506 return true;
3507 }
3508
3509 // Looking forward we would have to decide if there are function declaration
3510 // like arguments to the requires expression:
3511 // requires (T t) {
3512 // Or there is a constraint expression for the requires clause:
3513 // requires (C<T> && ...
3514
3515 // But first let's look behind.
3516 auto *PreviousNonComment = FormatTok->getPreviousNonComment();
3517
3518 if (!PreviousNonComment ||
3519 PreviousNonComment->is(TT_RequiresExpressionLBrace)) {
3520 // If there is no token, or an expression left brace, we are a requires
3521 // clause within a requires expression.
3522 parseRequiresClause();
3523 return true;
3524 }
3525
3526 switch (PreviousNonComment->Tok.getKind()) {
3527 case tok::greater:
3528 case tok::r_paren:
3529 case tok::kw_noexcept:
3530 case tok::kw_const:
3531 case tok::star:
3532 case tok::amp:
3533 // This is a requires clause.
3534 parseRequiresClause();
3535 return true;
3536 case tok::ampamp: {
3537 // This can be either:
3538 // if (... && requires (T t) ...)
3539 // Or
3540 // void member(...) && requires (C<T> ...
3541 // We check the one token before that for a const:
3542 // void member(...) const && requires (C<T> ...
3543 auto PrevPrev = PreviousNonComment->getPreviousNonComment();
3544 if ((PrevPrev && PrevPrev->is(tok::kw_const)) || !SeenEqual) {
3545 parseRequiresClause();
3546 return true;
3547 }
3548 break;
3549 }
3550 default:
3551 if (PreviousNonComment->isTypeOrIdentifier(LangOpts)) {
3552 // This is a requires clause.
3553 parseRequiresClause();
3554 return true;
3555 }
3556 // It's an expression.
3557 parseRequiresExpression();
3558 return false;
3559 }
3560
3561 // Now we look forward and try to check if the paren content is a parameter
3562 // list. The parameters can be cv-qualified and contain references or
3563 // pointers.
3564 // So we want basically to check for TYPE NAME, but TYPE can contain all kinds
3565 // of stuff: typename, const, *, &, &&, ::, identifiers.
3566
3567 unsigned StoredPosition = Tokens->getPosition();
3568 FormatToken *NextToken = Tokens->getNextToken();
3569 int Lookahead = 0;
3570 auto PeekNext = [&Lookahead, &NextToken, this] {
3571 ++Lookahead;
3572 NextToken = Tokens->getNextToken();
3573 };
3574
3575 bool FoundType = false;
3576 bool LastWasColonColon = false;
3577 int OpenAngles = 0;
3578
3579 for (; Lookahead < 50; PeekNext()) {
3580 switch (NextToken->Tok.getKind()) {
3581 case tok::kw_volatile:
3582 case tok::kw_const:
3583 case tok::comma:
3584 if (OpenAngles == 0) {
3585 FormatTok = Tokens->setPosition(StoredPosition);
3586 parseRequiresExpression();
3587 return false;
3588 }
3589 break;
3590 case tok::eof:
3591 // Break out of the loop.
3592 Lookahead = 50;
3593 break;
3594 case tok::coloncolon:
3595 LastWasColonColon = true;
3596 break;
3597 case tok::kw_decltype:
3598 case tok::identifier:
3599 if (FoundType && !LastWasColonColon && OpenAngles == 0) {
3600 FormatTok = Tokens->setPosition(StoredPosition);
3601 parseRequiresExpression();
3602 return false;
3603 }
3604 FoundType = true;
3605 LastWasColonColon = false;
3606 break;
3607 case tok::less:
3608 ++OpenAngles;
3609 break;
3610 case tok::greater:
3611 --OpenAngles;
3612 break;
3613 default:
3614 if (NextToken->isTypeName(LangOpts)) {
3615 FormatTok = Tokens->setPosition(StoredPosition);
3616 parseRequiresExpression();
3617 return false;
3618 }
3619 break;
3620 }
3621 }
3622 // This seems to be a complicated expression, just assume it's a clause.
3623 FormatTok = Tokens->setPosition(StoredPosition);
3624 parseRequiresClause();
3625 return true;
3626}
3627
3628/// Parses a requires clause.
3629/// \sa parseRequiresExpression
3630///
3631/// Returns if it either has finished parsing the clause, or it detects, that
3632/// the clause is incorrect.
3633void UnwrappedLineParser::parseRequiresClause() {
3634 assert(FormatTok->is(tok::kw_requires) && "'requires' expected");
3635
3636 // If there is no previous token, we are within a requires expression,
3637 // otherwise we will always have the template or function declaration in front
3638 // of it.
3639 bool InRequiresExpression =
3640 !FormatTok->Previous ||
3641 FormatTok->Previous->is(TT_RequiresExpressionLBrace);
3642
3643 FormatTok->setFinalizedType(InRequiresExpression
3644 ? TT_RequiresClauseInARequiresExpression
3645 : TT_RequiresClause);
3646 nextToken();
3647
3648 // NOTE: parseConstraintExpression is only ever called from this function.
3649 // It could be inlined into here.
3650 parseConstraintExpression();
3651
3652 if (!InRequiresExpression && FormatTok->Previous)
3653 FormatTok->Previous->ClosesRequiresClause = true;
3654}
3655
3656/// Parses a requires expression.
3657/// \sa parseRequiresClause
3658///
3659/// Returns if it either has finished parsing the expression, or it detects,
3660/// that the expression is incorrect.
3661void UnwrappedLineParser::parseRequiresExpression() {
3662 assert(FormatTok->is(tok::kw_requires) && "'requires' expected");
3663
3664 FormatTok->setFinalizedType(TT_RequiresExpression);
3665 nextToken();
3666
3667 if (FormatTok->is(tok::l_paren)) {
3668 FormatTok->setFinalizedType(TT_RequiresExpressionLParen);
3669 parseParens();
3670 }
3671
3672 if (FormatTok->is(tok::l_brace)) {
3673 FormatTok->setFinalizedType(TT_RequiresExpressionLBrace);
3674 parseChildBlock();
3675 }
3676}
3677
3678/// Parses a constraint expression.
3679///
3680/// This is the body of a requires clause. It returns, when the parsing is
3681/// complete, or the expression is incorrect.
3682void UnwrappedLineParser::parseConstraintExpression() {
3683 // The special handling for lambdas is needed since tryToParseLambda() eats a
3684 // token and if a requires expression is the last part of a requires clause
3685 // and followed by an attribute like [[nodiscard]] the ClosesRequiresClause is
3686 // not set on the correct token. Thus we need to be aware if we even expect a
3687 // lambda to be possible.
3688 // template <typename T> requires requires { ... } [[nodiscard]] ...;
3689 bool LambdaNextTimeAllowed = true;
3690
3691 // Within lambda declarations, it is permitted to put a requires clause after
3692 // its template parameter list, which would place the requires clause right
3693 // before the parentheses of the parameters of the lambda declaration. Thus,
3694 // we track if we expect to see grouping parentheses at all.
3695 // Without this check, `requires foo<T> (T t)` in the below example would be
3696 // seen as the whole requires clause, accidentally eating the parameters of
3697 // the lambda.
3698 // [&]<typename T> requires foo<T> (T t) { ... };
3699 bool TopLevelParensAllowed = true;
3700
3701 do {
3702 bool LambdaThisTimeAllowed = std::exchange(LambdaNextTimeAllowed, false);
3703
3704 switch (FormatTok->Tok.getKind()) {
3705 case tok::kw_requires:
3706 parseRequiresExpression();
3707 break;
3708
3709 case tok::l_paren:
3710 if (!TopLevelParensAllowed)
3711 return;
3712 parseParens(/*AmpAmpTokenType=*/TT_BinaryOperator);
3713 TopLevelParensAllowed = false;
3714 break;
3715
3716 case tok::l_square:
3717 if (!LambdaThisTimeAllowed || !tryToParseLambda())
3718 return;
3719 break;
3720
3721 case tok::kw_const:
3722 case tok::semi:
3723 case tok::kw_class:
3724 case tok::kw_struct:
3725 case tok::kw_union:
3726 return;
3727
3728 case tok::l_brace:
3729 // Potential function body.
3730 return;
3731
3732 case tok::ampamp:
3733 case tok::pipepipe:
3734 FormatTok->setFinalizedType(TT_BinaryOperator);
3735 nextToken();
3736 LambdaNextTimeAllowed = true;
3737 TopLevelParensAllowed = true;
3738 break;
3739
3740 case tok::comma:
3741 case tok::comment:
3742 LambdaNextTimeAllowed = LambdaThisTimeAllowed;
3743 nextToken();
3744 break;
3745
3746 case tok::kw_sizeof:
3747 case tok::greater:
3748 case tok::greaterequal:
3749 case tok::greatergreater:
3750 case tok::less:
3751 case tok::lessequal:
3752 case tok::lessless:
3753 case tok::equalequal:
3754 case tok::exclaim:
3755 case tok::exclaimequal:
3756 case tok::plus:
3757 case tok::minus:
3758 case tok::star:
3759 case tok::slash:
3760 LambdaNextTimeAllowed = true;
3761 TopLevelParensAllowed = true;
3762 // Just eat them.
3763 nextToken();
3764 break;
3765
3766 case tok::numeric_constant:
3767 case tok::coloncolon:
3768 case tok::kw_true:
3769 case tok::kw_false:
3770 TopLevelParensAllowed = false;
3771 // Just eat them.
3772 nextToken();
3773 break;
3774
3775 case tok::kw_static_cast:
3776 case tok::kw_const_cast:
3777 case tok::kw_reinterpret_cast:
3778 case tok::kw_dynamic_cast:
3779 nextToken();
3780 if (FormatTok->isNot(tok::less))
3781 return;
3782
3783 nextToken();
3784 parseBracedList(/*IsAngleBracket=*/true);
3785 break;
3786
3787 default:
3788 if (!FormatTok->Tok.getIdentifierInfo()) {
3789 // Identifiers are part of the default case, we check for more then
3790 // tok::identifier to handle builtin type traits.
3791 return;
3792 }
3793
3794 // We need to differentiate identifiers for a template deduction guide,
3795 // variables, or function return types (the constraint expression has
3796 // ended before that), and basically all other cases. But it's easier to
3797 // check the other way around.
3798 assert(FormatTok->Previous);
3799 switch (FormatTok->Previous->Tok.getKind()) {
3800 case tok::coloncolon: // Nested identifier.
3801 case tok::ampamp: // Start of a function or variable for the
3802 case tok::pipepipe: // constraint expression. (binary)
3803 case tok::exclaim: // The same as above, but unary.
3804 case tok::kw_requires: // Initial identifier of a requires clause.
3805 case tok::equal: // Initial identifier of a concept declaration.
3806 break;
3807 default:
3808 return;
3809 }
3810
3811 // Read identifier with optional template declaration.
3812 nextToken();
3813 if (FormatTok->is(tok::less)) {
3814 nextToken();
3815 parseBracedList(/*IsAngleBracket=*/true);
3816 }
3817 TopLevelParensAllowed = false;
3818 break;
3819 }
3820 } while (!eof());
3821}
3822
3823bool UnwrappedLineParser::parseEnum() {
3824 const FormatToken &InitialToken = *FormatTok;
3825
3826 // Won't be 'enum' for NS_ENUMs.
3827 if (FormatTok->is(tok::kw_enum))
3828 nextToken();
3829
3830 // In TypeScript, "enum" can also be used as property name, e.g. in interface
3831 // declarations. An "enum" keyword followed by a colon would be a syntax
3832 // error and thus assume it is just an identifier.
3833 if (Style.isJavaScript() && FormatTok->isOneOf(tok::colon, tok::question))
3834 return false;
3835
3836 // In protobuf, "enum" can be used as a field name.
3837 if (Style.Language == FormatStyle::LK_Proto && FormatTok->is(tok::equal))
3838 return false;
3839
3840 if (IsCpp) {
3841 // Eat up enum class ...
3842 if (FormatTok->isOneOf(tok::kw_class, tok::kw_struct))
3843 nextToken();
3844 while (FormatTok->is(tok::l_square))
3845 if (!handleCppAttributes())
3846 return false;
3847 }
3848
3849 while (FormatTok->Tok.getIdentifierInfo() ||
3850 FormatTok->isOneOf(tok::colon, tok::coloncolon, tok::less,
3851 tok::greater, tok::comma, tok::question,
3852 tok::l_square)) {
3853 if (FormatTok->is(tok::colon))
3854 FormatTok->setFinalizedType(TT_EnumUnderlyingTypeColon);
3855 if (Style.isVerilog()) {
3856 FormatTok->setFinalizedType(TT_VerilogDimensionedTypeName);
3857 nextToken();
3858 // In Verilog the base type can have dimensions.
3859 while (FormatTok->is(tok::l_square))
3860 parseSquare();
3861 } else {
3862 nextToken();
3863 }
3864 // We can have macros or attributes in between 'enum' and the enum name.
3865 if (FormatTok->is(tok::l_paren))
3866 parseParens();
3867 if (FormatTok->is(tok::identifier)) {
3868 nextToken();
3869 // If there are two identifiers in a row, this is likely an elaborate
3870 // return type. In Java, this can be "implements", etc.
3871 if (IsCpp && FormatTok->is(tok::identifier))
3872 return false;
3873 }
3874 }
3875
3876 // Just a declaration or something is wrong.
3877 if (FormatTok->isNot(tok::l_brace))
3878 return true;
3879 FormatTok->setFinalizedType(TT_EnumLBrace);
3880 FormatTok->setBlockKind(BK_Block);
3881
3882 if (Style.isJava()) {
3883 // Java enums are different.
3884 parseJavaEnumBody();
3885 return true;
3886 }
3887 if (Style.Language == FormatStyle::LK_Proto) {
3888 parseBlock(/*MustBeDeclaration=*/true);
3889 return true;
3890 }
3891
3892 const bool ManageWhitesmithsBraces =
3893 Style.BreakBeforeBraces == FormatStyle::BS_Whitesmiths;
3894
3895 if (!Style.AllowShortEnumsOnASingleLine &&
3896 ShouldBreakBeforeBrace(Style, InitialToken,
3897 Tokens->peekNextToken()->is(tok::r_brace))) {
3898 addUnwrappedLine();
3899
3900 // If we're in Whitesmiths mode, indent the brace if we're not indenting
3901 // the whole block.
3902 if (ManageWhitesmithsBraces)
3903 ++Line->Level;
3904 }
3905 // Parse enum body.
3906 nextToken();
3907 if (!Style.AllowShortEnumsOnASingleLine) {
3908 addUnwrappedLine();
3909 if (!ManageWhitesmithsBraces)
3910 ++Line->Level;
3911 }
3912 const auto OpeningLineIndex = CurrentLines->empty()
3913 ? UnwrappedLine::kInvalidIndex
3914 : CurrentLines->size() - 1;
3915 bool HasError = !parseBracedList(/*IsAngleBracket=*/false, /*IsEnum=*/true);
3916 if (!Style.AllowShortEnumsOnASingleLine && !ManageWhitesmithsBraces)
3917 --Line->Level;
3918 if (HasError) {
3919 if (FormatTok->is(tok::semi))
3920 nextToken();
3921 addUnwrappedLine();
3922 }
3923 setPreviousRBraceType(TT_EnumRBrace);
3924 if (ManageWhitesmithsBraces)
3925 Line->MatchingOpeningBlockLineIndex = OpeningLineIndex;
3926 return true;
3927
3928 // There is no addUnwrappedLine() here so that we fall through to parsing a
3929 // structural element afterwards. Thus, in "enum A {} n, m;",
3930 // "} n, m;" will end up in one unwrapped line.
3931}
3932
3933bool UnwrappedLineParser::parseStructLike() {
3934 // parseRecord falls through and does not yet add an unwrapped line as a
3935 // record declaration or definition can start a structural element.
3936 parseRecord();
3937 // This does not apply to Java, JavaScript and C#.
3938 if (Style.isJava() || Style.isJavaScript() || Style.isCSharp()) {
3939 if (FormatTok->is(tok::semi))
3940 nextToken();
3941 addUnwrappedLine();
3942 return true;
3943 }
3944 return false;
3945}
3946
3947namespace {
3948// A class used to set and restore the Token position when peeking
3949// ahead in the token source.
3950class ScopedTokenPosition {
3951 unsigned StoredPosition;
3952 FormatTokenSource *Tokens;
3953
3954public:
3955 ScopedTokenPosition(FormatTokenSource *Tokens) : Tokens(Tokens) {
3956 assert(Tokens && "Tokens expected to not be null");
3957 StoredPosition = Tokens->getPosition();
3958 }
3959
3960 ~ScopedTokenPosition() { Tokens->setPosition(StoredPosition); }
3961};
3962} // namespace
3963
3964// Look to see if we have [[ by looking ahead, if
3965// its not then rewind to the original position.
3966bool UnwrappedLineParser::tryToParseSimpleAttribute() {
3967 ScopedTokenPosition AutoPosition(Tokens);
3968 FormatToken *Tok = Tokens->getNextToken();
3969 // We already read the first [ check for the second.
3970 if (Tok->isNot(tok::l_square))
3971 return false;
3972 // Double check that the attribute is just something
3973 // fairly simple.
3974 while (Tok->isNot(tok::eof)) {
3975 if (Tok->is(tok::r_square))
3976 break;
3977 Tok = Tokens->getNextToken();
3978 }
3979 if (Tok->is(tok::eof))
3980 return false;
3981 Tok = Tokens->getNextToken();
3982 if (Tok->isNot(tok::r_square))
3983 return false;
3984 Tok = Tokens->getNextToken();
3985 if (Tok->is(tok::semi))
3986 return false;
3987 return true;
3988}
3989
3990void UnwrappedLineParser::parseJavaEnumBody() {
3991 assert(FormatTok->is(tok::l_brace));
3992 const FormatToken *OpeningBrace = FormatTok;
3993
3994 // Determine whether the enum is simple, i.e. does not have a semicolon or
3995 // constants with class bodies. Simple enums can be formatted like braced
3996 // lists, contracted to a single line, etc.
3997 unsigned StoredPosition = Tokens->getPosition();
3998 bool IsSimple = true;
3999 FormatToken *Tok = Tokens->getNextToken();
4000 while (Tok->isNot(tok::eof)) {
4001 if (Tok->is(tok::r_brace))
4002 break;
4003 if (Tok->isOneOf(tok::l_brace, tok::semi)) {
4004 IsSimple = false;
4005 break;
4006 }
4007 // FIXME: This will also mark enums with braces in the arguments to enum
4008 // constants as "not simple". This is probably fine in practice, though.
4009 Tok = Tokens->getNextToken();
4010 }
4011 FormatTok = Tokens->setPosition(StoredPosition);
4012
4013 if (IsSimple) {
4014 nextToken();
4015 parseBracedList();
4016 addUnwrappedLine();
4017 return;
4018 }
4019
4020 // Parse the body of a more complex enum.
4021 // First add a line for everything up to the "{".
4022 nextToken();
4023 addUnwrappedLine();
4024 ++Line->Level;
4025
4026 // Parse the enum constants.
4027 while (!eof()) {
4028 if (FormatTok->is(tok::l_brace)) {
4029 // Parse the constant's class body.
4030 parseBlock(/*MustBeDeclaration=*/true, /*AddLevels=*/1u,
4031 /*MunchSemi=*/false);
4032 } else if (FormatTok->is(tok::l_paren)) {
4033 parseParens();
4034 } else if (FormatTok->is(tok::comma)) {
4035 nextToken();
4036 addUnwrappedLine();
4037 } else if (FormatTok->is(tok::semi)) {
4038 nextToken();
4039 addUnwrappedLine();
4040 break;
4041 } else if (FormatTok->is(tok::r_brace)) {
4042 addUnwrappedLine();
4043 break;
4044 } else {
4045 nextToken();
4046 }
4047 }
4048
4049 // Parse the class body after the enum's ";" if any.
4050 parseLevel(OpeningBrace);
4051 nextToken();
4052 --Line->Level;
4053 addUnwrappedLine();
4054}
4055
4056void UnwrappedLineParser::parseRecord(bool ParseAsExpr, bool IsJavaRecord) {
4057 assert(!IsJavaRecord || FormatTok->is(Keywords.kw_record));
4058 const FormatToken &InitialToken = *FormatTok;
4059 nextToken();
4060
4061 FormatToken *ClassName =
4062 IsJavaRecord && FormatTok->is(tok::identifier) ? FormatTok : nullptr;
4063 bool IsDerived = false;
4064 auto IsNonMacroIdentifier = [](const FormatToken *Tok) {
4065 return Tok->is(tok::identifier) && Tok->TokenText != Tok->TokenText.upper();
4066 };
4067 // JavaScript/TypeScript supports anonymous classes like:
4068 // a = class extends foo { }
4069 bool JSPastExtendsOrImplements = false;
4070 // The actual identifier can be a nested name specifier, and in macros
4071 // it is often token-pasted.
4072 // An [[attribute]] can be before the identifier.
4073 while (FormatTok->isOneOf(tok::identifier, tok::coloncolon, tok::hashhash,
4074 tok::kw_alignas, tok::l_square) ||
4075 FormatTok->isAttribute() ||
4076 ((Style.isJava() || Style.isJavaScript()) &&
4077 FormatTok->isOneOf(tok::period, tok::comma))) {
4078 if (Style.isJavaScript() &&
4079 FormatTok->isOneOf(Keywords.kw_extends, Keywords.kw_implements)) {
4080 JSPastExtendsOrImplements = true;
4081 // JavaScript/TypeScript supports inline object types in
4082 // extends/implements positions:
4083 // class Foo implements {bar: number} { }
4084 nextToken();
4085 if (FormatTok->is(tok::l_brace)) {
4086 tryToParseBracedList();
4087 continue;
4088 }
4089 }
4090 if (FormatTok->is(tok::l_square) && handleCppAttributes())
4091 continue;
4092 auto *Previous = FormatTok;
4093 nextToken();
4094 switch (FormatTok->Tok.getKind()) {
4095 case tok::l_paren:
4096 // We can have macros in between 'class' and the class name.
4097 if (IsJavaRecord || !IsNonMacroIdentifier(Previous) ||
4098 // e.g. `struct macro(a) S { int i; };`
4099 Previous->Previous == &InitialToken) {
4100 parseParens();
4101 }
4102 break;
4103 case tok::coloncolon:
4104 case tok::hashhash:
4105 break;
4106 default:
4107 if (JSPastExtendsOrImplements || ClassName ||
4108 Previous->isNot(tok::identifier) || Previous->is(TT_AttributeMacro)) {
4109 break;
4110 }
4111 if (const auto Text = Previous->TokenText;
4112 Text.size() == 1 || Text != Text.upper()) {
4113 ClassName = Previous;
4114 }
4115 }
4116 }
4117
4118 auto IsListInitialization = [&] {
4119 if (!ClassName || IsDerived || JSPastExtendsOrImplements)
4120 return false;
4121 assert(FormatTok->is(tok::l_brace));
4122 const auto *Prev = FormatTok->getPreviousNonComment();
4123 assert(Prev);
4124 return Prev != ClassName && Prev->is(tok::identifier) &&
4125 Prev->isNot(Keywords.kw_final) && tryToParseBracedList();
4126 };
4127
4128 if (FormatTok->isOneOf(tok::colon, tok::less)) {
4129 int AngleNestingLevel = 0;
4130 do {
4131 if (FormatTok->is(tok::less))
4132 ++AngleNestingLevel;
4133 else if (FormatTok->is(tok::greater))
4134 --AngleNestingLevel;
4135
4136 if (AngleNestingLevel == 0) {
4137 if (FormatTok->is(tok::colon)) {
4138 IsDerived = true;
4139 } else if (!IsDerived && FormatTok->is(tok::identifier) &&
4140 FormatTok->Previous->is(tok::coloncolon)) {
4141 ClassName = FormatTok;
4142 } else if (FormatTok->is(tok::l_paren) &&
4143 IsNonMacroIdentifier(FormatTok->Previous)) {
4144 break;
4145 }
4146 }
4147 if (FormatTok->is(tok::l_brace)) {
4148 if (AngleNestingLevel == 0 && IsListInitialization())
4149 return;
4150 calculateBraceTypes(/*ExpectClassBody=*/true);
4151 if (!tryToParseBracedList())
4152 break;
4153 }
4154 if (FormatTok->is(tok::l_square)) {
4155 FormatToken *Previous = FormatTok->Previous;
4156 if (!Previous || (Previous->isNot(tok::r_paren) &&
4157 !Previous->isTypeOrIdentifier(LangOpts))) {
4158 // Don't try parsing a lambda if we had a closing parenthesis before,
4159 // it was probably a pointer to an array: int (*)[].
4160 if (!tryToParseLambda())
4161 continue;
4162 } else {
4163 parseSquare();
4164 continue;
4165 }
4166 }
4167 if (FormatTok->is(tok::semi))
4168 return;
4169 if (Style.isCSharp() && FormatTok->is(Keywords.kw_where)) {
4170 addUnwrappedLine();
4171 nextToken();
4172 parseCSharpGenericTypeConstraint();
4173 break;
4174 }
4175 nextToken();
4176 } while (!eof());
4177 }
4178
4179 auto GetBraceTypes =
4180 [](const FormatToken &RecordTok) -> std::pair<TokenType, TokenType> {
4181 switch (RecordTok.Tok.getKind()) {
4182 case tok::kw_class:
4183 return {TT_ClassLBrace, TT_ClassRBrace};
4184 case tok::kw_struct:
4185 return {TT_StructLBrace, TT_StructRBrace};
4186 case tok::kw_union:
4187 return {TT_UnionLBrace, TT_UnionRBrace};
4188 default:
4189 // Useful for e.g. interface.
4190 return {TT_RecordLBrace, TT_RecordRBrace};
4191 }
4192 };
4193 if (FormatTok->is(tok::l_brace)) {
4194 if (IsListInitialization())
4195 return;
4196 if (ClassName)
4197 ClassName->setFinalizedType(TT_ClassHeadName);
4198 auto [OpenBraceType, ClosingBraceType] = GetBraceTypes(InitialToken);
4199 FormatTok->setFinalizedType(OpenBraceType);
4200 if (ParseAsExpr) {
4201 parseChildBlock();
4202 } else {
4203 if (ShouldBreakBeforeBrace(Style, InitialToken,
4204 Tokens->peekNextToken()->is(tok::r_brace),
4205 IsJavaRecord)) {
4206 addUnwrappedLine();
4207 }
4208
4209 unsigned AddLevels = Style.IndentAccessModifiers ? 2u : 1u;
4210 parseBlock(/*MustBeDeclaration=*/true, AddLevels, /*MunchSemi=*/false);
4211 }
4212 setPreviousRBraceType(ClosingBraceType);
4213 }
4214 // There is no addUnwrappedLine() here so that we fall through to parsing a
4215 // structural element afterwards. Thus, in "class A {} n, m;",
4216 // "} n, m;" will end up in one unwrapped line.
4217}
4218
4219void UnwrappedLineParser::parseObjCMethod() {
4220 assert(FormatTok->isOneOf(tok::l_paren, tok::identifier) &&
4221 "'(' or identifier expected.");
4222 do {
4223 if (FormatTok->is(tok::semi)) {
4224 nextToken();
4225 addUnwrappedLine();
4226 return;
4227 } else if (FormatTok->is(tok::l_brace)) {
4228 if (Style.BraceWrapping.AfterFunction)
4229 addUnwrappedLine();
4230 parseBlock();
4231 addUnwrappedLine();
4232 return;
4233 } else {
4234 nextToken();
4235 }
4236 } while (!eof());
4237}
4238
4239void UnwrappedLineParser::parseObjCProtocolList() {
4240 assert(FormatTok->is(tok::less) && "'<' expected.");
4241 do {
4242 nextToken();
4243 // Early exit in case someone forgot a close angle.
4244 if (FormatTok->isOneOf(tok::semi, tok::l_brace, tok::objc_end))
4245 return;
4246 } while (!eof() && FormatTok->isNot(tok::greater));
4247 nextToken(); // Skip '>'.
4248}
4249
4250void UnwrappedLineParser::parseObjCUntilAtEnd() {
4251 do {
4252 if (FormatTok->is(tok::objc_end)) {
4253 nextToken();
4254 addUnwrappedLine();
4255 break;
4256 }
4257 if (FormatTok->is(tok::l_brace)) {
4258 parseBlock();
4259 // In ObjC interfaces, nothing should be following the "}".
4260 addUnwrappedLine();
4261 } else if (FormatTok->is(tok::r_brace)) {
4262 // Ignore stray "}". parseStructuralElement doesn't consume them.
4263 nextToken();
4264 addUnwrappedLine();
4265 } else if (FormatTok->isOneOf(tok::minus, tok::plus)) {
4266 nextToken();
4267 parseObjCMethod();
4268 } else {
4269 parseStructuralElement();
4270 }
4271 } while (!eof());
4272}
4273
4274void UnwrappedLineParser::parseObjCInterfaceOrImplementation() {
4275 assert(FormatTok->isOneOf(tok::objc_interface, tok::objc_implementation));
4276 nextToken();
4277 nextToken(); // interface name
4278
4279 // @interface can be followed by a lightweight generic
4280 // specialization list, then either a base class or a category.
4281 if (FormatTok->is(tok::less))
4282 parseObjCLightweightGenerics();
4283 if (FormatTok->is(tok::colon)) {
4284 nextToken();
4285 nextToken(); // base class name
4286 // The base class can also have lightweight generics applied to it.
4287 if (FormatTok->is(tok::less))
4288 parseObjCLightweightGenerics();
4289 } else if (FormatTok->is(tok::l_paren)) {
4290 // Skip category, if present.
4291 parseParens();
4292 }
4293
4294 if (FormatTok->is(tok::less))
4295 parseObjCProtocolList();
4296
4297 if (FormatTok->is(tok::l_brace)) {
4298 if (Style.BraceWrapping.AfterObjCDeclaration)
4299 addUnwrappedLine();
4300 parseBlock(/*MustBeDeclaration=*/true);
4301 }
4302
4303 // With instance variables, this puts '}' on its own line. Without instance
4304 // variables, this ends the @interface line.
4305 addUnwrappedLine();
4306
4307 parseObjCUntilAtEnd();
4308}
4309
4310void UnwrappedLineParser::parseObjCLightweightGenerics() {
4311 assert(FormatTok->is(tok::less));
4312 // Unlike protocol lists, generic parameterizations support
4313 // nested angles:
4314 //
4315 // @interface Foo<ValueType : id <NSCopying, NSSecureCoding>> :
4316 // NSObject <NSCopying, NSSecureCoding>
4317 //
4318 // so we need to count how many open angles we have left.
4319 unsigned NumOpenAngles = 1;
4320 do {
4321 nextToken();
4322 // Early exit in case someone forgot a close angle.
4323 if (FormatTok->isOneOf(tok::semi, tok::l_brace, tok::objc_end))
4324 break;
4325 if (FormatTok->is(tok::less)) {
4326 ++NumOpenAngles;
4327 } else if (FormatTok->is(tok::greater)) {
4328 assert(NumOpenAngles > 0 && "'>' makes NumOpenAngles negative");
4329 --NumOpenAngles;
4330 }
4331 } while (!eof() && NumOpenAngles != 0);
4332 nextToken(); // Skip '>'.
4333}
4334
4335// Returns true for the declaration/definition form of @protocol,
4336// false for the expression form.
4337bool UnwrappedLineParser::parseObjCProtocol() {
4338 assert(FormatTok->is(tok::objc_protocol));
4339 nextToken();
4340
4341 if (FormatTok->is(tok::l_paren)) {
4342 // The expression form of @protocol, e.g. "Protocol* p = @protocol(foo);".
4343 return false;
4344 }
4345
4346 // The definition/declaration form,
4347 // @protocol Foo
4348 // - (int)someMethod;
4349 // @end
4350
4351 nextToken(); // protocol name
4352
4353 if (FormatTok->is(tok::less))
4354 parseObjCProtocolList();
4355
4356 // Check for protocol declaration.
4357 if (FormatTok->is(tok::semi)) {
4358 nextToken();
4359 addUnwrappedLine();
4360 return true;
4361 }
4362
4363 addUnwrappedLine();
4364 parseObjCUntilAtEnd();
4365 return true;
4366}
4367
4368void UnwrappedLineParser::parseJavaScriptEs6ImportExport() {
4369 bool IsImport = FormatTok->is(Keywords.kw_import);
4370 assert(IsImport || FormatTok->is(tok::kw_export));
4371 nextToken();
4372
4373 // Consume the "default" in "export default class/function".
4374 if (FormatTok->is(tok::kw_default))
4375 nextToken();
4376
4377 // Consume "async function", "function" and "default function", so that these
4378 // get parsed as free-standing JS functions, i.e. do not require a trailing
4379 // semicolon.
4380 if (FormatTok->is(Keywords.kw_async))
4381 nextToken();
4382 if (FormatTok->is(Keywords.kw_function)) {
4383 nextToken();
4384 return;
4385 }
4386
4387 // For imports, `export *`, `export {...}`, consume the rest of the line up
4388 // to the terminating `;`. For everything else, just return and continue
4389 // parsing the structural element, i.e. the declaration or expression for
4390 // `export default`.
4391 if (!IsImport && FormatTok->isNoneOf(tok::l_brace, tok::star) &&
4392 !FormatTok->isStringLiteral() &&
4393 !(FormatTok->is(Keywords.kw_type) &&
4394 Tokens->peekNextToken()->isOneOf(tok::l_brace, tok::star))) {
4395 return;
4396 }
4397
4398 while (!eof()) {
4399 if (FormatTok->is(tok::semi))
4400 return;
4401 if (Line->Tokens.empty()) {
4402 // Common issue: Automatic Semicolon Insertion wrapped the line, so the
4403 // import statement should terminate.
4404 return;
4405 }
4406 if (FormatTok->is(tok::l_brace)) {
4407 FormatTok->setBlockKind(BK_Block);
4408 nextToken();
4409 parseBracedList();
4410 } else {
4411 nextToken();
4412 }
4413 }
4414}
4415
4416void UnwrappedLineParser::parseStatementMacro() {
4417 nextToken();
4418 if (FormatTok->is(tok::l_paren))
4419 parseParens();
4420 if (FormatTok->is(tok::semi))
4421 nextToken();
4422 addUnwrappedLine();
4423}
4424
4425void UnwrappedLineParser::parseVerilogHierarchyIdentifier() {
4426 // consume things like a::`b.c[d:e] or a::*
4427 while (true) {
4428 if (FormatTok->isOneOf(tok::star, tok::period, tok::periodstar,
4429 tok::coloncolon, tok::hash) ||
4430 Keywords.isVerilogIdentifier(*FormatTok)) {
4431 nextToken();
4432 } else if (FormatTok->is(tok::l_square)) {
4433 parseSquare();
4434 } else {
4435 break;
4436 }
4437 }
4438}
4439
4440void UnwrappedLineParser::parseVerilogSensitivityList() {
4441 if (FormatTok->isNot(tok::at))
4442 return;
4443 nextToken();
4444 // A block event expression has 2 at signs.
4445 if (FormatTok->is(tok::at))
4446 nextToken();
4447 switch (FormatTok->Tok.getKind()) {
4448 case tok::star:
4449 nextToken();
4450 break;
4451 case tok::l_paren:
4452 parseParens();
4453 break;
4454 default:
4455 parseVerilogHierarchyIdentifier();
4456 break;
4457 }
4458}
4459
4460unsigned UnwrappedLineParser::parseVerilogHierarchyHeader() {
4461 unsigned AddLevels = 0;
4462
4463 if (FormatTok->is(Keywords.kw_clocking)) {
4464 nextToken();
4465 if (Keywords.isVerilogIdentifier(*FormatTok))
4466 nextToken();
4467 parseVerilogSensitivityList();
4468 if (FormatTok->is(tok::semi))
4469 nextToken();
4470 } else if (FormatTok->isOneOf(tok::kw_case, Keywords.kw_casex,
4471 Keywords.kw_casez, Keywords.kw_randcase,
4472 Keywords.kw_randsequence)) {
4473 if (Style.IndentCaseLabels)
4474 AddLevels++;
4475 nextToken();
4476 if (FormatTok->is(tok::l_paren)) {
4477 FormatTok->setFinalizedType(TT_ConditionLParen);
4478 parseParens();
4479 }
4480 if (FormatTok->isOneOf(Keywords.kw_inside, Keywords.kw_matches))
4481 nextToken();
4482 // The case header has no semicolon.
4483 } else {
4484 // "module" etc.
4485 nextToken();
4486 // all the words like the name of the module and specifiers like
4487 // "automatic" and the width of function return type
4488 while (true) {
4489 if (FormatTok->is(tok::l_square)) {
4490 auto Prev = FormatTok->getPreviousNonComment();
4491 if (Prev && Keywords.isVerilogIdentifier(*Prev))
4492 Prev->setFinalizedType(TT_VerilogDimensionedTypeName);
4493 parseSquare();
4494 } else if (Keywords.isVerilogIdentifier(*FormatTok) ||
4495 FormatTok->isOneOf(tok::hash, tok::hashhash, tok::coloncolon,
4496 Keywords.kw_automatic, tok::kw_static)) {
4497 nextToken();
4498 } else {
4499 break;
4500 }
4501 }
4502
4503 auto NewLine = [this]() {
4504 addUnwrappedLine();
4505 Line->IsContinuation = true;
4506 };
4507
4508 // package imports
4509 while (FormatTok->is(Keywords.kw_import)) {
4510 NewLine();
4511 nextToken();
4512 parseVerilogHierarchyIdentifier();
4513 if (FormatTok->is(tok::semi))
4514 nextToken();
4515 }
4516
4517 // parameters and ports
4518 if (FormatTok->is(Keywords.kw_verilogHash)) {
4519 NewLine();
4520 nextToken();
4521 if (FormatTok->is(tok::l_paren)) {
4522 FormatTok->setFinalizedType(TT_VerilogMultiLineListLParen);
4523 parseParens();
4524 }
4525 }
4526 if (FormatTok->is(tok::l_paren)) {
4527 NewLine();
4528 FormatTok->setFinalizedType(TT_VerilogMultiLineListLParen);
4529 parseParens();
4530 }
4531
4532 // extends and implements
4533 if (FormatTok->is(Keywords.kw_extends)) {
4534 NewLine();
4535 nextToken();
4536 parseVerilogHierarchyIdentifier();
4537 if (FormatTok->is(tok::l_paren))
4538 parseParens();
4539 }
4540 if (FormatTok->is(Keywords.kw_implements)) {
4541 NewLine();
4542 do {
4543 nextToken();
4544 parseVerilogHierarchyIdentifier();
4545 } while (FormatTok->is(tok::comma));
4546 }
4547
4548 // Coverage event for cover groups.
4549 if (FormatTok->is(tok::at)) {
4550 NewLine();
4551 parseVerilogSensitivityList();
4552 }
4553
4554 if (FormatTok->is(tok::semi))
4555 nextToken(/*LevelDifference=*/1);
4556 addUnwrappedLine();
4557 }
4558
4559 return AddLevels;
4560}
4561
4562void UnwrappedLineParser::parseVerilogTable() {
4563 assert(FormatTok->is(Keywords.kw_table));
4564 nextToken(/*LevelDifference=*/1);
4565 addUnwrappedLine();
4566
4567 auto InitialLevel = Line->Level++;
4568 while (!eof() && !Keywords.isVerilogEnd(*FormatTok)) {
4569 FormatToken *Tok = FormatTok;
4570 nextToken();
4571 if (Tok->is(tok::semi))
4572 addUnwrappedLine();
4573 else if (Tok->isOneOf(tok::star, tok::colon, tok::question, tok::minus))
4574 Tok->setFinalizedType(TT_VerilogTableItem);
4575 }
4576 Line->Level = InitialLevel;
4577 nextToken(/*LevelDifference=*/-1);
4578 addUnwrappedLine();
4579}
4580
4581void UnwrappedLineParser::parseVerilogCaseLabel() {
4582 // The label will get unindented in AnnotatingParser. If there are no leading
4583 // spaces, indent the rest here so that things inside the block will be
4584 // indented relative to things outside. We don't use parseLabel because we
4585 // don't know whether this colon is a label or a ternary expression at this
4586 // point.
4587 auto OrigLevel = Line->Level;
4588 auto FirstLine = CurrentLines->size();
4589 if (Line->Level == 0 || (Line->InPPDirective && Line->Level <= 1))
4590 ++Line->Level;
4591 else if (!Style.IndentCaseBlocks && Keywords.isVerilogBegin(*FormatTok))
4592 --Line->Level;
4593 parseStructuralElement();
4594 // Restore the indentation in both the new line and the line that has the
4595 // label.
4596 if (CurrentLines->size() > FirstLine)
4597 (*CurrentLines)[FirstLine].Level = OrigLevel;
4598 Line->Level = OrigLevel;
4599}
4600
4601void UnwrappedLineParser::parseVerilogExtern() {
4602 assert(
4603 FormatTok->isOneOf(tok::kw_extern, tok::kw_export, Keywords.kw_import));
4604 nextToken();
4605 // "DPI-C"
4606 if (FormatTok->is(tok::string_literal))
4607 nextToken();
4608 if (FormatTok->isOneOf(Keywords.kw_context, Keywords.kw_pure))
4609 nextToken();
4610 if (Keywords.isVerilogIdentifier(*FormatTok))
4611 nextToken();
4612 if (FormatTok->is(tok::equal))
4613 nextToken();
4614 if (Keywords.isVerilogHierarchy(*FormatTok))
4615 parseVerilogHierarchyHeader();
4616}
4617
4618bool UnwrappedLineParser::containsExpansion(const UnwrappedLine &Line) const {
4619 for (const auto &N : Line.Tokens) {
4620 if (N.Tok->MacroCtx)
4621 return true;
4622 for (const UnwrappedLine &Child : N.Children)
4623 if (containsExpansion(Child))
4624 return true;
4625 }
4626 return false;
4627}
4628
4629void UnwrappedLineParser::addUnwrappedLine(LineLevel AdjustLevel) {
4630 if (Line->Tokens.empty())
4631 return;
4632 LLVM_DEBUG({
4633 if (!parsingPPDirective()) {
4634 llvm::dbgs() << "Adding unwrapped line:\n";
4635 printDebugInfo(*Line);
4636 }
4637 });
4638
4639 // If this line closes a block when in Whitesmiths mode, remember that
4640 // information so that the level can be decreased after the line is added.
4641 // This has to happen after the addition of the line since the line itself
4642 // needs to be indented.
4643 bool ClosesWhitesmithsBlock =
4644 Line->MatchingOpeningBlockLineIndex != UnwrappedLine::kInvalidIndex &&
4645 Style.BreakBeforeBraces == FormatStyle::BS_Whitesmiths;
4646
4647 // If the current line was expanded from a macro call, we use it to
4648 // reconstruct an unwrapped line from the structure of the expanded unwrapped
4649 // line and the unexpanded token stream.
4650 if (!parsingPPDirective() && !InExpansion && containsExpansion(*Line)) {
4651 if (!Reconstruct)
4652 Reconstruct.emplace(Line->Level, Unexpanded);
4653 Reconstruct->addLine(*Line);
4654
4655 // While the reconstructed unexpanded lines are stored in the normal
4656 // flow of lines, the expanded lines are stored on the side to be analyzed
4657 // in an extra step.
4658 CurrentExpandedLines.push_back(std::move(*Line));
4659
4660 if (Reconstruct->finished()) {
4661 UnwrappedLine Reconstructed = std::move(*Reconstruct).takeResult();
4662 assert(!Reconstructed.Tokens.empty() &&
4663 "Reconstructed must at least contain the macro identifier.");
4664 assert(!parsingPPDirective());
4665 LLVM_DEBUG({
4666 llvm::dbgs() << "Adding unexpanded line:\n";
4667 printDebugInfo(Reconstructed);
4668 });
4669 ExpandedLines[Reconstructed.Tokens.begin()->Tok] = CurrentExpandedLines;
4670 Lines.push_back(std::move(Reconstructed));
4671 CurrentExpandedLines.clear();
4672 Reconstruct.reset();
4673 }
4674 } else {
4675 // At the top level we only get here when no unexpansion is going on, or
4676 // when conditional formatting led to unfinished macro reconstructions.
4677 assert(!Reconstruct || (CurrentLines != &Lines) || !PPStack.empty());
4678 CurrentLines->push_back(std::move(*Line));
4679 }
4680 Line->Tokens.clear();
4681 Line->MatchingOpeningBlockLineIndex = UnwrappedLine::kInvalidIndex;
4682 Line->FirstStartColumn = 0;
4683 Line->IsContinuation = false;
4684 Line->SeenDecltypeAuto = false;
4685
4686 if (ClosesWhitesmithsBlock && AdjustLevel == LineLevel::Remove)
4687 --Line->Level;
4688 if (!parsingPPDirective() && !PreprocessorDirectives.empty()) {
4689 CurrentLines->append(
4690 std::make_move_iterator(PreprocessorDirectives.begin()),
4691 std::make_move_iterator(PreprocessorDirectives.end()));
4692 PreprocessorDirectives.clear();
4693 }
4694 // Disconnect the current token from the last token on the previous line.
4695 FormatTok->Previous = nullptr;
4696}
4697
4698bool UnwrappedLineParser::eof() const { return FormatTok->is(tok::eof); }
4699
4700bool UnwrappedLineParser::isOnNewLine(const FormatToken &FormatTok) {
4701 return (Line->InPPDirective || FormatTok.HasUnescapedNewline) &&
4702 FormatTok.NewlinesBefore > 0;
4703}
4704
4705// Checks if \p FormatTok is a line comment that continues the line comment
4706// section on \p Line.
4707static bool
4709 const UnwrappedLine &Line, const FormatStyle &Style,
4710 const llvm::Regex &CommentPragmasRegex) {
4711 if (Line.Tokens.empty() || Style.ReflowComments != FormatStyle::RCS_Always)
4712 return false;
4713
4714 StringRef IndentContent = FormatTok.TokenText;
4715 if (FormatTok.TokenText.starts_with("//") ||
4716 FormatTok.TokenText.starts_with("/*")) {
4717 IndentContent = FormatTok.TokenText.substr(2);
4718 }
4719 if (CommentPragmasRegex.match(IndentContent))
4720 return false;
4721
4722 // If Line starts with a line comment, then FormatTok continues the comment
4723 // section if its original column is greater or equal to the original start
4724 // column of the line.
4725 //
4726 // Define the min column token of a line as follows: if a line ends in '{' or
4727 // contains a '{' followed by a line comment, then the min column token is
4728 // that '{'. Otherwise, the min column token of the line is the first token of
4729 // the line.
4730 //
4731 // If Line starts with a token other than a line comment, then FormatTok
4732 // continues the comment section if its original column is greater than the
4733 // original start column of the min column token of the line.
4734 //
4735 // For example, the second line comment continues the first in these cases:
4736 //
4737 // // first line
4738 // // second line
4739 //
4740 // and:
4741 //
4742 // // first line
4743 // // second line
4744 //
4745 // and:
4746 //
4747 // int i; // first line
4748 // // second line
4749 //
4750 // and:
4751 //
4752 // do { // first line
4753 // // second line
4754 // int i;
4755 // } while (true);
4756 //
4757 // and:
4758 //
4759 // enum {
4760 // a, // first line
4761 // // second line
4762 // b
4763 // };
4764 //
4765 // The second line comment doesn't continue the first in these cases:
4766 //
4767 // // first line
4768 // // second line
4769 //
4770 // and:
4771 //
4772 // int i; // first line
4773 // // second line
4774 //
4775 // and:
4776 //
4777 // do { // first line
4778 // // second line
4779 // int i;
4780 // } while (true);
4781 //
4782 // and:
4783 //
4784 // enum {
4785 // a, // first line
4786 // // second line
4787 // };
4788 const FormatToken *MinColumnToken = Line.Tokens.front().Tok;
4789
4790 // Scan for '{//'. If found, use the column of '{' as a min column for line
4791 // comment section continuation.
4792 const FormatToken *PreviousToken = nullptr;
4793 for (const UnwrappedLineNode &Node : Line.Tokens) {
4794 if (PreviousToken && PreviousToken->is(tok::l_brace) &&
4795 isLineComment(*Node.Tok)) {
4796 MinColumnToken = PreviousToken;
4797 break;
4798 }
4799 PreviousToken = Node.Tok;
4800
4801 // Grab the last newline preceding a token in this unwrapped line.
4802 if (Node.Tok->NewlinesBefore > 0)
4803 MinColumnToken = Node.Tok;
4804 }
4805 if (PreviousToken && PreviousToken->is(tok::l_brace))
4806 MinColumnToken = PreviousToken;
4807
4808 return continuesLineComment(FormatTok, /*Previous=*/Line.Tokens.back().Tok,
4809 MinColumnToken);
4810}
4811
4812void UnwrappedLineParser::flushComments(bool NewlineBeforeNext) {
4813 bool JustComments = Line->Tokens.empty();
4814 for (FormatToken *Tok : CommentsBeforeNextToken) {
4815 // Line comments that belong to the same line comment section are put on the
4816 // same line since later we might want to reflow content between them.
4817 // Additional fine-grained breaking of line comment sections is controlled
4818 // by the class BreakableLineCommentSection in case it is desirable to keep
4819 // several line comment sections in the same unwrapped line.
4820 //
4821 // FIXME: Consider putting separate line comment sections as children to the
4822 // unwrapped line instead.
4823 Tok->ContinuesLineCommentSection =
4824 continuesLineCommentSection(*Tok, *Line, Style, CommentPragmasRegex);
4825 if (isOnNewLine(*Tok) && JustComments && !Tok->ContinuesLineCommentSection)
4826 addUnwrappedLine();
4827 pushToken(Tok);
4828 }
4829 if (NewlineBeforeNext && JustComments)
4830 addUnwrappedLine();
4831 CommentsBeforeNextToken.clear();
4832}
4833
4834void UnwrappedLineParser::nextToken(int LevelDifference) {
4835 if (eof())
4836 return;
4837 flushComments(isOnNewLine(*FormatTok));
4838 pushToken(FormatTok);
4839 FormatToken *Previous = FormatTok;
4840 if (!Style.isJavaScript())
4841 readToken(LevelDifference);
4842 else
4843 readTokenWithJavaScriptASI();
4844 FormatTok->Previous = Previous;
4845 if (Style.isVerilog()) {
4846 // Blocks in Verilog can have `begin` and `end` instead of braces. For
4847 // keywords like `begin`, we can't treat them the same as left braces
4848 // because some contexts require one of them. For example structs use
4849 // braces and if blocks use keywords, and a left brace can occur in an if
4850 // statement, but it is not a block. For keywords like `end`, we simply
4851 // treat them the same as right braces.
4852 if (Keywords.isVerilogEnd(*FormatTok))
4853 FormatTok->Tok.setKind(tok::r_brace);
4854 }
4855}
4856
4857void UnwrappedLineParser::distributeComments(
4858 const ArrayRef<FormatToken *> &Comments, const FormatToken *NextTok) {
4859 // Whether or not a line comment token continues a line is controlled by
4860 // the method continuesLineCommentSection, with the following caveat:
4861 //
4862 // Define a trail of Comments to be a nonempty proper postfix of Comments such
4863 // that each comment line from the trail is aligned with the next token, if
4864 // the next token exists. If a trail exists, the beginning of the maximal
4865 // trail is marked as a start of a new comment section.
4866 //
4867 // For example in this code:
4868 //
4869 // int a; // line about a
4870 // // line 1 about b
4871 // // line 2 about b
4872 // int b;
4873 //
4874 // the two lines about b form a maximal trail, so there are two sections, the
4875 // first one consisting of the single comment "// line about a" and the
4876 // second one consisting of the next two comments.
4877 if (Comments.empty())
4878 return;
4879 bool ShouldPushCommentsInCurrentLine = true;
4880 bool HasTrailAlignedWithNextToken = false;
4881 unsigned StartOfTrailAlignedWithNextToken = 0;
4882 if (NextTok) {
4883 // We are skipping the first element intentionally.
4884 for (unsigned i = Comments.size() - 1; i > 0; --i) {
4885 if (Comments[i]->OriginalColumn == NextTok->OriginalColumn) {
4886 HasTrailAlignedWithNextToken = true;
4887 StartOfTrailAlignedWithNextToken = i;
4888 }
4889 }
4890 }
4891 for (unsigned i = 0, e = Comments.size(); i < e; ++i) {
4892 FormatToken *FormatTok = Comments[i];
4893 if (HasTrailAlignedWithNextToken && i == StartOfTrailAlignedWithNextToken) {
4894 FormatTok->ContinuesLineCommentSection = false;
4895 } else {
4896 FormatTok->ContinuesLineCommentSection = continuesLineCommentSection(
4897 *FormatTok, *Line, Style, CommentPragmasRegex);
4898 }
4899 if (!FormatTok->ContinuesLineCommentSection &&
4900 (isOnNewLine(*FormatTok) || FormatTok->IsFirst)) {
4901 ShouldPushCommentsInCurrentLine = false;
4902 }
4903 if (ShouldPushCommentsInCurrentLine)
4904 pushToken(FormatTok);
4905 else
4906 CommentsBeforeNextToken.push_back(FormatTok);
4907 }
4908}
4909
4910void UnwrappedLineParser::readToken(int LevelDifference) {
4912 bool PreviousWasComment = false;
4913 bool FirstNonCommentOnLine = false;
4914 do {
4915 FormatTok = Tokens->getNextToken();
4916 assert(FormatTok);
4917 while (FormatTok->isOneOf(TT_ConflictStart, TT_ConflictEnd,
4918 TT_ConflictAlternative)) {
4919 if (FormatTok->is(TT_ConflictStart))
4920 conditionalCompilationStart(/*Unreachable=*/false);
4921 else if (FormatTok->is(TT_ConflictAlternative))
4922 conditionalCompilationAlternative();
4923 else if (FormatTok->is(TT_ConflictEnd))
4924 conditionalCompilationEnd();
4925 FormatTok = Tokens->getNextToken();
4926 FormatTok->MustBreakBefore = true;
4927 FormatTok->MustBreakBeforeFinalized = true;
4928 }
4929
4930 auto IsFirstNonCommentOnLine = [](bool FirstNonCommentOnLine,
4931 const FormatToken &Tok,
4932 bool PreviousWasComment) {
4933 auto IsFirstOnLine = [](const FormatToken &Tok) {
4934 return Tok.HasUnescapedNewline || Tok.IsFirst;
4935 };
4936
4937 // Consider preprocessor directives preceded by block comments as first
4938 // on line.
4939 if (PreviousWasComment)
4940 return FirstNonCommentOnLine || IsFirstOnLine(Tok);
4941 return IsFirstOnLine(Tok);
4942 };
4943
4944 FirstNonCommentOnLine = IsFirstNonCommentOnLine(
4945 FirstNonCommentOnLine, *FormatTok, PreviousWasComment);
4946 PreviousWasComment = FormatTok->is(tok::comment);
4947
4948 while (!Line->InPPDirective && FormatTok->is(tok::hash) &&
4949 FirstNonCommentOnLine) {
4950 // In Verilog, the backtick is used for macro invocations. In TableGen,
4951 // the single hash is used for the paste operator.
4952 const auto *Next = Tokens->peekNextToken();
4953 if ((Style.isVerilog() && !Keywords.isVerilogPPDirective(*Next)) ||
4954 (Style.isTableGen() &&
4955 Next->isNoneOf(tok::kw_else, tok::pp_define, tok::pp_ifdef,
4956 tok::pp_ifndef, tok::pp_endif))) {
4957 break;
4958 }
4959 distributeComments(Comments, FormatTok);
4960 Comments.clear();
4961 // If there is an unfinished unwrapped line, we flush the preprocessor
4962 // directives only after that unwrapped line was finished later.
4963 bool SwitchToPreprocessorLines = !Line->Tokens.empty();
4964 ScopedLineState BlockState(*this, SwitchToPreprocessorLines);
4965 assert((LevelDifference >= 0 ||
4966 static_cast<unsigned>(-LevelDifference) <= Line->Level) &&
4967 "LevelDifference makes Line->Level negative");
4968 Line->Level += LevelDifference;
4969 // Comments stored before the preprocessor directive need to be output
4970 // before the preprocessor directive, at the same level as the
4971 // preprocessor directive, as we consider them to apply to the directive.
4972 if (Style.IndentPPDirectives == FormatStyle::PPDIS_BeforeHash &&
4973 PPBranchLevel > 0) {
4974 Line->Level += PPBranchLevel;
4975 }
4976 assert(Line->Level >= Line->UnbracedBodyLevel);
4977 Line->Level -= Line->UnbracedBodyLevel;
4978 flushComments(isOnNewLine(*FormatTok));
4979 const bool IsEndIf = Tokens->peekNextToken()->is(tok::pp_endif);
4980 parsePPDirective();
4981 PreviousWasComment = FormatTok->is(tok::comment);
4982 FirstNonCommentOnLine = IsFirstNonCommentOnLine(
4983 FirstNonCommentOnLine, *FormatTok, PreviousWasComment);
4984 // If the #endif of a potential include guard is the last thing in the
4985 // file, then we found an include guard.
4986 if (IsEndIf && IncludeGuard == IG_Defined && PPBranchLevel == -1 &&
4987 getIncludeGuardState(Style.IndentPPDirectives) == IG_Inited &&
4988 (eof() ||
4989 (PreviousWasComment &&
4990 Tokens->peekNextToken(/*SkipComment=*/true)->is(tok::eof)))) {
4991 IncludeGuard = IG_Found;
4992 }
4993 }
4994
4995 if (!PPStack.empty() && (PPStack.back().Kind == PP_Unreachable) &&
4996 !Line->InPPDirective) {
4997 continue;
4998 }
4999
5000 if (FormatTok->is(tok::identifier) &&
5001 Macros.defined(FormatTok->TokenText) &&
5002 // FIXME: Allow expanding macros in preprocessor directives.
5003 !Line->InPPDirective) {
5004 FormatToken *ID = FormatTok;
5005 unsigned Position = Tokens->getPosition();
5006
5007 // To correctly parse the code, we need to replace the tokens of the macro
5008 // call with its expansion.
5009 auto PreCall = std::move(Line);
5010 Line.reset(new UnwrappedLine);
5011 bool OldInExpansion = InExpansion;
5012 InExpansion = true;
5013 // We parse the macro call into a new line.
5014 auto Args = parseMacroCall();
5015 InExpansion = OldInExpansion;
5016 assert(Line->Tokens.front().Tok == ID);
5017 // And remember the unexpanded macro call tokens.
5018 auto UnexpandedLine = std::move(Line);
5019 // Reset to the old line.
5020 Line = std::move(PreCall);
5021
5022 LLVM_DEBUG({
5023 llvm::dbgs() << "Macro call: " << ID->TokenText << "(";
5024 if (Args) {
5025 llvm::dbgs() << "(";
5026 for (const auto &Arg : Args.value())
5027 for (const auto &T : Arg)
5028 llvm::dbgs() << T->TokenText << " ";
5029 llvm::dbgs() << ")";
5030 }
5031 llvm::dbgs() << "\n";
5032 });
5033 if (Macros.objectLike(ID->TokenText) && Args &&
5034 !Macros.hasArity(ID->TokenText, Args->size())) {
5035 // The macro is either
5036 // - object-like, but we got argumnets, or
5037 // - overloaded to be both object-like and function-like, but none of
5038 // the function-like arities match the number of arguments.
5039 // Thus, expand as object-like macro.
5040 LLVM_DEBUG(llvm::dbgs()
5041 << "Macro \"" << ID->TokenText
5042 << "\" not overloaded for arity " << Args->size()
5043 << "or not function-like, using object-like overload.");
5044 Args.reset();
5045 UnexpandedLine->Tokens.resize(1);
5046 Tokens->setPosition(Position);
5047 nextToken();
5048 assert(!Args && Macros.objectLike(ID->TokenText));
5049 }
5050 if ((!Args && Macros.objectLike(ID->TokenText)) ||
5051 (Args && Macros.hasArity(ID->TokenText, Args->size()))) {
5052 // Next, we insert the expanded tokens in the token stream at the
5053 // current position, and continue parsing.
5054 Unexpanded[ID] = std::move(UnexpandedLine);
5056 Macros.expand(ID, std::move(Args));
5057 if (!Expansion.empty())
5058 FormatTok = Tokens->insertTokens(Expansion);
5059
5060 LLVM_DEBUG({
5061 llvm::dbgs() << "Expanded: ";
5062 for (const auto &T : Expansion)
5063 llvm::dbgs() << T->TokenText << " ";
5064 llvm::dbgs() << "\n";
5065 });
5066 } else {
5067 LLVM_DEBUG({
5068 llvm::dbgs() << "Did not expand macro \"" << ID->TokenText
5069 << "\", because it was used ";
5070 if (Args)
5071 llvm::dbgs() << "with " << Args->size();
5072 else
5073 llvm::dbgs() << "without";
5074 llvm::dbgs() << " arguments, which doesn't match any definition.\n";
5075 });
5076 Tokens->setPosition(Position);
5077 FormatTok = ID;
5078 }
5079 }
5080
5081 if (FormatTok->isNot(tok::comment)) {
5082 distributeComments(Comments, FormatTok);
5083 Comments.clear();
5084 return;
5085 }
5086
5087 Comments.push_back(FormatTok);
5088 } while (!eof());
5089
5090 distributeComments(Comments, nullptr);
5091 Comments.clear();
5092}
5093
5094namespace {
5095template <typename Iterator>
5096void pushTokens(Iterator Begin, Iterator End,
5098 for (auto I = Begin; I != End; ++I) {
5099 Into.push_back(I->Tok);
5100 for (const auto &Child : I->Children)
5101 pushTokens(Child.Tokens.begin(), Child.Tokens.end(), Into);
5102 }
5103}
5104} // namespace
5105
5106std::optional<llvm::SmallVector<llvm::SmallVector<FormatToken *, 8>, 1>>
5107UnwrappedLineParser::parseMacroCall() {
5108 std::optional<llvm::SmallVector<llvm::SmallVector<FormatToken *, 8>, 1>> Args;
5109 assert(Line->Tokens.empty());
5110 nextToken();
5111 if (FormatTok->isNot(tok::l_paren))
5112 return Args;
5113 unsigned Position = Tokens->getPosition();
5114 FormatToken *Tok = FormatTok;
5115 nextToken();
5116 Args.emplace();
5117 auto ArgStart = std::prev(Line->Tokens.end());
5118
5119 int Parens = 0;
5120 do {
5121 switch (FormatTok->Tok.getKind()) {
5122 case tok::l_paren:
5123 ++Parens;
5124 nextToken();
5125 break;
5126 case tok::r_paren: {
5127 if (Parens > 0) {
5128 --Parens;
5129 nextToken();
5130 break;
5131 }
5132 Args->push_back({});
5133 pushTokens(std::next(ArgStart), Line->Tokens.end(), Args->back());
5134 nextToken();
5135 return Args;
5136 }
5137 case tok::comma: {
5138 if (Parens > 0) {
5139 nextToken();
5140 break;
5141 }
5142 Args->push_back({});
5143 pushTokens(std::next(ArgStart), Line->Tokens.end(), Args->back());
5144 nextToken();
5145 ArgStart = std::prev(Line->Tokens.end());
5146 break;
5147 }
5148 default:
5149 nextToken();
5150 break;
5151 }
5152 } while (!eof());
5153 Line->Tokens.resize(1);
5154 Tokens->setPosition(Position);
5155 FormatTok = Tok;
5156 return {};
5157}
5158
5159void UnwrappedLineParser::pushToken(FormatToken *Tok) {
5160 Line->Tokens.push_back(UnwrappedLineNode(Tok));
5161 if (AtEndOfPPLine) {
5162 auto &Tok = *Line->Tokens.back().Tok;
5163 Tok.MustBreakBefore = true;
5164 Tok.MustBreakBeforeFinalized = true;
5165 Tok.FirstAfterPPLine = true;
5166 AtEndOfPPLine = false;
5167 }
5168}
5169
5170} // end namespace format
5171} // 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:214
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:4405
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:3910
nullptr
This class represents a compute construct, representing a 'Kind' of ‘parallel’, 'serial',...
bool isCpp() const
Definition Format.h:3802
@ 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:2249
#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:2874
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 ...