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