blob: 03f097000f48716a782b423229eecfa14cd74f4a [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,
Daniel Jasper2ca37412013-07-09 14:36:48 +000054 TT_TrailingReturnArrow,
Alexander Kornienko3b711552013-06-03 16:45:03 +000055 TT_TrailingUnaryOperator,
56 TT_UnaryOperator,
57 TT_Unknown
58};
59
Daniel Jasper0de1c4d2013-07-09 09:06:29 +000060// Represents what type of block a set of braces open.
61enum BraceBlockKind {
62 BK_Unknown,
63 BK_Block,
64 BK_BracedInit
65};
66
Daniel Jasperc7bd68f2013-07-10 14:02:49 +000067// The packing kind of a function's parameters.
68enum ParameterPackingKind {
69 PPK_BinPacked,
70 PPK_OnePerLine,
71 PPK_Inconclusive
72};
73
Alexander Kornienko3b711552013-06-03 16:45:03 +000074/// \brief A wrapper around a \c Token storing information about the
75/// whitespace characters preceeding it.
76struct FormatToken {
77 FormatToken()
78 : NewlinesBefore(0), HasUnescapedNewline(false), LastNewlineOffset(0),
Alexander Kornienko54e6c9d2013-06-07 17:45:07 +000079 CodePointCount(0), IsFirst(false), MustBreakBefore(false),
Daniel Jasper0de1c4d2013-07-09 09:06:29 +000080 BlockKind(BK_Unknown), Type(TT_Unknown), SpacesRequiredBefore(0),
81 CanBreakBefore(false), ClosesTemplateDeclaration(false),
Daniel Jasperc7bd68f2013-07-10 14:02:49 +000082 ParameterCount(0), PackingKind(PPK_Inconclusive), TotalLength(0),
83 UnbreakableTailLength(0), BindingStrength(0), SplitPenalty(0),
84 LongestObjCSelectorName(0), FakeRParens(0), LastInChainOfCalls(false),
Alexander Kornienko3b711552013-06-03 16:45:03 +000085 PartOfMultiVariableDeclStmt(false), MatchingParen(NULL), Previous(NULL),
86 Next(NULL) {}
87
88 /// \brief The \c Token.
89 Token Tok;
90
91 /// \brief The number of newlines immediately before the \c Token.
92 ///
93 /// This can be used to determine what the user wrote in the original code
94 /// and thereby e.g. leave an empty line between two function definitions.
95 unsigned NewlinesBefore;
96
97 /// \brief Whether there is at least one unescaped newline before the \c
98 /// Token.
99 bool HasUnescapedNewline;
100
101 /// \brief The range of the whitespace immediately preceeding the \c Token.
102 SourceRange WhitespaceRange;
103
104 /// \brief The offset just past the last '\n' in this token's leading
105 /// whitespace (relative to \c WhiteSpaceStart). 0 if there is no '\n'.
106 unsigned LastNewlineOffset;
107
Alexander Kornienko00895102013-06-05 14:09:10 +0000108 /// \brief The length of the non-whitespace parts of the token in CodePoints.
109 /// We need this to correctly measure number of columns a token spans.
110 unsigned CodePointCount;
Alexander Kornienko3b711552013-06-03 16:45:03 +0000111
112 /// \brief Indicates that this is the first token.
113 bool IsFirst;
114
115 /// \brief Whether there must be a line break before this token.
116 ///
117 /// This happens for example when a preprocessor directive ended directly
118 /// before the token.
119 bool MustBreakBefore;
120
121 /// \brief Returns actual token start location without leading escaped
122 /// newlines and whitespace.
123 ///
124 /// This can be different to Tok.getLocation(), which includes leading escaped
125 /// newlines.
126 SourceLocation getStartOfNonWhitespace() const {
127 return WhitespaceRange.getEnd();
128 }
129
130 /// \brief The raw text of the token.
131 ///
132 /// Contains the raw token text without leading whitespace and without leading
133 /// escaped newlines.
134 StringRef TokenText;
135
Daniel Jasper0de1c4d2013-07-09 09:06:29 +0000136 /// \brief Contains the kind of block if this token is a brace.
137 BraceBlockKind BlockKind;
138
Alexander Kornienko3b711552013-06-03 16:45:03 +0000139 TokenType Type;
140
141 unsigned SpacesRequiredBefore;
142 bool CanBreakBefore;
143
144 bool ClosesTemplateDeclaration;
145
146 /// \brief Number of parameters, if this is "(", "[" or "<".
147 ///
148 /// This is initialized to 1 as we don't need to distinguish functions with
149 /// 0 parameters from functions with 1 parameter. Thus, we can simply count
150 /// the number of commas.
151 unsigned ParameterCount;
152
Daniel Jasperc7bd68f2013-07-10 14:02:49 +0000153 /// \brief If this is an opening parenthesis, how are the parameters packed?
154 ParameterPackingKind PackingKind;
155
Alexander Kornienko3b711552013-06-03 16:45:03 +0000156 /// \brief The total length of the line up to and including this token.
157 unsigned TotalLength;
158
159 /// \brief The length of following tokens until the next natural split point,
160 /// or the next token that can be broken.
161 unsigned UnbreakableTailLength;
162
163 // FIXME: Come up with a 'cleaner' concept.
164 /// \brief The binding strength of a token. This is a combined value of
165 /// operator precedence, parenthesis nesting, etc.
166 unsigned BindingStrength;
167
168 /// \brief Penalty for inserting a line break before this token.
169 unsigned SplitPenalty;
170
171 /// \brief If this is the first ObjC selector name in an ObjC method
172 /// definition or call, this contains the length of the longest name.
173 unsigned LongestObjCSelectorName;
174
175 /// \brief Stores the number of required fake parentheses and the
176 /// corresponding operator precedence.
177 ///
178 /// If multiple fake parentheses start at a token, this vector stores them in
179 /// reverse order, i.e. inner fake parenthesis first.
180 SmallVector<prec::Level, 4> FakeLParens;
181 /// \brief Insert this many fake ) after this token for correct indentation.
182 unsigned FakeRParens;
183
184 /// \brief Is this the last "." or "->" in a builder-type call?
185 bool LastInChainOfCalls;
186
187 /// \brief Is this token part of a \c DeclStmt defining multiple variables?
188 ///
189 /// Only set if \c Type == \c TT_StartOfName.
190 bool PartOfMultiVariableDeclStmt;
191
192 bool is(tok::TokenKind Kind) const { return Tok.is(Kind); }
193
194 bool isOneOf(tok::TokenKind K1, tok::TokenKind K2) const {
195 return is(K1) || is(K2);
196 }
197
198 bool isOneOf(tok::TokenKind K1, tok::TokenKind K2, tok::TokenKind K3) const {
199 return is(K1) || is(K2) || is(K3);
200 }
201
202 bool isOneOf(tok::TokenKind K1, tok::TokenKind K2, tok::TokenKind K3,
203 tok::TokenKind K4, tok::TokenKind K5 = tok::NUM_TOKENS,
204 tok::TokenKind K6 = tok::NUM_TOKENS,
205 tok::TokenKind K7 = tok::NUM_TOKENS,
206 tok::TokenKind K8 = tok::NUM_TOKENS,
207 tok::TokenKind K9 = tok::NUM_TOKENS,
208 tok::TokenKind K10 = tok::NUM_TOKENS,
209 tok::TokenKind K11 = tok::NUM_TOKENS,
210 tok::TokenKind K12 = tok::NUM_TOKENS) const {
211 return is(K1) || is(K2) || is(K3) || is(K4) || is(K5) || is(K6) || is(K7) ||
212 is(K8) || is(K9) || is(K10) || is(K11) || is(K12);
213 }
214
215 bool isNot(tok::TokenKind Kind) const { return Tok.isNot(Kind); }
216
217 bool isObjCAtKeyword(tok::ObjCKeywordKind Kind) const {
218 return Tok.isObjCAtKeyword(Kind);
219 }
220
221 bool isAccessSpecifier(bool ColonRequired = true) const {
222 return isOneOf(tok::kw_public, tok::kw_protected, tok::kw_private) &&
223 (!ColonRequired || (Next && Next->is(tok::colon)));
224 }
225
226 bool isObjCAccessSpecifier() const {
227 return is(tok::at) && Next && (Next->isObjCAtKeyword(tok::objc_public) ||
228 Next->isObjCAtKeyword(tok::objc_protected) ||
229 Next->isObjCAtKeyword(tok::objc_package) ||
230 Next->isObjCAtKeyword(tok::objc_private));
231 }
232
233 /// \brief Returns whether \p Tok is ([{ or a template opening <.
234 bool opensScope() const {
235 return isOneOf(tok::l_paren, tok::l_brace, tok::l_square) ||
236 Type == TT_TemplateOpener;
Alexander Kornienko3b711552013-06-03 16:45:03 +0000237 }
Nico Weber0e5a8882013-06-25 19:25:12 +0000238 /// \brief Returns whether \p Tok is )]} or a template closing >.
Alexander Kornienko3b711552013-06-03 16:45:03 +0000239 bool closesScope() const {
240 return isOneOf(tok::r_paren, tok::r_brace, tok::r_square) ||
241 Type == TT_TemplateCloser;
242 }
243
244 bool isUnaryOperator() const {
245 switch (Tok.getKind()) {
246 case tok::plus:
247 case tok::plusplus:
248 case tok::minus:
249 case tok::minusminus:
250 case tok::exclaim:
251 case tok::tilde:
252 case tok::kw_sizeof:
253 case tok::kw_alignof:
254 return true;
255 default:
256 return false;
257 }
258 }
259 bool isBinaryOperator() const {
260 // Comma is a binary operator, but does not behave as such wrt. formatting.
261 return getPrecedence() > prec::Comma;
262 }
263 bool isTrailingComment() const {
264 return is(tok::comment) && (!Next || Next->NewlinesBefore > 0);
265 }
266
267 prec::Level getPrecedence() const {
268 return getBinOpPrecedence(Tok.getKind(), true, true);
269 }
270
271 /// \brief Returns the previous token ignoring comments.
Alexander Kornienko0bdc6432013-07-04 14:47:51 +0000272 FormatToken *getPreviousNonComment() const {
Alexander Kornienko3b711552013-06-03 16:45:03 +0000273 FormatToken *Tok = Previous;
274 while (Tok != NULL && Tok->is(tok::comment))
275 Tok = Tok->Previous;
276 return Tok;
277 }
278
279 /// \brief Returns the next token ignoring comments.
Alexander Kornienko0bdc6432013-07-04 14:47:51 +0000280 const FormatToken *getNextNonComment() const {
Alexander Kornienko3b711552013-06-03 16:45:03 +0000281 const FormatToken *Tok = Next;
282 while (Tok != NULL && Tok->is(tok::comment))
283 Tok = Tok->Next;
284 return Tok;
285 }
286
287 FormatToken *MatchingParen;
288
289 FormatToken *Previous;
290 FormatToken *Next;
291
292private:
293 // Disallow copying.
Craig Topper53d4f312013-07-01 04:07:34 +0000294 FormatToken(const FormatToken &) LLVM_DELETED_FUNCTION;
295 void operator=(const FormatToken &) LLVM_DELETED_FUNCTION;
Alexander Kornienko3b711552013-06-03 16:45:03 +0000296};
297
298} // namespace format
299} // namespace clang
300
301#endif // LLVM_CLANG_FORMAT_FORMAT_TOKEN_H