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