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