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