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