| 1 | //===--- TokenAnnotator.cpp - Format C++ code -----------------------------===// |
| 2 | // |
| 3 | // Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions. |
| 4 | // See https://llvm.org/LICENSE.txt for license information. |
| 5 | // SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception |
| 6 | // |
| 7 | //===----------------------------------------------------------------------===// |
| 8 | /// |
| 9 | /// \file |
| 10 | /// This file implements a token annotator, i.e. creates |
| 11 | /// \c AnnotatedTokens out of \c FormatTokens with required extra information. |
| 12 | /// |
| 13 | //===----------------------------------------------------------------------===// |
| 14 | |
| 15 | #include "TokenAnnotator.h" |
| 16 | #include "FormatToken.h" |
| 17 | #include "clang/Basic/SourceManager.h" |
| 18 | #include "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 | namespace { |
| 27 | |
| 28 | /// Returns \c true if the token can be used as an identifier in |
| 29 | /// an Objective-C \c @selector, \c false otherwise. |
| 30 | /// |
| 31 | /// Because getFormattingLangOpts() always lexes source code as |
| 32 | /// Objective-C++, C++ keywords like \c new and \c delete are |
| 33 | /// lexed as tok::kw_*, not tok::identifier, even for Objective-C. |
| 34 | /// |
| 35 | /// For Objective-C and Objective-C++, both identifiers and keywords |
| 36 | /// are valid inside @selector(...) (or a macro which |
| 37 | /// invokes @selector(...)). So, we allow treat any identifier or |
| 38 | /// keyword as a potential Objective-C selector component. |
| 39 | static bool canBeObjCSelectorComponent(const FormatToken &Tok) { |
| 40 | return Tok.Tok.getIdentifierInfo() != nullptr; |
| 41 | } |
| 42 | |
| 43 | /// A parser that gathers additional information about tokens. |
| 44 | /// |
| 45 | /// The \c TokenAnnotator tries to match parenthesis and square brakets and |
| 46 | /// store a parenthesis levels. It also tries to resolve matching "<" and ">" |
| 47 | /// into template parameter lists. |
| 48 | class AnnotatingParser { |
| 49 | public: |
| 50 | AnnotatingParser(const FormatStyle &Style, AnnotatedLine &Line, |
| 51 | const AdditionalKeywords &Keywords) |
| 52 | : Style(Style), Line(Line), CurrentToken(Line.First), AutoFound(false), |
| 53 | Keywords(Keywords) { |
| 54 | Contexts.push_back(Context(tok::unknown, 1, /*IsExpression=*/false)); |
| 55 | resetTokenMetadata(CurrentToken); |
| 56 | } |
| 57 | |
| 58 | private: |
| 59 | bool parseAngle() { |
| 60 | if (!CurrentToken || !CurrentToken->Previous) |
| 61 | return false; |
| 62 | if (NonTemplateLess.count(CurrentToken->Previous)) |
| 63 | return false; |
| 64 | |
| 65 | const FormatToken &Previous = *CurrentToken->Previous; // The '<'. |
| 66 | if (Previous.Previous) { |
| 67 | if (Previous.Previous->Tok.isLiteral()) |
| 68 | return false; |
| 69 | if (Previous.Previous->is(tok::r_paren) && Contexts.size() > 1 && |
| 70 | (!Previous.Previous->MatchingParen || |
| 71 | !Previous.Previous->MatchingParen->is(TT_OverloadedOperatorLParen))) |
| 72 | return false; |
| 73 | } |
| 74 | |
| 75 | FormatToken *Left = CurrentToken->Previous; |
| 76 | Left->ParentBracket = Contexts.back().ContextKind; |
| 77 | ScopedContextCreator ContextCreator(*this, tok::less, 12); |
| 78 | |
| 79 | // If this angle is in the context of an expression, we need to be more |
| 80 | // hesitant to detect it as opening template parameters. |
| 81 | bool InExprContext = Contexts.back().IsExpression; |
| 82 | |
| 83 | Contexts.back().IsExpression = false; |
| 84 | // If there's a template keyword before the opening angle bracket, this is a |
| 85 | // template parameter, not an argument. |
| 86 | Contexts.back().InTemplateArgument = |
| 87 | Left->Previous && Left->Previous->Tok.isNot(tok::kw_template); |
| 88 | |
| 89 | if (Style.Language == FormatStyle::LK_Java && |
| 90 | CurrentToken->is(tok::question)) |
| 91 | next(); |
| 92 | |
| 93 | while (CurrentToken) { |
| 94 | if (CurrentToken->is(tok::greater)) { |
| 95 | Left->MatchingParen = CurrentToken; |
| 96 | CurrentToken->MatchingParen = Left; |
| 97 | // In TT_Proto, we must distignuish between: |
| 98 | // map<key, value> |
| 99 | // msg < item: data > |
| 100 | // msg: < item: data > |
| 101 | // In TT_TextProto, map<key, value> does not occur. |
| 102 | if (Style.Language == FormatStyle::LK_TextProto || |
| 103 | (Style.Language == FormatStyle::LK_Proto && Left->Previous && |
| 104 | Left->Previous->isOneOf(TT_SelectorName, TT_DictLiteral))) |
| 105 | CurrentToken->Type = TT_DictLiteral; |
| 106 | else |
| 107 | CurrentToken->Type = TT_TemplateCloser; |
| 108 | next(); |
| 109 | return true; |
| 110 | } |
| 111 | if (CurrentToken->is(tok::question) && |
| 112 | Style.Language == FormatStyle::LK_Java) { |
| 113 | next(); |
| 114 | continue; |
| 115 | } |
| 116 | if (CurrentToken->isOneOf(tok::r_paren, tok::r_square, tok::r_brace) || |
| 117 | (CurrentToken->isOneOf(tok::colon, tok::question) && InExprContext && |
| 118 | Style.Language != FormatStyle::LK_Proto && |
| 119 | Style.Language != FormatStyle::LK_TextProto)) |
| 120 | return false; |
| 121 | // If a && or || is found and interpreted as a binary operator, this set |
| 122 | // of angles is likely part of something like "a < b && c > d". If the |
| 123 | // angles are inside an expression, the ||/&& might also be a binary |
| 124 | // operator that was misinterpreted because we are parsing template |
| 125 | // parameters. |
| 126 | // FIXME: This is getting out of hand, write a decent parser. |
| 127 | if (CurrentToken->Previous->isOneOf(tok::pipepipe, tok::ampamp) && |
| 128 | CurrentToken->Previous->is(TT_BinaryOperator) && |
| 129 | Contexts[Contexts.size() - 2].IsExpression && |
| 130 | !Line.startsWith(tok::kw_template)) |
| 131 | return false; |
| 132 | updateParameterCount(Left, CurrentToken); |
| 133 | if (Style.Language == FormatStyle::LK_Proto) { |
| 134 | if (FormatToken *Previous = CurrentToken->getPreviousNonComment()) { |
| 135 | if (CurrentToken->is(tok::colon) || |
| 136 | (CurrentToken->isOneOf(tok::l_brace, tok::less) && |
| 137 | Previous->isNot(tok::colon))) |
| 138 | Previous->Type = TT_SelectorName; |
| 139 | } |
| 140 | } |
| 141 | if (!consumeToken()) |
| 142 | return false; |
| 143 | } |
| 144 | return false; |
| 145 | } |
| 146 | |
| 147 | bool parseParens(bool LookForDecls = false) { |
| 148 | if (!CurrentToken) |
| 149 | return false; |
| 150 | FormatToken *Left = CurrentToken->Previous; |
| 151 | Left->ParentBracket = Contexts.back().ContextKind; |
| 152 | ScopedContextCreator ContextCreator(*this, tok::l_paren, 1); |
| 153 | |
| 154 | // FIXME: This is a bit of a hack. Do better. |
| 155 | Contexts.back().ColonIsForRangeExpr = |
| 156 | Contexts.size() == 2 && Contexts[0].ColonIsForRangeExpr; |
| 157 | |
| 158 | bool StartsObjCMethodExpr = false; |
| 159 | if (FormatToken *MaybeSel = Left->Previous) { |
| 160 | // @selector( starts a selector. |
| 161 | if (MaybeSel->isObjCAtKeyword(tok::objc_selector) && MaybeSel->Previous && |
| 162 | MaybeSel->Previous->is(tok::at)) { |
| 163 | StartsObjCMethodExpr = true; |
| 164 | } |
| 165 | } |
| 166 | |
| 167 | if (Left->is(TT_OverloadedOperatorLParen)) { |
| 168 | Contexts.back().IsExpression = false; |
| 169 | } else if (Style.Language == FormatStyle::LK_JavaScript && |
| 170 | (Line.startsWith(Keywords.kw_type, tok::identifier) || |
| 171 | Line.startsWith(tok::kw_export, Keywords.kw_type, |
| 172 | tok::identifier))) { |
| 173 | // type X = (...); |
| 174 | // export type X = (...); |
| 175 | Contexts.back().IsExpression = false; |
| 176 | } else if (Left->Previous && |
| 177 | (Left->Previous->isOneOf(tok::kw_static_assert, tok::kw_decltype, |
| 178 | tok::kw_if, tok::kw_while, tok::l_paren, |
| 179 | tok::comma) || |
| 180 | Left->Previous->endsSequence(tok::kw_constexpr, tok::kw_if) || |
| 181 | Left->Previous->is(TT_BinaryOperator))) { |
| 182 | // static_assert, if and while usually contain expressions. |
| 183 | Contexts.back().IsExpression = true; |
| 184 | } else if (Style.Language == FormatStyle::LK_JavaScript && Left->Previous && |
| 185 | (Left->Previous->is(Keywords.kw_function) || |
| 186 | (Left->Previous->endsSequence(tok::identifier, |
| 187 | Keywords.kw_function)))) { |
| 188 | // function(...) or function f(...) |
| 189 | Contexts.back().IsExpression = false; |
| 190 | } else if (Style.Language == FormatStyle::LK_JavaScript && Left->Previous && |
| 191 | Left->Previous->is(TT_JsTypeColon)) { |
| 192 | // let x: (SomeType); |
| 193 | Contexts.back().IsExpression = false; |
| 194 | } else if (Left->Previous && Left->Previous->is(tok::r_square) && |
| 195 | Left->Previous->MatchingParen && |
| 196 | Left->Previous->MatchingParen->is(TT_LambdaLSquare)) { |
| 197 | // This is a parameter list of a lambda expression. |
| 198 | Contexts.back().IsExpression = false; |
| 199 | } else if (Line.InPPDirective && |
| 200 | (!Left->Previous || !Left->Previous->is(tok::identifier))) { |
| 201 | Contexts.back().IsExpression = true; |
| 202 | } else if (Contexts[Contexts.size() - 2].CaretFound) { |
| 203 | // This is the parameter list of an ObjC block. |
| 204 | Contexts.back().IsExpression = false; |
| 205 | } else if (Left->Previous && Left->Previous->is(tok::kw___attribute)) { |
| 206 | Left->Type = TT_AttributeParen; |
| 207 | } else if (Left->Previous && Left->Previous->is(TT_ForEachMacro)) { |
| 208 | // The first argument to a foreach macro is a declaration. |
| 209 | Contexts.back().IsForEachMacro = true; |
| 210 | Contexts.back().IsExpression = false; |
| 211 | } else if (Left->Previous && Left->Previous->MatchingParen && |
| 212 | Left->Previous->MatchingParen->is(TT_ObjCBlockLParen)) { |
| 213 | Contexts.back().IsExpression = false; |
| 214 | } else if (!Line.MustBeDeclaration && !Line.InPPDirective) { |
| 215 | bool IsForOrCatch = |
| 216 | Left->Previous && Left->Previous->isOneOf(tok::kw_for, tok::kw_catch); |
| 217 | Contexts.back().IsExpression = !IsForOrCatch; |
| 218 | } |
| 219 | |
| 220 | if (StartsObjCMethodExpr) { |
| 221 | Contexts.back().ColonIsObjCMethodExpr = true; |
| 222 | Left->Type = TT_ObjCMethodExpr; |
| 223 | } |
| 224 | |
| 225 | // MightBeFunctionType and ProbablyFunctionType are used for |
| 226 | // function pointer and reference types as well as Objective-C |
| 227 | // block types: |
| 228 | // |
| 229 | // void (*FunctionPointer)(void); |
| 230 | // void (&FunctionReference)(void); |
| 231 | // void (^ObjCBlock)(void); |
| 232 | bool MightBeFunctionType = !Contexts[Contexts.size() - 2].IsExpression; |
| 233 | bool ProbablyFunctionType = |
| 234 | CurrentToken->isOneOf(tok::star, tok::amp, tok::caret); |
| 235 | bool HasMultipleLines = false; |
| 236 | bool HasMultipleParametersOnALine = false; |
| 237 | bool MightBeObjCForRangeLoop = |
| 238 | Left->Previous && Left->Previous->is(tok::kw_for); |
| 239 | FormatToken *PossibleObjCForInToken = nullptr; |
| 240 | while (CurrentToken) { |
| 241 | // LookForDecls is set when "if (" has been seen. Check for |
| 242 | // 'identifier' '*' 'identifier' followed by not '=' -- this |
| 243 | // '*' has to be a binary operator but determineStarAmpUsage() will |
| 244 | // categorize it as an unary operator, so set the right type here. |
| 245 | if (LookForDecls && CurrentToken->Next) { |
| 246 | FormatToken *Prev = CurrentToken->getPreviousNonComment(); |
| 247 | if (Prev) { |
| 248 | FormatToken *PrevPrev = Prev->getPreviousNonComment(); |
| 249 | FormatToken *Next = CurrentToken->Next; |
| 250 | if (PrevPrev && PrevPrev->is(tok::identifier) && |
| 251 | Prev->isOneOf(tok::star, tok::amp, tok::ampamp) && |
| 252 | CurrentToken->is(tok::identifier) && Next->isNot(tok::equal)) { |
| 253 | Prev->Type = TT_BinaryOperator; |
| 254 | LookForDecls = false; |
| 255 | } |
| 256 | } |
| 257 | } |
| 258 | |
| 259 | if (CurrentToken->Previous->is(TT_PointerOrReference) && |
| 260 | CurrentToken->Previous->Previous->isOneOf(tok::l_paren, |
| 261 | tok::coloncolon)) |
| 262 | ProbablyFunctionType = true; |
| 263 | if (CurrentToken->is(tok::comma)) |
| 264 | MightBeFunctionType = false; |
| 265 | if (CurrentToken->Previous->is(TT_BinaryOperator)) |
| 266 | Contexts.back().IsExpression = true; |
| 267 | if (CurrentToken->is(tok::r_paren)) { |
| 268 | if (MightBeFunctionType && ProbablyFunctionType && CurrentToken->Next && |
| 269 | (CurrentToken->Next->is(tok::l_paren) || |
| 270 | (CurrentToken->Next->is(tok::l_square) && Line.MustBeDeclaration))) |
| 271 | Left->Type = Left->Next->is(tok::caret) ? TT_ObjCBlockLParen |
| 272 | : TT_FunctionTypeLParen; |
| 273 | Left->MatchingParen = CurrentToken; |
| 274 | CurrentToken->MatchingParen = Left; |
| 275 | |
| 276 | if (CurrentToken->Next && CurrentToken->Next->is(tok::l_brace) && |
| 277 | Left->Previous && Left->Previous->is(tok::l_paren)) { |
| 278 | // Detect the case where macros are used to generate lambdas or |
| 279 | // function bodies, e.g.: |
| 280 | // auto my_lambda = MARCO((Type *type, int i) { .. body .. }); |
| 281 | for (FormatToken *Tok = Left; Tok != CurrentToken; Tok = Tok->Next) { |
| 282 | if (Tok->is(TT_BinaryOperator) && |
| 283 | Tok->isOneOf(tok::star, tok::amp, tok::ampamp)) |
| 284 | Tok->Type = TT_PointerOrReference; |
| 285 | } |
| 286 | } |
| 287 | |
| 288 | if (StartsObjCMethodExpr) { |
| 289 | CurrentToken->Type = TT_ObjCMethodExpr; |
| 290 | if (Contexts.back().FirstObjCSelectorName) { |
| 291 | Contexts.back().FirstObjCSelectorName->LongestObjCSelectorName = |
| 292 | Contexts.back().LongestObjCSelectorName; |
| 293 | } |
| 294 | } |
| 295 | |
| 296 | if (Left->is(TT_AttributeParen)) |
| 297 | CurrentToken->Type = TT_AttributeParen; |
| 298 | if (Left->Previous && Left->Previous->is(TT_JavaAnnotation)) |
| 299 | CurrentToken->Type = TT_JavaAnnotation; |
| 300 | if (Left->Previous && Left->Previous->is(TT_LeadingJavaAnnotation)) |
| 301 | CurrentToken->Type = TT_LeadingJavaAnnotation; |
| 302 | if (Left->Previous && Left->Previous->is(TT_AttributeSquare)) |
| 303 | CurrentToken->Type = TT_AttributeSquare; |
| 304 | |
| 305 | if (!HasMultipleLines) |
| 306 | Left->PackingKind = PPK_Inconclusive; |
| 307 | else if (HasMultipleParametersOnALine) |
| 308 | Left->PackingKind = PPK_BinPacked; |
| 309 | else |
| 310 | Left->PackingKind = PPK_OnePerLine; |
| 311 | |
| 312 | next(); |
| 313 | return true; |
| 314 | } |
| 315 | if (CurrentToken->isOneOf(tok::r_square, tok::r_brace)) |
| 316 | return false; |
| 317 | |
| 318 | if (CurrentToken->is(tok::l_brace)) |
| 319 | Left->Type = TT_Unknown; // Not TT_ObjCBlockLParen |
| 320 | if (CurrentToken->is(tok::comma) && CurrentToken->Next && |
| 321 | !CurrentToken->Next->HasUnescapedNewline && |
| 322 | !CurrentToken->Next->isTrailingComment()) |
| 323 | HasMultipleParametersOnALine = true; |
| 324 | if ((CurrentToken->Previous->isOneOf(tok::kw_const, tok::kw_auto) || |
| 325 | CurrentToken->Previous->isSimpleTypeSpecifier()) && |
| 326 | !CurrentToken->is(tok::l_brace)) |
| 327 | Contexts.back().IsExpression = false; |
| 328 | if (CurrentToken->isOneOf(tok::semi, tok::colon)) { |
| 329 | MightBeObjCForRangeLoop = false; |
| 330 | if (PossibleObjCForInToken) { |
| 331 | PossibleObjCForInToken->Type = TT_Unknown; |
| 332 | PossibleObjCForInToken = nullptr; |
| 333 | } |
| 334 | } |
| 335 | if (MightBeObjCForRangeLoop && CurrentToken->is(Keywords.kw_in)) { |
| 336 | PossibleObjCForInToken = CurrentToken; |
| 337 | PossibleObjCForInToken->Type = TT_ObjCForIn; |
| 338 | } |
| 339 | // When we discover a 'new', we set CanBeExpression to 'false' in order to |
| 340 | // parse the type correctly. Reset that after a comma. |
| 341 | if (CurrentToken->is(tok::comma)) |
| 342 | Contexts.back().CanBeExpression = true; |
| 343 | |
| 344 | FormatToken *Tok = CurrentToken; |
| 345 | if (!consumeToken()) |
| 346 | return false; |
| 347 | updateParameterCount(Left, Tok); |
| 348 | if (CurrentToken && CurrentToken->HasUnescapedNewline) |
| 349 | HasMultipleLines = true; |
| 350 | } |
| 351 | return false; |
| 352 | } |
| 353 | |
| 354 | bool isCSharpAttributeSpecifier(const FormatToken &Tok) { |
| 355 | if (!Style.isCSharp()) |
| 356 | return false; |
| 357 | |
| 358 | const FormatToken *AttrTok = Tok.Next; |
| 359 | if (!AttrTok) |
| 360 | return false; |
| 361 | |
| 362 | // Just an empty declaration e.g. string []. |
| 363 | if (AttrTok->is(tok::r_square)) |
| 364 | return false; |
| 365 | |
| 366 | // Move along the tokens inbetween the '[' and ']' e.g. [STAThread]. |
| 367 | while (AttrTok && AttrTok->isNot(tok::r_square)) { |
| 368 | AttrTok = AttrTok->Next; |
| 369 | } |
| 370 | |
| 371 | if (!AttrTok) |
| 372 | return false; |
| 373 | |
| 374 | // Move past the end of ']'. |
| 375 | AttrTok = AttrTok->Next; |
| 376 | if (!AttrTok) |
| 377 | return false; |
| 378 | |
| 379 | // Limit this to being an access modifier that follows. |
| 380 | if (AttrTok->isOneOf(tok::kw_public, tok::kw_private, tok::kw_protected, |
| 381 | tok::kw_class, tok::kw_static, tok::l_square, |
| 382 | Keywords.kw_internal)) { |
| 383 | return true; |
| 384 | } |
| 385 | return false; |
| 386 | } |
| 387 | |
| 388 | bool isCpp11AttributeSpecifier(const FormatToken &Tok) { |
| 389 | if (!Style.isCpp() || !Tok.startsSequence(tok::l_square, tok::l_square)) |
| 390 | return false; |
| 391 | const FormatToken *AttrTok = Tok.Next->Next; |
| 392 | if (!AttrTok) |
| 393 | return false; |
| 394 | // C++17 '[[using ns: foo, bar(baz, blech)]]' |
| 395 | // We assume nobody will name an ObjC variable 'using'. |
| 396 | if (AttrTok->startsSequence(tok::kw_using, tok::identifier, tok::colon)) |
| 397 | return true; |
| 398 | if (AttrTok->isNot(tok::identifier)) |
| 399 | return false; |
| 400 | while (AttrTok && !AttrTok->startsSequence(tok::r_square, tok::r_square)) { |
| 401 | // ObjC message send. We assume nobody will use : in a C++11 attribute |
| 402 | // specifier parameter, although this is technically valid: |
| 403 | // [[foo(:)]] |
| 404 | if (AttrTok->is(tok::colon) || |
| 405 | AttrTok->startsSequence(tok::identifier, tok::identifier) || |
| 406 | AttrTok->startsSequence(tok::r_paren, tok::identifier)) |
| 407 | return false; |
| 408 | if (AttrTok->is(tok::ellipsis)) |
| 409 | return true; |
| 410 | AttrTok = AttrTok->Next; |
| 411 | } |
| 412 | return AttrTok && AttrTok->startsSequence(tok::r_square, tok::r_square); |
| 413 | } |
| 414 | |
| 415 | bool parseSquare() { |
| 416 | if (!CurrentToken) |
| 417 | return false; |
| 418 | |
| 419 | // A '[' could be an index subscript (after an identifier or after |
| 420 | // ')' or ']'), it could be the start of an Objective-C method |
| 421 | // expression, it could the start of an Objective-C array literal, |
| 422 | // or it could be a C++ attribute specifier [[foo::bar]]. |
| 423 | FormatToken *Left = CurrentToken->Previous; |
| 424 | Left->ParentBracket = Contexts.back().ContextKind; |
| 425 | FormatToken *Parent = Left->getPreviousNonComment(); |
| 426 | |
| 427 | // Cases where '>' is followed by '['. |
| 428 | // In C++, this can happen either in array of templates (foo<int>[10]) |
| 429 | // or when array is a nested template type (unique_ptr<type1<type2>[]>). |
| 430 | bool CppArrayTemplates = |
| 431 | Style.isCpp() && Parent && Parent->is(TT_TemplateCloser) && |
| 432 | (Contexts.back().CanBeExpression || Contexts.back().IsExpression || |
| 433 | Contexts.back().InTemplateArgument); |
| 434 | |
| 435 | bool IsCpp11AttributeSpecifier = isCpp11AttributeSpecifier(*Left) || |
| 436 | Contexts.back().InCpp11AttributeSpecifier; |
| 437 | |
| 438 | // Treat C# Attributes [STAThread] much like C++ attributes [[...]]. |
| 439 | bool IsCSharp11AttributeSpecifier = |
| 440 | isCSharpAttributeSpecifier(*Left) || |
| 441 | Contexts.back().InCSharpAttributeSpecifier; |
| 442 | |
| 443 | bool InsideInlineASM = Line.startsWith(tok::kw_asm); |
| 444 | bool IsCppStructuredBinding = Left->isCppStructuredBinding(Style); |
| 445 | bool StartsObjCMethodExpr = |
| 446 | !IsCppStructuredBinding && !InsideInlineASM && !CppArrayTemplates && |
| 447 | Style.isCpp() && !IsCpp11AttributeSpecifier && |
| 448 | Contexts.back().CanBeExpression && Left->isNot(TT_LambdaLSquare) && |
| 449 | !CurrentToken->isOneOf(tok::l_brace, tok::r_square) && |
| 450 | (!Parent || |
| 451 | Parent->isOneOf(tok::colon, tok::l_square, tok::l_paren, |
| 452 | tok::kw_return, tok::kw_throw) || |
| 453 | Parent->isUnaryOperator() || |
| 454 | // FIXME(bug 36976): ObjC return types shouldn't use TT_CastRParen. |
| 455 | Parent->isOneOf(TT_ObjCForIn, TT_CastRParen) || |
| 456 | (getBinOpPrecedence(Parent->Tok.getKind(), true, true) > |
| 457 | prec::Unknown)); |
| 458 | bool ColonFound = false; |
| 459 | |
| 460 | unsigned BindingIncrease = 1; |
| 461 | if (IsCppStructuredBinding) { |
| 462 | Left->Type = TT_StructuredBindingLSquare; |
| 463 | } else if (Left->is(TT_Unknown)) { |
| 464 | if (StartsObjCMethodExpr) { |
| 465 | Left->Type = TT_ObjCMethodExpr; |
| 466 | } else if (IsCpp11AttributeSpecifier) { |
| 467 | Left->Type = TT_AttributeSquare; |
| 468 | } else if (Style.Language == FormatStyle::LK_JavaScript && Parent && |
| 469 | Contexts.back().ContextKind == tok::l_brace && |
| 470 | Parent->isOneOf(tok::l_brace, tok::comma)) { |
| 471 | Left->Type = TT_JsComputedPropertyName; |
| 472 | } else if (Style.isCpp() && Contexts.back().ContextKind == tok::l_brace && |
| 473 | Parent && Parent->isOneOf(tok::l_brace, tok::comma)) { |
| 474 | Left->Type = TT_DesignatedInitializerLSquare; |
| 475 | } else if (CurrentToken->is(tok::r_square) && Parent && |
| 476 | Parent->is(TT_TemplateCloser)) { |
| 477 | Left->Type = TT_ArraySubscriptLSquare; |
| 478 | } else if (Style.Language == FormatStyle::LK_Proto || |
| 479 | Style.Language == FormatStyle::LK_TextProto) { |
| 480 | // Square braces in LK_Proto can either be message field attributes: |
| 481 | // |
| 482 | // optional Aaa aaa = 1 [ |
| 483 | // (aaa) = aaa |
| 484 | // ]; |
| 485 | // |
| 486 | // extensions 123 [ |
| 487 | // (aaa) = aaa |
| 488 | // ]; |
| 489 | // |
| 490 | // or text proto extensions (in options): |
| 491 | // |
| 492 | // option (Aaa.options) = { |
| 493 | // [type.type/type] { |
| 494 | // key: value |
| 495 | // } |
| 496 | // } |
| 497 | // |
| 498 | // or repeated fields (in options): |
| 499 | // |
| 500 | // option (Aaa.options) = { |
| 501 | // keys: [ 1, 2, 3 ] |
| 502 | // } |
| 503 | // |
| 504 | // In the first and the third case we want to spread the contents inside |
| 505 | // the square braces; in the second we want to keep them inline. |
| 506 | Left->Type = TT_ArrayInitializerLSquare; |
| 507 | if (!Left->endsSequence(tok::l_square, tok::numeric_constant, |
| 508 | tok::equal) && |
| 509 | !Left->endsSequence(tok::l_square, tok::numeric_constant, |
| 510 | tok::identifier) && |
| 511 | !Left->endsSequence(tok::l_square, tok::colon, TT_SelectorName)) { |
| 512 | Left->Type = TT_ProtoExtensionLSquare; |
| 513 | BindingIncrease = 10; |
| 514 | } |
| 515 | } else if (!CppArrayTemplates && Parent && |
| 516 | Parent->isOneOf(TT_BinaryOperator, TT_TemplateCloser, tok::at, |
| 517 | tok::comma, tok::l_paren, tok::l_square, |
| 518 | tok::question, tok::colon, tok::kw_return, |
| 519 | // Should only be relevant to JavaScript: |
| 520 | tok::kw_default)) { |
| 521 | Left->Type = TT_ArrayInitializerLSquare; |
| 522 | } else if (IsCSharp11AttributeSpecifier) { |
| 523 | Left->Type = TT_AttributeSquare; |
| 524 | } else { |
| 525 | BindingIncrease = 10; |
| 526 | Left->Type = TT_ArraySubscriptLSquare; |
| 527 | } |
| 528 | } |
| 529 | |
| 530 | ScopedContextCreator ContextCreator(*this, tok::l_square, BindingIncrease); |
| 531 | Contexts.back().IsExpression = true; |
| 532 | if (Style.Language == FormatStyle::LK_JavaScript && Parent && |
| 533 | Parent->is(TT_JsTypeColon)) |
| 534 | Contexts.back().IsExpression = false; |
| 535 | |
| 536 | Contexts.back().ColonIsObjCMethodExpr = StartsObjCMethodExpr; |
| 537 | Contexts.back().InCpp11AttributeSpecifier = IsCpp11AttributeSpecifier; |
| 538 | Contexts.back().InCSharpAttributeSpecifier = IsCSharp11AttributeSpecifier; |
| 539 | |
| 540 | while (CurrentToken) { |
| 541 | if (CurrentToken->is(tok::r_square)) { |
| 542 | if (IsCpp11AttributeSpecifier) |
| 543 | CurrentToken->Type = TT_AttributeSquare; |
| 544 | if (IsCSharp11AttributeSpecifier) |
| 545 | CurrentToken->Type = TT_AttributeSquare; |
| 546 | else if (((CurrentToken->Next && |
| 547 | CurrentToken->Next->is(tok::l_paren)) || |
| 548 | (CurrentToken->Previous && |
| 549 | CurrentToken->Previous->Previous == Left)) && |
| 550 | Left->is(TT_ObjCMethodExpr)) { |
| 551 | // An ObjC method call is rarely followed by an open parenthesis. It |
| 552 | // also can't be composed of just one token, unless it's a macro that |
| 553 | // will be expanded to more tokens. |
| 554 | // FIXME: Do we incorrectly label ":" with this? |
| 555 | StartsObjCMethodExpr = false; |
| 556 | Left->Type = TT_Unknown; |
| 557 | } |
| 558 | if (StartsObjCMethodExpr && CurrentToken->Previous != Left) { |
| 559 | CurrentToken->Type = TT_ObjCMethodExpr; |
| 560 | // If we haven't seen a colon yet, make sure the last identifier |
| 561 | // before the r_square is tagged as a selector name component. |
| 562 | if (!ColonFound && CurrentToken->Previous && |
| 563 | CurrentToken->Previous->is(TT_Unknown) && |
| 564 | canBeObjCSelectorComponent(*CurrentToken->Previous)) |
| 565 | CurrentToken->Previous->Type = TT_SelectorName; |
| 566 | // determineStarAmpUsage() thinks that '*' '[' is allocating an |
| 567 | // array of pointers, but if '[' starts a selector then '*' is a |
| 568 | // binary operator. |
| 569 | if (Parent && Parent->is(TT_PointerOrReference)) |
| 570 | Parent->Type = TT_BinaryOperator; |
| 571 | } |
| 572 | // An arrow after an ObjC method expression is not a lambda arrow. |
| 573 | if (CurrentToken->Type == TT_ObjCMethodExpr && CurrentToken->Next && |
| 574 | CurrentToken->Next->is(TT_LambdaArrow)) |
| 575 | CurrentToken->Next->Type = TT_Unknown; |
| 576 | Left->MatchingParen = CurrentToken; |
| 577 | CurrentToken->MatchingParen = Left; |
| 578 | // FirstObjCSelectorName is set when a colon is found. This does |
| 579 | // not work, however, when the method has no parameters. |
| 580 | // Here, we set FirstObjCSelectorName when the end of the method call is |
| 581 | // reached, in case it was not set already. |
| 582 | if (!Contexts.back().FirstObjCSelectorName) { |
| 583 | FormatToken *Previous = CurrentToken->getPreviousNonComment(); |
| 584 | if (Previous && Previous->is(TT_SelectorName)) { |
| 585 | Previous->ObjCSelectorNameParts = 1; |
| 586 | Contexts.back().FirstObjCSelectorName = Previous; |
| 587 | } |
| 588 | } else { |
| 589 | Left->ParameterCount = |
| 590 | Contexts.back().FirstObjCSelectorName->ObjCSelectorNameParts; |
| 591 | } |
| 592 | if (Contexts.back().FirstObjCSelectorName) { |
| 593 | Contexts.back().FirstObjCSelectorName->LongestObjCSelectorName = |
| 594 | Contexts.back().LongestObjCSelectorName; |
| 595 | if (Left->BlockParameterCount > 1) |
| 596 | Contexts.back().FirstObjCSelectorName->LongestObjCSelectorName = 0; |
| 597 | } |
| 598 | next(); |
| 599 | return true; |
| 600 | } |
| 601 | if (CurrentToken->isOneOf(tok::r_paren, tok::r_brace)) |
| 602 | return false; |
| 603 | if (CurrentToken->is(tok::colon)) { |
| 604 | if (IsCpp11AttributeSpecifier && |
| 605 | CurrentToken->endsSequence(tok::colon, tok::identifier, |
| 606 | tok::kw_using)) { |
| 607 | // Remember that this is a [[using ns: foo]] C++ attribute, so we |
| 608 | // don't add a space before the colon (unlike other colons). |
| 609 | CurrentToken->Type = TT_AttributeColon; |
| 610 | } else if (Left->isOneOf(TT_ArraySubscriptLSquare, |
| 611 | TT_DesignatedInitializerLSquare)) { |
| 612 | Left->Type = TT_ObjCMethodExpr; |
| 613 | StartsObjCMethodExpr = true; |
| 614 | Contexts.back().ColonIsObjCMethodExpr = true; |
| 615 | if (Parent && Parent->is(tok::r_paren)) |
| 616 | // FIXME(bug 36976): ObjC return types shouldn't use TT_CastRParen. |
| 617 | Parent->Type = TT_CastRParen; |
| 618 | } |
| 619 | ColonFound = true; |
| 620 | } |
| 621 | if (CurrentToken->is(tok::comma) && Left->is(TT_ObjCMethodExpr) && |
| 622 | !ColonFound) |
| 623 | Left->Type = TT_ArrayInitializerLSquare; |
| 624 | FormatToken *Tok = CurrentToken; |
| 625 | if (!consumeToken()) |
| 626 | return false; |
| 627 | updateParameterCount(Left, Tok); |
| 628 | } |
| 629 | return false; |
| 630 | } |
| 631 | |
| 632 | bool parseBrace() { |
| 633 | if (CurrentToken) { |
| 634 | FormatToken *Left = CurrentToken->Previous; |
| 635 | Left->ParentBracket = Contexts.back().ContextKind; |
| 636 | |
| 637 | if (Contexts.back().CaretFound) |
| 638 | Left->Type = TT_ObjCBlockLBrace; |
| 639 | Contexts.back().CaretFound = false; |
| 640 | |
| 641 | ScopedContextCreator ContextCreator(*this, tok::l_brace, 1); |
| 642 | Contexts.back().ColonIsDictLiteral = true; |
| 643 | if (Left->BlockKind == BK_BracedInit) |
| 644 | Contexts.back().IsExpression = true; |
| 645 | if (Style.Language == FormatStyle::LK_JavaScript && Left->Previous && |
| 646 | Left->Previous->is(TT_JsTypeColon)) |
| 647 | Contexts.back().IsExpression = false; |
| 648 | |
| 649 | while (CurrentToken) { |
| 650 | if (CurrentToken->is(tok::r_brace)) { |
| 651 | Left->MatchingParen = CurrentToken; |
| 652 | CurrentToken->MatchingParen = Left; |
| 653 | next(); |
| 654 | return true; |
| 655 | } |
| 656 | if (CurrentToken->isOneOf(tok::r_paren, tok::r_square)) |
| 657 | return false; |
| 658 | updateParameterCount(Left, CurrentToken); |
| 659 | if (CurrentToken->isOneOf(tok::colon, tok::l_brace, tok::less)) { |
| 660 | FormatToken *Previous = CurrentToken->getPreviousNonComment(); |
| 661 | if (Previous->is(TT_JsTypeOptionalQuestion)) |
| 662 | Previous = Previous->getPreviousNonComment(); |
| 663 | if ((CurrentToken->is(tok::colon) && |
| 664 | (!Contexts.back().ColonIsDictLiteral || !Style.isCpp())) || |
| 665 | Style.Language == FormatStyle::LK_Proto || |
| 666 | Style.Language == FormatStyle::LK_TextProto) { |
| 667 | Left->Type = TT_DictLiteral; |
| 668 | if (Previous->Tok.getIdentifierInfo() || |
| 669 | Previous->is(tok::string_literal)) |
| 670 | Previous->Type = TT_SelectorName; |
| 671 | } |
| 672 | if (CurrentToken->is(tok::colon) || |
| 673 | Style.Language == FormatStyle::LK_JavaScript) |
| 674 | Left->Type = TT_DictLiteral; |
| 675 | } |
| 676 | if (CurrentToken->is(tok::comma) && |
| 677 | Style.Language == FormatStyle::LK_JavaScript) |
| 678 | Left->Type = TT_DictLiteral; |
| 679 | if (!consumeToken()) |
| 680 | return false; |
| 681 | } |
| 682 | } |
| 683 | return true; |
| 684 | } |
| 685 | |
| 686 | void updateParameterCount(FormatToken *Left, FormatToken *Current) { |
| 687 | // For ObjC methods, the number of parameters is calculated differently as |
| 688 | // method declarations have a different structure (the parameters are not |
| 689 | // inside a bracket scope). |
| 690 | if (Current->is(tok::l_brace) && Current->BlockKind == BK_Block) |
| 691 | ++Left->BlockParameterCount; |
| 692 | if (Current->is(tok::comma)) { |
| 693 | ++Left->ParameterCount; |
| 694 | if (!Left->Role) |
| 695 | Left->Role.reset(new CommaSeparatedList(Style)); |
| 696 | Left->Role->CommaFound(Current); |
| 697 | } else if (Left->ParameterCount == 0 && Current->isNot(tok::comment)) { |
| 698 | Left->ParameterCount = 1; |
| 699 | } |
| 700 | } |
| 701 | |
| 702 | bool parseConditional() { |
| 703 | while (CurrentToken) { |
| 704 | if (CurrentToken->is(tok::colon)) { |
| 705 | CurrentToken->Type = TT_ConditionalExpr; |
| 706 | next(); |
| 707 | return true; |
| 708 | } |
| 709 | if (!consumeToken()) |
| 710 | return false; |
| 711 | } |
| 712 | return false; |
| 713 | } |
| 714 | |
| 715 | bool parseTemplateDeclaration() { |
| 716 | if (CurrentToken && CurrentToken->is(tok::less)) { |
| 717 | CurrentToken->Type = TT_TemplateOpener; |
| 718 | next(); |
| 719 | if (!parseAngle()) |
| 720 | return false; |
| 721 | if (CurrentToken) |
| 722 | CurrentToken->Previous->ClosesTemplateDeclaration = true; |
| 723 | return true; |
| 724 | } |
| 725 | return false; |
| 726 | } |
| 727 | |
| 728 | bool consumeToken() { |
| 729 | FormatToken *Tok = CurrentToken; |
| 730 | next(); |
| 731 | switch (Tok->Tok.getKind()) { |
| 732 | case tok::plus: |
| 733 | case tok::minus: |
| 734 | if (!Tok->Previous && Line.MustBeDeclaration) |
| 735 | Tok->Type = TT_ObjCMethodSpecifier; |
| 736 | break; |
| 737 | case tok::colon: |
| 738 | if (!Tok->Previous) |
| 739 | return false; |
| 740 | // Colons from ?: are handled in parseConditional(). |
| 741 | if (Style.Language == FormatStyle::LK_JavaScript) { |
| 742 | if (Contexts.back().ColonIsForRangeExpr || // colon in for loop |
| 743 | (Contexts.size() == 1 && // switch/case labels |
| 744 | !Line.First->isOneOf(tok::kw_enum, tok::kw_case)) || |
| 745 | Contexts.back().ContextKind == tok::l_paren || // function params |
| 746 | Contexts.back().ContextKind == tok::l_square || // array type |
| 747 | (!Contexts.back().IsExpression && |
| 748 | Contexts.back().ContextKind == tok::l_brace) || // object type |
| 749 | (Contexts.size() == 1 && |
| 750 | Line.MustBeDeclaration)) { // method/property declaration |
| 751 | Contexts.back().IsExpression = false; |
| 752 | Tok->Type = TT_JsTypeColon; |
| 753 | break; |
| 754 | } |
| 755 | } |
| 756 | if (Contexts.back().ColonIsDictLiteral || |
| 757 | Style.Language == FormatStyle::LK_Proto || |
| 758 | Style.Language == FormatStyle::LK_TextProto) { |
| 759 | Tok->Type = TT_DictLiteral; |
| 760 | if (Style.Language == FormatStyle::LK_TextProto) { |
| 761 | if (FormatToken *Previous = Tok->getPreviousNonComment()) |
| 762 | Previous->Type = TT_SelectorName; |
| 763 | } |
| 764 | } else if (Contexts.back().ColonIsObjCMethodExpr || |
| 765 | Line.startsWith(TT_ObjCMethodSpecifier)) { |
| 766 | Tok->Type = TT_ObjCMethodExpr; |
| 767 | const FormatToken *BeforePrevious = Tok->Previous->Previous; |
| 768 | // Ensure we tag all identifiers in method declarations as |
| 769 | // TT_SelectorName. |
| 770 | bool UnknownIdentifierInMethodDeclaration = |
| 771 | Line.startsWith(TT_ObjCMethodSpecifier) && |
| 772 | Tok->Previous->is(tok::identifier) && Tok->Previous->is(TT_Unknown); |
| 773 | if (!BeforePrevious || |
| 774 | // FIXME(bug 36976): ObjC return types shouldn't use TT_CastRParen. |
| 775 | !(BeforePrevious->is(TT_CastRParen) || |
| 776 | (BeforePrevious->is(TT_ObjCMethodExpr) && |
| 777 | BeforePrevious->is(tok::colon))) || |
| 778 | BeforePrevious->is(tok::r_square) || |
| 779 | Contexts.back().LongestObjCSelectorName == 0 || |
| 780 | UnknownIdentifierInMethodDeclaration) { |
| 781 | Tok->Previous->Type = TT_SelectorName; |
| 782 | if (!Contexts.back().FirstObjCSelectorName) |
| 783 | Contexts.back().FirstObjCSelectorName = Tok->Previous; |
| 784 | else if (Tok->Previous->ColumnWidth > |
| 785 | Contexts.back().LongestObjCSelectorName) |
| 786 | Contexts.back().LongestObjCSelectorName = |
| 787 | Tok->Previous->ColumnWidth; |
| 788 | Tok->Previous->ParameterIndex = |
| 789 | Contexts.back().FirstObjCSelectorName->ObjCSelectorNameParts; |
| 790 | ++Contexts.back().FirstObjCSelectorName->ObjCSelectorNameParts; |
| 791 | } |
| 792 | } else if (Contexts.back().ColonIsForRangeExpr) { |
| 793 | Tok->Type = TT_RangeBasedForLoopColon; |
| 794 | } else if (CurrentToken && CurrentToken->is(tok::numeric_constant)) { |
| 795 | Tok->Type = TT_BitFieldColon; |
| 796 | } else if (Contexts.size() == 1 && |
| 797 | !Line.First->isOneOf(tok::kw_enum, tok::kw_case)) { |
| 798 | if (Tok->getPreviousNonComment()->isOneOf(tok::r_paren, |
| 799 | tok::kw_noexcept)) |
| 800 | Tok->Type = TT_CtorInitializerColon; |
| 801 | else |
| 802 | Tok->Type = TT_InheritanceColon; |
| 803 | } else if (canBeObjCSelectorComponent(*Tok->Previous) && Tok->Next && |
| 804 | (Tok->Next->isOneOf(tok::r_paren, tok::comma) || |
| 805 | (canBeObjCSelectorComponent(*Tok->Next) && Tok->Next->Next && |
| 806 | Tok->Next->Next->is(tok::colon)))) { |
| 807 | // This handles a special macro in ObjC code where selectors including |
| 808 | // the colon are passed as macro arguments. |
| 809 | Tok->Type = TT_ObjCMethodExpr; |
| 810 | } else if (Contexts.back().ContextKind == tok::l_paren) { |
| 811 | Tok->Type = TT_InlineASMColon; |
| 812 | } |
| 813 | break; |
| 814 | case tok::pipe: |
| 815 | case tok::amp: |
| 816 | // | and & in declarations/type expressions represent union and |
| 817 | // intersection types, respectively. |
| 818 | if (Style.Language == FormatStyle::LK_JavaScript && |
| 819 | !Contexts.back().IsExpression) |
| 820 | Tok->Type = TT_JsTypeOperator; |
| 821 | break; |
| 822 | case tok::kw_if: |
| 823 | case tok::kw_while: |
| 824 | if (Tok->is(tok::kw_if) && CurrentToken && |
| 825 | CurrentToken->is(tok::kw_constexpr)) |
| 826 | next(); |
| 827 | if (CurrentToken && CurrentToken->is(tok::l_paren)) { |
| 828 | next(); |
| 829 | if (!parseParens(/*LookForDecls=*/true)) |
| 830 | return false; |
| 831 | } |
| 832 | break; |
| 833 | case tok::kw_for: |
| 834 | if (Style.Language == FormatStyle::LK_JavaScript) { |
| 835 | // x.for and {for: ...} |
| 836 | if ((Tok->Previous && Tok->Previous->is(tok::period)) || |
| 837 | (Tok->Next && Tok->Next->is(tok::colon))) |
| 838 | break; |
| 839 | // JS' for await ( ... |
| 840 | if (CurrentToken && CurrentToken->is(Keywords.kw_await)) |
| 841 | next(); |
| 842 | } |
| 843 | Contexts.back().ColonIsForRangeExpr = true; |
| 844 | next(); |
| 845 | if (!parseParens()) |
| 846 | return false; |
| 847 | break; |
| 848 | case tok::l_paren: |
| 849 | // When faced with 'operator()()', the kw_operator handler incorrectly |
| 850 | // marks the first l_paren as a OverloadedOperatorLParen. Here, we make |
| 851 | // the first two parens OverloadedOperators and the second l_paren an |
| 852 | // OverloadedOperatorLParen. |
| 853 | if (Tok->Previous && Tok->Previous->is(tok::r_paren) && |
| 854 | Tok->Previous->MatchingParen && |
| 855 | Tok->Previous->MatchingParen->is(TT_OverloadedOperatorLParen)) { |
| 856 | Tok->Previous->Type = TT_OverloadedOperator; |
| 857 | Tok->Previous->MatchingParen->Type = TT_OverloadedOperator; |
| 858 | Tok->Type = TT_OverloadedOperatorLParen; |
| 859 | } |
| 860 | |
| 861 | if (!parseParens()) |
| 862 | return false; |
| 863 | if (Line.MustBeDeclaration && Contexts.size() == 1 && |
| 864 | !Contexts.back().IsExpression && !Line.startsWith(TT_ObjCProperty) && |
| 865 | (!Tok->Previous || |
| 866 | !Tok->Previous->isOneOf(tok::kw_decltype, tok::kw___attribute, |
| 867 | TT_LeadingJavaAnnotation))) |
| 868 | Line.MightBeFunctionDecl = true; |
| 869 | break; |
| 870 | case tok::l_square: |
| 871 | if (!parseSquare()) |
| 872 | return false; |
| 873 | break; |
| 874 | case tok::l_brace: |
| 875 | if (Style.Language == FormatStyle::LK_TextProto) { |
| 876 | FormatToken *Previous = Tok->getPreviousNonComment(); |
| 877 | if (Previous && Previous->Type != TT_DictLiteral) |
| 878 | Previous->Type = TT_SelectorName; |
| 879 | } |
| 880 | if (!parseBrace()) |
| 881 | return false; |
| 882 | break; |
| 883 | case tok::less: |
| 884 | if (parseAngle()) { |
| 885 | Tok->Type = TT_TemplateOpener; |
| 886 | // In TT_Proto, we must distignuish between: |
| 887 | // map<key, value> |
| 888 | // msg < item: data > |
| 889 | // msg: < item: data > |
| 890 | // In TT_TextProto, map<key, value> does not occur. |
| 891 | if (Style.Language == FormatStyle::LK_TextProto || |
| 892 | (Style.Language == FormatStyle::LK_Proto && Tok->Previous && |
| 893 | Tok->Previous->isOneOf(TT_SelectorName, TT_DictLiteral))) { |
| 894 | Tok->Type = TT_DictLiteral; |
| 895 | FormatToken *Previous = Tok->getPreviousNonComment(); |
| 896 | if (Previous && Previous->Type != TT_DictLiteral) |
| 897 | Previous->Type = TT_SelectorName; |
| 898 | } |
| 899 | } else { |
| 900 | Tok->Type = TT_BinaryOperator; |
| 901 | NonTemplateLess.insert(Tok); |
| 902 | CurrentToken = Tok; |
| 903 | next(); |
| 904 | } |
| 905 | break; |
| 906 | case tok::r_paren: |
| 907 | case tok::r_square: |
| 908 | return false; |
| 909 | case tok::r_brace: |
| 910 | // Lines can start with '}'. |
| 911 | if (Tok->Previous) |
| 912 | return false; |
| 913 | break; |
| 914 | case tok::greater: |
| 915 | if (Style.Language != FormatStyle::LK_TextProto) |
| 916 | Tok->Type = TT_BinaryOperator; |
| 917 | break; |
| 918 | case tok::kw_operator: |
| 919 | if (Style.Language == FormatStyle::LK_TextProto || |
| 920 | Style.Language == FormatStyle::LK_Proto) |
| 921 | break; |
| 922 | while (CurrentToken && |
| 923 | !CurrentToken->isOneOf(tok::l_paren, tok::semi, tok::r_paren)) { |
| 924 | if (CurrentToken->isOneOf(tok::star, tok::amp)) |
| 925 | CurrentToken->Type = TT_PointerOrReference; |
| 926 | consumeToken(); |
| 927 | if (CurrentToken && |
| 928 | CurrentToken->Previous->isOneOf(TT_BinaryOperator, TT_UnaryOperator, |
| 929 | tok::comma)) |
| 930 | CurrentToken->Previous->Type = TT_OverloadedOperator; |
| 931 | } |
| 932 | if (CurrentToken) { |
| 933 | CurrentToken->Type = TT_OverloadedOperatorLParen; |
| 934 | if (CurrentToken->Previous->is(TT_BinaryOperator)) |
| 935 | CurrentToken->Previous->Type = TT_OverloadedOperator; |
| 936 | } |
| 937 | break; |
| 938 | case tok::question: |
| 939 | if (Style.Language == FormatStyle::LK_JavaScript && Tok->Next && |
| 940 | Tok->Next->isOneOf(tok::semi, tok::comma, tok::colon, tok::r_paren, |
| 941 | tok::r_brace)) { |
| 942 | // Question marks before semicolons, colons, etc. indicate optional |
| 943 | // types (fields, parameters), e.g. |
| 944 | // function(x?: string, y?) {...} |
| 945 | // class X { y?; } |
| 946 | Tok->Type = TT_JsTypeOptionalQuestion; |
| 947 | break; |
| 948 | } |
| 949 | // Declarations cannot be conditional expressions, this can only be part |
| 950 | // of a type declaration. |
| 951 | if (Line.MustBeDeclaration && !Contexts.back().IsExpression && |
| 952 | Style.Language == FormatStyle::LK_JavaScript) |
| 953 | break; |
| 954 | parseConditional(); |
| 955 | break; |
| 956 | case tok::kw_template: |
| 957 | parseTemplateDeclaration(); |
| 958 | break; |
| 959 | case tok::comma: |
| 960 | if (Contexts.back().InCtorInitializer) |
| 961 | Tok->Type = TT_CtorInitializerComma; |
| 962 | else if (Contexts.back().InInheritanceList) |
| 963 | Tok->Type = TT_InheritanceComma; |
| 964 | else if (Contexts.back().FirstStartOfName && |
| 965 | (Contexts.size() == 1 || Line.startsWith(tok::kw_for))) { |
| 966 | Contexts.back().FirstStartOfName->PartOfMultiVariableDeclStmt = true; |
| 967 | Line.IsMultiVariableDeclStmt = true; |
| 968 | } |
| 969 | if (Contexts.back().IsForEachMacro) |
| 970 | Contexts.back().IsExpression = true; |
| 971 | break; |
| 972 | case tok::identifier: |
| 973 | if (Tok->isOneOf(Keywords.kw___has_include, |
| 974 | Keywords.kw___has_include_next)) { |
| 975 | parseHasInclude(); |
| 976 | } |
| 977 | break; |
| 978 | default: |
| 979 | break; |
| 980 | } |
| 981 | return true; |
| 982 | } |
| 983 | |
| 984 | void parseIncludeDirective() { |
| 985 | if (CurrentToken && CurrentToken->is(tok::less)) { |
| 986 | next(); |
| 987 | while (CurrentToken) { |
| 988 | // Mark tokens up to the trailing line comments as implicit string |
| 989 | // literals. |
| 990 | if (CurrentToken->isNot(tok::comment) && |
| 991 | !CurrentToken->TokenText.startswith("//")) |
| 992 | CurrentToken->Type = TT_ImplicitStringLiteral; |
| 993 | next(); |
| 994 | } |
| 995 | } |
| 996 | } |
| 997 | |
| 998 | void parseWarningOrError() { |
| 999 | next(); |
| 1000 | // We still want to format the whitespace left of the first token of the |
| 1001 | // warning or error. |
| 1002 | next(); |
| 1003 | while (CurrentToken) { |
| 1004 | CurrentToken->Type = TT_ImplicitStringLiteral; |
| 1005 | next(); |
| 1006 | } |
| 1007 | } |
| 1008 | |
| 1009 | void parsePragma() { |
| 1010 | next(); // Consume "pragma". |
| 1011 | if (CurrentToken && |
| 1012 | CurrentToken->isOneOf(Keywords.kw_mark, Keywords.kw_option)) { |
| 1013 | bool IsMark = CurrentToken->is(Keywords.kw_mark); |
| 1014 | next(); // Consume "mark". |
| 1015 | next(); // Consume first token (so we fix leading whitespace). |
| 1016 | while (CurrentToken) { |
| 1017 | if (IsMark || CurrentToken->Previous->is(TT_BinaryOperator)) |
| 1018 | CurrentToken->Type = TT_ImplicitStringLiteral; |
| 1019 | next(); |
| 1020 | } |
| 1021 | } |
| 1022 | } |
| 1023 | |
| 1024 | void parseHasInclude() { |
| 1025 | if (!CurrentToken || !CurrentToken->is(tok::l_paren)) |
| 1026 | return; |
| 1027 | next(); // '(' |
| 1028 | parseIncludeDirective(); |
| 1029 | next(); // ')' |
| 1030 | } |
| 1031 | |
| 1032 | LineType parsePreprocessorDirective() { |
| 1033 | bool IsFirstToken = CurrentToken->IsFirst; |
| 1034 | LineType Type = LT_PreprocessorDirective; |
| 1035 | next(); |
| 1036 | if (!CurrentToken) |
| 1037 | return Type; |
| 1038 | |
| 1039 | if (Style.Language == FormatStyle::LK_JavaScript && IsFirstToken) { |
| 1040 | // JavaScript files can contain shebang lines of the form: |
| 1041 | // #!/usr/bin/env node |
| 1042 | // Treat these like C++ #include directives. |
| 1043 | while (CurrentToken) { |
| 1044 | // Tokens cannot be comments here. |
| 1045 | CurrentToken->Type = TT_ImplicitStringLiteral; |
| 1046 | next(); |
| 1047 | } |
| 1048 | return LT_ImportStatement; |
| 1049 | } |
| 1050 | |
| 1051 | if (CurrentToken->Tok.is(tok::numeric_constant)) { |
| 1052 | CurrentToken->SpacesRequiredBefore = 1; |
| 1053 | return Type; |
| 1054 | } |
| 1055 | // Hashes in the middle of a line can lead to any strange token |
| 1056 | // sequence. |
| 1057 | if (!CurrentToken->Tok.getIdentifierInfo()) |
| 1058 | return Type; |
| 1059 | switch (CurrentToken->Tok.getIdentifierInfo()->getPPKeywordID()) { |
| 1060 | case tok::pp_include: |
| 1061 | case tok::pp_include_next: |
| 1062 | case tok::pp_import: |
| 1063 | next(); |
| 1064 | parseIncludeDirective(); |
| 1065 | Type = LT_ImportStatement; |
| 1066 | break; |
| 1067 | case tok::pp_error: |
| 1068 | case tok::pp_warning: |
| 1069 | parseWarningOrError(); |
| 1070 | break; |
| 1071 | case tok::pp_pragma: |
| 1072 | parsePragma(); |
| 1073 | break; |
| 1074 | case tok::pp_if: |
| 1075 | case tok::pp_elif: |
| 1076 | Contexts.back().IsExpression = true; |
| 1077 | parseLine(); |
| 1078 | break; |
| 1079 | default: |
| 1080 | break; |
| 1081 | } |
| 1082 | while (CurrentToken) { |
| 1083 | FormatToken *Tok = CurrentToken; |
| 1084 | next(); |
| 1085 | if (Tok->is(tok::l_paren)) |
| 1086 | parseParens(); |
| 1087 | else if (Tok->isOneOf(Keywords.kw___has_include, |
| 1088 | Keywords.kw___has_include_next)) |
| 1089 | parseHasInclude(); |
| 1090 | } |
| 1091 | return Type; |
| 1092 | } |
| 1093 | |
| 1094 | public: |
| 1095 | LineType parseLine() { |
| 1096 | NonTemplateLess.clear(); |
| 1097 | if (CurrentToken->is(tok::hash)) |
| 1098 | return parsePreprocessorDirective(); |
| 1099 | |
| 1100 | // Directly allow to 'import <string-literal>' to support protocol buffer |
| 1101 | // definitions (github.com/google/protobuf) or missing "#" (either way we |
| 1102 | // should not break the line). |
| 1103 | IdentifierInfo *Info = CurrentToken->Tok.getIdentifierInfo(); |
| 1104 | if ((Style.Language == FormatStyle::LK_Java && |
| 1105 | CurrentToken->is(Keywords.kw_package)) || |
| 1106 | (Info && Info->getPPKeywordID() == tok::pp_import && |
| 1107 | CurrentToken->Next && |
| 1108 | CurrentToken->Next->isOneOf(tok::string_literal, tok::identifier, |
| 1109 | tok::kw_static))) { |
| 1110 | next(); |
| 1111 | parseIncludeDirective(); |
| 1112 | return LT_ImportStatement; |
| 1113 | } |
| 1114 | |
| 1115 | // If this line starts and ends in '<' and '>', respectively, it is likely |
| 1116 | // part of "#define <a/b.h>". |
| 1117 | if (CurrentToken->is(tok::less) && Line.Last->is(tok::greater)) { |
| 1118 | parseIncludeDirective(); |
| 1119 | return LT_ImportStatement; |
| 1120 | } |
| 1121 | |
| 1122 | // In .proto files, top-level options are very similar to import statements |
| 1123 | // and should not be line-wrapped. |
| 1124 | if (Style.Language == FormatStyle::LK_Proto && Line.Level == 0 && |
| 1125 | CurrentToken->is(Keywords.kw_option)) { |
| 1126 | next(); |
| 1127 | if (CurrentToken && CurrentToken->is(tok::identifier)) |
| 1128 | return LT_ImportStatement; |
| 1129 | } |
| 1130 | |
| 1131 | bool KeywordVirtualFound = false; |
| 1132 | bool ImportStatement = false; |
| 1133 | |
| 1134 | // import {...} from '...'; |
| 1135 | if (Style.Language == FormatStyle::LK_JavaScript && |
| 1136 | CurrentToken->is(Keywords.kw_import)) |
| 1137 | ImportStatement = true; |
| 1138 | |
| 1139 | while (CurrentToken) { |
| 1140 | if (CurrentToken->is(tok::kw_virtual)) |
| 1141 | KeywordVirtualFound = true; |
| 1142 | if (Style.Language == FormatStyle::LK_JavaScript) { |
| 1143 | // export {...} from '...'; |
| 1144 | // An export followed by "from 'some string';" is a re-export from |
| 1145 | // another module identified by a URI and is treated as a |
| 1146 | // LT_ImportStatement (i.e. prevent wraps on it for long URIs). |
| 1147 | // Just "export {...};" or "export class ..." should not be treated as |
| 1148 | // an import in this sense. |
| 1149 | if (Line.First->is(tok::kw_export) && |
| 1150 | CurrentToken->is(Keywords.kw_from) && CurrentToken->Next && |
| 1151 | CurrentToken->Next->isStringLiteral()) |
| 1152 | ImportStatement = true; |
| 1153 | if (isClosureImportStatement(*CurrentToken)) |
| 1154 | ImportStatement = true; |
| 1155 | } |
| 1156 | if (!consumeToken()) |
| 1157 | return LT_Invalid; |
| 1158 | } |
| 1159 | if (KeywordVirtualFound) |
| 1160 | return LT_VirtualFunctionDecl; |
| 1161 | if (ImportStatement) |
| 1162 | return LT_ImportStatement; |
| 1163 | |
| 1164 | if (Line.startsWith(TT_ObjCMethodSpecifier)) { |
| 1165 | if (Contexts.back().FirstObjCSelectorName) |
| 1166 | Contexts.back().FirstObjCSelectorName->LongestObjCSelectorName = |
| 1167 | Contexts.back().LongestObjCSelectorName; |
| 1168 | return LT_ObjCMethodDecl; |
| 1169 | } |
| 1170 | |
| 1171 | return LT_Other; |
| 1172 | } |
| 1173 | |
| 1174 | private: |
| 1175 | bool isClosureImportStatement(const FormatToken &Tok) { |
| 1176 | // FIXME: Closure-library specific stuff should not be hard-coded but be |
| 1177 | // configurable. |
| 1178 | return Tok.TokenText == "goog" && Tok.Next && Tok.Next->is(tok::period) && |
| 1179 | Tok.Next->Next && |
| 1180 | (Tok.Next->Next->TokenText == "module" || |
| 1181 | Tok.Next->Next->TokenText == "provide" || |
| 1182 | Tok.Next->Next->TokenText == "require" || |
| 1183 | Tok.Next->Next->TokenText == "requireType" || |
| 1184 | Tok.Next->Next->TokenText == "forwardDeclare") && |
| 1185 | Tok.Next->Next->Next && Tok.Next->Next->Next->is(tok::l_paren); |
| 1186 | } |
| 1187 | |
| 1188 | void resetTokenMetadata(FormatToken *Token) { |
| 1189 | if (!Token) |
| 1190 | return; |
| 1191 | |
| 1192 | // Reset token type in case we have already looked at it and then |
| 1193 | // recovered from an error (e.g. failure to find the matching >). |
| 1194 | if (!CurrentToken->isOneOf( |
| 1195 | TT_LambdaLSquare, TT_LambdaLBrace, TT_ForEachMacro, |
| 1196 | TT_FunctionLBrace, TT_ImplicitStringLiteral, TT_InlineASMBrace, |
| 1197 | TT_JsFatArrow, TT_LambdaArrow, TT_OverloadedOperator, |
| 1198 | TT_RegexLiteral, TT_TemplateString, TT_ObjCStringLiteral)) |
| 1199 | CurrentToken->Type = TT_Unknown; |
| 1200 | CurrentToken->Role.reset(); |
| 1201 | CurrentToken->MatchingParen = nullptr; |
| 1202 | CurrentToken->FakeLParens.clear(); |
| 1203 | CurrentToken->FakeRParens = 0; |
| 1204 | } |
| 1205 | |
| 1206 | void next() { |
| 1207 | if (CurrentToken) { |
| 1208 | CurrentToken->NestingLevel = Contexts.size() - 1; |
| 1209 | CurrentToken->BindingStrength = Contexts.back().BindingStrength; |
| 1210 | modifyContext(*CurrentToken); |
| 1211 | determineTokenType(*CurrentToken); |
| 1212 | CurrentToken = CurrentToken->Next; |
| 1213 | } |
| 1214 | |
| 1215 | resetTokenMetadata(CurrentToken); |
| 1216 | } |
| 1217 | |
| 1218 | /// A struct to hold information valid in a specific context, e.g. |
| 1219 | /// a pair of parenthesis. |
| 1220 | struct Context { |
| 1221 | Context(tok::TokenKind ContextKind, unsigned BindingStrength, |
| 1222 | bool IsExpression) |
| 1223 | : ContextKind(ContextKind), BindingStrength(BindingStrength), |
| 1224 | IsExpression(IsExpression) {} |
| 1225 | |
| 1226 | tok::TokenKind ContextKind; |
| 1227 | unsigned BindingStrength; |
| 1228 | bool IsExpression; |
| 1229 | unsigned LongestObjCSelectorName = 0; |
| 1230 | bool ColonIsForRangeExpr = false; |
| 1231 | bool ColonIsDictLiteral = false; |
| 1232 | bool ColonIsObjCMethodExpr = false; |
| 1233 | FormatToken *FirstObjCSelectorName = nullptr; |
| 1234 | FormatToken *FirstStartOfName = nullptr; |
| 1235 | bool CanBeExpression = true; |
| 1236 | bool InTemplateArgument = false; |
| 1237 | bool InCtorInitializer = false; |
| 1238 | bool InInheritanceList = false; |
| 1239 | bool CaretFound = false; |
| 1240 | bool IsForEachMacro = false; |
| 1241 | bool InCpp11AttributeSpecifier = false; |
| 1242 | bool InCSharpAttributeSpecifier = false; |
| 1243 | }; |
| 1244 | |
| 1245 | /// Puts a new \c Context onto the stack \c Contexts for the lifetime |
| 1246 | /// of each instance. |
| 1247 | struct ScopedContextCreator { |
| 1248 | AnnotatingParser &P; |
| 1249 | |
| 1250 | ScopedContextCreator(AnnotatingParser &P, tok::TokenKind ContextKind, |
| 1251 | unsigned Increase) |
| 1252 | : P(P) { |
| 1253 | P.Contexts.push_back(Context(ContextKind, |
| 1254 | P.Contexts.back().BindingStrength + Increase, |
| 1255 | P.Contexts.back().IsExpression)); |
| 1256 | } |
| 1257 | |
| 1258 | ~ScopedContextCreator() { P.Contexts.pop_back(); } |
| 1259 | }; |
| 1260 | |
| 1261 | void modifyContext(const FormatToken &Current) { |
| 1262 | if (Current.getPrecedence() == prec::Assignment && |
| 1263 | !Line.First->isOneOf(tok::kw_template, tok::kw_using, tok::kw_return) && |
| 1264 | // Type aliases use `type X = ...;` in TypeScript and can be exported |
| 1265 | // using `export type ...`. |
| 1266 | !(Style.Language == FormatStyle::LK_JavaScript && |
| 1267 | (Line.startsWith(Keywords.kw_type, tok::identifier) || |
| 1268 | Line.startsWith(tok::kw_export, Keywords.kw_type, |
| 1269 | tok::identifier))) && |
| 1270 | (!Current.Previous || Current.Previous->isNot(tok::kw_operator))) { |
| 1271 | Contexts.back().IsExpression = true; |
| 1272 | if (!Line.startsWith(TT_UnaryOperator)) { |
| 1273 | for (FormatToken *Previous = Current.Previous; |
| 1274 | Previous && Previous->Previous && |
| 1275 | !Previous->Previous->isOneOf(tok::comma, tok::semi); |
| 1276 | Previous = Previous->Previous) { |
| 1277 | if (Previous->isOneOf(tok::r_square, tok::r_paren)) { |
| 1278 | Previous = Previous->MatchingParen; |
| 1279 | if (!Previous) |
| 1280 | break; |
| 1281 | } |
| 1282 | if (Previous->opensScope()) |
| 1283 | break; |
| 1284 | if (Previous->isOneOf(TT_BinaryOperator, TT_UnaryOperator) && |
| 1285 | Previous->isOneOf(tok::star, tok::amp, tok::ampamp) && |
| 1286 | Previous->Previous && Previous->Previous->isNot(tok::equal)) |
| 1287 | Previous->Type = TT_PointerOrReference; |
| 1288 | } |
| 1289 | } |
| 1290 | } else if (Current.is(tok::lessless) && |
| 1291 | (!Current.Previous || !Current.Previous->is(tok::kw_operator))) { |
| 1292 | Contexts.back().IsExpression = true; |
| 1293 | } else if (Current.isOneOf(tok::kw_return, tok::kw_throw)) { |
| 1294 | Contexts.back().IsExpression = true; |
| 1295 | } else if (Current.is(TT_TrailingReturnArrow)) { |
| 1296 | Contexts.back().IsExpression = false; |
| 1297 | } else if (Current.is(TT_LambdaArrow) || Current.is(Keywords.kw_assert)) { |
| 1298 | Contexts.back().IsExpression = Style.Language == FormatStyle::LK_Java; |
| 1299 | } else if (Current.Previous && |
| 1300 | Current.Previous->is(TT_CtorInitializerColon)) { |
| 1301 | Contexts.back().IsExpression = true; |
| 1302 | Contexts.back().InCtorInitializer = true; |
| 1303 | } else if (Current.Previous && Current.Previous->is(TT_InheritanceColon)) { |
| 1304 | Contexts.back().InInheritanceList = true; |
| 1305 | } else if (Current.isOneOf(tok::r_paren, tok::greater, tok::comma)) { |
| 1306 | for (FormatToken *Previous = Current.Previous; |
| 1307 | Previous && Previous->isOneOf(tok::star, tok::amp); |
| 1308 | Previous = Previous->Previous) |
| 1309 | Previous->Type = TT_PointerOrReference; |
| 1310 | if (Line.MustBeDeclaration && !Contexts.front().InCtorInitializer) |
| 1311 | Contexts.back().IsExpression = false; |
| 1312 | } else if (Current.is(tok::kw_new)) { |
| 1313 | Contexts.back().CanBeExpression = false; |
| 1314 | } else if (Current.isOneOf(tok::semi, tok::exclaim)) { |
| 1315 | // This should be the condition or increment in a for-loop. |
| 1316 | Contexts.back().IsExpression = true; |
| 1317 | } |
| 1318 | } |
| 1319 | |
| 1320 | void determineTokenType(FormatToken &Current) { |
| 1321 | if (!Current.is(TT_Unknown)) |
| 1322 | // The token type is already known. |
| 1323 | return; |
| 1324 | |
| 1325 | if (Style.Language == FormatStyle::LK_JavaScript) { |
| 1326 | if (Current.is(tok::exclaim)) { |
| 1327 | if (Current.Previous && |
| 1328 | (Current.Previous->isOneOf(tok::identifier, tok::kw_namespace, |
| 1329 | tok::r_paren, tok::r_square, |
| 1330 | tok::r_brace) || |
| 1331 | Current.Previous->Tok.isLiteral())) { |
| 1332 | Current.Type = TT_JsNonNullAssertion; |
| 1333 | return; |
| 1334 | } |
| 1335 | if (Current.Next && |
| 1336 | Current.Next->isOneOf(TT_BinaryOperator, Keywords.kw_as)) { |
| 1337 | Current.Type = TT_JsNonNullAssertion; |
| 1338 | return; |
| 1339 | } |
| 1340 | } |
| 1341 | } |
| 1342 | |
| 1343 | // Line.MightBeFunctionDecl can only be true after the parentheses of a |
| 1344 | // function declaration have been found. In this case, 'Current' is a |
| 1345 | // trailing token of this declaration and thus cannot be a name. |
| 1346 | if (Current.is(Keywords.kw_instanceof)) { |
| 1347 | Current.Type = TT_BinaryOperator; |
| 1348 | } else if (isStartOfName(Current) && |
| 1349 | (!Line.MightBeFunctionDecl || Current.NestingLevel != 0)) { |
| 1350 | Contexts.back().FirstStartOfName = &Current; |
| 1351 | Current.Type = TT_StartOfName; |
| 1352 | } else if (Current.is(tok::semi)) { |
| 1353 | // Reset FirstStartOfName after finding a semicolon so that a for loop |
| 1354 | // with multiple increment statements is not confused with a for loop |
| 1355 | // having multiple variable declarations. |
| 1356 | Contexts.back().FirstStartOfName = nullptr; |
| 1357 | } else if (Current.isOneOf(tok::kw_auto, tok::kw___auto_type)) { |
| 1358 | AutoFound = true; |
| 1359 | } else if (Current.is(tok::arrow) && |
| 1360 | Style.Language == FormatStyle::LK_Java) { |
| 1361 | Current.Type = TT_LambdaArrow; |
| 1362 | } else if (Current.is(tok::arrow) && AutoFound && Line.MustBeDeclaration && |
| 1363 | Current.NestingLevel == 0) { |
| 1364 | Current.Type = TT_TrailingReturnArrow; |
| 1365 | } else if (Current.isOneOf(tok::star, tok::amp, tok::ampamp)) { |
| 1366 | Current.Type = determineStarAmpUsage(Current, |
| 1367 | Contexts.back().CanBeExpression && |
| 1368 | Contexts.back().IsExpression, |
| 1369 | Contexts.back().InTemplateArgument); |
| 1370 | } else if (Current.isOneOf(tok::minus, tok::plus, tok::caret)) { |
| 1371 | Current.Type = determinePlusMinusCaretUsage(Current); |
| 1372 | if (Current.is(TT_UnaryOperator) && Current.is(tok::caret)) |
| 1373 | Contexts.back().CaretFound = true; |
| 1374 | } else if (Current.isOneOf(tok::minusminus, tok::plusplus)) { |
| 1375 | Current.Type = determineIncrementUsage(Current); |
| 1376 | } else if (Current.isOneOf(tok::exclaim, tok::tilde)) { |
| 1377 | Current.Type = TT_UnaryOperator; |
| 1378 | } else if (Current.is(tok::question)) { |
| 1379 | if (Style.Language == FormatStyle::LK_JavaScript && |
| 1380 | Line.MustBeDeclaration && !Contexts.back().IsExpression) { |
| 1381 | // In JavaScript, `interface X { foo?(): bar; }` is an optional method |
| 1382 | // on the interface, not a ternary expression. |
| 1383 | Current.Type = TT_JsTypeOptionalQuestion; |
| 1384 | } else { |
| 1385 | Current.Type = TT_ConditionalExpr; |
| 1386 | } |
| 1387 | } else if (Current.isBinaryOperator() && |
| 1388 | (!Current.Previous || Current.Previous->isNot(tok::l_square)) && |
| 1389 | (!Current.is(tok::greater) && |
| 1390 | Style.Language != FormatStyle::LK_TextProto)) { |
| 1391 | Current.Type = TT_BinaryOperator; |
| 1392 | } else if (Current.is(tok::comment)) { |
| 1393 | if (Current.TokenText.startswith("/*")) { |
| 1394 | if (Current.TokenText.endswith("*/")) |
| 1395 | Current.Type = TT_BlockComment; |
| 1396 | else |
| 1397 | // The lexer has for some reason determined a comment here. But we |
| 1398 | // cannot really handle it, if it isn't properly terminated. |
| 1399 | Current.Tok.setKind(tok::unknown); |
| 1400 | } else { |
| 1401 | Current.Type = TT_LineComment; |
| 1402 | } |
| 1403 | } else if (Current.is(tok::r_paren)) { |
| 1404 | if (rParenEndsCast(Current)) |
| 1405 | Current.Type = TT_CastRParen; |
| 1406 | if (Current.MatchingParen && Current.Next && |
| 1407 | !Current.Next->isBinaryOperator() && |
| 1408 | !Current.Next->isOneOf(tok::semi, tok::colon, tok::l_brace, |
| 1409 | tok::comma, tok::period, tok::arrow, |
| 1410 | tok::coloncolon)) |
| 1411 | if (FormatToken *AfterParen = Current.MatchingParen->Next) { |
| 1412 | // Make sure this isn't the return type of an Obj-C block declaration |
| 1413 | if (AfterParen->Tok.isNot(tok::caret)) { |
| 1414 | if (FormatToken *BeforeParen = Current.MatchingParen->Previous) |
| 1415 | if (BeforeParen->is(tok::identifier) && |
| 1416 | BeforeParen->TokenText == BeforeParen->TokenText.upper() && |
| 1417 | (!BeforeParen->Previous || |
| 1418 | BeforeParen->Previous->ClosesTemplateDeclaration)) |
| 1419 | Current.Type = TT_FunctionAnnotationRParen; |
| 1420 | } |
| 1421 | } |
| 1422 | } else if (Current.is(tok::at) && Current.Next && |
| 1423 | Style.Language != FormatStyle::LK_JavaScript && |
| 1424 | Style.Language != FormatStyle::LK_Java) { |
| 1425 | // In Java & JavaScript, "@..." is a decorator or annotation. In ObjC, it |
| 1426 | // marks declarations and properties that need special formatting. |
| 1427 | switch (Current.Next->Tok.getObjCKeywordID()) { |
| 1428 | case tok::objc_interface: |
| 1429 | case tok::objc_implementation: |
| 1430 | case tok::objc_protocol: |
| 1431 | Current.Type = TT_ObjCDecl; |
| 1432 | break; |
| 1433 | case tok::objc_property: |
| 1434 | Current.Type = TT_ObjCProperty; |
| 1435 | break; |
| 1436 | default: |
| 1437 | break; |
| 1438 | } |
| 1439 | } else if (Current.is(tok::period)) { |
| 1440 | FormatToken *PreviousNoComment = Current.getPreviousNonComment(); |
| 1441 | if (PreviousNoComment && |
| 1442 | PreviousNoComment->isOneOf(tok::comma, tok::l_brace)) |
| 1443 | Current.Type = TT_DesignatedInitializerPeriod; |
| 1444 | else if (Style.Language == FormatStyle::LK_Java && Current.Previous && |
| 1445 | Current.Previous->isOneOf(TT_JavaAnnotation, |
| 1446 | TT_LeadingJavaAnnotation)) { |
| 1447 | Current.Type = Current.Previous->Type; |
| 1448 | } |
| 1449 | } else if (canBeObjCSelectorComponent(Current) && |
| 1450 | // FIXME(bug 36976): ObjC return types shouldn't use |
| 1451 | // TT_CastRParen. |
| 1452 | Current.Previous && Current.Previous->is(TT_CastRParen) && |
| 1453 | Current.Previous->MatchingParen && |
| 1454 | Current.Previous->MatchingParen->Previous && |
| 1455 | Current.Previous->MatchingParen->Previous->is( |
| 1456 | TT_ObjCMethodSpecifier)) { |
| 1457 | // This is the first part of an Objective-C selector name. (If there's no |
| 1458 | // colon after this, this is the only place which annotates the identifier |
| 1459 | // as a selector.) |
| 1460 | Current.Type = TT_SelectorName; |
| 1461 | } else if (Current.isOneOf(tok::identifier, tok::kw_const) && |
| 1462 | Current.Previous && |
| 1463 | !Current.Previous->isOneOf(tok::equal, tok::at) && |
| 1464 | Line.MightBeFunctionDecl && Contexts.size() == 1) { |
| 1465 | // Line.MightBeFunctionDecl can only be true after the parentheses of a |
| 1466 | // function declaration have been found. |
| 1467 | Current.Type = TT_TrailingAnnotation; |
| 1468 | } else if ((Style.Language == FormatStyle::LK_Java || |
| 1469 | Style.Language == FormatStyle::LK_JavaScript) && |
| 1470 | Current.Previous) { |
| 1471 | if (Current.Previous->is(tok::at) && |
| 1472 | Current.isNot(Keywords.kw_interface)) { |
| 1473 | const FormatToken &AtToken = *Current.Previous; |
| 1474 | const FormatToken *Previous = AtToken.getPreviousNonComment(); |
| 1475 | if (!Previous || Previous->is(TT_LeadingJavaAnnotation)) |
| 1476 | Current.Type = TT_LeadingJavaAnnotation; |
| 1477 | else |
| 1478 | Current.Type = TT_JavaAnnotation; |
| 1479 | } else if (Current.Previous->is(tok::period) && |
| 1480 | Current.Previous->isOneOf(TT_JavaAnnotation, |
| 1481 | TT_LeadingJavaAnnotation)) { |
| 1482 | Current.Type = Current.Previous->Type; |
| 1483 | } |
| 1484 | } |
| 1485 | } |
| 1486 | |
| 1487 | /// Take a guess at whether \p Tok starts a name of a function or |
| 1488 | /// variable declaration. |
| 1489 | /// |
| 1490 | /// This is a heuristic based on whether \p Tok is an identifier following |
| 1491 | /// something that is likely a type. |
| 1492 | bool isStartOfName(const FormatToken &Tok) { |
| 1493 | if (Tok.isNot(tok::identifier) || !Tok.Previous) |
| 1494 | return false; |
| 1495 | |
| 1496 | if (Tok.Previous->isOneOf(TT_LeadingJavaAnnotation, Keywords.kw_instanceof, |
| 1497 | Keywords.kw_as)) |
| 1498 | return false; |
| 1499 | if (Style.Language == FormatStyle::LK_JavaScript && |
| 1500 | Tok.Previous->is(Keywords.kw_in)) |
| 1501 | return false; |
| 1502 | |
| 1503 | // Skip "const" as it does not have an influence on whether this is a name. |
| 1504 | FormatToken *PreviousNotConst = Tok.getPreviousNonComment(); |
| 1505 | while (PreviousNotConst && PreviousNotConst->is(tok::kw_const)) |
| 1506 | PreviousNotConst = PreviousNotConst->getPreviousNonComment(); |
| 1507 | |
| 1508 | if (!PreviousNotConst) |
| 1509 | return false; |
| 1510 | |
| 1511 | bool IsPPKeyword = PreviousNotConst->is(tok::identifier) && |
| 1512 | PreviousNotConst->Previous && |
| 1513 | PreviousNotConst->Previous->is(tok::hash); |
| 1514 | |
| 1515 | if (PreviousNotConst->is(TT_TemplateCloser)) |
| 1516 | return PreviousNotConst && PreviousNotConst->MatchingParen && |
| 1517 | PreviousNotConst->MatchingParen->Previous && |
| 1518 | PreviousNotConst->MatchingParen->Previous->isNot(tok::period) && |
| 1519 | PreviousNotConst->MatchingParen->Previous->isNot(tok::kw_template); |
| 1520 | |
| 1521 | if (PreviousNotConst->is(tok::r_paren) && PreviousNotConst->MatchingParen && |
| 1522 | PreviousNotConst->MatchingParen->Previous && |
| 1523 | PreviousNotConst->MatchingParen->Previous->is(tok::kw_decltype)) |
| 1524 | return true; |
| 1525 | |
| 1526 | return (!IsPPKeyword && |
| 1527 | PreviousNotConst->isOneOf(tok::identifier, tok::kw_auto)) || |
| 1528 | PreviousNotConst->is(TT_PointerOrReference) || |
| 1529 | PreviousNotConst->isSimpleTypeSpecifier(); |
| 1530 | } |
| 1531 | |
| 1532 | /// Determine whether ')' is ending a cast. |
| 1533 | bool rParenEndsCast(const FormatToken &Tok) { |
| 1534 | // C-style casts are only used in C++ and Java. |
| 1535 | if (!Style.isCpp() && Style.Language != FormatStyle::LK_Java) |
| 1536 | return false; |
| 1537 | |
| 1538 | // Empty parens aren't casts and there are no casts at the end of the line. |
| 1539 | if (Tok.Previous == Tok.MatchingParen || !Tok.Next || !Tok.MatchingParen) |
| 1540 | return false; |
| 1541 | |
| 1542 | FormatToken *LeftOfParens = Tok.MatchingParen->getPreviousNonComment(); |
| 1543 | if (LeftOfParens) { |
| 1544 | // If there is a closing parenthesis left of the current parentheses, |
| 1545 | // look past it as these might be chained casts. |
| 1546 | if (LeftOfParens->is(tok::r_paren)) { |
| 1547 | if (!LeftOfParens->MatchingParen || |
| 1548 | !LeftOfParens->MatchingParen->Previous) |
| 1549 | return false; |
| 1550 | LeftOfParens = LeftOfParens->MatchingParen->Previous; |
| 1551 | } |
| 1552 | |
| 1553 | // If there is an identifier (or with a few exceptions a keyword) right |
| 1554 | // before the parentheses, this is unlikely to be a cast. |
| 1555 | if (LeftOfParens->Tok.getIdentifierInfo() && |
| 1556 | !LeftOfParens->isOneOf(Keywords.kw_in, tok::kw_return, tok::kw_case, |
| 1557 | tok::kw_delete)) |
| 1558 | return false; |
| 1559 | |
| 1560 | // Certain other tokens right before the parentheses are also signals that |
| 1561 | // this cannot be a cast. |
| 1562 | if (LeftOfParens->isOneOf(tok::at, tok::r_square, TT_OverloadedOperator, |
| 1563 | TT_TemplateCloser, tok::ellipsis)) |
| 1564 | return false; |
| 1565 | } |
| 1566 | |
| 1567 | if (Tok.Next->is(tok::question)) |
| 1568 | return false; |
| 1569 | |
| 1570 | // As Java has no function types, a "(" after the ")" likely means that this |
| 1571 | // is a cast. |
| 1572 | if (Style.Language == FormatStyle::LK_Java && Tok.Next->is(tok::l_paren)) |
| 1573 | return true; |
| 1574 | |
| 1575 | // If a (non-string) literal follows, this is likely a cast. |
| 1576 | if (Tok.Next->isNot(tok::string_literal) && |
| 1577 | (Tok.Next->Tok.isLiteral() || |
| 1578 | Tok.Next->isOneOf(tok::kw_sizeof, tok::kw_alignof))) |
| 1579 | return true; |
| 1580 | |
| 1581 | // Heuristically try to determine whether the parentheses contain a type. |
| 1582 | bool ParensAreType = |
| 1583 | !Tok.Previous || |
| 1584 | Tok.Previous->isOneOf(TT_PointerOrReference, TT_TemplateCloser) || |
| 1585 | Tok.Previous->isSimpleTypeSpecifier(); |
| 1586 | bool ParensCouldEndDecl = |
| 1587 | Tok.Next->isOneOf(tok::equal, tok::semi, tok::l_brace, tok::greater); |
| 1588 | if (ParensAreType && !ParensCouldEndDecl) |
| 1589 | return true; |
| 1590 | |
| 1591 | // At this point, we heuristically assume that there are no casts at the |
| 1592 | // start of the line. We assume that we have found most cases where there |
| 1593 | // are by the logic above, e.g. "(void)x;". |
| 1594 | if (!LeftOfParens) |
| 1595 | return false; |
| 1596 | |
| 1597 | // Certain token types inside the parentheses mean that this can't be a |
| 1598 | // cast. |
| 1599 | for (const FormatToken *Token = Tok.MatchingParen->Next; Token != &Tok; |
| 1600 | Token = Token->Next) |
| 1601 | if (Token->is(TT_BinaryOperator)) |
| 1602 | return false; |
| 1603 | |
| 1604 | // If the following token is an identifier or 'this', this is a cast. All |
| 1605 | // cases where this can be something else are handled above. |
| 1606 | if (Tok.Next->isOneOf(tok::identifier, tok::kw_this)) |
| 1607 | return true; |
| 1608 | |
| 1609 | if (!Tok.Next->Next) |
| 1610 | return false; |
| 1611 | |
| 1612 | // If the next token after the parenthesis is a unary operator, assume |
| 1613 | // that this is cast, unless there are unexpected tokens inside the |
| 1614 | // parenthesis. |
| 1615 | bool NextIsUnary = |
| 1616 | Tok.Next->isUnaryOperator() || Tok.Next->isOneOf(tok::amp, tok::star); |
| 1617 | if (!NextIsUnary || Tok.Next->is(tok::plus) || |
| 1618 | !Tok.Next->Next->isOneOf(tok::identifier, tok::numeric_constant)) |
| 1619 | return false; |
| 1620 | // Search for unexpected tokens. |
| 1621 | for (FormatToken *Prev = Tok.Previous; Prev != Tok.MatchingParen; |
| 1622 | Prev = Prev->Previous) { |
| 1623 | if (!Prev->isOneOf(tok::kw_const, tok::identifier, tok::coloncolon)) |
| 1624 | return false; |
| 1625 | } |
| 1626 | return true; |
| 1627 | } |
| 1628 | |
| 1629 | /// Return the type of the given token assuming it is * or &. |
| 1630 | TokenType determineStarAmpUsage(const FormatToken &Tok, bool IsExpression, |
| 1631 | bool InTemplateArgument) { |
| 1632 | if (Style.Language == FormatStyle::LK_JavaScript) |
| 1633 | return TT_BinaryOperator; |
| 1634 | |
| 1635 | const FormatToken *PrevToken = Tok.getPreviousNonComment(); |
| 1636 | if (!PrevToken) |
| 1637 | return TT_UnaryOperator; |
| 1638 | |
| 1639 | const FormatToken *NextToken = Tok.getNextNonComment(); |
| 1640 | if (!NextToken || |
| 1641 | NextToken->isOneOf(tok::arrow, tok::equal, tok::kw_const) || |
| 1642 | (NextToken->is(tok::l_brace) && !NextToken->getNextNonComment())) |
| 1643 | return TT_PointerOrReference; |
| 1644 | |
| 1645 | if (PrevToken->is(tok::coloncolon)) |
| 1646 | return TT_PointerOrReference; |
| 1647 | |
| 1648 | if (PrevToken->isOneOf(tok::l_paren, tok::l_square, tok::l_brace, |
| 1649 | tok::comma, tok::semi, tok::kw_return, tok::colon, |
| 1650 | tok::equal, tok::kw_delete, tok::kw_sizeof, |
| 1651 | tok::kw_throw) || |
| 1652 | PrevToken->isOneOf(TT_BinaryOperator, TT_ConditionalExpr, |
| 1653 | TT_UnaryOperator, TT_CastRParen)) |
| 1654 | return TT_UnaryOperator; |
| 1655 | |
| 1656 | if (NextToken->is(tok::l_square) && NextToken->isNot(TT_LambdaLSquare)) |
| 1657 | return TT_PointerOrReference; |
| 1658 | if (NextToken->is(tok::kw_operator) && !IsExpression) |
| 1659 | return TT_PointerOrReference; |
| 1660 | if (NextToken->isOneOf(tok::comma, tok::semi)) |
| 1661 | return TT_PointerOrReference; |
| 1662 | |
| 1663 | if (PrevToken->is(tok::r_paren) && PrevToken->MatchingParen) { |
| 1664 | FormatToken *TokenBeforeMatchingParen = |
| 1665 | PrevToken->MatchingParen->getPreviousNonComment(); |
| 1666 | if (TokenBeforeMatchingParen && |
| 1667 | TokenBeforeMatchingParen->isOneOf(tok::kw_typeof, tok::kw_decltype)) |
| 1668 | return TT_PointerOrReference; |
| 1669 | } |
| 1670 | |
| 1671 | if (PrevToken->Tok.isLiteral() || |
| 1672 | PrevToken->isOneOf(tok::r_paren, tok::r_square, tok::kw_true, |
| 1673 | tok::kw_false, tok::r_brace) || |
| 1674 | NextToken->Tok.isLiteral() || |
| 1675 | NextToken->isOneOf(tok::kw_true, tok::kw_false) || |
| 1676 | NextToken->isUnaryOperator() || |
| 1677 | // If we know we're in a template argument, there are no named |
| 1678 | // declarations. Thus, having an identifier on the right-hand side |
| 1679 | // indicates a binary operator. |
| 1680 | (InTemplateArgument && NextToken->Tok.isAnyIdentifier())) |
| 1681 | return TT_BinaryOperator; |
| 1682 | |
| 1683 | // "&&(" is quite unlikely to be two successive unary "&". |
| 1684 | if (Tok.is(tok::ampamp) && NextToken && NextToken->is(tok::l_paren)) |
| 1685 | return TT_BinaryOperator; |
| 1686 | |
| 1687 | // This catches some cases where evaluation order is used as control flow: |
| 1688 | // aaa && aaa->f(); |
| 1689 | const FormatToken *NextNextToken = NextToken->getNextNonComment(); |
| 1690 | if (NextNextToken && NextNextToken->is(tok::arrow)) |
| 1691 | return TT_BinaryOperator; |
| 1692 | |
| 1693 | // It is very unlikely that we are going to find a pointer or reference type |
| 1694 | // definition on the RHS of an assignment. |
| 1695 | if (IsExpression && !Contexts.back().CaretFound) |
| 1696 | return TT_BinaryOperator; |
| 1697 | |
| 1698 | return TT_PointerOrReference; |
| 1699 | } |
| 1700 | |
| 1701 | TokenType determinePlusMinusCaretUsage(const FormatToken &Tok) { |
| 1702 | const FormatToken *PrevToken = Tok.getPreviousNonComment(); |
| 1703 | if (!PrevToken) |
| 1704 | return TT_UnaryOperator; |
| 1705 | |
| 1706 | if (PrevToken->isOneOf(TT_CastRParen, TT_UnaryOperator)) |
| 1707 | // This must be a sequence of leading unary operators. |
| 1708 | return TT_UnaryOperator; |
| 1709 | |
| 1710 | // Use heuristics to recognize unary operators. |
| 1711 | if (PrevToken->isOneOf(tok::equal, tok::l_paren, tok::comma, tok::l_square, |
| 1712 | tok::question, tok::colon, tok::kw_return, |
| 1713 | tok::kw_case, tok::at, tok::l_brace)) |
| 1714 | return TT_UnaryOperator; |
| 1715 | |
| 1716 | // There can't be two consecutive binary operators. |
| 1717 | if (PrevToken->is(TT_BinaryOperator)) |
| 1718 | return TT_UnaryOperator; |
| 1719 | |
| 1720 | // Fall back to marking the token as binary operator. |
| 1721 | return TT_BinaryOperator; |
| 1722 | } |
| 1723 | |
| 1724 | /// Determine whether ++/-- are pre- or post-increments/-decrements. |
| 1725 | TokenType determineIncrementUsage(const FormatToken &Tok) { |
| 1726 | const FormatToken *PrevToken = Tok.getPreviousNonComment(); |
| 1727 | if (!PrevToken || PrevToken->is(TT_CastRParen)) |
| 1728 | return TT_UnaryOperator; |
| 1729 | if (PrevToken->isOneOf(tok::r_paren, tok::r_square, tok::identifier)) |
| 1730 | return TT_TrailingUnaryOperator; |
| 1731 | |
| 1732 | return TT_UnaryOperator; |
| 1733 | } |
| 1734 | |
| 1735 | SmallVector<Context, 8> Contexts; |
| 1736 | |
| 1737 | const FormatStyle &Style; |
| 1738 | AnnotatedLine &Line; |
| 1739 | FormatToken *CurrentToken; |
| 1740 | bool AutoFound; |
| 1741 | const AdditionalKeywords &Keywords; |
| 1742 | |
| 1743 | // Set of "<" tokens that do not open a template parameter list. If parseAngle |
| 1744 | // determines that a specific token can't be a template opener, it will make |
| 1745 | // same decision irrespective of the decisions for tokens leading up to it. |
| 1746 | // Store this information to prevent this from causing exponential runtime. |
| 1747 | llvm::SmallPtrSet<FormatToken *, 16> NonTemplateLess; |
| 1748 | }; |
| 1749 | |
| 1750 | static const int PrecedenceUnaryOperator = prec::PointerToMember + 1; |
| 1751 | static const int PrecedenceArrowAndPeriod = prec::PointerToMember + 2; |
| 1752 | |
| 1753 | /// Parses binary expressions by inserting fake parenthesis based on |
| 1754 | /// operator precedence. |
| 1755 | class ExpressionParser { |
| 1756 | public: |
| 1757 | ExpressionParser(const FormatStyle &Style, const AdditionalKeywords &Keywords, |
| 1758 | AnnotatedLine &Line) |
| 1759 | : Style(Style), Keywords(Keywords), Current(Line.First) {} |
| 1760 | |
| 1761 | /// Parse expressions with the given operator precedence. |
| 1762 | void parse(int Precedence = 0) { |
| 1763 | // Skip 'return' and ObjC selector colons as they are not part of a binary |
| 1764 | // expression. |
| 1765 | while (Current && (Current->is(tok::kw_return) || |
| 1766 | (Current->is(tok::colon) && |
| 1767 | Current->isOneOf(TT_ObjCMethodExpr, TT_DictLiteral)))) |
| 1768 | next(); |
| 1769 | |
| 1770 | if (!Current || Precedence > PrecedenceArrowAndPeriod) |
| 1771 | return; |
| 1772 | |
| 1773 | // Conditional expressions need to be parsed separately for proper nesting. |
| 1774 | if (Precedence == prec::Conditional) { |
| 1775 | parseConditionalExpr(); |
| 1776 | return; |
| 1777 | } |
| 1778 | |
| 1779 | // Parse unary operators, which all have a higher precedence than binary |
| 1780 | // operators. |
| 1781 | if (Precedence == PrecedenceUnaryOperator) { |
| 1782 | parseUnaryOperator(); |
| 1783 | return; |
| 1784 | } |
| 1785 | |
| 1786 | FormatToken *Start = Current; |
| 1787 | FormatToken *LatestOperator = nullptr; |
| 1788 | unsigned OperatorIndex = 0; |
| 1789 | |
| 1790 | while (Current) { |
| 1791 | // Consume operators with higher precedence. |
| 1792 | parse(Precedence + 1); |
| 1793 | |
| 1794 | int CurrentPrecedence = getCurrentPrecedence(); |
| 1795 | |
| 1796 | if (Current && Current->is(TT_SelectorName) && |
| 1797 | Precedence == CurrentPrecedence) { |
| 1798 | if (LatestOperator) |
| 1799 | addFakeParenthesis(Start, prec::Level(Precedence)); |
| 1800 | Start = Current; |
| 1801 | } |
| 1802 | |
| 1803 | // At the end of the line or when an operator with higher precedence is |
| 1804 | // found, insert fake parenthesis and return. |
| 1805 | if (!Current || |
| 1806 | (Current->closesScope() && |
| 1807 | (Current->MatchingParen || Current->is(TT_TemplateString))) || |
| 1808 | (CurrentPrecedence != -1 && CurrentPrecedence < Precedence) || |
| 1809 | (CurrentPrecedence == prec::Conditional && |
| 1810 | Precedence == prec::Assignment && Current->is(tok::colon))) { |
| 1811 | break; |
| 1812 | } |
| 1813 | |
| 1814 | // Consume scopes: (), [], <> and {} |
| 1815 | if (Current->opensScope()) { |
| 1816 | // In fragment of a JavaScript template string can look like '}..${' and |
| 1817 | // thus close a scope and open a new one at the same time. |
| 1818 | while (Current && (!Current->closesScope() || Current->opensScope())) { |
| 1819 | next(); |
| 1820 | parse(); |
| 1821 | } |
| 1822 | next(); |
| 1823 | } else { |
| 1824 | // Operator found. |
| 1825 | if (CurrentPrecedence == Precedence) { |
| 1826 | if (LatestOperator) |
| 1827 | LatestOperator->NextOperator = Current; |
| 1828 | LatestOperator = Current; |
| 1829 | Current->OperatorIndex = OperatorIndex; |
| 1830 | ++OperatorIndex; |
| 1831 | } |
| 1832 | next(/*SkipPastLeadingComments=*/Precedence > 0); |
| 1833 | } |
| 1834 | } |
| 1835 | |
| 1836 | if (LatestOperator && (Current || Precedence > 0)) { |
| 1837 | // LatestOperator->LastOperator = true; |
| 1838 | if (Precedence == PrecedenceArrowAndPeriod) { |
| 1839 | // Call expressions don't have a binary operator precedence. |
| 1840 | addFakeParenthesis(Start, prec::Unknown); |
| 1841 | } else { |
| 1842 | addFakeParenthesis(Start, prec::Level(Precedence)); |
| 1843 | } |
| 1844 | } |
| 1845 | } |
| 1846 | |
| 1847 | private: |
| 1848 | /// Gets the precedence (+1) of the given token for binary operators |
| 1849 | /// and other tokens that we treat like binary operators. |
| 1850 | int getCurrentPrecedence() { |
| 1851 | if (Current) { |
| 1852 | const FormatToken *NextNonComment = Current->getNextNonComment(); |
| 1853 | if (Current->is(TT_ConditionalExpr)) |
| 1854 | return prec::Conditional; |
| 1855 | if (NextNonComment && Current->is(TT_SelectorName) && |
| 1856 | (NextNonComment->isOneOf(TT_DictLiteral, TT_JsTypeColon) || |
| 1857 | ((Style.Language == FormatStyle::LK_Proto || |
| 1858 | Style.Language == FormatStyle::LK_TextProto) && |
| 1859 | NextNonComment->is(tok::less)))) |
| 1860 | return prec::Assignment; |
| 1861 | if (Current->is(TT_JsComputedPropertyName)) |
| 1862 | return prec::Assignment; |
| 1863 | if (Current->is(TT_LambdaArrow)) |
| 1864 | return prec::Comma; |
| 1865 | if (Current->is(TT_JsFatArrow)) |
| 1866 | return prec::Assignment; |
| 1867 | if (Current->isOneOf(tok::semi, TT_InlineASMColon, TT_SelectorName) || |
| 1868 | (Current->is(tok::comment) && NextNonComment && |
| 1869 | NextNonComment->is(TT_SelectorName))) |
| 1870 | return 0; |
| 1871 | if (Current->is(TT_RangeBasedForLoopColon)) |
| 1872 | return prec::Comma; |
| 1873 | if ((Style.Language == FormatStyle::LK_Java || |
| 1874 | Style.Language == FormatStyle::LK_JavaScript) && |
| 1875 | Current->is(Keywords.kw_instanceof)) |
| 1876 | return prec::Relational; |
| 1877 | if (Style.Language == FormatStyle::LK_JavaScript && |
| 1878 | Current->isOneOf(Keywords.kw_in, Keywords.kw_as)) |
| 1879 | return prec::Relational; |
| 1880 | if (Current->is(TT_BinaryOperator) || Current->is(tok::comma)) |
| 1881 | return Current->getPrecedence(); |
| 1882 | if (Current->isOneOf(tok::period, tok::arrow)) |
| 1883 | return PrecedenceArrowAndPeriod; |
| 1884 | if ((Style.Language == FormatStyle::LK_Java || |
| 1885 | Style.Language == FormatStyle::LK_JavaScript) && |
| 1886 | Current->isOneOf(Keywords.kw_extends, Keywords.kw_implements, |
| 1887 | Keywords.kw_throws)) |
| 1888 | return 0; |
| 1889 | } |
| 1890 | return -1; |
| 1891 | } |
| 1892 | |
| 1893 | void addFakeParenthesis(FormatToken *Start, prec::Level Precedence) { |
| 1894 | Start->FakeLParens.push_back(Precedence); |
| 1895 | if (Precedence > prec::Unknown) |
| 1896 | Start->StartsBinaryExpression = true; |
| 1897 | if (Current) { |
| 1898 | FormatToken *Previous = Current->Previous; |
| 1899 | while (Previous->is(tok::comment) && Previous->Previous) |
| 1900 | Previous = Previous->Previous; |
| 1901 | ++Previous->FakeRParens; |
| 1902 | if (Precedence > prec::Unknown) |
| 1903 | Previous->EndsBinaryExpression = true; |
| 1904 | } |
| 1905 | } |
| 1906 | |
| 1907 | /// Parse unary operator expressions and surround them with fake |
| 1908 | /// parentheses if appropriate. |
| 1909 | void parseUnaryOperator() { |
| 1910 | llvm::SmallVector<FormatToken *, 2> Tokens; |
| 1911 | while (Current && Current->is(TT_UnaryOperator)) { |
| 1912 | Tokens.push_back(Current); |
| 1913 | next(); |
| 1914 | } |
| 1915 | parse(PrecedenceArrowAndPeriod); |
| 1916 | for (FormatToken *Token : llvm::reverse(Tokens)) |
| 1917 | // The actual precedence doesn't matter. |
| 1918 | addFakeParenthesis(Token, prec::Unknown); |
| 1919 | } |
| 1920 | |
| 1921 | void parseConditionalExpr() { |
| 1922 | while (Current && Current->isTrailingComment()) { |
| 1923 | next(); |
| 1924 | } |
| 1925 | FormatToken *Start = Current; |
| 1926 | parse(prec::LogicalOr); |
| 1927 | if (!Current || !Current->is(tok::question)) |
| 1928 | return; |
| 1929 | next(); |
| 1930 | parse(prec::Assignment); |
| 1931 | if (!Current || Current->isNot(TT_ConditionalExpr)) |
| 1932 | return; |
| 1933 | next(); |
| 1934 | parse(prec::Assignment); |
| 1935 | addFakeParenthesis(Start, prec::Conditional); |
| 1936 | } |
| 1937 | |
| 1938 | void next(bool SkipPastLeadingComments = true) { |
| 1939 | if (Current) |
| 1940 | Current = Current->Next; |
| 1941 | while (Current && |
| 1942 | (Current->NewlinesBefore == 0 || SkipPastLeadingComments) && |
| 1943 | Current->isTrailingComment()) |
| 1944 | Current = Current->Next; |
| 1945 | } |
| 1946 | |
| 1947 | const FormatStyle &Style; |
| 1948 | const AdditionalKeywords &Keywords; |
| 1949 | FormatToken *Current; |
| 1950 | }; |
| 1951 | |
| 1952 | } // end anonymous namespace |
| 1953 | |
| 1954 | void TokenAnnotator::setCommentLineLevels( |
| 1955 | SmallVectorImpl<AnnotatedLine *> &Lines) { |
| 1956 | const AnnotatedLine *NextNonCommentLine = nullptr; |
| 1957 | for (SmallVectorImpl<AnnotatedLine *>::reverse_iterator I = Lines.rbegin(), |
| 1958 | E = Lines.rend(); |
| 1959 | I != E; ++I) { |
| 1960 | bool CommentLine = true; |
| 1961 | for (const FormatToken *Tok = (*I)->First; Tok; Tok = Tok->Next) { |
| 1962 | if (!Tok->is(tok::comment)) { |
| 1963 | CommentLine = false; |
| 1964 | break; |
| 1965 | } |
| 1966 | } |
| 1967 | |
| 1968 | // If the comment is currently aligned with the line immediately following |
| 1969 | // it, that's probably intentional and we should keep it. |
| 1970 | if (NextNonCommentLine && CommentLine && |
| 1971 | NextNonCommentLine->First->NewlinesBefore <= 1 && |
| 1972 | NextNonCommentLine->First->OriginalColumn == |
| 1973 | (*I)->First->OriginalColumn) { |
| 1974 | // Align comments for preprocessor lines with the # in column 0 if |
| 1975 | // preprocessor lines are not indented. Otherwise, align with the next |
| 1976 | // line. |
| 1977 | (*I)->Level = |
| 1978 | (Style.IndentPPDirectives != FormatStyle::PPDIS_BeforeHash && |
| 1979 | (NextNonCommentLine->Type == LT_PreprocessorDirective || |
| 1980 | NextNonCommentLine->Type == LT_ImportStatement)) |
| 1981 | ? 0 |
| 1982 | : NextNonCommentLine->Level; |
| 1983 | } else { |
| 1984 | NextNonCommentLine = (*I)->First->isNot(tok::r_brace) ? (*I) : nullptr; |
| 1985 | } |
| 1986 | |
| 1987 | setCommentLineLevels((*I)->Children); |
| 1988 | } |
| 1989 | } |
| 1990 | |
| 1991 | static unsigned maxNestingDepth(const AnnotatedLine &Line) { |
| 1992 | unsigned Result = 0; |
| 1993 | for (const auto *Tok = Line.First; Tok != nullptr; Tok = Tok->Next) |
| 1994 | Result = std::max(Result, Tok->NestingLevel); |
| 1995 | return Result; |
| 1996 | } |
| 1997 | |
| 1998 | void TokenAnnotator::annotate(AnnotatedLine &Line) { |
| 1999 | for (SmallVectorImpl<AnnotatedLine *>::iterator I = Line.Children.begin(), |
| 2000 | E = Line.Children.end(); |
| 2001 | I != E; ++I) { |
| 2002 | annotate(**I); |
| 2003 | } |
| 2004 | AnnotatingParser Parser(Style, Line, Keywords); |
| 2005 | Line.Type = Parser.parseLine(); |
| 2006 | |
| 2007 | // With very deep nesting, ExpressionParser uses lots of stack and the |
| 2008 | // formatting algorithm is very slow. We're not going to do a good job here |
| 2009 | // anyway - it's probably generated code being formatted by mistake. |
| 2010 | // Just skip the whole line. |
| 2011 | if (maxNestingDepth(Line) > 50) |
| 2012 | Line.Type = LT_Invalid; |
| 2013 | |
| 2014 | if (Line.Type == LT_Invalid) |
| 2015 | return; |
| 2016 | |
| 2017 | ExpressionParser ExprParser(Style, Keywords, Line); |
| 2018 | ExprParser.parse(); |
| 2019 | |
| 2020 | if (Line.startsWith(TT_ObjCMethodSpecifier)) |
| 2021 | Line.Type = LT_ObjCMethodDecl; |
| 2022 | else if (Line.startsWith(TT_ObjCDecl)) |
| 2023 | Line.Type = LT_ObjCDecl; |
| 2024 | else if (Line.startsWith(TT_ObjCProperty)) |
| 2025 | Line.Type = LT_ObjCProperty; |
| 2026 | |
| 2027 | Line.First->SpacesRequiredBefore = 1; |
| 2028 | Line.First->CanBreakBefore = Line.First->MustBreakBefore; |
| 2029 | } |
| 2030 | |
| 2031 | // This function heuristically determines whether 'Current' starts the name of a |
| 2032 | // function declaration. |
| 2033 | static bool isFunctionDeclarationName(const FormatToken &Current, |
| 2034 | const AnnotatedLine &Line) { |
| 2035 | auto skipOperatorName = [](const FormatToken *Next) -> const FormatToken * { |
| 2036 | for (; Next; Next = Next->Next) { |
| 2037 | if (Next->is(TT_OverloadedOperatorLParen)) |
| 2038 | return Next; |
| 2039 | if (Next->is(TT_OverloadedOperator)) |
| 2040 | continue; |
| 2041 | if (Next->isOneOf(tok::kw_new, tok::kw_delete)) { |
| 2042 | // For 'new[]' and 'delete[]'. |
| 2043 | if (Next->Next && Next->Next->is(tok::l_square) && Next->Next->Next && |
| 2044 | Next->Next->Next->is(tok::r_square)) |
| 2045 | Next = Next->Next->Next; |
| 2046 | continue; |
| 2047 | } |
| 2048 | |
| 2049 | break; |
| 2050 | } |
| 2051 | return nullptr; |
| 2052 | }; |
| 2053 | |
| 2054 | // Find parentheses of parameter list. |
| 2055 | const FormatToken *Next = Current.Next; |
| 2056 | if (Current.is(tok::kw_operator)) { |
| 2057 | if (Current.Previous && Current.Previous->is(tok::coloncolon)) |
| 2058 | return false; |
| 2059 | Next = skipOperatorName(Next); |
| 2060 | } else { |
| 2061 | if (!Current.is(TT_StartOfName) || Current.NestingLevel != 0) |
| 2062 | return false; |
| 2063 | for (; Next; Next = Next->Next) { |
| 2064 | if (Next->is(TT_TemplateOpener)) { |
| 2065 | Next = Next->MatchingParen; |
| 2066 | } else if (Next->is(tok::coloncolon)) { |
| 2067 | Next = Next->Next; |
| 2068 | if (!Next) |
| 2069 | return false; |
| 2070 | if (Next->is(tok::kw_operator)) { |
| 2071 | Next = skipOperatorName(Next->Next); |
| 2072 | break; |
| 2073 | } |
| 2074 | if (!Next->is(tok::identifier)) |
| 2075 | return false; |
| 2076 | } else if (Next->is(tok::l_paren)) { |
| 2077 | break; |
| 2078 | } else { |
| 2079 | return false; |
| 2080 | } |
| 2081 | } |
| 2082 | } |
| 2083 | |
| 2084 | // Check whether parameter list can belong to a function declaration. |
| 2085 | if (!Next || !Next->is(tok::l_paren) || !Next->MatchingParen) |
| 2086 | return false; |
| 2087 | // If the lines ends with "{", this is likely an function definition. |
| 2088 | if (Line.Last->is(tok::l_brace)) |
| 2089 | return true; |
| 2090 | if (Next->Next == Next->MatchingParen) |
| 2091 | return true; // Empty parentheses. |
| 2092 | // If there is an &/&& after the r_paren, this is likely a function. |
| 2093 | if (Next->MatchingParen->Next && |
| 2094 | Next->MatchingParen->Next->is(TT_PointerOrReference)) |
| 2095 | return true; |
| 2096 | for (const FormatToken *Tok = Next->Next; Tok && Tok != Next->MatchingParen; |
| 2097 | Tok = Tok->Next) { |
| 2098 | if (Tok->is(tok::l_paren) && Tok->MatchingParen) { |
| 2099 | Tok = Tok->MatchingParen; |
| 2100 | continue; |
| 2101 | } |
| 2102 | if (Tok->is(tok::kw_const) || Tok->isSimpleTypeSpecifier() || |
| 2103 | Tok->isOneOf(TT_PointerOrReference, TT_StartOfName, tok::ellipsis)) |
| 2104 | return true; |
| 2105 | if (Tok->isOneOf(tok::l_brace, tok::string_literal, TT_ObjCMethodExpr) || |
| 2106 | Tok->Tok.isLiteral()) |
| 2107 | return false; |
| 2108 | } |
| 2109 | return false; |
| 2110 | } |
| 2111 | |
| 2112 | bool TokenAnnotator::mustBreakForReturnType(const AnnotatedLine &Line) const { |
| 2113 | assert(Line.MightBeFunctionDecl); |
| 2114 | |
| 2115 | if ((Style.AlwaysBreakAfterReturnType == FormatStyle::RTBS_TopLevel || |
| 2116 | Style.AlwaysBreakAfterReturnType == |
| 2117 | FormatStyle::RTBS_TopLevelDefinitions) && |
| 2118 | Line.Level > 0) |
| 2119 | return false; |
| 2120 | |
| 2121 | switch (Style.AlwaysBreakAfterReturnType) { |
| 2122 | case FormatStyle::RTBS_None: |
| 2123 | return false; |
| 2124 | case FormatStyle::RTBS_All: |
| 2125 | case FormatStyle::RTBS_TopLevel: |
| 2126 | return true; |
| 2127 | case FormatStyle::RTBS_AllDefinitions: |
| 2128 | case FormatStyle::RTBS_TopLevelDefinitions: |
| 2129 | return Line.mightBeFunctionDefinition(); |
| 2130 | } |
| 2131 | |
| 2132 | return false; |
| 2133 | } |
| 2134 | |
| 2135 | void TokenAnnotator::calculateFormattingInformation(AnnotatedLine &Line) { |
| 2136 | for (SmallVectorImpl<AnnotatedLine *>::iterator I = Line.Children.begin(), |
| 2137 | E = Line.Children.end(); |
| 2138 | I != E; ++I) { |
| 2139 | calculateFormattingInformation(**I); |
| 2140 | } |
| 2141 | |
| 2142 | Line.First->TotalLength = |
| 2143 | Line.First->IsMultiline ? Style.ColumnLimit |
| 2144 | : Line.FirstStartColumn + Line.First->ColumnWidth; |
| 2145 | FormatToken *Current = Line.First->Next; |
| 2146 | bool InFunctionDecl = Line.MightBeFunctionDecl; |
| 2147 | while (Current) { |
| 2148 | if (isFunctionDeclarationName(*Current, Line)) |
| 2149 | Current->Type = TT_FunctionDeclarationName; |
| 2150 | if (Current->is(TT_LineComment)) { |
| 2151 | if (Current->Previous->BlockKind == BK_BracedInit && |
| 2152 | Current->Previous->opensScope()) |
| 2153 | Current->SpacesRequiredBefore = Style.Cpp11BracedListStyle ? 0 : 1; |
| 2154 | else |
| 2155 | Current->SpacesRequiredBefore = Style.SpacesBeforeTrailingComments; |
| 2156 | |
| 2157 | // If we find a trailing comment, iterate backwards to determine whether |
| 2158 | // it seems to relate to a specific parameter. If so, break before that |
| 2159 | // parameter to avoid changing the comment's meaning. E.g. don't move 'b' |
| 2160 | // to the previous line in: |
| 2161 | // SomeFunction(a, |
| 2162 | // b, // comment |
| 2163 | // c); |
| 2164 | if (!Current->HasUnescapedNewline) { |
| 2165 | for (FormatToken *Parameter = Current->Previous; Parameter; |
| 2166 | Parameter = Parameter->Previous) { |
| 2167 | if (Parameter->isOneOf(tok::comment, tok::r_brace)) |
| 2168 | break; |
| 2169 | if (Parameter->Previous && Parameter->Previous->is(tok::comma)) { |
| 2170 | if (!Parameter->Previous->is(TT_CtorInitializerComma) && |
| 2171 | Parameter->HasUnescapedNewline) |
| 2172 | Parameter->MustBreakBefore = true; |
| 2173 | break; |
| 2174 | } |
| 2175 | } |
| 2176 | } |
| 2177 | } else if (Current->SpacesRequiredBefore == 0 && |
| 2178 | spaceRequiredBefore(Line, *Current)) { |
| 2179 | Current->SpacesRequiredBefore = 1; |
| 2180 | } |
| 2181 | |
| 2182 | Current->MustBreakBefore = |
| 2183 | Current->MustBreakBefore || mustBreakBefore(Line, *Current); |
| 2184 | |
| 2185 | if (!Current->MustBreakBefore && InFunctionDecl && |
| 2186 | Current->is(TT_FunctionDeclarationName)) |
| 2187 | Current->MustBreakBefore = mustBreakForReturnType(Line); |
| 2188 | |
| 2189 | Current->CanBreakBefore = |
| 2190 | Current->MustBreakBefore || canBreakBefore(Line, *Current); |
| 2191 | unsigned ChildSize = 0; |
| 2192 | if (Current->Previous->Children.size() == 1) { |
| 2193 | FormatToken &LastOfChild = *Current->Previous->Children[0]->Last; |
| 2194 | ChildSize = LastOfChild.isTrailingComment() ? Style.ColumnLimit |
| 2195 | : LastOfChild.TotalLength + 1; |
| 2196 | } |
| 2197 | const FormatToken *Prev = Current->Previous; |
| 2198 | if (Current->MustBreakBefore || Prev->Children.size() > 1 || |
| 2199 | (Prev->Children.size() == 1 && |
| 2200 | Prev->Children[0]->First->MustBreakBefore) || |
| 2201 | Current->IsMultiline) |
| 2202 | Current->TotalLength = Prev->TotalLength + Style.ColumnLimit; |
| 2203 | else |
| 2204 | Current->TotalLength = Prev->TotalLength + Current->ColumnWidth + |
| 2205 | ChildSize + Current->SpacesRequiredBefore; |
| 2206 | |
| 2207 | if (Current->is(TT_CtorInitializerColon)) |
| 2208 | InFunctionDecl = false; |
| 2209 | |
| 2210 | // FIXME: Only calculate this if CanBreakBefore is true once static |
| 2211 | // initializers etc. are sorted out. |
| 2212 | // FIXME: Move magic numbers to a better place. |
| 2213 | |
| 2214 | // Reduce penalty for aligning ObjC method arguments using the colon |
| 2215 | // alignment as this is the canonical way (still prefer fitting everything |
| 2216 | // into one line if possible). Trying to fit a whole expression into one |
| 2217 | // line should not force other line breaks (e.g. when ObjC method |
| 2218 | // expression is a part of other expression). |
| 2219 | Current->SplitPenalty = splitPenalty(Line, *Current, InFunctionDecl); |
| 2220 | if (Style.Language == FormatStyle::LK_ObjC && |
| 2221 | Current->is(TT_SelectorName) && Current->ParameterIndex > 0) { |
| 2222 | if (Current->ParameterIndex == 1) |
| 2223 | Current->SplitPenalty += 5 * Current->BindingStrength; |
| 2224 | } else { |
| 2225 | Current->SplitPenalty += 20 * Current->BindingStrength; |
| 2226 | } |
| 2227 | |
| 2228 | Current = Current->Next; |
| 2229 | } |
| 2230 | |
| 2231 | calculateUnbreakableTailLengths(Line); |
| 2232 | unsigned IndentLevel = Line.Level; |
| 2233 | for (Current = Line.First; Current != nullptr; Current = Current->Next) { |
| 2234 | if (Current->Role) |
| 2235 | Current->Role->precomputeFormattingInfos(Current); |
| 2236 | if (Current->MatchingParen && |
| 2237 | Current->MatchingParen->opensBlockOrBlockTypeList(Style)) { |
| 2238 | 0", "/home/seafit/code_projects/clang_source/clang/lib/Format/TokenAnnotator.cpp", 2238, __PRETTY_FUNCTION__))" file_link="../../../include/assert.h.html#88" macro="true">assert(IndentLevel > 0); |
| 2239 | --IndentLevel; |
| 2240 | } |
| 2241 | Current->IndentLevel = IndentLevel; |
| 2242 | if (Current->opensBlockOrBlockTypeList(Style)) |
| 2243 | ++IndentLevel; |
| 2244 | } |
| 2245 | |
| 2246 | LLVM_DEBUG({ printDebugInfo(Line); }); |
| 2247 | } |
| 2248 | |
| 2249 | void TokenAnnotator::calculateUnbreakableTailLengths(AnnotatedLine &Line) { |
| 2250 | unsigned UnbreakableTailLength = 0; |
| 2251 | FormatToken *Current = Line.Last; |
| 2252 | while (Current) { |
| 2253 | Current->UnbreakableTailLength = UnbreakableTailLength; |
| 2254 | if (Current->CanBreakBefore || |
| 2255 | Current->isOneOf(tok::comment, tok::string_literal)) { |
| 2256 | UnbreakableTailLength = 0; |
| 2257 | } else { |
| 2258 | UnbreakableTailLength += |
| 2259 | Current->ColumnWidth + Current->SpacesRequiredBefore; |
| 2260 | } |
| 2261 | Current = Current->Previous; |
| 2262 | } |
| 2263 | } |
| 2264 | |
| 2265 | unsigned TokenAnnotator::splitPenalty(const AnnotatedLine &Line, |
| 2266 | const FormatToken &Tok, |
| 2267 | bool InFunctionDecl) { |
| 2268 | const FormatToken &Left = *Tok.Previous; |
| 2269 | const FormatToken &Right = Tok; |
| 2270 | |
| 2271 | if (Left.is(tok::semi)) |
| 2272 | return 0; |
| 2273 | |
| 2274 | if (Style.Language == FormatStyle::LK_Java) { |
| 2275 | if (Right.isOneOf(Keywords.kw_extends, Keywords.kw_throws)) |
| 2276 | return 1; |
| 2277 | if (Right.is(Keywords.kw_implements)) |
| 2278 | return 2; |
| 2279 | if (Left.is(tok::comma) && Left.NestingLevel == 0) |
| 2280 | return 3; |
| 2281 | } else if (Style.Language == FormatStyle::LK_JavaScript) { |
| 2282 | if (Right.is(Keywords.kw_function) && Left.isNot(tok::comma)) |
| 2283 | return 100; |
| 2284 | if (Left.is(TT_JsTypeColon)) |
| 2285 | return 35; |
| 2286 | if ((Left.is(TT_TemplateString) && Left.TokenText.endswith("${")) || |
| 2287 | (Right.is(TT_TemplateString) && Right.TokenText.startswith("}"))) |
| 2288 | return 100; |
| 2289 | // Prefer breaking call chains (".foo") over empty "{}", "[]" or "()". |
| 2290 | if (Left.opensScope() && Right.closesScope()) |
| 2291 | return 200; |
| 2292 | } |
| 2293 | |
| 2294 | if (Right.is(tok::identifier) && Right.Next && Right.Next->is(TT_DictLiteral)) |
| 2295 | return 1; |
| 2296 | if (Right.is(tok::l_square)) { |
| 2297 | if (Style.Language == FormatStyle::LK_Proto) |
| 2298 | return 1; |
| 2299 | if (Left.is(tok::r_square)) |
| 2300 | return 200; |
| 2301 | // Slightly prefer formatting local lambda definitions like functions. |
| 2302 | if (Right.is(TT_LambdaLSquare) && Left.is(tok::equal)) |
| 2303 | return 35; |
| 2304 | if (!Right.isOneOf(TT_ObjCMethodExpr, TT_LambdaLSquare, |
| 2305 | TT_ArrayInitializerLSquare, |
| 2306 | TT_DesignatedInitializerLSquare, TT_AttributeSquare)) |
| 2307 | return 500; |
| 2308 | } |
| 2309 | |
| 2310 | if (Left.is(tok::coloncolon) || |
| 2311 | (Right.is(tok::period) && Style.Language == FormatStyle::LK_Proto)) |
| 2312 | return 500; |
| 2313 | if (Right.isOneOf(TT_StartOfName, TT_FunctionDeclarationName) || |
| 2314 | Right.is(tok::kw_operator)) { |
| 2315 | if (Line.startsWith(tok::kw_for) && Right.PartOfMultiVariableDeclStmt) |
| 2316 | return 3; |
| 2317 | if (Left.is(TT_StartOfName)) |
| 2318 | return 110; |
| 2319 | if (InFunctionDecl && Right.NestingLevel == 0) |
| 2320 | return Style.PenaltyReturnTypeOnItsOwnLine; |
| 2321 | return 200; |
| 2322 | } |
| 2323 | if (Right.is(TT_PointerOrReference)) |
| 2324 | return 190; |
| 2325 | if (Right.is(TT_LambdaArrow)) |
| 2326 | return 110; |
| 2327 | if (Left.is(tok::equal) && Right.is(tok::l_brace)) |
| 2328 | return 160; |
| 2329 | if (Left.is(TT_CastRParen)) |
| 2330 | return 100; |
| 2331 | if (Left.isOneOf(tok::kw_class, tok::kw_struct)) |
| 2332 | return 5000; |
| 2333 | if (Left.is(tok::comment)) |
| 2334 | return 1000; |
| 2335 | |
| 2336 | if (Left.isOneOf(TT_RangeBasedForLoopColon, TT_InheritanceColon, |
| 2337 | TT_CtorInitializerColon)) |
| 2338 | return 2; |
| 2339 | |
| 2340 | if (Right.isMemberAccess()) { |
| 2341 | // Breaking before the "./->" of a chained call/member access is reasonably |
| 2342 | // cheap, as formatting those with one call per line is generally |
| 2343 | // desirable. In particular, it should be cheaper to break before the call |
| 2344 | // than it is to break inside a call's parameters, which could lead to weird |
| 2345 | // "hanging" indents. The exception is the very last "./->" to support this |
| 2346 | // frequent pattern: |
| 2347 | // |
| 2348 | // aaaaaaaa.aaaaaaaa.bbbbbbb().ccccccccccccccccccccc( |
| 2349 | // dddddddd); |
| 2350 | // |
| 2351 | // which might otherwise be blown up onto many lines. Here, clang-format |
| 2352 | // won't produce "hanging" indents anyway as there is no other trailing |
| 2353 | // call. |
| 2354 | // |
| 2355 | // Also apply higher penalty is not a call as that might lead to a wrapping |
| 2356 | // like: |
| 2357 | // |
| 2358 | // aaaaaaa |
| 2359 | // .aaaaaaaaa.bbbbbbbb(cccccccc); |
| 2360 | return !Right.NextOperator || !Right.NextOperator->Previous->closesScope() |
| 2361 | ? 150 |
| 2362 | : 35; |
| 2363 | } |
| 2364 | |
| 2365 | if (Right.is(TT_TrailingAnnotation) && |
| 2366 | (!Right.Next || Right.Next->isNot(tok::l_paren))) { |
| 2367 | // Moving trailing annotations to the next line is fine for ObjC method |
| 2368 | // declarations. |
| 2369 | if (Line.startsWith(TT_ObjCMethodSpecifier)) |
| 2370 | return 10; |
| 2371 | // Generally, breaking before a trailing annotation is bad unless it is |
| 2372 | // function-like. It seems to be especially preferable to keep standard |
| 2373 | // annotations (i.e. "const", "final" and "override") on the same line. |
| 2374 | // Use a slightly higher penalty after ")" so that annotations like |
| 2375 | // "const override" are kept together. |
| 2376 | bool is_short_annotation = Right.TokenText.size() < 10; |
| 2377 | return (Left.is(tok::r_paren) ? 100 : 120) + (is_short_annotation ? 50 : 0); |
| 2378 | } |
| 2379 | |
| 2380 | // In for-loops, prefer breaking at ',' and ';'. |
| 2381 | if (Line.startsWith(tok::kw_for) && Left.is(tok::equal)) |
| 2382 | return 4; |
| 2383 | |
| 2384 | // In Objective-C method expressions, prefer breaking before "param:" over |
| 2385 | // breaking after it. |
| 2386 | if (Right.is(TT_SelectorName)) |
| 2387 | return 0; |
| 2388 | if (Left.is(tok::colon) && Left.is(TT_ObjCMethodExpr)) |
| 2389 | return Line.MightBeFunctionDecl ? 50 : 500; |
| 2390 | |
| 2391 | // In Objective-C type declarations, avoid breaking after the category's |
| 2392 | // open paren (we'll prefer breaking after the protocol list's opening |
| 2393 | // angle bracket, if present). |
| 2394 | if (Line.Type == LT_ObjCDecl && Left.is(tok::l_paren) && Left.Previous && |
| 2395 | Left.Previous->isOneOf(tok::identifier, tok::greater)) |
| 2396 | return 500; |
| 2397 | |
| 2398 | if (Left.is(tok::l_paren) && InFunctionDecl && |
| 2399 | Style.AlignAfterOpenBracket != FormatStyle::BAS_DontAlign) |
| 2400 | return 100; |
| 2401 | if (Left.is(tok::l_paren) && Left.Previous && |
| 2402 | (Left.Previous->isOneOf(tok::kw_if, tok::kw_for) || |
| 2403 | Left.Previous->endsSequence(tok::kw_constexpr, tok::kw_if))) |
| 2404 | return 1000; |
| 2405 | if (Left.is(tok::equal) && InFunctionDecl) |
| 2406 | return 110; |
| 2407 | if (Right.is(tok::r_brace)) |
| 2408 | return 1; |
| 2409 | if (Left.is(TT_TemplateOpener)) |
| 2410 | return 100; |
| 2411 | if (Left.opensScope()) { |
| 2412 | if (Style.AlignAfterOpenBracket == FormatStyle::BAS_DontAlign) |
| 2413 | return 0; |
| 2414 | if (Left.is(tok::l_brace) && !Style.Cpp11BracedListStyle) |
| 2415 | return 19; |
| 2416 | return Left.ParameterCount > 1 ? Style.PenaltyBreakBeforeFirstCallParameter |
| 2417 | : 19; |
| 2418 | } |
| 2419 | if (Left.is(TT_JavaAnnotation)) |
| 2420 | return 50; |
| 2421 | |
| 2422 | if (Left.isOneOf(tok::plus, tok::comma) && Left.Previous && |
| 2423 | Left.Previous->isLabelString() && |
| 2424 | (Left.NextOperator || Left.OperatorIndex != 0)) |
| 2425 | return 50; |
| 2426 | if (Right.is(tok::plus) && Left.isLabelString() && |
| 2427 | (Right.NextOperator || Right.OperatorIndex != 0)) |
| 2428 | return 25; |
| 2429 | if (Left.is(tok::comma)) |
| 2430 | return 1; |
| 2431 | if (Right.is(tok::lessless) && Left.isLabelString() && |
| 2432 | (Right.NextOperator || Right.OperatorIndex != 1)) |
| 2433 | return 25; |
| 2434 | if (Right.is(tok::lessless)) { |
| 2435 | // Breaking at a << is really cheap. |
| 2436 | if (!Left.is(tok::r_paren) || Right.OperatorIndex > 0) |
| 2437 | // Slightly prefer to break before the first one in log-like statements. |
| 2438 | return 2; |
| 2439 | return 1; |
| 2440 | } |
| 2441 | if (Left.ClosesTemplateDeclaration) |
| 2442 | return Style.PenaltyBreakTemplateDeclaration; |
| 2443 | if (Left.is(TT_ConditionalExpr)) |
| 2444 | return prec::Conditional; |
| 2445 | prec::Level Level = Left.getPrecedence(); |
| 2446 | if (Level == prec::Unknown) |
| 2447 | Level = Right.getPrecedence(); |
| 2448 | if (Level == prec::Assignment) |
| 2449 | return Style.PenaltyBreakAssignment; |
| 2450 | if (Level != prec::Unknown) |
| 2451 | return Level; |
| 2452 | |
| 2453 | return 3; |
| 2454 | } |
| 2455 | |
| 2456 | bool TokenAnnotator::spaceRequiredBetween(const AnnotatedLine &Line, |
| 2457 | const FormatToken &Left, |
| 2458 | const FormatToken &Right) { |
| 2459 | if (Left.is(tok::kw_return) && Right.isNot(tok::semi)) |
| 2460 | return true; |
| 2461 | if (Left.is(Keywords.kw_assert) && Style.Language == FormatStyle::LK_Java) |
| 2462 | return true; |
| 2463 | if (Style.ObjCSpaceAfterProperty && Line.Type == LT_ObjCProperty && |
| 2464 | Left.Tok.getObjCKeywordID() == tok::objc_property) |
| 2465 | return true; |
| 2466 | if (Right.is(tok::hashhash)) |
| 2467 | return Left.is(tok::hash); |
| 2468 | if (Left.isOneOf(tok::hashhash, tok::hash)) |
| 2469 | return Right.is(tok::hash); |
| 2470 | if (Left.is(tok::l_paren) && Right.is(tok::r_paren)) |
| 2471 | return Style.SpaceInEmptyParentheses; |
| 2472 | if (Left.is(tok::l_paren) || Right.is(tok::r_paren)) |
| 2473 | return (Right.is(TT_CastRParen) || |
| 2474 | (Left.MatchingParen && Left.MatchingParen->is(TT_CastRParen))) |
| 2475 | ? Style.SpacesInCStyleCastParentheses |
| 2476 | : Style.SpacesInParentheses; |
| 2477 | if (Right.isOneOf(tok::semi, tok::comma)) |
| 2478 | return false; |
| 2479 | if (Right.is(tok::less) && Line.Type == LT_ObjCDecl) { |
| 2480 | bool IsLightweightGeneric = Right.MatchingParen && |
| 2481 | Right.MatchingParen->Next && |
| 2482 | Right.MatchingParen->Next->is(tok::colon); |
| 2483 | return !IsLightweightGeneric && Style.ObjCSpaceBeforeProtocolList; |
| 2484 | } |
| 2485 | if (Right.is(tok::less) && Left.is(tok::kw_template)) |
| 2486 | return Style.SpaceAfterTemplateKeyword; |
| 2487 | if (Left.isOneOf(tok::exclaim, tok::tilde)) |
| 2488 | return false; |
| 2489 | if (Left.is(tok::at) && |
| 2490 | Right.isOneOf(tok::identifier, tok::string_literal, tok::char_constant, |
| 2491 | tok::numeric_constant, tok::l_paren, tok::l_brace, |
| 2492 | tok::kw_true, tok::kw_false)) |
| 2493 | return false; |
| 2494 | if (Left.is(tok::colon)) |
| 2495 | return !Left.is(TT_ObjCMethodExpr); |
| 2496 | if (Left.is(tok::coloncolon)) |
| 2497 | return false; |
| 2498 | if (Left.is(tok::less) || Right.isOneOf(tok::greater, tok::less)) { |
| 2499 | if (Style.Language == FormatStyle::LK_TextProto || |
| 2500 | (Style.Language == FormatStyle::LK_Proto && |
| 2501 | (Left.is(TT_DictLiteral) || Right.is(TT_DictLiteral)))) { |
| 2502 | // Format empty list as `<>`. |
| 2503 | if (Left.is(tok::less) && Right.is(tok::greater)) |
| 2504 | return false; |
| 2505 | return !Style.Cpp11BracedListStyle; |
| 2506 | } |
| 2507 | return false; |
| 2508 | } |
| 2509 | if (Right.is(tok::ellipsis)) |
| 2510 | return Left.Tok.isLiteral() || (Left.is(tok::identifier) && Left.Previous && |
| 2511 | Left.Previous->is(tok::kw_case)); |
| 2512 | if (Left.is(tok::l_square) && Right.is(tok::amp)) |
| 2513 | return false; |
| 2514 | if (Right.is(TT_PointerOrReference)) { |
| 2515 | if (Left.is(tok::r_paren) && Line.MightBeFunctionDecl) { |
| 2516 | if (!Left.MatchingParen) |
| 2517 | return true; |
| 2518 | FormatToken *TokenBeforeMatchingParen = |
| 2519 | Left.MatchingParen->getPreviousNonComment(); |
| 2520 | if (!TokenBeforeMatchingParen || |
| 2521 | !TokenBeforeMatchingParen->isOneOf(tok::kw_typeof, tok::kw_decltype)) |
| 2522 | return true; |
| 2523 | } |
| 2524 | return (Left.Tok.isLiteral() || |
| 2525 | (!Left.isOneOf(TT_PointerOrReference, tok::l_paren) && |
| 2526 | (Style.PointerAlignment != FormatStyle::PAS_Left || |
| 2527 | (Line.IsMultiVariableDeclStmt && |
| 2528 | (Left.NestingLevel == 0 || |
| 2529 | (Left.NestingLevel == 1 && Line.First->is(tok::kw_for))))))); |
| 2530 | } |
| 2531 | if (Right.is(TT_FunctionTypeLParen) && Left.isNot(tok::l_paren) && |
| 2532 | (!Left.is(TT_PointerOrReference) || |
| 2533 | (Style.PointerAlignment != FormatStyle::PAS_Right && |
| 2534 | !Line.IsMultiVariableDeclStmt))) |
| 2535 | return true; |
| 2536 | if (Left.is(TT_PointerOrReference)) |
| 2537 | return Right.Tok.isLiteral() || Right.is(TT_BlockComment) || |
| 2538 | (Right.isOneOf(Keywords.kw_override, Keywords.kw_final) && |
| 2539 | !Right.is(TT_StartOfName)) || |
| 2540 | (Right.is(tok::l_brace) && Right.BlockKind == BK_Block) || |
| 2541 | (!Right.isOneOf(TT_PointerOrReference, TT_ArraySubscriptLSquare, |
| 2542 | tok::l_paren) && |
| 2543 | (Style.PointerAlignment != FormatStyle::PAS_Right && |
| 2544 | !Line.IsMultiVariableDeclStmt) && |
| 2545 | Left.Previous && |
| 2546 | !Left.Previous->isOneOf(tok::l_paren, tok::coloncolon)); |
| 2547 | if (Right.is(tok::star) && Left.is(tok::l_paren)) |
| 2548 | return false; |
| 2549 | const auto SpaceRequiredForArrayInitializerLSquare = |
| 2550 | [](const FormatToken &LSquareTok, const FormatStyle &Style) { |
| 2551 | return Style.SpacesInContainerLiterals || |
| 2552 | ((Style.Language == FormatStyle::LK_Proto || |
| 2553 | Style.Language == FormatStyle::LK_TextProto) && |
| 2554 | !Style.Cpp11BracedListStyle && |
| 2555 | LSquareTok.endsSequence(tok::l_square, tok::colon, |
| 2556 | TT_SelectorName)); |
| 2557 | }; |
| 2558 | if (Left.is(tok::l_square)) |
| 2559 | return (Left.is(TT_ArrayInitializerLSquare) && Right.isNot(tok::r_square) && |
| 2560 | SpaceRequiredForArrayInitializerLSquare(Left, Style)) || |
| 2561 | (Left.isOneOf(TT_ArraySubscriptLSquare, |
| 2562 | TT_StructuredBindingLSquare) && |
| 2563 | Style.SpacesInSquareBrackets && Right.isNot(tok::r_square)); |
| 2564 | if (Right.is(tok::r_square)) |
| 2565 | return Right.MatchingParen && |
| 2566 | ((Right.MatchingParen->is(TT_ArrayInitializerLSquare) && |
| 2567 | SpaceRequiredForArrayInitializerLSquare(*Right.MatchingParen, |
| 2568 | Style)) || |
| 2569 | (Style.SpacesInSquareBrackets && |
| 2570 | Right.MatchingParen->isOneOf(TT_ArraySubscriptLSquare, |
| 2571 | TT_StructuredBindingLSquare)) || |
| 2572 | Right.MatchingParen->is(TT_AttributeParen)); |
| 2573 | if (Right.is(tok::l_square) && |
| 2574 | !Right.isOneOf(TT_ObjCMethodExpr, TT_LambdaLSquare, |
| 2575 | TT_DesignatedInitializerLSquare, |
| 2576 | TT_StructuredBindingLSquare, TT_AttributeSquare) && |
| 2577 | !Left.isOneOf(tok::numeric_constant, TT_DictLiteral)) |
| 2578 | return false; |
| 2579 | if (Left.is(tok::l_brace) && Right.is(tok::r_brace)) |
| 2580 | return !Left.Children.empty(); // No spaces in "{}". |
| 2581 | if ((Left.is(tok::l_brace) && Left.BlockKind != BK_Block) || |
| 2582 | (Right.is(tok::r_brace) && Right.MatchingParen && |
| 2583 | Right.MatchingParen->BlockKind != BK_Block)) |
| 2584 | return !Style.Cpp11BracedListStyle; |
| 2585 | if (Left.is(TT_BlockComment)) |
| 2586 | // No whitespace in x(/*foo=*/1), except for JavaScript. |
| 2587 | return Style.Language == FormatStyle::LK_JavaScript || |
| 2588 | !Left.TokenText.endswith("=*/"); |
| 2589 | if (Right.is(tok::l_paren)) { |
| 2590 | if ((Left.is(tok::r_paren) && Left.is(TT_AttributeParen)) || |
| 2591 | (Left.is(tok::r_square) && Left.is(TT_AttributeSquare))) |
| 2592 | return true; |
| 2593 | return Line.Type == LT_ObjCDecl || Left.is(tok::semi) || |
| 2594 | (Style.SpaceBeforeParens != FormatStyle::SBPO_Never && |
| 2595 | (Left.isOneOf(tok::kw_if, tok::pp_elif, tok::kw_for, tok::kw_while, |
| 2596 | tok::kw_switch, tok::kw_case, TT_ForEachMacro, |
| 2597 | TT_ObjCForIn) || |
| 2598 | Left.endsSequence(tok::kw_constexpr, tok::kw_if) || |
| 2599 | (Left.isOneOf(tok::kw_try, Keywords.kw___except, tok::kw_catch, |
| 2600 | tok::kw_new, tok::kw_delete) && |
| 2601 | (!Left.Previous || Left.Previous->isNot(tok::period))))) || |
| 2602 | (Style.SpaceBeforeParens == FormatStyle::SBPO_Always && |
| 2603 | (Left.is(tok::identifier) || Left.isFunctionLikeKeyword() || |
| 2604 | Left.is(tok::r_paren) || |
| 2605 | (Left.is(tok::r_square) && Left.MatchingParen && |
| 2606 | Left.MatchingParen->is(TT_LambdaLSquare))) && |
| 2607 | Line.Type != LT_PreprocessorDirective); |
| 2608 | } |
| 2609 | if (Left.is(tok::at) && Right.Tok.getObjCKeywordID() != tok::objc_not_keyword) |
| 2610 | return false; |
| 2611 | if (Right.is(TT_UnaryOperator)) |
| 2612 | return !Left.isOneOf(tok::l_paren, tok::l_square, tok::at) && |
| 2613 | (Left.isNot(tok::colon) || Left.isNot(TT_ObjCMethodExpr)); |
| 2614 | if ((Left.isOneOf(tok::identifier, tok::greater, tok::r_square, |
| 2615 | tok::r_paren) || |
| 2616 | Left.isSimpleTypeSpecifier()) && |
| 2617 | Right.is(tok::l_brace) && Right.getNextNonComment() && |
| 2618 | Right.BlockKind != BK_Block) |
| 2619 | return false; |
| 2620 | if (Left.is(tok::period) || Right.is(tok::period)) |
| 2621 | return false; |
| 2622 | if (Right.is(tok::hash) && Left.is(tok::identifier) && Left.TokenText == "L") |
| 2623 | return false; |
| 2624 | if (Left.is(TT_TemplateCloser) && Left.MatchingParen && |
| 2625 | Left.MatchingParen->Previous && |
| 2626 | (Left.MatchingParen->Previous->is(tok::period) || |
| 2627 | Left.MatchingParen->Previous->is(tok::coloncolon))) |
| 2628 | // Java call to generic function with explicit type: |
| 2629 | // A.<B<C<...>>>DoSomething(); |
| 2630 | // A::<B<C<...>>>DoSomething(); // With a Java 8 method reference. |
| 2631 | return false; |
| 2632 | if (Left.is(TT_TemplateCloser) && Right.is(tok::l_square)) |
| 2633 | return false; |
| 2634 | if (Left.is(tok::l_brace) && Left.endsSequence(TT_DictLiteral, tok::at)) |
| 2635 | // Objective-C dictionary literal -> no space after opening brace. |
| 2636 | return false; |
| 2637 | if (Right.is(tok::r_brace) && Right.MatchingParen && |
| 2638 | Right.MatchingParen->endsSequence(TT_DictLiteral, tok::at)) |
| 2639 | // Objective-C dictionary literal -> no space before closing brace. |
| 2640 | return false; |
| 2641 | return true; |
| 2642 | } |
| 2643 | |
| 2644 | bool TokenAnnotator::spaceRequiredBefore(const AnnotatedLine &Line, |
| 2645 | const FormatToken &Right) { |
| 2646 | const FormatToken &Left = *Right.Previous; |
| 2647 | if (Right.Tok.getIdentifierInfo() && Left.Tok.getIdentifierInfo()) |
| 2648 | return true; // Never ever merge two identifiers. |
| 2649 | if (Style.isCpp()) { |
| 2650 | if (Left.is(tok::kw_operator)) |
| 2651 | return Right.is(tok::coloncolon); |
| 2652 | if (Right.is(tok::l_brace) && Right.BlockKind == BK_BracedInit && |
| 2653 | !Left.opensScope() && Style.SpaceBeforeCpp11BracedList) |
| 2654 | return true; |
| 2655 | } else if (Style.Language == FormatStyle::LK_Proto || |
| 2656 | Style.Language == FormatStyle::LK_TextProto) { |
| 2657 | if (Right.is(tok::period) && |
| 2658 | Left.isOneOf(Keywords.kw_optional, Keywords.kw_required, |
| 2659 | Keywords.kw_repeated, Keywords.kw_extend)) |
| 2660 | return true; |
| 2661 | if (Right.is(tok::l_paren) && |
| 2662 | Left.isOneOf(Keywords.kw_returns, Keywords.kw_option)) |
| 2663 | return true; |
| 2664 | if (Right.isOneOf(tok::l_brace, tok::less) && Left.is(TT_SelectorName)) |
| 2665 | return true; |
| 2666 | // Slashes occur in text protocol extension syntax: [type/type] { ... }. |
| 2667 | if (Left.is(tok::slash) || Right.is(tok::slash)) |
| 2668 | return false; |
| 2669 | if (Left.MatchingParen && |
| 2670 | Left.MatchingParen->is(TT_ProtoExtensionLSquare) && |
| 2671 | Right.isOneOf(tok::l_brace, tok::less)) |
| 2672 | return !Style.Cpp11BracedListStyle; |
| 2673 | // A percent is probably part of a formatting specification, such as %lld. |
| 2674 | if (Left.is(tok::percent)) |
| 2675 | return false; |
| 2676 | // Preserve the existence of a space before a percent for cases like 0x%04x |
| 2677 | // and "%d %d" |
| 2678 | if (Left.is(tok::numeric_constant) && Right.is(tok::percent)) |
| 2679 | return Right.WhitespaceRange.getEnd() != Right.WhitespaceRange.getBegin(); |
| 2680 | } else if (Style.Language == FormatStyle::LK_JavaScript || Style.isCSharp()) { |
| 2681 | if (Left.is(TT_JsFatArrow)) |
| 2682 | return true; |
| 2683 | // for await ( ... |
| 2684 | if (Right.is(tok::l_paren) && Left.is(Keywords.kw_await) && Left.Previous && |
| 2685 | Left.Previous->is(tok::kw_for)) |
| 2686 | return true; |
| 2687 | if (Left.is(Keywords.kw_async) && Right.is(tok::l_paren) && |
| 2688 | Right.MatchingParen) { |
| 2689 | const FormatToken *Next = Right.MatchingParen->getNextNonComment(); |
| 2690 | // An async arrow function, for example: `x = async () => foo();`, |
| 2691 | // as opposed to calling a function called async: `x = async();` |
| 2692 | if (Next && Next->is(TT_JsFatArrow)) |
| 2693 | return true; |
| 2694 | } |
| 2695 | if ((Left.is(TT_TemplateString) && Left.TokenText.endswith("${")) || |
| 2696 | (Right.is(TT_TemplateString) && Right.TokenText.startswith("}"))) |
| 2697 | return false; |
| 2698 | // In tagged template literals ("html`bar baz`"), there is no space between |
| 2699 | // the tag identifier and the template string. getIdentifierInfo makes sure |
| 2700 | // that the identifier is not a pseudo keyword like `yield`, either. |
| 2701 | if (Left.is(tok::identifier) && Keywords.IsJavaScriptIdentifier(Left) && |
| 2702 | Right.is(TT_TemplateString)) |
| 2703 | return false; |
| 2704 | if (Right.is(tok::star) && |
| 2705 | Left.isOneOf(Keywords.kw_function, Keywords.kw_yield)) |
| 2706 | return false; |
| 2707 | if (Right.isOneOf(tok::l_brace, tok::l_square) && |
| 2708 | Left.isOneOf(Keywords.kw_function, Keywords.kw_yield, |
| 2709 | Keywords.kw_extends, Keywords.kw_implements)) |
| 2710 | return true; |
| 2711 | if (Right.is(tok::l_paren)) { |
| 2712 | // JS methods can use some keywords as names (e.g. `delete()`). |
| 2713 | if (Line.MustBeDeclaration && Left.Tok.getIdentifierInfo()) |
| 2714 | return false; |
| 2715 | // Valid JS method names can include keywords, e.g. `foo.delete()` or |
| 2716 | // `bar.instanceof()`. Recognize call positions by preceding period. |
| 2717 | if (Left.Previous && Left.Previous->is(tok::period) && |
| 2718 | Left.Tok.getIdentifierInfo()) |
| 2719 | return false; |
| 2720 | // Additional unary JavaScript operators that need a space after. |
| 2721 | if (Left.isOneOf(tok::kw_throw, Keywords.kw_await, Keywords.kw_typeof, |
| 2722 | tok::kw_void)) |
| 2723 | return true; |
| 2724 | } |
| 2725 | if ((Left.isOneOf(Keywords.kw_let, Keywords.kw_var, Keywords.kw_in, |
| 2726 | tok::kw_const) || |
| 2727 | // "of" is only a keyword if it appears after another identifier |
| 2728 | // (e.g. as "const x of y" in a for loop), or after a destructuring |
| 2729 | // operation (const [x, y] of z, const {a, b} of c). |
| 2730 | (Left.is(Keywords.kw_of) && Left.Previous && |
| 2731 | (Left.Previous->Tok.is(tok::identifier) || |
| 2732 | Left.Previous->isOneOf(tok::r_square, tok::r_brace)))) && |
| 2733 | (!Left.Previous || !Left.Previous->is(tok::period))) |
| 2734 | return true; |
| 2735 | if (Left.isOneOf(tok::kw_for, Keywords.kw_as) && Left.Previous && |
| 2736 | Left.Previous->is(tok::period) && Right.is(tok::l_paren)) |
| 2737 | return false; |
| 2738 | if (Left.is(Keywords.kw_as) && |
| 2739 | Right.isOneOf(tok::l_square, tok::l_brace, tok::l_paren)) |
| 2740 | return true; |
| 2741 | if (Left.is(tok::kw_default) && Left.Previous && |
| 2742 | Left.Previous->is(tok::kw_export)) |
| 2743 | return true; |
| 2744 | if (Left.is(Keywords.kw_is) && Right.is(tok::l_brace)) |
| 2745 | return true; |
| 2746 | if (Right.isOneOf(TT_JsTypeColon, TT_JsTypeOptionalQuestion)) |
| 2747 | return false; |
| 2748 | if (Left.is(TT_JsTypeOperator) || Right.is(TT_JsTypeOperator)) |
| 2749 | return false; |
| 2750 | if ((Left.is(tok::l_brace) || Right.is(tok::r_brace)) && |
| 2751 | Line.First->isOneOf(Keywords.kw_import, tok::kw_export)) |
| 2752 | return false; |
| 2753 | if (Left.is(tok::ellipsis)) |
| 2754 | return false; |
| 2755 | if (Left.is(TT_TemplateCloser) && |
| 2756 | !Right.isOneOf(tok::equal, tok::l_brace, tok::comma, tok::l_square, |
| 2757 | Keywords.kw_implements, Keywords.kw_extends)) |
| 2758 | // Type assertions ('<type>expr') are not followed by whitespace. Other |
| 2759 | // locations that should have whitespace following are identified by the |
| 2760 | // above set of follower tokens. |
| 2761 | return false; |
| 2762 | if (Right.is(TT_JsNonNullAssertion)) |
| 2763 | return false; |
| 2764 | if (Left.is(TT_JsNonNullAssertion) && |
| 2765 | Right.isOneOf(Keywords.kw_as, Keywords.kw_in)) |
| 2766 | return true; // "x! as string", "x! in y" |
| 2767 | } else if (Style.Language == FormatStyle::LK_Java) { |
| 2768 | if (Left.is(tok::r_square) && Right.is(tok::l_brace)) |
| 2769 | return true; |
| 2770 | if (Left.is(Keywords.kw_synchronized) && Right.is(tok::l_paren)) |
| 2771 | return Style.SpaceBeforeParens != FormatStyle::SBPO_Never; |
| 2772 | if ((Left.isOneOf(tok::kw_static, tok::kw_public, tok::kw_private, |
| 2773 | tok::kw_protected) || |
| 2774 | Left.isOneOf(Keywords.kw_final, Keywords.kw_abstract, |
| 2775 | Keywords.kw_native)) && |
| 2776 | Right.is(TT_TemplateOpener)) |
| 2777 | return true; |
| 2778 | } |
| 2779 | if (Left.is(TT_ImplicitStringLiteral)) |
| 2780 | return Right.WhitespaceRange.getBegin() != Right.WhitespaceRange.getEnd(); |
| 2781 | if (Line.Type == LT_ObjCMethodDecl) { |
| 2782 | if (Left.is(TT_ObjCMethodSpecifier)) |
| 2783 | return true; |
| 2784 | if (Left.is(tok::r_paren) && canBeObjCSelectorComponent(Right)) |
| 2785 | // Don't space between ')' and <id> or ')' and 'new'. 'new' is not a |
| 2786 | // keyword in Objective-C, and '+ (instancetype)new;' is a standard class |
| 2787 | // method declaration. |
| 2788 | return false; |
| 2789 | } |
| 2790 | if (Line.Type == LT_ObjCProperty && |
| 2791 | (Right.is(tok::equal) || Left.is(tok::equal))) |
| 2792 | return false; |
| 2793 | |
| 2794 | if (Right.isOneOf(TT_TrailingReturnArrow, TT_LambdaArrow) || |
| 2795 | Left.isOneOf(TT_TrailingReturnArrow, TT_LambdaArrow)) |
| 2796 | return true; |
| 2797 | if (Right.is(TT_OverloadedOperatorLParen)) |
| 2798 | return Style.SpaceBeforeParens == FormatStyle::SBPO_Always; |
| 2799 | if (Left.is(tok::comma)) |
| 2800 | return true; |
| 2801 | if (Right.is(tok::comma)) |
| 2802 | return false; |
| 2803 | if (Right.is(TT_ObjCBlockLParen)) |
| 2804 | return true; |
| 2805 | if (Right.is(TT_CtorInitializerColon)) |
| 2806 | return Style.SpaceBeforeCtorInitializerColon; |
| 2807 | if (Right.is(TT_InheritanceColon) && !Style.SpaceBeforeInheritanceColon) |
| 2808 | return false; |
| 2809 | if (Right.is(TT_RangeBasedForLoopColon) && |
| 2810 | !Style.SpaceBeforeRangeBasedForLoopColon) |
| 2811 | return false; |
| 2812 | if (Right.is(tok::colon)) { |
| 2813 | if (Line.First->isOneOf(tok::kw_case, tok::kw_default) || |
| 2814 | !Right.getNextNonComment() || Right.getNextNonComment()->is(tok::semi)) |
| 2815 | return false; |
| 2816 | if (Right.is(TT_ObjCMethodExpr)) |
| 2817 | return false; |
| 2818 | if (Left.is(tok::question)) |
| 2819 | return false; |
| 2820 | if (Right.is(TT_InlineASMColon) && Left.is(tok::coloncolon)) |
| 2821 | return false; |
| 2822 | if (Right.is(TT_DictLiteral)) |
| 2823 | return Style.SpacesInContainerLiterals; |
| 2824 | if (Right.is(TT_AttributeColon)) |
| 2825 | return false; |
| 2826 | return true; |
| 2827 | } |
| 2828 | if (Left.is(TT_UnaryOperator)) |
| 2829 | return Right.is(TT_BinaryOperator); |
| 2830 | |
| 2831 | // If the next token is a binary operator or a selector name, we have |
| 2832 | // incorrectly classified the parenthesis as a cast. FIXME: Detect correctly. |
| 2833 | if (Left.is(TT_CastRParen)) |
| 2834 | return Style.SpaceAfterCStyleCast || |
| 2835 | Right.isOneOf(TT_BinaryOperator, TT_SelectorName); |
| 2836 | |
| 2837 | if (Left.is(tok::greater) && Right.is(tok::greater)) { |
| 2838 | if (Style.Language == FormatStyle::LK_TextProto || |
| 2839 | (Style.Language == FormatStyle::LK_Proto && Left.is(TT_DictLiteral))) |
| 2840 | return !Style.Cpp11BracedListStyle; |
| 2841 | return Right.is(TT_TemplateCloser) && Left.is(TT_TemplateCloser) && |
| 2842 | (Style.Standard != FormatStyle::LS_Cpp11 || Style.SpacesInAngles); |
| 2843 | } |
| 2844 | if (Right.isOneOf(tok::arrow, tok::arrowstar, tok::periodstar) || |
| 2845 | Left.isOneOf(tok::arrow, tok::period, tok::arrowstar, tok::periodstar) || |
| 2846 | (Right.is(tok::period) && Right.isNot(TT_DesignatedInitializerPeriod))) |
| 2847 | return false; |
| 2848 | if (!Style.SpaceBeforeAssignmentOperators && |
| 2849 | Right.getPrecedence() == prec::Assignment) |
| 2850 | return false; |
| 2851 | if (Style.Language == FormatStyle::LK_Java && Right.is(tok::coloncolon) && |
| 2852 | (Left.is(tok::identifier) || Left.is(tok::kw_this))) |
| 2853 | return false; |
| 2854 | if (Right.is(tok::coloncolon) && Left.is(tok::identifier)) |
| 2855 | // Generally don't remove existing spaces between an identifier and "::". |
| 2856 | // The identifier might actually be a macro name such as ALWAYS_INLINE. If |
| 2857 | // this turns out to be too lenient, add analysis of the identifier itself. |
| 2858 | return Right.WhitespaceRange.getBegin() != Right.WhitespaceRange.getEnd(); |
| 2859 | if (Right.is(tok::coloncolon) && !Left.isOneOf(tok::l_brace, tok::comment)) |
| 2860 | return (Left.is(TT_TemplateOpener) && |
| 2861 | Style.Standard == FormatStyle::LS_Cpp03) || |
| 2862 | !(Left.isOneOf(tok::l_paren, tok::r_paren, tok::l_square, |
| 2863 | tok::kw___super, TT_TemplateCloser, |
| 2864 | TT_TemplateOpener)) || |
| 2865 | (Left.is(tok ::l_paren) && Style.SpacesInParentheses); |
| 2866 | if ((Left.is(TT_TemplateOpener)) != (Right.is(TT_TemplateCloser))) |
| 2867 | return Style.SpacesInAngles; |
| 2868 | // Space before TT_StructuredBindingLSquare. |
| 2869 | if (Right.is(TT_StructuredBindingLSquare)) |
| 2870 | return !Left.isOneOf(tok::amp, tok::ampamp) || |
| 2871 | Style.PointerAlignment != FormatStyle::PAS_Right; |
| 2872 | // Space before & or && following a TT_StructuredBindingLSquare. |
| 2873 | if (Right.Next && Right.Next->is(TT_StructuredBindingLSquare) && |
| 2874 | Right.isOneOf(tok::amp, tok::ampamp)) |
| 2875 | return Style.PointerAlignment != FormatStyle::PAS_Left; |
| 2876 | if ((Right.is(TT_BinaryOperator) && !Left.is(tok::l_paren)) || |
| 2877 | (Left.isOneOf(TT_BinaryOperator, TT_ConditionalExpr) && |
| 2878 | !Right.is(tok::r_paren))) |
| 2879 | return true; |
| 2880 | if (Left.is(TT_TemplateCloser) && Right.is(tok::l_paren) && |
| 2881 | Right.isNot(TT_FunctionTypeLParen)) |
| 2882 | return Style.SpaceBeforeParens == FormatStyle::SBPO_Always; |
| 2883 | if (Right.is(TT_TemplateOpener) && Left.is(tok::r_paren) && |
| 2884 | Left.MatchingParen && Left.MatchingParen->is(TT_OverloadedOperatorLParen)) |
| 2885 | return false; |
| 2886 | if (Right.is(tok::less) && Left.isNot(tok::l_paren) && |
| 2887 | Line.startsWith(tok::hash)) |
| 2888 | return true; |
| 2889 | if (Right.is(TT_TrailingUnaryOperator)) |
| 2890 | return false; |
| 2891 | if (Left.is(TT_RegexLiteral)) |
| 2892 | return false; |
| 2893 | return spaceRequiredBetween(Line, Left, Right); |
| 2894 | } |
| 2895 | |
| 2896 | // Returns 'true' if 'Tok' is a brace we'd want to break before in Allman style. |
| 2897 | static bool isAllmanBrace(const FormatToken &Tok) { |
| 2898 | return Tok.is(tok::l_brace) && Tok.BlockKind == BK_Block && |
| 2899 | !Tok.isOneOf(TT_ObjCBlockLBrace, TT_LambdaLBrace, TT_DictLiteral); |
| 2900 | } |
| 2901 | |
| 2902 | bool TokenAnnotator::mustBreakBefore(const AnnotatedLine &Line, |
| 2903 | const FormatToken &Right) { |
| 2904 | const FormatToken &Left = *Right.Previous; |
| 2905 | if (Right.NewlinesBefore > 1 && Style.MaxEmptyLinesToKeep > 0) |
| 2906 | return true; |
| 2907 | |
| 2908 | if (Style.Language == FormatStyle::LK_JavaScript) { |
| 2909 | // FIXME: This might apply to other languages and token kinds. |
| 2910 | if (Right.is(tok::string_literal) && Left.is(tok::plus) && Left.Previous && |
| 2911 | Left.Previous->is(tok::string_literal)) |
| 2912 | return true; |
| 2913 | if (Left.is(TT_DictLiteral) && Left.is(tok::l_brace) && Line.Level == 0 && |
| 2914 | Left.Previous && Left.Previous->is(tok::equal) && |
| 2915 | Line.First->isOneOf(tok::identifier, Keywords.kw_import, tok::kw_export, |
| 2916 | tok::kw_const) && |
| 2917 | // kw_var/kw_let are pseudo-tokens that are tok::identifier, so match |
| 2918 | // above. |
| 2919 | !Line.First->isOneOf(Keywords.kw_var, Keywords.kw_let)) |
| 2920 | // Object literals on the top level of a file are treated as "enum-style". |
| 2921 | // Each key/value pair is put on a separate line, instead of bin-packing. |
| 2922 | return true; |
| 2923 | if (Left.is(tok::l_brace) && Line.Level == 0 && |
| 2924 | (Line.startsWith(tok::kw_enum) || |
| 2925 | Line.startsWith(tok::kw_const, tok::kw_enum) || |
| 2926 | Line.startsWith(tok::kw_export, tok::kw_enum) || |
| 2927 | Line.startsWith(tok::kw_export, tok::kw_const, tok::kw_enum))) |
| 2928 | // JavaScript top-level enum key/value pairs are put on separate lines |
| 2929 | // instead of bin-packing. |
| 2930 | return true; |
| 2931 | if (Right.is(tok::r_brace) && Left.is(tok::l_brace) && |
| 2932 | !Left.Children.empty()) |
| 2933 | // Support AllowShortFunctionsOnASingleLine for JavaScript. |
| 2934 | return Style.AllowShortFunctionsOnASingleLine == FormatStyle::SFS_None || |
| 2935 | Style.AllowShortFunctionsOnASingleLine == FormatStyle::SFS_Empty || |
| 2936 | (Left.NestingLevel == 0 && Line.Level == 0 && |
| 2937 | Style.AllowShortFunctionsOnASingleLine & |
| 2938 | FormatStyle::SFS_InlineOnly); |
| 2939 | } else if (Style.Language == FormatStyle::LK_Java) { |
| 2940 | if (Right.is(tok::plus) && Left.is(tok::string_literal) && Right.Next && |
| 2941 | Right.Next->is(tok::string_literal)) |
| 2942 | return true; |
| 2943 | } else if (Style.Language == FormatStyle::LK_Cpp || |
| 2944 | Style.Language == FormatStyle::LK_ObjC || |
| 2945 | Style.Language == FormatStyle::LK_Proto || |
| 2946 | Style.Language == FormatStyle::LK_TableGen || |
| 2947 | Style.Language == FormatStyle::LK_TextProto) { |
| 2948 | if (Left.isStringLiteral() && Right.isStringLiteral()) |
| 2949 | return true; |
| 2950 | } |
| 2951 | |
| 2952 | // If the last token before a '}', ']', or ')' is a comma or a trailing |
| 2953 | // comment, the intention is to insert a line break after it in order to make |
| 2954 | // shuffling around entries easier. Import statements, especially in |
| 2955 | // JavaScript, can be an exception to this rule. |
| 2956 | if (Style.JavaScriptWrapImports || Line.Type != LT_ImportStatement) { |
| 2957 | const FormatToken *BeforeClosingBrace = nullptr; |
| 2958 | if ((Left.isOneOf(tok::l_brace, TT_ArrayInitializerLSquare) || |
| 2959 | (Style.Language == FormatStyle::LK_JavaScript && |
| 2960 | Left.is(tok::l_paren))) && |
| 2961 | Left.BlockKind != BK_Block && Left.MatchingParen) |
| 2962 | BeforeClosingBrace = Left.MatchingParen->Previous; |
| 2963 | else if (Right.MatchingParen && |
| 2964 | (Right.MatchingParen->isOneOf(tok::l_brace, |
| 2965 | TT_ArrayInitializerLSquare) || |
| 2966 | (Style.Language == FormatStyle::LK_JavaScript && |
| 2967 | Right.MatchingParen->is(tok::l_paren)))) |
| 2968 | BeforeClosingBrace = &Left; |
| 2969 | if (BeforeClosingBrace && (BeforeClosingBrace->is(tok::comma) || |
| 2970 | BeforeClosingBrace->isTrailingComment())) |
| 2971 | return true; |
| 2972 | } |
| 2973 | |
| 2974 | if (Right.is(tok::comment)) |
| 2975 | return Left.BlockKind != BK_BracedInit && |
| 2976 | Left.isNot(TT_CtorInitializerColon) && |
| 2977 | (Right.NewlinesBefore > 0 && Right.HasUnescapedNewline); |
| 2978 | if (Left.isTrailingComment()) |
| 2979 | return true; |
| 2980 | if (Right.Previous->IsUnterminatedLiteral) |
| 2981 | return true; |
| 2982 | if (Right.is(tok::lessless) && Right.Next && |
| 2983 | Right.Previous->is(tok::string_literal) && |
| 2984 | Right.Next->is(tok::string_literal)) |
| 2985 | return true; |
| 2986 | if (Right.Previous->ClosesTemplateDeclaration && |
| 2987 | Right.Previous->MatchingParen && |
| 2988 | Right.Previous->MatchingParen->NestingLevel == 0 && |
| 2989 | Style.AlwaysBreakTemplateDeclarations == FormatStyle::BTDS_Yes) |
| 2990 | return true; |
| 2991 | if (Right.is(TT_CtorInitializerComma) && |
| 2992 | Style.BreakConstructorInitializers == FormatStyle::BCIS_BeforeComma && |
| 2993 | !Style.ConstructorInitializerAllOnOneLineOrOnePerLine) |
| 2994 | return true; |
| 2995 | if (Right.is(TT_CtorInitializerColon) && |
| 2996 | Style.BreakConstructorInitializers == FormatStyle::BCIS_BeforeComma && |
| 2997 | !Style.ConstructorInitializerAllOnOneLineOrOnePerLine) |
| 2998 | return true; |
| 2999 | // Break only if we have multiple inheritance. |
| 3000 | if (Style.BreakInheritanceList == FormatStyle::BILS_BeforeComma && |
| 3001 | Right.is(TT_InheritanceComma)) |
| 3002 | return true; |
| 3003 | if (Right.is(tok::string_literal) && Right.TokenText.startswith("R\"")) |
| 3004 | // Multiline raw string literals are special wrt. line breaks. The author |
| 3005 | // has made a deliberate choice and might have aligned the contents of the |
| 3006 | // string literal accordingly. Thus, we try keep existing line breaks. |
| 3007 | return Right.IsMultiline && Right.NewlinesBefore > 0; |
| 3008 | if ((Right.Previous->is(tok::l_brace) || |
| 3009 | (Right.Previous->is(tok::less) && Right.Previous->Previous && |
| 3010 | Right.Previous->Previous->is(tok::equal))) && |
| 3011 | Right.NestingLevel == 1 && Style.Language == FormatStyle::LK_Proto) { |
| 3012 | // Don't put enums or option definitions onto single lines in protocol |
| 3013 | // buffers. |
| 3014 | return true; |
| 3015 | } |
| 3016 | if (Right.is(TT_InlineASMBrace)) |
| 3017 | return Right.HasUnescapedNewline; |
| 3018 | if (isAllmanBrace(Left) || isAllmanBrace(Right)) |
| 3019 | return (Line.startsWith(tok::kw_enum) && Style.BraceWrapping.AfterEnum) || |
| 3020 | (Line.startsWith(tok::kw_typedef, tok::kw_enum) && |
| 3021 | Style.BraceWrapping.AfterEnum) || |
| 3022 | (Line.startsWith(tok::kw_class) && Style.BraceWrapping.AfterClass) || |
| 3023 | (Line.startsWith(tok::kw_struct) && Style.BraceWrapping.AfterStruct); |
| 3024 | if (Left.is(TT_ObjCBlockLBrace) && !Style.AllowShortBlocksOnASingleLine) |
| 3025 | return true; |
| 3026 | |
| 3027 | if (Left.is(TT_LambdaLBrace)) { |
| 3028 | if (Left.MatchingParen && Left.MatchingParen->Next && |
| 3029 | Left.MatchingParen->Next->isOneOf(tok::comma, tok::r_paren) && |
| 3030 | Style.AllowShortLambdasOnASingleLine == FormatStyle::SLS_Inline) |
| 3031 | return false; |
| 3032 | |
| 3033 | if (Style.AllowShortLambdasOnASingleLine == FormatStyle::SLS_None || |
| 3034 | Style.AllowShortLambdasOnASingleLine == FormatStyle::SLS_Inline || |
| 3035 | (!Left.Children.empty() && |
| 3036 | Style.AllowShortLambdasOnASingleLine == FormatStyle::SLS_Empty)) |
| 3037 | return true; |
| 3038 | } |
| 3039 | |
| 3040 | // Put multiple C# attributes on a new line. |
| 3041 | if (Style.isCSharp() && |
| 3042 | ((Left.is(TT_AttributeSquare) && Left.is(tok::r_square)) || |
| 3043 | (Left.is(tok::r_square) && Right.is(TT_AttributeSquare) && |
| 3044 | Right.is(tok::l_square)))) |
| 3045 | return true; |
| 3046 | |
| 3047 | // Put multiple Java annotation on a new line. |
| 3048 | if ((Style.Language == FormatStyle::LK_Java || |
| 3049 | Style.Language == FormatStyle::LK_JavaScript) && |
| 3050 | Left.is(TT_LeadingJavaAnnotation) && |
| 3051 | Right.isNot(TT_LeadingJavaAnnotation) && Right.isNot(tok::l_paren) && |
| 3052 | (Line.Last->is(tok::l_brace) || Style.BreakAfterJavaFieldAnnotations)) |
| 3053 | return true; |
| 3054 | |
| 3055 | if (Right.is(TT_ProtoExtensionLSquare)) |
| 3056 | return true; |
| 3057 | |
| 3058 | // In text proto instances if a submessage contains at least 2 entries and at |
| 3059 | // least one of them is a submessage, like A { ... B { ... } ... }, |
| 3060 | // put all of the entries of A on separate lines by forcing the selector of |
| 3061 | // the submessage B to be put on a newline. |
| 3062 | // |
| 3063 | // Example: these can stay on one line: |
| 3064 | // a { scalar_1: 1 scalar_2: 2 } |
| 3065 | // a { b { key: value } } |
| 3066 | // |
| 3067 | // and these entries need to be on a new line even if putting them all in one |
| 3068 | // line is under the column limit: |
| 3069 | // a { |
| 3070 | // scalar: 1 |
| 3071 | // b { key: value } |
| 3072 | // } |
| 3073 | // |
| 3074 | // We enforce this by breaking before a submessage field that has previous |
| 3075 | // siblings, *and* breaking before a field that follows a submessage field. |
| 3076 | // |
| 3077 | // Be careful to exclude the case [proto.ext] { ... } since the `]` is |
| 3078 | // the TT_SelectorName there, but we don't want to break inside the brackets. |
| 3079 | // |
| 3080 | // Another edge case is @submessage { key: value }, which is a common |
| 3081 | // substitution placeholder. In this case we want to keep `@` and `submessage` |
| 3082 | // together. |
| 3083 | // |
| 3084 | // We ensure elsewhere that extensions are always on their own line. |
| 3085 | if ((Style.Language == FormatStyle::LK_Proto || |
| 3086 | Style.Language == FormatStyle::LK_TextProto) && |
| 3087 | Right.is(TT_SelectorName) && !Right.is(tok::r_square) && Right.Next) { |
| 3088 | // Keep `@submessage` together in: |
| 3089 | // @submessage { key: value } |
| 3090 | if (Right.Previous && Right.Previous->is(tok::at)) |
| 3091 | return false; |
| 3092 | // Look for the scope opener after selector in cases like: |
| 3093 | // selector { ... |
| 3094 | // selector: { ... |
| 3095 | // selector: @base { ... |
| 3096 | FormatToken *LBrace = Right.Next; |
| 3097 | if (LBrace && LBrace->is(tok::colon)) { |
| 3098 | LBrace = LBrace->Next; |
| 3099 | if (LBrace && LBrace->is(tok::at)) { |
| 3100 | LBrace = LBrace->Next; |
| 3101 | if (LBrace) |
| 3102 | LBrace = LBrace->Next; |
| 3103 | } |
| 3104 | } |
| 3105 | if (LBrace && |
| 3106 | // The scope opener is one of {, [, <: |
| 3107 | // selector { ... } |
| 3108 | // selector [ ... ] |
| 3109 | // selector < ... > |
| 3110 | // |
| 3111 | // In case of selector { ... }, the l_brace is TT_DictLiteral. |
| 3112 | // In case of an empty selector {}, the l_brace is not TT_DictLiteral, |
| 3113 | // so we check for immediately following r_brace. |
| 3114 | ((LBrace->is(tok::l_brace) && |
| 3115 | (LBrace->is(TT_DictLiteral) || |
| 3116 | (LBrace->Next && LBrace->Next->is(tok::r_brace)))) || |
| 3117 | LBrace->is(TT_ArrayInitializerLSquare) || LBrace->is(tok::less))) { |
| 3118 | // If Left.ParameterCount is 0, then this submessage entry is not the |
| 3119 | // first in its parent submessage, and we want to break before this entry. |
| 3120 | // If Left.ParameterCount is greater than 0, then its parent submessage |
| 3121 | // might contain 1 or more entries and we want to break before this entry |
| 3122 | // if it contains at least 2 entries. We deal with this case later by |
| 3123 | // detecting and breaking before the next entry in the parent submessage. |
| 3124 | if (Left.ParameterCount == 0) |
| 3125 | return true; |
| 3126 | // However, if this submessage is the first entry in its parent |
| 3127 | // submessage, Left.ParameterCount might be 1 in some cases. |
| 3128 | // We deal with this case later by detecting an entry |
| 3129 | // following a closing paren of this submessage. |
| 3130 | } |
| 3131 | |
| 3132 | // If this is an entry immediately following a submessage, it will be |
| 3133 | // preceded by a closing paren of that submessage, like in: |
| 3134 | // left---. .---right |
| 3135 | // v v |
| 3136 | // sub: { ... } key: value |
| 3137 | // If there was a comment between `}` an `key` above, then `key` would be |
| 3138 | // put on a new line anyways. |
| 3139 | if (Left.isOneOf(tok::r_brace, tok::greater, tok::r_square)) |
| 3140 | return true; |
| 3141 | } |
| 3142 | |
| 3143 | // Deal with lambda arguments in C++ - we want consistent line breaks whether |
| 3144 | // they happen to be at arg0, arg1 or argN. The selection is a bit nuanced |
| 3145 | // as aggressive line breaks are placed when the lambda is not the last arg. |
| 3146 | if ((Style.Language == FormatStyle::LK_Cpp || |
| 3147 | Style.Language == FormatStyle::LK_ObjC) && |
| 3148 | Left.is(tok::l_paren) && Left.BlockParameterCount > 0 && |
| 3149 | !Right.isOneOf(tok::l_paren, TT_LambdaLSquare)) { |
| 3150 | // Multiple lambdas in the same function call force line breaks. |
| 3151 | if (Left.BlockParameterCount > 1) |
| 3152 | return true; |
| 3153 | |
| 3154 | // A lambda followed by another arg forces a line break. |
| 3155 | if (!Left.Role) |
| 3156 | return false; |
| 3157 | auto Comma = Left.Role->lastComma(); |
| 3158 | if (!Comma) |
| 3159 | return false; |
| 3160 | auto Next = Comma->getNextNonComment(); |
| 3161 | if (!Next) |
| 3162 | return false; |
| 3163 | if (!Next->isOneOf(TT_LambdaLSquare, tok::l_brace, tok::caret)) |
| 3164 | return true; |
| 3165 | } |
| 3166 | |
| 3167 | return false; |
| 3168 | } |
| 3169 | |
| 3170 | bool TokenAnnotator::canBreakBefore(const AnnotatedLine &Line, |
| 3171 | const FormatToken &Right) { |
| 3172 | const FormatToken &Left = *Right.Previous; |
| 3173 | |
| 3174 | // Language-specific stuff. |
| 3175 | if (Style.Language == FormatStyle::LK_Java) { |
| 3176 | if (Left.isOneOf(Keywords.kw_throws, Keywords.kw_extends, |
| 3177 | Keywords.kw_implements)) |
| 3178 | return false; |
| 3179 | if (Right.isOneOf(Keywords.kw_throws, Keywords.kw_extends, |
| 3180 | Keywords.kw_implements)) |
| 3181 | return true; |
| 3182 | } else if (Style.Language == FormatStyle::LK_JavaScript) { |
| 3183 | const FormatToken *NonComment = Right.getPreviousNonComment(); |
| 3184 | if (NonComment && |
| 3185 | NonComment->isOneOf( |
| 3186 | tok::kw_return, Keywords.kw_yield, tok::kw_continue, tok::kw_break, |
| 3187 | tok::kw_throw, Keywords.kw_interface, Keywords.kw_type, |
| 3188 | tok::kw_static, tok::kw_public, tok::kw_private, tok::kw_protected, |
| 3189 | Keywords.kw_readonly, Keywords.kw_abstract, Keywords.kw_get, |
| 3190 | Keywords.kw_set, Keywords.kw_async, Keywords.kw_await)) |
| 3191 | return false; // Otherwise automatic semicolon insertion would trigger. |
| 3192 | if (Right.NestingLevel == 0 && |
| 3193 | (Left.Tok.getIdentifierInfo() || |
| 3194 | Left.isOneOf(tok::r_square, tok::r_paren)) && |
| 3195 | Right.isOneOf(tok::l_square, tok::l_paren)) |
| 3196 | return false; // Otherwise automatic semicolon insertion would trigger. |
| 3197 | if (Left.is(TT_JsFatArrow) && Right.is(tok::l_brace)) |
| 3198 | return false; |
| 3199 | if (Left.is(TT_JsTypeColon)) |
| 3200 | return true; |
| 3201 | // Don't wrap between ":" and "!" of a strict prop init ("field!: type;"). |
| 3202 | if (Left.is(tok::exclaim) && Right.is(tok::colon)) |
| 3203 | return false; |
| 3204 | // Look for is type annotations like: |
| 3205 | // function f(): a is B { ... } |
| 3206 | // Do not break before is in these cases. |
| 3207 | if (Right.is(Keywords.kw_is)) { |
| 3208 | const FormatToken *Next = Right.getNextNonComment(); |
| 3209 | // If `is` is followed by a colon, it's likely that it's a dict key, so |
| 3210 | // ignore it for this check. |
| 3211 | // For example this is common in Polymer: |
| 3212 | // Polymer({ |
| 3213 | // is: 'name', |
| 3214 | // ... |
| 3215 | // }); |
| 3216 | if (!Next || !Next->is(tok::colon)) |
| 3217 | return false; |
| 3218 | } |
| 3219 | if (Left.is(Keywords.kw_in)) |
| 3220 | return Style.BreakBeforeBinaryOperators == FormatStyle::BOS_None; |
| 3221 | if (Right.is(Keywords.kw_in)) |
| 3222 | return Style.BreakBeforeBinaryOperators != FormatStyle::BOS_None; |
| 3223 | if (Right.is(Keywords.kw_as)) |
| 3224 | return false; // must not break before as in 'x as type' casts |
| 3225 | if (Right.isOneOf(Keywords.kw_extends, Keywords.kw_infer)) { |
| 3226 | // extends and infer can appear as keywords in conditional types: |
| 3227 | // https://www.typescriptlang.org/docs/handbook/release-notes/typescript-2-8.html#conditional-types |
| 3228 | // do not break before them, as the expressions are subject to ASI. |
| 3229 | return false; |
| 3230 | } |
| 3231 | if (Left.is(Keywords.kw_as)) |
| 3232 | return true; |
| 3233 | if (Left.is(TT_JsNonNullAssertion)) |
| 3234 | return true; |
| 3235 | if (Left.is(Keywords.kw_declare) && |
| 3236 | Right.isOneOf(Keywords.kw_module, tok::kw_namespace, |
| 3237 | Keywords.kw_function, tok::kw_class, tok::kw_enum, |
| 3238 | Keywords.kw_interface, Keywords.kw_type, Keywords.kw_var, |
| 3239 | Keywords.kw_let, tok::kw_const)) |
| 3240 | // See grammar for 'declare' statements at: |
| 3241 | // https://github.com/Microsoft/TypeScript/blob/master/doc/spec.md#A.10 |
| 3242 | return false; |
| 3243 | if (Left.isOneOf(Keywords.kw_module, tok::kw_namespace) && |
| 3244 | Right.isOneOf(tok::identifier, tok::string_literal)) |
| 3245 | return false; // must not break in "module foo { ...}" |
| 3246 | if (Right.is(TT_TemplateString) && Right.closesScope()) |
| 3247 | return false; |
| 3248 | // Don't split tagged template literal so there is a break between the tag |
| 3249 | // identifier and template string. |
| 3250 | if (Left.is(tok::identifier) && Right.is(TT_TemplateString)) { |
| 3251 | return false; |
| 3252 | } |
| 3253 | if (Left.is(TT_TemplateString) && Left.opensScope()) |
| 3254 | return true; |
| 3255 | } |
| 3256 | |
| 3257 | if (Left.is(tok::at)) |
| 3258 | return false; |
| 3259 | if (Left.Tok.getObjCKeywordID() == tok::objc_interface) |
| 3260 | return false; |
| 3261 | if (Left.isOneOf(TT_JavaAnnotation, TT_LeadingJavaAnnotation)) |
| 3262 | return !Right.is(tok::l_paren); |
| 3263 | if (Right.is(TT_PointerOrReference)) |
| 3264 | return Line.IsMultiVariableDeclStmt || |
| 3265 | (Style.PointerAlignment == FormatStyle::PAS_Right && |
| 3266 | (!Right.Next || Right.Next->isNot(TT_FunctionDeclarationName))); |
| 3267 | if (Right.isOneOf(TT_StartOfName, TT_FunctionDeclarationName) || |
| 3268 | Right.is(tok::kw_operator)) |
| 3269 | return true; |
| 3270 | if (Left.is(TT_PointerOrReference)) |
| 3271 | return false; |
| 3272 | if (Right.isTrailingComment()) |
| 3273 | // We rely on MustBreakBefore being set correctly here as we should not |
| 3274 | // change the "binding" behavior of a comment. |
| 3275 | // The first comment in a braced lists is always interpreted as belonging to |
| 3276 | // the first list element. Otherwise, it should be placed outside of the |
| 3277 | // list. |
| 3278 | return Left.BlockKind == BK_BracedInit || |
| 3279 | (Left.is(TT_CtorInitializerColon) && |
| 3280 | Style.BreakConstructorInitializers == FormatStyle::BCIS_AfterColon); |
| 3281 | if (Left.is(tok::question) && Right.is(tok::colon)) |
| 3282 | return false; |
| 3283 | if (Right.is(TT_ConditionalExpr) || Right.is(tok::question)) |
| 3284 | return Style.BreakBeforeTernaryOperators; |
| 3285 | if (Left.is(TT_ConditionalExpr) || Left.is(tok::question)) |
| 3286 | return !Style.BreakBeforeTernaryOperators; |
| 3287 | if (Left.is(TT_InheritanceColon)) |
| 3288 | return Style.BreakInheritanceList == FormatStyle::BILS_AfterColon; |
| 3289 | if (Right.is(TT_InheritanceColon)) |
| 3290 | return Style.BreakInheritanceList != FormatStyle::BILS_AfterColon; |
| 3291 | if (Right.is(TT_ObjCMethodExpr) && !Right.is(tok::r_square) && |
| 3292 | Left.isNot(TT_SelectorName)) |
| 3293 | return true; |
| 3294 | |
| 3295 | if (Right.is(tok::colon) && |
| 3296 | !Right.isOneOf(TT_CtorInitializerColon, TT_InlineASMColon)) |
| 3297 | return false; |
| 3298 | if (Left.is(tok::colon) && Left.isOneOf(TT_DictLiteral, TT_ObjCMethodExpr)) { |
| 3299 | if (Style.Language == FormatStyle::LK_Proto || |
| 3300 | Style.Language == FormatStyle::LK_TextProto) { |
| 3301 | if (!Style.AlwaysBreakBeforeMultilineStrings && Right.isStringLiteral()) |
| 3302 | return false; |
| 3303 | // Prevent cases like: |
| 3304 | // |
| 3305 | // submessage: |
| 3306 | // { key: valueeeeeeeeeeee } |
| 3307 | // |
| 3308 | // when the snippet does not fit into one line. |
| 3309 | // Prefer: |
| 3310 | // |
| 3311 | // submessage: { |
| 3312 | // key: valueeeeeeeeeeee |
| 3313 | // } |
| 3314 | // |
| 3315 | // instead, even if it is longer by one line. |
| 3316 | // |
| 3317 | // Note that this allows allows the "{" to go over the column limit |
| 3318 | // when the column limit is just between ":" and "{", but that does |
| 3319 | // not happen too often and alternative formattings in this case are |
| 3320 | // not much better. |
| 3321 | // |
| 3322 | // The code covers the cases: |
| 3323 | // |
| 3324 | // submessage: { ... } |
| 3325 | // submessage: < ... > |
| 3326 | // repeated: [ ... ] |
| 3327 | if (((Right.is(tok::l_brace) || Right.is(tok::less)) && |
| 3328 | Right.is(TT_DictLiteral)) || |
| 3329 | Right.is(TT_ArrayInitializerLSquare)) |
| 3330 | return false; |
| 3331 | } |
| 3332 | return true; |
| 3333 | } |
| 3334 | if (Right.is(tok::r_square) && Right.MatchingParen && |
| 3335 | Right.MatchingParen->is(TT_ProtoExtensionLSquare)) |
| 3336 | return false; |
| 3337 | if (Right.is(TT_SelectorName) || (Right.is(tok::identifier) && Right.Next && |
| 3338 | Right.Next->is(TT_ObjCMethodExpr))) |
| 3339 | return Left.isNot(tok::period); // FIXME: Properly parse ObjC calls. |
| 3340 | if (Left.is(tok::r_paren) && Line.Type == LT_ObjCProperty) |
| 3341 | return true; |
| 3342 | if (Left.ClosesTemplateDeclaration || Left.is(TT_FunctionAnnotationRParen)) |
| 3343 | return true; |
| 3344 | if (Right.isOneOf(TT_RangeBasedForLoopColon, TT_OverloadedOperatorLParen, |
| 3345 | TT_OverloadedOperator)) |
| 3346 | return false; |
| 3347 | if (Left.is(TT_RangeBasedForLoopColon)) |
| 3348 | return true; |
| 3349 | if (Right.is(TT_RangeBasedForLoopColon)) |
| 3350 | return false; |
| 3351 | if (Left.is(TT_TemplateCloser) && Right.is(TT_TemplateOpener)) |
| 3352 | return true; |
| 3353 | if (Left.isOneOf(TT_TemplateCloser, TT_UnaryOperator) || |
| 3354 | Left.is(tok::kw_operator)) |
| 3355 | return false; |
| 3356 | if (Left.is(tok::equal) && !Right.isOneOf(tok::kw_default, tok::kw_delete) && |
| 3357 | Line.Type == LT_VirtualFunctionDecl && Left.NestingLevel == 0) |
| 3358 | return false; |
| 3359 | if (Left.is(tok::equal) && Right.is(tok::l_brace) && |
| 3360 | !Style.Cpp11BracedListStyle) |
| 3361 | return false; |
| 3362 | if (Left.is(tok::l_paren) && Left.is(TT_AttributeParen)) |
| 3363 | return false; |
| 3364 | if (Left.is(tok::l_paren) && Left.Previous && |
| 3365 | (Left.Previous->isOneOf(TT_BinaryOperator, TT_CastRParen))) |
| 3366 | return false; |
| 3367 | if (Right.is(TT_ImplicitStringLiteral)) |
| 3368 | return false; |
| 3369 | |
| 3370 | if (Right.is(tok::r_paren) || Right.is(TT_TemplateCloser)) |
| 3371 | return false; |
| 3372 | if (Right.is(tok::r_square) && Right.MatchingParen && |
| 3373 | Right.MatchingParen->is(TT_LambdaLSquare)) |
| 3374 | return false; |
| 3375 | |
| 3376 | // We only break before r_brace if there was a corresponding break before |
| 3377 | // the l_brace, which is tracked by BreakBeforeClosingBrace. |
| 3378 | if (Right.is(tok::r_brace)) |
| 3379 | return Right.MatchingParen && Right.MatchingParen->BlockKind == BK_Block; |
| 3380 | |
| 3381 | // Allow breaking after a trailing annotation, e.g. after a method |
| 3382 | // declaration. |
| 3383 | if (Left.is(TT_TrailingAnnotation)) |
| 3384 | return !Right.isOneOf(tok::l_brace, tok::semi, tok::equal, tok::l_paren, |
| 3385 | tok::less, tok::coloncolon); |
| 3386 | |
| 3387 | if (Right.is(tok::kw___attribute) || |
| 3388 | (Right.is(tok::l_square) && Right.is(TT_AttributeSquare))) |
| 3389 | return true; |
| 3390 | |
| 3391 | if (Left.is(tok::identifier) && Right.is(tok::string_literal)) |
| 3392 | return true; |
| 3393 | |
| 3394 | if (Right.is(tok::identifier) && Right.Next && Right.Next->is(TT_DictLiteral)) |
| 3395 | return true; |
| 3396 | |
| 3397 | if (Left.is(TT_CtorInitializerColon)) |
| 3398 | return Style.BreakConstructorInitializers == FormatStyle::BCIS_AfterColon; |
| 3399 | if (Right.is(TT_CtorInitializerColon)) |
| 3400 | return Style.BreakConstructorInitializers != FormatStyle::BCIS_AfterColon; |
| 3401 | if (Left.is(TT_CtorInitializerComma) && |
| 3402 | Style.BreakConstructorInitializers == FormatStyle::BCIS_BeforeComma) |
| 3403 | return false; |
| 3404 | if (Right.is(TT_CtorInitializerComma) && |
| 3405 | Style.BreakConstructorInitializers == FormatStyle::BCIS_BeforeComma) |
| 3406 | return true; |
| 3407 | if (Left.is(TT_InheritanceComma) && |
| 3408 | Style.BreakInheritanceList == FormatStyle::BILS_BeforeComma) |
| 3409 | return false; |
| 3410 | if (Right.is(TT_InheritanceComma) && |
| 3411 | Style.BreakInheritanceList == FormatStyle::BILS_BeforeComma) |
| 3412 | return true; |
| 3413 | if ((Left.is(tok::greater) && Right.is(tok::greater)) || |
| 3414 | (Left.is(tok::less) && Right.is(tok::less))) |
| 3415 | return false; |
| 3416 | if (Right.is(TT_BinaryOperator) && |
| 3417 | Style.BreakBeforeBinaryOperators != FormatStyle::BOS_None && |
| 3418 | (Style.BreakBeforeBinaryOperators == FormatStyle::BOS_All || |
| 3419 | Right.getPrecedence() != prec::Assignment)) |
| 3420 | return true; |
| 3421 | if (Left.is(TT_ArrayInitializerLSquare)) |
| 3422 | return true; |
| 3423 | if (Right.is(tok::kw_typename) && Left.isNot(tok::kw_const)) |
| 3424 | return true; |
| 3425 | if ((Left.isBinaryOperator() || Left.is(TT_BinaryOperator)) && |
| 3426 | !Left.isOneOf(tok::arrowstar, tok::lessless) && |
| 3427 | Style.BreakBeforeBinaryOperators != FormatStyle::BOS_All && |
| 3428 | (Style.BreakBeforeBinaryOperators == FormatStyle::BOS_None || |
| 3429 | Left.getPrecedence() == prec::Assignment)) |
| 3430 | return true; |
| 3431 | if ((Left.is(TT_AttributeSquare) && Right.is(tok::l_square)) || |
| 3432 | (Left.is(tok::r_square) && Right.is(TT_AttributeSquare))) |
| 3433 | return false; |
| 3434 | return Left.isOneOf(tok::comma, tok::coloncolon, tok::semi, tok::l_brace, |
| 3435 | tok::kw_class, tok::kw_struct, tok::comment) || |
| 3436 | Right.isMemberAccess() || |
| 3437 | Right.isOneOf(TT_TrailingReturnArrow, TT_LambdaArrow, tok::lessless, |
| 3438 | tok::colon, tok::l_square, tok::at) || |
| 3439 | (Left.is(tok::r_paren) && |
| 3440 | Right.isOneOf(tok::identifier, tok::kw_const)) || |
| 3441 | (Left.is(tok::l_paren) && !Right.is(tok::r_paren)) || |
| 3442 | (Left.is(TT_TemplateOpener) && !Right.is(TT_TemplateCloser)); |
| 3443 | } |
| 3444 | |
| 3445 | void TokenAnnotator::printDebugInfo(const AnnotatedLine &Line) { |
| 3446 | llvm::errs() << "AnnotatedTokens(L=" << Line.Level << "):\n"; |
| 3447 | const FormatToken *Tok = Line.First; |
| 3448 | while (Tok) { |
| 3449 | llvm::errs() << " M=" << Tok->MustBreakBefore |
| 3450 | << " C=" << Tok->CanBreakBefore |
| 3451 | << " T=" << getTokenTypeName(Tok->Type) |
| 3452 | << " S=" << Tok->SpacesRequiredBefore |
| 3453 | << " B=" << Tok->BlockParameterCount |
| 3454 | << " BK=" << Tok->BlockKind << " P=" << Tok->SplitPenalty |
| 3455 | << " Name=" << Tok->Tok.getName() << " L=" << Tok->TotalLength |
| 3456 | << " PPK=" << Tok->PackingKind << " FakeLParens="; |
| 3457 | for (unsigned i = 0, e = Tok->FakeLParens.size(); i != e; ++i) |
| 3458 | llvm::errs() << Tok->FakeLParens[i] << "/"; |
| 3459 | llvm::errs() << " FakeRParens=" << Tok->FakeRParens; |
| 3460 | llvm::errs() << " II=" << Tok->Tok.getIdentifierInfo(); |
| 3461 | llvm::errs() << " Text='" << Tok->TokenText << "'\n"; |
| 3462 | if (!Tok->Next) |
| 3463 | assert(Tok == Line.Last); |
| 3464 | Tok = Tok->Next; |
| 3465 | } |
| 3466 | llvm::errs() << "----\n"; |
| 3467 | } |
| 3468 | |
| 3469 | } // namespace format |
| 3470 | } // namespace clang |
| 3471 | |