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