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