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