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