clang  13.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 "llvm/ADT/STLExtras.h"
18 #include "llvm/Support/Debug.h"
19 #include "llvm/Support/raw_ostream.h"
20 
21 #include <algorithm>
22 
23 #define DEBUG_TYPE "format-parser"
24 
25 namespace clang {
26 namespace format {
27 
29 public:
30  virtual ~FormatTokenSource() {}
31  virtual FormatToken *getNextToken() = 0;
32 
33  virtual unsigned getPosition() = 0;
34  virtual FormatToken *setPosition(unsigned Position) = 0;
35 };
36 
37 namespace {
38 
39 class ScopedDeclarationState {
40 public:
41  ScopedDeclarationState(UnwrappedLine &Line, std::vector<bool> &Stack,
42  bool MustBeDeclaration)
43  : Line(Line), Stack(Stack) {
44  Line.MustBeDeclaration = MustBeDeclaration;
45  Stack.push_back(MustBeDeclaration);
46  }
47  ~ScopedDeclarationState() {
48  Stack.pop_back();
49  if (!Stack.empty())
50  Line.MustBeDeclaration = Stack.back();
51  else
52  Line.MustBeDeclaration = true;
53  }
54 
55 private:
56  UnwrappedLine &Line;
57  std::vector<bool> &Stack;
58 };
59 
60 static bool isLineComment(const FormatToken &FormatTok) {
61  return FormatTok.is(tok::comment) && !FormatTok.TokenText.startswith("/*");
62 }
63 
64 // Checks if \p FormatTok is a line comment that continues the line comment
65 // \p Previous. The original column of \p MinColumnToken is used to determine
66 // whether \p FormatTok is indented enough to the right to continue \p Previous.
67 static bool continuesLineComment(const FormatToken &FormatTok,
68  const FormatToken *Previous,
69  const FormatToken *MinColumnToken) {
70  if (!Previous || !MinColumnToken)
71  return false;
72  unsigned MinContinueColumn =
73  MinColumnToken->OriginalColumn + (isLineComment(*MinColumnToken) ? 0 : 1);
74  return isLineComment(FormatTok) && FormatTok.NewlinesBefore == 1 &&
75  isLineComment(*Previous) &&
76  FormatTok.OriginalColumn >= MinContinueColumn;
77 }
78 
79 class ScopedMacroState : public FormatTokenSource {
80 public:
81  ScopedMacroState(UnwrappedLine &Line, FormatTokenSource *&TokenSource,
82  FormatToken *&ResetToken)
83  : Line(Line), TokenSource(TokenSource), ResetToken(ResetToken),
84  PreviousLineLevel(Line.Level), PreviousTokenSource(TokenSource),
85  Token(nullptr), PreviousToken(nullptr) {
86  FakeEOF.Tok.startToken();
87  FakeEOF.Tok.setKind(tok::eof);
88  TokenSource = this;
89  Line.Level = 0;
90  Line.InPPDirective = true;
91  }
92 
93  ~ScopedMacroState() override {
94  TokenSource = PreviousTokenSource;
95  ResetToken = Token;
96  Line.InPPDirective = false;
97  Line.Level = PreviousLineLevel;
98  }
99 
100  FormatToken *getNextToken() override {
101  // The \c UnwrappedLineParser guards against this by never calling
102  // \c getNextToken() after it has encountered the first eof token.
103  assert(!eof());
104  PreviousToken = Token;
105  Token = PreviousTokenSource->getNextToken();
106  if (eof())
107  return &FakeEOF;
108  return Token;
109  }
110 
111  unsigned getPosition() override { return PreviousTokenSource->getPosition(); }
112 
113  FormatToken *setPosition(unsigned Position) override {
114  PreviousToken = nullptr;
115  Token = PreviousTokenSource->setPosition(Position);
116  return Token;
117  }
118 
119 private:
120  bool eof() {
121  return Token && Token->HasUnescapedNewline &&
122  !continuesLineComment(*Token, PreviousToken,
123  /*MinColumnToken=*/PreviousToken);
124  }
125 
126  FormatToken FakeEOF;
127  UnwrappedLine &Line;
128  FormatTokenSource *&TokenSource;
129  FormatToken *&ResetToken;
130  unsigned PreviousLineLevel;
131  FormatTokenSource *PreviousTokenSource;
132 
133  FormatToken *Token;
134  FormatToken *PreviousToken;
135 };
136 
137 } // end anonymous namespace
138 
140 public:
142  bool SwitchToPreprocessorLines = false)
143  : Parser(Parser), OriginalLines(Parser.CurrentLines) {
144  if (SwitchToPreprocessorLines)
145  Parser.CurrentLines = &Parser.PreprocessorDirectives;
146  else if (!Parser.Line->Tokens.empty())
147  Parser.CurrentLines = &Parser.Line->Tokens.back().Children;
148  PreBlockLine = std::move(Parser.Line);
149  Parser.Line = std::make_unique<UnwrappedLine>();
150  Parser.Line->Level = PreBlockLine->Level;
151  Parser.Line->InPPDirective = PreBlockLine->InPPDirective;
152  }
153 
155  if (!Parser.Line->Tokens.empty()) {
156  Parser.addUnwrappedLine();
157  }
158  assert(Parser.Line->Tokens.empty());
159  Parser.Line = std::move(PreBlockLine);
160  if (Parser.CurrentLines == &Parser.PreprocessorDirectives)
161  Parser.MustBreakBeforeNextToken = true;
162  Parser.CurrentLines = OriginalLines;
163  }
164 
165 private:
167 
168  std::unique_ptr<UnwrappedLine> PreBlockLine;
169  SmallVectorImpl<UnwrappedLine> *OriginalLines;
170 };
171 
173 public:
175  const FormatStyle &Style, unsigned &LineLevel)
176  : CompoundStatementIndenter(Parser, LineLevel,
177  Style.BraceWrapping.AfterControlStatement,
178  Style.BraceWrapping.IndentBraces) {}
180  bool WrapBrace, bool IndentBrace)
181  : LineLevel(LineLevel), OldLineLevel(LineLevel) {
182  if (WrapBrace)
183  Parser->addUnwrappedLine();
184  if (IndentBrace)
185  ++LineLevel;
186  }
187  ~CompoundStatementIndenter() { LineLevel = OldLineLevel; }
188 
189 private:
190  unsigned &LineLevel;
191  unsigned OldLineLevel;
192 };
193 
194 namespace {
195 
196 class IndexedTokenSource : public FormatTokenSource {
197 public:
198  IndexedTokenSource(ArrayRef<FormatToken *> Tokens)
199  : Tokens(Tokens), Position(-1) {}
200 
201  FormatToken *getNextToken() override {
202  ++Position;
203  return Tokens[Position];
204  }
205 
206  unsigned getPosition() override {
207  assert(Position >= 0);
208  return Position;
209  }
210 
211  FormatToken *setPosition(unsigned P) override {
212  Position = P;
213  return Tokens[Position];
214  }
215 
216  void reset() { Position = -1; }
217 
218 private:
219  ArrayRef<FormatToken *> Tokens;
220  int Position;
221 };
222 
223 } // end anonymous namespace
224 
226  const AdditionalKeywords &Keywords,
227  unsigned FirstStartColumn,
229  UnwrappedLineConsumer &Callback)
230  : Line(new UnwrappedLine), MustBreakBeforeNextToken(false),
231  CurrentLines(&Lines), Style(Style), Keywords(Keywords),
232  CommentPragmasRegex(Style.CommentPragmas), Tokens(nullptr),
233  Callback(Callback), AllTokens(Tokens), PPBranchLevel(-1),
234  IncludeGuard(Style.IndentPPDirectives == FormatStyle::PPDIS_None
235  ? IG_Rejected
236  : IG_Inited),
237  IncludeGuardToken(nullptr), FirstStartColumn(FirstStartColumn) {}
238 
239 void UnwrappedLineParser::reset() {
240  PPBranchLevel = -1;
241  IncludeGuard = Style.IndentPPDirectives == FormatStyle::PPDIS_None
242  ? IG_Rejected
243  : IG_Inited;
244  IncludeGuardToken = nullptr;
245  Line.reset(new UnwrappedLine);
246  CommentsBeforeNextToken.clear();
247  FormatTok = nullptr;
248  MustBreakBeforeNextToken = false;
249  PreprocessorDirectives.clear();
250  CurrentLines = &Lines;
251  DeclarationScopeStack.clear();
252  PPStack.clear();
253  Line->FirstStartColumn = FirstStartColumn;
254 }
255 
257  IndexedTokenSource TokenSource(AllTokens);
258  Line->FirstStartColumn = FirstStartColumn;
259  do {
260  LLVM_DEBUG(llvm::dbgs() << "----\n");
261  reset();
262  Tokens = &TokenSource;
263  TokenSource.reset();
264 
265  readToken();
266  parseFile();
267 
268  // If we found an include guard then all preprocessor directives (other than
269  // the guard) are over-indented by one.
270  if (IncludeGuard == IG_Found)
271  for (auto &Line : Lines)
272  if (Line.InPPDirective && Line.Level > 0)
273  --Line.Level;
274 
275  // Create line with eof token.
276  pushToken(FormatTok);
277  addUnwrappedLine();
278 
279  for (SmallVectorImpl<UnwrappedLine>::iterator I = Lines.begin(),
280  E = Lines.end();
281  I != E; ++I) {
282  Callback.consumeUnwrappedLine(*I);
283  }
284  Callback.finishRun();
285  Lines.clear();
286  while (!PPLevelBranchIndex.empty() &&
287  PPLevelBranchIndex.back() + 1 >= PPLevelBranchCount.back()) {
288  PPLevelBranchIndex.resize(PPLevelBranchIndex.size() - 1);
289  PPLevelBranchCount.resize(PPLevelBranchCount.size() - 1);
290  }
291  if (!PPLevelBranchIndex.empty()) {
292  ++PPLevelBranchIndex.back();
293  assert(PPLevelBranchIndex.size() == PPLevelBranchCount.size());
294  assert(PPLevelBranchIndex.back() <= PPLevelBranchCount.back());
295  }
296  } while (!PPLevelBranchIndex.empty());
297 }
298 
299 void UnwrappedLineParser::parseFile() {
300  // The top-level context in a file always has declarations, except for pre-
301  // processor directives and JavaScript files.
302  bool MustBeDeclaration =
303  !Line->InPPDirective && Style.Language != FormatStyle::LK_JavaScript;
304  ScopedDeclarationState DeclarationState(*Line, DeclarationScopeStack,
305  MustBeDeclaration);
306  if (Style.Language == FormatStyle::LK_TextProto)
307  parseBracedList();
308  else
309  parseLevel(/*HasOpeningBrace=*/false);
310  // Make sure to format the remaining tokens.
311  //
312  // LK_TextProto is special since its top-level is parsed as the body of a
313  // braced list, which does not necessarily have natural line separators such
314  // as a semicolon. Comments after the last entry that have been determined to
315  // not belong to that line, as in:
316  // key: value
317  // // endfile comment
318  // do not have a chance to be put on a line of their own until this point.
319  // Here we add this newline before end-of-file comments.
320  if (Style.Language == FormatStyle::LK_TextProto &&
321  !CommentsBeforeNextToken.empty())
322  addUnwrappedLine();
323  flushComments(true);
324  addUnwrappedLine();
325 }
326 
327 void UnwrappedLineParser::parseCSharpGenericTypeConstraint() {
328  do {
329  switch (FormatTok->Tok.getKind()) {
330  case tok::l_brace:
331  return;
332  default:
333  if (FormatTok->is(Keywords.kw_where)) {
334  addUnwrappedLine();
335  nextToken();
336  parseCSharpGenericTypeConstraint();
337  break;
338  }
339  nextToken();
340  break;
341  }
342  } while (!eof());
343 }
344 
345 void UnwrappedLineParser::parseCSharpAttribute() {
346  int UnpairedSquareBrackets = 1;
347  do {
348  switch (FormatTok->Tok.getKind()) {
349  case tok::r_square:
350  nextToken();
351  --UnpairedSquareBrackets;
352  if (UnpairedSquareBrackets == 0) {
353  addUnwrappedLine();
354  return;
355  }
356  break;
357  case tok::l_square:
358  ++UnpairedSquareBrackets;
359  nextToken();
360  break;
361  default:
362  nextToken();
363  break;
364  }
365  } while (!eof());
366 }
367 
368 void UnwrappedLineParser::parseLevel(bool HasOpeningBrace) {
369  bool SwitchLabelEncountered = false;
370  do {
371  tok::TokenKind kind = FormatTok->Tok.getKind();
372  if (FormatTok->getType() == TT_MacroBlockBegin) {
373  kind = tok::l_brace;
374  } else if (FormatTok->getType() == TT_MacroBlockEnd) {
375  kind = tok::r_brace;
376  }
377 
378  switch (kind) {
379  case tok::comment:
380  nextToken();
381  addUnwrappedLine();
382  break;
383  case tok::l_brace:
384  // FIXME: Add parameter whether this can happen - if this happens, we must
385  // be in a non-declaration context.
386  if (!FormatTok->is(TT_MacroBlockBegin) && tryToParseBracedList())
387  continue;
388  parseBlock(/*MustBeDeclaration=*/false);
389  addUnwrappedLine();
390  break;
391  case tok::r_brace:
392  if (HasOpeningBrace)
393  return;
394  nextToken();
395  addUnwrappedLine();
396  break;
397  case tok::kw_default: {
398  unsigned StoredPosition = Tokens->getPosition();
399  FormatToken *Next;
400  do {
401  Next = Tokens->getNextToken();
402  } while (Next && Next->is(tok::comment));
403  FormatTok = Tokens->setPosition(StoredPosition);
404  if (Next && Next->isNot(tok::colon)) {
405  // default not followed by ':' is not a case label; treat it like
406  // an identifier.
407  parseStructuralElement();
408  break;
409  }
410  // Else, if it is 'default:', fall through to the case handling.
411  LLVM_FALLTHROUGH;
412  }
413  case tok::kw_case:
414  if (Style.Language == FormatStyle::LK_JavaScript &&
415  Line->MustBeDeclaration) {
416  // A 'case: string' style field declaration.
417  parseStructuralElement();
418  break;
419  }
420  if (!SwitchLabelEncountered &&
421  (Style.IndentCaseLabels || (Line->InPPDirective && Line->Level == 1)))
422  ++Line->Level;
423  SwitchLabelEncountered = true;
424  parseStructuralElement();
425  break;
426  case tok::l_square:
427  if (Style.isCSharp()) {
428  nextToken();
429  parseCSharpAttribute();
430  break;
431  }
432  LLVM_FALLTHROUGH;
433  default:
434  parseStructuralElement();
435  break;
436  }
437  } while (!eof());
438 }
439 
440 void UnwrappedLineParser::calculateBraceTypes(bool ExpectClassBody) {
441  // We'll parse forward through the tokens until we hit
442  // a closing brace or eof - note that getNextToken() will
443  // parse macros, so this will magically work inside macro
444  // definitions, too.
445  unsigned StoredPosition = Tokens->getPosition();
446  FormatToken *Tok = FormatTok;
447  const FormatToken *PrevTok = Tok->Previous;
448  // Keep a stack of positions of lbrace tokens. We will
449  // update information about whether an lbrace starts a
450  // braced init list or a different block during the loop.
451  SmallVector<FormatToken *, 8> LBraceStack;
452  assert(Tok->Tok.is(tok::l_brace));
453  do {
454  // Get next non-comment token.
455  FormatToken *NextTok;
456  unsigned ReadTokens = 0;
457  do {
458  NextTok = Tokens->getNextToken();
459  ++ReadTokens;
460  } while (NextTok->is(tok::comment));
461 
462  switch (Tok->Tok.getKind()) {
463  case tok::l_brace:
464  if (Style.Language == FormatStyle::LK_JavaScript && PrevTok) {
465  if (PrevTok->isOneOf(tok::colon, tok::less))
466  // A ':' indicates this code is in a type, or a braced list
467  // following a label in an object literal ({a: {b: 1}}).
468  // A '<' could be an object used in a comparison, but that is nonsense
469  // code (can never return true), so more likely it is a generic type
470  // argument (`X<{a: string; b: number}>`).
471  // The code below could be confused by semicolons between the
472  // individual members in a type member list, which would normally
473  // trigger BK_Block. In both cases, this must be parsed as an inline
474  // braced init.
475  Tok->setBlockKind(BK_BracedInit);
476  else if (PrevTok->is(tok::r_paren))
477  // `) { }` can only occur in function or method declarations in JS.
478  Tok->setBlockKind(BK_Block);
479  } else {
480  Tok->setBlockKind(BK_Unknown);
481  }
482  LBraceStack.push_back(Tok);
483  break;
484  case tok::r_brace:
485  if (LBraceStack.empty())
486  break;
487  if (LBraceStack.back()->is(BK_Unknown)) {
488  bool ProbablyBracedList = false;
489  if (Style.Language == FormatStyle::LK_Proto) {
490  ProbablyBracedList = NextTok->isOneOf(tok::comma, tok::r_square);
491  } else {
492  // Using OriginalColumn to distinguish between ObjC methods and
493  // binary operators is a bit hacky.
494  bool NextIsObjCMethod = NextTok->isOneOf(tok::plus, tok::minus) &&
495  NextTok->OriginalColumn == 0;
496 
497  // If there is a comma, semicolon or right paren after the closing
498  // brace, we assume this is a braced initializer list. Note that
499  // regardless how we mark inner braces here, we will overwrite the
500  // BlockKind later if we parse a braced list (where all blocks
501  // inside are by default braced lists), or when we explicitly detect
502  // blocks (for example while parsing lambdas).
503  // FIXME: Some of these do not apply to JS, e.g. "} {" can never be a
504  // braced list in JS.
505  ProbablyBracedList =
506  (Style.Language == FormatStyle::LK_JavaScript &&
507  NextTok->isOneOf(Keywords.kw_of, Keywords.kw_in,
508  Keywords.kw_as)) ||
509  (Style.isCpp() && NextTok->is(tok::l_paren)) ||
510  NextTok->isOneOf(tok::comma, tok::period, tok::colon,
511  tok::r_paren, tok::r_square, tok::l_brace,
512  tok::ellipsis) ||
513  (NextTok->is(tok::identifier) &&
514  !PrevTok->isOneOf(tok::semi, tok::r_brace, tok::l_brace)) ||
515  (NextTok->is(tok::semi) &&
516  (!ExpectClassBody || LBraceStack.size() != 1)) ||
517  (NextTok->isBinaryOperator() && !NextIsObjCMethod);
518  if (!Style.isCSharp() && NextTok->is(tok::l_square)) {
519  // We can have an array subscript after a braced init
520  // list, but C++11 attributes are expected after blocks.
521  NextTok = Tokens->getNextToken();
522  ++ReadTokens;
523  ProbablyBracedList = NextTok->isNot(tok::l_square);
524  }
525  }
526  if (ProbablyBracedList) {
527  Tok->setBlockKind(BK_BracedInit);
528  LBraceStack.back()->setBlockKind(BK_BracedInit);
529  } else {
530  Tok->setBlockKind(BK_Block);
531  LBraceStack.back()->setBlockKind(BK_Block);
532  }
533  }
534  LBraceStack.pop_back();
535  break;
536  case tok::identifier:
537  if (!Tok->is(TT_StatementMacro))
538  break;
539  LLVM_FALLTHROUGH;
540  case tok::at:
541  case tok::semi:
542  case tok::kw_if:
543  case tok::kw_while:
544  case tok::kw_for:
545  case tok::kw_switch:
546  case tok::kw_try:
547  case tok::kw___try:
548  if (!LBraceStack.empty() && LBraceStack.back()->is(BK_Unknown))
549  LBraceStack.back()->setBlockKind(BK_Block);
550  break;
551  default:
552  break;
553  }
554  PrevTok = Tok;
555  Tok = NextTok;
556  } while (Tok->Tok.isNot(tok::eof) && !LBraceStack.empty());
557 
558  // Assume other blocks for all unclosed opening braces.
559  for (unsigned i = 0, e = LBraceStack.size(); i != e; ++i) {
560  if (LBraceStack[i]->is(BK_Unknown))
561  LBraceStack[i]->setBlockKind(BK_Block);
562  }
563 
564  FormatTok = Tokens->setPosition(StoredPosition);
565 }
566 
567 template <class T>
568 static inline void hash_combine(std::size_t &seed, const T &v) {
569  std::hash<T> hasher;
570  seed ^= hasher(v) + 0x9e3779b9 + (seed << 6) + (seed >> 2);
571 }
572 
573 size_t UnwrappedLineParser::computePPHash() const {
574  size_t h = 0;
575  for (const auto &i : PPStack) {
576  hash_combine(h, size_t(i.Kind));
577  hash_combine(h, i.Line);
578  }
579  return h;
580 }
581 
582 void UnwrappedLineParser::parseBlock(bool MustBeDeclaration, unsigned AddLevels,
583  bool MunchSemi) {
584  assert(FormatTok->isOneOf(tok::l_brace, TT_MacroBlockBegin) &&
585  "'{' or macro block token expected");
586  const bool MacroBlock = FormatTok->is(TT_MacroBlockBegin);
587  FormatTok->setBlockKind(BK_Block);
588 
589  size_t PPStartHash = computePPHash();
590 
591  unsigned InitialLevel = Line->Level;
592  nextToken(/*LevelDifference=*/AddLevels);
593 
594  if (MacroBlock && FormatTok->is(tok::l_paren))
595  parseParens();
596 
597  size_t NbPreprocessorDirectives =
598  CurrentLines == &Lines ? PreprocessorDirectives.size() : 0;
599  addUnwrappedLine();
600  size_t OpeningLineIndex =
601  CurrentLines->empty()
603  : (CurrentLines->size() - 1 - NbPreprocessorDirectives);
604 
605  ScopedDeclarationState DeclarationState(*Line, DeclarationScopeStack,
606  MustBeDeclaration);
607  Line->Level += AddLevels;
608  parseLevel(/*HasOpeningBrace=*/true);
609 
610  if (eof())
611  return;
612 
613  if (MacroBlock ? !FormatTok->is(TT_MacroBlockEnd)
614  : !FormatTok->is(tok::r_brace)) {
615  Line->Level = InitialLevel;
616  FormatTok->setBlockKind(BK_Block);
617  return;
618  }
619 
620  size_t PPEndHash = computePPHash();
621 
622  // Munch the closing brace.
623  nextToken(/*LevelDifference=*/-AddLevels);
624 
625  if (MacroBlock && FormatTok->is(tok::l_paren))
626  parseParens();
627 
628  if (FormatTok->is(tok::arrow)) {
629  // Following the } we can find a trailing return type arrow
630  // as part of an implicit conversion constraint.
631  nextToken();
632  parseStructuralElement();
633  }
634 
635  if (MunchSemi && FormatTok->Tok.is(tok::semi))
636  nextToken();
637 
638  Line->Level = InitialLevel;
639 
640  if (PPStartHash == PPEndHash) {
641  Line->MatchingOpeningBlockLineIndex = OpeningLineIndex;
642  if (OpeningLineIndex != UnwrappedLine::kInvalidIndex) {
643  // Update the opening line to add the forward reference as well
644  (*CurrentLines)[OpeningLineIndex].MatchingClosingBlockLineIndex =
645  CurrentLines->size() - 1;
646  }
647  }
648 }
649 
650 static bool isGoogScope(const UnwrappedLine &Line) {
651  // FIXME: Closure-library specific stuff should not be hard-coded but be
652  // configurable.
653  if (Line.Tokens.size() < 4)
654  return false;
655  auto I = Line.Tokens.begin();
656  if (I->Tok->TokenText != "goog")
657  return false;
658  ++I;
659  if (I->Tok->isNot(tok::period))
660  return false;
661  ++I;
662  if (I->Tok->TokenText != "scope")
663  return false;
664  ++I;
665  return I->Tok->is(tok::l_paren);
666 }
667 
668 static bool isIIFE(const UnwrappedLine &Line,
669  const AdditionalKeywords &Keywords) {
670  // Look for the start of an immediately invoked anonymous function.
671  // https://en.wikipedia.org/wiki/Immediately-invoked_function_expression
672  // This is commonly done in JavaScript to create a new, anonymous scope.
673  // Example: (function() { ... })()
674  if (Line.Tokens.size() < 3)
675  return false;
676  auto I = Line.Tokens.begin();
677  if (I->Tok->isNot(tok::l_paren))
678  return false;
679  ++I;
680  if (I->Tok->isNot(Keywords.kw_function))
681  return false;
682  ++I;
683  return I->Tok->is(tok::l_paren);
684 }
685 
686 static bool ShouldBreakBeforeBrace(const FormatStyle &Style,
687  const FormatToken &InitialToken) {
688  if (InitialToken.isOneOf(tok::kw_namespace, TT_NamespaceMacro))
689  return Style.BraceWrapping.AfterNamespace;
690  if (InitialToken.is(tok::kw_class))
691  return Style.BraceWrapping.AfterClass;
692  if (InitialToken.is(tok::kw_union))
693  return Style.BraceWrapping.AfterUnion;
694  if (InitialToken.is(tok::kw_struct))
695  return Style.BraceWrapping.AfterStruct;
696  return false;
697 }
698 
699 void UnwrappedLineParser::parseChildBlock() {
700  FormatTok->setBlockKind(BK_Block);
701  nextToken();
702  {
703  bool SkipIndent = (Style.Language == FormatStyle::LK_JavaScript &&
704  (isGoogScope(*Line) || isIIFE(*Line, Keywords)));
705  ScopedLineState LineState(*this);
706  ScopedDeclarationState DeclarationState(*Line, DeclarationScopeStack,
707  /*MustBeDeclaration=*/false);
708  Line->Level += SkipIndent ? 0 : 1;
709  parseLevel(/*HasOpeningBrace=*/true);
710  flushComments(isOnNewLine(*FormatTok));
711  Line->Level -= SkipIndent ? 0 : 1;
712  }
713  nextToken();
714 }
715 
716 void UnwrappedLineParser::parsePPDirective() {
717  assert(FormatTok->Tok.is(tok::hash) && "'#' expected");
718  ScopedMacroState MacroState(*Line, Tokens, FormatTok);
719 
720  nextToken();
721 
722  if (!FormatTok->Tok.getIdentifierInfo()) {
723  parsePPUnknown();
724  return;
725  }
726 
727  switch (FormatTok->Tok.getIdentifierInfo()->getPPKeywordID()) {
728  case tok::pp_define:
729  parsePPDefine();
730  return;
731  case tok::pp_if:
732  parsePPIf(/*IfDef=*/false);
733  break;
734  case tok::pp_ifdef:
735  case tok::pp_ifndef:
736  parsePPIf(/*IfDef=*/true);
737  break;
738  case tok::pp_else:
739  parsePPElse();
740  break;
741  case tok::pp_elif:
742  parsePPElIf();
743  break;
744  case tok::pp_endif:
745  parsePPEndIf();
746  break;
747  default:
748  parsePPUnknown();
749  break;
750  }
751 }
752 
753 void UnwrappedLineParser::conditionalCompilationCondition(bool Unreachable) {
754  size_t Line = CurrentLines->size();
755  if (CurrentLines == &PreprocessorDirectives)
756  Line += Lines.size();
757 
758  if (Unreachable ||
759  (!PPStack.empty() && PPStack.back().Kind == PP_Unreachable))
760  PPStack.push_back({PP_Unreachable, Line});
761  else
762  PPStack.push_back({PP_Conditional, Line});
763 }
764 
765 void UnwrappedLineParser::conditionalCompilationStart(bool Unreachable) {
766  ++PPBranchLevel;
767  assert(PPBranchLevel >= 0 && PPBranchLevel <= (int)PPLevelBranchIndex.size());
768  if (PPBranchLevel == (int)PPLevelBranchIndex.size()) {
769  PPLevelBranchIndex.push_back(0);
770  PPLevelBranchCount.push_back(0);
771  }
772  PPChainBranchIndex.push(0);
773  bool Skip = PPLevelBranchIndex[PPBranchLevel] > 0;
774  conditionalCompilationCondition(Unreachable || Skip);
775 }
776 
777 void UnwrappedLineParser::conditionalCompilationAlternative() {
778  if (!PPStack.empty())
779  PPStack.pop_back();
780  assert(PPBranchLevel < (int)PPLevelBranchIndex.size());
781  if (!PPChainBranchIndex.empty())
782  ++PPChainBranchIndex.top();
783  conditionalCompilationCondition(
784  PPBranchLevel >= 0 && !PPChainBranchIndex.empty() &&
785  PPLevelBranchIndex[PPBranchLevel] != PPChainBranchIndex.top());
786 }
787 
788 void UnwrappedLineParser::conditionalCompilationEnd() {
789  assert(PPBranchLevel < (int)PPLevelBranchIndex.size());
790  if (PPBranchLevel >= 0 && !PPChainBranchIndex.empty()) {
791  if (PPChainBranchIndex.top() + 1 > PPLevelBranchCount[PPBranchLevel]) {
792  PPLevelBranchCount[PPBranchLevel] = PPChainBranchIndex.top() + 1;
793  }
794  }
795  // Guard against #endif's without #if.
796  if (PPBranchLevel > -1)
797  --PPBranchLevel;
798  if (!PPChainBranchIndex.empty())
799  PPChainBranchIndex.pop();
800  if (!PPStack.empty())
801  PPStack.pop_back();
802 }
803 
804 void UnwrappedLineParser::parsePPIf(bool IfDef) {
805  bool IfNDef = FormatTok->is(tok::pp_ifndef);
806  nextToken();
807  bool Unreachable = false;
808  if (!IfDef && (FormatTok->is(tok::kw_false) || FormatTok->TokenText == "0"))
809  Unreachable = true;
810  if (IfDef && !IfNDef && FormatTok->TokenText == "SWIG")
811  Unreachable = true;
812  conditionalCompilationStart(Unreachable);
813  FormatToken *IfCondition = FormatTok;
814  // If there's a #ifndef on the first line, and the only lines before it are
815  // comments, it could be an include guard.
816  bool MaybeIncludeGuard = IfNDef;
817  if (IncludeGuard == IG_Inited && MaybeIncludeGuard)
818  for (auto &Line : Lines) {
819  if (!Line.Tokens.front().Tok->is(tok::comment)) {
820  MaybeIncludeGuard = false;
821  IncludeGuard = IG_Rejected;
822  break;
823  }
824  }
825  --PPBranchLevel;
826  parsePPUnknown();
827  ++PPBranchLevel;
828  if (IncludeGuard == IG_Inited && MaybeIncludeGuard) {
829  IncludeGuard = IG_IfNdefed;
830  IncludeGuardToken = IfCondition;
831  }
832 }
833 
834 void UnwrappedLineParser::parsePPElse() {
835  // If a potential include guard has an #else, it's not an include guard.
836  if (IncludeGuard == IG_Defined && PPBranchLevel == 0)
837  IncludeGuard = IG_Rejected;
838  conditionalCompilationAlternative();
839  if (PPBranchLevel > -1)
840  --PPBranchLevel;
841  parsePPUnknown();
842  ++PPBranchLevel;
843 }
844 
845 void UnwrappedLineParser::parsePPElIf() { parsePPElse(); }
846 
847 void UnwrappedLineParser::parsePPEndIf() {
848  conditionalCompilationEnd();
849  parsePPUnknown();
850  // If the #endif of a potential include guard is the last thing in the file,
851  // then we found an include guard.
852  unsigned TokenPosition = Tokens->getPosition();
853  FormatToken *PeekNext = AllTokens[TokenPosition];
854  if (IncludeGuard == IG_Defined && PPBranchLevel == -1 &&
855  PeekNext->is(tok::eof) &&
856  Style.IndentPPDirectives != FormatStyle::PPDIS_None)
857  IncludeGuard = IG_Found;
858 }
859 
860 void UnwrappedLineParser::parsePPDefine() {
861  nextToken();
862 
863  if (!FormatTok->Tok.getIdentifierInfo()) {
864  IncludeGuard = IG_Rejected;
865  IncludeGuardToken = nullptr;
866  parsePPUnknown();
867  return;
868  }
869 
870  if (IncludeGuard == IG_IfNdefed &&
871  IncludeGuardToken->TokenText == FormatTok->TokenText) {
872  IncludeGuard = IG_Defined;
873  IncludeGuardToken = nullptr;
874  for (auto &Line : Lines) {
875  if (!Line.Tokens.front().Tok->isOneOf(tok::comment, tok::hash)) {
876  IncludeGuard = IG_Rejected;
877  break;
878  }
879  }
880  }
881 
882  nextToken();
883  if (FormatTok->Tok.getKind() == tok::l_paren &&
884  FormatTok->WhitespaceRange.getBegin() ==
885  FormatTok->WhitespaceRange.getEnd()) {
886  parseParens();
887  }
888  if (Style.IndentPPDirectives != FormatStyle::PPDIS_None)
889  Line->Level += PPBranchLevel + 1;
890  addUnwrappedLine();
891  ++Line->Level;
892 
893  // Errors during a preprocessor directive can only affect the layout of the
894  // preprocessor directive, and thus we ignore them. An alternative approach
895  // would be to use the same approach we use on the file level (no
896  // re-indentation if there was a structural error) within the macro
897  // definition.
898  parseFile();
899 }
900 
901 void UnwrappedLineParser::parsePPUnknown() {
902  do {
903  nextToken();
904  } while (!eof());
905  if (Style.IndentPPDirectives != FormatStyle::PPDIS_None)
906  Line->Level += PPBranchLevel + 1;
907  addUnwrappedLine();
908 }
909 
910 // Here we exclude certain tokens that are not usually the first token in an
911 // unwrapped line. This is used in attempt to distinguish macro calls without
912 // trailing semicolons from other constructs split to several lines.
913 static bool tokenCanStartNewLine(const FormatToken &Tok) {
914  // Semicolon can be a null-statement, l_square can be a start of a macro or
915  // a C++11 attribute, but this doesn't seem to be common.
916  return Tok.isNot(tok::semi) && Tok.isNot(tok::l_brace) &&
917  Tok.isNot(TT_AttributeSquare) &&
918  // Tokens that can only be used as binary operators and a part of
919  // overloaded operator names.
920  Tok.isNot(tok::period) && Tok.isNot(tok::periodstar) &&
921  Tok.isNot(tok::arrow) && Tok.isNot(tok::arrowstar) &&
922  Tok.isNot(tok::less) && Tok.isNot(tok::greater) &&
923  Tok.isNot(tok::slash) && Tok.isNot(tok::percent) &&
924  Tok.isNot(tok::lessless) && Tok.isNot(tok::greatergreater) &&
925  Tok.isNot(tok::equal) && Tok.isNot(tok::plusequal) &&
926  Tok.isNot(tok::minusequal) && Tok.isNot(tok::starequal) &&
927  Tok.isNot(tok::slashequal) && Tok.isNot(tok::percentequal) &&
928  Tok.isNot(tok::ampequal) && Tok.isNot(tok::pipeequal) &&
929  Tok.isNot(tok::caretequal) && Tok.isNot(tok::greatergreaterequal) &&
930  Tok.isNot(tok::lesslessequal) &&
931  // Colon is used in labels, base class lists, initializer lists,
932  // range-based for loops, ternary operator, but should never be the
933  // first token in an unwrapped line.
934  Tok.isNot(tok::colon) &&
935  // 'noexcept' is a trailing annotation.
936  Tok.isNot(tok::kw_noexcept);
937 }
938 
939 static bool mustBeJSIdent(const AdditionalKeywords &Keywords,
940  const FormatToken *FormatTok) {
941  // FIXME: This returns true for C/C++ keywords like 'struct'.
942  return FormatTok->is(tok::identifier) &&
943  (FormatTok->Tok.getIdentifierInfo() == nullptr ||
944  !FormatTok->isOneOf(
945  Keywords.kw_in, Keywords.kw_of, Keywords.kw_as, Keywords.kw_async,
946  Keywords.kw_await, Keywords.kw_yield, Keywords.kw_finally,
947  Keywords.kw_function, Keywords.kw_import, Keywords.kw_is,
948  Keywords.kw_let, Keywords.kw_var, tok::kw_const,
949  Keywords.kw_abstract, Keywords.kw_extends, Keywords.kw_implements,
950  Keywords.kw_instanceof, Keywords.kw_interface, Keywords.kw_throws,
951  Keywords.kw_from));
952 }
953 
954 static bool mustBeJSIdentOrValue(const AdditionalKeywords &Keywords,
955  const FormatToken *FormatTok) {
956  return FormatTok->Tok.isLiteral() ||
957  FormatTok->isOneOf(tok::kw_true, tok::kw_false) ||
958  mustBeJSIdent(Keywords, FormatTok);
959 }
960 
961 // isJSDeclOrStmt returns true if |FormatTok| starts a declaration or statement
962 // when encountered after a value (see mustBeJSIdentOrValue).
963 static bool isJSDeclOrStmt(const AdditionalKeywords &Keywords,
964  const FormatToken *FormatTok) {
965  return FormatTok->isOneOf(
966  tok::kw_return, Keywords.kw_yield,
967  // conditionals
968  tok::kw_if, tok::kw_else,
969  // loops
970  tok::kw_for, tok::kw_while, tok::kw_do, tok::kw_continue, tok::kw_break,
971  // switch/case
972  tok::kw_switch, tok::kw_case,
973  // exceptions
974  tok::kw_throw, tok::kw_try, tok::kw_catch, Keywords.kw_finally,
975  // declaration
976  tok::kw_const, tok::kw_class, Keywords.kw_var, Keywords.kw_let,
977  Keywords.kw_async, Keywords.kw_function,
978  // import/export
979  Keywords.kw_import, tok::kw_export);
980 }
981 
982 // readTokenWithJavaScriptASI reads the next token and terminates the current
983 // line if JavaScript Automatic Semicolon Insertion must
984 // happen between the current token and the next token.
985 //
986 // This method is conservative - it cannot cover all edge cases of JavaScript,
987 // but only aims to correctly handle certain well known cases. It *must not*
988 // return true in speculative cases.
989 void UnwrappedLineParser::readTokenWithJavaScriptASI() {
990  FormatToken *Previous = FormatTok;
991  readToken();
992  FormatToken *Next = FormatTok;
993 
994  bool IsOnSameLine =
995  CommentsBeforeNextToken.empty()
996  ? Next->NewlinesBefore == 0
997  : CommentsBeforeNextToken.front()->NewlinesBefore == 0;
998  if (IsOnSameLine)
999  return;
1000 
1001  bool PreviousMustBeValue = mustBeJSIdentOrValue(Keywords, Previous);
1002  bool PreviousStartsTemplateExpr =
1003  Previous->is(TT_TemplateString) && Previous->TokenText.endswith("${");
1004  if (PreviousMustBeValue || Previous->is(tok::r_paren)) {
1005  // If the line contains an '@' sign, the previous token might be an
1006  // annotation, which can precede another identifier/value.
1007  bool HasAt = std::find_if(Line->Tokens.begin(), Line->Tokens.end(),
1008  [](UnwrappedLineNode &LineNode) {
1009  return LineNode.Tok->is(tok::at);
1010  }) != Line->Tokens.end();
1011  if (HasAt)
1012  return;
1013  }
1014  if (Next->is(tok::exclaim) && PreviousMustBeValue)
1015  return addUnwrappedLine();
1016  bool NextMustBeValue = mustBeJSIdentOrValue(Keywords, Next);
1017  bool NextEndsTemplateExpr =
1018  Next->is(TT_TemplateString) && Next->TokenText.startswith("}");
1019  if (NextMustBeValue && !NextEndsTemplateExpr && !PreviousStartsTemplateExpr &&
1020  (PreviousMustBeValue ||
1021  Previous->isOneOf(tok::r_square, tok::r_paren, tok::plusplus,
1022  tok::minusminus)))
1023  return addUnwrappedLine();
1024  if ((PreviousMustBeValue || Previous->is(tok::r_paren)) &&
1025  isJSDeclOrStmt(Keywords, Next))
1026  return addUnwrappedLine();
1027 }
1028 
1029 void UnwrappedLineParser::parseStructuralElement() {
1030  assert(!FormatTok->is(tok::l_brace));
1031  if (Style.Language == FormatStyle::LK_TableGen &&
1032  FormatTok->is(tok::pp_include)) {
1033  nextToken();
1034  if (FormatTok->is(tok::string_literal))
1035  nextToken();
1036  addUnwrappedLine();
1037  return;
1038  }
1039  switch (FormatTok->Tok.getKind()) {
1040  case tok::kw_asm:
1041  nextToken();
1042  if (FormatTok->is(tok::l_brace)) {
1043  FormatTok->setType(TT_InlineASMBrace);
1044  nextToken();
1045  while (FormatTok && FormatTok->isNot(tok::eof)) {
1046  if (FormatTok->is(tok::r_brace)) {
1047  FormatTok->setType(TT_InlineASMBrace);
1048  nextToken();
1049  addUnwrappedLine();
1050  break;
1051  }
1052  FormatTok->Finalized = true;
1053  nextToken();
1054  }
1055  }
1056  break;
1057  case tok::kw_namespace:
1058  parseNamespace();
1059  return;
1060  case tok::kw_public:
1061  case tok::kw_protected:
1062  case tok::kw_private:
1063  if (Style.Language == FormatStyle::LK_Java ||
1064  Style.Language == FormatStyle::LK_JavaScript || Style.isCSharp())
1065  nextToken();
1066  else
1067  parseAccessSpecifier();
1068  return;
1069  case tok::kw_if:
1070  if (Style.Language == FormatStyle::LK_JavaScript && Line->MustBeDeclaration)
1071  // field/method declaration.
1072  break;
1073  parseIfThenElse();
1074  return;
1075  case tok::kw_for:
1076  case tok::kw_while:
1077  if (Style.Language == FormatStyle::LK_JavaScript && Line->MustBeDeclaration)
1078  // field/method declaration.
1079  break;
1080  parseForOrWhileLoop();
1081  return;
1082  case tok::kw_do:
1083  if (Style.Language == FormatStyle::LK_JavaScript && Line->MustBeDeclaration)
1084  // field/method declaration.
1085  break;
1086  parseDoWhile();
1087  return;
1088  case tok::kw_switch:
1089  if (Style.Language == FormatStyle::LK_JavaScript && Line->MustBeDeclaration)
1090  // 'switch: string' field declaration.
1091  break;
1092  parseSwitch();
1093  return;
1094  case tok::kw_default:
1095  if (Style.Language == FormatStyle::LK_JavaScript && Line->MustBeDeclaration)
1096  // 'default: string' field declaration.
1097  break;
1098  nextToken();
1099  if (FormatTok->is(tok::colon)) {
1100  parseLabel();
1101  return;
1102  }
1103  // e.g. "default void f() {}" in a Java interface.
1104  break;
1105  case tok::kw_case:
1106  if (Style.Language == FormatStyle::LK_JavaScript && Line->MustBeDeclaration)
1107  // 'case: string' field declaration.
1108  break;
1109  parseCaseLabel();
1110  return;
1111  case tok::kw_try:
1112  case tok::kw___try:
1113  if (Style.Language == FormatStyle::LK_JavaScript && Line->MustBeDeclaration)
1114  // field/method declaration.
1115  break;
1116  parseTryCatch();
1117  return;
1118  case tok::kw_extern:
1119  nextToken();
1120  if (FormatTok->Tok.is(tok::string_literal)) {
1121  nextToken();
1122  if (FormatTok->Tok.is(tok::l_brace)) {
1123  if (!Style.IndentExternBlock) {
1124  if (Style.BraceWrapping.AfterExternBlock) {
1125  addUnwrappedLine();
1126  }
1127  unsigned AddLevels = Style.BraceWrapping.AfterExternBlock ? 1u : 0u;
1128  parseBlock(/*MustBeDeclaration=*/true, AddLevels);
1129  } else {
1130  unsigned AddLevels =
1131  Style.IndentExternBlock == FormatStyle::IEBS_Indent ? 1u : 0u;
1132  parseBlock(/*MustBeDeclaration=*/true, AddLevels);
1133  }
1134  addUnwrappedLine();
1135  return;
1136  }
1137  }
1138  break;
1139  case tok::kw_export:
1140  if (Style.Language == FormatStyle::LK_JavaScript) {
1141  parseJavaScriptEs6ImportExport();
1142  return;
1143  }
1144  if (!Style.isCpp())
1145  break;
1146  // Handle C++ "(inline|export) namespace".
1147  LLVM_FALLTHROUGH;
1148  case tok::kw_inline:
1149  nextToken();
1150  if (FormatTok->Tok.is(tok::kw_namespace)) {
1151  parseNamespace();
1152  return;
1153  }
1154  break;
1155  case tok::identifier:
1156  if (FormatTok->is(TT_ForEachMacro)) {
1157  parseForOrWhileLoop();
1158  return;
1159  }
1160  if (FormatTok->is(TT_MacroBlockBegin)) {
1161  parseBlock(/*MustBeDeclaration=*/false, /*AddLevels=*/1u,
1162  /*MunchSemi=*/false);
1163  return;
1164  }
1165  if (FormatTok->is(Keywords.kw_import)) {
1166  if (Style.Language == FormatStyle::LK_JavaScript) {
1167  parseJavaScriptEs6ImportExport();
1168  return;
1169  }
1170  if (Style.Language == FormatStyle::LK_Proto) {
1171  nextToken();
1172  if (FormatTok->is(tok::kw_public))
1173  nextToken();
1174  if (!FormatTok->is(tok::string_literal))
1175  return;
1176  nextToken();
1177  if (FormatTok->is(tok::semi))
1178  nextToken();
1179  addUnwrappedLine();
1180  return;
1181  }
1182  }
1183  if (Style.isCpp() &&
1184  FormatTok->isOneOf(Keywords.kw_signals, Keywords.kw_qsignals,
1185  Keywords.kw_slots, Keywords.kw_qslots)) {
1186  nextToken();
1187  if (FormatTok->is(tok::colon)) {
1188  nextToken();
1189  addUnwrappedLine();
1190  return;
1191  }
1192  }
1193  if (Style.isCpp() && FormatTok->is(TT_StatementMacro)) {
1194  parseStatementMacro();
1195  return;
1196  }
1197  if (Style.isCpp() && FormatTok->is(TT_NamespaceMacro)) {
1198  parseNamespace();
1199  return;
1200  }
1201  // In all other cases, parse the declaration.
1202  break;
1203  default:
1204  break;
1205  }
1206  do {
1207  const FormatToken *Previous = FormatTok->Previous;
1208  switch (FormatTok->Tok.getKind()) {
1209  case tok::at:
1210  nextToken();
1211  if (FormatTok->Tok.is(tok::l_brace)) {
1212  nextToken();
1213  parseBracedList();
1214  break;
1215  } else if (Style.Language == FormatStyle::LK_Java &&
1216  FormatTok->is(Keywords.kw_interface)) {
1217  nextToken();
1218  break;
1219  }
1220  switch (FormatTok->Tok.getObjCKeywordID()) {
1221  case tok::objc_public:
1222  case tok::objc_protected:
1223  case tok::objc_package:
1224  case tok::objc_private:
1225  return parseAccessSpecifier();
1226  case tok::objc_interface:
1227  case tok::objc_implementation:
1228  return parseObjCInterfaceOrImplementation();
1229  case tok::objc_protocol:
1230  if (parseObjCProtocol())
1231  return;
1232  break;
1233  case tok::objc_end:
1234  return; // Handled by the caller.
1235  case tok::objc_optional:
1236  case tok::objc_required:
1237  nextToken();
1238  addUnwrappedLine();
1239  return;
1240  case tok::objc_autoreleasepool:
1241  nextToken();
1242  if (FormatTok->Tok.is(tok::l_brace)) {
1243  if (Style.BraceWrapping.AfterControlStatement ==
1244  FormatStyle::BWACS_Always)
1245  addUnwrappedLine();
1246  parseBlock(/*MustBeDeclaration=*/false);
1247  }
1248  addUnwrappedLine();
1249  return;
1250  case tok::objc_synchronized:
1251  nextToken();
1252  if (FormatTok->Tok.is(tok::l_paren))
1253  // Skip synchronization object
1254  parseParens();
1255  if (FormatTok->Tok.is(tok::l_brace)) {
1256  if (Style.BraceWrapping.AfterControlStatement ==
1257  FormatStyle::BWACS_Always)
1258  addUnwrappedLine();
1259  parseBlock(/*MustBeDeclaration=*/false);
1260  }
1261  addUnwrappedLine();
1262  return;
1263  case tok::objc_try:
1264  // This branch isn't strictly necessary (the kw_try case below would
1265  // do this too after the tok::at is parsed above). But be explicit.
1266  parseTryCatch();
1267  return;
1268  default:
1269  break;
1270  }
1271  break;
1272  case tok::kw_concept:
1273  parseConcept();
1274  break;
1275  case tok::kw_requires:
1276  parseRequires();
1277  break;
1278  case tok::kw_enum:
1279  // Ignore if this is part of "template <enum ...".
1280  if (Previous && Previous->is(tok::less)) {
1281  nextToken();
1282  break;
1283  }
1284 
1285  // parseEnum falls through and does not yet add an unwrapped line as an
1286  // enum definition can start a structural element.
1287  if (!parseEnum())
1288  break;
1289  // This only applies for C++.
1290  if (!Style.isCpp()) {
1291  addUnwrappedLine();
1292  return;
1293  }
1294  break;
1295  case tok::kw_typedef:
1296  nextToken();
1297  if (FormatTok->isOneOf(Keywords.kw_NS_ENUM, Keywords.kw_NS_OPTIONS,
1298  Keywords.kw_CF_ENUM, Keywords.kw_CF_OPTIONS,
1299  Keywords.kw_CF_CLOSED_ENUM,
1300  Keywords.kw_NS_CLOSED_ENUM))
1301  parseEnum();
1302  break;
1303  case tok::kw_struct:
1304  case tok::kw_union:
1305  case tok::kw_class:
1306  // parseRecord falls through and does not yet add an unwrapped line as a
1307  // record declaration or definition can start a structural element.
1308  parseRecord();
1309  // This does not apply for Java, JavaScript and C#.
1310  if (Style.Language == FormatStyle::LK_Java ||
1311  Style.Language == FormatStyle::LK_JavaScript || Style.isCSharp()) {
1312  if (FormatTok->is(tok::semi))
1313  nextToken();
1314  addUnwrappedLine();
1315  return;
1316  }
1317  break;
1318  case tok::period:
1319  nextToken();
1320  // In Java, classes have an implicit static member "class".
1321  if (Style.Language == FormatStyle::LK_Java && FormatTok &&
1322  FormatTok->is(tok::kw_class))
1323  nextToken();
1324  if (Style.Language == FormatStyle::LK_JavaScript && FormatTok &&
1325  FormatTok->Tok.getIdentifierInfo())
1326  // JavaScript only has pseudo keywords, all keywords are allowed to
1327  // appear in "IdentifierName" positions. See http://es5.github.io/#x7.6
1328  nextToken();
1329  break;
1330  case tok::semi:
1331  nextToken();
1332  addUnwrappedLine();
1333  return;
1334  case tok::r_brace:
1335  addUnwrappedLine();
1336  return;
1337  case tok::l_paren:
1338  parseParens();
1339  break;
1340  case tok::kw_operator:
1341  nextToken();
1342  if (FormatTok->isBinaryOperator())
1343  nextToken();
1344  break;
1345  case tok::caret:
1346  nextToken();
1347  if (FormatTok->Tok.isAnyIdentifier() ||
1348  FormatTok->isSimpleTypeSpecifier())
1349  nextToken();
1350  if (FormatTok->is(tok::l_paren))
1351  parseParens();
1352  if (FormatTok->is(tok::l_brace))
1353  parseChildBlock();
1354  break;
1355  case tok::l_brace:
1356  if (!tryToParsePropertyAccessor() && !tryToParseBracedList()) {
1357  // A block outside of parentheses must be the last part of a
1358  // structural element.
1359  // FIXME: Figure out cases where this is not true, and add projections
1360  // for them (the one we know is missing are lambdas).
1361  if (Style.BraceWrapping.AfterFunction)
1362  addUnwrappedLine();
1363  FormatTok->setType(TT_FunctionLBrace);
1364  parseBlock(/*MustBeDeclaration=*/false);
1365  addUnwrappedLine();
1366  return;
1367  }
1368  // Otherwise this was a braced init list, and the structural
1369  // element continues.
1370  break;
1371  case tok::kw_try:
1372  if (Style.Language == FormatStyle::LK_JavaScript &&
1373  Line->MustBeDeclaration) {
1374  // field/method declaration.
1375  nextToken();
1376  break;
1377  }
1378  // We arrive here when parsing function-try blocks.
1379  if (Style.BraceWrapping.AfterFunction)
1380  addUnwrappedLine();
1381  parseTryCatch();
1382  return;
1383  case tok::identifier: {
1384  if (Style.isCSharp() && FormatTok->is(Keywords.kw_where) &&
1385  Line->MustBeDeclaration) {
1386  addUnwrappedLine();
1387  parseCSharpGenericTypeConstraint();
1388  break;
1389  }
1390  if (FormatTok->is(TT_MacroBlockEnd)) {
1391  addUnwrappedLine();
1392  return;
1393  }
1394 
1395  // Function declarations (as opposed to function expressions) are parsed
1396  // on their own unwrapped line by continuing this loop. Function
1397  // expressions (functions that are not on their own line) must not create
1398  // a new unwrapped line, so they are special cased below.
1399  size_t TokenCount = Line->Tokens.size();
1400  if (Style.Language == FormatStyle::LK_JavaScript &&
1401  FormatTok->is(Keywords.kw_function) &&
1402  (TokenCount > 1 || (TokenCount == 1 && !Line->Tokens.front().Tok->is(
1403  Keywords.kw_async)))) {
1404  tryToParseJSFunction();
1405  break;
1406  }
1407  if ((Style.Language == FormatStyle::LK_JavaScript ||
1408  Style.Language == FormatStyle::LK_Java) &&
1409  FormatTok->is(Keywords.kw_interface)) {
1410  if (Style.Language == FormatStyle::LK_JavaScript) {
1411  // In JavaScript/TypeScript, "interface" can be used as a standalone
1412  // identifier, e.g. in `var interface = 1;`. If "interface" is
1413  // followed by another identifier, it is very like to be an actual
1414  // interface declaration.
1415  unsigned StoredPosition = Tokens->getPosition();
1416  FormatToken *Next = Tokens->getNextToken();
1417  FormatTok = Tokens->setPosition(StoredPosition);
1418  if (Next && !mustBeJSIdent(Keywords, Next)) {
1419  nextToken();
1420  break;
1421  }
1422  }
1423  parseRecord();
1424  addUnwrappedLine();
1425  return;
1426  }
1427 
1428  if (Style.isCpp() && FormatTok->is(TT_StatementMacro)) {
1429  parseStatementMacro();
1430  return;
1431  }
1432 
1433  // See if the following token should start a new unwrapped line.
1434  StringRef Text = FormatTok->TokenText;
1435  nextToken();
1436 
1437  // JS doesn't have macros, and within classes colons indicate fields, not
1438  // labels.
1439  if (Style.Language == FormatStyle::LK_JavaScript)
1440  break;
1441 
1442  TokenCount = Line->Tokens.size();
1443  if (TokenCount == 1 ||
1444  (TokenCount == 2 && Line->Tokens.front().Tok->is(tok::comment))) {
1445  if (FormatTok->Tok.is(tok::colon) && !Line->MustBeDeclaration) {
1446  Line->Tokens.begin()->Tok->MustBreakBefore = true;
1447  parseLabel(!Style.IndentGotoLabels);
1448  return;
1449  }
1450  // Recognize function-like macro usages without trailing semicolon as
1451  // well as free-standing macros like Q_OBJECT.
1452  bool FunctionLike = FormatTok->is(tok::l_paren);
1453  if (FunctionLike)
1454  parseParens();
1455 
1456  bool FollowedByNewline =
1457  CommentsBeforeNextToken.empty()
1458  ? FormatTok->NewlinesBefore > 0
1459  : CommentsBeforeNextToken.front()->NewlinesBefore > 0;
1460 
1461  if (FollowedByNewline && (Text.size() >= 5 || FunctionLike) &&
1462  tokenCanStartNewLine(*FormatTok) && Text == Text.upper()) {
1463  addUnwrappedLine();
1464  return;
1465  }
1466  }
1467  break;
1468  }
1469  case tok::equal:
1470  // Fat arrows (=>) have tok::TokenKind tok::equal but TokenType
1471  // TT_JsFatArrow. The always start an expression or a child block if
1472  // followed by a curly.
1473  if (FormatTok->is(TT_JsFatArrow)) {
1474  nextToken();
1475  if (FormatTok->is(tok::l_brace)) {
1476  // C# may break after => if the next character is a newline.
1477  if (Style.isCSharp() && Style.BraceWrapping.AfterFunction == true) {
1478  // calling `addUnwrappedLine()` here causes odd parsing errors.
1479  FormatTok->MustBreakBefore = true;
1480  }
1481  parseChildBlock();
1482  }
1483  break;
1484  }
1485 
1486  nextToken();
1487  if (FormatTok->Tok.is(tok::l_brace)) {
1488  // Block kind should probably be set to BK_BracedInit for any language.
1489  // C# needs this change to ensure that array initialisers and object
1490  // initialisers are indented the same way.
1491  if (Style.isCSharp())
1492  FormatTok->setBlockKind(BK_BracedInit);
1493  nextToken();
1494  parseBracedList();
1495  } else if (Style.Language == FormatStyle::LK_Proto &&
1496  FormatTok->Tok.is(tok::less)) {
1497  nextToken();
1498  parseBracedList(/*ContinueOnSemicolons=*/false, /*IsEnum=*/false,
1499  /*ClosingBraceKind=*/tok::greater);
1500  }
1501  break;
1502  case tok::l_square:
1503  parseSquare();
1504  break;
1505  case tok::kw_new:
1506  parseNew();
1507  break;
1508  default:
1509  nextToken();
1510  break;
1511  }
1512  } while (!eof());
1513 }
1514 
1515 bool UnwrappedLineParser::tryToParsePropertyAccessor() {
1516  assert(FormatTok->is(tok::l_brace));
1517  if (!Style.isCSharp())
1518  return false;
1519  // See if it's a property accessor.
1520  if (FormatTok->Previous->isNot(tok::identifier))
1521  return false;
1522 
1523  // See if we are inside a property accessor.
1524  //
1525  // Record the current tokenPosition so that we can advance and
1526  // reset the current token. `Next` is not set yet so we need
1527  // another way to advance along the token stream.
1528  unsigned int StoredPosition = Tokens->getPosition();
1529  FormatToken *Tok = Tokens->getNextToken();
1530 
1531  // A trivial property accessor is of the form:
1532  // { [ACCESS_SPECIFIER] [get]; [ACCESS_SPECIFIER] [set] }
1533  // Track these as they do not require line breaks to be introduced.
1534  bool HasGetOrSet = false;
1535  bool IsTrivialPropertyAccessor = true;
1536  while (!eof()) {
1537  if (Tok->isOneOf(tok::semi, tok::kw_public, tok::kw_private,
1538  tok::kw_protected, Keywords.kw_internal, Keywords.kw_get,
1539  Keywords.kw_set)) {
1540  if (Tok->isOneOf(Keywords.kw_get, Keywords.kw_set))
1541  HasGetOrSet = true;
1542  Tok = Tokens->getNextToken();
1543  continue;
1544  }
1545  if (Tok->isNot(tok::r_brace))
1546  IsTrivialPropertyAccessor = false;
1547  break;
1548  }
1549 
1550  if (!HasGetOrSet) {
1551  Tokens->setPosition(StoredPosition);
1552  return false;
1553  }
1554 
1555  // Try to parse the property accessor:
1556  // https://docs.microsoft.com/en-us/dotnet/csharp/programming-guide/classes-and-structs/properties
1557  Tokens->setPosition(StoredPosition);
1558  if (!IsTrivialPropertyAccessor && Style.BraceWrapping.AfterFunction == true)
1559  addUnwrappedLine();
1560  nextToken();
1561  do {
1562  switch (FormatTok->Tok.getKind()) {
1563  case tok::r_brace:
1564  nextToken();
1565  if (FormatTok->is(tok::equal)) {
1566  while (!eof() && FormatTok->isNot(tok::semi))
1567  nextToken();
1568  nextToken();
1569  }
1570  addUnwrappedLine();
1571  return true;
1572  case tok::l_brace:
1573  ++Line->Level;
1574  parseBlock(/*MustBeDeclaration=*/true);
1575  addUnwrappedLine();
1576  --Line->Level;
1577  break;
1578  case tok::equal:
1579  if (FormatTok->is(TT_JsFatArrow)) {
1580  ++Line->Level;
1581  do {
1582  nextToken();
1583  } while (!eof() && FormatTok->isNot(tok::semi));
1584  nextToken();
1585  addUnwrappedLine();
1586  --Line->Level;
1587  break;
1588  }
1589  nextToken();
1590  break;
1591  default:
1592  if (FormatTok->isOneOf(Keywords.kw_get, Keywords.kw_set) &&
1593  !IsTrivialPropertyAccessor) {
1594  // Non-trivial get/set needs to be on its own line.
1595  addUnwrappedLine();
1596  }
1597  nextToken();
1598  }
1599  } while (!eof());
1600 
1601  // Unreachable for well-formed code (paired '{' and '}').
1602  return true;
1603 }
1604 
1605 bool UnwrappedLineParser::tryToParseLambda() {
1606  if (!Style.isCpp()) {
1607  nextToken();
1608  return false;
1609  }
1610  assert(FormatTok->is(tok::l_square));
1611  FormatToken &LSquare = *FormatTok;
1612  if (!tryToParseLambdaIntroducer())
1613  return false;
1614 
1615  bool SeenArrow = false;
1616 
1617  while (FormatTok->isNot(tok::l_brace)) {
1618  if (FormatTok->isSimpleTypeSpecifier()) {
1619  nextToken();
1620  continue;
1621  }
1622  switch (FormatTok->Tok.getKind()) {
1623  case tok::l_brace:
1624  break;
1625  case tok::l_paren:
1626  parseParens();
1627  break;
1628  case tok::amp:
1629  case tok::star:
1630  case tok::kw_const:
1631  case tok::comma:
1632  case tok::less:
1633  case tok::greater:
1634  case tok::identifier:
1635  case tok::numeric_constant:
1636  case tok::coloncolon:
1637  case tok::kw_class:
1638  case tok::kw_mutable:
1639  case tok::kw_noexcept:
1640  case tok::kw_template:
1641  case tok::kw_typename:
1642  nextToken();
1643  break;
1644  // Specialization of a template with an integer parameter can contain
1645  // arithmetic, logical, comparison and ternary operators.
1646  //
1647  // FIXME: This also accepts sequences of operators that are not in the scope
1648  // of a template argument list.
1649  //
1650  // In a C++ lambda a template type can only occur after an arrow. We use
1651  // this as an heuristic to distinguish between Objective-C expressions
1652  // followed by an `a->b` expression, such as:
1653  // ([obj func:arg] + a->b)
1654  // Otherwise the code below would parse as a lambda.
1655  //
1656  // FIXME: This heuristic is incorrect for C++20 generic lambdas with
1657  // explicit template lists: []<bool b = true && false>(U &&u){}
1658  case tok::plus:
1659  case tok::minus:
1660  case tok::exclaim:
1661  case tok::tilde:
1662  case tok::slash:
1663  case tok::percent:
1664  case tok::lessless:
1665  case tok::pipe:
1666  case tok::pipepipe:
1667  case tok::ampamp:
1668  case tok::caret:
1669  case tok::equalequal:
1670  case tok::exclaimequal:
1671  case tok::greaterequal:
1672  case tok::lessequal:
1673  case tok::question:
1674  case tok::colon:
1675  case tok::ellipsis:
1676  case tok::kw_true:
1677  case tok::kw_false:
1678  if (SeenArrow) {
1679  nextToken();
1680  break;
1681  }
1682  return true;
1683  case tok::arrow:
1684  // This might or might not actually be a lambda arrow (this could be an
1685  // ObjC method invocation followed by a dereferencing arrow). We might
1686  // reset this back to TT_Unknown in TokenAnnotator.
1687  FormatTok->setType(TT_LambdaArrow);
1688  SeenArrow = true;
1689  nextToken();
1690  break;
1691  default:
1692  return true;
1693  }
1694  }
1695  FormatTok->setType(TT_LambdaLBrace);
1696  LSquare.setType(TT_LambdaLSquare);
1697  parseChildBlock();
1698  return true;
1699 }
1700 
1701 bool UnwrappedLineParser::tryToParseLambdaIntroducer() {
1702  const FormatToken *Previous = FormatTok->Previous;
1703  if (Previous &&
1704  (Previous->isOneOf(tok::identifier, tok::kw_operator, tok::kw_new,
1705  tok::kw_delete, tok::l_square) ||
1706  FormatTok->isCppStructuredBinding(Style) || Previous->closesScope() ||
1707  Previous->isSimpleTypeSpecifier())) {
1708  nextToken();
1709  return false;
1710  }
1711  nextToken();
1712  if (FormatTok->is(tok::l_square)) {
1713  return false;
1714  }
1715  parseSquare(/*LambdaIntroducer=*/true);
1716  return true;
1717 }
1718 
1719 void UnwrappedLineParser::tryToParseJSFunction() {
1720  assert(FormatTok->is(Keywords.kw_function) ||
1721  FormatTok->startsSequence(Keywords.kw_async, Keywords.kw_function));
1722  if (FormatTok->is(Keywords.kw_async))
1723  nextToken();
1724  // Consume "function".
1725  nextToken();
1726 
1727  // Consume * (generator function). Treat it like C++'s overloaded operators.
1728  if (FormatTok->is(tok::star)) {
1729  FormatTok->setType(TT_OverloadedOperator);
1730  nextToken();
1731  }
1732 
1733  // Consume function name.
1734  if (FormatTok->is(tok::identifier))
1735  nextToken();
1736 
1737  if (FormatTok->isNot(tok::l_paren))
1738  return;
1739 
1740  // Parse formal parameter list.
1741  parseParens();
1742 
1743  if (FormatTok->is(tok::colon)) {
1744  // Parse a type definition.
1745  nextToken();
1746 
1747  // Eat the type declaration. For braced inline object types, balance braces,
1748  // otherwise just parse until finding an l_brace for the function body.
1749  if (FormatTok->is(tok::l_brace))
1750  tryToParseBracedList();
1751  else
1752  while (!FormatTok->isOneOf(tok::l_brace, tok::semi) && !eof())
1753  nextToken();
1754  }
1755 
1756  if (FormatTok->is(tok::semi))
1757  return;
1758 
1759  parseChildBlock();
1760 }
1761 
1762 bool UnwrappedLineParser::tryToParseBracedList() {
1763  if (FormatTok->is(BK_Unknown))
1764  calculateBraceTypes();
1765  assert(FormatTok->isNot(BK_Unknown));
1766  if (FormatTok->is(BK_Block))
1767  return false;
1768  nextToken();
1769  parseBracedList();
1770  return true;
1771 }
1772 
1773 bool UnwrappedLineParser::parseBracedList(bool ContinueOnSemicolons,
1774  bool IsEnum,
1775  tok::TokenKind ClosingBraceKind) {
1776  bool HasError = false;
1777 
1778  // FIXME: Once we have an expression parser in the UnwrappedLineParser,
1779  // replace this by using parseAssigmentExpression() inside.
1780  do {
1781  if (Style.isCSharp()) {
1782  if (FormatTok->is(TT_JsFatArrow)) {
1783  nextToken();
1784  // Fat arrows can be followed by simple expressions or by child blocks
1785  // in curly braces.
1786  if (FormatTok->is(tok::l_brace)) {
1787  parseChildBlock();
1788  continue;
1789  }
1790  }
1791  }
1792  if (Style.Language == FormatStyle::LK_JavaScript) {
1793  if (FormatTok->is(Keywords.kw_function) ||
1794  FormatTok->startsSequence(Keywords.kw_async, Keywords.kw_function)) {
1795  tryToParseJSFunction();
1796  continue;
1797  }
1798  if (FormatTok->is(TT_JsFatArrow)) {
1799  nextToken();
1800  // Fat arrows can be followed by simple expressions or by child blocks
1801  // in curly braces.
1802  if (FormatTok->is(tok::l_brace)) {
1803  parseChildBlock();
1804  continue;
1805  }
1806  }
1807  if (FormatTok->is(tok::l_brace)) {
1808  // Could be a method inside of a braced list `{a() { return 1; }}`.
1809  if (tryToParseBracedList())
1810  continue;
1811  parseChildBlock();
1812  }
1813  }
1814  if (FormatTok->Tok.getKind() == ClosingBraceKind) {
1815  if (IsEnum && !Style.AllowShortEnumsOnASingleLine)
1816  addUnwrappedLine();
1817  nextToken();
1818  return !HasError;
1819  }
1820  switch (FormatTok->Tok.getKind()) {
1821  case tok::caret:
1822  nextToken();
1823  if (FormatTok->is(tok::l_brace)) {
1824  parseChildBlock();
1825  }
1826  break;
1827  case tok::l_square:
1828  if (Style.isCSharp())
1829  parseSquare();
1830  else
1831  tryToParseLambda();
1832  break;
1833  case tok::l_paren:
1834  parseParens();
1835  // JavaScript can just have free standing methods and getters/setters in
1836  // object literals. Detect them by a "{" following ")".
1837  if (Style.Language == FormatStyle::LK_JavaScript) {
1838  if (FormatTok->is(tok::l_brace))
1839  parseChildBlock();
1840  break;
1841  }
1842  break;
1843  case tok::l_brace:
1844  // Assume there are no blocks inside a braced init list apart
1845  // from the ones we explicitly parse out (like lambdas).
1846  FormatTok->setBlockKind(BK_BracedInit);
1847  nextToken();
1848  parseBracedList();
1849  break;
1850  case tok::less:
1851  if (Style.Language == FormatStyle::LK_Proto) {
1852  nextToken();
1853  parseBracedList(/*ContinueOnSemicolons=*/false, /*IsEnum=*/false,
1854  /*ClosingBraceKind=*/tok::greater);
1855  } else {
1856  nextToken();
1857  }
1858  break;
1859  case tok::semi:
1860  // JavaScript (or more precisely TypeScript) can have semicolons in braced
1861  // lists (in so-called TypeMemberLists). Thus, the semicolon cannot be
1862  // used for error recovery if we have otherwise determined that this is
1863  // a braced list.
1864  if (Style.Language == FormatStyle::LK_JavaScript) {
1865  nextToken();
1866  break;
1867  }
1868  HasError = true;
1869  if (!ContinueOnSemicolons)
1870  return !HasError;
1871  nextToken();
1872  break;
1873  case tok::comma:
1874  nextToken();
1875  if (IsEnum && !Style.AllowShortEnumsOnASingleLine)
1876  addUnwrappedLine();
1877  break;
1878  default:
1879  nextToken();
1880  break;
1881  }
1882  } while (!eof());
1883  return false;
1884 }
1885 
1886 void UnwrappedLineParser::parseParens() {
1887  assert(FormatTok->Tok.is(tok::l_paren) && "'(' expected.");
1888  nextToken();
1889  do {
1890  switch (FormatTok->Tok.getKind()) {
1891  case tok::l_paren:
1892  parseParens();
1893  if (Style.Language == FormatStyle::LK_Java && FormatTok->is(tok::l_brace))
1894  parseChildBlock();
1895  break;
1896  case tok::r_paren:
1897  nextToken();
1898  return;
1899  case tok::r_brace:
1900  // A "}" inside parenthesis is an error if there wasn't a matching "{".
1901  return;
1902  case tok::l_square:
1903  tryToParseLambda();
1904  break;
1905  case tok::l_brace:
1906  if (!tryToParseBracedList())
1907  parseChildBlock();
1908  break;
1909  case tok::at:
1910  nextToken();
1911  if (FormatTok->Tok.is(tok::l_brace)) {
1912  nextToken();
1913  parseBracedList();
1914  }
1915  break;
1916  case tok::kw_class:
1917  if (Style.Language == FormatStyle::LK_JavaScript)
1918  parseRecord(/*ParseAsExpr=*/true);
1919  else
1920  nextToken();
1921  break;
1922  case tok::identifier:
1923  if (Style.Language == FormatStyle::LK_JavaScript &&
1924  (FormatTok->is(Keywords.kw_function) ||
1925  FormatTok->startsSequence(Keywords.kw_async, Keywords.kw_function)))
1926  tryToParseJSFunction();
1927  else
1928  nextToken();
1929  break;
1930  default:
1931  nextToken();
1932  break;
1933  }
1934  } while (!eof());
1935 }
1936 
1937 void UnwrappedLineParser::parseSquare(bool LambdaIntroducer) {
1938  if (!LambdaIntroducer) {
1939  assert(FormatTok->Tok.is(tok::l_square) && "'[' expected.");
1940  if (tryToParseLambda())
1941  return;
1942  }
1943  do {
1944  switch (FormatTok->Tok.getKind()) {
1945  case tok::l_paren:
1946  parseParens();
1947  break;
1948  case tok::r_square:
1949  nextToken();
1950  return;
1951  case tok::r_brace:
1952  // A "}" inside parenthesis is an error if there wasn't a matching "{".
1953  return;
1954  case tok::l_square:
1955  parseSquare();
1956  break;
1957  case tok::l_brace: {
1958  if (!tryToParseBracedList())
1959  parseChildBlock();
1960  break;
1961  }
1962  case tok::at:
1963  nextToken();
1964  if (FormatTok->Tok.is(tok::l_brace)) {
1965  nextToken();
1966  parseBracedList();
1967  }
1968  break;
1969  default:
1970  nextToken();
1971  break;
1972  }
1973  } while (!eof());
1974 }
1975 
1976 void UnwrappedLineParser::parseIfThenElse() {
1977  assert(FormatTok->Tok.is(tok::kw_if) && "'if' expected");
1978  nextToken();
1979  if (FormatTok->Tok.isOneOf(tok::kw_constexpr, tok::identifier))
1980  nextToken();
1981  if (FormatTok->Tok.is(tok::l_paren))
1982  parseParens();
1983  // handle [[likely]] / [[unlikely]]
1984  if (FormatTok->is(tok::l_square) && tryToParseSimpleAttribute())
1985  parseSquare();
1986  bool NeedsUnwrappedLine = false;
1987  if (FormatTok->Tok.is(tok::l_brace)) {
1988  CompoundStatementIndenter Indenter(this, Style, Line->Level);
1989  parseBlock(/*MustBeDeclaration=*/false);
1990  if (Style.BraceWrapping.BeforeElse)
1991  addUnwrappedLine();
1992  else
1993  NeedsUnwrappedLine = true;
1994  } else {
1995  addUnwrappedLine();
1996  ++Line->Level;
1997  parseStructuralElement();
1998  --Line->Level;
1999  }
2000  if (FormatTok->Tok.is(tok::kw_else)) {
2001  nextToken();
2002  // handle [[likely]] / [[unlikely]]
2003  if (FormatTok->Tok.is(tok::l_square) && tryToParseSimpleAttribute())
2004  parseSquare();
2005  if (FormatTok->Tok.is(tok::l_brace)) {
2006  CompoundStatementIndenter Indenter(this, Style, Line->Level);
2007  parseBlock(/*MustBeDeclaration=*/false);
2008  addUnwrappedLine();
2009  } else if (FormatTok->Tok.is(tok::kw_if)) {
2010  parseIfThenElse();
2011  } else {
2012  addUnwrappedLine();
2013  ++Line->Level;
2014  parseStructuralElement();
2015  if (FormatTok->is(tok::eof))
2016  addUnwrappedLine();
2017  --Line->Level;
2018  }
2019  } else if (NeedsUnwrappedLine) {
2020  addUnwrappedLine();
2021  }
2022 }
2023 
2024 void UnwrappedLineParser::parseTryCatch() {
2025  assert(FormatTok->isOneOf(tok::kw_try, tok::kw___try) && "'try' expected");
2026  nextToken();
2027  bool NeedsUnwrappedLine = false;
2028  if (FormatTok->is(tok::colon)) {
2029  // We are in a function try block, what comes is an initializer list.
2030  nextToken();
2031 
2032  // In case identifiers were removed by clang-tidy, what might follow is
2033  // multiple commas in sequence - before the first identifier.
2034  while (FormatTok->is(tok::comma))
2035  nextToken();
2036 
2037  while (FormatTok->is(tok::identifier)) {
2038  nextToken();
2039  if (FormatTok->is(tok::l_paren))
2040  parseParens();
2041  if (FormatTok->Previous && FormatTok->Previous->is(tok::identifier) &&
2042  FormatTok->is(tok::l_brace)) {
2043  do {
2044  nextToken();
2045  } while (!FormatTok->is(tok::r_brace));
2046  nextToken();
2047  }
2048 
2049  // In case identifiers were removed by clang-tidy, what might follow is
2050  // multiple commas in sequence - after the first identifier.
2051  while (FormatTok->is(tok::comma))
2052  nextToken();
2053  }
2054  }
2055  // Parse try with resource.
2056  if (Style.Language == FormatStyle::LK_Java && FormatTok->is(tok::l_paren)) {
2057  parseParens();
2058  }
2059  if (FormatTok->is(tok::l_brace)) {
2060  CompoundStatementIndenter Indenter(this, Style, Line->Level);
2061  parseBlock(/*MustBeDeclaration=*/false);
2062  if (Style.BraceWrapping.BeforeCatch) {
2063  addUnwrappedLine();
2064  } else {
2065  NeedsUnwrappedLine = true;
2066  }
2067  } else if (!FormatTok->is(tok::kw_catch)) {
2068  // The C++ standard requires a compound-statement after a try.
2069  // If there's none, we try to assume there's a structuralElement
2070  // and try to continue.
2071  addUnwrappedLine();
2072  ++Line->Level;
2073  parseStructuralElement();
2074  --Line->Level;
2075  }
2076  while (1) {
2077  if (FormatTok->is(tok::at))
2078  nextToken();
2079  if (!(FormatTok->isOneOf(tok::kw_catch, Keywords.kw___except,
2080  tok::kw___finally) ||
2081  ((Style.Language == FormatStyle::LK_Java ||
2082  Style.Language == FormatStyle::LK_JavaScript) &&
2083  FormatTok->is(Keywords.kw_finally)) ||
2084  (FormatTok->Tok.isObjCAtKeyword(tok::objc_catch) ||
2085  FormatTok->Tok.isObjCAtKeyword(tok::objc_finally))))
2086  break;
2087  nextToken();
2088  while (FormatTok->isNot(tok::l_brace)) {
2089  if (FormatTok->is(tok::l_paren)) {
2090  parseParens();
2091  continue;
2092  }
2093  if (FormatTok->isOneOf(tok::semi, tok::r_brace, tok::eof))
2094  return;
2095  nextToken();
2096  }
2097  NeedsUnwrappedLine = false;
2098  CompoundStatementIndenter Indenter(this, Style, Line->Level);
2099  parseBlock(/*MustBeDeclaration=*/false);
2100  if (Style.BraceWrapping.BeforeCatch)
2101  addUnwrappedLine();
2102  else
2103  NeedsUnwrappedLine = true;
2104  }
2105  if (NeedsUnwrappedLine)
2106  addUnwrappedLine();
2107 }
2108 
2109 void UnwrappedLineParser::parseNamespace() {
2110  assert(FormatTok->isOneOf(tok::kw_namespace, TT_NamespaceMacro) &&
2111  "'namespace' expected");
2112 
2113  const FormatToken &InitialToken = *FormatTok;
2114  nextToken();
2115  if (InitialToken.is(TT_NamespaceMacro)) {
2116  parseParens();
2117  } else {
2118  while (FormatTok->isOneOf(tok::identifier, tok::coloncolon, tok::kw_inline,
2119  tok::l_square)) {
2120  if (FormatTok->is(tok::l_square))
2121  parseSquare();
2122  else
2123  nextToken();
2124  }
2125  }
2126  if (FormatTok->Tok.is(tok::l_brace)) {
2127  if (ShouldBreakBeforeBrace(Style, InitialToken))
2128  addUnwrappedLine();
2129 
2130  unsigned AddLevels =
2131  Style.NamespaceIndentation == FormatStyle::NI_All ||
2132  (Style.NamespaceIndentation == FormatStyle::NI_Inner &&
2133  DeclarationScopeStack.size() > 1)
2134  ? 1u
2135  : 0u;
2136  parseBlock(/*MustBeDeclaration=*/true, AddLevels);
2137  // Munch the semicolon after a namespace. This is more common than one would
2138  // think. Putting the semicolon into its own line is very ugly.
2139  if (FormatTok->Tok.is(tok::semi))
2140  nextToken();
2141  addUnwrappedLine();
2142  }
2143  // FIXME: Add error handling.
2144 }
2145 
2146 void UnwrappedLineParser::parseNew() {
2147  assert(FormatTok->is(tok::kw_new) && "'new' expected");
2148  nextToken();
2149 
2150  if (Style.isCSharp()) {
2151  do {
2152  if (FormatTok->is(tok::l_brace))
2153  parseBracedList();
2154 
2155  if (FormatTok->isOneOf(tok::semi, tok::comma))
2156  return;
2157 
2158  nextToken();
2159  } while (!eof());
2160  }
2161 
2162  if (Style.Language != FormatStyle::LK_Java)
2163  return;
2164 
2165  // In Java, we can parse everything up to the parens, which aren't optional.
2166  do {
2167  // There should not be a ;, { or } before the new's open paren.
2168  if (FormatTok->isOneOf(tok::semi, tok::l_brace, tok::r_brace))
2169  return;
2170 
2171  // Consume the parens.
2172  if (FormatTok->is(tok::l_paren)) {
2173  parseParens();
2174 
2175  // If there is a class body of an anonymous class, consume that as child.
2176  if (FormatTok->is(tok::l_brace))
2177  parseChildBlock();
2178  return;
2179  }
2180  nextToken();
2181  } while (!eof());
2182 }
2183 
2184 void UnwrappedLineParser::parseForOrWhileLoop() {
2185  assert(FormatTok->isOneOf(tok::kw_for, tok::kw_while, TT_ForEachMacro) &&
2186  "'for', 'while' or foreach macro expected");
2187  nextToken();
2188  // JS' for await ( ...
2189  if (Style.Language == FormatStyle::LK_JavaScript &&
2190  FormatTok->is(Keywords.kw_await))
2191  nextToken();
2192  if (FormatTok->Tok.is(tok::l_paren))
2193  parseParens();
2194  if (FormatTok->Tok.is(tok::l_brace)) {
2195  CompoundStatementIndenter Indenter(this, Style, Line->Level);
2196  parseBlock(/*MustBeDeclaration=*/false);
2197  addUnwrappedLine();
2198  } else {
2199  addUnwrappedLine();
2200  ++Line->Level;
2201  parseStructuralElement();
2202  --Line->Level;
2203  }
2204 }
2205 
2206 void UnwrappedLineParser::parseDoWhile() {
2207  assert(FormatTok->Tok.is(tok::kw_do) && "'do' expected");
2208  nextToken();
2209  if (FormatTok->Tok.is(tok::l_brace)) {
2210  CompoundStatementIndenter Indenter(this, Style, Line->Level);
2211  parseBlock(/*MustBeDeclaration=*/false);
2212  if (Style.BraceWrapping.BeforeWhile)
2213  addUnwrappedLine();
2214  } else {
2215  addUnwrappedLine();
2216  ++Line->Level;
2217  parseStructuralElement();
2218  --Line->Level;
2219  }
2220 
2221  // FIXME: Add error handling.
2222  if (!FormatTok->Tok.is(tok::kw_while)) {
2223  addUnwrappedLine();
2224  return;
2225  }
2226 
2227  nextToken();
2228  parseStructuralElement();
2229 }
2230 
2231 void UnwrappedLineParser::parseLabel(bool LeftAlignLabel) {
2232  nextToken();
2233  unsigned OldLineLevel = Line->Level;
2234  if (Line->Level > 1 || (!Line->InPPDirective && Line->Level > 0))
2235  --Line->Level;
2236  if (LeftAlignLabel)
2237  Line->Level = 0;
2238 
2239  bool RemoveWhitesmithsCaseIndent =
2240  (!Style.IndentCaseBlocks &&
2241  Style.BreakBeforeBraces == FormatStyle::BS_Whitesmiths);
2242 
2243  if (RemoveWhitesmithsCaseIndent)
2244  --Line->Level;
2245 
2246  if (!Style.IndentCaseBlocks && CommentsBeforeNextToken.empty() &&
2247  FormatTok->Tok.is(tok::l_brace)) {
2248 
2250  this, Line->Level, Style.BraceWrapping.AfterCaseLabel,
2251  Style.BraceWrapping.IndentBraces || RemoveWhitesmithsCaseIndent);
2252  parseBlock(/*MustBeDeclaration=*/false);
2253  if (FormatTok->Tok.is(tok::kw_break)) {
2254  if (Style.BraceWrapping.AfterControlStatement ==
2255  FormatStyle::BWACS_Always) {
2256  addUnwrappedLine();
2257  if (RemoveWhitesmithsCaseIndent) {
2258  Line->Level++;
2259  }
2260  }
2261  parseStructuralElement();
2262  }
2263  addUnwrappedLine();
2264  } else {
2265  if (FormatTok->is(tok::semi))
2266  nextToken();
2267  addUnwrappedLine();
2268  }
2269  Line->Level = OldLineLevel;
2270  if (FormatTok->isNot(tok::l_brace)) {
2271  parseStructuralElement();
2272  addUnwrappedLine();
2273  }
2274 }
2275 
2276 void UnwrappedLineParser::parseCaseLabel() {
2277  assert(FormatTok->Tok.is(tok::kw_case) && "'case' expected");
2278 
2279  // FIXME: fix handling of complex expressions here.
2280  do {
2281  nextToken();
2282  } while (!eof() && !FormatTok->Tok.is(tok::colon));
2283  parseLabel();
2284 }
2285 
2286 void UnwrappedLineParser::parseSwitch() {
2287  assert(FormatTok->Tok.is(tok::kw_switch) && "'switch' expected");
2288  nextToken();
2289  if (FormatTok->Tok.is(tok::l_paren))
2290  parseParens();
2291  if (FormatTok->Tok.is(tok::l_brace)) {
2292  CompoundStatementIndenter Indenter(this, Style, Line->Level);
2293  parseBlock(/*MustBeDeclaration=*/false);
2294  addUnwrappedLine();
2295  } else {
2296  addUnwrappedLine();
2297  ++Line->Level;
2298  parseStructuralElement();
2299  --Line->Level;
2300  }
2301 }
2302 
2303 void UnwrappedLineParser::parseAccessSpecifier() {
2304  nextToken();
2305  // Understand Qt's slots.
2306  if (FormatTok->isOneOf(Keywords.kw_slots, Keywords.kw_qslots))
2307  nextToken();
2308  // Otherwise, we don't know what it is, and we'd better keep the next token.
2309  if (FormatTok->Tok.is(tok::colon))
2310  nextToken();
2311  addUnwrappedLine();
2312 }
2313 
2314 void UnwrappedLineParser::parseConcept() {
2315  assert(FormatTok->Tok.is(tok::kw_concept) && "'concept' expected");
2316  nextToken();
2317  if (!FormatTok->Tok.is(tok::identifier))
2318  return;
2319  nextToken();
2320  if (!FormatTok->Tok.is(tok::equal))
2321  return;
2322  nextToken();
2323  if (FormatTok->Tok.is(tok::kw_requires)) {
2324  nextToken();
2325  parseRequiresExpression(Line->Level);
2326  } else {
2327  parseConstraintExpression(Line->Level);
2328  }
2329 }
2330 
2331 void UnwrappedLineParser::parseRequiresExpression(unsigned int OriginalLevel) {
2332  // requires (R range)
2333  if (FormatTok->Tok.is(tok::l_paren)) {
2334  parseParens();
2335  if (Style.IndentRequires && OriginalLevel != Line->Level) {
2336  addUnwrappedLine();
2337  --Line->Level;
2338  }
2339  }
2340 
2341  if (FormatTok->Tok.is(tok::l_brace)) {
2342  if (Style.BraceWrapping.AfterFunction)
2343  addUnwrappedLine();
2344  FormatTok->setType(TT_FunctionLBrace);
2345  parseBlock(/*MustBeDeclaration=*/false);
2346  addUnwrappedLine();
2347  } else {
2348  parseConstraintExpression(OriginalLevel);
2349  }
2350 }
2351 
2352 void UnwrappedLineParser::parseConstraintExpression(
2353  unsigned int OriginalLevel) {
2354  // requires Id<T> && Id<T> || Id<T>
2355  while (
2356  FormatTok->isOneOf(tok::identifier, tok::kw_requires, tok::coloncolon)) {
2357  nextToken();
2358  while (FormatTok->isOneOf(tok::identifier, tok::coloncolon, tok::less,
2359  tok::greater, tok::comma, tok::ellipsis)) {
2360  if (FormatTok->Tok.is(tok::less)) {
2361  parseBracedList(/*ContinueOnSemicolons=*/false, /*IsEnum=*/false,
2362  /*ClosingBraceKind=*/tok::greater);
2363  continue;
2364  }
2365  nextToken();
2366  }
2367  if (FormatTok->Tok.is(tok::kw_requires)) {
2368  parseRequiresExpression(OriginalLevel);
2369  }
2370  if (FormatTok->Tok.is(tok::less)) {
2371  parseBracedList(/*ContinueOnSemicolons=*/false, /*IsEnum=*/false,
2372  /*ClosingBraceKind=*/tok::greater);
2373  }
2374 
2375  if (FormatTok->Tok.is(tok::l_paren)) {
2376  parseParens();
2377  }
2378  if (FormatTok->Tok.is(tok::l_brace)) {
2379  if (Style.BraceWrapping.AfterFunction)
2380  addUnwrappedLine();
2381  FormatTok->setType(TT_FunctionLBrace);
2382  parseBlock(/*MustBeDeclaration=*/false);
2383  }
2384  if (FormatTok->Tok.is(tok::semi)) {
2385  // Eat any trailing semi.
2386  nextToken();
2387  addUnwrappedLine();
2388  }
2389  if (FormatTok->Tok.is(tok::colon)) {
2390  return;
2391  }
2392  if (!FormatTok->Tok.isOneOf(tok::ampamp, tok::pipepipe)) {
2393  if (FormatTok->Previous &&
2394  !FormatTok->Previous->isOneOf(tok::identifier, tok::kw_requires,
2395  tok::coloncolon)) {
2396  addUnwrappedLine();
2397  }
2398  if (Style.IndentRequires && OriginalLevel != Line->Level) {
2399  --Line->Level;
2400  }
2401  break;
2402  } else {
2403  FormatTok->setType(TT_ConstraintJunctions);
2404  }
2405 
2406  nextToken();
2407  }
2408 }
2409 
2410 void UnwrappedLineParser::parseRequires() {
2411  assert(FormatTok->Tok.is(tok::kw_requires) && "'requires' expected");
2412 
2413  unsigned OriginalLevel = Line->Level;
2414  if (FormatTok->Previous && FormatTok->Previous->is(tok::greater)) {
2415  addUnwrappedLine();
2416  if (Style.IndentRequires) {
2417  Line->Level++;
2418  }
2419  }
2420  nextToken();
2421 
2422  parseRequiresExpression(OriginalLevel);
2423 }
2424 
2425 bool UnwrappedLineParser::parseEnum() {
2426  // Won't be 'enum' for NS_ENUMs.
2427  if (FormatTok->Tok.is(tok::kw_enum))
2428  nextToken();
2429 
2430  // In TypeScript, "enum" can also be used as property name, e.g. in interface
2431  // declarations. An "enum" keyword followed by a colon would be a syntax
2432  // error and thus assume it is just an identifier.
2433  if (Style.Language == FormatStyle::LK_JavaScript &&
2434  FormatTok->isOneOf(tok::colon, tok::question))
2435  return false;
2436 
2437  // In protobuf, "enum" can be used as a field name.
2438  if (Style.Language == FormatStyle::LK_Proto && FormatTok->is(tok::equal))
2439  return false;
2440 
2441  // Eat up enum class ...
2442  if (FormatTok->Tok.is(tok::kw_class) || FormatTok->Tok.is(tok::kw_struct))
2443  nextToken();
2444 
2445  while (FormatTok->Tok.getIdentifierInfo() ||
2446  FormatTok->isOneOf(tok::colon, tok::coloncolon, tok::less,
2447  tok::greater, tok::comma, tok::question)) {
2448  nextToken();
2449  // We can have macros or attributes in between 'enum' and the enum name.
2450  if (FormatTok->is(tok::l_paren))
2451  parseParens();
2452  if (FormatTok->is(tok::identifier)) {
2453  nextToken();
2454  // If there are two identifiers in a row, this is likely an elaborate
2455  // return type. In Java, this can be "implements", etc.
2456  if (Style.isCpp() && FormatTok->is(tok::identifier))
2457  return false;
2458  }
2459  }
2460 
2461  // Just a declaration or something is wrong.
2462  if (FormatTok->isNot(tok::l_brace))
2463  return true;
2464  FormatTok->setBlockKind(BK_Block);
2465 
2466  if (Style.Language == FormatStyle::LK_Java) {
2467  // Java enums are different.
2468  parseJavaEnumBody();
2469  return true;
2470  }
2471  if (Style.Language == FormatStyle::LK_Proto) {
2472  parseBlock(/*MustBeDeclaration=*/true);
2473  return true;
2474  }
2475 
2476  if (!Style.AllowShortEnumsOnASingleLine)
2477  addUnwrappedLine();
2478  // Parse enum body.
2479  nextToken();
2480  if (!Style.AllowShortEnumsOnASingleLine) {
2481  addUnwrappedLine();
2482  Line->Level += 1;
2483  }
2484  bool HasError = !parseBracedList(/*ContinueOnSemicolons=*/true,
2485  /*IsEnum=*/true);
2486  if (!Style.AllowShortEnumsOnASingleLine)
2487  Line->Level -= 1;
2488  if (HasError) {
2489  if (FormatTok->is(tok::semi))
2490  nextToken();
2491  addUnwrappedLine();
2492  }
2493  return true;
2494 
2495  // There is no addUnwrappedLine() here so that we fall through to parsing a
2496  // structural element afterwards. Thus, in "enum A {} n, m;",
2497  // "} n, m;" will end up in one unwrapped line.
2498 }
2499 
2500 namespace {
2501 // A class used to set and restore the Token position when peeking
2502 // ahead in the token source.
2503 class ScopedTokenPosition {
2504  unsigned StoredPosition;
2505  FormatTokenSource *Tokens;
2506 
2507 public:
2508  ScopedTokenPosition(FormatTokenSource *Tokens) : Tokens(Tokens) {
2509  assert(Tokens && "Tokens expected to not be null");
2510  StoredPosition = Tokens->getPosition();
2511  }
2512 
2513  ~ScopedTokenPosition() { Tokens->setPosition(StoredPosition); }
2514 };
2515 } // namespace
2516 
2517 // Look to see if we have [[ by looking ahead, if
2518 // its not then rewind to the original position.
2519 bool UnwrappedLineParser::tryToParseSimpleAttribute() {
2520  ScopedTokenPosition AutoPosition(Tokens);
2521  FormatToken *Tok = Tokens->getNextToken();
2522  // We already read the first [ check for the second.
2523  if (Tok && !Tok->is(tok::l_square)) {
2524  return false;
2525  }
2526  // Double check that the attribute is just something
2527  // fairly simple.
2528  while (Tok) {
2529  if (Tok->is(tok::r_square)) {
2530  break;
2531  }
2532  Tok = Tokens->getNextToken();
2533  }
2534  Tok = Tokens->getNextToken();
2535  if (Tok && !Tok->is(tok::r_square)) {
2536  return false;
2537  }
2538  Tok = Tokens->getNextToken();
2539  if (Tok && Tok->is(tok::semi)) {
2540  return false;
2541  }
2542  return true;
2543 }
2544 
2545 void UnwrappedLineParser::parseJavaEnumBody() {
2546  // Determine whether the enum is simple, i.e. does not have a semicolon or
2547  // constants with class bodies. Simple enums can be formatted like braced
2548  // lists, contracted to a single line, etc.
2549  unsigned StoredPosition = Tokens->getPosition();
2550  bool IsSimple = true;
2551  FormatToken *Tok = Tokens->getNextToken();
2552  while (Tok) {
2553  if (Tok->is(tok::r_brace))
2554  break;
2555  if (Tok->isOneOf(tok::l_brace, tok::semi)) {
2556  IsSimple = false;
2557  break;
2558  }
2559  // FIXME: This will also mark enums with braces in the arguments to enum
2560  // constants as "not simple". This is probably fine in practice, though.
2561  Tok = Tokens->getNextToken();
2562  }
2563  FormatTok = Tokens->setPosition(StoredPosition);
2564 
2565  if (IsSimple) {
2566  nextToken();
2567  parseBracedList();
2568  addUnwrappedLine();
2569  return;
2570  }
2571 
2572  // Parse the body of a more complex enum.
2573  // First add a line for everything up to the "{".
2574  nextToken();
2575  addUnwrappedLine();
2576  ++Line->Level;
2577 
2578  // Parse the enum constants.
2579  while (FormatTok) {
2580  if (FormatTok->is(tok::l_brace)) {
2581  // Parse the constant's class body.
2582  parseBlock(/*MustBeDeclaration=*/true, /*AddLevels=*/1u,
2583  /*MunchSemi=*/false);
2584  } else if (FormatTok->is(tok::l_paren)) {
2585  parseParens();
2586  } else if (FormatTok->is(tok::comma)) {
2587  nextToken();
2588  addUnwrappedLine();
2589  } else if (FormatTok->is(tok::semi)) {
2590  nextToken();
2591  addUnwrappedLine();
2592  break;
2593  } else if (FormatTok->is(tok::r_brace)) {
2594  addUnwrappedLine();
2595  break;
2596  } else {
2597  nextToken();
2598  }
2599  }
2600 
2601  // Parse the class body after the enum's ";" if any.
2602  parseLevel(/*HasOpeningBrace=*/true);
2603  nextToken();
2604  --Line->Level;
2605  addUnwrappedLine();
2606 }
2607 
2608 void UnwrappedLineParser::parseRecord(bool ParseAsExpr) {
2609  const FormatToken &InitialToken = *FormatTok;
2610  nextToken();
2611 
2612  // The actual identifier can be a nested name specifier, and in macros
2613  // it is often token-pasted.
2614  // An [[attribute]] can be before the identifier.
2615  while (FormatTok->isOneOf(tok::identifier, tok::coloncolon, tok::hashhash,
2616  tok::kw___attribute, tok::kw___declspec,
2617  tok::kw_alignas, tok::l_square, tok::r_square) ||
2618  ((Style.Language == FormatStyle::LK_Java ||
2619  Style.Language == FormatStyle::LK_JavaScript) &&
2620  FormatTok->isOneOf(tok::period, tok::comma))) {
2621  if (Style.Language == FormatStyle::LK_JavaScript &&
2622  FormatTok->isOneOf(Keywords.kw_extends, Keywords.kw_implements)) {
2623  // JavaScript/TypeScript supports inline object types in
2624  // extends/implements positions:
2625  // class Foo implements {bar: number} { }
2626  nextToken();
2627  if (FormatTok->is(tok::l_brace)) {
2628  tryToParseBracedList();
2629  continue;
2630  }
2631  }
2632  bool IsNonMacroIdentifier =
2633  FormatTok->is(tok::identifier) &&
2634  FormatTok->TokenText != FormatTok->TokenText.upper();
2635  nextToken();
2636  // We can have macros or attributes in between 'class' and the class name.
2637  if (!IsNonMacroIdentifier) {
2638  if (FormatTok->Tok.is(tok::l_paren)) {
2639  parseParens();
2640  } else if (FormatTok->is(TT_AttributeSquare)) {
2641  parseSquare();
2642  // Consume the closing TT_AttributeSquare.
2643  if (FormatTok->Next && FormatTok->is(TT_AttributeSquare))
2644  nextToken();
2645  }
2646  }
2647  }
2648 
2649  // Note that parsing away template declarations here leads to incorrectly
2650  // accepting function declarations as record declarations.
2651  // In general, we cannot solve this problem. Consider:
2652  // class A<int> B() {}
2653  // which can be a function definition or a class definition when B() is a
2654  // macro. If we find enough real-world cases where this is a problem, we
2655  // can parse for the 'template' keyword in the beginning of the statement,
2656  // and thus rule out the record production in case there is no template
2657  // (this would still leave us with an ambiguity between template function
2658  // and class declarations).
2659  if (FormatTok->isOneOf(tok::colon, tok::less)) {
2660  while (!eof()) {
2661  if (FormatTok->is(tok::l_brace)) {
2662  calculateBraceTypes(/*ExpectClassBody=*/true);
2663  if (!tryToParseBracedList())
2664  break;
2665  }
2666  if (FormatTok->Tok.is(tok::semi))
2667  return;
2668  if (Style.isCSharp() && FormatTok->is(Keywords.kw_where)) {
2669  addUnwrappedLine();
2670  nextToken();
2671  parseCSharpGenericTypeConstraint();
2672  break;
2673  }
2674  nextToken();
2675  }
2676  }
2677  if (FormatTok->Tok.is(tok::l_brace)) {
2678  if (ParseAsExpr) {
2679  parseChildBlock();
2680  } else {
2681  if (ShouldBreakBeforeBrace(Style, InitialToken))
2682  addUnwrappedLine();
2683 
2684  unsigned AddLevels = Style.IndentAccessModifiers ? 2u : 1u;
2685  parseBlock(/*MustBeDeclaration=*/true, AddLevels, /*MunchSemi=*/false);
2686  }
2687  }
2688  // There is no addUnwrappedLine() here so that we fall through to parsing a
2689  // structural element afterwards. Thus, in "class A {} n, m;",
2690  // "} n, m;" will end up in one unwrapped line.
2691 }
2692 
2693 void UnwrappedLineParser::parseObjCMethod() {
2694  assert(FormatTok->Tok.isOneOf(tok::l_paren, tok::identifier) &&
2695  "'(' or identifier expected.");
2696  do {
2697  if (FormatTok->Tok.is(tok::semi)) {
2698  nextToken();
2699  addUnwrappedLine();
2700  return;
2701  } else if (FormatTok->Tok.is(tok::l_brace)) {
2702  if (Style.BraceWrapping.AfterFunction)
2703  addUnwrappedLine();
2704  parseBlock(/*MustBeDeclaration=*/false);
2705  addUnwrappedLine();
2706  return;
2707  } else {
2708  nextToken();
2709  }
2710  } while (!eof());
2711 }
2712 
2713 void UnwrappedLineParser::parseObjCProtocolList() {
2714  assert(FormatTok->Tok.is(tok::less) && "'<' expected.");
2715  do {
2716  nextToken();
2717  // Early exit in case someone forgot a close angle.
2718  if (FormatTok->isOneOf(tok::semi, tok::l_brace) ||
2719  FormatTok->Tok.isObjCAtKeyword(tok::objc_end))
2720  return;
2721  } while (!eof() && FormatTok->Tok.isNot(tok::greater));
2722  nextToken(); // Skip '>'.
2723 }
2724 
2725 void UnwrappedLineParser::parseObjCUntilAtEnd() {
2726  do {
2727  if (FormatTok->Tok.isObjCAtKeyword(tok::objc_end)) {
2728  nextToken();
2729  addUnwrappedLine();
2730  break;
2731  }
2732  if (FormatTok->is(tok::l_brace)) {
2733  parseBlock(/*MustBeDeclaration=*/false);
2734  // In ObjC interfaces, nothing should be following the "}".
2735  addUnwrappedLine();
2736  } else if (FormatTok->is(tok::r_brace)) {
2737  // Ignore stray "}". parseStructuralElement doesn't consume them.
2738  nextToken();
2739  addUnwrappedLine();
2740  } else if (FormatTok->isOneOf(tok::minus, tok::plus)) {
2741  nextToken();
2742  parseObjCMethod();
2743  } else {
2744  parseStructuralElement();
2745  }
2746  } while (!eof());
2747 }
2748 
2749 void UnwrappedLineParser::parseObjCInterfaceOrImplementation() {
2750  assert(FormatTok->Tok.getObjCKeywordID() == tok::objc_interface ||
2751  FormatTok->Tok.getObjCKeywordID() == tok::objc_implementation);
2752  nextToken();
2753  nextToken(); // interface name
2754 
2755  // @interface can be followed by a lightweight generic
2756  // specialization list, then either a base class or a category.
2757  if (FormatTok->Tok.is(tok::less)) {
2758  parseObjCLightweightGenerics();
2759  }
2760  if (FormatTok->Tok.is(tok::colon)) {
2761  nextToken();
2762  nextToken(); // base class name
2763  // The base class can also have lightweight generics applied to it.
2764  if (FormatTok->Tok.is(tok::less)) {
2765  parseObjCLightweightGenerics();
2766  }
2767  } else if (FormatTok->Tok.is(tok::l_paren))
2768  // Skip category, if present.
2769  parseParens();
2770 
2771  if (FormatTok->Tok.is(tok::less))
2772  parseObjCProtocolList();
2773 
2774  if (FormatTok->Tok.is(tok::l_brace)) {
2775  if (Style.BraceWrapping.AfterObjCDeclaration)
2776  addUnwrappedLine();
2777  parseBlock(/*MustBeDeclaration=*/true);
2778  }
2779 
2780  // With instance variables, this puts '}' on its own line. Without instance
2781  // variables, this ends the @interface line.
2782  addUnwrappedLine();
2783 
2784  parseObjCUntilAtEnd();
2785 }
2786 
2787 void UnwrappedLineParser::parseObjCLightweightGenerics() {
2788  assert(FormatTok->Tok.is(tok::less));
2789  // Unlike protocol lists, generic parameterizations support
2790  // nested angles:
2791  //
2792  // @interface Foo<ValueType : id <NSCopying, NSSecureCoding>> :
2793  // NSObject <NSCopying, NSSecureCoding>
2794  //
2795  // so we need to count how many open angles we have left.
2796  unsigned NumOpenAngles = 1;
2797  do {
2798  nextToken();
2799  // Early exit in case someone forgot a close angle.
2800  if (FormatTok->isOneOf(tok::semi, tok::l_brace) ||
2801  FormatTok->Tok.isObjCAtKeyword(tok::objc_end))
2802  break;
2803  if (FormatTok->Tok.is(tok::less))
2804  ++NumOpenAngles;
2805  else if (FormatTok->Tok.is(tok::greater)) {
2806  assert(NumOpenAngles > 0 && "'>' makes NumOpenAngles negative");
2807  --NumOpenAngles;
2808  }
2809  } while (!eof() && NumOpenAngles != 0);
2810  nextToken(); // Skip '>'.
2811 }
2812 
2813 // Returns true for the declaration/definition form of @protocol,
2814 // false for the expression form.
2815 bool UnwrappedLineParser::parseObjCProtocol() {
2816  assert(FormatTok->Tok.getObjCKeywordID() == tok::objc_protocol);
2817  nextToken();
2818 
2819  if (FormatTok->is(tok::l_paren))
2820  // The expression form of @protocol, e.g. "Protocol* p = @protocol(foo);".
2821  return false;
2822 
2823  // The definition/declaration form,
2824  // @protocol Foo
2825  // - (int)someMethod;
2826  // @end
2827 
2828  nextToken(); // protocol name
2829 
2830  if (FormatTok->Tok.is(tok::less))
2831  parseObjCProtocolList();
2832 
2833  // Check for protocol declaration.
2834  if (FormatTok->Tok.is(tok::semi)) {
2835  nextToken();
2836  addUnwrappedLine();
2837  return true;
2838  }
2839 
2840  addUnwrappedLine();
2841  parseObjCUntilAtEnd();
2842  return true;
2843 }
2844 
2845 void UnwrappedLineParser::parseJavaScriptEs6ImportExport() {
2846  bool IsImport = FormatTok->is(Keywords.kw_import);
2847  assert(IsImport || FormatTok->is(tok::kw_export));
2848  nextToken();
2849 
2850  // Consume the "default" in "export default class/function".
2851  if (FormatTok->is(tok::kw_default))
2852  nextToken();
2853 
2854  // Consume "async function", "function" and "default function", so that these
2855  // get parsed as free-standing JS functions, i.e. do not require a trailing
2856  // semicolon.
2857  if (FormatTok->is(Keywords.kw_async))
2858  nextToken();
2859  if (FormatTok->is(Keywords.kw_function)) {
2860  nextToken();
2861  return;
2862  }
2863 
2864  // For imports, `export *`, `export {...}`, consume the rest of the line up
2865  // to the terminating `;`. For everything else, just return and continue
2866  // parsing the structural element, i.e. the declaration or expression for
2867  // `export default`.
2868  if (!IsImport && !FormatTok->isOneOf(tok::l_brace, tok::star) &&
2869  !FormatTok->isStringLiteral())
2870  return;
2871 
2872  while (!eof()) {
2873  if (FormatTok->is(tok::semi))
2874  return;
2875  if (Line->Tokens.empty()) {
2876  // Common issue: Automatic Semicolon Insertion wrapped the line, so the
2877  // import statement should terminate.
2878  return;
2879  }
2880  if (FormatTok->is(tok::l_brace)) {
2881  FormatTok->setBlockKind(BK_Block);
2882  nextToken();
2883  parseBracedList();
2884  } else {
2885  nextToken();
2886  }
2887  }
2888 }
2889 
2890 void UnwrappedLineParser::parseStatementMacro() {
2891  nextToken();
2892  if (FormatTok->is(tok::l_paren))
2893  parseParens();
2894  if (FormatTok->is(tok::semi))
2895  nextToken();
2896  addUnwrappedLine();
2897 }
2898 
2899 LLVM_ATTRIBUTE_UNUSED static void printDebugInfo(const UnwrappedLine &Line,
2900  StringRef Prefix = "") {
2901  llvm::dbgs() << Prefix << "Line(" << Line.Level
2902  << ", FSC=" << Line.FirstStartColumn << ")"
2903  << (Line.InPPDirective ? " MACRO" : "") << ": ";
2904  for (std::list<UnwrappedLineNode>::const_iterator I = Line.Tokens.begin(),
2905  E = Line.Tokens.end();
2906  I != E; ++I) {
2907  llvm::dbgs() << I->Tok->Tok.getName() << "["
2908  << "T=" << (unsigned)I->Tok->getType()
2909  << ", OC=" << I->Tok->OriginalColumn << "] ";
2910  }
2911  for (std::list<UnwrappedLineNode>::const_iterator I = Line.Tokens.begin(),
2912  E = Line.Tokens.end();
2913  I != E; ++I) {
2914  const UnwrappedLineNode &Node = *I;
2916  I = Node.Children.begin(),
2917  E = Node.Children.end();
2918  I != E; ++I) {
2919  printDebugInfo(*I, "\nChild: ");
2920  }
2921  }
2922  llvm::dbgs() << "\n";
2923 }
2924 
2925 void UnwrappedLineParser::addUnwrappedLine() {
2926  if (Line->Tokens.empty())
2927  return;
2928  LLVM_DEBUG({
2929  if (CurrentLines == &Lines)
2930  printDebugInfo(*Line);
2931  });
2932  CurrentLines->push_back(std::move(*Line));
2933  Line->Tokens.clear();
2934  Line->MatchingOpeningBlockLineIndex = UnwrappedLine::kInvalidIndex;
2935  Line->FirstStartColumn = 0;
2936  if (CurrentLines == &Lines && !PreprocessorDirectives.empty()) {
2937  CurrentLines->append(
2938  std::make_move_iterator(PreprocessorDirectives.begin()),
2939  std::make_move_iterator(PreprocessorDirectives.end()));
2940  PreprocessorDirectives.clear();
2941  }
2942  // Disconnect the current token from the last token on the previous line.
2943  FormatTok->Previous = nullptr;
2944 }
2945 
2946 bool UnwrappedLineParser::eof() const { return FormatTok->Tok.is(tok::eof); }
2947 
2948 bool UnwrappedLineParser::isOnNewLine(const FormatToken &FormatTok) {
2949  return (Line->InPPDirective || FormatTok.HasUnescapedNewline) &&
2950  FormatTok.NewlinesBefore > 0;
2951 }
2952 
2953 // Checks if \p FormatTok is a line comment that continues the line comment
2954 // section on \p Line.
2955 static bool
2957  const UnwrappedLine &Line,
2958  const llvm::Regex &CommentPragmasRegex) {
2959  if (Line.Tokens.empty())
2960  return false;
2961 
2962  StringRef IndentContent = FormatTok.TokenText;
2963  if (FormatTok.TokenText.startswith("//") ||
2964  FormatTok.TokenText.startswith("/*"))
2965  IndentContent = FormatTok.TokenText.substr(2);
2966  if (CommentPragmasRegex.match(IndentContent))
2967  return false;
2968 
2969  // If Line starts with a line comment, then FormatTok continues the comment
2970  // section if its original column is greater or equal to the original start
2971  // column of the line.
2972  //
2973  // Define the min column token of a line as follows: if a line ends in '{' or
2974  // contains a '{' followed by a line comment, then the min column token is
2975  // that '{'. Otherwise, the min column token of the line is the first token of
2976  // the line.
2977  //
2978  // If Line starts with a token other than a line comment, then FormatTok
2979  // continues the comment section if its original column is greater than the
2980  // original start column of the min column token of the line.
2981  //
2982  // For example, the second line comment continues the first in these cases:
2983  //
2984  // // first line
2985  // // second line
2986  //
2987  // and:
2988  //
2989  // // first line
2990  // // second line
2991  //
2992  // and:
2993  //
2994  // int i; // first line
2995  // // second line
2996  //
2997  // and:
2998  //
2999  // do { // first line
3000  // // second line
3001  // int i;
3002  // } while (true);
3003  //
3004  // and:
3005  //
3006  // enum {
3007  // a, // first line
3008  // // second line
3009  // b
3010  // };
3011  //
3012  // The second line comment doesn't continue the first in these cases:
3013  //
3014  // // first line
3015  // // second line
3016  //
3017  // and:
3018  //
3019  // int i; // first line
3020  // // second line
3021  //
3022  // and:
3023  //
3024  // do { // first line
3025  // // second line
3026  // int i;
3027  // } while (true);
3028  //
3029  // and:
3030  //
3031  // enum {
3032  // a, // first line
3033  // // second line
3034  // };
3035  const FormatToken *MinColumnToken = Line.Tokens.front().Tok;
3036 
3037  // Scan for '{//'. If found, use the column of '{' as a min column for line
3038  // comment section continuation.
3039  const FormatToken *PreviousToken = nullptr;
3040  for (const UnwrappedLineNode &Node : Line.Tokens) {
3041  if (PreviousToken && PreviousToken->is(tok::l_brace) &&
3042  isLineComment(*Node.Tok)) {
3043  MinColumnToken = PreviousToken;
3044  break;
3045  }
3046  PreviousToken = Node.Tok;
3047 
3048  // Grab the last newline preceding a token in this unwrapped line.
3049  if (Node.Tok->NewlinesBefore > 0) {
3050  MinColumnToken = Node.Tok;
3051  }
3052  }
3053  if (PreviousToken && PreviousToken->is(tok::l_brace)) {
3054  MinColumnToken = PreviousToken;
3055  }
3056 
3057  return continuesLineComment(FormatTok, /*Previous=*/Line.Tokens.back().Tok,
3058  MinColumnToken);
3059 }
3060 
3061 void UnwrappedLineParser::flushComments(bool NewlineBeforeNext) {
3062  bool JustComments = Line->Tokens.empty();
3064  I = CommentsBeforeNextToken.begin(),
3065  E = CommentsBeforeNextToken.end();
3066  I != E; ++I) {
3067  // Line comments that belong to the same line comment section are put on the
3068  // same line since later we might want to reflow content between them.
3069  // Additional fine-grained breaking of line comment sections is controlled
3070  // by the class BreakableLineCommentSection in case it is desirable to keep
3071  // several line comment sections in the same unwrapped line.
3072  //
3073  // FIXME: Consider putting separate line comment sections as children to the
3074  // unwrapped line instead.
3075  (*I)->ContinuesLineCommentSection =
3076  continuesLineCommentSection(**I, *Line, CommentPragmasRegex);
3077  if (isOnNewLine(**I) && JustComments && !(*I)->ContinuesLineCommentSection)
3078  addUnwrappedLine();
3079  pushToken(*I);
3080  }
3081  if (NewlineBeforeNext && JustComments)
3082  addUnwrappedLine();
3083  CommentsBeforeNextToken.clear();
3084 }
3085 
3086 void UnwrappedLineParser::nextToken(int LevelDifference) {
3087  if (eof())
3088  return;
3089  flushComments(isOnNewLine(*FormatTok));
3090  pushToken(FormatTok);
3091  FormatToken *Previous = FormatTok;
3092  if (Style.Language != FormatStyle::LK_JavaScript)
3093  readToken(LevelDifference);
3094  else
3095  readTokenWithJavaScriptASI();
3096  FormatTok->Previous = Previous;
3097 }
3098 
3099 void UnwrappedLineParser::distributeComments(
3100  const SmallVectorImpl<FormatToken *> &Comments,
3101  const FormatToken *NextTok) {
3102  // Whether or not a line comment token continues a line is controlled by
3103  // the method continuesLineCommentSection, with the following caveat:
3104  //
3105  // Define a trail of Comments to be a nonempty proper postfix of Comments such
3106  // that each comment line from the trail is aligned with the next token, if
3107  // the next token exists. If a trail exists, the beginning of the maximal
3108  // trail is marked as a start of a new comment section.
3109  //
3110  // For example in this code:
3111  //
3112  // int a; // line about a
3113  // // line 1 about b
3114  // // line 2 about b
3115  // int b;
3116  //
3117  // the two lines about b form a maximal trail, so there are two sections, the
3118  // first one consisting of the single comment "// line about a" and the
3119  // second one consisting of the next two comments.
3120  if (Comments.empty())
3121  return;
3122  bool ShouldPushCommentsInCurrentLine = true;
3123  bool HasTrailAlignedWithNextToken = false;
3124  unsigned StartOfTrailAlignedWithNextToken = 0;
3125  if (NextTok) {
3126  // We are skipping the first element intentionally.
3127  for (unsigned i = Comments.size() - 1; i > 0; --i) {
3128  if (Comments[i]->OriginalColumn == NextTok->OriginalColumn) {
3129  HasTrailAlignedWithNextToken = true;
3130  StartOfTrailAlignedWithNextToken = i;
3131  }
3132  }
3133  }
3134  for (unsigned i = 0, e = Comments.size(); i < e; ++i) {
3135  FormatToken *FormatTok = Comments[i];
3136  if (HasTrailAlignedWithNextToken && i == StartOfTrailAlignedWithNextToken) {
3137  FormatTok->ContinuesLineCommentSection = false;
3138  } else {
3139  FormatTok->ContinuesLineCommentSection =
3140  continuesLineCommentSection(*FormatTok, *Line, CommentPragmasRegex);
3141  }
3142  if (!FormatTok->ContinuesLineCommentSection &&
3143  (isOnNewLine(*FormatTok) || FormatTok->IsFirst)) {
3144  ShouldPushCommentsInCurrentLine = false;
3145  }
3146  if (ShouldPushCommentsInCurrentLine) {
3147  pushToken(FormatTok);
3148  } else {
3149  CommentsBeforeNextToken.push_back(FormatTok);
3150  }
3151  }
3152 }
3153 
3154 void UnwrappedLineParser::readToken(int LevelDifference) {
3155  SmallVector<FormatToken *, 1> Comments;
3156  do {
3157  FormatTok = Tokens->getNextToken();
3158  assert(FormatTok);
3159  while (!Line->InPPDirective && FormatTok->Tok.is(tok::hash) &&
3160  (FormatTok->HasUnescapedNewline || FormatTok->IsFirst)) {
3161  distributeComments(Comments, FormatTok);
3162  Comments.clear();
3163  // If there is an unfinished unwrapped line, we flush the preprocessor
3164  // directives only after that unwrapped line was finished later.
3165  bool SwitchToPreprocessorLines = !Line->Tokens.empty();
3166  ScopedLineState BlockState(*this, SwitchToPreprocessorLines);
3167  assert((LevelDifference >= 0 ||
3168  static_cast<unsigned>(-LevelDifference) <= Line->Level) &&
3169  "LevelDifference makes Line->Level negative");
3170  Line->Level += LevelDifference;
3171  // Comments stored before the preprocessor directive need to be output
3172  // before the preprocessor directive, at the same level as the
3173  // preprocessor directive, as we consider them to apply to the directive.
3174  if (Style.IndentPPDirectives == FormatStyle::PPDIS_BeforeHash &&
3175  PPBranchLevel > 0)
3176  Line->Level += PPBranchLevel;
3177  flushComments(isOnNewLine(*FormatTok));
3178  parsePPDirective();
3179  }
3180  while (FormatTok->getType() == TT_ConflictStart ||
3181  FormatTok->getType() == TT_ConflictEnd ||
3182  FormatTok->getType() == TT_ConflictAlternative) {
3183  if (FormatTok->getType() == TT_ConflictStart) {
3184  conditionalCompilationStart(/*Unreachable=*/false);
3185  } else if (FormatTok->getType() == TT_ConflictAlternative) {
3186  conditionalCompilationAlternative();
3187  } else if (FormatTok->getType() == TT_ConflictEnd) {
3188  conditionalCompilationEnd();
3189  }
3190  FormatTok = Tokens->getNextToken();
3191  FormatTok->MustBreakBefore = true;
3192  }
3193 
3194  if (!PPStack.empty() && (PPStack.back().Kind == PP_Unreachable) &&
3195  !Line->InPPDirective) {
3196  continue;
3197  }
3198 
3199  if (!FormatTok->Tok.is(tok::comment)) {
3200  distributeComments(Comments, FormatTok);
3201  Comments.clear();
3202  return;
3203  }
3204 
3205  Comments.push_back(FormatTok);
3206  } while (!eof());
3207 
3208  distributeComments(Comments, nullptr);
3209  Comments.clear();
3210 }
3211 
3212 void UnwrappedLineParser::pushToken(FormatToken *Tok) {
3213  Line->Tokens.push_back(UnwrappedLineNode(Tok));
3214  if (MustBreakBeforeNextToken) {
3215  Line->Tokens.back().Tok->MustBreakBefore = true;
3216  MustBreakBeforeNextToken = false;
3217  }
3218 }
3219 
3220 } // end namespace format
3221 } // end namespace clang
clang::format::CompoundStatementIndenter
Definition: UnwrappedLineParser.cpp:172
clang::minimize_source_to_dependency_directives::pp_ifdef
@ pp_ifdef
Definition: DependencyDirectivesSourceMinimizer.h:44
clang::format::AdditionalKeywords::kw_implements
IdentifierInfo * kw_implements
Definition: FormatToken.h:989
clang::format::ScopedLineState
Definition: UnwrappedLineParser.cpp:139
clang::format::FormatTokenSource::getNextToken
virtual FormatToken * getNextToken()=0
clang::format::AdditionalKeywords::kw_NS_OPTIONS
IdentifierInfo * kw_NS_OPTIONS
Definition: FormatToken.h:959
clang::format::AdditionalKeywords::kw_abstract
IdentifierInfo * kw_abstract
Definition: FormatToken.h:986
clang::format::AdditionalKeywords::kw_CF_ENUM
IdentifierInfo * kw_CF_ENUM
Definition: FormatToken.h:955
clang::format::isJSDeclOrStmt
static bool isJSDeclOrStmt(const AdditionalKeywords &Keywords, const FormatToken *FormatTok)
Definition: UnwrappedLineParser.cpp:963
clang::format::ScopedLineState::~ScopedLineState
~ScopedLineState()
Definition: UnwrappedLineParser.cpp:154
clang::format::mustBeJSIdent
static bool mustBeJSIdent(const AdditionalKeywords &Keywords, const FormatToken *FormatTok)
Definition: UnwrappedLineParser.cpp:939
clang::format::AdditionalKeywords::kw_internal
IdentifierInfo * kw_internal
Definition: FormatToken.h:1025
clang::format::AdditionalKeywords::kw_instanceof
IdentifierInfo * kw_instanceof
Definition: FormatToken.h:990
clang::format::BK_Unknown
@ BK_Unknown
Definition: FormatToken.h:137
clang::format::AdditionalKeywords::kw_from
IdentifierInfo * kw_from
Definition: FormatToken.h:970
clang::format::UnwrappedLineParser::parse
void parse()
Definition: UnwrappedLineParser.cpp:256
clang::Token::getIdentifierInfo
IdentifierInfo * getIdentifierInfo() const
Definition: Token.h:179
clang::format::UnwrappedLine::kInvalidIndex
static const size_t kInvalidIndex
Definition: UnwrappedLineParser.h:61
clang::format::AnnotatedLine::Level
unsigned Level
Definition: TokenAnnotator.h:128
clang::format::AdditionalKeywords::kw_is
IdentifierInfo * kw_is
Definition: FormatToken.h:975
clang::format::FormatStyle
The FormatStyle is used to configure the formatting to follow specific guidelines.
Definition: Format.h:54
clang::minimize_source_to_dependency_directives::pp_ifndef
@ pp_ifndef
Definition: DependencyDirectivesSourceMinimizer.h:45
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:228
clang::format::AdditionalKeywords::kw_CF_CLOSED_ENUM
IdentifierInfo * kw_CF_CLOSED_ENUM
Definition: FormatToken.h:954
clang::format::FormatToken
A wrapper around a Token storing information about the whitespace characters preceding it.
Definition: FormatToken.h:211
clang::format::AdditionalKeywords
Encapsulates keywords that are context sensitive or for languages not properly supported by Clang's l...
Definition: FormatToken.h:833
clang::format::FormatToken::isNot
bool isNot(T Kind) const
Definition: FormatToken.h:463
clang::minimize_source_to_dependency_directives::pp_define
@ pp_define
Definition: DependencyDirectivesSourceMinimizer.h:37
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:36
clang::format::AdditionalKeywords::kw_as
IdentifierInfo * kw_as
Definition: FormatToken.h:965
clang::format::AdditionalKeywords::kw_signals
IdentifierInfo * kw_signals
Definition: FormatToken.h:1009
clang::format::BK_BracedInit
@ BK_BracedInit
Definition: FormatToken.h:137
clang::format::UnwrappedLineParser
Definition: UnwrappedLineParser.h:75
clang::format::AnnotatedLine::FirstStartColumn
unsigned FirstStartColumn
Definition: TokenAnnotator.h:147
clang::format::FormatTokenSource::~FormatTokenSource
virtual ~FormatTokenSource()
Definition: UnwrappedLineParser.cpp:30
size_t
__SIZE_TYPE__ size_t
The unsigned integer type of the result of the sizeof operator.
Definition: opencl-c-base.h:70
clang::minimize_source_to_dependency_directives::pp_if
@ pp_if
Definition: DependencyDirectivesSourceMinimizer.h:43
clang::format::AdditionalKeywords::kw_import
IdentifierInfo * kw_import
Definition: FormatToken.h:973
clang::format::UnwrappedLineParser::CompoundStatementIndenter
friend class CompoundStatementIndenter
Definition: UnwrappedLineParser.h:290
clang::format::AdditionalKeywords::kw_await
IdentifierInfo * kw_await
Definition: FormatToken.h:967
clang::format::AdditionalKeywords::kw_extends
IdentifierInfo * kw_extends
Definition: FormatToken.h:988
clang::format::AdditionalKeywords::kw_async
IdentifierInfo * kw_async
Definition: FormatToken.h:966
clang::format::CompoundStatementIndenter::~CompoundStatementIndenter
~CompoundStatementIndenter()
Definition: UnwrappedLineParser.cpp:187
clang::format::AdditionalKeywords::kw_qslots
IdentifierInfo * kw_qslots
Definition: FormatToken.h:1012
clang::format::AdditionalKeywords::kw_NS_ENUM
IdentifierInfo * kw_NS_ENUM
Definition: FormatToken.h:958
Node
DynTypedNode Node
Definition: ASTMatchFinder.cpp:67
clang::format::FormatToken::Tok
Token Tok
The Token.
Definition: FormatToken.h:222
clang::format::UnwrappedLineConsumer
Definition: UnwrappedLineParser.h:66
clang::format::AdditionalKeywords::kw_throws
IdentifierInfo * kw_throws
Definition: FormatToken.h:995
clang::format::ShouldBreakBeforeBrace
static bool ShouldBreakBeforeBrace(const FormatStyle &Style, const FormatToken &InitialToken)
Definition: UnwrappedLineParser.cpp:686
clang::format::continuesLineCommentSection
static bool continuesLineCommentSection(const FormatToken &FormatTok, const UnwrappedLine &Line, const llvm::Regex &CommentPragmasRegex)
Definition: UnwrappedLineParser.cpp:2956
clang::format::AdditionalKeywords::kw_in
IdentifierInfo * kw_in
Definition: FormatToken.h:952
clang::format::CompoundStatementIndenter::CompoundStatementIndenter
CompoundStatementIndenter(UnwrappedLineParser *Parser, const FormatStyle &Style, unsigned &LineLevel)
Definition: UnwrappedLineParser.cpp:174
clang::format::AdditionalKeywords::kw_var
IdentifierInfo * kw_var
Definition: FormatToken.h:982
clang::format::AdditionalKeywords::kw_of
IdentifierInfo * kw_of
Definition: FormatToken.h:953
clang::format::isGoogScope
static bool isGoogScope(const UnwrappedLine &Line)
Definition: UnwrappedLineParser.cpp:650
clang::format::AdditionalKeywords::kw_finally
IdentifierInfo * kw_finally
Definition: FormatToken.h:969
clang::format::BK_Block
@ BK_Block
Definition: FormatToken.h:137
clang::minimize_source_to_dependency_directives::pp_include
@ pp_include
Definition: DependencyDirectivesSourceMinimizer.h:35
clang::format::FormatTokenSource::setPosition
virtual FormatToken * setPosition(unsigned Position)=0
clang::format::AnnotatedLine::InPPDirective
bool InPPDirective
Definition: TokenAnnotator.h:131
Line
const AnnotatedLine * Line
Definition: UsingDeclarationsSorter.cpp:68
clang::format::CompoundStatementIndenter::CompoundStatementIndenter
CompoundStatementIndenter(UnwrappedLineParser *Parser, unsigned &LineLevel, bool WrapBrace, bool IndentBrace)
Definition: UnwrappedLineParser.cpp:179
clang::minimize_source_to_dependency_directives::pp_else
@ pp_else
Definition: DependencyDirectivesSourceMinimizer.h:47
clang::format::hash_combine
static void hash_combine(std::size_t &seed, const T &v)
Definition: UnwrappedLineParser.cpp:568
clang::Token::isLiteral
bool isLiteral() const
Return true if this is a "literal", like a numeric constant, string, etc.
Definition: Token.h:115
UnwrappedLineParser.h
clang::format::UnwrappedLineParser::ScopedLineState
friend class ScopedLineState
Definition: UnwrappedLineParser.h:289
clang::tok::TokenKind
TokenKind
Provides a simple uniform namespace for tokens from all C languages.
Definition: TokenKinds.h:25
P
StringRef P
Definition: ASTMatchersInternal.cpp:563
false
#define false
Definition: stdbool.h:17
clang::format::AdditionalKeywords::kw_let
IdentifierInfo * kw_let
Definition: FormatToken.h:976
llvm::ArrayRef
Definition: LLVM.h:31
clang::format::AdditionalKeywords::kw_get
IdentifierInfo * kw_get
Definition: FormatToken.h:972
clang::format::FormatToken::is
bool is(tok::TokenKind Kind) const
Definition: FormatToken.h:444
clang::format::AdditionalKeywords::kw_set
IdentifierInfo * kw_set
Definition: FormatToken.h:979
clang::format::FormatToken::isOneOf
bool isOneOf(A K1, B K2) const
Definition: FormatToken.h:456
clang::format::ScopedLineState::ScopedLineState
ScopedLineState(UnwrappedLineParser &Parser, bool SwitchToPreprocessorLines=false)
Definition: UnwrappedLineParser.cpp:141
clang::format::AdditionalKeywords::kw_function
IdentifierInfo * kw_function
Definition: FormatToken.h:971
clang::format::UnwrappedLineConsumer::consumeUnwrappedLine
virtual void consumeUnwrappedLine(const UnwrappedLine &Line)=0
clang::format::FormatTokenSource::getPosition
virtual unsigned getPosition()=0
clang::format::UnwrappedLineNode
Definition: UnwrappedLineParser.h:293
clang::format::AdditionalKeywords::kw_interface
IdentifierInfo * kw_interface
Definition: FormatToken.h:991
clang
Dataflow Directional Tag Classes.
Definition: CalledOnceCheck.h:17
Text
StringRef Text
Definition: Format.cpp:2146
clang::format::UnwrappedLineConsumer::finishRun
virtual void finishRun()=0
clang::format::AdditionalKeywords::kw_yield
IdentifierInfo * kw_yield
Definition: FormatToken.h:983
clang::format::AdditionalKeywords::kw___except
IdentifierInfo * kw___except
Definition: FormatToken.h:960
clang::prec::Level
Level
Definition: OperatorPrecedence.h:26
clang::format::AdditionalKeywords::kw_NS_CLOSED_ENUM
IdentifierInfo * kw_NS_CLOSED_ENUM
Definition: FormatToken.h:957
unsigned
clang::minimize_source_to_dependency_directives::pp_endif
@ pp_endif
Definition: DependencyDirectivesSourceMinimizer.h:48
clang::format::AdditionalKeywords::kw_CF_OPTIONS
IdentifierInfo * kw_CF_OPTIONS
Definition: FormatToken.h:956
clang::format::printDebugInfo
static LLVM_ATTRIBUTE_UNUSED void printDebugInfo(const UnwrappedLine &Line, StringRef Prefix="")
Definition: UnwrappedLineParser.cpp:2899
FormatToken.h
v
do v
Definition: arm_acle.h:76
clang::format::AdditionalKeywords::kw_qsignals
IdentifierInfo * kw_qsignals
Definition: FormatToken.h:1010
clang::format::AdditionalKeywords::kw_where
IdentifierInfo * kw_where
Definition: FormatToken.h:1045
clang::comments::tok::eof
@ eof
Definition: CommentLexer.h:33
llvm::SmallVectorImpl
Definition: LLVM.h:36
clang::format::mustBeJSIdentOrValue
static bool mustBeJSIdentOrValue(const AdditionalKeywords &Keywords, const FormatToken *FormatTok)
Definition: UnwrappedLineParser.cpp:954
Indenter
ContinuationIndenter * Indenter
Definition: UnwrappedLineFormatter.cpp:856
Previous
StateNode * Previous
Definition: UnwrappedLineFormatter.cpp:961
clang::format::tokenCanStartNewLine
static bool tokenCanStartNewLine(const FormatToken &Tok)
Definition: UnwrappedLineParser.cpp:913
clang::format::isIIFE
static bool isIIFE(const UnwrappedLine &Line, const AdditionalKeywords &Keywords)
Definition: UnwrappedLineParser.cpp:668
clang::format::AdditionalKeywords::kw_slots
IdentifierInfo * kw_slots
Definition: FormatToken.h:1011
clang::diag::kind
unsigned kind
All of the diagnostics that can be emitted by the frontend.
Definition: DiagnosticIDs.h:60
clang::minimize_source_to_dependency_directives::pp_elif
@ pp_elif
Definition: DependencyDirectivesSourceMinimizer.h:46
clang::format::UnwrappedLineParser::UnwrappedLineParser
UnwrappedLineParser(const FormatStyle &Style, const AdditionalKeywords &Keywords, unsigned FirstStartColumn, ArrayRef< FormatToken * > Tokens, UnwrappedLineConsumer &Callback)
Definition: UnwrappedLineParser.cpp:225
clang::format::FormatTokenSource
Definition: UnwrappedLineParser.cpp:28