TokenAnnotator.cpp revision b3c887dcb70220eced72935725cd94d7e8325912
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 2314e66498781b7d81639bdc48716e09700552ac21Craig Toppernamespace { 2414e66498781b7d81639bdc48716e09700552ac21Craig Topper 2532d28ee6061930b2a9c170cbaaeea028c88a7b89Daniel Jasper/// \brief A parser that gathers additional information about tokens. 2632d28ee6061930b2a9c170cbaaeea028c88a7b89Daniel Jasper/// 273fd9ccdd9f8d259bcf518e7056cfd419d992e984Alexander Kornienko/// The \c TokenAnnotator tries to match parenthesis and square brakets and 2832d28ee6061930b2a9c170cbaaeea028c88a7b89Daniel Jasper/// store a parenthesis levels. It also tries to resolve matching "<" and ">" 2932d28ee6061930b2a9c170cbaaeea028c88a7b89Daniel Jasper/// into template parameter lists. 3032d28ee6061930b2a9c170cbaaeea028c88a7b89Daniel Jasperclass AnnotatingParser { 3132d28ee6061930b2a9c170cbaaeea028c88a7b89Daniel Jasperpublic: 32d4a03dbb0bdbd4908375306283cb61813bd8f454Daniel Jasper AnnotatingParser(const FormatStyle &Style, AnnotatedLine &Line, 33d4a03dbb0bdbd4908375306283cb61813bd8f454Daniel Jasper IdentifierInfo &Ident_in) 34d4a03dbb0bdbd4908375306283cb61813bd8f454Daniel Jasper : Style(Style), Line(Line), CurrentToken(Line.First), 35d4a03dbb0bdbd4908375306283cb61813bd8f454Daniel Jasper KeywordVirtualFound(false), NameFound(false), AutoFound(false), 36d4a03dbb0bdbd4908375306283cb61813bd8f454Daniel Jasper Ident_in(Ident_in) { 372726877196b41b922f10f794801b313980e1a8adNico Weber Contexts.push_back(Context(tok::unknown, 1, /*IsExpression=*/false)); 3832d28ee6061930b2a9c170cbaaeea028c88a7b89Daniel Jasper } 3932d28ee6061930b2a9c170cbaaeea028c88a7b89Daniel Jasper 4095e8e468af284afb11ec1f994d0b5076fc7423d9Nico Weberprivate: 4132d28ee6061930b2a9c170cbaaeea028c88a7b89Daniel Jasper bool parseAngle() { 4232d28ee6061930b2a9c170cbaaeea028c88a7b89Daniel Jasper if (CurrentToken == NULL) 4332d28ee6061930b2a9c170cbaaeea028c88a7b89Daniel Jasper return false; 44923ebef120a37122ce50722a85cbe42c0c2dab53Daniel Jasper ScopedContextCreator ContextCreator(*this, tok::less, 10); 45b398701e4cbb9a55d90a60e3f4f4bc577446d098Manuel Klimek FormatToken *Left = CurrentToken->Previous; 464e778091b0ff42895644ca8bef30f1a91ba6b32bDaniel Jasper Contexts.back().IsExpression = false; 4732d28ee6061930b2a9c170cbaaeea028c88a7b89Daniel Jasper while (CurrentToken != NULL) { 4832d28ee6061930b2a9c170cbaaeea028c88a7b89Daniel Jasper if (CurrentToken->is(tok::greater)) { 4932d28ee6061930b2a9c170cbaaeea028c88a7b89Daniel Jasper Left->MatchingParen = CurrentToken; 5032d28ee6061930b2a9c170cbaaeea028c88a7b89Daniel Jasper CurrentToken->MatchingParen = Left; 5132d28ee6061930b2a9c170cbaaeea028c88a7b89Daniel Jasper CurrentToken->Type = TT_TemplateCloser; 5232d28ee6061930b2a9c170cbaaeea028c88a7b89Daniel Jasper next(); 5332d28ee6061930b2a9c170cbaaeea028c88a7b89Daniel Jasper return true; 5432d28ee6061930b2a9c170cbaaeea028c88a7b89Daniel Jasper } 55e74de28ec3692986f3467a7f160a0e293277fa7eAlexander Kornienko if (CurrentToken->isOneOf(tok::r_paren, tok::r_square, tok::r_brace, 565d823e3a00a3e21a0823288e6dee26a93758332bDaniel Jasper tok::question, tok::colon)) 575d823e3a00a3e21a0823288e6dee26a93758332bDaniel Jasper return false; 580348be0c78781c5ddb8c271976812705410c731aDaniel Jasper // If a && or || is found and interpreted as a binary operator, this set 5915f33f03e742fb6567e4789996fa0391a8e18068Daniel Jasper // of angles is likely part of something like "a < b && c > d". If the 600348be0c78781c5ddb8c271976812705410c731aDaniel Jasper // angles are inside an expression, the ||/&& might also be a binary 610348be0c78781c5ddb8c271976812705410c731aDaniel Jasper // operator that was misinterpreted because we are parsing template 620348be0c78781c5ddb8c271976812705410c731aDaniel Jasper // parameters. 630348be0c78781c5ddb8c271976812705410c731aDaniel Jasper // FIXME: This is getting out of hand, write a decent parser. 64b398701e4cbb9a55d90a60e3f4f4bc577446d098Manuel Klimek if (CurrentToken->Previous->isOneOf(tok::pipepipe, tok::ampamp) && 650348be0c78781c5ddb8c271976812705410c731aDaniel Jasper (CurrentToken->Previous->Type == TT_BinaryOperator || 660348be0c78781c5ddb8c271976812705410c731aDaniel Jasper Contexts[Contexts.size() - 2].IsExpression) && 67b398701e4cbb9a55d90a60e3f4f4bc577446d098Manuel Klimek Line.First->isNot(tok::kw_template)) 6832d28ee6061930b2a9c170cbaaeea028c88a7b89Daniel Jasper return false; 699fc56f2636137fcde8acb38865555ed6c7b84dfdDaniel Jasper updateParameterCount(Left, CurrentToken); 7032d28ee6061930b2a9c170cbaaeea028c88a7b89Daniel Jasper if (!consumeToken()) 7132d28ee6061930b2a9c170cbaaeea028c88a7b89Daniel Jasper return false; 7232d28ee6061930b2a9c170cbaaeea028c88a7b89Daniel Jasper } 7332d28ee6061930b2a9c170cbaaeea028c88a7b89Daniel Jasper return false; 7432d28ee6061930b2a9c170cbaaeea028c88a7b89Daniel Jasper } 7532d28ee6061930b2a9c170cbaaeea028c88a7b89Daniel Jasper 7632d28ee6061930b2a9c170cbaaeea028c88a7b89Daniel Jasper bool parseParens(bool LookForDecls = false) { 7732d28ee6061930b2a9c170cbaaeea028c88a7b89Daniel Jasper if (CurrentToken == NULL) 7832d28ee6061930b2a9c170cbaaeea028c88a7b89Daniel Jasper return false; 79923ebef120a37122ce50722a85cbe42c0c2dab53Daniel Jasper ScopedContextCreator ContextCreator(*this, tok::l_paren, 1); 804e778091b0ff42895644ca8bef30f1a91ba6b32bDaniel Jasper 814e778091b0ff42895644ca8bef30f1a91ba6b32bDaniel Jasper // FIXME: This is a bit of a hack. Do better. 824e778091b0ff42895644ca8bef30f1a91ba6b32bDaniel Jasper Contexts.back().ColonIsForRangeExpr = 834e778091b0ff42895644ca8bef30f1a91ba6b32bDaniel Jasper Contexts.size() == 2 && Contexts[0].ColonIsForRangeExpr; 844e778091b0ff42895644ca8bef30f1a91ba6b32bDaniel Jasper 8532d28ee6061930b2a9c170cbaaeea028c88a7b89Daniel Jasper bool StartsObjCMethodExpr = false; 86b398701e4cbb9a55d90a60e3f4f4bc577446d098Manuel Klimek FormatToken *Left = CurrentToken->Previous; 8732d28ee6061930b2a9c170cbaaeea028c88a7b89Daniel Jasper if (CurrentToken->is(tok::caret)) { 8832d28ee6061930b2a9c170cbaaeea028c88a7b89Daniel Jasper // ^( starts a block. 8932d28ee6061930b2a9c170cbaaeea028c88a7b89Daniel Jasper Left->Type = TT_ObjCBlockLParen; 90b398701e4cbb9a55d90a60e3f4f4bc577446d098Manuel Klimek } else if (FormatToken *MaybeSel = Left->Previous) { 9132d28ee6061930b2a9c170cbaaeea028c88a7b89Daniel Jasper // @selector( starts a selector. 92b398701e4cbb9a55d90a60e3f4f4bc577446d098Manuel Klimek if (MaybeSel->isObjCAtKeyword(tok::objc_selector) && MaybeSel->Previous && 93b398701e4cbb9a55d90a60e3f4f4bc577446d098Manuel Klimek MaybeSel->Previous->is(tok::at)) { 9432d28ee6061930b2a9c170cbaaeea028c88a7b89Daniel Jasper StartsObjCMethodExpr = true; 9532d28ee6061930b2a9c170cbaaeea028c88a7b89Daniel Jasper } 9632d28ee6061930b2a9c170cbaaeea028c88a7b89Daniel Jasper } 9732d28ee6061930b2a9c170cbaaeea028c88a7b89Daniel Jasper 98b644dd68d3d7261ceb8823595290439dc65530b1Daniel Jasper if (Left->Previous && Left->Previous->isOneOf(tok::kw_static_assert, 99b644dd68d3d7261ceb8823595290439dc65530b1Daniel Jasper tok::kw_if, tok::kw_while)) 100b7000ca629da16164f0073f5a7f9459ddf5ba281Daniel Jasper Contexts.back().IsExpression = true; 101b7000ca629da16164f0073f5a7f9459ddf5ba281Daniel Jasper 1024e778091b0ff42895644ca8bef30f1a91ba6b32bDaniel Jasper if (StartsObjCMethodExpr) { 1034e778091b0ff42895644ca8bef30f1a91ba6b32bDaniel Jasper Contexts.back().ColonIsObjCMethodExpr = true; 1044e778091b0ff42895644ca8bef30f1a91ba6b32bDaniel Jasper Left->Type = TT_ObjCMethodExpr; 1054e778091b0ff42895644ca8bef30f1a91ba6b32bDaniel Jasper } 10632d28ee6061930b2a9c170cbaaeea028c88a7b89Daniel Jasper 107431f5918281dd1f178bf838b7a47ab1d11c52961Daniel Jasper bool MightBeFunctionType = CurrentToken->is(tok::star); 108c7bd68f9edcbca95e882d0ab18d09371f0bdb82cDaniel Jasper bool HasMultipleLines = false; 109c7bd68f9edcbca95e882d0ab18d09371f0bdb82cDaniel Jasper bool HasMultipleParametersOnALine = false; 11032d28ee6061930b2a9c170cbaaeea028c88a7b89Daniel Jasper while (CurrentToken != NULL) { 11132d28ee6061930b2a9c170cbaaeea028c88a7b89Daniel Jasper // LookForDecls is set when "if (" has been seen. Check for 11232d28ee6061930b2a9c170cbaaeea028c88a7b89Daniel Jasper // 'identifier' '*' 'identifier' followed by not '=' -- this 11332d28ee6061930b2a9c170cbaaeea028c88a7b89Daniel Jasper // '*' has to be a binary operator but determineStarAmpUsage() will 11432d28ee6061930b2a9c170cbaaeea028c88a7b89Daniel Jasper // categorize it as an unary operator, so set the right type here. 115b398701e4cbb9a55d90a60e3f4f4bc577446d098Manuel Klimek if (LookForDecls && CurrentToken->Next) { 1160bdc6434fa0fea933b6ab566eff751afdba40a2aAlexander Kornienko FormatToken *Prev = CurrentToken->getPreviousNonComment(); 1172785b9aabcb2c3fd6f7dd8b63d3cd3d4b9bca284Alexander Kornienko if (Prev) { 1180bdc6434fa0fea933b6ab566eff751afdba40a2aAlexander Kornienko FormatToken *PrevPrev = Prev->getPreviousNonComment(); 1192785b9aabcb2c3fd6f7dd8b63d3cd3d4b9bca284Alexander Kornienko FormatToken *Next = CurrentToken->Next; 1202785b9aabcb2c3fd6f7dd8b63d3cd3d4b9bca284Alexander Kornienko if (PrevPrev && PrevPrev->is(tok::identifier) && 1212785b9aabcb2c3fd6f7dd8b63d3cd3d4b9bca284Alexander Kornienko Prev->isOneOf(tok::star, tok::amp, tok::ampamp) && 1222785b9aabcb2c3fd6f7dd8b63d3cd3d4b9bca284Alexander Kornienko CurrentToken->is(tok::identifier) && Next->isNot(tok::equal)) { 1232785b9aabcb2c3fd6f7dd8b63d3cd3d4b9bca284Alexander Kornienko Prev->Type = TT_BinaryOperator; 1242785b9aabcb2c3fd6f7dd8b63d3cd3d4b9bca284Alexander Kornienko LookForDecls = false; 1252785b9aabcb2c3fd6f7dd8b63d3cd3d4b9bca284Alexander Kornienko } 12632d28ee6061930b2a9c170cbaaeea028c88a7b89Daniel Jasper } 12732d28ee6061930b2a9c170cbaaeea028c88a7b89Daniel Jasper } 12832d28ee6061930b2a9c170cbaaeea028c88a7b89Daniel Jasper 12953352600b7370b1d33b9fde1adda207fd9d7dcd1Daniel Jasper if (CurrentToken->Previous->Type == TT_PointerOrReference && 13053352600b7370b1d33b9fde1adda207fd9d7dcd1Daniel Jasper CurrentToken->Previous->Previous->isOneOf(tok::l_paren, 13153352600b7370b1d33b9fde1adda207fd9d7dcd1Daniel Jasper tok::coloncolon)) 13253352600b7370b1d33b9fde1adda207fd9d7dcd1Daniel Jasper MightBeFunctionType = true; 13332d28ee6061930b2a9c170cbaaeea028c88a7b89Daniel Jasper if (CurrentToken->is(tok::r_paren)) { 134b398701e4cbb9a55d90a60e3f4f4bc577446d098Manuel Klimek if (MightBeFunctionType && CurrentToken->Next && 135e7d3bff31e3ef4fea1e2a5a7cd5441b6b0752e3fDaniel Jasper (CurrentToken->Next->is(tok::l_paren) || 136e7d3bff31e3ef4fea1e2a5a7cd5441b6b0752e3fDaniel Jasper (CurrentToken->Next->is(tok::l_square) && 137e7d3bff31e3ef4fea1e2a5a7cd5441b6b0752e3fDaniel Jasper !Contexts.back().IsExpression))) 138431f5918281dd1f178bf838b7a47ab1d11c52961Daniel Jasper Left->Type = TT_FunctionTypeLParen; 13932d28ee6061930b2a9c170cbaaeea028c88a7b89Daniel Jasper Left->MatchingParen = CurrentToken; 14032d28ee6061930b2a9c170cbaaeea028c88a7b89Daniel Jasper CurrentToken->MatchingParen = Left; 14132d28ee6061930b2a9c170cbaaeea028c88a7b89Daniel Jasper 14263d7cedca8616921c1908c88c2f23fcd67bbab99Daniel Jasper if (StartsObjCMethodExpr) { 1434e778091b0ff42895644ca8bef30f1a91ba6b32bDaniel Jasper CurrentToken->Type = TT_ObjCMethodExpr; 1444e778091b0ff42895644ca8bef30f1a91ba6b32bDaniel Jasper if (Contexts.back().FirstObjCSelectorName != NULL) { 1454e778091b0ff42895644ca8bef30f1a91ba6b32bDaniel Jasper Contexts.back().FirstObjCSelectorName->LongestObjCSelectorName = 1464e778091b0ff42895644ca8bef30f1a91ba6b32bDaniel Jasper Contexts.back().LongestObjCSelectorName; 14763d7cedca8616921c1908c88c2f23fcd67bbab99Daniel Jasper } 14863d7cedca8616921c1908c88c2f23fcd67bbab99Daniel Jasper } 14932d28ee6061930b2a9c170cbaaeea028c88a7b89Daniel Jasper 150c7bd68f9edcbca95e882d0ab18d09371f0bdb82cDaniel Jasper if (!HasMultipleLines) 151c7bd68f9edcbca95e882d0ab18d09371f0bdb82cDaniel Jasper Left->PackingKind = PPK_Inconclusive; 152c7bd68f9edcbca95e882d0ab18d09371f0bdb82cDaniel Jasper else if (HasMultipleParametersOnALine) 153c7bd68f9edcbca95e882d0ab18d09371f0bdb82cDaniel Jasper Left->PackingKind = PPK_BinPacked; 154c7bd68f9edcbca95e882d0ab18d09371f0bdb82cDaniel Jasper else 155c7bd68f9edcbca95e882d0ab18d09371f0bdb82cDaniel Jasper Left->PackingKind = PPK_OnePerLine; 156c7bd68f9edcbca95e882d0ab18d09371f0bdb82cDaniel Jasper 15732d28ee6061930b2a9c170cbaaeea028c88a7b89Daniel Jasper next(); 15832d28ee6061930b2a9c170cbaaeea028c88a7b89Daniel Jasper return true; 15932d28ee6061930b2a9c170cbaaeea028c88a7b89Daniel Jasper } 160e74de28ec3692986f3467a7f160a0e293277fa7eAlexander Kornienko if (CurrentToken->isOneOf(tok::r_square, tok::r_brace)) 16132d28ee6061930b2a9c170cbaaeea028c88a7b89Daniel Jasper return false; 1629fc56f2636137fcde8acb38865555ed6c7b84dfdDaniel Jasper updateParameterCount(Left, CurrentToken); 163c7bd68f9edcbca95e882d0ab18d09371f0bdb82cDaniel Jasper if (CurrentToken->is(tok::comma) && CurrentToken->Next && 164c7bd68f9edcbca95e882d0ab18d09371f0bdb82cDaniel Jasper !CurrentToken->Next->HasUnescapedNewline && 165c7bd68f9edcbca95e882d0ab18d09371f0bdb82cDaniel Jasper !CurrentToken->Next->isTrailingComment()) 166c7bd68f9edcbca95e882d0ab18d09371f0bdb82cDaniel Jasper HasMultipleParametersOnALine = true; 16732d28ee6061930b2a9c170cbaaeea028c88a7b89Daniel Jasper if (!consumeToken()) 16832d28ee6061930b2a9c170cbaaeea028c88a7b89Daniel Jasper return false; 169c7bd68f9edcbca95e882d0ab18d09371f0bdb82cDaniel Jasper if (CurrentToken && CurrentToken->HasUnescapedNewline) 170c7bd68f9edcbca95e882d0ab18d09371f0bdb82cDaniel Jasper HasMultipleLines = true; 17132d28ee6061930b2a9c170cbaaeea028c88a7b89Daniel Jasper } 17232d28ee6061930b2a9c170cbaaeea028c88a7b89Daniel Jasper return false; 17332d28ee6061930b2a9c170cbaaeea028c88a7b89Daniel Jasper } 17432d28ee6061930b2a9c170cbaaeea028c88a7b89Daniel Jasper 17532d28ee6061930b2a9c170cbaaeea028c88a7b89Daniel Jasper bool parseSquare() { 17632d28ee6061930b2a9c170cbaaeea028c88a7b89Daniel Jasper if (!CurrentToken) 17732d28ee6061930b2a9c170cbaaeea028c88a7b89Daniel Jasper return false; 17832d28ee6061930b2a9c170cbaaeea028c88a7b89Daniel Jasper 179d71b15badeecdc049440103ef044f9cdf5e1359cAlexander Kornienko // A '[' could be an index subscript (after an identifier or after 180051860ee770bf83c3e66ab893be3642bb8bc2680Nico Weber // ')' or ']'), it could be the start of an Objective-C method 181051860ee770bf83c3e66ab893be3642bb8bc2680Nico Weber // expression, or it could the the start of an Objective-C array literal. 182b398701e4cbb9a55d90a60e3f4f4bc577446d098Manuel Klimek FormatToken *Left = CurrentToken->Previous; 1830bdc6434fa0fea933b6ab566eff751afdba40a2aAlexander Kornienko FormatToken *Parent = Left->getPreviousNonComment(); 18432d28ee6061930b2a9c170cbaaeea028c88a7b89Daniel Jasper bool StartsObjCMethodExpr = 185567dcf95424d69657f75e4bfd028967ca1f9eb8dDaniel Jasper Contexts.back().CanBeExpression && Left->Type != TT_LambdaLSquare && 186e74de28ec3692986f3467a7f160a0e293277fa7eAlexander Kornienko (!Parent || Parent->isOneOf(tok::colon, tok::l_square, tok::l_paren, 187e74de28ec3692986f3467a7f160a0e293277fa7eAlexander Kornienko tok::kw_return, tok::kw_throw) || 188ac3223e45e7d17c65b143439313800eef4fdf71dDaniel Jasper Parent->isUnaryOperator() || Parent->Type == TT_ObjCForIn || 189e74de28ec3692986f3467a7f160a0e293277fa7eAlexander Kornienko Parent->Type == TT_CastRParen || 190b398701e4cbb9a55d90a60e3f4f4bc577446d098Manuel Klimek getBinOpPrecedence(Parent->Tok.getKind(), true, true) > prec::Unknown); 191923ebef120a37122ce50722a85cbe42c0c2dab53Daniel Jasper ScopedContextCreator ContextCreator(*this, tok::l_square, 10); 1926f21a988990ff5872822dcb049bd8fc65ce3d236Daniel Jasper Contexts.back().IsExpression = true; 193051860ee770bf83c3e66ab893be3642bb8bc2680Nico Weber bool StartsObjCArrayLiteral = Parent && Parent->is(tok::at); 19432d28ee6061930b2a9c170cbaaeea028c88a7b89Daniel Jasper 1954e778091b0ff42895644ca8bef30f1a91ba6b32bDaniel Jasper if (StartsObjCMethodExpr) { 1964e778091b0ff42895644ca8bef30f1a91ba6b32bDaniel Jasper Contexts.back().ColonIsObjCMethodExpr = true; 1974e778091b0ff42895644ca8bef30f1a91ba6b32bDaniel Jasper Left->Type = TT_ObjCMethodExpr; 198051860ee770bf83c3e66ab893be3642bb8bc2680Nico Weber } else if (StartsObjCArrayLiteral) { 199051860ee770bf83c3e66ab893be3642bb8bc2680Nico Weber Left->Type = TT_ObjCArrayLiteral; 2004e778091b0ff42895644ca8bef30f1a91ba6b32bDaniel Jasper } 20132d28ee6061930b2a9c170cbaaeea028c88a7b89Daniel Jasper 20232d28ee6061930b2a9c170cbaaeea028c88a7b89Daniel Jasper while (CurrentToken != NULL) { 20332d28ee6061930b2a9c170cbaaeea028c88a7b89Daniel Jasper if (CurrentToken->is(tok::r_square)) { 204ac2c974bc64d3767ad5e39451a874c50b3004b3aDaniel Jasper if (CurrentToken->Next && CurrentToken->Next->is(tok::l_paren) && 205ac2c974bc64d3767ad5e39451a874c50b3004b3aDaniel Jasper Left->Type == TT_ObjCMethodExpr) { 206e8a97985f72b4e11435ff2107c0f11e925fb6d96Nico Weber // An ObjC method call is rarely followed by an open parenthesis. 20732d28ee6061930b2a9c170cbaaeea028c88a7b89Daniel Jasper // FIXME: Do we incorrectly label ":" with this? 20832d28ee6061930b2a9c170cbaaeea028c88a7b89Daniel Jasper StartsObjCMethodExpr = false; 20932d28ee6061930b2a9c170cbaaeea028c88a7b89Daniel Jasper Left->Type = TT_Unknown; 21032d28ee6061930b2a9c170cbaaeea028c88a7b89Daniel Jasper } 2110178673f541685cf5067814dfeee2644078e39a9Daniel Jasper if (StartsObjCMethodExpr) { 2124e778091b0ff42895644ca8bef30f1a91ba6b32bDaniel Jasper CurrentToken->Type = TT_ObjCMethodExpr; 213e8a97985f72b4e11435ff2107c0f11e925fb6d96Nico Weber // determineStarAmpUsage() thinks that '*' '[' is allocating an 214e8a97985f72b4e11435ff2107c0f11e925fb6d96Nico Weber // array of pointers, but if '[' starts a selector then '*' is a 215e8a97985f72b4e11435ff2107c0f11e925fb6d96Nico Weber // binary operator. 2163fd9ccdd9f8d259bcf518e7056cfd419d992e984Alexander Kornienko if (Parent != NULL && Parent->Type == TT_PointerOrReference) 2174ed7f3e003c906d9fdb92a9484feeb8ac6e28e2fNico Weber Parent->Type = TT_BinaryOperator; 218051860ee770bf83c3e66ab893be3642bb8bc2680Nico Weber } else if (StartsObjCArrayLiteral) { 219051860ee770bf83c3e66ab893be3642bb8bc2680Nico Weber CurrentToken->Type = TT_ObjCArrayLiteral; 2200178673f541685cf5067814dfeee2644078e39a9Daniel Jasper } 22132d28ee6061930b2a9c170cbaaeea028c88a7b89Daniel Jasper Left->MatchingParen = CurrentToken; 22232d28ee6061930b2a9c170cbaaeea028c88a7b89Daniel Jasper CurrentToken->MatchingParen = Left; 2234e778091b0ff42895644ca8bef30f1a91ba6b32bDaniel Jasper if (Contexts.back().FirstObjCSelectorName != NULL) 2244e778091b0ff42895644ca8bef30f1a91ba6b32bDaniel Jasper Contexts.back().FirstObjCSelectorName->LongestObjCSelectorName = 2254e778091b0ff42895644ca8bef30f1a91ba6b32bDaniel Jasper Contexts.back().LongestObjCSelectorName; 22632d28ee6061930b2a9c170cbaaeea028c88a7b89Daniel Jasper next(); 22732d28ee6061930b2a9c170cbaaeea028c88a7b89Daniel Jasper return true; 22832d28ee6061930b2a9c170cbaaeea028c88a7b89Daniel Jasper } 229e74de28ec3692986f3467a7f160a0e293277fa7eAlexander Kornienko if (CurrentToken->isOneOf(tok::r_paren, tok::r_brace)) 23032d28ee6061930b2a9c170cbaaeea028c88a7b89Daniel Jasper return false; 2319fc56f2636137fcde8acb38865555ed6c7b84dfdDaniel Jasper updateParameterCount(Left, CurrentToken); 23232d28ee6061930b2a9c170cbaaeea028c88a7b89Daniel Jasper if (!consumeToken()) 23332d28ee6061930b2a9c170cbaaeea028c88a7b89Daniel Jasper return false; 23432d28ee6061930b2a9c170cbaaeea028c88a7b89Daniel Jasper } 23532d28ee6061930b2a9c170cbaaeea028c88a7b89Daniel Jasper return false; 23632d28ee6061930b2a9c170cbaaeea028c88a7b89Daniel Jasper } 23732d28ee6061930b2a9c170cbaaeea028c88a7b89Daniel Jasper 23832d28ee6061930b2a9c170cbaaeea028c88a7b89Daniel Jasper bool parseBrace() { 23953e72cdcc0bc217cab33fa56858a0fe5b94ca453Daniel Jasper if (CurrentToken != NULL) { 24053e72cdcc0bc217cab33fa56858a0fe5b94ca453Daniel Jasper ScopedContextCreator ContextCreator(*this, tok::l_brace, 1); 241b398701e4cbb9a55d90a60e3f4f4bc577446d098Manuel Klimek FormatToken *Left = CurrentToken->Previous; 242f2ff8126e9e9df368f31b1f968d8fc80f99809b3Nico Weber 2430bdc6434fa0fea933b6ab566eff751afdba40a2aAlexander Kornienko FormatToken *Parent = Left->getPreviousNonComment(); 244f2ff8126e9e9df368f31b1f968d8fc80f99809b3Nico Weber bool StartsObjCDictLiteral = Parent && Parent->is(tok::at); 245f2ff8126e9e9df368f31b1f968d8fc80f99809b3Nico Weber if (StartsObjCDictLiteral) { 246f2ff8126e9e9df368f31b1f968d8fc80f99809b3Nico Weber Contexts.back().ColonIsObjCDictLiteral = true; 247f2ff8126e9e9df368f31b1f968d8fc80f99809b3Nico Weber Left->Type = TT_ObjCDictLiteral; 248f2ff8126e9e9df368f31b1f968d8fc80f99809b3Nico Weber } 249f2ff8126e9e9df368f31b1f968d8fc80f99809b3Nico Weber 25053e72cdcc0bc217cab33fa56858a0fe5b94ca453Daniel Jasper while (CurrentToken != NULL) { 25153e72cdcc0bc217cab33fa56858a0fe5b94ca453Daniel Jasper if (CurrentToken->is(tok::r_brace)) { 252f2ff8126e9e9df368f31b1f968d8fc80f99809b3Nico Weber if (StartsObjCDictLiteral) 253f2ff8126e9e9df368f31b1f968d8fc80f99809b3Nico Weber CurrentToken->Type = TT_ObjCDictLiteral; 25453e72cdcc0bc217cab33fa56858a0fe5b94ca453Daniel Jasper Left->MatchingParen = CurrentToken; 25553e72cdcc0bc217cab33fa56858a0fe5b94ca453Daniel Jasper CurrentToken->MatchingParen = Left; 25653e72cdcc0bc217cab33fa56858a0fe5b94ca453Daniel Jasper next(); 25753e72cdcc0bc217cab33fa56858a0fe5b94ca453Daniel Jasper return true; 25853e72cdcc0bc217cab33fa56858a0fe5b94ca453Daniel Jasper } 25953e72cdcc0bc217cab33fa56858a0fe5b94ca453Daniel Jasper if (CurrentToken->isOneOf(tok::r_paren, tok::r_square)) 26053e72cdcc0bc217cab33fa56858a0fe5b94ca453Daniel Jasper return false; 26153e72cdcc0bc217cab33fa56858a0fe5b94ca453Daniel Jasper updateParameterCount(Left, CurrentToken); 26253e72cdcc0bc217cab33fa56858a0fe5b94ca453Daniel Jasper if (!consumeToken()) 26353e72cdcc0bc217cab33fa56858a0fe5b94ca453Daniel Jasper return false; 26432d28ee6061930b2a9c170cbaaeea028c88a7b89Daniel Jasper } 26532d28ee6061930b2a9c170cbaaeea028c88a7b89Daniel Jasper } 26653e72cdcc0bc217cab33fa56858a0fe5b94ca453Daniel Jasper // No closing "}" found, this probably starts a definition. 26753e72cdcc0bc217cab33fa56858a0fe5b94ca453Daniel Jasper Line.StartsDefinition = true; 26832d28ee6061930b2a9c170cbaaeea028c88a7b89Daniel Jasper return true; 26932d28ee6061930b2a9c170cbaaeea028c88a7b89Daniel Jasper } 270c4615b7aaf97e303a4fc675956f7f5572d492885Daniel Jasper 271b398701e4cbb9a55d90a60e3f4f4bc577446d098Manuel Klimek void updateParameterCount(FormatToken *Left, FormatToken *Current) { 272c7bd68f9edcbca95e882d0ab18d09371f0bdb82cDaniel Jasper if (Current->is(tok::comma)) { 2739fc56f2636137fcde8acb38865555ed6c7b84dfdDaniel Jasper ++Left->ParameterCount; 274d4a03dbb0bdbd4908375306283cb61813bd8f454Daniel Jasper if (!Left->Role) 275d4a03dbb0bdbd4908375306283cb61813bd8f454Daniel Jasper Left->Role.reset(new CommaSeparatedList(Style)); 276d4a03dbb0bdbd4908375306283cb61813bd8f454Daniel Jasper Left->Role->CommaFound(Current); 277c7bd68f9edcbca95e882d0ab18d09371f0bdb82cDaniel Jasper } else if (Left->ParameterCount == 0 && Current->isNot(tok::comment)) { 2789fc56f2636137fcde8acb38865555ed6c7b84dfdDaniel Jasper Left->ParameterCount = 1; 279c7bd68f9edcbca95e882d0ab18d09371f0bdb82cDaniel Jasper } 2809fc56f2636137fcde8acb38865555ed6c7b84dfdDaniel Jasper } 28132d28ee6061930b2a9c170cbaaeea028c88a7b89Daniel Jasper 28232d28ee6061930b2a9c170cbaaeea028c88a7b89Daniel Jasper bool parseConditional() { 28332d28ee6061930b2a9c170cbaaeea028c88a7b89Daniel Jasper while (CurrentToken != NULL) { 28432d28ee6061930b2a9c170cbaaeea028c88a7b89Daniel Jasper if (CurrentToken->is(tok::colon)) { 28532d28ee6061930b2a9c170cbaaeea028c88a7b89Daniel Jasper CurrentToken->Type = TT_ConditionalExpr; 28632d28ee6061930b2a9c170cbaaeea028c88a7b89Daniel Jasper next(); 28732d28ee6061930b2a9c170cbaaeea028c88a7b89Daniel Jasper return true; 28832d28ee6061930b2a9c170cbaaeea028c88a7b89Daniel Jasper } 28932d28ee6061930b2a9c170cbaaeea028c88a7b89Daniel Jasper if (!consumeToken()) 29032d28ee6061930b2a9c170cbaaeea028c88a7b89Daniel Jasper return false; 29132d28ee6061930b2a9c170cbaaeea028c88a7b89Daniel Jasper } 29232d28ee6061930b2a9c170cbaaeea028c88a7b89Daniel Jasper return false; 29332d28ee6061930b2a9c170cbaaeea028c88a7b89Daniel Jasper } 29432d28ee6061930b2a9c170cbaaeea028c88a7b89Daniel Jasper 29532d28ee6061930b2a9c170cbaaeea028c88a7b89Daniel Jasper bool parseTemplateDeclaration() { 29632d28ee6061930b2a9c170cbaaeea028c88a7b89Daniel Jasper if (CurrentToken != NULL && CurrentToken->is(tok::less)) { 29732d28ee6061930b2a9c170cbaaeea028c88a7b89Daniel Jasper CurrentToken->Type = TT_TemplateOpener; 29832d28ee6061930b2a9c170cbaaeea028c88a7b89Daniel Jasper next(); 29932d28ee6061930b2a9c170cbaaeea028c88a7b89Daniel Jasper if (!parseAngle()) 30032d28ee6061930b2a9c170cbaaeea028c88a7b89Daniel Jasper return false; 30134511fb79201ba9ed39c97f3a7ea0f157a79436dDaniel Jasper if (CurrentToken != NULL) 302b398701e4cbb9a55d90a60e3f4f4bc577446d098Manuel Klimek CurrentToken->Previous->ClosesTemplateDeclaration = true; 30332d28ee6061930b2a9c170cbaaeea028c88a7b89Daniel Jasper return true; 30432d28ee6061930b2a9c170cbaaeea028c88a7b89Daniel Jasper } 30532d28ee6061930b2a9c170cbaaeea028c88a7b89Daniel Jasper return false; 30632d28ee6061930b2a9c170cbaaeea028c88a7b89Daniel Jasper } 30732d28ee6061930b2a9c170cbaaeea028c88a7b89Daniel Jasper 30832d28ee6061930b2a9c170cbaaeea028c88a7b89Daniel Jasper bool consumeToken() { 309b398701e4cbb9a55d90a60e3f4f4bc577446d098Manuel Klimek FormatToken *Tok = CurrentToken; 31032d28ee6061930b2a9c170cbaaeea028c88a7b89Daniel Jasper next(); 311b398701e4cbb9a55d90a60e3f4f4bc577446d098Manuel Klimek switch (Tok->Tok.getKind()) { 31232d28ee6061930b2a9c170cbaaeea028c88a7b89Daniel Jasper case tok::plus: 31332d28ee6061930b2a9c170cbaaeea028c88a7b89Daniel Jasper case tok::minus: 314b398701e4cbb9a55d90a60e3f4f4bc577446d098Manuel Klimek if (Tok->Previous == NULL && Line.MustBeDeclaration) 31532d28ee6061930b2a9c170cbaaeea028c88a7b89Daniel Jasper Tok->Type = TT_ObjCMethodSpecifier; 31632d28ee6061930b2a9c170cbaaeea028c88a7b89Daniel Jasper break; 31732d28ee6061930b2a9c170cbaaeea028c88a7b89Daniel Jasper case tok::colon: 318b398701e4cbb9a55d90a60e3f4f4bc577446d098Manuel Klimek if (Tok->Previous == NULL) 319cf6d76af806f7e1ba97be7b72b31bc78b919e0f0Daniel Jasper return false; 32032d28ee6061930b2a9c170cbaaeea028c88a7b89Daniel Jasper // Colons from ?: are handled in parseConditional(). 321b398701e4cbb9a55d90a60e3f4f4bc577446d098Manuel Klimek if (Tok->Previous->is(tok::r_paren) && Contexts.size() == 1) { 32232d28ee6061930b2a9c170cbaaeea028c88a7b89Daniel Jasper Tok->Type = TT_CtorInitializerColon; 323f2ff8126e9e9df368f31b1f968d8fc80f99809b3Nico Weber } else if (Contexts.back().ColonIsObjCDictLiteral) { 324f2ff8126e9e9df368f31b1f968d8fc80f99809b3Nico Weber Tok->Type = TT_ObjCDictLiteral; 3254e778091b0ff42895644ca8bef30f1a91ba6b32bDaniel Jasper } else if (Contexts.back().ColonIsObjCMethodExpr || 326b398701e4cbb9a55d90a60e3f4f4bc577446d098Manuel Klimek Line.First->Type == TT_ObjCMethodSpecifier) { 32732d28ee6061930b2a9c170cbaaeea028c88a7b89Daniel Jasper Tok->Type = TT_ObjCMethodExpr; 328b398701e4cbb9a55d90a60e3f4f4bc577446d098Manuel Klimek Tok->Previous->Type = TT_ObjCSelectorName; 32983a7dcdf5fce1bdf74ce985419d77a41a51abfa2Alexander Kornienko if (Tok->Previous->ColumnWidth > 33000895106f9ed602af67984ec4d225a0cdc8c12afAlexander Kornienko Contexts.back().LongestObjCSelectorName) { 33101fe9f9f320dd4342664376f24eb1a0d004d03c8Alexander Kornienko Contexts.back().LongestObjCSelectorName = Tok->Previous->ColumnWidth; 33200895106f9ed602af67984ec4d225a0cdc8c12afAlexander Kornienko } 3334e778091b0ff42895644ca8bef30f1a91ba6b32bDaniel Jasper if (Contexts.back().FirstObjCSelectorName == NULL) 334b398701e4cbb9a55d90a60e3f4f4bc577446d098Manuel Klimek Contexts.back().FirstObjCSelectorName = Tok->Previous; 3354e778091b0ff42895644ca8bef30f1a91ba6b32bDaniel Jasper } else if (Contexts.back().ColonIsForRangeExpr) { 33632d28ee6061930b2a9c170cbaaeea028c88a7b89Daniel Jasper Tok->Type = TT_RangeBasedForLoopColon; 33701fe9f9f320dd4342664376f24eb1a0d004d03c8Alexander Kornienko } else if (CurrentToken != NULL && 33801fe9f9f320dd4342664376f24eb1a0d004d03c8Alexander Kornienko CurrentToken->is(tok::numeric_constant)) { 33901fe9f9f320dd4342664376f24eb1a0d004d03c8Alexander Kornienko Tok->Type = TT_BitFieldColon; 340cea014bd8d280070caeb27e4e6e33e5723b4226fDaniel Jasper } else if (Contexts.size() == 1 && Line.First->isNot(tok::kw_enum)) { 3416cabab48dacc1317821f8f078ed2d4c603b67affDaniel Jasper Tok->Type = TT_InheritanceColon; 342923ebef120a37122ce50722a85cbe42c0c2dab53Daniel Jasper } else if (Contexts.back().ContextKind == tok::l_paren) { 343923ebef120a37122ce50722a85cbe42c0c2dab53Daniel Jasper Tok->Type = TT_InlineASMColon; 34463d7cedca8616921c1908c88c2f23fcd67bbab99Daniel Jasper } 34532d28ee6061930b2a9c170cbaaeea028c88a7b89Daniel Jasper break; 34632d28ee6061930b2a9c170cbaaeea028c88a7b89Daniel Jasper case tok::kw_if: 34732d28ee6061930b2a9c170cbaaeea028c88a7b89Daniel Jasper case tok::kw_while: 34832d28ee6061930b2a9c170cbaaeea028c88a7b89Daniel Jasper if (CurrentToken != NULL && CurrentToken->is(tok::l_paren)) { 34932d28ee6061930b2a9c170cbaaeea028c88a7b89Daniel Jasper next(); 3502726877196b41b922f10f794801b313980e1a8adNico Weber if (!parseParens(/*LookForDecls=*/true)) 35132d28ee6061930b2a9c170cbaaeea028c88a7b89Daniel Jasper return false; 35232d28ee6061930b2a9c170cbaaeea028c88a7b89Daniel Jasper } 35332d28ee6061930b2a9c170cbaaeea028c88a7b89Daniel Jasper break; 35432d28ee6061930b2a9c170cbaaeea028c88a7b89Daniel Jasper case tok::kw_for: 3554e778091b0ff42895644ca8bef30f1a91ba6b32bDaniel Jasper Contexts.back().ColonIsForRangeExpr = true; 35632d28ee6061930b2a9c170cbaaeea028c88a7b89Daniel Jasper next(); 35732d28ee6061930b2a9c170cbaaeea028c88a7b89Daniel Jasper if (!parseParens()) 35832d28ee6061930b2a9c170cbaaeea028c88a7b89Daniel Jasper return false; 35932d28ee6061930b2a9c170cbaaeea028c88a7b89Daniel Jasper break; 36032d28ee6061930b2a9c170cbaaeea028c88a7b89Daniel Jasper case tok::l_paren: 36132d28ee6061930b2a9c170cbaaeea028c88a7b89Daniel Jasper if (!parseParens()) 36232d28ee6061930b2a9c170cbaaeea028c88a7b89Daniel Jasper return false; 3631407bee187d7b964d5293ac8bf4f7a490c78cec6Daniel Jasper if (Line.MustBeDeclaration && NameFound && !Contexts.back().IsExpression) 3643c08a818a6ac9115fe8880af9bbf5a0a87bdffaaDaniel Jasper Line.MightBeFunctionDecl = true; 36532d28ee6061930b2a9c170cbaaeea028c88a7b89Daniel Jasper break; 36632d28ee6061930b2a9c170cbaaeea028c88a7b89Daniel Jasper case tok::l_square: 36732d28ee6061930b2a9c170cbaaeea028c88a7b89Daniel Jasper if (!parseSquare()) 36832d28ee6061930b2a9c170cbaaeea028c88a7b89Daniel Jasper return false; 36932d28ee6061930b2a9c170cbaaeea028c88a7b89Daniel Jasper break; 37032d28ee6061930b2a9c170cbaaeea028c88a7b89Daniel Jasper case tok::l_brace: 37132d28ee6061930b2a9c170cbaaeea028c88a7b89Daniel Jasper if (!parseBrace()) 37232d28ee6061930b2a9c170cbaaeea028c88a7b89Daniel Jasper return false; 37332d28ee6061930b2a9c170cbaaeea028c88a7b89Daniel Jasper break; 37432d28ee6061930b2a9c170cbaaeea028c88a7b89Daniel Jasper case tok::less: 3750236dd09c040f2e6124654d620dde94a595c5ab0Daniel Jasper if (Tok->Previous && !Tok->Previous->Tok.isLiteral() && parseAngle()) 37632d28ee6061930b2a9c170cbaaeea028c88a7b89Daniel Jasper Tok->Type = TT_TemplateOpener; 37732d28ee6061930b2a9c170cbaaeea028c88a7b89Daniel Jasper else { 37832d28ee6061930b2a9c170cbaaeea028c88a7b89Daniel Jasper Tok->Type = TT_BinaryOperator; 37932d28ee6061930b2a9c170cbaaeea028c88a7b89Daniel Jasper CurrentToken = Tok; 38032d28ee6061930b2a9c170cbaaeea028c88a7b89Daniel Jasper next(); 38132d28ee6061930b2a9c170cbaaeea028c88a7b89Daniel Jasper } 38232d28ee6061930b2a9c170cbaaeea028c88a7b89Daniel Jasper break; 38332d28ee6061930b2a9c170cbaaeea028c88a7b89Daniel Jasper case tok::r_paren: 38432d28ee6061930b2a9c170cbaaeea028c88a7b89Daniel Jasper case tok::r_square: 38532d28ee6061930b2a9c170cbaaeea028c88a7b89Daniel Jasper return false; 38632d28ee6061930b2a9c170cbaaeea028c88a7b89Daniel Jasper case tok::r_brace: 38732d28ee6061930b2a9c170cbaaeea028c88a7b89Daniel Jasper // Lines can start with '}'. 388b398701e4cbb9a55d90a60e3f4f4bc577446d098Manuel Klimek if (Tok->Previous != NULL) 38932d28ee6061930b2a9c170cbaaeea028c88a7b89Daniel Jasper return false; 39032d28ee6061930b2a9c170cbaaeea028c88a7b89Daniel Jasper break; 39132d28ee6061930b2a9c170cbaaeea028c88a7b89Daniel Jasper case tok::greater: 39232d28ee6061930b2a9c170cbaaeea028c88a7b89Daniel Jasper Tok->Type = TT_BinaryOperator; 39332d28ee6061930b2a9c170cbaaeea028c88a7b89Daniel Jasper break; 39432d28ee6061930b2a9c170cbaaeea028c88a7b89Daniel Jasper case tok::kw_operator: 395174f60f005167984d00682d1d38a2927b9c04684Daniel Jasper while (CurrentToken && 396174f60f005167984d00682d1d38a2927b9c04684Daniel Jasper !CurrentToken->isOneOf(tok::l_paren, tok::semi, tok::r_paren)) { 397e74de28ec3692986f3467a7f160a0e293277fa7eAlexander Kornienko if (CurrentToken->isOneOf(tok::star, tok::amp)) 3982b4c924f85e156d66c01b3f16c850892c47dcc7aDaniel Jasper CurrentToken->Type = TT_PointerOrReference; 3992b4c924f85e156d66c01b3f16c850892c47dcc7aDaniel Jasper consumeToken(); 400174f60f005167984d00682d1d38a2927b9c04684Daniel Jasper if (CurrentToken && CurrentToken->Previous->Type == TT_BinaryOperator) 401c476ea976badd316e3afd0f34afe1f030a710117Daniel Jasper CurrentToken->Previous->Type = TT_OverloadedOperator; 40232d28ee6061930b2a9c170cbaaeea028c88a7b89Daniel Jasper } 4036ea933c7e8f6988d5647af4a0eafd393a4c3685aDaniel Jasper if (CurrentToken) { 4042b4c924f85e156d66c01b3f16c850892c47dcc7aDaniel Jasper CurrentToken->Type = TT_OverloadedOperatorLParen; 405b398701e4cbb9a55d90a60e3f4f4bc577446d098Manuel Klimek if (CurrentToken->Previous->Type == TT_BinaryOperator) 406b398701e4cbb9a55d90a60e3f4f4bc577446d098Manuel Klimek CurrentToken->Previous->Type = TT_OverloadedOperator; 4076ea933c7e8f6988d5647af4a0eafd393a4c3685aDaniel Jasper } 40832d28ee6061930b2a9c170cbaaeea028c88a7b89Daniel Jasper break; 40932d28ee6061930b2a9c170cbaaeea028c88a7b89Daniel Jasper case tok::question: 41032d28ee6061930b2a9c170cbaaeea028c88a7b89Daniel Jasper parseConditional(); 41132d28ee6061930b2a9c170cbaaeea028c88a7b89Daniel Jasper break; 41232d28ee6061930b2a9c170cbaaeea028c88a7b89Daniel Jasper case tok::kw_template: 41332d28ee6061930b2a9c170cbaaeea028c88a7b89Daniel Jasper parseTemplateDeclaration(); 41432d28ee6061930b2a9c170cbaaeea028c88a7b89Daniel Jasper break; 415c2e6d2a4a7fe9dfa7d52a38c6048b7b18e6b591aNico Weber case tok::identifier: 416b398701e4cbb9a55d90a60e3f4f4bc577446d098Manuel Klimek if (Line.First->is(tok::kw_for) && 417b398701e4cbb9a55d90a60e3f4f4bc577446d098Manuel Klimek Tok->Tok.getIdentifierInfo() == &Ident_in) 418c2e6d2a4a7fe9dfa7d52a38c6048b7b18e6b591aNico Weber Tok->Type = TT_ObjCForIn; 419c2e6d2a4a7fe9dfa7d52a38c6048b7b18e6b591aNico Weber break; 4208ed9f2b25f082a1643ab5310f9eec33cf31a7577Daniel Jasper case tok::comma: 4218ed9f2b25f082a1643ab5310f9eec33cf31a7577Daniel Jasper if (Contexts.back().FirstStartOfName) 4228ed9f2b25f082a1643ab5310f9eec33cf31a7577Daniel Jasper Contexts.back().FirstStartOfName->PartOfMultiVariableDeclStmt = true; 423e8b10d3d5b90efaf60ad89e96f6500f971ceec41Daniel Jasper if (Contexts.back().InCtorInitializer) 424e8b10d3d5b90efaf60ad89e96f6500f971ceec41Daniel Jasper Tok->Type = TT_CtorInitializerComma; 4258ed9f2b25f082a1643ab5310f9eec33cf31a7577Daniel Jasper break; 42632d28ee6061930b2a9c170cbaaeea028c88a7b89Daniel Jasper default: 42732d28ee6061930b2a9c170cbaaeea028c88a7b89Daniel Jasper break; 42832d28ee6061930b2a9c170cbaaeea028c88a7b89Daniel Jasper } 42932d28ee6061930b2a9c170cbaaeea028c88a7b89Daniel Jasper return true; 43032d28ee6061930b2a9c170cbaaeea028c88a7b89Daniel Jasper } 43132d28ee6061930b2a9c170cbaaeea028c88a7b89Daniel Jasper 43232d28ee6061930b2a9c170cbaaeea028c88a7b89Daniel Jasper void parseIncludeDirective() { 43332d28ee6061930b2a9c170cbaaeea028c88a7b89Daniel Jasper next(); 43432d28ee6061930b2a9c170cbaaeea028c88a7b89Daniel Jasper if (CurrentToken != NULL && CurrentToken->is(tok::less)) { 43532d28ee6061930b2a9c170cbaaeea028c88a7b89Daniel Jasper next(); 43632d28ee6061930b2a9c170cbaaeea028c88a7b89Daniel Jasper while (CurrentToken != NULL) { 437b398701e4cbb9a55d90a60e3f4f4bc577446d098Manuel Klimek if (CurrentToken->isNot(tok::comment) || CurrentToken->Next) 43832d28ee6061930b2a9c170cbaaeea028c88a7b89Daniel Jasper CurrentToken->Type = TT_ImplicitStringLiteral; 43932d28ee6061930b2a9c170cbaaeea028c88a7b89Daniel Jasper next(); 44032d28ee6061930b2a9c170cbaaeea028c88a7b89Daniel Jasper } 44132d28ee6061930b2a9c170cbaaeea028c88a7b89Daniel Jasper } else { 44232d28ee6061930b2a9c170cbaaeea028c88a7b89Daniel Jasper while (CurrentToken != NULL) { 4433a204418482c9ae70ad482e781132c54306c3aa6Daniel Jasper if (CurrentToken->is(tok::string_literal)) 4443a204418482c9ae70ad482e781132c54306c3aa6Daniel Jasper // Mark these string literals as "implicit" literals, too, so that 4453a204418482c9ae70ad482e781132c54306c3aa6Daniel Jasper // they are not split or line-wrapped. 4463a204418482c9ae70ad482e781132c54306c3aa6Daniel Jasper CurrentToken->Type = TT_ImplicitStringLiteral; 44732d28ee6061930b2a9c170cbaaeea028c88a7b89Daniel Jasper next(); 44832d28ee6061930b2a9c170cbaaeea028c88a7b89Daniel Jasper } 44932d28ee6061930b2a9c170cbaaeea028c88a7b89Daniel Jasper } 45032d28ee6061930b2a9c170cbaaeea028c88a7b89Daniel Jasper } 45132d28ee6061930b2a9c170cbaaeea028c88a7b89Daniel Jasper 45232d28ee6061930b2a9c170cbaaeea028c88a7b89Daniel Jasper void parseWarningOrError() { 45332d28ee6061930b2a9c170cbaaeea028c88a7b89Daniel Jasper next(); 45432d28ee6061930b2a9c170cbaaeea028c88a7b89Daniel Jasper // We still want to format the whitespace left of the first token of the 45532d28ee6061930b2a9c170cbaaeea028c88a7b89Daniel Jasper // warning or error. 45632d28ee6061930b2a9c170cbaaeea028c88a7b89Daniel Jasper next(); 45732d28ee6061930b2a9c170cbaaeea028c88a7b89Daniel Jasper while (CurrentToken != NULL) { 45832d28ee6061930b2a9c170cbaaeea028c88a7b89Daniel Jasper CurrentToken->Type = TT_ImplicitStringLiteral; 45932d28ee6061930b2a9c170cbaaeea028c88a7b89Daniel Jasper next(); 46032d28ee6061930b2a9c170cbaaeea028c88a7b89Daniel Jasper } 46132d28ee6061930b2a9c170cbaaeea028c88a7b89Daniel Jasper } 46232d28ee6061930b2a9c170cbaaeea028c88a7b89Daniel Jasper 46332d28ee6061930b2a9c170cbaaeea028c88a7b89Daniel Jasper void parsePreprocessorDirective() { 46432d28ee6061930b2a9c170cbaaeea028c88a7b89Daniel Jasper next(); 46532d28ee6061930b2a9c170cbaaeea028c88a7b89Daniel Jasper if (CurrentToken == NULL) 46632d28ee6061930b2a9c170cbaaeea028c88a7b89Daniel Jasper return; 467b18c258390f794d8803ef5ebbb56fb77bfea7ba4Alexander Kornienko if (CurrentToken->Tok.is(tok::numeric_constant)) { 468b18c258390f794d8803ef5ebbb56fb77bfea7ba4Alexander Kornienko CurrentToken->SpacesRequiredBefore = 1; 469b18c258390f794d8803ef5ebbb56fb77bfea7ba4Alexander Kornienko return; 470b18c258390f794d8803ef5ebbb56fb77bfea7ba4Alexander Kornienko } 47132d28ee6061930b2a9c170cbaaeea028c88a7b89Daniel Jasper // Hashes in the middle of a line can lead to any strange token 47232d28ee6061930b2a9c170cbaaeea028c88a7b89Daniel Jasper // sequence. 473b398701e4cbb9a55d90a60e3f4f4bc577446d098Manuel Klimek if (CurrentToken->Tok.getIdentifierInfo() == NULL) 47432d28ee6061930b2a9c170cbaaeea028c88a7b89Daniel Jasper return; 475b398701e4cbb9a55d90a60e3f4f4bc577446d098Manuel Klimek switch (CurrentToken->Tok.getIdentifierInfo()->getPPKeywordID()) { 47632d28ee6061930b2a9c170cbaaeea028c88a7b89Daniel Jasper case tok::pp_include: 47732d28ee6061930b2a9c170cbaaeea028c88a7b89Daniel Jasper case tok::pp_import: 47832d28ee6061930b2a9c170cbaaeea028c88a7b89Daniel Jasper parseIncludeDirective(); 47932d28ee6061930b2a9c170cbaaeea028c88a7b89Daniel Jasper break; 48032d28ee6061930b2a9c170cbaaeea028c88a7b89Daniel Jasper case tok::pp_error: 48132d28ee6061930b2a9c170cbaaeea028c88a7b89Daniel Jasper case tok::pp_warning: 48232d28ee6061930b2a9c170cbaaeea028c88a7b89Daniel Jasper parseWarningOrError(); 48332d28ee6061930b2a9c170cbaaeea028c88a7b89Daniel Jasper break; 484aae7bad5cdd804cb88e918e9defbf5faf69f4cfdDaniel Jasper case tok::pp_if: 485aae7bad5cdd804cb88e918e9defbf5faf69f4cfdDaniel Jasper case tok::pp_elif: 486aae7bad5cdd804cb88e918e9defbf5faf69f4cfdDaniel Jasper parseLine(); 487aae7bad5cdd804cb88e918e9defbf5faf69f4cfdDaniel Jasper break; 48832d28ee6061930b2a9c170cbaaeea028c88a7b89Daniel Jasper default: 48932d28ee6061930b2a9c170cbaaeea028c88a7b89Daniel Jasper break; 49032d28ee6061930b2a9c170cbaaeea028c88a7b89Daniel Jasper } 4915b7e7b0ec77f49c1b24deffc9b7032ca16ca9f0dDaniel Jasper while (CurrentToken != NULL) 4925b7e7b0ec77f49c1b24deffc9b7032ca16ca9f0dDaniel Jasper next(); 49332d28ee6061930b2a9c170cbaaeea028c88a7b89Daniel Jasper } 49432d28ee6061930b2a9c170cbaaeea028c88a7b89Daniel Jasper 49595e8e468af284afb11ec1f994d0b5076fc7423d9Nico Weberpublic: 49632d28ee6061930b2a9c170cbaaeea028c88a7b89Daniel Jasper LineType parseLine() { 49732d28ee6061930b2a9c170cbaaeea028c88a7b89Daniel Jasper if (CurrentToken->is(tok::hash)) { 49832d28ee6061930b2a9c170cbaaeea028c88a7b89Daniel Jasper parsePreprocessorDirective(); 49932d28ee6061930b2a9c170cbaaeea028c88a7b89Daniel Jasper return LT_PreprocessorDirective; 50032d28ee6061930b2a9c170cbaaeea028c88a7b89Daniel Jasper } 50132d28ee6061930b2a9c170cbaaeea028c88a7b89Daniel Jasper while (CurrentToken != NULL) { 50232d28ee6061930b2a9c170cbaaeea028c88a7b89Daniel Jasper if (CurrentToken->is(tok::kw_virtual)) 50332d28ee6061930b2a9c170cbaaeea028c88a7b89Daniel Jasper KeywordVirtualFound = true; 50432d28ee6061930b2a9c170cbaaeea028c88a7b89Daniel Jasper if (!consumeToken()) 50532d28ee6061930b2a9c170cbaaeea028c88a7b89Daniel Jasper return LT_Invalid; 50632d28ee6061930b2a9c170cbaaeea028c88a7b89Daniel Jasper } 50732d28ee6061930b2a9c170cbaaeea028c88a7b89Daniel Jasper if (KeywordVirtualFound) 50832d28ee6061930b2a9c170cbaaeea028c88a7b89Daniel Jasper return LT_VirtualFunctionDecl; 50932d28ee6061930b2a9c170cbaaeea028c88a7b89Daniel Jasper 510b398701e4cbb9a55d90a60e3f4f4bc577446d098Manuel Klimek if (Line.First->Type == TT_ObjCMethodSpecifier) { 5114e778091b0ff42895644ca8bef30f1a91ba6b32bDaniel Jasper if (Contexts.back().FirstObjCSelectorName != NULL) 5124e778091b0ff42895644ca8bef30f1a91ba6b32bDaniel Jasper Contexts.back().FirstObjCSelectorName->LongestObjCSelectorName = 5134e778091b0ff42895644ca8bef30f1a91ba6b32bDaniel Jasper Contexts.back().LongestObjCSelectorName; 51463d7cedca8616921c1908c88c2f23fcd67bbab99Daniel Jasper return LT_ObjCMethodDecl; 51563d7cedca8616921c1908c88c2f23fcd67bbab99Daniel Jasper } 51663d7cedca8616921c1908c88c2f23fcd67bbab99Daniel Jasper 51732d28ee6061930b2a9c170cbaaeea028c88a7b89Daniel Jasper return LT_Other; 51832d28ee6061930b2a9c170cbaaeea028c88a7b89Daniel Jasper } 51932d28ee6061930b2a9c170cbaaeea028c88a7b89Daniel Jasper 52095e8e468af284afb11ec1f994d0b5076fc7423d9Nico Weberprivate: 52132d28ee6061930b2a9c170cbaaeea028c88a7b89Daniel Jasper void next() { 5220178673f541685cf5067814dfeee2644078e39a9Daniel Jasper if (CurrentToken != NULL) { 5230178673f541685cf5067814dfeee2644078e39a9Daniel Jasper determineTokenType(*CurrentToken); 5244e778091b0ff42895644ca8bef30f1a91ba6b32bDaniel Jasper CurrentToken->BindingStrength = Contexts.back().BindingStrength; 5250178673f541685cf5067814dfeee2644078e39a9Daniel Jasper } 5260178673f541685cf5067814dfeee2644078e39a9Daniel Jasper 527b398701e4cbb9a55d90a60e3f4f4bc577446d098Manuel Klimek if (CurrentToken != NULL) 528b398701e4cbb9a55d90a60e3f4f4bc577446d098Manuel Klimek CurrentToken = CurrentToken->Next; 529d0f349be1422a123fdb28d6dd556f7300e6d51e9Daniel Jasper 530ae76f7f850a9101a20191b10241ca72c23dc40ddManuel Klimek if (CurrentToken != NULL) { 531ae76f7f850a9101a20191b10241ca72c23dc40ddManuel Klimek // Reset token type in case we have already looked at it and then 532ae76f7f850a9101a20191b10241ca72c23dc40ddManuel Klimek // recovered from an error (e.g. failure to find the matching >). 533ae76f7f850a9101a20191b10241ca72c23dc40ddManuel Klimek if (CurrentToken->Type != TT_LambdaLSquare && 534ae76f7f850a9101a20191b10241ca72c23dc40ddManuel Klimek CurrentToken->Type != TT_ImplicitStringLiteral) 535ae76f7f850a9101a20191b10241ca72c23dc40ddManuel Klimek CurrentToken->Type = TT_Unknown; 536ae76f7f850a9101a20191b10241ca72c23dc40ddManuel Klimek if (CurrentToken->Role) 537ae76f7f850a9101a20191b10241ca72c23dc40ddManuel Klimek CurrentToken->Role.reset(NULL); 538ae76f7f850a9101a20191b10241ca72c23dc40ddManuel Klimek CurrentToken->FakeLParens.clear(); 539ae76f7f850a9101a20191b10241ca72c23dc40ddManuel Klimek CurrentToken->FakeRParens = 0; 540ae76f7f850a9101a20191b10241ca72c23dc40ddManuel Klimek } 54132d28ee6061930b2a9c170cbaaeea028c88a7b89Daniel Jasper } 54232d28ee6061930b2a9c170cbaaeea028c88a7b89Daniel Jasper 5434e778091b0ff42895644ca8bef30f1a91ba6b32bDaniel Jasper /// \brief A struct to hold information valid in a specific context, e.g. 5444e778091b0ff42895644ca8bef30f1a91ba6b32bDaniel Jasper /// a pair of parenthesis. 5454e778091b0ff42895644ca8bef30f1a91ba6b32bDaniel Jasper struct Context { 546923ebef120a37122ce50722a85cbe42c0c2dab53Daniel Jasper Context(tok::TokenKind ContextKind, unsigned BindingStrength, 547923ebef120a37122ce50722a85cbe42c0c2dab53Daniel Jasper bool IsExpression) 548923ebef120a37122ce50722a85cbe42c0c2dab53Daniel Jasper : ContextKind(ContextKind), BindingStrength(BindingStrength), 549923ebef120a37122ce50722a85cbe42c0c2dab53Daniel Jasper LongestObjCSelectorName(0), ColonIsForRangeExpr(false), 550f2ff8126e9e9df368f31b1f968d8fc80f99809b3Nico Weber ColonIsObjCDictLiteral(false), ColonIsObjCMethodExpr(false), 551f2ff8126e9e9df368f31b1f968d8fc80f99809b3Nico Weber FirstObjCSelectorName(NULL), FirstStartOfName(NULL), 552e8b10d3d5b90efaf60ad89e96f6500f971ceec41Daniel Jasper IsExpression(IsExpression), CanBeExpression(true), 553e8b10d3d5b90efaf60ad89e96f6500f971ceec41Daniel Jasper InCtorInitializer(false) {} 554923ebef120a37122ce50722a85cbe42c0c2dab53Daniel Jasper 555923ebef120a37122ce50722a85cbe42c0c2dab53Daniel Jasper tok::TokenKind ContextKind; 5564e778091b0ff42895644ca8bef30f1a91ba6b32bDaniel Jasper unsigned BindingStrength; 5574e778091b0ff42895644ca8bef30f1a91ba6b32bDaniel Jasper unsigned LongestObjCSelectorName; 5584e778091b0ff42895644ca8bef30f1a91ba6b32bDaniel Jasper bool ColonIsForRangeExpr; 559f2ff8126e9e9df368f31b1f968d8fc80f99809b3Nico Weber bool ColonIsObjCDictLiteral; 5604e778091b0ff42895644ca8bef30f1a91ba6b32bDaniel Jasper bool ColonIsObjCMethodExpr; 561b398701e4cbb9a55d90a60e3f4f4bc577446d098Manuel Klimek FormatToken *FirstObjCSelectorName; 562b398701e4cbb9a55d90a60e3f4f4bc577446d098Manuel Klimek FormatToken *FirstStartOfName; 5634e778091b0ff42895644ca8bef30f1a91ba6b32bDaniel Jasper bool IsExpression; 5646f21a988990ff5872822dcb049bd8fc65ce3d236Daniel Jasper bool CanBeExpression; 565e8b10d3d5b90efaf60ad89e96f6500f971ceec41Daniel Jasper bool InCtorInitializer; 5664e778091b0ff42895644ca8bef30f1a91ba6b32bDaniel Jasper }; 5674e778091b0ff42895644ca8bef30f1a91ba6b32bDaniel Jasper 5684e778091b0ff42895644ca8bef30f1a91ba6b32bDaniel Jasper /// \brief Puts a new \c Context onto the stack \c Contexts for the lifetime 5694e778091b0ff42895644ca8bef30f1a91ba6b32bDaniel Jasper /// of each instance. 5704e778091b0ff42895644ca8bef30f1a91ba6b32bDaniel Jasper struct ScopedContextCreator { 5714e778091b0ff42895644ca8bef30f1a91ba6b32bDaniel Jasper AnnotatingParser &P; 5724e778091b0ff42895644ca8bef30f1a91ba6b32bDaniel Jasper 573923ebef120a37122ce50722a85cbe42c0c2dab53Daniel Jasper ScopedContextCreator(AnnotatingParser &P, tok::TokenKind ContextKind, 574923ebef120a37122ce50722a85cbe42c0c2dab53Daniel Jasper unsigned Increase) 575923ebef120a37122ce50722a85cbe42c0c2dab53Daniel Jasper : P(P) { 5762a409b62126d8f0b8f5749d5ed435ad2b394b526Daniel Jasper P.Contexts.push_back(Context(ContextKind, 5772a409b62126d8f0b8f5749d5ed435ad2b394b526Daniel Jasper P.Contexts.back().BindingStrength + Increase, 5782a409b62126d8f0b8f5749d5ed435ad2b394b526Daniel Jasper P.Contexts.back().IsExpression)); 5794e778091b0ff42895644ca8bef30f1a91ba6b32bDaniel Jasper } 5804e778091b0ff42895644ca8bef30f1a91ba6b32bDaniel Jasper 5814e778091b0ff42895644ca8bef30f1a91ba6b32bDaniel Jasper ~ScopedContextCreator() { P.Contexts.pop_back(); } 5824e778091b0ff42895644ca8bef30f1a91ba6b32bDaniel Jasper }; 5830178673f541685cf5067814dfeee2644078e39a9Daniel Jasper 584b398701e4cbb9a55d90a60e3f4f4bc577446d098Manuel Klimek void determineTokenType(FormatToken &Current) { 585b398701e4cbb9a55d90a60e3f4f4bc577446d098Manuel Klimek if (Current.getPrecedence() == prec::Assignment && 58653352600b7370b1d33b9fde1adda207fd9d7dcd1Daniel Jasper !Line.First->isOneOf(tok::kw_template, tok::kw_using) && 587b398701e4cbb9a55d90a60e3f4f4bc577446d098Manuel Klimek (!Current.Previous || Current.Previous->isNot(tok::kw_operator))) { 5884e778091b0ff42895644ca8bef30f1a91ba6b32bDaniel Jasper Contexts.back().IsExpression = true; 589b398701e4cbb9a55d90a60e3f4f4bc577446d098Manuel Klimek for (FormatToken *Previous = Current.Previous; 5907e27400c90e9e295bcf5857eebf2d60c4b32106eDaniel Jasper Previous && !Previous->isOneOf(tok::comma, tok::semi); 591b398701e4cbb9a55d90a60e3f4f4bc577446d098Manuel Klimek Previous = Previous->Previous) { 5929c65b069821b7de79427e291b006293a0f55ff8fDaniel Jasper if (Previous->is(tok::r_square)) 5939c65b069821b7de79427e291b006293a0f55ff8fDaniel Jasper Previous = Previous->MatchingParen; 5940178673f541685cf5067814dfeee2644078e39a9Daniel Jasper if (Previous->Type == TT_BinaryOperator && 595e74de28ec3692986f3467a7f160a0e293277fa7eAlexander Kornienko Previous->isOneOf(tok::star, tok::amp)) { 5960178673f541685cf5067814dfeee2644078e39a9Daniel Jasper Previous->Type = TT_PointerOrReference; 5970178673f541685cf5067814dfeee2644078e39a9Daniel Jasper } 5980178673f541685cf5067814dfeee2644078e39a9Daniel Jasper } 599e74de28ec3692986f3467a7f160a0e293277fa7eAlexander Kornienko } else if (Current.isOneOf(tok::kw_return, tok::kw_throw) || 60095e8e468af284afb11ec1f994d0b5076fc7423d9Nico Weber (Current.is(tok::l_paren) && !Line.MustBeDeclaration && 601378d93dcf7ec80661efc65642dc6266c9e13780aDaniel Jasper !Line.InPPDirective && 6022530fd5a235c1e57f8ebef2eae9b365042501009Manuel Klimek (!Current.Previous || 60353352600b7370b1d33b9fde1adda207fd9d7dcd1Daniel Jasper !Current.Previous->isOneOf(tok::kw_for, tok::kw_catch)))) { 6044e778091b0ff42895644ca8bef30f1a91ba6b32bDaniel Jasper Contexts.back().IsExpression = true; 605e74de28ec3692986f3467a7f160a0e293277fa7eAlexander Kornienko } else if (Current.isOneOf(tok::r_paren, tok::greater, tok::comma)) { 606b398701e4cbb9a55d90a60e3f4f4bc577446d098Manuel Klimek for (FormatToken *Previous = Current.Previous; 607e74de28ec3692986f3467a7f160a0e293277fa7eAlexander Kornienko Previous && Previous->isOneOf(tok::star, tok::amp); 608b398701e4cbb9a55d90a60e3f4f4bc577446d098Manuel Klimek Previous = Previous->Previous) 60995e8e468af284afb11ec1f994d0b5076fc7423d9Nico Weber Previous->Type = TT_PointerOrReference; 610b398701e4cbb9a55d90a60e3f4f4bc577446d098Manuel Klimek } else if (Current.Previous && 611b398701e4cbb9a55d90a60e3f4f4bc577446d098Manuel Klimek Current.Previous->Type == TT_CtorInitializerColon) { 612d0f349be1422a123fdb28d6dd556f7300e6d51e9Daniel Jasper Contexts.back().IsExpression = true; 613e8b10d3d5b90efaf60ad89e96f6500f971ceec41Daniel Jasper Contexts.back().InCtorInitializer = true; 6146f21a988990ff5872822dcb049bd8fc65ce3d236Daniel Jasper } else if (Current.is(tok::kw_new)) { 6156f21a988990ff5872822dcb049bd8fc65ce3d236Daniel Jasper Contexts.back().CanBeExpression = false; 61616a69ef481f4580c571b6c693a5d3dd64ea56b53Daniel Jasper } else if (Current.is(tok::semi)) { 61716a69ef481f4580c571b6c693a5d3dd64ea56b53Daniel Jasper // This should be the condition or increment in a for-loop. 61816a69ef481f4580c571b6c693a5d3dd64ea56b53Daniel Jasper Contexts.back().IsExpression = true; 61995e8e468af284afb11ec1f994d0b5076fc7423d9Nico Weber } 6200178673f541685cf5067814dfeee2644078e39a9Daniel Jasper 6210178673f541685cf5067814dfeee2644078e39a9Daniel Jasper if (Current.Type == TT_Unknown) { 6226b3ff8c4caaa6782289a780e096fe56ad6434bb7Daniel Jasper // Line.MightBeFunctionDecl can only be true after the parentheses of a 6236b3ff8c4caaa6782289a780e096fe56ad6434bb7Daniel Jasper // function declaration have been found. In this case, 'Current' is a 6246b3ff8c4caaa6782289a780e096fe56ad6434bb7Daniel Jasper // trailing token of this declaration and thus cannot be a name. 6256b3ff8c4caaa6782289a780e096fe56ad6434bb7Daniel Jasper if (isStartOfName(Current) && !Line.MightBeFunctionDecl) { 6268ed9f2b25f082a1643ab5310f9eec33cf31a7577Daniel Jasper Contexts.back().FirstStartOfName = &Current; 6273c08a818a6ac9115fe8880af9bbf5a0a87bdffaaDaniel Jasper Current.Type = TT_StartOfName; 6281407bee187d7b964d5293ac8bf4f7a490c78cec6Daniel Jasper NameFound = true; 6292ca3741a962ded08866596577aaee2f4ab74c955Daniel Jasper } else if (Current.is(tok::kw_auto)) { 6302ca3741a962ded08866596577aaee2f4ab74c955Daniel Jasper AutoFound = true; 6313262f4c10520cf11acea4cf590cfbf055924a41eDaniel Jasper } else if (Current.is(tok::arrow) && AutoFound && 6323262f4c10520cf11acea4cf590cfbf055924a41eDaniel Jasper Line.MustBeDeclaration) { 6332ca3741a962ded08866596577aaee2f4ab74c955Daniel Jasper Current.Type = TT_TrailingReturnArrow; 634e74de28ec3692986f3467a7f160a0e293277fa7eAlexander Kornienko } else if (Current.isOneOf(tok::star, tok::amp, tok::ampamp)) { 6354e778091b0ff42895644ca8bef30f1a91ba6b32bDaniel Jasper Current.Type = 636d6104f6c34639ebe66f83d955c5f32ea4a50c266Daniel Jasper determineStarAmpUsage(Current, Contexts.back().CanBeExpression && 637d6104f6c34639ebe66f83d955c5f32ea4a50c266Daniel Jasper Contexts.back().IsExpression); 638e74de28ec3692986f3467a7f160a0e293277fa7eAlexander Kornienko } else if (Current.isOneOf(tok::minus, tok::plus, tok::caret)) { 6390178673f541685cf5067814dfeee2644078e39a9Daniel Jasper Current.Type = determinePlusMinusCaretUsage(Current); 640e74de28ec3692986f3467a7f160a0e293277fa7eAlexander Kornienko } else if (Current.isOneOf(tok::minusminus, tok::plusplus)) { 6410178673f541685cf5067814dfeee2644078e39a9Daniel Jasper Current.Type = determineIncrementUsage(Current); 6420178673f541685cf5067814dfeee2644078e39a9Daniel Jasper } else if (Current.is(tok::exclaim)) { 6430178673f541685cf5067814dfeee2644078e39a9Daniel Jasper Current.Type = TT_UnaryOperator; 64431e44f7a5d50ab8f7f623a7d2e18d5d877ef400dManuel Klimek } else if (Current.isBinaryOperator() && 64531e44f7a5d50ab8f7f623a7d2e18d5d877ef400dManuel Klimek (!Current.Previous || 64631e44f7a5d50ab8f7f623a7d2e18d5d877ef400dManuel Klimek Current.Previous->isNot(tok::l_square))) { 6470178673f541685cf5067814dfeee2644078e39a9Daniel Jasper Current.Type = TT_BinaryOperator; 6480178673f541685cf5067814dfeee2644078e39a9Daniel Jasper } else if (Current.is(tok::comment)) { 64900895106f9ed602af67984ec4d225a0cdc8c12afAlexander Kornienko if (Current.TokenText.startswith("//")) 6500178673f541685cf5067814dfeee2644078e39a9Daniel Jasper Current.Type = TT_LineComment; 6510178673f541685cf5067814dfeee2644078e39a9Daniel Jasper else 6520178673f541685cf5067814dfeee2644078e39a9Daniel Jasper Current.Type = TT_BlockComment; 65337d693160eba22343e08d7bcf66cd132ace77e5cNico Weber } else if (Current.is(tok::r_paren)) { 654b8b4295b4ee161bfb76ff7b0ec1007bfd959553bDaniel Jasper FormatToken *LeftOfParens = NULL; 655b8b4295b4ee161bfb76ff7b0ec1007bfd959553bDaniel Jasper if (Current.MatchingParen) 6560bdc6434fa0fea933b6ab566eff751afdba40a2aAlexander Kornienko LeftOfParens = Current.MatchingParen->getPreviousNonComment(); 657b8b4295b4ee161bfb76ff7b0ec1007bfd959553bDaniel Jasper bool IsCast = false; 658b8b4295b4ee161bfb76ff7b0ec1007bfd959553bDaniel Jasper bool ParensAreEmpty = Current.Previous == Current.MatchingParen; 659b8b4295b4ee161bfb76ff7b0ec1007bfd959553bDaniel Jasper bool ParensAreType = !Current.Previous || 660b398701e4cbb9a55d90a60e3f4f4bc577446d098Manuel Klimek Current.Previous->Type == TT_PointerOrReference || 661b8b4295b4ee161bfb76ff7b0ec1007bfd959553bDaniel Jasper Current.Previous->Type == TT_TemplateCloser || 662b8b4295b4ee161bfb76ff7b0ec1007bfd959553bDaniel Jasper isSimpleTypeSpecifier(*Current.Previous); 66337d693160eba22343e08d7bcf66cd132ace77e5cNico Weber bool ParensCouldEndDecl = 664b398701e4cbb9a55d90a60e3f4f4bc577446d098Manuel Klimek Current.Next && 665b398701e4cbb9a55d90a60e3f4f4bc577446d098Manuel Klimek Current.Next->isOneOf(tok::equal, tok::semi, tok::l_brace); 6666a365aaa057a8c445d25344c0433726c752b3e7dDaniel Jasper bool IsSizeOfOrAlignOf = 667b8b4295b4ee161bfb76ff7b0ec1007bfd959553bDaniel Jasper LeftOfParens && 668b8b4295b4ee161bfb76ff7b0ec1007bfd959553bDaniel Jasper LeftOfParens->isOneOf(tok::kw_sizeof, tok::kw_alignof); 669b8b4295b4ee161bfb76ff7b0ec1007bfd959553bDaniel Jasper if (ParensAreType && !ParensCouldEndDecl && !IsSizeOfOrAlignOf && 6700c368787d9d1f92a3408b71b3f074a06edaa6bdeDaniel Jasper (Contexts.back().IsExpression || 6710c368787d9d1f92a3408b71b3f074a06edaa6bdeDaniel Jasper (Current.Next && Current.Next->isBinaryOperator()))) 672b8b4295b4ee161bfb76ff7b0ec1007bfd959553bDaniel Jasper IsCast = true; 6732a409b62126d8f0b8f5749d5ed435ad2b394b526Daniel Jasper if (Current.Next && Current.Next->isNot(tok::string_literal) && 674b8b4295b4ee161bfb76ff7b0ec1007bfd959553bDaniel Jasper (Current.Next->Tok.isLiteral() || 675b8b4295b4ee161bfb76ff7b0ec1007bfd959553bDaniel Jasper Current.Next->isOneOf(tok::kw_sizeof, tok::kw_alignof))) 676b8b4295b4ee161bfb76ff7b0ec1007bfd959553bDaniel Jasper IsCast = true; 677b8b4295b4ee161bfb76ff7b0ec1007bfd959553bDaniel Jasper // If there is an identifier after the (), it is likely a cast, unless 678b8b4295b4ee161bfb76ff7b0ec1007bfd959553bDaniel Jasper // there is also an identifier before the (). 679ff1a2e519ebcd6d7a060eac7ba8aca37b2bf89d0Daniel Jasper if (LeftOfParens && (LeftOfParens->Tok.getIdentifierInfo() == NULL || 680ff1a2e519ebcd6d7a060eac7ba8aca37b2bf89d0Daniel Jasper LeftOfParens->is(tok::kw_return)) && 681526df0f3a8d436e9084bd12118a2e119aa0bd724Daniel Jasper LeftOfParens->Type != TT_OverloadedOperator && 682465e8615a153ebd70eb27785af79f7e82e4a81d4Nico Weber LeftOfParens->Type != TT_TemplateCloser && Current.Next && 683465e8615a153ebd70eb27785af79f7e82e4a81d4Nico Weber Current.Next->is(tok::identifier)) 684b8b4295b4ee161bfb76ff7b0ec1007bfd959553bDaniel Jasper IsCast = true; 685b8b4295b4ee161bfb76ff7b0ec1007bfd959553bDaniel Jasper if (IsCast && !ParensAreEmpty) 68637d693160eba22343e08d7bcf66cd132ace77e5cNico Weber Current.Type = TT_CastRParen; 687b398701e4cbb9a55d90a60e3f4f4bc577446d098Manuel Klimek } else if (Current.is(tok::at) && Current.Next) { 688b398701e4cbb9a55d90a60e3f4f4bc577446d098Manuel Klimek switch (Current.Next->Tok.getObjCKeywordID()) { 6890178673f541685cf5067814dfeee2644078e39a9Daniel Jasper case tok::objc_interface: 6900178673f541685cf5067814dfeee2644078e39a9Daniel Jasper case tok::objc_implementation: 6910178673f541685cf5067814dfeee2644078e39a9Daniel Jasper case tok::objc_protocol: 6920178673f541685cf5067814dfeee2644078e39a9Daniel Jasper Current.Type = TT_ObjCDecl; 6930178673f541685cf5067814dfeee2644078e39a9Daniel Jasper break; 6940178673f541685cf5067814dfeee2644078e39a9Daniel Jasper case tok::objc_property: 6950178673f541685cf5067814dfeee2644078e39a9Daniel Jasper Current.Type = TT_ObjCProperty; 6960178673f541685cf5067814dfeee2644078e39a9Daniel Jasper break; 6970178673f541685cf5067814dfeee2644078e39a9Daniel Jasper default: 6980178673f541685cf5067814dfeee2644078e39a9Daniel Jasper break; 6990178673f541685cf5067814dfeee2644078e39a9Daniel Jasper } 7005ad390d27f8f7cb6628bc2c32beba4d25bc2718bDaniel Jasper } else if (Current.is(tok::period)) { 7010bdc6434fa0fea933b6ab566eff751afdba40a2aAlexander Kornienko FormatToken *PreviousNoComment = Current.getPreviousNonComment(); 7025ad390d27f8f7cb6628bc2c32beba4d25bc2718bDaniel Jasper if (PreviousNoComment && 7035ad390d27f8f7cb6628bc2c32beba4d25bc2718bDaniel Jasper PreviousNoComment->isOneOf(tok::comma, tok::l_brace)) 7045ad390d27f8f7cb6628bc2c32beba4d25bc2718bDaniel Jasper Current.Type = TT_DesignatedInitializerPeriod; 7050178673f541685cf5067814dfeee2644078e39a9Daniel Jasper } 7060178673f541685cf5067814dfeee2644078e39a9Daniel Jasper } 7070178673f541685cf5067814dfeee2644078e39a9Daniel Jasper } 7080178673f541685cf5067814dfeee2644078e39a9Daniel Jasper 7096ac431c7bae27a42351d2c2ad03dddd5b005ffd5Daniel Jasper /// \brief Take a guess at whether \p Tok starts a name of a function or 7106ac431c7bae27a42351d2c2ad03dddd5b005ffd5Daniel Jasper /// variable declaration. 7116ac431c7bae27a42351d2c2ad03dddd5b005ffd5Daniel Jasper /// 7126ac431c7bae27a42351d2c2ad03dddd5b005ffd5Daniel Jasper /// This is a heuristic based on whether \p Tok is an identifier following 7136ac431c7bae27a42351d2c2ad03dddd5b005ffd5Daniel Jasper /// something that is likely a type. 7146ac431c7bae27a42351d2c2ad03dddd5b005ffd5Daniel Jasper bool isStartOfName(const FormatToken &Tok) { 7156ac431c7bae27a42351d2c2ad03dddd5b005ffd5Daniel Jasper if (Tok.isNot(tok::identifier) || Tok.Previous == NULL) 7166ac431c7bae27a42351d2c2ad03dddd5b005ffd5Daniel Jasper return false; 7176ac431c7bae27a42351d2c2ad03dddd5b005ffd5Daniel Jasper 7186ac431c7bae27a42351d2c2ad03dddd5b005ffd5Daniel Jasper // Skip "const" as it does not have an influence on whether this is a name. 7196ac431c7bae27a42351d2c2ad03dddd5b005ffd5Daniel Jasper FormatToken *PreviousNotConst = Tok.Previous; 7206ac431c7bae27a42351d2c2ad03dddd5b005ffd5Daniel Jasper while (PreviousNotConst != NULL && PreviousNotConst->is(tok::kw_const)) 7216ac431c7bae27a42351d2c2ad03dddd5b005ffd5Daniel Jasper PreviousNotConst = PreviousNotConst->Previous; 7226ac431c7bae27a42351d2c2ad03dddd5b005ffd5Daniel Jasper 7236ac431c7bae27a42351d2c2ad03dddd5b005ffd5Daniel Jasper if (PreviousNotConst == NULL) 7246ac431c7bae27a42351d2c2ad03dddd5b005ffd5Daniel Jasper return false; 7256ac431c7bae27a42351d2c2ad03dddd5b005ffd5Daniel Jasper 7262a409b62126d8f0b8f5749d5ed435ad2b394b526Daniel Jasper bool IsPPKeyword = PreviousNotConst->is(tok::identifier) && 7272a409b62126d8f0b8f5749d5ed435ad2b394b526Daniel Jasper PreviousNotConst->Previous && 7282a409b62126d8f0b8f5749d5ed435ad2b394b526Daniel Jasper PreviousNotConst->Previous->is(tok::hash); 7296ac431c7bae27a42351d2c2ad03dddd5b005ffd5Daniel Jasper 73092495a8032d8624495a0ce769d5a6cdeba2dc8d8Daniel Jasper if (PreviousNotConst->Type == TT_TemplateCloser) 73192495a8032d8624495a0ce769d5a6cdeba2dc8d8Daniel Jasper return PreviousNotConst && PreviousNotConst->MatchingParen && 73292495a8032d8624495a0ce769d5a6cdeba2dc8d8Daniel Jasper PreviousNotConst->MatchingParen->Previous && 73392495a8032d8624495a0ce769d5a6cdeba2dc8d8Daniel Jasper PreviousNotConst->MatchingParen->Previous->isNot(tok::kw_template); 73492495a8032d8624495a0ce769d5a6cdeba2dc8d8Daniel Jasper 7356ac431c7bae27a42351d2c2ad03dddd5b005ffd5Daniel Jasper return (!IsPPKeyword && PreviousNotConst->is(tok::identifier)) || 7366ac431c7bae27a42351d2c2ad03dddd5b005ffd5Daniel Jasper PreviousNotConst->Type == TT_PointerOrReference || 7376ac431c7bae27a42351d2c2ad03dddd5b005ffd5Daniel Jasper isSimpleTypeSpecifier(*PreviousNotConst); 7386ac431c7bae27a42351d2c2ad03dddd5b005ffd5Daniel Jasper } 7396ac431c7bae27a42351d2c2ad03dddd5b005ffd5Daniel Jasper 7400178673f541685cf5067814dfeee2644078e39a9Daniel Jasper /// \brief Return the type of the given token assuming it is * or &. 741b398701e4cbb9a55d90a60e3f4f4bc577446d098Manuel Klimek TokenType determineStarAmpUsage(const FormatToken &Tok, bool IsExpression) { 7420bdc6434fa0fea933b6ab566eff751afdba40a2aAlexander Kornienko const FormatToken *PrevToken = Tok.getPreviousNonComment(); 7430178673f541685cf5067814dfeee2644078e39a9Daniel Jasper if (PrevToken == NULL) 7440178673f541685cf5067814dfeee2644078e39a9Daniel Jasper return TT_UnaryOperator; 7450178673f541685cf5067814dfeee2644078e39a9Daniel Jasper 7460bdc6434fa0fea933b6ab566eff751afdba40a2aAlexander Kornienko const FormatToken *NextToken = Tok.getNextNonComment(); 7470178673f541685cf5067814dfeee2644078e39a9Daniel Jasper if (NextToken == NULL) 7480178673f541685cf5067814dfeee2644078e39a9Daniel Jasper return TT_Unknown; 7490178673f541685cf5067814dfeee2644078e39a9Daniel Jasper 750431f5918281dd1f178bf838b7a47ab1d11c52961Daniel Jasper if (PrevToken->is(tok::coloncolon) || 751431f5918281dd1f178bf838b7a47ab1d11c52961Daniel Jasper (PrevToken->is(tok::l_paren) && !IsExpression)) 7528a5d7cd100ebfb8c6b353ee4ad5b14ab4105d32dDaniel Jasper return TT_PointerOrReference; 7538a5d7cd100ebfb8c6b353ee4ad5b14ab4105d32dDaniel Jasper 754e74de28ec3692986f3467a7f160a0e293277fa7eAlexander Kornienko if (PrevToken->isOneOf(tok::l_paren, tok::l_square, tok::l_brace, 755d3cf17b5f1fed43dbd0cd35c43d15139803c9c84Daniel Jasper tok::comma, tok::semi, tok::kw_return, tok::colon, 75665da8e952da32730202356290bb889c8839bbef5Daniel Jasper tok::equal, tok::kw_delete, tok::kw_sizeof) || 757e74de28ec3692986f3467a7f160a0e293277fa7eAlexander Kornienko PrevToken->Type == TT_BinaryOperator || 7580178673f541685cf5067814dfeee2644078e39a9Daniel Jasper PrevToken->Type == TT_UnaryOperator || PrevToken->Type == TT_CastRParen) 7590178673f541685cf5067814dfeee2644078e39a9Daniel Jasper return TT_UnaryOperator; 7600178673f541685cf5067814dfeee2644078e39a9Daniel Jasper 761e8a97985f72b4e11435ff2107c0f11e925fb6d96Nico Weber if (NextToken->is(tok::l_square)) 762e8a97985f72b4e11435ff2107c0f11e925fb6d96Nico Weber return TT_PointerOrReference; 763e8a97985f72b4e11435ff2107c0f11e925fb6d96Nico Weber 764db8afe424991b34884afb13ecf97ac458e41da9eDaniel Jasper if (PrevToken->is(tok::r_paren) && PrevToken->MatchingParen && 765db8afe424991b34884afb13ecf97ac458e41da9eDaniel Jasper PrevToken->MatchingParen->Previous && 766db8afe424991b34884afb13ecf97ac458e41da9eDaniel Jasper PrevToken->MatchingParen->Previous->is(tok::kw_typeof)) 767db8afe424991b34884afb13ecf97ac458e41da9eDaniel Jasper return TT_PointerOrReference; 768db8afe424991b34884afb13ecf97ac458e41da9eDaniel Jasper 769b398701e4cbb9a55d90a60e3f4f4bc577446d098Manuel Klimek if (PrevToken->Tok.isLiteral() || 770e74de28ec3692986f3467a7f160a0e293277fa7eAlexander Kornienko PrevToken->isOneOf(tok::r_paren, tok::r_square) || 771b398701e4cbb9a55d90a60e3f4f4bc577446d098Manuel Klimek NextToken->Tok.isLiteral() || NextToken->isUnaryOperator()) 7720178673f541685cf5067814dfeee2644078e39a9Daniel Jasper return TT_BinaryOperator; 7730178673f541685cf5067814dfeee2644078e39a9Daniel Jasper 7740178673f541685cf5067814dfeee2644078e39a9Daniel Jasper // It is very unlikely that we are going to find a pointer or reference type 7750178673f541685cf5067814dfeee2644078e39a9Daniel Jasper // definition on the RHS of an assignment. 7760178673f541685cf5067814dfeee2644078e39a9Daniel Jasper if (IsExpression) 7770178673f541685cf5067814dfeee2644078e39a9Daniel Jasper return TT_BinaryOperator; 7780178673f541685cf5067814dfeee2644078e39a9Daniel Jasper 7790178673f541685cf5067814dfeee2644078e39a9Daniel Jasper return TT_PointerOrReference; 7800178673f541685cf5067814dfeee2644078e39a9Daniel Jasper } 7810178673f541685cf5067814dfeee2644078e39a9Daniel Jasper 782b398701e4cbb9a55d90a60e3f4f4bc577446d098Manuel Klimek TokenType determinePlusMinusCaretUsage(const FormatToken &Tok) { 7830bdc6434fa0fea933b6ab566eff751afdba40a2aAlexander Kornienko const FormatToken *PrevToken = Tok.getPreviousNonComment(); 784b8b4295b4ee161bfb76ff7b0ec1007bfd959553bDaniel Jasper if (PrevToken == NULL || PrevToken->Type == TT_CastRParen) 7850178673f541685cf5067814dfeee2644078e39a9Daniel Jasper return TT_UnaryOperator; 7860178673f541685cf5067814dfeee2644078e39a9Daniel Jasper 7870178673f541685cf5067814dfeee2644078e39a9Daniel Jasper // Use heuristics to recognize unary operators. 788e74de28ec3692986f3467a7f160a0e293277fa7eAlexander Kornienko if (PrevToken->isOneOf(tok::equal, tok::l_paren, tok::comma, tok::l_square, 789e74de28ec3692986f3467a7f160a0e293277fa7eAlexander Kornienko tok::question, tok::colon, tok::kw_return, 790e74de28ec3692986f3467a7f160a0e293277fa7eAlexander Kornienko tok::kw_case, tok::at, tok::l_brace)) 7910178673f541685cf5067814dfeee2644078e39a9Daniel Jasper return TT_UnaryOperator; 7920178673f541685cf5067814dfeee2644078e39a9Daniel Jasper 793ee0feec7362053f22b6c01d12e1bfa06fb3ac93fNico Weber // There can't be two consecutive binary operators. 7940178673f541685cf5067814dfeee2644078e39a9Daniel Jasper if (PrevToken->Type == TT_BinaryOperator) 7950178673f541685cf5067814dfeee2644078e39a9Daniel Jasper return TT_UnaryOperator; 7960178673f541685cf5067814dfeee2644078e39a9Daniel Jasper 7970178673f541685cf5067814dfeee2644078e39a9Daniel Jasper // Fall back to marking the token as binary operator. 7980178673f541685cf5067814dfeee2644078e39a9Daniel Jasper return TT_BinaryOperator; 7990178673f541685cf5067814dfeee2644078e39a9Daniel Jasper } 8000178673f541685cf5067814dfeee2644078e39a9Daniel Jasper 8010178673f541685cf5067814dfeee2644078e39a9Daniel Jasper /// \brief Determine whether ++/-- are pre- or post-increments/-decrements. 802b398701e4cbb9a55d90a60e3f4f4bc577446d098Manuel Klimek TokenType determineIncrementUsage(const FormatToken &Tok) { 8030bdc6434fa0fea933b6ab566eff751afdba40a2aAlexander Kornienko const FormatToken *PrevToken = Tok.getPreviousNonComment(); 804b8b4295b4ee161bfb76ff7b0ec1007bfd959553bDaniel Jasper if (PrevToken == NULL || PrevToken->Type == TT_CastRParen) 8050178673f541685cf5067814dfeee2644078e39a9Daniel Jasper return TT_UnaryOperator; 806e74de28ec3692986f3467a7f160a0e293277fa7eAlexander Kornienko if (PrevToken->isOneOf(tok::r_paren, tok::r_square, tok::identifier)) 8070178673f541685cf5067814dfeee2644078e39a9Daniel Jasper return TT_TrailingUnaryOperator; 8080178673f541685cf5067814dfeee2644078e39a9Daniel Jasper 8090178673f541685cf5067814dfeee2644078e39a9Daniel Jasper return TT_UnaryOperator; 8100178673f541685cf5067814dfeee2644078e39a9Daniel Jasper } 8114e778091b0ff42895644ca8bef30f1a91ba6b32bDaniel Jasper 8128ed9f2b25f082a1643ab5310f9eec33cf31a7577Daniel Jasper // FIXME: This is copy&pasted from Sema. Put it in a common place and remove 8138ed9f2b25f082a1643ab5310f9eec33cf31a7577Daniel Jasper // duplication. 8148ed9f2b25f082a1643ab5310f9eec33cf31a7577Daniel Jasper /// \brief Determine whether the token kind starts a simple-type-specifier. 815b398701e4cbb9a55d90a60e3f4f4bc577446d098Manuel Klimek bool isSimpleTypeSpecifier(const FormatToken &Tok) const { 816b398701e4cbb9a55d90a60e3f4f4bc577446d098Manuel Klimek switch (Tok.Tok.getKind()) { 8178ed9f2b25f082a1643ab5310f9eec33cf31a7577Daniel Jasper case tok::kw_short: 8188ed9f2b25f082a1643ab5310f9eec33cf31a7577Daniel Jasper case tok::kw_long: 8198ed9f2b25f082a1643ab5310f9eec33cf31a7577Daniel Jasper case tok::kw___int64: 8208ed9f2b25f082a1643ab5310f9eec33cf31a7577Daniel Jasper case tok::kw___int128: 8218ed9f2b25f082a1643ab5310f9eec33cf31a7577Daniel Jasper case tok::kw_signed: 8228ed9f2b25f082a1643ab5310f9eec33cf31a7577Daniel Jasper case tok::kw_unsigned: 8238ed9f2b25f082a1643ab5310f9eec33cf31a7577Daniel Jasper case tok::kw_void: 8248ed9f2b25f082a1643ab5310f9eec33cf31a7577Daniel Jasper case tok::kw_char: 8258ed9f2b25f082a1643ab5310f9eec33cf31a7577Daniel Jasper case tok::kw_int: 8268ed9f2b25f082a1643ab5310f9eec33cf31a7577Daniel Jasper case tok::kw_half: 8278ed9f2b25f082a1643ab5310f9eec33cf31a7577Daniel Jasper case tok::kw_float: 8288ed9f2b25f082a1643ab5310f9eec33cf31a7577Daniel Jasper case tok::kw_double: 8298ed9f2b25f082a1643ab5310f9eec33cf31a7577Daniel Jasper case tok::kw_wchar_t: 8308ed9f2b25f082a1643ab5310f9eec33cf31a7577Daniel Jasper case tok::kw_bool: 8318ed9f2b25f082a1643ab5310f9eec33cf31a7577Daniel Jasper case tok::kw___underlying_type: 8328ed9f2b25f082a1643ab5310f9eec33cf31a7577Daniel Jasper case tok::annot_typename: 8338ed9f2b25f082a1643ab5310f9eec33cf31a7577Daniel Jasper case tok::kw_char16_t: 8348ed9f2b25f082a1643ab5310f9eec33cf31a7577Daniel Jasper case tok::kw_char32_t: 8358ed9f2b25f082a1643ab5310f9eec33cf31a7577Daniel Jasper case tok::kw_typeof: 8368ed9f2b25f082a1643ab5310f9eec33cf31a7577Daniel Jasper case tok::kw_decltype: 83700895106f9ed602af67984ec4d225a0cdc8c12afAlexander Kornienko return true; 8388ed9f2b25f082a1643ab5310f9eec33cf31a7577Daniel Jasper default: 83900895106f9ed602af67984ec4d225a0cdc8c12afAlexander Kornienko return false; 8408ed9f2b25f082a1643ab5310f9eec33cf31a7577Daniel Jasper } 8418ed9f2b25f082a1643ab5310f9eec33cf31a7577Daniel Jasper } 8428ed9f2b25f082a1643ab5310f9eec33cf31a7577Daniel Jasper 8434e778091b0ff42895644ca8bef30f1a91ba6b32bDaniel Jasper SmallVector<Context, 8> Contexts; 8444e778091b0ff42895644ca8bef30f1a91ba6b32bDaniel Jasper 845d4a03dbb0bdbd4908375306283cb61813bd8f454Daniel Jasper const FormatStyle &Style; 8464e778091b0ff42895644ca8bef30f1a91ba6b32bDaniel Jasper AnnotatedLine &Line; 847b398701e4cbb9a55d90a60e3f4f4bc577446d098Manuel Klimek FormatToken *CurrentToken; 8484e778091b0ff42895644ca8bef30f1a91ba6b32bDaniel Jasper bool KeywordVirtualFound; 8491407bee187d7b964d5293ac8bf4f7a490c78cec6Daniel Jasper bool NameFound; 8502ca3741a962ded08866596577aaee2f4ab74c955Daniel Jasper bool AutoFound; 851c2e6d2a4a7fe9dfa7d52a38c6048b7b18e6b591aNico Weber IdentifierInfo &Ident_in; 85232d28ee6061930b2a9c170cbaaeea028c88a7b89Daniel Jasper}; 85332d28ee6061930b2a9c170cbaaeea028c88a7b89Daniel Jasper 854d489f8ceb735458b0e1f814e3f952b154f49c025Daniel Jasperstatic int PrecedenceUnaryOperator = prec::PointerToMember + 1; 855d489f8ceb735458b0e1f814e3f952b154f49c025Daniel Jasperstatic int PrecedenceArrowAndPeriod = prec::PointerToMember + 2; 856d489f8ceb735458b0e1f814e3f952b154f49c025Daniel Jasper 85729f123b2fa0435bb1962f0d9e9a2e660f35fbb2fDaniel Jasper/// \brief Parses binary expressions by inserting fake parenthesis based on 85829f123b2fa0435bb1962f0d9e9a2e660f35fbb2fDaniel Jasper/// operator precedence. 85929f123b2fa0435bb1962f0d9e9a2e660f35fbb2fDaniel Jasperclass ExpressionParser { 86029f123b2fa0435bb1962f0d9e9a2e660f35fbb2fDaniel Jasperpublic: 8619acb8b4355028887e8cc4aa8f683aceee021a62bDaniel Jasper ExpressionParser(AnnotatedLine &Line) : Current(Line.First) { 8629acb8b4355028887e8cc4aa8f683aceee021a62bDaniel Jasper // Skip leading "}", e.g. in "} else if (...) {". 8639acb8b4355028887e8cc4aa8f683aceee021a62bDaniel Jasper if (Current->is(tok::r_brace)) 8649acb8b4355028887e8cc4aa8f683aceee021a62bDaniel Jasper next(); 8659acb8b4355028887e8cc4aa8f683aceee021a62bDaniel Jasper } 86629f123b2fa0435bb1962f0d9e9a2e660f35fbb2fDaniel Jasper 86729f123b2fa0435bb1962f0d9e9a2e660f35fbb2fDaniel Jasper /// \brief Parse expressions with the given operatore precedence. 868237d4c1c785be13656bff6c09e5b7ccd066ff5baDaniel Jasper void parse(int Precedence = 0) { 869f78bf4a0132a3ea366ba3baadd9d6af26c617d11Daniel Jasper // Skip 'return' as it is not part of a binary expression. 870f78bf4a0132a3ea366ba3baadd9d6af26c617d11Daniel Jasper while (Current && Current->is(tok::kw_return)) 871f78bf4a0132a3ea366ba3baadd9d6af26c617d11Daniel Jasper next(); 872f78bf4a0132a3ea366ba3baadd9d6af26c617d11Daniel Jasper 873d489f8ceb735458b0e1f814e3f952b154f49c025Daniel Jasper if (Current == NULL || Precedence > PrecedenceArrowAndPeriod) 8743618e6fae8b734ad94221d941417c12d4bd1e3a8Daniel Jasper return; 8753618e6fae8b734ad94221d941417c12d4bd1e3a8Daniel Jasper 876c01897c3f6139304ed25e3f673bef77be209617fDaniel Jasper // Conditional expressions need to be parsed separately for proper nesting. 877d489f8ceb735458b0e1f814e3f952b154f49c025Daniel Jasper if (Precedence == prec::Conditional) { 878c01897c3f6139304ed25e3f673bef77be209617fDaniel Jasper parseConditionalExpr(); 879c01897c3f6139304ed25e3f673bef77be209617fDaniel Jasper return; 880c01897c3f6139304ed25e3f673bef77be209617fDaniel Jasper } 8813618e6fae8b734ad94221d941417c12d4bd1e3a8Daniel Jasper 8823618e6fae8b734ad94221d941417c12d4bd1e3a8Daniel Jasper // Parse unary operators, which all have a higher precedence than binary 8833618e6fae8b734ad94221d941417c12d4bd1e3a8Daniel Jasper // operators. 884d489f8ceb735458b0e1f814e3f952b154f49c025Daniel Jasper if (Precedence == PrecedenceUnaryOperator) { 8853618e6fae8b734ad94221d941417c12d4bd1e3a8Daniel Jasper parseUnaryOperator(); 88629f123b2fa0435bb1962f0d9e9a2e660f35fbb2fDaniel Jasper return; 8873618e6fae8b734ad94221d941417c12d4bd1e3a8Daniel Jasper } 88829f123b2fa0435bb1962f0d9e9a2e660f35fbb2fDaniel Jasper 889b398701e4cbb9a55d90a60e3f4f4bc577446d098Manuel Klimek FormatToken *Start = Current; 890d489f8ceb735458b0e1f814e3f952b154f49c025Daniel Jasper FormatToken *LatestOperator = NULL; 89129f123b2fa0435bb1962f0d9e9a2e660f35fbb2fDaniel Jasper 892237d4c1c785be13656bff6c09e5b7ccd066ff5baDaniel Jasper while (Current) { 89329f123b2fa0435bb1962f0d9e9a2e660f35fbb2fDaniel Jasper // Consume operators with higher precedence. 894bf71ba2988b34c45af968f0965e28ac952e4a15fDaniel Jasper parse(Precedence + 1); 89529f123b2fa0435bb1962f0d9e9a2e660f35fbb2fDaniel Jasper 8963618e6fae8b734ad94221d941417c12d4bd1e3a8Daniel Jasper int CurrentPrecedence = getCurrentPrecedence(); 8973618e6fae8b734ad94221d941417c12d4bd1e3a8Daniel Jasper 8983618e6fae8b734ad94221d941417c12d4bd1e3a8Daniel Jasper if (Current && Current->Type == TT_ObjCSelectorName && 8993618e6fae8b734ad94221d941417c12d4bd1e3a8Daniel Jasper Precedence == CurrentPrecedence) 9003618e6fae8b734ad94221d941417c12d4bd1e3a8Daniel Jasper Start = Current; 901237d4c1c785be13656bff6c09e5b7ccd066ff5baDaniel Jasper 90229f123b2fa0435bb1962f0d9e9a2e660f35fbb2fDaniel Jasper // At the end of the line or when an operator with higher precedence is 90329f123b2fa0435bb1962f0d9e9a2e660f35fbb2fDaniel Jasper // found, insert fake parenthesis and return. 904ac3223e45e7d17c65b143439313800eef4fdf71dDaniel Jasper if (Current == NULL || Current->closesScope() || 905d489f8ceb735458b0e1f814e3f952b154f49c025Daniel Jasper (CurrentPrecedence != -1 && CurrentPrecedence < Precedence)) { 906d489f8ceb735458b0e1f814e3f952b154f49c025Daniel Jasper if (LatestOperator) { 907d489f8ceb735458b0e1f814e3f952b154f49c025Daniel Jasper if (Precedence == PrecedenceArrowAndPeriod) { 908d489f8ceb735458b0e1f814e3f952b154f49c025Daniel Jasper LatestOperator->LastInChainOfCalls = true; 909d489f8ceb735458b0e1f814e3f952b154f49c025Daniel Jasper // Call expressions don't have a binary operator precedence. 910d489f8ceb735458b0e1f814e3f952b154f49c025Daniel Jasper addFakeParenthesis(Start, prec::Unknown); 911d489f8ceb735458b0e1f814e3f952b154f49c025Daniel Jasper } else { 912d489f8ceb735458b0e1f814e3f952b154f49c025Daniel Jasper addFakeParenthesis(Start, prec::Level(Precedence)); 913d489f8ceb735458b0e1f814e3f952b154f49c025Daniel Jasper } 914d489f8ceb735458b0e1f814e3f952b154f49c025Daniel Jasper } 91529f123b2fa0435bb1962f0d9e9a2e660f35fbb2fDaniel Jasper return; 91629f123b2fa0435bb1962f0d9e9a2e660f35fbb2fDaniel Jasper } 91729f123b2fa0435bb1962f0d9e9a2e660f35fbb2fDaniel Jasper 91829f123b2fa0435bb1962f0d9e9a2e660f35fbb2fDaniel Jasper // Consume scopes: (), [], <> and {} 919ac3223e45e7d17c65b143439313800eef4fdf71dDaniel Jasper if (Current->opensScope()) { 920ac3223e45e7d17c65b143439313800eef4fdf71dDaniel Jasper while (Current && !Current->closesScope()) { 92129f123b2fa0435bb1962f0d9e9a2e660f35fbb2fDaniel Jasper next(); 92229f123b2fa0435bb1962f0d9e9a2e660f35fbb2fDaniel Jasper parse(); 92329f123b2fa0435bb1962f0d9e9a2e660f35fbb2fDaniel Jasper } 92429f123b2fa0435bb1962f0d9e9a2e660f35fbb2fDaniel Jasper next(); 92529f123b2fa0435bb1962f0d9e9a2e660f35fbb2fDaniel Jasper } else { 92629f123b2fa0435bb1962f0d9e9a2e660f35fbb2fDaniel Jasper // Operator found. 927237d4c1c785be13656bff6c09e5b7ccd066ff5baDaniel Jasper if (CurrentPrecedence == Precedence) 928d489f8ceb735458b0e1f814e3f952b154f49c025Daniel Jasper LatestOperator = Current; 92929f123b2fa0435bb1962f0d9e9a2e660f35fbb2fDaniel Jasper 93029f123b2fa0435bb1962f0d9e9a2e660f35fbb2fDaniel Jasper next(); 93129f123b2fa0435bb1962f0d9e9a2e660f35fbb2fDaniel Jasper } 93229f123b2fa0435bb1962f0d9e9a2e660f35fbb2fDaniel Jasper } 93329f123b2fa0435bb1962f0d9e9a2e660f35fbb2fDaniel Jasper } 93429f123b2fa0435bb1962f0d9e9a2e660f35fbb2fDaniel Jasper 93529f123b2fa0435bb1962f0d9e9a2e660f35fbb2fDaniel Jasperprivate: 9363618e6fae8b734ad94221d941417c12d4bd1e3a8Daniel Jasper /// \brief Gets the precedence (+1) of the given token for binary operators 9373618e6fae8b734ad94221d941417c12d4bd1e3a8Daniel Jasper /// and other tokens that we treat like binary operators. 9383618e6fae8b734ad94221d941417c12d4bd1e3a8Daniel Jasper int getCurrentPrecedence() { 9393618e6fae8b734ad94221d941417c12d4bd1e3a8Daniel Jasper if (Current) { 9403618e6fae8b734ad94221d941417c12d4bd1e3a8Daniel Jasper if (Current->Type == TT_ConditionalExpr) 941d489f8ceb735458b0e1f814e3f952b154f49c025Daniel Jasper return prec::Conditional; 9423618e6fae8b734ad94221d941417c12d4bd1e3a8Daniel Jasper else if (Current->is(tok::semi) || Current->Type == TT_InlineASMColon) 943d489f8ceb735458b0e1f814e3f952b154f49c025Daniel Jasper return 0; 9443618e6fae8b734ad94221d941417c12d4bd1e3a8Daniel Jasper else if (Current->Type == TT_BinaryOperator || Current->is(tok::comma)) 945d489f8ceb735458b0e1f814e3f952b154f49c025Daniel Jasper return Current->getPrecedence(); 9463618e6fae8b734ad94221d941417c12d4bd1e3a8Daniel Jasper else if (Current->Type == TT_ObjCSelectorName) 947d489f8ceb735458b0e1f814e3f952b154f49c025Daniel Jasper return prec::Assignment; 948d489f8ceb735458b0e1f814e3f952b154f49c025Daniel Jasper else if (Current->isOneOf(tok::period, tok::arrow)) 949d489f8ceb735458b0e1f814e3f952b154f49c025Daniel Jasper return PrecedenceArrowAndPeriod; 9503618e6fae8b734ad94221d941417c12d4bd1e3a8Daniel Jasper } 951d489f8ceb735458b0e1f814e3f952b154f49c025Daniel Jasper return -1; 9523618e6fae8b734ad94221d941417c12d4bd1e3a8Daniel Jasper } 9533618e6fae8b734ad94221d941417c12d4bd1e3a8Daniel Jasper 954c01897c3f6139304ed25e3f673bef77be209617fDaniel Jasper void addFakeParenthesis(FormatToken *Start, prec::Level Precedence) { 955c01897c3f6139304ed25e3f673bef77be209617fDaniel Jasper Start->FakeLParens.push_back(Precedence); 956db4813a3997fcf3864d1190f8021ef68e42cc057Daniel Jasper if (Precedence > prec::Unknown) 957db4813a3997fcf3864d1190f8021ef68e42cc057Daniel Jasper Start->StartsBinaryExpression = true; 958db4813a3997fcf3864d1190f8021ef68e42cc057Daniel Jasper if (Current) { 959c01897c3f6139304ed25e3f673bef77be209617fDaniel Jasper ++Current->Previous->FakeRParens; 960db4813a3997fcf3864d1190f8021ef68e42cc057Daniel Jasper if (Precedence > prec::Unknown) 961db4813a3997fcf3864d1190f8021ef68e42cc057Daniel Jasper Current->Previous->EndsBinaryExpression = true; 962db4813a3997fcf3864d1190f8021ef68e42cc057Daniel Jasper } 963c01897c3f6139304ed25e3f673bef77be209617fDaniel Jasper } 964c01897c3f6139304ed25e3f673bef77be209617fDaniel Jasper 9653618e6fae8b734ad94221d941417c12d4bd1e3a8Daniel Jasper /// \brief Parse unary operator expressions and surround them with fake 9663618e6fae8b734ad94221d941417c12d4bd1e3a8Daniel Jasper /// parentheses if appropriate. 9673618e6fae8b734ad94221d941417c12d4bd1e3a8Daniel Jasper void parseUnaryOperator() { 968d489f8ceb735458b0e1f814e3f952b154f49c025Daniel Jasper if (Current == NULL || Current->Type != TT_UnaryOperator) { 969d489f8ceb735458b0e1f814e3f952b154f49c025Daniel Jasper parse(PrecedenceArrowAndPeriod); 9703618e6fae8b734ad94221d941417c12d4bd1e3a8Daniel Jasper return; 971d489f8ceb735458b0e1f814e3f952b154f49c025Daniel Jasper } 9723618e6fae8b734ad94221d941417c12d4bd1e3a8Daniel Jasper 9733618e6fae8b734ad94221d941417c12d4bd1e3a8Daniel Jasper FormatToken *Start = Current; 9743618e6fae8b734ad94221d941417c12d4bd1e3a8Daniel Jasper next(); 975d489f8ceb735458b0e1f814e3f952b154f49c025Daniel Jasper parseUnaryOperator(); 9763618e6fae8b734ad94221d941417c12d4bd1e3a8Daniel Jasper 9773618e6fae8b734ad94221d941417c12d4bd1e3a8Daniel Jasper // The actual precedence doesn't matter. 978d489f8ceb735458b0e1f814e3f952b154f49c025Daniel Jasper addFakeParenthesis(Start, prec::Unknown); 9793618e6fae8b734ad94221d941417c12d4bd1e3a8Daniel Jasper } 9803618e6fae8b734ad94221d941417c12d4bd1e3a8Daniel Jasper 981c01897c3f6139304ed25e3f673bef77be209617fDaniel Jasper void parseConditionalExpr() { 982c01897c3f6139304ed25e3f673bef77be209617fDaniel Jasper FormatToken *Start = Current; 983d489f8ceb735458b0e1f814e3f952b154f49c025Daniel Jasper parse(prec::LogicalOr); 984c01897c3f6139304ed25e3f673bef77be209617fDaniel Jasper if (!Current || !Current->is(tok::question)) 985c01897c3f6139304ed25e3f673bef77be209617fDaniel Jasper return; 986c01897c3f6139304ed25e3f673bef77be209617fDaniel Jasper next(); 987d489f8ceb735458b0e1f814e3f952b154f49c025Daniel Jasper parse(prec::LogicalOr); 988c01897c3f6139304ed25e3f673bef77be209617fDaniel Jasper if (!Current || Current->Type != TT_ConditionalExpr) 989c01897c3f6139304ed25e3f673bef77be209617fDaniel Jasper return; 990c01897c3f6139304ed25e3f673bef77be209617fDaniel Jasper next(); 991c01897c3f6139304ed25e3f673bef77be209617fDaniel Jasper parseConditionalExpr(); 992c01897c3f6139304ed25e3f673bef77be209617fDaniel Jasper addFakeParenthesis(Start, prec::Conditional); 993c01897c3f6139304ed25e3f673bef77be209617fDaniel Jasper } 994c01897c3f6139304ed25e3f673bef77be209617fDaniel Jasper 99529f123b2fa0435bb1962f0d9e9a2e660f35fbb2fDaniel Jasper void next() { 996d71b15badeecdc049440103ef044f9cdf5e1359cAlexander Kornienko if (Current) 997d71b15badeecdc049440103ef044f9cdf5e1359cAlexander Kornienko Current = Current->Next; 998d71b15badeecdc049440103ef044f9cdf5e1359cAlexander Kornienko while (Current && Current->isTrailingComment()) 999b398701e4cbb9a55d90a60e3f4f4bc577446d098Manuel Klimek Current = Current->Next; 100029f123b2fa0435bb1962f0d9e9a2e660f35fbb2fDaniel Jasper } 100129f123b2fa0435bb1962f0d9e9a2e660f35fbb2fDaniel Jasper 1002b398701e4cbb9a55d90a60e3f4f4bc577446d098Manuel Klimek FormatToken *Current; 100329f123b2fa0435bb1962f0d9e9a2e660f35fbb2fDaniel Jasper}; 100429f123b2fa0435bb1962f0d9e9a2e660f35fbb2fDaniel Jasper 100514e66498781b7d81639bdc48716e09700552ac21Craig Topper} // end anonymous namespace 100614e66498781b7d81639bdc48716e09700552ac21Craig Topper 1007b77d741691a2775b5c31e29f021203cc659c26dfDaniel Jaspervoid 1008b77d741691a2775b5c31e29f021203cc659c26dfDaniel JasperTokenAnnotator::setCommentLineLevels(SmallVectorImpl<AnnotatedLine *> &Lines) { 1009b77d741691a2775b5c31e29f021203cc659c26dfDaniel Jasper if (Lines.empty()) 1010b77d741691a2775b5c31e29f021203cc659c26dfDaniel Jasper return; 1011b77d741691a2775b5c31e29f021203cc659c26dfDaniel Jasper 1012b77d741691a2775b5c31e29f021203cc659c26dfDaniel Jasper const AnnotatedLine *NextNonCommentLine = NULL; 1013b77d741691a2775b5c31e29f021203cc659c26dfDaniel Jasper for (unsigned i = Lines.size() - 1; i > 0; --i) { 1014b77d741691a2775b5c31e29f021203cc659c26dfDaniel Jasper if (NextNonCommentLine && Lines[i]->First->is(tok::comment) && 1015b77d741691a2775b5c31e29f021203cc659c26dfDaniel Jasper !Lines[i]->First->Next) 1016b77d741691a2775b5c31e29f021203cc659c26dfDaniel Jasper Lines[i]->Level = NextNonCommentLine->Level; 1017b77d741691a2775b5c31e29f021203cc659c26dfDaniel Jasper else 1018b77d741691a2775b5c31e29f021203cc659c26dfDaniel Jasper NextNonCommentLine = 1019b77d741691a2775b5c31e29f021203cc659c26dfDaniel Jasper Lines[i]->First->isNot(tok::r_brace) ? Lines[i] : NULL; 1020b77d741691a2775b5c31e29f021203cc659c26dfDaniel Jasper } 1021b77d741691a2775b5c31e29f021203cc659c26dfDaniel Jasper} 1022b77d741691a2775b5c31e29f021203cc659c26dfDaniel Jasper 10238ff690ab478b33e0d830a6203de12d191d94f8ffDaniel Jaspervoid TokenAnnotator::annotate(AnnotatedLine &Line) { 1024b77d741691a2775b5c31e29f021203cc659c26dfDaniel Jasper setCommentLineLevels(Line.Children); 1025b77d741691a2775b5c31e29f021203cc659c26dfDaniel Jasper for (SmallVectorImpl<AnnotatedLine *>::iterator I = Line.Children.begin(), 1026b77d741691a2775b5c31e29f021203cc659c26dfDaniel Jasper E = Line.Children.end(); 1027567dcf95424d69657f75e4bfd028967ca1f9eb8dDaniel Jasper I != E; ++I) { 1028567dcf95424d69657f75e4bfd028967ca1f9eb8dDaniel Jasper annotate(**I); 1029567dcf95424d69657f75e4bfd028967ca1f9eb8dDaniel Jasper } 1030d4a03dbb0bdbd4908375306283cb61813bd8f454Daniel Jasper AnnotatingParser Parser(Style, Line, Ident_in); 103132d28ee6061930b2a9c170cbaaeea028c88a7b89Daniel Jasper Line.Type = Parser.parseLine(); 103232d28ee6061930b2a9c170cbaaeea028c88a7b89Daniel Jasper if (Line.Type == LT_Invalid) 103332d28ee6061930b2a9c170cbaaeea028c88a7b89Daniel Jasper return; 103432d28ee6061930b2a9c170cbaaeea028c88a7b89Daniel Jasper 103529f123b2fa0435bb1962f0d9e9a2e660f35fbb2fDaniel Jasper ExpressionParser ExprParser(Line); 103629f123b2fa0435bb1962f0d9e9a2e660f35fbb2fDaniel Jasper ExprParser.parse(); 103729f123b2fa0435bb1962f0d9e9a2e660f35fbb2fDaniel Jasper 1038b398701e4cbb9a55d90a60e3f4f4bc577446d098Manuel Klimek if (Line.First->Type == TT_ObjCMethodSpecifier) 103932d28ee6061930b2a9c170cbaaeea028c88a7b89Daniel Jasper Line.Type = LT_ObjCMethodDecl; 1040b398701e4cbb9a55d90a60e3f4f4bc577446d098Manuel Klimek else if (Line.First->Type == TT_ObjCDecl) 104132d28ee6061930b2a9c170cbaaeea028c88a7b89Daniel Jasper Line.Type = LT_ObjCDecl; 1042b398701e4cbb9a55d90a60e3f4f4bc577446d098Manuel Klimek else if (Line.First->Type == TT_ObjCProperty) 104332d28ee6061930b2a9c170cbaaeea028c88a7b89Daniel Jasper Line.Type = LT_ObjCProperty; 104432d28ee6061930b2a9c170cbaaeea028c88a7b89Daniel Jasper 1045b398701e4cbb9a55d90a60e3f4f4bc577446d098Manuel Klimek Line.First->SpacesRequiredBefore = 1; 1046b398701e4cbb9a55d90a60e3f4f4bc577446d098Manuel Klimek Line.First->CanBreakBefore = Line.First->MustBreakBefore; 104732d28ee6061930b2a9c170cbaaeea028c88a7b89Daniel Jasper} 104832d28ee6061930b2a9c170cbaaeea028c88a7b89Daniel Jasper 10498ff690ab478b33e0d830a6203de12d191d94f8ffDaniel Jaspervoid TokenAnnotator::calculateFormattingInformation(AnnotatedLine &Line) { 105083a7dcdf5fce1bdf74ce985419d77a41a51abfa2Alexander Kornienko Line.First->TotalLength = 105183a7dcdf5fce1bdf74ce985419d77a41a51abfa2Alexander Kornienko Line.First->IsMultiline ? Style.ColumnLimit : Line.First->ColumnWidth; 1052b398701e4cbb9a55d90a60e3f4f4bc577446d098Manuel Klimek if (!Line.First->Next) 10538ff690ab478b33e0d830a6203de12d191d94f8ffDaniel Jasper return; 1054b398701e4cbb9a55d90a60e3f4f4bc577446d098Manuel Klimek FormatToken *Current = Line.First->Next; 10558ff690ab478b33e0d830a6203de12d191d94f8ffDaniel Jasper while (Current != NULL) { 1056729a743b317d877df3978e88a4a247d2edbf2090Daniel Jasper if (Current->Type == TT_LineComment) 1057729a743b317d877df3978e88a4a247d2edbf2090Daniel Jasper Current->SpacesRequiredBefore = Style.SpacesBeforeTrailingComments; 1058b18c258390f794d8803ef5ebbb56fb77bfea7ba4Alexander Kornienko else if (Current->SpacesRequiredBefore == 0 && 1059b18c258390f794d8803ef5ebbb56fb77bfea7ba4Alexander Kornienko spaceRequiredBefore(Line, *Current)) 1060b18c258390f794d8803ef5ebbb56fb77bfea7ba4Alexander Kornienko Current->SpacesRequiredBefore = 1; 10618ff690ab478b33e0d830a6203de12d191d94f8ffDaniel Jasper 1062ebaa1719fdd180e6dd9b4e6471e83500471a2f16Daniel Jasper Current->MustBreakBefore = 1063ebaa1719fdd180e6dd9b4e6471e83500471a2f16Daniel Jasper Current->MustBreakBefore || mustBreakBefore(Line, *Current); 1064ebaa1719fdd180e6dd9b4e6471e83500471a2f16Daniel Jasper 10658ff690ab478b33e0d830a6203de12d191d94f8ffDaniel Jasper Current->CanBreakBefore = 10668ff690ab478b33e0d830a6203de12d191d94f8ffDaniel Jasper Current->MustBreakBefore || canBreakBefore(Line, *Current); 1067567dcf95424d69657f75e4bfd028967ca1f9eb8dDaniel Jasper if (Current->MustBreakBefore || !Current->Children.empty() || 106883a7dcdf5fce1bdf74ce985419d77a41a51abfa2Alexander Kornienko Current->IsMultiline) 1069b398701e4cbb9a55d90a60e3f4f4bc577446d098Manuel Klimek Current->TotalLength = Current->Previous->TotalLength + Style.ColumnLimit; 10708ff690ab478b33e0d830a6203de12d191d94f8ffDaniel Jasper else 10712a409b62126d8f0b8f5749d5ed435ad2b394b526Daniel Jasper Current->TotalLength = Current->Previous->TotalLength + 107283a7dcdf5fce1bdf74ce985419d77a41a51abfa2Alexander Kornienko Current->ColumnWidth + 10732a409b62126d8f0b8f5749d5ed435ad2b394b526Daniel Jasper Current->SpacesRequiredBefore; 10748ff690ab478b33e0d830a6203de12d191d94f8ffDaniel Jasper // FIXME: Only calculate this if CanBreakBefore is true once static 10758ff690ab478b33e0d830a6203de12d191d94f8ffDaniel Jasper // initializers etc. are sorted out. 10768ff690ab478b33e0d830a6203de12d191d94f8ffDaniel Jasper // FIXME: Move magic numbers to a better place. 10778ff690ab478b33e0d830a6203de12d191d94f8ffDaniel Jasper Current->SplitPenalty = 10788ff690ab478b33e0d830a6203de12d191d94f8ffDaniel Jasper 20 * Current->BindingStrength + splitPenalty(Line, *Current); 10798ff690ab478b33e0d830a6203de12d191d94f8ffDaniel Jasper 1080b398701e4cbb9a55d90a60e3f4f4bc577446d098Manuel Klimek Current = Current->Next; 108132d28ee6061930b2a9c170cbaaeea028c88a7b89Daniel Jasper } 1082bf71ba2988b34c45af968f0965e28ac952e4a15fDaniel Jasper 1083e573c3f7fc40e813559ab4ff1e7eec4f66f1a50fManuel Klimek calculateUnbreakableTailLengths(Line); 1084d4a03dbb0bdbd4908375306283cb61813bd8f454Daniel Jasper for (Current = Line.First; Current != NULL; Current = Current->Next) { 1085d4a03dbb0bdbd4908375306283cb61813bd8f454Daniel Jasper if (Current->Role) 1086d4a03dbb0bdbd4908375306283cb61813bd8f454Daniel Jasper Current->Role->precomputeFormattingInfos(Current); 1087d4a03dbb0bdbd4908375306283cb61813bd8f454Daniel Jasper } 1088d4a03dbb0bdbd4908375306283cb61813bd8f454Daniel Jasper 1089567dcf95424d69657f75e4bfd028967ca1f9eb8dDaniel Jasper DEBUG({ printDebugInfo(Line); }); 1090567dcf95424d69657f75e4bfd028967ca1f9eb8dDaniel Jasper 1091b77d741691a2775b5c31e29f021203cc659c26dfDaniel Jasper for (SmallVectorImpl<AnnotatedLine *>::iterator I = Line.Children.begin(), 1092b77d741691a2775b5c31e29f021203cc659c26dfDaniel Jasper E = Line.Children.end(); 1093567dcf95424d69657f75e4bfd028967ca1f9eb8dDaniel Jasper I != E; ++I) { 1094567dcf95424d69657f75e4bfd028967ca1f9eb8dDaniel Jasper calculateFormattingInformation(**I); 1095567dcf95424d69657f75e4bfd028967ca1f9eb8dDaniel Jasper } 109632d28ee6061930b2a9c170cbaaeea028c88a7b89Daniel Jasper} 109732d28ee6061930b2a9c170cbaaeea028c88a7b89Daniel Jasper 1098e573c3f7fc40e813559ab4ff1e7eec4f66f1a50fManuel Klimekvoid TokenAnnotator::calculateUnbreakableTailLengths(AnnotatedLine &Line) { 1099e573c3f7fc40e813559ab4ff1e7eec4f66f1a50fManuel Klimek unsigned UnbreakableTailLength = 0; 1100b398701e4cbb9a55d90a60e3f4f4bc577446d098Manuel Klimek FormatToken *Current = Line.Last; 1101e573c3f7fc40e813559ab4ff1e7eec4f66f1a50fManuel Klimek while (Current != NULL) { 1102e573c3f7fc40e813559ab4ff1e7eec4f66f1a50fManuel Klimek Current->UnbreakableTailLength = UnbreakableTailLength; 1103e573c3f7fc40e813559ab4ff1e7eec4f66f1a50fManuel Klimek if (Current->CanBreakBefore || 1104e573c3f7fc40e813559ab4ff1e7eec4f66f1a50fManuel Klimek Current->isOneOf(tok::comment, tok::string_literal)) { 1105e573c3f7fc40e813559ab4ff1e7eec4f66f1a50fManuel Klimek UnbreakableTailLength = 0; 1106e573c3f7fc40e813559ab4ff1e7eec4f66f1a50fManuel Klimek } else { 1107e573c3f7fc40e813559ab4ff1e7eec4f66f1a50fManuel Klimek UnbreakableTailLength += 110883a7dcdf5fce1bdf74ce985419d77a41a51abfa2Alexander Kornienko Current->ColumnWidth + Current->SpacesRequiredBefore; 1109e573c3f7fc40e813559ab4ff1e7eec4f66f1a50fManuel Klimek } 1110b398701e4cbb9a55d90a60e3f4f4bc577446d098Manuel Klimek Current = Current->Previous; 1111e573c3f7fc40e813559ab4ff1e7eec4f66f1a50fManuel Klimek } 1112e573c3f7fc40e813559ab4ff1e7eec4f66f1a50fManuel Klimek} 1113e573c3f7fc40e813559ab4ff1e7eec4f66f1a50fManuel Klimek 11148ff690ab478b33e0d830a6203de12d191d94f8ffDaniel Jasperunsigned TokenAnnotator::splitPenalty(const AnnotatedLine &Line, 1115b398701e4cbb9a55d90a60e3f4f4bc577446d098Manuel Klimek const FormatToken &Tok) { 1116b398701e4cbb9a55d90a60e3f4f4bc577446d098Manuel Klimek const FormatToken &Left = *Tok.Previous; 1117b398701e4cbb9a55d90a60e3f4f4bc577446d098Manuel Klimek const FormatToken &Right = Tok; 111832d28ee6061930b2a9c170cbaaeea028c88a7b89Daniel Jasper 11195ad390d27f8f7cb6628bc2c32beba4d25bc2718bDaniel Jasper if (Left.is(tok::semi)) 11205ad390d27f8f7cb6628bc2c32beba4d25bc2718bDaniel Jasper return 0; 11215ad390d27f8f7cb6628bc2c32beba4d25bc2718bDaniel Jasper if (Left.is(tok::comma)) 11225ad390d27f8f7cb6628bc2c32beba4d25bc2718bDaniel Jasper return 1; 1123011c35dabb4c8abcb7389d8fbc6316f8f23576abDaniel Jasper if (Right.is(tok::l_square)) 1124011c35dabb4c8abcb7389d8fbc6316f8f23576abDaniel Jasper return 150; 11255ad390d27f8f7cb6628bc2c32beba4d25bc2718bDaniel Jasper 11266561f6a13b79ed752748ede590792191edf78ce8Daniel Jasper if (Right.Type == TT_StartOfName || Right.is(tok::kw_operator)) { 1127b398701e4cbb9a55d90a60e3f4f4bc577446d098Manuel Klimek if (Line.First->is(tok::kw_for) && Right.PartOfMultiVariableDeclStmt) 11283c08a818a6ac9115fe8880af9bbf5a0a87bdffaaDaniel Jasper return 3; 1129c18cff311118fc6a30929468fc82b2b35cbd7fbfDaniel Jasper if (Left.Type == TT_StartOfName) 1130c18cff311118fc6a30929468fc82b2b35cbd7fbfDaniel Jasper return 20; 113192495a8032d8624495a0ce769d5a6cdeba2dc8d8Daniel Jasper if (Line.MightBeFunctionDecl && Right.BindingStrength == 1) 11323c08a818a6ac9115fe8880af9bbf5a0a87bdffaaDaniel Jasper // FIXME: Clean up hack of using BindingStrength to find top-level names. 11333c08a818a6ac9115fe8880af9bbf5a0a87bdffaaDaniel Jasper return Style.PenaltyReturnTypeOnItsOwnLine; 113492495a8032d8624495a0ce769d5a6cdeba2dc8d8Daniel Jasper return 200; 11353c08a818a6ac9115fe8880af9bbf5a0a87bdffaaDaniel Jasper } 113632d28ee6061930b2a9c170cbaaeea028c88a7b89Daniel Jasper if (Left.is(tok::equal) && Right.is(tok::l_brace)) 113732d28ee6061930b2a9c170cbaaeea028c88a7b89Daniel Jasper return 150; 1138198c8bfee0b33ab0ed0c54224f460868f1c18e95Daniel Jasper if (Left.Type == TT_CastRParen) 1139198c8bfee0b33ab0ed0c54224f460868f1c18e95Daniel Jasper return 100; 114032d28ee6061930b2a9c170cbaaeea028c88a7b89Daniel Jasper if (Left.is(tok::coloncolon)) 114132d28ee6061930b2a9c170cbaaeea028c88a7b89Daniel Jasper return 500; 11426b119d63f5036344acd4e00a6ff2b3c72f26966fDaniel Jasper if (Left.isOneOf(tok::kw_class, tok::kw_struct)) 11436b119d63f5036344acd4e00a6ff2b3c72f26966fDaniel Jasper return 5000; 114432d28ee6061930b2a9c170cbaaeea028c88a7b89Daniel Jasper 11456cabab48dacc1317821f8f078ed2d4c603b67affDaniel Jasper if (Left.Type == TT_RangeBasedForLoopColon || 11466cabab48dacc1317821f8f078ed2d4c603b67affDaniel Jasper Left.Type == TT_InheritanceColon) 114784a1a63b034744b68a27ec171dca5b1b7cf303f0Daniel Jasper return 2; 114832d28ee6061930b2a9c170cbaaeea028c88a7b89Daniel Jasper 1149d3fef0fe26da19685bdec6a1bd844505f604d593Daniel Jasper if (Right.isMemberAccess()) { 11502f0a020d29286805e48b74b127fbf153af2c4ce7Daniel Jasper if (Left.isOneOf(tok::r_paren, tok::r_square) && Left.MatchingParen && 11512f0a020d29286805e48b74b127fbf153af2c4ce7Daniel Jasper Left.MatchingParen->ParameterCount > 0) 1152518ee34467c0722e253a58efea20456e96aa5802Daniel Jasper return 20; // Should be smaller than breaking at a nested comma. 115332d28ee6061930b2a9c170cbaaeea028c88a7b89Daniel Jasper return 150; 115432d28ee6061930b2a9c170cbaaeea028c88a7b89Daniel Jasper } 115532d28ee6061930b2a9c170cbaaeea028c88a7b89Daniel Jasper 115620a0f8cff96505abb65233a2eaf3af3cd8536cd2Daniel Jasper // Breaking before a trailing 'const' or not-function-like annotation is bad. 1157aa9e7b18a88d715b63e7b65d1b26a1759decc177Daniel Jasper if (Left.is(tok::r_paren) && Line.Type != LT_ObjCProperty && 115820a0f8cff96505abb65233a2eaf3af3cd8536cd2Daniel Jasper (Right.is(tok::kw_const) || (Right.is(tok::identifier) && Right.Next && 115920a0f8cff96505abb65233a2eaf3af3cd8536cd2Daniel Jasper Right.Next->isNot(tok::l_paren)))) 116053eb05ac6a00b95baf5680d9378ae8819dd09471Daniel Jasper return 100; 11615ad72bb8eb8e5cc4c061ccd28632295213d319dbDaniel Jasper 116232d28ee6061930b2a9c170cbaaeea028c88a7b89Daniel Jasper // In for-loops, prefer breaking at ',' and ';'. 1163b398701e4cbb9a55d90a60e3f4f4bc577446d098Manuel Klimek if (Line.First->is(tok::kw_for) && Left.is(tok::equal)) 11647d81281fc39f6d40d86be6600adba13c05b4a639Daniel Jasper return 4; 116532d28ee6061930b2a9c170cbaaeea028c88a7b89Daniel Jasper 116632d28ee6061930b2a9c170cbaaeea028c88a7b89Daniel Jasper // In Objective-C method expressions, prefer breaking before "param:" over 116732d28ee6061930b2a9c170cbaaeea028c88a7b89Daniel Jasper // breaking after it. 116863d7cedca8616921c1908c88c2f23fcd67bbab99Daniel Jasper if (Right.Type == TT_ObjCSelectorName) 116932d28ee6061930b2a9c170cbaaeea028c88a7b89Daniel Jasper return 0; 117063d7cedca8616921c1908c88c2f23fcd67bbab99Daniel Jasper if (Left.is(tok::colon) && Left.Type == TT_ObjCMethodExpr) 117132d28ee6061930b2a9c170cbaaeea028c88a7b89Daniel Jasper return 20; 117232d28ee6061930b2a9c170cbaaeea028c88a7b89Daniel Jasper 11731407bee187d7b964d5293ac8bf4f7a490c78cec6Daniel Jasper if (Left.is(tok::l_paren) && Line.MightBeFunctionDecl) 11741407bee187d7b964d5293ac8bf4f7a490c78cec6Daniel Jasper return 100; 1175ac3223e45e7d17c65b143439313800eef4fdf71dDaniel Jasper if (Left.opensScope()) 1176e60084d1e8a62716b80872f10e9828fbbb3cadfcDaniel Jasper return Left.ParameterCount > 1 ? prec::Comma : 19; 117732d28ee6061930b2a9c170cbaaeea028c88a7b89Daniel Jasper 11784e8a7b4a95962f63a938c0d21c9aca0a51b78345Daniel Jasper if (Right.is(tok::lessless)) { 11794e8a7b4a95962f63a938c0d21c9aca0a51b78345Daniel Jasper if (Left.is(tok::string_literal)) { 118000895106f9ed602af67984ec4d225a0cdc8c12afAlexander Kornienko StringRef Content = Left.TokenText; 118120376989402fb187c0bc48209f46560b9e402ea2Daniel Jasper if (Content.startswith("\"")) 118220376989402fb187c0bc48209f46560b9e402ea2Daniel Jasper Content = Content.drop_front(1); 118320376989402fb187c0bc48209f46560b9e402ea2Daniel Jasper if (Content.endswith("\"")) 118420376989402fb187c0bc48209f46560b9e402ea2Daniel Jasper Content = Content.drop_back(1); 118520376989402fb187c0bc48209f46560b9e402ea2Daniel Jasper Content = Content.trim(); 1186bfa1edd8247b80e951a570ff2486fe5fa9898c41Daniel Jasper if (Content.size() > 1 && 1187bfa1edd8247b80e951a570ff2486fe5fa9898c41Daniel Jasper (Content.back() == ':' || Content.back() == '=')) 11889637dda705e39110bfff66742542a58dd2470ad2Daniel Jasper return 25; 11894e8a7b4a95962f63a938c0d21c9aca0a51b78345Daniel Jasper } 11900c368787d9d1f92a3408b71b3f074a06edaa6bdeDaniel Jasper return 1; // Breaking at a << is really cheap. 11914e8a7b4a95962f63a938c0d21c9aca0a51b78345Daniel Jasper } 119232d28ee6061930b2a9c170cbaaeea028c88a7b89Daniel Jasper if (Left.Type == TT_ConditionalExpr) 1193518ee34467c0722e253a58efea20456e96aa5802Daniel Jasper return prec::Conditional; 1194b398701e4cbb9a55d90a60e3f4f4bc577446d098Manuel Klimek prec::Level Level = Left.getPrecedence(); 119532d28ee6061930b2a9c170cbaaeea028c88a7b89Daniel Jasper 119632d28ee6061930b2a9c170cbaaeea028c88a7b89Daniel Jasper if (Level != prec::Unknown) 119732d28ee6061930b2a9c170cbaaeea028c88a7b89Daniel Jasper return Level; 1198248497199bc56e86d1c089beb9529f3b3d77abb1Daniel Jasper 119932d28ee6061930b2a9c170cbaaeea028c88a7b89Daniel Jasper return 3; 120032d28ee6061930b2a9c170cbaaeea028c88a7b89Daniel Jasper} 120132d28ee6061930b2a9c170cbaaeea028c88a7b89Daniel Jasper 12028ff690ab478b33e0d830a6203de12d191d94f8ffDaniel Jasperbool TokenAnnotator::spaceRequiredBetween(const AnnotatedLine &Line, 1203b398701e4cbb9a55d90a60e3f4f4bc577446d098Manuel Klimek const FormatToken &Left, 1204b398701e4cbb9a55d90a60e3f4f4bc577446d098Manuel Klimek const FormatToken &Right) { 120532d28ee6061930b2a9c170cbaaeea028c88a7b89Daniel Jasper if (Right.is(tok::hashhash)) 120632d28ee6061930b2a9c170cbaaeea028c88a7b89Daniel Jasper return Left.is(tok::hash); 1207e74de28ec3692986f3467a7f160a0e293277fa7eAlexander Kornienko if (Left.isOneOf(tok::hashhash, tok::hash)) 120832d28ee6061930b2a9c170cbaaeea028c88a7b89Daniel Jasper return Right.is(tok::hash); 12097df56bfcf7186f73c99564cd54216f07a8db7352Daniel Jasper if (Left.is(tok::l_paren) && Right.is(tok::r_paren)) 12107df56bfcf7186f73c99564cd54216f07a8db7352Daniel Jasper return Style.SpaceInEmptyParentheses; 12117df56bfcf7186f73c99564cd54216f07a8db7352Daniel Jasper if (Left.is(tok::l_paren) || Right.is(tok::r_paren)) 121234f3d05d0ee625dfcac951e2851f212c4c1a8b83Daniel Jasper return (Right.Type == TT_CastRParen || 121334f3d05d0ee625dfcac951e2851f212c4c1a8b83Daniel Jasper (Left.MatchingParen && Left.MatchingParen->Type == TT_CastRParen)) 12147df56bfcf7186f73c99564cd54216f07a8db7352Daniel Jasper ? Style.SpacesInCStyleCastParentheses 12157df56bfcf7186f73c99564cd54216f07a8db7352Daniel Jasper : Style.SpacesInParentheses; 12167df56bfcf7186f73c99564cd54216f07a8db7352Daniel Jasper if (Right.isOneOf(tok::semi, tok::comma)) 121732d28ee6061930b2a9c170cbaaeea028c88a7b89Daniel Jasper return false; 121832d28ee6061930b2a9c170cbaaeea028c88a7b89Daniel Jasper if (Right.is(tok::less) && 121932d28ee6061930b2a9c170cbaaeea028c88a7b89Daniel Jasper (Left.is(tok::kw_template) || 122032d28ee6061930b2a9c170cbaaeea028c88a7b89Daniel Jasper (Line.Type == LT_ObjCDecl && Style.ObjCSpaceBeforeProtocolList))) 122132d28ee6061930b2a9c170cbaaeea028c88a7b89Daniel Jasper return true; 122232d28ee6061930b2a9c170cbaaeea028c88a7b89Daniel Jasper if (Left.is(tok::arrow) || Right.is(tok::arrow)) 122332d28ee6061930b2a9c170cbaaeea028c88a7b89Daniel Jasper return false; 1224e74de28ec3692986f3467a7f160a0e293277fa7eAlexander Kornienko if (Left.isOneOf(tok::exclaim, tok::tilde)) 122532d28ee6061930b2a9c170cbaaeea028c88a7b89Daniel Jasper return false; 122632d28ee6061930b2a9c170cbaaeea028c88a7b89Daniel Jasper if (Left.is(tok::at) && 1227e74de28ec3692986f3467a7f160a0e293277fa7eAlexander Kornienko Right.isOneOf(tok::identifier, tok::string_literal, tok::char_constant, 1228e74de28ec3692986f3467a7f160a0e293277fa7eAlexander Kornienko tok::numeric_constant, tok::l_paren, tok::l_brace, 1229e74de28ec3692986f3467a7f160a0e293277fa7eAlexander Kornienko tok::kw_true, tok::kw_false)) 123032d28ee6061930b2a9c170cbaaeea028c88a7b89Daniel Jasper return false; 123132d28ee6061930b2a9c170cbaaeea028c88a7b89Daniel Jasper if (Left.is(tok::coloncolon)) 123232d28ee6061930b2a9c170cbaaeea028c88a7b89Daniel Jasper return false; 123332d28ee6061930b2a9c170cbaaeea028c88a7b89Daniel Jasper if (Right.is(tok::coloncolon)) 123478a4e619e775b0dbaa10c9feaea0adf1d3dfe507Daniel Jasper return (Left.is(tok::less) && Style.Standard == FormatStyle::LS_Cpp03) || 123578a4e619e775b0dbaa10c9feaea0adf1d3dfe507Daniel Jasper !Left.isOneOf(tok::identifier, tok::greater, tok::l_paren, 123678a4e619e775b0dbaa10c9feaea0adf1d3dfe507Daniel Jasper tok::r_paren, tok::less); 1237e74de28ec3692986f3467a7f160a0e293277fa7eAlexander Kornienko if (Left.is(tok::less) || Right.isOneOf(tok::greater, tok::less)) 123832d28ee6061930b2a9c170cbaaeea028c88a7b89Daniel Jasper return false; 1239c47d7f1237b022eabbbdcebf77506e8a81aa54bdDaniel Jasper if (Right.is(tok::ellipsis)) 1240b3c887dcb70220eced72935725cd94d7e8325912Daniel Jasper return Left.Tok.isLiteral(); 124131e44f7a5d50ab8f7f623a7d2e18d5d877ef400dManuel Klimek if (Left.is(tok::l_square) && Right.is(tok::amp)) 124231e44f7a5d50ab8f7f623a7d2e18d5d877ef400dManuel Klimek return false; 12433fd9ccdd9f8d259bcf518e7056cfd419d992e984Alexander Kornienko if (Right.Type == TT_PointerOrReference) 1244b398701e4cbb9a55d90a60e3f4f4bc577446d098Manuel Klimek return Left.Tok.isLiteral() || 12453fd9ccdd9f8d259bcf518e7056cfd419d992e984Alexander Kornienko ((Left.Type != TT_PointerOrReference) && Left.isNot(tok::l_paren) && 12463fd9ccdd9f8d259bcf518e7056cfd419d992e984Alexander Kornienko !Style.PointerBindsToType); 12473ff4a2fea4aa6e5182b7799ccb4352e56961a212Daniel Jasper if (Right.Type == TT_FunctionTypeLParen && Left.isNot(tok::l_paren) && 1248395228fdc343df39c2507e414dc1406a185c6d37Daniel Jasper (Left.Type != TT_PointerOrReference || Style.PointerBindsToType)) 1249395228fdc343df39c2507e414dc1406a185c6d37Daniel Jasper return true; 12503fd9ccdd9f8d259bcf518e7056cfd419d992e984Alexander Kornienko if (Left.Type == TT_PointerOrReference) 12513a1847e0a1810a0b1b963182abc59114cc5ff53dDaniel Jasper return Right.Tok.isLiteral() || Right.Type == TT_BlockComment || 12529322aaee900b872c98f8fc10b38a231cb1e9b57aDaniel Jasper ((Right.Type != TT_PointerOrReference) && 125381d2d38d2d774a2550fa0d2efffa707e7a53b39cDaniel Jasper Right.isNot(tok::l_paren) && Style.PointerBindsToType && 1254b398701e4cbb9a55d90a60e3f4f4bc577446d098Manuel Klimek Left.Previous && 1255b398701e4cbb9a55d90a60e3f4f4bc577446d098Manuel Klimek !Left.Previous->isOneOf(tok::l_paren, tok::coloncolon)); 125632d28ee6061930b2a9c170cbaaeea028c88a7b89Daniel Jasper if (Right.is(tok::star) && Left.is(tok::l_paren)) 125732d28ee6061930b2a9c170cbaaeea028c88a7b89Daniel Jasper return false; 1258051860ee770bf83c3e66ab893be3642bb8bc2680Nico Weber if (Left.is(tok::l_square)) 1259051860ee770bf83c3e66ab893be3642bb8bc2680Nico Weber return Left.Type == TT_ObjCArrayLiteral && Right.isNot(tok::r_square); 1260051860ee770bf83c3e66ab893be3642bb8bc2680Nico Weber if (Right.is(tok::r_square)) 1261051860ee770bf83c3e66ab893be3642bb8bc2680Nico Weber return Right.Type == TT_ObjCArrayLiteral; 1262ec17226e82979592c16c7815d2368240201d18feDaniel Jasper if (Right.is(tok::l_square) && Right.Type != TT_ObjCMethodExpr && 1263567dcf95424d69657f75e4bfd028967ca1f9eb8dDaniel Jasper Right.Type != TT_LambdaLSquare && Left.isNot(tok::numeric_constant)) 126432d28ee6061930b2a9c170cbaaeea028c88a7b89Daniel Jasper return false; 126532d28ee6061930b2a9c170cbaaeea028c88a7b89Daniel Jasper if (Left.is(tok::colon)) 126632d28ee6061930b2a9c170cbaaeea028c88a7b89Daniel Jasper return Left.Type != TT_ObjCMethodExpr; 126732d28ee6061930b2a9c170cbaaeea028c88a7b89Daniel Jasper if (Right.is(tok::colon)) 1268ab3ce592d027e3a10fb21e703cab1507f8d9bb03Daniel Jasper return Right.Type != TT_ObjCMethodExpr && !Left.is(tok::question); 126932d28ee6061930b2a9c170cbaaeea028c88a7b89Daniel Jasper if (Right.is(tok::l_paren)) { 1270e0fa4c55a4bc6d2bbe0d9d657287c037158d5357Daniel Jasper if (Left.is(tok::r_paren) && Left.MatchingParen && 1271e0fa4c55a4bc6d2bbe0d9d657287c037158d5357Daniel Jasper Left.MatchingParen->Previous && 1272e0fa4c55a4bc6d2bbe0d9d657287c037158d5357Daniel Jasper Left.MatchingParen->Previous->is(tok::kw___attribute)) 1273e0fa4c55a4bc6d2bbe0d9d657287c037158d5357Daniel Jasper return true; 1274e74de28ec3692986f3467a7f160a0e293277fa7eAlexander Kornienko return Line.Type == LT_ObjCDecl || 127534f3d05d0ee625dfcac951e2851f212c4c1a8b83Daniel Jasper Left.isOneOf(tok::kw_return, tok::kw_new, tok::kw_delete, 127634f3d05d0ee625dfcac951e2851f212c4c1a8b83Daniel Jasper tok::semi) || 127734f3d05d0ee625dfcac951e2851f212c4c1a8b83Daniel Jasper (Style.SpaceAfterControlStatementKeyword && 127834f3d05d0ee625dfcac951e2851f212c4c1a8b83Daniel Jasper Left.isOneOf(tok::kw_if, tok::kw_for, tok::kw_while, tok::kw_switch, 127934f3d05d0ee625dfcac951e2851f212c4c1a8b83Daniel Jasper tok::kw_catch)); 128032d28ee6061930b2a9c170cbaaeea028c88a7b89Daniel Jasper } 1281b398701e4cbb9a55d90a60e3f4f4bc577446d098Manuel Klimek if (Left.is(tok::at) && Right.Tok.getObjCKeywordID() != tok::objc_not_keyword) 128232d28ee6061930b2a9c170cbaaeea028c88a7b89Daniel Jasper return false; 128332d28ee6061930b2a9c170cbaaeea028c88a7b89Daniel Jasper if (Left.is(tok::l_brace) && Right.is(tok::r_brace)) 1284567dcf95424d69657f75e4bfd028967ca1f9eb8dDaniel Jasper return !Left.Children.empty(); // No spaces in "{}". 12852424eefa6936ec2dc35188e19c99e2f85428b52eDaniel Jasper if (Left.is(tok::l_brace) || Right.is(tok::r_brace)) 1286b5dc3f4f53981b681a565cdf1d49f18e817541ffDaniel Jasper return !Style.Cpp11BracedListStyle; 12871bee0738b67b784f08d5e2f8351920260c9cfb1dDaniel Jasper if (Right.Type == TT_UnaryOperator) 12881bee0738b67b784f08d5e2f8351920260c9cfb1dDaniel Jasper return !Left.isOneOf(tok::l_paren, tok::l_square, tok::at) && 12891bee0738b67b784f08d5e2f8351920260c9cfb1dDaniel Jasper (Left.isNot(tok::colon) || Left.Type != TT_ObjCMethodExpr); 1290ce93356e2719d2992763ea747b65beada99f4c9bDaniel Jasper if (Left.isOneOf(tok::identifier, tok::greater, tok::r_square) && 129131e44f7a5d50ab8f7f623a7d2e18d5d877ef400dManuel Klimek Right.is(tok::l_brace) && Right.getNextNonComment() && 129231e44f7a5d50ab8f7f623a7d2e18d5d877ef400dManuel Klimek Right.BlockKind != BK_Block) 129332d28ee6061930b2a9c170cbaaeea028c88a7b89Daniel Jasper return false; 12945ad390d27f8f7cb6628bc2c32beba4d25bc2718bDaniel Jasper if (Left.is(tok::period) || Right.is(tok::period)) 12955ad390d27f8f7cb6628bc2c32beba4d25bc2718bDaniel Jasper return false; 1296861576b8019392f15c803ac14a4bc31fbd93aab2Nico Weber if (Left.Type == TT_BlockComment && Left.TokenText.endswith("=*/")) 1297861576b8019392f15c803ac14a4bc31fbd93aab2Nico Weber return false; 1298daa07e9ee76d438efa3c7e2c54b4d3d3ed19ea27Alexander Kornienko if (Right.is(tok::hash) && Left.is(tok::identifier) && Left.TokenText == "L") 1299daa07e9ee76d438efa3c7e2c54b4d3d3ed19ea27Alexander Kornienko return false; 130032d28ee6061930b2a9c170cbaaeea028c88a7b89Daniel Jasper return true; 130132d28ee6061930b2a9c170cbaaeea028c88a7b89Daniel Jasper} 130232d28ee6061930b2a9c170cbaaeea028c88a7b89Daniel Jasper 13038ff690ab478b33e0d830a6203de12d191d94f8ffDaniel Jasperbool TokenAnnotator::spaceRequiredBefore(const AnnotatedLine &Line, 1304b398701e4cbb9a55d90a60e3f4f4bc577446d098Manuel Klimek const FormatToken &Tok) { 1305b398701e4cbb9a55d90a60e3f4f4bc577446d098Manuel Klimek if (Tok.Tok.getIdentifierInfo() && Tok.Previous->Tok.getIdentifierInfo()) 13062b4c924f85e156d66c01b3f16c850892c47dcc7aDaniel Jasper return true; // Never ever merge two identifiers. 13071d82b1a33bcfe85f4834fb6920517ed07e9355d3Daniel Jasper if (Tok.Previous->Type == TT_ImplicitStringLiteral) 13081d82b1a33bcfe85f4834fb6920517ed07e9355d3Daniel Jasper return Tok.WhitespaceRange.getBegin() != Tok.WhitespaceRange.getEnd(); 130932d28ee6061930b2a9c170cbaaeea028c88a7b89Daniel Jasper if (Line.Type == LT_ObjCMethodDecl) { 1310b398701e4cbb9a55d90a60e3f4f4bc577446d098Manuel Klimek if (Tok.Previous->Type == TT_ObjCMethodSpecifier) 131132d28ee6061930b2a9c170cbaaeea028c88a7b89Daniel Jasper return true; 1312b398701e4cbb9a55d90a60e3f4f4bc577446d098Manuel Klimek if (Tok.Previous->is(tok::r_paren) && Tok.is(tok::identifier)) 131332d28ee6061930b2a9c170cbaaeea028c88a7b89Daniel Jasper // Don't space between ')' and <id> 131432d28ee6061930b2a9c170cbaaeea028c88a7b89Daniel Jasper return false; 131532d28ee6061930b2a9c170cbaaeea028c88a7b89Daniel Jasper } 131632d28ee6061930b2a9c170cbaaeea028c88a7b89Daniel Jasper if (Line.Type == LT_ObjCProperty && 1317b398701e4cbb9a55d90a60e3f4f4bc577446d098Manuel Klimek (Tok.is(tok::equal) || Tok.Previous->is(tok::equal))) 131832d28ee6061930b2a9c170cbaaeea028c88a7b89Daniel Jasper return false; 131932d28ee6061930b2a9c170cbaaeea028c88a7b89Daniel Jasper 13202ca3741a962ded08866596577aaee2f4ab74c955Daniel Jasper if (Tok.Type == TT_TrailingReturnArrow || 13212ca3741a962ded08866596577aaee2f4ab74c955Daniel Jasper Tok.Previous->Type == TT_TrailingReturnArrow) 13222ca3741a962ded08866596577aaee2f4ab74c955Daniel Jasper return true; 1323b398701e4cbb9a55d90a60e3f4f4bc577446d098Manuel Klimek if (Tok.Previous->is(tok::comma)) 132432d28ee6061930b2a9c170cbaaeea028c88a7b89Daniel Jasper return true; 13259c3c7b3130bc72b3f50703c11b85152b1264fc90Daniel Jasper if (Tok.is(tok::comma)) 13269c3c7b3130bc72b3f50703c11b85152b1264fc90Daniel Jasper return false; 132732d28ee6061930b2a9c170cbaaeea028c88a7b89Daniel Jasper if (Tok.Type == TT_CtorInitializerColon || Tok.Type == TT_ObjCBlockLParen) 132832d28ee6061930b2a9c170cbaaeea028c88a7b89Daniel Jasper return true; 1329b398701e4cbb9a55d90a60e3f4f4bc577446d098Manuel Klimek if (Tok.Previous->Tok.is(tok::kw_operator)) 13302b4c924f85e156d66c01b3f16c850892c47dcc7aDaniel Jasper return false; 13312b4c924f85e156d66c01b3f16c850892c47dcc7aDaniel Jasper if (Tok.Type == TT_OverloadedOperatorLParen) 133232d28ee6061930b2a9c170cbaaeea028c88a7b89Daniel Jasper return false; 133332d28ee6061930b2a9c170cbaaeea028c88a7b89Daniel Jasper if (Tok.is(tok::colon)) 1334b398701e4cbb9a55d90a60e3f4f4bc577446d098Manuel Klimek return !Line.First->isOneOf(tok::kw_case, tok::kw_default) && 1335ab3ce592d027e3a10fb21e703cab1507f8d9bb03Daniel Jasper Tok.getNextNonComment() != NULL && Tok.Type != TT_ObjCMethodExpr && 1336ab3ce592d027e3a10fb21e703cab1507f8d9bb03Daniel Jasper !Tok.Previous->is(tok::question); 1337b398701e4cbb9a55d90a60e3f4f4bc577446d098Manuel Klimek if (Tok.Previous->Type == TT_UnaryOperator || 1338b398701e4cbb9a55d90a60e3f4f4bc577446d098Manuel Klimek Tok.Previous->Type == TT_CastRParen) 133932d28ee6061930b2a9c170cbaaeea028c88a7b89Daniel Jasper return false; 1340b398701e4cbb9a55d90a60e3f4f4bc577446d098Manuel Klimek if (Tok.Previous->is(tok::greater) && Tok.is(tok::greater)) { 134129f123b2fa0435bb1962f0d9e9a2e660f35fbb2fDaniel Jasper return Tok.Type == TT_TemplateCloser && 1342b398701e4cbb9a55d90a60e3f4f4bc577446d098Manuel Klimek Tok.Previous->Type == TT_TemplateCloser && 134329f123b2fa0435bb1962f0d9e9a2e660f35fbb2fDaniel Jasper Style.Standard != FormatStyle::LS_Cpp11; 134432d28ee6061930b2a9c170cbaaeea028c88a7b89Daniel Jasper } 134554a38bd5cf243310290f34b43fc940a498a00f90Alexander Kornienko if (Tok.isOneOf(tok::arrowstar, tok::periodstar) || 1346b398701e4cbb9a55d90a60e3f4f4bc577446d098Manuel Klimek Tok.Previous->isOneOf(tok::arrowstar, tok::periodstar)) 13479c3c7b3130bc72b3f50703c11b85152b1264fc90Daniel Jasper return false; 13489b4de85e2f47a01974f451d21fed0276ff912e32Daniel Jasper if (!Style.SpaceBeforeAssignmentOperators && 13499b4de85e2f47a01974f451d21fed0276ff912e32Daniel Jasper Tok.getPrecedence() == prec::Assignment) 13509b4de85e2f47a01974f451d21fed0276ff912e32Daniel Jasper return false; 13511dc6f745eb19c94527503012d798dc9b9b5ba6daDaniel Jasper if ((Tok.Type == TT_BinaryOperator && !Tok.Previous->is(tok::l_paren)) || 13521dc6f745eb19c94527503012d798dc9b9b5ba6daDaniel Jasper Tok.Previous->Type == TT_BinaryOperator) 135332d28ee6061930b2a9c170cbaaeea028c88a7b89Daniel Jasper return true; 1354b398701e4cbb9a55d90a60e3f4f4bc577446d098Manuel Klimek if (Tok.Previous->Type == TT_TemplateCloser && Tok.is(tok::l_paren)) 135532d28ee6061930b2a9c170cbaaeea028c88a7b89Daniel Jasper return false; 1356a4dd982805e89a37a080350bf8de9069135c5a60Daniel Jasper if (Tok.is(tok::less) && Tok.Previous->isNot(tok::l_paren) && 1357a4dd982805e89a37a080350bf8de9069135c5a60Daniel Jasper Line.First->is(tok::hash)) 135832d28ee6061930b2a9c170cbaaeea028c88a7b89Daniel Jasper return true; 135932d28ee6061930b2a9c170cbaaeea028c88a7b89Daniel Jasper if (Tok.Type == TT_TrailingUnaryOperator) 136032d28ee6061930b2a9c170cbaaeea028c88a7b89Daniel Jasper return false; 1361b398701e4cbb9a55d90a60e3f4f4bc577446d098Manuel Klimek return spaceRequiredBetween(Line, *Tok.Previous, Tok); 136232d28ee6061930b2a9c170cbaaeea028c88a7b89Daniel Jasper} 136332d28ee6061930b2a9c170cbaaeea028c88a7b89Daniel Jasper 1364ebaa1719fdd180e6dd9b4e6471e83500471a2f16Daniel Jasperbool TokenAnnotator::mustBreakBefore(const AnnotatedLine &Line, 1365ebaa1719fdd180e6dd9b4e6471e83500471a2f16Daniel Jasper const FormatToken &Right) { 1366ebaa1719fdd180e6dd9b4e6471e83500471a2f16Daniel Jasper if (Right.is(tok::comment)) { 1367ebaa1719fdd180e6dd9b4e6471e83500471a2f16Daniel Jasper return Right.NewlinesBefore > 0; 1368ebaa1719fdd180e6dd9b4e6471e83500471a2f16Daniel Jasper } else if (Right.Previous->isTrailingComment() || 1369ebaa1719fdd180e6dd9b4e6471e83500471a2f16Daniel Jasper (Right.is(tok::string_literal) && 1370ebaa1719fdd180e6dd9b4e6471e83500471a2f16Daniel Jasper Right.Previous->is(tok::string_literal))) { 1371ebaa1719fdd180e6dd9b4e6471e83500471a2f16Daniel Jasper return true; 1372ebaa1719fdd180e6dd9b4e6471e83500471a2f16Daniel Jasper } else if (Right.Previous->IsUnterminatedLiteral) { 1373ebaa1719fdd180e6dd9b4e6471e83500471a2f16Daniel Jasper return true; 1374ebaa1719fdd180e6dd9b4e6471e83500471a2f16Daniel Jasper } else if (Right.is(tok::lessless) && Right.Next && 1375ebaa1719fdd180e6dd9b4e6471e83500471a2f16Daniel Jasper Right.Previous->is(tok::string_literal) && 1376ebaa1719fdd180e6dd9b4e6471e83500471a2f16Daniel Jasper Right.Next->is(tok::string_literal)) { 1377ebaa1719fdd180e6dd9b4e6471e83500471a2f16Daniel Jasper return true; 1378ebaa1719fdd180e6dd9b4e6471e83500471a2f16Daniel Jasper } else if (Right.Previous->ClosesTemplateDeclaration && 1379ebaa1719fdd180e6dd9b4e6471e83500471a2f16Daniel Jasper Right.Previous->MatchingParen && 1380ebaa1719fdd180e6dd9b4e6471e83500471a2f16Daniel Jasper Right.Previous->MatchingParen->BindingStrength == 1 && 1381ebaa1719fdd180e6dd9b4e6471e83500471a2f16Daniel Jasper Style.AlwaysBreakTemplateDeclarations) { 1382ebaa1719fdd180e6dd9b4e6471e83500471a2f16Daniel Jasper // FIXME: Fix horrible hack of using BindingStrength to find top-level <>. 1383ebaa1719fdd180e6dd9b4e6471e83500471a2f16Daniel Jasper return true; 1384ebaa1719fdd180e6dd9b4e6471e83500471a2f16Daniel Jasper } else if (Right.Type == TT_CtorInitializerComma && 138519ccb1227f5dc338d4b2d9dbbaeaa973c293f8d0Daniel Jasper Style.BreakConstructorInitializersBeforeComma && 138619ccb1227f5dc338d4b2d9dbbaeaa973c293f8d0Daniel Jasper !Style.ConstructorInitializerAllOnOneLineOrOnePerLine) { 1387ebaa1719fdd180e6dd9b4e6471e83500471a2f16Daniel Jasper return true; 1388ebaa1719fdd180e6dd9b4e6471e83500471a2f16Daniel Jasper } else if (Right.Previous->BlockKind == BK_Block && 138901fe9f9f320dd4342664376f24eb1a0d004d03c8Alexander Kornienko Right.Previous->isNot(tok::r_brace) && Right.isNot(tok::r_brace)) { 1390ebaa1719fdd180e6dd9b4e6471e83500471a2f16Daniel Jasper return true; 1391ebaa1719fdd180e6dd9b4e6471e83500471a2f16Daniel Jasper } else if (Right.is(tok::l_brace) && (Right.BlockKind == BK_Block)) { 1392ebaa1719fdd180e6dd9b4e6471e83500471a2f16Daniel Jasper return Style.BreakBeforeBraces == FormatStyle::BS_Allman; 1393ebaa1719fdd180e6dd9b4e6471e83500471a2f16Daniel Jasper } 1394ebaa1719fdd180e6dd9b4e6471e83500471a2f16Daniel Jasper return false; 1395ebaa1719fdd180e6dd9b4e6471e83500471a2f16Daniel Jasper} 1396ebaa1719fdd180e6dd9b4e6471e83500471a2f16Daniel Jasper 13978ff690ab478b33e0d830a6203de12d191d94f8ffDaniel Jasperbool TokenAnnotator::canBreakBefore(const AnnotatedLine &Line, 1398b398701e4cbb9a55d90a60e3f4f4bc577446d098Manuel Klimek const FormatToken &Right) { 1399b398701e4cbb9a55d90a60e3f4f4bc577446d098Manuel Klimek const FormatToken &Left = *Right.Previous; 14006561f6a13b79ed752748ede590792191edf78ce8Daniel Jasper if (Right.Type == TT_StartOfName || Right.is(tok::kw_operator)) 140132d28ee6061930b2a9c170cbaaeea028c88a7b89Daniel Jasper return true; 1402f2ff8126e9e9df368f31b1f968d8fc80f99809b3Nico Weber if (Right.is(tok::colon) && 1403f2ff8126e9e9df368f31b1f968d8fc80f99809b3Nico Weber (Right.Type == TT_ObjCDictLiteral || Right.Type == TT_ObjCMethodExpr)) 140432d28ee6061930b2a9c170cbaaeea028c88a7b89Daniel Jasper return false; 1405f2ff8126e9e9df368f31b1f968d8fc80f99809b3Nico Weber if (Left.is(tok::colon) && 1406f2ff8126e9e9df368f31b1f968d8fc80f99809b3Nico Weber (Left.Type == TT_ObjCDictLiteral || Left.Type == TT_ObjCMethodExpr)) 140732d28ee6061930b2a9c170cbaaeea028c88a7b89Daniel Jasper return true; 140863d7cedca8616921c1908c88c2f23fcd67bbab99Daniel Jasper if (Right.Type == TT_ObjCSelectorName) 140932d28ee6061930b2a9c170cbaaeea028c88a7b89Daniel Jasper return true; 1410aa9e7b18a88d715b63e7b65d1b26a1759decc177Daniel Jasper if (Left.is(tok::r_paren) && Line.Type == LT_ObjCProperty) 1411aa9e7b18a88d715b63e7b65d1b26a1759decc177Daniel Jasper return true; 141232d28ee6061930b2a9c170cbaaeea028c88a7b89Daniel Jasper if (Left.ClosesTemplateDeclaration) 141332d28ee6061930b2a9c170cbaaeea028c88a7b89Daniel Jasper return true; 1414ab3ce592d027e3a10fb21e703cab1507f8d9bb03Daniel Jasper if ((Right.Type == TT_ConditionalExpr && 1415ab3ce592d027e3a10fb21e703cab1507f8d9bb03Daniel Jasper !(Right.is(tok::colon) && Left.is(tok::question))) || 1416ab3ce592d027e3a10fb21e703cab1507f8d9bb03Daniel Jasper Right.is(tok::question)) 141732d28ee6061930b2a9c170cbaaeea028c88a7b89Daniel Jasper return true; 14186cabab48dacc1317821f8f078ed2d4c603b67affDaniel Jasper if (Right.Type == TT_RangeBasedForLoopColon || 1419c476ea976badd316e3afd0f34afe1f030a710117Daniel Jasper Right.Type == TT_OverloadedOperatorLParen || 1420c476ea976badd316e3afd0f34afe1f030a710117Daniel Jasper Right.Type == TT_OverloadedOperator) 14216cabab48dacc1317821f8f078ed2d4c603b67affDaniel Jasper return false; 1422c194c95036b7bf1281a6f2ed683f7c85ee5d2c20Daniel Jasper if (Left.Type == TT_RangeBasedForLoopColon) 142332d28ee6061930b2a9c170cbaaeea028c88a7b89Daniel Jasper return true; 14247d81281fc39f6d40d86be6600adba13c05b4a639Daniel Jasper if (Right.Type == TT_RangeBasedForLoopColon) 14257d81281fc39f6d40d86be6600adba13c05b4a639Daniel Jasper return false; 142632d28ee6061930b2a9c170cbaaeea028c88a7b89Daniel Jasper if (Left.Type == TT_PointerOrReference || Left.Type == TT_TemplateCloser || 142732d28ee6061930b2a9c170cbaaeea028c88a7b89Daniel Jasper Left.Type == TT_UnaryOperator || Left.Type == TT_ConditionalExpr || 1428e74de28ec3692986f3467a7f160a0e293277fa7eAlexander Kornienko Left.isOneOf(tok::question, tok::kw_operator)) 142932d28ee6061930b2a9c170cbaaeea028c88a7b89Daniel Jasper return false; 143032d28ee6061930b2a9c170cbaaeea028c88a7b89Daniel Jasper if (Left.is(tok::equal) && Line.Type == LT_VirtualFunctionDecl) 143132d28ee6061930b2a9c170cbaaeea028c88a7b89Daniel Jasper return false; 1432198c8bfee0b33ab0ed0c54224f460868f1c18e95Daniel Jasper if (Left.Previous) { 1433198c8bfee0b33ab0ed0c54224f460868f1c18e95Daniel Jasper if (Left.is(tok::l_paren) && Right.is(tok::l_paren) && 1434198c8bfee0b33ab0ed0c54224f460868f1c18e95Daniel Jasper Left.Previous->is(tok::kw___attribute)) 1435198c8bfee0b33ab0ed0c54224f460868f1c18e95Daniel Jasper return false; 14362ca3741a962ded08866596577aaee2f4ab74c955Daniel Jasper if (Left.is(tok::l_paren) && (Left.Previous->Type == TT_BinaryOperator || 14375e2169f94fca20bbfda317c222b156751a431c13Daniel Jasper Left.Previous->Type == TT_CastRParen)) 1438198c8bfee0b33ab0ed0c54224f460868f1c18e95Daniel Jasper return false; 1439198c8bfee0b33ab0ed0c54224f460868f1c18e95Daniel Jasper } 144032d28ee6061930b2a9c170cbaaeea028c88a7b89Daniel Jasper 144165d2c3829494d254039683c73f95843c29c661b4Daniel Jasper if (Right.isTrailingComment()) 144232d28ee6061930b2a9c170cbaaeea028c88a7b89Daniel Jasper // We rely on MustBreakBefore being set correctly here as we should not 144332d28ee6061930b2a9c170cbaaeea028c88a7b89Daniel Jasper // change the "binding" behavior of a comment. 144432d28ee6061930b2a9c170cbaaeea028c88a7b89Daniel Jasper return false; 144532d28ee6061930b2a9c170cbaaeea028c88a7b89Daniel Jasper 1446567dcf95424d69657f75e4bfd028967ca1f9eb8dDaniel Jasper if (Right.is(tok::r_paren) || Right.Type == TT_TemplateCloser) 1447567dcf95424d69657f75e4bfd028967ca1f9eb8dDaniel Jasper return false; 1448567dcf95424d69657f75e4bfd028967ca1f9eb8dDaniel Jasper 14495ad72bb8eb8e5cc4c061ccd28632295213d319dbDaniel Jasper // We only break before r_brace if there was a corresponding break before 14505ad72bb8eb8e5cc4c061ccd28632295213d319dbDaniel Jasper // the l_brace, which is tracked by BreakBeforeClosingBrace. 1451567dcf95424d69657f75e4bfd028967ca1f9eb8dDaniel Jasper if (Right.is(tok::r_brace)) 1452567dcf95424d69657f75e4bfd028967ca1f9eb8dDaniel Jasper return Right.MatchingParen && Right.MatchingParen->BlockKind == BK_Block; 14535ad72bb8eb8e5cc4c061ccd28632295213d319dbDaniel Jasper 145432d28ee6061930b2a9c170cbaaeea028c88a7b89Daniel Jasper // Allow breaking after a trailing 'const', e.g. after a method declaration, 145532d28ee6061930b2a9c170cbaaeea028c88a7b89Daniel Jasper // unless it is follow by ';', '{' or '='. 1456b398701e4cbb9a55d90a60e3f4f4bc577446d098Manuel Klimek if (Left.is(tok::kw_const) && Left.Previous != NULL && 1457b398701e4cbb9a55d90a60e3f4f4bc577446d098Manuel Klimek Left.Previous->is(tok::r_paren)) 1458e74de28ec3692986f3467a7f160a0e293277fa7eAlexander Kornienko return !Right.isOneOf(tok::l_brace, tok::semi, tok::equal); 145932d28ee6061930b2a9c170cbaaeea028c88a7b89Daniel Jasper 14608ef19a22956defa392df46c79e2d499ab7b16647Daniel Jasper if (Right.is(tok::kw___attribute)) 14618ef19a22956defa392df46c79e2d499ab7b16647Daniel Jasper return true; 14628ef19a22956defa392df46c79e2d499ab7b16647Daniel Jasper 14633a204418482c9ae70ad482e781132c54306c3aa6Daniel Jasper if (Left.is(tok::identifier) && Right.is(tok::string_literal)) 14643a204418482c9ae70ad482e781132c54306c3aa6Daniel Jasper return true; 1465e8b10d3d5b90efaf60ad89e96f6500f971ceec41Daniel Jasper 1466e8b10d3d5b90efaf60ad89e96f6500f971ceec41Daniel Jasper if (Left.Type == TT_CtorInitializerComma && 1467e8b10d3d5b90efaf60ad89e96f6500f971ceec41Daniel Jasper Style.BreakConstructorInitializersBeforeComma) 1468e8b10d3d5b90efaf60ad89e96f6500f971ceec41Daniel Jasper return false; 146919ccb1227f5dc338d4b2d9dbbaeaa973c293f8d0Daniel Jasper if (Right.Type == TT_CtorInitializerComma && 147019ccb1227f5dc338d4b2d9dbbaeaa973c293f8d0Daniel Jasper Style.BreakConstructorInitializersBeforeComma) 147119ccb1227f5dc338d4b2d9dbbaeaa973c293f8d0Daniel Jasper return true; 1472e8b10d3d5b90efaf60ad89e96f6500f971ceec41Daniel Jasper if (Right.isBinaryOperator() && Style.BreakBeforeBinaryOperators) 1473e8b10d3d5b90efaf60ad89e96f6500f971ceec41Daniel Jasper return true; 147426356ccf00f813cf358d420b55939fc737eb2cfaDaniel Jasper if (Left.is(tok::greater) && Right.is(tok::greater) && 147526356ccf00f813cf358d420b55939fc737eb2cfaDaniel Jasper Left.Type != TT_TemplateCloser) 147626356ccf00f813cf358d420b55939fc737eb2cfaDaniel Jasper return false; 1477e8b10d3d5b90efaf60ad89e96f6500f971ceec41Daniel Jasper return (Left.isBinaryOperator() && Left.isNot(tok::lessless) && 1478e8b10d3d5b90efaf60ad89e96f6500f971ceec41Daniel Jasper !Style.BreakBeforeBinaryOperators) || 14796b119d63f5036344acd4e00a6ff2b3c72f26966fDaniel Jasper Left.isOneOf(tok::comma, tok::coloncolon, tok::semi, tok::l_brace, 14806b119d63f5036344acd4e00a6ff2b3c72f26966fDaniel Jasper tok::kw_class, tok::kw_struct) || 1481e74de28ec3692986f3467a7f160a0e293277fa7eAlexander Kornienko Right.isOneOf(tok::lessless, tok::arrow, tok::period, tok::colon) || 1482198c8bfee0b33ab0ed0c54224f460868f1c18e95Daniel Jasper (Left.is(tok::r_paren) && 1483e033e87cbe77341777100093f8066167888d4440Daniel Jasper Right.isOneOf(tok::identifier, tok::kw_const, tok::kw___attribute)) || 148432d28ee6061930b2a9c170cbaaeea028c88a7b89Daniel Jasper (Left.is(tok::l_paren) && !Right.is(tok::r_paren)) || 1485011c35dabb4c8abcb7389d8fbc6316f8f23576abDaniel Jasper Right.is(tok::l_square); 148632d28ee6061930b2a9c170cbaaeea028c88a7b89Daniel Jasper} 148732d28ee6061930b2a9c170cbaaeea028c88a7b89Daniel Jasper 1488bf71ba2988b34c45af968f0965e28ac952e4a15fDaniel Jaspervoid TokenAnnotator::printDebugInfo(const AnnotatedLine &Line) { 1489bf71ba2988b34c45af968f0965e28ac952e4a15fDaniel Jasper llvm::errs() << "AnnotatedTokens:\n"; 1490b398701e4cbb9a55d90a60e3f4f4bc577446d098Manuel Klimek const FormatToken *Tok = Line.First; 1491bf71ba2988b34c45af968f0965e28ac952e4a15fDaniel Jasper while (Tok) { 1492b398701e4cbb9a55d90a60e3f4f4bc577446d098Manuel Klimek llvm::errs() << " M=" << Tok->MustBreakBefore 1493c7bd68f9edcbca95e882d0ab18d09371f0bdb82cDaniel Jasper << " C=" << Tok->CanBreakBefore << " T=" << Tok->Type 1494c7bd68f9edcbca95e882d0ab18d09371f0bdb82cDaniel Jasper << " S=" << Tok->SpacesRequiredBefore 1495c7bd68f9edcbca95e882d0ab18d09371f0bdb82cDaniel Jasper << " P=" << Tok->SplitPenalty << " Name=" << Tok->Tok.getName() 1496ae76f7f850a9101a20191b10241ca72c23dc40ddManuel Klimek << " L=" << Tok->TotalLength << " PPK=" << Tok->PackingKind 1497ae76f7f850a9101a20191b10241ca72c23dc40ddManuel Klimek << " FakeLParens="; 1498bf71ba2988b34c45af968f0965e28ac952e4a15fDaniel Jasper for (unsigned i = 0, e = Tok->FakeLParens.size(); i != e; ++i) 1499bf71ba2988b34c45af968f0965e28ac952e4a15fDaniel Jasper llvm::errs() << Tok->FakeLParens[i] << "/"; 1500bf71ba2988b34c45af968f0965e28ac952e4a15fDaniel Jasper llvm::errs() << " FakeRParens=" << Tok->FakeRParens << "\n"; 1501ae76f7f850a9101a20191b10241ca72c23dc40ddManuel Klimek if (Tok->Next == NULL) 1502ae76f7f850a9101a20191b10241ca72c23dc40ddManuel Klimek assert(Tok == Line.Last); 1503b398701e4cbb9a55d90a60e3f4f4bc577446d098Manuel Klimek Tok = Tok->Next; 1504bf71ba2988b34c45af968f0965e28ac952e4a15fDaniel Jasper } 1505bf71ba2988b34c45af968f0965e28ac952e4a15fDaniel Jasper llvm::errs() << "----\n"; 1506bf71ba2988b34c45af968f0965e28ac952e4a15fDaniel Jasper} 1507bf71ba2988b34c45af968f0965e28ac952e4a15fDaniel Jasper 150832d28ee6061930b2a9c170cbaaeea028c88a7b89Daniel Jasper} // namespace format 150932d28ee6061930b2a9c170cbaaeea028c88a7b89Daniel Jasper} // namespace clang 1510