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,
45 TT_ObjCMethodSpecifier,
48 TT_OverloadedOperatorLParen,
49 TT_PointerOrReference,
50 TT_PureVirtualSpecifier,
51 TT_RangeBasedForLoopColon,
55 TT_TrailingUnaryOperator,
64 LT_PreprocessorDirective,
65 LT_VirtualFunctionDecl,
66 LT_ObjCDecl, // An @interface, @implementation, or @protocol line.
68 LT_ObjCProperty // An @property line.
71 class AnnotatedToken {
73 explicit AnnotatedToken(const FormatToken &FormatTok)
74 : FormatTok(FormatTok), Type(TT_Unknown), SpacesRequiredBefore(0),
75 CanBreakBefore(false), MustBreakBefore(false),
76 ClosesTemplateDeclaration(false), MatchingParen(NULL),
77 ParameterCount(0), BindingStrength(0), SplitPenalty(0),
78 LongestObjCSelectorName(0), Parent(NULL),
79 FakeRParens(0), LastInChainOfCalls(false),
80 PartOfMultiVariableDeclStmt(false) {}
82 bool is(tok::TokenKind Kind) const { return FormatTok.Tok.is(Kind); }
84 bool isOneOf(tok::TokenKind K1, tok::TokenKind K2) const {
85 return is(K1) || is(K2);
88 bool isOneOf(tok::TokenKind K1, tok::TokenKind K2, tok::TokenKind K3) const {
89 return is(K1) || is(K2) || is(K3);
93 tok::TokenKind K1, tok::TokenKind K2, tok::TokenKind K3,
94 tok::TokenKind K4, tok::TokenKind K5 = tok::NUM_TOKENS,
95 tok::TokenKind K6 = tok::NUM_TOKENS, tok::TokenKind K7 = tok::NUM_TOKENS,
96 tok::TokenKind K8 = tok::NUM_TOKENS, tok::TokenKind K9 = tok::NUM_TOKENS,
97 tok::TokenKind K10 = tok::NUM_TOKENS,
98 tok::TokenKind K11 = tok::NUM_TOKENS,
99 tok::TokenKind K12 = tok::NUM_TOKENS) const {
100 return is(K1) || is(K2) || is(K3) || is(K4) || is(K5) || is(K6) || is(K7) ||
101 is(K8) || is(K9) || is(K10) || is(K11) || is(K12);
104 bool isNot(tok::TokenKind Kind) const { return FormatTok.Tok.isNot(Kind); }
106 bool isObjCAtKeyword(tok::ObjCKeywordKind Kind) const {
107 return FormatTok.Tok.isObjCAtKeyword(Kind);
110 bool isAccessSpecifier(bool ColonRequired = true) const {
111 return isOneOf(tok::kw_public, tok::kw_protected, tok::kw_private) &&
113 (!Children.empty() && Children[0].is(tok::colon)));
116 bool isObjCAccessSpecifier() const {
117 return is(tok::at) && !Children.empty() &&
118 (Children[0].isObjCAtKeyword(tok::objc_public) ||
119 Children[0].isObjCAtKeyword(tok::objc_protected) ||
120 Children[0].isObjCAtKeyword(tok::objc_package) ||
121 Children[0].isObjCAtKeyword(tok::objc_private));
124 FormatToken FormatTok;
128 unsigned SpacesRequiredBefore;
130 bool MustBreakBefore;
132 bool ClosesTemplateDeclaration;
134 AnnotatedToken *MatchingParen;
136 /// \brief Number of parameters, if this is "(", "[" or "<".
138 /// This is initialized to 1 as we don't need to distinguish functions with
139 /// 0 parameters from functions with 1 parameter. Thus, we can simply count
140 /// the number of commas.
141 unsigned ParameterCount;
143 /// \brief The total length of the line up to and including this token.
144 unsigned TotalLength;
146 // FIXME: Come up with a 'cleaner' concept.
147 /// \brief The binding strength of a token. This is a combined value of
148 /// operator precedence, parenthesis nesting, etc.
149 unsigned BindingStrength;
151 /// \brief Penalty for inserting a line break before this token.
152 unsigned SplitPenalty;
154 /// \brief If this is the first ObjC selector name in an ObjC method
155 /// definition or call, this contains the length of the longest name.
156 unsigned LongestObjCSelectorName;
158 std::vector<AnnotatedToken> Children;
159 AnnotatedToken *Parent;
161 /// \brief Stores the number of required fake parentheses and the
162 /// corresponding operator precedence.
164 /// If multiple fake parentheses start at a token, this vector stores them in
165 /// reverse order, i.e. inner fake parenthesis first.
166 SmallVector<prec::Level, 4> FakeLParens;
167 /// \brief Insert this many fake ) after this token for correct indentation.
168 unsigned FakeRParens;
170 /// \brief Is this the last "." or "->" in a builder-type call?
171 bool LastInChainOfCalls;
173 /// \brief Is this token part of a \c DeclStmt defining multiple variables?
175 /// Only set if \c Type == \c TT_StartOfName.
176 bool PartOfMultiVariableDeclStmt;
178 const AnnotatedToken *getPreviousNoneComment() const {
179 AnnotatedToken *Tok = Parent;
180 while (Tok != NULL && Tok->is(tok::comment))
186 class AnnotatedLine {
188 AnnotatedLine(const UnwrappedLine &Line)
189 : First(Line.Tokens.front()), Level(Line.Level),
190 InPPDirective(Line.InPPDirective),
191 MustBeDeclaration(Line.MustBeDeclaration),
192 MightBeFunctionDecl(false) {
193 assert(!Line.Tokens.empty());
194 AnnotatedToken *Current = &First;
195 for (std::list<FormatToken>::const_iterator I = ++Line.Tokens.begin(),
196 E = Line.Tokens.end();
198 Current->Children.push_back(AnnotatedToken(*I));
199 Current->Children[0].Parent = Current;
200 Current = &Current->Children[0];
204 AnnotatedLine(const AnnotatedLine &Other)
205 : First(Other.First), Type(Other.Type), Level(Other.Level),
206 InPPDirective(Other.InPPDirective),
207 MustBeDeclaration(Other.MustBeDeclaration),
208 MightBeFunctionDecl(Other.MightBeFunctionDecl) {
210 while (!Last->Children.empty()) {
211 Last->Children[0].Parent = Last;
212 Last = &Last->Children[0];
216 AnnotatedToken First;
217 AnnotatedToken *Last;
222 bool MustBeDeclaration;
223 bool MightBeFunctionDecl;
226 inline prec::Level getPrecedence(const AnnotatedToken &Tok) {
227 return getBinOpPrecedence(Tok.FormatTok.Tok.getKind(), true, true);
230 /// \brief Returns whether \p Tok is ([{ or a template opening <.
231 bool opensScope(const AnnotatedToken &Tok);
232 /// \brief Returns whether \p Tok is )]} or a template opening >.
233 bool closesScope(const AnnotatedToken &Tok);
235 /// \brief Determines extra information about the tokens comprising an
236 /// \c UnwrappedLine.
237 class TokenAnnotator {
239 TokenAnnotator(const FormatStyle &Style, SourceManager &SourceMgr, Lexer &Lex,
240 IdentifierInfo &Ident_in)
241 : Style(Style), SourceMgr(SourceMgr), Lex(Lex), Ident_in(Ident_in) {
244 void annotate(AnnotatedLine &Line);
245 void calculateFormattingInformation(AnnotatedLine &Line);
248 /// \brief Calculate the penalty for splitting before \c Tok.
249 unsigned splitPenalty(const AnnotatedLine &Line, const AnnotatedToken &Tok);
251 bool spaceRequiredBetween(const AnnotatedLine &Line,
252 const AnnotatedToken &Left,
253 const AnnotatedToken &Right);
255 bool spaceRequiredBefore(const AnnotatedLine &Line,
256 const AnnotatedToken &Tok);
258 bool canBreakBefore(const AnnotatedLine &Line, const AnnotatedToken &Right);
260 void printDebugInfo(const AnnotatedLine &Line);
262 const FormatStyle &Style;
263 SourceManager &SourceMgr;
266 // Contextual keywords:
267 IdentifierInfo &Ident_in;
270 } // end namespace format
271 } // end namespace clang
273 #endif // LLVM_CLANG_FORMAT_TOKEN_ANNOTATOR_H