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