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