1 //===--- TokenAnnotator.h - Format C++ code ---------------------*- C++ -*-===//
3 // The LLVM Compiler Infrastructure
5 // This file is distributed under the University of Illinois Open Source
6 // License. See LICENSE.TXT for details.
8 //===----------------------------------------------------------------------===//
11 /// \brief This file implements a token annotator, i.e. creates
12 /// \c AnnotatedTokens out of \c FormatTokens with required extra information.
14 //===----------------------------------------------------------------------===//
16 #ifndef LLVM_CLANG_FORMAT_TOKEN_ANNOTATOR_H
17 #define LLVM_CLANG_FORMAT_TOKEN_ANNOTATOR_H
19 #include "UnwrappedLineParser.h"
20 #include "clang/Basic/OperatorPrecedence.h"
21 #include "clang/Format/Format.h"
35 TT_CtorInitializerColon,
36 TT_ImplicitStringLiteral,
39 TT_FunctionTypeLParen,
47 TT_ObjCMethodSpecifier,
50 TT_OverloadedOperator,
51 TT_OverloadedOperatorLParen,
52 TT_PointerOrReference,
53 TT_PureVirtualSpecifier,
54 TT_RangeBasedForLoopColon,
58 TT_TrailingUnaryOperator,
67 LT_PreprocessorDirective,
68 LT_VirtualFunctionDecl,
69 LT_ObjCDecl, // An @interface, @implementation, or @protocol line.
71 LT_ObjCProperty // An @property line.
74 class AnnotatedToken {
76 explicit AnnotatedToken(const FormatToken &FormatTok)
77 : FormatTok(FormatTok), Type(TT_Unknown), SpacesRequiredBefore(0),
78 CanBreakBefore(false), MustBreakBefore(false),
79 ClosesTemplateDeclaration(false), MatchingParen(NULL),
80 ParameterCount(0), TotalLength(FormatTok.TokenLength),
81 UnbreakableTailLength(0), BindingStrength(0), SplitPenalty(0),
82 LongestObjCSelectorName(0), Parent(NULL), FakeRParens(0),
83 LastInChainOfCalls(false), PartOfMultiVariableDeclStmt(false) {}
85 bool is(tok::TokenKind Kind) const { return FormatTok.Tok.is(Kind); }
87 bool isOneOf(tok::TokenKind K1, tok::TokenKind K2) const {
88 return is(K1) || is(K2);
91 bool isOneOf(tok::TokenKind K1, tok::TokenKind K2, tok::TokenKind K3) const {
92 return is(K1) || is(K2) || is(K3);
96 tok::TokenKind K1, tok::TokenKind K2, tok::TokenKind K3,
97 tok::TokenKind K4, tok::TokenKind K5 = tok::NUM_TOKENS,
98 tok::TokenKind K6 = tok::NUM_TOKENS, tok::TokenKind K7 = tok::NUM_TOKENS,
99 tok::TokenKind K8 = tok::NUM_TOKENS, tok::TokenKind K9 = tok::NUM_TOKENS,
100 tok::TokenKind K10 = tok::NUM_TOKENS,
101 tok::TokenKind K11 = tok::NUM_TOKENS,
102 tok::TokenKind K12 = tok::NUM_TOKENS) const {
103 return is(K1) || is(K2) || is(K3) || is(K4) || is(K5) || is(K6) || is(K7) ||
104 is(K8) || is(K9) || is(K10) || is(K11) || is(K12);
107 bool isNot(tok::TokenKind Kind) const { return FormatTok.Tok.isNot(Kind); }
109 bool isObjCAtKeyword(tok::ObjCKeywordKind Kind) const {
110 return FormatTok.Tok.isObjCAtKeyword(Kind);
113 bool isAccessSpecifier(bool ColonRequired = true) const {
114 return isOneOf(tok::kw_public, tok::kw_protected, tok::kw_private) &&
116 (!Children.empty() && Children[0].is(tok::colon)));
119 bool isObjCAccessSpecifier() const {
120 return is(tok::at) && !Children.empty() &&
121 (Children[0].isObjCAtKeyword(tok::objc_public) ||
122 Children[0].isObjCAtKeyword(tok::objc_protected) ||
123 Children[0].isObjCAtKeyword(tok::objc_package) ||
124 Children[0].isObjCAtKeyword(tok::objc_private));
127 /// \brief Returns whether \p Tok is ([{ or a template opening <.
128 bool opensScope() const;
129 /// \brief Returns whether \p Tok is )]} or a template opening >.
130 bool closesScope() const;
132 bool isUnaryOperator() const;
133 bool isBinaryOperator() const;
134 bool isTrailingComment() const;
136 FormatToken FormatTok;
140 unsigned SpacesRequiredBefore;
142 bool MustBreakBefore;
144 bool ClosesTemplateDeclaration;
146 AnnotatedToken *MatchingParen;
148 /// \brief Number of parameters, if this is "(", "[" or "<".
150 /// This is initialized to 1 as we don't need to distinguish functions with
151 /// 0 parameters from functions with 1 parameter. Thus, we can simply count
152 /// the number of commas.
153 unsigned ParameterCount;
155 /// \brief The total length of the line up to and including this token.
156 unsigned TotalLength;
158 /// \brief The length of following tokens until the next natural split point,
159 /// or the next token that can be broken.
160 unsigned UnbreakableTailLength;
162 // FIXME: Come up with a 'cleaner' concept.
163 /// \brief The binding strength of a token. This is a combined value of
164 /// operator precedence, parenthesis nesting, etc.
165 unsigned BindingStrength;
167 /// \brief Penalty for inserting a line break before this token.
168 unsigned SplitPenalty;
170 /// \brief If this is the first ObjC selector name in an ObjC method
171 /// definition or call, this contains the length of the longest name.
172 unsigned LongestObjCSelectorName;
174 std::vector<AnnotatedToken> Children;
175 AnnotatedToken *Parent;
177 /// \brief Stores the number of required fake parentheses and the
178 /// corresponding operator precedence.
180 /// If multiple fake parentheses start at a token, this vector stores them in
181 /// reverse order, i.e. inner fake parenthesis first.
182 SmallVector<prec::Level, 4> FakeLParens;
183 /// \brief Insert this many fake ) after this token for correct indentation.
184 unsigned FakeRParens;
186 /// \brief Is this the last "." or "->" in a builder-type call?
187 bool LastInChainOfCalls;
189 /// \brief Is this token part of a \c DeclStmt defining multiple variables?
191 /// Only set if \c Type == \c TT_StartOfName.
192 bool PartOfMultiVariableDeclStmt;
194 /// \brief Returns the previous token ignoring comments.
195 AnnotatedToken *getPreviousNoneComment() const;
197 /// \brief Returns the next token ignoring comments.
198 const AnnotatedToken *getNextNoneComment() const;
201 class AnnotatedLine {
203 AnnotatedLine(const UnwrappedLine &Line)
204 : First(Line.Tokens.front()), Level(Line.Level),
205 InPPDirective(Line.InPPDirective),
206 MustBeDeclaration(Line.MustBeDeclaration), MightBeFunctionDecl(false),
207 StartsDefinition(false) {
208 assert(!Line.Tokens.empty());
209 AnnotatedToken *Current = &First;
210 for (std::list<FormatToken>::const_iterator I = ++Line.Tokens.begin(),
211 E = Line.Tokens.end();
213 Current->Children.push_back(AnnotatedToken(*I));
214 Current->Children[0].Parent = Current;
215 Current = &Current->Children[0];
219 AnnotatedLine(const AnnotatedLine &Other)
220 : First(Other.First), Type(Other.Type), Level(Other.Level),
221 InPPDirective(Other.InPPDirective),
222 MustBeDeclaration(Other.MustBeDeclaration),
223 MightBeFunctionDecl(Other.MightBeFunctionDecl),
224 StartsDefinition(Other.StartsDefinition) {
226 while (!Last->Children.empty()) {
227 Last->Children[0].Parent = Last;
228 Last = &Last->Children[0];
232 AnnotatedToken First;
233 AnnotatedToken *Last;
238 bool MustBeDeclaration;
239 bool MightBeFunctionDecl;
240 bool StartsDefinition;
243 inline prec::Level getPrecedence(const AnnotatedToken &Tok) {
244 return getBinOpPrecedence(Tok.FormatTok.Tok.getKind(), true, true);
247 /// \brief Determines extra information about the tokens comprising an
248 /// \c UnwrappedLine.
249 class TokenAnnotator {
251 TokenAnnotator(const FormatStyle &Style, SourceManager &SourceMgr, Lexer &Lex,
252 IdentifierInfo &Ident_in)
253 : Style(Style), SourceMgr(SourceMgr), Lex(Lex), Ident_in(Ident_in) {
256 void annotate(AnnotatedLine &Line);
257 void calculateFormattingInformation(AnnotatedLine &Line);
260 /// \brief Calculate the penalty for splitting before \c Tok.
261 unsigned splitPenalty(const AnnotatedLine &Line, const AnnotatedToken &Tok);
263 bool spaceRequiredBetween(const AnnotatedLine &Line,
264 const AnnotatedToken &Left,
265 const AnnotatedToken &Right);
267 bool spaceRequiredBefore(const AnnotatedLine &Line,
268 const AnnotatedToken &Tok);
270 bool canBreakBefore(const AnnotatedLine &Line, const AnnotatedToken &Right);
272 void printDebugInfo(const AnnotatedLine &Line);
274 void calculateUnbreakableTailLengths(AnnotatedLine &Line);
276 const FormatStyle &Style;
277 SourceManager &SourceMgr;
280 // Contextual keywords:
281 IdentifierInfo &Ident_in;
284 } // end namespace format
285 } // end namespace clang
287 #endif // LLVM_CLANG_FORMAT_TOKEN_ANNOTATOR_H