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,
42 TT_ObjCMethodSpecifier,
45 TT_OverloadedOperatorLParen,
46 TT_PointerOrReference,
47 TT_PureVirtualSpecifier,
48 TT_RangeBasedForLoopColon,
52 TT_TrailingUnaryOperator,
61 LT_PreprocessorDirective,
62 LT_VirtualFunctionDecl,
63 LT_ObjCDecl, // An @interface, @implementation, or @protocol line.
65 LT_ObjCProperty // An @property line.
68 class AnnotatedToken {
70 explicit AnnotatedToken(const FormatToken &FormatTok)
71 : FormatTok(FormatTok), Type(TT_Unknown), SpacesRequiredBefore(0),
72 CanBreakBefore(false), MustBreakBefore(false),
73 ClosesTemplateDeclaration(false), MatchingParen(NULL),
74 ParameterCount(1), BindingStrength(0), SplitPenalty(0),
75 LongestObjCSelectorName(0), Parent(NULL), FakeLParens(0),
79 bool is(tok::TokenKind Kind) const { return FormatTok.Tok.is(Kind); }
80 bool isNot(tok::TokenKind Kind) const { return FormatTok.Tok.isNot(Kind); }
82 bool isObjCAtKeyword(tok::ObjCKeywordKind Kind) const {
83 return FormatTok.Tok.isObjCAtKeyword(Kind);
86 FormatToken FormatTok;
90 unsigned SpacesRequiredBefore;
94 bool ClosesTemplateDeclaration;
96 AnnotatedToken *MatchingParen;
98 /// \brief Number of parameters, if this is "(", "[" or "<".
100 /// This is initialized to 1 as we don't need to distinguish functions with
101 /// 0 parameters from functions with 1 parameter. Thus, we can simply count
102 /// the number of commas.
103 unsigned ParameterCount;
105 /// \brief The total length of the line up to and including this token.
106 unsigned TotalLength;
108 // FIXME: Come up with a 'cleaner' concept.
109 /// \brief The binding strength of a token. This is a combined value of
110 /// operator precedence, parenthesis nesting, etc.
111 unsigned BindingStrength;
113 /// \brief Penalty for inserting a line break before this token.
114 unsigned SplitPenalty;
116 /// \brief If this is the first ObjC selector name in an ObjC method
117 /// definition or call, this contains the length of the longest name.
118 unsigned LongestObjCSelectorName;
120 std::vector<AnnotatedToken> Children;
121 AnnotatedToken *Parent;
123 /// \brief Insert this many fake ( before this token for correct indentation.
124 unsigned FakeLParens;
125 /// \brief Insert this many fake ) after this token for correct indentation.
126 unsigned FakeRParens;
128 const AnnotatedToken *getPreviousNoneComment() const {
129 AnnotatedToken *Tok = Parent;
130 while (Tok != NULL && Tok->is(tok::comment))
136 class AnnotatedLine {
138 AnnotatedLine(const UnwrappedLine &Line)
139 : First(Line.Tokens.front()), Level(Line.Level),
140 InPPDirective(Line.InPPDirective),
141 MustBeDeclaration(Line.MustBeDeclaration) {
142 assert(!Line.Tokens.empty());
143 AnnotatedToken *Current = &First;
144 for (std::list<FormatToken>::const_iterator I = ++Line.Tokens.begin(),
145 E = Line.Tokens.end();
147 Current->Children.push_back(AnnotatedToken(*I));
148 Current->Children[0].Parent = Current;
149 Current = &Current->Children[0];
153 AnnotatedLine(const AnnotatedLine &Other)
154 : First(Other.First), Type(Other.Type), Level(Other.Level),
155 InPPDirective(Other.InPPDirective),
156 MustBeDeclaration(Other.MustBeDeclaration) {
158 while (!Last->Children.empty()) {
159 Last->Children[0].Parent = Last;
160 Last = &Last->Children[0];
164 AnnotatedToken First;
165 AnnotatedToken *Last;
170 bool MustBeDeclaration;
173 inline prec::Level getPrecedence(const AnnotatedToken &Tok) {
174 return getBinOpPrecedence(Tok.FormatTok.Tok.getKind(), true, true);
177 /// \brief Determines extra information about the tokens comprising an
178 /// \c UnwrappedLine.
179 class TokenAnnotator {
181 TokenAnnotator(const FormatStyle &Style, SourceManager &SourceMgr, Lexer &Lex)
182 : Style(Style), SourceMgr(SourceMgr), Lex(Lex) {
185 void annotate(AnnotatedLine &Line);
186 void calculateFormattingInformation(AnnotatedLine &Line);
189 /// \brief Calculate the penalty for splitting before \c Tok.
190 unsigned splitPenalty(const AnnotatedLine &Line, const AnnotatedToken &Tok);
192 bool spaceRequiredBetween(const AnnotatedLine &Line,
193 const AnnotatedToken &Left,
194 const AnnotatedToken &Right);
196 bool spaceRequiredBefore(const AnnotatedLine &Line,
197 const AnnotatedToken &Tok);
199 bool canBreakBefore(const AnnotatedLine &Line, const AnnotatedToken &Right);
201 const FormatStyle &Style;
202 SourceManager &SourceMgr;
206 } // end namespace format
207 } // end namespace clang
209 #endif // LLVM_CLANG_FORMAT_TOKEN_ANNOTATOR_H