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,
43 TT_ObjCMethodSpecifier,
46 TT_OverloadedOperatorLParen,
47 TT_PointerOrReference,
48 TT_PureVirtualSpecifier,
49 TT_RangeBasedForLoopColon,
53 TT_TrailingUnaryOperator,
62 LT_PreprocessorDirective,
63 LT_VirtualFunctionDecl,
64 LT_ObjCDecl, // An @interface, @implementation, or @protocol line.
66 LT_ObjCProperty // An @property line.
69 class AnnotatedToken {
71 explicit AnnotatedToken(const FormatToken &FormatTok)
72 : FormatTok(FormatTok), Type(TT_Unknown), SpacesRequiredBefore(0),
73 CanBreakBefore(false), MustBreakBefore(false),
74 ClosesTemplateDeclaration(false), MatchingParen(NULL),
75 ParameterCount(1), BindingStrength(0), SplitPenalty(0),
76 LongestObjCSelectorName(0), Parent(NULL), FakeLParens(0),
80 bool is(tok::TokenKind Kind) const { return FormatTok.Tok.is(Kind); }
81 bool isNot(tok::TokenKind Kind) const { return FormatTok.Tok.isNot(Kind); }
83 bool isObjCAtKeyword(tok::ObjCKeywordKind Kind) const {
84 return FormatTok.Tok.isObjCAtKeyword(Kind);
87 FormatToken FormatTok;
91 unsigned SpacesRequiredBefore;
95 bool ClosesTemplateDeclaration;
97 AnnotatedToken *MatchingParen;
99 /// \brief Number of parameters, if this is "(", "[" or "<".
101 /// This is initialized to 1 as we don't need to distinguish functions with
102 /// 0 parameters from functions with 1 parameter. Thus, we can simply count
103 /// the number of commas.
104 unsigned ParameterCount;
106 /// \brief The total length of the line up to and including this token.
107 unsigned TotalLength;
109 // FIXME: Come up with a 'cleaner' concept.
110 /// \brief The binding strength of a token. This is a combined value of
111 /// operator precedence, parenthesis nesting, etc.
112 unsigned BindingStrength;
114 /// \brief Penalty for inserting a line break before this token.
115 unsigned SplitPenalty;
117 /// \brief If this is the first ObjC selector name in an ObjC method
118 /// definition or call, this contains the length of the longest name.
119 unsigned LongestObjCSelectorName;
121 std::vector<AnnotatedToken> Children;
122 AnnotatedToken *Parent;
124 /// \brief Insert this many fake ( before this token for correct indentation.
125 unsigned FakeLParens;
126 /// \brief Insert this many fake ) after this token for correct indentation.
127 unsigned FakeRParens;
129 const AnnotatedToken *getPreviousNoneComment() const {
130 AnnotatedToken *Tok = Parent;
131 while (Tok != NULL && Tok->is(tok::comment))
137 class AnnotatedLine {
139 AnnotatedLine(const UnwrappedLine &Line)
140 : First(Line.Tokens.front()), Level(Line.Level),
141 InPPDirective(Line.InPPDirective),
142 MustBeDeclaration(Line.MustBeDeclaration) {
143 assert(!Line.Tokens.empty());
144 AnnotatedToken *Current = &First;
145 for (std::list<FormatToken>::const_iterator I = ++Line.Tokens.begin(),
146 E = Line.Tokens.end();
148 Current->Children.push_back(AnnotatedToken(*I));
149 Current->Children[0].Parent = Current;
150 Current = &Current->Children[0];
154 AnnotatedLine(const AnnotatedLine &Other)
155 : First(Other.First), Type(Other.Type), Level(Other.Level),
156 InPPDirective(Other.InPPDirective),
157 MustBeDeclaration(Other.MustBeDeclaration) {
159 while (!Last->Children.empty()) {
160 Last->Children[0].Parent = Last;
161 Last = &Last->Children[0];
165 AnnotatedToken First;
166 AnnotatedToken *Last;
171 bool MustBeDeclaration;
174 inline prec::Level getPrecedence(const AnnotatedToken &Tok) {
175 return getBinOpPrecedence(Tok.FormatTok.Tok.getKind(), true, true);
178 /// \brief Determines extra information about the tokens comprising an
179 /// \c UnwrappedLine.
180 class TokenAnnotator {
182 TokenAnnotator(const FormatStyle &Style, SourceManager &SourceMgr, Lexer &Lex,
183 IdentifierInfo &Ident_in)
184 : Style(Style), SourceMgr(SourceMgr), Lex(Lex), Ident_in(Ident_in) {
187 void annotate(AnnotatedLine &Line);
188 void calculateFormattingInformation(AnnotatedLine &Line);
191 /// \brief Calculate the penalty for splitting before \c Tok.
192 unsigned splitPenalty(const AnnotatedLine &Line, const AnnotatedToken &Tok);
194 bool spaceRequiredBetween(const AnnotatedLine &Line,
195 const AnnotatedToken &Left,
196 const AnnotatedToken &Right);
198 bool spaceRequiredBefore(const AnnotatedLine &Line,
199 const AnnotatedToken &Tok);
201 bool canBreakBefore(const AnnotatedLine &Line, const AnnotatedToken &Right);
203 const FormatStyle &Style;
204 SourceManager &SourceMgr;
207 // Contextual keywords:
208 IdentifierInfo &Ident_in;
211 } // end namespace format
212 } // end namespace clang
214 #endif // LLVM_CLANG_FORMAT_TOKEN_ANNOTATOR_H