TokenAnnotator.cpp revision d71b15badeecdc049440103ef044f9cdf5e1359c
132d28ee6061930b2a9c170cbaaeea028c88a7b89Daniel Jasper//===--- TokenAnnotator.cpp - Format C++ code -----------------------------===// 232d28ee6061930b2a9c170cbaaeea028c88a7b89Daniel Jasper// 332d28ee6061930b2a9c170cbaaeea028c88a7b89Daniel Jasper// The LLVM Compiler Infrastructure 432d28ee6061930b2a9c170cbaaeea028c88a7b89Daniel Jasper// 532d28ee6061930b2a9c170cbaaeea028c88a7b89Daniel Jasper// This file is distributed under the University of Illinois Open Source 632d28ee6061930b2a9c170cbaaeea028c88a7b89Daniel Jasper// License. See LICENSE.TXT for details. 732d28ee6061930b2a9c170cbaaeea028c88a7b89Daniel Jasper// 832d28ee6061930b2a9c170cbaaeea028c88a7b89Daniel Jasper//===----------------------------------------------------------------------===// 932d28ee6061930b2a9c170cbaaeea028c88a7b89Daniel Jasper/// 1032d28ee6061930b2a9c170cbaaeea028c88a7b89Daniel Jasper/// \file 1132d28ee6061930b2a9c170cbaaeea028c88a7b89Daniel Jasper/// \brief This file implements a token annotator, i.e. creates 1232d28ee6061930b2a9c170cbaaeea028c88a7b89Daniel Jasper/// \c AnnotatedTokens out of \c FormatTokens with required extra information. 1332d28ee6061930b2a9c170cbaaeea028c88a7b89Daniel Jasper/// 1432d28ee6061930b2a9c170cbaaeea028c88a7b89Daniel Jasper//===----------------------------------------------------------------------===// 1532d28ee6061930b2a9c170cbaaeea028c88a7b89Daniel Jasper 1632d28ee6061930b2a9c170cbaaeea028c88a7b89Daniel Jasper#include "TokenAnnotator.h" 1732d28ee6061930b2a9c170cbaaeea028c88a7b89Daniel Jasper#include "clang/Basic/SourceManager.h" 18bf71ba2988b34c45af968f0965e28ac952e4a15fDaniel Jasper#include "llvm/Support/Debug.h" 1932d28ee6061930b2a9c170cbaaeea028c88a7b89Daniel Jasper 2032d28ee6061930b2a9c170cbaaeea028c88a7b89Daniel Jaspernamespace clang { 2132d28ee6061930b2a9c170cbaaeea028c88a7b89Daniel Jaspernamespace format { 2232d28ee6061930b2a9c170cbaaeea028c88a7b89Daniel Jasper 2332d28ee6061930b2a9c170cbaaeea028c88a7b89Daniel Jasper/// \brief A parser that gathers additional information about tokens. 2432d28ee6061930b2a9c170cbaaeea028c88a7b89Daniel Jasper/// 253fd9ccdd9f8d259bcf518e7056cfd419d992e984Alexander Kornienko/// The \c TokenAnnotator tries to match parenthesis and square brakets and 2632d28ee6061930b2a9c170cbaaeea028c88a7b89Daniel Jasper/// store a parenthesis levels. It also tries to resolve matching "<" and ">" 2732d28ee6061930b2a9c170cbaaeea028c88a7b89Daniel Jasper/// into template parameter lists. 2832d28ee6061930b2a9c170cbaaeea028c88a7b89Daniel Jasperclass AnnotatingParser { 2932d28ee6061930b2a9c170cbaaeea028c88a7b89Daniel Jasperpublic: 3000895106f9ed602af67984ec4d225a0cdc8c12afAlexander Kornienko AnnotatingParser(AnnotatedLine &Line, IdentifierInfo &Ident_in) 3100895106f9ed602af67984ec4d225a0cdc8c12afAlexander Kornienko : Line(Line), CurrentToken(Line.First), KeywordVirtualFound(false), 3200895106f9ed602af67984ec4d225a0cdc8c12afAlexander Kornienko NameFound(false), Ident_in(Ident_in) { 33923ebef120a37122ce50722a85cbe42c0c2dab53Daniel Jasper Contexts.push_back(Context(tok::unknown, 1, /*IsExpression=*/ false)); 3432d28ee6061930b2a9c170cbaaeea028c88a7b89Daniel Jasper } 3532d28ee6061930b2a9c170cbaaeea028c88a7b89Daniel Jasper 3695e8e468af284afb11ec1f994d0b5076fc7423d9Nico Weberprivate: 3732d28ee6061930b2a9c170cbaaeea028c88a7b89Daniel Jasper bool parseAngle() { 3832d28ee6061930b2a9c170cbaaeea028c88a7b89Daniel Jasper if (CurrentToken == NULL) 3932d28ee6061930b2a9c170cbaaeea028c88a7b89Daniel Jasper return false; 40923ebef120a37122ce50722a85cbe42c0c2dab53Daniel Jasper ScopedContextCreator ContextCreator(*this, tok::less, 10); 41b398701e4cbb9a55d90a60e3f4f4bc577446d098Manuel Klimek FormatToken *Left = CurrentToken->Previous; 424e778091b0ff42895644ca8bef30f1a91ba6b32bDaniel Jasper Contexts.back().IsExpression = false; 4332d28ee6061930b2a9c170cbaaeea028c88a7b89Daniel Jasper while (CurrentToken != NULL) { 4432d28ee6061930b2a9c170cbaaeea028c88a7b89Daniel Jasper if (CurrentToken->is(tok::greater)) { 4532d28ee6061930b2a9c170cbaaeea028c88a7b89Daniel Jasper Left->MatchingParen = CurrentToken; 4632d28ee6061930b2a9c170cbaaeea028c88a7b89Daniel Jasper CurrentToken->MatchingParen = Left; 4732d28ee6061930b2a9c170cbaaeea028c88a7b89Daniel Jasper CurrentToken->Type = TT_TemplateCloser; 4832d28ee6061930b2a9c170cbaaeea028c88a7b89Daniel Jasper next(); 4932d28ee6061930b2a9c170cbaaeea028c88a7b89Daniel Jasper return true; 5032d28ee6061930b2a9c170cbaaeea028c88a7b89Daniel Jasper } 51e74de28ec3692986f3467a7f160a0e293277fa7eAlexander Kornienko if (CurrentToken->isOneOf(tok::r_paren, tok::r_square, tok::r_brace, 525d823e3a00a3e21a0823288e6dee26a93758332bDaniel Jasper tok::question, tok::colon)) 535d823e3a00a3e21a0823288e6dee26a93758332bDaniel Jasper return false; 540348be0c78781c5ddb8c271976812705410c731aDaniel Jasper // If a && or || is found and interpreted as a binary operator, this set 5515f33f03e742fb6567e4789996fa0391a8e18068Daniel Jasper // of angles is likely part of something like "a < b && c > d". If the 560348be0c78781c5ddb8c271976812705410c731aDaniel Jasper // angles are inside an expression, the ||/&& might also be a binary 570348be0c78781c5ddb8c271976812705410c731aDaniel Jasper // operator that was misinterpreted because we are parsing template 580348be0c78781c5ddb8c271976812705410c731aDaniel Jasper // parameters. 590348be0c78781c5ddb8c271976812705410c731aDaniel Jasper // FIXME: This is getting out of hand, write a decent parser. 60b398701e4cbb9a55d90a60e3f4f4bc577446d098Manuel Klimek if (CurrentToken->Previous->isOneOf(tok::pipepipe, tok::ampamp) && 610348be0c78781c5ddb8c271976812705410c731aDaniel Jasper (CurrentToken->Previous->Type == TT_BinaryOperator || 620348be0c78781c5ddb8c271976812705410c731aDaniel Jasper Contexts[Contexts.size() - 2].IsExpression) && 63b398701e4cbb9a55d90a60e3f4f4bc577446d098Manuel Klimek Line.First->isNot(tok::kw_template)) 6432d28ee6061930b2a9c170cbaaeea028c88a7b89Daniel Jasper return false; 659fc56f2636137fcde8acb38865555ed6c7b84dfdDaniel Jasper updateParameterCount(Left, CurrentToken); 6632d28ee6061930b2a9c170cbaaeea028c88a7b89Daniel Jasper if (!consumeToken()) 6732d28ee6061930b2a9c170cbaaeea028c88a7b89Daniel Jasper return false; 6832d28ee6061930b2a9c170cbaaeea028c88a7b89Daniel Jasper } 6932d28ee6061930b2a9c170cbaaeea028c88a7b89Daniel Jasper return false; 7032d28ee6061930b2a9c170cbaaeea028c88a7b89Daniel Jasper } 7132d28ee6061930b2a9c170cbaaeea028c88a7b89Daniel Jasper 7232d28ee6061930b2a9c170cbaaeea028c88a7b89Daniel Jasper bool parseParens(bool LookForDecls = false) { 7332d28ee6061930b2a9c170cbaaeea028c88a7b89Daniel Jasper if (CurrentToken == NULL) 7432d28ee6061930b2a9c170cbaaeea028c88a7b89Daniel Jasper return false; 75923ebef120a37122ce50722a85cbe42c0c2dab53Daniel Jasper ScopedContextCreator ContextCreator(*this, tok::l_paren, 1); 764e778091b0ff42895644ca8bef30f1a91ba6b32bDaniel Jasper 774e778091b0ff42895644ca8bef30f1a91ba6b32bDaniel Jasper // FIXME: This is a bit of a hack. Do better. 784e778091b0ff42895644ca8bef30f1a91ba6b32bDaniel Jasper Contexts.back().ColonIsForRangeExpr = 794e778091b0ff42895644ca8bef30f1a91ba6b32bDaniel Jasper Contexts.size() == 2 && Contexts[0].ColonIsForRangeExpr; 804e778091b0ff42895644ca8bef30f1a91ba6b32bDaniel Jasper 8132d28ee6061930b2a9c170cbaaeea028c88a7b89Daniel Jasper bool StartsObjCMethodExpr = false; 82b398701e4cbb9a55d90a60e3f4f4bc577446d098Manuel Klimek FormatToken *Left = CurrentToken->Previous; 8332d28ee6061930b2a9c170cbaaeea028c88a7b89Daniel Jasper if (CurrentToken->is(tok::caret)) { 8432d28ee6061930b2a9c170cbaaeea028c88a7b89Daniel Jasper // ^( starts a block. 8532d28ee6061930b2a9c170cbaaeea028c88a7b89Daniel Jasper Left->Type = TT_ObjCBlockLParen; 86b398701e4cbb9a55d90a60e3f4f4bc577446d098Manuel Klimek } else if (FormatToken *MaybeSel = Left->Previous) { 8732d28ee6061930b2a9c170cbaaeea028c88a7b89Daniel Jasper // @selector( starts a selector. 88b398701e4cbb9a55d90a60e3f4f4bc577446d098Manuel Klimek if (MaybeSel->isObjCAtKeyword(tok::objc_selector) && MaybeSel->Previous && 89b398701e4cbb9a55d90a60e3f4f4bc577446d098Manuel Klimek MaybeSel->Previous->is(tok::at)) { 9032d28ee6061930b2a9c170cbaaeea028c88a7b89Daniel Jasper StartsObjCMethodExpr = true; 9132d28ee6061930b2a9c170cbaaeea028c88a7b89Daniel Jasper } 9232d28ee6061930b2a9c170cbaaeea028c88a7b89Daniel Jasper } 9332d28ee6061930b2a9c170cbaaeea028c88a7b89Daniel Jasper 944e778091b0ff42895644ca8bef30f1a91ba6b32bDaniel Jasper if (StartsObjCMethodExpr) { 954e778091b0ff42895644ca8bef30f1a91ba6b32bDaniel Jasper Contexts.back().ColonIsObjCMethodExpr = true; 964e778091b0ff42895644ca8bef30f1a91ba6b32bDaniel Jasper Left->Type = TT_ObjCMethodExpr; 974e778091b0ff42895644ca8bef30f1a91ba6b32bDaniel Jasper } 9832d28ee6061930b2a9c170cbaaeea028c88a7b89Daniel Jasper 99431f5918281dd1f178bf838b7a47ab1d11c52961Daniel Jasper bool MightBeFunctionType = CurrentToken->is(tok::star); 10032d28ee6061930b2a9c170cbaaeea028c88a7b89Daniel Jasper while (CurrentToken != NULL) { 10132d28ee6061930b2a9c170cbaaeea028c88a7b89Daniel Jasper // LookForDecls is set when "if (" has been seen. Check for 10232d28ee6061930b2a9c170cbaaeea028c88a7b89Daniel Jasper // 'identifier' '*' 'identifier' followed by not '=' -- this 10332d28ee6061930b2a9c170cbaaeea028c88a7b89Daniel Jasper // '*' has to be a binary operator but determineStarAmpUsage() will 10432d28ee6061930b2a9c170cbaaeea028c88a7b89Daniel Jasper // categorize it as an unary operator, so set the right type here. 105b398701e4cbb9a55d90a60e3f4f4bc577446d098Manuel Klimek if (LookForDecls && CurrentToken->Next) { 1062785b9aabcb2c3fd6f7dd8b63d3cd3d4b9bca284Alexander Kornienko FormatToken *Prev = CurrentToken->getPreviousNoneComment(); 1072785b9aabcb2c3fd6f7dd8b63d3cd3d4b9bca284Alexander Kornienko if (Prev) { 1082785b9aabcb2c3fd6f7dd8b63d3cd3d4b9bca284Alexander Kornienko FormatToken *PrevPrev = Prev->getPreviousNoneComment(); 1092785b9aabcb2c3fd6f7dd8b63d3cd3d4b9bca284Alexander Kornienko FormatToken *Next = CurrentToken->Next; 1102785b9aabcb2c3fd6f7dd8b63d3cd3d4b9bca284Alexander Kornienko if (PrevPrev && PrevPrev->is(tok::identifier) && 1112785b9aabcb2c3fd6f7dd8b63d3cd3d4b9bca284Alexander Kornienko Prev->isOneOf(tok::star, tok::amp, tok::ampamp) && 1122785b9aabcb2c3fd6f7dd8b63d3cd3d4b9bca284Alexander Kornienko CurrentToken->is(tok::identifier) && Next->isNot(tok::equal)) { 1132785b9aabcb2c3fd6f7dd8b63d3cd3d4b9bca284Alexander Kornienko Prev->Type = TT_BinaryOperator; 1142785b9aabcb2c3fd6f7dd8b63d3cd3d4b9bca284Alexander Kornienko LookForDecls = false; 1152785b9aabcb2c3fd6f7dd8b63d3cd3d4b9bca284Alexander Kornienko } 11632d28ee6061930b2a9c170cbaaeea028c88a7b89Daniel Jasper } 11732d28ee6061930b2a9c170cbaaeea028c88a7b89Daniel Jasper } 11832d28ee6061930b2a9c170cbaaeea028c88a7b89Daniel Jasper 11932d28ee6061930b2a9c170cbaaeea028c88a7b89Daniel Jasper if (CurrentToken->is(tok::r_paren)) { 120b398701e4cbb9a55d90a60e3f4f4bc577446d098Manuel Klimek if (MightBeFunctionType && CurrentToken->Next && 121b398701e4cbb9a55d90a60e3f4f4bc577446d098Manuel Klimek CurrentToken->Next->isOneOf(tok::l_paren, tok::l_square)) 122431f5918281dd1f178bf838b7a47ab1d11c52961Daniel Jasper Left->Type = TT_FunctionTypeLParen; 12332d28ee6061930b2a9c170cbaaeea028c88a7b89Daniel Jasper Left->MatchingParen = CurrentToken; 12432d28ee6061930b2a9c170cbaaeea028c88a7b89Daniel Jasper CurrentToken->MatchingParen = Left; 12532d28ee6061930b2a9c170cbaaeea028c88a7b89Daniel Jasper 12663d7cedca8616921c1908c88c2f23fcd67bbab99Daniel Jasper if (StartsObjCMethodExpr) { 1274e778091b0ff42895644ca8bef30f1a91ba6b32bDaniel Jasper CurrentToken->Type = TT_ObjCMethodExpr; 1284e778091b0ff42895644ca8bef30f1a91ba6b32bDaniel Jasper if (Contexts.back().FirstObjCSelectorName != NULL) { 1294e778091b0ff42895644ca8bef30f1a91ba6b32bDaniel Jasper Contexts.back().FirstObjCSelectorName->LongestObjCSelectorName = 1304e778091b0ff42895644ca8bef30f1a91ba6b32bDaniel Jasper Contexts.back().LongestObjCSelectorName; 13163d7cedca8616921c1908c88c2f23fcd67bbab99Daniel Jasper } 13263d7cedca8616921c1908c88c2f23fcd67bbab99Daniel Jasper } 13332d28ee6061930b2a9c170cbaaeea028c88a7b89Daniel Jasper 13432d28ee6061930b2a9c170cbaaeea028c88a7b89Daniel Jasper next(); 13532d28ee6061930b2a9c170cbaaeea028c88a7b89Daniel Jasper return true; 13632d28ee6061930b2a9c170cbaaeea028c88a7b89Daniel Jasper } 137e74de28ec3692986f3467a7f160a0e293277fa7eAlexander Kornienko if (CurrentToken->isOneOf(tok::r_square, tok::r_brace)) 13832d28ee6061930b2a9c170cbaaeea028c88a7b89Daniel Jasper return false; 139b398701e4cbb9a55d90a60e3f4f4bc577446d098Manuel Klimek if (CurrentToken->Previous->Type == TT_PointerOrReference && 140b398701e4cbb9a55d90a60e3f4f4bc577446d098Manuel Klimek CurrentToken->Previous->Previous->isOneOf(tok::l_paren, 141b398701e4cbb9a55d90a60e3f4f4bc577446d098Manuel Klimek tok::coloncolon)) 142431f5918281dd1f178bf838b7a47ab1d11c52961Daniel Jasper MightBeFunctionType = true; 1439fc56f2636137fcde8acb38865555ed6c7b84dfdDaniel Jasper updateParameterCount(Left, CurrentToken); 14432d28ee6061930b2a9c170cbaaeea028c88a7b89Daniel Jasper if (!consumeToken()) 14532d28ee6061930b2a9c170cbaaeea028c88a7b89Daniel Jasper return false; 14632d28ee6061930b2a9c170cbaaeea028c88a7b89Daniel Jasper } 14732d28ee6061930b2a9c170cbaaeea028c88a7b89Daniel Jasper return false; 14832d28ee6061930b2a9c170cbaaeea028c88a7b89Daniel Jasper } 14932d28ee6061930b2a9c170cbaaeea028c88a7b89Daniel Jasper 15032d28ee6061930b2a9c170cbaaeea028c88a7b89Daniel Jasper bool parseSquare() { 15132d28ee6061930b2a9c170cbaaeea028c88a7b89Daniel Jasper if (!CurrentToken) 15232d28ee6061930b2a9c170cbaaeea028c88a7b89Daniel Jasper return false; 15332d28ee6061930b2a9c170cbaaeea028c88a7b89Daniel Jasper 154d71b15badeecdc049440103ef044f9cdf5e1359cAlexander Kornienko // A '[' could be an index subscript (after an identifier or after 155051860ee770bf83c3e66ab893be3642bb8bc2680Nico Weber // ')' or ']'), it could be the start of an Objective-C method 156051860ee770bf83c3e66ab893be3642bb8bc2680Nico Weber // expression, or it could the the start of an Objective-C array literal. 157b398701e4cbb9a55d90a60e3f4f4bc577446d098Manuel Klimek FormatToken *Left = CurrentToken->Previous; 158b398701e4cbb9a55d90a60e3f4f4bc577446d098Manuel Klimek FormatToken *Parent = Left->getPreviousNoneComment(); 15932d28ee6061930b2a9c170cbaaeea028c88a7b89Daniel Jasper bool StartsObjCMethodExpr = 1606f21a988990ff5872822dcb049bd8fc65ce3d236Daniel Jasper Contexts.back().CanBeExpression && 161e74de28ec3692986f3467a7f160a0e293277fa7eAlexander Kornienko (!Parent || Parent->isOneOf(tok::colon, tok::l_square, tok::l_paren, 162e74de28ec3692986f3467a7f160a0e293277fa7eAlexander Kornienko tok::kw_return, tok::kw_throw) || 163ac3223e45e7d17c65b143439313800eef4fdf71dDaniel Jasper Parent->isUnaryOperator() || Parent->Type == TT_ObjCForIn || 164e74de28ec3692986f3467a7f160a0e293277fa7eAlexander Kornienko Parent->Type == TT_CastRParen || 165b398701e4cbb9a55d90a60e3f4f4bc577446d098Manuel Klimek getBinOpPrecedence(Parent->Tok.getKind(), true, true) > prec::Unknown); 166923ebef120a37122ce50722a85cbe42c0c2dab53Daniel Jasper ScopedContextCreator ContextCreator(*this, tok::l_square, 10); 1676f21a988990ff5872822dcb049bd8fc65ce3d236Daniel Jasper Contexts.back().IsExpression = true; 168051860ee770bf83c3e66ab893be3642bb8bc2680Nico Weber bool StartsObjCArrayLiteral = Parent && Parent->is(tok::at); 16932d28ee6061930b2a9c170cbaaeea028c88a7b89Daniel Jasper 1704e778091b0ff42895644ca8bef30f1a91ba6b32bDaniel Jasper if (StartsObjCMethodExpr) { 1714e778091b0ff42895644ca8bef30f1a91ba6b32bDaniel Jasper Contexts.back().ColonIsObjCMethodExpr = true; 1724e778091b0ff42895644ca8bef30f1a91ba6b32bDaniel Jasper Left->Type = TT_ObjCMethodExpr; 173051860ee770bf83c3e66ab893be3642bb8bc2680Nico Weber } else if (StartsObjCArrayLiteral) { 174051860ee770bf83c3e66ab893be3642bb8bc2680Nico Weber Left->Type = TT_ObjCArrayLiteral; 1754e778091b0ff42895644ca8bef30f1a91ba6b32bDaniel Jasper } 17632d28ee6061930b2a9c170cbaaeea028c88a7b89Daniel Jasper 17732d28ee6061930b2a9c170cbaaeea028c88a7b89Daniel Jasper while (CurrentToken != NULL) { 17832d28ee6061930b2a9c170cbaaeea028c88a7b89Daniel Jasper if (CurrentToken->is(tok::r_square)) { 179b398701e4cbb9a55d90a60e3f4f4bc577446d098Manuel Klimek if (CurrentToken->Next && CurrentToken->Next->is(tok::l_paren)) { 180e8a97985f72b4e11435ff2107c0f11e925fb6d96Nico Weber // An ObjC method call is rarely followed by an open parenthesis. 18132d28ee6061930b2a9c170cbaaeea028c88a7b89Daniel Jasper // FIXME: Do we incorrectly label ":" with this? 18232d28ee6061930b2a9c170cbaaeea028c88a7b89Daniel Jasper StartsObjCMethodExpr = false; 18332d28ee6061930b2a9c170cbaaeea028c88a7b89Daniel Jasper Left->Type = TT_Unknown; 18432d28ee6061930b2a9c170cbaaeea028c88a7b89Daniel Jasper } 1850178673f541685cf5067814dfeee2644078e39a9Daniel Jasper if (StartsObjCMethodExpr) { 1864e778091b0ff42895644ca8bef30f1a91ba6b32bDaniel Jasper CurrentToken->Type = TT_ObjCMethodExpr; 187e8a97985f72b4e11435ff2107c0f11e925fb6d96Nico Weber // determineStarAmpUsage() thinks that '*' '[' is allocating an 188e8a97985f72b4e11435ff2107c0f11e925fb6d96Nico Weber // array of pointers, but if '[' starts a selector then '*' is a 189e8a97985f72b4e11435ff2107c0f11e925fb6d96Nico Weber // binary operator. 1903fd9ccdd9f8d259bcf518e7056cfd419d992e984Alexander Kornienko if (Parent != NULL && Parent->Type == TT_PointerOrReference) 1914ed7f3e003c906d9fdb92a9484feeb8ac6e28e2fNico Weber Parent->Type = TT_BinaryOperator; 192051860ee770bf83c3e66ab893be3642bb8bc2680Nico Weber } else if (StartsObjCArrayLiteral) { 193051860ee770bf83c3e66ab893be3642bb8bc2680Nico Weber CurrentToken->Type = TT_ObjCArrayLiteral; 1940178673f541685cf5067814dfeee2644078e39a9Daniel Jasper } 19532d28ee6061930b2a9c170cbaaeea028c88a7b89Daniel Jasper Left->MatchingParen = CurrentToken; 19632d28ee6061930b2a9c170cbaaeea028c88a7b89Daniel Jasper CurrentToken->MatchingParen = Left; 1974e778091b0ff42895644ca8bef30f1a91ba6b32bDaniel Jasper if (Contexts.back().FirstObjCSelectorName != NULL) 1984e778091b0ff42895644ca8bef30f1a91ba6b32bDaniel Jasper Contexts.back().FirstObjCSelectorName->LongestObjCSelectorName = 1994e778091b0ff42895644ca8bef30f1a91ba6b32bDaniel Jasper Contexts.back().LongestObjCSelectorName; 20032d28ee6061930b2a9c170cbaaeea028c88a7b89Daniel Jasper next(); 20132d28ee6061930b2a9c170cbaaeea028c88a7b89Daniel Jasper return true; 20232d28ee6061930b2a9c170cbaaeea028c88a7b89Daniel Jasper } 203e74de28ec3692986f3467a7f160a0e293277fa7eAlexander Kornienko if (CurrentToken->isOneOf(tok::r_paren, tok::r_brace)) 20432d28ee6061930b2a9c170cbaaeea028c88a7b89Daniel Jasper return false; 2059fc56f2636137fcde8acb38865555ed6c7b84dfdDaniel Jasper updateParameterCount(Left, CurrentToken); 20632d28ee6061930b2a9c170cbaaeea028c88a7b89Daniel Jasper if (!consumeToken()) 20732d28ee6061930b2a9c170cbaaeea028c88a7b89Daniel Jasper return false; 20832d28ee6061930b2a9c170cbaaeea028c88a7b89Daniel Jasper } 20932d28ee6061930b2a9c170cbaaeea028c88a7b89Daniel Jasper return false; 21032d28ee6061930b2a9c170cbaaeea028c88a7b89Daniel Jasper } 21132d28ee6061930b2a9c170cbaaeea028c88a7b89Daniel Jasper 21232d28ee6061930b2a9c170cbaaeea028c88a7b89Daniel Jasper bool parseBrace() { 21353e72cdcc0bc217cab33fa56858a0fe5b94ca453Daniel Jasper if (CurrentToken != NULL) { 21453e72cdcc0bc217cab33fa56858a0fe5b94ca453Daniel Jasper ScopedContextCreator ContextCreator(*this, tok::l_brace, 1); 215b398701e4cbb9a55d90a60e3f4f4bc577446d098Manuel Klimek FormatToken *Left = CurrentToken->Previous; 216f2ff8126e9e9df368f31b1f968d8fc80f99809b3Nico Weber 217b398701e4cbb9a55d90a60e3f4f4bc577446d098Manuel Klimek FormatToken *Parent = Left->getPreviousNoneComment(); 218f2ff8126e9e9df368f31b1f968d8fc80f99809b3Nico Weber bool StartsObjCDictLiteral = Parent && Parent->is(tok::at); 219f2ff8126e9e9df368f31b1f968d8fc80f99809b3Nico Weber if (StartsObjCDictLiteral) { 220f2ff8126e9e9df368f31b1f968d8fc80f99809b3Nico Weber Contexts.back().ColonIsObjCDictLiteral = true; 221f2ff8126e9e9df368f31b1f968d8fc80f99809b3Nico Weber Left->Type = TT_ObjCDictLiteral; 222f2ff8126e9e9df368f31b1f968d8fc80f99809b3Nico Weber } 223f2ff8126e9e9df368f31b1f968d8fc80f99809b3Nico Weber 22453e72cdcc0bc217cab33fa56858a0fe5b94ca453Daniel Jasper while (CurrentToken != NULL) { 22553e72cdcc0bc217cab33fa56858a0fe5b94ca453Daniel Jasper if (CurrentToken->is(tok::r_brace)) { 226f2ff8126e9e9df368f31b1f968d8fc80f99809b3Nico Weber if (StartsObjCDictLiteral) 227f2ff8126e9e9df368f31b1f968d8fc80f99809b3Nico Weber CurrentToken->Type = TT_ObjCDictLiteral; 22853e72cdcc0bc217cab33fa56858a0fe5b94ca453Daniel Jasper Left->MatchingParen = CurrentToken; 22953e72cdcc0bc217cab33fa56858a0fe5b94ca453Daniel Jasper CurrentToken->MatchingParen = Left; 23053e72cdcc0bc217cab33fa56858a0fe5b94ca453Daniel Jasper next(); 23153e72cdcc0bc217cab33fa56858a0fe5b94ca453Daniel Jasper return true; 23253e72cdcc0bc217cab33fa56858a0fe5b94ca453Daniel Jasper } 23353e72cdcc0bc217cab33fa56858a0fe5b94ca453Daniel Jasper if (CurrentToken->isOneOf(tok::r_paren, tok::r_square)) 23453e72cdcc0bc217cab33fa56858a0fe5b94ca453Daniel Jasper return false; 23553e72cdcc0bc217cab33fa56858a0fe5b94ca453Daniel Jasper updateParameterCount(Left, CurrentToken); 23653e72cdcc0bc217cab33fa56858a0fe5b94ca453Daniel Jasper if (!consumeToken()) 23753e72cdcc0bc217cab33fa56858a0fe5b94ca453Daniel Jasper return false; 23832d28ee6061930b2a9c170cbaaeea028c88a7b89Daniel Jasper } 23932d28ee6061930b2a9c170cbaaeea028c88a7b89Daniel Jasper } 24053e72cdcc0bc217cab33fa56858a0fe5b94ca453Daniel Jasper // No closing "}" found, this probably starts a definition. 24153e72cdcc0bc217cab33fa56858a0fe5b94ca453Daniel Jasper Line.StartsDefinition = true; 24232d28ee6061930b2a9c170cbaaeea028c88a7b89Daniel Jasper return true; 24332d28ee6061930b2a9c170cbaaeea028c88a7b89Daniel Jasper } 244c4615b7aaf97e303a4fc675956f7f5572d492885Daniel Jasper 245b398701e4cbb9a55d90a60e3f4f4bc577446d098Manuel Klimek void updateParameterCount(FormatToken *Left, FormatToken *Current) { 2469fc56f2636137fcde8acb38865555ed6c7b84dfdDaniel Jasper if (Current->is(tok::comma)) 2479fc56f2636137fcde8acb38865555ed6c7b84dfdDaniel Jasper ++Left->ParameterCount; 2489fc56f2636137fcde8acb38865555ed6c7b84dfdDaniel Jasper else if (Left->ParameterCount == 0 && Current->isNot(tok::comment)) 2499fc56f2636137fcde8acb38865555ed6c7b84dfdDaniel Jasper Left->ParameterCount = 1; 2509fc56f2636137fcde8acb38865555ed6c7b84dfdDaniel Jasper } 25132d28ee6061930b2a9c170cbaaeea028c88a7b89Daniel Jasper 25232d28ee6061930b2a9c170cbaaeea028c88a7b89Daniel Jasper bool parseConditional() { 25332d28ee6061930b2a9c170cbaaeea028c88a7b89Daniel Jasper while (CurrentToken != NULL) { 25432d28ee6061930b2a9c170cbaaeea028c88a7b89Daniel Jasper if (CurrentToken->is(tok::colon)) { 25532d28ee6061930b2a9c170cbaaeea028c88a7b89Daniel Jasper CurrentToken->Type = TT_ConditionalExpr; 25632d28ee6061930b2a9c170cbaaeea028c88a7b89Daniel Jasper next(); 25732d28ee6061930b2a9c170cbaaeea028c88a7b89Daniel Jasper return true; 25832d28ee6061930b2a9c170cbaaeea028c88a7b89Daniel Jasper } 25932d28ee6061930b2a9c170cbaaeea028c88a7b89Daniel Jasper if (!consumeToken()) 26032d28ee6061930b2a9c170cbaaeea028c88a7b89Daniel Jasper return false; 26132d28ee6061930b2a9c170cbaaeea028c88a7b89Daniel Jasper } 26232d28ee6061930b2a9c170cbaaeea028c88a7b89Daniel Jasper return false; 26332d28ee6061930b2a9c170cbaaeea028c88a7b89Daniel Jasper } 26432d28ee6061930b2a9c170cbaaeea028c88a7b89Daniel Jasper 26532d28ee6061930b2a9c170cbaaeea028c88a7b89Daniel Jasper bool parseTemplateDeclaration() { 26632d28ee6061930b2a9c170cbaaeea028c88a7b89Daniel Jasper if (CurrentToken != NULL && CurrentToken->is(tok::less)) { 26732d28ee6061930b2a9c170cbaaeea028c88a7b89Daniel Jasper CurrentToken->Type = TT_TemplateOpener; 26832d28ee6061930b2a9c170cbaaeea028c88a7b89Daniel Jasper next(); 26932d28ee6061930b2a9c170cbaaeea028c88a7b89Daniel Jasper if (!parseAngle()) 27032d28ee6061930b2a9c170cbaaeea028c88a7b89Daniel Jasper return false; 27134511fb79201ba9ed39c97f3a7ea0f157a79436dDaniel Jasper if (CurrentToken != NULL) 272b398701e4cbb9a55d90a60e3f4f4bc577446d098Manuel Klimek CurrentToken->Previous->ClosesTemplateDeclaration = true; 27332d28ee6061930b2a9c170cbaaeea028c88a7b89Daniel Jasper return true; 27432d28ee6061930b2a9c170cbaaeea028c88a7b89Daniel Jasper } 27532d28ee6061930b2a9c170cbaaeea028c88a7b89Daniel Jasper return false; 27632d28ee6061930b2a9c170cbaaeea028c88a7b89Daniel Jasper } 27732d28ee6061930b2a9c170cbaaeea028c88a7b89Daniel Jasper 27832d28ee6061930b2a9c170cbaaeea028c88a7b89Daniel Jasper bool consumeToken() { 279b398701e4cbb9a55d90a60e3f4f4bc577446d098Manuel Klimek FormatToken *Tok = CurrentToken; 28032d28ee6061930b2a9c170cbaaeea028c88a7b89Daniel Jasper next(); 281b398701e4cbb9a55d90a60e3f4f4bc577446d098Manuel Klimek switch (Tok->Tok.getKind()) { 28232d28ee6061930b2a9c170cbaaeea028c88a7b89Daniel Jasper case tok::plus: 28332d28ee6061930b2a9c170cbaaeea028c88a7b89Daniel Jasper case tok::minus: 284b398701e4cbb9a55d90a60e3f4f4bc577446d098Manuel Klimek if (Tok->Previous == NULL && Line.MustBeDeclaration) 28532d28ee6061930b2a9c170cbaaeea028c88a7b89Daniel Jasper Tok->Type = TT_ObjCMethodSpecifier; 28632d28ee6061930b2a9c170cbaaeea028c88a7b89Daniel Jasper break; 28732d28ee6061930b2a9c170cbaaeea028c88a7b89Daniel Jasper case tok::colon: 288b398701e4cbb9a55d90a60e3f4f4bc577446d098Manuel Klimek if (Tok->Previous == NULL) 289cf6d76af806f7e1ba97be7b72b31bc78b919e0f0Daniel Jasper return false; 29032d28ee6061930b2a9c170cbaaeea028c88a7b89Daniel Jasper // Colons from ?: are handled in parseConditional(). 291b398701e4cbb9a55d90a60e3f4f4bc577446d098Manuel Klimek if (Tok->Previous->is(tok::r_paren) && Contexts.size() == 1) { 29232d28ee6061930b2a9c170cbaaeea028c88a7b89Daniel Jasper Tok->Type = TT_CtorInitializerColon; 293f2ff8126e9e9df368f31b1f968d8fc80f99809b3Nico Weber } else if (Contexts.back().ColonIsObjCDictLiteral) { 294f2ff8126e9e9df368f31b1f968d8fc80f99809b3Nico Weber Tok->Type = TT_ObjCDictLiteral; 2954e778091b0ff42895644ca8bef30f1a91ba6b32bDaniel Jasper } else if (Contexts.back().ColonIsObjCMethodExpr || 296b398701e4cbb9a55d90a60e3f4f4bc577446d098Manuel Klimek Line.First->Type == TT_ObjCMethodSpecifier) { 29732d28ee6061930b2a9c170cbaaeea028c88a7b89Daniel Jasper Tok->Type = TT_ObjCMethodExpr; 298b398701e4cbb9a55d90a60e3f4f4bc577446d098Manuel Klimek Tok->Previous->Type = TT_ObjCSelectorName; 29900895106f9ed602af67984ec4d225a0cdc8c12afAlexander Kornienko if (Tok->Previous->CodePointCount > 30000895106f9ed602af67984ec4d225a0cdc8c12afAlexander Kornienko Contexts.back().LongestObjCSelectorName) { 30100895106f9ed602af67984ec4d225a0cdc8c12afAlexander Kornienko Contexts.back().LongestObjCSelectorName = 30200895106f9ed602af67984ec4d225a0cdc8c12afAlexander Kornienko Tok->Previous->CodePointCount; 30300895106f9ed602af67984ec4d225a0cdc8c12afAlexander Kornienko } 3044e778091b0ff42895644ca8bef30f1a91ba6b32bDaniel Jasper if (Contexts.back().FirstObjCSelectorName == NULL) 305b398701e4cbb9a55d90a60e3f4f4bc577446d098Manuel Klimek Contexts.back().FirstObjCSelectorName = Tok->Previous; 3064e778091b0ff42895644ca8bef30f1a91ba6b32bDaniel Jasper } else if (Contexts.back().ColonIsForRangeExpr) { 30732d28ee6061930b2a9c170cbaaeea028c88a7b89Daniel Jasper Tok->Type = TT_RangeBasedForLoopColon; 3086cabab48dacc1317821f8f078ed2d4c603b67affDaniel Jasper } else if (Contexts.size() == 1) { 3096cabab48dacc1317821f8f078ed2d4c603b67affDaniel Jasper Tok->Type = TT_InheritanceColon; 310923ebef120a37122ce50722a85cbe42c0c2dab53Daniel Jasper } else if (Contexts.back().ContextKind == tok::l_paren) { 311923ebef120a37122ce50722a85cbe42c0c2dab53Daniel Jasper Tok->Type = TT_InlineASMColon; 31263d7cedca8616921c1908c88c2f23fcd67bbab99Daniel Jasper } 31332d28ee6061930b2a9c170cbaaeea028c88a7b89Daniel Jasper break; 31432d28ee6061930b2a9c170cbaaeea028c88a7b89Daniel Jasper case tok::kw_if: 31532d28ee6061930b2a9c170cbaaeea028c88a7b89Daniel Jasper case tok::kw_while: 31632d28ee6061930b2a9c170cbaaeea028c88a7b89Daniel Jasper if (CurrentToken != NULL && CurrentToken->is(tok::l_paren)) { 31732d28ee6061930b2a9c170cbaaeea028c88a7b89Daniel Jasper next(); 31832d28ee6061930b2a9c170cbaaeea028c88a7b89Daniel Jasper if (!parseParens(/*LookForDecls=*/ true)) 31932d28ee6061930b2a9c170cbaaeea028c88a7b89Daniel Jasper return false; 32032d28ee6061930b2a9c170cbaaeea028c88a7b89Daniel Jasper } 32132d28ee6061930b2a9c170cbaaeea028c88a7b89Daniel Jasper break; 32232d28ee6061930b2a9c170cbaaeea028c88a7b89Daniel Jasper case tok::kw_for: 3234e778091b0ff42895644ca8bef30f1a91ba6b32bDaniel Jasper Contexts.back().ColonIsForRangeExpr = true; 32432d28ee6061930b2a9c170cbaaeea028c88a7b89Daniel Jasper next(); 32532d28ee6061930b2a9c170cbaaeea028c88a7b89Daniel Jasper if (!parseParens()) 32632d28ee6061930b2a9c170cbaaeea028c88a7b89Daniel Jasper return false; 32732d28ee6061930b2a9c170cbaaeea028c88a7b89Daniel Jasper break; 32832d28ee6061930b2a9c170cbaaeea028c88a7b89Daniel Jasper case tok::l_paren: 32932d28ee6061930b2a9c170cbaaeea028c88a7b89Daniel Jasper if (!parseParens()) 33032d28ee6061930b2a9c170cbaaeea028c88a7b89Daniel Jasper return false; 3311407bee187d7b964d5293ac8bf4f7a490c78cec6Daniel Jasper if (Line.MustBeDeclaration && NameFound && !Contexts.back().IsExpression) 3323c08a818a6ac9115fe8880af9bbf5a0a87bdffaaDaniel Jasper Line.MightBeFunctionDecl = true; 33332d28ee6061930b2a9c170cbaaeea028c88a7b89Daniel Jasper break; 33432d28ee6061930b2a9c170cbaaeea028c88a7b89Daniel Jasper case tok::l_square: 33532d28ee6061930b2a9c170cbaaeea028c88a7b89Daniel Jasper if (!parseSquare()) 33632d28ee6061930b2a9c170cbaaeea028c88a7b89Daniel Jasper return false; 33732d28ee6061930b2a9c170cbaaeea028c88a7b89Daniel Jasper break; 33832d28ee6061930b2a9c170cbaaeea028c88a7b89Daniel Jasper case tok::l_brace: 33932d28ee6061930b2a9c170cbaaeea028c88a7b89Daniel Jasper if (!parseBrace()) 34032d28ee6061930b2a9c170cbaaeea028c88a7b89Daniel Jasper return false; 34132d28ee6061930b2a9c170cbaaeea028c88a7b89Daniel Jasper break; 34232d28ee6061930b2a9c170cbaaeea028c88a7b89Daniel Jasper case tok::less: 34332d28ee6061930b2a9c170cbaaeea028c88a7b89Daniel Jasper if (parseAngle()) 34432d28ee6061930b2a9c170cbaaeea028c88a7b89Daniel Jasper Tok->Type = TT_TemplateOpener; 34532d28ee6061930b2a9c170cbaaeea028c88a7b89Daniel Jasper else { 34632d28ee6061930b2a9c170cbaaeea028c88a7b89Daniel Jasper Tok->Type = TT_BinaryOperator; 34732d28ee6061930b2a9c170cbaaeea028c88a7b89Daniel Jasper CurrentToken = Tok; 34832d28ee6061930b2a9c170cbaaeea028c88a7b89Daniel Jasper next(); 34932d28ee6061930b2a9c170cbaaeea028c88a7b89Daniel Jasper } 35032d28ee6061930b2a9c170cbaaeea028c88a7b89Daniel Jasper break; 35132d28ee6061930b2a9c170cbaaeea028c88a7b89Daniel Jasper case tok::r_paren: 35232d28ee6061930b2a9c170cbaaeea028c88a7b89Daniel Jasper case tok::r_square: 35332d28ee6061930b2a9c170cbaaeea028c88a7b89Daniel Jasper return false; 35432d28ee6061930b2a9c170cbaaeea028c88a7b89Daniel Jasper case tok::r_brace: 35532d28ee6061930b2a9c170cbaaeea028c88a7b89Daniel Jasper // Lines can start with '}'. 356b398701e4cbb9a55d90a60e3f4f4bc577446d098Manuel Klimek if (Tok->Previous != NULL) 35732d28ee6061930b2a9c170cbaaeea028c88a7b89Daniel Jasper return false; 35832d28ee6061930b2a9c170cbaaeea028c88a7b89Daniel Jasper break; 35932d28ee6061930b2a9c170cbaaeea028c88a7b89Daniel Jasper case tok::greater: 36032d28ee6061930b2a9c170cbaaeea028c88a7b89Daniel Jasper Tok->Type = TT_BinaryOperator; 36132d28ee6061930b2a9c170cbaaeea028c88a7b89Daniel Jasper break; 36232d28ee6061930b2a9c170cbaaeea028c88a7b89Daniel Jasper case tok::kw_operator: 3632b4c924f85e156d66c01b3f16c850892c47dcc7aDaniel Jasper while (CurrentToken && CurrentToken->isNot(tok::l_paren)) { 364e74de28ec3692986f3467a7f160a0e293277fa7eAlexander Kornienko if (CurrentToken->isOneOf(tok::star, tok::amp)) 3652b4c924f85e156d66c01b3f16c850892c47dcc7aDaniel Jasper CurrentToken->Type = TT_PointerOrReference; 3662b4c924f85e156d66c01b3f16c850892c47dcc7aDaniel Jasper consumeToken(); 36732d28ee6061930b2a9c170cbaaeea028c88a7b89Daniel Jasper } 3686ea933c7e8f6988d5647af4a0eafd393a4c3685aDaniel Jasper if (CurrentToken) { 3692b4c924f85e156d66c01b3f16c850892c47dcc7aDaniel Jasper CurrentToken->Type = TT_OverloadedOperatorLParen; 370b398701e4cbb9a55d90a60e3f4f4bc577446d098Manuel Klimek if (CurrentToken->Previous->Type == TT_BinaryOperator) 371b398701e4cbb9a55d90a60e3f4f4bc577446d098Manuel Klimek CurrentToken->Previous->Type = TT_OverloadedOperator; 3726ea933c7e8f6988d5647af4a0eafd393a4c3685aDaniel Jasper } 37332d28ee6061930b2a9c170cbaaeea028c88a7b89Daniel Jasper break; 37432d28ee6061930b2a9c170cbaaeea028c88a7b89Daniel Jasper case tok::question: 37532d28ee6061930b2a9c170cbaaeea028c88a7b89Daniel Jasper parseConditional(); 37632d28ee6061930b2a9c170cbaaeea028c88a7b89Daniel Jasper break; 37732d28ee6061930b2a9c170cbaaeea028c88a7b89Daniel Jasper case tok::kw_template: 37832d28ee6061930b2a9c170cbaaeea028c88a7b89Daniel Jasper parseTemplateDeclaration(); 37932d28ee6061930b2a9c170cbaaeea028c88a7b89Daniel Jasper break; 380c2e6d2a4a7fe9dfa7d52a38c6048b7b18e6b591aNico Weber case tok::identifier: 381b398701e4cbb9a55d90a60e3f4f4bc577446d098Manuel Klimek if (Line.First->is(tok::kw_for) && 382b398701e4cbb9a55d90a60e3f4f4bc577446d098Manuel Klimek Tok->Tok.getIdentifierInfo() == &Ident_in) 383c2e6d2a4a7fe9dfa7d52a38c6048b7b18e6b591aNico Weber Tok->Type = TT_ObjCForIn; 384c2e6d2a4a7fe9dfa7d52a38c6048b7b18e6b591aNico Weber break; 3858ed9f2b25f082a1643ab5310f9eec33cf31a7577Daniel Jasper case tok::comma: 3868ed9f2b25f082a1643ab5310f9eec33cf31a7577Daniel Jasper if (Contexts.back().FirstStartOfName) 3878ed9f2b25f082a1643ab5310f9eec33cf31a7577Daniel Jasper Contexts.back().FirstStartOfName->PartOfMultiVariableDeclStmt = true; 3888ed9f2b25f082a1643ab5310f9eec33cf31a7577Daniel Jasper break; 38932d28ee6061930b2a9c170cbaaeea028c88a7b89Daniel Jasper default: 39032d28ee6061930b2a9c170cbaaeea028c88a7b89Daniel Jasper break; 39132d28ee6061930b2a9c170cbaaeea028c88a7b89Daniel Jasper } 39232d28ee6061930b2a9c170cbaaeea028c88a7b89Daniel Jasper return true; 39332d28ee6061930b2a9c170cbaaeea028c88a7b89Daniel Jasper } 39432d28ee6061930b2a9c170cbaaeea028c88a7b89Daniel Jasper 39532d28ee6061930b2a9c170cbaaeea028c88a7b89Daniel Jasper void parseIncludeDirective() { 39632d28ee6061930b2a9c170cbaaeea028c88a7b89Daniel Jasper next(); 39732d28ee6061930b2a9c170cbaaeea028c88a7b89Daniel Jasper if (CurrentToken != NULL && CurrentToken->is(tok::less)) { 39832d28ee6061930b2a9c170cbaaeea028c88a7b89Daniel Jasper next(); 39932d28ee6061930b2a9c170cbaaeea028c88a7b89Daniel Jasper while (CurrentToken != NULL) { 400b398701e4cbb9a55d90a60e3f4f4bc577446d098Manuel Klimek if (CurrentToken->isNot(tok::comment) || CurrentToken->Next) 40132d28ee6061930b2a9c170cbaaeea028c88a7b89Daniel Jasper CurrentToken->Type = TT_ImplicitStringLiteral; 40232d28ee6061930b2a9c170cbaaeea028c88a7b89Daniel Jasper next(); 40332d28ee6061930b2a9c170cbaaeea028c88a7b89Daniel Jasper } 40432d28ee6061930b2a9c170cbaaeea028c88a7b89Daniel Jasper } else { 40532d28ee6061930b2a9c170cbaaeea028c88a7b89Daniel Jasper while (CurrentToken != NULL) { 4063a204418482c9ae70ad482e781132c54306c3aa6Daniel Jasper if (CurrentToken->is(tok::string_literal)) 4073a204418482c9ae70ad482e781132c54306c3aa6Daniel Jasper // Mark these string literals as "implicit" literals, too, so that 4083a204418482c9ae70ad482e781132c54306c3aa6Daniel Jasper // they are not split or line-wrapped. 4093a204418482c9ae70ad482e781132c54306c3aa6Daniel Jasper CurrentToken->Type = TT_ImplicitStringLiteral; 41032d28ee6061930b2a9c170cbaaeea028c88a7b89Daniel Jasper next(); 41132d28ee6061930b2a9c170cbaaeea028c88a7b89Daniel Jasper } 41232d28ee6061930b2a9c170cbaaeea028c88a7b89Daniel Jasper } 41332d28ee6061930b2a9c170cbaaeea028c88a7b89Daniel Jasper } 41432d28ee6061930b2a9c170cbaaeea028c88a7b89Daniel Jasper 41532d28ee6061930b2a9c170cbaaeea028c88a7b89Daniel Jasper void parseWarningOrError() { 41632d28ee6061930b2a9c170cbaaeea028c88a7b89Daniel Jasper next(); 41732d28ee6061930b2a9c170cbaaeea028c88a7b89Daniel Jasper // We still want to format the whitespace left of the first token of the 41832d28ee6061930b2a9c170cbaaeea028c88a7b89Daniel Jasper // warning or error. 41932d28ee6061930b2a9c170cbaaeea028c88a7b89Daniel Jasper next(); 42032d28ee6061930b2a9c170cbaaeea028c88a7b89Daniel Jasper while (CurrentToken != NULL) { 42132d28ee6061930b2a9c170cbaaeea028c88a7b89Daniel Jasper CurrentToken->Type = TT_ImplicitStringLiteral; 42232d28ee6061930b2a9c170cbaaeea028c88a7b89Daniel Jasper next(); 42332d28ee6061930b2a9c170cbaaeea028c88a7b89Daniel Jasper } 42432d28ee6061930b2a9c170cbaaeea028c88a7b89Daniel Jasper } 42532d28ee6061930b2a9c170cbaaeea028c88a7b89Daniel Jasper 42632d28ee6061930b2a9c170cbaaeea028c88a7b89Daniel Jasper void parsePreprocessorDirective() { 42732d28ee6061930b2a9c170cbaaeea028c88a7b89Daniel Jasper next(); 42832d28ee6061930b2a9c170cbaaeea028c88a7b89Daniel Jasper if (CurrentToken == NULL) 42932d28ee6061930b2a9c170cbaaeea028c88a7b89Daniel Jasper return; 43032d28ee6061930b2a9c170cbaaeea028c88a7b89Daniel Jasper // Hashes in the middle of a line can lead to any strange token 43132d28ee6061930b2a9c170cbaaeea028c88a7b89Daniel Jasper // sequence. 432b398701e4cbb9a55d90a60e3f4f4bc577446d098Manuel Klimek if (CurrentToken->Tok.getIdentifierInfo() == NULL) 43332d28ee6061930b2a9c170cbaaeea028c88a7b89Daniel Jasper return; 434b398701e4cbb9a55d90a60e3f4f4bc577446d098Manuel Klimek switch (CurrentToken->Tok.getIdentifierInfo()->getPPKeywordID()) { 43532d28ee6061930b2a9c170cbaaeea028c88a7b89Daniel Jasper case tok::pp_include: 43632d28ee6061930b2a9c170cbaaeea028c88a7b89Daniel Jasper case tok::pp_import: 43732d28ee6061930b2a9c170cbaaeea028c88a7b89Daniel Jasper parseIncludeDirective(); 43832d28ee6061930b2a9c170cbaaeea028c88a7b89Daniel Jasper break; 43932d28ee6061930b2a9c170cbaaeea028c88a7b89Daniel Jasper case tok::pp_error: 44032d28ee6061930b2a9c170cbaaeea028c88a7b89Daniel Jasper case tok::pp_warning: 44132d28ee6061930b2a9c170cbaaeea028c88a7b89Daniel Jasper parseWarningOrError(); 44232d28ee6061930b2a9c170cbaaeea028c88a7b89Daniel Jasper break; 443aae7bad5cdd804cb88e918e9defbf5faf69f4cfdDaniel Jasper case tok::pp_if: 444aae7bad5cdd804cb88e918e9defbf5faf69f4cfdDaniel Jasper case tok::pp_elif: 445aae7bad5cdd804cb88e918e9defbf5faf69f4cfdDaniel Jasper parseLine(); 446aae7bad5cdd804cb88e918e9defbf5faf69f4cfdDaniel Jasper break; 44732d28ee6061930b2a9c170cbaaeea028c88a7b89Daniel Jasper default: 44832d28ee6061930b2a9c170cbaaeea028c88a7b89Daniel Jasper break; 44932d28ee6061930b2a9c170cbaaeea028c88a7b89Daniel Jasper } 4505b7e7b0ec77f49c1b24deffc9b7032ca16ca9f0dDaniel Jasper while (CurrentToken != NULL) 4515b7e7b0ec77f49c1b24deffc9b7032ca16ca9f0dDaniel Jasper next(); 45232d28ee6061930b2a9c170cbaaeea028c88a7b89Daniel Jasper } 45332d28ee6061930b2a9c170cbaaeea028c88a7b89Daniel Jasper 45495e8e468af284afb11ec1f994d0b5076fc7423d9Nico Weberpublic: 45532d28ee6061930b2a9c170cbaaeea028c88a7b89Daniel Jasper LineType parseLine() { 45632d28ee6061930b2a9c170cbaaeea028c88a7b89Daniel Jasper int PeriodsAndArrows = 0; 457b398701e4cbb9a55d90a60e3f4f4bc577446d098Manuel Klimek FormatToken *LastPeriodOrArrow = NULL; 45832d28ee6061930b2a9c170cbaaeea028c88a7b89Daniel Jasper bool CanBeBuilderTypeStmt = true; 45932d28ee6061930b2a9c170cbaaeea028c88a7b89Daniel Jasper if (CurrentToken->is(tok::hash)) { 46032d28ee6061930b2a9c170cbaaeea028c88a7b89Daniel Jasper parsePreprocessorDirective(); 46132d28ee6061930b2a9c170cbaaeea028c88a7b89Daniel Jasper return LT_PreprocessorDirective; 46232d28ee6061930b2a9c170cbaaeea028c88a7b89Daniel Jasper } 46332d28ee6061930b2a9c170cbaaeea028c88a7b89Daniel Jasper while (CurrentToken != NULL) { 46432d28ee6061930b2a9c170cbaaeea028c88a7b89Daniel Jasper if (CurrentToken->is(tok::kw_virtual)) 46532d28ee6061930b2a9c170cbaaeea028c88a7b89Daniel Jasper KeywordVirtualFound = true; 466e74de28ec3692986f3467a7f160a0e293277fa7eAlexander Kornienko if (CurrentToken->isOneOf(tok::period, tok::arrow)) { 46732d28ee6061930b2a9c170cbaaeea028c88a7b89Daniel Jasper ++PeriodsAndArrows; 468248497199bc56e86d1c089beb9529f3b3d77abb1Daniel Jasper LastPeriodOrArrow = CurrentToken; 469248497199bc56e86d1c089beb9529f3b3d77abb1Daniel Jasper } 470b398701e4cbb9a55d90a60e3f4f4bc577446d098Manuel Klimek FormatToken *TheToken = CurrentToken; 47132d28ee6061930b2a9c170cbaaeea028c88a7b89Daniel Jasper if (!consumeToken()) 47232d28ee6061930b2a9c170cbaaeea028c88a7b89Daniel Jasper return LT_Invalid; 473b398701e4cbb9a55d90a60e3f4f4bc577446d098Manuel Klimek if (TheToken->getPrecedence() > prec::Assignment && 47482282dc907a04b1931f8f578693b035ad751fd3bDaniel Jasper TheToken->Type == TT_BinaryOperator) 4754a544e5856ceadef1c095c7d1ae5c8d760851d59Daniel Jasper CanBeBuilderTypeStmt = false; 47632d28ee6061930b2a9c170cbaaeea028c88a7b89Daniel Jasper } 47732d28ee6061930b2a9c170cbaaeea028c88a7b89Daniel Jasper if (KeywordVirtualFound) 47832d28ee6061930b2a9c170cbaaeea028c88a7b89Daniel Jasper return LT_VirtualFunctionDecl; 47932d28ee6061930b2a9c170cbaaeea028c88a7b89Daniel Jasper 48032d28ee6061930b2a9c170cbaaeea028c88a7b89Daniel Jasper // Assume a builder-type call if there are 2 or more "." and "->". 481248497199bc56e86d1c089beb9529f3b3d77abb1Daniel Jasper if (PeriodsAndArrows >= 2 && CanBeBuilderTypeStmt) { 482248497199bc56e86d1c089beb9529f3b3d77abb1Daniel Jasper LastPeriodOrArrow->LastInChainOfCalls = true; 48332d28ee6061930b2a9c170cbaaeea028c88a7b89Daniel Jasper return LT_BuilderTypeCall; 484248497199bc56e86d1c089beb9529f3b3d77abb1Daniel Jasper } 48532d28ee6061930b2a9c170cbaaeea028c88a7b89Daniel Jasper 486b398701e4cbb9a55d90a60e3f4f4bc577446d098Manuel Klimek if (Line.First->Type == TT_ObjCMethodSpecifier) { 4874e778091b0ff42895644ca8bef30f1a91ba6b32bDaniel Jasper if (Contexts.back().FirstObjCSelectorName != NULL) 4884e778091b0ff42895644ca8bef30f1a91ba6b32bDaniel Jasper Contexts.back().FirstObjCSelectorName->LongestObjCSelectorName = 4894e778091b0ff42895644ca8bef30f1a91ba6b32bDaniel Jasper Contexts.back().LongestObjCSelectorName; 49063d7cedca8616921c1908c88c2f23fcd67bbab99Daniel Jasper return LT_ObjCMethodDecl; 49163d7cedca8616921c1908c88c2f23fcd67bbab99Daniel Jasper } 49263d7cedca8616921c1908c88c2f23fcd67bbab99Daniel Jasper 49332d28ee6061930b2a9c170cbaaeea028c88a7b89Daniel Jasper return LT_Other; 49432d28ee6061930b2a9c170cbaaeea028c88a7b89Daniel Jasper } 49532d28ee6061930b2a9c170cbaaeea028c88a7b89Daniel Jasper 49695e8e468af284afb11ec1f994d0b5076fc7423d9Nico Weberprivate: 49732d28ee6061930b2a9c170cbaaeea028c88a7b89Daniel Jasper void next() { 4980178673f541685cf5067814dfeee2644078e39a9Daniel Jasper if (CurrentToken != NULL) { 4990178673f541685cf5067814dfeee2644078e39a9Daniel Jasper determineTokenType(*CurrentToken); 5004e778091b0ff42895644ca8bef30f1a91ba6b32bDaniel Jasper CurrentToken->BindingStrength = Contexts.back().BindingStrength; 5010178673f541685cf5067814dfeee2644078e39a9Daniel Jasper } 5020178673f541685cf5067814dfeee2644078e39a9Daniel Jasper 503b398701e4cbb9a55d90a60e3f4f4bc577446d098Manuel Klimek if (CurrentToken != NULL) 504b398701e4cbb9a55d90a60e3f4f4bc577446d098Manuel Klimek CurrentToken = CurrentToken->Next; 505d0f349be1422a123fdb28d6dd556f7300e6d51e9Daniel Jasper 506d0f349be1422a123fdb28d6dd556f7300e6d51e9Daniel Jasper // Reset token type in case we have already looked at it and then recovered 507d0f349be1422a123fdb28d6dd556f7300e6d51e9Daniel Jasper // from an error (e.g. failure to find the matching >). 508d0f349be1422a123fdb28d6dd556f7300e6d51e9Daniel Jasper if (CurrentToken != NULL) 509d0f349be1422a123fdb28d6dd556f7300e6d51e9Daniel Jasper CurrentToken->Type = TT_Unknown; 51032d28ee6061930b2a9c170cbaaeea028c88a7b89Daniel Jasper } 51132d28ee6061930b2a9c170cbaaeea028c88a7b89Daniel Jasper 5124e778091b0ff42895644ca8bef30f1a91ba6b32bDaniel Jasper /// \brief A struct to hold information valid in a specific context, e.g. 5134e778091b0ff42895644ca8bef30f1a91ba6b32bDaniel Jasper /// a pair of parenthesis. 5144e778091b0ff42895644ca8bef30f1a91ba6b32bDaniel Jasper struct Context { 515923ebef120a37122ce50722a85cbe42c0c2dab53Daniel Jasper Context(tok::TokenKind ContextKind, unsigned BindingStrength, 516923ebef120a37122ce50722a85cbe42c0c2dab53Daniel Jasper bool IsExpression) 517923ebef120a37122ce50722a85cbe42c0c2dab53Daniel Jasper : ContextKind(ContextKind), BindingStrength(BindingStrength), 518923ebef120a37122ce50722a85cbe42c0c2dab53Daniel Jasper LongestObjCSelectorName(0), ColonIsForRangeExpr(false), 519f2ff8126e9e9df368f31b1f968d8fc80f99809b3Nico Weber ColonIsObjCDictLiteral(false), ColonIsObjCMethodExpr(false), 520f2ff8126e9e9df368f31b1f968d8fc80f99809b3Nico Weber FirstObjCSelectorName(NULL), FirstStartOfName(NULL), 521f2ff8126e9e9df368f31b1f968d8fc80f99809b3Nico Weber IsExpression(IsExpression), CanBeExpression(true) {} 522923ebef120a37122ce50722a85cbe42c0c2dab53Daniel Jasper 523923ebef120a37122ce50722a85cbe42c0c2dab53Daniel Jasper tok::TokenKind ContextKind; 5244e778091b0ff42895644ca8bef30f1a91ba6b32bDaniel Jasper unsigned BindingStrength; 5254e778091b0ff42895644ca8bef30f1a91ba6b32bDaniel Jasper unsigned LongestObjCSelectorName; 5264e778091b0ff42895644ca8bef30f1a91ba6b32bDaniel Jasper bool ColonIsForRangeExpr; 527f2ff8126e9e9df368f31b1f968d8fc80f99809b3Nico Weber bool ColonIsObjCDictLiteral; 5284e778091b0ff42895644ca8bef30f1a91ba6b32bDaniel Jasper bool ColonIsObjCMethodExpr; 529b398701e4cbb9a55d90a60e3f4f4bc577446d098Manuel Klimek FormatToken *FirstObjCSelectorName; 530b398701e4cbb9a55d90a60e3f4f4bc577446d098Manuel Klimek FormatToken *FirstStartOfName; 5314e778091b0ff42895644ca8bef30f1a91ba6b32bDaniel Jasper bool IsExpression; 5326f21a988990ff5872822dcb049bd8fc65ce3d236Daniel Jasper bool CanBeExpression; 5334e778091b0ff42895644ca8bef30f1a91ba6b32bDaniel Jasper }; 5344e778091b0ff42895644ca8bef30f1a91ba6b32bDaniel Jasper 5354e778091b0ff42895644ca8bef30f1a91ba6b32bDaniel Jasper /// \brief Puts a new \c Context onto the stack \c Contexts for the lifetime 5364e778091b0ff42895644ca8bef30f1a91ba6b32bDaniel Jasper /// of each instance. 5374e778091b0ff42895644ca8bef30f1a91ba6b32bDaniel Jasper struct ScopedContextCreator { 5384e778091b0ff42895644ca8bef30f1a91ba6b32bDaniel Jasper AnnotatingParser &P; 5394e778091b0ff42895644ca8bef30f1a91ba6b32bDaniel Jasper 540923ebef120a37122ce50722a85cbe42c0c2dab53Daniel Jasper ScopedContextCreator(AnnotatingParser &P, tok::TokenKind ContextKind, 541923ebef120a37122ce50722a85cbe42c0c2dab53Daniel Jasper unsigned Increase) 542923ebef120a37122ce50722a85cbe42c0c2dab53Daniel Jasper : P(P) { 543923ebef120a37122ce50722a85cbe42c0c2dab53Daniel Jasper P.Contexts.push_back( 544923ebef120a37122ce50722a85cbe42c0c2dab53Daniel Jasper Context(ContextKind, P.Contexts.back().BindingStrength + Increase, 545923ebef120a37122ce50722a85cbe42c0c2dab53Daniel Jasper P.Contexts.back().IsExpression)); 5464e778091b0ff42895644ca8bef30f1a91ba6b32bDaniel Jasper } 5474e778091b0ff42895644ca8bef30f1a91ba6b32bDaniel Jasper 5484e778091b0ff42895644ca8bef30f1a91ba6b32bDaniel Jasper ~ScopedContextCreator() { P.Contexts.pop_back(); } 5494e778091b0ff42895644ca8bef30f1a91ba6b32bDaniel Jasper }; 5500178673f541685cf5067814dfeee2644078e39a9Daniel Jasper 551b398701e4cbb9a55d90a60e3f4f4bc577446d098Manuel Klimek void determineTokenType(FormatToken &Current) { 552b398701e4cbb9a55d90a60e3f4f4bc577446d098Manuel Klimek if (Current.getPrecedence() == prec::Assignment && 553b398701e4cbb9a55d90a60e3f4f4bc577446d098Manuel Klimek (!Current.Previous || Current.Previous->isNot(tok::kw_operator))) { 5544e778091b0ff42895644ca8bef30f1a91ba6b32bDaniel Jasper Contexts.back().IsExpression = true; 555b398701e4cbb9a55d90a60e3f4f4bc577446d098Manuel Klimek for (FormatToken *Previous = Current.Previous; 55695e8e468af284afb11ec1f994d0b5076fc7423d9Nico Weber Previous && Previous->isNot(tok::comma); 557b398701e4cbb9a55d90a60e3f4f4bc577446d098Manuel Klimek Previous = Previous->Previous) { 5589c65b069821b7de79427e291b006293a0f55ff8fDaniel Jasper if (Previous->is(tok::r_square)) 5599c65b069821b7de79427e291b006293a0f55ff8fDaniel Jasper Previous = Previous->MatchingParen; 5600178673f541685cf5067814dfeee2644078e39a9Daniel Jasper if (Previous->Type == TT_BinaryOperator && 561e74de28ec3692986f3467a7f160a0e293277fa7eAlexander Kornienko Previous->isOneOf(tok::star, tok::amp)) { 5620178673f541685cf5067814dfeee2644078e39a9Daniel Jasper Previous->Type = TT_PointerOrReference; 5630178673f541685cf5067814dfeee2644078e39a9Daniel Jasper } 5640178673f541685cf5067814dfeee2644078e39a9Daniel Jasper } 565e74de28ec3692986f3467a7f160a0e293277fa7eAlexander Kornienko } else if (Current.isOneOf(tok::kw_return, tok::kw_throw) || 56695e8e468af284afb11ec1f994d0b5076fc7423d9Nico Weber (Current.is(tok::l_paren) && !Line.MustBeDeclaration && 567378d93dcf7ec80661efc65642dc6266c9e13780aDaniel Jasper !Line.InPPDirective && 568b398701e4cbb9a55d90a60e3f4f4bc577446d098Manuel Klimek (!Current.Previous || Current.Previous->isNot(tok::kw_for)))) { 5694e778091b0ff42895644ca8bef30f1a91ba6b32bDaniel Jasper Contexts.back().IsExpression = true; 570e74de28ec3692986f3467a7f160a0e293277fa7eAlexander Kornienko } else if (Current.isOneOf(tok::r_paren, tok::greater, tok::comma)) { 571b398701e4cbb9a55d90a60e3f4f4bc577446d098Manuel Klimek for (FormatToken *Previous = Current.Previous; 572e74de28ec3692986f3467a7f160a0e293277fa7eAlexander Kornienko Previous && Previous->isOneOf(tok::star, tok::amp); 573b398701e4cbb9a55d90a60e3f4f4bc577446d098Manuel Klimek Previous = Previous->Previous) 57495e8e468af284afb11ec1f994d0b5076fc7423d9Nico Weber Previous->Type = TT_PointerOrReference; 575b398701e4cbb9a55d90a60e3f4f4bc577446d098Manuel Klimek } else if (Current.Previous && 576b398701e4cbb9a55d90a60e3f4f4bc577446d098Manuel Klimek Current.Previous->Type == TT_CtorInitializerColon) { 577d0f349be1422a123fdb28d6dd556f7300e6d51e9Daniel Jasper Contexts.back().IsExpression = true; 5786f21a988990ff5872822dcb049bd8fc65ce3d236Daniel Jasper } else if (Current.is(tok::kw_new)) { 5796f21a988990ff5872822dcb049bd8fc65ce3d236Daniel Jasper Contexts.back().CanBeExpression = false; 58016a69ef481f4580c571b6c693a5d3dd64ea56b53Daniel Jasper } else if (Current.is(tok::semi)) { 58116a69ef481f4580c571b6c693a5d3dd64ea56b53Daniel Jasper // This should be the condition or increment in a for-loop. 58216a69ef481f4580c571b6c693a5d3dd64ea56b53Daniel Jasper Contexts.back().IsExpression = true; 58395e8e468af284afb11ec1f994d0b5076fc7423d9Nico Weber } 5840178673f541685cf5067814dfeee2644078e39a9Daniel Jasper 5850178673f541685cf5067814dfeee2644078e39a9Daniel Jasper if (Current.Type == TT_Unknown) { 586b398701e4cbb9a55d90a60e3f4f4bc577446d098Manuel Klimek if (Current.Previous && Current.is(tok::identifier) && 587b398701e4cbb9a55d90a60e3f4f4bc577446d098Manuel Klimek ((Current.Previous->is(tok::identifier) && 588b398701e4cbb9a55d90a60e3f4f4bc577446d098Manuel Klimek Current.Previous->Tok.getIdentifierInfo()->getPPKeywordID() == 58911e13801d8a25cea011c2a154380c371b6ddaaf6Daniel Jasper tok::pp_not_keyword) || 590b398701e4cbb9a55d90a60e3f4f4bc577446d098Manuel Klimek isSimpleTypeSpecifier(*Current.Previous) || 591b398701e4cbb9a55d90a60e3f4f4bc577446d098Manuel Klimek Current.Previous->Type == TT_PointerOrReference || 592b398701e4cbb9a55d90a60e3f4f4bc577446d098Manuel Klimek Current.Previous->Type == TT_TemplateCloser)) { 5938ed9f2b25f082a1643ab5310f9eec33cf31a7577Daniel Jasper Contexts.back().FirstStartOfName = &Current; 5943c08a818a6ac9115fe8880af9bbf5a0a87bdffaaDaniel Jasper Current.Type = TT_StartOfName; 5951407bee187d7b964d5293ac8bf4f7a490c78cec6Daniel Jasper NameFound = true; 596e74de28ec3692986f3467a7f160a0e293277fa7eAlexander Kornienko } else if (Current.isOneOf(tok::star, tok::amp, tok::ampamp)) { 5974e778091b0ff42895644ca8bef30f1a91ba6b32bDaniel Jasper Current.Type = 5984e778091b0ff42895644ca8bef30f1a91ba6b32bDaniel Jasper determineStarAmpUsage(Current, Contexts.back().IsExpression); 599e74de28ec3692986f3467a7f160a0e293277fa7eAlexander Kornienko } else if (Current.isOneOf(tok::minus, tok::plus, tok::caret)) { 6000178673f541685cf5067814dfeee2644078e39a9Daniel Jasper Current.Type = determinePlusMinusCaretUsage(Current); 601e74de28ec3692986f3467a7f160a0e293277fa7eAlexander Kornienko } else if (Current.isOneOf(tok::minusminus, tok::plusplus)) { 6020178673f541685cf5067814dfeee2644078e39a9Daniel Jasper Current.Type = determineIncrementUsage(Current); 6030178673f541685cf5067814dfeee2644078e39a9Daniel Jasper } else if (Current.is(tok::exclaim)) { 6040178673f541685cf5067814dfeee2644078e39a9Daniel Jasper Current.Type = TT_UnaryOperator; 605ac3223e45e7d17c65b143439313800eef4fdf71dDaniel Jasper } else if (Current.isBinaryOperator()) { 6060178673f541685cf5067814dfeee2644078e39a9Daniel Jasper Current.Type = TT_BinaryOperator; 6070178673f541685cf5067814dfeee2644078e39a9Daniel Jasper } else if (Current.is(tok::comment)) { 60800895106f9ed602af67984ec4d225a0cdc8c12afAlexander Kornienko if (Current.TokenText.startswith("//")) 6090178673f541685cf5067814dfeee2644078e39a9Daniel Jasper Current.Type = TT_LineComment; 6100178673f541685cf5067814dfeee2644078e39a9Daniel Jasper else 6110178673f541685cf5067814dfeee2644078e39a9Daniel Jasper Current.Type = TT_BlockComment; 61237d693160eba22343e08d7bcf66cd132ace77e5cNico Weber } else if (Current.is(tok::r_paren)) { 613b8b4295b4ee161bfb76ff7b0ec1007bfd959553bDaniel Jasper FormatToken *LeftOfParens = NULL; 614b8b4295b4ee161bfb76ff7b0ec1007bfd959553bDaniel Jasper if (Current.MatchingParen) 615b8b4295b4ee161bfb76ff7b0ec1007bfd959553bDaniel Jasper LeftOfParens = Current.MatchingParen->getPreviousNoneComment(); 616b8b4295b4ee161bfb76ff7b0ec1007bfd959553bDaniel Jasper bool IsCast = false; 617b8b4295b4ee161bfb76ff7b0ec1007bfd959553bDaniel Jasper bool ParensAreEmpty = Current.Previous == Current.MatchingParen; 618b8b4295b4ee161bfb76ff7b0ec1007bfd959553bDaniel Jasper bool ParensAreType = !Current.Previous || 619b398701e4cbb9a55d90a60e3f4f4bc577446d098Manuel Klimek Current.Previous->Type == TT_PointerOrReference || 620b8b4295b4ee161bfb76ff7b0ec1007bfd959553bDaniel Jasper Current.Previous->Type == TT_TemplateCloser || 621b8b4295b4ee161bfb76ff7b0ec1007bfd959553bDaniel Jasper isSimpleTypeSpecifier(*Current.Previous); 62237d693160eba22343e08d7bcf66cd132ace77e5cNico Weber bool ParensCouldEndDecl = 623b398701e4cbb9a55d90a60e3f4f4bc577446d098Manuel Klimek Current.Next && 624b398701e4cbb9a55d90a60e3f4f4bc577446d098Manuel Klimek Current.Next->isOneOf(tok::equal, tok::semi, tok::l_brace); 6256a365aaa057a8c445d25344c0433726c752b3e7dDaniel Jasper bool IsSizeOfOrAlignOf = 626b8b4295b4ee161bfb76ff7b0ec1007bfd959553bDaniel Jasper LeftOfParens && 627b8b4295b4ee161bfb76ff7b0ec1007bfd959553bDaniel Jasper LeftOfParens->isOneOf(tok::kw_sizeof, tok::kw_alignof); 628b8b4295b4ee161bfb76ff7b0ec1007bfd959553bDaniel Jasper if (ParensAreType && !ParensCouldEndDecl && !IsSizeOfOrAlignOf && 62937eff83413a064c504c5a42097e4f5dd0b2962d2Daniel Jasper Contexts.back().IsExpression) 630b8b4295b4ee161bfb76ff7b0ec1007bfd959553bDaniel Jasper IsCast = true; 631b8b4295b4ee161bfb76ff7b0ec1007bfd959553bDaniel Jasper if (Current.Next && 632ff1a2e519ebcd6d7a060eac7ba8aca37b2bf89d0Daniel Jasper Current.Next->isNot(tok::string_literal) && 633b8b4295b4ee161bfb76ff7b0ec1007bfd959553bDaniel Jasper (Current.Next->Tok.isLiteral() || 634b8b4295b4ee161bfb76ff7b0ec1007bfd959553bDaniel Jasper Current.Next->isOneOf(tok::kw_sizeof, tok::kw_alignof))) 635b8b4295b4ee161bfb76ff7b0ec1007bfd959553bDaniel Jasper IsCast = true; 636b8b4295b4ee161bfb76ff7b0ec1007bfd959553bDaniel Jasper // If there is an identifier after the (), it is likely a cast, unless 637b8b4295b4ee161bfb76ff7b0ec1007bfd959553bDaniel Jasper // there is also an identifier before the (). 638ff1a2e519ebcd6d7a060eac7ba8aca37b2bf89d0Daniel Jasper if (LeftOfParens && (LeftOfParens->Tok.getIdentifierInfo() == NULL || 639ff1a2e519ebcd6d7a060eac7ba8aca37b2bf89d0Daniel Jasper LeftOfParens->is(tok::kw_return)) && 640ff1a2e519ebcd6d7a060eac7ba8aca37b2bf89d0Daniel Jasper LeftOfParens->Type != TT_TemplateCloser && 641b8b4295b4ee161bfb76ff7b0ec1007bfd959553bDaniel Jasper LeftOfParens->Type != TT_ObjCMethodExpr && Current.Next && 642b8b4295b4ee161bfb76ff7b0ec1007bfd959553bDaniel Jasper (Current.Next->is(tok::identifier))) 643b8b4295b4ee161bfb76ff7b0ec1007bfd959553bDaniel Jasper IsCast = true; 644b8b4295b4ee161bfb76ff7b0ec1007bfd959553bDaniel Jasper if (IsCast && !ParensAreEmpty) 64537d693160eba22343e08d7bcf66cd132ace77e5cNico Weber Current.Type = TT_CastRParen; 646b398701e4cbb9a55d90a60e3f4f4bc577446d098Manuel Klimek } else if (Current.is(tok::at) && Current.Next) { 647b398701e4cbb9a55d90a60e3f4f4bc577446d098Manuel Klimek switch (Current.Next->Tok.getObjCKeywordID()) { 6480178673f541685cf5067814dfeee2644078e39a9Daniel Jasper case tok::objc_interface: 6490178673f541685cf5067814dfeee2644078e39a9Daniel Jasper case tok::objc_implementation: 6500178673f541685cf5067814dfeee2644078e39a9Daniel Jasper case tok::objc_protocol: 6510178673f541685cf5067814dfeee2644078e39a9Daniel Jasper Current.Type = TT_ObjCDecl; 6520178673f541685cf5067814dfeee2644078e39a9Daniel Jasper break; 6530178673f541685cf5067814dfeee2644078e39a9Daniel Jasper case tok::objc_property: 6540178673f541685cf5067814dfeee2644078e39a9Daniel Jasper Current.Type = TT_ObjCProperty; 6550178673f541685cf5067814dfeee2644078e39a9Daniel Jasper break; 6560178673f541685cf5067814dfeee2644078e39a9Daniel Jasper default: 6570178673f541685cf5067814dfeee2644078e39a9Daniel Jasper break; 6580178673f541685cf5067814dfeee2644078e39a9Daniel Jasper } 6595ad390d27f8f7cb6628bc2c32beba4d25bc2718bDaniel Jasper } else if (Current.is(tok::period)) { 660b398701e4cbb9a55d90a60e3f4f4bc577446d098Manuel Klimek FormatToken *PreviousNoComment = Current.getPreviousNoneComment(); 6615ad390d27f8f7cb6628bc2c32beba4d25bc2718bDaniel Jasper if (PreviousNoComment && 6625ad390d27f8f7cb6628bc2c32beba4d25bc2718bDaniel Jasper PreviousNoComment->isOneOf(tok::comma, tok::l_brace)) 6635ad390d27f8f7cb6628bc2c32beba4d25bc2718bDaniel Jasper Current.Type = TT_DesignatedInitializerPeriod; 6640178673f541685cf5067814dfeee2644078e39a9Daniel Jasper } 6650178673f541685cf5067814dfeee2644078e39a9Daniel Jasper } 6660178673f541685cf5067814dfeee2644078e39a9Daniel Jasper } 6670178673f541685cf5067814dfeee2644078e39a9Daniel Jasper 6680178673f541685cf5067814dfeee2644078e39a9Daniel Jasper /// \brief Return the type of the given token assuming it is * or &. 669b398701e4cbb9a55d90a60e3f4f4bc577446d098Manuel Klimek TokenType determineStarAmpUsage(const FormatToken &Tok, bool IsExpression) { 670b398701e4cbb9a55d90a60e3f4f4bc577446d098Manuel Klimek const FormatToken *PrevToken = Tok.getPreviousNoneComment(); 6710178673f541685cf5067814dfeee2644078e39a9Daniel Jasper if (PrevToken == NULL) 6720178673f541685cf5067814dfeee2644078e39a9Daniel Jasper return TT_UnaryOperator; 6730178673f541685cf5067814dfeee2644078e39a9Daniel Jasper 674b398701e4cbb9a55d90a60e3f4f4bc577446d098Manuel Klimek const FormatToken *NextToken = Tok.getNextNoneComment(); 6750178673f541685cf5067814dfeee2644078e39a9Daniel Jasper if (NextToken == NULL) 6760178673f541685cf5067814dfeee2644078e39a9Daniel Jasper return TT_Unknown; 6770178673f541685cf5067814dfeee2644078e39a9Daniel Jasper 678431f5918281dd1f178bf838b7a47ab1d11c52961Daniel Jasper if (PrevToken->is(tok::coloncolon) || 679431f5918281dd1f178bf838b7a47ab1d11c52961Daniel Jasper (PrevToken->is(tok::l_paren) && !IsExpression)) 6808a5d7cd100ebfb8c6b353ee4ad5b14ab4105d32dDaniel Jasper return TT_PointerOrReference; 6818a5d7cd100ebfb8c6b353ee4ad5b14ab4105d32dDaniel Jasper 682e74de28ec3692986f3467a7f160a0e293277fa7eAlexander Kornienko if (PrevToken->isOneOf(tok::l_paren, tok::l_square, tok::l_brace, 683d3cf17b5f1fed43dbd0cd35c43d15139803c9c84Daniel Jasper tok::comma, tok::semi, tok::kw_return, tok::colon, 684dbef71ebc66fe5553db01eb8e95b696c3223e737Daniel Jasper tok::equal, tok::kw_delete) || 685e74de28ec3692986f3467a7f160a0e293277fa7eAlexander Kornienko PrevToken->Type == TT_BinaryOperator || 6860178673f541685cf5067814dfeee2644078e39a9Daniel Jasper PrevToken->Type == TT_UnaryOperator || PrevToken->Type == TT_CastRParen) 6870178673f541685cf5067814dfeee2644078e39a9Daniel Jasper return TT_UnaryOperator; 6880178673f541685cf5067814dfeee2644078e39a9Daniel Jasper 689e8a97985f72b4e11435ff2107c0f11e925fb6d96Nico Weber if (NextToken->is(tok::l_square)) 690e8a97985f72b4e11435ff2107c0f11e925fb6d96Nico Weber return TT_PointerOrReference; 691e8a97985f72b4e11435ff2107c0f11e925fb6d96Nico Weber 692b398701e4cbb9a55d90a60e3f4f4bc577446d098Manuel Klimek if (PrevToken->Tok.isLiteral() || 693e74de28ec3692986f3467a7f160a0e293277fa7eAlexander Kornienko PrevToken->isOneOf(tok::r_paren, tok::r_square) || 694b398701e4cbb9a55d90a60e3f4f4bc577446d098Manuel Klimek NextToken->Tok.isLiteral() || NextToken->isUnaryOperator()) 6950178673f541685cf5067814dfeee2644078e39a9Daniel Jasper return TT_BinaryOperator; 6960178673f541685cf5067814dfeee2644078e39a9Daniel Jasper 6970178673f541685cf5067814dfeee2644078e39a9Daniel Jasper // It is very unlikely that we are going to find a pointer or reference type 6980178673f541685cf5067814dfeee2644078e39a9Daniel Jasper // definition on the RHS of an assignment. 6990178673f541685cf5067814dfeee2644078e39a9Daniel Jasper if (IsExpression) 7000178673f541685cf5067814dfeee2644078e39a9Daniel Jasper return TT_BinaryOperator; 7010178673f541685cf5067814dfeee2644078e39a9Daniel Jasper 7020178673f541685cf5067814dfeee2644078e39a9Daniel Jasper return TT_PointerOrReference; 7030178673f541685cf5067814dfeee2644078e39a9Daniel Jasper } 7040178673f541685cf5067814dfeee2644078e39a9Daniel Jasper 705b398701e4cbb9a55d90a60e3f4f4bc577446d098Manuel Klimek TokenType determinePlusMinusCaretUsage(const FormatToken &Tok) { 706b398701e4cbb9a55d90a60e3f4f4bc577446d098Manuel Klimek const FormatToken *PrevToken = Tok.getPreviousNoneComment(); 707b8b4295b4ee161bfb76ff7b0ec1007bfd959553bDaniel Jasper if (PrevToken == NULL || PrevToken->Type == TT_CastRParen) 7080178673f541685cf5067814dfeee2644078e39a9Daniel Jasper return TT_UnaryOperator; 7090178673f541685cf5067814dfeee2644078e39a9Daniel Jasper 7100178673f541685cf5067814dfeee2644078e39a9Daniel Jasper // Use heuristics to recognize unary operators. 711e74de28ec3692986f3467a7f160a0e293277fa7eAlexander Kornienko if (PrevToken->isOneOf(tok::equal, tok::l_paren, tok::comma, tok::l_square, 712e74de28ec3692986f3467a7f160a0e293277fa7eAlexander Kornienko tok::question, tok::colon, tok::kw_return, 713e74de28ec3692986f3467a7f160a0e293277fa7eAlexander Kornienko tok::kw_case, tok::at, tok::l_brace)) 7140178673f541685cf5067814dfeee2644078e39a9Daniel Jasper return TT_UnaryOperator; 7150178673f541685cf5067814dfeee2644078e39a9Daniel Jasper 716ee0feec7362053f22b6c01d12e1bfa06fb3ac93fNico Weber // There can't be two consecutive binary operators. 7170178673f541685cf5067814dfeee2644078e39a9Daniel Jasper if (PrevToken->Type == TT_BinaryOperator) 7180178673f541685cf5067814dfeee2644078e39a9Daniel Jasper return TT_UnaryOperator; 7190178673f541685cf5067814dfeee2644078e39a9Daniel Jasper 7200178673f541685cf5067814dfeee2644078e39a9Daniel Jasper // Fall back to marking the token as binary operator. 7210178673f541685cf5067814dfeee2644078e39a9Daniel Jasper return TT_BinaryOperator; 7220178673f541685cf5067814dfeee2644078e39a9Daniel Jasper } 7230178673f541685cf5067814dfeee2644078e39a9Daniel Jasper 7240178673f541685cf5067814dfeee2644078e39a9Daniel Jasper /// \brief Determine whether ++/-- are pre- or post-increments/-decrements. 725b398701e4cbb9a55d90a60e3f4f4bc577446d098Manuel Klimek TokenType determineIncrementUsage(const FormatToken &Tok) { 726b398701e4cbb9a55d90a60e3f4f4bc577446d098Manuel Klimek const FormatToken *PrevToken = Tok.getPreviousNoneComment(); 727b8b4295b4ee161bfb76ff7b0ec1007bfd959553bDaniel Jasper if (PrevToken == NULL || PrevToken->Type == TT_CastRParen) 7280178673f541685cf5067814dfeee2644078e39a9Daniel Jasper return TT_UnaryOperator; 729e74de28ec3692986f3467a7f160a0e293277fa7eAlexander Kornienko if (PrevToken->isOneOf(tok::r_paren, tok::r_square, tok::identifier)) 7300178673f541685cf5067814dfeee2644078e39a9Daniel Jasper return TT_TrailingUnaryOperator; 7310178673f541685cf5067814dfeee2644078e39a9Daniel Jasper 7320178673f541685cf5067814dfeee2644078e39a9Daniel Jasper return TT_UnaryOperator; 7330178673f541685cf5067814dfeee2644078e39a9Daniel Jasper } 7344e778091b0ff42895644ca8bef30f1a91ba6b32bDaniel Jasper 7358ed9f2b25f082a1643ab5310f9eec33cf31a7577Daniel Jasper // FIXME: This is copy&pasted from Sema. Put it in a common place and remove 7368ed9f2b25f082a1643ab5310f9eec33cf31a7577Daniel Jasper // duplication. 7378ed9f2b25f082a1643ab5310f9eec33cf31a7577Daniel Jasper /// \brief Determine whether the token kind starts a simple-type-specifier. 738b398701e4cbb9a55d90a60e3f4f4bc577446d098Manuel Klimek bool isSimpleTypeSpecifier(const FormatToken &Tok) const { 739b398701e4cbb9a55d90a60e3f4f4bc577446d098Manuel Klimek switch (Tok.Tok.getKind()) { 7408ed9f2b25f082a1643ab5310f9eec33cf31a7577Daniel Jasper case tok::kw_short: 7418ed9f2b25f082a1643ab5310f9eec33cf31a7577Daniel Jasper case tok::kw_long: 7428ed9f2b25f082a1643ab5310f9eec33cf31a7577Daniel Jasper case tok::kw___int64: 7438ed9f2b25f082a1643ab5310f9eec33cf31a7577Daniel Jasper case tok::kw___int128: 7448ed9f2b25f082a1643ab5310f9eec33cf31a7577Daniel Jasper case tok::kw_signed: 7458ed9f2b25f082a1643ab5310f9eec33cf31a7577Daniel Jasper case tok::kw_unsigned: 7468ed9f2b25f082a1643ab5310f9eec33cf31a7577Daniel Jasper case tok::kw_void: 7478ed9f2b25f082a1643ab5310f9eec33cf31a7577Daniel Jasper case tok::kw_char: 7488ed9f2b25f082a1643ab5310f9eec33cf31a7577Daniel Jasper case tok::kw_int: 7498ed9f2b25f082a1643ab5310f9eec33cf31a7577Daniel Jasper case tok::kw_half: 7508ed9f2b25f082a1643ab5310f9eec33cf31a7577Daniel Jasper case tok::kw_float: 7518ed9f2b25f082a1643ab5310f9eec33cf31a7577Daniel Jasper case tok::kw_double: 7528ed9f2b25f082a1643ab5310f9eec33cf31a7577Daniel Jasper case tok::kw_wchar_t: 7538ed9f2b25f082a1643ab5310f9eec33cf31a7577Daniel Jasper case tok::kw_bool: 7548ed9f2b25f082a1643ab5310f9eec33cf31a7577Daniel Jasper case tok::kw___underlying_type: 7558ed9f2b25f082a1643ab5310f9eec33cf31a7577Daniel Jasper case tok::annot_typename: 7568ed9f2b25f082a1643ab5310f9eec33cf31a7577Daniel Jasper case tok::kw_char16_t: 7578ed9f2b25f082a1643ab5310f9eec33cf31a7577Daniel Jasper case tok::kw_char32_t: 7588ed9f2b25f082a1643ab5310f9eec33cf31a7577Daniel Jasper case tok::kw_typeof: 7598ed9f2b25f082a1643ab5310f9eec33cf31a7577Daniel Jasper case tok::kw_decltype: 76000895106f9ed602af67984ec4d225a0cdc8c12afAlexander Kornienko return true; 7618ed9f2b25f082a1643ab5310f9eec33cf31a7577Daniel Jasper default: 76200895106f9ed602af67984ec4d225a0cdc8c12afAlexander Kornienko return false; 7638ed9f2b25f082a1643ab5310f9eec33cf31a7577Daniel Jasper } 7648ed9f2b25f082a1643ab5310f9eec33cf31a7577Daniel Jasper } 7658ed9f2b25f082a1643ab5310f9eec33cf31a7577Daniel Jasper 7664e778091b0ff42895644ca8bef30f1a91ba6b32bDaniel Jasper SmallVector<Context, 8> Contexts; 7674e778091b0ff42895644ca8bef30f1a91ba6b32bDaniel Jasper 7684e778091b0ff42895644ca8bef30f1a91ba6b32bDaniel Jasper AnnotatedLine &Line; 769b398701e4cbb9a55d90a60e3f4f4bc577446d098Manuel Klimek FormatToken *CurrentToken; 7704e778091b0ff42895644ca8bef30f1a91ba6b32bDaniel Jasper bool KeywordVirtualFound; 7711407bee187d7b964d5293ac8bf4f7a490c78cec6Daniel Jasper bool NameFound; 772c2e6d2a4a7fe9dfa7d52a38c6048b7b18e6b591aNico Weber IdentifierInfo &Ident_in; 77332d28ee6061930b2a9c170cbaaeea028c88a7b89Daniel Jasper}; 77432d28ee6061930b2a9c170cbaaeea028c88a7b89Daniel Jasper 77529f123b2fa0435bb1962f0d9e9a2e660f35fbb2fDaniel Jasper/// \brief Parses binary expressions by inserting fake parenthesis based on 77629f123b2fa0435bb1962f0d9e9a2e660f35fbb2fDaniel Jasper/// operator precedence. 77729f123b2fa0435bb1962f0d9e9a2e660f35fbb2fDaniel Jasperclass ExpressionParser { 77829f123b2fa0435bb1962f0d9e9a2e660f35fbb2fDaniel Jasperpublic: 7799acb8b4355028887e8cc4aa8f683aceee021a62bDaniel Jasper ExpressionParser(AnnotatedLine &Line) : Current(Line.First) { 7809acb8b4355028887e8cc4aa8f683aceee021a62bDaniel Jasper // Skip leading "}", e.g. in "} else if (...) {". 7819acb8b4355028887e8cc4aa8f683aceee021a62bDaniel Jasper if (Current->is(tok::r_brace)) 7829acb8b4355028887e8cc4aa8f683aceee021a62bDaniel Jasper next(); 7839acb8b4355028887e8cc4aa8f683aceee021a62bDaniel Jasper } 78429f123b2fa0435bb1962f0d9e9a2e660f35fbb2fDaniel Jasper 78529f123b2fa0435bb1962f0d9e9a2e660f35fbb2fDaniel Jasper /// \brief Parse expressions with the given operatore precedence. 786237d4c1c785be13656bff6c09e5b7ccd066ff5baDaniel Jasper void parse(int Precedence = 0) { 787c01897c3f6139304ed25e3f673bef77be209617fDaniel Jasper // Conditional expressions need to be parsed separately for proper nesting. 788c01897c3f6139304ed25e3f673bef77be209617fDaniel Jasper if (Precedence == prec::Conditional + 1) { 789c01897c3f6139304ed25e3f673bef77be209617fDaniel Jasper parseConditionalExpr(); 790c01897c3f6139304ed25e3f673bef77be209617fDaniel Jasper return; 791c01897c3f6139304ed25e3f673bef77be209617fDaniel Jasper } 79229f123b2fa0435bb1962f0d9e9a2e660f35fbb2fDaniel Jasper if (Precedence > prec::PointerToMember || Current == NULL) 79329f123b2fa0435bb1962f0d9e9a2e660f35fbb2fDaniel Jasper return; 79429f123b2fa0435bb1962f0d9e9a2e660f35fbb2fDaniel Jasper 795b398701e4cbb9a55d90a60e3f4f4bc577446d098Manuel Klimek FormatToken *Start = Current; 79629f123b2fa0435bb1962f0d9e9a2e660f35fbb2fDaniel Jasper bool OperatorFound = false; 79729f123b2fa0435bb1962f0d9e9a2e660f35fbb2fDaniel Jasper 798237d4c1c785be13656bff6c09e5b7ccd066ff5baDaniel Jasper while (Current) { 79929f123b2fa0435bb1962f0d9e9a2e660f35fbb2fDaniel Jasper // Consume operators with higher precedence. 800bf71ba2988b34c45af968f0965e28ac952e4a15fDaniel Jasper parse(Precedence + 1); 80129f123b2fa0435bb1962f0d9e9a2e660f35fbb2fDaniel Jasper 802237d4c1c785be13656bff6c09e5b7ccd066ff5baDaniel Jasper int CurrentPrecedence = 0; 803237d4c1c785be13656bff6c09e5b7ccd066ff5baDaniel Jasper if (Current) { 804237d4c1c785be13656bff6c09e5b7ccd066ff5baDaniel Jasper if (Current->Type == TT_ConditionalExpr) 805b8b4295b4ee161bfb76ff7b0ec1007bfd959553bDaniel Jasper CurrentPrecedence = 1 + (int)prec::Conditional; 806bf71ba2988b34c45af968f0965e28ac952e4a15fDaniel Jasper else if (Current->is(tok::semi) || Current->Type == TT_InlineASMColon) 807237d4c1c785be13656bff6c09e5b7ccd066ff5baDaniel Jasper CurrentPrecedence = 1; 808237d4c1c785be13656bff6c09e5b7ccd066ff5baDaniel Jasper else if (Current->Type == TT_BinaryOperator || Current->is(tok::comma)) 809b8b4295b4ee161bfb76ff7b0ec1007bfd959553bDaniel Jasper CurrentPrecedence = 1 + (int)Current->getPrecedence(); 810237d4c1c785be13656bff6c09e5b7ccd066ff5baDaniel Jasper } 811237d4c1c785be13656bff6c09e5b7ccd066ff5baDaniel Jasper 81229f123b2fa0435bb1962f0d9e9a2e660f35fbb2fDaniel Jasper // At the end of the line or when an operator with higher precedence is 81329f123b2fa0435bb1962f0d9e9a2e660f35fbb2fDaniel Jasper // found, insert fake parenthesis and return. 814ac3223e45e7d17c65b143439313800eef4fdf71dDaniel Jasper if (Current == NULL || Current->closesScope() || 815237d4c1c785be13656bff6c09e5b7ccd066ff5baDaniel Jasper (CurrentPrecedence != 0 && CurrentPrecedence < Precedence)) { 816c01897c3f6139304ed25e3f673bef77be209617fDaniel Jasper if (OperatorFound) 817c01897c3f6139304ed25e3f673bef77be209617fDaniel Jasper addFakeParenthesis(Start, prec::Level(Precedence - 1)); 81829f123b2fa0435bb1962f0d9e9a2e660f35fbb2fDaniel Jasper return; 81929f123b2fa0435bb1962f0d9e9a2e660f35fbb2fDaniel Jasper } 82029f123b2fa0435bb1962f0d9e9a2e660f35fbb2fDaniel Jasper 82129f123b2fa0435bb1962f0d9e9a2e660f35fbb2fDaniel Jasper // Consume scopes: (), [], <> and {} 822ac3223e45e7d17c65b143439313800eef4fdf71dDaniel Jasper if (Current->opensScope()) { 823ac3223e45e7d17c65b143439313800eef4fdf71dDaniel Jasper while (Current && !Current->closesScope()) { 82429f123b2fa0435bb1962f0d9e9a2e660f35fbb2fDaniel Jasper next(); 82529f123b2fa0435bb1962f0d9e9a2e660f35fbb2fDaniel Jasper parse(); 82629f123b2fa0435bb1962f0d9e9a2e660f35fbb2fDaniel Jasper } 82729f123b2fa0435bb1962f0d9e9a2e660f35fbb2fDaniel Jasper next(); 82829f123b2fa0435bb1962f0d9e9a2e660f35fbb2fDaniel Jasper } else { 82929f123b2fa0435bb1962f0d9e9a2e660f35fbb2fDaniel Jasper // Operator found. 830237d4c1c785be13656bff6c09e5b7ccd066ff5baDaniel Jasper if (CurrentPrecedence == Precedence) 83129f123b2fa0435bb1962f0d9e9a2e660f35fbb2fDaniel Jasper OperatorFound = true; 83229f123b2fa0435bb1962f0d9e9a2e660f35fbb2fDaniel Jasper 83329f123b2fa0435bb1962f0d9e9a2e660f35fbb2fDaniel Jasper next(); 83429f123b2fa0435bb1962f0d9e9a2e660f35fbb2fDaniel Jasper } 83529f123b2fa0435bb1962f0d9e9a2e660f35fbb2fDaniel Jasper } 83629f123b2fa0435bb1962f0d9e9a2e660f35fbb2fDaniel Jasper } 83729f123b2fa0435bb1962f0d9e9a2e660f35fbb2fDaniel Jasper 83829f123b2fa0435bb1962f0d9e9a2e660f35fbb2fDaniel Jasperprivate: 839c01897c3f6139304ed25e3f673bef77be209617fDaniel Jasper void addFakeParenthesis(FormatToken *Start, prec::Level Precedence) { 840c01897c3f6139304ed25e3f673bef77be209617fDaniel Jasper Start->FakeLParens.push_back(Precedence); 841c01897c3f6139304ed25e3f673bef77be209617fDaniel Jasper if (Current) 842c01897c3f6139304ed25e3f673bef77be209617fDaniel Jasper ++Current->Previous->FakeRParens; 843c01897c3f6139304ed25e3f673bef77be209617fDaniel Jasper } 844c01897c3f6139304ed25e3f673bef77be209617fDaniel Jasper 845c01897c3f6139304ed25e3f673bef77be209617fDaniel Jasper void parseConditionalExpr() { 846c01897c3f6139304ed25e3f673bef77be209617fDaniel Jasper FormatToken *Start = Current; 847c01897c3f6139304ed25e3f673bef77be209617fDaniel Jasper parse(prec::LogicalOr + 1); 848c01897c3f6139304ed25e3f673bef77be209617fDaniel Jasper if (!Current || !Current->is(tok::question)) 849c01897c3f6139304ed25e3f673bef77be209617fDaniel Jasper return; 850c01897c3f6139304ed25e3f673bef77be209617fDaniel Jasper next(); 851c01897c3f6139304ed25e3f673bef77be209617fDaniel Jasper parse(prec::LogicalOr + 1); 852c01897c3f6139304ed25e3f673bef77be209617fDaniel Jasper if (!Current || Current->Type != TT_ConditionalExpr) 853c01897c3f6139304ed25e3f673bef77be209617fDaniel Jasper return; 854c01897c3f6139304ed25e3f673bef77be209617fDaniel Jasper next(); 855c01897c3f6139304ed25e3f673bef77be209617fDaniel Jasper parseConditionalExpr(); 856c01897c3f6139304ed25e3f673bef77be209617fDaniel Jasper addFakeParenthesis(Start, prec::Conditional); 857c01897c3f6139304ed25e3f673bef77be209617fDaniel Jasper } 858c01897c3f6139304ed25e3f673bef77be209617fDaniel Jasper 85929f123b2fa0435bb1962f0d9e9a2e660f35fbb2fDaniel Jasper void next() { 860d71b15badeecdc049440103ef044f9cdf5e1359cAlexander Kornienko if (Current) 861d71b15badeecdc049440103ef044f9cdf5e1359cAlexander Kornienko Current = Current->Next; 862d71b15badeecdc049440103ef044f9cdf5e1359cAlexander Kornienko while (Current && Current->isTrailingComment()) 863b398701e4cbb9a55d90a60e3f4f4bc577446d098Manuel Klimek Current = Current->Next; 86429f123b2fa0435bb1962f0d9e9a2e660f35fbb2fDaniel Jasper } 86529f123b2fa0435bb1962f0d9e9a2e660f35fbb2fDaniel Jasper 866b398701e4cbb9a55d90a60e3f4f4bc577446d098Manuel Klimek FormatToken *Current; 86729f123b2fa0435bb1962f0d9e9a2e660f35fbb2fDaniel Jasper}; 86829f123b2fa0435bb1962f0d9e9a2e660f35fbb2fDaniel Jasper 8698ff690ab478b33e0d830a6203de12d191d94f8ffDaniel Jaspervoid TokenAnnotator::annotate(AnnotatedLine &Line) { 87000895106f9ed602af67984ec4d225a0cdc8c12afAlexander Kornienko AnnotatingParser Parser(Line, Ident_in); 87132d28ee6061930b2a9c170cbaaeea028c88a7b89Daniel Jasper Line.Type = Parser.parseLine(); 87232d28ee6061930b2a9c170cbaaeea028c88a7b89Daniel Jasper if (Line.Type == LT_Invalid) 87332d28ee6061930b2a9c170cbaaeea028c88a7b89Daniel Jasper return; 87432d28ee6061930b2a9c170cbaaeea028c88a7b89Daniel Jasper 87529f123b2fa0435bb1962f0d9e9a2e660f35fbb2fDaniel Jasper ExpressionParser ExprParser(Line); 87629f123b2fa0435bb1962f0d9e9a2e660f35fbb2fDaniel Jasper ExprParser.parse(); 87729f123b2fa0435bb1962f0d9e9a2e660f35fbb2fDaniel Jasper 878b398701e4cbb9a55d90a60e3f4f4bc577446d098Manuel Klimek if (Line.First->Type == TT_ObjCMethodSpecifier) 87932d28ee6061930b2a9c170cbaaeea028c88a7b89Daniel Jasper Line.Type = LT_ObjCMethodDecl; 880b398701e4cbb9a55d90a60e3f4f4bc577446d098Manuel Klimek else if (Line.First->Type == TT_ObjCDecl) 88132d28ee6061930b2a9c170cbaaeea028c88a7b89Daniel Jasper Line.Type = LT_ObjCDecl; 882b398701e4cbb9a55d90a60e3f4f4bc577446d098Manuel Klimek else if (Line.First->Type == TT_ObjCProperty) 88332d28ee6061930b2a9c170cbaaeea028c88a7b89Daniel Jasper Line.Type = LT_ObjCProperty; 88432d28ee6061930b2a9c170cbaaeea028c88a7b89Daniel Jasper 885b398701e4cbb9a55d90a60e3f4f4bc577446d098Manuel Klimek Line.First->SpacesRequiredBefore = 1; 886b398701e4cbb9a55d90a60e3f4f4bc577446d098Manuel Klimek Line.First->CanBreakBefore = Line.First->MustBreakBefore; 88732d28ee6061930b2a9c170cbaaeea028c88a7b89Daniel Jasper} 88832d28ee6061930b2a9c170cbaaeea028c88a7b89Daniel Jasper 8898ff690ab478b33e0d830a6203de12d191d94f8ffDaniel Jaspervoid TokenAnnotator::calculateFormattingInformation(AnnotatedLine &Line) { 89000895106f9ed602af67984ec4d225a0cdc8c12afAlexander Kornienko Line.First->TotalLength = Line.First->CodePointCount; 891b398701e4cbb9a55d90a60e3f4f4bc577446d098Manuel Klimek if (!Line.First->Next) 8928ff690ab478b33e0d830a6203de12d191d94f8ffDaniel Jasper return; 893b398701e4cbb9a55d90a60e3f4f4bc577446d098Manuel Klimek FormatToken *Current = Line.First->Next; 8948ff690ab478b33e0d830a6203de12d191d94f8ffDaniel Jasper while (Current != NULL) { 895729a743b317d877df3978e88a4a247d2edbf2090Daniel Jasper if (Current->Type == TT_LineComment) 896729a743b317d877df3978e88a4a247d2edbf2090Daniel Jasper Current->SpacesRequiredBefore = Style.SpacesBeforeTrailingComments; 897729a743b317d877df3978e88a4a247d2edbf2090Daniel Jasper else 898729a743b317d877df3978e88a4a247d2edbf2090Daniel Jasper Current->SpacesRequiredBefore = 899729a743b317d877df3978e88a4a247d2edbf2090Daniel Jasper spaceRequiredBefore(Line, *Current) ? 1 : 0; 9008ff690ab478b33e0d830a6203de12d191d94f8ffDaniel Jasper 901b398701e4cbb9a55d90a60e3f4f4bc577446d098Manuel Klimek if (Current->MustBreakBefore) { 902e3f1197cad5cda243093e8b39caa43f13111de89Alexander Kornienko } else if (Current->is(tok::comment)) { 903b398701e4cbb9a55d90a60e3f4f4bc577446d098Manuel Klimek Current->MustBreakBefore = Current->NewlinesBefore > 0; 904b398701e4cbb9a55d90a60e3f4f4bc577446d098Manuel Klimek } else if (Current->Previous->isTrailingComment() || 9058ff690ab478b33e0d830a6203de12d191d94f8ffDaniel Jasper (Current->is(tok::string_literal) && 906b398701e4cbb9a55d90a60e3f4f4bc577446d098Manuel Klimek Current->Previous->is(tok::string_literal))) { 9078ff690ab478b33e0d830a6203de12d191d94f8ffDaniel Jasper Current->MustBreakBefore = true; 908b398701e4cbb9a55d90a60e3f4f4bc577446d098Manuel Klimek } else if (Current->is(tok::lessless) && Current->Next && 909b398701e4cbb9a55d90a60e3f4f4bc577446d098Manuel Klimek Current->Previous->is(tok::string_literal) && 910b398701e4cbb9a55d90a60e3f4f4bc577446d098Manuel Klimek Current->Next->is(tok::string_literal)) { 9118ff690ab478b33e0d830a6203de12d191d94f8ffDaniel Jasper Current->MustBreakBefore = true; 912b398701e4cbb9a55d90a60e3f4f4bc577446d098Manuel Klimek } else if (Current->Previous->ClosesTemplateDeclaration && 913bbc8776493fc0176d925a5b528e61ee400895047Daniel Jasper Style.AlwaysBreakTemplateDeclarations) { 914bbc8776493fc0176d925a5b528e61ee400895047Daniel Jasper Current->MustBreakBefore = true; 9158ff690ab478b33e0d830a6203de12d191d94f8ffDaniel Jasper } else { 9168ff690ab478b33e0d830a6203de12d191d94f8ffDaniel Jasper Current->MustBreakBefore = false; 9178ff690ab478b33e0d830a6203de12d191d94f8ffDaniel Jasper } 9188ff690ab478b33e0d830a6203de12d191d94f8ffDaniel Jasper Current->CanBreakBefore = 9198ff690ab478b33e0d830a6203de12d191d94f8ffDaniel Jasper Current->MustBreakBefore || canBreakBefore(Line, *Current); 9208ff690ab478b33e0d830a6203de12d191d94f8ffDaniel Jasper if (Current->MustBreakBefore) 921b398701e4cbb9a55d90a60e3f4f4bc577446d098Manuel Klimek Current->TotalLength = Current->Previous->TotalLength + Style.ColumnLimit; 9228ff690ab478b33e0d830a6203de12d191d94f8ffDaniel Jasper else 9238ff690ab478b33e0d830a6203de12d191d94f8ffDaniel Jasper Current->TotalLength = 92400895106f9ed602af67984ec4d225a0cdc8c12afAlexander Kornienko Current->Previous->TotalLength + Current->CodePointCount + 925729a743b317d877df3978e88a4a247d2edbf2090Daniel Jasper Current->SpacesRequiredBefore; 9268ff690ab478b33e0d830a6203de12d191d94f8ffDaniel Jasper // FIXME: Only calculate this if CanBreakBefore is true once static 9278ff690ab478b33e0d830a6203de12d191d94f8ffDaniel Jasper // initializers etc. are sorted out. 9288ff690ab478b33e0d830a6203de12d191d94f8ffDaniel Jasper // FIXME: Move magic numbers to a better place. 9298ff690ab478b33e0d830a6203de12d191d94f8ffDaniel Jasper Current->SplitPenalty = 9308ff690ab478b33e0d830a6203de12d191d94f8ffDaniel Jasper 20 * Current->BindingStrength + splitPenalty(Line, *Current); 9318ff690ab478b33e0d830a6203de12d191d94f8ffDaniel Jasper 932b398701e4cbb9a55d90a60e3f4f4bc577446d098Manuel Klimek Current = Current->Next; 93332d28ee6061930b2a9c170cbaaeea028c88a7b89Daniel Jasper } 934bf71ba2988b34c45af968f0965e28ac952e4a15fDaniel Jasper 935e573c3f7fc40e813559ab4ff1e7eec4f66f1a50fManuel Klimek calculateUnbreakableTailLengths(Line); 936bf71ba2988b34c45af968f0965e28ac952e4a15fDaniel Jasper DEBUG({ 937bf71ba2988b34c45af968f0965e28ac952e4a15fDaniel Jasper printDebugInfo(Line); 938bf71ba2988b34c45af968f0965e28ac952e4a15fDaniel Jasper }); 93932d28ee6061930b2a9c170cbaaeea028c88a7b89Daniel Jasper} 94032d28ee6061930b2a9c170cbaaeea028c88a7b89Daniel Jasper 941e573c3f7fc40e813559ab4ff1e7eec4f66f1a50fManuel Klimekvoid TokenAnnotator::calculateUnbreakableTailLengths(AnnotatedLine &Line) { 942e573c3f7fc40e813559ab4ff1e7eec4f66f1a50fManuel Klimek unsigned UnbreakableTailLength = 0; 943b398701e4cbb9a55d90a60e3f4f4bc577446d098Manuel Klimek FormatToken *Current = Line.Last; 944e573c3f7fc40e813559ab4ff1e7eec4f66f1a50fManuel Klimek while (Current != NULL) { 945e573c3f7fc40e813559ab4ff1e7eec4f66f1a50fManuel Klimek Current->UnbreakableTailLength = UnbreakableTailLength; 946e573c3f7fc40e813559ab4ff1e7eec4f66f1a50fManuel Klimek if (Current->CanBreakBefore || 947e573c3f7fc40e813559ab4ff1e7eec4f66f1a50fManuel Klimek Current->isOneOf(tok::comment, tok::string_literal)) { 948e573c3f7fc40e813559ab4ff1e7eec4f66f1a50fManuel Klimek UnbreakableTailLength = 0; 949e573c3f7fc40e813559ab4ff1e7eec4f66f1a50fManuel Klimek } else { 950e573c3f7fc40e813559ab4ff1e7eec4f66f1a50fManuel Klimek UnbreakableTailLength += 95100895106f9ed602af67984ec4d225a0cdc8c12afAlexander Kornienko Current->CodePointCount + Current->SpacesRequiredBefore; 952e573c3f7fc40e813559ab4ff1e7eec4f66f1a50fManuel Klimek } 953b398701e4cbb9a55d90a60e3f4f4bc577446d098Manuel Klimek Current = Current->Previous; 954e573c3f7fc40e813559ab4ff1e7eec4f66f1a50fManuel Klimek } 955e573c3f7fc40e813559ab4ff1e7eec4f66f1a50fManuel Klimek} 956e573c3f7fc40e813559ab4ff1e7eec4f66f1a50fManuel Klimek 9578ff690ab478b33e0d830a6203de12d191d94f8ffDaniel Jasperunsigned TokenAnnotator::splitPenalty(const AnnotatedLine &Line, 958b398701e4cbb9a55d90a60e3f4f4bc577446d098Manuel Klimek const FormatToken &Tok) { 959b398701e4cbb9a55d90a60e3f4f4bc577446d098Manuel Klimek const FormatToken &Left = *Tok.Previous; 960b398701e4cbb9a55d90a60e3f4f4bc577446d098Manuel Klimek const FormatToken &Right = Tok; 96132d28ee6061930b2a9c170cbaaeea028c88a7b89Daniel Jasper 9625ad390d27f8f7cb6628bc2c32beba4d25bc2718bDaniel Jasper if (Left.is(tok::semi)) 9635ad390d27f8f7cb6628bc2c32beba4d25bc2718bDaniel Jasper return 0; 9645ad390d27f8f7cb6628bc2c32beba4d25bc2718bDaniel Jasper if (Left.is(tok::comma)) 9655ad390d27f8f7cb6628bc2c32beba4d25bc2718bDaniel Jasper return 1; 9665ad390d27f8f7cb6628bc2c32beba4d25bc2718bDaniel Jasper 9673c08a818a6ac9115fe8880af9bbf5a0a87bdffaaDaniel Jasper if (Right.Type == TT_StartOfName) { 968b398701e4cbb9a55d90a60e3f4f4bc577446d098Manuel Klimek if (Line.First->is(tok::kw_for) && Right.PartOfMultiVariableDeclStmt) 9693c08a818a6ac9115fe8880af9bbf5a0a87bdffaaDaniel Jasper return 3; 9703c08a818a6ac9115fe8880af9bbf5a0a87bdffaaDaniel Jasper else if (Line.MightBeFunctionDecl && Right.BindingStrength == 1) 9713c08a818a6ac9115fe8880af9bbf5a0a87bdffaaDaniel Jasper // FIXME: Clean up hack of using BindingStrength to find top-level names. 9723c08a818a6ac9115fe8880af9bbf5a0a87bdffaaDaniel Jasper return Style.PenaltyReturnTypeOnItsOwnLine; 9733c08a818a6ac9115fe8880af9bbf5a0a87bdffaaDaniel Jasper else 9741407bee187d7b964d5293ac8bf4f7a490c78cec6Daniel Jasper return 200; 9753c08a818a6ac9115fe8880af9bbf5a0a87bdffaaDaniel Jasper } 97632d28ee6061930b2a9c170cbaaeea028c88a7b89Daniel Jasper if (Left.is(tok::equal) && Right.is(tok::l_brace)) 97732d28ee6061930b2a9c170cbaaeea028c88a7b89Daniel Jasper return 150; 97832d28ee6061930b2a9c170cbaaeea028c88a7b89Daniel Jasper if (Left.is(tok::coloncolon)) 97932d28ee6061930b2a9c170cbaaeea028c88a7b89Daniel Jasper return 500; 9806b119d63f5036344acd4e00a6ff2b3c72f26966fDaniel Jasper if (Left.isOneOf(tok::kw_class, tok::kw_struct)) 9816b119d63f5036344acd4e00a6ff2b3c72f26966fDaniel Jasper return 5000; 98232d28ee6061930b2a9c170cbaaeea028c88a7b89Daniel Jasper 9836cabab48dacc1317821f8f078ed2d4c603b67affDaniel Jasper if (Left.Type == TT_RangeBasedForLoopColon || 9846cabab48dacc1317821f8f078ed2d4c603b67affDaniel Jasper Left.Type == TT_InheritanceColon) 98584a1a63b034744b68a27ec171dca5b1b7cf303f0Daniel Jasper return 2; 98632d28ee6061930b2a9c170cbaaeea028c88a7b89Daniel Jasper 9875ad390d27f8f7cb6628bc2c32beba4d25bc2718bDaniel Jasper if (Right.isOneOf(tok::arrow, tok::period) && 9885ad390d27f8f7cb6628bc2c32beba4d25bc2718bDaniel Jasper Right.Type != TT_DesignatedInitializerPeriod) { 989515f65df40624a767bc8763a0b6b678146b8e3c9Daniel Jasper if (Line.Type == LT_BuilderTypeCall) 9906a365aaa057a8c445d25344c0433726c752b3e7dDaniel Jasper return prec::PointerToMember; 991e74de28ec3692986f3467a7f160a0e293277fa7eAlexander Kornienko if (Left.isOneOf(tok::r_paren, tok::r_square) && Left.MatchingParen && 992e74de28ec3692986f3467a7f160a0e293277fa7eAlexander Kornienko Left.MatchingParen->ParameterCount > 0) 993518ee34467c0722e253a58efea20456e96aa5802Daniel Jasper return 20; // Should be smaller than breaking at a nested comma. 99432d28ee6061930b2a9c170cbaaeea028c88a7b89Daniel Jasper return 150; 99532d28ee6061930b2a9c170cbaaeea028c88a7b89Daniel Jasper } 99632d28ee6061930b2a9c170cbaaeea028c88a7b89Daniel Jasper 9975ad72bb8eb8e5cc4c061ccd28632295213d319dbDaniel Jasper // Breaking before a trailing 'const' is bad. 9985ad72bb8eb8e5cc4c061ccd28632295213d319dbDaniel Jasper if (Left.is(tok::r_paren) && Right.is(tok::kw_const)) 9995ad72bb8eb8e5cc4c061ccd28632295213d319dbDaniel Jasper return 150; 10005ad72bb8eb8e5cc4c061ccd28632295213d319dbDaniel Jasper 100132d28ee6061930b2a9c170cbaaeea028c88a7b89Daniel Jasper // In for-loops, prefer breaking at ',' and ';'. 1002b398701e4cbb9a55d90a60e3f4f4bc577446d098Manuel Klimek if (Line.First->is(tok::kw_for) && Left.is(tok::equal)) 10037d81281fc39f6d40d86be6600adba13c05b4a639Daniel Jasper return 4; 100432d28ee6061930b2a9c170cbaaeea028c88a7b89Daniel Jasper 100532d28ee6061930b2a9c170cbaaeea028c88a7b89Daniel Jasper // In Objective-C method expressions, prefer breaking before "param:" over 100632d28ee6061930b2a9c170cbaaeea028c88a7b89Daniel Jasper // breaking after it. 100763d7cedca8616921c1908c88c2f23fcd67bbab99Daniel Jasper if (Right.Type == TT_ObjCSelectorName) 100832d28ee6061930b2a9c170cbaaeea028c88a7b89Daniel Jasper return 0; 100963d7cedca8616921c1908c88c2f23fcd67bbab99Daniel Jasper if (Left.is(tok::colon) && Left.Type == TT_ObjCMethodExpr) 101032d28ee6061930b2a9c170cbaaeea028c88a7b89Daniel Jasper return 20; 101132d28ee6061930b2a9c170cbaaeea028c88a7b89Daniel Jasper 10121407bee187d7b964d5293ac8bf4f7a490c78cec6Daniel Jasper if (Left.is(tok::l_paren) && Line.MightBeFunctionDecl) 10131407bee187d7b964d5293ac8bf4f7a490c78cec6Daniel Jasper return 100; 1014ac3223e45e7d17c65b143439313800eef4fdf71dDaniel Jasper if (Left.opensScope()) 101564f092865c01c72ecb9e380432e241f3af55c249Daniel Jasper return Left.ParameterCount > 1 ? prec::Comma : 20; 101632d28ee6061930b2a9c170cbaaeea028c88a7b89Daniel Jasper 10174e8a7b4a95962f63a938c0d21c9aca0a51b78345Daniel Jasper if (Right.is(tok::lessless)) { 10184e8a7b4a95962f63a938c0d21c9aca0a51b78345Daniel Jasper if (Left.is(tok::string_literal)) { 101900895106f9ed602af67984ec4d225a0cdc8c12afAlexander Kornienko StringRef Content = Left.TokenText; 1020bfa1edd8247b80e951a570ff2486fe5fa9898c41Daniel Jasper Content = Content.drop_back(1).drop_front(1).trim(); 1021bfa1edd8247b80e951a570ff2486fe5fa9898c41Daniel Jasper if (Content.size() > 1 && 1022bfa1edd8247b80e951a570ff2486fe5fa9898c41Daniel Jasper (Content.back() == ':' || Content.back() == '=')) 10236e6efa7ec880559b7dd3096423b2345c0ff21c4cDaniel Jasper return 20; 10244e8a7b4a95962f63a938c0d21c9aca0a51b78345Daniel Jasper } 10250178673f541685cf5067814dfeee2644078e39a9Daniel Jasper return prec::Shift; 10264e8a7b4a95962f63a938c0d21c9aca0a51b78345Daniel Jasper } 102732d28ee6061930b2a9c170cbaaeea028c88a7b89Daniel Jasper if (Left.Type == TT_ConditionalExpr) 1028518ee34467c0722e253a58efea20456e96aa5802Daniel Jasper return prec::Conditional; 1029b398701e4cbb9a55d90a60e3f4f4bc577446d098Manuel Klimek prec::Level Level = Left.getPrecedence(); 103032d28ee6061930b2a9c170cbaaeea028c88a7b89Daniel Jasper 103132d28ee6061930b2a9c170cbaaeea028c88a7b89Daniel Jasper if (Level != prec::Unknown) 103232d28ee6061930b2a9c170cbaaeea028c88a7b89Daniel Jasper return Level; 1033248497199bc56e86d1c089beb9529f3b3d77abb1Daniel Jasper 103432d28ee6061930b2a9c170cbaaeea028c88a7b89Daniel Jasper return 3; 103532d28ee6061930b2a9c170cbaaeea028c88a7b89Daniel Jasper} 103632d28ee6061930b2a9c170cbaaeea028c88a7b89Daniel Jasper 10378ff690ab478b33e0d830a6203de12d191d94f8ffDaniel Jasperbool TokenAnnotator::spaceRequiredBetween(const AnnotatedLine &Line, 1038b398701e4cbb9a55d90a60e3f4f4bc577446d098Manuel Klimek const FormatToken &Left, 1039b398701e4cbb9a55d90a60e3f4f4bc577446d098Manuel Klimek const FormatToken &Right) { 104032d28ee6061930b2a9c170cbaaeea028c88a7b89Daniel Jasper if (Right.is(tok::hashhash)) 104132d28ee6061930b2a9c170cbaaeea028c88a7b89Daniel Jasper return Left.is(tok::hash); 1042e74de28ec3692986f3467a7f160a0e293277fa7eAlexander Kornienko if (Left.isOneOf(tok::hashhash, tok::hash)) 104332d28ee6061930b2a9c170cbaaeea028c88a7b89Daniel Jasper return Right.is(tok::hash); 1044e74de28ec3692986f3467a7f160a0e293277fa7eAlexander Kornienko if (Right.isOneOf(tok::r_paren, tok::semi, tok::comma)) 104532d28ee6061930b2a9c170cbaaeea028c88a7b89Daniel Jasper return false; 104632d28ee6061930b2a9c170cbaaeea028c88a7b89Daniel Jasper if (Right.is(tok::less) && 104732d28ee6061930b2a9c170cbaaeea028c88a7b89Daniel Jasper (Left.is(tok::kw_template) || 104832d28ee6061930b2a9c170cbaaeea028c88a7b89Daniel Jasper (Line.Type == LT_ObjCDecl && Style.ObjCSpaceBeforeProtocolList))) 104932d28ee6061930b2a9c170cbaaeea028c88a7b89Daniel Jasper return true; 105032d28ee6061930b2a9c170cbaaeea028c88a7b89Daniel Jasper if (Left.is(tok::arrow) || Right.is(tok::arrow)) 105132d28ee6061930b2a9c170cbaaeea028c88a7b89Daniel Jasper return false; 1052e74de28ec3692986f3467a7f160a0e293277fa7eAlexander Kornienko if (Left.isOneOf(tok::exclaim, tok::tilde)) 105332d28ee6061930b2a9c170cbaaeea028c88a7b89Daniel Jasper return false; 105432d28ee6061930b2a9c170cbaaeea028c88a7b89Daniel Jasper if (Left.is(tok::at) && 1055e74de28ec3692986f3467a7f160a0e293277fa7eAlexander Kornienko Right.isOneOf(tok::identifier, tok::string_literal, tok::char_constant, 1056e74de28ec3692986f3467a7f160a0e293277fa7eAlexander Kornienko tok::numeric_constant, tok::l_paren, tok::l_brace, 1057e74de28ec3692986f3467a7f160a0e293277fa7eAlexander Kornienko tok::kw_true, tok::kw_false)) 105832d28ee6061930b2a9c170cbaaeea028c88a7b89Daniel Jasper return false; 105932d28ee6061930b2a9c170cbaaeea028c88a7b89Daniel Jasper if (Left.is(tok::coloncolon)) 106032d28ee6061930b2a9c170cbaaeea028c88a7b89Daniel Jasper return false; 106132d28ee6061930b2a9c170cbaaeea028c88a7b89Daniel Jasper if (Right.is(tok::coloncolon)) 1062e74de28ec3692986f3467a7f160a0e293277fa7eAlexander Kornienko return !Left.isOneOf(tok::identifier, tok::greater, tok::l_paren); 1063e74de28ec3692986f3467a7f160a0e293277fa7eAlexander Kornienko if (Left.is(tok::less) || Right.isOneOf(tok::greater, tok::less)) 106432d28ee6061930b2a9c170cbaaeea028c88a7b89Daniel Jasper return false; 10653fd9ccdd9f8d259bcf518e7056cfd419d992e984Alexander Kornienko if (Right.Type == TT_PointerOrReference) 1066b398701e4cbb9a55d90a60e3f4f4bc577446d098Manuel Klimek return Left.Tok.isLiteral() || 10673fd9ccdd9f8d259bcf518e7056cfd419d992e984Alexander Kornienko ((Left.Type != TT_PointerOrReference) && Left.isNot(tok::l_paren) && 10683fd9ccdd9f8d259bcf518e7056cfd419d992e984Alexander Kornienko !Style.PointerBindsToType); 10693ff4a2fea4aa6e5182b7799ccb4352e56961a212Daniel Jasper if (Right.Type == TT_FunctionTypeLParen && Left.isNot(tok::l_paren) && 1070395228fdc343df39c2507e414dc1406a185c6d37Daniel Jasper (Left.Type != TT_PointerOrReference || Style.PointerBindsToType)) 1071395228fdc343df39c2507e414dc1406a185c6d37Daniel Jasper return true; 10723fd9ccdd9f8d259bcf518e7056cfd419d992e984Alexander Kornienko if (Left.Type == TT_PointerOrReference) 1073b398701e4cbb9a55d90a60e3f4f4bc577446d098Manuel Klimek return Right.Tok.isLiteral() || 10749322aaee900b872c98f8fc10b38a231cb1e9b57aDaniel Jasper ((Right.Type != TT_PointerOrReference) && 107581d2d38d2d774a2550fa0d2efffa707e7a53b39cDaniel Jasper Right.isNot(tok::l_paren) && Style.PointerBindsToType && 1076b398701e4cbb9a55d90a60e3f4f4bc577446d098Manuel Klimek Left.Previous && 1077b398701e4cbb9a55d90a60e3f4f4bc577446d098Manuel Klimek !Left.Previous->isOneOf(tok::l_paren, tok::coloncolon)); 107832d28ee6061930b2a9c170cbaaeea028c88a7b89Daniel Jasper if (Right.is(tok::star) && Left.is(tok::l_paren)) 107932d28ee6061930b2a9c170cbaaeea028c88a7b89Daniel Jasper return false; 1080051860ee770bf83c3e66ab893be3642bb8bc2680Nico Weber if (Left.is(tok::l_square)) 1081051860ee770bf83c3e66ab893be3642bb8bc2680Nico Weber return Left.Type == TT_ObjCArrayLiteral && Right.isNot(tok::r_square); 1082051860ee770bf83c3e66ab893be3642bb8bc2680Nico Weber if (Right.is(tok::r_square)) 1083051860ee770bf83c3e66ab893be3642bb8bc2680Nico Weber return Right.Type == TT_ObjCArrayLiteral; 108432d28ee6061930b2a9c170cbaaeea028c88a7b89Daniel Jasper if (Right.is(tok::l_square) && Right.Type != TT_ObjCMethodExpr) 108532d28ee6061930b2a9c170cbaaeea028c88a7b89Daniel Jasper return false; 108632d28ee6061930b2a9c170cbaaeea028c88a7b89Daniel Jasper if (Left.is(tok::colon)) 108732d28ee6061930b2a9c170cbaaeea028c88a7b89Daniel Jasper return Left.Type != TT_ObjCMethodExpr; 108832d28ee6061930b2a9c170cbaaeea028c88a7b89Daniel Jasper if (Right.is(tok::colon)) 108932d28ee6061930b2a9c170cbaaeea028c88a7b89Daniel Jasper return Right.Type != TT_ObjCMethodExpr; 109032d28ee6061930b2a9c170cbaaeea028c88a7b89Daniel Jasper if (Left.is(tok::l_paren)) 109132d28ee6061930b2a9c170cbaaeea028c88a7b89Daniel Jasper return false; 109232d28ee6061930b2a9c170cbaaeea028c88a7b89Daniel Jasper if (Right.is(tok::l_paren)) { 1093e74de28ec3692986f3467a7f160a0e293277fa7eAlexander Kornienko return Line.Type == LT_ObjCDecl || 1094e74de28ec3692986f3467a7f160a0e293277fa7eAlexander Kornienko Left.isOneOf(tok::kw_if, tok::kw_for, tok::kw_while, tok::kw_switch, 1095e74de28ec3692986f3467a7f160a0e293277fa7eAlexander Kornienko tok::kw_return, tok::kw_catch, tok::kw_new, 1096454cb70f9470dc37d0c0646012877d64fb1e73d7Daniel Jasper tok::kw_delete, tok::semi); 109732d28ee6061930b2a9c170cbaaeea028c88a7b89Daniel Jasper } 1098b398701e4cbb9a55d90a60e3f4f4bc577446d098Manuel Klimek if (Left.is(tok::at) && Right.Tok.getObjCKeywordID() != tok::objc_not_keyword) 109932d28ee6061930b2a9c170cbaaeea028c88a7b89Daniel Jasper return false; 110032d28ee6061930b2a9c170cbaaeea028c88a7b89Daniel Jasper if (Left.is(tok::l_brace) && Right.is(tok::r_brace)) 11012424eefa6936ec2dc35188e19c99e2f85428b52eDaniel Jasper return false; // No spaces in "{}". 11022424eefa6936ec2dc35188e19c99e2f85428b52eDaniel Jasper if (Left.is(tok::l_brace) || Right.is(tok::r_brace)) 11032424eefa6936ec2dc35188e19c99e2f85428b52eDaniel Jasper return Style.SpacesInBracedLists; 11041bee0738b67b784f08d5e2f8351920260c9cfb1dDaniel Jasper if (Right.Type == TT_UnaryOperator) 11051bee0738b67b784f08d5e2f8351920260c9cfb1dDaniel Jasper return !Left.isOneOf(tok::l_paren, tok::l_square, tok::at) && 11061bee0738b67b784f08d5e2f8351920260c9cfb1dDaniel Jasper (Left.isNot(tok::colon) || Left.Type != TT_ObjCMethodExpr); 1107ce93356e2719d2992763ea747b65beada99f4c9bDaniel Jasper if (Left.isOneOf(tok::identifier, tok::greater, tok::r_square) && 1108ce93356e2719d2992763ea747b65beada99f4c9bDaniel Jasper Right.is(tok::l_brace) && Right.getNextNoneComment()) 110932d28ee6061930b2a9c170cbaaeea028c88a7b89Daniel Jasper return false; 1110f022018698ced7e538b338d4f7befef9f81051eaDaniel Jasper if (Right.is(tok::ellipsis)) 1111f022018698ced7e538b338d4f7befef9f81051eaDaniel Jasper return false; 11125ad390d27f8f7cb6628bc2c32beba4d25bc2718bDaniel Jasper if (Left.is(tok::period) || Right.is(tok::period)) 11135ad390d27f8f7cb6628bc2c32beba4d25bc2718bDaniel Jasper return false; 111432d28ee6061930b2a9c170cbaaeea028c88a7b89Daniel Jasper return true; 111532d28ee6061930b2a9c170cbaaeea028c88a7b89Daniel Jasper} 111632d28ee6061930b2a9c170cbaaeea028c88a7b89Daniel Jasper 11178ff690ab478b33e0d830a6203de12d191d94f8ffDaniel Jasperbool TokenAnnotator::spaceRequiredBefore(const AnnotatedLine &Line, 1118b398701e4cbb9a55d90a60e3f4f4bc577446d098Manuel Klimek const FormatToken &Tok) { 1119b398701e4cbb9a55d90a60e3f4f4bc577446d098Manuel Klimek if (Tok.Tok.getIdentifierInfo() && Tok.Previous->Tok.getIdentifierInfo()) 11202b4c924f85e156d66c01b3f16c850892c47dcc7aDaniel Jasper return true; // Never ever merge two identifiers. 112132d28ee6061930b2a9c170cbaaeea028c88a7b89Daniel Jasper if (Line.Type == LT_ObjCMethodDecl) { 1122b398701e4cbb9a55d90a60e3f4f4bc577446d098Manuel Klimek if (Tok.Previous->Type == TT_ObjCMethodSpecifier) 112332d28ee6061930b2a9c170cbaaeea028c88a7b89Daniel Jasper return true; 1124b398701e4cbb9a55d90a60e3f4f4bc577446d098Manuel Klimek if (Tok.Previous->is(tok::r_paren) && Tok.is(tok::identifier)) 112532d28ee6061930b2a9c170cbaaeea028c88a7b89Daniel Jasper // Don't space between ')' and <id> 112632d28ee6061930b2a9c170cbaaeea028c88a7b89Daniel Jasper return false; 112732d28ee6061930b2a9c170cbaaeea028c88a7b89Daniel Jasper } 112832d28ee6061930b2a9c170cbaaeea028c88a7b89Daniel Jasper if (Line.Type == LT_ObjCProperty && 1129b398701e4cbb9a55d90a60e3f4f4bc577446d098Manuel Klimek (Tok.is(tok::equal) || Tok.Previous->is(tok::equal))) 113032d28ee6061930b2a9c170cbaaeea028c88a7b89Daniel Jasper return false; 113132d28ee6061930b2a9c170cbaaeea028c88a7b89Daniel Jasper 1132b398701e4cbb9a55d90a60e3f4f4bc577446d098Manuel Klimek if (Tok.Previous->is(tok::comma)) 113332d28ee6061930b2a9c170cbaaeea028c88a7b89Daniel Jasper return true; 11349c3c7b3130bc72b3f50703c11b85152b1264fc90Daniel Jasper if (Tok.is(tok::comma)) 11359c3c7b3130bc72b3f50703c11b85152b1264fc90Daniel Jasper return false; 113632d28ee6061930b2a9c170cbaaeea028c88a7b89Daniel Jasper if (Tok.Type == TT_CtorInitializerColon || Tok.Type == TT_ObjCBlockLParen) 113732d28ee6061930b2a9c170cbaaeea028c88a7b89Daniel Jasper return true; 1138b398701e4cbb9a55d90a60e3f4f4bc577446d098Manuel Klimek if (Tok.Previous->Tok.is(tok::kw_operator)) 11392b4c924f85e156d66c01b3f16c850892c47dcc7aDaniel Jasper return false; 11402b4c924f85e156d66c01b3f16c850892c47dcc7aDaniel Jasper if (Tok.Type == TT_OverloadedOperatorLParen) 114132d28ee6061930b2a9c170cbaaeea028c88a7b89Daniel Jasper return false; 114232d28ee6061930b2a9c170cbaaeea028c88a7b89Daniel Jasper if (Tok.is(tok::colon)) 1143b398701e4cbb9a55d90a60e3f4f4bc577446d098Manuel Klimek return !Line.First->isOneOf(tok::kw_case, tok::kw_default) && 1144ac3223e45e7d17c65b143439313800eef4fdf71dDaniel Jasper Tok.getNextNoneComment() != NULL && Tok.Type != TT_ObjCMethodExpr; 1145b398701e4cbb9a55d90a60e3f4f4bc577446d098Manuel Klimek if (Tok.Previous->Type == TT_UnaryOperator || 1146b398701e4cbb9a55d90a60e3f4f4bc577446d098Manuel Klimek Tok.Previous->Type == TT_CastRParen) 114732d28ee6061930b2a9c170cbaaeea028c88a7b89Daniel Jasper return false; 1148b398701e4cbb9a55d90a60e3f4f4bc577446d098Manuel Klimek if (Tok.Previous->is(tok::greater) && Tok.is(tok::greater)) { 114929f123b2fa0435bb1962f0d9e9a2e660f35fbb2fDaniel Jasper return Tok.Type == TT_TemplateCloser && 1150b398701e4cbb9a55d90a60e3f4f4bc577446d098Manuel Klimek Tok.Previous->Type == TT_TemplateCloser && 115129f123b2fa0435bb1962f0d9e9a2e660f35fbb2fDaniel Jasper Style.Standard != FormatStyle::LS_Cpp11; 115232d28ee6061930b2a9c170cbaaeea028c88a7b89Daniel Jasper } 115354a38bd5cf243310290f34b43fc940a498a00f90Alexander Kornienko if (Tok.isOneOf(tok::arrowstar, tok::periodstar) || 1154b398701e4cbb9a55d90a60e3f4f4bc577446d098Manuel Klimek Tok.Previous->isOneOf(tok::arrowstar, tok::periodstar)) 11559c3c7b3130bc72b3f50703c11b85152b1264fc90Daniel Jasper return false; 1156b398701e4cbb9a55d90a60e3f4f4bc577446d098Manuel Klimek if (Tok.Type == TT_BinaryOperator || Tok.Previous->Type == TT_BinaryOperator) 115732d28ee6061930b2a9c170cbaaeea028c88a7b89Daniel Jasper return true; 1158b398701e4cbb9a55d90a60e3f4f4bc577446d098Manuel Klimek if (Tok.Previous->Type == TT_TemplateCloser && Tok.is(tok::l_paren)) 115932d28ee6061930b2a9c170cbaaeea028c88a7b89Daniel Jasper return false; 1160b398701e4cbb9a55d90a60e3f4f4bc577446d098Manuel Klimek if (Tok.is(tok::less) && Line.First->is(tok::hash)) 116132d28ee6061930b2a9c170cbaaeea028c88a7b89Daniel Jasper return true; 116232d28ee6061930b2a9c170cbaaeea028c88a7b89Daniel Jasper if (Tok.Type == TT_TrailingUnaryOperator) 116332d28ee6061930b2a9c170cbaaeea028c88a7b89Daniel Jasper return false; 1164b398701e4cbb9a55d90a60e3f4f4bc577446d098Manuel Klimek return spaceRequiredBetween(Line, *Tok.Previous, Tok); 116532d28ee6061930b2a9c170cbaaeea028c88a7b89Daniel Jasper} 116632d28ee6061930b2a9c170cbaaeea028c88a7b89Daniel Jasper 11678ff690ab478b33e0d830a6203de12d191d94f8ffDaniel Jasperbool TokenAnnotator::canBreakBefore(const AnnotatedLine &Line, 1168b398701e4cbb9a55d90a60e3f4f4bc577446d098Manuel Klimek const FormatToken &Right) { 1169b398701e4cbb9a55d90a60e3f4f4bc577446d098Manuel Klimek const FormatToken &Left = *Right.Previous; 1170a03ab10f0e4d888139b3b694dd55d176982f72a4Daniel Jasper if (Right.Type == TT_StartOfName) 117132d28ee6061930b2a9c170cbaaeea028c88a7b89Daniel Jasper return true; 1172f2ff8126e9e9df368f31b1f968d8fc80f99809b3Nico Weber if (Right.is(tok::colon) && 1173f2ff8126e9e9df368f31b1f968d8fc80f99809b3Nico Weber (Right.Type == TT_ObjCDictLiteral || Right.Type == TT_ObjCMethodExpr)) 117432d28ee6061930b2a9c170cbaaeea028c88a7b89Daniel Jasper return false; 1175f2ff8126e9e9df368f31b1f968d8fc80f99809b3Nico Weber if (Left.is(tok::colon) && 1176f2ff8126e9e9df368f31b1f968d8fc80f99809b3Nico Weber (Left.Type == TT_ObjCDictLiteral || Left.Type == TT_ObjCMethodExpr)) 117732d28ee6061930b2a9c170cbaaeea028c88a7b89Daniel Jasper return true; 117863d7cedca8616921c1908c88c2f23fcd67bbab99Daniel Jasper if (Right.Type == TT_ObjCSelectorName) 117932d28ee6061930b2a9c170cbaaeea028c88a7b89Daniel Jasper return true; 118032d28ee6061930b2a9c170cbaaeea028c88a7b89Daniel Jasper if (Left.ClosesTemplateDeclaration) 118132d28ee6061930b2a9c170cbaaeea028c88a7b89Daniel Jasper return true; 118232d28ee6061930b2a9c170cbaaeea028c88a7b89Daniel Jasper if (Right.Type == TT_ConditionalExpr || Right.is(tok::question)) 118332d28ee6061930b2a9c170cbaaeea028c88a7b89Daniel Jasper return true; 11846cabab48dacc1317821f8f078ed2d4c603b67affDaniel Jasper if (Right.Type == TT_RangeBasedForLoopColon || 118527b91cc046f580fbe825f15b3e27f1b6407ee3e3Daniel Jasper Right.Type == TT_OverloadedOperatorLParen) 11866cabab48dacc1317821f8f078ed2d4c603b67affDaniel Jasper return false; 1187c194c95036b7bf1281a6f2ed683f7c85ee5d2c20Daniel Jasper if (Left.Type == TT_RangeBasedForLoopColon) 118832d28ee6061930b2a9c170cbaaeea028c88a7b89Daniel Jasper return true; 11897d81281fc39f6d40d86be6600adba13c05b4a639Daniel Jasper if (Right.Type == TT_RangeBasedForLoopColon) 11907d81281fc39f6d40d86be6600adba13c05b4a639Daniel Jasper return false; 119132d28ee6061930b2a9c170cbaaeea028c88a7b89Daniel Jasper if (Left.Type == TT_PointerOrReference || Left.Type == TT_TemplateCloser || 119232d28ee6061930b2a9c170cbaaeea028c88a7b89Daniel Jasper Left.Type == TT_UnaryOperator || Left.Type == TT_ConditionalExpr || 1193e74de28ec3692986f3467a7f160a0e293277fa7eAlexander Kornienko Left.isOneOf(tok::question, tok::kw_operator)) 119432d28ee6061930b2a9c170cbaaeea028c88a7b89Daniel Jasper return false; 119532d28ee6061930b2a9c170cbaaeea028c88a7b89Daniel Jasper if (Left.is(tok::equal) && Line.Type == LT_VirtualFunctionDecl) 119632d28ee6061930b2a9c170cbaaeea028c88a7b89Daniel Jasper return false; 1197b398701e4cbb9a55d90a60e3f4f4bc577446d098Manuel Klimek if (Left.is(tok::l_paren) && Right.is(tok::l_paren) && Left.Previous && 1198b398701e4cbb9a55d90a60e3f4f4bc577446d098Manuel Klimek Left.Previous->is(tok::kw___attribute)) 11998ed4100410ea055a03be5ec4a92a947a0ee664cdDaniel Jasper return false; 120032d28ee6061930b2a9c170cbaaeea028c88a7b89Daniel Jasper 120165d2c3829494d254039683c73f95843c29c661b4Daniel Jasper if (Right.isTrailingComment()) 120232d28ee6061930b2a9c170cbaaeea028c88a7b89Daniel Jasper // We rely on MustBreakBefore being set correctly here as we should not 120332d28ee6061930b2a9c170cbaaeea028c88a7b89Daniel Jasper // change the "binding" behavior of a comment. 120432d28ee6061930b2a9c170cbaaeea028c88a7b89Daniel Jasper return false; 120532d28ee6061930b2a9c170cbaaeea028c88a7b89Daniel Jasper 12065ad72bb8eb8e5cc4c061ccd28632295213d319dbDaniel Jasper // We only break before r_brace if there was a corresponding break before 12075ad72bb8eb8e5cc4c061ccd28632295213d319dbDaniel Jasper // the l_brace, which is tracked by BreakBeforeClosingBrace. 12085ad72bb8eb8e5cc4c061ccd28632295213d319dbDaniel Jasper if (Right.isOneOf(tok::r_brace, tok::r_paren, tok::greater)) 12095ad72bb8eb8e5cc4c061ccd28632295213d319dbDaniel Jasper return false; 12105ad72bb8eb8e5cc4c061ccd28632295213d319dbDaniel Jasper 121132d28ee6061930b2a9c170cbaaeea028c88a7b89Daniel Jasper // Allow breaking after a trailing 'const', e.g. after a method declaration, 121232d28ee6061930b2a9c170cbaaeea028c88a7b89Daniel Jasper // unless it is follow by ';', '{' or '='. 1213b398701e4cbb9a55d90a60e3f4f4bc577446d098Manuel Klimek if (Left.is(tok::kw_const) && Left.Previous != NULL && 1214b398701e4cbb9a55d90a60e3f4f4bc577446d098Manuel Klimek Left.Previous->is(tok::r_paren)) 1215e74de28ec3692986f3467a7f160a0e293277fa7eAlexander Kornienko return !Right.isOneOf(tok::l_brace, tok::semi, tok::equal); 121632d28ee6061930b2a9c170cbaaeea028c88a7b89Daniel Jasper 12178ef19a22956defa392df46c79e2d499ab7b16647Daniel Jasper if (Right.is(tok::kw___attribute)) 12188ef19a22956defa392df46c79e2d499ab7b16647Daniel Jasper return true; 12198ef19a22956defa392df46c79e2d499ab7b16647Daniel Jasper 12203a204418482c9ae70ad482e781132c54306c3aa6Daniel Jasper if (Left.is(tok::identifier) && Right.is(tok::string_literal)) 12213a204418482c9ae70ad482e781132c54306c3aa6Daniel Jasper return true; 1222ac3223e45e7d17c65b143439313800eef4fdf71dDaniel Jasper return (Left.isBinaryOperator() && Left.isNot(tok::lessless)) || 12236b119d63f5036344acd4e00a6ff2b3c72f26966fDaniel Jasper Left.isOneOf(tok::comma, tok::coloncolon, tok::semi, tok::l_brace, 12246b119d63f5036344acd4e00a6ff2b3c72f26966fDaniel Jasper tok::kw_class, tok::kw_struct) || 1225e74de28ec3692986f3467a7f160a0e293277fa7eAlexander Kornienko Right.isOneOf(tok::lessless, tok::arrow, tok::period, tok::colon) || 122632d28ee6061930b2a9c170cbaaeea028c88a7b89Daniel Jasper (Left.is(tok::r_paren) && Left.Type != TT_CastRParen && 1227e033e87cbe77341777100093f8066167888d4440Daniel Jasper Right.isOneOf(tok::identifier, tok::kw_const, tok::kw___attribute)) || 122832d28ee6061930b2a9c170cbaaeea028c88a7b89Daniel Jasper (Left.is(tok::l_paren) && !Right.is(tok::r_paren)) || 122932d28ee6061930b2a9c170cbaaeea028c88a7b89Daniel Jasper (Left.is(tok::l_square) && !Right.is(tok::r_square)); 123032d28ee6061930b2a9c170cbaaeea028c88a7b89Daniel Jasper} 123132d28ee6061930b2a9c170cbaaeea028c88a7b89Daniel Jasper 1232bf71ba2988b34c45af968f0965e28ac952e4a15fDaniel Jaspervoid TokenAnnotator::printDebugInfo(const AnnotatedLine &Line) { 1233bf71ba2988b34c45af968f0965e28ac952e4a15fDaniel Jasper llvm::errs() << "AnnotatedTokens:\n"; 1234b398701e4cbb9a55d90a60e3f4f4bc577446d098Manuel Klimek const FormatToken *Tok = Line.First; 1235bf71ba2988b34c45af968f0965e28ac952e4a15fDaniel Jasper while (Tok) { 1236b398701e4cbb9a55d90a60e3f4f4bc577446d098Manuel Klimek llvm::errs() << " M=" << Tok->MustBreakBefore 1237b398701e4cbb9a55d90a60e3f4f4bc577446d098Manuel Klimek << " C=" << Tok->CanBreakBefore << " T=" << Tok->Type << " S=" 1238b398701e4cbb9a55d90a60e3f4f4bc577446d098Manuel Klimek << Tok->SpacesRequiredBefore << " P=" << Tok->SplitPenalty 1239b398701e4cbb9a55d90a60e3f4f4bc577446d098Manuel Klimek << " Name=" << Tok->Tok.getName() << " FakeLParens="; 1240bf71ba2988b34c45af968f0965e28ac952e4a15fDaniel Jasper for (unsigned i = 0, e = Tok->FakeLParens.size(); i != e; ++i) 1241bf71ba2988b34c45af968f0965e28ac952e4a15fDaniel Jasper llvm::errs() << Tok->FakeLParens[i] << "/"; 1242bf71ba2988b34c45af968f0965e28ac952e4a15fDaniel Jasper llvm::errs() << " FakeRParens=" << Tok->FakeRParens << "\n"; 1243b398701e4cbb9a55d90a60e3f4f4bc577446d098Manuel Klimek Tok = Tok->Next; 1244bf71ba2988b34c45af968f0965e28ac952e4a15fDaniel Jasper } 1245bf71ba2988b34c45af968f0965e28ac952e4a15fDaniel Jasper llvm::errs() << "----\n"; 1246bf71ba2988b34c45af968f0965e28ac952e4a15fDaniel Jasper} 1247bf71ba2988b34c45af968f0965e28ac952e4a15fDaniel Jasper 124832d28ee6061930b2a9c170cbaaeea028c88a7b89Daniel Jasper} // namespace format 124932d28ee6061930b2a9c170cbaaeea028c88a7b89Daniel Jasper} // namespace clang 1250