clang  14.0.0git
TokenAnnotator.cpp
Go to the documentation of this file.
1 //===--- TokenAnnotator.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 implements a token annotator, i.e. creates
11 /// \c AnnotatedTokens out of \c FormatTokens with required extra information.
12 ///
13 //===----------------------------------------------------------------------===//
14 
15 #include "TokenAnnotator.h"
16 #include "FormatToken.h"
18 #include "clang/Basic/TokenKinds.h"
19 #include "llvm/ADT/SmallPtrSet.h"
20 #include "llvm/Support/Debug.h"
21 
22 #define DEBUG_TYPE "format-token-annotator"
23 
24 namespace clang {
25 namespace format {
26 
27 namespace {
28 
29 /// Returns \c true if the token can be used as an identifier in
30 /// an Objective-C \c \@selector, \c false otherwise.
31 ///
32 /// Because getFormattingLangOpts() always lexes source code as
33 /// Objective-C++, C++ keywords like \c new and \c delete are
34 /// lexed as tok::kw_*, not tok::identifier, even for Objective-C.
35 ///
36 /// For Objective-C and Objective-C++, both identifiers and keywords
37 /// are valid inside @selector(...) (or a macro which
38 /// invokes @selector(...)). So, we allow treat any identifier or
39 /// keyword as a potential Objective-C selector component.
40 static bool canBeObjCSelectorComponent(const FormatToken &Tok) {
41  return Tok.Tok.getIdentifierInfo() != nullptr;
42 }
43 
44 /// With `Left` being '(', check if we're at either `[...](` or
45 /// `[...]<...>(`, where the [ opens a lambda capture list.
46 static bool isLambdaParameterList(const FormatToken *Left) {
47  // Skip <...> if present.
48  if (Left->Previous && Left->Previous->is(tok::greater) &&
49  Left->Previous->MatchingParen &&
50  Left->Previous->MatchingParen->is(TT_TemplateOpener))
51  Left = Left->Previous->MatchingParen;
52 
53  // Check for `[...]`.
54  return Left->Previous && Left->Previous->is(tok::r_square) &&
55  Left->Previous->MatchingParen &&
56  Left->Previous->MatchingParen->is(TT_LambdaLSquare);
57 }
58 
59 /// Returns \c true if the token is followed by a boolean condition, \c false
60 /// otherwise.
61 static bool isKeywordWithCondition(const FormatToken &Tok) {
62  return Tok.isOneOf(tok::kw_if, tok::kw_for, tok::kw_while, tok::kw_switch,
63  tok::kw_constexpr, tok::kw_catch);
64 }
65 
66 /// A parser that gathers additional information about tokens.
67 ///
68 /// The \c TokenAnnotator tries to match parenthesis and square brakets and
69 /// store a parenthesis levels. It also tries to resolve matching "<" and ">"
70 /// into template parameter lists.
71 class AnnotatingParser {
72 public:
73  AnnotatingParser(const FormatStyle &Style, AnnotatedLine &Line,
74  const AdditionalKeywords &Keywords)
75  : Style(Style), Line(Line), CurrentToken(Line.First), AutoFound(false),
76  Keywords(Keywords) {
77  Contexts.push_back(Context(tok::unknown, 1, /*IsExpression=*/false));
78  resetTokenMetadata();
79  }
80 
81 private:
82  bool parseAngle() {
83  if (!CurrentToken || !CurrentToken->Previous)
84  return false;
85  if (NonTemplateLess.count(CurrentToken->Previous))
86  return false;
87 
88  const FormatToken &Previous = *CurrentToken->Previous; // The '<'.
89  if (Previous.Previous) {
90  if (Previous.Previous->Tok.isLiteral())
91  return false;
92  if (Previous.Previous->is(tok::r_paren) && Contexts.size() > 1 &&
93  (!Previous.Previous->MatchingParen ||
94  !Previous.Previous->MatchingParen->is(TT_OverloadedOperatorLParen)))
95  return false;
96  }
97 
98  FormatToken *Left = CurrentToken->Previous;
99  Left->ParentBracket = Contexts.back().ContextKind;
100  ScopedContextCreator ContextCreator(*this, tok::less, 12);
101 
102  // If this angle is in the context of an expression, we need to be more
103  // hesitant to detect it as opening template parameters.
104  bool InExprContext = Contexts.back().IsExpression;
105 
106  Contexts.back().IsExpression = false;
107  // If there's a template keyword before the opening angle bracket, this is a
108  // template parameter, not an argument.
109  Contexts.back().InTemplateArgument =
110  Left->Previous && Left->Previous->Tok.isNot(tok::kw_template);
111 
112  if (Style.Language == FormatStyle::LK_Java &&
113  CurrentToken->is(tok::question))
114  next();
115 
116  while (CurrentToken) {
117  if (CurrentToken->is(tok::greater)) {
118  // Try to do a better job at looking for ">>" within the condition of
119  // a statement. Conservatively insert spaces between consecutive ">"
120  // tokens to prevent splitting right bitshift operators and potentially
121  // altering program semantics. This check is overly conservative and
122  // will prevent spaces from being inserted in select nested template
123  // parameter cases, but should not alter program semantics.
124  if (CurrentToken->Next && CurrentToken->Next->is(tok::greater) &&
125  Left->ParentBracket != tok::less &&
126  (isKeywordWithCondition(*Line.First) ||
127  CurrentToken->getStartOfNonWhitespace() ==
128  CurrentToken->Next->getStartOfNonWhitespace().getLocWithOffset(
129  -1)))
130  return false;
131  Left->MatchingParen = CurrentToken;
132  CurrentToken->MatchingParen = Left;
133  // In TT_Proto, we must distignuish between:
134  // map<key, value>
135  // msg < item: data >
136  // msg: < item: data >
137  // In TT_TextProto, map<key, value> does not occur.
138  if (Style.Language == FormatStyle::LK_TextProto ||
139  (Style.Language == FormatStyle::LK_Proto && Left->Previous &&
140  Left->Previous->isOneOf(TT_SelectorName, TT_DictLiteral)))
141  CurrentToken->setType(TT_DictLiteral);
142  else
143  CurrentToken->setType(TT_TemplateCloser);
144  next();
145  return true;
146  }
147  if (CurrentToken->is(tok::question) &&
148  Style.Language == FormatStyle::LK_Java) {
149  next();
150  continue;
151  }
152  if (CurrentToken->isOneOf(tok::r_paren, tok::r_square, tok::r_brace) ||
153  (CurrentToken->isOneOf(tok::colon, tok::question) && InExprContext &&
154  !Style.isCSharp() && Style.Language != FormatStyle::LK_Proto &&
155  Style.Language != FormatStyle::LK_TextProto))
156  return false;
157  // If a && or || is found and interpreted as a binary operator, this set
158  // of angles is likely part of something like "a < b && c > d". If the
159  // angles are inside an expression, the ||/&& might also be a binary
160  // operator that was misinterpreted because we are parsing template
161  // parameters.
162  // FIXME: This is getting out of hand, write a decent parser.
163  if (CurrentToken->Previous->isOneOf(tok::pipepipe, tok::ampamp) &&
164  CurrentToken->Previous->is(TT_BinaryOperator) &&
165  Contexts[Contexts.size() - 2].IsExpression &&
166  !Line.startsWith(tok::kw_template))
167  return false;
168  updateParameterCount(Left, CurrentToken);
169  if (Style.Language == FormatStyle::LK_Proto) {
170  if (FormatToken *Previous = CurrentToken->getPreviousNonComment()) {
171  if (CurrentToken->is(tok::colon) ||
172  (CurrentToken->isOneOf(tok::l_brace, tok::less) &&
173  Previous->isNot(tok::colon)))
174  Previous->setType(TT_SelectorName);
175  }
176  }
177  if (!consumeToken())
178  return false;
179  }
180  return false;
181  }
182 
183  bool parseUntouchableParens() {
184  while (CurrentToken) {
185  CurrentToken->Finalized = true;
186  switch (CurrentToken->Tok.getKind()) {
187  case tok::l_paren:
188  next();
189  if (!parseUntouchableParens())
190  return false;
191  continue;
192  case tok::r_paren:
193  next();
194  return true;
195  default:
196  // no-op
197  break;
198  }
199  next();
200  }
201  return false;
202  }
203 
204  bool parseParens(bool LookForDecls = false) {
205  if (!CurrentToken)
206  return false;
207  FormatToken *Left = CurrentToken->Previous;
208  assert(Left && "Unknown previous token");
209  FormatToken *PrevNonComment = Left->getPreviousNonComment();
210  Left->ParentBracket = Contexts.back().ContextKind;
211  ScopedContextCreator ContextCreator(*this, tok::l_paren, 1);
212 
213  // FIXME: This is a bit of a hack. Do better.
214  Contexts.back().ColonIsForRangeExpr =
215  Contexts.size() == 2 && Contexts[0].ColonIsForRangeExpr;
216 
217  if (Left->Previous && Left->Previous->is(TT_UntouchableMacroFunc)) {
218  Left->Finalized = true;
219  return parseUntouchableParens();
220  }
221 
222  bool StartsObjCMethodExpr = false;
223  if (FormatToken *MaybeSel = Left->Previous) {
224  // @selector( starts a selector.
225  if (MaybeSel->isObjCAtKeyword(tok::objc_selector) && MaybeSel->Previous &&
226  MaybeSel->Previous->is(tok::at)) {
227  StartsObjCMethodExpr = true;
228  }
229  }
230 
231  if (Left->is(TT_OverloadedOperatorLParen)) {
232  // Find the previous kw_operator token.
233  FormatToken *Prev = Left;
234  while (!Prev->is(tok::kw_operator)) {
235  Prev = Prev->Previous;
236  assert(Prev && "Expect a kw_operator prior to the OperatorLParen!");
237  }
238 
239  // If faced with "a.operator*(argument)" or "a->operator*(argument)",
240  // i.e. the operator is called as a member function,
241  // then the argument must be an expression.
242  bool OperatorCalledAsMemberFunction =
243  Prev->Previous && Prev->Previous->isOneOf(tok::period, tok::arrow);
244  Contexts.back().IsExpression = OperatorCalledAsMemberFunction;
245  } else if (Style.isJavaScript() &&
246  (Line.startsWith(Keywords.kw_type, tok::identifier) ||
247  Line.startsWith(tok::kw_export, Keywords.kw_type,
248  tok::identifier))) {
249  // type X = (...);
250  // export type X = (...);
251  Contexts.back().IsExpression = false;
252  } else if (Left->Previous &&
253  (Left->Previous->isOneOf(tok::kw_static_assert, tok::kw_while,
254  tok::l_paren, tok::comma) ||
255  Left->Previous->isIf() ||
256  Left->Previous->is(TT_BinaryOperator))) {
257  // static_assert, if and while usually contain expressions.
258  Contexts.back().IsExpression = true;
259  } else if (Style.isJavaScript() && Left->Previous &&
260  (Left->Previous->is(Keywords.kw_function) ||
261  (Left->Previous->endsSequence(tok::identifier,
262  Keywords.kw_function)))) {
263  // function(...) or function f(...)
264  Contexts.back().IsExpression = false;
265  } else if (Style.isJavaScript() && Left->Previous &&
266  Left->Previous->is(TT_JsTypeColon)) {
267  // let x: (SomeType);
268  Contexts.back().IsExpression = false;
269  } else if (isLambdaParameterList(Left)) {
270  // This is a parameter list of a lambda expression.
271  Contexts.back().IsExpression = false;
272  } else if (Line.InPPDirective &&
273  (!Left->Previous || !Left->Previous->is(tok::identifier))) {
274  Contexts.back().IsExpression = true;
275  } else if (Contexts[Contexts.size() - 2].CaretFound) {
276  // This is the parameter list of an ObjC block.
277  Contexts.back().IsExpression = false;
278  } else if (Left->Previous && Left->Previous->is(TT_ForEachMacro)) {
279  // The first argument to a foreach macro is a declaration.
280  Contexts.back().IsForEachMacro = true;
281  Contexts.back().IsExpression = false;
282  } else if (Left->Previous && Left->Previous->MatchingParen &&
283  Left->Previous->MatchingParen->is(TT_ObjCBlockLParen)) {
284  Contexts.back().IsExpression = false;
285  } else if (!Line.MustBeDeclaration && !Line.InPPDirective) {
286  bool IsForOrCatch =
287  Left->Previous && Left->Previous->isOneOf(tok::kw_for, tok::kw_catch);
288  Contexts.back().IsExpression = !IsForOrCatch;
289  }
290 
291  // Infer the role of the l_paren based on the previous token if we haven't
292  // detected one one yet.
293  if (PrevNonComment && Left->is(TT_Unknown)) {
294  if (PrevNonComment->is(tok::kw___attribute)) {
295  Left->setType(TT_AttributeParen);
296  } else if (PrevNonComment->isOneOf(TT_TypenameMacro, tok::kw_decltype,
297  tok::kw_typeof, tok::kw__Atomic,
298  tok::kw___underlying_type)) {
299  Left->setType(TT_TypeDeclarationParen);
300  // decltype() and typeof() usually contain expressions.
301  if (PrevNonComment->isOneOf(tok::kw_decltype, tok::kw_typeof))
302  Contexts.back().IsExpression = true;
303  }
304  }
305 
306  if (StartsObjCMethodExpr) {
307  Contexts.back().ColonIsObjCMethodExpr = true;
308  Left->setType(TT_ObjCMethodExpr);
309  }
310 
311  // MightBeFunctionType and ProbablyFunctionType are used for
312  // function pointer and reference types as well as Objective-C
313  // block types:
314  //
315  // void (*FunctionPointer)(void);
316  // void (&FunctionReference)(void);
317  // void (&&FunctionReference)(void);
318  // void (^ObjCBlock)(void);
319  bool MightBeFunctionType = !Contexts[Contexts.size() - 2].IsExpression;
320  bool ProbablyFunctionType =
321  CurrentToken->isOneOf(tok::star, tok::amp, tok::ampamp, tok::caret);
322  bool HasMultipleLines = false;
323  bool HasMultipleParametersOnALine = false;
324  bool MightBeObjCForRangeLoop =
325  Left->Previous && Left->Previous->is(tok::kw_for);
326  FormatToken *PossibleObjCForInToken = nullptr;
327  while (CurrentToken) {
328  // LookForDecls is set when "if (" has been seen. Check for
329  // 'identifier' '*' 'identifier' followed by not '=' -- this
330  // '*' has to be a binary operator but determineStarAmpUsage() will
331  // categorize it as an unary operator, so set the right type here.
332  if (LookForDecls && CurrentToken->Next) {
333  FormatToken *Prev = CurrentToken->getPreviousNonComment();
334  if (Prev) {
335  FormatToken *PrevPrev = Prev->getPreviousNonComment();
336  FormatToken *Next = CurrentToken->Next;
337  if (PrevPrev && PrevPrev->is(tok::identifier) &&
338  Prev->isOneOf(tok::star, tok::amp, tok::ampamp) &&
339  CurrentToken->is(tok::identifier) && Next->isNot(tok::equal)) {
340  Prev->setType(TT_BinaryOperator);
341  LookForDecls = false;
342  }
343  }
344  }
345 
346  if (CurrentToken->Previous->is(TT_PointerOrReference) &&
347  CurrentToken->Previous->Previous->isOneOf(tok::l_paren,
348  tok::coloncolon))
349  ProbablyFunctionType = true;
350  if (CurrentToken->is(tok::comma))
351  MightBeFunctionType = false;
352  if (CurrentToken->Previous->is(TT_BinaryOperator))
353  Contexts.back().IsExpression = true;
354  if (CurrentToken->is(tok::r_paren)) {
355  if (MightBeFunctionType && ProbablyFunctionType && CurrentToken->Next &&
356  (CurrentToken->Next->is(tok::l_paren) ||
357  (CurrentToken->Next->is(tok::l_square) && Line.MustBeDeclaration)))
358  Left->setType(Left->Next->is(tok::caret) ? TT_ObjCBlockLParen
359  : TT_FunctionTypeLParen);
360  Left->MatchingParen = CurrentToken;
361  CurrentToken->MatchingParen = Left;
362 
363  if (CurrentToken->Next && CurrentToken->Next->is(tok::l_brace) &&
364  Left->Previous && Left->Previous->is(tok::l_paren)) {
365  // Detect the case where macros are used to generate lambdas or
366  // function bodies, e.g.:
367  // auto my_lambda = MACRO((Type *type, int i) { .. body .. });
368  for (FormatToken *Tok = Left; Tok != CurrentToken; Tok = Tok->Next) {
369  if (Tok->is(TT_BinaryOperator) &&
370  Tok->isOneOf(tok::star, tok::amp, tok::ampamp))
371  Tok->setType(TT_PointerOrReference);
372  }
373  }
374 
375  if (StartsObjCMethodExpr) {
376  CurrentToken->setType(TT_ObjCMethodExpr);
377  if (Contexts.back().FirstObjCSelectorName) {
378  Contexts.back().FirstObjCSelectorName->LongestObjCSelectorName =
379  Contexts.back().LongestObjCSelectorName;
380  }
381  }
382 
383  if (Left->is(TT_AttributeParen))
384  CurrentToken->setType(TT_AttributeParen);
385  if (Left->is(TT_TypeDeclarationParen))
386  CurrentToken->setType(TT_TypeDeclarationParen);
387  if (Left->Previous && Left->Previous->is(TT_JavaAnnotation))
388  CurrentToken->setType(TT_JavaAnnotation);
389  if (Left->Previous && Left->Previous->is(TT_LeadingJavaAnnotation))
390  CurrentToken->setType(TT_LeadingJavaAnnotation);
391  if (Left->Previous && Left->Previous->is(TT_AttributeSquare))
392  CurrentToken->setType(TT_AttributeSquare);
393 
394  if (!HasMultipleLines)
395  Left->setPackingKind(PPK_Inconclusive);
396  else if (HasMultipleParametersOnALine)
397  Left->setPackingKind(PPK_BinPacked);
398  else
399  Left->setPackingKind(PPK_OnePerLine);
400 
401  next();
402  return true;
403  }
404  if (CurrentToken->isOneOf(tok::r_square, tok::r_brace))
405  return false;
406 
407  if (CurrentToken->is(tok::l_brace))
408  Left->setType(TT_Unknown); // Not TT_ObjCBlockLParen
409  if (CurrentToken->is(tok::comma) && CurrentToken->Next &&
410  !CurrentToken->Next->HasUnescapedNewline &&
411  !CurrentToken->Next->isTrailingComment())
412  HasMultipleParametersOnALine = true;
413  bool ProbablyFunctionTypeLParen =
414  (CurrentToken->is(tok::l_paren) && CurrentToken->Next &&
415  CurrentToken->Next->isOneOf(tok::star, tok::amp, tok::caret));
416  if ((CurrentToken->Previous->isOneOf(tok::kw_const, tok::kw_auto) ||
417  CurrentToken->Previous->isSimpleTypeSpecifier()) &&
418  !(CurrentToken->is(tok::l_brace) ||
419  (CurrentToken->is(tok::l_paren) && !ProbablyFunctionTypeLParen)))
420  Contexts.back().IsExpression = false;
421  if (CurrentToken->isOneOf(tok::semi, tok::colon)) {
422  MightBeObjCForRangeLoop = false;
423  if (PossibleObjCForInToken) {
424  PossibleObjCForInToken->setType(TT_Unknown);
425  PossibleObjCForInToken = nullptr;
426  }
427  }
428  if (MightBeObjCForRangeLoop && CurrentToken->is(Keywords.kw_in)) {
429  PossibleObjCForInToken = CurrentToken;
430  PossibleObjCForInToken->setType(TT_ObjCForIn);
431  }
432  // When we discover a 'new', we set CanBeExpression to 'false' in order to
433  // parse the type correctly. Reset that after a comma.
434  if (CurrentToken->is(tok::comma))
435  Contexts.back().CanBeExpression = true;
436 
437  FormatToken *Tok = CurrentToken;
438  if (!consumeToken())
439  return false;
440  updateParameterCount(Left, Tok);
441  if (CurrentToken && CurrentToken->HasUnescapedNewline)
442  HasMultipleLines = true;
443  }
444  return false;
445  }
446 
447  bool isCSharpAttributeSpecifier(const FormatToken &Tok) {
448  if (!Style.isCSharp())
449  return false;
450 
451  // `identifier[i]` is not an attribute.
452  if (Tok.Previous && Tok.Previous->is(tok::identifier))
453  return false;
454 
455  // Chains of [] in `identifier[i][j][k]` are not attributes.
456  if (Tok.Previous && Tok.Previous->is(tok::r_square)) {
457  auto *MatchingParen = Tok.Previous->MatchingParen;
458  if (!MatchingParen || MatchingParen->is(TT_ArraySubscriptLSquare))
459  return false;
460  }
461 
462  const FormatToken *AttrTok = Tok.Next;
463  if (!AttrTok)
464  return false;
465 
466  // Just an empty declaration e.g. string [].
467  if (AttrTok->is(tok::r_square))
468  return false;
469 
470  // Move along the tokens inbetween the '[' and ']' e.g. [STAThread].
471  while (AttrTok && AttrTok->isNot(tok::r_square)) {
472  AttrTok = AttrTok->Next;
473  }
474 
475  if (!AttrTok)
476  return false;
477 
478  // Allow an attribute to be the only content of a file.
479  AttrTok = AttrTok->Next;
480  if (!AttrTok)
481  return true;
482 
483  // Limit this to being an access modifier that follows.
484  if (AttrTok->isOneOf(tok::kw_public, tok::kw_private, tok::kw_protected,
485  tok::comment, tok::kw_class, tok::kw_static,
486  tok::l_square, Keywords.kw_internal)) {
487  return true;
488  }
489 
490  // incase its a [XXX] retval func(....
491  if (AttrTok->Next &&
492  AttrTok->Next->startsSequence(tok::identifier, tok::l_paren))
493  return true;
494 
495  return false;
496  }
497 
498  bool isCpp11AttributeSpecifier(const FormatToken &Tok) {
499  if (!Style.isCpp() || !Tok.startsSequence(tok::l_square, tok::l_square))
500  return false;
501  // The first square bracket is part of an ObjC array literal
502  if (Tok.Previous && Tok.Previous->is(tok::at)) {
503  return false;
504  }
505  const FormatToken *AttrTok = Tok.Next->Next;
506  if (!AttrTok)
507  return false;
508  // C++17 '[[using ns: foo, bar(baz, blech)]]'
509  // We assume nobody will name an ObjC variable 'using'.
510  if (AttrTok->startsSequence(tok::kw_using, tok::identifier, tok::colon))
511  return true;
512  if (AttrTok->isNot(tok::identifier))
513  return false;
514  while (AttrTok && !AttrTok->startsSequence(tok::r_square, tok::r_square)) {
515  // ObjC message send. We assume nobody will use : in a C++11 attribute
516  // specifier parameter, although this is technically valid:
517  // [[foo(:)]].
518  if (AttrTok->is(tok::colon) ||
519  AttrTok->startsSequence(tok::identifier, tok::identifier) ||
520  AttrTok->startsSequence(tok::r_paren, tok::identifier))
521  return false;
522  if (AttrTok->is(tok::ellipsis))
523  return true;
524  AttrTok = AttrTok->Next;
525  }
526  return AttrTok && AttrTok->startsSequence(tok::r_square, tok::r_square);
527  }
528 
529  bool parseSquare() {
530  if (!CurrentToken)
531  return false;
532 
533  // A '[' could be an index subscript (after an identifier or after
534  // ')' or ']'), it could be the start of an Objective-C method
535  // expression, it could the start of an Objective-C array literal,
536  // or it could be a C++ attribute specifier [[foo::bar]].
537  FormatToken *Left = CurrentToken->Previous;
538  Left->ParentBracket = Contexts.back().ContextKind;
539  FormatToken *Parent = Left->getPreviousNonComment();
540 
541  // Cases where '>' is followed by '['.
542  // In C++, this can happen either in array of templates (foo<int>[10])
543  // or when array is a nested template type (unique_ptr<type1<type2>[]>).
544  bool CppArrayTemplates =
545  Style.isCpp() && Parent && Parent->is(TT_TemplateCloser) &&
546  (Contexts.back().CanBeExpression || Contexts.back().IsExpression ||
547  Contexts.back().InTemplateArgument);
548 
549  bool IsCpp11AttributeSpecifier = isCpp11AttributeSpecifier(*Left) ||
550  Contexts.back().InCpp11AttributeSpecifier;
551 
552  // Treat C# Attributes [STAThread] much like C++ attributes [[...]].
553  bool IsCSharpAttributeSpecifier =
554  isCSharpAttributeSpecifier(*Left) ||
555  Contexts.back().InCSharpAttributeSpecifier;
556 
557  bool InsideInlineASM = Line.startsWith(tok::kw_asm);
558  bool IsCppStructuredBinding = Left->isCppStructuredBinding(Style);
559  bool StartsObjCMethodExpr =
560  !IsCppStructuredBinding && !InsideInlineASM && !CppArrayTemplates &&
561  Style.isCpp() && !IsCpp11AttributeSpecifier &&
562  !IsCSharpAttributeSpecifier && Contexts.back().CanBeExpression &&
563  Left->isNot(TT_LambdaLSquare) &&
564  !CurrentToken->isOneOf(tok::l_brace, tok::r_square) &&
565  (!Parent ||
566  Parent->isOneOf(tok::colon, tok::l_square, tok::l_paren,
567  tok::kw_return, tok::kw_throw) ||
568  Parent->isUnaryOperator() ||
569  // FIXME(bug 36976): ObjC return types shouldn't use TT_CastRParen.
570  Parent->isOneOf(TT_ObjCForIn, TT_CastRParen) ||
571  (getBinOpPrecedence(Parent->Tok.getKind(), true, true) >
572  prec::Unknown));
573  bool ColonFound = false;
574 
575  unsigned BindingIncrease = 1;
576  if (IsCppStructuredBinding) {
577  Left->setType(TT_StructuredBindingLSquare);
578  } else if (Left->is(TT_Unknown)) {
579  if (StartsObjCMethodExpr) {
580  Left->setType(TT_ObjCMethodExpr);
581  } else if (InsideInlineASM) {
582  Left->setType(TT_InlineASMSymbolicNameLSquare);
583  } else if (IsCpp11AttributeSpecifier) {
584  Left->setType(TT_AttributeSquare);
585  } else if (Style.isJavaScript() && Parent &&
586  Contexts.back().ContextKind == tok::l_brace &&
587  Parent->isOneOf(tok::l_brace, tok::comma)) {
588  Left->setType(TT_JsComputedPropertyName);
589  } else if (Style.isCpp() && Contexts.back().ContextKind == tok::l_brace &&
590  Parent && Parent->isOneOf(tok::l_brace, tok::comma)) {
591  Left->setType(TT_DesignatedInitializerLSquare);
592  } else if (IsCSharpAttributeSpecifier) {
593  Left->setType(TT_AttributeSquare);
594  } else if (CurrentToken->is(tok::r_square) && Parent &&
595  Parent->is(TT_TemplateCloser)) {
596  Left->setType(TT_ArraySubscriptLSquare);
597  } else if (Style.Language == FormatStyle::LK_Proto ||
598  Style.Language == FormatStyle::LK_TextProto) {
599  // Square braces in LK_Proto can either be message field attributes:
600  //
601  // optional Aaa aaa = 1 [
602  // (aaa) = aaa
603  // ];
604  //
605  // extensions 123 [
606  // (aaa) = aaa
607  // ];
608  //
609  // or text proto extensions (in options):
610  //
611  // option (Aaa.options) = {
612  // [type.type/type] {
613  // key: value
614  // }
615  // }
616  //
617  // or repeated fields (in options):
618  //
619  // option (Aaa.options) = {
620  // keys: [ 1, 2, 3 ]
621  // }
622  //
623  // In the first and the third case we want to spread the contents inside
624  // the square braces; in the second we want to keep them inline.
625  Left->setType(TT_ArrayInitializerLSquare);
626  if (!Left->endsSequence(tok::l_square, tok::numeric_constant,
627  tok::equal) &&
628  !Left->endsSequence(tok::l_square, tok::numeric_constant,
629  tok::identifier) &&
630  !Left->endsSequence(tok::l_square, tok::colon, TT_SelectorName)) {
631  Left->setType(TT_ProtoExtensionLSquare);
632  BindingIncrease = 10;
633  }
634  } else if (!CppArrayTemplates && Parent &&
635  Parent->isOneOf(TT_BinaryOperator, TT_TemplateCloser, tok::at,
636  tok::comma, tok::l_paren, tok::l_square,
637  tok::question, tok::colon, tok::kw_return,
638  // Should only be relevant to JavaScript:
639  tok::kw_default)) {
640  Left->setType(TT_ArrayInitializerLSquare);
641  } else {
642  BindingIncrease = 10;
643  Left->setType(TT_ArraySubscriptLSquare);
644  }
645  }
646 
647  ScopedContextCreator ContextCreator(*this, tok::l_square, BindingIncrease);
648  Contexts.back().IsExpression = true;
649  if (Style.isJavaScript() && Parent && Parent->is(TT_JsTypeColon))
650  Contexts.back().IsExpression = false;
651 
652  Contexts.back().ColonIsObjCMethodExpr = StartsObjCMethodExpr;
653  Contexts.back().InCpp11AttributeSpecifier = IsCpp11AttributeSpecifier;
654  Contexts.back().InCSharpAttributeSpecifier = IsCSharpAttributeSpecifier;
655 
656  while (CurrentToken) {
657  if (CurrentToken->is(tok::r_square)) {
658  if (IsCpp11AttributeSpecifier)
659  CurrentToken->setType(TT_AttributeSquare);
660  if (IsCSharpAttributeSpecifier)
661  CurrentToken->setType(TT_AttributeSquare);
662  else if (((CurrentToken->Next &&
663  CurrentToken->Next->is(tok::l_paren)) ||
664  (CurrentToken->Previous &&
665  CurrentToken->Previous->Previous == Left)) &&
666  Left->is(TT_ObjCMethodExpr)) {
667  // An ObjC method call is rarely followed by an open parenthesis. It
668  // also can't be composed of just one token, unless it's a macro that
669  // will be expanded to more tokens.
670  // FIXME: Do we incorrectly label ":" with this?
671  StartsObjCMethodExpr = false;
672  Left->setType(TT_Unknown);
673  }
674  if (StartsObjCMethodExpr && CurrentToken->Previous != Left) {
675  CurrentToken->setType(TT_ObjCMethodExpr);
676  // If we haven't seen a colon yet, make sure the last identifier
677  // before the r_square is tagged as a selector name component.
678  if (!ColonFound && CurrentToken->Previous &&
679  CurrentToken->Previous->is(TT_Unknown) &&
680  canBeObjCSelectorComponent(*CurrentToken->Previous))
681  CurrentToken->Previous->setType(TT_SelectorName);
682  // determineStarAmpUsage() thinks that '*' '[' is allocating an
683  // array of pointers, but if '[' starts a selector then '*' is a
684  // binary operator.
685  if (Parent && Parent->is(TT_PointerOrReference))
686  Parent->setType(TT_BinaryOperator);
687  }
688  // An arrow after an ObjC method expression is not a lambda arrow.
689  if (CurrentToken->getType() == TT_ObjCMethodExpr &&
690  CurrentToken->Next && CurrentToken->Next->is(TT_LambdaArrow))
691  CurrentToken->Next->setType(TT_Unknown);
692  Left->MatchingParen = CurrentToken;
693  CurrentToken->MatchingParen = Left;
694  // FirstObjCSelectorName is set when a colon is found. This does
695  // not work, however, when the method has no parameters.
696  // Here, we set FirstObjCSelectorName when the end of the method call is
697  // reached, in case it was not set already.
698  if (!Contexts.back().FirstObjCSelectorName) {
699  FormatToken *Previous = CurrentToken->getPreviousNonComment();
700  if (Previous && Previous->is(TT_SelectorName)) {
701  Previous->ObjCSelectorNameParts = 1;
702  Contexts.back().FirstObjCSelectorName = Previous;
703  }
704  } else {
705  Left->ParameterCount =
706  Contexts.back().FirstObjCSelectorName->ObjCSelectorNameParts;
707  }
708  if (Contexts.back().FirstObjCSelectorName) {
709  Contexts.back().FirstObjCSelectorName->LongestObjCSelectorName =
710  Contexts.back().LongestObjCSelectorName;
711  if (Left->BlockParameterCount > 1)
712  Contexts.back().FirstObjCSelectorName->LongestObjCSelectorName = 0;
713  }
714  next();
715  return true;
716  }
717  if (CurrentToken->isOneOf(tok::r_paren, tok::r_brace))
718  return false;
719  if (CurrentToken->is(tok::colon)) {
720  if (IsCpp11AttributeSpecifier &&
721  CurrentToken->endsSequence(tok::colon, tok::identifier,
722  tok::kw_using)) {
723  // Remember that this is a [[using ns: foo]] C++ attribute, so we
724  // don't add a space before the colon (unlike other colons).
725  CurrentToken->setType(TT_AttributeColon);
726  } else if (Left->isOneOf(TT_ArraySubscriptLSquare,
727  TT_DesignatedInitializerLSquare)) {
728  Left->setType(TT_ObjCMethodExpr);
729  StartsObjCMethodExpr = true;
730  Contexts.back().ColonIsObjCMethodExpr = true;
731  if (Parent && Parent->is(tok::r_paren))
732  // FIXME(bug 36976): ObjC return types shouldn't use TT_CastRParen.
733  Parent->setType(TT_CastRParen);
734  }
735  ColonFound = true;
736  }
737  if (CurrentToken->is(tok::comma) && Left->is(TT_ObjCMethodExpr) &&
738  !ColonFound)
739  Left->setType(TT_ArrayInitializerLSquare);
740  FormatToken *Tok = CurrentToken;
741  if (!consumeToken())
742  return false;
743  updateParameterCount(Left, Tok);
744  }
745  return false;
746  }
747 
748  bool couldBeInStructArrayInitializer() const {
749  if (Contexts.size() < 2)
750  return false;
751  // We want to back up no more then 2 context levels i.e.
752  // . { { <-
753  const auto End = std::next(Contexts.rbegin(), 2);
754  auto Last = Contexts.rbegin();
755  unsigned Depth = 0;
756  for (; Last != End; ++Last) {
757  if (Last->ContextKind == tok::l_brace)
758  ++Depth;
759  }
760  return Depth == 2 && Last->ContextKind != tok::l_brace;
761  }
762 
763  bool parseBrace() {
764  if (CurrentToken) {
765  FormatToken *Left = CurrentToken->Previous;
766  Left->ParentBracket = Contexts.back().ContextKind;
767 
768  if (Contexts.back().CaretFound)
769  Left->setType(TT_ObjCBlockLBrace);
770  Contexts.back().CaretFound = false;
771 
772  ScopedContextCreator ContextCreator(*this, tok::l_brace, 1);
773  Contexts.back().ColonIsDictLiteral = true;
774  if (Left->is(BK_BracedInit))
775  Contexts.back().IsExpression = true;
776  if (Style.isJavaScript() && Left->Previous &&
777  Left->Previous->is(TT_JsTypeColon))
778  Contexts.back().IsExpression = false;
779 
780  unsigned CommaCount = 0;
781  while (CurrentToken) {
782  if (CurrentToken->is(tok::r_brace)) {
783  assert(Left->Optional == CurrentToken->Optional);
784  Left->MatchingParen = CurrentToken;
785  CurrentToken->MatchingParen = Left;
786  if (Style.AlignArrayOfStructures != FormatStyle::AIAS_None) {
787  if (Left->ParentBracket == tok::l_brace &&
788  couldBeInStructArrayInitializer() && CommaCount > 0) {
789  Contexts.back().InStructArrayInitializer = true;
790  }
791  }
792  next();
793  return true;
794  }
795  if (CurrentToken->isOneOf(tok::r_paren, tok::r_square))
796  return false;
797  updateParameterCount(Left, CurrentToken);
798  if (CurrentToken->isOneOf(tok::colon, tok::l_brace, tok::less)) {
799  FormatToken *Previous = CurrentToken->getPreviousNonComment();
800  if (Previous->is(TT_JsTypeOptionalQuestion))
801  Previous = Previous->getPreviousNonComment();
802  if ((CurrentToken->is(tok::colon) &&
803  (!Contexts.back().ColonIsDictLiteral || !Style.isCpp())) ||
804  Style.Language == FormatStyle::LK_Proto ||
805  Style.Language == FormatStyle::LK_TextProto) {
806  Left->setType(TT_DictLiteral);
807  if (Previous->Tok.getIdentifierInfo() ||
808  Previous->is(tok::string_literal))
809  Previous->setType(TT_SelectorName);
810  }
811  if (CurrentToken->is(tok::colon) || Style.isJavaScript())
812  Left->setType(TT_DictLiteral);
813  }
814  if (CurrentToken->is(tok::comma)) {
815  if (Style.isJavaScript())
816  Left->setType(TT_DictLiteral);
817  ++CommaCount;
818  }
819  if (!consumeToken())
820  return false;
821  }
822  }
823  return true;
824  }
825 
826  void updateParameterCount(FormatToken *Left, FormatToken *Current) {
827  // For ObjC methods, the number of parameters is calculated differently as
828  // method declarations have a different structure (the parameters are not
829  // inside a bracket scope).
830  if (Current->is(tok::l_brace) && Current->is(BK_Block))
831  ++Left->BlockParameterCount;
832  if (Current->is(tok::comma)) {
833  ++Left->ParameterCount;
834  if (!Left->Role)
835  Left->Role.reset(new CommaSeparatedList(Style));
836  Left->Role->CommaFound(Current);
837  } else if (Left->ParameterCount == 0 && Current->isNot(tok::comment)) {
838  Left->ParameterCount = 1;
839  }
840  }
841 
842  bool parseConditional() {
843  while (CurrentToken) {
844  if (CurrentToken->is(tok::colon)) {
845  CurrentToken->setType(TT_ConditionalExpr);
846  next();
847  return true;
848  }
849  if (!consumeToken())
850  return false;
851  }
852  return false;
853  }
854 
855  bool parseTemplateDeclaration() {
856  if (CurrentToken && CurrentToken->is(tok::less)) {
857  CurrentToken->setType(TT_TemplateOpener);
858  next();
859  if (!parseAngle())
860  return false;
861  if (CurrentToken)
862  CurrentToken->Previous->ClosesTemplateDeclaration = true;
863  return true;
864  }
865  return false;
866  }
867 
868  bool consumeToken() {
869  FormatToken *Tok = CurrentToken;
870  next();
871  switch (Tok->Tok.getKind()) {
872  case tok::plus:
873  case tok::minus:
874  if (!Tok->Previous && Line.MustBeDeclaration)
875  Tok->setType(TT_ObjCMethodSpecifier);
876  break;
877  case tok::colon:
878  if (!Tok->Previous)
879  return false;
880  // Colons from ?: are handled in parseConditional().
881  if (Style.isJavaScript()) {
882  if (Contexts.back().ColonIsForRangeExpr || // colon in for loop
883  (Contexts.size() == 1 && // switch/case labels
884  !Line.First->isOneOf(tok::kw_enum, tok::kw_case)) ||
885  Contexts.back().ContextKind == tok::l_paren || // function params
886  Contexts.back().ContextKind == tok::l_square || // array type
887  (!Contexts.back().IsExpression &&
888  Contexts.back().ContextKind == tok::l_brace) || // object type
889  (Contexts.size() == 1 &&
890  Line.MustBeDeclaration)) { // method/property declaration
891  Contexts.back().IsExpression = false;
892  Tok->setType(TT_JsTypeColon);
893  break;
894  }
895  } else if (Style.isCSharp()) {
896  if (Contexts.back().InCSharpAttributeSpecifier) {
897  Tok->setType(TT_AttributeColon);
898  break;
899  }
900  if (Contexts.back().ContextKind == tok::l_paren) {
901  Tok->setType(TT_CSharpNamedArgumentColon);
902  break;
903  }
904  }
905  if (Line.First->isOneOf(Keywords.kw_module, Keywords.kw_import) ||
906  Line.First->startsSequence(tok::kw_export, Keywords.kw_module) ||
907  Line.First->startsSequence(tok::kw_export, Keywords.kw_import)) {
908  Tok->setType(TT_ModulePartitionColon);
909  } else if (Contexts.back().ColonIsDictLiteral ||
910  Style.Language == FormatStyle::LK_Proto ||
911  Style.Language == FormatStyle::LK_TextProto) {
912  Tok->setType(TT_DictLiteral);
913  if (Style.Language == FormatStyle::LK_TextProto) {
914  if (FormatToken *Previous = Tok->getPreviousNonComment())
915  Previous->setType(TT_SelectorName);
916  }
917  } else if (Contexts.back().ColonIsObjCMethodExpr ||
918  Line.startsWith(TT_ObjCMethodSpecifier)) {
919  Tok->setType(TT_ObjCMethodExpr);
920  const FormatToken *BeforePrevious = Tok->Previous->Previous;
921  // Ensure we tag all identifiers in method declarations as
922  // TT_SelectorName.
923  bool UnknownIdentifierInMethodDeclaration =
924  Line.startsWith(TT_ObjCMethodSpecifier) &&
925  Tok->Previous->is(tok::identifier) && Tok->Previous->is(TT_Unknown);
926  if (!BeforePrevious ||
927  // FIXME(bug 36976): ObjC return types shouldn't use TT_CastRParen.
928  !(BeforePrevious->is(TT_CastRParen) ||
929  (BeforePrevious->is(TT_ObjCMethodExpr) &&
930  BeforePrevious->is(tok::colon))) ||
931  BeforePrevious->is(tok::r_square) ||
932  Contexts.back().LongestObjCSelectorName == 0 ||
933  UnknownIdentifierInMethodDeclaration) {
934  Tok->Previous->setType(TT_SelectorName);
935  if (!Contexts.back().FirstObjCSelectorName)
936  Contexts.back().FirstObjCSelectorName = Tok->Previous;
937  else if (Tok->Previous->ColumnWidth >
938  Contexts.back().LongestObjCSelectorName)
939  Contexts.back().LongestObjCSelectorName =
940  Tok->Previous->ColumnWidth;
941  Tok->Previous->ParameterIndex =
942  Contexts.back().FirstObjCSelectorName->ObjCSelectorNameParts;
943  ++Contexts.back().FirstObjCSelectorName->ObjCSelectorNameParts;
944  }
945  } else if (Contexts.back().ColonIsForRangeExpr) {
946  Tok->setType(TT_RangeBasedForLoopColon);
947  } else if (CurrentToken && CurrentToken->is(tok::numeric_constant)) {
948  Tok->setType(TT_BitFieldColon);
949  } else if (Contexts.size() == 1 &&
950  !Line.First->isOneOf(tok::kw_enum, tok::kw_case,
951  tok::kw_default)) {
952  FormatToken *Prev = Tok->getPreviousNonComment();
953  if (!Prev)
954  break;
955  if (Prev->isOneOf(tok::r_paren, tok::kw_noexcept))
956  Tok->setType(TT_CtorInitializerColon);
957  else if (Prev->is(tok::kw_try)) {
958  // Member initializer list within function try block.
959  FormatToken *PrevPrev = Prev->getPreviousNonComment();
960  if (!PrevPrev)
961  break;
962  if (PrevPrev && PrevPrev->isOneOf(tok::r_paren, tok::kw_noexcept))
963  Tok->setType(TT_CtorInitializerColon);
964  } else
965  Tok->setType(TT_InheritanceColon);
966  } else if (canBeObjCSelectorComponent(*Tok->Previous) && Tok->Next &&
967  (Tok->Next->isOneOf(tok::r_paren, tok::comma) ||
968  (canBeObjCSelectorComponent(*Tok->Next) && Tok->Next->Next &&
969  Tok->Next->Next->is(tok::colon)))) {
970  // This handles a special macro in ObjC code where selectors including
971  // the colon are passed as macro arguments.
972  Tok->setType(TT_ObjCMethodExpr);
973  } else if (Contexts.back().ContextKind == tok::l_paren) {
974  Tok->setType(TT_InlineASMColon);
975  }
976  break;
977  case tok::pipe:
978  case tok::amp:
979  // | and & in declarations/type expressions represent union and
980  // intersection types, respectively.
981  if (Style.isJavaScript() && !Contexts.back().IsExpression)
982  Tok->setType(TT_JsTypeOperator);
983  break;
984  case tok::kw_if:
985  case tok::kw_while:
986  if (Tok->is(tok::kw_if) && CurrentToken &&
987  CurrentToken->isOneOf(tok::kw_constexpr, tok::identifier))
988  next();
989  if (CurrentToken && CurrentToken->is(tok::l_paren)) {
990  next();
991  if (!parseParens(/*LookForDecls=*/true))
992  return false;
993  }
994  break;
995  case tok::kw_for:
996  if (Style.isJavaScript()) {
997  // x.for and {for: ...}
998  if ((Tok->Previous && Tok->Previous->is(tok::period)) ||
999  (Tok->Next && Tok->Next->is(tok::colon)))
1000  break;
1001  // JS' for await ( ...
1002  if (CurrentToken && CurrentToken->is(Keywords.kw_await))
1003  next();
1004  }
1005  if (Style.isCpp() && CurrentToken && CurrentToken->is(tok::kw_co_await))
1006  next();
1007  Contexts.back().ColonIsForRangeExpr = true;
1008  next();
1009  if (!parseParens())
1010  return false;
1011  break;
1012  case tok::l_paren:
1013  // When faced with 'operator()()', the kw_operator handler incorrectly
1014  // marks the first l_paren as a OverloadedOperatorLParen. Here, we make
1015  // the first two parens OverloadedOperators and the second l_paren an
1016  // OverloadedOperatorLParen.
1017  if (Tok->Previous && Tok->Previous->is(tok::r_paren) &&
1018  Tok->Previous->MatchingParen &&
1019  Tok->Previous->MatchingParen->is(TT_OverloadedOperatorLParen)) {
1020  Tok->Previous->setType(TT_OverloadedOperator);
1021  Tok->Previous->MatchingParen->setType(TT_OverloadedOperator);
1022  Tok->setType(TT_OverloadedOperatorLParen);
1023  }
1024 
1025  if (!parseParens())
1026  return false;
1027  if (Line.MustBeDeclaration && Contexts.size() == 1 &&
1028  !Contexts.back().IsExpression && !Line.startsWith(TT_ObjCProperty) &&
1029  !Tok->is(TT_TypeDeclarationParen) &&
1030  (!Tok->Previous || !Tok->Previous->isOneOf(tok::kw___attribute,
1031  TT_LeadingJavaAnnotation)))
1032  Line.MightBeFunctionDecl = true;
1033  break;
1034  case tok::l_square:
1035  if (!parseSquare())
1036  return false;
1037  break;
1038  case tok::l_brace:
1039  if (Style.Language == FormatStyle::LK_TextProto) {
1040  FormatToken *Previous = Tok->getPreviousNonComment();
1041  if (Previous && Previous->getType() != TT_DictLiteral)
1042  Previous->setType(TT_SelectorName);
1043  }
1044  if (!parseBrace())
1045  return false;
1046  break;
1047  case tok::less:
1048  if (parseAngle()) {
1049  Tok->setType(TT_TemplateOpener);
1050  // In TT_Proto, we must distignuish between:
1051  // map<key, value>
1052  // msg < item: data >
1053  // msg: < item: data >
1054  // In TT_TextProto, map<key, value> does not occur.
1055  if (Style.Language == FormatStyle::LK_TextProto ||
1056  (Style.Language == FormatStyle::LK_Proto && Tok->Previous &&
1057  Tok->Previous->isOneOf(TT_SelectorName, TT_DictLiteral))) {
1058  Tok->setType(TT_DictLiteral);
1059  FormatToken *Previous = Tok->getPreviousNonComment();
1060  if (Previous && Previous->getType() != TT_DictLiteral)
1061  Previous->setType(TT_SelectorName);
1062  }
1063  } else {
1064  Tok->setType(TT_BinaryOperator);
1065  NonTemplateLess.insert(Tok);
1066  CurrentToken = Tok;
1067  next();
1068  }
1069  break;
1070  case tok::r_paren:
1071  case tok::r_square:
1072  return false;
1073  case tok::r_brace:
1074  // Lines can start with '}'.
1075  if (Tok->Previous)
1076  return false;
1077  break;
1078  case tok::greater:
1079  if (Style.Language != FormatStyle::LK_TextProto)
1080  Tok->setType(TT_BinaryOperator);
1081  if (Tok->Previous && Tok->Previous->is(TT_TemplateCloser))
1082  Tok->SpacesRequiredBefore = 1;
1083  break;
1084  case tok::kw_operator:
1085  if (Style.Language == FormatStyle::LK_TextProto ||
1086  Style.Language == FormatStyle::LK_Proto)
1087  break;
1088  while (CurrentToken &&
1089  !CurrentToken->isOneOf(tok::l_paren, tok::semi, tok::r_paren)) {
1090  if (CurrentToken->isOneOf(tok::star, tok::amp))
1091  CurrentToken->setType(TT_PointerOrReference);
1092  consumeToken();
1093  if (CurrentToken && CurrentToken->is(tok::comma) &&
1094  CurrentToken->Previous->isNot(tok::kw_operator))
1095  break;
1096  if (CurrentToken && CurrentToken->Previous->isOneOf(
1097  TT_BinaryOperator, TT_UnaryOperator, tok::comma,
1098  tok::star, tok::arrow, tok::amp, tok::ampamp))
1099  CurrentToken->Previous->setType(TT_OverloadedOperator);
1100  }
1101  if (CurrentToken && CurrentToken->is(tok::l_paren))
1102  CurrentToken->setType(TT_OverloadedOperatorLParen);
1103  if (CurrentToken && CurrentToken->Previous->is(TT_BinaryOperator))
1104  CurrentToken->Previous->setType(TT_OverloadedOperator);
1105  break;
1106  case tok::question:
1107  if (Style.isJavaScript() && Tok->Next &&
1108  Tok->Next->isOneOf(tok::semi, tok::comma, tok::colon, tok::r_paren,
1109  tok::r_brace)) {
1110  // Question marks before semicolons, colons, etc. indicate optional
1111  // types (fields, parameters), e.g.
1112  // function(x?: string, y?) {...}
1113  // class X { y?; }
1114  Tok->setType(TT_JsTypeOptionalQuestion);
1115  break;
1116  }
1117  // Declarations cannot be conditional expressions, this can only be part
1118  // of a type declaration.
1119  if (Line.MustBeDeclaration && !Contexts.back().IsExpression &&
1120  Style.isJavaScript())
1121  break;
1122  if (Style.isCSharp()) {
1123  // `Type?)`, `Type?>`, `Type? name;` and `Type? name =` can only be
1124  // nullable types.
1125  // Line.MustBeDeclaration will be true for `Type? name;`.
1126  if ((!Contexts.back().IsExpression && Line.MustBeDeclaration) ||
1127  (Tok->Next && Tok->Next->isOneOf(tok::r_paren, tok::greater)) ||
1128  (Tok->Next && Tok->Next->is(tok::identifier) && Tok->Next->Next &&
1129  Tok->Next->Next->is(tok::equal))) {
1130  Tok->setType(TT_CSharpNullable);
1131  break;
1132  }
1133  }
1134  parseConditional();
1135  break;
1136  case tok::kw_template:
1137  parseTemplateDeclaration();
1138  break;
1139  case tok::comma:
1140  if (Contexts.back().InCtorInitializer)
1141  Tok->setType(TT_CtorInitializerComma);
1142  else if (Contexts.back().InInheritanceList)
1143  Tok->setType(TT_InheritanceComma);
1144  else if (Contexts.back().FirstStartOfName &&
1145  (Contexts.size() == 1 || Line.startsWith(tok::kw_for))) {
1146  Contexts.back().FirstStartOfName->PartOfMultiVariableDeclStmt = true;
1147  Line.IsMultiVariableDeclStmt = true;
1148  }
1149  if (Contexts.back().IsForEachMacro)
1150  Contexts.back().IsExpression = true;
1151  break;
1152  case tok::identifier:
1153  if (Tok->isOneOf(Keywords.kw___has_include,
1154  Keywords.kw___has_include_next)) {
1155  parseHasInclude();
1156  }
1157  if (Style.isCSharp() && Tok->is(Keywords.kw_where) && Tok->Next &&
1158  Tok->Next->isNot(tok::l_paren)) {
1159  Tok->setType(TT_CSharpGenericTypeConstraint);
1160  parseCSharpGenericTypeConstraint();
1161  }
1162  break;
1163  default:
1164  break;
1165  }
1166  return true;
1167  }
1168 
1169  void parseCSharpGenericTypeConstraint() {
1170  int OpenAngleBracketsCount = 0;
1171  while (CurrentToken) {
1172  if (CurrentToken->is(tok::less)) {
1173  // parseAngle is too greedy and will consume the whole line.
1174  CurrentToken->setType(TT_TemplateOpener);
1175  ++OpenAngleBracketsCount;
1176  next();
1177  } else if (CurrentToken->is(tok::greater)) {
1178  CurrentToken->setType(TT_TemplateCloser);
1179  --OpenAngleBracketsCount;
1180  next();
1181  } else if (CurrentToken->is(tok::comma) && OpenAngleBracketsCount == 0) {
1182  // We allow line breaks after GenericTypeConstraintComma's
1183  // so do not flag commas in Generics as GenericTypeConstraintComma's.
1184  CurrentToken->setType(TT_CSharpGenericTypeConstraintComma);
1185  next();
1186  } else if (CurrentToken->is(Keywords.kw_where)) {
1187  CurrentToken->setType(TT_CSharpGenericTypeConstraint);
1188  next();
1189  } else if (CurrentToken->is(tok::colon)) {
1190  CurrentToken->setType(TT_CSharpGenericTypeConstraintColon);
1191  next();
1192  } else {
1193  next();
1194  }
1195  }
1196  }
1197 
1198  void parseIncludeDirective() {
1199  if (CurrentToken && CurrentToken->is(tok::less)) {
1200  next();
1201  while (CurrentToken) {
1202  // Mark tokens up to the trailing line comments as implicit string
1203  // literals.
1204  if (CurrentToken->isNot(tok::comment) &&
1205  !CurrentToken->TokenText.startswith("//"))
1206  CurrentToken->setType(TT_ImplicitStringLiteral);
1207  next();
1208  }
1209  }
1210  }
1211 
1212  void parseWarningOrError() {
1213  next();
1214  // We still want to format the whitespace left of the first token of the
1215  // warning or error.
1216  next();
1217  while (CurrentToken) {
1218  CurrentToken->setType(TT_ImplicitStringLiteral);
1219  next();
1220  }
1221  }
1222 
1223  void parsePragma() {
1224  next(); // Consume "pragma".
1225  if (CurrentToken &&
1226  CurrentToken->isOneOf(Keywords.kw_mark, Keywords.kw_option)) {
1227  bool IsMark = CurrentToken->is(Keywords.kw_mark);
1228  next(); // Consume "mark".
1229  next(); // Consume first token (so we fix leading whitespace).
1230  while (CurrentToken) {
1231  if (IsMark || CurrentToken->Previous->is(TT_BinaryOperator))
1232  CurrentToken->setType(TT_ImplicitStringLiteral);
1233  next();
1234  }
1235  }
1236  }
1237 
1238  void parseHasInclude() {
1239  if (!CurrentToken || !CurrentToken->is(tok::l_paren))
1240  return;
1241  next(); // '('
1242  parseIncludeDirective();
1243  next(); // ')'
1244  }
1245 
1246  LineType parsePreprocessorDirective() {
1247  bool IsFirstToken = CurrentToken->IsFirst;
1249  next();
1250  if (!CurrentToken)
1251  return Type;
1252 
1253  if (Style.isJavaScript() && IsFirstToken) {
1254  // JavaScript files can contain shebang lines of the form:
1255  // #!/usr/bin/env node
1256  // Treat these like C++ #include directives.
1257  while (CurrentToken) {
1258  // Tokens cannot be comments here.
1259  CurrentToken->setType(TT_ImplicitStringLiteral);
1260  next();
1261  }
1262  return LT_ImportStatement;
1263  }
1264 
1265  if (CurrentToken->Tok.is(tok::numeric_constant)) {
1266  CurrentToken->SpacesRequiredBefore = 1;
1267  return Type;
1268  }
1269  // Hashes in the middle of a line can lead to any strange token
1270  // sequence.
1271  if (!CurrentToken->Tok.getIdentifierInfo())
1272  return Type;
1273  switch (CurrentToken->Tok.getIdentifierInfo()->getPPKeywordID()) {
1274  case tok::pp_include:
1275  case tok::pp_include_next:
1276  case tok::pp_import:
1277  next();
1278  parseIncludeDirective();
1280  break;
1281  case tok::pp_error:
1282  case tok::pp_warning:
1283  parseWarningOrError();
1284  break;
1285  case tok::pp_pragma:
1286  parsePragma();
1287  break;
1288  case tok::pp_if:
1289  case tok::pp_elif:
1290  Contexts.back().IsExpression = true;
1291  next();
1292  parseLine();
1293  break;
1294  default:
1295  break;
1296  }
1297  while (CurrentToken) {
1298  FormatToken *Tok = CurrentToken;
1299  next();
1300  if (Tok->is(tok::l_paren))
1301  parseParens();
1302  else if (Tok->isOneOf(Keywords.kw___has_include,
1303  Keywords.kw___has_include_next))
1304  parseHasInclude();
1305  }
1306  return Type;
1307  }
1308 
1309 public:
1310  LineType parseLine() {
1311  if (!CurrentToken)
1312  return LT_Invalid;
1313  NonTemplateLess.clear();
1314  if (CurrentToken->is(tok::hash))
1315  return parsePreprocessorDirective();
1316 
1317  // Directly allow to 'import <string-literal>' to support protocol buffer
1318  // definitions (github.com/google/protobuf) or missing "#" (either way we
1319  // should not break the line).
1320  IdentifierInfo *Info = CurrentToken->Tok.getIdentifierInfo();
1321  if ((Style.Language == FormatStyle::LK_Java &&
1322  CurrentToken->is(Keywords.kw_package)) ||
1323  (Info && Info->getPPKeywordID() == tok::pp_import &&
1324  CurrentToken->Next &&
1325  CurrentToken->Next->isOneOf(tok::string_literal, tok::identifier,
1326  tok::kw_static))) {
1327  next();
1328  parseIncludeDirective();
1329  return LT_ImportStatement;
1330  }
1331 
1332  // If this line starts and ends in '<' and '>', respectively, it is likely
1333  // part of "#define <a/b.h>".
1334  if (CurrentToken->is(tok::less) && Line.Last->is(tok::greater)) {
1335  parseIncludeDirective();
1336  return LT_ImportStatement;
1337  }
1338 
1339  // In .proto files, top-level options and package statements are very
1340  // similar to import statements and should not be line-wrapped.
1341  if (Style.Language == FormatStyle::LK_Proto && Line.Level == 0 &&
1342  CurrentToken->isOneOf(Keywords.kw_option, Keywords.kw_package)) {
1343  next();
1344  if (CurrentToken && CurrentToken->is(tok::identifier)) {
1345  while (CurrentToken)
1346  next();
1347  return LT_ImportStatement;
1348  }
1349  }
1350 
1351  bool KeywordVirtualFound = false;
1352  bool ImportStatement = false;
1353 
1354  // import {...} from '...';
1355  if (Style.isJavaScript() && CurrentToken->is(Keywords.kw_import))
1356  ImportStatement = true;
1357 
1358  while (CurrentToken) {
1359  if (CurrentToken->is(tok::kw_virtual))
1360  KeywordVirtualFound = true;
1361  if (Style.isJavaScript()) {
1362  // export {...} from '...';
1363  // An export followed by "from 'some string';" is a re-export from
1364  // another module identified by a URI and is treated as a
1365  // LT_ImportStatement (i.e. prevent wraps on it for long URIs).
1366  // Just "export {...};" or "export class ..." should not be treated as
1367  // an import in this sense.
1368  if (Line.First->is(tok::kw_export) &&
1369  CurrentToken->is(Keywords.kw_from) && CurrentToken->Next &&
1370  CurrentToken->Next->isStringLiteral())
1371  ImportStatement = true;
1372  if (isClosureImportStatement(*CurrentToken))
1373  ImportStatement = true;
1374  }
1375  if (!consumeToken())
1376  return LT_Invalid;
1377  }
1378  if (KeywordVirtualFound)
1379  return LT_VirtualFunctionDecl;
1380  if (ImportStatement)
1381  return LT_ImportStatement;
1382 
1383  if (Line.startsWith(TT_ObjCMethodSpecifier)) {
1384  if (Contexts.back().FirstObjCSelectorName)
1385  Contexts.back().FirstObjCSelectorName->LongestObjCSelectorName =
1386  Contexts.back().LongestObjCSelectorName;
1387  return LT_ObjCMethodDecl;
1388  }
1389 
1390  for (const auto &ctx : Contexts) {
1391  if (ctx.InStructArrayInitializer) {
1393  }
1394  }
1395 
1396  return LT_Other;
1397  }
1398 
1399 private:
1400  bool isClosureImportStatement(const FormatToken &Tok) {
1401  // FIXME: Closure-library specific stuff should not be hard-coded but be
1402  // configurable.
1403  return Tok.TokenText == "goog" && Tok.Next && Tok.Next->is(tok::period) &&
1404  Tok.Next->Next &&
1405  (Tok.Next->Next->TokenText == "module" ||
1406  Tok.Next->Next->TokenText == "provide" ||
1407  Tok.Next->Next->TokenText == "require" ||
1408  Tok.Next->Next->TokenText == "requireType" ||
1409  Tok.Next->Next->TokenText == "forwardDeclare") &&
1410  Tok.Next->Next->Next && Tok.Next->Next->Next->is(tok::l_paren);
1411  }
1412 
1413  void resetTokenMetadata() {
1414  if (!CurrentToken)
1415  return;
1416 
1417  // Reset token type in case we have already looked at it and then
1418  // recovered from an error (e.g. failure to find the matching >).
1419  if (!CurrentToken->isOneOf(
1420  TT_LambdaLSquare, TT_LambdaLBrace, TT_AttributeMacro, TT_IfMacro,
1421  TT_ForEachMacro, TT_TypenameMacro, TT_FunctionLBrace,
1422  TT_ImplicitStringLiteral, TT_InlineASMBrace, TT_FatArrow,
1423  TT_LambdaArrow, TT_NamespaceMacro, TT_OverloadedOperator,
1424  TT_RegexLiteral, TT_TemplateString, TT_ObjCStringLiteral,
1425  TT_UntouchableMacroFunc, TT_ConstraintJunctions,
1426  TT_StatementAttributeLikeMacro, TT_FunctionLikeOrFreestandingMacro))
1427  CurrentToken->setType(TT_Unknown);
1428  CurrentToken->Role.reset();
1429  CurrentToken->MatchingParen = nullptr;
1430  CurrentToken->FakeLParens.clear();
1431  CurrentToken->FakeRParens = 0;
1432  }
1433 
1434  void next() {
1435  if (!CurrentToken)
1436  return;
1437 
1438  CurrentToken->NestingLevel = Contexts.size() - 1;
1439  CurrentToken->BindingStrength = Contexts.back().BindingStrength;
1440  modifyContext(*CurrentToken);
1441  determineTokenType(*CurrentToken);
1442  CurrentToken = CurrentToken->Next;
1443 
1444  resetTokenMetadata();
1445  }
1446 
1447  /// A struct to hold information valid in a specific context, e.g.
1448  /// a pair of parenthesis.
1449  struct Context {
1450  Context(tok::TokenKind ContextKind, unsigned BindingStrength,
1451  bool IsExpression)
1454 
1459  bool ColonIsForRangeExpr = false;
1460  bool ColonIsDictLiteral = false;
1462  FormatToken *FirstObjCSelectorName = nullptr;
1463  FormatToken *FirstStartOfName = nullptr;
1464  bool CanBeExpression = true;
1465  bool InTemplateArgument = false;
1466  bool InCtorInitializer = false;
1467  bool InInheritanceList = false;
1468  bool CaretFound = false;
1469  bool IsForEachMacro = false;
1473  };
1474 
1475  /// Puts a new \c Context onto the stack \c Contexts for the lifetime
1476  /// of each instance.
1477  struct ScopedContextCreator {
1478  AnnotatingParser &P;
1479 
1480  ScopedContextCreator(AnnotatingParser &P, tok::TokenKind ContextKind,
1481  unsigned Increase)
1482  : P(P) {
1483  P.Contexts.push_back(Context(ContextKind,
1484  P.Contexts.back().BindingStrength + Increase,
1485  P.Contexts.back().IsExpression));
1486  }
1487 
1488  ~ScopedContextCreator() {
1489  if (P.Style.AlignArrayOfStructures != FormatStyle::AIAS_None) {
1490  if (P.Contexts.back().InStructArrayInitializer) {
1491  P.Contexts.pop_back();
1492  P.Contexts.back().InStructArrayInitializer = true;
1493  return;
1494  }
1495  }
1496  P.Contexts.pop_back();
1497  }
1498  };
1499 
1500  void modifyContext(const FormatToken &Current) {
1501  if (Current.getPrecedence() == prec::Assignment &&
1502  !Line.First->isOneOf(tok::kw_template, tok::kw_using, tok::kw_return) &&
1503  // Type aliases use `type X = ...;` in TypeScript and can be exported
1504  // using `export type ...`.
1505  !(Style.isJavaScript() &&
1506  (Line.startsWith(Keywords.kw_type, tok::identifier) ||
1507  Line.startsWith(tok::kw_export, Keywords.kw_type,
1508  tok::identifier))) &&
1509  (!Current.Previous || Current.Previous->isNot(tok::kw_operator))) {
1510  Contexts.back().IsExpression = true;
1511  if (!Line.startsWith(TT_UnaryOperator)) {
1512  for (FormatToken *Previous = Current.Previous;
1513  Previous && Previous->Previous &&
1514  !Previous->Previous->isOneOf(tok::comma, tok::semi);
1515  Previous = Previous->Previous) {
1516  if (Previous->isOneOf(tok::r_square, tok::r_paren)) {
1517  Previous = Previous->MatchingParen;
1518  if (!Previous)
1519  break;
1520  }
1521  if (Previous->opensScope())
1522  break;
1523  if (Previous->isOneOf(TT_BinaryOperator, TT_UnaryOperator) &&
1524  Previous->isOneOf(tok::star, tok::amp, tok::ampamp) &&
1525  Previous->Previous && Previous->Previous->isNot(tok::equal))
1526  Previous->setType(TT_PointerOrReference);
1527  }
1528  }
1529  } else if (Current.is(tok::lessless) &&
1530  (!Current.Previous || !Current.Previous->is(tok::kw_operator))) {
1531  Contexts.back().IsExpression = true;
1532  } else if (Current.isOneOf(tok::kw_return, tok::kw_throw)) {
1533  Contexts.back().IsExpression = true;
1534  } else if (Current.is(TT_TrailingReturnArrow)) {
1535  Contexts.back().IsExpression = false;
1536  } else if (Current.is(TT_LambdaArrow) || Current.is(Keywords.kw_assert)) {
1537  Contexts.back().IsExpression = Style.Language == FormatStyle::LK_Java;
1538  } else if (Current.Previous &&
1539  Current.Previous->is(TT_CtorInitializerColon)) {
1540  Contexts.back().IsExpression = true;
1541  Contexts.back().InCtorInitializer = true;
1542  } else if (Current.Previous && Current.Previous->is(TT_InheritanceColon)) {
1543  Contexts.back().InInheritanceList = true;
1544  } else if (Current.isOneOf(tok::r_paren, tok::greater, tok::comma)) {
1545  for (FormatToken *Previous = Current.Previous;
1546  Previous && Previous->isOneOf(tok::star, tok::amp);
1547  Previous = Previous->Previous)
1548  Previous->setType(TT_PointerOrReference);
1549  if (Line.MustBeDeclaration && !Contexts.front().InCtorInitializer)
1550  Contexts.back().IsExpression = false;
1551  } else if (Current.is(tok::kw_new)) {
1552  Contexts.back().CanBeExpression = false;
1553  } else if (Current.is(tok::semi) ||
1554  (Current.is(tok::exclaim) && Current.Previous &&
1555  !Current.Previous->is(tok::kw_operator))) {
1556  // This should be the condition or increment in a for-loop.
1557  // But not operator !() (can't use TT_OverloadedOperator here as its not
1558  // been annotated yet).
1559  Contexts.back().IsExpression = true;
1560  }
1561  }
1562 
1563  static FormatToken *untilMatchingParen(FormatToken *Current) {
1564  // Used when `MatchingParen` is not yet established.
1565  int ParenLevel = 0;
1566  while (Current) {
1567  if (Current->is(tok::l_paren))
1568  ++ParenLevel;
1569  if (Current->is(tok::r_paren))
1570  --ParenLevel;
1571  if (ParenLevel < 1)
1572  break;
1573  Current = Current->Next;
1574  }
1575  return Current;
1576  }
1577 
1578  static bool isDeductionGuide(FormatToken &Current) {
1579  // Look for a deduction guide template<T> A(...) -> A<...>;
1580  if (Current.Previous && Current.Previous->is(tok::r_paren) &&
1581  Current.startsSequence(tok::arrow, tok::identifier, tok::less)) {
1582  // Find the TemplateCloser.
1583  FormatToken *TemplateCloser = Current.Next->Next;
1584  int NestingLevel = 0;
1585  while (TemplateCloser) {
1586  // Skip over an expressions in parens A<(3 < 2)>;
1587  if (TemplateCloser->is(tok::l_paren)) {
1588  // No Matching Paren yet so skip to matching paren
1589  TemplateCloser = untilMatchingParen(TemplateCloser);
1590  if (!TemplateCloser)
1591  break;
1592  }
1593  if (TemplateCloser->is(tok::less))
1594  ++NestingLevel;
1595  if (TemplateCloser->is(tok::greater))
1596  --NestingLevel;
1597  if (NestingLevel < 1)
1598  break;
1599  TemplateCloser = TemplateCloser->Next;
1600  }
1601  // Assuming we have found the end of the template ensure its followed
1602  // with a semi-colon.
1603  if (TemplateCloser && TemplateCloser->Next &&
1604  TemplateCloser->Next->is(tok::semi) &&
1605  Current.Previous->MatchingParen) {
1606  // Determine if the identifier `A` prior to the A<..>; is the same as
1607  // prior to the A(..)
1608  FormatToken *LeadingIdentifier =
1609  Current.Previous->MatchingParen->Previous;
1610 
1611  // Differentiate a deduction guide by seeing the
1612  // > of the template prior to the leading identifier.
1613  if (LeadingIdentifier) {
1614  FormatToken *PriorLeadingIdentifier = LeadingIdentifier->Previous;
1615  // Skip back past explicit decoration
1616  if (PriorLeadingIdentifier &&
1617  PriorLeadingIdentifier->is(tok::kw_explicit))
1618  PriorLeadingIdentifier = PriorLeadingIdentifier->Previous;
1619 
1620  return (PriorLeadingIdentifier &&
1621  PriorLeadingIdentifier->is(TT_TemplateCloser) &&
1622  LeadingIdentifier->TokenText == Current.Next->TokenText);
1623  }
1624  }
1625  }
1626  return false;
1627  }
1628 
1629  void determineTokenType(FormatToken &Current) {
1630  if (!Current.is(TT_Unknown))
1631  // The token type is already known.
1632  return;
1633 
1634  if ((Style.isJavaScript() || Style.isCSharp()) &&
1635  Current.is(tok::exclaim)) {
1636  if (Current.Previous) {
1637  bool IsIdentifier =
1638  Style.isJavaScript()
1639  ? Keywords.IsJavaScriptIdentifier(
1640  *Current.Previous, /* AcceptIdentifierName= */ true)
1641  : Current.Previous->is(tok::identifier);
1642  if (IsIdentifier ||
1643  Current.Previous->isOneOf(
1644  tok::kw_namespace, tok::r_paren, tok::r_square, tok::r_brace,
1645  tok::kw_false, tok::kw_true, Keywords.kw_type, Keywords.kw_get,
1646  Keywords.kw_set) ||
1647  Current.Previous->Tok.isLiteral()) {
1648  Current.setType(TT_NonNullAssertion);
1649  return;
1650  }
1651  }
1652  if (Current.Next &&
1653  Current.Next->isOneOf(TT_BinaryOperator, Keywords.kw_as)) {
1654  Current.setType(TT_NonNullAssertion);
1655  return;
1656  }
1657  }
1658 
1659  // Line.MightBeFunctionDecl can only be true after the parentheses of a
1660  // function declaration have been found. In this case, 'Current' is a
1661  // trailing token of this declaration and thus cannot be a name.
1662  if (Current.is(Keywords.kw_instanceof)) {
1663  Current.setType(TT_BinaryOperator);
1664  } else if (isStartOfName(Current) &&
1665  (!Line.MightBeFunctionDecl || Current.NestingLevel != 0)) {
1666  Contexts.back().FirstStartOfName = &Current;
1667  Current.setType(TT_StartOfName);
1668  } else if (Current.is(tok::semi)) {
1669  // Reset FirstStartOfName after finding a semicolon so that a for loop
1670  // with multiple increment statements is not confused with a for loop
1671  // having multiple variable declarations.
1672  Contexts.back().FirstStartOfName = nullptr;
1673  } else if (Current.isOneOf(tok::kw_auto, tok::kw___auto_type)) {
1674  AutoFound = true;
1675  } else if (Current.is(tok::arrow) &&
1676  Style.Language == FormatStyle::LK_Java) {
1677  Current.setType(TT_LambdaArrow);
1678  } else if (Current.is(tok::arrow) && AutoFound && Line.MustBeDeclaration &&
1679  Current.NestingLevel == 0 &&
1680  !Current.Previous->isOneOf(tok::kw_operator, tok::identifier)) {
1681  // not auto operator->() -> xxx;
1682  Current.setType(TT_TrailingReturnArrow);
1683  } else if (Current.is(tok::arrow) && Current.Previous &&
1684  Current.Previous->is(tok::r_brace)) {
1685  // Concept implicit conversion constraint needs to be treated like
1686  // a trailing return type ... } -> <type>.
1687  Current.setType(TT_TrailingReturnArrow);
1688  } else if (isDeductionGuide(Current)) {
1689  // Deduction guides trailing arrow " A(...) -> A<T>;".
1690  Current.setType(TT_TrailingReturnArrow);
1691  } else if (Current.isOneOf(tok::star, tok::amp, tok::ampamp)) {
1692  Current.setType(determineStarAmpUsage(
1693  Current,
1694  Contexts.back().CanBeExpression && Contexts.back().IsExpression,
1695  Contexts.back().InTemplateArgument));
1696  } else if (Current.isOneOf(tok::minus, tok::plus, tok::caret)) {
1697  Current.setType(determinePlusMinusCaretUsage(Current));
1698  if (Current.is(TT_UnaryOperator) && Current.is(tok::caret))
1699  Contexts.back().CaretFound = true;
1700  } else if (Current.isOneOf(tok::minusminus, tok::plusplus)) {
1701  Current.setType(determineIncrementUsage(Current));
1702  } else if (Current.isOneOf(tok::exclaim, tok::tilde)) {
1703  Current.setType(TT_UnaryOperator);
1704  } else if (Current.is(tok::question)) {
1705  if (Style.isJavaScript() && Line.MustBeDeclaration &&
1706  !Contexts.back().IsExpression) {
1707  // In JavaScript, `interface X { foo?(): bar; }` is an optional method
1708  // on the interface, not a ternary expression.
1709  Current.setType(TT_JsTypeOptionalQuestion);
1710  } else {
1711  Current.setType(TT_ConditionalExpr);
1712  }
1713  } else if (Current.isBinaryOperator() &&
1714  (!Current.Previous || Current.Previous->isNot(tok::l_square)) &&
1715  (!Current.is(tok::greater) &&
1716  Style.Language != FormatStyle::LK_TextProto)) {
1717  Current.setType(TT_BinaryOperator);
1718  } else if (Current.is(tok::comment)) {
1719  if (Current.TokenText.startswith("/*")) {
1720  if (Current.TokenText.endswith("*/"))
1721  Current.setType(TT_BlockComment);
1722  else
1723  // The lexer has for some reason determined a comment here. But we
1724  // cannot really handle it, if it isn't properly terminated.
1725  Current.Tok.setKind(tok::unknown);
1726  } else {
1727  Current.setType(TT_LineComment);
1728  }
1729  } else if (Current.is(tok::r_paren)) {
1730  if (rParenEndsCast(Current))
1731  Current.setType(TT_CastRParen);
1732  if (Current.MatchingParen && Current.Next &&
1733  !Current.Next->isBinaryOperator() &&
1734  !Current.Next->isOneOf(tok::semi, tok::colon, tok::l_brace,
1735  tok::comma, tok::period, tok::arrow,
1736  tok::coloncolon))
1737  if (FormatToken *AfterParen = Current.MatchingParen->Next) {
1738  // Make sure this isn't the return type of an Obj-C block declaration
1739  if (AfterParen->Tok.isNot(tok::caret)) {
1740  if (FormatToken *BeforeParen = Current.MatchingParen->Previous)
1741  if (BeforeParen->is(tok::identifier) &&
1742  !BeforeParen->is(TT_TypenameMacro) &&
1743  BeforeParen->TokenText == BeforeParen->TokenText.upper() &&
1744  (!BeforeParen->Previous ||
1745  BeforeParen->Previous->ClosesTemplateDeclaration))
1746  Current.setType(TT_FunctionAnnotationRParen);
1747  }
1748  }
1749  } else if (Current.is(tok::at) && Current.Next && !Style.isJavaScript() &&
1750  Style.Language != FormatStyle::LK_Java) {
1751  // In Java & JavaScript, "@..." is a decorator or annotation. In ObjC, it
1752  // marks declarations and properties that need special formatting.
1753  switch (Current.Next->Tok.getObjCKeywordID()) {
1754  case tok::objc_interface:
1755  case tok::objc_implementation:
1756  case tok::objc_protocol:
1757  Current.setType(TT_ObjCDecl);
1758  break;
1759  case tok::objc_property:
1760  Current.setType(TT_ObjCProperty);
1761  break;
1762  default:
1763  break;
1764  }
1765  } else if (Current.is(tok::period)) {
1766  FormatToken *PreviousNoComment = Current.getPreviousNonComment();
1767  if (PreviousNoComment &&
1768  PreviousNoComment->isOneOf(tok::comma, tok::l_brace))
1769  Current.setType(TT_DesignatedInitializerPeriod);
1770  else if (Style.Language == FormatStyle::LK_Java && Current.Previous &&
1771  Current.Previous->isOneOf(TT_JavaAnnotation,
1772  TT_LeadingJavaAnnotation)) {
1773  Current.setType(Current.Previous->getType());
1774  }
1775  } else if (canBeObjCSelectorComponent(Current) &&
1776  // FIXME(bug 36976): ObjC return types shouldn't use
1777  // TT_CastRParen.
1778  Current.Previous && Current.Previous->is(TT_CastRParen) &&
1779  Current.Previous->MatchingParen &&
1780  Current.Previous->MatchingParen->Previous &&
1781  Current.Previous->MatchingParen->Previous->is(
1782  TT_ObjCMethodSpecifier)) {
1783  // This is the first part of an Objective-C selector name. (If there's no
1784  // colon after this, this is the only place which annotates the identifier
1785  // as a selector.)
1786  Current.setType(TT_SelectorName);
1787  } else if (Current.isOneOf(tok::identifier, tok::kw_const, tok::kw_noexcept,
1788  tok::kw_requires) &&
1789  Current.Previous &&
1790  !Current.Previous->isOneOf(tok::equal, tok::at) &&
1791  Line.MightBeFunctionDecl && Contexts.size() == 1) {
1792  // Line.MightBeFunctionDecl can only be true after the parentheses of a
1793  // function declaration have been found.
1794  Current.setType(TT_TrailingAnnotation);
1795  } else if ((Style.Language == FormatStyle::LK_Java ||
1796  Style.isJavaScript()) &&
1797  Current.Previous) {
1798  if (Current.Previous->is(tok::at) &&
1799  Current.isNot(Keywords.kw_interface)) {
1800  const FormatToken &AtToken = *Current.Previous;
1801  const FormatToken *Previous = AtToken.getPreviousNonComment();
1802  if (!Previous || Previous->is(TT_LeadingJavaAnnotation))
1803  Current.setType(TT_LeadingJavaAnnotation);
1804  else
1805  Current.setType(TT_JavaAnnotation);
1806  } else if (Current.Previous->is(tok::period) &&
1807  Current.Previous->isOneOf(TT_JavaAnnotation,
1808  TT_LeadingJavaAnnotation)) {
1809  Current.setType(Current.Previous->getType());
1810  }
1811  }
1812  }
1813 
1814  /// Take a guess at whether \p Tok starts a name of a function or
1815  /// variable declaration.
1816  ///
1817  /// This is a heuristic based on whether \p Tok is an identifier following
1818  /// something that is likely a type.
1819  bool isStartOfName(const FormatToken &Tok) {
1820  if (Tok.isNot(tok::identifier) || !Tok.Previous)
1821  return false;
1822 
1823  if (Tok.Previous->isOneOf(TT_LeadingJavaAnnotation, Keywords.kw_instanceof,
1824  Keywords.kw_as))
1825  return false;
1826  if (Style.isJavaScript() && Tok.Previous->is(Keywords.kw_in))
1827  return false;
1828 
1829  // Skip "const" as it does not have an influence on whether this is a name.
1830  FormatToken *PreviousNotConst = Tok.getPreviousNonComment();
1831 
1832  // For javascript const can be like "let" or "var"
1833  if (!Style.isJavaScript())
1834  while (PreviousNotConst && PreviousNotConst->is(tok::kw_const))
1835  PreviousNotConst = PreviousNotConst->getPreviousNonComment();
1836 
1837  if (!PreviousNotConst)
1838  return false;
1839 
1840  bool IsPPKeyword = PreviousNotConst->is(tok::identifier) &&
1841  PreviousNotConst->Previous &&
1842  PreviousNotConst->Previous->is(tok::hash);
1843 
1844  if (PreviousNotConst->is(TT_TemplateCloser))
1845  return PreviousNotConst && PreviousNotConst->MatchingParen &&
1846  PreviousNotConst->MatchingParen->Previous &&
1847  PreviousNotConst->MatchingParen->Previous->isNot(tok::period) &&
1848  PreviousNotConst->MatchingParen->Previous->isNot(tok::kw_template);
1849 
1850  if (PreviousNotConst->is(tok::r_paren) &&
1851  PreviousNotConst->is(TT_TypeDeclarationParen))
1852  return true;
1853 
1854  // If is a preprocess keyword like #define.
1855  if (IsPPKeyword)
1856  return false;
1857 
1858  // int a or auto a.
1859  if (PreviousNotConst->isOneOf(tok::identifier, tok::kw_auto))
1860  return true;
1861 
1862  // *a or &a or &&a.
1863  if (PreviousNotConst->is(TT_PointerOrReference))
1864  return true;
1865 
1866  // MyClass a;
1867  if (PreviousNotConst->isSimpleTypeSpecifier())
1868  return true;
1869 
1870  // const a = in JavaScript.
1871  return (Style.isJavaScript() && PreviousNotConst->is(tok::kw_const));
1872  }
1873 
1874  /// Determine whether ')' is ending a cast.
1875  bool rParenEndsCast(const FormatToken &Tok) {
1876  // C-style casts are only used in C++, C# and Java.
1877  if (!Style.isCSharp() && !Style.isCpp() &&
1878  Style.Language != FormatStyle::LK_Java)
1879  return false;
1880 
1881  // Empty parens aren't casts and there are no casts at the end of the line.
1882  if (Tok.Previous == Tok.MatchingParen || !Tok.Next || !Tok.MatchingParen)
1883  return false;
1884 
1885  FormatToken *LeftOfParens = Tok.MatchingParen->getPreviousNonComment();
1886  if (LeftOfParens) {
1887  // If there is a closing parenthesis left of the current
1888  // parentheses, look past it as these might be chained casts.
1889  if (LeftOfParens->is(tok::r_paren) &&
1890  LeftOfParens->isNot(TT_CastRParen)) {
1891  if (!LeftOfParens->MatchingParen ||
1892  !LeftOfParens->MatchingParen->Previous)
1893  return false;
1894  LeftOfParens = LeftOfParens->MatchingParen->Previous;
1895  }
1896 
1897  // The Condition directly below this one will see the operator arguments
1898  // as a (void *foo) cast.
1899  // void operator delete(void *foo) ATTRIB;
1900  if (LeftOfParens->Tok.getIdentifierInfo() && LeftOfParens->Previous &&
1901  LeftOfParens->Previous->is(tok::kw_operator))
1902  return false;
1903 
1904  // If there is an identifier (or with a few exceptions a keyword) right
1905  // before the parentheses, this is unlikely to be a cast.
1906  if (LeftOfParens->Tok.getIdentifierInfo() &&
1907  !LeftOfParens->isOneOf(Keywords.kw_in, tok::kw_return, tok::kw_case,
1908  tok::kw_delete))
1909  return false;
1910 
1911  // Certain other tokens right before the parentheses are also signals that
1912  // this cannot be a cast.
1913  if (LeftOfParens->isOneOf(tok::at, tok::r_square, TT_OverloadedOperator,
1914  TT_TemplateCloser, tok::ellipsis))
1915  return false;
1916  }
1917 
1918  if (Tok.Next->is(tok::question))
1919  return false;
1920 
1921  // `foreach((A a, B b) in someList)` should not be seen as a cast.
1922  if (Tok.Next->is(Keywords.kw_in) && Style.isCSharp())
1923  return false;
1924 
1925  // Functions which end with decorations like volatile, noexcept are unlikely
1926  // to be casts.
1927  if (Tok.Next->isOneOf(tok::kw_noexcept, tok::kw_volatile, tok::kw_const,
1928  tok::kw_requires, tok::kw_throw, tok::arrow,
1929  Keywords.kw_override, Keywords.kw_final) ||
1930  isCpp11AttributeSpecifier(*Tok.Next))
1931  return false;
1932 
1933  // As Java has no function types, a "(" after the ")" likely means that this
1934  // is a cast.
1935  if (Style.Language == FormatStyle::LK_Java && Tok.Next->is(tok::l_paren))
1936  return true;
1937 
1938  // If a (non-string) literal follows, this is likely a cast.
1939  if (Tok.Next->isNot(tok::string_literal) &&
1940  (Tok.Next->Tok.isLiteral() ||
1941  Tok.Next->isOneOf(tok::kw_sizeof, tok::kw_alignof)))
1942  return true;
1943 
1944  // Heuristically try to determine whether the parentheses contain a type.
1945  auto IsQualifiedPointerOrReference = [](FormatToken *T) {
1946  // This is used to handle cases such as x = (foo *const)&y;
1947  assert(!T->isSimpleTypeSpecifier() && "Should have already been checked");
1948  // Strip trailing qualifiers such as const or volatile when checking
1949  // whether the parens could be a cast to a pointer/reference type.
1950  while (T) {
1951  if (T->is(TT_AttributeParen)) {
1952  // Handle `x = (foo *__attribute__((foo)))&v;`:
1953  if (T->MatchingParen && T->MatchingParen->Previous &&
1954  T->MatchingParen->Previous->is(tok::kw___attribute)) {
1955  T = T->MatchingParen->Previous->Previous;
1956  continue;
1957  }
1958  } else if (T->is(TT_AttributeSquare)) {
1959  // Handle `x = (foo *[[clang::foo]])&v;`:
1960  if (T->MatchingParen && T->MatchingParen->Previous) {
1961  T = T->MatchingParen->Previous;
1962  continue;
1963  }
1964  } else if (T->canBePointerOrReferenceQualifier()) {
1965  T = T->Previous;
1966  continue;
1967  }
1968  break;
1969  }
1970  return T && T->is(TT_PointerOrReference);
1971  };
1972  bool ParensAreType =
1973  !Tok.Previous ||
1974  Tok.Previous->isOneOf(TT_TemplateCloser, TT_TypeDeclarationParen) ||
1975  Tok.Previous->isSimpleTypeSpecifier() ||
1976  IsQualifiedPointerOrReference(Tok.Previous);
1977  bool ParensCouldEndDecl =
1978  Tok.Next->isOneOf(tok::equal, tok::semi, tok::l_brace, tok::greater);
1979  if (ParensAreType && !ParensCouldEndDecl)
1980  return true;
1981 
1982  // At this point, we heuristically assume that there are no casts at the
1983  // start of the line. We assume that we have found most cases where there
1984  // are by the logic above, e.g. "(void)x;".
1985  if (!LeftOfParens)
1986  return false;
1987 
1988  // Certain token types inside the parentheses mean that this can't be a
1989  // cast.
1990  for (const FormatToken *Token = Tok.MatchingParen->Next; Token != &Tok;
1991  Token = Token->Next)
1992  if (Token->is(TT_BinaryOperator))
1993  return false;
1994 
1995  // If the following token is an identifier or 'this', this is a cast. All
1996  // cases where this can be something else are handled above.
1997  if (Tok.Next->isOneOf(tok::identifier, tok::kw_this))
1998  return true;
1999 
2000  // Look for a cast `( x ) (`.
2001  if (Tok.Next->is(tok::l_paren) && Tok.Previous && Tok.Previous->Previous) {
2002  if (Tok.Previous->is(tok::identifier) &&
2003  Tok.Previous->Previous->is(tok::l_paren))
2004  return true;
2005  }
2006 
2007  if (!Tok.Next->Next)
2008  return false;
2009 
2010  // If the next token after the parenthesis is a unary operator, assume
2011  // that this is cast, unless there are unexpected tokens inside the
2012  // parenthesis.
2013  bool NextIsUnary =
2014  Tok.Next->isUnaryOperator() || Tok.Next->isOneOf(tok::amp, tok::star);
2015  if (!NextIsUnary || Tok.Next->is(tok::plus) ||
2016  !Tok.Next->Next->isOneOf(tok::identifier, tok::numeric_constant))
2017  return false;
2018  // Search for unexpected tokens.
2019  for (FormatToken *Prev = Tok.Previous; Prev != Tok.MatchingParen;
2020  Prev = Prev->Previous) {
2021  if (!Prev->isOneOf(tok::kw_const, tok::identifier, tok::coloncolon))
2022  return false;
2023  }
2024  return true;
2025  }
2026 
2027  /// Return the type of the given token assuming it is * or &.
2028  TokenType determineStarAmpUsage(const FormatToken &Tok, bool IsExpression,
2029  bool InTemplateArgument) {
2030  if (Style.isJavaScript())
2031  return TT_BinaryOperator;
2032 
2033  // && in C# must be a binary operator.
2034  if (Style.isCSharp() && Tok.is(tok::ampamp))
2035  return TT_BinaryOperator;
2036 
2037  const FormatToken *PrevToken = Tok.getPreviousNonComment();
2038  if (!PrevToken)
2039  return TT_UnaryOperator;
2040 
2041  const FormatToken *NextToken = Tok.getNextNonComment();
2042  if (!NextToken ||
2043  NextToken->isOneOf(tok::arrow, tok::equal, tok::kw_noexcept) ||
2044  NextToken->canBePointerOrReferenceQualifier() ||
2045  (NextToken->is(tok::l_brace) && !NextToken->getNextNonComment()))
2046  return TT_PointerOrReference;
2047 
2048  if (PrevToken->is(tok::coloncolon))
2049  return TT_PointerOrReference;
2050 
2051  if (PrevToken->is(tok::r_paren) && PrevToken->is(TT_TypeDeclarationParen))
2052  return TT_PointerOrReference;
2053 
2054  if (PrevToken->isOneOf(tok::l_paren, tok::l_square, tok::l_brace,
2055  tok::comma, tok::semi, tok::kw_return, tok::colon,
2056  tok::kw_co_return, tok::kw_co_await,
2057  tok::kw_co_yield, tok::equal, tok::kw_delete,
2058  tok::kw_sizeof, tok::kw_throw, TT_BinaryOperator,
2059  TT_ConditionalExpr, TT_UnaryOperator, TT_CastRParen))
2060  return TT_UnaryOperator;
2061 
2062  if (NextToken->is(tok::l_square) && NextToken->isNot(TT_LambdaLSquare))
2063  return TT_PointerOrReference;
2064  if (NextToken->is(tok::kw_operator) && !IsExpression)
2065  return TT_PointerOrReference;
2066  if (NextToken->isOneOf(tok::comma, tok::semi))
2067  return TT_PointerOrReference;
2068 
2069  if (PrevToken->Tok.isLiteral() ||
2070  PrevToken->isOneOf(tok::r_paren, tok::r_square, tok::kw_true,
2071  tok::kw_false, tok::r_brace) ||
2072  NextToken->Tok.isLiteral() ||
2073  NextToken->isOneOf(tok::kw_true, tok::kw_false) ||
2074  NextToken->isUnaryOperator() ||
2075  // If we know we're in a template argument, there are no named
2076  // declarations. Thus, having an identifier on the right-hand side
2077  // indicates a binary operator.
2078  (InTemplateArgument && NextToken->Tok.isAnyIdentifier()))
2079  return TT_BinaryOperator;
2080 
2081  // "&&(" is quite unlikely to be two successive unary "&".
2082  if (Tok.is(tok::ampamp) && NextToken->is(tok::l_paren))
2083  return TT_BinaryOperator;
2084 
2085  // This catches some cases where evaluation order is used as control flow:
2086  // aaa && aaa->f();
2087  if (NextToken->Tok.isAnyIdentifier()) {
2088  const FormatToken *NextNextToken = NextToken->getNextNonComment();
2089  if (NextNextToken && NextNextToken->is(tok::arrow))
2090  return TT_BinaryOperator;
2091  }
2092 
2093  // It is very unlikely that we are going to find a pointer or reference type
2094  // definition on the RHS of an assignment.
2095  if (IsExpression && !Contexts.back().CaretFound)
2096  return TT_BinaryOperator;
2097 
2098  return TT_PointerOrReference;
2099  }
2100 
2101  TokenType determinePlusMinusCaretUsage(const FormatToken &Tok) {
2102  const FormatToken *PrevToken = Tok.getPreviousNonComment();
2103  if (!PrevToken)
2104  return TT_UnaryOperator;
2105 
2106  if (PrevToken->isOneOf(TT_CastRParen, TT_UnaryOperator))
2107  // This must be a sequence of leading unary operators.
2108  return TT_UnaryOperator;
2109 
2110  // Use heuristics to recognize unary operators.
2111  if (PrevToken->isOneOf(tok::equal, tok::l_paren, tok::comma, tok::l_square,
2112  tok::question, tok::colon, tok::kw_return,
2113  tok::kw_case, tok::at, tok::l_brace, tok::kw_throw,
2114  tok::kw_co_return, tok::kw_co_yield))
2115  return TT_UnaryOperator;
2116 
2117  // There can't be two consecutive binary operators.
2118  if (PrevToken->is(TT_BinaryOperator))
2119  return TT_UnaryOperator;
2120 
2121  // Fall back to marking the token as binary operator.
2122  return TT_BinaryOperator;
2123  }
2124 
2125  /// Determine whether ++/-- are pre- or post-increments/-decrements.
2126  TokenType determineIncrementUsage(const FormatToken &Tok) {
2127  const FormatToken *PrevToken = Tok.getPreviousNonComment();
2128  if (!PrevToken || PrevToken->is(TT_CastRParen))
2129  return TT_UnaryOperator;
2130  if (PrevToken->isOneOf(tok::r_paren, tok::r_square, tok::identifier))
2131  return TT_TrailingUnaryOperator;
2132 
2133  return TT_UnaryOperator;
2134  }
2135 
2136  SmallVector<Context, 8> Contexts;
2137 
2138  const FormatStyle &Style;
2139  AnnotatedLine &Line;
2140  FormatToken *CurrentToken;
2141  bool AutoFound;
2142  const AdditionalKeywords &Keywords;
2143 
2144  // Set of "<" tokens that do not open a template parameter list. If parseAngle
2145  // determines that a specific token can't be a template opener, it will make
2146  // same decision irrespective of the decisions for tokens leading up to it.
2147  // Store this information to prevent this from causing exponential runtime.
2148  llvm::SmallPtrSet<FormatToken *, 16> NonTemplateLess;
2149 };
2150 
2151 static const int PrecedenceUnaryOperator = prec::PointerToMember + 1;
2152 static const int PrecedenceArrowAndPeriod = prec::PointerToMember + 2;
2153 
2154 /// Parses binary expressions by inserting fake parenthesis based on
2155 /// operator precedence.
2156 class ExpressionParser {
2157 public:
2158  ExpressionParser(const FormatStyle &Style, const AdditionalKeywords &Keywords,
2159  AnnotatedLine &Line)
2160  : Style(Style), Keywords(Keywords), Current(Line.First) {}
2161 
2162  /// Parse expressions with the given operator precedence.
2163  void parse(int Precedence = 0) {
2164  // Skip 'return' and ObjC selector colons as they are not part of a binary
2165  // expression.
2166  while (Current && (Current->is(tok::kw_return) ||
2167  (Current->is(tok::colon) &&
2168  Current->isOneOf(TT_ObjCMethodExpr, TT_DictLiteral))))
2169  next();
2170 
2171  if (!Current || Precedence > PrecedenceArrowAndPeriod)
2172  return;
2173 
2174  // Conditional expressions need to be parsed separately for proper nesting.
2175  if (Precedence == prec::Conditional) {
2176  parseConditionalExpr();
2177  return;
2178  }
2179 
2180  // Parse unary operators, which all have a higher precedence than binary
2181  // operators.
2182  if (Precedence == PrecedenceUnaryOperator) {
2183  parseUnaryOperator();
2184  return;
2185  }
2186 
2187  FormatToken *Start = Current;
2188  FormatToken *LatestOperator = nullptr;
2189  unsigned OperatorIndex = 0;
2190 
2191  while (Current) {
2192  // Consume operators with higher precedence.
2193  parse(Precedence + 1);
2194 
2195  int CurrentPrecedence = getCurrentPrecedence();
2196 
2197  if (Precedence == CurrentPrecedence && Current &&
2198  Current->is(TT_SelectorName)) {
2199  if (LatestOperator)
2200  addFakeParenthesis(Start, prec::Level(Precedence));
2201  Start = Current;
2202  }
2203 
2204  // At the end of the line or when an operator with higher precedence is
2205  // found, insert fake parenthesis and return.
2206  if (!Current ||
2207  (Current->closesScope() &&
2208  (Current->MatchingParen || Current->is(TT_TemplateString))) ||
2209  (CurrentPrecedence != -1 && CurrentPrecedence < Precedence) ||
2210  (CurrentPrecedence == prec::Conditional &&
2211  Precedence == prec::Assignment && Current->is(tok::colon))) {
2212  break;
2213  }
2214 
2215  // Consume scopes: (), [], <> and {}
2216  if (Current->opensScope()) {
2217  // In fragment of a JavaScript template string can look like '}..${' and
2218  // thus close a scope and open a new one at the same time.
2219  while (Current && (!Current->closesScope() || Current->opensScope())) {
2220  next();
2221  parse();
2222  }
2223  next();
2224  } else {
2225  // Operator found.
2226  if (CurrentPrecedence == Precedence) {
2227  if (LatestOperator)
2228  LatestOperator->NextOperator = Current;
2229  LatestOperator = Current;
2230  Current->OperatorIndex = OperatorIndex;
2231  ++OperatorIndex;
2232  }
2233  next(/*SkipPastLeadingComments=*/Precedence > 0);
2234  }
2235  }
2236 
2237  if (LatestOperator && (Current || Precedence > 0)) {
2238  // LatestOperator->LastOperator = true;
2239  if (Precedence == PrecedenceArrowAndPeriod) {
2240  // Call expressions don't have a binary operator precedence.
2241  addFakeParenthesis(Start, prec::Unknown);
2242  } else {
2243  addFakeParenthesis(Start, prec::Level(Precedence));
2244  }
2245  }
2246  }
2247 
2248 private:
2249  /// Gets the precedence (+1) of the given token for binary operators
2250  /// and other tokens that we treat like binary operators.
2251  int getCurrentPrecedence() {
2252  if (Current) {
2253  const FormatToken *NextNonComment = Current->getNextNonComment();
2254  if (Current->is(TT_ConditionalExpr))
2255  return prec::Conditional;
2256  if (NextNonComment && Current->is(TT_SelectorName) &&
2257  (NextNonComment->isOneOf(TT_DictLiteral, TT_JsTypeColon) ||
2258  ((Style.Language == FormatStyle::LK_Proto ||
2259  Style.Language == FormatStyle::LK_TextProto) &&
2260  NextNonComment->is(tok::less))))
2261  return prec::Assignment;
2262  if (Current->is(TT_JsComputedPropertyName))
2263  return prec::Assignment;
2264  if (Current->is(TT_LambdaArrow))
2265  return prec::Comma;
2266  if (Current->is(TT_FatArrow))
2267  return prec::Assignment;
2268  if (Current->isOneOf(tok::semi, TT_InlineASMColon, TT_SelectorName) ||
2269  (Current->is(tok::comment) && NextNonComment &&
2270  NextNonComment->is(TT_SelectorName)))
2271  return 0;
2272  if (Current->is(TT_RangeBasedForLoopColon))
2273  return prec::Comma;
2274  if ((Style.Language == FormatStyle::LK_Java || Style.isJavaScript()) &&
2275  Current->is(Keywords.kw_instanceof))
2276  return prec::Relational;
2277  if (Style.isJavaScript() &&
2278  Current->isOneOf(Keywords.kw_in, Keywords.kw_as))
2279  return prec::Relational;
2280  if (Current->is(TT_BinaryOperator) || Current->is(tok::comma))
2281  return Current->getPrecedence();
2282  if (Current->isOneOf(tok::period, tok::arrow))
2283  return PrecedenceArrowAndPeriod;
2284  if ((Style.Language == FormatStyle::LK_Java || Style.isJavaScript()) &&
2285  Current->isOneOf(Keywords.kw_extends, Keywords.kw_implements,
2286  Keywords.kw_throws))
2287  return 0;
2288  }
2289  return -1;
2290  }
2291 
2292  void addFakeParenthesis(FormatToken *Start, prec::Level Precedence) {
2293  Start->FakeLParens.push_back(Precedence);
2294  if (Precedence > prec::Unknown)
2295  Start->StartsBinaryExpression = true;
2296  if (Current) {
2297  FormatToken *Previous = Current->Previous;
2298  while (Previous->is(tok::comment) && Previous->Previous)
2299  Previous = Previous->Previous;
2300  ++Previous->FakeRParens;
2301  if (Precedence > prec::Unknown)
2302  Previous->EndsBinaryExpression = true;
2303  }
2304  }
2305 
2306  /// Parse unary operator expressions and surround them with fake
2307  /// parentheses if appropriate.
2308  void parseUnaryOperator() {
2310  while (Current && Current->is(TT_UnaryOperator)) {
2311  Tokens.push_back(Current);
2312  next();
2313  }
2314  parse(PrecedenceArrowAndPeriod);
2315  for (FormatToken *Token : llvm::reverse(Tokens))
2316  // The actual precedence doesn't matter.
2317  addFakeParenthesis(Token, prec::Unknown);
2318  }
2319 
2320  void parseConditionalExpr() {
2321  while (Current && Current->isTrailingComment()) {
2322  next();
2323  }
2324  FormatToken *Start = Current;
2325  parse(prec::LogicalOr);
2326  if (!Current || !Current->is(tok::question))
2327  return;
2328  next();
2329  parse(prec::Assignment);
2330  if (!Current || Current->isNot(TT_ConditionalExpr))
2331  return;
2332  next();
2333  parse(prec::Assignment);
2334  addFakeParenthesis(Start, prec::Conditional);
2335  }
2336 
2337  void next(bool SkipPastLeadingComments = true) {
2338  if (Current)
2339  Current = Current->Next;
2340  while (Current &&
2341  (Current->NewlinesBefore == 0 || SkipPastLeadingComments) &&
2342  Current->isTrailingComment())
2343  Current = Current->Next;
2344  }
2345 
2346  const FormatStyle &Style;
2347  const AdditionalKeywords &Keywords;
2348  FormatToken *Current;
2349 };
2350 
2351 } // end anonymous namespace
2352 
2355  const AnnotatedLine *NextNonCommentLine = nullptr;
2356  for (AnnotatedLine *Line : llvm::reverse(Lines)) {
2357  assert(Line->First);
2358  bool CommentLine = true;
2359  for (const FormatToken *Tok = Line->First; Tok; Tok = Tok->Next) {
2360  if (!Tok->is(tok::comment)) {
2361  CommentLine = false;
2362  break;
2363  }
2364  }
2365 
2366  // If the comment is currently aligned with the line immediately following
2367  // it, that's probably intentional and we should keep it.
2368  if (NextNonCommentLine && CommentLine &&
2369  NextNonCommentLine->First->NewlinesBefore <= 1 &&
2370  NextNonCommentLine->First->OriginalColumn ==
2371  Line->First->OriginalColumn) {
2372  // Align comments for preprocessor lines with the # in column 0 if
2373  // preprocessor lines are not indented. Otherwise, align with the next
2374  // line.
2375  Line->Level =
2376  (Style.IndentPPDirectives != FormatStyle::PPDIS_BeforeHash &&
2377  (NextNonCommentLine->Type == LT_PreprocessorDirective ||
2378  NextNonCommentLine->Type == LT_ImportStatement))
2379  ? 0
2380  : NextNonCommentLine->Level;
2381  } else {
2382  NextNonCommentLine = Line->First->isNot(tok::r_brace) ? Line : nullptr;
2383  }
2384 
2385  setCommentLineLevels(Line->Children);
2386  }
2387 }
2388 
2389 static unsigned maxNestingDepth(const AnnotatedLine &Line) {
2390  unsigned Result = 0;
2391  for (const auto *Tok = Line.First; Tok != nullptr; Tok = Tok->Next)
2392  Result = std::max(Result, Tok->NestingLevel);
2393  return Result;
2394 }
2395 
2397  for (auto &Child : Line.Children)
2398  annotate(*Child);
2399 
2400  AnnotatingParser Parser(Style, Line, Keywords);
2401  Line.Type = Parser.parseLine();
2402 
2403  // With very deep nesting, ExpressionParser uses lots of stack and the
2404  // formatting algorithm is very slow. We're not going to do a good job here
2405  // anyway - it's probably generated code being formatted by mistake.
2406  // Just skip the whole line.
2407  if (maxNestingDepth(Line) > 50)
2408  Line.Type = LT_Invalid;
2409 
2410  if (Line.Type == LT_Invalid)
2411  return;
2412 
2413  ExpressionParser ExprParser(Style, Keywords, Line);
2414  ExprParser.parse();
2415 
2416  if (Line.startsWith(TT_ObjCMethodSpecifier))
2417  Line.Type = LT_ObjCMethodDecl;
2418  else if (Line.startsWith(TT_ObjCDecl))
2419  Line.Type = LT_ObjCDecl;
2420  else if (Line.startsWith(TT_ObjCProperty))
2421  Line.Type = LT_ObjCProperty;
2422 
2423  Line.First->SpacesRequiredBefore = 1;
2424  Line.First->CanBreakBefore = Line.First->MustBreakBefore;
2425 }
2426 
2427 // This function heuristically determines whether 'Current' starts the name of a
2428 // function declaration.
2429 static bool isFunctionDeclarationName(bool IsCpp, const FormatToken &Current,
2430  const AnnotatedLine &Line) {
2431  auto skipOperatorName = [](const FormatToken *Next) -> const FormatToken * {
2432  for (; Next; Next = Next->Next) {
2433  if (Next->is(TT_OverloadedOperatorLParen))
2434  return Next;
2435  if (Next->is(TT_OverloadedOperator))
2436  continue;
2437  if (Next->isOneOf(tok::kw_new, tok::kw_delete)) {
2438  // For 'new[]' and 'delete[]'.
2439  if (Next->Next &&
2440  Next->Next->startsSequence(tok::l_square, tok::r_square))
2441  Next = Next->Next->Next;
2442  continue;
2443  }
2444  if (Next->startsSequence(tok::l_square, tok::r_square)) {
2445  // For operator[]().
2446  Next = Next->Next;
2447  continue;
2448  }
2449  if ((Next->isSimpleTypeSpecifier() || Next->is(tok::identifier)) &&
2450  Next->Next && Next->Next->isOneOf(tok::star, tok::amp, tok::ampamp)) {
2451  // For operator void*(), operator char*(), operator Foo*().
2452  Next = Next->Next;
2453  continue;
2454  }
2455  if (Next->is(TT_TemplateOpener) && Next->MatchingParen) {
2456  Next = Next->MatchingParen;
2457  continue;
2458  }
2459 
2460  break;
2461  }
2462  return nullptr;
2463  };
2464 
2465  // Find parentheses of parameter list.
2466  const FormatToken *Next = Current.Next;
2467  if (Current.is(tok::kw_operator)) {
2468  if (Current.Previous && Current.Previous->is(tok::coloncolon))
2469  return false;
2470  Next = skipOperatorName(Next);
2471  } else {
2472  if (!Current.is(TT_StartOfName) || Current.NestingLevel != 0)
2473  return false;
2474  for (; Next; Next = Next->Next) {
2475  if (Next->is(TT_TemplateOpener)) {
2476  Next = Next->MatchingParen;
2477  } else if (Next->is(tok::coloncolon)) {
2478  Next = Next->Next;
2479  if (!Next)
2480  return false;
2481  if (Next->is(tok::kw_operator)) {
2482  Next = skipOperatorName(Next->Next);
2483  break;
2484  }
2485  if (!Next->is(tok::identifier))
2486  return false;
2487  } else if (Next->is(tok::l_paren)) {
2488  break;
2489  } else {
2490  return false;
2491  }
2492  }
2493  }
2494 
2495  // Check whether parameter list can belong to a function declaration.
2496  if (!Next || !Next->is(tok::l_paren) || !Next->MatchingParen)
2497  return false;
2498  // If the lines ends with "{", this is likely a function definition.
2499  if (Line.Last->is(tok::l_brace))
2500  return true;
2501  if (Next->Next == Next->MatchingParen)
2502  return true; // Empty parentheses.
2503  // If there is an &/&& after the r_paren, this is likely a function.
2504  if (Next->MatchingParen->Next &&
2505  Next->MatchingParen->Next->is(TT_PointerOrReference))
2506  return true;
2507 
2508  // Check for K&R C function definitions (and C++ function definitions with
2509  // unnamed parameters), e.g.:
2510  // int f(i)
2511  // {
2512  // return i + 1;
2513  // }
2514  // bool g(size_t = 0, bool b = false)
2515  // {
2516  // return !b;
2517  // }
2518  if (IsCpp && Next->Next && Next->Next->is(tok::identifier) &&
2519  !Line.endsWith(tok::semi))
2520  return true;
2521 
2522  for (const FormatToken *Tok = Next->Next; Tok && Tok != Next->MatchingParen;
2523  Tok = Tok->Next) {
2524  if (Tok->is(TT_TypeDeclarationParen))
2525  return true;
2526  if (Tok->isOneOf(tok::l_paren, TT_TemplateOpener) && Tok->MatchingParen) {
2527  Tok = Tok->MatchingParen;
2528  continue;
2529  }
2530  if (Tok->is(tok::kw_const) || Tok->isSimpleTypeSpecifier() ||
2531  Tok->isOneOf(TT_PointerOrReference, TT_StartOfName, tok::ellipsis))
2532  return true;
2533  if (Tok->isOneOf(tok::l_brace, tok::string_literal, TT_ObjCMethodExpr) ||
2534  Tok->Tok.isLiteral())
2535  return false;
2536  }
2537  return false;
2538 }
2539 
2540 bool TokenAnnotator::mustBreakForReturnType(const AnnotatedLine &Line) const {
2541  assert(Line.MightBeFunctionDecl);
2542 
2543  if ((Style.AlwaysBreakAfterReturnType == FormatStyle::RTBS_TopLevel ||
2544  Style.AlwaysBreakAfterReturnType ==
2546  Line.Level > 0)
2547  return false;
2548 
2549  switch (Style.AlwaysBreakAfterReturnType) {
2551  return false;
2552  case FormatStyle::RTBS_All:
2554  return true;
2557  return Line.mightBeFunctionDefinition();
2558  }
2559 
2560  return false;
2561 }
2562 
2564  for (AnnotatedLine *ChildLine : Line.Children)
2565  calculateFormattingInformation(*ChildLine);
2566 
2567  Line.First->TotalLength =
2568  Line.First->IsMultiline ? Style.ColumnLimit
2569  : Line.FirstStartColumn + Line.First->ColumnWidth;
2570  FormatToken *Current = Line.First->Next;
2571  bool InFunctionDecl = Line.MightBeFunctionDecl;
2572  bool AlignArrayOfStructures =
2573  (Style.AlignArrayOfStructures != FormatStyle::AIAS_None &&
2575  if (AlignArrayOfStructures)
2576  calculateArrayInitializerColumnList(Line);
2577 
2578  while (Current) {
2579  if (isFunctionDeclarationName(Style.isCpp(), *Current, Line))
2580  Current->setType(TT_FunctionDeclarationName);
2581  const FormatToken *Prev = Current->Previous;
2582  if (Current->is(TT_LineComment)) {
2583  if (Prev->is(BK_BracedInit) && Prev->opensScope())
2584  Current->SpacesRequiredBefore =
2585  (Style.Cpp11BracedListStyle && !Style.SpacesInParentheses) ? 0 : 1;
2586  else
2587  Current->SpacesRequiredBefore = Style.SpacesBeforeTrailingComments;
2588 
2589  // If we find a trailing comment, iterate backwards to determine whether
2590  // it seems to relate to a specific parameter. If so, break before that
2591  // parameter to avoid changing the comment's meaning. E.g. don't move 'b'
2592  // to the previous line in:
2593  // SomeFunction(a,
2594  // b, // comment
2595  // c);
2596  if (!Current->HasUnescapedNewline) {
2597  for (FormatToken *Parameter = Current->Previous; Parameter;
2598  Parameter = Parameter->Previous) {
2599  if (Parameter->isOneOf(tok::comment, tok::r_brace))
2600  break;
2601  if (Parameter->Previous && Parameter->Previous->is(tok::comma)) {
2602  if (!Parameter->Previous->is(TT_CtorInitializerComma) &&
2603  Parameter->HasUnescapedNewline)
2604  Parameter->MustBreakBefore = true;
2605  break;
2606  }
2607  }
2608  }
2609  } else if (Current->SpacesRequiredBefore == 0 &&
2610  spaceRequiredBefore(Line, *Current)) {
2611  Current->SpacesRequiredBefore = 1;
2612  }
2613 
2614  Current->MustBreakBefore =
2615  Current->MustBreakBefore || mustBreakBefore(Line, *Current);
2616 
2617  if (!Current->MustBreakBefore && InFunctionDecl &&
2618  Current->is(TT_FunctionDeclarationName))
2619  Current->MustBreakBefore = mustBreakForReturnType(Line);
2620 
2621  Current->CanBreakBefore =
2622  Current->MustBreakBefore || canBreakBefore(Line, *Current);
2623  unsigned ChildSize = 0;
2624  if (Prev->Children.size() == 1) {
2625  FormatToken &LastOfChild = *Prev->Children[0]->Last;
2626  ChildSize = LastOfChild.isTrailingComment() ? Style.ColumnLimit
2627  : LastOfChild.TotalLength + 1;
2628  }
2629  if (Current->MustBreakBefore || Prev->Children.size() > 1 ||
2630  (Prev->Children.size() == 1 &&
2631  Prev->Children[0]->First->MustBreakBefore) ||
2632  Current->IsMultiline)
2633  Current->TotalLength = Prev->TotalLength + Style.ColumnLimit;
2634  else
2635  Current->TotalLength = Prev->TotalLength + Current->ColumnWidth +
2636  ChildSize + Current->SpacesRequiredBefore;
2637 
2638  if (Current->is(TT_CtorInitializerColon))
2639  InFunctionDecl = false;
2640 
2641  // FIXME: Only calculate this if CanBreakBefore is true once static
2642  // initializers etc. are sorted out.
2643  // FIXME: Move magic numbers to a better place.
2644 
2645  // Reduce penalty for aligning ObjC method arguments using the colon
2646  // alignment as this is the canonical way (still prefer fitting everything
2647  // into one line if possible). Trying to fit a whole expression into one
2648  // line should not force other line breaks (e.g. when ObjC method
2649  // expression is a part of other expression).
2650  Current->SplitPenalty = splitPenalty(Line, *Current, InFunctionDecl);
2651  if (Style.Language == FormatStyle::LK_ObjC &&
2652  Current->is(TT_SelectorName) && Current->ParameterIndex > 0) {
2653  if (Current->ParameterIndex == 1)
2654  Current->SplitPenalty += 5 * Current->BindingStrength;
2655  } else {
2656  Current->SplitPenalty += 20 * Current->BindingStrength;
2657  }
2658 
2659  Current = Current->Next;
2660  }
2661 
2662  calculateUnbreakableTailLengths(Line);
2663  unsigned IndentLevel = Line.Level;
2664  for (Current = Line.First; Current != nullptr; Current = Current->Next) {
2665  if (Current->Role)
2666  Current->Role->precomputeFormattingInfos(Current);
2667  if (Current->MatchingParen &&
2668  Current->MatchingParen->opensBlockOrBlockTypeList(Style) &&
2669  IndentLevel > 0) {
2670  --IndentLevel;
2671  }
2672  Current->IndentLevel = IndentLevel;
2673  if (Current->opensBlockOrBlockTypeList(Style))
2674  ++IndentLevel;
2675  }
2676 
2677  LLVM_DEBUG({ printDebugInfo(Line); });
2678 }
2679 
2680 void TokenAnnotator::calculateUnbreakableTailLengths(AnnotatedLine &Line) {
2681  unsigned UnbreakableTailLength = 0;
2682  FormatToken *Current = Line.Last;
2683  while (Current) {
2684  Current->UnbreakableTailLength = UnbreakableTailLength;
2685  if (Current->CanBreakBefore ||
2686  Current->isOneOf(tok::comment, tok::string_literal)) {
2687  UnbreakableTailLength = 0;
2688  } else {
2689  UnbreakableTailLength +=
2690  Current->ColumnWidth + Current->SpacesRequiredBefore;
2691  }
2692  Current = Current->Previous;
2693  }
2694 }
2695 
2696 void TokenAnnotator::calculateArrayInitializerColumnList(AnnotatedLine &Line) {
2697  if (Line.First == Line.Last) {
2698  return;
2699  }
2700  auto *CurrentToken = Line.First;
2701  CurrentToken->ArrayInitializerLineStart = true;
2702  unsigned Depth = 0;
2703  while (CurrentToken != nullptr && CurrentToken != Line.Last) {
2704  if (CurrentToken->is(tok::l_brace)) {
2705  CurrentToken->IsArrayInitializer = true;
2706  if (CurrentToken->Next != nullptr)
2707  CurrentToken->Next->MustBreakBefore = true;
2708  CurrentToken =
2709  calculateInitializerColumnList(Line, CurrentToken->Next, Depth + 1);
2710  } else {
2711  CurrentToken = CurrentToken->Next;
2712  }
2713  }
2714 }
2715 
2716 FormatToken *TokenAnnotator::calculateInitializerColumnList(
2717  AnnotatedLine &Line, FormatToken *CurrentToken, unsigned Depth) {
2718  while (CurrentToken != nullptr && CurrentToken != Line.Last) {
2719  if (CurrentToken->is(tok::l_brace))
2720  ++Depth;
2721  else if (CurrentToken->is(tok::r_brace))
2722  --Depth;
2723  if (Depth == 2 && CurrentToken->isOneOf(tok::l_brace, tok::comma)) {
2724  CurrentToken = CurrentToken->Next;
2725  if (CurrentToken == nullptr)
2726  break;
2727  CurrentToken->StartsColumn = true;
2728  CurrentToken = CurrentToken->Previous;
2729  }
2730  CurrentToken = CurrentToken->Next;
2731  }
2732  return CurrentToken;
2733 }
2734 
2735 unsigned TokenAnnotator::splitPenalty(const AnnotatedLine &Line,
2736  const FormatToken &Tok,
2737  bool InFunctionDecl) {
2738  const FormatToken &Left = *Tok.Previous;
2739  const FormatToken &Right = Tok;
2740 
2741  if (Left.is(tok::semi))
2742  return 0;
2743 
2744  if (Style.Language == FormatStyle::LK_Java) {
2745  if (Right.isOneOf(Keywords.kw_extends, Keywords.kw_throws))
2746  return 1;
2747  if (Right.is(Keywords.kw_implements))
2748  return 2;
2749  if (Left.is(tok::comma) && Left.NestingLevel == 0)
2750  return 3;
2751  } else if (Style.isJavaScript()) {
2752  if (Right.is(Keywords.kw_function) && Left.isNot(tok::comma))
2753  return 100;
2754  if (Left.is(TT_JsTypeColon))
2755  return 35;
2756  if ((Left.is(TT_TemplateString) && Left.TokenText.endswith("${")) ||
2757  (Right.is(TT_TemplateString) && Right.TokenText.startswith("}")))
2758  return 100;
2759  // Prefer breaking call chains (".foo") over empty "{}", "[]" or "()".
2760  if (Left.opensScope() && Right.closesScope())
2761  return 200;
2762  }
2763 
2764  if (Right.is(tok::identifier) && Right.Next && Right.Next->is(TT_DictLiteral))
2765  return 1;
2766  if (Right.is(tok::l_square)) {
2767  if (Style.Language == FormatStyle::LK_Proto)
2768  return 1;
2769  if (Left.is(tok::r_square))
2770  return 200;
2771  // Slightly prefer formatting local lambda definitions like functions.
2772  if (Right.is(TT_LambdaLSquare) && Left.is(tok::equal))
2773  return 35;
2774  if (!Right.isOneOf(TT_ObjCMethodExpr, TT_LambdaLSquare,
2775  TT_ArrayInitializerLSquare,
2776  TT_DesignatedInitializerLSquare, TT_AttributeSquare))
2777  return 500;
2778  }
2779 
2780  if (Left.is(tok::coloncolon) ||
2781  (Right.is(tok::period) && Style.Language == FormatStyle::LK_Proto))
2782  return 500;
2783  if (Right.isOneOf(TT_StartOfName, TT_FunctionDeclarationName) ||
2784  Right.is(tok::kw_operator)) {
2785  if (Line.startsWith(tok::kw_for) && Right.PartOfMultiVariableDeclStmt)
2786  return 3;
2787  if (Left.is(TT_StartOfName))
2788  return 110;
2789  if (InFunctionDecl && Right.NestingLevel == 0)
2790  return Style.PenaltyReturnTypeOnItsOwnLine;
2791  return 200;
2792  }
2793  if (Right.is(TT_PointerOrReference))
2794  return 190;
2795  if (Right.is(TT_LambdaArrow))
2796  return 110;
2797  if (Left.is(tok::equal) && Right.is(tok::l_brace))
2798  return 160;
2799  if (Left.is(TT_CastRParen))
2800  return 100;
2801  if (Left.isOneOf(tok::kw_class, tok::kw_struct))
2802  return 5000;
2803  if (Left.is(tok::comment))
2804  return 1000;
2805 
2806  if (Left.isOneOf(TT_RangeBasedForLoopColon, TT_InheritanceColon,
2807  TT_CtorInitializerColon))
2808  return 2;
2809 
2810  if (Right.isMemberAccess()) {
2811  // Breaking before the "./->" of a chained call/member access is reasonably
2812  // cheap, as formatting those with one call per line is generally
2813  // desirable. In particular, it should be cheaper to break before the call
2814  // than it is to break inside a call's parameters, which could lead to weird
2815  // "hanging" indents. The exception is the very last "./->" to support this
2816  // frequent pattern:
2817  //
2818  // aaaaaaaa.aaaaaaaa.bbbbbbb().ccccccccccccccccccccc(
2819  // dddddddd);
2820  //
2821  // which might otherwise be blown up onto many lines. Here, clang-format
2822  // won't produce "hanging" indents anyway as there is no other trailing
2823  // call.
2824  //
2825  // Also apply higher penalty is not a call as that might lead to a wrapping
2826  // like:
2827  //
2828  // aaaaaaa
2829  // .aaaaaaaaa.bbbbbbbb(cccccccc);
2830  return !Right.NextOperator || !Right.NextOperator->Previous->closesScope()
2831  ? 150
2832  : 35;
2833  }
2834 
2835  if (Right.is(TT_TrailingAnnotation) &&
2836  (!Right.Next || Right.Next->isNot(tok::l_paren))) {
2837  // Moving trailing annotations to the next line is fine for ObjC method
2838  // declarations.
2839  if (Line.startsWith(TT_ObjCMethodSpecifier))
2840  return 10;
2841  // Generally, breaking before a trailing annotation is bad unless it is
2842  // function-like. It seems to be especially preferable to keep standard
2843  // annotations (i.e. "const", "final" and "override") on the same line.
2844  // Use a slightly higher penalty after ")" so that annotations like
2845  // "const override" are kept together.
2846  bool is_short_annotation = Right.TokenText.size() < 10;
2847  return (Left.is(tok::r_paren) ? 100 : 120) + (is_short_annotation ? 50 : 0);
2848  }
2849 
2850  // In for-loops, prefer breaking at ',' and ';'.
2851  if (Line.startsWith(tok::kw_for) && Left.is(tok::equal))
2852  return 4;
2853 
2854  // In Objective-C method expressions, prefer breaking before "param:" over
2855  // breaking after it.
2856  if (Right.is(TT_SelectorName))
2857  return 0;
2858  if (Left.is(tok::colon) && Left.is(TT_ObjCMethodExpr))
2859  return Line.MightBeFunctionDecl ? 50 : 500;
2860 
2861  // In Objective-C type declarations, avoid breaking after the category's
2862  // open paren (we'll prefer breaking after the protocol list's opening
2863  // angle bracket, if present).
2864  if (Line.Type == LT_ObjCDecl && Left.is(tok::l_paren) && Left.Previous &&
2865  Left.Previous->isOneOf(tok::identifier, tok::greater))
2866  return 500;
2867 
2868  if (Left.is(tok::l_paren) && Style.PenaltyBreakOpenParenthesis != 0)
2869  return Style.PenaltyBreakOpenParenthesis;
2870  if (Left.is(tok::l_paren) && InFunctionDecl &&
2871  Style.AlignAfterOpenBracket != FormatStyle::BAS_DontAlign)
2872  return 100;
2873  if (Left.is(tok::l_paren) && Left.Previous &&
2874  (Left.Previous->is(tok::kw_for) || Left.Previous->isIf()))
2875  return 1000;
2876  if (Left.is(tok::equal) && InFunctionDecl)
2877  return 110;
2878  if (Right.is(tok::r_brace))
2879  return 1;
2880  if (Left.is(TT_TemplateOpener))
2881  return 100;
2882  if (Left.opensScope()) {
2883  // If we aren't aligning after opening parens/braces we can always break
2884  // here unless the style does not want us to place all arguments on the
2885  // next line.
2886  if (Style.AlignAfterOpenBracket == FormatStyle::BAS_DontAlign &&
2887  (Left.ParameterCount <= 1 || Style.AllowAllArgumentsOnNextLine))
2888  return 0;
2889  if (Left.is(tok::l_brace) && !Style.Cpp11BracedListStyle)
2890  return 19;
2891  return Left.ParameterCount > 1 ? Style.PenaltyBreakBeforeFirstCallParameter
2892  : 19;
2893  }
2894  if (Left.is(TT_JavaAnnotation))
2895  return 50;
2896 
2897  if (Left.is(TT_UnaryOperator))
2898  return 60;
2899  if (Left.isOneOf(tok::plus, tok::comma) && Left.Previous &&
2900  Left.Previous->isLabelString() &&
2901  (Left.NextOperator || Left.OperatorIndex != 0))
2902  return 50;
2903  if (Right.is(tok::plus) && Left.isLabelString() &&
2904  (Right.NextOperator || Right.OperatorIndex != 0))
2905  return 25;
2906  if (Left.is(tok::comma))
2907  return 1;
2908  if (Right.is(tok::lessless) && Left.isLabelString() &&
2909  (Right.NextOperator || Right.OperatorIndex != 1))
2910  return 25;
2911  if (Right.is(tok::lessless)) {
2912  // Breaking at a << is really cheap.
2913  if (!Left.is(tok::r_paren) || Right.OperatorIndex > 0)
2914  // Slightly prefer to break before the first one in log-like statements.
2915  return 2;
2916  return 1;
2917  }
2918  if (Left.ClosesTemplateDeclaration)
2919  return Style.PenaltyBreakTemplateDeclaration;
2920  if (Left.is(TT_ConditionalExpr))
2921  return prec::Conditional;
2922  prec::Level Level = Left.getPrecedence();
2923  if (Level == prec::Unknown)
2924  Level = Right.getPrecedence();
2925  if (Level == prec::Assignment)
2926  return Style.PenaltyBreakAssignment;
2927  if (Level != prec::Unknown)
2928  return Level;
2929 
2930  return 3;
2931 }
2932 
2933 bool TokenAnnotator::spaceRequiredBeforeParens(const FormatToken &Right) const {
2934  if (Style.SpaceBeforeParens == FormatStyle::SBPO_Always)
2935  return true;
2936  if (Right.is(TT_OverloadedOperatorLParen) &&
2937  Style.SpaceBeforeParensOptions.AfterOverloadedOperator)
2938  return true;
2939  if (Style.SpaceBeforeParensOptions.BeforeNonEmptyParentheses &&
2940  Right.ParameterCount > 0)
2941  return true;
2942  return false;
2943 }
2944 
2945 bool TokenAnnotator::spaceRequiredBetween(const AnnotatedLine &Line,
2946  const FormatToken &Left,
2947  const FormatToken &Right) {
2948  if (Left.is(tok::kw_return) && Right.isNot(tok::semi))
2949  return true;
2950  if (Style.isJson() && Left.is(tok::string_literal) && Right.is(tok::colon))
2951  return false;
2952  if (Left.is(Keywords.kw_assert) && Style.Language == FormatStyle::LK_Java)
2953  return true;
2954  if (Style.ObjCSpaceAfterProperty && Line.Type == LT_ObjCProperty &&
2955  Left.Tok.getObjCKeywordID() == tok::objc_property)
2956  return true;
2957  if (Right.is(tok::hashhash))
2958  return Left.is(tok::hash);
2959  if (Left.isOneOf(tok::hashhash, tok::hash))
2960  return Right.is(tok::hash);
2961  if ((Left.is(tok::l_paren) && Right.is(tok::r_paren)) ||
2962  (Left.is(tok::l_brace) && Left.isNot(BK_Block) &&
2963  Right.is(tok::r_brace) && Right.isNot(BK_Block)))
2964  return Style.SpaceInEmptyParentheses;
2965  if (Style.SpacesInConditionalStatement) {
2966  if (Left.is(tok::l_paren) && Left.Previous &&
2967  isKeywordWithCondition(*Left.Previous))
2968  return true;
2969  if (Right.is(tok::r_paren) && Right.MatchingParen &&
2970  Right.MatchingParen->Previous &&
2971  isKeywordWithCondition(*Right.MatchingParen->Previous))
2972  return true;
2973  }
2974 
2975  // auto{x} auto(x)
2976  if (Left.is(tok::kw_auto) && Right.isOneOf(tok::l_paren, tok::l_brace))
2977  return false;
2978 
2979  // operator co_await(x)
2980  if (Right.is(tok::l_paren) && Left.is(tok::kw_co_await) && Left.Previous &&
2981  Left.Previous->is(tok::kw_operator))
2982  return false;
2983  // co_await (x), co_yield (x), co_return (x)
2984  if (Left.isOneOf(tok::kw_co_await, tok::kw_co_yield, tok::kw_co_return) &&
2985  Right.isNot(tok::semi))
2986  return true;
2987  // requires clause Concept1<T> && Concept2<T>
2988  if (Left.is(TT_ConstraintJunctions) && Right.is(tok::identifier))
2989  return true;
2990 
2991  if (Left.is(tok::l_paren) || Right.is(tok::r_paren))
2992  return (Right.is(TT_CastRParen) ||
2993  (Left.MatchingParen && Left.MatchingParen->is(TT_CastRParen)))
2994  ? Style.SpacesInCStyleCastParentheses
2995  : Style.SpacesInParentheses;
2996  if (Right.isOneOf(tok::semi, tok::comma))
2997  return false;
2998  if (Right.is(tok::less) && Line.Type == LT_ObjCDecl) {
2999  bool IsLightweightGeneric = Right.MatchingParen &&
3000  Right.MatchingParen->Next &&
3001  Right.MatchingParen->Next->is(tok::colon);
3002  return !IsLightweightGeneric && Style.ObjCSpaceBeforeProtocolList;
3003  }
3004  if (Right.is(tok::less) && Left.is(tok::kw_template))
3005  return Style.SpaceAfterTemplateKeyword;
3006  if (Left.isOneOf(tok::exclaim, tok::tilde))
3007  return false;
3008  if (Left.is(tok::at) &&
3009  Right.isOneOf(tok::identifier, tok::string_literal, tok::char_constant,
3010  tok::numeric_constant, tok::l_paren, tok::l_brace,
3011  tok::kw_true, tok::kw_false))
3012  return false;
3013  if (Left.is(tok::colon))
3014  return !Left.is(TT_ObjCMethodExpr);
3015  if (Left.is(tok::coloncolon))
3016  return false;
3017  if (Left.is(tok::less) || Right.isOneOf(tok::greater, tok::less)) {
3018  if (Style.Language == FormatStyle::LK_TextProto ||
3019  (Style.Language == FormatStyle::LK_Proto &&
3020  (Left.is(TT_DictLiteral) || Right.is(TT_DictLiteral)))) {
3021  // Format empty list as `<>`.
3022  if (Left.is(tok::less) && Right.is(tok::greater))
3023  return false;
3024  return !Style.Cpp11BracedListStyle;
3025  }
3026  return false;
3027  }
3028  if (Right.is(tok::ellipsis))
3029  return Left.Tok.isLiteral() || (Left.is(tok::identifier) && Left.Previous &&
3030  Left.Previous->is(tok::kw_case));
3031  if (Left.is(tok::l_square) && Right.is(tok::amp))
3032  return Style.SpacesInSquareBrackets;
3033  if (Right.is(TT_PointerOrReference)) {
3034  if (Left.is(tok::r_paren) && Line.MightBeFunctionDecl) {
3035  if (!Left.MatchingParen)
3036  return true;
3037  FormatToken *TokenBeforeMatchingParen =
3038  Left.MatchingParen->getPreviousNonComment();
3039  if (!TokenBeforeMatchingParen || !Left.is(TT_TypeDeclarationParen))
3040  return true;
3041  }
3042  // Add a space if the previous token is a pointer qualifier or the closing
3043  // parenthesis of __attribute__(()) expression and the style requires spaces
3044  // after pointer qualifiers.
3045  if ((Style.SpaceAroundPointerQualifiers == FormatStyle::SAPQ_After ||
3046  Style.SpaceAroundPointerQualifiers == FormatStyle::SAPQ_Both) &&
3047  (Left.is(TT_AttributeParen) || Left.canBePointerOrReferenceQualifier()))
3048  return true;
3049  if (Left.Tok.isLiteral())
3050  return true;
3051  // for (auto a = 0, b = 0; const auto & c : {1, 2, 3})
3052  if (Left.isTypeOrIdentifier() && Right.Next && Right.Next->Next &&
3053  Right.Next->Next->is(TT_RangeBasedForLoopColon))
3054  return getTokenPointerOrReferenceAlignment(Right) !=
3056  return (
3057  (!Left.isOneOf(TT_PointerOrReference, tok::l_paren) &&
3058  (getTokenPointerOrReferenceAlignment(Right) != FormatStyle::PAS_Left ||
3059  (Line.IsMultiVariableDeclStmt &&
3060  (Left.NestingLevel == 0 ||
3061  (Left.NestingLevel == 1 && Line.First->is(tok::kw_for)))))));
3062  }
3063  if (Right.is(TT_FunctionTypeLParen) && Left.isNot(tok::l_paren) &&
3064  (!Left.is(TT_PointerOrReference) ||
3065  (getTokenPointerOrReferenceAlignment(Left) != FormatStyle::PAS_Right &&
3066  !Line.IsMultiVariableDeclStmt)))
3067  return true;
3068  if (Left.is(TT_PointerOrReference)) {
3069  // Add a space if the next token is a pointer qualifier and the style
3070  // requires spaces before pointer qualifiers.
3071  if ((Style.SpaceAroundPointerQualifiers == FormatStyle::SAPQ_Before ||
3072  Style.SpaceAroundPointerQualifiers == FormatStyle::SAPQ_Both) &&
3073  Right.canBePointerOrReferenceQualifier())
3074  return true;
3075  // & 1
3076  if (Right.Tok.isLiteral())
3077  return true;
3078  // & /* comment
3079  if (Right.is(TT_BlockComment))
3080  return true;
3081  // foo() -> const Bar * override/final
3082  if (Right.isOneOf(Keywords.kw_override, Keywords.kw_final) &&
3083  !Right.is(TT_StartOfName))
3084  return true;
3085  // & {
3086  if (Right.is(tok::l_brace) && Right.is(BK_Block))
3087  return true;
3088  // for (auto a = 0, b = 0; const auto& c : {1, 2, 3})
3089  if (Left.Previous && Left.Previous->isTypeOrIdentifier() && Right.Next &&
3090  Right.Next->is(TT_RangeBasedForLoopColon))
3091  return getTokenPointerOrReferenceAlignment(Left) !=
3093  return !Right.isOneOf(TT_PointerOrReference, TT_ArraySubscriptLSquare,
3094  tok::l_paren) &&
3095  (getTokenPointerOrReferenceAlignment(Left) !=
3097  !Line.IsMultiVariableDeclStmt) &&
3098  Left.Previous &&
3099  !Left.Previous->isOneOf(tok::l_paren, tok::coloncolon,
3100  tok::l_square);
3101  }
3102  // Ensure right pointer alignment with ellipsis e.g. int *...P
3103  if (Left.is(tok::ellipsis) && Left.Previous &&
3104  Left.Previous->isOneOf(tok::star, tok::amp, tok::ampamp))
3105  return Style.PointerAlignment != FormatStyle::PAS_Right;
3106 
3107  if (Right.is(tok::star) && Left.is(tok::l_paren))
3108  return false;
3109  if (Left.is(tok::star) && Right.isOneOf(tok::star, tok::amp, tok::ampamp))
3110  return false;
3111  if (Right.isOneOf(tok::star, tok::amp, tok::ampamp)) {
3112  const FormatToken *Previous = &Left;
3113  while (Previous && !Previous->is(tok::kw_operator)) {
3114  if (Previous->is(tok::identifier) || Previous->isSimpleTypeSpecifier()) {
3115  Previous = Previous->getPreviousNonComment();
3116  continue;
3117  }
3118  if (Previous->is(TT_TemplateCloser) && Previous->MatchingParen) {
3119  Previous = Previous->MatchingParen->getPreviousNonComment();
3120  continue;
3121  }
3122  if (Previous->is(tok::coloncolon)) {
3123  Previous = Previous->getPreviousNonComment();
3124  continue;
3125  }
3126  break;
3127  }
3128  // Space between the type and the * in:
3129  // operator void*()
3130  // operator char*()
3131  // operator void const*()
3132  // operator void volatile*()
3133  // operator /*comment*/ const char*()
3134  // operator volatile /*comment*/ char*()
3135  // operator Foo*()
3136  // operator C<T>*()
3137  // operator std::Foo*()
3138  // operator C<T>::D<U>*()
3139  // dependent on PointerAlignment style.
3140  if (Previous) {
3141  if (Previous->endsSequence(tok::kw_operator))
3142  return (Style.PointerAlignment != FormatStyle::PAS_Left);
3143  if (Previous->is(tok::kw_const) || Previous->is(tok::kw_volatile))
3144  return (Style.PointerAlignment != FormatStyle::PAS_Left) ||
3145  (Style.SpaceAroundPointerQualifiers ==
3147  (Style.SpaceAroundPointerQualifiers == FormatStyle::SAPQ_Both);
3148  }
3149  }
3150  const auto SpaceRequiredForArrayInitializerLSquare =
3151  [](const FormatToken &LSquareTok, const FormatStyle &Style) {
3152  return Style.SpacesInContainerLiterals ||
3153  ((Style.Language == FormatStyle::LK_Proto ||
3154  Style.Language == FormatStyle::LK_TextProto) &&
3155  !Style.Cpp11BracedListStyle &&
3156  LSquareTok.endsSequence(tok::l_square, tok::colon,
3157  TT_SelectorName));
3158  };
3159  if (Left.is(tok::l_square))
3160  return (Left.is(TT_ArrayInitializerLSquare) && Right.isNot(tok::r_square) &&
3161  SpaceRequiredForArrayInitializerLSquare(Left, Style)) ||
3162  (Left.isOneOf(TT_ArraySubscriptLSquare, TT_StructuredBindingLSquare,
3163  TT_LambdaLSquare) &&
3164  Style.SpacesInSquareBrackets && Right.isNot(tok::r_square));
3165  if (Right.is(tok::r_square))
3166  return Right.MatchingParen &&
3167  ((Right.MatchingParen->is(TT_ArrayInitializerLSquare) &&
3168  SpaceRequiredForArrayInitializerLSquare(*Right.MatchingParen,
3169  Style)) ||
3170  (Style.SpacesInSquareBrackets &&
3171  Right.MatchingParen->isOneOf(TT_ArraySubscriptLSquare,
3172  TT_StructuredBindingLSquare,
3173  TT_LambdaLSquare)) ||
3174  Right.MatchingParen->is(TT_AttributeParen));
3175  if (Right.is(tok::l_square) &&
3176  !Right.isOneOf(TT_ObjCMethodExpr, TT_LambdaLSquare,
3177  TT_DesignatedInitializerLSquare,
3178  TT_StructuredBindingLSquare, TT_AttributeSquare) &&
3179  !Left.isOneOf(tok::numeric_constant, TT_DictLiteral) &&
3180  !(!Left.is(tok::r_square) && Style.SpaceBeforeSquareBrackets &&
3181  Right.is(TT_ArraySubscriptLSquare)))
3182  return false;
3183  if (Left.is(tok::l_brace) && Right.is(tok::r_brace))
3184  return !Left.Children.empty(); // No spaces in "{}".
3185  if ((Left.is(tok::l_brace) && Left.isNot(BK_Block)) ||
3186  (Right.is(tok::r_brace) && Right.MatchingParen &&
3187  Right.MatchingParen->isNot(BK_Block)))
3188  return Style.Cpp11BracedListStyle ? Style.SpacesInParentheses : true;
3189  if (Left.is(TT_BlockComment))
3190  // No whitespace in x(/*foo=*/1), except for JavaScript.
3191  return Style.isJavaScript() || !Left.TokenText.endswith("=*/");
3192 
3193  // Space between template and attribute.
3194  // e.g. template <typename T> [[nodiscard]] ...
3195  if (Left.is(TT_TemplateCloser) && Right.is(TT_AttributeSquare))
3196  return true;
3197  // Space before parentheses common for all languages
3198  if (Right.is(tok::l_paren)) {
3199  if (Left.is(TT_TemplateCloser) && Right.isNot(TT_FunctionTypeLParen))
3200  return spaceRequiredBeforeParens(Right);
3201  if (Left.is(tok::kw_requires))
3202  return spaceRequiredBeforeParens(Right);
3203  if ((Left.is(tok::r_paren) && Left.is(TT_AttributeParen)) ||
3204  (Left.is(tok::r_square) && Left.is(TT_AttributeSquare)))
3205  return true;
3206  if (Left.is(TT_ForEachMacro))
3207  return (Style.SpaceBeforeParensOptions.AfterForeachMacros ||
3208  spaceRequiredBeforeParens(Right));
3209  if (Left.is(TT_IfMacro))
3210  return (Style.SpaceBeforeParensOptions.AfterIfMacros ||
3211  spaceRequiredBeforeParens(Right));
3212  if (Line.Type == LT_ObjCDecl)
3213  return true;
3214  if (Left.is(tok::semi))
3215  return true;
3216  if (Left.isOneOf(tok::pp_elif, tok::kw_for, tok::kw_while, tok::kw_switch,
3217  tok::kw_case, TT_ForEachMacro, TT_ObjCForIn))
3218  return Style.SpaceBeforeParensOptions.AfterControlStatements ||
3219  spaceRequiredBeforeParens(Right);
3220  if (Left.isIf(Line.Type != LT_PreprocessorDirective))
3221  return Style.SpaceBeforeParensOptions.AfterControlStatements ||
3222  spaceRequiredBeforeParens(Right);
3223 
3224  // TODO add Operator overloading specific Options to
3225  // SpaceBeforeParensOptions
3226  if (Right.is(TT_OverloadedOperatorLParen))
3227  return spaceRequiredBeforeParens(Right);
3228  // Function declaration or definition
3229  if (Line.MightBeFunctionDecl && (Left.is(TT_FunctionDeclarationName))) {
3230  if (Line.mightBeFunctionDefinition())
3231  return Style.SpaceBeforeParensOptions.AfterFunctionDefinitionName ||
3232  spaceRequiredBeforeParens(Right);
3233  else
3234  return Style.SpaceBeforeParensOptions.AfterFunctionDeclarationName ||
3235  spaceRequiredBeforeParens(Right);
3236  }
3237  // Lambda
3238  if (Line.Type != LT_PreprocessorDirective && Left.is(tok::r_square) &&
3239  Left.MatchingParen && Left.MatchingParen->is(TT_LambdaLSquare))
3240  return Style.SpaceBeforeParensOptions.AfterFunctionDefinitionName ||
3241  spaceRequiredBeforeParens(Right);
3242  if (!Left.Previous || Left.Previous->isNot(tok::period)) {
3243  if (Left.isOneOf(tok::kw_try, Keywords.kw___except, tok::kw_catch))
3244  return Style.SpaceBeforeParensOptions.AfterControlStatements ||
3245  spaceRequiredBeforeParens(Right);
3246  if (Left.isOneOf(tok::kw_new, tok::kw_delete))
3247  return Style.SpaceBeforeParens != FormatStyle::SBPO_Never ||
3248  spaceRequiredBeforeParens(Right);
3249  }
3250  if (Line.Type != LT_PreprocessorDirective &&
3251  (Left.is(tok::identifier) || Left.isFunctionLikeKeyword() ||
3252  Left.is(tok::r_paren) || Left.isSimpleTypeSpecifier()))
3253  return spaceRequiredBeforeParens(Right);
3254  return false;
3255  }
3256  if (Left.is(tok::at) && Right.Tok.getObjCKeywordID() != tok::objc_not_keyword)
3257  return false;
3258  if (Right.is(TT_UnaryOperator))
3259  return !Left.isOneOf(tok::l_paren, tok::l_square, tok::at) &&
3260  (Left.isNot(tok::colon) || Left.isNot(TT_ObjCMethodExpr));
3261  if ((Left.isOneOf(tok::identifier, tok::greater, tok::r_square,
3262  tok::r_paren) ||
3263  Left.isSimpleTypeSpecifier()) &&
3264  Right.is(tok::l_brace) && Right.getNextNonComment() &&
3265  Right.isNot(BK_Block))
3266  return false;
3267  if (Left.is(tok::period) || Right.is(tok::period))
3268  return false;
3269  // u#str, U#str, L#str, u8#str
3270  // uR#str, UR#str, LR#str, u8R#str
3271  if (Right.is(tok::hash) && Left.is(tok::identifier) &&
3272  (Left.TokenText == "L" || Left.TokenText == "u" ||
3273  Left.TokenText == "U" || Left.TokenText == "u8" ||
3274  Left.TokenText == "LR" || Left.TokenText == "uR" ||
3275  Left.TokenText == "UR" || Left.TokenText == "u8R"))
3276  return false;
3277  if (Left.is(TT_TemplateCloser) && Left.MatchingParen &&
3278  Left.MatchingParen->Previous &&
3279  (Left.MatchingParen->Previous->is(tok::period) ||
3280  Left.MatchingParen->Previous->is(tok::coloncolon)))
3281  // Java call to generic function with explicit type:
3282  // A.<B<C<...>>>DoSomething();
3283  // A::<B<C<...>>>DoSomething(); // With a Java 8 method reference.
3284  return false;
3285  if (Left.is(TT_TemplateCloser) && Right.is(tok::l_square))
3286  return false;
3287  if (Left.is(tok::l_brace) && Left.endsSequence(TT_DictLiteral, tok::at))
3288  // Objective-C dictionary literal -> no space after opening brace.
3289  return false;
3290  if (Right.is(tok::r_brace) && Right.MatchingParen &&
3291  Right.MatchingParen->endsSequence(TT_DictLiteral, tok::at))
3292  // Objective-C dictionary literal -> no space before closing brace.
3293  return false;
3294  if (Right.getType() == TT_TrailingAnnotation &&
3295  Right.isOneOf(tok::amp, tok::ampamp) &&
3296  Left.isOneOf(tok::kw_const, tok::kw_volatile) &&
3297  (!Right.Next || Right.Next->is(tok::semi)))
3298  // Match const and volatile ref-qualifiers without any additional
3299  // qualifiers such as
3300  // void Fn() const &;
3301  return getTokenReferenceAlignment(Right) != FormatStyle::PAS_Left;
3302 
3303  return true;
3304 }
3305 
3306 bool TokenAnnotator::spaceRequiredBefore(const AnnotatedLine &Line,
3307  const FormatToken &Right) {
3308  const FormatToken &Left = *Right.Previous;
3309 
3310  // If the token is finalized don't touch it (as it could be in a
3311  // clang-format-off section).
3312  if (Left.Finalized)
3313  return Right.hasWhitespaceBefore();
3314 
3315  if (Right.Tok.getIdentifierInfo() && Left.Tok.getIdentifierInfo())
3316  return true; // Never ever merge two identifiers.
3317 
3318  // Leave a space between * and /* to avoid C4138 `comment end` found outside
3319  // of comment.
3320  if (Left.is(tok::star) && Right.is(tok::comment))
3321  return true;
3322 
3323  if (Style.isCpp()) {
3324  // Space between import <iostream>.
3325  // or import .....;
3326  if (Left.is(Keywords.kw_import) && Right.isOneOf(tok::less, tok::ellipsis))
3327  return true;
3328  // Space between `module :` and `import :`.
3329  if (Left.isOneOf(Keywords.kw_module, Keywords.kw_import) &&
3330  Right.is(TT_ModulePartitionColon))
3331  return true;
3332  // No space between import foo:bar but keep a space between import :bar;
3333  if (Left.is(tok::identifier) && Right.is(TT_ModulePartitionColon))
3334  return false;
3335  // No space between :bar;
3336  if (Left.is(TT_ModulePartitionColon) &&
3337  Right.isOneOf(tok::identifier, tok::kw_private))
3338  return false;
3339  if (Left.is(tok::ellipsis) && Right.is(tok::identifier) &&
3340  Line.First->is(Keywords.kw_import))
3341  return false;
3342  // Space in __attribute__((attr)) ::type.
3343  if (Left.is(TT_AttributeParen) && Right.is(tok::coloncolon))
3344  return true;
3345 
3346  if (Left.is(tok::kw_operator))
3347  return Right.is(tok::coloncolon);
3348  if (Right.is(tok::l_brace) && Right.is(BK_BracedInit) &&
3349  !Left.opensScope() && Style.SpaceBeforeCpp11BracedList)
3350  return true;
3351  if (Left.is(tok::less) && Left.is(TT_OverloadedOperator) &&
3352  Right.is(TT_TemplateOpener))
3353  return true;
3354  } else if (Style.Language == FormatStyle::LK_Proto ||
3355  Style.Language == FormatStyle::LK_TextProto) {
3356  if (Right.is(tok::period) &&
3357  Left.isOneOf(Keywords.kw_optional, Keywords.kw_required,
3358  Keywords.kw_repeated, Keywords.kw_extend))
3359  return true;
3360  if (Right.is(tok::l_paren) &&
3361  Left.isOneOf(Keywords.kw_returns, Keywords.kw_option))
3362  return true;
3363  if (Right.isOneOf(tok::l_brace, tok::less) && Left.is(TT_SelectorName))
3364  return true;
3365  // Slashes occur in text protocol extension syntax: [type/type] { ... }.
3366  if (Left.is(tok::slash) || Right.is(tok::slash))
3367  return false;
3368  if (Left.MatchingParen &&
3369  Left.MatchingParen->is(TT_ProtoExtensionLSquare) &&
3370  Right.isOneOf(tok::l_brace, tok::less))
3371  return !Style.Cpp11BracedListStyle;
3372  // A percent is probably part of a formatting specification, such as %lld.
3373  if (Left.is(tok::percent))
3374  return false;
3375  // Preserve the existence of a space before a percent for cases like 0x%04x
3376  // and "%d %d"
3377  if (Left.is(tok::numeric_constant) && Right.is(tok::percent))
3378  return Right.hasWhitespaceBefore();
3379  } else if (Style.isJson()) {
3380  if (Right.is(tok::colon))
3381  return false;
3382  } else if (Style.isCSharp()) {
3383  // Require spaces around '{' and before '}' unless they appear in
3384  // interpolated strings. Interpolated strings are merged into a single token
3385  // so cannot have spaces inserted by this function.
3386 
3387  // No space between 'this' and '['
3388  if (Left.is(tok::kw_this) && Right.is(tok::l_square))
3389  return false;
3390 
3391  // No space between 'new' and '('
3392  if (Left.is(tok::kw_new) && Right.is(tok::l_paren))
3393  return false;
3394 
3395  // Space before { (including space within '{ {').
3396  if (Right.is(tok::l_brace))
3397  return true;
3398 
3399  // Spaces inside braces.
3400  if (Left.is(tok::l_brace) && Right.isNot(tok::r_brace))
3401  return true;
3402 
3403  if (Left.isNot(tok::l_brace) && Right.is(tok::r_brace))
3404  return true;
3405 
3406  // Spaces around '=>'.
3407  if (Left.is(TT_FatArrow) || Right.is(TT_FatArrow))
3408  return true;
3409 
3410  // No spaces around attribute target colons
3411  if (Left.is(TT_AttributeColon) || Right.is(TT_AttributeColon))
3412  return false;
3413 
3414  // space between type and variable e.g. Dictionary<string,string> foo;
3415  if (Left.is(TT_TemplateCloser) && Right.is(TT_StartOfName))
3416  return true;
3417 
3418  // spaces inside square brackets.
3419  if (Left.is(tok::l_square) || Right.is(tok::r_square))
3420  return Style.SpacesInSquareBrackets;
3421 
3422  // No space before ? in nullable types.
3423  if (Right.is(TT_CSharpNullable))
3424  return false;
3425 
3426  // No space before null forgiving '!'.
3427  if (Right.is(TT_NonNullAssertion))
3428  return false;
3429 
3430  // No space between consecutive commas '[,,]'.
3431  if (Left.is(tok::comma) && Right.is(tok::comma))
3432  return false;
3433 
3434  // space after var in `var (key, value)`
3435  if (Left.is(Keywords.kw_var) && Right.is(tok::l_paren))
3436  return true;
3437 
3438  // space between keywords and paren e.g. "using ("
3439  if (Right.is(tok::l_paren))
3440  if (Left.isOneOf(tok::kw_using, Keywords.kw_async, Keywords.kw_when,
3441  Keywords.kw_lock))
3442  return Style.SpaceBeforeParensOptions.AfterControlStatements ||
3443  spaceRequiredBeforeParens(Right);
3444 
3445  // space between method modifier and opening parenthesis of a tuple return
3446  // type
3447  if (Left.isOneOf(tok::kw_public, tok::kw_private, tok::kw_protected,
3448  tok::kw_virtual, tok::kw_extern, tok::kw_static,
3449  Keywords.kw_internal, Keywords.kw_abstract,
3450  Keywords.kw_sealed, Keywords.kw_override,
3451  Keywords.kw_async, Keywords.kw_unsafe) &&
3452  Right.is(tok::l_paren))
3453  return true;
3454  } else if (Style.isJavaScript()) {
3455  if (Left.is(TT_FatArrow))
3456  return true;
3457  // for await ( ...
3458  if (Right.is(tok::l_paren) && Left.is(Keywords.kw_await) && Left.Previous &&
3459  Left.Previous->is(tok::kw_for))
3460  return true;
3461  if (Left.is(Keywords.kw_async) && Right.is(tok::l_paren) &&
3462  Right.MatchingParen) {
3463  const FormatToken *Next = Right.MatchingParen->getNextNonComment();
3464  // An async arrow function, for example: `x = async () => foo();`,
3465  // as opposed to calling a function called async: `x = async();`
3466  if (Next && Next->is(TT_FatArrow))
3467  return true;
3468  }
3469  if ((Left.is(TT_TemplateString) && Left.TokenText.endswith("${")) ||
3470  (Right.is(TT_TemplateString) && Right.TokenText.startswith("}")))
3471  return false;
3472  // In tagged template literals ("html`bar baz`"), there is no space between
3473  // the tag identifier and the template string.
3474  if (Keywords.IsJavaScriptIdentifier(Left,
3475  /* AcceptIdentifierName= */ false) &&
3476  Right.is(TT_TemplateString))
3477  return false;
3478  if (Right.is(tok::star) &&
3479  Left.isOneOf(Keywords.kw_function, Keywords.kw_yield))
3480  return false;
3481  if (Right.isOneOf(tok::l_brace, tok::l_square) &&
3482  Left.isOneOf(Keywords.kw_function, Keywords.kw_yield,
3483  Keywords.kw_extends, Keywords.kw_implements))
3484  return true;
3485  if (Right.is(tok::l_paren)) {
3486  // JS methods can use some keywords as names (e.g. `delete()`).
3487  if (Line.MustBeDeclaration && Left.Tok.getIdentifierInfo())
3488  return false;
3489  // Valid JS method names can include keywords, e.g. `foo.delete()` or
3490  // `bar.instanceof()`. Recognize call positions by preceding period.
3491  if (Left.Previous && Left.Previous->is(tok::period) &&
3492  Left.Tok.getIdentifierInfo())
3493  return false;
3494  // Additional unary JavaScript operators that need a space after.
3495  if (Left.isOneOf(tok::kw_throw, Keywords.kw_await, Keywords.kw_typeof,
3496  tok::kw_void))
3497  return true;
3498  }
3499  // `foo as const;` casts into a const type.
3500  if (Left.endsSequence(tok::kw_const, Keywords.kw_as)) {
3501  return false;
3502  }
3503  if ((Left.isOneOf(Keywords.kw_let, Keywords.kw_var, Keywords.kw_in,
3504  tok::kw_const) ||
3505  // "of" is only a keyword if it appears after another identifier
3506  // (e.g. as "const x of y" in a for loop), or after a destructuring
3507  // operation (const [x, y] of z, const {a, b} of c).
3508  (Left.is(Keywords.kw_of) && Left.Previous &&
3509  (Left.Previous->Tok.is(tok::identifier) ||
3510  Left.Previous->isOneOf(tok::r_square, tok::r_brace)))) &&
3511  (!Left.Previous || !Left.Previous->is(tok::period)))
3512  return true;
3513  if (Left.isOneOf(tok::kw_for, Keywords.kw_as) && Left.Previous &&
3514  Left.Previous->is(tok::period) && Right.is(tok::l_paren))
3515  return false;
3516  if (Left.is(Keywords.kw_as) &&
3517  Right.isOneOf(tok::l_square, tok::l_brace, tok::l_paren))
3518  return true;
3519  if (Left.is(tok::kw_default) && Left.Previous &&
3520  Left.Previous->is(tok::kw_export))
3521  return true;
3522  if (Left.is(Keywords.kw_is) && Right.is(tok::l_brace))
3523  return true;
3524  if (Right.isOneOf(TT_JsTypeColon, TT_JsTypeOptionalQuestion))
3525  return false;
3526  if (Left.is(TT_JsTypeOperator) || Right.is(TT_JsTypeOperator))
3527  return false;
3528  if ((Left.is(tok::l_brace) || Right.is(tok::r_brace)) &&
3529  Line.First->isOneOf(Keywords.kw_import, tok::kw_export))
3530  return false;
3531  if (Left.is(tok::ellipsis))
3532  return false;
3533  if (Left.is(TT_TemplateCloser) &&
3534  !Right.isOneOf(tok::equal, tok::l_brace, tok::comma, tok::l_square,
3535  Keywords.kw_implements, Keywords.kw_extends))
3536  // Type assertions ('<type>expr') are not followed by whitespace. Other
3537  // locations that should have whitespace following are identified by the
3538  // above set of follower tokens.
3539  return false;
3540  if (Right.is(TT_NonNullAssertion))
3541  return false;
3542  if (Left.is(TT_NonNullAssertion) &&
3543  Right.isOneOf(Keywords.kw_as, Keywords.kw_in))
3544  return true; // "x! as string", "x! in y"
3545  } else if (Style.Language == FormatStyle::LK_Java) {
3546  if (Left.is(tok::r_square) && Right.is(tok::l_brace))
3547  return true;
3548  if (Left.is(Keywords.kw_synchronized) && Right.is(tok::l_paren))
3549  return Style.SpaceBeforeParensOptions.AfterControlStatements ||
3550  spaceRequiredBeforeParens(Right);
3551  if ((Left.isOneOf(tok::kw_static, tok::kw_public, tok::kw_private,
3552  tok::kw_protected) ||
3553  Left.isOneOf(Keywords.kw_final, Keywords.kw_abstract,
3554  Keywords.kw_native)) &&
3555  Right.is(TT_TemplateOpener))
3556  return true;
3557  }
3558  if (Left.is(TT_ImplicitStringLiteral))
3559  return Right.hasWhitespaceBefore();
3560  if (Line.Type == LT_ObjCMethodDecl) {
3561  if (Left.is(TT_ObjCMethodSpecifier))
3562  return true;
3563  if (Left.is(tok::r_paren) && canBeObjCSelectorComponent(Right))
3564  // Don't space between ')' and <id> or ')' and 'new'. 'new' is not a
3565  // keyword in Objective-C, and '+ (instancetype)new;' is a standard class
3566  // method declaration.
3567  return false;
3568  }
3569  if (Line.Type == LT_ObjCProperty &&
3570  (Right.is(tok::equal) || Left.is(tok::equal)))
3571  return false;
3572 
3573  if (Right.isOneOf(TT_TrailingReturnArrow, TT_LambdaArrow) ||
3574  Left.isOneOf(TT_TrailingReturnArrow, TT_LambdaArrow))
3575  return true;
3576  if (Left.is(tok::comma) && !Right.is(TT_OverloadedOperatorLParen))
3577  return true;
3578  if (Right.is(tok::comma))
3579  return false;
3580  if (Right.is(TT_ObjCBlockLParen))
3581  return true;
3582  if (Right.is(TT_CtorInitializerColon))
3583  return Style.SpaceBeforeCtorInitializerColon;
3584  if (Right.is(TT_InheritanceColon) && !Style.SpaceBeforeInheritanceColon)
3585  return false;
3586  if (Right.is(TT_RangeBasedForLoopColon) &&
3587  !Style.SpaceBeforeRangeBasedForLoopColon)
3588  return false;
3589  if (Left.is(TT_BitFieldColon))
3590  return Style.BitFieldColonSpacing == FormatStyle::BFCS_Both ||
3591  Style.BitFieldColonSpacing == FormatStyle::BFCS_After;
3592  if (Right.is(tok::colon)) {
3593  if (Line.First->isOneOf(tok::kw_default, tok::kw_case))
3594  return Style.SpaceBeforeCaseColon;
3595  if (!Right.getNextNonComment() || Right.getNextNonComment()->is(tok::semi))
3596  return false;
3597  if (Right.is(TT_ObjCMethodExpr))
3598  return false;
3599  if (Left.is(tok::question))
3600  return false;
3601  if (Right.is(TT_InlineASMColon) && Left.is(tok::coloncolon))
3602  return false;
3603  if (Right.is(TT_DictLiteral))
3604  return Style.SpacesInContainerLiterals;
3605  if (Right.is(TT_AttributeColon))
3606  return false;
3607  if (Right.is(TT_CSharpNamedArgumentColon))
3608  return false;
3609  if (Right.is(TT_BitFieldColon))
3610  return Style.BitFieldColonSpacing == FormatStyle::BFCS_Both ||
3611  Style.BitFieldColonSpacing == FormatStyle::BFCS_Before;
3612  return true;
3613  }
3614  // Do not merge "- -" into "--".
3615  if ((Left.isOneOf(tok::minus, tok::minusminus) &&
3616  Right.isOneOf(tok::minus, tok::minusminus)) ||
3617  (Left.isOneOf(tok::plus, tok::plusplus) &&
3618  Right.isOneOf(tok::plus, tok::plusplus)))
3619  return true;
3620  if (Left.is(TT_UnaryOperator)) {
3621  if (!Right.is(tok::l_paren)) {
3622  // The alternative operators for ~ and ! are "compl" and "not".
3623  // If they are used instead, we do not want to combine them with
3624  // the token to the right, unless that is a left paren.
3625  if (Left.is(tok::exclaim) && Left.TokenText == "not")
3626  return true;
3627  if (Left.is(tok::tilde) && Left.TokenText == "compl")
3628  return true;
3629  // Lambda captures allow for a lone &, so "&]" needs to be properly
3630  // handled.
3631  if (Left.is(tok::amp) && Right.is(tok::r_square))
3632  return Style.SpacesInSquareBrackets;
3633  }
3634  return (Style.SpaceAfterLogicalNot && Left.is(tok::exclaim)) ||
3635  Right.is(TT_BinaryOperator);
3636  }
3637 
3638  // If the next token is a binary operator or a selector name, we have
3639  // incorrectly classified the parenthesis as a cast. FIXME: Detect correctly.
3640  if (Left.is(TT_CastRParen))
3641  return Style.SpaceAfterCStyleCast ||
3642  Right.isOneOf(TT_BinaryOperator, TT_SelectorName);
3643 
3644  auto ShouldAddSpacesInAngles = [this, &Right]() {
3645  if (this->Style.SpacesInAngles == FormatStyle::SIAS_Always)
3646  return true;
3647  if (this->Style.SpacesInAngles == FormatStyle::SIAS_Leave)
3648  return Right.hasWhitespaceBefore();
3649  return false;
3650  };
3651 
3652  if (Left.is(tok::greater) && Right.is(tok::greater)) {
3653  if (Style.Language == FormatStyle::LK_TextProto ||
3654  (Style.Language == FormatStyle::LK_Proto && Left.is(TT_DictLiteral)))
3655  return !Style.Cpp11BracedListStyle;
3656  return Right.is(TT_TemplateCloser) && Left.is(TT_TemplateCloser) &&
3657  ((Style.Standard < FormatStyle::LS_Cpp11) ||
3658  ShouldAddSpacesInAngles());
3659  }
3660  if (Right.isOneOf(tok::arrow, tok::arrowstar, tok::periodstar) ||
3661  Left.isOneOf(tok::arrow, tok::period, tok::arrowstar, tok::periodstar) ||
3662  (Right.is(tok::period) && Right.isNot(TT_DesignatedInitializerPeriod)))
3663  return false;
3664  if (!Style.SpaceBeforeAssignmentOperators && Left.isNot(TT_TemplateCloser) &&
3665  Right.getPrecedence() == prec::Assignment)
3666  return false;
3667  if (Style.Language == FormatStyle::LK_Java && Right.is(tok::coloncolon) &&
3668  (Left.is(tok::identifier) || Left.is(tok::kw_this)))
3669  return false;
3670  if (Right.is(tok::coloncolon) && Left.is(tok::identifier))
3671  // Generally don't remove existing spaces between an identifier and "::".
3672  // The identifier might actually be a macro name such as ALWAYS_INLINE. If
3673  // this turns out to be too lenient, add analysis of the identifier itself.
3674  return Right.hasWhitespaceBefore();
3675  if (Right.is(tok::coloncolon) &&
3676  !Left.isOneOf(tok::l_brace, tok::comment, tok::l_paren))
3677  // Put a space between < and :: in vector< ::std::string >
3678  return (Left.is(TT_TemplateOpener) &&
3679  ((Style.Standard < FormatStyle::LS_Cpp11) ||
3680  ShouldAddSpacesInAngles())) ||
3681  !(Left.isOneOf(tok::l_paren, tok::r_paren, tok::l_square,
3682  tok::kw___super, TT_TemplateOpener,
3683  TT_TemplateCloser)) ||
3684  (Left.is(tok::l_paren) && Style.SpacesInParentheses);
3685  if ((Left.is(TT_TemplateOpener)) != (Right.is(TT_TemplateCloser)))
3686  return ShouldAddSpacesInAngles();
3687  // Space before TT_StructuredBindingLSquare.
3688  if (Right.is(TT_StructuredBindingLSquare))
3689  return !Left.isOneOf(tok::amp, tok::ampamp) ||
3690  getTokenReferenceAlignment(Left) != FormatStyle::PAS_Right;
3691  // Space before & or && following a TT_StructuredBindingLSquare.
3692  if (Right.Next && Right.Next->is(TT_StructuredBindingLSquare) &&
3693  Right.isOneOf(tok::amp, tok::ampamp))
3694  return getTokenReferenceAlignment(Right) != FormatStyle::PAS_Left;
3695  if ((Right.is(TT_BinaryOperator) && !Left.is(tok::l_paren)) ||
3696  (Left.isOneOf(TT_BinaryOperator, TT_ConditionalExpr) &&
3697  !Right.is(tok::r_paren)))
3698  return true;
3699  if (Right.is(TT_TemplateOpener) && Left.is(tok::r_paren) &&
3700  Left.MatchingParen && Left.MatchingParen->is(TT_OverloadedOperatorLParen))
3701  return false;
3702  if (Right.is(tok::less) && Left.isNot(tok::l_paren) &&
3703  Line.startsWith(tok::hash))
3704  return true;
3705  if (Right.is(TT_TrailingUnaryOperator))
3706  return false;
3707  if (Left.is(TT_RegexLiteral))
3708  return false;
3709  return spaceRequiredBetween(Line, Left, Right);
3710 }
3711 
3712 // Returns 'true' if 'Tok' is a brace we'd want to break before in Allman style.
3713 static bool isAllmanBrace(const FormatToken &Tok) {
3714  return Tok.is(tok::l_brace) && Tok.is(BK_Block) &&
3715  !Tok.isOneOf(TT_ObjCBlockLBrace, TT_LambdaLBrace, TT_DictLiteral);
3716 }
3717 
3718 // Returns 'true' if 'Tok' is a function argument.
3719 static bool IsFunctionArgument(const FormatToken &Tok) {
3720  return Tok.MatchingParen && Tok.MatchingParen->Next &&
3721  Tok.MatchingParen->Next->isOneOf(tok::comma, tok::r_paren);
3722 }
3723 
3724 static bool
3726  FormatStyle::ShortLambdaStyle ShortLambdaOption) {
3727  return Tok.Children.empty() && ShortLambdaOption != FormatStyle::SLS_None;
3728 }
3729 
3730 static bool isAllmanLambdaBrace(const FormatToken &Tok) {
3731  return (Tok.is(tok::l_brace) && Tok.is(BK_Block) &&
3732  !Tok.isOneOf(TT_ObjCBlockLBrace, TT_DictLiteral));
3733 }
3734 
3735 // Returns the first token on the line that is not a comment.
3737  const FormatToken *Next = Line.First;
3738  if (!Next)
3739  return Next;
3740  if (Next->is(tok::comment))
3741  Next = Next->getNextNonComment();
3742  return Next;
3743 }
3744 
3745 bool TokenAnnotator::mustBreakBefore(const AnnotatedLine &Line,
3746  const FormatToken &Right) {
3747  const FormatToken &Left = *Right.Previous;
3748  if (Right.NewlinesBefore > 1 && Style.MaxEmptyLinesToKeep > 0)
3749  return true;
3750 
3751  if (Style.isCSharp()) {
3752  if (Left.is(TT_FatArrow) && Right.is(tok::l_brace) &&
3753  Style.BraceWrapping.AfterFunction)
3754  return true;
3755  if (Right.is(TT_CSharpNamedArgumentColon) ||
3756  Left.is(TT_CSharpNamedArgumentColon))
3757  return false;
3758  if (Right.is(TT_CSharpGenericTypeConstraint))
3759  return true;
3760  if (Right.Next && Right.Next->is(TT_FatArrow) &&
3761  (Right.is(tok::numeric_constant) ||
3762  (Right.is(tok::identifier) && Right.TokenText == "_")))
3763  return true;
3764 
3765  // Break after C# [...] and before public/protected/private/internal.
3766  if (Left.is(TT_AttributeSquare) && Left.is(tok::r_square) &&
3767  (Right.isAccessSpecifier(/*ColonRequired=*/false) ||
3768  Right.is(Keywords.kw_internal)))
3769  return true;
3770  // Break between ] and [ but only when there are really 2 attributes.
3771  if (Left.is(TT_AttributeSquare) && Right.is(TT_AttributeSquare) &&
3772  Left.is(tok::r_square) && Right.is(tok::l_square))
3773  return true;
3774 
3775  } else if (Style.isJavaScript()) {
3776  // FIXME: This might apply to other languages and token kinds.
3777  if (Right.is(tok::string_literal) && Left.is(tok::plus) && Left.Previous &&
3778  Left.Previous->is(tok::string_literal))
3779  return true;
3780  if (Left.is(TT_DictLiteral) && Left.is(tok::l_brace) && Line.Level == 0 &&
3781  Left.Previous && Left.Previous->is(tok::equal) &&
3782  Line.First->isOneOf(tok::identifier, Keywords.kw_import, tok::kw_export,
3783  tok::kw_const) &&
3784  // kw_var/kw_let are pseudo-tokens that are tok::identifier, so match
3785  // above.
3786  !Line.First->isOneOf(Keywords.kw_var, Keywords.kw_let))
3787  // Object literals on the top level of a file are treated as "enum-style".
3788  // Each key/value pair is put on a separate line, instead of bin-packing.
3789  return true;
3790  if (Left.is(tok::l_brace) && Line.Level == 0 &&
3791  (Line.startsWith(tok::kw_enum) ||
3792  Line.startsWith(tok::kw_const, tok::kw_enum) ||
3793  Line.startsWith(tok::kw_export, tok::kw_enum) ||
3794  Line.startsWith(tok::kw_export, tok::kw_const, tok::kw_enum)))
3795  // JavaScript top-level enum key/value pairs are put on separate lines
3796  // instead of bin-packing.
3797  return true;
3798  if (Right.is(tok::r_brace) && Left.is(tok::l_brace) && Left.Previous &&
3799  Left.Previous->is(TT_FatArrow)) {
3800  // JS arrow function (=> {...}).
3801  switch (Style.AllowShortLambdasOnASingleLine) {
3802  case FormatStyle::SLS_All:
3803  return false;
3804  case FormatStyle::SLS_None:
3805  return true;
3807  return !Left.Children.empty();
3809  // allow one-lining inline (e.g. in function call args) and empty arrow
3810  // functions.
3811  return (Left.NestingLevel == 0 && Line.Level == 0) &&
3812  !Left.Children.empty();
3813  }
3814  llvm_unreachable("Unknown FormatStyle::ShortLambdaStyle enum");
3815  }
3816 
3817  if (Right.is(tok::r_brace) && Left.is(tok::l_brace) &&
3818  !Left.Children.empty())
3819  // Support AllowShortFunctionsOnASingleLine for JavaScript.
3820  return Style.AllowShortFunctionsOnASingleLine == FormatStyle::SFS_None ||
3821  Style.AllowShortFunctionsOnASingleLine == FormatStyle::SFS_Empty ||
3822  (Left.NestingLevel == 0 && Line.Level == 0 &&
3823  Style.AllowShortFunctionsOnASingleLine &
3825  } else if (Style.Language == FormatStyle::LK_Java) {
3826  if (Right.is(tok::plus) && Left.is(tok::string_literal) && Right.Next &&
3827  Right.Next->is(tok::string_literal))
3828  return true;
3829  } else if (Style.Language == FormatStyle::LK_Cpp ||
3830  Style.Language == FormatStyle::LK_ObjC ||
3831  Style.Language == FormatStyle::LK_Proto ||
3832  Style.Language == FormatStyle::LK_TableGen ||
3833  Style.Language == FormatStyle::LK_TextProto) {
3834  if (Left.isStringLiteral() && Right.isStringLiteral())
3835  return true;
3836  }
3837 
3838  // Basic JSON newline processing.
3839  if (Style.isJson()) {
3840  // Always break after a JSON record opener.
3841  // {
3842  // }
3843  if (Left.is(TT_DictLiteral) && Left.is(tok::l_brace))
3844  return true;
3845  // Always break after a JSON array opener.
3846  // [
3847  // ]
3848  if (Left.is(TT_ArrayInitializerLSquare) && Left.is(tok::l_square) &&
3849  !Right.is(tok::r_square))
3850  return true;
3851  // Always break after successive entries.
3852  // 1,
3853  // 2
3854  if (Left.is(tok::comma))
3855  return true;
3856  }
3857 
3858  // If the last token before a '}', ']', or ')' is a comma or a trailing
3859  // comment, the intention is to insert a line break after it in order to make
3860  // shuffling around entries easier. Import statements, especially in
3861  // JavaScript, can be an exception to this rule.
3862  if (Style.JavaScriptWrapImports || Line.Type != LT_ImportStatement) {
3863  const FormatToken *BeforeClosingBrace = nullptr;
3864  if ((Left.isOneOf(tok::l_brace, TT_ArrayInitializerLSquare) ||
3865  (Style.isJavaScript() && Left.is(tok::l_paren))) &&
3866  Left.isNot(BK_Block) && Left.MatchingParen)
3867  BeforeClosingBrace = Left.MatchingParen->Previous;
3868  else if (Right.MatchingParen &&
3869  (Right.MatchingParen->isOneOf(tok::l_brace,
3870  TT_ArrayInitializerLSquare) ||
3871  (Style.isJavaScript() && Right.MatchingParen->is(tok::l_paren))))
3872  BeforeClosingBrace = &Left;
3873  if (BeforeClosingBrace && (BeforeClosingBrace->is(tok::comma) ||
3874  BeforeClosingBrace->isTrailingComment()))
3875  return true;
3876  }
3877 
3878  if (Right.is(tok::comment))
3879  return Left.isNot(BK_BracedInit) && Left.isNot(TT_CtorInitializerColon) &&
3880  (Right.NewlinesBefore > 0 && Right.HasUnescapedNewline);
3881  if (Left.isTrailingComment())
3882  return true;
3883  if (Left.IsUnterminatedLiteral)
3884  return true;
3885  if (Right.is(tok::lessless) && Right.Next && Left.is(tok::string_literal) &&
3886  Right.Next->is(tok::string_literal))
3887  return true;
3888  // Can break after template<> declaration
3889  if (Left.ClosesTemplateDeclaration && Left.MatchingParen &&
3890  Left.MatchingParen->NestingLevel == 0) {
3891  // Put concepts on the next line e.g.
3892  // template<typename T>
3893  // concept ...
3894  if (Right.is(tok::kw_concept))
3895  return Style.BreakBeforeConceptDeclarations;
3896  return (Style.AlwaysBreakTemplateDeclarations == FormatStyle::BTDS_Yes);
3897  }
3898  if (Style.PackConstructorInitializers == FormatStyle::PCIS_Never) {
3899  if (Style.BreakConstructorInitializers == FormatStyle::BCIS_BeforeColon &&
3900  (Left.is(TT_CtorInitializerComma) || Right.is(TT_CtorInitializerColon)))
3901  return true;
3902 
3903  if (Style.BreakConstructorInitializers == FormatStyle::BCIS_AfterColon &&
3904  Left.isOneOf(TT_CtorInitializerColon, TT_CtorInitializerComma))
3905  return true;
3906  }
3907  if (Style.PackConstructorInitializers < FormatStyle::PCIS_CurrentLine &&
3908  Style.BreakConstructorInitializers == FormatStyle::BCIS_BeforeComma &&
3909  Right.isOneOf(TT_CtorInitializerComma, TT_CtorInitializerColon))
3910  return true;
3911  // Break only if we have multiple inheritance.
3912  if (Style.BreakInheritanceList == FormatStyle::BILS_BeforeComma &&
3913  Right.is(TT_InheritanceComma))
3914  return true;
3915  if (Style.BreakInheritanceList == FormatStyle::BILS_AfterComma &&
3916  Left.is(TT_InheritanceComma))
3917  return true;
3918  if (Right.is(tok::string_literal) && Right.TokenText.startswith("R\""))
3919  // Multiline raw string literals are special wrt. line breaks. The author
3920  // has made a deliberate choice and might have aligned the contents of the
3921  // string literal accordingly. Thus, we try keep existing line breaks.
3922  return Right.IsMultiline && Right.NewlinesBefore > 0;
3923  if ((Left.is(tok::l_brace) || (Left.is(tok::less) && Left.Previous &&
3924  Left.Previous->is(tok::equal))) &&
3925  Right.NestingLevel == 1 && Style.Language == FormatStyle::LK_Proto) {
3926  // Don't put enums or option definitions onto single lines in protocol
3927  // buffers.
3928  return true;
3929  }
3930  if (Right.is(TT_InlineASMBrace))
3931  return Right.HasUnescapedNewline;
3932 
3933  if (isAllmanBrace(Left) || isAllmanBrace(Right)) {
3934  auto FirstNonComment = getFirstNonComment(Line);
3935  bool AccessSpecifier =
3936  FirstNonComment &&
3937  FirstNonComment->isOneOf(Keywords.kw_internal, tok::kw_public,
3938  tok::kw_private, tok::kw_protected);
3939 
3940  if (Style.BraceWrapping.AfterEnum) {
3941  if (Line.startsWith(tok::kw_enum) ||
3942  Line.startsWith(tok::kw_typedef, tok::kw_enum))
3943  return true;
3944  // Ensure BraceWrapping for `public enum A {`.
3945  if (AccessSpecifier && FirstNonComment->Next &&
3946  FirstNonComment->Next->is(tok::kw_enum))
3947  return true;
3948  }
3949 
3950  // Ensure BraceWrapping for `public interface A {`.
3951  if (Style.BraceWrapping.AfterClass &&
3952  ((AccessSpecifier && FirstNonComment->Next &&
3953  FirstNonComment->Next->is(Keywords.kw_interface)) ||
3954  Line.startsWith(Keywords.kw_interface)))
3955  return true;
3956 
3957  return (Line.startsWith(tok::kw_class) && Style.BraceWrapping.AfterClass) ||
3958  (Line.startsWith(tok::kw_struct) && Style.BraceWrapping.AfterStruct);
3959  }
3960 
3961  if (Left.is(TT_ObjCBlockLBrace) &&
3962  Style.AllowShortBlocksOnASingleLine == FormatStyle::SBS_Never)
3963  return true;
3964 
3965  // Ensure wrapping after __attribute__((XX)) and @interface etc.
3966  if (Left.is(TT_AttributeParen) && Right.is(TT_ObjCDecl))
3967  return true;
3968 
3969  if (Left.is(TT_LambdaLBrace)) {
3970  if (IsFunctionArgument(Left) &&
3971  Style.AllowShortLambdasOnASingleLine == FormatStyle::SLS_Inline)
3972  return false;
3973 
3974  if (Style.AllowShortLambdasOnASingleLine == FormatStyle::SLS_None ||
3975  Style.AllowShortLambdasOnASingleLine == FormatStyle::SLS_Inline ||
3976  (!Left.Children.empty() &&
3977  Style.AllowShortLambdasOnASingleLine == FormatStyle::SLS_Empty))
3978  return true;
3979  }
3980 
3981  if (Style.BraceWrapping.BeforeLambdaBody && Right.is(TT_LambdaLBrace) &&
3982  Left.isOneOf(tok::star, tok::amp, tok::ampamp, TT_TemplateCloser)) {
3983  return true;
3984  }
3985 
3986  // Put multiple Java annotation on a new line.
3987  if ((Style.Language == FormatStyle::LK_Java || Style.isJavaScript()) &&
3988  Left.is(TT_LeadingJavaAnnotation) &&
3989  Right.isNot(TT_LeadingJavaAnnotation) && Right.isNot(tok::l_paren) &&
3990  (Line.Last->is(tok::l_brace) || Style.BreakAfterJavaFieldAnnotations))
3991  return true;
3992 
3993  if (Right.is(TT_ProtoExtensionLSquare))
3994  return true;
3995 
3996  // In text proto instances if a submessage contains at least 2 entries and at
3997  // least one of them is a submessage, like A { ... B { ... } ... },
3998  // put all of the entries of A on separate lines by forcing the selector of
3999  // the submessage B to be put on a newline.
4000  //
4001  // Example: these can stay on one line:
4002  // a { scalar_1: 1 scalar_2: 2 }
4003  // a { b { key: value } }
4004  //
4005  // and these entries need to be on a new line even if putting them all in one
4006  // line is under the column limit:
4007  // a {
4008  // scalar: 1
4009  // b { key: value }
4010  // }
4011  //
4012  // We enforce this by breaking before a submessage field that has previous
4013  // siblings, *and* breaking before a field that follows a submessage field.
4014  //
4015  // Be careful to exclude the case [proto.ext] { ... } since the `]` is
4016  // the TT_SelectorName there, but we don't want to break inside the brackets.
4017  //
4018  // Another edge case is @submessage { key: value }, which is a common
4019  // substitution placeholder. In this case we want to keep `@` and `submessage`
4020  // together.
4021  //
4022  // We ensure elsewhere that extensions are always on their own line.
4023  if ((Style.Language == FormatStyle::LK_Proto ||
4024  Style.Language == FormatStyle::LK_TextProto) &&
4025  Right.is(TT_SelectorName) && !Right.is(tok::r_square) && Right.Next) {
4026  // Keep `@submessage` together in:
4027  // @submessage { key: value }
4028  if (Left.is(tok::at))
4029  return false;
4030  // Look for the scope opener after selector in cases like:
4031  // selector { ...
4032  // selector: { ...
4033  // selector: @base { ...
4034  FormatToken *LBrace = Right.Next;
4035  if (LBrace && LBrace->is(tok::colon)) {
4036  LBrace = LBrace->Next;
4037  if (LBrace && LBrace->is(tok::at)) {
4038  LBrace = LBrace->Next;
4039  if (LBrace)
4040  LBrace = LBrace->Next;
4041  }
4042  }
4043  if (LBrace &&
4044  // The scope opener is one of {, [, <:
4045  // selector { ... }
4046  // selector [ ... ]
4047  // selector < ... >
4048  //
4049  // In case of selector { ... }, the l_brace is TT_DictLiteral.
4050  // In case of an empty selector {}, the l_brace is not TT_DictLiteral,
4051  // so we check for immediately following r_brace.
4052  ((LBrace->is(tok::l_brace) &&
4053  (LBrace->is(TT_DictLiteral) ||
4054  (LBrace->Next && LBrace->Next->is(tok::r_brace)))) ||
4055  LBrace->is(TT_ArrayInitializerLSquare) || LBrace->is(tok::less))) {
4056  // If Left.ParameterCount is 0, then this submessage entry is not the
4057  // first in its parent submessage, and we want to break before this entry.
4058  // If Left.ParameterCount is greater than 0, then its parent submessage
4059  // might contain 1 or more entries and we want to break before this entry
4060  // if it contains at least 2 entries. We deal with this case later by
4061  // detecting and breaking before the next entry in the parent submessage.
4062  if (Left.ParameterCount == 0)
4063  return true;
4064  // However, if this submessage is the first entry in its parent
4065  // submessage, Left.ParameterCount might be 1 in some cases.
4066  // We deal with this case later by detecting an entry
4067  // following a closing paren of this submessage.
4068  }
4069 
4070  // If this is an entry immediately following a submessage, it will be
4071  // preceded by a closing paren of that submessage, like in:
4072  // left---. .---right
4073  // v v
4074  // sub: { ... } key: value
4075  // If there was a comment between `}` an `key` above, then `key` would be
4076  // put on a new line anyways.
4077  if (Left.isOneOf(tok::r_brace, tok::greater, tok::r_square))
4078  return true;
4079  }
4080 
4081  // Deal with lambda arguments in C++ - we want consistent line breaks whether
4082  // they happen to be at arg0, arg1 or argN. The selection is a bit nuanced
4083  // as aggressive line breaks are placed when the lambda is not the last arg.
4084  if ((Style.Language == FormatStyle::LK_Cpp ||
4085  Style.Language == FormatStyle::LK_ObjC) &&
4086  Left.is(tok::l_paren) && Left.BlockParameterCount > 0 &&
4087  !Right.isOneOf(tok::l_paren, TT_LambdaLSquare)) {
4088  // Multiple lambdas in the same function call force line breaks.
4089  if (Left.BlockParameterCount > 1)
4090  return true;
4091 
4092  // A lambda followed by another arg forces a line break.
4093  if (!Left.Role)
4094  return false;
4095  auto Comma = Left.Role->lastComma();
4096  if (!Comma)
4097  return false;
4098  auto Next = Comma->getNextNonComment();
4099  if (!Next)
4100  return false;
4101  if (!Next->isOneOf(TT_LambdaLSquare, tok::l_brace, tok::caret))
4102  return true;
4103  }
4104 
4105  return false;
4106 }
4107 
4108 bool TokenAnnotator::canBreakBefore(const AnnotatedLine &Line,
4109  const FormatToken &Right) {
4110  const FormatToken &Left = *Right.Previous;
4111  // Language-specific stuff.
4112  if (Style.isCSharp()) {
4113  if (Left.isOneOf(TT_CSharpNamedArgumentColon, TT_AttributeColon) ||
4114  Right.isOneOf(TT_CSharpNamedArgumentColon, TT_AttributeColon))
4115  return false;
4116  // Only break after commas for generic type constraints.
4117  if (Line.First->is(TT_CSharpGenericTypeConstraint))
4118  return Left.is(TT_CSharpGenericTypeConstraintComma);
4119  // Keep nullable operators attached to their identifiers.
4120  if (Right.is(TT_CSharpNullable)) {
4121  return false;
4122  }
4123  } else if (Style.Language == FormatStyle::LK_Java) {
4124  if (Left.isOneOf(Keywords.kw_throws, Keywords.kw_extends,
4125  Keywords.kw_implements))
4126  return false;
4127  if (Right.isOneOf(Keywords.kw_throws, Keywords.kw_extends,
4128  Keywords.kw_implements))
4129  return true;
4130  } else if (Style.isJavaScript()) {
4131  const FormatToken *NonComment = Right.getPreviousNonComment();
4132  if (NonComment &&
4133  NonComment->isOneOf(
4134  tok::kw_return, Keywords.kw_yield, tok::kw_continue, tok::kw_break,
4135  tok::kw_throw, Keywords.kw_interface, Keywords.kw_type,
4136  tok::kw_static, tok::kw_public, tok::kw_private, tok::kw_protected,
4137  Keywords.kw_readonly, Keywords.kw_override, Keywords.kw_abstract,
4138  Keywords.kw_get, Keywords.kw_set, Keywords.kw_async,
4139  Keywords.kw_await))
4140  return false; // Otherwise automatic semicolon insertion would trigger.
4141  if (Right.NestingLevel == 0 &&
4142  (Left.Tok.getIdentifierInfo() ||
4143  Left.isOneOf(tok::r_square, tok::r_paren)) &&
4144  Right.isOneOf(tok::l_square, tok::l_paren))
4145  return false; // Otherwise automatic semicolon insertion would trigger.
4146  if (NonComment && NonComment->is(tok::identifier) &&
4147  NonComment->TokenText == "asserts")
4148  return false;
4149  if (Left.is(TT_FatArrow) && Right.is(tok::l_brace))
4150  return false;
4151  if (Left.is(TT_JsTypeColon))
4152  return true;
4153  // Don't wrap between ":" and "!" of a strict prop init ("field!: type;").
4154  if (Left.is(tok::exclaim) && Right.is(tok::colon))
4155  return false;
4156  // Look for is type annotations like:
4157  // function f(): a is B { ... }
4158  // Do not break before is in these cases.
4159  if (Right.is(Keywords.kw_is)) {
4160  const FormatToken *Next = Right.getNextNonComment();
4161  // If `is` is followed by a colon, it's likely that it's a dict key, so
4162  // ignore it for this check.
4163  // For example this is common in Polymer:
4164  // Polymer({
4165  // is: 'name',
4166  // ...
4167  // });
4168  if (!Next || !Next->is(tok::colon))
4169  return false;
4170  }
4171  if (Left.is(Keywords.kw_in))
4172  return Style.BreakBeforeBinaryOperators == FormatStyle::BOS_None;
4173  if (Right.is(Keywords.kw_in))
4174  return Style.BreakBeforeBinaryOperators != FormatStyle::BOS_None;
4175  if (Right.is(Keywords.kw_as))
4176  return false; // must not break before as in 'x as type' casts
4177  if (Right.isOneOf(Keywords.kw_extends, Keywords.kw_infer)) {
4178  // extends and infer can appear as keywords in conditional types:
4179  // https://www.typescriptlang.org/docs/handbook/release-notes/typescript-2-8.html#conditional-types
4180  // do not break before them, as the expressions are subject to ASI.
4181  return false;
4182  }
4183  if (Left.is(Keywords.kw_as))
4184  return true;
4185  if (Left.is(TT_NonNullAssertion))
4186  return true;
4187  if (Left.is(Keywords.kw_declare) &&
4188  Right.isOneOf(Keywords.kw_module, tok::kw_namespace,
4189  Keywords.kw_function, tok::kw_class, tok::kw_enum,
4190  Keywords.kw_interface, Keywords.kw_type, Keywords.kw_var,
4191  Keywords.kw_let, tok::kw_const))
4192  // See grammar for 'declare' statements at:
4193  // https://github.com/Microsoft/TypeScript/blob/main/doc/spec-ARCHIVED.md#A.10
4194  return false;
4195  if (Left.isOneOf(Keywords.kw_module, tok::kw_namespace) &&
4196  Right.isOneOf(tok::identifier, tok::string_literal))
4197  return false; // must not break in "module foo { ...}"
4198  if (Right.is(TT_TemplateString) && Right.closesScope())
4199  return false;
4200  // Don't split tagged template literal so there is a break between the tag
4201  // identifier and template string.
4202  if (Left.is(tok::identifier) && Right.is(TT_TemplateString)) {
4203  return false;
4204  }
4205  if (Left.is(TT_TemplateString) && Left.opensScope())
4206  return true;
4207  }
4208 
4209  if (Left.is(tok::at))
4210  return false;
4211  if (Left.Tok.getObjCKeywordID() == tok::objc_interface)
4212  return false;
4213  if (Left.isOneOf(TT_JavaAnnotation, TT_LeadingJavaAnnotation))
4214  return !Right.is(tok::l_paren);
4215  if (Right.is(TT_PointerOrReference))
4216  return Line.IsMultiVariableDeclStmt ||
4217  (getTokenPointerOrReferenceAlignment(Right) ==
4219  (!Right.Next || Right.Next->isNot(TT_FunctionDeclarationName)));
4220  if (Right.isOneOf(TT_StartOfName, TT_FunctionDeclarationName) ||
4221  Right.is(tok::kw_operator))
4222  return true;
4223  if (Left.is(TT_PointerOrReference))
4224  return false;
4225  if (Right.isTrailingComment())
4226  // We rely on MustBreakBefore being set correctly here as we should not
4227  // change the "binding" behavior of a comment.
4228  // The first comment in a braced lists is always interpreted as belonging to
4229  // the first list element. Otherwise, it should be placed outside of the
4230  // list.
4231  return Left.is(BK_BracedInit) ||
4232  (Left.is(TT_CtorInitializerColon) &&
4233  Style.BreakConstructorInitializers == FormatStyle::BCIS_AfterColon);
4234  if (Left.is(tok::question) && Right.is(tok::colon))
4235  return false;
4236  if (Right.is(TT_ConditionalExpr) || Right.is(tok::question))
4237  return Style.BreakBeforeTernaryOperators;
4238  if (Left.is(TT_ConditionalExpr) || Left.is(tok::question))
4239  return !Style.BreakBeforeTernaryOperators;
4240  if (Left.is(TT_InheritanceColon))
4241  return Style.BreakInheritanceList == FormatStyle::BILS_AfterColon;
4242  if (Right.is(TT_InheritanceColon))
4243  return Style.BreakInheritanceList != FormatStyle::BILS_AfterColon;
4244  if (Right.is(TT_ObjCMethodExpr) && !Right.is(tok::r_square) &&
4245  Left.isNot(TT_SelectorName))
4246  return true;
4247 
4248  if (Right.is(tok::colon) &&
4249  !Right.isOneOf(TT_CtorInitializerColon, TT_InlineASMColon))
4250  return false;
4251  if (Left.is(tok::colon) && Left.isOneOf(TT_DictLiteral, TT_ObjCMethodExpr)) {
4252  if (Style.Language == FormatStyle::LK_Proto ||
4253  Style.Language == FormatStyle::LK_TextProto) {
4254  if (!Style.AlwaysBreakBeforeMultilineStrings && Right.isStringLiteral())
4255  return false;
4256  // Prevent cases like:
4257  //
4258  // submessage:
4259  // { key: valueeeeeeeeeeee }
4260  //
4261  // when the snippet does not fit into one line.
4262  // Prefer:
4263  //
4264  // submessage: {
4265  // key: valueeeeeeeeeeee
4266  // }
4267  //
4268  // instead, even if it is longer by one line.
4269  //
4270  // Note that this allows allows the "{" to go over the column limit
4271  // when the column limit is just between ":" and "{", but that does
4272  // not happen too often and alternative formattings in this case are
4273  // not much better.
4274  //
4275  // The code covers the cases:
4276  //
4277  // submessage: { ... }
4278  // submessage: < ... >
4279  // repeated: [ ... ]
4280  if (((Right.is(tok::l_brace) || Right.is(tok::less)) &&
4281  Right.is(TT_DictLiteral)) ||
4282  Right.is(TT_ArrayInitializerLSquare))
4283  return false;
4284  }
4285  return true;
4286  }
4287  if (Right.is(tok::r_square) && Right.MatchingParen &&
4288  Right.MatchingParen->is(TT_ProtoExtensionLSquare))
4289  return false;
4290  if (Right.is(TT_SelectorName) || (Right.is(tok::identifier) && Right.Next &&
4291  Right.Next->is(TT_ObjCMethodExpr)))
4292  return Left.isNot(tok::period); // FIXME: Properly parse ObjC calls.
4293  if (Left.is(tok::r_paren) && Line.Type == LT_ObjCProperty)
4294  return true;
4295  if (Left.ClosesTemplateDeclaration || Left.is(TT_FunctionAnnotationRParen))
4296  return true;
4297  if (Right.isOneOf(TT_RangeBasedForLoopColon, TT_OverloadedOperatorLParen,
4298  TT_OverloadedOperator))
4299  return false;
4300  if (Left.is(TT_RangeBasedForLoopColon))
4301  return true;
4302  if (Right.is(TT_RangeBasedForLoopColon))
4303  return false;
4304  if (Left.is(TT_TemplateCloser) && Right.is(TT_TemplateOpener))
4305  return true;
4306  if (Left.isOneOf(TT_TemplateCloser, TT_UnaryOperator) ||
4307  Left.is(tok::kw_operator))
4308  return false;
4309  if (Left.is(tok::equal) && !Right.isOneOf(tok::kw_default, tok::kw_delete) &&
4310  Line.Type == LT_VirtualFunctionDecl && Left.NestingLevel == 0)
4311  return false;
4312  if (Left.is(tok::equal) && Right.is(tok::l_brace) &&
4313  !Style.Cpp11BracedListStyle)
4314  return false;
4315  if (Left.is(tok::l_paren) &&
4316  Left.isOneOf(TT_AttributeParen, TT_TypeDeclarationParen))
4317  return false;
4318  if (Left.is(tok::l_paren) && Left.Previous &&
4319  (Left.Previous->isOneOf(TT_BinaryOperator, TT_CastRParen)))
4320  return false;
4321  if (Right.is(TT_ImplicitStringLiteral))
4322  return false;
4323 
4324  if (Right.is(TT_TemplateCloser))
4325  return false;
4326  if (Right.is(tok::r_square) && Right.MatchingParen &&
4327  Right.MatchingParen->is(TT_LambdaLSquare))
4328  return false;
4329 
4330  // We only break before r_brace if there was a corresponding break before
4331  // the l_brace, which is tracked by BreakBeforeClosingBrace.
4332  if (Right.is(tok::r_brace))
4333  return Right.MatchingParen && Right.MatchingParen->is(BK_Block);
4334 
4335  // We only break before r_paren if we're in a block indented context.
4336  if (Right.is(tok::r_paren)) {
4337  if (Style.AlignAfterOpenBracket == FormatStyle::BAS_BlockIndent) {
4338  return Right.MatchingParen &&
4339  !(Right.MatchingParen->Previous &&
4340  (Right.MatchingParen->Previous->is(tok::kw_for) ||
4341  Right.MatchingParen->Previous->isIf()));
4342  }
4343 
4344  return false;
4345  }
4346 
4347  // Allow breaking after a trailing annotation, e.g. after a method
4348  // declaration.
4349  if (Left.is(TT_TrailingAnnotation))
4350  return !Right.isOneOf(tok::l_brace, tok::semi, tok::equal, tok::l_paren,
4351  tok::less, tok::coloncolon);
4352 
4353  if (Right.is(tok::kw___attribute) ||
4354  (Right.is(tok::l_square) && Right.is(TT_AttributeSquare)))
4355  return !Left.is(TT_AttributeSquare);
4356 
4357  if (Left.is(tok::identifier) && Right.is(tok::string_literal))
4358  return true;
4359 
4360  if (Right.is(tok::identifier) && Right.Next && Right.Next->is(TT_DictLiteral))
4361  return true;
4362 
4363  if (Left.is(TT_CtorInitializerColon))
4364  return Style.BreakConstructorInitializers == FormatStyle::BCIS_AfterColon;
4365  if (Right.is(TT_CtorInitializerColon))
4366  return Style.BreakConstructorInitializers != FormatStyle::BCIS_AfterColon;
4367  if (Left.is(TT_CtorInitializerComma) &&
4368  Style.BreakConstructorInitializers == FormatStyle::BCIS_BeforeComma)
4369  return false;
4370  if (Right.is(TT_CtorInitializerComma) &&
4371  Style.BreakConstructorInitializers == FormatStyle::BCIS_BeforeComma)
4372  return true;
4373  if (Left.is(TT_InheritanceComma) &&
4374  Style.BreakInheritanceList == FormatStyle::BILS_BeforeComma)
4375  return false;
4376  if (Right.is(TT_InheritanceComma) &&
4377  Style.BreakInheritanceList == FormatStyle::BILS_BeforeComma)
4378  return true;
4379  if ((Left.is(tok::greater) && Right.is(tok::greater)) ||
4380  (Left.is(tok::less) && Right.is(tok::less)))
4381  return false;
4382  if (Right.is(TT_BinaryOperator) &&
4383  Style.BreakBeforeBinaryOperators != FormatStyle::BOS_None &&
4384  (Style.BreakBeforeBinaryOperators == FormatStyle::BOS_All ||
4385  Right.getPrecedence() != prec::Assignment))
4386  return true;
4387  if (Left.is(TT_ArrayInitializerLSquare))
4388  return true;
4389  if (Right.is(tok::kw_typename) && Left.isNot(tok::kw_const))
4390  return true;
4391  if ((Left.isBinaryOperator() || Left.is(TT_BinaryOperator)) &&
4392  !Left.isOneOf(tok::arrowstar, tok::lessless) &&
4393  Style.BreakBeforeBinaryOperators != FormatStyle::BOS_All &&
4394  (Style.BreakBeforeBinaryOperators == FormatStyle::BOS_None ||
4395  Left.getPrecedence() == prec::Assignment))
4396  return true;
4397  if ((Left.is(TT_AttributeSquare) && Right.is(tok::l_square)) ||
4398  (Left.is(tok::r_square) && Right.is(TT_AttributeSquare)))
4399  return false;
4400 
4401  auto ShortLambdaOption = Style.AllowShortLambdasOnASingleLine;
4402  if (Style.BraceWrapping.BeforeLambdaBody && Right.is(TT_LambdaLBrace)) {
4403  if (isAllmanLambdaBrace(Left))
4404  return !isItAnEmptyLambdaAllowed(Left, ShortLambdaOption);
4405  if (isAllmanLambdaBrace(Right))
4406  return !isItAnEmptyLambdaAllowed(Right, ShortLambdaOption);
4407  }
4408 
4409  return Left.isOneOf(tok::comma, tok::coloncolon, tok::semi, tok::l_brace,
4410  tok::kw_class, tok::kw_struct, tok::comment) ||
4411  Right.isMemberAccess() ||
4412  Right.isOneOf(TT_TrailingReturnArrow, TT_LambdaArrow, tok::lessless,
4413  tok::colon, tok::l_square, tok::at) ||
4414  (Left.is(tok::r_paren) &&
4415  Right.isOneOf(tok::identifier, tok::kw_const)) ||
4416  (Left.is(tok::l_paren) && !Right.is(tok::r_paren)) ||
4417  (Left.is(TT_TemplateOpener) && !Right.is(TT_TemplateCloser));
4418 }
4419 
4420 void TokenAnnotator::printDebugInfo(const AnnotatedLine &Line) {
4421  llvm::errs() << "AnnotatedTokens(L=" << Line.Level << "):\n";
4422  const FormatToken *Tok = Line.First;
4423  while (Tok) {
4424  llvm::errs() << " M=" << Tok->MustBreakBefore
4425  << " C=" << Tok->CanBreakBefore
4426  << " T=" << getTokenTypeName(Tok->getType())
4427  << " S=" << Tok->SpacesRequiredBefore
4428  << " F=" << Tok->Finalized << " B=" << Tok->BlockParameterCount
4429  << " BK=" << Tok->getBlockKind() << " P=" << Tok->SplitPenalty
4430  << " Name=" << Tok->Tok.getName() << " L=" << Tok->TotalLength
4431  << " PPK=" << Tok->getPackingKind() << " FakeLParens=";
4432  for (unsigned i = 0, e = Tok->FakeLParens.size(); i != e; ++i)
4433  llvm::errs() << Tok->FakeLParens[i] << "/";
4434  llvm::errs() << " FakeRParens=" << Tok->FakeRParens;
4435  llvm::errs() << " II=" << Tok->Tok.getIdentifierInfo();
4436  llvm::errs() << " Text='" << Tok->TokenText << "'\n";
4437  if (!Tok->Next)
4438  assert(Tok == Line.Last);
4439  Tok = Tok->Next;
4440  }
4441  llvm::errs() << "----\n";
4442 }
4443 
4445 TokenAnnotator::getTokenReferenceAlignment(const FormatToken &Reference) {
4446  assert(Reference.isOneOf(tok::amp, tok::ampamp));
4447  switch (Style.ReferenceAlignment) {
4449  return Style.PointerAlignment;
4450  case FormatStyle::RAS_Left:
4451  return FormatStyle::PAS_Left;
4453  return FormatStyle::PAS_Right;
4455  return FormatStyle::PAS_Middle;
4456  }
4457  assert(0); //"Unhandled value of ReferenceAlignment"
4458  return Style.PointerAlignment;
4459 }
4460 
4462 TokenAnnotator::getTokenPointerOrReferenceAlignment(
4463  const FormatToken &PointerOrReference) {
4464  if (PointerOrReference.isOneOf(tok::amp, tok::ampamp)) {
4465  switch (Style.ReferenceAlignment) {
4467  return Style.PointerAlignment;
4468  case FormatStyle::RAS_Left:
4469  return FormatStyle::PAS_Left;
4471  return FormatStyle::PAS_Right;
4473  return FormatStyle::PAS_Middle;
4474  }
4475  }
4476  assert(PointerOrReference.is(tok::star));
4477  return Style.PointerAlignment;
4478 }
4479 
4480 } // namespace format
4481 } // namespace clang
clang::format::FormatStyle::PAS_Left
@ PAS_Left
Align pointer to the left.
Definition: Format.h:2930
clang::prec::Comma
@ Comma
Definition: OperatorPrecedence.h:28
clang::prec::Conditional
@ Conditional
Definition: OperatorPrecedence.h:30
max
__DEVICE__ int max(int __a, int __b)
Definition: __clang_cuda_math.h:196
clang::format::TokenAnnotator::calculateFormattingInformation
void calculateFormattingInformation(AnnotatedLine &Line)
Definition: TokenAnnotator.cpp:2563
clang::format::isAllmanLambdaBrace
static bool isAllmanLambdaBrace(const FormatToken &Tok)
Definition: TokenAnnotator.cpp:3730
clang::format::FormatStyle::BILS_AfterColon
@ BILS_AfterColon
Break inheritance list after the colon and commas.
Definition: Format.h:1966
clang::format::FormatStyle::ShortLambdaStyle
ShortLambdaStyle
Different styles for merging short lambdas containing at most one statement.
Definition: Format.h:741
clang::ObjCSubstitutionContext::Parameter
@ Parameter
The parameter type of a method or function.
return
return(__x >> __y)|(__x<<(32 - __y))
clang::format::maxNestingDepth
static unsigned maxNestingDepth(const AnnotatedLine &Line)
Definition: TokenAnnotator.cpp:2389
clang::format::AnnotatedLine::First
FormatToken * First
Definition: TokenAnnotator.h:118
llvm::SmallVector
Definition: LLVM.h:38
clang::format::FormatStyle::SBPO_Always
@ SBPO_Always
Always put a space before opening parentheses, except when it's prohibited by the syntax rules (in fu...
Definition: Format.h:3441
clang::format::FormatStyle::BFCS_Before
@ BFCS_Before
Add space before the : only.
Definition: Format.h:3730
clang::if
if(T->getSizeExpr()) TRY_TO(TraverseStmt(T -> getSizeExpr()))
Definition: RecursiveASTVisitor.h:1003
clang::format::FormatStyle::LK_Cpp
@ LK_Cpp
Should be used for C, C++.
Definition: Format.h:2657
clang::format::FormatStyle::SAPQ_After
@ SAPQ_After
Ensure that there is a space after pointer qualifiers.
Definition: Format.h:3324
clang::format::FormatStyle::SLS_Empty
@ SLS_Empty
Only merge empty lambdas.
Definition: Format.h:751
clang::format::FormatStyle::SAPQ_Both
@ SAPQ_Both
Ensure that there is a space both before and after pointer qualifiers.
Definition: Format.h:3330
clang::format::AnnotatedLine::Level
unsigned Level
Definition: TokenAnnotator.h:124
clang::Parser
Parser - This implements a parser for the C family of languages.
Definition: Parser.h:60
clang::dataflow::SkipPast::Reference
@ Reference
An optional reference should be skipped past.
clang::format::FormatStyle::RTBS_All
@ RTBS_All
Always break after the return type.
Definition: Format.h:818
clang::format::FormatStyle::SLS_None
@ SLS_None
Never merge lambdas into a single line.
Definition: Format.h:743
clang::format::FormatToken
A wrapper around a Token storing information about the whitespace characters preceding it.
Definition: FormatToken.h:212
clang::ComparisonCategoryType::First
@ First
clang::minimize_source_to_dependency_directives::pp_import
@ pp_import
Definition: DependencyDirectivesSourceMinimizer.h:39
llvm::SmallPtrSet
Definition: ASTContext.h:82
InCSharpAttributeSpecifier
bool InCSharpAttributeSpecifier
Definition: TokenAnnotator.cpp:1471
clang::prec::Assignment
@ Assignment
Definition: OperatorPrecedence.h:29
SourceManager.h
clang::format::FormatStyle::BFCS_Both
@ BFCS_Both
Add one space on each side of the :
Definition: Format.h:3719
clang::format::FormatToken::OriginalColumn
unsigned OriginalColumn
The original 0-based column of this token, including expanded tabs.
Definition: FormatToken.h:373
clang::format::FormatStyle::RAS_Left
@ RAS_Left
Align reference to the left.
Definition: Format.h:3030
clang::format::FormatStyle::SBS_Never
@ SBS_Never
Never merge blocks into a single line.
Definition: Format.h:579
clang::format::FormatStyle::LK_TableGen
@ LK_TableGen
Should be used for TableGen code.
Definition: Format.h:2672
clang::format::BK_BracedInit
@ BK_BracedInit
Definition: FormatToken.h:138
End
SourceLocation End
Definition: USRLocFinder.cpp:167
clang::format::PPK_BinPacked
@ PPK_BinPacked
Definition: FormatToken.h:141
clang::format::FormatToken::NewlinesBefore
unsigned NewlinesBefore
The number of newlines immediately before the Token.
Definition: FormatToken.h:338
clang::format::FormatStyle::BTDS_Yes
@ BTDS_Yes
Always break after template declaration.
Definition: Format.h:918
InCtorInitializer
bool InCtorInitializer
Definition: TokenAnnotator.cpp:1466
clang::format::FormatStyle::BCIS_AfterColon
@ BCIS_AfterColon
Break constructor initializers after the colon and commas.
Definition: Format.h:1823
InTemplateArgument
bool InTemplateArgument
Definition: TokenAnnotator.cpp:1465
TokenAnnotator.h
clang::minimize_source_to_dependency_directives::pp_if
@ pp_if
Definition: DependencyDirectivesSourceMinimizer.h:46
TokenKinds.h
clang::format::LT_PreprocessorDirective
@ LT_PreprocessorDirective
Definition: TokenAnnotator.h:31
clang::format::FormatStyle::LK_Proto
@ LK_Proto
Should be used for Protocol Buffers (https://developers.google.com/protocol-buffers/).
Definition: Format.h:2670
clang::format::FormatToken::NestingLevel
unsigned NestingLevel
The nesting level of this token, i.e.
Definition: FormatToken.h:386
clang::format::FormatToken::Previous
FormatToken * Previous
The previous token in the unwrapped line.
Definition: FormatToken.h:432
clang::format::FormatStyle::SIAS_Always
@ SIAS_Always
Add spaces after < and before >.
Definition: Format.h:3607
clang::prec::Unknown
@ Unknown
Definition: OperatorPrecedence.h:27
clang::format::FormatStyle::BAS_DontAlign
@ BAS_DontAlign
Don't align, instead use ContinuationIndentWidth, e.g.
Definition: Format.h:77
clang::format::FormatStyle::AIAS_None
@ AIAS_None
Don't align array initializer columns.
Definition: Format.h:130
InCpp11AttributeSpecifier
bool InCpp11AttributeSpecifier
Definition: TokenAnnotator.cpp:1470
clang::minimize_source_to_dependency_directives::pp_include_next
@ pp_include_next
Definition: DependencyDirectivesSourceMinimizer.h:45
clang::format::FormatStyle::BCIS_BeforeColon
@ BCIS_BeforeColon
Break constructor initializers before the colon and after the commas.
Definition: Format.h:1808
clang::format::FormatStyle::SIAS_Leave
@ SIAS_Leave
Keep a single space after < and before > if any spaces were present.
Definition: Format.h:3610
clang::format::FormatStyle::SFS_Empty
@ SFS_Empty
Only merge empty functions.
Definition: Format.h:641
clang::format::FormatStyle::LK_TextProto
@ LK_TextProto
Should be used for Protocol Buffer messages in text format (https://developers.google....
Definition: Format.h:2675
clang::format::AnnotatedLine
Definition: TokenAnnotator.h:36
ColonIsObjCMethodExpr
bool ColonIsObjCMethodExpr
Definition: TokenAnnotator.cpp:1461
Depth
int Depth
Definition: ASTDiff.cpp:191
clang::format::FormatStyle::RTBS_TopLevel
@ RTBS_TopLevel
Always break after the return types of top-level functions.
Definition: Format.h:831
P
AnnotatingParser & P
Definition: TokenAnnotator.cpp:1478
clang::format::LT_ImportStatement
@ LT_ImportStatement
Definition: TokenAnnotator.h:26
clang::format::IsFunctionArgument
static bool IsFunctionArgument(const FormatToken &Tok)
Definition: TokenAnnotator.cpp:3719
clang::format::FormatToken::isTrailingComment
bool isTrailingComment() const
Definition: FormatToken.h:589
clang::format::isAllmanBrace
static bool isAllmanBrace(const FormatToken &Tok)
Definition: TokenAnnotator.cpp:3713
clang::format::AnnotatedLine::Type
LineType Type
Definition: TokenAnnotator.h:123
clang::format::BK_Block
@ BK_Block
Definition: FormatToken.h:138
clang::minimize_source_to_dependency_directives::pp_include
@ pp_include
Definition: DependencyDirectivesSourceMinimizer.h:35
clang::format::FormatStyle::BOS_All
@ BOS_All
Break before operators.
Definition: Format.h:1056
clang::format::TokenAnnotator::annotate
void annotate(AnnotatedLine &Line)
Definition: TokenAnnotator.cpp:2396
clang::format::FormatStyle::PointerAlignmentStyle
PointerAlignmentStyle
The &, && and * alignment style.
Definition: Format.h:2925
Line
const AnnotatedLine * Line
Definition: UsingDeclarationsSorter.cpp:68
clang::format::LT_Invalid
@ LT_Invalid
Definition: TokenAnnotator.h:25
ColonIsForRangeExpr
bool ColonIsForRangeExpr
Definition: TokenAnnotator.cpp:1459
clang::prec::PointerToMember
@ PointerToMember
Definition: OperatorPrecedence.h:42
clang::format::PPK_OnePerLine
@ PPK_OnePerLine
Definition: FormatToken.h:141
clang::format::FormatStyle::LK_Java
@ LK_Java
Should be used for Java.
Definition: Format.h:2661
clang::format::getTokenTypeName
const char * getTokenTypeName(TokenType Type)
Determines the name of a token type.
Definition: FormatToken.cpp:24
CaretFound
bool CaretFound
Definition: TokenAnnotator.cpp:1468
IsExpression
bool IsExpression
Definition: TokenAnnotator.cpp:1457
clang::format::FormatStyle::SBPO_Never
@ SBPO_Never
Never put a space before opening parentheses.
Definition: Format.h:3395
clang::format::FormatStyle::RTBS_AllDefinitions
@ RTBS_AllDefinitions
Always break after the return type of function definitions.
Definition: Format.h:846
clang::format::FormatStyle::BILS_BeforeComma
@ BILS_BeforeComma
Break inheritance list before the colon and commas, and align the commas with the colon.
Definition: Format.h:1958
clang::AccessSpecifier
AccessSpecifier
A C++ access specifier (public, private, protected), plus the special value "none" which means differ...
Definition: Specifiers.h:108
clang::format::FormatStyle::PAS_Middle
@ PAS_Middle
Align pointer in the middle.
Definition: Format.h:2940
clang::format::FormatStyle::BILS_AfterComma
@ BILS_AfterComma
Break inheritance list only after the commas.
Definition: Format.h:1973
clang::tok::TokenKind
TokenKind
Provides a simple uniform namespace for tokens from all C languages.
Definition: TokenKinds.h:25
clang::format::FormatStyle::SFS_InlineOnly
@ SFS_InlineOnly
Only merge functions defined inside a class.
Definition: Format.h:633
clang::format::LineType
LineType
Definition: TokenAnnotator.h:24
clang::format::getFirstNonComment
static const FormatToken * getFirstNonComment(const AnnotatedLine &Line)
Definition: TokenAnnotator.cpp:3736
clang::format::FormatStyle::RAS_Right
@ RAS_Right
Align reference to the right.
Definition: Format.h:3035
LongestObjCSelectorName
unsigned LongestObjCSelectorName
Definition: TokenAnnotator.cpp:1458
clang::format::LT_ObjCDecl
@ LT_ObjCDecl
Definition: TokenAnnotator.h:27
FirstStartOfName
FormatToken * FirstStartOfName
Definition: TokenAnnotator.cpp:1463
clang::format::FormatStyle::RTBS_TopLevelDefinitions
@ RTBS_TopLevelDefinitions
Always break after the return type of top-level definitions.
Definition: Format.h:858
clang::format::AnnotatedLine::Last
FormatToken * Last
Definition: TokenAnnotator.h:119
clang::format::FormatStyle::RAS_Pointer
@ RAS_Pointer
Align reference like PointerAlignment.
Definition: Format.h:3025
clang::format::isFunctionDeclarationName
static bool isFunctionDeclarationName(bool IsCpp, const FormatToken &Current, const AnnotatedLine &Line)
Definition: TokenAnnotator.cpp:2429
clang::format::FormatStyle::SLS_Inline
@ SLS_Inline
Merge lambda into a single line if argument of a function.
Definition: Format.h:759
IsForEachMacro
bool IsForEachMacro
Definition: TokenAnnotator.cpp:1469
clang::format::FormatToken::is
bool is(tok::TokenKind Kind) const
Definition: FormatToken.h:457
clang::format::FormatStyle::SAPQ_Before
@ SAPQ_Before
Ensure that there is a space before pointer qualifiers.
Definition: Format.h:3318
clang::format::FormatToken::isOneOf
bool isOneOf(A K1, B K2) const
Definition: FormatToken.h:469
clang::format::FormatStyle::PAS_Right
@ PAS_Right
Align pointer to the right.
Definition: Format.h:2935
clang::format::TokenAnnotator::setCommentLineLevels
void setCommentLineLevels(SmallVectorImpl< AnnotatedLine * > &Lines)
Adapts the indent levels of comment lines to the indent of the subsequent line.
Definition: TokenAnnotator.cpp:2353
clang::format::FormatStyle::BCIS_BeforeComma
@ BCIS_BeforeComma
Break constructor initializers before the colon and commas, and align the commas with the colon.
Definition: Format.h:1816
clang::format::isItAnEmptyLambdaAllowed
static bool isItAnEmptyLambdaAllowed(const FormatToken &Tok, FormatStyle::ShortLambdaStyle ShortLambdaOption)
Definition: TokenAnnotator.cpp:3725
clang::prec::Relational
@ Relational
Definition: OperatorPrecedence.h:37
clang::format::FormatStyle::BAS_BlockIndent
@ BAS_BlockIndent
Always break after an open bracket, if the parameters don't fit on a single line.
Definition: Format.h:97
BindingStrength
unsigned BindingStrength
Definition: TokenAnnotator.cpp:1456
clang::format::FormatStyle::PCIS_Never
@ PCIS_Never
Always put each constructor initializer on its own line.
Definition: Format.h:2200
clang
Definition: CalledOnceCheck.h:17
clang::format::FormatToken::Children
SmallVector< AnnotatedLine *, 1 > Children
If this token starts a block, this contains all the unwrapped lines in it.
Definition: FormatToken.h:451
clang::format::AnnotatedLine::endsWith
bool endsWith(Ts... Tokens) const
true if this line ends with the given tokens in reversed order, ignoring comments.
Definition: TokenAnnotator.h:90
clang::prec::LogicalOr
@ LogicalOr
Definition: OperatorPrecedence.h:31
clang::format::FormatStyle::LK_ObjC
@ LK_ObjC
Should be used for Objective-C, Objective-C++.
Definition: Format.h:2667
clang::format::LT_ArrayOfStructInitializer
@ LT_ArrayOfStructInitializer
Definition: TokenAnnotator.h:33
clang::prec::Level
Level
Definition: OperatorPrecedence.h:26
clang::format::FormatStyle::LS_Cpp11
@ LS_Cpp11
Parse and format as C++11.
Definition: Format.h:3755
clang::getBinOpPrecedence
prec::Level getBinOpPrecedence(tok::TokenKind Kind, bool GreaterThanIsOperator, bool CPlusPlus11)
Return the precedence of the specified binary operator token.
Definition: OperatorPrecedence.cpp:17
clang::format::FormatStyle::RAS_Middle
@ RAS_Middle
Align reference in the middle.
Definition: Format.h:3040
ColonIsDictLiteral
bool ColonIsDictLiteral
Definition: TokenAnnotator.cpp:1460
clang::format::FormatStyle::BFCS_After
@ BFCS_After
Add space after the : only (space may be added before if needed for AlignConsecutiveBitFields).
Definition: Format.h:3736
FormatToken.h
Parent
NodeId Parent
Definition: ASTDiff.cpp:192
clang::format::LT_Other
@ LT_Other
Definition: TokenAnnotator.h:30
clang::format::FormatStyle::BOS_None
@ BOS_None
Break after operators.
Definition: Format.h:1032
llvm::SmallVectorImpl
Definition: LLVM.h:39
clang::format::FormatToken::TotalLength
unsigned TotalLength
The total length of the unwrapped line up to and including this token.
Definition: FormatToken.h:369
Previous
StateNode * Previous
Definition: UnwrappedLineFormatter.cpp:1054
clang::format::PPK_Inconclusive
@ PPK_Inconclusive
Definition: FormatToken.h:141
clang::format::FormatToken::Next
FormatToken * Next
The next token in the unwrapped line.
Definition: FormatToken.h:435
clang::format::LT_ObjCMethodDecl
@ LT_ObjCMethodDecl
Definition: TokenAnnotator.h:28
CanBeExpression
bool CanBeExpression
Definition: TokenAnnotator.cpp:1464
clang::format::TokenType
TokenType
Determines the semantic type of a syntactic token, e.g.
Definition: FormatToken.h:127
InInheritanceList
bool InInheritanceList
Definition: TokenAnnotator.cpp:1467
clang::format::LT_VirtualFunctionDecl
@ LT_VirtualFunctionDecl
Definition: TokenAnnotator.h:32
clang::format::FormatStyle::SFS_None
@ SFS_None
Never merge functions into a single line.
Definition: Format.h:619
clang::format::FormatStyle::SLS_All
@ SLS_All
Merge all lambdas fitting on a single line.
Definition: Format.h:765
clang::format::FormatStyle::RTBS_None
@ RTBS_None
Break after return type automatically.
Definition: Format.h:802
clang::format::FormatStyle::PCIS_CurrentLine
@ PCIS_CurrentLine
Put all constructor initializers on the current line if they fit.
Definition: Format.h:2218
clang::format::FormatToken::MatchingParen
FormatToken * MatchingParen
If this is a bracket, this points to the matching one.
Definition: FormatToken.h:429
clang::minimize_source_to_dependency_directives::pp_elif
@ pp_elif
Definition: DependencyDirectivesSourceMinimizer.h:49
clang::format::LT_ObjCProperty
@ LT_ObjCProperty
Definition: TokenAnnotator.h:29
clang::format::FormatStyle::PPDIS_BeforeHash
@ PPDIS_BeforeHash
Indents directives before the hash.
Definition: Format.h:2465
ContextKind
tok::TokenKind ContextKind
Definition: TokenAnnotator.cpp:1455
FirstObjCSelectorName
FormatToken * FirstObjCSelectorName
Definition: TokenAnnotator.cpp:1462
Type
MatchType Type
Definition: ASTMatchFinder.cpp:70
clang::format::FormatToken::opensScope
bool opensScope() const
Returns whether Tok is ([{ or an opening < of a template or in protos.
Definition: FormatToken.h:542
InStructArrayInitializer
bool InStructArrayInitializer
Definition: TokenAnnotator.cpp:1472