TokenAnnotator.cpp revision ac2c974bc64d3767ad5e39451a874c50b3004b3a
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;
32900895106f9ed602af67984ec4d225a0cdc8c12afAlexander Kornienko        if (Tok->Previous->CodePointCount >
33000895106f9ed602af67984ec4d225a0cdc8c12afAlexander Kornienko            Contexts.back().LongestObjCSelectorName) {
33100895106f9ed602af67984ec4d225a0cdc8c12afAlexander Kornienko          Contexts.back().LongestObjCSelectorName =
33200895106f9ed602af67984ec4d225a0cdc8c12afAlexander Kornienko              Tok->Previous->CodePointCount;
33300895106f9ed602af67984ec4d225a0cdc8c12afAlexander Kornienko        }
3344e778091b0ff42895644ca8bef30f1a91ba6b32bDaniel Jasper        if (Contexts.back().FirstObjCSelectorName == NULL)
335b398701e4cbb9a55d90a60e3f4f4bc577446d098Manuel Klimek          Contexts.back().FirstObjCSelectorName = Tok->Previous;
3364e778091b0ff42895644ca8bef30f1a91ba6b32bDaniel Jasper      } else if (Contexts.back().ColonIsForRangeExpr) {
33732d28ee6061930b2a9c170cbaaeea028c88a7b89Daniel Jasper        Tok->Type = TT_RangeBasedForLoopColon;
3386cabab48dacc1317821f8f078ed2d4c603b67affDaniel Jasper      } else if (Contexts.size() == 1) {
3396cabab48dacc1317821f8f078ed2d4c603b67affDaniel Jasper        Tok->Type = TT_InheritanceColon;
340923ebef120a37122ce50722a85cbe42c0c2dab53Daniel Jasper      } else if (Contexts.back().ContextKind == tok::l_paren) {
341923ebef120a37122ce50722a85cbe42c0c2dab53Daniel Jasper        Tok->Type = TT_InlineASMColon;
34263d7cedca8616921c1908c88c2f23fcd67bbab99Daniel Jasper      }
34332d28ee6061930b2a9c170cbaaeea028c88a7b89Daniel Jasper      break;
34432d28ee6061930b2a9c170cbaaeea028c88a7b89Daniel Jasper    case tok::kw_if:
34532d28ee6061930b2a9c170cbaaeea028c88a7b89Daniel Jasper    case tok::kw_while:
34632d28ee6061930b2a9c170cbaaeea028c88a7b89Daniel Jasper      if (CurrentToken != NULL && CurrentToken->is(tok::l_paren)) {
34732d28ee6061930b2a9c170cbaaeea028c88a7b89Daniel Jasper        next();
3482726877196b41b922f10f794801b313980e1a8adNico Weber        if (!parseParens(/*LookForDecls=*/true))
34932d28ee6061930b2a9c170cbaaeea028c88a7b89Daniel Jasper          return false;
35032d28ee6061930b2a9c170cbaaeea028c88a7b89Daniel Jasper      }
35132d28ee6061930b2a9c170cbaaeea028c88a7b89Daniel Jasper      break;
35232d28ee6061930b2a9c170cbaaeea028c88a7b89Daniel Jasper    case tok::kw_for:
3534e778091b0ff42895644ca8bef30f1a91ba6b32bDaniel Jasper      Contexts.back().ColonIsForRangeExpr = true;
35432d28ee6061930b2a9c170cbaaeea028c88a7b89Daniel Jasper      next();
35532d28ee6061930b2a9c170cbaaeea028c88a7b89Daniel Jasper      if (!parseParens())
35632d28ee6061930b2a9c170cbaaeea028c88a7b89Daniel Jasper        return false;
35732d28ee6061930b2a9c170cbaaeea028c88a7b89Daniel Jasper      break;
35832d28ee6061930b2a9c170cbaaeea028c88a7b89Daniel Jasper    case tok::l_paren:
35932d28ee6061930b2a9c170cbaaeea028c88a7b89Daniel Jasper      if (!parseParens())
36032d28ee6061930b2a9c170cbaaeea028c88a7b89Daniel Jasper        return false;
3611407bee187d7b964d5293ac8bf4f7a490c78cec6Daniel Jasper      if (Line.MustBeDeclaration && NameFound && !Contexts.back().IsExpression)
3623c08a818a6ac9115fe8880af9bbf5a0a87bdffaaDaniel Jasper        Line.MightBeFunctionDecl = true;
36332d28ee6061930b2a9c170cbaaeea028c88a7b89Daniel Jasper      break;
36432d28ee6061930b2a9c170cbaaeea028c88a7b89Daniel Jasper    case tok::l_square:
36532d28ee6061930b2a9c170cbaaeea028c88a7b89Daniel Jasper      if (!parseSquare())
36632d28ee6061930b2a9c170cbaaeea028c88a7b89Daniel Jasper        return false;
36732d28ee6061930b2a9c170cbaaeea028c88a7b89Daniel Jasper      break;
36832d28ee6061930b2a9c170cbaaeea028c88a7b89Daniel Jasper    case tok::l_brace:
36932d28ee6061930b2a9c170cbaaeea028c88a7b89Daniel Jasper      if (!parseBrace())
37032d28ee6061930b2a9c170cbaaeea028c88a7b89Daniel Jasper        return false;
37132d28ee6061930b2a9c170cbaaeea028c88a7b89Daniel Jasper      break;
37232d28ee6061930b2a9c170cbaaeea028c88a7b89Daniel Jasper    case tok::less:
3730236dd09c040f2e6124654d620dde94a595c5ab0Daniel Jasper      if (Tok->Previous && !Tok->Previous->Tok.isLiteral() && parseAngle())
37432d28ee6061930b2a9c170cbaaeea028c88a7b89Daniel Jasper        Tok->Type = TT_TemplateOpener;
37532d28ee6061930b2a9c170cbaaeea028c88a7b89Daniel Jasper      else {
37632d28ee6061930b2a9c170cbaaeea028c88a7b89Daniel Jasper        Tok->Type = TT_BinaryOperator;
37732d28ee6061930b2a9c170cbaaeea028c88a7b89Daniel Jasper        CurrentToken = Tok;
37832d28ee6061930b2a9c170cbaaeea028c88a7b89Daniel Jasper        next();
37932d28ee6061930b2a9c170cbaaeea028c88a7b89Daniel Jasper      }
38032d28ee6061930b2a9c170cbaaeea028c88a7b89Daniel Jasper      break;
38132d28ee6061930b2a9c170cbaaeea028c88a7b89Daniel Jasper    case tok::r_paren:
38232d28ee6061930b2a9c170cbaaeea028c88a7b89Daniel Jasper    case tok::r_square:
38332d28ee6061930b2a9c170cbaaeea028c88a7b89Daniel Jasper      return false;
38432d28ee6061930b2a9c170cbaaeea028c88a7b89Daniel Jasper    case tok::r_brace:
38532d28ee6061930b2a9c170cbaaeea028c88a7b89Daniel Jasper      // Lines can start with '}'.
386b398701e4cbb9a55d90a60e3f4f4bc577446d098Manuel Klimek      if (Tok->Previous != NULL)
38732d28ee6061930b2a9c170cbaaeea028c88a7b89Daniel Jasper        return false;
38832d28ee6061930b2a9c170cbaaeea028c88a7b89Daniel Jasper      break;
38932d28ee6061930b2a9c170cbaaeea028c88a7b89Daniel Jasper    case tok::greater:
39032d28ee6061930b2a9c170cbaaeea028c88a7b89Daniel Jasper      Tok->Type = TT_BinaryOperator;
39132d28ee6061930b2a9c170cbaaeea028c88a7b89Daniel Jasper      break;
39232d28ee6061930b2a9c170cbaaeea028c88a7b89Daniel Jasper    case tok::kw_operator:
393174f60f005167984d00682d1d38a2927b9c04684Daniel Jasper      while (CurrentToken &&
394174f60f005167984d00682d1d38a2927b9c04684Daniel Jasper             !CurrentToken->isOneOf(tok::l_paren, tok::semi, tok::r_paren)) {
395e74de28ec3692986f3467a7f160a0e293277fa7eAlexander Kornienko        if (CurrentToken->isOneOf(tok::star, tok::amp))
3962b4c924f85e156d66c01b3f16c850892c47dcc7aDaniel Jasper          CurrentToken->Type = TT_PointerOrReference;
3972b4c924f85e156d66c01b3f16c850892c47dcc7aDaniel Jasper        consumeToken();
398174f60f005167984d00682d1d38a2927b9c04684Daniel Jasper        if (CurrentToken && CurrentToken->Previous->Type == TT_BinaryOperator)
399c476ea976badd316e3afd0f34afe1f030a710117Daniel Jasper          CurrentToken->Previous->Type = TT_OverloadedOperator;
40032d28ee6061930b2a9c170cbaaeea028c88a7b89Daniel Jasper      }
4016ea933c7e8f6988d5647af4a0eafd393a4c3685aDaniel Jasper      if (CurrentToken) {
4022b4c924f85e156d66c01b3f16c850892c47dcc7aDaniel Jasper        CurrentToken->Type = TT_OverloadedOperatorLParen;
403b398701e4cbb9a55d90a60e3f4f4bc577446d098Manuel Klimek        if (CurrentToken->Previous->Type == TT_BinaryOperator)
404b398701e4cbb9a55d90a60e3f4f4bc577446d098Manuel Klimek          CurrentToken->Previous->Type = TT_OverloadedOperator;
4056ea933c7e8f6988d5647af4a0eafd393a4c3685aDaniel Jasper      }
40632d28ee6061930b2a9c170cbaaeea028c88a7b89Daniel Jasper      break;
40732d28ee6061930b2a9c170cbaaeea028c88a7b89Daniel Jasper    case tok::question:
40832d28ee6061930b2a9c170cbaaeea028c88a7b89Daniel Jasper      parseConditional();
40932d28ee6061930b2a9c170cbaaeea028c88a7b89Daniel Jasper      break;
41032d28ee6061930b2a9c170cbaaeea028c88a7b89Daniel Jasper    case tok::kw_template:
41132d28ee6061930b2a9c170cbaaeea028c88a7b89Daniel Jasper      parseTemplateDeclaration();
41232d28ee6061930b2a9c170cbaaeea028c88a7b89Daniel Jasper      break;
413c2e6d2a4a7fe9dfa7d52a38c6048b7b18e6b591aNico Weber    case tok::identifier:
414b398701e4cbb9a55d90a60e3f4f4bc577446d098Manuel Klimek      if (Line.First->is(tok::kw_for) &&
415b398701e4cbb9a55d90a60e3f4f4bc577446d098Manuel Klimek          Tok->Tok.getIdentifierInfo() == &Ident_in)
416c2e6d2a4a7fe9dfa7d52a38c6048b7b18e6b591aNico Weber        Tok->Type = TT_ObjCForIn;
417c2e6d2a4a7fe9dfa7d52a38c6048b7b18e6b591aNico Weber      break;
4188ed9f2b25f082a1643ab5310f9eec33cf31a7577Daniel Jasper    case tok::comma:
4198ed9f2b25f082a1643ab5310f9eec33cf31a7577Daniel Jasper      if (Contexts.back().FirstStartOfName)
4208ed9f2b25f082a1643ab5310f9eec33cf31a7577Daniel Jasper        Contexts.back().FirstStartOfName->PartOfMultiVariableDeclStmt = true;
421e8b10d3d5b90efaf60ad89e96f6500f971ceec41Daniel Jasper      if (Contexts.back().InCtorInitializer)
422e8b10d3d5b90efaf60ad89e96f6500f971ceec41Daniel Jasper        Tok->Type = TT_CtorInitializerComma;
4238ed9f2b25f082a1643ab5310f9eec33cf31a7577Daniel Jasper      break;
42432d28ee6061930b2a9c170cbaaeea028c88a7b89Daniel Jasper    default:
42532d28ee6061930b2a9c170cbaaeea028c88a7b89Daniel Jasper      break;
42632d28ee6061930b2a9c170cbaaeea028c88a7b89Daniel Jasper    }
42732d28ee6061930b2a9c170cbaaeea028c88a7b89Daniel Jasper    return true;
42832d28ee6061930b2a9c170cbaaeea028c88a7b89Daniel Jasper  }
42932d28ee6061930b2a9c170cbaaeea028c88a7b89Daniel Jasper
43032d28ee6061930b2a9c170cbaaeea028c88a7b89Daniel Jasper  void parseIncludeDirective() {
43132d28ee6061930b2a9c170cbaaeea028c88a7b89Daniel Jasper    next();
43232d28ee6061930b2a9c170cbaaeea028c88a7b89Daniel Jasper    if (CurrentToken != NULL && CurrentToken->is(tok::less)) {
43332d28ee6061930b2a9c170cbaaeea028c88a7b89Daniel Jasper      next();
43432d28ee6061930b2a9c170cbaaeea028c88a7b89Daniel Jasper      while (CurrentToken != NULL) {
435b398701e4cbb9a55d90a60e3f4f4bc577446d098Manuel Klimek        if (CurrentToken->isNot(tok::comment) || CurrentToken->Next)
43632d28ee6061930b2a9c170cbaaeea028c88a7b89Daniel Jasper          CurrentToken->Type = TT_ImplicitStringLiteral;
43732d28ee6061930b2a9c170cbaaeea028c88a7b89Daniel Jasper        next();
43832d28ee6061930b2a9c170cbaaeea028c88a7b89Daniel Jasper      }
43932d28ee6061930b2a9c170cbaaeea028c88a7b89Daniel Jasper    } else {
44032d28ee6061930b2a9c170cbaaeea028c88a7b89Daniel Jasper      while (CurrentToken != NULL) {
4413a204418482c9ae70ad482e781132c54306c3aa6Daniel Jasper        if (CurrentToken->is(tok::string_literal))
4423a204418482c9ae70ad482e781132c54306c3aa6Daniel Jasper          // Mark these string literals as "implicit" literals, too, so that
4433a204418482c9ae70ad482e781132c54306c3aa6Daniel Jasper          // they are not split or line-wrapped.
4443a204418482c9ae70ad482e781132c54306c3aa6Daniel Jasper          CurrentToken->Type = TT_ImplicitStringLiteral;
44532d28ee6061930b2a9c170cbaaeea028c88a7b89Daniel Jasper        next();
44632d28ee6061930b2a9c170cbaaeea028c88a7b89Daniel Jasper      }
44732d28ee6061930b2a9c170cbaaeea028c88a7b89Daniel Jasper    }
44832d28ee6061930b2a9c170cbaaeea028c88a7b89Daniel Jasper  }
44932d28ee6061930b2a9c170cbaaeea028c88a7b89Daniel Jasper
45032d28ee6061930b2a9c170cbaaeea028c88a7b89Daniel Jasper  void parseWarningOrError() {
45132d28ee6061930b2a9c170cbaaeea028c88a7b89Daniel Jasper    next();
45232d28ee6061930b2a9c170cbaaeea028c88a7b89Daniel Jasper    // We still want to format the whitespace left of the first token of the
45332d28ee6061930b2a9c170cbaaeea028c88a7b89Daniel Jasper    // warning or error.
45432d28ee6061930b2a9c170cbaaeea028c88a7b89Daniel Jasper    next();
45532d28ee6061930b2a9c170cbaaeea028c88a7b89Daniel Jasper    while (CurrentToken != NULL) {
45632d28ee6061930b2a9c170cbaaeea028c88a7b89Daniel Jasper      CurrentToken->Type = TT_ImplicitStringLiteral;
45732d28ee6061930b2a9c170cbaaeea028c88a7b89Daniel Jasper      next();
45832d28ee6061930b2a9c170cbaaeea028c88a7b89Daniel Jasper    }
45932d28ee6061930b2a9c170cbaaeea028c88a7b89Daniel Jasper  }
46032d28ee6061930b2a9c170cbaaeea028c88a7b89Daniel Jasper
46132d28ee6061930b2a9c170cbaaeea028c88a7b89Daniel Jasper  void parsePreprocessorDirective() {
46232d28ee6061930b2a9c170cbaaeea028c88a7b89Daniel Jasper    next();
46332d28ee6061930b2a9c170cbaaeea028c88a7b89Daniel Jasper    if (CurrentToken == NULL)
46432d28ee6061930b2a9c170cbaaeea028c88a7b89Daniel Jasper      return;
46532d28ee6061930b2a9c170cbaaeea028c88a7b89Daniel Jasper    // Hashes in the middle of a line can lead to any strange token
46632d28ee6061930b2a9c170cbaaeea028c88a7b89Daniel Jasper    // sequence.
467b398701e4cbb9a55d90a60e3f4f4bc577446d098Manuel Klimek    if (CurrentToken->Tok.getIdentifierInfo() == NULL)
46832d28ee6061930b2a9c170cbaaeea028c88a7b89Daniel Jasper      return;
469b398701e4cbb9a55d90a60e3f4f4bc577446d098Manuel Klimek    switch (CurrentToken->Tok.getIdentifierInfo()->getPPKeywordID()) {
47032d28ee6061930b2a9c170cbaaeea028c88a7b89Daniel Jasper    case tok::pp_include:
47132d28ee6061930b2a9c170cbaaeea028c88a7b89Daniel Jasper    case tok::pp_import:
47232d28ee6061930b2a9c170cbaaeea028c88a7b89Daniel Jasper      parseIncludeDirective();
47332d28ee6061930b2a9c170cbaaeea028c88a7b89Daniel Jasper      break;
47432d28ee6061930b2a9c170cbaaeea028c88a7b89Daniel Jasper    case tok::pp_error:
47532d28ee6061930b2a9c170cbaaeea028c88a7b89Daniel Jasper    case tok::pp_warning:
47632d28ee6061930b2a9c170cbaaeea028c88a7b89Daniel Jasper      parseWarningOrError();
47732d28ee6061930b2a9c170cbaaeea028c88a7b89Daniel Jasper      break;
478aae7bad5cdd804cb88e918e9defbf5faf69f4cfdDaniel Jasper    case tok::pp_if:
479aae7bad5cdd804cb88e918e9defbf5faf69f4cfdDaniel Jasper    case tok::pp_elif:
480aae7bad5cdd804cb88e918e9defbf5faf69f4cfdDaniel Jasper      parseLine();
481aae7bad5cdd804cb88e918e9defbf5faf69f4cfdDaniel Jasper      break;
48232d28ee6061930b2a9c170cbaaeea028c88a7b89Daniel Jasper    default:
48332d28ee6061930b2a9c170cbaaeea028c88a7b89Daniel Jasper      break;
48432d28ee6061930b2a9c170cbaaeea028c88a7b89Daniel Jasper    }
4855b7e7b0ec77f49c1b24deffc9b7032ca16ca9f0dDaniel Jasper    while (CurrentToken != NULL)
4865b7e7b0ec77f49c1b24deffc9b7032ca16ca9f0dDaniel Jasper      next();
48732d28ee6061930b2a9c170cbaaeea028c88a7b89Daniel Jasper  }
48832d28ee6061930b2a9c170cbaaeea028c88a7b89Daniel Jasper
48995e8e468af284afb11ec1f994d0b5076fc7423d9Nico Weberpublic:
49032d28ee6061930b2a9c170cbaaeea028c88a7b89Daniel Jasper  LineType parseLine() {
49132d28ee6061930b2a9c170cbaaeea028c88a7b89Daniel Jasper    if (CurrentToken->is(tok::hash)) {
49232d28ee6061930b2a9c170cbaaeea028c88a7b89Daniel Jasper      parsePreprocessorDirective();
49332d28ee6061930b2a9c170cbaaeea028c88a7b89Daniel Jasper      return LT_PreprocessorDirective;
49432d28ee6061930b2a9c170cbaaeea028c88a7b89Daniel Jasper    }
49532d28ee6061930b2a9c170cbaaeea028c88a7b89Daniel Jasper    while (CurrentToken != NULL) {
49632d28ee6061930b2a9c170cbaaeea028c88a7b89Daniel Jasper      if (CurrentToken->is(tok::kw_virtual))
49732d28ee6061930b2a9c170cbaaeea028c88a7b89Daniel Jasper        KeywordVirtualFound = true;
49832d28ee6061930b2a9c170cbaaeea028c88a7b89Daniel Jasper      if (!consumeToken())
49932d28ee6061930b2a9c170cbaaeea028c88a7b89Daniel Jasper        return LT_Invalid;
50032d28ee6061930b2a9c170cbaaeea028c88a7b89Daniel Jasper    }
50132d28ee6061930b2a9c170cbaaeea028c88a7b89Daniel Jasper    if (KeywordVirtualFound)
50232d28ee6061930b2a9c170cbaaeea028c88a7b89Daniel Jasper      return LT_VirtualFunctionDecl;
50332d28ee6061930b2a9c170cbaaeea028c88a7b89Daniel Jasper
504b398701e4cbb9a55d90a60e3f4f4bc577446d098Manuel Klimek    if (Line.First->Type == TT_ObjCMethodSpecifier) {
5054e778091b0ff42895644ca8bef30f1a91ba6b32bDaniel Jasper      if (Contexts.back().FirstObjCSelectorName != NULL)
5064e778091b0ff42895644ca8bef30f1a91ba6b32bDaniel Jasper        Contexts.back().FirstObjCSelectorName->LongestObjCSelectorName =
5074e778091b0ff42895644ca8bef30f1a91ba6b32bDaniel Jasper            Contexts.back().LongestObjCSelectorName;
50863d7cedca8616921c1908c88c2f23fcd67bbab99Daniel Jasper      return LT_ObjCMethodDecl;
50963d7cedca8616921c1908c88c2f23fcd67bbab99Daniel Jasper    }
51063d7cedca8616921c1908c88c2f23fcd67bbab99Daniel Jasper
51132d28ee6061930b2a9c170cbaaeea028c88a7b89Daniel Jasper    return LT_Other;
51232d28ee6061930b2a9c170cbaaeea028c88a7b89Daniel Jasper  }
51332d28ee6061930b2a9c170cbaaeea028c88a7b89Daniel Jasper
51495e8e468af284afb11ec1f994d0b5076fc7423d9Nico Weberprivate:
51532d28ee6061930b2a9c170cbaaeea028c88a7b89Daniel Jasper  void next() {
5160178673f541685cf5067814dfeee2644078e39a9Daniel Jasper    if (CurrentToken != NULL) {
5170178673f541685cf5067814dfeee2644078e39a9Daniel Jasper      determineTokenType(*CurrentToken);
5184e778091b0ff42895644ca8bef30f1a91ba6b32bDaniel Jasper      CurrentToken->BindingStrength = Contexts.back().BindingStrength;
5190178673f541685cf5067814dfeee2644078e39a9Daniel Jasper    }
5200178673f541685cf5067814dfeee2644078e39a9Daniel Jasper
521b398701e4cbb9a55d90a60e3f4f4bc577446d098Manuel Klimek    if (CurrentToken != NULL)
522b398701e4cbb9a55d90a60e3f4f4bc577446d098Manuel Klimek      CurrentToken = CurrentToken->Next;
523d0f349be1422a123fdb28d6dd556f7300e6d51e9Daniel Jasper
524d0f349be1422a123fdb28d6dd556f7300e6d51e9Daniel Jasper    // Reset token type in case we have already looked at it and then recovered
525d0f349be1422a123fdb28d6dd556f7300e6d51e9Daniel Jasper    // from an error (e.g. failure to find the matching >).
526567dcf95424d69657f75e4bfd028967ca1f9eb8dDaniel Jasper    if (CurrentToken != NULL && CurrentToken->Type != TT_LambdaLSquare)
527d0f349be1422a123fdb28d6dd556f7300e6d51e9Daniel Jasper      CurrentToken->Type = TT_Unknown;
52832d28ee6061930b2a9c170cbaaeea028c88a7b89Daniel Jasper  }
52932d28ee6061930b2a9c170cbaaeea028c88a7b89Daniel Jasper
5304e778091b0ff42895644ca8bef30f1a91ba6b32bDaniel Jasper  /// \brief A struct to hold information valid in a specific context, e.g.
5314e778091b0ff42895644ca8bef30f1a91ba6b32bDaniel Jasper  /// a pair of parenthesis.
5324e778091b0ff42895644ca8bef30f1a91ba6b32bDaniel Jasper  struct Context {
533923ebef120a37122ce50722a85cbe42c0c2dab53Daniel Jasper    Context(tok::TokenKind ContextKind, unsigned BindingStrength,
534923ebef120a37122ce50722a85cbe42c0c2dab53Daniel Jasper            bool IsExpression)
535923ebef120a37122ce50722a85cbe42c0c2dab53Daniel Jasper        : ContextKind(ContextKind), BindingStrength(BindingStrength),
536923ebef120a37122ce50722a85cbe42c0c2dab53Daniel Jasper          LongestObjCSelectorName(0), ColonIsForRangeExpr(false),
537f2ff8126e9e9df368f31b1f968d8fc80f99809b3Nico Weber          ColonIsObjCDictLiteral(false), ColonIsObjCMethodExpr(false),
538f2ff8126e9e9df368f31b1f968d8fc80f99809b3Nico Weber          FirstObjCSelectorName(NULL), FirstStartOfName(NULL),
539e8b10d3d5b90efaf60ad89e96f6500f971ceec41Daniel Jasper          IsExpression(IsExpression), CanBeExpression(true),
540e8b10d3d5b90efaf60ad89e96f6500f971ceec41Daniel Jasper          InCtorInitializer(false) {}
541923ebef120a37122ce50722a85cbe42c0c2dab53Daniel Jasper
542923ebef120a37122ce50722a85cbe42c0c2dab53Daniel Jasper    tok::TokenKind ContextKind;
5434e778091b0ff42895644ca8bef30f1a91ba6b32bDaniel Jasper    unsigned BindingStrength;
5444e778091b0ff42895644ca8bef30f1a91ba6b32bDaniel Jasper    unsigned LongestObjCSelectorName;
5454e778091b0ff42895644ca8bef30f1a91ba6b32bDaniel Jasper    bool ColonIsForRangeExpr;
546f2ff8126e9e9df368f31b1f968d8fc80f99809b3Nico Weber    bool ColonIsObjCDictLiteral;
5474e778091b0ff42895644ca8bef30f1a91ba6b32bDaniel Jasper    bool ColonIsObjCMethodExpr;
548b398701e4cbb9a55d90a60e3f4f4bc577446d098Manuel Klimek    FormatToken *FirstObjCSelectorName;
549b398701e4cbb9a55d90a60e3f4f4bc577446d098Manuel Klimek    FormatToken *FirstStartOfName;
5504e778091b0ff42895644ca8bef30f1a91ba6b32bDaniel Jasper    bool IsExpression;
5516f21a988990ff5872822dcb049bd8fc65ce3d236Daniel Jasper    bool CanBeExpression;
552e8b10d3d5b90efaf60ad89e96f6500f971ceec41Daniel Jasper    bool InCtorInitializer;
5534e778091b0ff42895644ca8bef30f1a91ba6b32bDaniel Jasper  };
5544e778091b0ff42895644ca8bef30f1a91ba6b32bDaniel Jasper
5554e778091b0ff42895644ca8bef30f1a91ba6b32bDaniel Jasper  /// \brief Puts a new \c Context onto the stack \c Contexts for the lifetime
5564e778091b0ff42895644ca8bef30f1a91ba6b32bDaniel Jasper  /// of each instance.
5574e778091b0ff42895644ca8bef30f1a91ba6b32bDaniel Jasper  struct ScopedContextCreator {
5584e778091b0ff42895644ca8bef30f1a91ba6b32bDaniel Jasper    AnnotatingParser &P;
5594e778091b0ff42895644ca8bef30f1a91ba6b32bDaniel Jasper
560923ebef120a37122ce50722a85cbe42c0c2dab53Daniel Jasper    ScopedContextCreator(AnnotatingParser &P, tok::TokenKind ContextKind,
561923ebef120a37122ce50722a85cbe42c0c2dab53Daniel Jasper                         unsigned Increase)
562923ebef120a37122ce50722a85cbe42c0c2dab53Daniel Jasper        : P(P) {
5632a409b62126d8f0b8f5749d5ed435ad2b394b526Daniel Jasper      P.Contexts.push_back(Context(ContextKind,
5642a409b62126d8f0b8f5749d5ed435ad2b394b526Daniel Jasper                                   P.Contexts.back().BindingStrength + Increase,
5652a409b62126d8f0b8f5749d5ed435ad2b394b526Daniel Jasper                                   P.Contexts.back().IsExpression));
5664e778091b0ff42895644ca8bef30f1a91ba6b32bDaniel Jasper    }
5674e778091b0ff42895644ca8bef30f1a91ba6b32bDaniel Jasper
5684e778091b0ff42895644ca8bef30f1a91ba6b32bDaniel Jasper    ~ScopedContextCreator() { P.Contexts.pop_back(); }
5694e778091b0ff42895644ca8bef30f1a91ba6b32bDaniel Jasper  };
5700178673f541685cf5067814dfeee2644078e39a9Daniel Jasper
571b398701e4cbb9a55d90a60e3f4f4bc577446d098Manuel Klimek  void determineTokenType(FormatToken &Current) {
572b398701e4cbb9a55d90a60e3f4f4bc577446d098Manuel Klimek    if (Current.getPrecedence() == prec::Assignment &&
57353352600b7370b1d33b9fde1adda207fd9d7dcd1Daniel Jasper        !Line.First->isOneOf(tok::kw_template, tok::kw_using) &&
574b398701e4cbb9a55d90a60e3f4f4bc577446d098Manuel Klimek        (!Current.Previous || Current.Previous->isNot(tok::kw_operator))) {
5754e778091b0ff42895644ca8bef30f1a91ba6b32bDaniel Jasper      Contexts.back().IsExpression = true;
576b398701e4cbb9a55d90a60e3f4f4bc577446d098Manuel Klimek      for (FormatToken *Previous = Current.Previous;
57795e8e468af284afb11ec1f994d0b5076fc7423d9Nico Weber           Previous && Previous->isNot(tok::comma);
578b398701e4cbb9a55d90a60e3f4f4bc577446d098Manuel Klimek           Previous = Previous->Previous) {
5799c65b069821b7de79427e291b006293a0f55ff8fDaniel Jasper        if (Previous->is(tok::r_square))
5809c65b069821b7de79427e291b006293a0f55ff8fDaniel Jasper          Previous = Previous->MatchingParen;
5810178673f541685cf5067814dfeee2644078e39a9Daniel Jasper        if (Previous->Type == TT_BinaryOperator &&
582e74de28ec3692986f3467a7f160a0e293277fa7eAlexander Kornienko            Previous->isOneOf(tok::star, tok::amp)) {
5830178673f541685cf5067814dfeee2644078e39a9Daniel Jasper          Previous->Type = TT_PointerOrReference;
5840178673f541685cf5067814dfeee2644078e39a9Daniel Jasper        }
5850178673f541685cf5067814dfeee2644078e39a9Daniel Jasper      }
586e74de28ec3692986f3467a7f160a0e293277fa7eAlexander Kornienko    } else if (Current.isOneOf(tok::kw_return, tok::kw_throw) ||
58795e8e468af284afb11ec1f994d0b5076fc7423d9Nico Weber               (Current.is(tok::l_paren) && !Line.MustBeDeclaration &&
588378d93dcf7ec80661efc65642dc6266c9e13780aDaniel Jasper                !Line.InPPDirective &&
5892530fd5a235c1e57f8ebef2eae9b365042501009Manuel Klimek                (!Current.Previous ||
59053352600b7370b1d33b9fde1adda207fd9d7dcd1Daniel Jasper                 !Current.Previous->isOneOf(tok::kw_for, tok::kw_catch)))) {
5914e778091b0ff42895644ca8bef30f1a91ba6b32bDaniel Jasper      Contexts.back().IsExpression = true;
592e74de28ec3692986f3467a7f160a0e293277fa7eAlexander Kornienko    } else if (Current.isOneOf(tok::r_paren, tok::greater, tok::comma)) {
593b398701e4cbb9a55d90a60e3f4f4bc577446d098Manuel Klimek      for (FormatToken *Previous = Current.Previous;
594e74de28ec3692986f3467a7f160a0e293277fa7eAlexander Kornienko           Previous && Previous->isOneOf(tok::star, tok::amp);
595b398701e4cbb9a55d90a60e3f4f4bc577446d098Manuel Klimek           Previous = Previous->Previous)
59695e8e468af284afb11ec1f994d0b5076fc7423d9Nico Weber        Previous->Type = TT_PointerOrReference;
597b398701e4cbb9a55d90a60e3f4f4bc577446d098Manuel Klimek    } else if (Current.Previous &&
598b398701e4cbb9a55d90a60e3f4f4bc577446d098Manuel Klimek               Current.Previous->Type == TT_CtorInitializerColon) {
599d0f349be1422a123fdb28d6dd556f7300e6d51e9Daniel Jasper      Contexts.back().IsExpression = true;
600e8b10d3d5b90efaf60ad89e96f6500f971ceec41Daniel Jasper      Contexts.back().InCtorInitializer = true;
6016f21a988990ff5872822dcb049bd8fc65ce3d236Daniel Jasper    } else if (Current.is(tok::kw_new)) {
6026f21a988990ff5872822dcb049bd8fc65ce3d236Daniel Jasper      Contexts.back().CanBeExpression = false;
60316a69ef481f4580c571b6c693a5d3dd64ea56b53Daniel Jasper    } else if (Current.is(tok::semi)) {
60416a69ef481f4580c571b6c693a5d3dd64ea56b53Daniel Jasper      // This should be the condition or increment in a for-loop.
60516a69ef481f4580c571b6c693a5d3dd64ea56b53Daniel Jasper      Contexts.back().IsExpression = true;
60695e8e468af284afb11ec1f994d0b5076fc7423d9Nico Weber    }
6070178673f541685cf5067814dfeee2644078e39a9Daniel Jasper
6080178673f541685cf5067814dfeee2644078e39a9Daniel Jasper    if (Current.Type == TT_Unknown) {
6096ac431c7bae27a42351d2c2ad03dddd5b005ffd5Daniel Jasper      if (isStartOfName(Current)) {
6108ed9f2b25f082a1643ab5310f9eec33cf31a7577Daniel Jasper        Contexts.back().FirstStartOfName = &Current;
6113c08a818a6ac9115fe8880af9bbf5a0a87bdffaaDaniel Jasper        Current.Type = TT_StartOfName;
6121407bee187d7b964d5293ac8bf4f7a490c78cec6Daniel Jasper        NameFound = true;
6132ca3741a962ded08866596577aaee2f4ab74c955Daniel Jasper      } else if (Current.is(tok::kw_auto)) {
6142ca3741a962ded08866596577aaee2f4ab74c955Daniel Jasper        AutoFound = true;
6153262f4c10520cf11acea4cf590cfbf055924a41eDaniel Jasper      } else if (Current.is(tok::arrow) && AutoFound &&
6163262f4c10520cf11acea4cf590cfbf055924a41eDaniel Jasper                 Line.MustBeDeclaration) {
6172ca3741a962ded08866596577aaee2f4ab74c955Daniel Jasper        Current.Type = TT_TrailingReturnArrow;
618e74de28ec3692986f3467a7f160a0e293277fa7eAlexander Kornienko      } else if (Current.isOneOf(tok::star, tok::amp, tok::ampamp)) {
6194e778091b0ff42895644ca8bef30f1a91ba6b32bDaniel Jasper        Current.Type =
620d6104f6c34639ebe66f83d955c5f32ea4a50c266Daniel Jasper            determineStarAmpUsage(Current, Contexts.back().CanBeExpression &&
621d6104f6c34639ebe66f83d955c5f32ea4a50c266Daniel Jasper                                               Contexts.back().IsExpression);
622e74de28ec3692986f3467a7f160a0e293277fa7eAlexander Kornienko      } else if (Current.isOneOf(tok::minus, tok::plus, tok::caret)) {
6230178673f541685cf5067814dfeee2644078e39a9Daniel Jasper        Current.Type = determinePlusMinusCaretUsage(Current);
624e74de28ec3692986f3467a7f160a0e293277fa7eAlexander Kornienko      } else if (Current.isOneOf(tok::minusminus, tok::plusplus)) {
6250178673f541685cf5067814dfeee2644078e39a9Daniel Jasper        Current.Type = determineIncrementUsage(Current);
6260178673f541685cf5067814dfeee2644078e39a9Daniel Jasper      } else if (Current.is(tok::exclaim)) {
6270178673f541685cf5067814dfeee2644078e39a9Daniel Jasper        Current.Type = TT_UnaryOperator;
62831e44f7a5d50ab8f7f623a7d2e18d5d877ef400dManuel Klimek      } else if (Current.isBinaryOperator() &&
62931e44f7a5d50ab8f7f623a7d2e18d5d877ef400dManuel Klimek                 (!Current.Previous ||
63031e44f7a5d50ab8f7f623a7d2e18d5d877ef400dManuel Klimek                  Current.Previous->isNot(tok::l_square))) {
6310178673f541685cf5067814dfeee2644078e39a9Daniel Jasper        Current.Type = TT_BinaryOperator;
6320178673f541685cf5067814dfeee2644078e39a9Daniel Jasper      } else if (Current.is(tok::comment)) {
63300895106f9ed602af67984ec4d225a0cdc8c12afAlexander Kornienko        if (Current.TokenText.startswith("//"))
6340178673f541685cf5067814dfeee2644078e39a9Daniel Jasper          Current.Type = TT_LineComment;
6350178673f541685cf5067814dfeee2644078e39a9Daniel Jasper        else
6360178673f541685cf5067814dfeee2644078e39a9Daniel Jasper          Current.Type = TT_BlockComment;
63737d693160eba22343e08d7bcf66cd132ace77e5cNico Weber      } else if (Current.is(tok::r_paren)) {
638b8b4295b4ee161bfb76ff7b0ec1007bfd959553bDaniel Jasper        FormatToken *LeftOfParens = NULL;
639b8b4295b4ee161bfb76ff7b0ec1007bfd959553bDaniel Jasper        if (Current.MatchingParen)
6400bdc6434fa0fea933b6ab566eff751afdba40a2aAlexander Kornienko          LeftOfParens = Current.MatchingParen->getPreviousNonComment();
641b8b4295b4ee161bfb76ff7b0ec1007bfd959553bDaniel Jasper        bool IsCast = false;
642b8b4295b4ee161bfb76ff7b0ec1007bfd959553bDaniel Jasper        bool ParensAreEmpty = Current.Previous == Current.MatchingParen;
643b8b4295b4ee161bfb76ff7b0ec1007bfd959553bDaniel Jasper        bool ParensAreType = !Current.Previous ||
644b398701e4cbb9a55d90a60e3f4f4bc577446d098Manuel Klimek                             Current.Previous->Type == TT_PointerOrReference ||
645b8b4295b4ee161bfb76ff7b0ec1007bfd959553bDaniel Jasper                             Current.Previous->Type == TT_TemplateCloser ||
646b8b4295b4ee161bfb76ff7b0ec1007bfd959553bDaniel Jasper                             isSimpleTypeSpecifier(*Current.Previous);
64737d693160eba22343e08d7bcf66cd132ace77e5cNico Weber        bool ParensCouldEndDecl =
648b398701e4cbb9a55d90a60e3f4f4bc577446d098Manuel Klimek            Current.Next &&
649b398701e4cbb9a55d90a60e3f4f4bc577446d098Manuel Klimek            Current.Next->isOneOf(tok::equal, tok::semi, tok::l_brace);
6506a365aaa057a8c445d25344c0433726c752b3e7dDaniel Jasper        bool IsSizeOfOrAlignOf =
651b8b4295b4ee161bfb76ff7b0ec1007bfd959553bDaniel Jasper            LeftOfParens &&
652b8b4295b4ee161bfb76ff7b0ec1007bfd959553bDaniel Jasper            LeftOfParens->isOneOf(tok::kw_sizeof, tok::kw_alignof);
653b8b4295b4ee161bfb76ff7b0ec1007bfd959553bDaniel Jasper        if (ParensAreType && !ParensCouldEndDecl && !IsSizeOfOrAlignOf &&
6540c368787d9d1f92a3408b71b3f074a06edaa6bdeDaniel Jasper            (Contexts.back().IsExpression ||
6550c368787d9d1f92a3408b71b3f074a06edaa6bdeDaniel Jasper             (Current.Next && Current.Next->isBinaryOperator())))
656b8b4295b4ee161bfb76ff7b0ec1007bfd959553bDaniel Jasper          IsCast = true;
6572a409b62126d8f0b8f5749d5ed435ad2b394b526Daniel Jasper        if (Current.Next && Current.Next->isNot(tok::string_literal) &&
658b8b4295b4ee161bfb76ff7b0ec1007bfd959553bDaniel Jasper            (Current.Next->Tok.isLiteral() ||
659b8b4295b4ee161bfb76ff7b0ec1007bfd959553bDaniel Jasper             Current.Next->isOneOf(tok::kw_sizeof, tok::kw_alignof)))
660b8b4295b4ee161bfb76ff7b0ec1007bfd959553bDaniel Jasper          IsCast = true;
661b8b4295b4ee161bfb76ff7b0ec1007bfd959553bDaniel Jasper        // If there is an identifier after the (), it is likely a cast, unless
662b8b4295b4ee161bfb76ff7b0ec1007bfd959553bDaniel Jasper        // there is also an identifier before the ().
663ff1a2e519ebcd6d7a060eac7ba8aca37b2bf89d0Daniel Jasper        if (LeftOfParens && (LeftOfParens->Tok.getIdentifierInfo() == NULL ||
664ff1a2e519ebcd6d7a060eac7ba8aca37b2bf89d0Daniel Jasper                             LeftOfParens->is(tok::kw_return)) &&
665526df0f3a8d436e9084bd12118a2e119aa0bd724Daniel Jasper            LeftOfParens->Type != TT_OverloadedOperator &&
666465e8615a153ebd70eb27785af79f7e82e4a81d4Nico Weber            LeftOfParens->Type != TT_TemplateCloser && Current.Next &&
667465e8615a153ebd70eb27785af79f7e82e4a81d4Nico Weber            Current.Next->is(tok::identifier))
668b8b4295b4ee161bfb76ff7b0ec1007bfd959553bDaniel Jasper          IsCast = true;
669b8b4295b4ee161bfb76ff7b0ec1007bfd959553bDaniel Jasper        if (IsCast && !ParensAreEmpty)
67037d693160eba22343e08d7bcf66cd132ace77e5cNico Weber          Current.Type = TT_CastRParen;
671b398701e4cbb9a55d90a60e3f4f4bc577446d098Manuel Klimek      } else if (Current.is(tok::at) && Current.Next) {
672b398701e4cbb9a55d90a60e3f4f4bc577446d098Manuel Klimek        switch (Current.Next->Tok.getObjCKeywordID()) {
6730178673f541685cf5067814dfeee2644078e39a9Daniel Jasper        case tok::objc_interface:
6740178673f541685cf5067814dfeee2644078e39a9Daniel Jasper        case tok::objc_implementation:
6750178673f541685cf5067814dfeee2644078e39a9Daniel Jasper        case tok::objc_protocol:
6760178673f541685cf5067814dfeee2644078e39a9Daniel Jasper          Current.Type = TT_ObjCDecl;
6770178673f541685cf5067814dfeee2644078e39a9Daniel Jasper          break;
6780178673f541685cf5067814dfeee2644078e39a9Daniel Jasper        case tok::objc_property:
6790178673f541685cf5067814dfeee2644078e39a9Daniel Jasper          Current.Type = TT_ObjCProperty;
6800178673f541685cf5067814dfeee2644078e39a9Daniel Jasper          break;
6810178673f541685cf5067814dfeee2644078e39a9Daniel Jasper        default:
6820178673f541685cf5067814dfeee2644078e39a9Daniel Jasper          break;
6830178673f541685cf5067814dfeee2644078e39a9Daniel Jasper        }
6845ad390d27f8f7cb6628bc2c32beba4d25bc2718bDaniel Jasper      } else if (Current.is(tok::period)) {
6850bdc6434fa0fea933b6ab566eff751afdba40a2aAlexander Kornienko        FormatToken *PreviousNoComment = Current.getPreviousNonComment();
6865ad390d27f8f7cb6628bc2c32beba4d25bc2718bDaniel Jasper        if (PreviousNoComment &&
6875ad390d27f8f7cb6628bc2c32beba4d25bc2718bDaniel Jasper            PreviousNoComment->isOneOf(tok::comma, tok::l_brace))
6885ad390d27f8f7cb6628bc2c32beba4d25bc2718bDaniel Jasper          Current.Type = TT_DesignatedInitializerPeriod;
6890178673f541685cf5067814dfeee2644078e39a9Daniel Jasper      }
6900178673f541685cf5067814dfeee2644078e39a9Daniel Jasper    }
6910178673f541685cf5067814dfeee2644078e39a9Daniel Jasper  }
6920178673f541685cf5067814dfeee2644078e39a9Daniel Jasper
6936ac431c7bae27a42351d2c2ad03dddd5b005ffd5Daniel Jasper  /// \brief Take a guess at whether \p Tok starts a name of a function or
6946ac431c7bae27a42351d2c2ad03dddd5b005ffd5Daniel Jasper  /// variable declaration.
6956ac431c7bae27a42351d2c2ad03dddd5b005ffd5Daniel Jasper  ///
6966ac431c7bae27a42351d2c2ad03dddd5b005ffd5Daniel Jasper  /// This is a heuristic based on whether \p Tok is an identifier following
6976ac431c7bae27a42351d2c2ad03dddd5b005ffd5Daniel Jasper  /// something that is likely a type.
6986ac431c7bae27a42351d2c2ad03dddd5b005ffd5Daniel Jasper  bool isStartOfName(const FormatToken &Tok) {
6996ac431c7bae27a42351d2c2ad03dddd5b005ffd5Daniel Jasper    if (Tok.isNot(tok::identifier) || Tok.Previous == NULL)
7006ac431c7bae27a42351d2c2ad03dddd5b005ffd5Daniel Jasper      return false;
7016ac431c7bae27a42351d2c2ad03dddd5b005ffd5Daniel Jasper
7026ac431c7bae27a42351d2c2ad03dddd5b005ffd5Daniel Jasper    // Skip "const" as it does not have an influence on whether this is a name.
7036ac431c7bae27a42351d2c2ad03dddd5b005ffd5Daniel Jasper    FormatToken *PreviousNotConst = Tok.Previous;
7046ac431c7bae27a42351d2c2ad03dddd5b005ffd5Daniel Jasper    while (PreviousNotConst != NULL && PreviousNotConst->is(tok::kw_const))
7056ac431c7bae27a42351d2c2ad03dddd5b005ffd5Daniel Jasper      PreviousNotConst = PreviousNotConst->Previous;
7066ac431c7bae27a42351d2c2ad03dddd5b005ffd5Daniel Jasper
7076ac431c7bae27a42351d2c2ad03dddd5b005ffd5Daniel Jasper    if (PreviousNotConst == NULL)
7086ac431c7bae27a42351d2c2ad03dddd5b005ffd5Daniel Jasper      return false;
7096ac431c7bae27a42351d2c2ad03dddd5b005ffd5Daniel Jasper
7102a409b62126d8f0b8f5749d5ed435ad2b394b526Daniel Jasper    bool IsPPKeyword = PreviousNotConst->is(tok::identifier) &&
7112a409b62126d8f0b8f5749d5ed435ad2b394b526Daniel Jasper                       PreviousNotConst->Previous &&
7122a409b62126d8f0b8f5749d5ed435ad2b394b526Daniel Jasper                       PreviousNotConst->Previous->is(tok::hash);
7136ac431c7bae27a42351d2c2ad03dddd5b005ffd5Daniel Jasper
71492495a8032d8624495a0ce769d5a6cdeba2dc8d8Daniel Jasper    if (PreviousNotConst->Type == TT_TemplateCloser)
71592495a8032d8624495a0ce769d5a6cdeba2dc8d8Daniel Jasper      return PreviousNotConst && PreviousNotConst->MatchingParen &&
71692495a8032d8624495a0ce769d5a6cdeba2dc8d8Daniel Jasper             PreviousNotConst->MatchingParen->Previous &&
71792495a8032d8624495a0ce769d5a6cdeba2dc8d8Daniel Jasper             PreviousNotConst->MatchingParen->Previous->isNot(tok::kw_template);
71892495a8032d8624495a0ce769d5a6cdeba2dc8d8Daniel Jasper
7196ac431c7bae27a42351d2c2ad03dddd5b005ffd5Daniel Jasper    return (!IsPPKeyword && PreviousNotConst->is(tok::identifier)) ||
7206ac431c7bae27a42351d2c2ad03dddd5b005ffd5Daniel Jasper           PreviousNotConst->Type == TT_PointerOrReference ||
7216ac431c7bae27a42351d2c2ad03dddd5b005ffd5Daniel Jasper           isSimpleTypeSpecifier(*PreviousNotConst);
7226ac431c7bae27a42351d2c2ad03dddd5b005ffd5Daniel Jasper  }
7236ac431c7bae27a42351d2c2ad03dddd5b005ffd5Daniel Jasper
7240178673f541685cf5067814dfeee2644078e39a9Daniel Jasper  /// \brief Return the type of the given token assuming it is * or &.
725b398701e4cbb9a55d90a60e3f4f4bc577446d098Manuel Klimek  TokenType determineStarAmpUsage(const FormatToken &Tok, bool IsExpression) {
7260bdc6434fa0fea933b6ab566eff751afdba40a2aAlexander Kornienko    const FormatToken *PrevToken = Tok.getPreviousNonComment();
7270178673f541685cf5067814dfeee2644078e39a9Daniel Jasper    if (PrevToken == NULL)
7280178673f541685cf5067814dfeee2644078e39a9Daniel Jasper      return TT_UnaryOperator;
7290178673f541685cf5067814dfeee2644078e39a9Daniel Jasper
7300bdc6434fa0fea933b6ab566eff751afdba40a2aAlexander Kornienko    const FormatToken *NextToken = Tok.getNextNonComment();
7310178673f541685cf5067814dfeee2644078e39a9Daniel Jasper    if (NextToken == NULL)
7320178673f541685cf5067814dfeee2644078e39a9Daniel Jasper      return TT_Unknown;
7330178673f541685cf5067814dfeee2644078e39a9Daniel Jasper
734431f5918281dd1f178bf838b7a47ab1d11c52961Daniel Jasper    if (PrevToken->is(tok::coloncolon) ||
735431f5918281dd1f178bf838b7a47ab1d11c52961Daniel Jasper        (PrevToken->is(tok::l_paren) && !IsExpression))
7368a5d7cd100ebfb8c6b353ee4ad5b14ab4105d32dDaniel Jasper      return TT_PointerOrReference;
7378a5d7cd100ebfb8c6b353ee4ad5b14ab4105d32dDaniel Jasper
738e74de28ec3692986f3467a7f160a0e293277fa7eAlexander Kornienko    if (PrevToken->isOneOf(tok::l_paren, tok::l_square, tok::l_brace,
739d3cf17b5f1fed43dbd0cd35c43d15139803c9c84Daniel Jasper                           tok::comma, tok::semi, tok::kw_return, tok::colon,
740dbef71ebc66fe5553db01eb8e95b696c3223e737Daniel Jasper                           tok::equal, tok::kw_delete) ||
741e74de28ec3692986f3467a7f160a0e293277fa7eAlexander Kornienko        PrevToken->Type == TT_BinaryOperator ||
7420178673f541685cf5067814dfeee2644078e39a9Daniel Jasper        PrevToken->Type == TT_UnaryOperator || PrevToken->Type == TT_CastRParen)
7430178673f541685cf5067814dfeee2644078e39a9Daniel Jasper      return TT_UnaryOperator;
7440178673f541685cf5067814dfeee2644078e39a9Daniel Jasper
745e8a97985f72b4e11435ff2107c0f11e925fb6d96Nico Weber    if (NextToken->is(tok::l_square))
746e8a97985f72b4e11435ff2107c0f11e925fb6d96Nico Weber      return TT_PointerOrReference;
747e8a97985f72b4e11435ff2107c0f11e925fb6d96Nico Weber
748b398701e4cbb9a55d90a60e3f4f4bc577446d098Manuel Klimek    if (PrevToken->Tok.isLiteral() ||
749e74de28ec3692986f3467a7f160a0e293277fa7eAlexander Kornienko        PrevToken->isOneOf(tok::r_paren, tok::r_square) ||
750b398701e4cbb9a55d90a60e3f4f4bc577446d098Manuel Klimek        NextToken->Tok.isLiteral() || NextToken->isUnaryOperator())
7510178673f541685cf5067814dfeee2644078e39a9Daniel Jasper      return TT_BinaryOperator;
7520178673f541685cf5067814dfeee2644078e39a9Daniel Jasper
7530178673f541685cf5067814dfeee2644078e39a9Daniel Jasper    // It is very unlikely that we are going to find a pointer or reference type
7540178673f541685cf5067814dfeee2644078e39a9Daniel Jasper    // definition on the RHS of an assignment.
7550178673f541685cf5067814dfeee2644078e39a9Daniel Jasper    if (IsExpression)
7560178673f541685cf5067814dfeee2644078e39a9Daniel Jasper      return TT_BinaryOperator;
7570178673f541685cf5067814dfeee2644078e39a9Daniel Jasper
7580178673f541685cf5067814dfeee2644078e39a9Daniel Jasper    return TT_PointerOrReference;
7590178673f541685cf5067814dfeee2644078e39a9Daniel Jasper  }
7600178673f541685cf5067814dfeee2644078e39a9Daniel Jasper
761b398701e4cbb9a55d90a60e3f4f4bc577446d098Manuel Klimek  TokenType determinePlusMinusCaretUsage(const FormatToken &Tok) {
7620bdc6434fa0fea933b6ab566eff751afdba40a2aAlexander Kornienko    const FormatToken *PrevToken = Tok.getPreviousNonComment();
763b8b4295b4ee161bfb76ff7b0ec1007bfd959553bDaniel Jasper    if (PrevToken == NULL || PrevToken->Type == TT_CastRParen)
7640178673f541685cf5067814dfeee2644078e39a9Daniel Jasper      return TT_UnaryOperator;
7650178673f541685cf5067814dfeee2644078e39a9Daniel Jasper
7660178673f541685cf5067814dfeee2644078e39a9Daniel Jasper    // Use heuristics to recognize unary operators.
767e74de28ec3692986f3467a7f160a0e293277fa7eAlexander Kornienko    if (PrevToken->isOneOf(tok::equal, tok::l_paren, tok::comma, tok::l_square,
768e74de28ec3692986f3467a7f160a0e293277fa7eAlexander Kornienko                           tok::question, tok::colon, tok::kw_return,
769e74de28ec3692986f3467a7f160a0e293277fa7eAlexander Kornienko                           tok::kw_case, tok::at, tok::l_brace))
7700178673f541685cf5067814dfeee2644078e39a9Daniel Jasper      return TT_UnaryOperator;
7710178673f541685cf5067814dfeee2644078e39a9Daniel Jasper
772ee0feec7362053f22b6c01d12e1bfa06fb3ac93fNico Weber    // There can't be two consecutive binary operators.
7730178673f541685cf5067814dfeee2644078e39a9Daniel Jasper    if (PrevToken->Type == TT_BinaryOperator)
7740178673f541685cf5067814dfeee2644078e39a9Daniel Jasper      return TT_UnaryOperator;
7750178673f541685cf5067814dfeee2644078e39a9Daniel Jasper
7760178673f541685cf5067814dfeee2644078e39a9Daniel Jasper    // Fall back to marking the token as binary operator.
7770178673f541685cf5067814dfeee2644078e39a9Daniel Jasper    return TT_BinaryOperator;
7780178673f541685cf5067814dfeee2644078e39a9Daniel Jasper  }
7790178673f541685cf5067814dfeee2644078e39a9Daniel Jasper
7800178673f541685cf5067814dfeee2644078e39a9Daniel Jasper  /// \brief Determine whether ++/-- are pre- or post-increments/-decrements.
781b398701e4cbb9a55d90a60e3f4f4bc577446d098Manuel Klimek  TokenType determineIncrementUsage(const FormatToken &Tok) {
7820bdc6434fa0fea933b6ab566eff751afdba40a2aAlexander Kornienko    const FormatToken *PrevToken = Tok.getPreviousNonComment();
783b8b4295b4ee161bfb76ff7b0ec1007bfd959553bDaniel Jasper    if (PrevToken == NULL || PrevToken->Type == TT_CastRParen)
7840178673f541685cf5067814dfeee2644078e39a9Daniel Jasper      return TT_UnaryOperator;
785e74de28ec3692986f3467a7f160a0e293277fa7eAlexander Kornienko    if (PrevToken->isOneOf(tok::r_paren, tok::r_square, tok::identifier))
7860178673f541685cf5067814dfeee2644078e39a9Daniel Jasper      return TT_TrailingUnaryOperator;
7870178673f541685cf5067814dfeee2644078e39a9Daniel Jasper
7880178673f541685cf5067814dfeee2644078e39a9Daniel Jasper    return TT_UnaryOperator;
7890178673f541685cf5067814dfeee2644078e39a9Daniel Jasper  }
7904e778091b0ff42895644ca8bef30f1a91ba6b32bDaniel Jasper
7918ed9f2b25f082a1643ab5310f9eec33cf31a7577Daniel Jasper  // FIXME: This is copy&pasted from Sema. Put it in a common place and remove
7928ed9f2b25f082a1643ab5310f9eec33cf31a7577Daniel Jasper  // duplication.
7938ed9f2b25f082a1643ab5310f9eec33cf31a7577Daniel Jasper  /// \brief Determine whether the token kind starts a simple-type-specifier.
794b398701e4cbb9a55d90a60e3f4f4bc577446d098Manuel Klimek  bool isSimpleTypeSpecifier(const FormatToken &Tok) const {
795b398701e4cbb9a55d90a60e3f4f4bc577446d098Manuel Klimek    switch (Tok.Tok.getKind()) {
7968ed9f2b25f082a1643ab5310f9eec33cf31a7577Daniel Jasper    case tok::kw_short:
7978ed9f2b25f082a1643ab5310f9eec33cf31a7577Daniel Jasper    case tok::kw_long:
7988ed9f2b25f082a1643ab5310f9eec33cf31a7577Daniel Jasper    case tok::kw___int64:
7998ed9f2b25f082a1643ab5310f9eec33cf31a7577Daniel Jasper    case tok::kw___int128:
8008ed9f2b25f082a1643ab5310f9eec33cf31a7577Daniel Jasper    case tok::kw_signed:
8018ed9f2b25f082a1643ab5310f9eec33cf31a7577Daniel Jasper    case tok::kw_unsigned:
8028ed9f2b25f082a1643ab5310f9eec33cf31a7577Daniel Jasper    case tok::kw_void:
8038ed9f2b25f082a1643ab5310f9eec33cf31a7577Daniel Jasper    case tok::kw_char:
8048ed9f2b25f082a1643ab5310f9eec33cf31a7577Daniel Jasper    case tok::kw_int:
8058ed9f2b25f082a1643ab5310f9eec33cf31a7577Daniel Jasper    case tok::kw_half:
8068ed9f2b25f082a1643ab5310f9eec33cf31a7577Daniel Jasper    case tok::kw_float:
8078ed9f2b25f082a1643ab5310f9eec33cf31a7577Daniel Jasper    case tok::kw_double:
8088ed9f2b25f082a1643ab5310f9eec33cf31a7577Daniel Jasper    case tok::kw_wchar_t:
8098ed9f2b25f082a1643ab5310f9eec33cf31a7577Daniel Jasper    case tok::kw_bool:
8108ed9f2b25f082a1643ab5310f9eec33cf31a7577Daniel Jasper    case tok::kw___underlying_type:
8118ed9f2b25f082a1643ab5310f9eec33cf31a7577Daniel Jasper    case tok::annot_typename:
8128ed9f2b25f082a1643ab5310f9eec33cf31a7577Daniel Jasper    case tok::kw_char16_t:
8138ed9f2b25f082a1643ab5310f9eec33cf31a7577Daniel Jasper    case tok::kw_char32_t:
8148ed9f2b25f082a1643ab5310f9eec33cf31a7577Daniel Jasper    case tok::kw_typeof:
8158ed9f2b25f082a1643ab5310f9eec33cf31a7577Daniel Jasper    case tok::kw_decltype:
81600895106f9ed602af67984ec4d225a0cdc8c12afAlexander Kornienko      return true;
8178ed9f2b25f082a1643ab5310f9eec33cf31a7577Daniel Jasper    default:
81800895106f9ed602af67984ec4d225a0cdc8c12afAlexander Kornienko      return false;
8198ed9f2b25f082a1643ab5310f9eec33cf31a7577Daniel Jasper    }
8208ed9f2b25f082a1643ab5310f9eec33cf31a7577Daniel Jasper  }
8218ed9f2b25f082a1643ab5310f9eec33cf31a7577Daniel Jasper
8224e778091b0ff42895644ca8bef30f1a91ba6b32bDaniel Jasper  SmallVector<Context, 8> Contexts;
8234e778091b0ff42895644ca8bef30f1a91ba6b32bDaniel Jasper
824d4a03dbb0bdbd4908375306283cb61813bd8f454Daniel Jasper  const FormatStyle &Style;
8254e778091b0ff42895644ca8bef30f1a91ba6b32bDaniel Jasper  AnnotatedLine &Line;
826b398701e4cbb9a55d90a60e3f4f4bc577446d098Manuel Klimek  FormatToken *CurrentToken;
8274e778091b0ff42895644ca8bef30f1a91ba6b32bDaniel Jasper  bool KeywordVirtualFound;
8281407bee187d7b964d5293ac8bf4f7a490c78cec6Daniel Jasper  bool NameFound;
8292ca3741a962ded08866596577aaee2f4ab74c955Daniel Jasper  bool AutoFound;
830c2e6d2a4a7fe9dfa7d52a38c6048b7b18e6b591aNico Weber  IdentifierInfo &Ident_in;
83132d28ee6061930b2a9c170cbaaeea028c88a7b89Daniel Jasper};
83232d28ee6061930b2a9c170cbaaeea028c88a7b89Daniel Jasper
833d489f8ceb735458b0e1f814e3f952b154f49c025Daniel Jasperstatic int PrecedenceUnaryOperator = prec::PointerToMember + 1;
834d489f8ceb735458b0e1f814e3f952b154f49c025Daniel Jasperstatic int PrecedenceArrowAndPeriod = prec::PointerToMember + 2;
835d489f8ceb735458b0e1f814e3f952b154f49c025Daniel Jasper
83629f123b2fa0435bb1962f0d9e9a2e660f35fbb2fDaniel Jasper/// \brief Parses binary expressions by inserting fake parenthesis based on
83729f123b2fa0435bb1962f0d9e9a2e660f35fbb2fDaniel Jasper/// operator precedence.
83829f123b2fa0435bb1962f0d9e9a2e660f35fbb2fDaniel Jasperclass ExpressionParser {
83929f123b2fa0435bb1962f0d9e9a2e660f35fbb2fDaniel Jasperpublic:
8409acb8b4355028887e8cc4aa8f683aceee021a62bDaniel Jasper  ExpressionParser(AnnotatedLine &Line) : Current(Line.First) {
8419acb8b4355028887e8cc4aa8f683aceee021a62bDaniel Jasper    // Skip leading "}", e.g. in "} else if (...) {".
8429acb8b4355028887e8cc4aa8f683aceee021a62bDaniel Jasper    if (Current->is(tok::r_brace))
8439acb8b4355028887e8cc4aa8f683aceee021a62bDaniel Jasper      next();
8449acb8b4355028887e8cc4aa8f683aceee021a62bDaniel Jasper  }
84529f123b2fa0435bb1962f0d9e9a2e660f35fbb2fDaniel Jasper
84629f123b2fa0435bb1962f0d9e9a2e660f35fbb2fDaniel Jasper  /// \brief Parse expressions with the given operatore precedence.
847237d4c1c785be13656bff6c09e5b7ccd066ff5baDaniel Jasper  void parse(int Precedence = 0) {
848d489f8ceb735458b0e1f814e3f952b154f49c025Daniel Jasper    if (Current == NULL || Precedence > PrecedenceArrowAndPeriod)
8493618e6fae8b734ad94221d941417c12d4bd1e3a8Daniel Jasper      return;
8503618e6fae8b734ad94221d941417c12d4bd1e3a8Daniel Jasper
851c01897c3f6139304ed25e3f673bef77be209617fDaniel Jasper    // Conditional expressions need to be parsed separately for proper nesting.
852d489f8ceb735458b0e1f814e3f952b154f49c025Daniel Jasper    if (Precedence == prec::Conditional) {
853c01897c3f6139304ed25e3f673bef77be209617fDaniel Jasper      parseConditionalExpr();
854c01897c3f6139304ed25e3f673bef77be209617fDaniel Jasper      return;
855c01897c3f6139304ed25e3f673bef77be209617fDaniel Jasper    }
8563618e6fae8b734ad94221d941417c12d4bd1e3a8Daniel Jasper
8573618e6fae8b734ad94221d941417c12d4bd1e3a8Daniel Jasper    // Parse unary operators, which all have a higher precedence than binary
8583618e6fae8b734ad94221d941417c12d4bd1e3a8Daniel Jasper    // operators.
859d489f8ceb735458b0e1f814e3f952b154f49c025Daniel Jasper    if (Precedence == PrecedenceUnaryOperator) {
8603618e6fae8b734ad94221d941417c12d4bd1e3a8Daniel Jasper      parseUnaryOperator();
86129f123b2fa0435bb1962f0d9e9a2e660f35fbb2fDaniel Jasper      return;
8623618e6fae8b734ad94221d941417c12d4bd1e3a8Daniel Jasper    }
86329f123b2fa0435bb1962f0d9e9a2e660f35fbb2fDaniel Jasper
864b398701e4cbb9a55d90a60e3f4f4bc577446d098Manuel Klimek    FormatToken *Start = Current;
865d489f8ceb735458b0e1f814e3f952b154f49c025Daniel Jasper    FormatToken *LatestOperator = NULL;
86629f123b2fa0435bb1962f0d9e9a2e660f35fbb2fDaniel Jasper
867237d4c1c785be13656bff6c09e5b7ccd066ff5baDaniel Jasper    while (Current) {
86829f123b2fa0435bb1962f0d9e9a2e660f35fbb2fDaniel Jasper      // Consume operators with higher precedence.
869bf71ba2988b34c45af968f0965e28ac952e4a15fDaniel Jasper      parse(Precedence + 1);
87029f123b2fa0435bb1962f0d9e9a2e660f35fbb2fDaniel Jasper
8713618e6fae8b734ad94221d941417c12d4bd1e3a8Daniel Jasper      int CurrentPrecedence = getCurrentPrecedence();
8723618e6fae8b734ad94221d941417c12d4bd1e3a8Daniel Jasper
8733618e6fae8b734ad94221d941417c12d4bd1e3a8Daniel Jasper      if (Current && Current->Type == TT_ObjCSelectorName &&
8743618e6fae8b734ad94221d941417c12d4bd1e3a8Daniel Jasper          Precedence == CurrentPrecedence)
8753618e6fae8b734ad94221d941417c12d4bd1e3a8Daniel Jasper        Start = Current;
876237d4c1c785be13656bff6c09e5b7ccd066ff5baDaniel Jasper
87729f123b2fa0435bb1962f0d9e9a2e660f35fbb2fDaniel Jasper      // At the end of the line or when an operator with higher precedence is
87829f123b2fa0435bb1962f0d9e9a2e660f35fbb2fDaniel Jasper      // found, insert fake parenthesis and return.
879ac3223e45e7d17c65b143439313800eef4fdf71dDaniel Jasper      if (Current == NULL || Current->closesScope() ||
880d489f8ceb735458b0e1f814e3f952b154f49c025Daniel Jasper          (CurrentPrecedence != -1 && CurrentPrecedence < Precedence)) {
881d489f8ceb735458b0e1f814e3f952b154f49c025Daniel Jasper        if (LatestOperator) {
882d489f8ceb735458b0e1f814e3f952b154f49c025Daniel Jasper          if (Precedence == PrecedenceArrowAndPeriod) {
883d489f8ceb735458b0e1f814e3f952b154f49c025Daniel Jasper            LatestOperator->LastInChainOfCalls = true;
884d489f8ceb735458b0e1f814e3f952b154f49c025Daniel Jasper            // Call expressions don't have a binary operator precedence.
885d489f8ceb735458b0e1f814e3f952b154f49c025Daniel Jasper            addFakeParenthesis(Start, prec::Unknown);
886d489f8ceb735458b0e1f814e3f952b154f49c025Daniel Jasper          } else {
887d489f8ceb735458b0e1f814e3f952b154f49c025Daniel Jasper            addFakeParenthesis(Start, prec::Level(Precedence));
888d489f8ceb735458b0e1f814e3f952b154f49c025Daniel Jasper          }
889d489f8ceb735458b0e1f814e3f952b154f49c025Daniel Jasper        }
89029f123b2fa0435bb1962f0d9e9a2e660f35fbb2fDaniel Jasper        return;
89129f123b2fa0435bb1962f0d9e9a2e660f35fbb2fDaniel Jasper      }
89229f123b2fa0435bb1962f0d9e9a2e660f35fbb2fDaniel Jasper
89329f123b2fa0435bb1962f0d9e9a2e660f35fbb2fDaniel Jasper      // Consume scopes: (), [], <> and {}
894ac3223e45e7d17c65b143439313800eef4fdf71dDaniel Jasper      if (Current->opensScope()) {
895ac3223e45e7d17c65b143439313800eef4fdf71dDaniel Jasper        while (Current && !Current->closesScope()) {
89629f123b2fa0435bb1962f0d9e9a2e660f35fbb2fDaniel Jasper          next();
89729f123b2fa0435bb1962f0d9e9a2e660f35fbb2fDaniel Jasper          parse();
89829f123b2fa0435bb1962f0d9e9a2e660f35fbb2fDaniel Jasper        }
89929f123b2fa0435bb1962f0d9e9a2e660f35fbb2fDaniel Jasper        next();
90029f123b2fa0435bb1962f0d9e9a2e660f35fbb2fDaniel Jasper      } else {
90129f123b2fa0435bb1962f0d9e9a2e660f35fbb2fDaniel Jasper        // Operator found.
902237d4c1c785be13656bff6c09e5b7ccd066ff5baDaniel Jasper        if (CurrentPrecedence == Precedence)
903d489f8ceb735458b0e1f814e3f952b154f49c025Daniel Jasper          LatestOperator = Current;
90429f123b2fa0435bb1962f0d9e9a2e660f35fbb2fDaniel Jasper
90529f123b2fa0435bb1962f0d9e9a2e660f35fbb2fDaniel Jasper        next();
90629f123b2fa0435bb1962f0d9e9a2e660f35fbb2fDaniel Jasper      }
90729f123b2fa0435bb1962f0d9e9a2e660f35fbb2fDaniel Jasper    }
90829f123b2fa0435bb1962f0d9e9a2e660f35fbb2fDaniel Jasper  }
90929f123b2fa0435bb1962f0d9e9a2e660f35fbb2fDaniel Jasper
91029f123b2fa0435bb1962f0d9e9a2e660f35fbb2fDaniel Jasperprivate:
9113618e6fae8b734ad94221d941417c12d4bd1e3a8Daniel Jasper  /// \brief Gets the precedence (+1) of the given token for binary operators
9123618e6fae8b734ad94221d941417c12d4bd1e3a8Daniel Jasper  /// and other tokens that we treat like binary operators.
9133618e6fae8b734ad94221d941417c12d4bd1e3a8Daniel Jasper  int getCurrentPrecedence() {
9143618e6fae8b734ad94221d941417c12d4bd1e3a8Daniel Jasper    if (Current) {
9153618e6fae8b734ad94221d941417c12d4bd1e3a8Daniel Jasper      if (Current->Type == TT_ConditionalExpr)
916d489f8ceb735458b0e1f814e3f952b154f49c025Daniel Jasper        return prec::Conditional;
9173618e6fae8b734ad94221d941417c12d4bd1e3a8Daniel Jasper      else if (Current->is(tok::semi) || Current->Type == TT_InlineASMColon)
918d489f8ceb735458b0e1f814e3f952b154f49c025Daniel Jasper        return 0;
9193618e6fae8b734ad94221d941417c12d4bd1e3a8Daniel Jasper      else if (Current->Type == TT_BinaryOperator || Current->is(tok::comma))
920d489f8ceb735458b0e1f814e3f952b154f49c025Daniel Jasper        return Current->getPrecedence();
9213618e6fae8b734ad94221d941417c12d4bd1e3a8Daniel Jasper      else if (Current->Type == TT_ObjCSelectorName)
922d489f8ceb735458b0e1f814e3f952b154f49c025Daniel Jasper        return prec::Assignment;
923d489f8ceb735458b0e1f814e3f952b154f49c025Daniel Jasper      else if (Current->isOneOf(tok::period, tok::arrow))
924d489f8ceb735458b0e1f814e3f952b154f49c025Daniel Jasper        return PrecedenceArrowAndPeriod;
9253618e6fae8b734ad94221d941417c12d4bd1e3a8Daniel Jasper    }
926d489f8ceb735458b0e1f814e3f952b154f49c025Daniel Jasper    return -1;
9273618e6fae8b734ad94221d941417c12d4bd1e3a8Daniel Jasper  }
9283618e6fae8b734ad94221d941417c12d4bd1e3a8Daniel Jasper
929c01897c3f6139304ed25e3f673bef77be209617fDaniel Jasper  void addFakeParenthesis(FormatToken *Start, prec::Level Precedence) {
930c01897c3f6139304ed25e3f673bef77be209617fDaniel Jasper    Start->FakeLParens.push_back(Precedence);
931c01897c3f6139304ed25e3f673bef77be209617fDaniel Jasper    if (Current)
932c01897c3f6139304ed25e3f673bef77be209617fDaniel Jasper      ++Current->Previous->FakeRParens;
933c01897c3f6139304ed25e3f673bef77be209617fDaniel Jasper  }
934c01897c3f6139304ed25e3f673bef77be209617fDaniel Jasper
9353618e6fae8b734ad94221d941417c12d4bd1e3a8Daniel Jasper  /// \brief Parse unary operator expressions and surround them with fake
9363618e6fae8b734ad94221d941417c12d4bd1e3a8Daniel Jasper  /// parentheses if appropriate.
9373618e6fae8b734ad94221d941417c12d4bd1e3a8Daniel Jasper  void parseUnaryOperator() {
938d489f8ceb735458b0e1f814e3f952b154f49c025Daniel Jasper    if (Current == NULL || Current->Type != TT_UnaryOperator) {
939d489f8ceb735458b0e1f814e3f952b154f49c025Daniel Jasper      parse(PrecedenceArrowAndPeriod);
9403618e6fae8b734ad94221d941417c12d4bd1e3a8Daniel Jasper      return;
941d489f8ceb735458b0e1f814e3f952b154f49c025Daniel Jasper    }
9423618e6fae8b734ad94221d941417c12d4bd1e3a8Daniel Jasper
9433618e6fae8b734ad94221d941417c12d4bd1e3a8Daniel Jasper    FormatToken *Start = Current;
9443618e6fae8b734ad94221d941417c12d4bd1e3a8Daniel Jasper    next();
945d489f8ceb735458b0e1f814e3f952b154f49c025Daniel Jasper    parseUnaryOperator();
9463618e6fae8b734ad94221d941417c12d4bd1e3a8Daniel Jasper
9473618e6fae8b734ad94221d941417c12d4bd1e3a8Daniel Jasper    // The actual precedence doesn't matter.
948d489f8ceb735458b0e1f814e3f952b154f49c025Daniel Jasper    addFakeParenthesis(Start, prec::Unknown);
9493618e6fae8b734ad94221d941417c12d4bd1e3a8Daniel Jasper  }
9503618e6fae8b734ad94221d941417c12d4bd1e3a8Daniel Jasper
951c01897c3f6139304ed25e3f673bef77be209617fDaniel Jasper  void parseConditionalExpr() {
952c01897c3f6139304ed25e3f673bef77be209617fDaniel Jasper    FormatToken *Start = Current;
953d489f8ceb735458b0e1f814e3f952b154f49c025Daniel Jasper    parse(prec::LogicalOr);
954c01897c3f6139304ed25e3f673bef77be209617fDaniel Jasper    if (!Current || !Current->is(tok::question))
955c01897c3f6139304ed25e3f673bef77be209617fDaniel Jasper      return;
956c01897c3f6139304ed25e3f673bef77be209617fDaniel Jasper    next();
957d489f8ceb735458b0e1f814e3f952b154f49c025Daniel Jasper    parse(prec::LogicalOr);
958c01897c3f6139304ed25e3f673bef77be209617fDaniel Jasper    if (!Current || Current->Type != TT_ConditionalExpr)
959c01897c3f6139304ed25e3f673bef77be209617fDaniel Jasper      return;
960c01897c3f6139304ed25e3f673bef77be209617fDaniel Jasper    next();
961c01897c3f6139304ed25e3f673bef77be209617fDaniel Jasper    parseConditionalExpr();
962c01897c3f6139304ed25e3f673bef77be209617fDaniel Jasper    addFakeParenthesis(Start, prec::Conditional);
963c01897c3f6139304ed25e3f673bef77be209617fDaniel Jasper  }
964c01897c3f6139304ed25e3f673bef77be209617fDaniel Jasper
96529f123b2fa0435bb1962f0d9e9a2e660f35fbb2fDaniel Jasper  void next() {
966d71b15badeecdc049440103ef044f9cdf5e1359cAlexander Kornienko    if (Current)
967d71b15badeecdc049440103ef044f9cdf5e1359cAlexander Kornienko      Current = Current->Next;
968d71b15badeecdc049440103ef044f9cdf5e1359cAlexander Kornienko    while (Current && Current->isTrailingComment())
969b398701e4cbb9a55d90a60e3f4f4bc577446d098Manuel Klimek      Current = Current->Next;
97029f123b2fa0435bb1962f0d9e9a2e660f35fbb2fDaniel Jasper  }
97129f123b2fa0435bb1962f0d9e9a2e660f35fbb2fDaniel Jasper
972b398701e4cbb9a55d90a60e3f4f4bc577446d098Manuel Klimek  FormatToken *Current;
97329f123b2fa0435bb1962f0d9e9a2e660f35fbb2fDaniel Jasper};
97429f123b2fa0435bb1962f0d9e9a2e660f35fbb2fDaniel Jasper
97514e66498781b7d81639bdc48716e09700552ac21Craig Topper} // end anonymous namespace
97614e66498781b7d81639bdc48716e09700552ac21Craig Topper
9778ff690ab478b33e0d830a6203de12d191d94f8ffDaniel Jaspervoid TokenAnnotator::annotate(AnnotatedLine &Line) {
978567dcf95424d69657f75e4bfd028967ca1f9eb8dDaniel Jasper  for (std::vector<AnnotatedLine *>::iterator I = Line.Children.begin(),
979567dcf95424d69657f75e4bfd028967ca1f9eb8dDaniel Jasper                                              E = Line.Children.end();
980567dcf95424d69657f75e4bfd028967ca1f9eb8dDaniel Jasper       I != E; ++I) {
981567dcf95424d69657f75e4bfd028967ca1f9eb8dDaniel Jasper    annotate(**I);
982567dcf95424d69657f75e4bfd028967ca1f9eb8dDaniel Jasper  }
983d4a03dbb0bdbd4908375306283cb61813bd8f454Daniel Jasper  AnnotatingParser Parser(Style, Line, Ident_in);
98432d28ee6061930b2a9c170cbaaeea028c88a7b89Daniel Jasper  Line.Type = Parser.parseLine();
98532d28ee6061930b2a9c170cbaaeea028c88a7b89Daniel Jasper  if (Line.Type == LT_Invalid)
98632d28ee6061930b2a9c170cbaaeea028c88a7b89Daniel Jasper    return;
98732d28ee6061930b2a9c170cbaaeea028c88a7b89Daniel Jasper
98829f123b2fa0435bb1962f0d9e9a2e660f35fbb2fDaniel Jasper  ExpressionParser ExprParser(Line);
98929f123b2fa0435bb1962f0d9e9a2e660f35fbb2fDaniel Jasper  ExprParser.parse();
99029f123b2fa0435bb1962f0d9e9a2e660f35fbb2fDaniel Jasper
991b398701e4cbb9a55d90a60e3f4f4bc577446d098Manuel Klimek  if (Line.First->Type == TT_ObjCMethodSpecifier)
99232d28ee6061930b2a9c170cbaaeea028c88a7b89Daniel Jasper    Line.Type = LT_ObjCMethodDecl;
993b398701e4cbb9a55d90a60e3f4f4bc577446d098Manuel Klimek  else if (Line.First->Type == TT_ObjCDecl)
99432d28ee6061930b2a9c170cbaaeea028c88a7b89Daniel Jasper    Line.Type = LT_ObjCDecl;
995b398701e4cbb9a55d90a60e3f4f4bc577446d098Manuel Klimek  else if (Line.First->Type == TT_ObjCProperty)
99632d28ee6061930b2a9c170cbaaeea028c88a7b89Daniel Jasper    Line.Type = LT_ObjCProperty;
99732d28ee6061930b2a9c170cbaaeea028c88a7b89Daniel Jasper
998b398701e4cbb9a55d90a60e3f4f4bc577446d098Manuel Klimek  Line.First->SpacesRequiredBefore = 1;
999b398701e4cbb9a55d90a60e3f4f4bc577446d098Manuel Klimek  Line.First->CanBreakBefore = Line.First->MustBreakBefore;
100032d28ee6061930b2a9c170cbaaeea028c88a7b89Daniel Jasper}
100132d28ee6061930b2a9c170cbaaeea028c88a7b89Daniel Jasper
10028ff690ab478b33e0d830a6203de12d191d94f8ffDaniel Jaspervoid TokenAnnotator::calculateFormattingInformation(AnnotatedLine &Line) {
100300895106f9ed602af67984ec4d225a0cdc8c12afAlexander Kornienko  Line.First->TotalLength = Line.First->CodePointCount;
1004b398701e4cbb9a55d90a60e3f4f4bc577446d098Manuel Klimek  if (!Line.First->Next)
10058ff690ab478b33e0d830a6203de12d191d94f8ffDaniel Jasper    return;
1006b398701e4cbb9a55d90a60e3f4f4bc577446d098Manuel Klimek  FormatToken *Current = Line.First->Next;
10078ff690ab478b33e0d830a6203de12d191d94f8ffDaniel Jasper  while (Current != NULL) {
1008729a743b317d877df3978e88a4a247d2edbf2090Daniel Jasper    if (Current->Type == TT_LineComment)
1009729a743b317d877df3978e88a4a247d2edbf2090Daniel Jasper      Current->SpacesRequiredBefore = Style.SpacesBeforeTrailingComments;
1010729a743b317d877df3978e88a4a247d2edbf2090Daniel Jasper    else
1011729a743b317d877df3978e88a4a247d2edbf2090Daniel Jasper      Current->SpacesRequiredBefore =
1012729a743b317d877df3978e88a4a247d2edbf2090Daniel Jasper          spaceRequiredBefore(Line, *Current) ? 1 : 0;
10138ff690ab478b33e0d830a6203de12d191d94f8ffDaniel Jasper
1014561211d35b5a2825fba6d0c017044f6896b204fdDaniel Jasper    if (Current->is(tok::comment)) {
1015b398701e4cbb9a55d90a60e3f4f4bc577446d098Manuel Klimek      Current->MustBreakBefore = Current->NewlinesBefore > 0;
1016b398701e4cbb9a55d90a60e3f4f4bc577446d098Manuel Klimek    } else if (Current->Previous->isTrailingComment() ||
10178ff690ab478b33e0d830a6203de12d191d94f8ffDaniel Jasper               (Current->is(tok::string_literal) &&
1018b398701e4cbb9a55d90a60e3f4f4bc577446d098Manuel Klimek                Current->Previous->is(tok::string_literal))) {
10198ff690ab478b33e0d830a6203de12d191d94f8ffDaniel Jasper      Current->MustBreakBefore = true;
1020561211d35b5a2825fba6d0c017044f6896b204fdDaniel Jasper    } else if (Current->Previous->IsUnterminatedLiteral) {
1021561211d35b5a2825fba6d0c017044f6896b204fdDaniel Jasper      Current->MustBreakBefore = true;
1022b398701e4cbb9a55d90a60e3f4f4bc577446d098Manuel Klimek    } else if (Current->is(tok::lessless) && Current->Next &&
1023b398701e4cbb9a55d90a60e3f4f4bc577446d098Manuel Klimek               Current->Previous->is(tok::string_literal) &&
1024b398701e4cbb9a55d90a60e3f4f4bc577446d098Manuel Klimek               Current->Next->is(tok::string_literal)) {
10258ff690ab478b33e0d830a6203de12d191d94f8ffDaniel Jasper      Current->MustBreakBefore = true;
1026b398701e4cbb9a55d90a60e3f4f4bc577446d098Manuel Klimek    } else if (Current->Previous->ClosesTemplateDeclaration &&
1027bbc8776493fc0176d925a5b528e61ee400895047Daniel Jasper               Style.AlwaysBreakTemplateDeclarations) {
1028bbc8776493fc0176d925a5b528e61ee400895047Daniel Jasper      Current->MustBreakBefore = true;
1029e8b10d3d5b90efaf60ad89e96f6500f971ceec41Daniel Jasper    } else if (Current->Type == TT_CtorInitializerComma &&
1030e8b10d3d5b90efaf60ad89e96f6500f971ceec41Daniel Jasper               Style.BreakConstructorInitializersBeforeComma) {
1031e8b10d3d5b90efaf60ad89e96f6500f971ceec41Daniel Jasper      Current->MustBreakBefore = true;
10328ff690ab478b33e0d830a6203de12d191d94f8ffDaniel Jasper    }
10338ff690ab478b33e0d830a6203de12d191d94f8ffDaniel Jasper    Current->CanBreakBefore =
10348ff690ab478b33e0d830a6203de12d191d94f8ffDaniel Jasper        Current->MustBreakBefore || canBreakBefore(Line, *Current);
1035567dcf95424d69657f75e4bfd028967ca1f9eb8dDaniel Jasper    if (Current->MustBreakBefore || !Current->Children.empty() ||
10364b762a91faac0473fa380ea9731992b24867e2e6Alexander Kornienko        (Current->is(tok::string_literal) && Current->isMultiline()))
1037b398701e4cbb9a55d90a60e3f4f4bc577446d098Manuel Klimek      Current->TotalLength = Current->Previous->TotalLength + Style.ColumnLimit;
10388ff690ab478b33e0d830a6203de12d191d94f8ffDaniel Jasper    else
10392a409b62126d8f0b8f5749d5ed435ad2b394b526Daniel Jasper      Current->TotalLength = Current->Previous->TotalLength +
10402a409b62126d8f0b8f5749d5ed435ad2b394b526Daniel Jasper                             Current->CodePointCount +
10412a409b62126d8f0b8f5749d5ed435ad2b394b526Daniel Jasper                             Current->SpacesRequiredBefore;
10428ff690ab478b33e0d830a6203de12d191d94f8ffDaniel Jasper    // FIXME: Only calculate this if CanBreakBefore is true once static
10438ff690ab478b33e0d830a6203de12d191d94f8ffDaniel Jasper    // initializers etc. are sorted out.
10448ff690ab478b33e0d830a6203de12d191d94f8ffDaniel Jasper    // FIXME: Move magic numbers to a better place.
10458ff690ab478b33e0d830a6203de12d191d94f8ffDaniel Jasper    Current->SplitPenalty =
10468ff690ab478b33e0d830a6203de12d191d94f8ffDaniel Jasper        20 * Current->BindingStrength + splitPenalty(Line, *Current);
10478ff690ab478b33e0d830a6203de12d191d94f8ffDaniel Jasper
1048b398701e4cbb9a55d90a60e3f4f4bc577446d098Manuel Klimek    Current = Current->Next;
104932d28ee6061930b2a9c170cbaaeea028c88a7b89Daniel Jasper  }
1050bf71ba2988b34c45af968f0965e28ac952e4a15fDaniel Jasper
1051e573c3f7fc40e813559ab4ff1e7eec4f66f1a50fManuel Klimek  calculateUnbreakableTailLengths(Line);
1052d4a03dbb0bdbd4908375306283cb61813bd8f454Daniel Jasper  for (Current = Line.First; Current != NULL; Current = Current->Next) {
1053d4a03dbb0bdbd4908375306283cb61813bd8f454Daniel Jasper    if (Current->Role)
1054d4a03dbb0bdbd4908375306283cb61813bd8f454Daniel Jasper      Current->Role->precomputeFormattingInfos(Current);
1055d4a03dbb0bdbd4908375306283cb61813bd8f454Daniel Jasper  }
1056d4a03dbb0bdbd4908375306283cb61813bd8f454Daniel Jasper
1057567dcf95424d69657f75e4bfd028967ca1f9eb8dDaniel Jasper  DEBUG({ printDebugInfo(Line); });
1058567dcf95424d69657f75e4bfd028967ca1f9eb8dDaniel Jasper
1059567dcf95424d69657f75e4bfd028967ca1f9eb8dDaniel Jasper  for (std::vector<AnnotatedLine *>::iterator I = Line.Children.begin(),
1060567dcf95424d69657f75e4bfd028967ca1f9eb8dDaniel Jasper                                              E = Line.Children.end();
1061567dcf95424d69657f75e4bfd028967ca1f9eb8dDaniel Jasper       I != E; ++I) {
1062567dcf95424d69657f75e4bfd028967ca1f9eb8dDaniel Jasper    calculateFormattingInformation(**I);
1063567dcf95424d69657f75e4bfd028967ca1f9eb8dDaniel Jasper  }
106432d28ee6061930b2a9c170cbaaeea028c88a7b89Daniel Jasper}
106532d28ee6061930b2a9c170cbaaeea028c88a7b89Daniel Jasper
1066e573c3f7fc40e813559ab4ff1e7eec4f66f1a50fManuel Klimekvoid TokenAnnotator::calculateUnbreakableTailLengths(AnnotatedLine &Line) {
1067e573c3f7fc40e813559ab4ff1e7eec4f66f1a50fManuel Klimek  unsigned UnbreakableTailLength = 0;
1068b398701e4cbb9a55d90a60e3f4f4bc577446d098Manuel Klimek  FormatToken *Current = Line.Last;
1069e573c3f7fc40e813559ab4ff1e7eec4f66f1a50fManuel Klimek  while (Current != NULL) {
1070e573c3f7fc40e813559ab4ff1e7eec4f66f1a50fManuel Klimek    Current->UnbreakableTailLength = UnbreakableTailLength;
1071e573c3f7fc40e813559ab4ff1e7eec4f66f1a50fManuel Klimek    if (Current->CanBreakBefore ||
1072e573c3f7fc40e813559ab4ff1e7eec4f66f1a50fManuel Klimek        Current->isOneOf(tok::comment, tok::string_literal)) {
1073e573c3f7fc40e813559ab4ff1e7eec4f66f1a50fManuel Klimek      UnbreakableTailLength = 0;
1074e573c3f7fc40e813559ab4ff1e7eec4f66f1a50fManuel Klimek    } else {
1075e573c3f7fc40e813559ab4ff1e7eec4f66f1a50fManuel Klimek      UnbreakableTailLength +=
107600895106f9ed602af67984ec4d225a0cdc8c12afAlexander Kornienko          Current->CodePointCount + Current->SpacesRequiredBefore;
1077e573c3f7fc40e813559ab4ff1e7eec4f66f1a50fManuel Klimek    }
1078b398701e4cbb9a55d90a60e3f4f4bc577446d098Manuel Klimek    Current = Current->Previous;
1079e573c3f7fc40e813559ab4ff1e7eec4f66f1a50fManuel Klimek  }
1080e573c3f7fc40e813559ab4ff1e7eec4f66f1a50fManuel Klimek}
1081e573c3f7fc40e813559ab4ff1e7eec4f66f1a50fManuel Klimek
10828ff690ab478b33e0d830a6203de12d191d94f8ffDaniel Jasperunsigned TokenAnnotator::splitPenalty(const AnnotatedLine &Line,
1083b398701e4cbb9a55d90a60e3f4f4bc577446d098Manuel Klimek                                      const FormatToken &Tok) {
1084b398701e4cbb9a55d90a60e3f4f4bc577446d098Manuel Klimek  const FormatToken &Left = *Tok.Previous;
1085b398701e4cbb9a55d90a60e3f4f4bc577446d098Manuel Klimek  const FormatToken &Right = Tok;
108632d28ee6061930b2a9c170cbaaeea028c88a7b89Daniel Jasper
10875ad390d27f8f7cb6628bc2c32beba4d25bc2718bDaniel Jasper  if (Left.is(tok::semi))
10885ad390d27f8f7cb6628bc2c32beba4d25bc2718bDaniel Jasper    return 0;
10895ad390d27f8f7cb6628bc2c32beba4d25bc2718bDaniel Jasper  if (Left.is(tok::comma))
10905ad390d27f8f7cb6628bc2c32beba4d25bc2718bDaniel Jasper    return 1;
1091011c35dabb4c8abcb7389d8fbc6316f8f23576abDaniel Jasper  if (Right.is(tok::l_square))
1092011c35dabb4c8abcb7389d8fbc6316f8f23576abDaniel Jasper    return 150;
10935ad390d27f8f7cb6628bc2c32beba4d25bc2718bDaniel Jasper
10946561f6a13b79ed752748ede590792191edf78ce8Daniel Jasper  if (Right.Type == TT_StartOfName || Right.is(tok::kw_operator)) {
1095b398701e4cbb9a55d90a60e3f4f4bc577446d098Manuel Klimek    if (Line.First->is(tok::kw_for) && Right.PartOfMultiVariableDeclStmt)
10963c08a818a6ac9115fe8880af9bbf5a0a87bdffaaDaniel Jasper      return 3;
1097c18cff311118fc6a30929468fc82b2b35cbd7fbfDaniel Jasper    if (Left.Type == TT_StartOfName)
1098c18cff311118fc6a30929468fc82b2b35cbd7fbfDaniel Jasper      return 20;
109992495a8032d8624495a0ce769d5a6cdeba2dc8d8Daniel Jasper    if (Line.MightBeFunctionDecl && Right.BindingStrength == 1)
11003c08a818a6ac9115fe8880af9bbf5a0a87bdffaaDaniel Jasper      // FIXME: Clean up hack of using BindingStrength to find top-level names.
11013c08a818a6ac9115fe8880af9bbf5a0a87bdffaaDaniel Jasper      return Style.PenaltyReturnTypeOnItsOwnLine;
110292495a8032d8624495a0ce769d5a6cdeba2dc8d8Daniel Jasper    return 200;
11033c08a818a6ac9115fe8880af9bbf5a0a87bdffaaDaniel Jasper  }
110432d28ee6061930b2a9c170cbaaeea028c88a7b89Daniel Jasper  if (Left.is(tok::equal) && Right.is(tok::l_brace))
110532d28ee6061930b2a9c170cbaaeea028c88a7b89Daniel Jasper    return 150;
1106198c8bfee0b33ab0ed0c54224f460868f1c18e95Daniel Jasper  if (Left.Type == TT_CastRParen)
1107198c8bfee0b33ab0ed0c54224f460868f1c18e95Daniel Jasper    return 100;
110832d28ee6061930b2a9c170cbaaeea028c88a7b89Daniel Jasper  if (Left.is(tok::coloncolon))
110932d28ee6061930b2a9c170cbaaeea028c88a7b89Daniel Jasper    return 500;
11106b119d63f5036344acd4e00a6ff2b3c72f26966fDaniel Jasper  if (Left.isOneOf(tok::kw_class, tok::kw_struct))
11116b119d63f5036344acd4e00a6ff2b3c72f26966fDaniel Jasper    return 5000;
111232d28ee6061930b2a9c170cbaaeea028c88a7b89Daniel Jasper
11136cabab48dacc1317821f8f078ed2d4c603b67affDaniel Jasper  if (Left.Type == TT_RangeBasedForLoopColon ||
11146cabab48dacc1317821f8f078ed2d4c603b67affDaniel Jasper      Left.Type == TT_InheritanceColon)
111584a1a63b034744b68a27ec171dca5b1b7cf303f0Daniel Jasper    return 2;
111632d28ee6061930b2a9c170cbaaeea028c88a7b89Daniel Jasper
1117d3fef0fe26da19685bdec6a1bd844505f604d593Daniel Jasper  if (Right.isMemberAccess()) {
1118d3fef0fe26da19685bdec6a1bd844505f604d593Daniel Jasper    if (Left.isOneOf(tok::r_paren, tok::r_square))
1119518ee34467c0722e253a58efea20456e96aa5802Daniel Jasper      return 20; // Should be smaller than breaking at a nested comma.
112032d28ee6061930b2a9c170cbaaeea028c88a7b89Daniel Jasper    return 150;
112132d28ee6061930b2a9c170cbaaeea028c88a7b89Daniel Jasper  }
112232d28ee6061930b2a9c170cbaaeea028c88a7b89Daniel Jasper
112320a0f8cff96505abb65233a2eaf3af3cd8536cd2Daniel Jasper  // Breaking before a trailing 'const' or not-function-like annotation is bad.
1124aa9e7b18a88d715b63e7b65d1b26a1759decc177Daniel Jasper  if (Left.is(tok::r_paren) && Line.Type != LT_ObjCProperty &&
112520a0f8cff96505abb65233a2eaf3af3cd8536cd2Daniel Jasper      (Right.is(tok::kw_const) || (Right.is(tok::identifier) && Right.Next &&
112620a0f8cff96505abb65233a2eaf3af3cd8536cd2Daniel Jasper                                   Right.Next->isNot(tok::l_paren))))
11275ad72bb8eb8e5cc4c061ccd28632295213d319dbDaniel Jasper    return 150;
11285ad72bb8eb8e5cc4c061ccd28632295213d319dbDaniel Jasper
112932d28ee6061930b2a9c170cbaaeea028c88a7b89Daniel Jasper  // In for-loops, prefer breaking at ',' and ';'.
1130b398701e4cbb9a55d90a60e3f4f4bc577446d098Manuel Klimek  if (Line.First->is(tok::kw_for) && Left.is(tok::equal))
11317d81281fc39f6d40d86be6600adba13c05b4a639Daniel Jasper    return 4;
113232d28ee6061930b2a9c170cbaaeea028c88a7b89Daniel Jasper
113332d28ee6061930b2a9c170cbaaeea028c88a7b89Daniel Jasper  // In Objective-C method expressions, prefer breaking before "param:" over
113432d28ee6061930b2a9c170cbaaeea028c88a7b89Daniel Jasper  // breaking after it.
113563d7cedca8616921c1908c88c2f23fcd67bbab99Daniel Jasper  if (Right.Type == TT_ObjCSelectorName)
113632d28ee6061930b2a9c170cbaaeea028c88a7b89Daniel Jasper    return 0;
113763d7cedca8616921c1908c88c2f23fcd67bbab99Daniel Jasper  if (Left.is(tok::colon) && Left.Type == TT_ObjCMethodExpr)
113832d28ee6061930b2a9c170cbaaeea028c88a7b89Daniel Jasper    return 20;
113932d28ee6061930b2a9c170cbaaeea028c88a7b89Daniel Jasper
11401407bee187d7b964d5293ac8bf4f7a490c78cec6Daniel Jasper  if (Left.is(tok::l_paren) && Line.MightBeFunctionDecl)
11411407bee187d7b964d5293ac8bf4f7a490c78cec6Daniel Jasper    return 100;
1142ac3223e45e7d17c65b143439313800eef4fdf71dDaniel Jasper  if (Left.opensScope())
1143e60084d1e8a62716b80872f10e9828fbbb3cadfcDaniel Jasper    return Left.ParameterCount > 1 ? prec::Comma : 19;
114432d28ee6061930b2a9c170cbaaeea028c88a7b89Daniel Jasper
11454e8a7b4a95962f63a938c0d21c9aca0a51b78345Daniel Jasper  if (Right.is(tok::lessless)) {
11464e8a7b4a95962f63a938c0d21c9aca0a51b78345Daniel Jasper    if (Left.is(tok::string_literal)) {
114700895106f9ed602af67984ec4d225a0cdc8c12afAlexander Kornienko      StringRef Content = Left.TokenText;
1148bfa1edd8247b80e951a570ff2486fe5fa9898c41Daniel Jasper      Content = Content.drop_back(1).drop_front(1).trim();
1149bfa1edd8247b80e951a570ff2486fe5fa9898c41Daniel Jasper      if (Content.size() > 1 &&
1150bfa1edd8247b80e951a570ff2486fe5fa9898c41Daniel Jasper          (Content.back() == ':' || Content.back() == '='))
11519637dda705e39110bfff66742542a58dd2470ad2Daniel Jasper        return 25;
11524e8a7b4a95962f63a938c0d21c9aca0a51b78345Daniel Jasper    }
11530c368787d9d1f92a3408b71b3f074a06edaa6bdeDaniel Jasper    return 1; // Breaking at a << is really cheap.
11544e8a7b4a95962f63a938c0d21c9aca0a51b78345Daniel Jasper  }
115532d28ee6061930b2a9c170cbaaeea028c88a7b89Daniel Jasper  if (Left.Type == TT_ConditionalExpr)
1156518ee34467c0722e253a58efea20456e96aa5802Daniel Jasper    return prec::Conditional;
1157b398701e4cbb9a55d90a60e3f4f4bc577446d098Manuel Klimek  prec::Level Level = Left.getPrecedence();
115832d28ee6061930b2a9c170cbaaeea028c88a7b89Daniel Jasper
115932d28ee6061930b2a9c170cbaaeea028c88a7b89Daniel Jasper  if (Level != prec::Unknown)
116032d28ee6061930b2a9c170cbaaeea028c88a7b89Daniel Jasper    return Level;
1161248497199bc56e86d1c089beb9529f3b3d77abb1Daniel Jasper
116232d28ee6061930b2a9c170cbaaeea028c88a7b89Daniel Jasper  return 3;
116332d28ee6061930b2a9c170cbaaeea028c88a7b89Daniel Jasper}
116432d28ee6061930b2a9c170cbaaeea028c88a7b89Daniel Jasper
11658ff690ab478b33e0d830a6203de12d191d94f8ffDaniel Jasperbool TokenAnnotator::spaceRequiredBetween(const AnnotatedLine &Line,
1166b398701e4cbb9a55d90a60e3f4f4bc577446d098Manuel Klimek                                          const FormatToken &Left,
1167b398701e4cbb9a55d90a60e3f4f4bc577446d098Manuel Klimek                                          const FormatToken &Right) {
116832d28ee6061930b2a9c170cbaaeea028c88a7b89Daniel Jasper  if (Right.is(tok::hashhash))
116932d28ee6061930b2a9c170cbaaeea028c88a7b89Daniel Jasper    return Left.is(tok::hash);
1170e74de28ec3692986f3467a7f160a0e293277fa7eAlexander Kornienko  if (Left.isOneOf(tok::hashhash, tok::hash))
117132d28ee6061930b2a9c170cbaaeea028c88a7b89Daniel Jasper    return Right.is(tok::hash);
11727df56bfcf7186f73c99564cd54216f07a8db7352Daniel Jasper  if (Left.is(tok::l_paren) && Right.is(tok::r_paren))
11737df56bfcf7186f73c99564cd54216f07a8db7352Daniel Jasper    return Style.SpaceInEmptyParentheses;
11747df56bfcf7186f73c99564cd54216f07a8db7352Daniel Jasper  if (Left.is(tok::l_paren) || Right.is(tok::r_paren))
117534f3d05d0ee625dfcac951e2851f212c4c1a8b83Daniel Jasper    return (Right.Type == TT_CastRParen ||
117634f3d05d0ee625dfcac951e2851f212c4c1a8b83Daniel Jasper            (Left.MatchingParen && Left.MatchingParen->Type == TT_CastRParen))
11777df56bfcf7186f73c99564cd54216f07a8db7352Daniel Jasper               ? Style.SpacesInCStyleCastParentheses
11787df56bfcf7186f73c99564cd54216f07a8db7352Daniel Jasper               : Style.SpacesInParentheses;
11797df56bfcf7186f73c99564cd54216f07a8db7352Daniel Jasper  if (Right.isOneOf(tok::semi, tok::comma))
118032d28ee6061930b2a9c170cbaaeea028c88a7b89Daniel Jasper    return false;
118132d28ee6061930b2a9c170cbaaeea028c88a7b89Daniel Jasper  if (Right.is(tok::less) &&
118232d28ee6061930b2a9c170cbaaeea028c88a7b89Daniel Jasper      (Left.is(tok::kw_template) ||
118332d28ee6061930b2a9c170cbaaeea028c88a7b89Daniel Jasper       (Line.Type == LT_ObjCDecl && Style.ObjCSpaceBeforeProtocolList)))
118432d28ee6061930b2a9c170cbaaeea028c88a7b89Daniel Jasper    return true;
118532d28ee6061930b2a9c170cbaaeea028c88a7b89Daniel Jasper  if (Left.is(tok::arrow) || Right.is(tok::arrow))
118632d28ee6061930b2a9c170cbaaeea028c88a7b89Daniel Jasper    return false;
1187e74de28ec3692986f3467a7f160a0e293277fa7eAlexander Kornienko  if (Left.isOneOf(tok::exclaim, tok::tilde))
118832d28ee6061930b2a9c170cbaaeea028c88a7b89Daniel Jasper    return false;
118932d28ee6061930b2a9c170cbaaeea028c88a7b89Daniel Jasper  if (Left.is(tok::at) &&
1190e74de28ec3692986f3467a7f160a0e293277fa7eAlexander Kornienko      Right.isOneOf(tok::identifier, tok::string_literal, tok::char_constant,
1191e74de28ec3692986f3467a7f160a0e293277fa7eAlexander Kornienko                    tok::numeric_constant, tok::l_paren, tok::l_brace,
1192e74de28ec3692986f3467a7f160a0e293277fa7eAlexander Kornienko                    tok::kw_true, tok::kw_false))
119332d28ee6061930b2a9c170cbaaeea028c88a7b89Daniel Jasper    return false;
119432d28ee6061930b2a9c170cbaaeea028c88a7b89Daniel Jasper  if (Left.is(tok::coloncolon))
119532d28ee6061930b2a9c170cbaaeea028c88a7b89Daniel Jasper    return false;
119632d28ee6061930b2a9c170cbaaeea028c88a7b89Daniel Jasper  if (Right.is(tok::coloncolon))
1197081fc17374baf55cc6a6069a9f5a1596fb378ef3Daniel Jasper    return !Left.isOneOf(tok::identifier, tok::greater, tok::l_paren,
1198081fc17374baf55cc6a6069a9f5a1596fb378ef3Daniel Jasper                         tok::r_paren);
1199e74de28ec3692986f3467a7f160a0e293277fa7eAlexander Kornienko  if (Left.is(tok::less) || Right.isOneOf(tok::greater, tok::less))
120032d28ee6061930b2a9c170cbaaeea028c88a7b89Daniel Jasper    return false;
1201c47d7f1237b022eabbbdcebf77506e8a81aa54bdDaniel Jasper  if (Right.is(tok::ellipsis))
1202c47d7f1237b022eabbbdcebf77506e8a81aa54bdDaniel Jasper    return false;
120331e44f7a5d50ab8f7f623a7d2e18d5d877ef400dManuel Klimek  if (Left.is(tok::l_square) && Right.is(tok::amp))
120431e44f7a5d50ab8f7f623a7d2e18d5d877ef400dManuel Klimek    return false;
12053fd9ccdd9f8d259bcf518e7056cfd419d992e984Alexander Kornienko  if (Right.Type == TT_PointerOrReference)
1206b398701e4cbb9a55d90a60e3f4f4bc577446d098Manuel Klimek    return Left.Tok.isLiteral() ||
12073fd9ccdd9f8d259bcf518e7056cfd419d992e984Alexander Kornienko           ((Left.Type != TT_PointerOrReference) && Left.isNot(tok::l_paren) &&
12083fd9ccdd9f8d259bcf518e7056cfd419d992e984Alexander Kornienko            !Style.PointerBindsToType);
12093ff4a2fea4aa6e5182b7799ccb4352e56961a212Daniel Jasper  if (Right.Type == TT_FunctionTypeLParen && Left.isNot(tok::l_paren) &&
1210395228fdc343df39c2507e414dc1406a185c6d37Daniel Jasper      (Left.Type != TT_PointerOrReference || Style.PointerBindsToType))
1211395228fdc343df39c2507e414dc1406a185c6d37Daniel Jasper    return true;
12123fd9ccdd9f8d259bcf518e7056cfd419d992e984Alexander Kornienko  if (Left.Type == TT_PointerOrReference)
12133a1847e0a1810a0b1b963182abc59114cc5ff53dDaniel Jasper    return Right.Tok.isLiteral() || Right.Type == TT_BlockComment ||
12149322aaee900b872c98f8fc10b38a231cb1e9b57aDaniel Jasper           ((Right.Type != TT_PointerOrReference) &&
121581d2d38d2d774a2550fa0d2efffa707e7a53b39cDaniel Jasper            Right.isNot(tok::l_paren) && Style.PointerBindsToType &&
1216b398701e4cbb9a55d90a60e3f4f4bc577446d098Manuel Klimek            Left.Previous &&
1217b398701e4cbb9a55d90a60e3f4f4bc577446d098Manuel Klimek            !Left.Previous->isOneOf(tok::l_paren, tok::coloncolon));
121832d28ee6061930b2a9c170cbaaeea028c88a7b89Daniel Jasper  if (Right.is(tok::star) && Left.is(tok::l_paren))
121932d28ee6061930b2a9c170cbaaeea028c88a7b89Daniel Jasper    return false;
1220051860ee770bf83c3e66ab893be3642bb8bc2680Nico Weber  if (Left.is(tok::l_square))
1221051860ee770bf83c3e66ab893be3642bb8bc2680Nico Weber    return Left.Type == TT_ObjCArrayLiteral && Right.isNot(tok::r_square);
1222051860ee770bf83c3e66ab893be3642bb8bc2680Nico Weber  if (Right.is(tok::r_square))
1223051860ee770bf83c3e66ab893be3642bb8bc2680Nico Weber    return Right.Type == TT_ObjCArrayLiteral;
1224ec17226e82979592c16c7815d2368240201d18feDaniel Jasper  if (Right.is(tok::l_square) && Right.Type != TT_ObjCMethodExpr &&
1225567dcf95424d69657f75e4bfd028967ca1f9eb8dDaniel Jasper      Right.Type != TT_LambdaLSquare && Left.isNot(tok::numeric_constant))
122632d28ee6061930b2a9c170cbaaeea028c88a7b89Daniel Jasper    return false;
122732d28ee6061930b2a9c170cbaaeea028c88a7b89Daniel Jasper  if (Left.is(tok::colon))
122832d28ee6061930b2a9c170cbaaeea028c88a7b89Daniel Jasper    return Left.Type != TT_ObjCMethodExpr;
122932d28ee6061930b2a9c170cbaaeea028c88a7b89Daniel Jasper  if (Right.is(tok::colon))
1230ab3ce592d027e3a10fb21e703cab1507f8d9bb03Daniel Jasper    return Right.Type != TT_ObjCMethodExpr && !Left.is(tok::question);
123132d28ee6061930b2a9c170cbaaeea028c88a7b89Daniel Jasper  if (Right.is(tok::l_paren)) {
1232e0fa4c55a4bc6d2bbe0d9d657287c037158d5357Daniel Jasper    if (Left.is(tok::r_paren) && Left.MatchingParen &&
1233e0fa4c55a4bc6d2bbe0d9d657287c037158d5357Daniel Jasper        Left.MatchingParen->Previous &&
1234e0fa4c55a4bc6d2bbe0d9d657287c037158d5357Daniel Jasper        Left.MatchingParen->Previous->is(tok::kw___attribute))
1235e0fa4c55a4bc6d2bbe0d9d657287c037158d5357Daniel Jasper      return true;
1236e74de28ec3692986f3467a7f160a0e293277fa7eAlexander Kornienko    return Line.Type == LT_ObjCDecl ||
123734f3d05d0ee625dfcac951e2851f212c4c1a8b83Daniel Jasper           Left.isOneOf(tok::kw_return, tok::kw_new, tok::kw_delete,
123834f3d05d0ee625dfcac951e2851f212c4c1a8b83Daniel Jasper                        tok::semi) ||
123934f3d05d0ee625dfcac951e2851f212c4c1a8b83Daniel Jasper           (Style.SpaceAfterControlStatementKeyword &&
124034f3d05d0ee625dfcac951e2851f212c4c1a8b83Daniel Jasper            Left.isOneOf(tok::kw_if, tok::kw_for, tok::kw_while, tok::kw_switch,
124134f3d05d0ee625dfcac951e2851f212c4c1a8b83Daniel Jasper                         tok::kw_catch));
124232d28ee6061930b2a9c170cbaaeea028c88a7b89Daniel Jasper  }
1243b398701e4cbb9a55d90a60e3f4f4bc577446d098Manuel Klimek  if (Left.is(tok::at) && Right.Tok.getObjCKeywordID() != tok::objc_not_keyword)
124432d28ee6061930b2a9c170cbaaeea028c88a7b89Daniel Jasper    return false;
124532d28ee6061930b2a9c170cbaaeea028c88a7b89Daniel Jasper  if (Left.is(tok::l_brace) && Right.is(tok::r_brace))
1246567dcf95424d69657f75e4bfd028967ca1f9eb8dDaniel Jasper    return !Left.Children.empty(); // No spaces in "{}".
12472424eefa6936ec2dc35188e19c99e2f85428b52eDaniel Jasper  if (Left.is(tok::l_brace) || Right.is(tok::r_brace))
1248b5dc3f4f53981b681a565cdf1d49f18e817541ffDaniel Jasper    return !Style.Cpp11BracedListStyle;
12491bee0738b67b784f08d5e2f8351920260c9cfb1dDaniel Jasper  if (Right.Type == TT_UnaryOperator)
12501bee0738b67b784f08d5e2f8351920260c9cfb1dDaniel Jasper    return !Left.isOneOf(tok::l_paren, tok::l_square, tok::at) &&
12511bee0738b67b784f08d5e2f8351920260c9cfb1dDaniel Jasper           (Left.isNot(tok::colon) || Left.Type != TT_ObjCMethodExpr);
1252ce93356e2719d2992763ea747b65beada99f4c9bDaniel Jasper  if (Left.isOneOf(tok::identifier, tok::greater, tok::r_square) &&
125331e44f7a5d50ab8f7f623a7d2e18d5d877ef400dManuel Klimek      Right.is(tok::l_brace) && Right.getNextNonComment() &&
125431e44f7a5d50ab8f7f623a7d2e18d5d877ef400dManuel Klimek      Right.BlockKind != BK_Block)
125532d28ee6061930b2a9c170cbaaeea028c88a7b89Daniel Jasper    return false;
12565ad390d27f8f7cb6628bc2c32beba4d25bc2718bDaniel Jasper  if (Left.is(tok::period) || Right.is(tok::period))
12575ad390d27f8f7cb6628bc2c32beba4d25bc2718bDaniel Jasper    return false;
1258861576b8019392f15c803ac14a4bc31fbd93aab2Nico Weber  if (Left.Type == TT_BlockComment && Left.TokenText.endswith("=*/"))
1259861576b8019392f15c803ac14a4bc31fbd93aab2Nico Weber    return false;
126032d28ee6061930b2a9c170cbaaeea028c88a7b89Daniel Jasper  return true;
126132d28ee6061930b2a9c170cbaaeea028c88a7b89Daniel Jasper}
126232d28ee6061930b2a9c170cbaaeea028c88a7b89Daniel Jasper
12638ff690ab478b33e0d830a6203de12d191d94f8ffDaniel Jasperbool TokenAnnotator::spaceRequiredBefore(const AnnotatedLine &Line,
1264b398701e4cbb9a55d90a60e3f4f4bc577446d098Manuel Klimek                                         const FormatToken &Tok) {
1265b398701e4cbb9a55d90a60e3f4f4bc577446d098Manuel Klimek  if (Tok.Tok.getIdentifierInfo() && Tok.Previous->Tok.getIdentifierInfo())
12662b4c924f85e156d66c01b3f16c850892c47dcc7aDaniel Jasper    return true; // Never ever merge two identifiers.
126732d28ee6061930b2a9c170cbaaeea028c88a7b89Daniel Jasper  if (Line.Type == LT_ObjCMethodDecl) {
1268b398701e4cbb9a55d90a60e3f4f4bc577446d098Manuel Klimek    if (Tok.Previous->Type == TT_ObjCMethodSpecifier)
126932d28ee6061930b2a9c170cbaaeea028c88a7b89Daniel Jasper      return true;
1270b398701e4cbb9a55d90a60e3f4f4bc577446d098Manuel Klimek    if (Tok.Previous->is(tok::r_paren) && Tok.is(tok::identifier))
127132d28ee6061930b2a9c170cbaaeea028c88a7b89Daniel Jasper      // Don't space between ')' and <id>
127232d28ee6061930b2a9c170cbaaeea028c88a7b89Daniel Jasper      return false;
127332d28ee6061930b2a9c170cbaaeea028c88a7b89Daniel Jasper  }
127432d28ee6061930b2a9c170cbaaeea028c88a7b89Daniel Jasper  if (Line.Type == LT_ObjCProperty &&
1275b398701e4cbb9a55d90a60e3f4f4bc577446d098Manuel Klimek      (Tok.is(tok::equal) || Tok.Previous->is(tok::equal)))
127632d28ee6061930b2a9c170cbaaeea028c88a7b89Daniel Jasper    return false;
127732d28ee6061930b2a9c170cbaaeea028c88a7b89Daniel Jasper
12782ca3741a962ded08866596577aaee2f4ab74c955Daniel Jasper  if (Tok.Type == TT_TrailingReturnArrow ||
12792ca3741a962ded08866596577aaee2f4ab74c955Daniel Jasper      Tok.Previous->Type == TT_TrailingReturnArrow)
12802ca3741a962ded08866596577aaee2f4ab74c955Daniel Jasper    return true;
1281b398701e4cbb9a55d90a60e3f4f4bc577446d098Manuel Klimek  if (Tok.Previous->is(tok::comma))
128232d28ee6061930b2a9c170cbaaeea028c88a7b89Daniel Jasper    return true;
12839c3c7b3130bc72b3f50703c11b85152b1264fc90Daniel Jasper  if (Tok.is(tok::comma))
12849c3c7b3130bc72b3f50703c11b85152b1264fc90Daniel Jasper    return false;
128532d28ee6061930b2a9c170cbaaeea028c88a7b89Daniel Jasper  if (Tok.Type == TT_CtorInitializerColon || Tok.Type == TT_ObjCBlockLParen)
128632d28ee6061930b2a9c170cbaaeea028c88a7b89Daniel Jasper    return true;
1287b398701e4cbb9a55d90a60e3f4f4bc577446d098Manuel Klimek  if (Tok.Previous->Tok.is(tok::kw_operator))
12882b4c924f85e156d66c01b3f16c850892c47dcc7aDaniel Jasper    return false;
12892b4c924f85e156d66c01b3f16c850892c47dcc7aDaniel Jasper  if (Tok.Type == TT_OverloadedOperatorLParen)
129032d28ee6061930b2a9c170cbaaeea028c88a7b89Daniel Jasper    return false;
129132d28ee6061930b2a9c170cbaaeea028c88a7b89Daniel Jasper  if (Tok.is(tok::colon))
1292b398701e4cbb9a55d90a60e3f4f4bc577446d098Manuel Klimek    return !Line.First->isOneOf(tok::kw_case, tok::kw_default) &&
1293ab3ce592d027e3a10fb21e703cab1507f8d9bb03Daniel Jasper           Tok.getNextNonComment() != NULL && Tok.Type != TT_ObjCMethodExpr &&
1294ab3ce592d027e3a10fb21e703cab1507f8d9bb03Daniel Jasper           !Tok.Previous->is(tok::question);
1295b398701e4cbb9a55d90a60e3f4f4bc577446d098Manuel Klimek  if (Tok.Previous->Type == TT_UnaryOperator ||
1296b398701e4cbb9a55d90a60e3f4f4bc577446d098Manuel Klimek      Tok.Previous->Type == TT_CastRParen)
129732d28ee6061930b2a9c170cbaaeea028c88a7b89Daniel Jasper    return false;
1298b398701e4cbb9a55d90a60e3f4f4bc577446d098Manuel Klimek  if (Tok.Previous->is(tok::greater) && Tok.is(tok::greater)) {
129929f123b2fa0435bb1962f0d9e9a2e660f35fbb2fDaniel Jasper    return Tok.Type == TT_TemplateCloser &&
1300b398701e4cbb9a55d90a60e3f4f4bc577446d098Manuel Klimek           Tok.Previous->Type == TT_TemplateCloser &&
130129f123b2fa0435bb1962f0d9e9a2e660f35fbb2fDaniel Jasper           Style.Standard != FormatStyle::LS_Cpp11;
130232d28ee6061930b2a9c170cbaaeea028c88a7b89Daniel Jasper  }
130354a38bd5cf243310290f34b43fc940a498a00f90Alexander Kornienko  if (Tok.isOneOf(tok::arrowstar, tok::periodstar) ||
1304b398701e4cbb9a55d90a60e3f4f4bc577446d098Manuel Klimek      Tok.Previous->isOneOf(tok::arrowstar, tok::periodstar))
13059c3c7b3130bc72b3f50703c11b85152b1264fc90Daniel Jasper    return false;
13061dc6f745eb19c94527503012d798dc9b9b5ba6daDaniel Jasper  if ((Tok.Type == TT_BinaryOperator && !Tok.Previous->is(tok::l_paren)) ||
13071dc6f745eb19c94527503012d798dc9b9b5ba6daDaniel Jasper      Tok.Previous->Type == TT_BinaryOperator)
130832d28ee6061930b2a9c170cbaaeea028c88a7b89Daniel Jasper    return true;
1309b398701e4cbb9a55d90a60e3f4f4bc577446d098Manuel Klimek  if (Tok.Previous->Type == TT_TemplateCloser && Tok.is(tok::l_paren))
131032d28ee6061930b2a9c170cbaaeea028c88a7b89Daniel Jasper    return false;
1311a4dd982805e89a37a080350bf8de9069135c5a60Daniel Jasper  if (Tok.is(tok::less) && Tok.Previous->isNot(tok::l_paren) &&
1312a4dd982805e89a37a080350bf8de9069135c5a60Daniel Jasper      Line.First->is(tok::hash))
131332d28ee6061930b2a9c170cbaaeea028c88a7b89Daniel Jasper    return true;
131432d28ee6061930b2a9c170cbaaeea028c88a7b89Daniel Jasper  if (Tok.Type == TT_TrailingUnaryOperator)
131532d28ee6061930b2a9c170cbaaeea028c88a7b89Daniel Jasper    return false;
1316b398701e4cbb9a55d90a60e3f4f4bc577446d098Manuel Klimek  return spaceRequiredBetween(Line, *Tok.Previous, Tok);
131732d28ee6061930b2a9c170cbaaeea028c88a7b89Daniel Jasper}
131832d28ee6061930b2a9c170cbaaeea028c88a7b89Daniel Jasper
13198ff690ab478b33e0d830a6203de12d191d94f8ffDaniel Jasperbool TokenAnnotator::canBreakBefore(const AnnotatedLine &Line,
1320b398701e4cbb9a55d90a60e3f4f4bc577446d098Manuel Klimek                                    const FormatToken &Right) {
1321b398701e4cbb9a55d90a60e3f4f4bc577446d098Manuel Klimek  const FormatToken &Left = *Right.Previous;
13226561f6a13b79ed752748ede590792191edf78ce8Daniel Jasper  if (Right.Type == TT_StartOfName || Right.is(tok::kw_operator))
132332d28ee6061930b2a9c170cbaaeea028c88a7b89Daniel Jasper    return true;
1324f2ff8126e9e9df368f31b1f968d8fc80f99809b3Nico Weber  if (Right.is(tok::colon) &&
1325f2ff8126e9e9df368f31b1f968d8fc80f99809b3Nico Weber      (Right.Type == TT_ObjCDictLiteral || Right.Type == TT_ObjCMethodExpr))
132632d28ee6061930b2a9c170cbaaeea028c88a7b89Daniel Jasper    return false;
1327f2ff8126e9e9df368f31b1f968d8fc80f99809b3Nico Weber  if (Left.is(tok::colon) &&
1328f2ff8126e9e9df368f31b1f968d8fc80f99809b3Nico Weber      (Left.Type == TT_ObjCDictLiteral || Left.Type == TT_ObjCMethodExpr))
132932d28ee6061930b2a9c170cbaaeea028c88a7b89Daniel Jasper    return true;
133063d7cedca8616921c1908c88c2f23fcd67bbab99Daniel Jasper  if (Right.Type == TT_ObjCSelectorName)
133132d28ee6061930b2a9c170cbaaeea028c88a7b89Daniel Jasper    return true;
1332aa9e7b18a88d715b63e7b65d1b26a1759decc177Daniel Jasper  if (Left.is(tok::r_paren) && Line.Type == LT_ObjCProperty)
1333aa9e7b18a88d715b63e7b65d1b26a1759decc177Daniel Jasper    return true;
133432d28ee6061930b2a9c170cbaaeea028c88a7b89Daniel Jasper  if (Left.ClosesTemplateDeclaration)
133532d28ee6061930b2a9c170cbaaeea028c88a7b89Daniel Jasper    return true;
1336ab3ce592d027e3a10fb21e703cab1507f8d9bb03Daniel Jasper  if ((Right.Type == TT_ConditionalExpr &&
1337ab3ce592d027e3a10fb21e703cab1507f8d9bb03Daniel Jasper       !(Right.is(tok::colon) && Left.is(tok::question))) ||
1338ab3ce592d027e3a10fb21e703cab1507f8d9bb03Daniel Jasper      Right.is(tok::question))
133932d28ee6061930b2a9c170cbaaeea028c88a7b89Daniel Jasper    return true;
13406cabab48dacc1317821f8f078ed2d4c603b67affDaniel Jasper  if (Right.Type == TT_RangeBasedForLoopColon ||
1341c476ea976badd316e3afd0f34afe1f030a710117Daniel Jasper      Right.Type == TT_OverloadedOperatorLParen ||
1342c476ea976badd316e3afd0f34afe1f030a710117Daniel Jasper      Right.Type == TT_OverloadedOperator)
13436cabab48dacc1317821f8f078ed2d4c603b67affDaniel Jasper    return false;
1344c194c95036b7bf1281a6f2ed683f7c85ee5d2c20Daniel Jasper  if (Left.Type == TT_RangeBasedForLoopColon)
134532d28ee6061930b2a9c170cbaaeea028c88a7b89Daniel Jasper    return true;
13467d81281fc39f6d40d86be6600adba13c05b4a639Daniel Jasper  if (Right.Type == TT_RangeBasedForLoopColon)
13477d81281fc39f6d40d86be6600adba13c05b4a639Daniel Jasper    return false;
134832d28ee6061930b2a9c170cbaaeea028c88a7b89Daniel Jasper  if (Left.Type == TT_PointerOrReference || Left.Type == TT_TemplateCloser ||
134932d28ee6061930b2a9c170cbaaeea028c88a7b89Daniel Jasper      Left.Type == TT_UnaryOperator || Left.Type == TT_ConditionalExpr ||
1350e74de28ec3692986f3467a7f160a0e293277fa7eAlexander Kornienko      Left.isOneOf(tok::question, tok::kw_operator))
135132d28ee6061930b2a9c170cbaaeea028c88a7b89Daniel Jasper    return false;
135232d28ee6061930b2a9c170cbaaeea028c88a7b89Daniel Jasper  if (Left.is(tok::equal) && Line.Type == LT_VirtualFunctionDecl)
135332d28ee6061930b2a9c170cbaaeea028c88a7b89Daniel Jasper    return false;
1354198c8bfee0b33ab0ed0c54224f460868f1c18e95Daniel Jasper  if (Left.Previous) {
1355198c8bfee0b33ab0ed0c54224f460868f1c18e95Daniel Jasper    if (Left.is(tok::l_paren) && Right.is(tok::l_paren) &&
1356198c8bfee0b33ab0ed0c54224f460868f1c18e95Daniel Jasper        Left.Previous->is(tok::kw___attribute))
1357198c8bfee0b33ab0ed0c54224f460868f1c18e95Daniel Jasper      return false;
13582ca3741a962ded08866596577aaee2f4ab74c955Daniel Jasper    if (Left.is(tok::l_paren) && (Left.Previous->Type == TT_BinaryOperator ||
13595e2169f94fca20bbfda317c222b156751a431c13Daniel Jasper                                  Left.Previous->Type == TT_CastRParen))
1360198c8bfee0b33ab0ed0c54224f460868f1c18e95Daniel Jasper      return false;
1361198c8bfee0b33ab0ed0c54224f460868f1c18e95Daniel Jasper  }
136232d28ee6061930b2a9c170cbaaeea028c88a7b89Daniel Jasper
136365d2c3829494d254039683c73f95843c29c661b4Daniel Jasper  if (Right.isTrailingComment())
136432d28ee6061930b2a9c170cbaaeea028c88a7b89Daniel Jasper    // We rely on MustBreakBefore being set correctly here as we should not
136532d28ee6061930b2a9c170cbaaeea028c88a7b89Daniel Jasper    // change the "binding" behavior of a comment.
136632d28ee6061930b2a9c170cbaaeea028c88a7b89Daniel Jasper    return false;
136732d28ee6061930b2a9c170cbaaeea028c88a7b89Daniel Jasper
1368567dcf95424d69657f75e4bfd028967ca1f9eb8dDaniel Jasper  if (Right.is(tok::r_paren) || Right.Type == TT_TemplateCloser)
1369567dcf95424d69657f75e4bfd028967ca1f9eb8dDaniel Jasper    return false;
1370567dcf95424d69657f75e4bfd028967ca1f9eb8dDaniel Jasper
13715ad72bb8eb8e5cc4c061ccd28632295213d319dbDaniel Jasper  // We only break before r_brace if there was a corresponding break before
13725ad72bb8eb8e5cc4c061ccd28632295213d319dbDaniel Jasper  // the l_brace, which is tracked by BreakBeforeClosingBrace.
1373567dcf95424d69657f75e4bfd028967ca1f9eb8dDaniel Jasper  if (Right.is(tok::r_brace))
1374567dcf95424d69657f75e4bfd028967ca1f9eb8dDaniel Jasper    return Right.MatchingParen && Right.MatchingParen->BlockKind == BK_Block;
13755ad72bb8eb8e5cc4c061ccd28632295213d319dbDaniel Jasper
137632d28ee6061930b2a9c170cbaaeea028c88a7b89Daniel Jasper  // Allow breaking after a trailing 'const', e.g. after a method declaration,
137732d28ee6061930b2a9c170cbaaeea028c88a7b89Daniel Jasper  // unless it is follow by ';', '{' or '='.
1378b398701e4cbb9a55d90a60e3f4f4bc577446d098Manuel Klimek  if (Left.is(tok::kw_const) && Left.Previous != NULL &&
1379b398701e4cbb9a55d90a60e3f4f4bc577446d098Manuel Klimek      Left.Previous->is(tok::r_paren))
1380e74de28ec3692986f3467a7f160a0e293277fa7eAlexander Kornienko    return !Right.isOneOf(tok::l_brace, tok::semi, tok::equal);
138132d28ee6061930b2a9c170cbaaeea028c88a7b89Daniel Jasper
13828ef19a22956defa392df46c79e2d499ab7b16647Daniel Jasper  if (Right.is(tok::kw___attribute))
13838ef19a22956defa392df46c79e2d499ab7b16647Daniel Jasper    return true;
13848ef19a22956defa392df46c79e2d499ab7b16647Daniel Jasper
13853a204418482c9ae70ad482e781132c54306c3aa6Daniel Jasper  if (Left.is(tok::identifier) && Right.is(tok::string_literal))
13863a204418482c9ae70ad482e781132c54306c3aa6Daniel Jasper    return true;
1387e8b10d3d5b90efaf60ad89e96f6500f971ceec41Daniel Jasper
1388e8b10d3d5b90efaf60ad89e96f6500f971ceec41Daniel Jasper  if (Left.Type == TT_CtorInitializerComma &&
1389e8b10d3d5b90efaf60ad89e96f6500f971ceec41Daniel Jasper      Style.BreakConstructorInitializersBeforeComma)
1390e8b10d3d5b90efaf60ad89e96f6500f971ceec41Daniel Jasper    return false;
1391e8b10d3d5b90efaf60ad89e96f6500f971ceec41Daniel Jasper  if (Right.isBinaryOperator() && Style.BreakBeforeBinaryOperators)
1392e8b10d3d5b90efaf60ad89e96f6500f971ceec41Daniel Jasper    return true;
1393e8b10d3d5b90efaf60ad89e96f6500f971ceec41Daniel Jasper  return (Left.isBinaryOperator() && Left.isNot(tok::lessless) &&
1394e8b10d3d5b90efaf60ad89e96f6500f971ceec41Daniel Jasper          !Style.BreakBeforeBinaryOperators) ||
13956b119d63f5036344acd4e00a6ff2b3c72f26966fDaniel Jasper         Left.isOneOf(tok::comma, tok::coloncolon, tok::semi, tok::l_brace,
13966b119d63f5036344acd4e00a6ff2b3c72f26966fDaniel Jasper                      tok::kw_class, tok::kw_struct) ||
1397e74de28ec3692986f3467a7f160a0e293277fa7eAlexander Kornienko         Right.isOneOf(tok::lessless, tok::arrow, tok::period, tok::colon) ||
1398198c8bfee0b33ab0ed0c54224f460868f1c18e95Daniel Jasper         (Left.is(tok::r_paren) &&
1399e033e87cbe77341777100093f8066167888d4440Daniel Jasper          Right.isOneOf(tok::identifier, tok::kw_const, tok::kw___attribute)) ||
140032d28ee6061930b2a9c170cbaaeea028c88a7b89Daniel Jasper         (Left.is(tok::l_paren) && !Right.is(tok::r_paren)) ||
1401011c35dabb4c8abcb7389d8fbc6316f8f23576abDaniel Jasper         Right.is(tok::l_square);
140232d28ee6061930b2a9c170cbaaeea028c88a7b89Daniel Jasper}
140332d28ee6061930b2a9c170cbaaeea028c88a7b89Daniel Jasper
1404bf71ba2988b34c45af968f0965e28ac952e4a15fDaniel Jaspervoid TokenAnnotator::printDebugInfo(const AnnotatedLine &Line) {
1405bf71ba2988b34c45af968f0965e28ac952e4a15fDaniel Jasper  llvm::errs() << "AnnotatedTokens:\n";
1406b398701e4cbb9a55d90a60e3f4f4bc577446d098Manuel Klimek  const FormatToken *Tok = Line.First;
1407bf71ba2988b34c45af968f0965e28ac952e4a15fDaniel Jasper  while (Tok) {
1408b398701e4cbb9a55d90a60e3f4f4bc577446d098Manuel Klimek    llvm::errs() << " M=" << Tok->MustBreakBefore
1409c7bd68f9edcbca95e882d0ab18d09371f0bdb82cDaniel Jasper                 << " C=" << Tok->CanBreakBefore << " T=" << Tok->Type
1410c7bd68f9edcbca95e882d0ab18d09371f0bdb82cDaniel Jasper                 << " S=" << Tok->SpacesRequiredBefore
1411c7bd68f9edcbca95e882d0ab18d09371f0bdb82cDaniel Jasper                 << " P=" << Tok->SplitPenalty << " Name=" << Tok->Tok.getName()
1412c7bd68f9edcbca95e882d0ab18d09371f0bdb82cDaniel Jasper                 << " PPK=" << Tok->PackingKind << " FakeLParens=";
1413bf71ba2988b34c45af968f0965e28ac952e4a15fDaniel Jasper    for (unsigned i = 0, e = Tok->FakeLParens.size(); i != e; ++i)
1414bf71ba2988b34c45af968f0965e28ac952e4a15fDaniel Jasper      llvm::errs() << Tok->FakeLParens[i] << "/";
1415bf71ba2988b34c45af968f0965e28ac952e4a15fDaniel Jasper    llvm::errs() << " FakeRParens=" << Tok->FakeRParens << "\n";
1416b398701e4cbb9a55d90a60e3f4f4bc577446d098Manuel Klimek    Tok = Tok->Next;
1417bf71ba2988b34c45af968f0965e28ac952e4a15fDaniel Jasper  }
1418bf71ba2988b34c45af968f0965e28ac952e4a15fDaniel Jasper  llvm::errs() << "----\n";
1419bf71ba2988b34c45af968f0965e28ac952e4a15fDaniel Jasper}
1420bf71ba2988b34c45af968f0965e28ac952e4a15fDaniel Jasper
142132d28ee6061930b2a9c170cbaaeea028c88a7b89Daniel Jasper} // namespace format
142232d28ee6061930b2a9c170cbaaeea028c88a7b89Daniel Jasper} // namespace clang
1423