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