Bug Summary

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