blob: e77ac016be0af2086bfeeae4d3d9752893e77678 [file] [log] [blame]
Alexander Kornienko3b711552013-06-03 16:45:03 +00001//===--- FormatToken.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 contains the declaration of the FormatToken, a wrapper
12/// around Token with additional information related to formatting.
13///
14//===----------------------------------------------------------------------===//
15
16#ifndef LLVM_CLANG_FORMAT_FORMAT_TOKEN_H
17#define LLVM_CLANG_FORMAT_FORMAT_TOKEN_H
18
19#include "clang/Basic/OperatorPrecedence.h"
20#include "clang/Lex/Lexer.h"
21
22namespace clang {
23namespace format {
24
25enum TokenType {
26 TT_BinaryOperator,
27 TT_BlockComment,
28 TT_CastRParen,
29 TT_ConditionalExpr,
30 TT_CtorInitializerColon,
31 TT_DesignatedInitializerPeriod,
32 TT_ImplicitStringLiteral,
33 TT_InlineASMColon,
34 TT_InheritanceColon,
35 TT_FunctionTypeLParen,
36 TT_LineComment,
37 TT_ObjCArrayLiteral,
38 TT_ObjCBlockLParen,
39 TT_ObjCDecl,
40 TT_ObjCDictLiteral,
41 TT_ObjCForIn,
42 TT_ObjCMethodExpr,
43 TT_ObjCMethodSpecifier,
44 TT_ObjCProperty,
45 TT_ObjCSelectorName,
46 TT_OverloadedOperator,
47 TT_OverloadedOperatorLParen,
48 TT_PointerOrReference,
49 TT_PureVirtualSpecifier,
50 TT_RangeBasedForLoopColon,
51 TT_StartOfName,
52 TT_TemplateCloser,
53 TT_TemplateOpener,
54 TT_TrailingUnaryOperator,
55 TT_UnaryOperator,
56 TT_Unknown
57};
58
Daniel Jasper0de1c4d2013-07-09 09:06:29 +000059// Represents what type of block a set of braces open.
60enum BraceBlockKind {
61 BK_Unknown,
62 BK_Block,
63 BK_BracedInit
64};
65
Alexander Kornienko3b711552013-06-03 16:45:03 +000066/// \brief A wrapper around a \c Token storing information about the
67/// whitespace characters preceeding it.
68struct FormatToken {
69 FormatToken()
70 : NewlinesBefore(0), HasUnescapedNewline(false), LastNewlineOffset(0),
Alexander Kornienko54e6c9d2013-06-07 17:45:07 +000071 CodePointCount(0), IsFirst(false), MustBreakBefore(false),
Daniel Jasper0de1c4d2013-07-09 09:06:29 +000072 BlockKind(BK_Unknown), Type(TT_Unknown), SpacesRequiredBefore(0),
73 CanBreakBefore(false), ClosesTemplateDeclaration(false),
74 ParameterCount(0), TotalLength(0), UnbreakableTailLength(0),
75 BindingStrength(0), SplitPenalty(0), LongestObjCSelectorName(0),
76 FakeRParens(0), LastInChainOfCalls(false),
Alexander Kornienko3b711552013-06-03 16:45:03 +000077 PartOfMultiVariableDeclStmt(false), MatchingParen(NULL), Previous(NULL),
78 Next(NULL) {}
79
80 /// \brief The \c Token.
81 Token Tok;
82
83 /// \brief The number of newlines immediately before the \c Token.
84 ///
85 /// This can be used to determine what the user wrote in the original code
86 /// and thereby e.g. leave an empty line between two function definitions.
87 unsigned NewlinesBefore;
88
89 /// \brief Whether there is at least one unescaped newline before the \c
90 /// Token.
91 bool HasUnescapedNewline;
92
93 /// \brief The range of the whitespace immediately preceeding the \c Token.
94 SourceRange WhitespaceRange;
95
96 /// \brief The offset just past the last '\n' in this token's leading
97 /// whitespace (relative to \c WhiteSpaceStart). 0 if there is no '\n'.
98 unsigned LastNewlineOffset;
99
Alexander Kornienko00895102013-06-05 14:09:10 +0000100 /// \brief The length of the non-whitespace parts of the token in CodePoints.
101 /// We need this to correctly measure number of columns a token spans.
102 unsigned CodePointCount;
Alexander Kornienko3b711552013-06-03 16:45:03 +0000103
104 /// \brief Indicates that this is the first token.
105 bool IsFirst;
106
107 /// \brief Whether there must be a line break before this token.
108 ///
109 /// This happens for example when a preprocessor directive ended directly
110 /// before the token.
111 bool MustBreakBefore;
112
113 /// \brief Returns actual token start location without leading escaped
114 /// newlines and whitespace.
115 ///
116 /// This can be different to Tok.getLocation(), which includes leading escaped
117 /// newlines.
118 SourceLocation getStartOfNonWhitespace() const {
119 return WhitespaceRange.getEnd();
120 }
121
122 /// \brief The raw text of the token.
123 ///
124 /// Contains the raw token text without leading whitespace and without leading
125 /// escaped newlines.
126 StringRef TokenText;
127
Daniel Jasper0de1c4d2013-07-09 09:06:29 +0000128 /// \brief Contains the kind of block if this token is a brace.
129 BraceBlockKind BlockKind;
130
Alexander Kornienko3b711552013-06-03 16:45:03 +0000131 TokenType Type;
132
133 unsigned SpacesRequiredBefore;
134 bool CanBreakBefore;
135
136 bool ClosesTemplateDeclaration;
137
138 /// \brief Number of parameters, if this is "(", "[" or "<".
139 ///
140 /// This is initialized to 1 as we don't need to distinguish functions with
141 /// 0 parameters from functions with 1 parameter. Thus, we can simply count
142 /// the number of commas.
143 unsigned ParameterCount;
144
145 /// \brief The total length of the line up to and including this token.
146 unsigned TotalLength;
147
148 /// \brief The length of following tokens until the next natural split point,
149 /// or the next token that can be broken.
150 unsigned UnbreakableTailLength;
151
152 // FIXME: Come up with a 'cleaner' concept.
153 /// \brief The binding strength of a token. This is a combined value of
154 /// operator precedence, parenthesis nesting, etc.
155 unsigned BindingStrength;
156
157 /// \brief Penalty for inserting a line break before this token.
158 unsigned SplitPenalty;
159
160 /// \brief If this is the first ObjC selector name in an ObjC method
161 /// definition or call, this contains the length of the longest name.
162 unsigned LongestObjCSelectorName;
163
164 /// \brief Stores the number of required fake parentheses and the
165 /// corresponding operator precedence.
166 ///
167 /// If multiple fake parentheses start at a token, this vector stores them in
168 /// reverse order, i.e. inner fake parenthesis first.
169 SmallVector<prec::Level, 4> FakeLParens;
170 /// \brief Insert this many fake ) after this token for correct indentation.
171 unsigned FakeRParens;
172
173 /// \brief Is this the last "." or "->" in a builder-type call?
174 bool LastInChainOfCalls;
175
176 /// \brief Is this token part of a \c DeclStmt defining multiple variables?
177 ///
178 /// Only set if \c Type == \c TT_StartOfName.
179 bool PartOfMultiVariableDeclStmt;
180
181 bool is(tok::TokenKind Kind) const { return Tok.is(Kind); }
182
183 bool isOneOf(tok::TokenKind K1, tok::TokenKind K2) const {
184 return is(K1) || is(K2);
185 }
186
187 bool isOneOf(tok::TokenKind K1, tok::TokenKind K2, tok::TokenKind K3) const {
188 return is(K1) || is(K2) || is(K3);
189 }
190
191 bool isOneOf(tok::TokenKind K1, tok::TokenKind K2, tok::TokenKind K3,
192 tok::TokenKind K4, tok::TokenKind K5 = tok::NUM_TOKENS,
193 tok::TokenKind K6 = tok::NUM_TOKENS,
194 tok::TokenKind K7 = tok::NUM_TOKENS,
195 tok::TokenKind K8 = tok::NUM_TOKENS,
196 tok::TokenKind K9 = tok::NUM_TOKENS,
197 tok::TokenKind K10 = tok::NUM_TOKENS,
198 tok::TokenKind K11 = tok::NUM_TOKENS,
199 tok::TokenKind K12 = tok::NUM_TOKENS) const {
200 return is(K1) || is(K2) || is(K3) || is(K4) || is(K5) || is(K6) || is(K7) ||
201 is(K8) || is(K9) || is(K10) || is(K11) || is(K12);
202 }
203
204 bool isNot(tok::TokenKind Kind) const { return Tok.isNot(Kind); }
205
206 bool isObjCAtKeyword(tok::ObjCKeywordKind Kind) const {
207 return Tok.isObjCAtKeyword(Kind);
208 }
209
210 bool isAccessSpecifier(bool ColonRequired = true) const {
211 return isOneOf(tok::kw_public, tok::kw_protected, tok::kw_private) &&
212 (!ColonRequired || (Next && Next->is(tok::colon)));
213 }
214
215 bool isObjCAccessSpecifier() const {
216 return is(tok::at) && Next && (Next->isObjCAtKeyword(tok::objc_public) ||
217 Next->isObjCAtKeyword(tok::objc_protected) ||
218 Next->isObjCAtKeyword(tok::objc_package) ||
219 Next->isObjCAtKeyword(tok::objc_private));
220 }
221
222 /// \brief Returns whether \p Tok is ([{ or a template opening <.
223 bool opensScope() const {
224 return isOneOf(tok::l_paren, tok::l_brace, tok::l_square) ||
225 Type == TT_TemplateOpener;
Alexander Kornienko3b711552013-06-03 16:45:03 +0000226 }
Nico Weber0e5a8882013-06-25 19:25:12 +0000227 /// \brief Returns whether \p Tok is )]} or a template closing >.
Alexander Kornienko3b711552013-06-03 16:45:03 +0000228 bool closesScope() const {
229 return isOneOf(tok::r_paren, tok::r_brace, tok::r_square) ||
230 Type == TT_TemplateCloser;
231 }
232
233 bool isUnaryOperator() const {
234 switch (Tok.getKind()) {
235 case tok::plus:
236 case tok::plusplus:
237 case tok::minus:
238 case tok::minusminus:
239 case tok::exclaim:
240 case tok::tilde:
241 case tok::kw_sizeof:
242 case tok::kw_alignof:
243 return true;
244 default:
245 return false;
246 }
247 }
248 bool isBinaryOperator() const {
249 // Comma is a binary operator, but does not behave as such wrt. formatting.
250 return getPrecedence() > prec::Comma;
251 }
252 bool isTrailingComment() const {
253 return is(tok::comment) && (!Next || Next->NewlinesBefore > 0);
254 }
255
256 prec::Level getPrecedence() const {
257 return getBinOpPrecedence(Tok.getKind(), true, true);
258 }
259
260 /// \brief Returns the previous token ignoring comments.
Alexander Kornienko0bdc6432013-07-04 14:47:51 +0000261 FormatToken *getPreviousNonComment() const {
Alexander Kornienko3b711552013-06-03 16:45:03 +0000262 FormatToken *Tok = Previous;
263 while (Tok != NULL && Tok->is(tok::comment))
264 Tok = Tok->Previous;
265 return Tok;
266 }
267
268 /// \brief Returns the next token ignoring comments.
Alexander Kornienko0bdc6432013-07-04 14:47:51 +0000269 const FormatToken *getNextNonComment() const {
Alexander Kornienko3b711552013-06-03 16:45:03 +0000270 const FormatToken *Tok = Next;
271 while (Tok != NULL && Tok->is(tok::comment))
272 Tok = Tok->Next;
273 return Tok;
274 }
275
276 FormatToken *MatchingParen;
277
278 FormatToken *Previous;
279 FormatToken *Next;
280
281private:
282 // Disallow copying.
Craig Topper53d4f312013-07-01 04:07:34 +0000283 FormatToken(const FormatToken &) LLVM_DELETED_FUNCTION;
284 void operator=(const FormatToken &) LLVM_DELETED_FUNCTION;
Alexander Kornienko3b711552013-06-03 16:45:03 +0000285};
286
287} // namespace format
288} // namespace clang
289
290#endif // LLVM_CLANG_FORMAT_FORMAT_TOKEN_H