Daniel Jasper | 32d28ee | 2013-01-29 21:01:14 +0000 | [diff] [blame] | 1 | //===--- TokenAnnotator.cpp - Format C++ code -----------------------------===// |
| 2 | // |
| 3 | // The LLVM Compiler Infrastructure |
| 4 | // |
| 5 | // This file is distributed under the University of Illinois Open Source |
| 6 | // License. See LICENSE.TXT for details. |
| 7 | // |
| 8 | //===----------------------------------------------------------------------===// |
| 9 | /// |
| 10 | /// \file |
| 11 | /// \brief This file implements a token annotator, i.e. creates |
| 12 | /// \c AnnotatedTokens out of \c FormatTokens with required extra information. |
| 13 | /// |
| 14 | //===----------------------------------------------------------------------===// |
| 15 | |
| 16 | #include "TokenAnnotator.h" |
| 17 | #include "clang/Basic/SourceManager.h" |
| 18 | #include "clang/Lex/Lexer.h" |
Daniel Jasper | bf71ba2 | 2013-04-08 20:33:42 +0000 | [diff] [blame^] | 19 | #include "llvm/Support/Debug.h" |
Daniel Jasper | 32d28ee | 2013-01-29 21:01:14 +0000 | [diff] [blame] | 20 | |
| 21 | namespace clang { |
| 22 | namespace format { |
| 23 | |
Nico Weber | ee0feec | 2013-02-05 16:21:00 +0000 | [diff] [blame] | 24 | static bool isUnaryOperator(const AnnotatedToken &Tok) { |
| 25 | switch (Tok.FormatTok.Tok.getKind()) { |
| 26 | case tok::plus: |
| 27 | case tok::plusplus: |
| 28 | case tok::minus: |
| 29 | case tok::minusminus: |
| 30 | case tok::exclaim: |
| 31 | case tok::tilde: |
| 32 | case tok::kw_sizeof: |
| 33 | case tok::kw_alignof: |
| 34 | return true; |
| 35 | default: |
| 36 | return false; |
| 37 | } |
| 38 | } |
| 39 | |
Daniel Jasper | 32d28ee | 2013-01-29 21:01:14 +0000 | [diff] [blame] | 40 | static bool isBinaryOperator(const AnnotatedToken &Tok) { |
| 41 | // Comma is a binary operator, but does not behave as such wrt. formatting. |
| 42 | return getPrecedence(Tok) > prec::Comma; |
| 43 | } |
| 44 | |
Daniel Jasper | 0178673 | 2013-02-04 07:21:18 +0000 | [diff] [blame] | 45 | // Returns the previous token ignoring comments. |
Nico Weber | 4ed7f3e | 2013-02-06 16:54:35 +0000 | [diff] [blame] | 46 | static AnnotatedToken *getPreviousToken(AnnotatedToken &Tok) { |
| 47 | AnnotatedToken *PrevToken = Tok.Parent; |
Daniel Jasper | 0178673 | 2013-02-04 07:21:18 +0000 | [diff] [blame] | 48 | while (PrevToken != NULL && PrevToken->is(tok::comment)) |
| 49 | PrevToken = PrevToken->Parent; |
| 50 | return PrevToken; |
| 51 | } |
Nico Weber | 4ed7f3e | 2013-02-06 16:54:35 +0000 | [diff] [blame] | 52 | static const AnnotatedToken *getPreviousToken(const AnnotatedToken &Tok) { |
| 53 | return getPreviousToken(const_cast<AnnotatedToken &>(Tok)); |
| 54 | } |
Daniel Jasper | 0178673 | 2013-02-04 07:21:18 +0000 | [diff] [blame] | 55 | |
Daniel Jasper | 29f123b | 2013-02-08 15:28:42 +0000 | [diff] [blame] | 56 | static bool isTrailingComment(AnnotatedToken *Tok) { |
| 57 | return Tok != NULL && Tok->is(tok::comment) && |
| 58 | (Tok->Children.empty() || |
| 59 | Tok->Children[0].FormatTok.NewlinesBefore > 0); |
| 60 | } |
| 61 | |
Daniel Jasper | 0178673 | 2013-02-04 07:21:18 +0000 | [diff] [blame] | 62 | // Returns the next token ignoring comments. |
| 63 | static const AnnotatedToken *getNextToken(const AnnotatedToken &Tok) { |
| 64 | if (Tok.Children.empty()) |
| 65 | return NULL; |
| 66 | const AnnotatedToken *NextToken = &Tok.Children[0]; |
| 67 | while (NextToken->is(tok::comment)) { |
| 68 | if (NextToken->Children.empty()) |
| 69 | return NULL; |
| 70 | NextToken = &NextToken->Children[0]; |
| 71 | } |
| 72 | return NextToken; |
| 73 | } |
| 74 | |
Daniel Jasper | bf71ba2 | 2013-04-08 20:33:42 +0000 | [diff] [blame^] | 75 | bool closesScope(const AnnotatedToken &Tok) { |
Alexander Kornienko | e74de28 | 2013-03-13 14:41:29 +0000 | [diff] [blame] | 76 | return Tok.isOneOf(tok::r_paren, tok::r_brace, tok::r_square) || |
| 77 | Tok.Type == TT_TemplateCloser; |
| 78 | } |
| 79 | |
Daniel Jasper | bf71ba2 | 2013-04-08 20:33:42 +0000 | [diff] [blame^] | 80 | bool opensScope(const AnnotatedToken &Tok) { |
Alexander Kornienko | e74de28 | 2013-03-13 14:41:29 +0000 | [diff] [blame] | 81 | return Tok.isOneOf(tok::l_paren, tok::l_brace, tok::l_square) || |
| 82 | Tok.Type == TT_TemplateOpener; |
| 83 | } |
| 84 | |
Daniel Jasper | 32d28ee | 2013-01-29 21:01:14 +0000 | [diff] [blame] | 85 | /// \brief A parser that gathers additional information about tokens. |
| 86 | /// |
Alexander Kornienko | 3fd9ccd | 2013-03-12 16:28:18 +0000 | [diff] [blame] | 87 | /// The \c TokenAnnotator tries to match parenthesis and square brakets and |
Daniel Jasper | 32d28ee | 2013-01-29 21:01:14 +0000 | [diff] [blame] | 88 | /// store a parenthesis levels. It also tries to resolve matching "<" and ">" |
| 89 | /// into template parameter lists. |
| 90 | class AnnotatingParser { |
| 91 | public: |
Nico Weber | c2e6d2a | 2013-02-11 15:32:15 +0000 | [diff] [blame] | 92 | AnnotatingParser(SourceManager &SourceMgr, Lexer &Lex, AnnotatedLine &Line, |
| 93 | IdentifierInfo &Ident_in) |
Daniel Jasper | 0178673 | 2013-02-04 07:21:18 +0000 | [diff] [blame] | 94 | : SourceMgr(SourceMgr), Lex(Lex), Line(Line), CurrentToken(&Line.First), |
Nico Weber | c2e6d2a | 2013-02-11 15:32:15 +0000 | [diff] [blame] | 95 | KeywordVirtualFound(false), Ident_in(Ident_in) { |
Daniel Jasper | 923ebef | 2013-03-14 13:45:21 +0000 | [diff] [blame] | 96 | Contexts.push_back(Context(tok::unknown, 1, /*IsExpression=*/ false)); |
Daniel Jasper | 32d28ee | 2013-01-29 21:01:14 +0000 | [diff] [blame] | 97 | } |
| 98 | |
Nico Weber | 95e8e46 | 2013-02-12 16:17:07 +0000 | [diff] [blame] | 99 | private: |
Daniel Jasper | 32d28ee | 2013-01-29 21:01:14 +0000 | [diff] [blame] | 100 | bool parseAngle() { |
| 101 | if (CurrentToken == NULL) |
| 102 | return false; |
Daniel Jasper | 923ebef | 2013-03-14 13:45:21 +0000 | [diff] [blame] | 103 | ScopedContextCreator ContextCreator(*this, tok::less, 10); |
Daniel Jasper | 32d28ee | 2013-01-29 21:01:14 +0000 | [diff] [blame] | 104 | AnnotatedToken *Left = CurrentToken->Parent; |
Daniel Jasper | 4e77809 | 2013-02-06 10:05:46 +0000 | [diff] [blame] | 105 | Contexts.back().IsExpression = false; |
Daniel Jasper | 32d28ee | 2013-01-29 21:01:14 +0000 | [diff] [blame] | 106 | while (CurrentToken != NULL) { |
| 107 | if (CurrentToken->is(tok::greater)) { |
| 108 | Left->MatchingParen = CurrentToken; |
| 109 | CurrentToken->MatchingParen = Left; |
| 110 | CurrentToken->Type = TT_TemplateCloser; |
| 111 | next(); |
| 112 | return true; |
| 113 | } |
Alexander Kornienko | e74de28 | 2013-03-13 14:41:29 +0000 | [diff] [blame] | 114 | if (CurrentToken->isOneOf(tok::r_paren, tok::r_square, tok::r_brace, |
| 115 | tok::pipepipe, tok::ampamp, tok::question, |
| 116 | tok::colon)) |
Daniel Jasper | 32d28ee | 2013-01-29 21:01:14 +0000 | [diff] [blame] | 117 | return false; |
Daniel Jasper | 9fc56f2 | 2013-02-14 15:01:34 +0000 | [diff] [blame] | 118 | updateParameterCount(Left, CurrentToken); |
Daniel Jasper | 32d28ee | 2013-01-29 21:01:14 +0000 | [diff] [blame] | 119 | if (!consumeToken()) |
| 120 | return false; |
| 121 | } |
| 122 | return false; |
| 123 | } |
| 124 | |
| 125 | bool parseParens(bool LookForDecls = false) { |
| 126 | if (CurrentToken == NULL) |
| 127 | return false; |
Daniel Jasper | 923ebef | 2013-03-14 13:45:21 +0000 | [diff] [blame] | 128 | ScopedContextCreator ContextCreator(*this, tok::l_paren, 1); |
Daniel Jasper | 4e77809 | 2013-02-06 10:05:46 +0000 | [diff] [blame] | 129 | |
| 130 | // FIXME: This is a bit of a hack. Do better. |
| 131 | Contexts.back().ColonIsForRangeExpr = |
| 132 | Contexts.size() == 2 && Contexts[0].ColonIsForRangeExpr; |
| 133 | |
Daniel Jasper | 32d28ee | 2013-01-29 21:01:14 +0000 | [diff] [blame] | 134 | bool StartsObjCMethodExpr = false; |
| 135 | AnnotatedToken *Left = CurrentToken->Parent; |
| 136 | if (CurrentToken->is(tok::caret)) { |
| 137 | // ^( starts a block. |
| 138 | Left->Type = TT_ObjCBlockLParen; |
| 139 | } else if (AnnotatedToken *MaybeSel = Left->Parent) { |
| 140 | // @selector( starts a selector. |
| 141 | if (MaybeSel->isObjCAtKeyword(tok::objc_selector) && MaybeSel->Parent && |
| 142 | MaybeSel->Parent->is(tok::at)) { |
| 143 | StartsObjCMethodExpr = true; |
| 144 | } |
| 145 | } |
| 146 | |
Daniel Jasper | 4e77809 | 2013-02-06 10:05:46 +0000 | [diff] [blame] | 147 | if (StartsObjCMethodExpr) { |
| 148 | Contexts.back().ColonIsObjCMethodExpr = true; |
| 149 | Left->Type = TT_ObjCMethodExpr; |
| 150 | } |
Daniel Jasper | 32d28ee | 2013-01-29 21:01:14 +0000 | [diff] [blame] | 151 | |
| 152 | while (CurrentToken != NULL) { |
| 153 | // LookForDecls is set when "if (" has been seen. Check for |
| 154 | // 'identifier' '*' 'identifier' followed by not '=' -- this |
| 155 | // '*' has to be a binary operator but determineStarAmpUsage() will |
| 156 | // categorize it as an unary operator, so set the right type here. |
| 157 | if (LookForDecls && !CurrentToken->Children.empty()) { |
| 158 | AnnotatedToken &Prev = *CurrentToken->Parent; |
| 159 | AnnotatedToken &Next = CurrentToken->Children[0]; |
| 160 | if (Prev.Parent->is(tok::identifier) && |
Alexander Kornienko | e74de28 | 2013-03-13 14:41:29 +0000 | [diff] [blame] | 161 | Prev.isOneOf(tok::star, tok::amp, tok::ampamp) && |
Daniel Jasper | 32d28ee | 2013-01-29 21:01:14 +0000 | [diff] [blame] | 162 | CurrentToken->is(tok::identifier) && Next.isNot(tok::equal)) { |
| 163 | Prev.Type = TT_BinaryOperator; |
| 164 | LookForDecls = false; |
| 165 | } |
| 166 | } |
| 167 | |
| 168 | if (CurrentToken->is(tok::r_paren)) { |
| 169 | Left->MatchingParen = CurrentToken; |
| 170 | CurrentToken->MatchingParen = Left; |
| 171 | |
Daniel Jasper | 63d7ced | 2013-02-05 10:07:47 +0000 | [diff] [blame] | 172 | if (StartsObjCMethodExpr) { |
Daniel Jasper | 4e77809 | 2013-02-06 10:05:46 +0000 | [diff] [blame] | 173 | CurrentToken->Type = TT_ObjCMethodExpr; |
| 174 | if (Contexts.back().FirstObjCSelectorName != NULL) { |
| 175 | Contexts.back().FirstObjCSelectorName->LongestObjCSelectorName = |
| 176 | Contexts.back().LongestObjCSelectorName; |
Daniel Jasper | 63d7ced | 2013-02-05 10:07:47 +0000 | [diff] [blame] | 177 | } |
| 178 | } |
Daniel Jasper | 32d28ee | 2013-01-29 21:01:14 +0000 | [diff] [blame] | 179 | |
| 180 | next(); |
| 181 | return true; |
| 182 | } |
Alexander Kornienko | e74de28 | 2013-03-13 14:41:29 +0000 | [diff] [blame] | 183 | if (CurrentToken->isOneOf(tok::r_square, tok::r_brace)) |
Daniel Jasper | 32d28ee | 2013-01-29 21:01:14 +0000 | [diff] [blame] | 184 | return false; |
Daniel Jasper | 9fc56f2 | 2013-02-14 15:01:34 +0000 | [diff] [blame] | 185 | updateParameterCount(Left, CurrentToken); |
Daniel Jasper | 32d28ee | 2013-01-29 21:01:14 +0000 | [diff] [blame] | 186 | if (!consumeToken()) |
| 187 | return false; |
| 188 | } |
| 189 | return false; |
| 190 | } |
| 191 | |
| 192 | bool parseSquare() { |
| 193 | if (!CurrentToken) |
| 194 | return false; |
| 195 | |
| 196 | // A '[' could be an index subscript (after an indentifier or after |
Nico Weber | 051860e | 2013-02-10 02:08:05 +0000 | [diff] [blame] | 197 | // ')' or ']'), it could be the start of an Objective-C method |
| 198 | // expression, or it could the the start of an Objective-C array literal. |
Daniel Jasper | 32d28ee | 2013-01-29 21:01:14 +0000 | [diff] [blame] | 199 | AnnotatedToken *Left = CurrentToken->Parent; |
Nico Weber | 4ed7f3e | 2013-02-06 16:54:35 +0000 | [diff] [blame] | 200 | AnnotatedToken *Parent = getPreviousToken(*Left); |
Daniel Jasper | 32d28ee | 2013-01-29 21:01:14 +0000 | [diff] [blame] | 201 | bool StartsObjCMethodExpr = |
Daniel Jasper | 6f21a98 | 2013-03-13 07:49:51 +0000 | [diff] [blame] | 202 | Contexts.back().CanBeExpression && |
Alexander Kornienko | e74de28 | 2013-03-13 14:41:29 +0000 | [diff] [blame] | 203 | (!Parent || Parent->isOneOf(tok::colon, tok::l_square, tok::l_paren, |
| 204 | tok::kw_return, tok::kw_throw) || |
| 205 | isUnaryOperator(*Parent) || Parent->Type == TT_ObjCForIn || |
| 206 | Parent->Type == TT_CastRParen || |
Daniel Jasper | 6f21a98 | 2013-03-13 07:49:51 +0000 | [diff] [blame] | 207 | getBinOpPrecedence(Parent->FormatTok.Tok.getKind(), true, true) > |
Daniel Jasper | f9955d3 | 2013-03-20 12:37:50 +0000 | [diff] [blame] | 208 | prec::Unknown); |
Daniel Jasper | 923ebef | 2013-03-14 13:45:21 +0000 | [diff] [blame] | 209 | ScopedContextCreator ContextCreator(*this, tok::l_square, 10); |
Daniel Jasper | 6f21a98 | 2013-03-13 07:49:51 +0000 | [diff] [blame] | 210 | Contexts.back().IsExpression = true; |
Nico Weber | 051860e | 2013-02-10 02:08:05 +0000 | [diff] [blame] | 211 | bool StartsObjCArrayLiteral = Parent && Parent->is(tok::at); |
Daniel Jasper | 32d28ee | 2013-01-29 21:01:14 +0000 | [diff] [blame] | 212 | |
Daniel Jasper | 4e77809 | 2013-02-06 10:05:46 +0000 | [diff] [blame] | 213 | if (StartsObjCMethodExpr) { |
| 214 | Contexts.back().ColonIsObjCMethodExpr = true; |
| 215 | Left->Type = TT_ObjCMethodExpr; |
Nico Weber | 051860e | 2013-02-10 02:08:05 +0000 | [diff] [blame] | 216 | } else if (StartsObjCArrayLiteral) { |
| 217 | Left->Type = TT_ObjCArrayLiteral; |
Daniel Jasper | 4e77809 | 2013-02-06 10:05:46 +0000 | [diff] [blame] | 218 | } |
Daniel Jasper | 32d28ee | 2013-01-29 21:01:14 +0000 | [diff] [blame] | 219 | |
| 220 | while (CurrentToken != NULL) { |
| 221 | if (CurrentToken->is(tok::r_square)) { |
| 222 | if (!CurrentToken->Children.empty() && |
| 223 | CurrentToken->Children[0].is(tok::l_paren)) { |
Nico Weber | e8a9798 | 2013-02-06 06:20:11 +0000 | [diff] [blame] | 224 | // An ObjC method call is rarely followed by an open parenthesis. |
Daniel Jasper | 32d28ee | 2013-01-29 21:01:14 +0000 | [diff] [blame] | 225 | // FIXME: Do we incorrectly label ":" with this? |
| 226 | StartsObjCMethodExpr = false; |
| 227 | Left->Type = TT_Unknown; |
| 228 | } |
Daniel Jasper | 0178673 | 2013-02-04 07:21:18 +0000 | [diff] [blame] | 229 | if (StartsObjCMethodExpr) { |
Daniel Jasper | 4e77809 | 2013-02-06 10:05:46 +0000 | [diff] [blame] | 230 | CurrentToken->Type = TT_ObjCMethodExpr; |
Nico Weber | e8a9798 | 2013-02-06 06:20:11 +0000 | [diff] [blame] | 231 | // determineStarAmpUsage() thinks that '*' '[' is allocating an |
| 232 | // array of pointers, but if '[' starts a selector then '*' is a |
| 233 | // binary operator. |
Alexander Kornienko | 3fd9ccd | 2013-03-12 16:28:18 +0000 | [diff] [blame] | 234 | if (Parent != NULL && Parent->Type == TT_PointerOrReference) |
Nico Weber | 4ed7f3e | 2013-02-06 16:54:35 +0000 | [diff] [blame] | 235 | Parent->Type = TT_BinaryOperator; |
Nico Weber | 051860e | 2013-02-10 02:08:05 +0000 | [diff] [blame] | 236 | } else if (StartsObjCArrayLiteral) { |
| 237 | CurrentToken->Type = TT_ObjCArrayLiteral; |
Daniel Jasper | 0178673 | 2013-02-04 07:21:18 +0000 | [diff] [blame] | 238 | } |
Daniel Jasper | 32d28ee | 2013-01-29 21:01:14 +0000 | [diff] [blame] | 239 | Left->MatchingParen = CurrentToken; |
| 240 | CurrentToken->MatchingParen = Left; |
Daniel Jasper | 4e77809 | 2013-02-06 10:05:46 +0000 | [diff] [blame] | 241 | if (Contexts.back().FirstObjCSelectorName != NULL) |
| 242 | Contexts.back().FirstObjCSelectorName->LongestObjCSelectorName = |
| 243 | Contexts.back().LongestObjCSelectorName; |
Daniel Jasper | 32d28ee | 2013-01-29 21:01:14 +0000 | [diff] [blame] | 244 | next(); |
| 245 | return true; |
| 246 | } |
Alexander Kornienko | e74de28 | 2013-03-13 14:41:29 +0000 | [diff] [blame] | 247 | if (CurrentToken->isOneOf(tok::r_paren, tok::r_brace)) |
Daniel Jasper | 32d28ee | 2013-01-29 21:01:14 +0000 | [diff] [blame] | 248 | return false; |
Daniel Jasper | 9fc56f2 | 2013-02-14 15:01:34 +0000 | [diff] [blame] | 249 | updateParameterCount(Left, CurrentToken); |
Daniel Jasper | 32d28ee | 2013-01-29 21:01:14 +0000 | [diff] [blame] | 250 | if (!consumeToken()) |
| 251 | return false; |
| 252 | } |
| 253 | return false; |
| 254 | } |
| 255 | |
| 256 | bool parseBrace() { |
| 257 | // Lines are fine to end with '{'. |
| 258 | if (CurrentToken == NULL) |
| 259 | return true; |
Daniel Jasper | 923ebef | 2013-03-14 13:45:21 +0000 | [diff] [blame] | 260 | ScopedContextCreator ContextCreator(*this, tok::l_brace, 1); |
Daniel Jasper | 32d28ee | 2013-01-29 21:01:14 +0000 | [diff] [blame] | 261 | AnnotatedToken *Left = CurrentToken->Parent; |
| 262 | while (CurrentToken != NULL) { |
| 263 | if (CurrentToken->is(tok::r_brace)) { |
| 264 | Left->MatchingParen = CurrentToken; |
| 265 | CurrentToken->MatchingParen = Left; |
| 266 | next(); |
| 267 | return true; |
| 268 | } |
Alexander Kornienko | e74de28 | 2013-03-13 14:41:29 +0000 | [diff] [blame] | 269 | if (CurrentToken->isOneOf(tok::r_paren, tok::r_square)) |
Daniel Jasper | 32d28ee | 2013-01-29 21:01:14 +0000 | [diff] [blame] | 270 | return false; |
Daniel Jasper | 9fc56f2 | 2013-02-14 15:01:34 +0000 | [diff] [blame] | 271 | updateParameterCount(Left, CurrentToken); |
Daniel Jasper | 32d28ee | 2013-01-29 21:01:14 +0000 | [diff] [blame] | 272 | if (!consumeToken()) |
| 273 | return false; |
| 274 | } |
| 275 | return true; |
| 276 | } |
Daniel Jasper | c4615b7 | 2013-02-20 12:56:39 +0000 | [diff] [blame] | 277 | |
Daniel Jasper | 9fc56f2 | 2013-02-14 15:01:34 +0000 | [diff] [blame] | 278 | void updateParameterCount(AnnotatedToken *Left, AnnotatedToken *Current) { |
| 279 | if (Current->is(tok::comma)) |
| 280 | ++Left->ParameterCount; |
| 281 | else if (Left->ParameterCount == 0 && Current->isNot(tok::comment)) |
| 282 | Left->ParameterCount = 1; |
| 283 | } |
Daniel Jasper | 32d28ee | 2013-01-29 21:01:14 +0000 | [diff] [blame] | 284 | |
| 285 | bool parseConditional() { |
| 286 | while (CurrentToken != NULL) { |
| 287 | if (CurrentToken->is(tok::colon)) { |
| 288 | CurrentToken->Type = TT_ConditionalExpr; |
| 289 | next(); |
| 290 | return true; |
| 291 | } |
| 292 | if (!consumeToken()) |
| 293 | return false; |
| 294 | } |
| 295 | return false; |
| 296 | } |
| 297 | |
| 298 | bool parseTemplateDeclaration() { |
| 299 | if (CurrentToken != NULL && CurrentToken->is(tok::less)) { |
| 300 | CurrentToken->Type = TT_TemplateOpener; |
| 301 | next(); |
| 302 | if (!parseAngle()) |
| 303 | return false; |
Daniel Jasper | 34511fb | 2013-02-19 17:14:38 +0000 | [diff] [blame] | 304 | if (CurrentToken != NULL) |
| 305 | CurrentToken->Parent->ClosesTemplateDeclaration = true; |
Daniel Jasper | 32d28ee | 2013-01-29 21:01:14 +0000 | [diff] [blame] | 306 | return true; |
| 307 | } |
| 308 | return false; |
| 309 | } |
| 310 | |
| 311 | bool consumeToken() { |
| 312 | AnnotatedToken *Tok = CurrentToken; |
| 313 | next(); |
| 314 | switch (Tok->FormatTok.Tok.getKind()) { |
| 315 | case tok::plus: |
| 316 | case tok::minus: |
Daniel Jasper | 627707b | 2013-03-22 16:55:40 +0000 | [diff] [blame] | 317 | if (Tok->Parent == NULL && Line.MustBeDeclaration) |
Daniel Jasper | 32d28ee | 2013-01-29 21:01:14 +0000 | [diff] [blame] | 318 | Tok->Type = TT_ObjCMethodSpecifier; |
| 319 | break; |
| 320 | case tok::colon: |
Daniel Jasper | cf6d76a | 2013-03-18 12:50:26 +0000 | [diff] [blame] | 321 | if (Tok->Parent == NULL) |
| 322 | return false; |
Daniel Jasper | 32d28ee | 2013-01-29 21:01:14 +0000 | [diff] [blame] | 323 | // Colons from ?: are handled in parseConditional(). |
Daniel Jasper | 923ebef | 2013-03-14 13:45:21 +0000 | [diff] [blame] | 324 | if (Tok->Parent->is(tok::r_paren) && Contexts.size() == 1) { |
Daniel Jasper | 32d28ee | 2013-01-29 21:01:14 +0000 | [diff] [blame] | 325 | Tok->Type = TT_CtorInitializerColon; |
Daniel Jasper | 4e77809 | 2013-02-06 10:05:46 +0000 | [diff] [blame] | 326 | } else if (Contexts.back().ColonIsObjCMethodExpr || |
Daniel Jasper | 63d7ced | 2013-02-05 10:07:47 +0000 | [diff] [blame] | 327 | Line.First.Type == TT_ObjCMethodSpecifier) { |
Daniel Jasper | 32d28ee | 2013-01-29 21:01:14 +0000 | [diff] [blame] | 328 | Tok->Type = TT_ObjCMethodExpr; |
Daniel Jasper | 63d7ced | 2013-02-05 10:07:47 +0000 | [diff] [blame] | 329 | Tok->Parent->Type = TT_ObjCSelectorName; |
Daniel Jasper | 4e77809 | 2013-02-06 10:05:46 +0000 | [diff] [blame] | 330 | if (Tok->Parent->FormatTok.TokenLength > |
Daniel Jasper | f9955d3 | 2013-03-20 12:37:50 +0000 | [diff] [blame] | 331 | Contexts.back().LongestObjCSelectorName) |
Daniel Jasper | 4e77809 | 2013-02-06 10:05:46 +0000 | [diff] [blame] | 332 | Contexts.back().LongestObjCSelectorName = |
| 333 | Tok->Parent->FormatTok.TokenLength; |
| 334 | if (Contexts.back().FirstObjCSelectorName == NULL) |
| 335 | Contexts.back().FirstObjCSelectorName = Tok->Parent; |
| 336 | } else if (Contexts.back().ColonIsForRangeExpr) { |
Daniel Jasper | 32d28ee | 2013-01-29 21:01:14 +0000 | [diff] [blame] | 337 | Tok->Type = TT_RangeBasedForLoopColon; |
Daniel Jasper | 6cabab4 | 2013-02-14 08:42:54 +0000 | [diff] [blame] | 338 | } else if (Contexts.size() == 1) { |
| 339 | Tok->Type = TT_InheritanceColon; |
Daniel Jasper | 923ebef | 2013-03-14 13:45:21 +0000 | [diff] [blame] | 340 | } else if (Contexts.back().ContextKind == tok::l_paren) { |
| 341 | Tok->Type = TT_InlineASMColon; |
Daniel Jasper | 63d7ced | 2013-02-05 10:07:47 +0000 | [diff] [blame] | 342 | } |
Daniel Jasper | 32d28ee | 2013-01-29 21:01:14 +0000 | [diff] [blame] | 343 | break; |
| 344 | case tok::kw_if: |
| 345 | case tok::kw_while: |
| 346 | if (CurrentToken != NULL && CurrentToken->is(tok::l_paren)) { |
| 347 | next(); |
| 348 | if (!parseParens(/*LookForDecls=*/ true)) |
| 349 | return false; |
| 350 | } |
| 351 | break; |
| 352 | case tok::kw_for: |
Daniel Jasper | 4e77809 | 2013-02-06 10:05:46 +0000 | [diff] [blame] | 353 | Contexts.back().ColonIsForRangeExpr = true; |
Daniel Jasper | 32d28ee | 2013-01-29 21:01:14 +0000 | [diff] [blame] | 354 | next(); |
| 355 | if (!parseParens()) |
| 356 | return false; |
| 357 | break; |
| 358 | case tok::l_paren: |
| 359 | if (!parseParens()) |
| 360 | return false; |
Daniel Jasper | 3c08a81 | 2013-02-24 18:54:32 +0000 | [diff] [blame] | 361 | if (Line.MustBeDeclaration) |
| 362 | Line.MightBeFunctionDecl = true; |
Daniel Jasper | 32d28ee | 2013-01-29 21:01:14 +0000 | [diff] [blame] | 363 | break; |
| 364 | case tok::l_square: |
| 365 | if (!parseSquare()) |
| 366 | return false; |
| 367 | break; |
| 368 | case tok::l_brace: |
| 369 | if (!parseBrace()) |
| 370 | return false; |
| 371 | break; |
| 372 | case tok::less: |
| 373 | if (parseAngle()) |
| 374 | Tok->Type = TT_TemplateOpener; |
| 375 | else { |
| 376 | Tok->Type = TT_BinaryOperator; |
| 377 | CurrentToken = Tok; |
| 378 | next(); |
| 379 | } |
| 380 | break; |
| 381 | case tok::r_paren: |
| 382 | case tok::r_square: |
| 383 | return false; |
| 384 | case tok::r_brace: |
| 385 | // Lines can start with '}'. |
| 386 | if (Tok->Parent != NULL) |
| 387 | return false; |
| 388 | break; |
| 389 | case tok::greater: |
| 390 | Tok->Type = TT_BinaryOperator; |
| 391 | break; |
| 392 | case tok::kw_operator: |
Daniel Jasper | 2b4c924 | 2013-02-11 08:01:18 +0000 | [diff] [blame] | 393 | while (CurrentToken && CurrentToken->isNot(tok::l_paren)) { |
Alexander Kornienko | e74de28 | 2013-03-13 14:41:29 +0000 | [diff] [blame] | 394 | if (CurrentToken->isOneOf(tok::star, tok::amp)) |
Daniel Jasper | 2b4c924 | 2013-02-11 08:01:18 +0000 | [diff] [blame] | 395 | CurrentToken->Type = TT_PointerOrReference; |
| 396 | consumeToken(); |
Daniel Jasper | 32d28ee | 2013-01-29 21:01:14 +0000 | [diff] [blame] | 397 | } |
Daniel Jasper | 2b4c924 | 2013-02-11 08:01:18 +0000 | [diff] [blame] | 398 | if (CurrentToken) |
| 399 | CurrentToken->Type = TT_OverloadedOperatorLParen; |
Daniel Jasper | 32d28ee | 2013-01-29 21:01:14 +0000 | [diff] [blame] | 400 | break; |
| 401 | case tok::question: |
| 402 | parseConditional(); |
| 403 | break; |
| 404 | case tok::kw_template: |
| 405 | parseTemplateDeclaration(); |
| 406 | break; |
Nico Weber | c2e6d2a | 2013-02-11 15:32:15 +0000 | [diff] [blame] | 407 | case tok::identifier: |
| 408 | if (Line.First.is(tok::kw_for) && |
| 409 | Tok->FormatTok.Tok.getIdentifierInfo() == &Ident_in) |
| 410 | Tok->Type = TT_ObjCForIn; |
| 411 | break; |
Daniel Jasper | 8ed9f2b | 2013-04-03 13:36:17 +0000 | [diff] [blame] | 412 | case tok::comma: |
| 413 | if (Contexts.back().FirstStartOfName) |
| 414 | Contexts.back().FirstStartOfName->PartOfMultiVariableDeclStmt = true; |
| 415 | break; |
Daniel Jasper | 32d28ee | 2013-01-29 21:01:14 +0000 | [diff] [blame] | 416 | default: |
| 417 | break; |
| 418 | } |
| 419 | return true; |
| 420 | } |
| 421 | |
| 422 | void parseIncludeDirective() { |
| 423 | next(); |
| 424 | if (CurrentToken != NULL && CurrentToken->is(tok::less)) { |
| 425 | next(); |
| 426 | while (CurrentToken != NULL) { |
| 427 | if (CurrentToken->isNot(tok::comment) || |
| 428 | !CurrentToken->Children.empty()) |
| 429 | CurrentToken->Type = TT_ImplicitStringLiteral; |
| 430 | next(); |
| 431 | } |
| 432 | } else { |
| 433 | while (CurrentToken != NULL) { |
Daniel Jasper | 3a20441 | 2013-02-23 07:46:38 +0000 | [diff] [blame] | 434 | if (CurrentToken->is(tok::string_literal)) |
| 435 | // Mark these string literals as "implicit" literals, too, so that |
| 436 | // they are not split or line-wrapped. |
| 437 | CurrentToken->Type = TT_ImplicitStringLiteral; |
Daniel Jasper | 32d28ee | 2013-01-29 21:01:14 +0000 | [diff] [blame] | 438 | next(); |
| 439 | } |
| 440 | } |
| 441 | } |
| 442 | |
| 443 | void parseWarningOrError() { |
| 444 | next(); |
| 445 | // We still want to format the whitespace left of the first token of the |
| 446 | // warning or error. |
| 447 | next(); |
| 448 | while (CurrentToken != NULL) { |
| 449 | CurrentToken->Type = TT_ImplicitStringLiteral; |
| 450 | next(); |
| 451 | } |
| 452 | } |
| 453 | |
| 454 | void parsePreprocessorDirective() { |
| 455 | next(); |
| 456 | if (CurrentToken == NULL) |
| 457 | return; |
| 458 | // Hashes in the middle of a line can lead to any strange token |
| 459 | // sequence. |
| 460 | if (CurrentToken->FormatTok.Tok.getIdentifierInfo() == NULL) |
| 461 | return; |
| 462 | switch (CurrentToken->FormatTok.Tok.getIdentifierInfo()->getPPKeywordID()) { |
| 463 | case tok::pp_include: |
| 464 | case tok::pp_import: |
| 465 | parseIncludeDirective(); |
| 466 | break; |
| 467 | case tok::pp_error: |
| 468 | case tok::pp_warning: |
| 469 | parseWarningOrError(); |
| 470 | break; |
| 471 | default: |
| 472 | break; |
| 473 | } |
Daniel Jasper | 5b7e7b0 | 2013-02-05 09:34:14 +0000 | [diff] [blame] | 474 | while (CurrentToken != NULL) |
| 475 | next(); |
Daniel Jasper | 32d28ee | 2013-01-29 21:01:14 +0000 | [diff] [blame] | 476 | } |
| 477 | |
Nico Weber | 95e8e46 | 2013-02-12 16:17:07 +0000 | [diff] [blame] | 478 | public: |
Daniel Jasper | 32d28ee | 2013-01-29 21:01:14 +0000 | [diff] [blame] | 479 | LineType parseLine() { |
| 480 | int PeriodsAndArrows = 0; |
Daniel Jasper | 2484971 | 2013-03-01 16:48:32 +0000 | [diff] [blame] | 481 | AnnotatedToken *LastPeriodOrArrow = NULL; |
Daniel Jasper | 32d28ee | 2013-01-29 21:01:14 +0000 | [diff] [blame] | 482 | bool CanBeBuilderTypeStmt = true; |
| 483 | if (CurrentToken->is(tok::hash)) { |
| 484 | parsePreprocessorDirective(); |
| 485 | return LT_PreprocessorDirective; |
| 486 | } |
| 487 | while (CurrentToken != NULL) { |
| 488 | if (CurrentToken->is(tok::kw_virtual)) |
| 489 | KeywordVirtualFound = true; |
Alexander Kornienko | e74de28 | 2013-03-13 14:41:29 +0000 | [diff] [blame] | 490 | if (CurrentToken->isOneOf(tok::period, tok::arrow)) { |
Daniel Jasper | 32d28ee | 2013-01-29 21:01:14 +0000 | [diff] [blame] | 491 | ++PeriodsAndArrows; |
Daniel Jasper | 2484971 | 2013-03-01 16:48:32 +0000 | [diff] [blame] | 492 | LastPeriodOrArrow = CurrentToken; |
| 493 | } |
Daniel Jasper | 4a544e5 | 2013-02-15 20:33:06 +0000 | [diff] [blame] | 494 | AnnotatedToken *TheToken = CurrentToken; |
Daniel Jasper | 32d28ee | 2013-01-29 21:01:14 +0000 | [diff] [blame] | 495 | if (!consumeToken()) |
| 496 | return LT_Invalid; |
Daniel Jasper | 4a544e5 | 2013-02-15 20:33:06 +0000 | [diff] [blame] | 497 | if (getPrecedence(*TheToken) > prec::Assignment && |
Daniel Jasper | 82282dc | 2013-02-18 13:52:06 +0000 | [diff] [blame] | 498 | TheToken->Type == TT_BinaryOperator) |
Daniel Jasper | 4a544e5 | 2013-02-15 20:33:06 +0000 | [diff] [blame] | 499 | CanBeBuilderTypeStmt = false; |
Daniel Jasper | 32d28ee | 2013-01-29 21:01:14 +0000 | [diff] [blame] | 500 | } |
| 501 | if (KeywordVirtualFound) |
| 502 | return LT_VirtualFunctionDecl; |
| 503 | |
| 504 | // Assume a builder-type call if there are 2 or more "." and "->". |
Daniel Jasper | 2484971 | 2013-03-01 16:48:32 +0000 | [diff] [blame] | 505 | if (PeriodsAndArrows >= 2 && CanBeBuilderTypeStmt) { |
| 506 | LastPeriodOrArrow->LastInChainOfCalls = true; |
Daniel Jasper | 32d28ee | 2013-01-29 21:01:14 +0000 | [diff] [blame] | 507 | return LT_BuilderTypeCall; |
Daniel Jasper | 2484971 | 2013-03-01 16:48:32 +0000 | [diff] [blame] | 508 | } |
Daniel Jasper | 32d28ee | 2013-01-29 21:01:14 +0000 | [diff] [blame] | 509 | |
Daniel Jasper | 63d7ced | 2013-02-05 10:07:47 +0000 | [diff] [blame] | 510 | if (Line.First.Type == TT_ObjCMethodSpecifier) { |
Daniel Jasper | 4e77809 | 2013-02-06 10:05:46 +0000 | [diff] [blame] | 511 | if (Contexts.back().FirstObjCSelectorName != NULL) |
| 512 | Contexts.back().FirstObjCSelectorName->LongestObjCSelectorName = |
| 513 | Contexts.back().LongestObjCSelectorName; |
Daniel Jasper | 63d7ced | 2013-02-05 10:07:47 +0000 | [diff] [blame] | 514 | return LT_ObjCMethodDecl; |
| 515 | } |
| 516 | |
Daniel Jasper | 32d28ee | 2013-01-29 21:01:14 +0000 | [diff] [blame] | 517 | return LT_Other; |
| 518 | } |
| 519 | |
Nico Weber | 95e8e46 | 2013-02-12 16:17:07 +0000 | [diff] [blame] | 520 | private: |
Daniel Jasper | 32d28ee | 2013-01-29 21:01:14 +0000 | [diff] [blame] | 521 | void next() { |
Daniel Jasper | 0178673 | 2013-02-04 07:21:18 +0000 | [diff] [blame] | 522 | if (CurrentToken != NULL) { |
| 523 | determineTokenType(*CurrentToken); |
Daniel Jasper | 4e77809 | 2013-02-06 10:05:46 +0000 | [diff] [blame] | 524 | CurrentToken->BindingStrength = Contexts.back().BindingStrength; |
Daniel Jasper | 0178673 | 2013-02-04 07:21:18 +0000 | [diff] [blame] | 525 | } |
| 526 | |
Daniel Jasper | 32d28ee | 2013-01-29 21:01:14 +0000 | [diff] [blame] | 527 | if (CurrentToken != NULL && !CurrentToken->Children.empty()) |
| 528 | CurrentToken = &CurrentToken->Children[0]; |
| 529 | else |
| 530 | CurrentToken = NULL; |
Daniel Jasper | d0f349b | 2013-02-18 12:44:35 +0000 | [diff] [blame] | 531 | |
| 532 | // Reset token type in case we have already looked at it and then recovered |
| 533 | // from an error (e.g. failure to find the matching >). |
| 534 | if (CurrentToken != NULL) |
| 535 | CurrentToken->Type = TT_Unknown; |
Daniel Jasper | 32d28ee | 2013-01-29 21:01:14 +0000 | [diff] [blame] | 536 | } |
| 537 | |
Daniel Jasper | 4e77809 | 2013-02-06 10:05:46 +0000 | [diff] [blame] | 538 | /// \brief A struct to hold information valid in a specific context, e.g. |
| 539 | /// a pair of parenthesis. |
| 540 | struct Context { |
Daniel Jasper | 923ebef | 2013-03-14 13:45:21 +0000 | [diff] [blame] | 541 | Context(tok::TokenKind ContextKind, unsigned BindingStrength, |
| 542 | bool IsExpression) |
| 543 | : ContextKind(ContextKind), BindingStrength(BindingStrength), |
| 544 | LongestObjCSelectorName(0), ColonIsForRangeExpr(false), |
| 545 | ColonIsObjCMethodExpr(false), FirstObjCSelectorName(NULL), |
Daniel Jasper | 8ed9f2b | 2013-04-03 13:36:17 +0000 | [diff] [blame] | 546 | FirstStartOfName(NULL), IsExpression(IsExpression), |
| 547 | CanBeExpression(true) {} |
Daniel Jasper | 0178673 | 2013-02-04 07:21:18 +0000 | [diff] [blame] | 548 | |
Daniel Jasper | 923ebef | 2013-03-14 13:45:21 +0000 | [diff] [blame] | 549 | tok::TokenKind ContextKind; |
Daniel Jasper | 4e77809 | 2013-02-06 10:05:46 +0000 | [diff] [blame] | 550 | unsigned BindingStrength; |
| 551 | unsigned LongestObjCSelectorName; |
| 552 | bool ColonIsForRangeExpr; |
| 553 | bool ColonIsObjCMethodExpr; |
| 554 | AnnotatedToken *FirstObjCSelectorName; |
Daniel Jasper | 8ed9f2b | 2013-04-03 13:36:17 +0000 | [diff] [blame] | 555 | AnnotatedToken *FirstStartOfName; |
Daniel Jasper | 4e77809 | 2013-02-06 10:05:46 +0000 | [diff] [blame] | 556 | bool IsExpression; |
Daniel Jasper | 6f21a98 | 2013-03-13 07:49:51 +0000 | [diff] [blame] | 557 | bool CanBeExpression; |
Daniel Jasper | 4e77809 | 2013-02-06 10:05:46 +0000 | [diff] [blame] | 558 | }; |
| 559 | |
| 560 | /// \brief Puts a new \c Context onto the stack \c Contexts for the lifetime |
| 561 | /// of each instance. |
| 562 | struct ScopedContextCreator { |
| 563 | AnnotatingParser &P; |
| 564 | |
Daniel Jasper | 923ebef | 2013-03-14 13:45:21 +0000 | [diff] [blame] | 565 | ScopedContextCreator(AnnotatingParser &P, tok::TokenKind ContextKind, |
| 566 | unsigned Increase) |
| 567 | : P(P) { |
| 568 | P.Contexts.push_back( |
| 569 | Context(ContextKind, P.Contexts.back().BindingStrength + Increase, |
| 570 | P.Contexts.back().IsExpression)); |
Daniel Jasper | 4e77809 | 2013-02-06 10:05:46 +0000 | [diff] [blame] | 571 | } |
| 572 | |
| 573 | ~ScopedContextCreator() { P.Contexts.pop_back(); } |
| 574 | }; |
Daniel Jasper | 0178673 | 2013-02-04 07:21:18 +0000 | [diff] [blame] | 575 | |
| 576 | void determineTokenType(AnnotatedToken &Current) { |
| 577 | if (getPrecedence(Current) == prec::Assignment) { |
Daniel Jasper | 4e77809 | 2013-02-06 10:05:46 +0000 | [diff] [blame] | 578 | Contexts.back().IsExpression = true; |
Nico Weber | 95e8e46 | 2013-02-12 16:17:07 +0000 | [diff] [blame] | 579 | for (AnnotatedToken *Previous = Current.Parent; |
| 580 | Previous && Previous->isNot(tok::comma); |
| 581 | Previous = Previous->Parent) { |
Daniel Jasper | 9c65b06 | 2013-02-27 11:43:50 +0000 | [diff] [blame] | 582 | if (Previous->is(tok::r_square)) |
| 583 | Previous = Previous->MatchingParen; |
Daniel Jasper | 0178673 | 2013-02-04 07:21:18 +0000 | [diff] [blame] | 584 | if (Previous->Type == TT_BinaryOperator && |
Alexander Kornienko | e74de28 | 2013-03-13 14:41:29 +0000 | [diff] [blame] | 585 | Previous->isOneOf(tok::star, tok::amp)) { |
Daniel Jasper | 0178673 | 2013-02-04 07:21:18 +0000 | [diff] [blame] | 586 | Previous->Type = TT_PointerOrReference; |
| 587 | } |
Daniel Jasper | 0178673 | 2013-02-04 07:21:18 +0000 | [diff] [blame] | 588 | } |
Alexander Kornienko | e74de28 | 2013-03-13 14:41:29 +0000 | [diff] [blame] | 589 | } else if (Current.isOneOf(tok::kw_return, tok::kw_throw) || |
Nico Weber | 95e8e46 | 2013-02-12 16:17:07 +0000 | [diff] [blame] | 590 | (Current.is(tok::l_paren) && !Line.MustBeDeclaration && |
| 591 | (!Current.Parent || Current.Parent->isNot(tok::kw_for)))) { |
Daniel Jasper | 4e77809 | 2013-02-06 10:05:46 +0000 | [diff] [blame] | 592 | Contexts.back().IsExpression = true; |
Alexander Kornienko | e74de28 | 2013-03-13 14:41:29 +0000 | [diff] [blame] | 593 | } else if (Current.isOneOf(tok::r_paren, tok::greater, tok::comma)) { |
Nico Weber | 95e8e46 | 2013-02-12 16:17:07 +0000 | [diff] [blame] | 594 | for (AnnotatedToken *Previous = Current.Parent; |
Alexander Kornienko | e74de28 | 2013-03-13 14:41:29 +0000 | [diff] [blame] | 595 | Previous && Previous->isOneOf(tok::star, tok::amp); |
Nico Weber | 95e8e46 | 2013-02-12 16:17:07 +0000 | [diff] [blame] | 596 | Previous = Previous->Parent) |
| 597 | Previous->Type = TT_PointerOrReference; |
Daniel Jasper | d0f349b | 2013-02-18 12:44:35 +0000 | [diff] [blame] | 598 | } else if (Current.Parent && |
| 599 | Current.Parent->Type == TT_CtorInitializerColon) { |
| 600 | Contexts.back().IsExpression = true; |
Daniel Jasper | 6f21a98 | 2013-03-13 07:49:51 +0000 | [diff] [blame] | 601 | } else if (Current.is(tok::kw_new)) { |
| 602 | Contexts.back().CanBeExpression = false; |
Nico Weber | 95e8e46 | 2013-02-12 16:17:07 +0000 | [diff] [blame] | 603 | } |
Daniel Jasper | 0178673 | 2013-02-04 07:21:18 +0000 | [diff] [blame] | 604 | |
| 605 | if (Current.Type == TT_Unknown) { |
Daniel Jasper | 3c08a81 | 2013-02-24 18:54:32 +0000 | [diff] [blame] | 606 | if (Current.Parent && Current.is(tok::identifier) && |
| 607 | ((Current.Parent->is(tok::identifier) && |
| 608 | Current.Parent->FormatTok.Tok.getIdentifierInfo() |
| 609 | ->getPPKeywordID() == tok::pp_not_keyword) || |
Daniel Jasper | 8ed9f2b | 2013-04-03 13:36:17 +0000 | [diff] [blame] | 610 | isSimpleTypeSpecifier(*Current.Parent) || |
Daniel Jasper | 3c08a81 | 2013-02-24 18:54:32 +0000 | [diff] [blame] | 611 | Current.Parent->Type == TT_PointerOrReference || |
| 612 | Current.Parent->Type == TT_TemplateCloser)) { |
Daniel Jasper | 8ed9f2b | 2013-04-03 13:36:17 +0000 | [diff] [blame] | 613 | Contexts.back().FirstStartOfName = &Current; |
Daniel Jasper | 3c08a81 | 2013-02-24 18:54:32 +0000 | [diff] [blame] | 614 | Current.Type = TT_StartOfName; |
Alexander Kornienko | e74de28 | 2013-03-13 14:41:29 +0000 | [diff] [blame] | 615 | } else if (Current.isOneOf(tok::star, tok::amp, tok::ampamp)) { |
Daniel Jasper | 4e77809 | 2013-02-06 10:05:46 +0000 | [diff] [blame] | 616 | Current.Type = |
| 617 | determineStarAmpUsage(Current, Contexts.back().IsExpression); |
Alexander Kornienko | e74de28 | 2013-03-13 14:41:29 +0000 | [diff] [blame] | 618 | } else if (Current.isOneOf(tok::minus, tok::plus, tok::caret)) { |
Daniel Jasper | 0178673 | 2013-02-04 07:21:18 +0000 | [diff] [blame] | 619 | Current.Type = determinePlusMinusCaretUsage(Current); |
Alexander Kornienko | e74de28 | 2013-03-13 14:41:29 +0000 | [diff] [blame] | 620 | } else if (Current.isOneOf(tok::minusminus, tok::plusplus)) { |
Daniel Jasper | 0178673 | 2013-02-04 07:21:18 +0000 | [diff] [blame] | 621 | Current.Type = determineIncrementUsage(Current); |
| 622 | } else if (Current.is(tok::exclaim)) { |
| 623 | Current.Type = TT_UnaryOperator; |
| 624 | } else if (isBinaryOperator(Current)) { |
| 625 | Current.Type = TT_BinaryOperator; |
| 626 | } else if (Current.is(tok::comment)) { |
| 627 | std::string Data(Lexer::getSpelling(Current.FormatTok.Tok, SourceMgr, |
| 628 | Lex.getLangOpts())); |
| 629 | if (StringRef(Data).startswith("//")) |
| 630 | Current.Type = TT_LineComment; |
| 631 | else |
| 632 | Current.Type = TT_BlockComment; |
Nico Weber | 37d6931 | 2013-02-13 04:13:13 +0000 | [diff] [blame] | 633 | } else if (Current.is(tok::r_paren)) { |
Daniel Jasper | 03628b8 | 2013-02-19 20:05:41 +0000 | [diff] [blame] | 634 | bool ParensNotExpr = !Current.Parent || |
| 635 | Current.Parent->Type == TT_PointerOrReference || |
Nico Weber | 37d6931 | 2013-02-13 04:13:13 +0000 | [diff] [blame] | 636 | Current.Parent->Type == TT_TemplateCloser; |
| 637 | bool ParensCouldEndDecl = |
Alexander Kornienko | e74de28 | 2013-03-13 14:41:29 +0000 | [diff] [blame] | 638 | !Current.Children.empty() && |
| 639 | Current.Children[0].isOneOf(tok::equal, tok::semi, tok::l_brace); |
Daniel Jasper | 6a365aa | 2013-03-13 17:13:53 +0000 | [diff] [blame] | 640 | bool IsSizeOfOrAlignOf = |
| 641 | Current.MatchingParen && Current.MatchingParen->Parent && |
| 642 | Current.MatchingParen->Parent->isOneOf(tok::kw_sizeof, |
| 643 | tok::kw_alignof); |
| 644 | if (ParensNotExpr && !ParensCouldEndDecl && !IsSizeOfOrAlignOf && |
Daniel Jasper | 37eff83 | 2013-02-23 08:07:18 +0000 | [diff] [blame] | 645 | Contexts.back().IsExpression) |
Nico Weber | 37d6931 | 2013-02-13 04:13:13 +0000 | [diff] [blame] | 646 | // FIXME: We need to get smarter and understand more cases of casts. |
| 647 | Current.Type = TT_CastRParen; |
Daniel Jasper | 0178673 | 2013-02-04 07:21:18 +0000 | [diff] [blame] | 648 | } else if (Current.is(tok::at) && Current.Children.size()) { |
| 649 | switch (Current.Children[0].FormatTok.Tok.getObjCKeywordID()) { |
| 650 | case tok::objc_interface: |
| 651 | case tok::objc_implementation: |
| 652 | case tok::objc_protocol: |
| 653 | Current.Type = TT_ObjCDecl; |
| 654 | break; |
| 655 | case tok::objc_property: |
| 656 | Current.Type = TT_ObjCProperty; |
| 657 | break; |
| 658 | default: |
| 659 | break; |
| 660 | } |
| 661 | } |
| 662 | } |
| 663 | } |
| 664 | |
Daniel Jasper | 0178673 | 2013-02-04 07:21:18 +0000 | [diff] [blame] | 665 | /// \brief Return the type of the given token assuming it is * or &. |
| 666 | TokenType |
| 667 | determineStarAmpUsage(const AnnotatedToken &Tok, bool IsExpression) { |
| 668 | const AnnotatedToken *PrevToken = getPreviousToken(Tok); |
| 669 | if (PrevToken == NULL) |
| 670 | return TT_UnaryOperator; |
| 671 | |
| 672 | const AnnotatedToken *NextToken = getNextToken(Tok); |
| 673 | if (NextToken == NULL) |
| 674 | return TT_Unknown; |
| 675 | |
Daniel Jasper | 8a5d7cd | 2013-03-01 17:13:29 +0000 | [diff] [blame] | 676 | if (PrevToken->is(tok::l_paren) && !IsExpression) |
| 677 | return TT_PointerOrReference; |
| 678 | |
Alexander Kornienko | e74de28 | 2013-03-13 14:41:29 +0000 | [diff] [blame] | 679 | if (PrevToken->isOneOf(tok::l_paren, tok::l_square, tok::l_brace, |
Daniel Jasper | d3cf17b | 2013-03-14 10:50:25 +0000 | [diff] [blame] | 680 | tok::comma, tok::semi, tok::kw_return, tok::colon, |
Alexander Kornienko | e74de28 | 2013-03-13 14:41:29 +0000 | [diff] [blame] | 681 | tok::equal) || |
| 682 | PrevToken->Type == TT_BinaryOperator || |
Daniel Jasper | 0178673 | 2013-02-04 07:21:18 +0000 | [diff] [blame] | 683 | PrevToken->Type == TT_UnaryOperator || PrevToken->Type == TT_CastRParen) |
| 684 | return TT_UnaryOperator; |
| 685 | |
Nico Weber | e8a9798 | 2013-02-06 06:20:11 +0000 | [diff] [blame] | 686 | if (NextToken->is(tok::l_square)) |
| 687 | return TT_PointerOrReference; |
| 688 | |
Alexander Kornienko | e74de28 | 2013-03-13 14:41:29 +0000 | [diff] [blame] | 689 | if (PrevToken->FormatTok.Tok.isLiteral() || |
| 690 | PrevToken->isOneOf(tok::r_paren, tok::r_square) || |
Daniel Jasper | 9322aae | 2013-03-20 09:53:18 +0000 | [diff] [blame] | 691 | NextToken->FormatTok.Tok.isLiteral() || isUnaryOperator(*NextToken)) |
Daniel Jasper | 0178673 | 2013-02-04 07:21:18 +0000 | [diff] [blame] | 692 | return TT_BinaryOperator; |
| 693 | |
Daniel Jasper | 0178673 | 2013-02-04 07:21:18 +0000 | [diff] [blame] | 694 | // It is very unlikely that we are going to find a pointer or reference type |
| 695 | // definition on the RHS of an assignment. |
| 696 | if (IsExpression) |
| 697 | return TT_BinaryOperator; |
| 698 | |
| 699 | return TT_PointerOrReference; |
| 700 | } |
| 701 | |
| 702 | TokenType determinePlusMinusCaretUsage(const AnnotatedToken &Tok) { |
| 703 | const AnnotatedToken *PrevToken = getPreviousToken(Tok); |
| 704 | if (PrevToken == NULL) |
| 705 | return TT_UnaryOperator; |
| 706 | |
| 707 | // Use heuristics to recognize unary operators. |
Alexander Kornienko | e74de28 | 2013-03-13 14:41:29 +0000 | [diff] [blame] | 708 | if (PrevToken->isOneOf(tok::equal, tok::l_paren, tok::comma, tok::l_square, |
| 709 | tok::question, tok::colon, tok::kw_return, |
| 710 | tok::kw_case, tok::at, tok::l_brace)) |
Daniel Jasper | 0178673 | 2013-02-04 07:21:18 +0000 | [diff] [blame] | 711 | return TT_UnaryOperator; |
| 712 | |
Nico Weber | ee0feec | 2013-02-05 16:21:00 +0000 | [diff] [blame] | 713 | // There can't be two consecutive binary operators. |
Daniel Jasper | 0178673 | 2013-02-04 07:21:18 +0000 | [diff] [blame] | 714 | if (PrevToken->Type == TT_BinaryOperator) |
| 715 | return TT_UnaryOperator; |
| 716 | |
| 717 | // Fall back to marking the token as binary operator. |
| 718 | return TT_BinaryOperator; |
| 719 | } |
| 720 | |
| 721 | /// \brief Determine whether ++/-- are pre- or post-increments/-decrements. |
| 722 | TokenType determineIncrementUsage(const AnnotatedToken &Tok) { |
| 723 | const AnnotatedToken *PrevToken = getPreviousToken(Tok); |
| 724 | if (PrevToken == NULL) |
| 725 | return TT_UnaryOperator; |
Alexander Kornienko | e74de28 | 2013-03-13 14:41:29 +0000 | [diff] [blame] | 726 | if (PrevToken->isOneOf(tok::r_paren, tok::r_square, tok::identifier)) |
Daniel Jasper | 0178673 | 2013-02-04 07:21:18 +0000 | [diff] [blame] | 727 | return TT_TrailingUnaryOperator; |
| 728 | |
| 729 | return TT_UnaryOperator; |
| 730 | } |
Daniel Jasper | 4e77809 | 2013-02-06 10:05:46 +0000 | [diff] [blame] | 731 | |
Daniel Jasper | 8ed9f2b | 2013-04-03 13:36:17 +0000 | [diff] [blame] | 732 | // FIXME: This is copy&pasted from Sema. Put it in a common place and remove |
| 733 | // duplication. |
| 734 | /// \brief Determine whether the token kind starts a simple-type-specifier. |
| 735 | bool isSimpleTypeSpecifier(const AnnotatedToken &Tok) const { |
| 736 | switch (Tok.FormatTok.Tok.getKind()) { |
| 737 | case tok::kw_short: |
| 738 | case tok::kw_long: |
| 739 | case tok::kw___int64: |
| 740 | case tok::kw___int128: |
| 741 | case tok::kw_signed: |
| 742 | case tok::kw_unsigned: |
| 743 | case tok::kw_void: |
| 744 | case tok::kw_char: |
| 745 | case tok::kw_int: |
| 746 | case tok::kw_half: |
| 747 | case tok::kw_float: |
| 748 | case tok::kw_double: |
| 749 | case tok::kw_wchar_t: |
| 750 | case tok::kw_bool: |
| 751 | case tok::kw___underlying_type: |
| 752 | return true; |
| 753 | case tok::annot_typename: |
| 754 | case tok::kw_char16_t: |
| 755 | case tok::kw_char32_t: |
| 756 | case tok::kw_typeof: |
| 757 | case tok::kw_decltype: |
| 758 | return Lex.getLangOpts().CPlusPlus; |
| 759 | default: |
| 760 | break; |
| 761 | } |
| 762 | return false; |
| 763 | } |
| 764 | |
Daniel Jasper | 4e77809 | 2013-02-06 10:05:46 +0000 | [diff] [blame] | 765 | SmallVector<Context, 8> Contexts; |
| 766 | |
| 767 | SourceManager &SourceMgr; |
| 768 | Lexer &Lex; |
| 769 | AnnotatedLine &Line; |
| 770 | AnnotatedToken *CurrentToken; |
| 771 | bool KeywordVirtualFound; |
Nico Weber | c2e6d2a | 2013-02-11 15:32:15 +0000 | [diff] [blame] | 772 | IdentifierInfo &Ident_in; |
Daniel Jasper | 32d28ee | 2013-01-29 21:01:14 +0000 | [diff] [blame] | 773 | }; |
| 774 | |
Daniel Jasper | 29f123b | 2013-02-08 15:28:42 +0000 | [diff] [blame] | 775 | /// \brief Parses binary expressions by inserting fake parenthesis based on |
| 776 | /// operator precedence. |
| 777 | class ExpressionParser { |
| 778 | public: |
| 779 | ExpressionParser(AnnotatedLine &Line) : Current(&Line.First) {} |
| 780 | |
| 781 | /// \brief Parse expressions with the given operatore precedence. |
Daniel Jasper | 237d4c1 | 2013-02-23 21:01:55 +0000 | [diff] [blame] | 782 | void parse(int Precedence = 0) { |
Daniel Jasper | 29f123b | 2013-02-08 15:28:42 +0000 | [diff] [blame] | 783 | if (Precedence > prec::PointerToMember || Current == NULL) |
| 784 | return; |
| 785 | |
Daniel Jasper | 29f123b | 2013-02-08 15:28:42 +0000 | [diff] [blame] | 786 | // Eagerly consume trailing comments. |
Daniel Jasper | 65622aa | 2013-04-08 10:45:44 +0000 | [diff] [blame] | 787 | while (isTrailingComment(Current)) { |
Daniel Jasper | 29f123b | 2013-02-08 15:28:42 +0000 | [diff] [blame] | 788 | next(); |
| 789 | } |
| 790 | |
| 791 | AnnotatedToken *Start = Current; |
| 792 | bool OperatorFound = false; |
| 793 | |
Daniel Jasper | 237d4c1 | 2013-02-23 21:01:55 +0000 | [diff] [blame] | 794 | while (Current) { |
Daniel Jasper | 29f123b | 2013-02-08 15:28:42 +0000 | [diff] [blame] | 795 | // Consume operators with higher precedence. |
Daniel Jasper | bf71ba2 | 2013-04-08 20:33:42 +0000 | [diff] [blame^] | 796 | parse(Precedence + 1); |
Daniel Jasper | 29f123b | 2013-02-08 15:28:42 +0000 | [diff] [blame] | 797 | |
Daniel Jasper | 237d4c1 | 2013-02-23 21:01:55 +0000 | [diff] [blame] | 798 | int CurrentPrecedence = 0; |
| 799 | if (Current) { |
| 800 | if (Current->Type == TT_ConditionalExpr) |
| 801 | CurrentPrecedence = 1 + (int) prec::Conditional; |
Daniel Jasper | bf71ba2 | 2013-04-08 20:33:42 +0000 | [diff] [blame^] | 802 | else if (Current->is(tok::semi) || Current->Type == TT_InlineASMColon) |
Daniel Jasper | 237d4c1 | 2013-02-23 21:01:55 +0000 | [diff] [blame] | 803 | CurrentPrecedence = 1; |
| 804 | else if (Current->Type == TT_BinaryOperator || Current->is(tok::comma)) |
| 805 | CurrentPrecedence = 1 + (int) getPrecedence(*Current); |
| 806 | } |
| 807 | |
Daniel Jasper | 29f123b | 2013-02-08 15:28:42 +0000 | [diff] [blame] | 808 | // At the end of the line or when an operator with higher precedence is |
| 809 | // found, insert fake parenthesis and return. |
Daniel Jasper | 237d4c1 | 2013-02-23 21:01:55 +0000 | [diff] [blame] | 810 | if (Current == NULL || closesScope(*Current) || |
| 811 | (CurrentPrecedence != 0 && CurrentPrecedence < Precedence)) { |
Daniel Jasper | 29f123b | 2013-02-08 15:28:42 +0000 | [diff] [blame] | 812 | if (OperatorFound) { |
Daniel Jasper | bf71ba2 | 2013-04-08 20:33:42 +0000 | [diff] [blame^] | 813 | Start->FakeLParens.push_back(prec::Level(Precedence - 1)); |
Daniel Jasper | 237d4c1 | 2013-02-23 21:01:55 +0000 | [diff] [blame] | 814 | if (Current) |
Daniel Jasper | 087387a | 2013-02-08 16:49:27 +0000 | [diff] [blame] | 815 | ++Current->Parent->FakeRParens; |
Daniel Jasper | 29f123b | 2013-02-08 15:28:42 +0000 | [diff] [blame] | 816 | } |
| 817 | return; |
| 818 | } |
| 819 | |
| 820 | // Consume scopes: (), [], <> and {} |
| 821 | if (opensScope(*Current)) { |
Daniel Jasper | 237d4c1 | 2013-02-23 21:01:55 +0000 | [diff] [blame] | 822 | while (Current && !closesScope(*Current)) { |
Daniel Jasper | 29f123b | 2013-02-08 15:28:42 +0000 | [diff] [blame] | 823 | next(); |
| 824 | parse(); |
| 825 | } |
| 826 | next(); |
| 827 | } else { |
| 828 | // Operator found. |
Daniel Jasper | 237d4c1 | 2013-02-23 21:01:55 +0000 | [diff] [blame] | 829 | if (CurrentPrecedence == Precedence) |
Daniel Jasper | 29f123b | 2013-02-08 15:28:42 +0000 | [diff] [blame] | 830 | OperatorFound = true; |
| 831 | |
| 832 | next(); |
| 833 | } |
| 834 | } |
| 835 | } |
| 836 | |
| 837 | private: |
| 838 | void next() { |
| 839 | if (Current != NULL) |
| 840 | Current = Current->Children.empty() ? NULL : &Current->Children[0]; |
| 841 | } |
| 842 | |
Daniel Jasper | 29f123b | 2013-02-08 15:28:42 +0000 | [diff] [blame] | 843 | AnnotatedToken *Current; |
| 844 | }; |
| 845 | |
Daniel Jasper | 8ff690a | 2013-02-06 14:22:40 +0000 | [diff] [blame] | 846 | void TokenAnnotator::annotate(AnnotatedLine &Line) { |
Nico Weber | c2e6d2a | 2013-02-11 15:32:15 +0000 | [diff] [blame] | 847 | AnnotatingParser Parser(SourceMgr, Lex, Line, Ident_in); |
Daniel Jasper | 32d28ee | 2013-01-29 21:01:14 +0000 | [diff] [blame] | 848 | Line.Type = Parser.parseLine(); |
| 849 | if (Line.Type == LT_Invalid) |
| 850 | return; |
| 851 | |
Daniel Jasper | 29f123b | 2013-02-08 15:28:42 +0000 | [diff] [blame] | 852 | ExpressionParser ExprParser(Line); |
| 853 | ExprParser.parse(); |
| 854 | |
Daniel Jasper | 32d28ee | 2013-01-29 21:01:14 +0000 | [diff] [blame] | 855 | if (Line.First.Type == TT_ObjCMethodSpecifier) |
| 856 | Line.Type = LT_ObjCMethodDecl; |
| 857 | else if (Line.First.Type == TT_ObjCDecl) |
| 858 | Line.Type = LT_ObjCDecl; |
| 859 | else if (Line.First.Type == TT_ObjCProperty) |
| 860 | Line.Type = LT_ObjCProperty; |
| 861 | |
Daniel Jasper | 729a743 | 2013-02-11 12:36:37 +0000 | [diff] [blame] | 862 | Line.First.SpacesRequiredBefore = 1; |
Daniel Jasper | 32d28ee | 2013-01-29 21:01:14 +0000 | [diff] [blame] | 863 | Line.First.MustBreakBefore = Line.First.FormatTok.MustBreakBefore; |
| 864 | Line.First.CanBreakBefore = Line.First.MustBreakBefore; |
| 865 | |
| 866 | Line.First.TotalLength = Line.First.FormatTok.TokenLength; |
Daniel Jasper | 32d28ee | 2013-01-29 21:01:14 +0000 | [diff] [blame] | 867 | } |
| 868 | |
Daniel Jasper | 8ff690a | 2013-02-06 14:22:40 +0000 | [diff] [blame] | 869 | void TokenAnnotator::calculateFormattingInformation(AnnotatedLine &Line) { |
| 870 | if (Line.First.Children.empty()) |
| 871 | return; |
| 872 | AnnotatedToken *Current = &Line.First.Children[0]; |
| 873 | while (Current != NULL) { |
Daniel Jasper | 729a743 | 2013-02-11 12:36:37 +0000 | [diff] [blame] | 874 | if (Current->Type == TT_LineComment) |
| 875 | Current->SpacesRequiredBefore = Style.SpacesBeforeTrailingComments; |
| 876 | else |
| 877 | Current->SpacesRequiredBefore = |
| 878 | spaceRequiredBefore(Line, *Current) ? 1 : 0; |
Daniel Jasper | 32d28ee | 2013-01-29 21:01:14 +0000 | [diff] [blame] | 879 | |
Daniel Jasper | 8ff690a | 2013-02-06 14:22:40 +0000 | [diff] [blame] | 880 | if (Current->FormatTok.MustBreakBefore) { |
| 881 | Current->MustBreakBefore = true; |
| 882 | } else if (Current->Type == TT_LineComment) { |
| 883 | Current->MustBreakBefore = Current->FormatTok.NewlinesBefore > 0; |
Daniel Jasper | 29f123b | 2013-02-08 15:28:42 +0000 | [diff] [blame] | 884 | } else if (isTrailingComment(Current->Parent) || |
Daniel Jasper | 8ff690a | 2013-02-06 14:22:40 +0000 | [diff] [blame] | 885 | (Current->is(tok::string_literal) && |
| 886 | Current->Parent->is(tok::string_literal))) { |
| 887 | Current->MustBreakBefore = true; |
| 888 | } else if (Current->is(tok::lessless) && !Current->Children.empty() && |
| 889 | Current->Parent->is(tok::string_literal) && |
| 890 | Current->Children[0].is(tok::string_literal)) { |
| 891 | Current->MustBreakBefore = true; |
| 892 | } else { |
| 893 | Current->MustBreakBefore = false; |
| 894 | } |
| 895 | Current->CanBreakBefore = |
| 896 | Current->MustBreakBefore || canBreakBefore(Line, *Current); |
| 897 | if (Current->MustBreakBefore) |
| 898 | Current->TotalLength = Current->Parent->TotalLength + Style.ColumnLimit; |
| 899 | else |
| 900 | Current->TotalLength = |
| 901 | Current->Parent->TotalLength + Current->FormatTok.TokenLength + |
Daniel Jasper | 729a743 | 2013-02-11 12:36:37 +0000 | [diff] [blame] | 902 | Current->SpacesRequiredBefore; |
Daniel Jasper | 8ff690a | 2013-02-06 14:22:40 +0000 | [diff] [blame] | 903 | // FIXME: Only calculate this if CanBreakBefore is true once static |
| 904 | // initializers etc. are sorted out. |
| 905 | // FIXME: Move magic numbers to a better place. |
| 906 | Current->SplitPenalty = |
| 907 | 20 * Current->BindingStrength + splitPenalty(Line, *Current); |
| 908 | |
| 909 | Current = Current->Children.empty() ? NULL : &Current->Children[0]; |
Daniel Jasper | 32d28ee | 2013-01-29 21:01:14 +0000 | [diff] [blame] | 910 | } |
Daniel Jasper | bf71ba2 | 2013-04-08 20:33:42 +0000 | [diff] [blame^] | 911 | |
| 912 | DEBUG({ |
| 913 | printDebugInfo(Line); |
| 914 | }); |
Daniel Jasper | 32d28ee | 2013-01-29 21:01:14 +0000 | [diff] [blame] | 915 | } |
| 916 | |
Daniel Jasper | 8ff690a | 2013-02-06 14:22:40 +0000 | [diff] [blame] | 917 | unsigned TokenAnnotator::splitPenalty(const AnnotatedLine &Line, |
| 918 | const AnnotatedToken &Tok) { |
Daniel Jasper | 32d28ee | 2013-01-29 21:01:14 +0000 | [diff] [blame] | 919 | const AnnotatedToken &Left = *Tok.Parent; |
| 920 | const AnnotatedToken &Right = Tok; |
| 921 | |
Daniel Jasper | 3c08a81 | 2013-02-24 18:54:32 +0000 | [diff] [blame] | 922 | if (Right.Type == TT_StartOfName) { |
Daniel Jasper | 8ed9f2b | 2013-04-03 13:36:17 +0000 | [diff] [blame] | 923 | if (Line.First.is(tok::kw_for) && Right.PartOfMultiVariableDeclStmt) |
Daniel Jasper | 3c08a81 | 2013-02-24 18:54:32 +0000 | [diff] [blame] | 924 | return 3; |
| 925 | else if (Line.MightBeFunctionDecl && Right.BindingStrength == 1) |
| 926 | // FIXME: Clean up hack of using BindingStrength to find top-level names. |
| 927 | return Style.PenaltyReturnTypeOnItsOwnLine; |
| 928 | else |
| 929 | return 100; |
| 930 | } |
Daniel Jasper | 32d28ee | 2013-01-29 21:01:14 +0000 | [diff] [blame] | 931 | if (Left.is(tok::equal) && Right.is(tok::l_brace)) |
| 932 | return 150; |
| 933 | if (Left.is(tok::coloncolon)) |
| 934 | return 500; |
Daniel Jasper | 6b119d6 | 2013-04-05 17:22:09 +0000 | [diff] [blame] | 935 | if (Left.isOneOf(tok::kw_class, tok::kw_struct)) |
| 936 | return 5000; |
Daniel Jasper | 32d28ee | 2013-01-29 21:01:14 +0000 | [diff] [blame] | 937 | |
Daniel Jasper | 6cabab4 | 2013-02-14 08:42:54 +0000 | [diff] [blame] | 938 | if (Left.Type == TT_RangeBasedForLoopColon || |
| 939 | Left.Type == TT_InheritanceColon) |
Daniel Jasper | 84a1a63 | 2013-02-26 13:18:08 +0000 | [diff] [blame] | 940 | return 2; |
Daniel Jasper | 32d28ee | 2013-01-29 21:01:14 +0000 | [diff] [blame] | 941 | |
Alexander Kornienko | e74de28 | 2013-03-13 14:41:29 +0000 | [diff] [blame] | 942 | if (Right.isOneOf(tok::arrow, tok::period)) { |
Daniel Jasper | 515f65d | 2013-02-18 13:24:21 +0000 | [diff] [blame] | 943 | if (Line.Type == LT_BuilderTypeCall) |
Daniel Jasper | 6a365aa | 2013-03-13 17:13:53 +0000 | [diff] [blame] | 944 | return prec::PointerToMember; |
Alexander Kornienko | e74de28 | 2013-03-13 14:41:29 +0000 | [diff] [blame] | 945 | if (Left.isOneOf(tok::r_paren, tok::r_square) && Left.MatchingParen && |
| 946 | Left.MatchingParen->ParameterCount > 0) |
Daniel Jasper | 518ee34 | 2013-02-26 13:59:14 +0000 | [diff] [blame] | 947 | return 20; // Should be smaller than breaking at a nested comma. |
Daniel Jasper | 32d28ee | 2013-01-29 21:01:14 +0000 | [diff] [blame] | 948 | return 150; |
| 949 | } |
| 950 | |
| 951 | // In for-loops, prefer breaking at ',' and ';'. |
Daniel Jasper | 7d81281 | 2013-02-21 15:00:29 +0000 | [diff] [blame] | 952 | if (Line.First.is(tok::kw_for) && Left.is(tok::equal)) |
| 953 | return 4; |
Daniel Jasper | 32d28ee | 2013-01-29 21:01:14 +0000 | [diff] [blame] | 954 | |
Daniel Jasper | 8159d2f | 2013-02-04 07:30:30 +0000 | [diff] [blame] | 955 | if (Left.is(tok::semi)) |
Daniel Jasper | 32d28ee | 2013-01-29 21:01:14 +0000 | [diff] [blame] | 956 | return 0; |
Daniel Jasper | 8159d2f | 2013-02-04 07:30:30 +0000 | [diff] [blame] | 957 | if (Left.is(tok::comma)) |
| 958 | return 1; |
Daniel Jasper | 32d28ee | 2013-01-29 21:01:14 +0000 | [diff] [blame] | 959 | |
| 960 | // In Objective-C method expressions, prefer breaking before "param:" over |
| 961 | // breaking after it. |
Daniel Jasper | 63d7ced | 2013-02-05 10:07:47 +0000 | [diff] [blame] | 962 | if (Right.Type == TT_ObjCSelectorName) |
Daniel Jasper | 32d28ee | 2013-01-29 21:01:14 +0000 | [diff] [blame] | 963 | return 0; |
Daniel Jasper | 63d7ced | 2013-02-05 10:07:47 +0000 | [diff] [blame] | 964 | if (Left.is(tok::colon) && Left.Type == TT_ObjCMethodExpr) |
Daniel Jasper | 32d28ee | 2013-01-29 21:01:14 +0000 | [diff] [blame] | 965 | return 20; |
| 966 | |
Alexander Kornienko | e74de28 | 2013-03-13 14:41:29 +0000 | [diff] [blame] | 967 | if (opensScope(Left)) |
Daniel Jasper | 64f0928 | 2013-03-20 13:53:11 +0000 | [diff] [blame] | 968 | return Left.ParameterCount > 1 ? prec::Comma : 20; |
Daniel Jasper | 32d28ee | 2013-01-29 21:01:14 +0000 | [diff] [blame] | 969 | |
Daniel Jasper | 4e8a7b4 | 2013-02-06 21:04:05 +0000 | [diff] [blame] | 970 | if (Right.is(tok::lessless)) { |
| 971 | if (Left.is(tok::string_literal)) { |
Daniel Jasper | bfa1edd | 2013-03-14 14:00:17 +0000 | [diff] [blame] | 972 | StringRef Content = StringRef(Left.FormatTok.Tok.getLiteralData(), |
| 973 | Left.FormatTok.TokenLength); |
| 974 | Content = Content.drop_back(1).drop_front(1).trim(); |
| 975 | if (Content.size() > 1 && |
| 976 | (Content.back() == ':' || Content.back() == '=')) |
Daniel Jasper | 4e8a7b4 | 2013-02-06 21:04:05 +0000 | [diff] [blame] | 977 | return 100; |
| 978 | } |
Daniel Jasper | 0178673 | 2013-02-04 07:21:18 +0000 | [diff] [blame] | 979 | return prec::Shift; |
Daniel Jasper | 4e8a7b4 | 2013-02-06 21:04:05 +0000 | [diff] [blame] | 980 | } |
Daniel Jasper | 32d28ee | 2013-01-29 21:01:14 +0000 | [diff] [blame] | 981 | if (Left.Type == TT_ConditionalExpr) |
Daniel Jasper | 518ee34 | 2013-02-26 13:59:14 +0000 | [diff] [blame] | 982 | return prec::Conditional; |
Daniel Jasper | 32d28ee | 2013-01-29 21:01:14 +0000 | [diff] [blame] | 983 | prec::Level Level = getPrecedence(Left); |
| 984 | |
| 985 | if (Level != prec::Unknown) |
| 986 | return Level; |
Daniel Jasper | 2484971 | 2013-03-01 16:48:32 +0000 | [diff] [blame] | 987 | |
Daniel Jasper | 32d28ee | 2013-01-29 21:01:14 +0000 | [diff] [blame] | 988 | return 3; |
| 989 | } |
| 990 | |
Daniel Jasper | 8ff690a | 2013-02-06 14:22:40 +0000 | [diff] [blame] | 991 | bool TokenAnnotator::spaceRequiredBetween(const AnnotatedLine &Line, |
| 992 | const AnnotatedToken &Left, |
Daniel Jasper | 32d28ee | 2013-01-29 21:01:14 +0000 | [diff] [blame] | 993 | const AnnotatedToken &Right) { |
| 994 | if (Right.is(tok::hashhash)) |
| 995 | return Left.is(tok::hash); |
Alexander Kornienko | e74de28 | 2013-03-13 14:41:29 +0000 | [diff] [blame] | 996 | if (Left.isOneOf(tok::hashhash, tok::hash)) |
Daniel Jasper | 32d28ee | 2013-01-29 21:01:14 +0000 | [diff] [blame] | 997 | return Right.is(tok::hash); |
Alexander Kornienko | e74de28 | 2013-03-13 14:41:29 +0000 | [diff] [blame] | 998 | if (Right.isOneOf(tok::r_paren, tok::semi, tok::comma)) |
Daniel Jasper | 32d28ee | 2013-01-29 21:01:14 +0000 | [diff] [blame] | 999 | return false; |
| 1000 | if (Right.is(tok::less) && |
| 1001 | (Left.is(tok::kw_template) || |
| 1002 | (Line.Type == LT_ObjCDecl && Style.ObjCSpaceBeforeProtocolList))) |
| 1003 | return true; |
| 1004 | if (Left.is(tok::arrow) || Right.is(tok::arrow)) |
| 1005 | return false; |
Alexander Kornienko | e74de28 | 2013-03-13 14:41:29 +0000 | [diff] [blame] | 1006 | if (Left.isOneOf(tok::exclaim, tok::tilde)) |
Daniel Jasper | 32d28ee | 2013-01-29 21:01:14 +0000 | [diff] [blame] | 1007 | return false; |
| 1008 | if (Left.is(tok::at) && |
Alexander Kornienko | e74de28 | 2013-03-13 14:41:29 +0000 | [diff] [blame] | 1009 | Right.isOneOf(tok::identifier, tok::string_literal, tok::char_constant, |
| 1010 | tok::numeric_constant, tok::l_paren, tok::l_brace, |
| 1011 | tok::kw_true, tok::kw_false)) |
Daniel Jasper | 32d28ee | 2013-01-29 21:01:14 +0000 | [diff] [blame] | 1012 | return false; |
| 1013 | if (Left.is(tok::coloncolon)) |
| 1014 | return false; |
| 1015 | if (Right.is(tok::coloncolon)) |
Alexander Kornienko | e74de28 | 2013-03-13 14:41:29 +0000 | [diff] [blame] | 1016 | return !Left.isOneOf(tok::identifier, tok::greater, tok::l_paren); |
| 1017 | if (Left.is(tok::less) || Right.isOneOf(tok::greater, tok::less)) |
Daniel Jasper | 32d28ee | 2013-01-29 21:01:14 +0000 | [diff] [blame] | 1018 | return false; |
Alexander Kornienko | 3fd9ccd | 2013-03-12 16:28:18 +0000 | [diff] [blame] | 1019 | if (Right.Type == TT_PointerOrReference) |
Daniel Jasper | 32d28ee | 2013-01-29 21:01:14 +0000 | [diff] [blame] | 1020 | return Left.FormatTok.Tok.isLiteral() || |
Alexander Kornienko | 3fd9ccd | 2013-03-12 16:28:18 +0000 | [diff] [blame] | 1021 | ((Left.Type != TT_PointerOrReference) && Left.isNot(tok::l_paren) && |
| 1022 | !Style.PointerBindsToType); |
| 1023 | if (Left.Type == TT_PointerOrReference) |
Nico Weber | 95e8e46 | 2013-02-12 16:17:07 +0000 | [diff] [blame] | 1024 | return Right.FormatTok.Tok.isLiteral() || |
Daniel Jasper | 9322aae | 2013-03-20 09:53:18 +0000 | [diff] [blame] | 1025 | ((Right.Type != TT_PointerOrReference) && |
Daniel Jasper | 81d2d38 | 2013-04-01 17:13:26 +0000 | [diff] [blame] | 1026 | Right.isNot(tok::l_paren) && Style.PointerBindsToType && |
| 1027 | Left.Parent && Left.Parent->isNot(tok::l_paren)); |
Daniel Jasper | 32d28ee | 2013-01-29 21:01:14 +0000 | [diff] [blame] | 1028 | if (Right.is(tok::star) && Left.is(tok::l_paren)) |
| 1029 | return false; |
Nico Weber | 051860e | 2013-02-10 02:08:05 +0000 | [diff] [blame] | 1030 | if (Left.is(tok::l_square)) |
| 1031 | return Left.Type == TT_ObjCArrayLiteral && Right.isNot(tok::r_square); |
| 1032 | if (Right.is(tok::r_square)) |
| 1033 | return Right.Type == TT_ObjCArrayLiteral; |
Daniel Jasper | 32d28ee | 2013-01-29 21:01:14 +0000 | [diff] [blame] | 1034 | if (Right.is(tok::l_square) && Right.Type != TT_ObjCMethodExpr) |
| 1035 | return false; |
| 1036 | if (Left.is(tok::period) || Right.is(tok::period)) |
| 1037 | return false; |
| 1038 | if (Left.is(tok::colon)) |
| 1039 | return Left.Type != TT_ObjCMethodExpr; |
| 1040 | if (Right.is(tok::colon)) |
| 1041 | return Right.Type != TT_ObjCMethodExpr; |
| 1042 | if (Left.is(tok::l_paren)) |
| 1043 | return false; |
| 1044 | if (Right.is(tok::l_paren)) { |
Alexander Kornienko | e74de28 | 2013-03-13 14:41:29 +0000 | [diff] [blame] | 1045 | return Line.Type == LT_ObjCDecl || |
| 1046 | Left.isOneOf(tok::kw_if, tok::kw_for, tok::kw_while, tok::kw_switch, |
| 1047 | tok::kw_return, tok::kw_catch, tok::kw_new, |
| 1048 | tok::kw_delete); |
Daniel Jasper | 32d28ee | 2013-01-29 21:01:14 +0000 | [diff] [blame] | 1049 | } |
| 1050 | if (Left.is(tok::at) && |
| 1051 | Right.FormatTok.Tok.getObjCKeywordID() != tok::objc_not_keyword) |
| 1052 | return false; |
| 1053 | if (Left.is(tok::l_brace) && Right.is(tok::r_brace)) |
| 1054 | return false; |
| 1055 | return true; |
| 1056 | } |
| 1057 | |
Daniel Jasper | 8ff690a | 2013-02-06 14:22:40 +0000 | [diff] [blame] | 1058 | bool TokenAnnotator::spaceRequiredBefore(const AnnotatedLine &Line, |
| 1059 | const AnnotatedToken &Tok) { |
Daniel Jasper | 2b4c924 | 2013-02-11 08:01:18 +0000 | [diff] [blame] | 1060 | if (Tok.FormatTok.Tok.getIdentifierInfo() && |
| 1061 | Tok.Parent->FormatTok.Tok.getIdentifierInfo()) |
| 1062 | return true; // Never ever merge two identifiers. |
Daniel Jasper | 32d28ee | 2013-01-29 21:01:14 +0000 | [diff] [blame] | 1063 | if (Line.Type == LT_ObjCMethodDecl) { |
Daniel Jasper | 32d28ee | 2013-01-29 21:01:14 +0000 | [diff] [blame] | 1064 | if (Tok.Parent->Type == TT_ObjCMethodSpecifier) |
| 1065 | return true; |
| 1066 | if (Tok.Parent->is(tok::r_paren) && Tok.is(tok::identifier)) |
| 1067 | // Don't space between ')' and <id> |
| 1068 | return false; |
Daniel Jasper | 32d28ee | 2013-01-29 21:01:14 +0000 | [diff] [blame] | 1069 | } |
| 1070 | if (Line.Type == LT_ObjCProperty && |
| 1071 | (Tok.is(tok::equal) || Tok.Parent->is(tok::equal))) |
| 1072 | return false; |
| 1073 | |
| 1074 | if (Tok.Parent->is(tok::comma)) |
| 1075 | return true; |
Daniel Jasper | 9c3c7b3 | 2013-02-28 13:40:17 +0000 | [diff] [blame] | 1076 | if (Tok.is(tok::comma)) |
| 1077 | return false; |
Daniel Jasper | 32d28ee | 2013-01-29 21:01:14 +0000 | [diff] [blame] | 1078 | if (Tok.Type == TT_CtorInitializerColon || Tok.Type == TT_ObjCBlockLParen) |
| 1079 | return true; |
Daniel Jasper | 2b4c924 | 2013-02-11 08:01:18 +0000 | [diff] [blame] | 1080 | if (Tok.Parent->FormatTok.Tok.is(tok::kw_operator)) |
| 1081 | return false; |
| 1082 | if (Tok.Type == TT_OverloadedOperatorLParen) |
Daniel Jasper | 32d28ee | 2013-01-29 21:01:14 +0000 | [diff] [blame] | 1083 | return false; |
| 1084 | if (Tok.is(tok::colon)) |
Alexander Kornienko | e74de28 | 2013-03-13 14:41:29 +0000 | [diff] [blame] | 1085 | return !Line.First.isOneOf(tok::kw_case, tok::kw_default) && |
| 1086 | !Tok.Children.empty() && Tok.Type != TT_ObjCMethodExpr; |
Daniel Jasper | 8a5d7cd | 2013-03-01 17:13:29 +0000 | [diff] [blame] | 1087 | if (Tok.is(tok::l_paren) && !Tok.Children.empty() && |
| 1088 | Tok.Children[0].Type == TT_PointerOrReference && |
| 1089 | !Tok.Children[0].Children.empty() && |
Alexander Kornienko | 54a38bd | 2013-03-20 16:41:56 +0000 | [diff] [blame] | 1090 | Tok.Children[0].Children[0].isNot(tok::r_paren) && |
Daniel Jasper | 81d2d38 | 2013-04-01 17:13:26 +0000 | [diff] [blame] | 1091 | Tok.Parent->isNot(tok::l_paren) && |
| 1092 | (Tok.Parent->Type != TT_PointerOrReference || Style.PointerBindsToType)) |
Daniel Jasper | 8a5d7cd | 2013-03-01 17:13:29 +0000 | [diff] [blame] | 1093 | return true; |
Daniel Jasper | 32d28ee | 2013-01-29 21:01:14 +0000 | [diff] [blame] | 1094 | if (Tok.Parent->Type == TT_UnaryOperator || Tok.Parent->Type == TT_CastRParen) |
| 1095 | return false; |
| 1096 | if (Tok.Type == TT_UnaryOperator) |
Alexander Kornienko | e74de28 | 2013-03-13 14:41:29 +0000 | [diff] [blame] | 1097 | return !Tok.Parent->isOneOf(tok::l_paren, tok::l_square, tok::at) && |
Daniel Jasper | 32d28ee | 2013-01-29 21:01:14 +0000 | [diff] [blame] | 1098 | (Tok.Parent->isNot(tok::colon) || |
| 1099 | Tok.Parent->Type != TT_ObjCMethodExpr); |
| 1100 | if (Tok.Parent->is(tok::greater) && Tok.is(tok::greater)) { |
Daniel Jasper | 29f123b | 2013-02-08 15:28:42 +0000 | [diff] [blame] | 1101 | return Tok.Type == TT_TemplateCloser && |
| 1102 | Tok.Parent->Type == TT_TemplateCloser && |
| 1103 | Style.Standard != FormatStyle::LS_Cpp11; |
Daniel Jasper | 32d28ee | 2013-01-29 21:01:14 +0000 | [diff] [blame] | 1104 | } |
Alexander Kornienko | 54a38bd | 2013-03-20 16:41:56 +0000 | [diff] [blame] | 1105 | if (Tok.isOneOf(tok::arrowstar, tok::periodstar) || |
| 1106 | Tok.Parent->isOneOf(tok::arrowstar, tok::periodstar)) |
Daniel Jasper | 9c3c7b3 | 2013-02-28 13:40:17 +0000 | [diff] [blame] | 1107 | return false; |
| 1108 | if (Tok.Type == TT_BinaryOperator || Tok.Parent->Type == TT_BinaryOperator) |
Daniel Jasper | 32d28ee | 2013-01-29 21:01:14 +0000 | [diff] [blame] | 1109 | return true; |
| 1110 | if (Tok.Parent->Type == TT_TemplateCloser && Tok.is(tok::l_paren)) |
| 1111 | return false; |
| 1112 | if (Tok.is(tok::less) && Line.First.is(tok::hash)) |
| 1113 | return true; |
| 1114 | if (Tok.Type == TT_TrailingUnaryOperator) |
| 1115 | return false; |
Daniel Jasper | 8ff690a | 2013-02-06 14:22:40 +0000 | [diff] [blame] | 1116 | return spaceRequiredBetween(Line, *Tok.Parent, Tok); |
Daniel Jasper | 32d28ee | 2013-01-29 21:01:14 +0000 | [diff] [blame] | 1117 | } |
| 1118 | |
Daniel Jasper | 8ff690a | 2013-02-06 14:22:40 +0000 | [diff] [blame] | 1119 | bool TokenAnnotator::canBreakBefore(const AnnotatedLine &Line, |
| 1120 | const AnnotatedToken &Right) { |
Daniel Jasper | 32d28ee | 2013-01-29 21:01:14 +0000 | [diff] [blame] | 1121 | const AnnotatedToken &Left = *Right.Parent; |
Daniel Jasper | a03ab10 | 2013-02-13 20:33:44 +0000 | [diff] [blame] | 1122 | if (Right.Type == TT_StartOfName) |
Daniel Jasper | 32d28ee | 2013-01-29 21:01:14 +0000 | [diff] [blame] | 1123 | return true; |
| 1124 | if (Right.is(tok::colon) && Right.Type == TT_ObjCMethodExpr) |
| 1125 | return false; |
| 1126 | if (Left.is(tok::colon) && Left.Type == TT_ObjCMethodExpr) |
| 1127 | return true; |
Daniel Jasper | 63d7ced | 2013-02-05 10:07:47 +0000 | [diff] [blame] | 1128 | if (Right.Type == TT_ObjCSelectorName) |
Daniel Jasper | 32d28ee | 2013-01-29 21:01:14 +0000 | [diff] [blame] | 1129 | return true; |
| 1130 | if (Left.ClosesTemplateDeclaration) |
| 1131 | return true; |
| 1132 | if (Right.Type == TT_ConditionalExpr || Right.is(tok::question)) |
| 1133 | return true; |
Daniel Jasper | 6cabab4 | 2013-02-14 08:42:54 +0000 | [diff] [blame] | 1134 | if (Right.Type == TT_RangeBasedForLoopColon || |
Daniel Jasper | 27b91cc | 2013-04-05 17:21:59 +0000 | [diff] [blame] | 1135 | Right.Type == TT_InheritanceColon || |
| 1136 | Right.Type == TT_OverloadedOperatorLParen) |
Daniel Jasper | 6cabab4 | 2013-02-14 08:42:54 +0000 | [diff] [blame] | 1137 | return false; |
| 1138 | if (Left.Type == TT_RangeBasedForLoopColon || |
| 1139 | Left.Type == TT_InheritanceColon) |
Daniel Jasper | 32d28ee | 2013-01-29 21:01:14 +0000 | [diff] [blame] | 1140 | return true; |
Daniel Jasper | 7d81281 | 2013-02-21 15:00:29 +0000 | [diff] [blame] | 1141 | if (Right.Type == TT_RangeBasedForLoopColon) |
| 1142 | return false; |
Daniel Jasper | 32d28ee | 2013-01-29 21:01:14 +0000 | [diff] [blame] | 1143 | if (Left.Type == TT_PointerOrReference || Left.Type == TT_TemplateCloser || |
| 1144 | Left.Type == TT_UnaryOperator || Left.Type == TT_ConditionalExpr || |
Alexander Kornienko | e74de28 | 2013-03-13 14:41:29 +0000 | [diff] [blame] | 1145 | Left.isOneOf(tok::question, tok::kw_operator)) |
Daniel Jasper | 32d28ee | 2013-01-29 21:01:14 +0000 | [diff] [blame] | 1146 | return false; |
| 1147 | if (Left.is(tok::equal) && Line.Type == LT_VirtualFunctionDecl) |
| 1148 | return false; |
Daniel Jasper | 8ed4100 | 2013-02-28 14:44:25 +0000 | [diff] [blame] | 1149 | if (Left.is(tok::l_paren) && Right.is(tok::l_paren) && Left.Parent && |
| 1150 | Left.Parent->is(tok::kw___attribute)) |
| 1151 | return false; |
Daniel Jasper | 32d28ee | 2013-01-29 21:01:14 +0000 | [diff] [blame] | 1152 | |
| 1153 | if (Right.Type == TT_LineComment) |
| 1154 | // We rely on MustBreakBefore being set correctly here as we should not |
| 1155 | // change the "binding" behavior of a comment. |
| 1156 | return false; |
| 1157 | |
Daniel Jasper | 32d28ee | 2013-01-29 21:01:14 +0000 | [diff] [blame] | 1158 | // Allow breaking after a trailing 'const', e.g. after a method declaration, |
| 1159 | // unless it is follow by ';', '{' or '='. |
| 1160 | if (Left.is(tok::kw_const) && Left.Parent != NULL && |
| 1161 | Left.Parent->is(tok::r_paren)) |
Alexander Kornienko | e74de28 | 2013-03-13 14:41:29 +0000 | [diff] [blame] | 1162 | return !Right.isOneOf(tok::l_brace, tok::semi, tok::equal); |
Daniel Jasper | 32d28ee | 2013-01-29 21:01:14 +0000 | [diff] [blame] | 1163 | |
Daniel Jasper | 8ef19a2 | 2013-03-14 09:50:46 +0000 | [diff] [blame] | 1164 | if (Right.is(tok::kw___attribute)) |
| 1165 | return true; |
| 1166 | |
Daniel Jasper | 32d28ee | 2013-01-29 21:01:14 +0000 | [diff] [blame] | 1167 | // We only break before r_brace if there was a corresponding break before |
| 1168 | // the l_brace, which is tracked by BreakBeforeClosingBrace. |
Daniel Jasper | 8ef19a2 | 2013-03-14 09:50:46 +0000 | [diff] [blame] | 1169 | if (Right.isOneOf(tok::r_brace, tok::r_paren, tok::greater)) |
Daniel Jasper | 32d28ee | 2013-01-29 21:01:14 +0000 | [diff] [blame] | 1170 | return false; |
Daniel Jasper | 3a20441 | 2013-02-23 07:46:38 +0000 | [diff] [blame] | 1171 | if (Left.is(tok::identifier) && Right.is(tok::string_literal)) |
| 1172 | return true; |
Daniel Jasper | 32d28ee | 2013-01-29 21:01:14 +0000 | [diff] [blame] | 1173 | return (isBinaryOperator(Left) && Left.isNot(tok::lessless)) || |
Daniel Jasper | 6b119d6 | 2013-04-05 17:22:09 +0000 | [diff] [blame] | 1174 | Left.isOneOf(tok::comma, tok::coloncolon, tok::semi, tok::l_brace, |
| 1175 | tok::kw_class, tok::kw_struct) || |
Alexander Kornienko | e74de28 | 2013-03-13 14:41:29 +0000 | [diff] [blame] | 1176 | Right.isOneOf(tok::lessless, tok::arrow, tok::period, tok::colon) || |
Daniel Jasper | 32d28ee | 2013-01-29 21:01:14 +0000 | [diff] [blame] | 1177 | (Left.is(tok::r_paren) && Left.Type != TT_CastRParen && |
Alexander Kornienko | e74de28 | 2013-03-13 14:41:29 +0000 | [diff] [blame] | 1178 | Right.isOneOf(tok::identifier, tok::kw___attribute)) || |
Daniel Jasper | 32d28ee | 2013-01-29 21:01:14 +0000 | [diff] [blame] | 1179 | (Left.is(tok::l_paren) && !Right.is(tok::r_paren)) || |
| 1180 | (Left.is(tok::l_square) && !Right.is(tok::r_square)); |
| 1181 | } |
| 1182 | |
Daniel Jasper | bf71ba2 | 2013-04-08 20:33:42 +0000 | [diff] [blame^] | 1183 | void TokenAnnotator::printDebugInfo(const AnnotatedLine &Line) { |
| 1184 | llvm::errs() << "AnnotatedTokens:\n"; |
| 1185 | const AnnotatedToken *Tok = &Line.First; |
| 1186 | while (Tok) { |
| 1187 | llvm::errs() << " M=" << Tok->MustBreakBefore |
| 1188 | << " C=" << Tok->CanBreakBefore << " T=" << Tok->Type |
| 1189 | << " S=" << Tok->SpacesRequiredBefore |
| 1190 | << " Name=" << Tok->FormatTok.Tok.getName() << " FakeLParens="; |
| 1191 | for (unsigned i = 0, e = Tok->FakeLParens.size(); i != e; ++i) |
| 1192 | llvm::errs() << Tok->FakeLParens[i] << "/"; |
| 1193 | llvm::errs() << " FakeRParens=" << Tok->FakeRParens << "\n"; |
| 1194 | Tok = Tok->Children.empty() ? NULL : &Tok->Children[0]; |
| 1195 | } |
| 1196 | llvm::errs() << "----\n"; |
| 1197 | } |
| 1198 | |
Daniel Jasper | 32d28ee | 2013-01-29 21:01:14 +0000 | [diff] [blame] | 1199 | } // namespace format |
| 1200 | } // namespace clang |