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