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