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