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