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