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