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 // Skip attributes.
1455 while (FormatTok->is(tok::l_paren) &&
1456 Tokens->peekNextToken()->is(tok::star)) {
1457 parseParens();
1458 }
1459 // Skip things that can exist before keywords like 'if' and 'case'.
1460 if (FormatTok->isOneOf(Keywords.kw_priority, Keywords.kw_unique,
1461 Keywords.kw_unique0)) {
1462 nextToken();
1463 }
1464
1465 if (Keywords.isVerilogStructuredProcedure(*FormatTok)) {
1466 parseForOrWhileLoop(/*HasParens=*/false);
1467 return;
1468 }
1469 if (FormatTok->isOneOf(Keywords.kw_foreach, Keywords.kw_repeat)) {
1470 parseForOrWhileLoop();
1471 return;
1472 }
1473 if (FormatTok->isOneOf(tok::kw_restrict, Keywords.kw_assert,
1474 Keywords.kw_assume, Keywords.kw_cover)) {
1475 parseIfThenElse(IfKind, /*KeepBraces=*/false, /*IsVerilogAssert=*/true);
1476 return;
1477 }
1478 }
1479
1480 // Tokens that only make sense at the beginning of a line.
1481 if (FormatTok->isAccessSpecifierKeyword()) {
1482 if (Style.isJava() || Style.isJavaScript() || Style.isCSharp())
1483 nextToken();
1484 else
1485 parseAccessSpecifier();
1486 return;
1487 }
1488 switch (FormatTok->Tok.getKind()) {
1489 case tok::kw_asm:
1490 nextToken();
1491 if (FormatTok->is(tok::l_brace)) {
1492 FormatTok->setFinalizedType(TT_InlineASMBrace);
1493 nextToken();
1494 while (FormatTok && !eof()) {
1495 if (FormatTok->is(tok::r_brace)) {
1496 FormatTok->setFinalizedType(TT_InlineASMBrace);
1497 nextToken();
1498 addUnwrappedLine();
1499 break;
1500 }
1501 FormatTok->Finalized = true;
1502 nextToken();
1503 }
1504 }
1505 break;
1506 case tok::kw_namespace:
1507 parseNamespace();
1508 return;
1509 case tok::kw_if: {
1510 if (Style.isJavaScript() && Line->MustBeDeclaration) {
1511 // field/method declaration.
1512 break;
1513 }
1514 FormatToken *Tok = parseIfThenElse(IfKind);
1515 if (IfLeftBrace)
1516 *IfLeftBrace = Tok;
1517 return;
1518 }
1519 case tok::kw_for:
1520 case tok::kw_while:
1521 if (Style.isJavaScript() && Line->MustBeDeclaration) {
1522 // field/method declaration.
1523 break;
1524 }
1525 parseForOrWhileLoop();
1526 return;
1527 case tok::kw_do:
1528 if (Style.isJavaScript() && Line->MustBeDeclaration) {
1529 // field/method declaration.
1530 break;
1531 }
1532 parseDoWhile();
1533 if (HasDoWhile)
1534 *HasDoWhile = true;
1535 return;
1536 case tok::kw_switch:
1537 if (Style.isJavaScript() && Line->MustBeDeclaration) {
1538 // 'switch: string' field declaration.
1539 break;
1540 }
1541 parseSwitch(/*IsExpr=*/false);
1542 return;
1543 case tok::kw_default: {
1544 // In Verilog default along with other labels are handled in the next loop.
1545 if (Style.isVerilog())
1546 break;
1547 if (Style.isJavaScript() && Line->MustBeDeclaration) {
1548 // 'default: string' field declaration.
1549 break;
1550 }
1551 auto *Default = FormatTok;
1552 nextToken();
1553 if (FormatTok->is(tok::colon)) {
1554 FormatTok->setFinalizedType(TT_CaseLabelColon);
1555 parseLabel();
1556 return;
1557 }
1558 if (FormatTok->is(tok::arrow)) {
1559 FormatTok->setFinalizedType(TT_CaseLabelArrow);
1560 Default->setFinalizedType(TT_SwitchExpressionLabel);
1561 parseLabel();
1562 return;
1563 }
1564 // e.g. "default void f() {}" in a Java interface.
1565 break;
1566 }
1567 case tok::kw_case:
1568 // Proto: there are no switch/case statements.
1569 if (Style.Language == FormatStyle::LK_Proto) {
1570 nextToken();
1571 return;
1572 }
1573 if (Style.isVerilog()) {
1574 parseBlock();
1575 addUnwrappedLine();
1576 return;
1577 }
1578 if (Style.isJavaScript() && Line->MustBeDeclaration) {
1579 // 'case: string' field declaration.
1580 nextToken();
1581 break;
1582 }
1583 parseCaseLabel();
1584 return;
1585 case tok::kw_goto:
1586 nextToken();
1587 if (FormatTok->is(tok::kw_case))
1588 nextToken();
1589 break;
1590 case tok::kw_try:
1591 case tok::kw___try:
1592 if (Style.isJavaScript() && Line->MustBeDeclaration) {
1593 // field/method declaration.
1594 break;
1595 }
1596 parseTryCatch();
1597 return;
1598 case tok::kw_extern:
1599 if (Style.isVerilog()) {
1600 // In Verilog an extern module declaration looks like a start of module.
1601 // But there is no body and endmodule. So we handle it separately.
1602 parseVerilogExtern();
1603 return;
1604 }
1605 nextToken();
1606 if (FormatTok->is(tok::string_literal)) {
1607 nextToken();
1608 if (FormatTok->is(tok::l_brace)) {
1609 if (Style.BraceWrapping.AfterExternBlock)
1610 addUnwrappedLine();
1611 // Either we indent or for backwards compatibility we follow the
1612 // AfterExternBlock style.
1613 unsigned AddLevels =
1614 (Style.IndentExternBlock == FormatStyle::IEBS_Indent) ||
1615 (Style.BraceWrapping.AfterExternBlock &&
1616 Style.IndentExternBlock ==
1617 FormatStyle::IEBS_AfterExternBlock)
1618 ? 1u
1619 : 0u;
1620 parseBlock(/*MustBeDeclaration=*/true, AddLevels);
1621 addUnwrappedLine();
1622 return;
1623 }
1624 }
1625 break;
1626 case tok::kw_export:
1627 if (Style.isJavaScript()) {
1628 parseJavaScriptEs6ImportExport();
1629 return;
1630 }
1631 if (Style.isVerilog()) {
1632 parseVerilogExtern();
1633 return;
1634 }
1635 if (IsCpp) {
1636 nextToken();
1637 if (FormatTok->is(tok::kw_namespace)) {
1638 parseNamespace();
1639 return;
1640 }
1641 if (FormatTok->is(tok::l_brace)) {
1642 parseCppExportBlock();
1643 return;
1644 }
1645 if (FormatTok->is(Keywords.kw_import) && parseModuleImport())
1646 return;
1647 }
1648 break;
1649 case tok::kw_inline:
1650 nextToken();
1651 if (FormatTok->is(tok::kw_namespace)) {
1652 parseNamespace();
1653 return;
1654 }
1655 break;
1656 case tok::identifier:
1657 if (FormatTok->is(TT_ForEachMacro)) {
1658 parseForOrWhileLoop();
1659 return;
1660 }
1661 if (FormatTok->is(TT_MacroBlockBegin)) {
1662 parseBlock(/*MustBeDeclaration=*/false, /*AddLevels=*/1u,
1663 /*MunchSemi=*/false);
1664 return;
1665 }
1666 if (FormatTok->is(Keywords.kw_import)) {
1667 if (Style.isJavaScript()) {
1668 parseJavaScriptEs6ImportExport();
1669 return;
1670 }
1671 if (Style.Language == FormatStyle::LK_Proto) {
1672 nextToken();
1673 if (FormatTok->is(tok::kw_public))
1674 nextToken();
1675 if (FormatTok->isNot(tok::string_literal))
1676 return;
1677 nextToken();
1678 if (FormatTok->is(tok::semi))
1679 nextToken();
1680 addUnwrappedLine();
1681 return;
1682 }
1683 if (Style.isVerilog()) {
1684 parseVerilogExtern();
1685 return;
1686 }
1687 if (IsCpp && parseModuleImport())
1688 return;
1689 }
1690 if (IsCpp && FormatTok->isOneOf(Keywords.kw_signals, Keywords.kw_qsignals,
1691 Keywords.kw_slots, Keywords.kw_qslots)) {
1692 nextToken();
1693 if (FormatTok->is(tok::colon)) {
1694 nextToken();
1695 addUnwrappedLine();
1696 return;
1697 }
1698 }
1699 if (IsCpp && FormatTok->is(TT_StatementMacro)) {
1700 parseStatementMacro();
1701 return;
1702 }
1703 if (IsCpp && FormatTok->is(TT_NamespaceMacro)) {
1704 parseNamespace();
1705 return;
1706 }
1707 // In Verilog labels can be any expression, so we don't do them here.
1708 // JS doesn't have macros, and within classes colons indicate fields, not
1709 // labels.
1710 // TableGen doesn't have labels.
1711 if (!Style.isJavaScript() && !Style.isVerilog() && !Style.isTableGen() &&
1712 Tokens->peekNextToken()->is(tok::colon) && !Line->MustBeDeclaration) {
1713 nextToken();
1714 if (!Line->InMacroBody || CurrentLines->size() > 1)
1715 Line->Tokens.begin()->Tok->MustBreakBefore = true;
1716 FormatTok->setFinalizedType(TT_GotoLabelColon);
1717 parseLabel(Style.IndentGotoLabels);
1718 if (HasLabel)
1719 *HasLabel = true;
1720 return;
1721 }
1722 if (Style.isJava() && FormatTok->is(Keywords.kw_record)) {
1723 parseRecord(/*ParseAsExpr=*/false, /*IsJavaRecord=*/true);
1724 addUnwrappedLine();
1725 return;
1726 }
1727 // In all other cases, parse the declaration.
1728 break;
1729 default:
1730 break;
1731 }
1732
1733 bool SeenEqual = false;
1734 for (const bool InRequiresExpression =
1735 OpeningBrace && OpeningBrace->isOneOf(TT_RequiresExpressionLBrace,
1736 TT_CompoundRequirementLBrace);
1737 !eof();) {
1738 const FormatToken *Previous = FormatTok->Previous;
1739 switch (FormatTok->Tok.getKind()) {
1740 case tok::at:
1741 nextToken();
1742 if (FormatTok->is(tok::l_brace)) {
1743 nextToken();
1744 parseBracedList();
1745 break;
1746 }
1747 if (Style.isJava() && FormatTok->is(Keywords.kw_interface)) {
1748 nextToken();
1749 break;
1750 }
1751 switch (bool IsAutoRelease = false; FormatTok->Tok.getObjCKeywordID()) {
1752 case tok::objc_public:
1753 case tok::objc_protected:
1754 case tok::objc_package:
1755 case tok::objc_private:
1756 return parseAccessSpecifier();
1757 case tok::objc_interface:
1758 case tok::objc_implementation:
1759 return parseObjCInterfaceOrImplementation();
1760 case tok::objc_protocol:
1761 if (parseObjCProtocol())
1762 return;
1763 break;
1764 case tok::objc_end:
1765 return; // Handled by the caller.
1766 case tok::objc_optional:
1767 case tok::objc_required:
1768 nextToken();
1769 addUnwrappedLine();
1770 return;
1771 case tok::objc_autoreleasepool:
1772 IsAutoRelease = true;
1773 [[fallthrough]];
1774 case tok::objc_synchronized:
1775 nextToken();
1776 if (!IsAutoRelease && FormatTok->is(tok::l_paren)) {
1777 // Skip synchronization object
1778 parseParens();
1779 }
1780 if (FormatTok->is(tok::l_brace)) {
1781 if (Style.BraceWrapping.AfterControlStatement ==
1782 FormatStyle::BWACS_Always) {
1783 addUnwrappedLine();
1784 }
1785 parseBlock();
1786 }
1787 addUnwrappedLine();
1788 return;
1789 case tok::objc_try:
1790 // This branch isn't strictly necessary (the kw_try case below would
1791 // do this too after the tok::at is parsed above). But be explicit.
1792 parseTryCatch();
1793 return;
1794 default:
1795 break;
1796 }
1797 break;
1798 case tok::kw_requires: {
1799 if (IsCpp) {
1800 bool ParsedClause = parseRequires(SeenEqual);
1801 if (ParsedClause)
1802 return;
1803 } else {
1804 nextToken();
1805 }
1806 break;
1807 }
1808 case tok::kw_enum:
1809 // Ignore if this is part of "template <enum ..." or "... -> enum" or
1810 // "template <..., enum ...>".
1811 if (Previous && Previous->isOneOf(tok::less, tok::arrow, tok::comma)) {
1812 nextToken();
1813 break;
1814 }
1815
1816 // parseEnum falls through and does not yet add an unwrapped line as an
1817 // enum definition can start a structural element.
1818 if (!parseEnum())
1819 break;
1820 // This only applies to C++ and Verilog.
1821 if (!IsCpp && !Style.isVerilog()) {
1822 addUnwrappedLine();
1823 return;
1824 }
1825 break;
1826 case tok::kw_typedef:
1827 nextToken();
1828 if (FormatTok->isOneOf(Keywords.kw_NS_ENUM, Keywords.kw_NS_OPTIONS,
1829 Keywords.kw_CF_ENUM, Keywords.kw_CF_OPTIONS,
1830 Keywords.kw_CF_CLOSED_ENUM,
1831 Keywords.kw_NS_CLOSED_ENUM)) {
1832 parseEnum();
1833 }
1834 break;
1835 case tok::kw_class:
1836 if (Style.isVerilog()) {
1837 parseBlock();
1838 addUnwrappedLine();
1839 return;
1840 }
1841 if (Style.isTableGen()) {
1842 // Do nothing special. In this case the l_brace becomes FunctionLBrace.
1843 // This is same as def and so on.
1844 nextToken();
1845 break;
1846 }
1847 [[fallthrough]];
1848 case tok::kw_struct:
1849 case tok::kw_union:
1850 if (parseStructLike())
1851 return;
1852 break;
1853 case tok::kw_decltype:
1854 nextToken();
1855 if (FormatTok->is(tok::l_paren)) {
1856 parseParens();
1857 if (FormatTok->Previous &&
1858 FormatTok->Previous->endsSequence(tok::r_paren, tok::kw_auto,
1859 tok::l_paren)) {
1860 Line->SeenDecltypeAuto = true;
1861 }
1862 }
1863 break;
1864 case tok::period:
1865 nextToken();
1866 // In Java, classes have an implicit static member "class".
1867 if (Style.isJava() && FormatTok && FormatTok->is(tok::kw_class))
1868 nextToken();
1869 if (Style.isJavaScript() && FormatTok &&
1870 FormatTok->Tok.getIdentifierInfo()) {
1871 // JavaScript only has pseudo keywords, all keywords are allowed to
1872 // appear in "IdentifierName" positions. See http://es5.github.io/#x7.6
1873 nextToken();
1874 }
1875 break;
1876 case tok::semi:
1877 nextToken();
1878 addUnwrappedLine();
1879 return;
1880 case tok::r_brace:
1881 addUnwrappedLine();
1882 return;
1883 case tok::string_literal:
1884 if (Style.isVerilog() && FormatTok->is(TT_VerilogProtected)) {
1885 FormatTok->Finalized = true;
1886 nextToken();
1887 addUnwrappedLine();
1888 return;
1889 }
1890 nextToken();
1891 break;
1892 case tok::l_paren: {
1893 parseParens();
1894 // Break the unwrapped line if a K&R C function definition has a parameter
1895 // declaration.
1896 if (OpeningBrace || !IsCpp || !Previous || eof())
1897 break;
1898 if (isC78ParameterDecl(FormatTok,
1899 Tokens->peekNextToken(/*SkipComment=*/true),
1900 Previous)) {
1901 addUnwrappedLine();
1902 return;
1903 }
1904 break;
1905 }
1906 case tok::kw_operator:
1907 nextToken();
1908 if (FormatTok->isBinaryOperator())
1909 nextToken();
1910 break;
1911 case tok::caret: {
1912 const auto *Prev = FormatTok->getPreviousNonComment();
1913 nextToken();
1914 if (Prev && Prev->is(tok::identifier))
1915 break;
1916 // Block return type.
1917 if (FormatTok->Tok.isAnyIdentifier() || FormatTok->isTypeName(LangOpts)) {
1918 nextToken();
1919 // Return types: pointers are ok too.
1920 while (FormatTok->is(tok::star))
1921 nextToken();
1922 }
1923 // Block argument list.
1924 if (FormatTok->is(tok::l_paren))
1925 parseParens();
1926 // Block body.
1927 if (FormatTok->is(tok::l_brace))
1928 parseChildBlock();
1929 break;
1930 }
1931 case tok::l_brace:
1932 if (InRequiresExpression)
1933 FormatTok->setFinalizedType(TT_BracedListLBrace);
1934 if (!tryToParsePropertyAccessor() && !tryToParseBracedList()) {
1935 IsDecltypeAutoFunction = Line->SeenDecltypeAuto;
1936 // A block outside of parentheses must be the last part of a
1937 // structural element.
1938 // FIXME: Figure out cases where this is not true, and add projections
1939 // for them (the one we know is missing are lambdas).
1940 if (Style.isJava() &&
1941 Line->Tokens.front().Tok->is(Keywords.kw_synchronized)) {
1942 // If necessary, we could set the type to something different than
1943 // TT_FunctionLBrace.
1944 if (Style.BraceWrapping.AfterControlStatement ==
1945 FormatStyle::BWACS_Always) {
1946 addUnwrappedLine();
1947 }
1948 } else if (Style.BraceWrapping.AfterFunction) {
1949 addUnwrappedLine();
1950 }
1951 if (!Previous || Previous->isNot(TT_TypeDeclarationParen))
1952 FormatTok->setFinalizedType(TT_FunctionLBrace);
1953 parseBlock();
1954 IsDecltypeAutoFunction = false;
1955 addUnwrappedLine();
1956 return;
1957 }
1958 // Otherwise this was a braced init list, and the structural
1959 // element continues.
1960 break;
1961 case tok::kw_try:
1962 if (Style.isJavaScript() && Line->MustBeDeclaration) {
1963 // field/method declaration.
1964 nextToken();
1965 break;
1966 }
1967 // We arrive here when parsing function-try blocks.
1968 if (Style.BraceWrapping.AfterFunction)
1969 addUnwrappedLine();
1970 parseTryCatch();
1971 return;
1972 case tok::identifier: {
1973 if (Style.isCSharp() && FormatTok->is(Keywords.kw_where) &&
1974 Line->MustBeDeclaration) {
1975 addUnwrappedLine();
1976 parseCSharpGenericTypeConstraint();
1977 break;
1978 }
1979 if (FormatTok->is(TT_MacroBlockEnd)) {
1980 addUnwrappedLine();
1981 return;
1982 }
1983
1984 // Function declarations (as opposed to function expressions) are parsed
1985 // on their own unwrapped line by continuing this loop. Function
1986 // expressions (functions that are not on their own line) must not create
1987 // a new unwrapped line, so they are special cased below.
1988 size_t TokenCount = Line->Tokens.size();
1989 if (Style.isJavaScript() && FormatTok->is(Keywords.kw_function) &&
1990 (TokenCount > 1 ||
1991 (TokenCount == 1 &&
1992 Line->Tokens.front().Tok->isNot(Keywords.kw_async)))) {
1993 tryToParseJSFunction();
1994 break;
1995 }
1996 if ((Style.isJavaScript() || Style.isJava()) &&
1997 FormatTok->is(Keywords.kw_interface)) {
1998 if (Style.isJavaScript()) {
1999 // In JavaScript/TypeScript, "interface" can be used as a standalone
2000 // identifier, e.g. in `var interface = 1;`. If "interface" is
2001 // followed by another identifier, it is very like to be an actual
2002 // interface declaration.
2003 unsigned StoredPosition = Tokens->getPosition();
2004 FormatToken *Next = Tokens->getNextToken();
2005 FormatTok = Tokens->setPosition(StoredPosition);
2006 if (!mustBeJSIdent(Keywords, Next)) {
2007 nextToken();
2008 break;
2009 }
2010 }
2011 parseRecord();
2012 addUnwrappedLine();
2013 return;
2014 }
2015
2016 if (Style.isVerilog()) {
2017 if (FormatTok->is(Keywords.kw_table)) {
2018 parseVerilogTable();
2019 return;
2020 }
2021 if (Keywords.isVerilogBegin(*FormatTok) ||
2022 Keywords.isVerilogHierarchy(*FormatTok)) {
2023 parseBlock();
2024 addUnwrappedLine();
2025 return;
2026 }
2027 }
2028
2029 if (!IsCpp && FormatTok->is(Keywords.kw_interface)) {
2030 if (parseStructLike())
2031 return;
2032 break;
2033 }
2034
2035 if (IsCpp && FormatTok->is(TT_StatementMacro)) {
2036 parseStatementMacro();
2037 return;
2038 }
2039
2040 // See if the following token should start a new unwrapped line.
2041 StringRef Text = FormatTok->TokenText;
2042
2043 FormatToken *PreviousToken = FormatTok;
2044 nextToken();
2045
2046 // JS doesn't have macros, and within classes colons indicate fields, not
2047 // labels.
2048 if (Style.isJavaScript())
2049 break;
2050
2051 auto OneTokenSoFar = [&]() {
2052 auto I = Line->Tokens.begin(), E = Line->Tokens.end();
2053 while (I != E && I->Tok->is(tok::comment))
2054 ++I;
2055 if (Style.isVerilog())
2056 while (I != E && I->Tok->is(tok::hash))
2057 ++I;
2058 return I != E && (++I == E);
2059 };
2060 if (OneTokenSoFar()) {
2061 // Recognize function-like macro usages without trailing semicolon as
2062 // well as free-standing macros like Q_OBJECT.
2063 bool FunctionLike = FormatTok->is(tok::l_paren);
2064 if (FunctionLike)
2065 parseParens();
2066
2067 bool FollowedByNewline =
2068 CommentsBeforeNextToken.empty()
2069 ? FormatTok->NewlinesBefore > 0
2070 : CommentsBeforeNextToken.front()->NewlinesBefore > 0;
2071
2072 if (FollowedByNewline &&
2073 (Text.size() >= 5 ||
2074 (FunctionLike && FormatTok->isNot(tok::l_paren))) &&
2075 tokenCanStartNewLine(*FormatTok) && Text == Text.upper()) {
2076 if (PreviousToken->isNot(TT_UntouchableMacroFunc))
2077 PreviousToken->setFinalizedType(TT_FunctionLikeOrFreestandingMacro);
2078 addUnwrappedLine();
2079 return;
2080 }
2081 }
2082 break;
2083 }
2084 case tok::equal:
2085 if ((Style.isJavaScript() || Style.isCSharp()) &&
2086 FormatTok->is(TT_FatArrow)) {
2087 tryToParseChildBlock();
2088 break;
2089 }
2090
2091 SeenEqual = true;
2092 nextToken();
2093 if (FormatTok->is(tok::l_brace)) {
2094 // C# needs this change to ensure that array initialisers and object
2095 // initialisers are indented the same way. In TypeScript, the brace
2096 // can also be an object type definition.
2097 if (!Style.isJavaScript())
2098 FormatTok->setBlockKind(BK_BracedInit);
2099 // TableGen's defset statement has syntax of the form,
2100 // `defset <type> <name> = { <statement>... }`
2101 if (Style.isTableGen() &&
2102 Line->Tokens.begin()->Tok->is(Keywords.kw_defset)) {
2103 FormatTok->setFinalizedType(TT_FunctionLBrace);
2104 parseBlock(/*MustBeDeclaration=*/false, /*AddLevels=*/1u,
2105 /*MunchSemi=*/false);
2106 addUnwrappedLine();
2107 break;
2108 }
2109 nextToken();
2110 parseBracedList();
2111 } else if (Style.Language == FormatStyle::LK_Proto &&
2112 FormatTok->is(tok::less)) {
2113 nextToken();
2114 parseBracedList(/*IsAngleBracket=*/true);
2115 }
2116 break;
2117 case tok::l_square:
2118 parseSquare();
2119 break;
2120 case tok::kw_new:
2121 if (Style.isCSharp() &&
2122 (Tokens->peekNextToken()->isAccessSpecifierKeyword() ||
2123 (Previous && Previous->isAccessSpecifierKeyword()))) {
2124 nextToken();
2125 } else {
2126 parseNew();
2127 }
2128 break;
2129 case tok::kw_switch:
2130 if (Style.isJava())
2131 parseSwitch(/*IsExpr=*/true);
2132 else
2133 nextToken();
2134 break;
2135 case tok::kw_case:
2136 // Proto: there are no switch/case statements.
2137 if (Style.Language == FormatStyle::LK_Proto) {
2138 nextToken();
2139 return;
2140 }
2141 // In Verilog switch is called case.
2142 if (Style.isVerilog()) {
2143 parseBlock();
2144 addUnwrappedLine();
2145 return;
2146 }
2147 if (Style.isJavaScript() && Line->MustBeDeclaration) {
2148 // 'case: string' field declaration.
2149 nextToken();
2150 break;
2151 }
2152 parseCaseLabel();
2153 break;
2154 case tok::kw_default:
2155 nextToken();
2156 if (Style.isVerilog()) {
2157 if (FormatTok->is(tok::colon)) {
2158 // The label will be handled in the next iteration.
2159 break;
2160 }
2161 if (FormatTok->is(Keywords.kw_clocking)) {
2162 // A default clocking block.
2163 parseBlock();
2164 addUnwrappedLine();
2165 return;
2166 }
2167 parseVerilogCaseLabel();
2168 return;
2169 }
2170 break;
2171 case tok::colon:
2172 nextToken();
2173 if (Style.isVerilog()) {
2174 parseVerilogCaseLabel();
2175 return;
2176 }
2177 break;
2178 case tok::greater:
2179 nextToken();
2180 if (FormatTok->is(tok::l_brace))
2181 FormatTok->Previous->setFinalizedType(TT_TemplateCloser);
2182 break;
2183 default:
2184 nextToken();
2185 break;
2186 }
2187 }
2188}
2189
2190bool UnwrappedLineParser::tryToParsePropertyAccessor() {
2191 assert(FormatTok->is(tok::l_brace));
2192 if (!Style.isCSharp())
2193 return false;
2194 // See if it's a property accessor.
2195 if (!FormatTok->Previous || FormatTok->Previous->isNot(tok::identifier))
2196 return false;
2197
2198 // See if we are inside a property accessor.
2199 //
2200 // Record the current tokenPosition so that we can advance and
2201 // reset the current token. `Next` is not set yet so we need
2202 // another way to advance along the token stream.
2203 unsigned int StoredPosition = Tokens->getPosition();
2204 FormatToken *Tok = Tokens->getNextToken();
2205
2206 // A trivial property accessor is of the form:
2207 // { [ACCESS_SPECIFIER] [get]; [ACCESS_SPECIFIER] [set|init] }
2208 // Track these as they do not require line breaks to be introduced.
2209 bool HasSpecialAccessor = false;
2210 bool IsTrivialPropertyAccessor = true;
2211 bool HasAttribute = false;
2212 while (!eof()) {
2213 if (const bool IsAccessorKeyword =
2214 Tok->isOneOf(Keywords.kw_get, Keywords.kw_init, Keywords.kw_set);
2215 IsAccessorKeyword || Tok->isAccessSpecifierKeyword() ||
2216 Tok->isOneOf(tok::l_square, tok::semi, Keywords.kw_internal)) {
2217 if (IsAccessorKeyword)
2218 HasSpecialAccessor = true;
2219 else if (Tok->is(tok::l_square))
2220 HasAttribute = true;
2221 Tok = Tokens->getNextToken();
2222 continue;
2223 }
2224 if (Tok->isNot(tok::r_brace))
2225 IsTrivialPropertyAccessor = false;
2226 break;
2227 }
2228
2229 if (!HasSpecialAccessor || HasAttribute) {
2230 Tokens->setPosition(StoredPosition);
2231 return false;
2232 }
2233
2234 // Try to parse the property accessor:
2235 // https://docs.microsoft.com/en-us/dotnet/csharp/programming-guide/classes-and-structs/properties
2236 Tokens->setPosition(StoredPosition);
2237 if (!IsTrivialPropertyAccessor && Style.BraceWrapping.AfterFunction)
2238 addUnwrappedLine();
2239 nextToken();
2240 do {
2241 switch (FormatTok->Tok.getKind()) {
2242 case tok::r_brace:
2243 nextToken();
2244 if (FormatTok->is(tok::equal)) {
2245 while (!eof() && FormatTok->isNot(tok::semi))
2246 nextToken();
2247 nextToken();
2248 }
2249 addUnwrappedLine();
2250 return true;
2251 case tok::l_brace:
2252 ++Line->Level;
2253 parseBlock(/*MustBeDeclaration=*/true);
2254 addUnwrappedLine();
2255 --Line->Level;
2256 break;
2257 case tok::equal:
2258 if (FormatTok->is(TT_FatArrow)) {
2259 ++Line->Level;
2260 do {
2261 nextToken();
2262 } while (!eof() && FormatTok->isNot(tok::semi));
2263 nextToken();
2264 addUnwrappedLine();
2265 --Line->Level;
2266 break;
2267 }
2268 nextToken();
2269 break;
2270 default:
2271 if (FormatTok->isOneOf(Keywords.kw_get, Keywords.kw_init,
2272 Keywords.kw_set) &&
2273 !IsTrivialPropertyAccessor) {
2274 // Non-trivial get/set needs to be on its own line.
2275 addUnwrappedLine();
2276 }
2277 nextToken();
2278 }
2279 } while (!eof());
2280
2281 // Unreachable for well-formed code (paired '{' and '}').
2282 return true;
2283}
2284
2285bool UnwrappedLineParser::tryToParseLambda() {
2286 assert(FormatTok->is(tok::l_square));
2287 if (!IsCpp) {
2288 nextToken();
2289 return false;
2290 }
2291 FormatToken &LSquare = *FormatTok;
2292 if (!tryToParseLambdaIntroducer())
2293 return false;
2294
2295 FormatToken *Arrow = nullptr;
2296 bool InTemplateParameterList = false;
2297
2298 while (FormatTok->isNot(tok::l_brace)) {
2299 if (FormatTok->isTypeName(LangOpts) || FormatTok->isAttribute()) {
2300 nextToken();
2301 continue;
2302 }
2303 switch (FormatTok->Tok.getKind()) {
2304 case tok::l_brace:
2305 break;
2306 case tok::l_paren:
2307 parseParens(/*AmpAmpTokenType=*/TT_PointerOrReference);
2308 break;
2309 case tok::l_square:
2310 parseSquare();
2311 break;
2312 case tok::less:
2313 assert(FormatTok->Previous);
2314 if (FormatTok->Previous->is(tok::r_square))
2315 InTemplateParameterList = true;
2316 nextToken();
2317 break;
2318 case tok::kw_auto:
2319 case tok::kw_class:
2320 case tok::kw_struct:
2321 case tok::kw_union:
2322 case tok::kw_template:
2323 case tok::kw_typename:
2324 case tok::amp:
2325 case tok::star:
2326 case tok::kw_const:
2327 case tok::kw_constexpr:
2328 case tok::kw_consteval:
2329 case tok::comma:
2330 case tok::greater:
2331 case tok::identifier:
2332 case tok::numeric_constant:
2333 case tok::coloncolon:
2334 case tok::kw_mutable:
2335 case tok::kw_noexcept:
2336 case tok::kw_static:
2337 nextToken();
2338 break;
2339 // Specialization of a template with an integer parameter can contain
2340 // arithmetic, logical, comparison and ternary operators.
2341 //
2342 // FIXME: This also accepts sequences of operators that are not in the scope
2343 // of a template argument list.
2344 //
2345 // In a C++ lambda a template type can only occur after an arrow. We use
2346 // this as an heuristic to distinguish between Objective-C expressions
2347 // followed by an `a->b` expression, such as:
2348 // ([obj func:arg] + a->b)
2349 // Otherwise the code below would parse as a lambda.
2350 case tok::plus:
2351 case tok::minus:
2352 case tok::exclaim:
2353 case tok::tilde:
2354 case tok::slash:
2355 case tok::percent:
2356 case tok::lessless:
2357 case tok::pipe:
2358 case tok::pipepipe:
2359 case tok::ampamp:
2360 case tok::caret:
2361 case tok::equalequal:
2362 case tok::exclaimequal:
2363 case tok::greaterequal:
2364 case tok::lessequal:
2365 case tok::question:
2366 case tok::colon:
2367 case tok::ellipsis:
2368 case tok::kw_true:
2369 case tok::kw_false:
2370 if (Arrow || InTemplateParameterList) {
2371 nextToken();
2372 break;
2373 }
2374 return true;
2375 case tok::arrow:
2376 Arrow = FormatTok;
2377 nextToken();
2378 break;
2379 case tok::kw_requires:
2380 parseRequiresClause();
2381 break;
2382 case tok::equal:
2383 if (!InTemplateParameterList)
2384 return true;
2385 nextToken();
2386 break;
2387 default:
2388 return true;
2389 }
2390 }
2391
2392 FormatTok->setFinalizedType(TT_LambdaLBrace);
2393 LSquare.setFinalizedType(TT_LambdaLSquare);
2394
2395 if (Arrow)
2396 Arrow->setFinalizedType(TT_LambdaArrow);
2397
2398 NestedLambdas.push_back(Line->SeenDecltypeAuto);
2399 parseChildBlock();
2400 assert(!NestedLambdas.empty());
2401 NestedLambdas.pop_back();
2402
2403 return true;
2404}
2405
2406bool UnwrappedLineParser::tryToParseLambdaIntroducer() {
2407 const FormatToken *Previous = FormatTok->Previous;
2408 const FormatToken *LeftSquare = FormatTok;
2409 nextToken();
2410 if (Previous) {
2411 const auto *PrevPrev = Previous->getPreviousNonComment();
2412 if (Previous->is(tok::star) && PrevPrev && PrevPrev->isTypeName(LangOpts))
2413 return false;
2414 if (Previous->closesScope()) {
2415 // Not a potential C-style cast.
2416 if (Previous->isNot(tok::r_paren))
2417 return false;
2418 // Lambdas can be cast to function types only, e.g. `std::function<int()>`
2419 // and `int (*)()`.
2420 if (!PrevPrev || PrevPrev->isNoneOf(tok::greater, tok::r_paren))
2421 return false;
2422 }
2423 if (Previous && Previous->Tok.getIdentifierInfo() &&
2424 Previous->isNoneOf(tok::kw_return, tok::kw_co_await, tok::kw_co_yield,
2425 tok::kw_co_return)) {
2426 return false;
2427 }
2428 }
2429 if (LeftSquare->isCppStructuredBinding(IsCpp))
2430 return false;
2431 if (FormatTok->is(tok::l_square) || tok::isLiteral(FormatTok->Tok.getKind()))
2432 return false;
2433 if (FormatTok->is(tok::r_square)) {
2434 const FormatToken *Next = Tokens->peekNextToken(/*SkipComment=*/true);
2435 if (Next->is(tok::greater))
2436 return false;
2437 }
2438 parseSquare(/*LambdaIntroducer=*/true);
2439 return true;
2440}
2441
2442void UnwrappedLineParser::tryToParseJSFunction() {
2443 assert(FormatTok->is(Keywords.kw_function));
2444 if (FormatTok->is(Keywords.kw_async))
2445 nextToken();
2446 // Consume "function".
2447 nextToken();
2448
2449 // Consume * (generator function). Treat it like C++'s overloaded operators.
2450 if (FormatTok->is(tok::star)) {
2451 FormatTok->setFinalizedType(TT_OverloadedOperator);
2452 nextToken();
2453 }
2454
2455 // Consume function name.
2456 if (FormatTok->is(tok::identifier))
2457 nextToken();
2458
2459 if (FormatTok->isNot(tok::l_paren))
2460 return;
2461
2462 // Parse formal parameter list.
2463 parseParens();
2464
2465 if (FormatTok->is(tok::colon)) {
2466 // Parse a type definition.
2467 nextToken();
2468
2469 // Eat the type declaration. For braced inline object types, balance braces,
2470 // otherwise just parse until finding an l_brace for the function body.
2471 if (FormatTok->is(tok::l_brace))
2472 tryToParseBracedList();
2473 else
2474 while (FormatTok->isNoneOf(tok::l_brace, tok::semi) && !eof())
2475 nextToken();
2476 }
2477
2478 if (FormatTok->is(tok::semi))
2479 return;
2480
2481 parseChildBlock();
2482}
2483
2484bool UnwrappedLineParser::tryToParseBracedList() {
2485 if (FormatTok->is(BK_Unknown))
2486 calculateBraceTypes();
2487 assert(FormatTok->isNot(BK_Unknown));
2488 if (FormatTok->is(BK_Block))
2489 return false;
2490 nextToken();
2491 parseBracedList();
2492 return true;
2493}
2494
2495bool UnwrappedLineParser::tryToParseChildBlock() {
2496 assert(Style.isJavaScript() || Style.isCSharp());
2497 assert(FormatTok->is(TT_FatArrow));
2498 // Fat arrows (=>) have tok::TokenKind tok::equal but TokenType TT_FatArrow.
2499 // They always start an expression or a child block if followed by a curly
2500 // brace.
2501 nextToken();
2502 if (FormatTok->isNot(tok::l_brace))
2503 return false;
2504 parseChildBlock();
2505 return true;
2506}
2507
2508bool UnwrappedLineParser::parseBracedList(bool IsAngleBracket, bool IsEnum) {
2509 assert(!IsAngleBracket || !IsEnum);
2510 bool HasError = false;
2511
2512 // FIXME: Once we have an expression parser in the UnwrappedLineParser,
2513 // replace this by using parseAssignmentExpression() inside.
2514 do {
2515 if (Style.isCSharp() && FormatTok->is(TT_FatArrow) &&
2516 tryToParseChildBlock()) {
2517 continue;
2518 }
2519 if (Style.isJavaScript()) {
2520 if (FormatTok->is(Keywords.kw_function)) {
2521 tryToParseJSFunction();
2522 continue;
2523 }
2524 if (FormatTok->is(tok::l_brace)) {
2525 // Could be a method inside of a braced list `{a() { return 1; }}`.
2526 if (tryToParseBracedList())
2527 continue;
2528 parseChildBlock();
2529 }
2530 }
2531 if (FormatTok->is(IsAngleBracket ? tok::greater : tok::r_brace)) {
2532 if (IsEnum) {
2533 FormatTok->setBlockKind(BK_Block);
2534 if (!Style.AllowShortEnumsOnASingleLine)
2535 addUnwrappedLine();
2536 }
2537 nextToken();
2538 return !HasError;
2539 }
2540 switch (FormatTok->Tok.getKind()) {
2541 case tok::l_square:
2542 if (Style.isCSharp())
2543 parseSquare();
2544 else
2545 tryToParseLambda();
2546 break;
2547 case tok::l_paren:
2548 parseParens();
2549 // JavaScript can just have free standing methods and getters/setters in
2550 // object literals. Detect them by a "{" following ")".
2551 if (Style.isJavaScript()) {
2552 if (FormatTok->is(tok::l_brace))
2553 parseChildBlock();
2554 break;
2555 }
2556 break;
2557 case tok::l_brace:
2558 // Assume there are no blocks inside a braced init list apart
2559 // from the ones we explicitly parse out (like lambdas).
2560 FormatTok->setBlockKind(BK_BracedInit);
2561 if (!IsAngleBracket) {
2562 auto *Prev = FormatTok->Previous;
2563 if (Prev && Prev->is(tok::greater))
2564 Prev->setFinalizedType(TT_TemplateCloser);
2565 }
2566 nextToken();
2567 parseBracedList();
2568 break;
2569 case tok::less:
2570 nextToken();
2571 if (IsAngleBracket)
2572 parseBracedList(/*IsAngleBracket=*/true);
2573 break;
2574 case tok::semi:
2575 // JavaScript (or more precisely TypeScript) can have semicolons in braced
2576 // lists (in so-called TypeMemberLists). Thus, the semicolon cannot be
2577 // used for error recovery if we have otherwise determined that this is
2578 // a braced list.
2579 if (Style.isJavaScript()) {
2580 nextToken();
2581 break;
2582 }
2583 HasError = true;
2584 if (!IsEnum)
2585 return false;
2586 nextToken();
2587 break;
2588 case tok::comma:
2589 nextToken();
2590 if (IsEnum && !Style.AllowShortEnumsOnASingleLine)
2591 addUnwrappedLine();
2592 break;
2593 case tok::kw_requires:
2594 parseRequiresExpression();
2595 break;
2596 default:
2597 nextToken();
2598 break;
2599 }
2600 } while (!eof());
2601 return false;
2602}
2603
2604/// Parses a pair of parentheses (and everything between them).
2605/// \param StarAndAmpTokenType If different than TT_Unknown sets this type for
2606/// all (double) ampersands and stars. This applies for all nested scopes as
2607/// well.
2608///
2609/// Returns whether there is a `=` token between the parentheses.
2610bool UnwrappedLineParser::parseParens(TokenType StarAndAmpTokenType,
2611 bool InMacroCall) {
2612 assert(FormatTok->is(tok::l_paren) && "'(' expected.");
2613 auto *LParen = FormatTok;
2614 auto *Prev = FormatTok->Previous;
2615 bool SeenComma = false;
2616 bool SeenEqual = false;
2617 bool MightBeFoldExpr = false;
2618 nextToken();
2619 const bool MightBeStmtExpr = FormatTok->is(tok::l_brace);
2620 if (!InMacroCall && Prev && Prev->is(TT_FunctionLikeMacro))
2621 InMacroCall = true;
2622 do {
2623 switch (FormatTok->Tok.getKind()) {
2624 case tok::l_paren:
2625 if (parseParens(StarAndAmpTokenType, InMacroCall))
2626 SeenEqual = true;
2627 if (Style.isJava() && FormatTok->is(tok::l_brace))
2628 parseChildBlock();
2629 break;
2630 case tok::r_paren: {
2631 auto *RParen = FormatTok;
2632 nextToken();
2633 if (Prev) {
2634 auto OptionalParens = [&] {
2635 if (Style.RemoveParentheses == FormatStyle::RPS_Leave ||
2636 MightBeStmtExpr || MightBeFoldExpr || SeenComma || InMacroCall ||
2637 Line->InMacroBody || RParen->getPreviousNonComment() == LParen) {
2638 return false;
2639 }
2640 const bool DoubleParens =
2641 Prev->is(tok::l_paren) && FormatTok->is(tok::r_paren);
2642 if (DoubleParens) {
2643 const auto *PrevPrev = Prev->getPreviousNonComment();
2644 const bool Excluded =
2645 PrevPrev &&
2646 (PrevPrev->isOneOf(tok::kw___attribute, tok::kw_decltype) ||
2647 (SeenEqual &&
2648 (PrevPrev->isOneOf(tok::kw_if, tok::kw_while) ||
2649 PrevPrev->endsSequence(tok::kw_constexpr, tok::kw_if))));
2650 if (!Excluded)
2651 return true;
2652 } else {
2653 const bool CommaSeparated =
2654 Prev->isOneOf(tok::l_paren, tok::comma) &&
2655 FormatTok->isOneOf(tok::comma, tok::r_paren);
2656 if (CommaSeparated &&
2657 // LParen is not preceded by ellipsis, comma.
2658 !Prev->endsSequence(tok::comma, tok::ellipsis) &&
2659 // RParen is not followed by comma, ellipsis.
2660 !(FormatTok->is(tok::comma) &&
2661 Tokens->peekNextToken()->is(tok::ellipsis))) {
2662 return true;
2663 }
2664 const bool ReturnParens =
2665 Style.RemoveParentheses == FormatStyle::RPS_ReturnStatement &&
2666 ((NestedLambdas.empty() && !IsDecltypeAutoFunction) ||
2667 (!NestedLambdas.empty() && !NestedLambdas.back())) &&
2668 Prev->isOneOf(tok::kw_return, tok::kw_co_return) &&
2669 FormatTok->is(tok::semi);
2670 if (ReturnParens)
2671 return true;
2672 }
2673 return false;
2674 };
2675 if (OptionalParens()) {
2676 LParen->Optional = true;
2677 RParen->Optional = true;
2678 } else if (Prev->is(TT_TypenameMacro)) {
2679 LParen->setFinalizedType(TT_TypeDeclarationParen);
2680 RParen->setFinalizedType(TT_TypeDeclarationParen);
2681 } else if (Prev->is(tok::greater) && RParen->Previous == LParen) {
2682 Prev->setFinalizedType(TT_TemplateCloser);
2683 } else if (FormatTok->is(tok::l_brace) && Prev->is(tok::amp) &&
2684 !Prev->Previous) {
2685 FormatTok->setBlockKind(BK_BracedInit);
2686 }
2687 }
2688 return SeenEqual;
2689 }
2690 case tok::r_brace:
2691 // A "}" inside parenthesis is an error if there wasn't a matching "{".
2692 return SeenEqual;
2693 case tok::l_square:
2694 tryToParseLambda();
2695 break;
2696 case tok::l_brace:
2697 if (!tryToParseBracedList())
2698 parseChildBlock();
2699 break;
2700 case tok::at:
2701 nextToken();
2702 if (FormatTok->is(tok::l_brace)) {
2703 nextToken();
2704 parseBracedList();
2705 }
2706 break;
2707 case tok::comma:
2708 SeenComma = true;
2709 nextToken();
2710 break;
2711 case tok::ellipsis:
2712 MightBeFoldExpr = true;
2713 nextToken();
2714 break;
2715 case tok::equal:
2716 SeenEqual = true;
2717 if (Style.isCSharp() && FormatTok->is(TT_FatArrow))
2718 tryToParseChildBlock();
2719 else
2720 nextToken();
2721 break;
2722 case tok::kw_class:
2723 if (Style.isJavaScript())
2724 parseRecord(/*ParseAsExpr=*/true);
2725 else
2726 nextToken();
2727 break;
2728 case tok::identifier:
2729 if (Style.isJavaScript() && (FormatTok->is(Keywords.kw_function)))
2730 tryToParseJSFunction();
2731 else
2732 nextToken();
2733 break;
2734 case tok::kw_switch:
2735 if (Style.isJava())
2736 parseSwitch(/*IsExpr=*/true);
2737 else
2738 nextToken();
2739 break;
2740 case tok::kw_requires:
2741 parseRequiresExpression();
2742 break;
2743 case tok::star:
2744 case tok::amp:
2745 case tok::ampamp:
2746 if (StarAndAmpTokenType != TT_Unknown)
2747 FormatTok->setFinalizedType(StarAndAmpTokenType);
2748 [[fallthrough]];
2749 default:
2750 nextToken();
2751 break;
2752 }
2753 } while (!eof());
2754 return SeenEqual;
2755}
2756
2757void UnwrappedLineParser::parseSquare(bool LambdaIntroducer) {
2758 if (!LambdaIntroducer) {
2759 assert(FormatTok->is(tok::l_square) && "'[' expected.");
2760 if (tryToParseLambda())
2761 return;
2762 }
2763 do {
2764 switch (FormatTok->Tok.getKind()) {
2765 case tok::l_paren:
2766 parseParens();
2767 break;
2768 case tok::r_square:
2769 nextToken();
2770 return;
2771 case tok::r_brace:
2772 // A "}" inside parenthesis is an error if there wasn't a matching "{".
2773 return;
2774 case tok::l_square:
2775 parseSquare();
2776 break;
2777 case tok::l_brace: {
2778 if (!tryToParseBracedList())
2779 parseChildBlock();
2780 break;
2781 }
2782 case tok::at:
2783 case tok::colon:
2784 nextToken();
2785 if (FormatTok->is(tok::l_brace)) {
2786 nextToken();
2787 parseBracedList();
2788 }
2789 break;
2790 default:
2791 nextToken();
2792 break;
2793 }
2794 } while (!eof());
2795}
2796
2797void UnwrappedLineParser::keepAncestorBraces() {
2798 if (!Style.RemoveBracesLLVM)
2799 return;
2800
2801 const int MaxNestingLevels = 2;
2802 const int Size = NestedTooDeep.size();
2803 if (Size >= MaxNestingLevels)
2804 NestedTooDeep[Size - MaxNestingLevels] = true;
2805 NestedTooDeep.push_back(false);
2806}
2807
2809 for (const auto &Token : llvm::reverse(Line.Tokens))
2810 if (Token.Tok->isNot(tok::comment))
2811 return Token.Tok;
2812
2813 return nullptr;
2814}
2815
2816void UnwrappedLineParser::parseUnbracedBody(bool CheckEOF) {
2817 FormatToken *Tok = nullptr;
2818
2819 if (Style.InsertBraces && !Line->InPPDirective && !Line->Tokens.empty() &&
2820 PreprocessorDirectives.empty() && FormatTok->isNot(tok::semi)) {
2821 Tok = Style.BraceWrapping.AfterControlStatement == FormatStyle::BWACS_Never
2822 ? getLastNonComment(*Line)
2823 : Line->Tokens.back().Tok;
2824 assert(Tok);
2825 if (Tok->BraceCount < 0) {
2826 assert(Tok->BraceCount == -1);
2827 Tok = nullptr;
2828 } else {
2829 Tok->BraceCount = -1;
2830 }
2831 }
2832
2833 addUnwrappedLine();
2834 ++Line->Level;
2835 ++Line->UnbracedBodyLevel;
2836 parseStructuralElement();
2837 --Line->UnbracedBodyLevel;
2838
2839 if (Tok) {
2840 assert(!Line->InPPDirective);
2841 Tok = nullptr;
2842 for (const auto &L : llvm::reverse(*CurrentLines)) {
2843 if (!L.InPPDirective && getLastNonComment(L)) {
2844 Tok = L.Tokens.back().Tok;
2845 break;
2846 }
2847 }
2848 assert(Tok);
2849 ++Tok->BraceCount;
2850 }
2851
2852 if (CheckEOF && eof())
2853 addUnwrappedLine();
2854
2855 --Line->Level;
2856}
2857
2858static void markOptionalBraces(FormatToken *LeftBrace) {
2859 if (!LeftBrace)
2860 return;
2861
2862 assert(LeftBrace->is(tok::l_brace));
2863
2864 FormatToken *RightBrace = LeftBrace->MatchingParen;
2865 if (!RightBrace) {
2866 assert(!LeftBrace->Optional);
2867 return;
2868 }
2869
2870 assert(RightBrace->is(tok::r_brace));
2871 assert(RightBrace->MatchingParen == LeftBrace);
2872 assert(LeftBrace->Optional == RightBrace->Optional);
2873
2874 LeftBrace->Optional = true;
2875 RightBrace->Optional = true;
2876}
2877
2878void UnwrappedLineParser::handleAttributes() {
2879 // Handle AttributeMacro, e.g. `if (x) UNLIKELY`.
2880 if (FormatTok->isAttribute())
2881 nextToken();
2882 else if (FormatTok->is(tok::l_square))
2883 handleCppAttributes();
2884}
2885
2886bool UnwrappedLineParser::handleCppAttributes() {
2887 // Handle [[likely]] / [[unlikely]] attributes.
2888 assert(FormatTok->is(tok::l_square));
2889 if (!tryToParseSimpleAttribute())
2890 return false;
2891 parseSquare();
2892 return true;
2893}
2894
2895/// Returns whether \c Tok begins a block.
2896bool UnwrappedLineParser::isBlockBegin(const FormatToken &Tok) const {
2897 // FIXME: rename the function or make
2898 // Tok.isOneOf(tok::l_brace, TT_MacroBlockBegin) work.
2899 return Style.isVerilog() ? Keywords.isVerilogBegin(Tok)
2900 : Tok.is(tok::l_brace);
2901}
2902
2903FormatToken *UnwrappedLineParser::parseIfThenElse(IfStmtKind *IfKind,
2904 bool KeepBraces,
2905 bool IsVerilogAssert) {
2906 assert((FormatTok->is(tok::kw_if) ||
2907 (Style.isVerilog() &&
2908 FormatTok->isOneOf(tok::kw_restrict, Keywords.kw_assert,
2909 Keywords.kw_assume, Keywords.kw_cover))) &&
2910 "'if' expected");
2911 nextToken();
2912
2913 if (IsVerilogAssert) {
2914 // Handle `assert #0` and `assert final`.
2915 if (FormatTok->is(Keywords.kw_verilogHash)) {
2916 nextToken();
2917 if (FormatTok->is(tok::numeric_constant))
2918 nextToken();
2919 } else if (FormatTok->isOneOf(Keywords.kw_final, Keywords.kw_property,
2920 Keywords.kw_sequence)) {
2921 nextToken();
2922 }
2923 }
2924
2925 // TableGen's if statement has the form of `if <cond> then { ... }`.
2926 if (Style.isTableGen()) {
2927 while (!eof() && FormatTok->isNot(Keywords.kw_then)) {
2928 // Simply skip until then. This range only contains a value.
2929 nextToken();
2930 }
2931 }
2932
2933 // Handle `if !consteval`.
2934 if (FormatTok->is(tok::exclaim))
2935 nextToken();
2936
2937 bool KeepIfBraces = true;
2938 if (FormatTok->is(tok::kw_consteval)) {
2939 nextToken();
2940 } else {
2941 KeepIfBraces = !Style.RemoveBracesLLVM || KeepBraces;
2942 if (FormatTok->isOneOf(tok::kw_constexpr, tok::identifier))
2943 nextToken();
2944 if (FormatTok->is(tok::l_paren)) {
2945 FormatTok->setFinalizedType(TT_ConditionLParen);
2946 parseParens();
2947 }
2948 }
2949 handleAttributes();
2950 // The then action is optional in Verilog assert statements.
2951 if (IsVerilogAssert && FormatTok->is(tok::semi)) {
2952 nextToken();
2953 addUnwrappedLine();
2954 return nullptr;
2955 }
2956
2957 bool NeedsUnwrappedLine = false;
2958 keepAncestorBraces();
2959
2960 FormatToken *IfLeftBrace = nullptr;
2961 IfStmtKind IfBlockKind = IfStmtKind::NotIf;
2962
2963 if (isBlockBegin(*FormatTok)) {
2964 FormatTok->setFinalizedType(TT_ControlStatementLBrace);
2965 IfLeftBrace = FormatTok;
2966 CompoundStatementIndenter Indenter(this, Style, Line->Level);
2967 parseBlock(/*MustBeDeclaration=*/false, /*AddLevels=*/1u,
2968 /*MunchSemi=*/true, KeepIfBraces, &IfBlockKind);
2969 setPreviousRBraceType(TT_ControlStatementRBrace);
2970 if (Style.BraceWrapping.BeforeElse)
2971 addUnwrappedLine();
2972 else
2973 NeedsUnwrappedLine = true;
2974 } else if (IsVerilogAssert && FormatTok->is(tok::kw_else)) {
2975 addUnwrappedLine();
2976 } else {
2977 parseUnbracedBody();
2978 }
2979
2980 if (Style.RemoveBracesLLVM) {
2981 assert(!NestedTooDeep.empty());
2982 KeepIfBraces = KeepIfBraces ||
2983 (IfLeftBrace && !IfLeftBrace->MatchingParen) ||
2984 NestedTooDeep.back() || IfBlockKind == IfStmtKind::IfOnly ||
2985 IfBlockKind == IfStmtKind::IfElseIf;
2986 }
2987
2988 bool KeepElseBraces = KeepIfBraces;
2989 FormatToken *ElseLeftBrace = nullptr;
2990 IfStmtKind Kind = IfStmtKind::IfOnly;
2991
2992 if (FormatTok->is(tok::kw_else)) {
2993 if (Style.RemoveBracesLLVM) {
2994 NestedTooDeep.back() = false;
2995 Kind = IfStmtKind::IfElse;
2996 }
2997 nextToken();
2998 handleAttributes();
2999 if (isBlockBegin(*FormatTok)) {
3000 const bool FollowedByIf = Tokens->peekNextToken()->is(tok::kw_if);
3001 FormatTok->setFinalizedType(TT_ElseLBrace);
3002 ElseLeftBrace = FormatTok;
3003 CompoundStatementIndenter Indenter(this, Style, Line->Level);
3004 IfStmtKind ElseBlockKind = IfStmtKind::NotIf;
3005 FormatToken *IfLBrace =
3006 parseBlock(/*MustBeDeclaration=*/false, /*AddLevels=*/1u,
3007 /*MunchSemi=*/true, KeepElseBraces, &ElseBlockKind);
3008 setPreviousRBraceType(TT_ElseRBrace);
3009 if (FormatTok->is(tok::kw_else)) {
3010 KeepElseBraces = KeepElseBraces ||
3011 ElseBlockKind == IfStmtKind::IfOnly ||
3012 ElseBlockKind == IfStmtKind::IfElseIf;
3013 } else if (FollowedByIf && IfLBrace && !IfLBrace->Optional) {
3014 KeepElseBraces = true;
3015 assert(ElseLeftBrace->MatchingParen);
3016 markOptionalBraces(ElseLeftBrace);
3017 }
3018 addUnwrappedLine();
3019 } else if (!IsVerilogAssert && FormatTok->is(tok::kw_if)) {
3020 const FormatToken *Previous = Tokens->getPreviousToken();
3021 assert(Previous);
3022 const bool IsPrecededByComment = Previous->is(tok::comment);
3023 if (IsPrecededByComment) {
3024 addUnwrappedLine();
3025 ++Line->Level;
3026 }
3027 bool TooDeep = true;
3028 if (Style.RemoveBracesLLVM) {
3029 Kind = IfStmtKind::IfElseIf;
3030 TooDeep = NestedTooDeep.pop_back_val();
3031 }
3032 ElseLeftBrace = parseIfThenElse(/*IfKind=*/nullptr, KeepIfBraces);
3033 if (Style.RemoveBracesLLVM)
3034 NestedTooDeep.push_back(TooDeep);
3035 if (IsPrecededByComment)
3036 --Line->Level;
3037 } else {
3038 parseUnbracedBody(/*CheckEOF=*/true);
3039 }
3040 } else {
3041 KeepIfBraces = KeepIfBraces || IfBlockKind == IfStmtKind::IfElse;
3042 if (NeedsUnwrappedLine)
3043 addUnwrappedLine();
3044 }
3045
3046 if (!Style.RemoveBracesLLVM)
3047 return nullptr;
3048
3049 assert(!NestedTooDeep.empty());
3050 KeepElseBraces = KeepElseBraces ||
3051 (ElseLeftBrace && !ElseLeftBrace->MatchingParen) ||
3052 NestedTooDeep.back();
3053
3054 NestedTooDeep.pop_back();
3055
3056 if (!KeepIfBraces && !KeepElseBraces) {
3057 markOptionalBraces(IfLeftBrace);
3058 markOptionalBraces(ElseLeftBrace);
3059 } else if (IfLeftBrace) {
3060 FormatToken *IfRightBrace = IfLeftBrace->MatchingParen;
3061 if (IfRightBrace) {
3062 assert(IfRightBrace->MatchingParen == IfLeftBrace);
3063 assert(!IfLeftBrace->Optional);
3064 assert(!IfRightBrace->Optional);
3065 IfLeftBrace->MatchingParen = nullptr;
3066 IfRightBrace->MatchingParen = nullptr;
3067 }
3068 }
3069
3070 if (IfKind)
3071 *IfKind = Kind;
3072
3073 return IfLeftBrace;
3074}
3075
3076void UnwrappedLineParser::parseTryCatch() {
3077 assert(FormatTok->isOneOf(tok::kw_try, tok::kw___try) && "'try' expected");
3078 nextToken();
3079 bool NeedsUnwrappedLine = false;
3080 bool HasCtorInitializer = false;
3081 if (FormatTok->is(tok::colon)) {
3082 auto *Colon = FormatTok;
3083 // We are in a function try block, what comes is an initializer list.
3084 nextToken();
3085 if (FormatTok->is(tok::identifier)) {
3086 HasCtorInitializer = true;
3087 Colon->setFinalizedType(TT_CtorInitializerColon);
3088 }
3089
3090 // In case identifiers were removed by clang-tidy, what might follow is
3091 // multiple commas in sequence - before the first identifier.
3092 while (FormatTok->is(tok::comma))
3093 nextToken();
3094
3095 while (FormatTok->is(tok::identifier)) {
3096 nextToken();
3097 if (FormatTok->is(tok::l_paren)) {
3098 parseParens();
3099 } else if (FormatTok->is(tok::l_brace)) {
3100 nextToken();
3101 parseBracedList();
3102 }
3103
3104 // In case identifiers were removed by clang-tidy, what might follow is
3105 // multiple commas in sequence - after the first identifier.
3106 while (FormatTok->is(tok::comma))
3107 nextToken();
3108 }
3109 }
3110 // Parse try with resource.
3111 if (Style.isJava() && FormatTok->is(tok::l_paren))
3112 parseParens();
3113
3114 keepAncestorBraces();
3115
3116 if (FormatTok->is(tok::l_brace)) {
3117 if (HasCtorInitializer)
3118 FormatTok->setFinalizedType(TT_FunctionLBrace);
3119 CompoundStatementIndenter Indenter(this, Style, Line->Level);
3120 parseBlock();
3121 if (Style.BraceWrapping.BeforeCatch)
3122 addUnwrappedLine();
3123 else
3124 NeedsUnwrappedLine = true;
3125 } else if (FormatTok->isNot(tok::kw_catch)) {
3126 // The C++ standard requires a compound-statement after a try.
3127 // If there's none, we try to assume there's a structuralElement
3128 // and try to continue.
3129 addUnwrappedLine();
3130 ++Line->Level;
3131 parseStructuralElement();
3132 --Line->Level;
3133 }
3134 for (bool SeenCatch = false;;) {
3135 if (FormatTok->is(tok::at))
3136 nextToken();
3137 if (FormatTok->isNoneOf(tok::kw_catch, Keywords.kw___except,
3138 tok::kw___finally, tok::objc_catch,
3139 tok::objc_finally) &&
3140 !((Style.isJava() || Style.isJavaScript()) &&
3141 FormatTok->is(Keywords.kw_finally))) {
3142 break;
3143 }
3144 if (FormatTok->is(tok::kw_catch))
3145 SeenCatch = true;
3146 nextToken();
3147 while (FormatTok->isNot(tok::l_brace)) {
3148 if (FormatTok->is(tok::l_paren)) {
3149 parseParens();
3150 continue;
3151 }
3152 if (FormatTok->isOneOf(tok::semi, tok::r_brace) || eof()) {
3153 if (Style.RemoveBracesLLVM)
3154 NestedTooDeep.pop_back();
3155 return;
3156 }
3157 nextToken();
3158 }
3159 if (SeenCatch) {
3160 FormatTok->setFinalizedType(TT_ControlStatementLBrace);
3161 SeenCatch = false;
3162 }
3163 NeedsUnwrappedLine = false;
3164 Line->MustBeDeclaration = false;
3165 CompoundStatementIndenter Indenter(this, Style, Line->Level);
3166 parseBlock();
3167 if (Style.BraceWrapping.BeforeCatch)
3168 addUnwrappedLine();
3169 else
3170 NeedsUnwrappedLine = true;
3171 }
3172
3173 if (Style.RemoveBracesLLVM)
3174 NestedTooDeep.pop_back();
3175
3176 if (NeedsUnwrappedLine)
3177 addUnwrappedLine();
3178}
3179
3180void UnwrappedLineParser::parseNamespaceOrExportBlock(unsigned AddLevels) {
3181 bool ManageWhitesmithsBraces =
3182 AddLevels == 0u && Style.BreakBeforeBraces == FormatStyle::BS_Whitesmiths;
3183
3184 // If we're in Whitesmiths mode, indent the brace if we're not indenting
3185 // the whole block.
3186 if (ManageWhitesmithsBraces)
3187 ++Line->Level;
3188
3189 // Munch the semicolon after the block. This is more common than one would
3190 // think. Putting the semicolon into its own line is very ugly.
3191 parseBlock(/*MustBeDeclaration=*/true, AddLevels, /*MunchSemi=*/true,
3192 /*KeepBraces=*/true, /*IfKind=*/nullptr, ManageWhitesmithsBraces);
3193
3194 addUnwrappedLine(AddLevels > 0 ? LineLevel::Remove : LineLevel::Keep);
3195
3196 if (ManageWhitesmithsBraces)
3197 --Line->Level;
3198}
3199
3200void UnwrappedLineParser::parseNamespace() {
3201 assert(FormatTok->isOneOf(tok::kw_namespace, TT_NamespaceMacro) &&
3202 "'namespace' expected");
3203
3204 const FormatToken &InitialToken = *FormatTok;
3205 nextToken();
3206 if (InitialToken.is(TT_NamespaceMacro)) {
3207 parseParens();
3208 } else {
3209 while (FormatTok->isOneOf(tok::identifier, tok::coloncolon, tok::kw_inline,
3210 tok::l_square, tok::period, tok::l_paren) ||
3211 (Style.isCSharp() && FormatTok->is(tok::kw_union))) {
3212 if (FormatTok->is(tok::l_square))
3213 parseSquare();
3214 else if (FormatTok->is(tok::l_paren))
3215 parseParens();
3216 else
3217 nextToken();
3218 }
3219 }
3220 if (FormatTok->is(tok::l_brace)) {
3221 FormatTok->setFinalizedType(TT_NamespaceLBrace);
3222
3223 if (ShouldBreakBeforeBrace(Style, InitialToken,
3224 Tokens->peekNextToken()->is(tok::r_brace))) {
3225 addUnwrappedLine();
3226 }
3227
3228 unsigned AddLevels =
3229 Style.NamespaceIndentation == FormatStyle::NI_All ||
3230 (Style.NamespaceIndentation == FormatStyle::NI_Inner &&
3231 DeclarationScopeStack.size() > 1)
3232 ? 1u
3233 : 0u;
3234 parseNamespaceOrExportBlock(AddLevels);
3235 }
3236 // FIXME: Add error handling.
3237}
3238
3239void UnwrappedLineParser::parseCppExportBlock() {
3240 parseNamespaceOrExportBlock(/*AddLevels=*/Style.IndentExportBlock ? 1 : 0);
3241}
3242
3243void UnwrappedLineParser::parseNew() {
3244 assert(FormatTok->is(tok::kw_new) && "'new' expected");
3245 nextToken();
3246
3247 if (Style.isCSharp()) {
3248 do {
3249 // Handle constructor invocation, e.g. `new(field: value)`.
3250 if (FormatTok->is(tok::l_paren))
3251 parseParens();
3252
3253 // Handle array initialization syntax, e.g. `new[] {10, 20, 30}`.
3254 if (FormatTok->is(tok::l_brace))
3255 parseBracedList();
3256
3257 if (FormatTok->isOneOf(tok::semi, tok::comma))
3258 return;
3259
3260 nextToken();
3261 } while (!eof());
3262 }
3263
3264 if (!Style.isJava())
3265 return;
3266
3267 // In Java, we can parse everything up to the parens, which aren't optional.
3268 do {
3269 // There should not be a ;, { or } before the new's open paren.
3270 if (FormatTok->isOneOf(tok::semi, tok::l_brace, tok::r_brace))
3271 return;
3272
3273 // Consume the parens.
3274 if (FormatTok->is(tok::l_paren)) {
3275 parseParens();
3276
3277 // If there is a class body of an anonymous class, consume that as child.
3278 if (FormatTok->is(tok::l_brace))
3279 parseChildBlock();
3280 return;
3281 }
3282 nextToken();
3283 } while (!eof());
3284}
3285
3286void UnwrappedLineParser::parseLoopBody(bool KeepBraces, bool WrapRightBrace) {
3287 keepAncestorBraces();
3288
3289 if (isBlockBegin(*FormatTok)) {
3290 FormatTok->setFinalizedType(TT_ControlStatementLBrace);
3291 FormatToken *LeftBrace = FormatTok;
3292 CompoundStatementIndenter Indenter(this, Style, Line->Level);
3293 parseBlock(/*MustBeDeclaration=*/false, /*AddLevels=*/1u,
3294 /*MunchSemi=*/true, KeepBraces);
3295 setPreviousRBraceType(TT_ControlStatementRBrace);
3296 if (!KeepBraces) {
3297 assert(!NestedTooDeep.empty());
3298 if (!NestedTooDeep.back())
3299 markOptionalBraces(LeftBrace);
3300 }
3301 if (WrapRightBrace)
3302 addUnwrappedLine();
3303 } else {
3304 parseUnbracedBody();
3305 }
3306
3307 if (!KeepBraces)
3308 NestedTooDeep.pop_back();
3309}
3310
3311void UnwrappedLineParser::parseForOrWhileLoop(bool HasParens) {
3312 assert((FormatTok->isOneOf(tok::kw_for, tok::kw_while, TT_ForEachMacro) ||
3313 (Style.isVerilog() &&
3314 FormatTok->isOneOf(Keywords.kw_always, Keywords.kw_always_comb,
3315 Keywords.kw_always_ff, Keywords.kw_always_latch,
3316 Keywords.kw_final, Keywords.kw_initial,
3317 Keywords.kw_foreach, Keywords.kw_forever,
3318 Keywords.kw_repeat))) &&
3319 "'for', 'while' or foreach macro expected");
3320 const bool KeepBraces = !Style.RemoveBracesLLVM ||
3321 FormatTok->isNoneOf(tok::kw_for, tok::kw_while);
3322
3323 nextToken();
3324 // JS' for await ( ...
3325 if (Style.isJavaScript() && FormatTok->is(Keywords.kw_await))
3326 nextToken();
3327 if (IsCpp && FormatTok->is(tok::kw_co_await))
3328 nextToken();
3329 if (HasParens && FormatTok->is(tok::l_paren)) {
3330 // The type is only set for Verilog basically because we were afraid to
3331 // change the existing behavior for loops. See the discussion on D121756 for
3332 // details.
3333 if (Style.isVerilog())
3334 FormatTok->setFinalizedType(TT_ConditionLParen);
3335 parseParens();
3336 }
3337
3338 if (Style.isVerilog()) {
3339 // Event control.
3340 parseVerilogSensitivityList();
3341 } else if (Style.AllowShortLoopsOnASingleLine && FormatTok->is(tok::semi) &&
3342 Tokens->getPreviousToken()->is(tok::r_paren)) {
3343 nextToken();
3344 addUnwrappedLine();
3345 return;
3346 }
3347
3348 handleAttributes();
3349 parseLoopBody(KeepBraces, /*WrapRightBrace=*/true);
3350}
3351
3352void UnwrappedLineParser::parseDoWhile() {
3353 assert(FormatTok->is(tok::kw_do) && "'do' expected");
3354 nextToken();
3355
3356 parseLoopBody(/*KeepBraces=*/true, Style.BraceWrapping.BeforeWhile);
3357
3358 // FIXME: Add error handling.
3359 if (FormatTok->isNot(tok::kw_while)) {
3360 addUnwrappedLine();
3361 return;
3362 }
3363
3364 FormatTok->setFinalizedType(TT_DoWhile);
3365
3366 // If in Whitesmiths mode, the line with the while() needs to be indented
3367 // to the same level as the block.
3368 if (Style.BreakBeforeBraces == FormatStyle::BS_Whitesmiths)
3369 ++Line->Level;
3370
3371 nextToken();
3372 parseStructuralElement();
3373}
3374
3375void UnwrappedLineParser::parseLabel(
3376 FormatStyle::IndentGotoLabelStyle IndentGotoLabels) {
3377 const bool IsGotoLabel = FormatTok->is(TT_GotoLabelColon);
3378 nextToken();
3379 unsigned OldLineLevel = Line->Level;
3380
3381 switch (IndentGotoLabels) {
3382 case FormatStyle::IGLS_NoIndent:
3383 Line->Level = 0;
3384 break;
3385 case FormatStyle::IGLS_OuterIndent:
3386 if (Line->Level > 1 || (!Line->InPPDirective && Line->Level > 0))
3387 --Line->Level;
3388 break;
3389 case FormatStyle::IGLS_HalfIndent:
3390 case FormatStyle::IGLS_InnerIndent:
3391 break;
3392 }
3393
3394 if (!IsGotoLabel && !Style.IndentCaseBlocks &&
3395 CommentsBeforeNextToken.empty() && FormatTok->is(tok::l_brace)) {
3396 CompoundStatementIndenter Indenter(this, Line->Level,
3397 Style.BraceWrapping.AfterCaseLabel,
3398 Style.BraceWrapping.IndentBraces);
3399 parseBlock();
3400 if (FormatTok->is(tok::kw_break)) {
3401 if (Style.BraceWrapping.AfterControlStatement ==
3402 FormatStyle::BWACS_Always) {
3403 addUnwrappedLine();
3404 if (!Style.IndentCaseBlocks &&
3405 Style.BreakBeforeBraces == FormatStyle::BS_Whitesmiths) {
3406 ++Line->Level;
3407 }
3408 }
3409 parseStructuralElement();
3410 }
3411 addUnwrappedLine();
3412 } else {
3413 if (FormatTok->is(tok::semi))
3414 nextToken();
3415 addUnwrappedLine();
3416 }
3417 Line->Level = OldLineLevel;
3418 if (FormatTok->isNot(tok::l_brace)) {
3419 parseStructuralElement();
3420 addUnwrappedLine();
3421 }
3422}
3423
3424void UnwrappedLineParser::parseCaseLabel() {
3425 assert(FormatTok->is(tok::kw_case) && "'case' expected");
3426 auto *Case = FormatTok;
3427
3428 // FIXME: fix handling of complex expressions here.
3429 do {
3430 nextToken();
3431 if (FormatTok->is(tok::colon)) {
3432 FormatTok->setFinalizedType(TT_CaseLabelColon);
3433 break;
3434 }
3435 if (Style.isJava() && FormatTok->is(tok::arrow)) {
3436 FormatTok->setFinalizedType(TT_CaseLabelArrow);
3437 Case->setFinalizedType(TT_SwitchExpressionLabel);
3438 break;
3439 }
3440 } while (!eof());
3441 parseLabel();
3442}
3443
3444void UnwrappedLineParser::parseSwitch(bool IsExpr) {
3445 assert(FormatTok->is(tok::kw_switch) && "'switch' expected");
3446 nextToken();
3447 if (FormatTok->is(tok::l_paren))
3448 parseParens();
3449
3450 keepAncestorBraces();
3451
3452 if (FormatTok->is(tok::l_brace)) {
3453 CompoundStatementIndenter Indenter(this, Style, Line->Level);
3454 FormatTok->setFinalizedType(IsExpr ? TT_SwitchExpressionLBrace
3455 : TT_ControlStatementLBrace);
3456 if (IsExpr)
3457 parseChildBlock();
3458 else
3459 parseBlock();
3460 setPreviousRBraceType(TT_ControlStatementRBrace);
3461 if (!IsExpr)
3462 addUnwrappedLine();
3463 } else {
3464 addUnwrappedLine();
3465 ++Line->Level;
3466 parseStructuralElement();
3467 --Line->Level;
3468 }
3469
3470 if (Style.RemoveBracesLLVM)
3471 NestedTooDeep.pop_back();
3472}
3473
3474void UnwrappedLineParser::parseAccessSpecifier() {
3475 nextToken();
3476 // Understand Qt's slots.
3477 if (FormatTok->isOneOf(Keywords.kw_slots, Keywords.kw_qslots))
3478 nextToken();
3479 // Otherwise, we don't know what it is, and we'd better keep the next token.
3480 if (FormatTok->is(tok::colon))
3481 nextToken();
3482 addUnwrappedLine();
3483}
3484
3485/// Parses a requires, decides if it is a clause or an expression.
3486/// \pre The current token has to be the requires keyword.
3487/// \returns true if it parsed a clause.
3488bool UnwrappedLineParser::parseRequires(bool SeenEqual) {
3489 assert(FormatTok->is(tok::kw_requires) && "'requires' expected");
3490
3491 // We try to guess if it is a requires clause, or a requires expression. For
3492 // that we first check the next token.
3493 switch (Tokens->peekNextToken(/*SkipComment=*/true)->Tok.getKind()) {
3494 case tok::l_brace:
3495 // This can only be an expression, never a clause.
3496 parseRequiresExpression();
3497 return false;
3498 case tok::l_paren:
3499 // Clauses and expression can start with a paren, it's unclear what we have.
3500 break;
3501 default:
3502 // All other tokens can only be a clause.
3503 parseRequiresClause();
3504 return true;
3505 }
3506
3507 // Looking forward we would have to decide if there are function declaration
3508 // like arguments to the requires expression:
3509 // requires (T t) {
3510 // Or there is a constraint expression for the requires clause:
3511 // requires (C<T> && ...
3512
3513 // But first let's look behind.
3514 auto *PreviousNonComment = FormatTok->getPreviousNonComment();
3515
3516 if (!PreviousNonComment ||
3517 PreviousNonComment->is(TT_RequiresExpressionLBrace)) {
3518 // If there is no token, or an expression left brace, we are a requires
3519 // clause within a requires expression.
3520 parseRequiresClause();
3521 return true;
3522 }
3523
3524 switch (PreviousNonComment->Tok.getKind()) {
3525 case tok::greater:
3526 case tok::r_paren:
3527 case tok::kw_noexcept:
3528 case tok::kw_const:
3529 case tok::star:
3530 case tok::amp:
3531 // This is a requires clause.
3532 parseRequiresClause();
3533 return true;
3534 case tok::ampamp: {
3535 // This can be either:
3536 // if (... && requires (T t) ...)
3537 // Or
3538 // void member(...) && requires (C<T> ...
3539 // We check the one token before that for a const:
3540 // void member(...) const && requires (C<T> ...
3541 auto PrevPrev = PreviousNonComment->getPreviousNonComment();
3542 if ((PrevPrev && PrevPrev->is(tok::kw_const)) || !SeenEqual) {
3543 parseRequiresClause();
3544 return true;
3545 }
3546 break;
3547 }
3548 default:
3549 if (PreviousNonComment->isTypeOrIdentifier(LangOpts)) {
3550 // This is a requires clause.
3551 parseRequiresClause();
3552 return true;
3553 }
3554 // It's an expression.
3555 parseRequiresExpression();
3556 return false;
3557 }
3558
3559 // Now we look forward and try to check if the paren content is a parameter
3560 // list. The parameters can be cv-qualified and contain references or
3561 // pointers.
3562 // So we want basically to check for TYPE NAME, but TYPE can contain all kinds
3563 // of stuff: typename, const, *, &, &&, ::, identifiers.
3564
3565 unsigned StoredPosition = Tokens->getPosition();
3566 FormatToken *NextToken = Tokens->getNextToken();
3567 int Lookahead = 0;
3568 auto PeekNext = [&Lookahead, &NextToken, this] {
3569 ++Lookahead;
3570 NextToken = Tokens->getNextToken();
3571 };
3572
3573 bool FoundType = false;
3574 bool LastWasColonColon = false;
3575 int OpenAngles = 0;
3576
3577 for (; Lookahead < 50; PeekNext()) {
3578 switch (NextToken->Tok.getKind()) {
3579 case tok::kw_volatile:
3580 case tok::kw_const:
3581 case tok::comma:
3582 if (OpenAngles == 0) {
3583 FormatTok = Tokens->setPosition(StoredPosition);
3584 parseRequiresExpression();
3585 return false;
3586 }
3587 break;
3588 case tok::eof:
3589 // Break out of the loop.
3590 Lookahead = 50;
3591 break;
3592 case tok::coloncolon:
3593 LastWasColonColon = true;
3594 break;
3595 case tok::kw_decltype:
3596 case tok::identifier:
3597 if (FoundType && !LastWasColonColon && OpenAngles == 0) {
3598 FormatTok = Tokens->setPosition(StoredPosition);
3599 parseRequiresExpression();
3600 return false;
3601 }
3602 FoundType = true;
3603 LastWasColonColon = false;
3604 break;
3605 case tok::less:
3606 ++OpenAngles;
3607 break;
3608 case tok::greater:
3609 --OpenAngles;
3610 break;
3611 default:
3612 if (NextToken->isTypeName(LangOpts)) {
3613 FormatTok = Tokens->setPosition(StoredPosition);
3614 parseRequiresExpression();
3615 return false;
3616 }
3617 break;
3618 }
3619 }
3620 // This seems to be a complicated expression, just assume it's a clause.
3621 FormatTok = Tokens->setPosition(StoredPosition);
3622 parseRequiresClause();
3623 return true;
3624}
3625
3626/// Parses a requires clause.
3627/// \sa parseRequiresExpression
3628///
3629/// Returns if it either has finished parsing the clause, or it detects, that
3630/// the clause is incorrect.
3631void UnwrappedLineParser::parseRequiresClause() {
3632 assert(FormatTok->is(tok::kw_requires) && "'requires' expected");
3633
3634 // If there is no previous token, we are within a requires expression,
3635 // otherwise we will always have the template or function declaration in front
3636 // of it.
3637 bool InRequiresExpression =
3638 !FormatTok->Previous ||
3639 FormatTok->Previous->is(TT_RequiresExpressionLBrace);
3640
3641 FormatTok->setFinalizedType(InRequiresExpression
3642 ? TT_RequiresClauseInARequiresExpression
3643 : TT_RequiresClause);
3644 nextToken();
3645
3646 // NOTE: parseConstraintExpression is only ever called from this function.
3647 // It could be inlined into here.
3648 parseConstraintExpression();
3649
3650 if (!InRequiresExpression && FormatTok->Previous)
3651 FormatTok->Previous->ClosesRequiresClause = true;
3652}
3653
3654/// Parses a requires expression.
3655/// \sa parseRequiresClause
3656///
3657/// Returns if it either has finished parsing the expression, or it detects,
3658/// that the expression is incorrect.
3659void UnwrappedLineParser::parseRequiresExpression() {
3660 assert(FormatTok->is(tok::kw_requires) && "'requires' expected");
3661
3662 FormatTok->setFinalizedType(TT_RequiresExpression);
3663 nextToken();
3664
3665 if (FormatTok->is(tok::l_paren)) {
3666 FormatTok->setFinalizedType(TT_RequiresExpressionLParen);
3667 parseParens();
3668 }
3669
3670 if (FormatTok->is(tok::l_brace)) {
3671 FormatTok->setFinalizedType(TT_RequiresExpressionLBrace);
3672 parseChildBlock();
3673 }
3674}
3675
3676/// Parses a constraint expression.
3677///
3678/// This is the body of a requires clause. It returns, when the parsing is
3679/// complete, or the expression is incorrect.
3680void UnwrappedLineParser::parseConstraintExpression() {
3681 // The special handling for lambdas is needed since tryToParseLambda() eats a
3682 // token and if a requires expression is the last part of a requires clause
3683 // and followed by an attribute like [[nodiscard]] the ClosesRequiresClause is
3684 // not set on the correct token. Thus we need to be aware if we even expect a
3685 // lambda to be possible.
3686 // template <typename T> requires requires { ... } [[nodiscard]] ...;
3687 bool LambdaNextTimeAllowed = true;
3688
3689 // Within lambda declarations, it is permitted to put a requires clause after
3690 // its template parameter list, which would place the requires clause right
3691 // before the parentheses of the parameters of the lambda declaration. Thus,
3692 // we track if we expect to see grouping parentheses at all.
3693 // Without this check, `requires foo<T> (T t)` in the below example would be
3694 // seen as the whole requires clause, accidentally eating the parameters of
3695 // the lambda.
3696 // [&]<typename T> requires foo<T> (T t) { ... };
3697 bool TopLevelParensAllowed = true;
3698
3699 do {
3700 bool LambdaThisTimeAllowed = std::exchange(LambdaNextTimeAllowed, false);
3701
3702 switch (FormatTok->Tok.getKind()) {
3703 case tok::kw_requires:
3704 parseRequiresExpression();
3705 break;
3706
3707 case tok::l_paren:
3708 if (!TopLevelParensAllowed)
3709 return;
3710 parseParens(/*AmpAmpTokenType=*/TT_BinaryOperator);
3711 TopLevelParensAllowed = false;
3712 break;
3713
3714 case tok::l_square:
3715 if (!LambdaThisTimeAllowed || !tryToParseLambda())
3716 return;
3717 break;
3718
3719 case tok::kw_const:
3720 case tok::semi:
3721 case tok::kw_class:
3722 case tok::kw_struct:
3723 case tok::kw_union:
3724 return;
3725
3726 case tok::l_brace:
3727 // Potential function body.
3728 return;
3729
3730 case tok::ampamp:
3731 case tok::pipepipe:
3732 FormatTok->setFinalizedType(TT_BinaryOperator);
3733 nextToken();
3734 LambdaNextTimeAllowed = true;
3735 TopLevelParensAllowed = true;
3736 break;
3737
3738 case tok::comma:
3739 case tok::comment:
3740 LambdaNextTimeAllowed = LambdaThisTimeAllowed;
3741 nextToken();
3742 break;
3743
3744 case tok::kw_sizeof:
3745 case tok::greater:
3746 case tok::greaterequal:
3747 case tok::greatergreater:
3748 case tok::less:
3749 case tok::lessequal:
3750 case tok::lessless:
3751 case tok::equalequal:
3752 case tok::exclaim:
3753 case tok::exclaimequal:
3754 case tok::plus:
3755 case tok::minus:
3756 case tok::star:
3757 case tok::slash:
3758 LambdaNextTimeAllowed = true;
3759 TopLevelParensAllowed = true;
3760 // Just eat them.
3761 nextToken();
3762 break;
3763
3764 case tok::numeric_constant:
3765 case tok::coloncolon:
3766 case tok::kw_true:
3767 case tok::kw_false:
3768 TopLevelParensAllowed = false;
3769 // Just eat them.
3770 nextToken();
3771 break;
3772
3773 case tok::kw_static_cast:
3774 case tok::kw_const_cast:
3775 case tok::kw_reinterpret_cast:
3776 case tok::kw_dynamic_cast:
3777 nextToken();
3778 if (FormatTok->isNot(tok::less))
3779 return;
3780
3781 nextToken();
3782 parseBracedList(/*IsAngleBracket=*/true);
3783 break;
3784
3785 default:
3786 if (!FormatTok->Tok.getIdentifierInfo()) {
3787 // Identifiers are part of the default case, we check for more then
3788 // tok::identifier to handle builtin type traits.
3789 return;
3790 }
3791
3792 // We need to differentiate identifiers for a template deduction guide,
3793 // variables, or function return types (the constraint expression has
3794 // ended before that), and basically all other cases. But it's easier to
3795 // check the other way around.
3796 assert(FormatTok->Previous);
3797 switch (FormatTok->Previous->Tok.getKind()) {
3798 case tok::coloncolon: // Nested identifier.
3799 case tok::ampamp: // Start of a function or variable for the
3800 case tok::pipepipe: // constraint expression. (binary)
3801 case tok::exclaim: // The same as above, but unary.
3802 case tok::kw_requires: // Initial identifier of a requires clause.
3803 case tok::equal: // Initial identifier of a concept declaration.
3804 break;
3805 default:
3806 return;
3807 }
3808
3809 // Read identifier with optional template declaration.
3810 nextToken();
3811 if (FormatTok->is(tok::less)) {
3812 nextToken();
3813 parseBracedList(/*IsAngleBracket=*/true);
3814 }
3815 TopLevelParensAllowed = false;
3816 break;
3817 }
3818 } while (!eof());
3819}
3820
3821bool UnwrappedLineParser::parseEnum() {
3822 const FormatToken &InitialToken = *FormatTok;
3823
3824 // Won't be 'enum' for NS_ENUMs.
3825 if (FormatTok->is(tok::kw_enum))
3826 nextToken();
3827
3828 // In TypeScript, "enum" can also be used as property name, e.g. in interface
3829 // declarations. An "enum" keyword followed by a colon would be a syntax
3830 // error and thus assume it is just an identifier.
3831 if (Style.isJavaScript() && FormatTok->isOneOf(tok::colon, tok::question))
3832 return false;
3833
3834 // In protobuf, "enum" can be used as a field name.
3835 if (Style.Language == FormatStyle::LK_Proto && FormatTok->is(tok::equal))
3836 return false;
3837
3838 if (IsCpp) {
3839 // Eat up enum class ...
3840 if (FormatTok->isOneOf(tok::kw_class, tok::kw_struct))
3841 nextToken();
3842 while (FormatTok->is(tok::l_square))
3843 if (!handleCppAttributes())
3844 return false;
3845 }
3846
3847 while (FormatTok->Tok.getIdentifierInfo() ||
3848 FormatTok->isOneOf(tok::colon, tok::coloncolon, tok::less,
3849 tok::greater, tok::comma, tok::question,
3850 tok::l_square)) {
3851 if (FormatTok->is(tok::colon))
3852 FormatTok->setFinalizedType(TT_EnumUnderlyingTypeColon);
3853 if (Style.isVerilog()) {
3854 FormatTok->setFinalizedType(TT_VerilogDimensionedTypeName);
3855 nextToken();
3856 // In Verilog the base type can have dimensions.
3857 while (FormatTok->is(tok::l_square))
3858 parseSquare();
3859 } else {
3860 nextToken();
3861 }
3862 // We can have macros or attributes in between 'enum' and the enum name.
3863 if (FormatTok->is(tok::l_paren))
3864 parseParens();
3865 if (FormatTok->is(tok::identifier)) {
3866 nextToken();
3867 // If there are two identifiers in a row, this is likely an elaborate
3868 // return type. In Java, this can be "implements", etc.
3869 if (IsCpp && FormatTok->is(tok::identifier))
3870 return false;
3871 }
3872 }
3873
3874 // Just a declaration or something is wrong.
3875 if (FormatTok->isNot(tok::l_brace))
3876 return true;
3877 FormatTok->setFinalizedType(TT_EnumLBrace);
3878 FormatTok->setBlockKind(BK_Block);
3879
3880 if (Style.isJava()) {
3881 // Java enums are different.
3882 parseJavaEnumBody();
3883 return true;
3884 }
3885 if (Style.Language == FormatStyle::LK_Proto) {
3886 parseBlock(/*MustBeDeclaration=*/true);
3887 return true;
3888 }
3889
3890 const bool ManageWhitesmithsBraces =
3891 Style.BreakBeforeBraces == FormatStyle::BS_Whitesmiths;
3892
3893 if (!Style.AllowShortEnumsOnASingleLine &&
3894 ShouldBreakBeforeBrace(Style, InitialToken,
3895 Tokens->peekNextToken()->is(tok::r_brace))) {
3896 addUnwrappedLine();
3897
3898 // If we're in Whitesmiths mode, indent the brace if we're not indenting
3899 // the whole block.
3900 if (ManageWhitesmithsBraces)
3901 ++Line->Level;
3902 }
3903 // Parse enum body.
3904 nextToken();
3905 if (!Style.AllowShortEnumsOnASingleLine) {
3906 addUnwrappedLine();
3907 if (!ManageWhitesmithsBraces)
3908 ++Line->Level;
3909 }
3910 const auto OpeningLineIndex = CurrentLines->empty()
3911 ? UnwrappedLine::kInvalidIndex
3912 : CurrentLines->size() - 1;
3913 bool HasError = !parseBracedList(/*IsAngleBracket=*/false, /*IsEnum=*/true);
3914 if (!Style.AllowShortEnumsOnASingleLine && !ManageWhitesmithsBraces)
3915 --Line->Level;
3916 if (HasError) {
3917 if (FormatTok->is(tok::semi))
3918 nextToken();
3919 addUnwrappedLine();
3920 }
3921 setPreviousRBraceType(TT_EnumRBrace);
3922 if (ManageWhitesmithsBraces)
3923 Line->MatchingOpeningBlockLineIndex = OpeningLineIndex;
3924 return true;
3925
3926 // There is no addUnwrappedLine() here so that we fall through to parsing a
3927 // structural element afterwards. Thus, in "enum A {} n, m;",
3928 // "} n, m;" will end up in one unwrapped line.
3929}
3930
3931bool UnwrappedLineParser::parseStructLike() {
3932 // parseRecord falls through and does not yet add an unwrapped line as a
3933 // record declaration or definition can start a structural element.
3934 parseRecord();
3935 // This does not apply to Java, JavaScript and C#.
3936 if (Style.isJava() || Style.isJavaScript() || Style.isCSharp()) {
3937 if (FormatTok->is(tok::semi))
3938 nextToken();
3939 addUnwrappedLine();
3940 return true;
3941 }
3942 return false;
3943}
3944
3945namespace {
3946// A class used to set and restore the Token position when peeking
3947// ahead in the token source.
3948class ScopedTokenPosition {
3949 unsigned StoredPosition;
3950 FormatTokenSource *Tokens;
3951
3952public:
3953 ScopedTokenPosition(FormatTokenSource *Tokens) : Tokens(Tokens) {
3954 assert(Tokens && "Tokens expected to not be null");
3955 StoredPosition = Tokens->getPosition();
3956 }
3957
3958 ~ScopedTokenPosition() { Tokens->setPosition(StoredPosition); }
3959};
3960} // namespace
3961
3962// Look to see if we have [[ by looking ahead, if
3963// its not then rewind to the original position.
3964bool UnwrappedLineParser::tryToParseSimpleAttribute() {
3965 ScopedTokenPosition AutoPosition(Tokens);
3966 FormatToken *Tok = Tokens->getNextToken();
3967 // We already read the first [ check for the second.
3968 if (Tok->isNot(tok::l_square))
3969 return false;
3970 // Double check that the attribute is just something
3971 // fairly simple.
3972 while (Tok->isNot(tok::eof)) {
3973 if (Tok->is(tok::r_square))
3974 break;
3975 Tok = Tokens->getNextToken();
3976 }
3977 if (Tok->is(tok::eof))
3978 return false;
3979 Tok = Tokens->getNextToken();
3980 if (Tok->isNot(tok::r_square))
3981 return false;
3982 Tok = Tokens->getNextToken();
3983 if (Tok->is(tok::semi))
3984 return false;
3985 return true;
3986}
3987
3988void UnwrappedLineParser::parseJavaEnumBody() {
3989 assert(FormatTok->is(tok::l_brace));
3990 const FormatToken *OpeningBrace = FormatTok;
3991
3992 // Determine whether the enum is simple, i.e. does not have a semicolon or
3993 // constants with class bodies. Simple enums can be formatted like braced
3994 // lists, contracted to a single line, etc.
3995 unsigned StoredPosition = Tokens->getPosition();
3996 bool IsSimple = true;
3997 FormatToken *Tok = Tokens->getNextToken();
3998 while (Tok->isNot(tok::eof)) {
3999 if (Tok->is(tok::r_brace))
4000 break;
4001 if (Tok->isOneOf(tok::l_brace, tok::semi)) {
4002 IsSimple = false;
4003 break;
4004 }
4005 // FIXME: This will also mark enums with braces in the arguments to enum
4006 // constants as "not simple". This is probably fine in practice, though.
4007 Tok = Tokens->getNextToken();
4008 }
4009 FormatTok = Tokens->setPosition(StoredPosition);
4010
4011 if (IsSimple) {
4012 nextToken();
4013 parseBracedList();
4014 addUnwrappedLine();
4015 return;
4016 }
4017
4018 // Parse the body of a more complex enum.
4019 // First add a line for everything up to the "{".
4020 nextToken();
4021 addUnwrappedLine();
4022 ++Line->Level;
4023
4024 // Parse the enum constants.
4025 while (!eof()) {
4026 if (FormatTok->is(tok::l_brace)) {
4027 // Parse the constant's class body.
4028 parseBlock(/*MustBeDeclaration=*/true, /*AddLevels=*/1u,
4029 /*MunchSemi=*/false);
4030 } else if (FormatTok->is(tok::l_paren)) {
4031 parseParens();
4032 } else if (FormatTok->is(tok::comma)) {
4033 nextToken();
4034 addUnwrappedLine();
4035 } else if (FormatTok->is(tok::semi)) {
4036 nextToken();
4037 addUnwrappedLine();
4038 break;
4039 } else if (FormatTok->is(tok::r_brace)) {
4040 addUnwrappedLine();
4041 break;
4042 } else {
4043 nextToken();
4044 }
4045 }
4046
4047 // Parse the class body after the enum's ";" if any.
4048 parseLevel(OpeningBrace);
4049 nextToken();
4050 --Line->Level;
4051 addUnwrappedLine();
4052}
4053
4054void UnwrappedLineParser::parseRecord(bool ParseAsExpr, bool IsJavaRecord) {
4055 assert(!IsJavaRecord || FormatTok->is(Keywords.kw_record));
4056 const FormatToken &InitialToken = *FormatTok;
4057 nextToken();
4058
4059 FormatToken *ClassName =
4060 IsJavaRecord && FormatTok->is(tok::identifier) ? FormatTok : nullptr;
4061 bool IsDerived = false;
4062 auto IsNonMacroIdentifier = [](const FormatToken *Tok) {
4063 return Tok->is(tok::identifier) && Tok->TokenText != Tok->TokenText.upper();
4064 };
4065 // JavaScript/TypeScript supports anonymous classes like:
4066 // a = class extends foo { }
4067 bool JSPastExtendsOrImplements = false;
4068 // The actual identifier can be a nested name specifier, and in macros
4069 // it is often token-pasted.
4070 // An [[attribute]] can be before the identifier.
4071 while (FormatTok->isOneOf(tok::identifier, tok::coloncolon, tok::hashhash,
4072 tok::kw_alignas, tok::l_square) ||
4073 FormatTok->isAttribute() ||
4074 ((Style.isJava() || Style.isJavaScript()) &&
4075 FormatTok->isOneOf(tok::period, tok::comma))) {
4076 if (Style.isJavaScript() &&
4077 FormatTok->isOneOf(Keywords.kw_extends, Keywords.kw_implements)) {
4078 JSPastExtendsOrImplements = true;
4079 // JavaScript/TypeScript supports inline object types in
4080 // extends/implements positions:
4081 // class Foo implements {bar: number} { }
4082 nextToken();
4083 if (FormatTok->is(tok::l_brace)) {
4084 tryToParseBracedList();
4085 continue;
4086 }
4087 }
4088 if (FormatTok->is(tok::l_square) && handleCppAttributes())
4089 continue;
4090 auto *Previous = FormatTok;
4091 nextToken();
4092 switch (FormatTok->Tok.getKind()) {
4093 case tok::l_paren:
4094 // We can have macros in between 'class' and the class name.
4095 if (IsJavaRecord || !IsNonMacroIdentifier(Previous) ||
4096 // e.g. `struct macro(a) S { int i; };`
4097 Previous->Previous == &InitialToken) {
4098 parseParens();
4099 }
4100 break;
4101 case tok::coloncolon:
4102 case tok::hashhash:
4103 break;
4104 default:
4105 if (JSPastExtendsOrImplements || ClassName ||
4106 Previous->isNot(tok::identifier) || Previous->is(TT_AttributeMacro)) {
4107 break;
4108 }
4109 if (const auto Text = Previous->TokenText;
4110 Text.size() == 1 || Text != Text.upper()) {
4111 ClassName = Previous;
4112 }
4113 }
4114 }
4115
4116 auto IsListInitialization = [&] {
4117 if (!ClassName || IsDerived || JSPastExtendsOrImplements)
4118 return false;
4119 assert(FormatTok->is(tok::l_brace));
4120 const auto *Prev = FormatTok->getPreviousNonComment();
4121 assert(Prev);
4122 return Prev != ClassName && Prev->is(tok::identifier) &&
4123 Prev->isNot(Keywords.kw_final) && tryToParseBracedList();
4124 };
4125
4126 if (FormatTok->isOneOf(tok::colon, tok::less)) {
4127 int AngleNestingLevel = 0;
4128 do {
4129 if (FormatTok->is(tok::less))
4130 ++AngleNestingLevel;
4131 else if (FormatTok->is(tok::greater))
4132 --AngleNestingLevel;
4133
4134 if (AngleNestingLevel == 0) {
4135 if (FormatTok->is(tok::colon)) {
4136 IsDerived = true;
4137 } else if (!IsDerived && FormatTok->is(tok::identifier) &&
4138 FormatTok->Previous->is(tok::coloncolon)) {
4139 ClassName = FormatTok;
4140 } else if (FormatTok->is(tok::l_paren) &&
4141 IsNonMacroIdentifier(FormatTok->Previous)) {
4142 break;
4143 }
4144 }
4145 if (FormatTok->is(tok::l_brace)) {
4146 if (AngleNestingLevel == 0 && IsListInitialization())
4147 return;
4148 calculateBraceTypes(/*ExpectClassBody=*/true);
4149 if (!tryToParseBracedList())
4150 break;
4151 }
4152 if (FormatTok->is(tok::l_square)) {
4153 FormatToken *Previous = FormatTok->Previous;
4154 if (!Previous || (Previous->isNot(tok::r_paren) &&
4155 !Previous->isTypeOrIdentifier(LangOpts))) {
4156 // Don't try parsing a lambda if we had a closing parenthesis before,
4157 // it was probably a pointer to an array: int (*)[].
4158 if (!tryToParseLambda())
4159 continue;
4160 } else {
4161 parseSquare();
4162 continue;
4163 }
4164 }
4165 if (FormatTok->is(tok::semi))
4166 return;
4167 if (Style.isCSharp() && FormatTok->is(Keywords.kw_where)) {
4168 addUnwrappedLine();
4169 nextToken();
4170 parseCSharpGenericTypeConstraint();
4171 break;
4172 }
4173 nextToken();
4174 } while (!eof());
4175 }
4176
4177 auto GetBraceTypes =
4178 [](const FormatToken &RecordTok) -> std::pair<TokenType, TokenType> {
4179 switch (RecordTok.Tok.getKind()) {
4180 case tok::kw_class:
4181 return {TT_ClassLBrace, TT_ClassRBrace};
4182 case tok::kw_struct:
4183 return {TT_StructLBrace, TT_StructRBrace};
4184 case tok::kw_union:
4185 return {TT_UnionLBrace, TT_UnionRBrace};
4186 default:
4187 // Useful for e.g. interface.
4188 return {TT_RecordLBrace, TT_RecordRBrace};
4189 }
4190 };
4191 if (FormatTok->is(tok::l_brace)) {
4192 if (IsListInitialization())
4193 return;
4194 if (ClassName)
4195 ClassName->setFinalizedType(TT_ClassHeadName);
4196 auto [OpenBraceType, ClosingBraceType] = GetBraceTypes(InitialToken);
4197 FormatTok->setFinalizedType(OpenBraceType);
4198 if (ParseAsExpr) {
4199 parseChildBlock();
4200 } else {
4201 if (ShouldBreakBeforeBrace(Style, InitialToken,
4202 Tokens->peekNextToken()->is(tok::r_brace),
4203 IsJavaRecord)) {
4204 addUnwrappedLine();
4205 }
4206
4207 unsigned AddLevels = Style.IndentAccessModifiers ? 2u : 1u;
4208 parseBlock(/*MustBeDeclaration=*/true, AddLevels, /*MunchSemi=*/false);
4209 }
4210 setPreviousRBraceType(ClosingBraceType);
4211 }
4212 // There is no addUnwrappedLine() here so that we fall through to parsing a
4213 // structural element afterwards. Thus, in "class A {} n, m;",
4214 // "} n, m;" will end up in one unwrapped line.
4215}
4216
4217void UnwrappedLineParser::parseObjCMethod() {
4218 assert(FormatTok->isOneOf(tok::l_paren, tok::identifier) &&
4219 "'(' or identifier expected.");
4220 do {
4221 if (FormatTok->is(tok::semi)) {
4222 nextToken();
4223 addUnwrappedLine();
4224 return;
4225 } else if (FormatTok->is(tok::l_brace)) {
4226 if (Style.BraceWrapping.AfterFunction)
4227 addUnwrappedLine();
4228 parseBlock();
4229 addUnwrappedLine();
4230 return;
4231 } else {
4232 nextToken();
4233 }
4234 } while (!eof());
4235}
4236
4237void UnwrappedLineParser::parseObjCProtocolList() {
4238 assert(FormatTok->is(tok::less) && "'<' expected.");
4239 do {
4240 nextToken();
4241 // Early exit in case someone forgot a close angle.
4242 if (FormatTok->isOneOf(tok::semi, tok::l_brace, tok::objc_end))
4243 return;
4244 } while (!eof() && FormatTok->isNot(tok::greater));
4245 nextToken(); // Skip '>'.
4246}
4247
4248void UnwrappedLineParser::parseObjCUntilAtEnd() {
4249 do {
4250 if (FormatTok->is(tok::objc_end)) {
4251 nextToken();
4252 addUnwrappedLine();
4253 break;
4254 }
4255 if (FormatTok->is(tok::l_brace)) {
4256 parseBlock();
4257 // In ObjC interfaces, nothing should be following the "}".
4258 addUnwrappedLine();
4259 } else if (FormatTok->is(tok::r_brace)) {
4260 // Ignore stray "}". parseStructuralElement doesn't consume them.
4261 nextToken();
4262 addUnwrappedLine();
4263 } else if (FormatTok->isOneOf(tok::minus, tok::plus)) {
4264 nextToken();
4265 parseObjCMethod();
4266 } else {
4267 parseStructuralElement();
4268 }
4269 } while (!eof());
4270}
4271
4272void UnwrappedLineParser::parseObjCInterfaceOrImplementation() {
4273 assert(FormatTok->isOneOf(tok::objc_interface, tok::objc_implementation));
4274 nextToken();
4275 nextToken(); // interface name
4276
4277 // @interface can be followed by a lightweight generic
4278 // specialization list, then either a base class or a category.
4279 if (FormatTok->is(tok::less))
4280 parseObjCLightweightGenerics();
4281 if (FormatTok->is(tok::colon)) {
4282 nextToken();
4283 nextToken(); // base class name
4284 // The base class can also have lightweight generics applied to it.
4285 if (FormatTok->is(tok::less))
4286 parseObjCLightweightGenerics();
4287 } else if (FormatTok->is(tok::l_paren)) {
4288 // Skip category, if present.
4289 parseParens();
4290 }
4291
4292 if (FormatTok->is(tok::less))
4293 parseObjCProtocolList();
4294
4295 if (FormatTok->is(tok::l_brace)) {
4296 if (Style.BraceWrapping.AfterObjCDeclaration)
4297 addUnwrappedLine();
4298 parseBlock(/*MustBeDeclaration=*/true);
4299 }
4300
4301 // With instance variables, this puts '}' on its own line. Without instance
4302 // variables, this ends the @interface line.
4303 addUnwrappedLine();
4304
4305 parseObjCUntilAtEnd();
4306}
4307
4308void UnwrappedLineParser::parseObjCLightweightGenerics() {
4309 assert(FormatTok->is(tok::less));
4310 // Unlike protocol lists, generic parameterizations support
4311 // nested angles:
4312 //
4313 // @interface Foo<ValueType : id <NSCopying, NSSecureCoding>> :
4314 // NSObject <NSCopying, NSSecureCoding>
4315 //
4316 // so we need to count how many open angles we have left.
4317 unsigned NumOpenAngles = 1;
4318 do {
4319 nextToken();
4320 // Early exit in case someone forgot a close angle.
4321 if (FormatTok->isOneOf(tok::semi, tok::l_brace, tok::objc_end))
4322 break;
4323 if (FormatTok->is(tok::less)) {
4324 ++NumOpenAngles;
4325 } else if (FormatTok->is(tok::greater)) {
4326 assert(NumOpenAngles > 0 && "'>' makes NumOpenAngles negative");
4327 --NumOpenAngles;
4328 }
4329 } while (!eof() && NumOpenAngles != 0);
4330 nextToken(); // Skip '>'.
4331}
4332
4333// Returns true for the declaration/definition form of @protocol,
4334// false for the expression form.
4335bool UnwrappedLineParser::parseObjCProtocol() {
4336 assert(FormatTok->is(tok::objc_protocol));
4337 nextToken();
4338
4339 if (FormatTok->is(tok::l_paren)) {
4340 // The expression form of @protocol, e.g. "Protocol* p = @protocol(foo);".
4341 return false;
4342 }
4343
4344 // The definition/declaration form,
4345 // @protocol Foo
4346 // - (int)someMethod;
4347 // @end
4348
4349 nextToken(); // protocol name
4350
4351 if (FormatTok->is(tok::less))
4352 parseObjCProtocolList();
4353
4354 // Check for protocol declaration.
4355 if (FormatTok->is(tok::semi)) {
4356 nextToken();
4357 addUnwrappedLine();
4358 return true;
4359 }
4360
4361 addUnwrappedLine();
4362 parseObjCUntilAtEnd();
4363 return true;
4364}
4365
4366void UnwrappedLineParser::parseJavaScriptEs6ImportExport() {
4367 bool IsImport = FormatTok->is(Keywords.kw_import);
4368 assert(IsImport || FormatTok->is(tok::kw_export));
4369 nextToken();
4370
4371 // Consume the "default" in "export default class/function".
4372 if (FormatTok->is(tok::kw_default))
4373 nextToken();
4374
4375 // Consume "async function", "function" and "default function", so that these
4376 // get parsed as free-standing JS functions, i.e. do not require a trailing
4377 // semicolon.
4378 if (FormatTok->is(Keywords.kw_async))
4379 nextToken();
4380 if (FormatTok->is(Keywords.kw_function)) {
4381 nextToken();
4382 return;
4383 }
4384
4385 // For imports, `export *`, `export {...}`, consume the rest of the line up
4386 // to the terminating `;`. For everything else, just return and continue
4387 // parsing the structural element, i.e. the declaration or expression for
4388 // `export default`.
4389 if (!IsImport && FormatTok->isNoneOf(tok::l_brace, tok::star) &&
4390 !FormatTok->isStringLiteral() &&
4391 !(FormatTok->is(Keywords.kw_type) &&
4392 Tokens->peekNextToken()->isOneOf(tok::l_brace, tok::star))) {
4393 return;
4394 }
4395
4396 while (!eof()) {
4397 if (FormatTok->is(tok::semi))
4398 return;
4399 if (Line->Tokens.empty()) {
4400 // Common issue: Automatic Semicolon Insertion wrapped the line, so the
4401 // import statement should terminate.
4402 return;
4403 }
4404 if (FormatTok->is(tok::l_brace)) {
4405 FormatTok->setBlockKind(BK_Block);
4406 nextToken();
4407 parseBracedList();
4408 } else {
4409 nextToken();
4410 }
4411 }
4412}
4413
4414void UnwrappedLineParser::parseStatementMacro() {
4415 nextToken();
4416 if (FormatTok->is(tok::l_paren))
4417 parseParens();
4418 if (FormatTok->is(tok::semi))
4419 nextToken();
4420 addUnwrappedLine();
4421}
4422
4423void UnwrappedLineParser::parseVerilogHierarchyIdentifier() {
4424 // consume things like a::`b.c[d:e] or a::*
4425 while (true) {
4426 if (FormatTok->isOneOf(tok::star, tok::period, tok::periodstar,
4427 tok::coloncolon, tok::hash) ||
4428 Keywords.isVerilogIdentifier(*FormatTok)) {
4429 nextToken();
4430 } else if (FormatTok->is(tok::l_square)) {
4431 parseSquare();
4432 } else {
4433 break;
4434 }
4435 }
4436}
4437
4438void UnwrappedLineParser::parseVerilogSensitivityList() {
4439 if (FormatTok->isNot(tok::at))
4440 return;
4441 nextToken();
4442 // A block event expression has 2 at signs.
4443 if (FormatTok->is(tok::at))
4444 nextToken();
4445 switch (FormatTok->Tok.getKind()) {
4446 case tok::star:
4447 nextToken();
4448 break;
4449 case tok::l_paren:
4450 parseParens();
4451 break;
4452 default:
4453 parseVerilogHierarchyIdentifier();
4454 break;
4455 }
4456}
4457
4458unsigned UnwrappedLineParser::parseVerilogHierarchyHeader() {
4459 unsigned AddLevels = 0;
4460
4461 if (FormatTok->is(Keywords.kw_clocking)) {
4462 nextToken();
4463 if (Keywords.isVerilogIdentifier(*FormatTok))
4464 nextToken();
4465 parseVerilogSensitivityList();
4466 if (FormatTok->is(tok::semi))
4467 nextToken();
4468 } else if (FormatTok->isOneOf(tok::kw_case, Keywords.kw_casex,
4469 Keywords.kw_casez, Keywords.kw_randcase,
4470 Keywords.kw_randsequence)) {
4471 if (Style.IndentCaseLabels)
4472 AddLevels++;
4473 nextToken();
4474 if (FormatTok->is(tok::l_paren)) {
4475 FormatTok->setFinalizedType(TT_ConditionLParen);
4476 parseParens();
4477 }
4478 if (FormatTok->isOneOf(Keywords.kw_inside, Keywords.kw_matches))
4479 nextToken();
4480 // The case header has no semicolon.
4481 } else {
4482 // "module" etc.
4483 nextToken();
4484 // all the words like the name of the module and specifiers like
4485 // "automatic" and the width of function return type
4486 while (true) {
4487 if (FormatTok->is(tok::l_square)) {
4488 auto Prev = FormatTok->getPreviousNonComment();
4489 if (Prev && Keywords.isVerilogIdentifier(*Prev))
4490 Prev->setFinalizedType(TT_VerilogDimensionedTypeName);
4491 parseSquare();
4492 } else if (Keywords.isVerilogIdentifier(*FormatTok) ||
4493 FormatTok->isOneOf(tok::hash, tok::hashhash, tok::coloncolon,
4494 Keywords.kw_automatic, tok::kw_static)) {
4495 nextToken();
4496 } else {
4497 break;
4498 }
4499 }
4500
4501 auto NewLine = [this]() {
4502 addUnwrappedLine();
4503 Line->IsContinuation = true;
4504 };
4505
4506 // package imports
4507 while (FormatTok->is(Keywords.kw_import)) {
4508 NewLine();
4509 nextToken();
4510 parseVerilogHierarchyIdentifier();
4511 if (FormatTok->is(tok::semi))
4512 nextToken();
4513 }
4514
4515 // parameters and ports
4516 if (FormatTok->is(Keywords.kw_verilogHash)) {
4517 NewLine();
4518 nextToken();
4519 if (FormatTok->is(tok::l_paren)) {
4520 FormatTok->setFinalizedType(TT_VerilogMultiLineListLParen);
4521 parseParens();
4522 }
4523 }
4524 if (FormatTok->is(tok::l_paren)) {
4525 NewLine();
4526 FormatTok->setFinalizedType(TT_VerilogMultiLineListLParen);
4527 parseParens();
4528 }
4529
4530 // extends and implements
4531 if (FormatTok->is(Keywords.kw_extends)) {
4532 NewLine();
4533 nextToken();
4534 parseVerilogHierarchyIdentifier();
4535 if (FormatTok->is(tok::l_paren))
4536 parseParens();
4537 }
4538 if (FormatTok->is(Keywords.kw_implements)) {
4539 NewLine();
4540 do {
4541 nextToken();
4542 parseVerilogHierarchyIdentifier();
4543 } while (FormatTok->is(tok::comma));
4544 }
4545
4546 // Coverage event for cover groups.
4547 if (FormatTok->is(tok::at)) {
4548 NewLine();
4549 parseVerilogSensitivityList();
4550 }
4551
4552 if (FormatTok->is(tok::semi))
4553 nextToken(/*LevelDifference=*/1);
4554 addUnwrappedLine();
4555 }
4556
4557 return AddLevels;
4558}
4559
4560void UnwrappedLineParser::parseVerilogTable() {
4561 assert(FormatTok->is(Keywords.kw_table));
4562 nextToken(/*LevelDifference=*/1);
4563 addUnwrappedLine();
4564
4565 auto InitialLevel = Line->Level++;
4566 while (!eof() && !Keywords.isVerilogEnd(*FormatTok)) {
4567 FormatToken *Tok = FormatTok;
4568 nextToken();
4569 if (Tok->is(tok::semi))
4570 addUnwrappedLine();
4571 else if (Tok->isOneOf(tok::star, tok::colon, tok::question, tok::minus))
4572 Tok->setFinalizedType(TT_VerilogTableItem);
4573 }
4574 Line->Level = InitialLevel;
4575 nextToken(/*LevelDifference=*/-1);
4576 addUnwrappedLine();
4577}
4578
4579void UnwrappedLineParser::parseVerilogCaseLabel() {
4580 // The label will get unindented in AnnotatingParser. If there are no leading
4581 // spaces, indent the rest here so that things inside the block will be
4582 // indented relative to things outside. We don't use parseLabel because we
4583 // don't know whether this colon is a label or a ternary expression at this
4584 // point.
4585 auto OrigLevel = Line->Level;
4586 auto FirstLine = CurrentLines->size();
4587 if (Line->Level == 0 || (Line->InPPDirective && Line->Level <= 1))
4588 ++Line->Level;
4589 else if (!Style.IndentCaseBlocks && Keywords.isVerilogBegin(*FormatTok))
4590 --Line->Level;
4591 parseStructuralElement();
4592 // Restore the indentation in both the new line and the line that has the
4593 // label.
4594 if (CurrentLines->size() > FirstLine)
4595 (*CurrentLines)[FirstLine].Level = OrigLevel;
4596 Line->Level = OrigLevel;
4597}
4598
4599void UnwrappedLineParser::parseVerilogExtern() {
4600 assert(
4601 FormatTok->isOneOf(tok::kw_extern, tok::kw_export, Keywords.kw_import));
4602 nextToken();
4603 // "DPI-C"
4604 if (FormatTok->is(tok::string_literal))
4605 nextToken();
4606 if (FormatTok->isOneOf(Keywords.kw_context, Keywords.kw_pure))
4607 nextToken();
4608 if (Keywords.isVerilogIdentifier(*FormatTok))
4609 nextToken();
4610 if (FormatTok->is(tok::equal))
4611 nextToken();
4612 if (Keywords.isVerilogHierarchy(*FormatTok))
4613 parseVerilogHierarchyHeader();
4614}
4615
4616bool UnwrappedLineParser::containsExpansion(const UnwrappedLine &Line) const {
4617 for (const auto &N : Line.Tokens) {
4618 if (N.Tok->MacroCtx)
4619 return true;
4620 for (const UnwrappedLine &Child : N.Children)
4621 if (containsExpansion(Child))
4622 return true;
4623 }
4624 return false;
4625}
4626
4627void UnwrappedLineParser::addUnwrappedLine(LineLevel AdjustLevel) {
4628 if (Line->Tokens.empty())
4629 return;
4630 LLVM_DEBUG({
4631 if (!parsingPPDirective()) {
4632 llvm::dbgs() << "Adding unwrapped line:\n";
4633 printDebugInfo(*Line);
4634 }
4635 });
4636
4637 // If this line closes a block when in Whitesmiths mode, remember that
4638 // information so that the level can be decreased after the line is added.
4639 // This has to happen after the addition of the line since the line itself
4640 // needs to be indented.
4641 bool ClosesWhitesmithsBlock =
4642 Line->MatchingOpeningBlockLineIndex != UnwrappedLine::kInvalidIndex &&
4643 Style.BreakBeforeBraces == FormatStyle::BS_Whitesmiths;
4644
4645 // If the current line was expanded from a macro call, we use it to
4646 // reconstruct an unwrapped line from the structure of the expanded unwrapped
4647 // line and the unexpanded token stream.
4648 if (!parsingPPDirective() && !InExpansion && containsExpansion(*Line)) {
4649 if (!Reconstruct)
4650 Reconstruct.emplace(Line->Level, Unexpanded);
4651 Reconstruct->addLine(*Line);
4652
4653 // While the reconstructed unexpanded lines are stored in the normal
4654 // flow of lines, the expanded lines are stored on the side to be analyzed
4655 // in an extra step.
4656 CurrentExpandedLines.push_back(std::move(*Line));
4657
4658 if (Reconstruct->finished()) {
4659 UnwrappedLine Reconstructed = std::move(*Reconstruct).takeResult();
4660 assert(!Reconstructed.Tokens.empty() &&
4661 "Reconstructed must at least contain the macro identifier.");
4662 assert(!parsingPPDirective());
4663 LLVM_DEBUG({
4664 llvm::dbgs() << "Adding unexpanded line:\n";
4665 printDebugInfo(Reconstructed);
4666 });
4667 ExpandedLines[Reconstructed.Tokens.begin()->Tok] = CurrentExpandedLines;
4668 Lines.push_back(std::move(Reconstructed));
4669 CurrentExpandedLines.clear();
4670 Reconstruct.reset();
4671 }
4672 } else {
4673 // At the top level we only get here when no unexpansion is going on, or
4674 // when conditional formatting led to unfinished macro reconstructions.
4675 assert(!Reconstruct || (CurrentLines != &Lines) || !PPStack.empty());
4676 CurrentLines->push_back(std::move(*Line));
4677 }
4678 Line->Tokens.clear();
4679 Line->MatchingOpeningBlockLineIndex = UnwrappedLine::kInvalidIndex;
4680 Line->FirstStartColumn = 0;
4681 Line->IsContinuation = false;
4682 Line->SeenDecltypeAuto = false;
4683
4684 if (ClosesWhitesmithsBlock && AdjustLevel == LineLevel::Remove)
4685 --Line->Level;
4686 if (!parsingPPDirective() && !PreprocessorDirectives.empty()) {
4687 CurrentLines->append(
4688 std::make_move_iterator(PreprocessorDirectives.begin()),
4689 std::make_move_iterator(PreprocessorDirectives.end()));
4690 PreprocessorDirectives.clear();
4691 }
4692 // Disconnect the current token from the last token on the previous line.
4693 FormatTok->Previous = nullptr;
4694}
4695
4696bool UnwrappedLineParser::eof() const { return FormatTok->is(tok::eof); }
4697
4698bool UnwrappedLineParser::isOnNewLine(const FormatToken &FormatTok) {
4699 return (Line->InPPDirective || FormatTok.HasUnescapedNewline) &&
4700 FormatTok.NewlinesBefore > 0;
4701}
4702
4703// Checks if \p FormatTok is a line comment that continues the line comment
4704// section on \p Line.
4705static bool
4707 const UnwrappedLine &Line, const FormatStyle &Style,
4708 const llvm::Regex &CommentPragmasRegex) {
4709 if (Line.Tokens.empty() || Style.ReflowComments != FormatStyle::RCS_Always)
4710 return false;
4711
4712 StringRef IndentContent = FormatTok.TokenText;
4713 if (FormatTok.TokenText.starts_with("//") ||
4714 FormatTok.TokenText.starts_with("/*")) {
4715 IndentContent = FormatTok.TokenText.substr(2);
4716 }
4717 if (CommentPragmasRegex.match(IndentContent))
4718 return false;
4719
4720 // If Line starts with a line comment, then FormatTok continues the comment
4721 // section if its original column is greater or equal to the original start
4722 // column of the line.
4723 //
4724 // Define the min column token of a line as follows: if a line ends in '{' or
4725 // contains a '{' followed by a line comment, then the min column token is
4726 // that '{'. Otherwise, the min column token of the line is the first token of
4727 // the line.
4728 //
4729 // If Line starts with a token other than a line comment, then FormatTok
4730 // continues the comment section if its original column is greater than the
4731 // original start column of the min column token of the line.
4732 //
4733 // For example, the second line comment continues the first in these cases:
4734 //
4735 // // first line
4736 // // second line
4737 //
4738 // and:
4739 //
4740 // // first line
4741 // // second line
4742 //
4743 // and:
4744 //
4745 // int i; // first line
4746 // // second line
4747 //
4748 // and:
4749 //
4750 // do { // first line
4751 // // second line
4752 // int i;
4753 // } while (true);
4754 //
4755 // and:
4756 //
4757 // enum {
4758 // a, // first line
4759 // // second line
4760 // b
4761 // };
4762 //
4763 // The second line comment doesn't continue the first in these cases:
4764 //
4765 // // first line
4766 // // second line
4767 //
4768 // and:
4769 //
4770 // int i; // first line
4771 // // second line
4772 //
4773 // and:
4774 //
4775 // do { // first line
4776 // // second line
4777 // int i;
4778 // } while (true);
4779 //
4780 // and:
4781 //
4782 // enum {
4783 // a, // first line
4784 // // second line
4785 // };
4786 const FormatToken *MinColumnToken = Line.Tokens.front().Tok;
4787
4788 // Scan for '{//'. If found, use the column of '{' as a min column for line
4789 // comment section continuation.
4790 const FormatToken *PreviousToken = nullptr;
4791 for (const UnwrappedLineNode &Node : Line.Tokens) {
4792 if (PreviousToken && PreviousToken->is(tok::l_brace) &&
4793 isLineComment(*Node.Tok)) {
4794 MinColumnToken = PreviousToken;
4795 break;
4796 }
4797 PreviousToken = Node.Tok;
4798
4799 // Grab the last newline preceding a token in this unwrapped line.
4800 if (Node.Tok->NewlinesBefore > 0)
4801 MinColumnToken = Node.Tok;
4802 }
4803 if (PreviousToken && PreviousToken->is(tok::l_brace))
4804 MinColumnToken = PreviousToken;
4805
4806 return continuesLineComment(FormatTok, /*Previous=*/Line.Tokens.back().Tok,
4807 MinColumnToken);
4808}
4809
4810void UnwrappedLineParser::flushComments(bool NewlineBeforeNext) {
4811 bool JustComments = Line->Tokens.empty();
4812 for (FormatToken *Tok : CommentsBeforeNextToken) {
4813 // Line comments that belong to the same line comment section are put on the
4814 // same line since later we might want to reflow content between them.
4815 // Additional fine-grained breaking of line comment sections is controlled
4816 // by the class BreakableLineCommentSection in case it is desirable to keep
4817 // several line comment sections in the same unwrapped line.
4818 //
4819 // FIXME: Consider putting separate line comment sections as children to the
4820 // unwrapped line instead.
4821 Tok->ContinuesLineCommentSection =
4822 continuesLineCommentSection(*Tok, *Line, Style, CommentPragmasRegex);
4823 if (isOnNewLine(*Tok) && JustComments && !Tok->ContinuesLineCommentSection)
4824 addUnwrappedLine();
4825 pushToken(Tok);
4826 }
4827 if (NewlineBeforeNext && JustComments)
4828 addUnwrappedLine();
4829 CommentsBeforeNextToken.clear();
4830}
4831
4832void UnwrappedLineParser::nextToken(int LevelDifference) {
4833 if (eof())
4834 return;
4835 flushComments(isOnNewLine(*FormatTok));
4836 pushToken(FormatTok);
4837 FormatToken *Previous = FormatTok;
4838 if (!Style.isJavaScript())
4839 readToken(LevelDifference);
4840 else
4841 readTokenWithJavaScriptASI();
4842 FormatTok->Previous = Previous;
4843 if (Style.isVerilog()) {
4844 // Blocks in Verilog can have `begin` and `end` instead of braces. For
4845 // keywords like `begin`, we can't treat them the same as left braces
4846 // because some contexts require one of them. For example structs use
4847 // braces and if blocks use keywords, and a left brace can occur in an if
4848 // statement, but it is not a block. For keywords like `end`, we simply
4849 // treat them the same as right braces.
4850 if (Keywords.isVerilogEnd(*FormatTok))
4851 FormatTok->Tok.setKind(tok::r_brace);
4852 }
4853}
4854
4855void UnwrappedLineParser::distributeComments(
4856 const ArrayRef<FormatToken *> &Comments, const FormatToken *NextTok) {
4857 // Whether or not a line comment token continues a line is controlled by
4858 // the method continuesLineCommentSection, with the following caveat:
4859 //
4860 // Define a trail of Comments to be a nonempty proper postfix of Comments such
4861 // that each comment line from the trail is aligned with the next token, if
4862 // the next token exists. If a trail exists, the beginning of the maximal
4863 // trail is marked as a start of a new comment section.
4864 //
4865 // For example in this code:
4866 //
4867 // int a; // line about a
4868 // // line 1 about b
4869 // // line 2 about b
4870 // int b;
4871 //
4872 // the two lines about b form a maximal trail, so there are two sections, the
4873 // first one consisting of the single comment "// line about a" and the
4874 // second one consisting of the next two comments.
4875 if (Comments.empty())
4876 return;
4877 bool ShouldPushCommentsInCurrentLine = true;
4878 bool HasTrailAlignedWithNextToken = false;
4879 unsigned StartOfTrailAlignedWithNextToken = 0;
4880 if (NextTok) {
4881 // We are skipping the first element intentionally.
4882 for (unsigned i = Comments.size() - 1; i > 0; --i) {
4883 if (Comments[i]->OriginalColumn == NextTok->OriginalColumn) {
4884 HasTrailAlignedWithNextToken = true;
4885 StartOfTrailAlignedWithNextToken = i;
4886 }
4887 }
4888 }
4889 for (unsigned i = 0, e = Comments.size(); i < e; ++i) {
4890 FormatToken *FormatTok = Comments[i];
4891 if (HasTrailAlignedWithNextToken && i == StartOfTrailAlignedWithNextToken) {
4892 FormatTok->ContinuesLineCommentSection = false;
4893 } else {
4894 FormatTok->ContinuesLineCommentSection = continuesLineCommentSection(
4895 *FormatTok, *Line, Style, CommentPragmasRegex);
4896 }
4897 if (!FormatTok->ContinuesLineCommentSection &&
4898 (isOnNewLine(*FormatTok) || FormatTok->IsFirst)) {
4899 ShouldPushCommentsInCurrentLine = false;
4900 }
4901 if (ShouldPushCommentsInCurrentLine)
4902 pushToken(FormatTok);
4903 else
4904 CommentsBeforeNextToken.push_back(FormatTok);
4905 }
4906}
4907
4908void UnwrappedLineParser::readToken(int LevelDifference) {
4910 bool PreviousWasComment = false;
4911 bool FirstNonCommentOnLine = false;
4912 do {
4913 FormatTok = Tokens->getNextToken();
4914 assert(FormatTok);
4915 while (FormatTok->isOneOf(TT_ConflictStart, TT_ConflictEnd,
4916 TT_ConflictAlternative)) {
4917 if (FormatTok->is(TT_ConflictStart))
4918 conditionalCompilationStart(/*Unreachable=*/false);
4919 else if (FormatTok->is(TT_ConflictAlternative))
4920 conditionalCompilationAlternative();
4921 else if (FormatTok->is(TT_ConflictEnd))
4922 conditionalCompilationEnd();
4923 FormatTok = Tokens->getNextToken();
4924 FormatTok->MustBreakBefore = true;
4925 FormatTok->MustBreakBeforeFinalized = true;
4926 }
4927
4928 auto IsFirstNonCommentOnLine = [](bool FirstNonCommentOnLine,
4929 const FormatToken &Tok,
4930 bool PreviousWasComment) {
4931 auto IsFirstOnLine = [](const FormatToken &Tok) {
4932 return Tok.HasUnescapedNewline || Tok.IsFirst;
4933 };
4934
4935 // Consider preprocessor directives preceded by block comments as first
4936 // on line.
4937 if (PreviousWasComment)
4938 return FirstNonCommentOnLine || IsFirstOnLine(Tok);
4939 return IsFirstOnLine(Tok);
4940 };
4941
4942 FirstNonCommentOnLine = IsFirstNonCommentOnLine(
4943 FirstNonCommentOnLine, *FormatTok, PreviousWasComment);
4944 PreviousWasComment = FormatTok->is(tok::comment);
4945
4946 while (!Line->InPPDirective && FormatTok->is(tok::hash) &&
4947 FirstNonCommentOnLine) {
4948 // In Verilog, the backtick is used for macro invocations. In TableGen,
4949 // the single hash is used for the paste operator.
4950 const auto *Next = Tokens->peekNextToken();
4951 if ((Style.isVerilog() && !Keywords.isVerilogPPDirective(*Next)) ||
4952 (Style.isTableGen() &&
4953 Next->isNoneOf(tok::kw_else, tok::pp_define, tok::pp_ifdef,
4954 tok::pp_ifndef, tok::pp_endif))) {
4955 break;
4956 }
4957 distributeComments(Comments, FormatTok);
4958 Comments.clear();
4959 // If there is an unfinished unwrapped line, we flush the preprocessor
4960 // directives only after that unwrapped line was finished later.
4961 bool SwitchToPreprocessorLines = !Line->Tokens.empty();
4962 ScopedLineState BlockState(*this, SwitchToPreprocessorLines);
4963 assert((LevelDifference >= 0 ||
4964 static_cast<unsigned>(-LevelDifference) <= Line->Level) &&
4965 "LevelDifference makes Line->Level negative");
4966 Line->Level += LevelDifference;
4967 // Comments stored before the preprocessor directive need to be output
4968 // before the preprocessor directive, at the same level as the
4969 // preprocessor directive, as we consider them to apply to the directive.
4970 if (Style.IndentPPDirectives == FormatStyle::PPDIS_BeforeHash &&
4971 PPBranchLevel > 0) {
4972 Line->Level += PPBranchLevel;
4973 }
4974 assert(Line->Level >= Line->UnbracedBodyLevel);
4975 Line->Level -= Line->UnbracedBodyLevel;
4976 flushComments(isOnNewLine(*FormatTok));
4977 const bool IsEndIf = Tokens->peekNextToken()->is(tok::pp_endif);
4978 parsePPDirective();
4979 PreviousWasComment = FormatTok->is(tok::comment);
4980 FirstNonCommentOnLine = IsFirstNonCommentOnLine(
4981 FirstNonCommentOnLine, *FormatTok, PreviousWasComment);
4982 // If the #endif of a potential include guard is the last thing in the
4983 // file, then we found an include guard.
4984 if (IsEndIf && IncludeGuard == IG_Defined && PPBranchLevel == -1 &&
4985 getIncludeGuardState(Style.IndentPPDirectives) == IG_Inited &&
4986 (eof() ||
4987 (PreviousWasComment &&
4988 Tokens->peekNextToken(/*SkipComment=*/true)->is(tok::eof)))) {
4989 IncludeGuard = IG_Found;
4990 }
4991 }
4992
4993 if (!PPStack.empty() && (PPStack.back().Kind == PP_Unreachable) &&
4994 !Line->InPPDirective) {
4995 continue;
4996 }
4997
4998 if (FormatTok->is(tok::identifier) &&
4999 Macros.defined(FormatTok->TokenText) &&
5000 // FIXME: Allow expanding macros in preprocessor directives.
5001 !Line->InPPDirective) {
5002 FormatToken *ID = FormatTok;
5003 unsigned Position = Tokens->getPosition();
5004
5005 // To correctly parse the code, we need to replace the tokens of the macro
5006 // call with its expansion.
5007 auto PreCall = std::move(Line);
5008 Line.reset(new UnwrappedLine);
5009 bool OldInExpansion = InExpansion;
5010 InExpansion = true;
5011 // We parse the macro call into a new line.
5012 auto Args = parseMacroCall();
5013 InExpansion = OldInExpansion;
5014 assert(Line->Tokens.front().Tok == ID);
5015 // And remember the unexpanded macro call tokens.
5016 auto UnexpandedLine = std::move(Line);
5017 // Reset to the old line.
5018 Line = std::move(PreCall);
5019
5020 LLVM_DEBUG({
5021 llvm::dbgs() << "Macro call: " << ID->TokenText << "(";
5022 if (Args) {
5023 llvm::dbgs() << "(";
5024 for (const auto &Arg : Args.value())
5025 for (const auto &T : Arg)
5026 llvm::dbgs() << T->TokenText << " ";
5027 llvm::dbgs() << ")";
5028 }
5029 llvm::dbgs() << "\n";
5030 });
5031 if (Macros.objectLike(ID->TokenText) && Args &&
5032 !Macros.hasArity(ID->TokenText, Args->size())) {
5033 // The macro is either
5034 // - object-like, but we got argumnets, or
5035 // - overloaded to be both object-like and function-like, but none of
5036 // the function-like arities match the number of arguments.
5037 // Thus, expand as object-like macro.
5038 LLVM_DEBUG(llvm::dbgs()
5039 << "Macro \"" << ID->TokenText
5040 << "\" not overloaded for arity " << Args->size()
5041 << "or not function-like, using object-like overload.");
5042 Args.reset();
5043 UnexpandedLine->Tokens.resize(1);
5044 Tokens->setPosition(Position);
5045 nextToken();
5046 assert(!Args && Macros.objectLike(ID->TokenText));
5047 }
5048 if ((!Args && Macros.objectLike(ID->TokenText)) ||
5049 (Args && Macros.hasArity(ID->TokenText, Args->size()))) {
5050 // Next, we insert the expanded tokens in the token stream at the
5051 // current position, and continue parsing.
5052 Unexpanded[ID] = std::move(UnexpandedLine);
5054 Macros.expand(ID, std::move(Args));
5055 if (!Expansion.empty())
5056 FormatTok = Tokens->insertTokens(Expansion);
5057
5058 LLVM_DEBUG({
5059 llvm::dbgs() << "Expanded: ";
5060 for (const auto &T : Expansion)
5061 llvm::dbgs() << T->TokenText << " ";
5062 llvm::dbgs() << "\n";
5063 });
5064 } else {
5065 LLVM_DEBUG({
5066 llvm::dbgs() << "Did not expand macro \"" << ID->TokenText
5067 << "\", because it was used ";
5068 if (Args)
5069 llvm::dbgs() << "with " << Args->size();
5070 else
5071 llvm::dbgs() << "without";
5072 llvm::dbgs() << " arguments, which doesn't match any definition.\n";
5073 });
5074 Tokens->setPosition(Position);
5075 FormatTok = ID;
5076 }
5077 }
5078
5079 if (FormatTok->isNot(tok::comment)) {
5080 distributeComments(Comments, FormatTok);
5081 Comments.clear();
5082 return;
5083 }
5084
5085 Comments.push_back(FormatTok);
5086 } while (!eof());
5087
5088 distributeComments(Comments, nullptr);
5089 Comments.clear();
5090}
5091
5092namespace {
5093template <typename Iterator>
5094void pushTokens(Iterator Begin, Iterator End,
5096 for (auto I = Begin; I != End; ++I) {
5097 Into.push_back(I->Tok);
5098 for (const auto &Child : I->Children)
5099 pushTokens(Child.Tokens.begin(), Child.Tokens.end(), Into);
5100 }
5101}
5102} // namespace
5103
5104std::optional<llvm::SmallVector<llvm::SmallVector<FormatToken *, 8>, 1>>
5105UnwrappedLineParser::parseMacroCall() {
5106 std::optional<llvm::SmallVector<llvm::SmallVector<FormatToken *, 8>, 1>> Args;
5107 assert(Line->Tokens.empty());
5108 nextToken();
5109 if (FormatTok->isNot(tok::l_paren))
5110 return Args;
5111 unsigned Position = Tokens->getPosition();
5112 FormatToken *Tok = FormatTok;
5113 nextToken();
5114 Args.emplace();
5115 auto ArgStart = std::prev(Line->Tokens.end());
5116
5117 int Parens = 0;
5118 do {
5119 switch (FormatTok->Tok.getKind()) {
5120 case tok::l_paren:
5121 ++Parens;
5122 nextToken();
5123 break;
5124 case tok::r_paren: {
5125 if (Parens > 0) {
5126 --Parens;
5127 nextToken();
5128 break;
5129 }
5130 Args->push_back({});
5131 pushTokens(std::next(ArgStart), Line->Tokens.end(), Args->back());
5132 nextToken();
5133 return Args;
5134 }
5135 case tok::comma: {
5136 if (Parens > 0) {
5137 nextToken();
5138 break;
5139 }
5140 Args->push_back({});
5141 pushTokens(std::next(ArgStart), Line->Tokens.end(), Args->back());
5142 nextToken();
5143 ArgStart = std::prev(Line->Tokens.end());
5144 break;
5145 }
5146 default:
5147 nextToken();
5148 break;
5149 }
5150 } while (!eof());
5151 Line->Tokens.resize(1);
5152 Tokens->setPosition(Position);
5153 FormatTok = Tok;
5154 return {};
5155}
5156
5157void UnwrappedLineParser::pushToken(FormatToken *Tok) {
5158 Line->Tokens.push_back(UnwrappedLineNode(Tok));
5159 if (AtEndOfPPLine) {
5160 auto &Tok = *Line->Tokens.back().Tok;
5161 Tok.MustBreakBefore = true;
5162 Tok.MustBreakBeforeFinalized = true;
5163 Tok.FirstAfterPPLine = true;
5164 AtEndOfPPLine = false;
5165 }
5166}
5167
5168} // end namespace format
5169} // 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:256
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:4458
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:3951
nullptr
This class represents a compute construct, representing a 'Kind' of ‘parallel’, 'serial',...
bool isCpp() const
Definition Format.h:3843
@ 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:2880
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 ...