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