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 | /// |
Alexander Kornienko | 3fd9ccd | 2013-03-12 16:28:18 +0000 | [diff] [blame] | 76 | /// The \c TokenAnnotator tries to match parenthesis and square brakets and |
Daniel Jasper | 32d28ee | 2013-01-29 21:01:14 +0000 | [diff] [blame] | 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) && |
Alexander Kornienko | 3fd9ccd | 2013-03-12 16:28:18 +0000 | [diff] [blame] | 152 | (Prev.is(tok::star) || Prev.is(tok::amp) || Prev.is(tok::ampamp)) && |
Daniel Jasper | 32d28ee | 2013-01-29 21:01:14 +0000 | [diff] [blame] | 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; |
| 186 | |
| 187 | // A '[' could be an index subscript (after an indentifier or after |
Nico Weber | 051860e | 2013-02-10 02:08:05 +0000 | [diff] [blame] | 188 | // ')' or ']'), it could be the start of an Objective-C method |
| 189 | // 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] | 190 | AnnotatedToken *Left = CurrentToken->Parent; |
Nico Weber | 4ed7f3e | 2013-02-06 16:54:35 +0000 | [diff] [blame] | 191 | AnnotatedToken *Parent = getPreviousToken(*Left); |
Daniel Jasper | 32d28ee | 2013-01-29 21:01:14 +0000 | [diff] [blame] | 192 | bool StartsObjCMethodExpr = |
Daniel Jasper | 6f21a98 | 2013-03-13 07:49:51 +0000 | [diff] [blame^] | 193 | Contexts.back().CanBeExpression && |
| 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) || |
| 197 | Parent->Type == TT_ObjCForIn || Parent->Type == TT_CastRParen || |
| 198 | getBinOpPrecedence(Parent->FormatTok.Tok.getKind(), true, true) > |
| 199 | prec::Unknown); |
| 200 | ScopedContextCreator ContextCreator(*this, 10); |
| 201 | Contexts.back().IsExpression = true; |
Nico Weber | 051860e | 2013-02-10 02:08:05 +0000 | [diff] [blame] | 202 | bool StartsObjCArrayLiteral = Parent && Parent->is(tok::at); |
Daniel Jasper | 32d28ee | 2013-01-29 21:01:14 +0000 | [diff] [blame] | 203 | |
Daniel Jasper | 4e77809 | 2013-02-06 10:05:46 +0000 | [diff] [blame] | 204 | if (StartsObjCMethodExpr) { |
| 205 | Contexts.back().ColonIsObjCMethodExpr = true; |
| 206 | Left->Type = TT_ObjCMethodExpr; |
Nico Weber | 051860e | 2013-02-10 02:08:05 +0000 | [diff] [blame] | 207 | } else if (StartsObjCArrayLiteral) { |
| 208 | Left->Type = TT_ObjCArrayLiteral; |
Daniel Jasper | 4e77809 | 2013-02-06 10:05:46 +0000 | [diff] [blame] | 209 | } |
Daniel Jasper | 32d28ee | 2013-01-29 21:01:14 +0000 | [diff] [blame] | 210 | |
| 211 | while (CurrentToken != NULL) { |
| 212 | if (CurrentToken->is(tok::r_square)) { |
| 213 | if (!CurrentToken->Children.empty() && |
| 214 | CurrentToken->Children[0].is(tok::l_paren)) { |
Nico Weber | e8a9798 | 2013-02-06 06:20:11 +0000 | [diff] [blame] | 215 | // An ObjC method call is rarely followed by an open parenthesis. |
Daniel Jasper | 32d28ee | 2013-01-29 21:01:14 +0000 | [diff] [blame] | 216 | // FIXME: Do we incorrectly label ":" with this? |
| 217 | StartsObjCMethodExpr = false; |
| 218 | Left->Type = TT_Unknown; |
| 219 | } |
Daniel Jasper | 0178673 | 2013-02-04 07:21:18 +0000 | [diff] [blame] | 220 | if (StartsObjCMethodExpr) { |
Daniel Jasper | 4e77809 | 2013-02-06 10:05:46 +0000 | [diff] [blame] | 221 | CurrentToken->Type = TT_ObjCMethodExpr; |
Nico Weber | e8a9798 | 2013-02-06 06:20:11 +0000 | [diff] [blame] | 222 | // determineStarAmpUsage() thinks that '*' '[' is allocating an |
| 223 | // array of pointers, but if '[' starts a selector then '*' is a |
| 224 | // binary operator. |
Alexander Kornienko | 3fd9ccd | 2013-03-12 16:28:18 +0000 | [diff] [blame] | 225 | if (Parent != NULL && Parent->Type == TT_PointerOrReference) |
Nico Weber | 4ed7f3e | 2013-02-06 16:54:35 +0000 | [diff] [blame] | 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; |
Daniel Jasper | 2484971 | 2013-03-01 16:48:32 +0000 | [diff] [blame] | 466 | AnnotatedToken *LastPeriodOrArrow = NULL; |
Daniel Jasper | 32d28ee | 2013-01-29 21:01:14 +0000 | [diff] [blame] | 467 | bool CanBeBuilderTypeStmt = true; |
| 468 | if (CurrentToken->is(tok::hash)) { |
| 469 | parsePreprocessorDirective(); |
| 470 | return LT_PreprocessorDirective; |
| 471 | } |
| 472 | while (CurrentToken != NULL) { |
| 473 | if (CurrentToken->is(tok::kw_virtual)) |
| 474 | KeywordVirtualFound = true; |
Daniel Jasper | 2484971 | 2013-03-01 16:48:32 +0000 | [diff] [blame] | 475 | if (CurrentToken->is(tok::period) || CurrentToken->is(tok::arrow)) { |
Daniel Jasper | 32d28ee | 2013-01-29 21:01:14 +0000 | [diff] [blame] | 476 | ++PeriodsAndArrows; |
Daniel Jasper | 2484971 | 2013-03-01 16:48:32 +0000 | [diff] [blame] | 477 | LastPeriodOrArrow = CurrentToken; |
| 478 | } |
Daniel Jasper | 4a544e5 | 2013-02-15 20:33:06 +0000 | [diff] [blame] | 479 | AnnotatedToken *TheToken = CurrentToken; |
Daniel Jasper | 32d28ee | 2013-01-29 21:01:14 +0000 | [diff] [blame] | 480 | if (!consumeToken()) |
| 481 | return LT_Invalid; |
Daniel Jasper | 4a544e5 | 2013-02-15 20:33:06 +0000 | [diff] [blame] | 482 | if (getPrecedence(*TheToken) > prec::Assignment && |
Daniel Jasper | 82282dc | 2013-02-18 13:52:06 +0000 | [diff] [blame] | 483 | TheToken->Type == TT_BinaryOperator) |
Daniel Jasper | 4a544e5 | 2013-02-15 20:33:06 +0000 | [diff] [blame] | 484 | CanBeBuilderTypeStmt = false; |
Daniel Jasper | 32d28ee | 2013-01-29 21:01:14 +0000 | [diff] [blame] | 485 | } |
| 486 | if (KeywordVirtualFound) |
| 487 | return LT_VirtualFunctionDecl; |
| 488 | |
| 489 | // Assume a builder-type call if there are 2 or more "." and "->". |
Daniel Jasper | 2484971 | 2013-03-01 16:48:32 +0000 | [diff] [blame] | 490 | if (PeriodsAndArrows >= 2 && CanBeBuilderTypeStmt) { |
| 491 | LastPeriodOrArrow->LastInChainOfCalls = true; |
Daniel Jasper | 32d28ee | 2013-01-29 21:01:14 +0000 | [diff] [blame] | 492 | return LT_BuilderTypeCall; |
Daniel Jasper | 2484971 | 2013-03-01 16:48:32 +0000 | [diff] [blame] | 493 | } |
Daniel Jasper | 32d28ee | 2013-01-29 21:01:14 +0000 | [diff] [blame] | 494 | |
Daniel Jasper | 63d7ced | 2013-02-05 10:07:47 +0000 | [diff] [blame] | 495 | if (Line.First.Type == TT_ObjCMethodSpecifier) { |
Daniel Jasper | 4e77809 | 2013-02-06 10:05:46 +0000 | [diff] [blame] | 496 | if (Contexts.back().FirstObjCSelectorName != NULL) |
| 497 | Contexts.back().FirstObjCSelectorName->LongestObjCSelectorName = |
| 498 | Contexts.back().LongestObjCSelectorName; |
Daniel Jasper | 63d7ced | 2013-02-05 10:07:47 +0000 | [diff] [blame] | 499 | return LT_ObjCMethodDecl; |
| 500 | } |
| 501 | |
Daniel Jasper | 32d28ee | 2013-01-29 21:01:14 +0000 | [diff] [blame] | 502 | return LT_Other; |
| 503 | } |
| 504 | |
Nico Weber | 95e8e46 | 2013-02-12 16:17:07 +0000 | [diff] [blame] | 505 | private: |
Daniel Jasper | 32d28ee | 2013-01-29 21:01:14 +0000 | [diff] [blame] | 506 | void next() { |
Daniel Jasper | 0178673 | 2013-02-04 07:21:18 +0000 | [diff] [blame] | 507 | if (CurrentToken != NULL) { |
| 508 | determineTokenType(*CurrentToken); |
Daniel Jasper | 4e77809 | 2013-02-06 10:05:46 +0000 | [diff] [blame] | 509 | CurrentToken->BindingStrength = Contexts.back().BindingStrength; |
Daniel Jasper | 0178673 | 2013-02-04 07:21:18 +0000 | [diff] [blame] | 510 | } |
| 511 | |
Daniel Jasper | 32d28ee | 2013-01-29 21:01:14 +0000 | [diff] [blame] | 512 | if (CurrentToken != NULL && !CurrentToken->Children.empty()) |
| 513 | CurrentToken = &CurrentToken->Children[0]; |
| 514 | else |
| 515 | CurrentToken = NULL; |
Daniel Jasper | d0f349b | 2013-02-18 12:44:35 +0000 | [diff] [blame] | 516 | |
| 517 | // Reset token type in case we have already looked at it and then recovered |
| 518 | // from an error (e.g. failure to find the matching >). |
| 519 | if (CurrentToken != NULL) |
| 520 | CurrentToken->Type = TT_Unknown; |
Daniel Jasper | 32d28ee | 2013-01-29 21:01:14 +0000 | [diff] [blame] | 521 | } |
| 522 | |
Daniel Jasper | 4e77809 | 2013-02-06 10:05:46 +0000 | [diff] [blame] | 523 | /// \brief A struct to hold information valid in a specific context, e.g. |
| 524 | /// a pair of parenthesis. |
| 525 | struct Context { |
| 526 | Context(unsigned BindingStrength, bool IsExpression) |
| 527 | : BindingStrength(BindingStrength), LongestObjCSelectorName(0), |
| 528 | ColonIsForRangeExpr(false), ColonIsObjCMethodExpr(false), |
Daniel Jasper | 6f21a98 | 2013-03-13 07:49:51 +0000 | [diff] [blame^] | 529 | FirstObjCSelectorName(NULL), IsExpression(IsExpression), |
| 530 | CanBeExpression(true) {} |
Daniel Jasper | 0178673 | 2013-02-04 07:21:18 +0000 | [diff] [blame] | 531 | |
Daniel Jasper | 4e77809 | 2013-02-06 10:05:46 +0000 | [diff] [blame] | 532 | unsigned BindingStrength; |
| 533 | unsigned LongestObjCSelectorName; |
| 534 | bool ColonIsForRangeExpr; |
| 535 | bool ColonIsObjCMethodExpr; |
| 536 | AnnotatedToken *FirstObjCSelectorName; |
| 537 | bool IsExpression; |
Daniel Jasper | 6f21a98 | 2013-03-13 07:49:51 +0000 | [diff] [blame^] | 538 | bool CanBeExpression; |
Daniel Jasper | 4e77809 | 2013-02-06 10:05:46 +0000 | [diff] [blame] | 539 | }; |
| 540 | |
| 541 | /// \brief Puts a new \c Context onto the stack \c Contexts for the lifetime |
| 542 | /// of each instance. |
| 543 | struct ScopedContextCreator { |
| 544 | AnnotatingParser &P; |
| 545 | |
Daniel Jasper | fc75908 | 2013-02-14 14:26:07 +0000 | [diff] [blame] | 546 | ScopedContextCreator(AnnotatingParser &P, unsigned Increase) : P(P) { |
| 547 | P.Contexts.push_back(Context(P.Contexts.back().BindingStrength + Increase, |
| 548 | P.Contexts.back().IsExpression)); |
Daniel Jasper | 4e77809 | 2013-02-06 10:05:46 +0000 | [diff] [blame] | 549 | } |
| 550 | |
| 551 | ~ScopedContextCreator() { P.Contexts.pop_back(); } |
| 552 | }; |
Daniel Jasper | 0178673 | 2013-02-04 07:21:18 +0000 | [diff] [blame] | 553 | |
| 554 | void determineTokenType(AnnotatedToken &Current) { |
| 555 | if (getPrecedence(Current) == prec::Assignment) { |
Daniel Jasper | 4e77809 | 2013-02-06 10:05:46 +0000 | [diff] [blame] | 556 | Contexts.back().IsExpression = true; |
Nico Weber | 95e8e46 | 2013-02-12 16:17:07 +0000 | [diff] [blame] | 557 | for (AnnotatedToken *Previous = Current.Parent; |
| 558 | Previous && Previous->isNot(tok::comma); |
| 559 | Previous = Previous->Parent) { |
Daniel Jasper | 9c65b06 | 2013-02-27 11:43:50 +0000 | [diff] [blame] | 560 | if (Previous->is(tok::r_square)) |
| 561 | Previous = Previous->MatchingParen; |
Daniel Jasper | 0178673 | 2013-02-04 07:21:18 +0000 | [diff] [blame] | 562 | if (Previous->Type == TT_BinaryOperator && |
| 563 | (Previous->is(tok::star) || Previous->is(tok::amp))) { |
| 564 | Previous->Type = TT_PointerOrReference; |
| 565 | } |
Daniel Jasper | 0178673 | 2013-02-04 07:21:18 +0000 | [diff] [blame] | 566 | } |
Nico Weber | 95e8e46 | 2013-02-12 16:17:07 +0000 | [diff] [blame] | 567 | } else if (Current.is(tok::kw_return) || Current.is(tok::kw_throw) || |
| 568 | (Current.is(tok::l_paren) && !Line.MustBeDeclaration && |
| 569 | (!Current.Parent || Current.Parent->isNot(tok::kw_for)))) { |
Daniel Jasper | 4e77809 | 2013-02-06 10:05:46 +0000 | [diff] [blame] | 570 | Contexts.back().IsExpression = true; |
Nico Weber | 95e8e46 | 2013-02-12 16:17:07 +0000 | [diff] [blame] | 571 | } else if (Current.is(tok::r_paren) || Current.is(tok::greater) || |
| 572 | Current.is(tok::comma)) { |
| 573 | for (AnnotatedToken *Previous = Current.Parent; |
| 574 | Previous && (Previous->is(tok::star) || Previous->is(tok::amp)); |
| 575 | Previous = Previous->Parent) |
| 576 | Previous->Type = TT_PointerOrReference; |
Daniel Jasper | d0f349b | 2013-02-18 12:44:35 +0000 | [diff] [blame] | 577 | } else if (Current.Parent && |
| 578 | Current.Parent->Type == TT_CtorInitializerColon) { |
| 579 | Contexts.back().IsExpression = true; |
Daniel Jasper | 6f21a98 | 2013-03-13 07:49:51 +0000 | [diff] [blame^] | 580 | } else if (Current.is(tok::kw_new)) { |
| 581 | Contexts.back().CanBeExpression = false; |
Nico Weber | 95e8e46 | 2013-02-12 16:17:07 +0000 | [diff] [blame] | 582 | } |
Daniel Jasper | 0178673 | 2013-02-04 07:21:18 +0000 | [diff] [blame] | 583 | |
| 584 | if (Current.Type == TT_Unknown) { |
Daniel Jasper | 3c08a81 | 2013-02-24 18:54:32 +0000 | [diff] [blame] | 585 | if (Current.Parent && Current.is(tok::identifier) && |
| 586 | ((Current.Parent->is(tok::identifier) && |
| 587 | Current.Parent->FormatTok.Tok.getIdentifierInfo() |
| 588 | ->getPPKeywordID() == tok::pp_not_keyword) || |
| 589 | Current.Parent->Type == TT_PointerOrReference || |
| 590 | Current.Parent->Type == TT_TemplateCloser)) { |
| 591 | Current.Type = TT_StartOfName; |
Alexander Kornienko | 3fd9ccd | 2013-03-12 16:28:18 +0000 | [diff] [blame] | 592 | } else if (Current.is(tok::star) || Current.is(tok::amp) || |
| 593 | Current.is(tok::ampamp)) { |
Daniel Jasper | 4e77809 | 2013-02-06 10:05:46 +0000 | [diff] [blame] | 594 | Current.Type = |
| 595 | determineStarAmpUsage(Current, Contexts.back().IsExpression); |
Daniel Jasper | 0178673 | 2013-02-04 07:21:18 +0000 | [diff] [blame] | 596 | } else if (Current.is(tok::minus) || Current.is(tok::plus) || |
| 597 | Current.is(tok::caret)) { |
| 598 | Current.Type = determinePlusMinusCaretUsage(Current); |
| 599 | } else if (Current.is(tok::minusminus) || Current.is(tok::plusplus)) { |
| 600 | Current.Type = determineIncrementUsage(Current); |
| 601 | } else if (Current.is(tok::exclaim)) { |
| 602 | Current.Type = TT_UnaryOperator; |
| 603 | } else if (isBinaryOperator(Current)) { |
| 604 | Current.Type = TT_BinaryOperator; |
| 605 | } else if (Current.is(tok::comment)) { |
| 606 | std::string Data(Lexer::getSpelling(Current.FormatTok.Tok, SourceMgr, |
| 607 | Lex.getLangOpts())); |
| 608 | if (StringRef(Data).startswith("//")) |
| 609 | Current.Type = TT_LineComment; |
| 610 | else |
| 611 | Current.Type = TT_BlockComment; |
Nico Weber | 37d6931 | 2013-02-13 04:13:13 +0000 | [diff] [blame] | 612 | } else if (Current.is(tok::r_paren)) { |
Daniel Jasper | 03628b8 | 2013-02-19 20:05:41 +0000 | [diff] [blame] | 613 | bool ParensNotExpr = !Current.Parent || |
| 614 | Current.Parent->Type == TT_PointerOrReference || |
Nico Weber | 37d6931 | 2013-02-13 04:13:13 +0000 | [diff] [blame] | 615 | Current.Parent->Type == TT_TemplateCloser; |
| 616 | bool ParensCouldEndDecl = |
| 617 | !Current.Children.empty() && (Current.Children[0].is(tok::equal) || |
| 618 | Current.Children[0].is(tok::semi) || |
| 619 | Current.Children[0].is(tok::l_brace)); |
Daniel Jasper | 37eff83 | 2013-02-23 08:07:18 +0000 | [diff] [blame] | 620 | if (ParensNotExpr && !ParensCouldEndDecl && |
| 621 | Contexts.back().IsExpression) |
Nico Weber | 37d6931 | 2013-02-13 04:13:13 +0000 | [diff] [blame] | 622 | // FIXME: We need to get smarter and understand more cases of casts. |
| 623 | Current.Type = TT_CastRParen; |
Daniel Jasper | 0178673 | 2013-02-04 07:21:18 +0000 | [diff] [blame] | 624 | } else if (Current.is(tok::at) && Current.Children.size()) { |
| 625 | switch (Current.Children[0].FormatTok.Tok.getObjCKeywordID()) { |
| 626 | case tok::objc_interface: |
| 627 | case tok::objc_implementation: |
| 628 | case tok::objc_protocol: |
| 629 | Current.Type = TT_ObjCDecl; |
| 630 | break; |
| 631 | case tok::objc_property: |
| 632 | Current.Type = TT_ObjCProperty; |
| 633 | break; |
| 634 | default: |
| 635 | break; |
| 636 | } |
| 637 | } |
| 638 | } |
| 639 | } |
| 640 | |
Daniel Jasper | 0178673 | 2013-02-04 07:21:18 +0000 | [diff] [blame] | 641 | /// \brief Return the type of the given token assuming it is * or &. |
| 642 | TokenType |
| 643 | determineStarAmpUsage(const AnnotatedToken &Tok, bool IsExpression) { |
| 644 | const AnnotatedToken *PrevToken = getPreviousToken(Tok); |
| 645 | if (PrevToken == NULL) |
| 646 | return TT_UnaryOperator; |
| 647 | |
| 648 | const AnnotatedToken *NextToken = getNextToken(Tok); |
| 649 | if (NextToken == NULL) |
| 650 | return TT_Unknown; |
| 651 | |
Daniel Jasper | 8a5d7cd | 2013-03-01 17:13:29 +0000 | [diff] [blame] | 652 | if (PrevToken->is(tok::l_paren) && !IsExpression) |
| 653 | return TT_PointerOrReference; |
| 654 | |
Daniel Jasper | 0178673 | 2013-02-04 07:21:18 +0000 | [diff] [blame] | 655 | if (PrevToken->is(tok::l_paren) || PrevToken->is(tok::l_square) || |
| 656 | PrevToken->is(tok::l_brace) || PrevToken->is(tok::comma) || |
| 657 | PrevToken->is(tok::kw_return) || PrevToken->is(tok::colon) || |
Nico Weber | e8a9798 | 2013-02-06 06:20:11 +0000 | [diff] [blame] | 658 | PrevToken->is(tok::equal) || PrevToken->Type == TT_BinaryOperator || |
Daniel Jasper | 0178673 | 2013-02-04 07:21:18 +0000 | [diff] [blame] | 659 | PrevToken->Type == TT_UnaryOperator || PrevToken->Type == TT_CastRParen) |
| 660 | return TT_UnaryOperator; |
| 661 | |
Nico Weber | e8a9798 | 2013-02-06 06:20:11 +0000 | [diff] [blame] | 662 | if (NextToken->is(tok::l_square)) |
| 663 | return TT_PointerOrReference; |
| 664 | |
Daniel Jasper | 0178673 | 2013-02-04 07:21:18 +0000 | [diff] [blame] | 665 | if (PrevToken->FormatTok.Tok.isLiteral() || PrevToken->is(tok::r_paren) || |
| 666 | PrevToken->is(tok::r_square) || NextToken->FormatTok.Tok.isLiteral() || |
Nico Weber | ee0feec | 2013-02-05 16:21:00 +0000 | [diff] [blame] | 667 | isUnaryOperator(*NextToken) || NextToken->is(tok::l_paren) || |
| 668 | NextToken->is(tok::l_square)) |
Daniel Jasper | 0178673 | 2013-02-04 07:21:18 +0000 | [diff] [blame] | 669 | return TT_BinaryOperator; |
| 670 | |
Daniel Jasper | 0178673 | 2013-02-04 07:21:18 +0000 | [diff] [blame] | 671 | // It is very unlikely that we are going to find a pointer or reference type |
| 672 | // definition on the RHS of an assignment. |
| 673 | if (IsExpression) |
| 674 | return TT_BinaryOperator; |
| 675 | |
| 676 | return TT_PointerOrReference; |
| 677 | } |
| 678 | |
| 679 | TokenType determinePlusMinusCaretUsage(const AnnotatedToken &Tok) { |
| 680 | const AnnotatedToken *PrevToken = getPreviousToken(Tok); |
| 681 | if (PrevToken == NULL) |
| 682 | return TT_UnaryOperator; |
| 683 | |
| 684 | // Use heuristics to recognize unary operators. |
| 685 | if (PrevToken->is(tok::equal) || PrevToken->is(tok::l_paren) || |
| 686 | PrevToken->is(tok::comma) || PrevToken->is(tok::l_square) || |
| 687 | PrevToken->is(tok::question) || PrevToken->is(tok::colon) || |
| 688 | PrevToken->is(tok::kw_return) || PrevToken->is(tok::kw_case) || |
| 689 | PrevToken->is(tok::at) || PrevToken->is(tok::l_brace)) |
| 690 | return TT_UnaryOperator; |
| 691 | |
Nico Weber | ee0feec | 2013-02-05 16:21:00 +0000 | [diff] [blame] | 692 | // There can't be two consecutive binary operators. |
Daniel Jasper | 0178673 | 2013-02-04 07:21:18 +0000 | [diff] [blame] | 693 | if (PrevToken->Type == TT_BinaryOperator) |
| 694 | return TT_UnaryOperator; |
| 695 | |
| 696 | // Fall back to marking the token as binary operator. |
| 697 | return TT_BinaryOperator; |
| 698 | } |
| 699 | |
| 700 | /// \brief Determine whether ++/-- are pre- or post-increments/-decrements. |
| 701 | TokenType determineIncrementUsage(const AnnotatedToken &Tok) { |
| 702 | const AnnotatedToken *PrevToken = getPreviousToken(Tok); |
| 703 | if (PrevToken == NULL) |
| 704 | return TT_UnaryOperator; |
| 705 | if (PrevToken->is(tok::r_paren) || PrevToken->is(tok::r_square) || |
| 706 | PrevToken->is(tok::identifier)) |
| 707 | return TT_TrailingUnaryOperator; |
| 708 | |
| 709 | return TT_UnaryOperator; |
| 710 | } |
Daniel Jasper | 4e77809 | 2013-02-06 10:05:46 +0000 | [diff] [blame] | 711 | |
| 712 | SmallVector<Context, 8> Contexts; |
| 713 | |
| 714 | SourceManager &SourceMgr; |
| 715 | Lexer &Lex; |
| 716 | AnnotatedLine &Line; |
| 717 | AnnotatedToken *CurrentToken; |
| 718 | bool KeywordVirtualFound; |
Nico Weber | c2e6d2a | 2013-02-11 15:32:15 +0000 | [diff] [blame] | 719 | IdentifierInfo &Ident_in; |
Daniel Jasper | 32d28ee | 2013-01-29 21:01:14 +0000 | [diff] [blame] | 720 | }; |
| 721 | |
Daniel Jasper | 29f123b | 2013-02-08 15:28:42 +0000 | [diff] [blame] | 722 | /// \brief Parses binary expressions by inserting fake parenthesis based on |
| 723 | /// operator precedence. |
| 724 | class ExpressionParser { |
| 725 | public: |
| 726 | ExpressionParser(AnnotatedLine &Line) : Current(&Line.First) {} |
| 727 | |
| 728 | /// \brief Parse expressions with the given operatore precedence. |
Daniel Jasper | 237d4c1 | 2013-02-23 21:01:55 +0000 | [diff] [blame] | 729 | void parse(int Precedence = 0) { |
Daniel Jasper | 29f123b | 2013-02-08 15:28:42 +0000 | [diff] [blame] | 730 | if (Precedence > prec::PointerToMember || Current == NULL) |
| 731 | return; |
| 732 | |
| 733 | // Skip over "return" until we can properly parse it. |
| 734 | if (Current->is(tok::kw_return)) |
| 735 | next(); |
| 736 | |
| 737 | // Eagerly consume trailing comments. |
| 738 | while (isTrailingComment(Current)) { |
| 739 | next(); |
| 740 | } |
| 741 | |
| 742 | AnnotatedToken *Start = Current; |
| 743 | bool OperatorFound = false; |
| 744 | |
Daniel Jasper | 237d4c1 | 2013-02-23 21:01:55 +0000 | [diff] [blame] | 745 | while (Current) { |
Daniel Jasper | 29f123b | 2013-02-08 15:28:42 +0000 | [diff] [blame] | 746 | // Consume operators with higher precedence. |
Dmitri Gribenko | 6ba52aa | 2013-02-16 20:03:26 +0000 | [diff] [blame] | 747 | parse(prec::Level(Precedence + 1)); |
Daniel Jasper | 29f123b | 2013-02-08 15:28:42 +0000 | [diff] [blame] | 748 | |
Daniel Jasper | 237d4c1 | 2013-02-23 21:01:55 +0000 | [diff] [blame] | 749 | int CurrentPrecedence = 0; |
| 750 | if (Current) { |
| 751 | if (Current->Type == TT_ConditionalExpr) |
| 752 | CurrentPrecedence = 1 + (int) prec::Conditional; |
| 753 | else if (Current->is(tok::semi)) |
| 754 | CurrentPrecedence = 1; |
| 755 | else if (Current->Type == TT_BinaryOperator || Current->is(tok::comma)) |
| 756 | CurrentPrecedence = 1 + (int) getPrecedence(*Current); |
| 757 | } |
| 758 | |
Daniel Jasper | 29f123b | 2013-02-08 15:28:42 +0000 | [diff] [blame] | 759 | // At the end of the line or when an operator with higher precedence is |
| 760 | // found, insert fake parenthesis and return. |
Daniel Jasper | 237d4c1 | 2013-02-23 21:01:55 +0000 | [diff] [blame] | 761 | if (Current == NULL || closesScope(*Current) || |
| 762 | (CurrentPrecedence != 0 && CurrentPrecedence < Precedence)) { |
Daniel Jasper | 29f123b | 2013-02-08 15:28:42 +0000 | [diff] [blame] | 763 | if (OperatorFound) { |
| 764 | ++Start->FakeLParens; |
Daniel Jasper | 237d4c1 | 2013-02-23 21:01:55 +0000 | [diff] [blame] | 765 | if (Current) |
Daniel Jasper | 087387a | 2013-02-08 16:49:27 +0000 | [diff] [blame] | 766 | ++Current->Parent->FakeRParens; |
Daniel Jasper | 29f123b | 2013-02-08 15:28:42 +0000 | [diff] [blame] | 767 | } |
| 768 | return; |
| 769 | } |
| 770 | |
| 771 | // Consume scopes: (), [], <> and {} |
| 772 | if (opensScope(*Current)) { |
Daniel Jasper | 237d4c1 | 2013-02-23 21:01:55 +0000 | [diff] [blame] | 773 | AnnotatedToken *Left = Current; |
| 774 | while (Current && !closesScope(*Current)) { |
Daniel Jasper | 29f123b | 2013-02-08 15:28:42 +0000 | [diff] [blame] | 775 | next(); |
| 776 | parse(); |
| 777 | } |
Daniel Jasper | 237d4c1 | 2013-02-23 21:01:55 +0000 | [diff] [blame] | 778 | // Remove fake parens that just duplicate the real parens. |
| 779 | if (Current && Left->Children[0].FakeLParens > 0 && |
| 780 | Current->Parent->FakeRParens > 0) { |
| 781 | --Left->Children[0].FakeLParens; |
| 782 | --Current->Parent->FakeRParens; |
| 783 | } |
Daniel Jasper | 29f123b | 2013-02-08 15:28:42 +0000 | [diff] [blame] | 784 | next(); |
| 785 | } else { |
| 786 | // Operator found. |
Daniel Jasper | 237d4c1 | 2013-02-23 21:01:55 +0000 | [diff] [blame] | 787 | if (CurrentPrecedence == Precedence) |
Daniel Jasper | 29f123b | 2013-02-08 15:28:42 +0000 | [diff] [blame] | 788 | OperatorFound = true; |
| 789 | |
| 790 | next(); |
| 791 | } |
| 792 | } |
| 793 | } |
| 794 | |
| 795 | private: |
| 796 | void next() { |
| 797 | if (Current != NULL) |
| 798 | Current = Current->Children.empty() ? NULL : &Current->Children[0]; |
| 799 | } |
| 800 | |
| 801 | bool closesScope(const AnnotatedToken &Tok) { |
| 802 | return Current->is(tok::r_paren) || Current->Type == TT_TemplateCloser || |
| 803 | Current->is(tok::r_brace) || Current->is(tok::r_square); |
| 804 | } |
| 805 | |
| 806 | bool opensScope(const AnnotatedToken &Tok) { |
| 807 | return Current->is(tok::l_paren) || Current->Type == TT_TemplateOpener || |
| 808 | Current->is(tok::l_brace) || Current->is(tok::l_square); |
| 809 | } |
| 810 | |
| 811 | AnnotatedToken *Current; |
| 812 | }; |
| 813 | |
Daniel Jasper | 8ff690a | 2013-02-06 14:22:40 +0000 | [diff] [blame] | 814 | void TokenAnnotator::annotate(AnnotatedLine &Line) { |
Nico Weber | c2e6d2a | 2013-02-11 15:32:15 +0000 | [diff] [blame] | 815 | AnnotatingParser Parser(SourceMgr, Lex, Line, Ident_in); |
Daniel Jasper | 32d28ee | 2013-01-29 21:01:14 +0000 | [diff] [blame] | 816 | Line.Type = Parser.parseLine(); |
| 817 | if (Line.Type == LT_Invalid) |
| 818 | return; |
| 819 | |
Daniel Jasper | 29f123b | 2013-02-08 15:28:42 +0000 | [diff] [blame] | 820 | ExpressionParser ExprParser(Line); |
| 821 | ExprParser.parse(); |
| 822 | |
Daniel Jasper | 32d28ee | 2013-01-29 21:01:14 +0000 | [diff] [blame] | 823 | if (Line.First.Type == TT_ObjCMethodSpecifier) |
| 824 | Line.Type = LT_ObjCMethodDecl; |
| 825 | else if (Line.First.Type == TT_ObjCDecl) |
| 826 | Line.Type = LT_ObjCDecl; |
| 827 | else if (Line.First.Type == TT_ObjCProperty) |
| 828 | Line.Type = LT_ObjCProperty; |
| 829 | |
Daniel Jasper | 729a743 | 2013-02-11 12:36:37 +0000 | [diff] [blame] | 830 | Line.First.SpacesRequiredBefore = 1; |
Daniel Jasper | 32d28ee | 2013-01-29 21:01:14 +0000 | [diff] [blame] | 831 | Line.First.MustBreakBefore = Line.First.FormatTok.MustBreakBefore; |
| 832 | Line.First.CanBreakBefore = Line.First.MustBreakBefore; |
| 833 | |
| 834 | Line.First.TotalLength = Line.First.FormatTok.TokenLength; |
Daniel Jasper | 32d28ee | 2013-01-29 21:01:14 +0000 | [diff] [blame] | 835 | } |
| 836 | |
Daniel Jasper | 8ff690a | 2013-02-06 14:22:40 +0000 | [diff] [blame] | 837 | void TokenAnnotator::calculateFormattingInformation(AnnotatedLine &Line) { |
| 838 | if (Line.First.Children.empty()) |
| 839 | return; |
| 840 | AnnotatedToken *Current = &Line.First.Children[0]; |
| 841 | while (Current != NULL) { |
Daniel Jasper | 729a743 | 2013-02-11 12:36:37 +0000 | [diff] [blame] | 842 | if (Current->Type == TT_LineComment) |
| 843 | Current->SpacesRequiredBefore = Style.SpacesBeforeTrailingComments; |
| 844 | else |
| 845 | Current->SpacesRequiredBefore = |
| 846 | spaceRequiredBefore(Line, *Current) ? 1 : 0; |
Daniel Jasper | 32d28ee | 2013-01-29 21:01:14 +0000 | [diff] [blame] | 847 | |
Daniel Jasper | 8ff690a | 2013-02-06 14:22:40 +0000 | [diff] [blame] | 848 | if (Current->FormatTok.MustBreakBefore) { |
| 849 | Current->MustBreakBefore = true; |
| 850 | } else if (Current->Type == TT_LineComment) { |
| 851 | Current->MustBreakBefore = Current->FormatTok.NewlinesBefore > 0; |
Daniel Jasper | 29f123b | 2013-02-08 15:28:42 +0000 | [diff] [blame] | 852 | } else if (isTrailingComment(Current->Parent) || |
Daniel Jasper | 8ff690a | 2013-02-06 14:22:40 +0000 | [diff] [blame] | 853 | (Current->is(tok::string_literal) && |
| 854 | Current->Parent->is(tok::string_literal))) { |
| 855 | Current->MustBreakBefore = true; |
| 856 | } else if (Current->is(tok::lessless) && !Current->Children.empty() && |
| 857 | Current->Parent->is(tok::string_literal) && |
| 858 | Current->Children[0].is(tok::string_literal)) { |
| 859 | Current->MustBreakBefore = true; |
| 860 | } else { |
| 861 | Current->MustBreakBefore = false; |
| 862 | } |
| 863 | Current->CanBreakBefore = |
| 864 | Current->MustBreakBefore || canBreakBefore(Line, *Current); |
| 865 | if (Current->MustBreakBefore) |
| 866 | Current->TotalLength = Current->Parent->TotalLength + Style.ColumnLimit; |
| 867 | else |
| 868 | Current->TotalLength = |
| 869 | Current->Parent->TotalLength + Current->FormatTok.TokenLength + |
Daniel Jasper | 729a743 | 2013-02-11 12:36:37 +0000 | [diff] [blame] | 870 | Current->SpacesRequiredBefore; |
Daniel Jasper | 8ff690a | 2013-02-06 14:22:40 +0000 | [diff] [blame] | 871 | // FIXME: Only calculate this if CanBreakBefore is true once static |
| 872 | // initializers etc. are sorted out. |
| 873 | // FIXME: Move magic numbers to a better place. |
| 874 | Current->SplitPenalty = |
| 875 | 20 * Current->BindingStrength + splitPenalty(Line, *Current); |
| 876 | |
| 877 | Current = Current->Children.empty() ? NULL : &Current->Children[0]; |
Daniel Jasper | 32d28ee | 2013-01-29 21:01:14 +0000 | [diff] [blame] | 878 | } |
Daniel Jasper | 32d28ee | 2013-01-29 21:01:14 +0000 | [diff] [blame] | 879 | } |
| 880 | |
Daniel Jasper | 8ff690a | 2013-02-06 14:22:40 +0000 | [diff] [blame] | 881 | unsigned TokenAnnotator::splitPenalty(const AnnotatedLine &Line, |
| 882 | const AnnotatedToken &Tok) { |
Daniel Jasper | 32d28ee | 2013-01-29 21:01:14 +0000 | [diff] [blame] | 883 | const AnnotatedToken &Left = *Tok.Parent; |
| 884 | const AnnotatedToken &Right = Tok; |
| 885 | |
Daniel Jasper | 3c08a81 | 2013-02-24 18:54:32 +0000 | [diff] [blame] | 886 | if (Right.Type == TT_StartOfName) { |
| 887 | if (Line.First.is(tok::kw_for)) |
| 888 | return 3; |
| 889 | else if (Line.MightBeFunctionDecl && Right.BindingStrength == 1) |
| 890 | // FIXME: Clean up hack of using BindingStrength to find top-level names. |
| 891 | return Style.PenaltyReturnTypeOnItsOwnLine; |
| 892 | else |
| 893 | return 100; |
| 894 | } |
Daniel Jasper | 32d28ee | 2013-01-29 21:01:14 +0000 | [diff] [blame] | 895 | if (Left.is(tok::equal) && Right.is(tok::l_brace)) |
| 896 | return 150; |
| 897 | if (Left.is(tok::coloncolon)) |
| 898 | return 500; |
| 899 | |
Daniel Jasper | 6cabab4 | 2013-02-14 08:42:54 +0000 | [diff] [blame] | 900 | if (Left.Type == TT_RangeBasedForLoopColon || |
| 901 | Left.Type == TT_InheritanceColon) |
Daniel Jasper | 84a1a63 | 2013-02-26 13:18:08 +0000 | [diff] [blame] | 902 | return 2; |
Daniel Jasper | 32d28ee | 2013-01-29 21:01:14 +0000 | [diff] [blame] | 903 | |
| 904 | if (Right.is(tok::arrow) || Right.is(tok::period)) { |
Daniel Jasper | 515f65d | 2013-02-18 13:24:21 +0000 | [diff] [blame] | 905 | if (Line.Type == LT_BuilderTypeCall) |
Daniel Jasper | 518ee34 | 2013-02-26 13:59:14 +0000 | [diff] [blame] | 906 | return 5; |
Daniel Jasper | fc75908 | 2013-02-14 14:26:07 +0000 | [diff] [blame] | 907 | if ((Left.is(tok::r_paren) || Left.is(tok::r_square)) && |
| 908 | Left.MatchingParen && Left.MatchingParen->ParameterCount > 0) |
Daniel Jasper | 518ee34 | 2013-02-26 13:59:14 +0000 | [diff] [blame] | 909 | return 20; // Should be smaller than breaking at a nested comma. |
Daniel Jasper | 32d28ee | 2013-01-29 21:01:14 +0000 | [diff] [blame] | 910 | return 150; |
| 911 | } |
| 912 | |
| 913 | // In for-loops, prefer breaking at ',' and ';'. |
Daniel Jasper | 7d81281 | 2013-02-21 15:00:29 +0000 | [diff] [blame] | 914 | if (Line.First.is(tok::kw_for) && Left.is(tok::equal)) |
| 915 | return 4; |
Daniel Jasper | 32d28ee | 2013-01-29 21:01:14 +0000 | [diff] [blame] | 916 | |
Daniel Jasper | 8159d2f | 2013-02-04 07:30:30 +0000 | [diff] [blame] | 917 | if (Left.is(tok::semi)) |
Daniel Jasper | 32d28ee | 2013-01-29 21:01:14 +0000 | [diff] [blame] | 918 | return 0; |
Daniel Jasper | 8159d2f | 2013-02-04 07:30:30 +0000 | [diff] [blame] | 919 | if (Left.is(tok::comma)) |
| 920 | return 1; |
Daniel Jasper | 32d28ee | 2013-01-29 21:01:14 +0000 | [diff] [blame] | 921 | |
| 922 | // In Objective-C method expressions, prefer breaking before "param:" over |
| 923 | // breaking after it. |
Daniel Jasper | 63d7ced | 2013-02-05 10:07:47 +0000 | [diff] [blame] | 924 | if (Right.Type == TT_ObjCSelectorName) |
Daniel Jasper | 32d28ee | 2013-01-29 21:01:14 +0000 | [diff] [blame] | 925 | return 0; |
Daniel Jasper | 63d7ced | 2013-02-05 10:07:47 +0000 | [diff] [blame] | 926 | if (Left.is(tok::colon) && Left.Type == TT_ObjCMethodExpr) |
Daniel Jasper | 32d28ee | 2013-01-29 21:01:14 +0000 | [diff] [blame] | 927 | return 20; |
| 928 | |
Daniel Jasper | 0178673 | 2013-02-04 07:21:18 +0000 | [diff] [blame] | 929 | if (Left.is(tok::l_paren) || Left.is(tok::l_square) || |
Daniel Jasper | 25e81b2 | 2013-02-28 15:04:12 +0000 | [diff] [blame] | 930 | Left.is(tok::l_brace) || Left.Type == TT_TemplateOpener) |
Daniel Jasper | 32d28ee | 2013-01-29 21:01:14 +0000 | [diff] [blame] | 931 | return 20; |
Daniel Jasper | 32d28ee | 2013-01-29 21:01:14 +0000 | [diff] [blame] | 932 | |
Daniel Jasper | 4e8a7b4 | 2013-02-06 21:04:05 +0000 | [diff] [blame] | 933 | if (Right.is(tok::lessless)) { |
| 934 | if (Left.is(tok::string_literal)) { |
| 935 | char LastChar = |
| 936 | StringRef(Left.FormatTok.Tok.getLiteralData(), |
| 937 | Left.FormatTok.TokenLength).drop_back(1).rtrim().back(); |
| 938 | if (LastChar == ':' || LastChar == '=') |
| 939 | return 100; |
| 940 | } |
Daniel Jasper | 0178673 | 2013-02-04 07:21:18 +0000 | [diff] [blame] | 941 | return prec::Shift; |
Daniel Jasper | 4e8a7b4 | 2013-02-06 21:04:05 +0000 | [diff] [blame] | 942 | } |
Daniel Jasper | 32d28ee | 2013-01-29 21:01:14 +0000 | [diff] [blame] | 943 | if (Left.Type == TT_ConditionalExpr) |
Daniel Jasper | 518ee34 | 2013-02-26 13:59:14 +0000 | [diff] [blame] | 944 | return prec::Conditional; |
Daniel Jasper | 32d28ee | 2013-01-29 21:01:14 +0000 | [diff] [blame] | 945 | prec::Level Level = getPrecedence(Left); |
| 946 | |
| 947 | if (Level != prec::Unknown) |
| 948 | return Level; |
Daniel Jasper | 2484971 | 2013-03-01 16:48:32 +0000 | [diff] [blame] | 949 | |
Daniel Jasper | 32d28ee | 2013-01-29 21:01:14 +0000 | [diff] [blame] | 950 | return 3; |
| 951 | } |
| 952 | |
Daniel Jasper | 8ff690a | 2013-02-06 14:22:40 +0000 | [diff] [blame] | 953 | bool TokenAnnotator::spaceRequiredBetween(const AnnotatedLine &Line, |
| 954 | const AnnotatedToken &Left, |
Daniel Jasper | 32d28ee | 2013-01-29 21:01:14 +0000 | [diff] [blame] | 955 | const AnnotatedToken &Right) { |
| 956 | if (Right.is(tok::hashhash)) |
| 957 | return Left.is(tok::hash); |
| 958 | if (Left.is(tok::hashhash) || Left.is(tok::hash)) |
| 959 | return Right.is(tok::hash); |
| 960 | if (Right.is(tok::r_paren) || Right.is(tok::semi) || Right.is(tok::comma)) |
| 961 | return false; |
| 962 | if (Right.is(tok::less) && |
| 963 | (Left.is(tok::kw_template) || |
| 964 | (Line.Type == LT_ObjCDecl && Style.ObjCSpaceBeforeProtocolList))) |
| 965 | return true; |
| 966 | if (Left.is(tok::arrow) || Right.is(tok::arrow)) |
| 967 | return false; |
| 968 | if (Left.is(tok::exclaim) || Left.is(tok::tilde)) |
| 969 | return false; |
| 970 | if (Left.is(tok::at) && |
| 971 | (Right.is(tok::identifier) || Right.is(tok::string_literal) || |
| 972 | Right.is(tok::char_constant) || Right.is(tok::numeric_constant) || |
| 973 | Right.is(tok::l_paren) || Right.is(tok::l_brace) || |
| 974 | Right.is(tok::kw_true) || Right.is(tok::kw_false))) |
| 975 | return false; |
| 976 | if (Left.is(tok::coloncolon)) |
| 977 | return false; |
| 978 | if (Right.is(tok::coloncolon)) |
Daniel Jasper | daf1a15 | 2013-02-07 21:08:36 +0000 | [diff] [blame] | 979 | return Left.isNot(tok::identifier) && Left.isNot(tok::greater) && |
| 980 | Left.isNot(tok::l_paren); |
Daniel Jasper | 32d28ee | 2013-01-29 21:01:14 +0000 | [diff] [blame] | 981 | if (Left.is(tok::less) || Right.is(tok::greater) || Right.is(tok::less)) |
| 982 | return false; |
Alexander Kornienko | 3fd9ccd | 2013-03-12 16:28:18 +0000 | [diff] [blame] | 983 | if (Right.Type == TT_PointerOrReference) |
Daniel Jasper | 32d28ee | 2013-01-29 21:01:14 +0000 | [diff] [blame] | 984 | return Left.FormatTok.Tok.isLiteral() || |
Alexander Kornienko | 3fd9ccd | 2013-03-12 16:28:18 +0000 | [diff] [blame] | 985 | ((Left.Type != TT_PointerOrReference) && Left.isNot(tok::l_paren) && |
| 986 | !Style.PointerBindsToType); |
| 987 | if (Left.Type == TT_PointerOrReference) |
Nico Weber | 95e8e46 | 2013-02-12 16:17:07 +0000 | [diff] [blame] | 988 | return Right.FormatTok.Tok.isLiteral() || |
Alexander Kornienko | 3fd9ccd | 2013-03-12 16:28:18 +0000 | [diff] [blame] | 989 | ((Right.Type != TT_PointerOrReference) && Style.PointerBindsToType); |
Daniel Jasper | 32d28ee | 2013-01-29 21:01:14 +0000 | [diff] [blame] | 990 | if (Right.is(tok::star) && Left.is(tok::l_paren)) |
| 991 | return false; |
Nico Weber | 051860e | 2013-02-10 02:08:05 +0000 | [diff] [blame] | 992 | if (Left.is(tok::l_square)) |
| 993 | return Left.Type == TT_ObjCArrayLiteral && Right.isNot(tok::r_square); |
| 994 | if (Right.is(tok::r_square)) |
| 995 | return Right.Type == TT_ObjCArrayLiteral; |
Daniel Jasper | 32d28ee | 2013-01-29 21:01:14 +0000 | [diff] [blame] | 996 | if (Right.is(tok::l_square) && Right.Type != TT_ObjCMethodExpr) |
| 997 | return false; |
| 998 | if (Left.is(tok::period) || Right.is(tok::period)) |
| 999 | return false; |
| 1000 | if (Left.is(tok::colon)) |
| 1001 | return Left.Type != TT_ObjCMethodExpr; |
| 1002 | if (Right.is(tok::colon)) |
| 1003 | return Right.Type != TT_ObjCMethodExpr; |
| 1004 | if (Left.is(tok::l_paren)) |
| 1005 | return false; |
| 1006 | if (Right.is(tok::l_paren)) { |
| 1007 | return Line.Type == LT_ObjCDecl || Left.is(tok::kw_if) || |
| 1008 | Left.is(tok::kw_for) || Left.is(tok::kw_while) || |
| 1009 | Left.is(tok::kw_switch) || Left.is(tok::kw_return) || |
| 1010 | Left.is(tok::kw_catch) || Left.is(tok::kw_new) || |
| 1011 | Left.is(tok::kw_delete); |
| 1012 | } |
| 1013 | if (Left.is(tok::at) && |
| 1014 | Right.FormatTok.Tok.getObjCKeywordID() != tok::objc_not_keyword) |
| 1015 | return false; |
| 1016 | if (Left.is(tok::l_brace) && Right.is(tok::r_brace)) |
| 1017 | return false; |
| 1018 | return true; |
| 1019 | } |
| 1020 | |
Daniel Jasper | 8ff690a | 2013-02-06 14:22:40 +0000 | [diff] [blame] | 1021 | bool TokenAnnotator::spaceRequiredBefore(const AnnotatedLine &Line, |
| 1022 | const AnnotatedToken &Tok) { |
Daniel Jasper | 2b4c924 | 2013-02-11 08:01:18 +0000 | [diff] [blame] | 1023 | if (Tok.FormatTok.Tok.getIdentifierInfo() && |
| 1024 | Tok.Parent->FormatTok.Tok.getIdentifierInfo()) |
| 1025 | return true; // Never ever merge two identifiers. |
Daniel Jasper | 32d28ee | 2013-01-29 21:01:14 +0000 | [diff] [blame] | 1026 | if (Line.Type == LT_ObjCMethodDecl) { |
Daniel Jasper | 32d28ee | 2013-01-29 21:01:14 +0000 | [diff] [blame] | 1027 | if (Tok.Parent->Type == TT_ObjCMethodSpecifier) |
| 1028 | return true; |
| 1029 | if (Tok.Parent->is(tok::r_paren) && Tok.is(tok::identifier)) |
| 1030 | // Don't space between ')' and <id> |
| 1031 | return false; |
Daniel Jasper | 32d28ee | 2013-01-29 21:01:14 +0000 | [diff] [blame] | 1032 | } |
| 1033 | if (Line.Type == LT_ObjCProperty && |
| 1034 | (Tok.is(tok::equal) || Tok.Parent->is(tok::equal))) |
| 1035 | return false; |
| 1036 | |
| 1037 | if (Tok.Parent->is(tok::comma)) |
| 1038 | return true; |
Daniel Jasper | 9c3c7b3 | 2013-02-28 13:40:17 +0000 | [diff] [blame] | 1039 | if (Tok.is(tok::comma)) |
| 1040 | return false; |
Daniel Jasper | 32d28ee | 2013-01-29 21:01:14 +0000 | [diff] [blame] | 1041 | if (Tok.Type == TT_CtorInitializerColon || Tok.Type == TT_ObjCBlockLParen) |
| 1042 | return true; |
Daniel Jasper | 2b4c924 | 2013-02-11 08:01:18 +0000 | [diff] [blame] | 1043 | if (Tok.Parent->FormatTok.Tok.is(tok::kw_operator)) |
| 1044 | return false; |
| 1045 | if (Tok.Type == TT_OverloadedOperatorLParen) |
Daniel Jasper | 32d28ee | 2013-01-29 21:01:14 +0000 | [diff] [blame] | 1046 | return false; |
| 1047 | if (Tok.is(tok::colon)) |
Daniel Jasper | 4ba9bfd | 2013-03-12 12:26:55 +0000 | [diff] [blame] | 1048 | return Line.First.isNot(tok::kw_case) && |
| 1049 | Line.First.isNot(tok::kw_default) && !Tok.Children.empty() && |
Daniel Jasper | 32d28ee | 2013-01-29 21:01:14 +0000 | [diff] [blame] | 1050 | Tok.Type != TT_ObjCMethodExpr; |
Daniel Jasper | 8a5d7cd | 2013-03-01 17:13:29 +0000 | [diff] [blame] | 1051 | if (Tok.is(tok::l_paren) && !Tok.Children.empty() && |
| 1052 | Tok.Children[0].Type == TT_PointerOrReference && |
| 1053 | !Tok.Children[0].Children.empty() && |
| 1054 | Tok.Children[0].Children[0].isNot(tok::r_paren)) |
| 1055 | return true; |
Daniel Jasper | 32d28ee | 2013-01-29 21:01:14 +0000 | [diff] [blame] | 1056 | if (Tok.Parent->Type == TT_UnaryOperator || Tok.Parent->Type == TT_CastRParen) |
| 1057 | return false; |
| 1058 | if (Tok.Type == TT_UnaryOperator) |
| 1059 | return Tok.Parent->isNot(tok::l_paren) && |
| 1060 | Tok.Parent->isNot(tok::l_square) && Tok.Parent->isNot(tok::at) && |
| 1061 | (Tok.Parent->isNot(tok::colon) || |
| 1062 | Tok.Parent->Type != TT_ObjCMethodExpr); |
| 1063 | if (Tok.Parent->is(tok::greater) && Tok.is(tok::greater)) { |
Daniel Jasper | 29f123b | 2013-02-08 15:28:42 +0000 | [diff] [blame] | 1064 | return Tok.Type == TT_TemplateCloser && |
| 1065 | Tok.Parent->Type == TT_TemplateCloser && |
| 1066 | Style.Standard != FormatStyle::LS_Cpp11; |
Daniel Jasper | 32d28ee | 2013-01-29 21:01:14 +0000 | [diff] [blame] | 1067 | } |
Daniel Jasper | 9c3c7b3 | 2013-02-28 13:40:17 +0000 | [diff] [blame] | 1068 | if (Tok.is(tok::arrowstar) || Tok.Parent->is(tok::arrowstar)) |
| 1069 | return false; |
| 1070 | if (Tok.Type == TT_BinaryOperator || Tok.Parent->Type == TT_BinaryOperator) |
Daniel Jasper | 32d28ee | 2013-01-29 21:01:14 +0000 | [diff] [blame] | 1071 | return true; |
| 1072 | if (Tok.Parent->Type == TT_TemplateCloser && Tok.is(tok::l_paren)) |
| 1073 | return false; |
| 1074 | if (Tok.is(tok::less) && Line.First.is(tok::hash)) |
| 1075 | return true; |
| 1076 | if (Tok.Type == TT_TrailingUnaryOperator) |
| 1077 | return false; |
Daniel Jasper | 8ff690a | 2013-02-06 14:22:40 +0000 | [diff] [blame] | 1078 | return spaceRequiredBetween(Line, *Tok.Parent, Tok); |
Daniel Jasper | 32d28ee | 2013-01-29 21:01:14 +0000 | [diff] [blame] | 1079 | } |
| 1080 | |
Daniel Jasper | 8ff690a | 2013-02-06 14:22:40 +0000 | [diff] [blame] | 1081 | bool TokenAnnotator::canBreakBefore(const AnnotatedLine &Line, |
| 1082 | const AnnotatedToken &Right) { |
Daniel Jasper | 32d28ee | 2013-01-29 21:01:14 +0000 | [diff] [blame] | 1083 | const AnnotatedToken &Left = *Right.Parent; |
Daniel Jasper | a03ab10 | 2013-02-13 20:33:44 +0000 | [diff] [blame] | 1084 | if (Right.Type == TT_StartOfName) |
Daniel Jasper | 32d28ee | 2013-01-29 21:01:14 +0000 | [diff] [blame] | 1085 | return true; |
| 1086 | if (Right.is(tok::colon) && Right.Type == TT_ObjCMethodExpr) |
| 1087 | return false; |
| 1088 | if (Left.is(tok::colon) && Left.Type == TT_ObjCMethodExpr) |
| 1089 | return true; |
Daniel Jasper | 63d7ced | 2013-02-05 10:07:47 +0000 | [diff] [blame] | 1090 | if (Right.Type == TT_ObjCSelectorName) |
Daniel Jasper | 32d28ee | 2013-01-29 21:01:14 +0000 | [diff] [blame] | 1091 | return true; |
| 1092 | if (Left.ClosesTemplateDeclaration) |
| 1093 | return true; |
| 1094 | if (Right.Type == TT_ConditionalExpr || Right.is(tok::question)) |
| 1095 | return true; |
Daniel Jasper | 6cabab4 | 2013-02-14 08:42:54 +0000 | [diff] [blame] | 1096 | if (Right.Type == TT_RangeBasedForLoopColon || |
| 1097 | Right.Type == TT_InheritanceColon) |
| 1098 | return false; |
| 1099 | if (Left.Type == TT_RangeBasedForLoopColon || |
| 1100 | Left.Type == TT_InheritanceColon) |
Daniel Jasper | 32d28ee | 2013-01-29 21:01:14 +0000 | [diff] [blame] | 1101 | return true; |
Daniel Jasper | 7d81281 | 2013-02-21 15:00:29 +0000 | [diff] [blame] | 1102 | if (Right.Type == TT_RangeBasedForLoopColon) |
| 1103 | return false; |
Daniel Jasper | 32d28ee | 2013-01-29 21:01:14 +0000 | [diff] [blame] | 1104 | if (Left.Type == TT_PointerOrReference || Left.Type == TT_TemplateCloser || |
| 1105 | Left.Type == TT_UnaryOperator || Left.Type == TT_ConditionalExpr || |
Daniel Jasper | dc2efa1 | 2013-02-15 19:24:08 +0000 | [diff] [blame] | 1106 | Left.is(tok::question) || Left.is(tok::kw_operator)) |
Daniel Jasper | 32d28ee | 2013-01-29 21:01:14 +0000 | [diff] [blame] | 1107 | return false; |
| 1108 | if (Left.is(tok::equal) && Line.Type == LT_VirtualFunctionDecl) |
| 1109 | return false; |
Daniel Jasper | 8ed4100 | 2013-02-28 14:44:25 +0000 | [diff] [blame] | 1110 | if (Left.is(tok::l_paren) && Right.is(tok::l_paren) && Left.Parent && |
| 1111 | Left.Parent->is(tok::kw___attribute)) |
| 1112 | return false; |
Daniel Jasper | 32d28ee | 2013-01-29 21:01:14 +0000 | [diff] [blame] | 1113 | |
| 1114 | if (Right.Type == TT_LineComment) |
| 1115 | // We rely on MustBreakBefore being set correctly here as we should not |
| 1116 | // change the "binding" behavior of a comment. |
| 1117 | return false; |
| 1118 | |
Daniel Jasper | 32d28ee | 2013-01-29 21:01:14 +0000 | [diff] [blame] | 1119 | // Allow breaking after a trailing 'const', e.g. after a method declaration, |
| 1120 | // unless it is follow by ';', '{' or '='. |
| 1121 | if (Left.is(tok::kw_const) && Left.Parent != NULL && |
| 1122 | Left.Parent->is(tok::r_paren)) |
| 1123 | return Right.isNot(tok::l_brace) && Right.isNot(tok::semi) && |
| 1124 | Right.isNot(tok::equal); |
| 1125 | |
| 1126 | // We only break before r_brace if there was a corresponding break before |
| 1127 | // the l_brace, which is tracked by BreakBeforeClosingBrace. |
| 1128 | if (Right.is(tok::r_brace)) |
| 1129 | return false; |
| 1130 | |
| 1131 | if (Right.is(tok::r_paren) || Right.is(tok::greater)) |
| 1132 | return false; |
Daniel Jasper | 3a20441 | 2013-02-23 07:46:38 +0000 | [diff] [blame] | 1133 | if (Left.is(tok::identifier) && Right.is(tok::string_literal)) |
| 1134 | return true; |
Daniel Jasper | 32d28ee | 2013-01-29 21:01:14 +0000 | [diff] [blame] | 1135 | return (isBinaryOperator(Left) && Left.isNot(tok::lessless)) || |
| 1136 | Left.is(tok::comma) || Right.is(tok::lessless) || |
| 1137 | Right.is(tok::arrow) || Right.is(tok::period) || |
| 1138 | Right.is(tok::colon) || Left.is(tok::coloncolon) || |
| 1139 | Left.is(tok::semi) || Left.is(tok::l_brace) || |
Daniel Jasper | 32d28ee | 2013-01-29 21:01:14 +0000 | [diff] [blame] | 1140 | (Left.is(tok::r_paren) && Left.Type != TT_CastRParen && |
Daniel Jasper | 8ed4100 | 2013-02-28 14:44:25 +0000 | [diff] [blame] | 1141 | (Right.is(tok::identifier) || Right.is(tok::kw___attribute))) || |
Daniel Jasper | 32d28ee | 2013-01-29 21:01:14 +0000 | [diff] [blame] | 1142 | (Left.is(tok::l_paren) && !Right.is(tok::r_paren)) || |
| 1143 | (Left.is(tok::l_square) && !Right.is(tok::r_square)); |
| 1144 | } |
| 1145 | |
| 1146 | } // namespace format |
| 1147 | } // namespace clang |