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