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