Daniel Jasper | 32d28ee | 2013-01-29 21:01:14 +0000 | [diff] [blame] | 1 | //===--- TokenAnnotator.h - Format C++ code ---------------------*- C++ -*-===// |
| 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 | #ifndef LLVM_CLANG_FORMAT_TOKEN_ANNOTATOR_H |
| 17 | #define LLVM_CLANG_FORMAT_TOKEN_ANNOTATOR_H |
| 18 | |
| 19 | #include "UnwrappedLineParser.h" |
| 20 | #include "clang/Basic/OperatorPrecedence.h" |
| 21 | #include "clang/Format/Format.h" |
| 22 | #include <string> |
| 23 | |
| 24 | namespace clang { |
| 25 | class Lexer; |
| 26 | class SourceManager; |
| 27 | |
| 28 | namespace format { |
| 29 | |
| 30 | enum TokenType { |
| 31 | TT_BinaryOperator, |
| 32 | TT_BlockComment, |
| 33 | TT_CastRParen, |
| 34 | TT_ConditionalExpr, |
| 35 | TT_CtorInitializerColon, |
| 36 | TT_ImplicitStringLiteral, |
Daniel Jasper | 923ebef | 2013-03-14 13:45:21 +0000 | [diff] [blame] | 37 | TT_InlineASMColon, |
Daniel Jasper | 6cabab4 | 2013-02-14 08:42:54 +0000 | [diff] [blame] | 38 | TT_InheritanceColon, |
Daniel Jasper | 32d28ee | 2013-01-29 21:01:14 +0000 | [diff] [blame] | 39 | TT_LineComment, |
Nico Weber | 051860e | 2013-02-10 02:08:05 +0000 | [diff] [blame] | 40 | TT_ObjCArrayLiteral, |
Daniel Jasper | 32d28ee | 2013-01-29 21:01:14 +0000 | [diff] [blame] | 41 | TT_ObjCBlockLParen, |
| 42 | TT_ObjCDecl, |
Nico Weber | c2e6d2a | 2013-02-11 15:32:15 +0000 | [diff] [blame] | 43 | TT_ObjCForIn, |
Daniel Jasper | 32d28ee | 2013-01-29 21:01:14 +0000 | [diff] [blame] | 44 | TT_ObjCMethodExpr, |
Nico Weber | 051860e | 2013-02-10 02:08:05 +0000 | [diff] [blame] | 45 | TT_ObjCMethodSpecifier, |
Daniel Jasper | 32d28ee | 2013-01-29 21:01:14 +0000 | [diff] [blame] | 46 | TT_ObjCProperty, |
Daniel Jasper | 63d7ced | 2013-02-05 10:07:47 +0000 | [diff] [blame] | 47 | TT_ObjCSelectorName, |
Daniel Jasper | 6ea933c | 2013-05-10 07:59:58 +0000 | [diff] [blame^] | 48 | TT_OverloadedOperator, |
Daniel Jasper | 2b4c924 | 2013-02-11 08:01:18 +0000 | [diff] [blame] | 49 | TT_OverloadedOperatorLParen, |
Daniel Jasper | 32d28ee | 2013-01-29 21:01:14 +0000 | [diff] [blame] | 50 | TT_PointerOrReference, |
| 51 | TT_PureVirtualSpecifier, |
| 52 | TT_RangeBasedForLoopColon, |
| 53 | TT_StartOfName, |
| 54 | TT_TemplateCloser, |
| 55 | TT_TemplateOpener, |
| 56 | TT_TrailingUnaryOperator, |
| 57 | TT_UnaryOperator, |
| 58 | TT_Unknown |
| 59 | }; |
| 60 | |
| 61 | enum LineType { |
| 62 | LT_Invalid, |
| 63 | LT_Other, |
| 64 | LT_BuilderTypeCall, |
| 65 | LT_PreprocessorDirective, |
| 66 | LT_VirtualFunctionDecl, |
| 67 | LT_ObjCDecl, // An @interface, @implementation, or @protocol line. |
| 68 | LT_ObjCMethodDecl, |
| 69 | LT_ObjCProperty // An @property line. |
| 70 | }; |
| 71 | |
| 72 | class AnnotatedToken { |
| 73 | public: |
| 74 | explicit AnnotatedToken(const FormatToken &FormatTok) |
Daniel Jasper | 729a743 | 2013-02-11 12:36:37 +0000 | [diff] [blame] | 75 | : FormatTok(FormatTok), Type(TT_Unknown), SpacesRequiredBefore(0), |
Daniel Jasper | 32d28ee | 2013-01-29 21:01:14 +0000 | [diff] [blame] | 76 | CanBreakBefore(false), MustBreakBefore(false), |
| 77 | ClosesTemplateDeclaration(false), MatchingParen(NULL), |
Daniel Jasper | 9fc56f2 | 2013-02-14 15:01:34 +0000 | [diff] [blame] | 78 | ParameterCount(0), BindingStrength(0), SplitPenalty(0), |
Daniel Jasper | 395228f | 2013-05-08 14:58:20 +0000 | [diff] [blame] | 79 | LongestObjCSelectorName(0), DefinesFunctionType(false), Parent(NULL), |
Daniel Jasper | 8ed9f2b | 2013-04-03 13:36:17 +0000 | [diff] [blame] | 80 | FakeRParens(0), LastInChainOfCalls(false), |
Daniel Jasper | fca24bc | 2013-04-25 13:31:51 +0000 | [diff] [blame] | 81 | PartOfMultiVariableDeclStmt(false), NoMoreTokensOnLevel(false) {} |
Daniel Jasper | 32d28ee | 2013-01-29 21:01:14 +0000 | [diff] [blame] | 82 | |
| 83 | bool is(tok::TokenKind Kind) const { return FormatTok.Tok.is(Kind); } |
Alexander Kornienko | e74de28 | 2013-03-13 14:41:29 +0000 | [diff] [blame] | 84 | |
| 85 | bool isOneOf(tok::TokenKind K1, tok::TokenKind K2) const { |
| 86 | return is(K1) || is(K2); |
| 87 | } |
| 88 | |
| 89 | bool isOneOf(tok::TokenKind K1, tok::TokenKind K2, tok::TokenKind K3) const { |
| 90 | return is(K1) || is(K2) || is(K3); |
| 91 | } |
| 92 | |
| 93 | bool isOneOf( |
| 94 | tok::TokenKind K1, tok::TokenKind K2, tok::TokenKind K3, |
| 95 | tok::TokenKind K4, tok::TokenKind K5 = tok::NUM_TOKENS, |
| 96 | tok::TokenKind K6 = tok::NUM_TOKENS, tok::TokenKind K7 = tok::NUM_TOKENS, |
| 97 | tok::TokenKind K8 = tok::NUM_TOKENS, tok::TokenKind K9 = tok::NUM_TOKENS, |
| 98 | tok::TokenKind K10 = tok::NUM_TOKENS, |
| 99 | tok::TokenKind K11 = tok::NUM_TOKENS, |
| 100 | tok::TokenKind K12 = tok::NUM_TOKENS) const { |
| 101 | return is(K1) || is(K2) || is(K3) || is(K4) || is(K5) || is(K6) || is(K7) || |
| 102 | is(K8) || is(K9) || is(K10) || is(K11) || is(K12); |
| 103 | } |
| 104 | |
Daniel Jasper | 32d28ee | 2013-01-29 21:01:14 +0000 | [diff] [blame] | 105 | bool isNot(tok::TokenKind Kind) const { return FormatTok.Tok.isNot(Kind); } |
| 106 | |
| 107 | bool isObjCAtKeyword(tok::ObjCKeywordKind Kind) const { |
| 108 | return FormatTok.Tok.isObjCAtKeyword(Kind); |
| 109 | } |
| 110 | |
Alexander Kornienko | 94b748f | 2013-03-27 17:08:02 +0000 | [diff] [blame] | 111 | bool isAccessSpecifier(bool ColonRequired = true) const { |
| 112 | return isOneOf(tok::kw_public, tok::kw_protected, tok::kw_private) && |
| 113 | (!ColonRequired || |
| 114 | (!Children.empty() && Children[0].is(tok::colon))); |
| 115 | } |
| 116 | |
| 117 | bool isObjCAccessSpecifier() const { |
| 118 | return is(tok::at) && !Children.empty() && |
| 119 | (Children[0].isObjCAtKeyword(tok::objc_public) || |
| 120 | Children[0].isObjCAtKeyword(tok::objc_protected) || |
| 121 | Children[0].isObjCAtKeyword(tok::objc_package) || |
| 122 | Children[0].isObjCAtKeyword(tok::objc_private)); |
| 123 | } |
| 124 | |
Daniel Jasper | ac3223e | 2013-04-10 09:49:49 +0000 | [diff] [blame] | 125 | /// \brief Returns whether \p Tok is ([{ or a template opening <. |
| 126 | bool opensScope() const; |
| 127 | /// \brief Returns whether \p Tok is )]} or a template opening >. |
| 128 | bool closesScope() const; |
| 129 | |
| 130 | bool isUnaryOperator() const; |
| 131 | bool isBinaryOperator() const; |
| 132 | bool isTrailingComment() const; |
| 133 | |
Daniel Jasper | 32d28ee | 2013-01-29 21:01:14 +0000 | [diff] [blame] | 134 | FormatToken FormatTok; |
| 135 | |
| 136 | TokenType Type; |
| 137 | |
Daniel Jasper | 729a743 | 2013-02-11 12:36:37 +0000 | [diff] [blame] | 138 | unsigned SpacesRequiredBefore; |
Daniel Jasper | 32d28ee | 2013-01-29 21:01:14 +0000 | [diff] [blame] | 139 | bool CanBreakBefore; |
| 140 | bool MustBreakBefore; |
| 141 | |
| 142 | bool ClosesTemplateDeclaration; |
| 143 | |
| 144 | AnnotatedToken *MatchingParen; |
| 145 | |
| 146 | /// \brief Number of parameters, if this is "(", "[" or "<". |
| 147 | /// |
| 148 | /// This is initialized to 1 as we don't need to distinguish functions with |
| 149 | /// 0 parameters from functions with 1 parameter. Thus, we can simply count |
| 150 | /// the number of commas. |
| 151 | unsigned ParameterCount; |
| 152 | |
| 153 | /// \brief The total length of the line up to and including this token. |
| 154 | unsigned TotalLength; |
| 155 | |
Daniel Jasper | 0178673 | 2013-02-04 07:21:18 +0000 | [diff] [blame] | 156 | // FIXME: Come up with a 'cleaner' concept. |
| 157 | /// \brief The binding strength of a token. This is a combined value of |
| 158 | /// operator precedence, parenthesis nesting, etc. |
| 159 | unsigned BindingStrength; |
| 160 | |
Daniel Jasper | 32d28ee | 2013-01-29 21:01:14 +0000 | [diff] [blame] | 161 | /// \brief Penalty for inserting a line break before this token. |
| 162 | unsigned SplitPenalty; |
| 163 | |
Daniel Jasper | 63d7ced | 2013-02-05 10:07:47 +0000 | [diff] [blame] | 164 | /// \brief If this is the first ObjC selector name in an ObjC method |
| 165 | /// definition or call, this contains the length of the longest name. |
| 166 | unsigned LongestObjCSelectorName; |
| 167 | |
Daniel Jasper | 395228f | 2013-05-08 14:58:20 +0000 | [diff] [blame] | 168 | /// \brief \c true if this is a "(" that starts a function type definition. |
| 169 | bool DefinesFunctionType; |
| 170 | |
Daniel Jasper | 32d28ee | 2013-01-29 21:01:14 +0000 | [diff] [blame] | 171 | std::vector<AnnotatedToken> Children; |
| 172 | AnnotatedToken *Parent; |
| 173 | |
Daniel Jasper | bf71ba2 | 2013-04-08 20:33:42 +0000 | [diff] [blame] | 174 | /// \brief Stores the number of required fake parentheses and the |
| 175 | /// corresponding operator precedence. |
| 176 | /// |
| 177 | /// If multiple fake parentheses start at a token, this vector stores them in |
| 178 | /// reverse order, i.e. inner fake parenthesis first. |
| 179 | SmallVector<prec::Level, 4> FakeLParens; |
Daniel Jasper | 087387a | 2013-02-08 16:49:27 +0000 | [diff] [blame] | 180 | /// \brief Insert this many fake ) after this token for correct indentation. |
Daniel Jasper | 29f123b | 2013-02-08 15:28:42 +0000 | [diff] [blame] | 181 | unsigned FakeRParens; |
| 182 | |
Daniel Jasper | 2484971 | 2013-03-01 16:48:32 +0000 | [diff] [blame] | 183 | /// \brief Is this the last "." or "->" in a builder-type call? |
| 184 | bool LastInChainOfCalls; |
| 185 | |
Daniel Jasper | 8ed9f2b | 2013-04-03 13:36:17 +0000 | [diff] [blame] | 186 | /// \brief Is this token part of a \c DeclStmt defining multiple variables? |
| 187 | /// |
| 188 | /// Only set if \c Type == \c TT_StartOfName. |
| 189 | bool PartOfMultiVariableDeclStmt; |
| 190 | |
Daniel Jasper | fca24bc | 2013-04-25 13:31:51 +0000 | [diff] [blame] | 191 | /// \brief Set to \c true for "("-tokens if this is the last token other than |
| 192 | /// ")" in the next higher parenthesis level. |
| 193 | /// |
| 194 | /// If this is \c true, no more formatting decisions have to be made on the |
| 195 | /// next higher parenthesis level, enabling optimizations. |
| 196 | /// |
| 197 | /// Example: |
| 198 | /// \code |
| 199 | /// aaaaaa(aaaaaa()); |
| 200 | /// ^ // Set to true for this parenthesis. |
| 201 | /// \endcode |
| 202 | bool NoMoreTokensOnLevel; |
| 203 | |
Daniel Jasper | ac3223e | 2013-04-10 09:49:49 +0000 | [diff] [blame] | 204 | /// \brief Returns the previous token ignoring comments. |
| 205 | AnnotatedToken *getPreviousNoneComment() const; |
| 206 | |
| 207 | /// \brief Returns the next token ignoring comments. |
| 208 | const AnnotatedToken *getNextNoneComment() const; |
Daniel Jasper | 32d28ee | 2013-01-29 21:01:14 +0000 | [diff] [blame] | 209 | }; |
| 210 | |
| 211 | class AnnotatedLine { |
| 212 | public: |
| 213 | AnnotatedLine(const UnwrappedLine &Line) |
| 214 | : First(Line.Tokens.front()), Level(Line.Level), |
| 215 | InPPDirective(Line.InPPDirective), |
Daniel Jasper | 53e72cd | 2013-05-06 08:27:33 +0000 | [diff] [blame] | 216 | MustBeDeclaration(Line.MustBeDeclaration), MightBeFunctionDecl(false), |
| 217 | StartsDefinition(false) { |
Daniel Jasper | 32d28ee | 2013-01-29 21:01:14 +0000 | [diff] [blame] | 218 | assert(!Line.Tokens.empty()); |
| 219 | AnnotatedToken *Current = &First; |
| 220 | for (std::list<FormatToken>::const_iterator I = ++Line.Tokens.begin(), |
| 221 | E = Line.Tokens.end(); |
| 222 | I != E; ++I) { |
| 223 | Current->Children.push_back(AnnotatedToken(*I)); |
| 224 | Current->Children[0].Parent = Current; |
| 225 | Current = &Current->Children[0]; |
| 226 | } |
| 227 | Last = Current; |
| 228 | } |
| 229 | AnnotatedLine(const AnnotatedLine &Other) |
| 230 | : First(Other.First), Type(Other.Type), Level(Other.Level), |
| 231 | InPPDirective(Other.InPPDirective), |
Daniel Jasper | 3c08a81 | 2013-02-24 18:54:32 +0000 | [diff] [blame] | 232 | MustBeDeclaration(Other.MustBeDeclaration), |
Daniel Jasper | 53e72cd | 2013-05-06 08:27:33 +0000 | [diff] [blame] | 233 | MightBeFunctionDecl(Other.MightBeFunctionDecl), |
| 234 | StartsDefinition(Other.StartsDefinition) { |
Daniel Jasper | 32d28ee | 2013-01-29 21:01:14 +0000 | [diff] [blame] | 235 | Last = &First; |
| 236 | while (!Last->Children.empty()) { |
| 237 | Last->Children[0].Parent = Last; |
| 238 | Last = &Last->Children[0]; |
| 239 | } |
| 240 | } |
| 241 | |
| 242 | AnnotatedToken First; |
| 243 | AnnotatedToken *Last; |
| 244 | |
| 245 | LineType Type; |
| 246 | unsigned Level; |
| 247 | bool InPPDirective; |
| 248 | bool MustBeDeclaration; |
Daniel Jasper | 3c08a81 | 2013-02-24 18:54:32 +0000 | [diff] [blame] | 249 | bool MightBeFunctionDecl; |
Daniel Jasper | 53e72cd | 2013-05-06 08:27:33 +0000 | [diff] [blame] | 250 | bool StartsDefinition; |
Daniel Jasper | 32d28ee | 2013-01-29 21:01:14 +0000 | [diff] [blame] | 251 | }; |
| 252 | |
| 253 | inline prec::Level getPrecedence(const AnnotatedToken &Tok) { |
| 254 | return getBinOpPrecedence(Tok.FormatTok.Tok.getKind(), true, true); |
| 255 | } |
| 256 | |
| 257 | /// \brief Determines extra information about the tokens comprising an |
| 258 | /// \c UnwrappedLine. |
| 259 | class TokenAnnotator { |
| 260 | public: |
Nico Weber | c2e6d2a | 2013-02-11 15:32:15 +0000 | [diff] [blame] | 261 | TokenAnnotator(const FormatStyle &Style, SourceManager &SourceMgr, Lexer &Lex, |
| 262 | IdentifierInfo &Ident_in) |
| 263 | : Style(Style), SourceMgr(SourceMgr), Lex(Lex), Ident_in(Ident_in) { |
Daniel Jasper | 32d28ee | 2013-01-29 21:01:14 +0000 | [diff] [blame] | 264 | } |
| 265 | |
Daniel Jasper | 8ff690a | 2013-02-06 14:22:40 +0000 | [diff] [blame] | 266 | void annotate(AnnotatedLine &Line); |
| 267 | void calculateFormattingInformation(AnnotatedLine &Line); |
Daniel Jasper | 32d28ee | 2013-01-29 21:01:14 +0000 | [diff] [blame] | 268 | |
| 269 | private: |
| 270 | /// \brief Calculate the penalty for splitting before \c Tok. |
Daniel Jasper | 8ff690a | 2013-02-06 14:22:40 +0000 | [diff] [blame] | 271 | unsigned splitPenalty(const AnnotatedLine &Line, const AnnotatedToken &Tok); |
Daniel Jasper | 32d28ee | 2013-01-29 21:01:14 +0000 | [diff] [blame] | 272 | |
Daniel Jasper | 8ff690a | 2013-02-06 14:22:40 +0000 | [diff] [blame] | 273 | bool spaceRequiredBetween(const AnnotatedLine &Line, |
| 274 | const AnnotatedToken &Left, |
Daniel Jasper | 32d28ee | 2013-01-29 21:01:14 +0000 | [diff] [blame] | 275 | const AnnotatedToken &Right); |
| 276 | |
Daniel Jasper | 8ff690a | 2013-02-06 14:22:40 +0000 | [diff] [blame] | 277 | bool spaceRequiredBefore(const AnnotatedLine &Line, |
| 278 | const AnnotatedToken &Tok); |
Daniel Jasper | 32d28ee | 2013-01-29 21:01:14 +0000 | [diff] [blame] | 279 | |
Daniel Jasper | 8ff690a | 2013-02-06 14:22:40 +0000 | [diff] [blame] | 280 | bool canBreakBefore(const AnnotatedLine &Line, const AnnotatedToken &Right); |
Daniel Jasper | 32d28ee | 2013-01-29 21:01:14 +0000 | [diff] [blame] | 281 | |
Daniel Jasper | bf71ba2 | 2013-04-08 20:33:42 +0000 | [diff] [blame] | 282 | void printDebugInfo(const AnnotatedLine &Line); |
| 283 | |
Daniel Jasper | 8ff690a | 2013-02-06 14:22:40 +0000 | [diff] [blame] | 284 | const FormatStyle &Style; |
Daniel Jasper | 32d28ee | 2013-01-29 21:01:14 +0000 | [diff] [blame] | 285 | SourceManager &SourceMgr; |
| 286 | Lexer &Lex; |
Nico Weber | c2e6d2a | 2013-02-11 15:32:15 +0000 | [diff] [blame] | 287 | |
| 288 | // Contextual keywords: |
| 289 | IdentifierInfo &Ident_in; |
Daniel Jasper | 32d28ee | 2013-01-29 21:01:14 +0000 | [diff] [blame] | 290 | }; |
| 291 | |
Daniel Jasper | 32d28ee | 2013-01-29 21:01:14 +0000 | [diff] [blame] | 292 | } // end namespace format |
| 293 | } // end namespace clang |
| 294 | |
| 295 | #endif // LLVM_CLANG_FORMAT_TOKEN_ANNOTATOR_H |