blob: 64c597e0d91e63136dfeef5a2e1b0ce013d2df04 [file] [log] [blame]
Daniel Jasper32d28ee2013-01-29 21:01:14 +00001//===--- 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
24namespace clang {
25class Lexer;
26class SourceManager;
27
28namespace format {
29
30enum TokenType {
31 TT_BinaryOperator,
32 TT_BlockComment,
33 TT_CastRParen,
34 TT_ConditionalExpr,
35 TT_CtorInitializerColon,
36 TT_ImplicitStringLiteral,
37 TT_LineComment,
38 TT_ObjCBlockLParen,
39 TT_ObjCDecl,
40 TT_ObjCMethodSpecifier,
41 TT_ObjCMethodExpr,
42 TT_ObjCProperty,
Daniel Jasper63d7ced2013-02-05 10:07:47 +000043 TT_ObjCSelectorName,
Daniel Jasper32d28ee2013-01-29 21:01:14 +000044 TT_OverloadedOperator,
45 TT_PointerOrReference,
46 TT_PureVirtualSpecifier,
47 TT_RangeBasedForLoopColon,
48 TT_StartOfName,
49 TT_TemplateCloser,
50 TT_TemplateOpener,
51 TT_TrailingUnaryOperator,
52 TT_UnaryOperator,
53 TT_Unknown
54};
55
56enum LineType {
57 LT_Invalid,
58 LT_Other,
59 LT_BuilderTypeCall,
60 LT_PreprocessorDirective,
61 LT_VirtualFunctionDecl,
62 LT_ObjCDecl, // An @interface, @implementation, or @protocol line.
63 LT_ObjCMethodDecl,
64 LT_ObjCProperty // An @property line.
65};
66
67class AnnotatedToken {
68public:
69 explicit AnnotatedToken(const FormatToken &FormatTok)
70 : FormatTok(FormatTok), Type(TT_Unknown), SpaceRequiredBefore(false),
71 CanBreakBefore(false), MustBreakBefore(false),
72 ClosesTemplateDeclaration(false), MatchingParen(NULL),
Daniel Jasper63d7ced2013-02-05 10:07:47 +000073 ParameterCount(1), BindingStrength(0), SplitPenalty(0),
74 LongestObjCSelectorName(0), Parent(NULL) {
Daniel Jasper32d28ee2013-01-29 21:01:14 +000075 }
76
77 bool is(tok::TokenKind Kind) const { return FormatTok.Tok.is(Kind); }
78 bool isNot(tok::TokenKind Kind) const { return FormatTok.Tok.isNot(Kind); }
79
80 bool isObjCAtKeyword(tok::ObjCKeywordKind Kind) const {
81 return FormatTok.Tok.isObjCAtKeyword(Kind);
82 }
83
84 FormatToken FormatTok;
85
86 TokenType Type;
87
88 bool SpaceRequiredBefore;
89 bool CanBreakBefore;
90 bool MustBreakBefore;
91
92 bool ClosesTemplateDeclaration;
93
94 AnnotatedToken *MatchingParen;
95
96 /// \brief Number of parameters, if this is "(", "[" or "<".
97 ///
98 /// This is initialized to 1 as we don't need to distinguish functions with
99 /// 0 parameters from functions with 1 parameter. Thus, we can simply count
100 /// the number of commas.
101 unsigned ParameterCount;
102
103 /// \brief The total length of the line up to and including this token.
104 unsigned TotalLength;
105
Daniel Jasper01786732013-02-04 07:21:18 +0000106 // FIXME: Come up with a 'cleaner' concept.
107 /// \brief The binding strength of a token. This is a combined value of
108 /// operator precedence, parenthesis nesting, etc.
109 unsigned BindingStrength;
110
Daniel Jasper32d28ee2013-01-29 21:01:14 +0000111 /// \brief Penalty for inserting a line break before this token.
112 unsigned SplitPenalty;
113
Daniel Jasper63d7ced2013-02-05 10:07:47 +0000114 /// \brief If this is the first ObjC selector name in an ObjC method
115 /// definition or call, this contains the length of the longest name.
116 unsigned LongestObjCSelectorName;
117
Daniel Jasper32d28ee2013-01-29 21:01:14 +0000118 std::vector<AnnotatedToken> Children;
119 AnnotatedToken *Parent;
120
121 const AnnotatedToken *getPreviousNoneComment() const {
122 AnnotatedToken *Tok = Parent;
123 while (Tok != NULL && Tok->is(tok::comment))
124 Tok = Tok->Parent;
125 return Tok;
126 }
127};
128
129class AnnotatedLine {
130public:
131 AnnotatedLine(const UnwrappedLine &Line)
132 : First(Line.Tokens.front()), Level(Line.Level),
133 InPPDirective(Line.InPPDirective),
134 MustBeDeclaration(Line.MustBeDeclaration) {
135 assert(!Line.Tokens.empty());
136 AnnotatedToken *Current = &First;
137 for (std::list<FormatToken>::const_iterator I = ++Line.Tokens.begin(),
138 E = Line.Tokens.end();
139 I != E; ++I) {
140 Current->Children.push_back(AnnotatedToken(*I));
141 Current->Children[0].Parent = Current;
142 Current = &Current->Children[0];
143 }
144 Last = Current;
145 }
146 AnnotatedLine(const AnnotatedLine &Other)
147 : First(Other.First), Type(Other.Type), Level(Other.Level),
148 InPPDirective(Other.InPPDirective),
149 MustBeDeclaration(Other.MustBeDeclaration) {
150 Last = &First;
151 while (!Last->Children.empty()) {
152 Last->Children[0].Parent = Last;
153 Last = &Last->Children[0];
154 }
155 }
156
157 AnnotatedToken First;
158 AnnotatedToken *Last;
159
160 LineType Type;
161 unsigned Level;
162 bool InPPDirective;
163 bool MustBeDeclaration;
164};
165
166inline prec::Level getPrecedence(const AnnotatedToken &Tok) {
167 return getBinOpPrecedence(Tok.FormatTok.Tok.getKind(), true, true);
168}
169
170/// \brief Determines extra information about the tokens comprising an
171/// \c UnwrappedLine.
172class TokenAnnotator {
173public:
174 TokenAnnotator(const FormatStyle &Style, SourceManager &SourceMgr, Lexer &Lex,
175 AnnotatedLine &Line)
176 : Style(Style), SourceMgr(SourceMgr), Lex(Lex), Line(Line) {
177 }
178
179 void annotate();
Daniel Jasper01786732013-02-04 07:21:18 +0000180 void calculateFormattingInformation(AnnotatedToken &Current);
Daniel Jasper32d28ee2013-01-29 21:01:14 +0000181
182private:
183 /// \brief Calculate the penalty for splitting before \c Tok.
184 unsigned splitPenalty(const AnnotatedToken &Tok);
185
Daniel Jasper32d28ee2013-01-29 21:01:14 +0000186 bool spaceRequiredBetween(const AnnotatedToken &Left,
187 const AnnotatedToken &Right);
188
189 bool spaceRequiredBefore(const AnnotatedToken &Tok);
190
191 bool canBreakBefore(const AnnotatedToken &Right);
192
193 FormatStyle Style;
194 SourceManager &SourceMgr;
195 Lexer &Lex;
196 AnnotatedLine &Line;
197};
198
Daniel Jasper32d28ee2013-01-29 21:01:14 +0000199} // end namespace format
200} // end namespace clang
201
202#endif // LLVM_CLANG_FORMAT_TOKEN_ANNOTATOR_H