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