Bug Summary

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