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