Bug Summary

File:build/source/clang/lib/Format/TokenAnnotator.cpp
Warning:line 2753, column 5
Called C++ object pointer is null

Annotated Source Code

Press '?' to see keyboard shortcuts

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