Alexander Kornienko | 4b67207 | 2013-06-03 16:45:03 +0000 | [diff] [blame] | 1 | //===--- 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" |
Daniel Jasper | 8de9ed0 | 2013-08-22 15:00:41 +0000 | [diff] [blame] | 20 | #include "clang/Format/Format.h" |
Alexander Kornienko | 4b67207 | 2013-06-03 16:45:03 +0000 | [diff] [blame] | 21 | #include "clang/Lex/Lexer.h" |
Ahmed Charles | dfca6f9 | 2014-03-09 11:36:40 +0000 | [diff] [blame] | 22 | #include <memory> |
Alexander Kornienko | 4b67207 | 2013-06-03 16:45:03 +0000 | [diff] [blame] | 23 | |
| 24 | namespace clang { |
| 25 | namespace format { |
| 26 | |
| 27 | enum TokenType { |
Daniel Jasper | 1db6c38 | 2013-10-22 15:30:28 +0000 | [diff] [blame] | 28 | TT_ArrayInitializerLSquare, |
| 29 | TT_ArraySubscriptLSquare, |
Daniel Jasper | 559b63c | 2014-01-28 20:13:43 +0000 | [diff] [blame] | 30 | TT_AttributeParen, |
Alexander Kornienko | 4b67207 | 2013-06-03 16:45:03 +0000 | [diff] [blame] | 31 | TT_BinaryOperator, |
Alexander Kornienko | 60d1b04 | 2013-10-10 13:36:20 +0000 | [diff] [blame] | 32 | TT_BitFieldColon, |
Alexander Kornienko | 4b67207 | 2013-06-03 16:45:03 +0000 | [diff] [blame] | 33 | TT_BlockComment, |
| 34 | TT_CastRParen, |
| 35 | TT_ConditionalExpr, |
Manuel Klimek | 68b0304 | 2014-04-14 09:14:11 +0000 | [diff] [blame^] | 36 | TT_ConflictAlternative, |
| 37 | TT_ConflictEnd, |
| 38 | TT_ConflictStart, |
Alexander Kornienko | 4b67207 | 2013-06-03 16:45:03 +0000 | [diff] [blame] | 39 | TT_CtorInitializerColon, |
Daniel Jasper | e33d4af | 2013-07-26 16:56:36 +0000 | [diff] [blame] | 40 | TT_CtorInitializerComma, |
Alexander Kornienko | 4b67207 | 2013-06-03 16:45:03 +0000 | [diff] [blame] | 41 | TT_DesignatedInitializerPeriod, |
Daniel Jasper | b596fb2 | 2013-10-24 10:31:50 +0000 | [diff] [blame] | 42 | TT_DictLiteral, |
Alexander Kornienko | 3cfa973 | 2013-11-20 16:33:05 +0000 | [diff] [blame] | 43 | TT_FunctionLBrace, |
Alexander Kornienko | 4b67207 | 2013-06-03 16:45:03 +0000 | [diff] [blame] | 44 | TT_FunctionTypeLParen, |
Manuel Klimek | 68b0304 | 2014-04-14 09:14:11 +0000 | [diff] [blame^] | 45 | TT_ImplicitStringLiteral, |
| 46 | TT_InheritanceColon, |
| 47 | TT_InlineASMColon, |
Daniel Jasper | 9fe0e8d | 2013-09-05 09:29:45 +0000 | [diff] [blame] | 48 | TT_LambdaLSquare, |
Alexander Kornienko | 4b67207 | 2013-06-03 16:45:03 +0000 | [diff] [blame] | 49 | TT_LineComment, |
Daniel Jasper | c13ee34 | 2014-03-27 09:43:54 +0000 | [diff] [blame] | 50 | TT_ObjCBlockLBrace, |
Manuel Klimek | 68b0304 | 2014-04-14 09:14:11 +0000 | [diff] [blame^] | 51 | TT_ObjCBlockLParen, |
Alexander Kornienko | 4b67207 | 2013-06-03 16:45:03 +0000 | [diff] [blame] | 52 | TT_ObjCDecl, |
Alexander Kornienko | 4b67207 | 2013-06-03 16:45:03 +0000 | [diff] [blame] | 53 | TT_ObjCForIn, |
| 54 | TT_ObjCMethodExpr, |
| 55 | TT_ObjCMethodSpecifier, |
| 56 | TT_ObjCProperty, |
| 57 | TT_ObjCSelectorName, |
| 58 | TT_OverloadedOperator, |
| 59 | TT_OverloadedOperatorLParen, |
| 60 | TT_PointerOrReference, |
| 61 | TT_PureVirtualSpecifier, |
| 62 | TT_RangeBasedForLoopColon, |
| 63 | TT_StartOfName, |
| 64 | TT_TemplateCloser, |
| 65 | TT_TemplateOpener, |
Daniel Jasper | 43e6a28 | 2013-12-16 15:01:54 +0000 | [diff] [blame] | 66 | TT_TrailingAnnotation, |
Daniel Jasper | 6cdec7c | 2013-07-09 14:36:48 +0000 | [diff] [blame] | 67 | TT_TrailingReturnArrow, |
Alexander Kornienko | 4b67207 | 2013-06-03 16:45:03 +0000 | [diff] [blame] | 68 | TT_TrailingUnaryOperator, |
| 69 | TT_UnaryOperator, |
| 70 | TT_Unknown |
| 71 | }; |
| 72 | |
Daniel Jasper | b1f74a8 | 2013-07-09 09:06:29 +0000 | [diff] [blame] | 73 | // Represents what type of block a set of braces open. |
| 74 | enum BraceBlockKind { |
| 75 | BK_Unknown, |
| 76 | BK_Block, |
| 77 | BK_BracedInit |
| 78 | }; |
| 79 | |
Daniel Jasper | b10cbc4 | 2013-07-10 14:02:49 +0000 | [diff] [blame] | 80 | // The packing kind of a function's parameters. |
| 81 | enum ParameterPackingKind { |
| 82 | PPK_BinPacked, |
| 83 | PPK_OnePerLine, |
| 84 | PPK_Inconclusive |
| 85 | }; |
| 86 | |
Manuel Klimek | 71814b4 | 2013-10-11 21:25:45 +0000 | [diff] [blame] | 87 | enum FormatDecision { |
| 88 | FD_Unformatted, |
| 89 | FD_Continue, |
| 90 | FD_Break |
| 91 | }; |
| 92 | |
Daniel Jasper | 8de9ed0 | 2013-08-22 15:00:41 +0000 | [diff] [blame] | 93 | class TokenRole; |
Daniel Jasper | 9fe0e8d | 2013-09-05 09:29:45 +0000 | [diff] [blame] | 94 | class AnnotatedLine; |
Daniel Jasper | 8de9ed0 | 2013-08-22 15:00:41 +0000 | [diff] [blame] | 95 | |
Alexander Kornienko | 4b67207 | 2013-06-03 16:45:03 +0000 | [diff] [blame] | 96 | /// \brief A wrapper around a \c Token storing information about the |
Alp Toker | f6a24ce | 2013-12-05 16:25:25 +0000 | [diff] [blame] | 97 | /// whitespace characters preceding it. |
Alexander Kornienko | 4b67207 | 2013-06-03 16:45:03 +0000 | [diff] [blame] | 98 | struct FormatToken { |
| 99 | FormatToken() |
Alexander Kornienko | 632abb9 | 2013-09-02 13:58:14 +0000 | [diff] [blame] | 100 | : NewlinesBefore(0), HasUnescapedNewline(false), LastNewlineOffset(0), |
Alexander Kornienko | 39856b7 | 2013-09-10 09:38:25 +0000 | [diff] [blame] | 101 | ColumnWidth(0), LastLineColumnWidth(0), IsMultiline(false), |
Alexander Kornienko | 632abb9 | 2013-09-02 13:58:14 +0000 | [diff] [blame] | 102 | IsFirst(false), MustBreakBefore(false), IsUnterminatedLiteral(false), |
Alexander Kornienko | d7b837e | 2013-08-29 17:32:57 +0000 | [diff] [blame] | 103 | BlockKind(BK_Unknown), Type(TT_Unknown), SpacesRequiredBefore(0), |
| 104 | CanBreakBefore(false), ClosesTemplateDeclaration(false), |
| 105 | ParameterCount(0), PackingKind(PPK_Inconclusive), TotalLength(0), |
Daniel Jasper | 63af7c4 | 2013-12-09 14:40:19 +0000 | [diff] [blame] | 106 | UnbreakableTailLength(0), BindingStrength(0), NestingLevel(0), |
| 107 | SplitPenalty(0), LongestObjCSelectorName(0), FakeRParens(0), |
Daniel Jasper | 562ecd4 | 2013-09-06 08:08:14 +0000 | [diff] [blame] | 108 | StartsBinaryExpression(false), EndsBinaryExpression(false), |
| 109 | LastInChainOfCalls(false), PartOfMultiVariableDeclStmt(false), |
Daniel Jasper | e1e4319 | 2014-04-01 12:55:11 +0000 | [diff] [blame] | 110 | IsForEachMacro(false), MatchingParen(NULL), Previous(NULL), Next(NULL), |
Manuel Klimek | 71814b4 | 2013-10-11 21:25:45 +0000 | [diff] [blame] | 111 | Decision(FD_Unformatted), Finalized(false) {} |
Alexander Kornienko | 4b67207 | 2013-06-03 16:45:03 +0000 | [diff] [blame] | 112 | |
| 113 | /// \brief The \c Token. |
| 114 | Token Tok; |
| 115 | |
| 116 | /// \brief The number of newlines immediately before the \c Token. |
| 117 | /// |
| 118 | /// This can be used to determine what the user wrote in the original code |
| 119 | /// and thereby e.g. leave an empty line between two function definitions. |
| 120 | unsigned NewlinesBefore; |
| 121 | |
| 122 | /// \brief Whether there is at least one unescaped newline before the \c |
| 123 | /// Token. |
| 124 | bool HasUnescapedNewline; |
| 125 | |
Alp Toker | f6a24ce | 2013-12-05 16:25:25 +0000 | [diff] [blame] | 126 | /// \brief The range of the whitespace immediately preceding the \c Token. |
Alexander Kornienko | 4b67207 | 2013-06-03 16:45:03 +0000 | [diff] [blame] | 127 | SourceRange WhitespaceRange; |
| 128 | |
| 129 | /// \brief The offset just past the last '\n' in this token's leading |
| 130 | /// whitespace (relative to \c WhiteSpaceStart). 0 if there is no '\n'. |
| 131 | unsigned LastNewlineOffset; |
| 132 | |
Alexander Kornienko | 39856b7 | 2013-09-10 09:38:25 +0000 | [diff] [blame] | 133 | /// \brief The width of the non-whitespace parts of the token (or its first |
| 134 | /// line for multi-line tokens) in columns. |
Alexander Kornienko | ffcc010 | 2013-06-05 14:09:10 +0000 | [diff] [blame] | 135 | /// We need this to correctly measure number of columns a token spans. |
Alexander Kornienko | 39856b7 | 2013-09-10 09:38:25 +0000 | [diff] [blame] | 136 | unsigned ColumnWidth; |
Alexander Kornienko | 4b67207 | 2013-06-03 16:45:03 +0000 | [diff] [blame] | 137 | |
Alexander Kornienko | 39856b7 | 2013-09-10 09:38:25 +0000 | [diff] [blame] | 138 | /// \brief Contains the width in columns of the last line of a multi-line |
Alexander Kornienko | 632abb9 | 2013-09-02 13:58:14 +0000 | [diff] [blame] | 139 | /// token. |
Alexander Kornienko | ebb43ca | 2013-09-05 14:08:34 +0000 | [diff] [blame] | 140 | unsigned LastLineColumnWidth; |
Alexander Kornienko | 632abb9 | 2013-09-02 13:58:14 +0000 | [diff] [blame] | 141 | |
Alexander Kornienko | 39856b7 | 2013-09-10 09:38:25 +0000 | [diff] [blame] | 142 | /// \brief Whether the token text contains newlines (escaped or not). |
| 143 | bool IsMultiline; |
Alexander Kornienko | 632abb9 | 2013-09-02 13:58:14 +0000 | [diff] [blame] | 144 | |
Alexander Kornienko | 4b67207 | 2013-06-03 16:45:03 +0000 | [diff] [blame] | 145 | /// \brief Indicates that this is the first token. |
| 146 | bool IsFirst; |
| 147 | |
| 148 | /// \brief Whether there must be a line break before this token. |
| 149 | /// |
| 150 | /// This happens for example when a preprocessor directive ended directly |
| 151 | /// before the token. |
| 152 | bool MustBreakBefore; |
| 153 | |
| 154 | /// \brief Returns actual token start location without leading escaped |
| 155 | /// newlines and whitespace. |
| 156 | /// |
| 157 | /// This can be different to Tok.getLocation(), which includes leading escaped |
| 158 | /// newlines. |
| 159 | SourceLocation getStartOfNonWhitespace() const { |
| 160 | return WhitespaceRange.getEnd(); |
| 161 | } |
| 162 | |
| 163 | /// \brief The raw text of the token. |
| 164 | /// |
| 165 | /// Contains the raw token text without leading whitespace and without leading |
| 166 | /// escaped newlines. |
| 167 | StringRef TokenText; |
| 168 | |
Daniel Jasper | 8369aa5 | 2013-07-16 20:28:33 +0000 | [diff] [blame] | 169 | /// \brief Set to \c true if this token is an unterminated literal. |
| 170 | bool IsUnterminatedLiteral; |
| 171 | |
Daniel Jasper | b1f74a8 | 2013-07-09 09:06:29 +0000 | [diff] [blame] | 172 | /// \brief Contains the kind of block if this token is a brace. |
| 173 | BraceBlockKind BlockKind; |
| 174 | |
Alexander Kornienko | 4b67207 | 2013-06-03 16:45:03 +0000 | [diff] [blame] | 175 | TokenType Type; |
| 176 | |
Daniel Jasper | 8de9ed0 | 2013-08-22 15:00:41 +0000 | [diff] [blame] | 177 | /// \brief The number of spaces that should be inserted before this token. |
Alexander Kornienko | 4b67207 | 2013-06-03 16:45:03 +0000 | [diff] [blame] | 178 | unsigned SpacesRequiredBefore; |
Daniel Jasper | 8de9ed0 | 2013-08-22 15:00:41 +0000 | [diff] [blame] | 179 | |
| 180 | /// \brief \c true if it is allowed to break before this token. |
Alexander Kornienko | 4b67207 | 2013-06-03 16:45:03 +0000 | [diff] [blame] | 181 | bool CanBreakBefore; |
| 182 | |
| 183 | bool ClosesTemplateDeclaration; |
| 184 | |
| 185 | /// \brief Number of parameters, if this is "(", "[" or "<". |
| 186 | /// |
| 187 | /// This is initialized to 1 as we don't need to distinguish functions with |
| 188 | /// 0 parameters from functions with 1 parameter. Thus, we can simply count |
| 189 | /// the number of commas. |
| 190 | unsigned ParameterCount; |
| 191 | |
Daniel Jasper | 8de9ed0 | 2013-08-22 15:00:41 +0000 | [diff] [blame] | 192 | /// \brief A token can have a special role that can carry extra information |
| 193 | /// about the token's formatting. |
Ahmed Charles | b898432 | 2014-03-07 20:03:18 +0000 | [diff] [blame] | 194 | std::unique_ptr<TokenRole> Role; |
Daniel Jasper | 8de9ed0 | 2013-08-22 15:00:41 +0000 | [diff] [blame] | 195 | |
Daniel Jasper | b10cbc4 | 2013-07-10 14:02:49 +0000 | [diff] [blame] | 196 | /// \brief If this is an opening parenthesis, how are the parameters packed? |
| 197 | ParameterPackingKind PackingKind; |
| 198 | |
Manuel Klimek | 31c8592 | 2013-08-29 15:21:40 +0000 | [diff] [blame] | 199 | /// \brief The total length of the unwrapped line up to and including this |
| 200 | /// token. |
Alexander Kornienko | 4b67207 | 2013-06-03 16:45:03 +0000 | [diff] [blame] | 201 | unsigned TotalLength; |
| 202 | |
Alexander Kornienko | 39856b7 | 2013-09-10 09:38:25 +0000 | [diff] [blame] | 203 | /// \brief The original 0-based column of this token, including expanded tabs. |
| 204 | /// The configured TabWidth is used as tab width. |
Manuel Klimek | 31c8592 | 2013-08-29 15:21:40 +0000 | [diff] [blame] | 205 | unsigned OriginalColumn; |
| 206 | |
Alexander Kornienko | 4b67207 | 2013-06-03 16:45:03 +0000 | [diff] [blame] | 207 | /// \brief The length of following tokens until the next natural split point, |
| 208 | /// or the next token that can be broken. |
| 209 | unsigned UnbreakableTailLength; |
| 210 | |
| 211 | // FIXME: Come up with a 'cleaner' concept. |
| 212 | /// \brief The binding strength of a token. This is a combined value of |
| 213 | /// operator precedence, parenthesis nesting, etc. |
| 214 | unsigned BindingStrength; |
| 215 | |
Daniel Jasper | 63af7c4 | 2013-12-09 14:40:19 +0000 | [diff] [blame] | 216 | /// \brief The nesting level of this token, i.e. the number of surrounding (), |
| 217 | /// [], {} or <>. |
| 218 | unsigned NestingLevel; |
| 219 | |
Alexander Kornienko | 4b67207 | 2013-06-03 16:45:03 +0000 | [diff] [blame] | 220 | /// \brief Penalty for inserting a line break before this token. |
| 221 | unsigned SplitPenalty; |
| 222 | |
| 223 | /// \brief If this is the first ObjC selector name in an ObjC method |
| 224 | /// definition or call, this contains the length of the longest name. |
Daniel Jasper | b88b25f | 2013-12-23 07:29:06 +0000 | [diff] [blame] | 225 | /// |
| 226 | /// This being set to 0 means that the selectors should not be colon-aligned, |
| 227 | /// e.g. because several of them are block-type. |
Alexander Kornienko | 4b67207 | 2013-06-03 16:45:03 +0000 | [diff] [blame] | 228 | unsigned LongestObjCSelectorName; |
| 229 | |
| 230 | /// \brief Stores the number of required fake parentheses and the |
| 231 | /// corresponding operator precedence. |
| 232 | /// |
| 233 | /// If multiple fake parentheses start at a token, this vector stores them in |
| 234 | /// reverse order, i.e. inner fake parenthesis first. |
| 235 | SmallVector<prec::Level, 4> FakeLParens; |
| 236 | /// \brief Insert this many fake ) after this token for correct indentation. |
| 237 | unsigned FakeRParens; |
| 238 | |
Daniel Jasper | 562ecd4 | 2013-09-06 08:08:14 +0000 | [diff] [blame] | 239 | /// \brief \c true if this token starts a binary expression, i.e. has at least |
| 240 | /// one fake l_paren with a precedence greater than prec::Unknown. |
| 241 | bool StartsBinaryExpression; |
| 242 | /// \brief \c true if this token ends a binary expression. |
| 243 | bool EndsBinaryExpression; |
| 244 | |
Alexander Kornienko | 4b67207 | 2013-06-03 16:45:03 +0000 | [diff] [blame] | 245 | /// \brief Is this the last "." or "->" in a builder-type call? |
| 246 | bool LastInChainOfCalls; |
| 247 | |
| 248 | /// \brief Is this token part of a \c DeclStmt defining multiple variables? |
| 249 | /// |
| 250 | /// Only set if \c Type == \c TT_StartOfName. |
| 251 | bool PartOfMultiVariableDeclStmt; |
| 252 | |
Daniel Jasper | e1e4319 | 2014-04-01 12:55:11 +0000 | [diff] [blame] | 253 | /// \brief Is this a foreach macro? |
| 254 | bool IsForEachMacro; |
| 255 | |
Alexander Kornienko | 4b67207 | 2013-06-03 16:45:03 +0000 | [diff] [blame] | 256 | bool is(tok::TokenKind Kind) const { return Tok.is(Kind); } |
| 257 | |
| 258 | bool isOneOf(tok::TokenKind K1, tok::TokenKind K2) const { |
| 259 | return is(K1) || is(K2); |
| 260 | } |
| 261 | |
| 262 | bool isOneOf(tok::TokenKind K1, tok::TokenKind K2, tok::TokenKind K3) const { |
| 263 | return is(K1) || is(K2) || is(K3); |
| 264 | } |
| 265 | |
| 266 | bool isOneOf(tok::TokenKind K1, tok::TokenKind K2, tok::TokenKind K3, |
| 267 | tok::TokenKind K4, tok::TokenKind K5 = tok::NUM_TOKENS, |
| 268 | tok::TokenKind K6 = tok::NUM_TOKENS, |
| 269 | tok::TokenKind K7 = tok::NUM_TOKENS, |
| 270 | tok::TokenKind K8 = tok::NUM_TOKENS, |
| 271 | tok::TokenKind K9 = tok::NUM_TOKENS, |
| 272 | tok::TokenKind K10 = tok::NUM_TOKENS, |
| 273 | tok::TokenKind K11 = tok::NUM_TOKENS, |
| 274 | tok::TokenKind K12 = tok::NUM_TOKENS) const { |
| 275 | return is(K1) || is(K2) || is(K3) || is(K4) || is(K5) || is(K6) || is(K7) || |
| 276 | is(K8) || is(K9) || is(K10) || is(K11) || is(K12); |
| 277 | } |
| 278 | |
| 279 | bool isNot(tok::TokenKind Kind) const { return Tok.isNot(Kind); } |
Daniel Jasper | 04b6a08 | 2013-12-20 06:22:01 +0000 | [diff] [blame] | 280 | bool isStringLiteral() const { return tok::isStringLiteral(Tok.getKind()); } |
Alexander Kornienko | 4b67207 | 2013-06-03 16:45:03 +0000 | [diff] [blame] | 281 | |
| 282 | bool isObjCAtKeyword(tok::ObjCKeywordKind Kind) const { |
| 283 | return Tok.isObjCAtKeyword(Kind); |
| 284 | } |
| 285 | |
| 286 | bool isAccessSpecifier(bool ColonRequired = true) const { |
| 287 | return isOneOf(tok::kw_public, tok::kw_protected, tok::kw_private) && |
| 288 | (!ColonRequired || (Next && Next->is(tok::colon))); |
| 289 | } |
| 290 | |
Daniel Jasper | cb51cf4 | 2014-01-16 09:11:55 +0000 | [diff] [blame] | 291 | /// \brief Determine whether the token is a simple-type-specifier. |
| 292 | bool isSimpleTypeSpecifier() const; |
| 293 | |
Alexander Kornienko | 4b67207 | 2013-06-03 16:45:03 +0000 | [diff] [blame] | 294 | bool isObjCAccessSpecifier() const { |
| 295 | return is(tok::at) && Next && (Next->isObjCAtKeyword(tok::objc_public) || |
| 296 | Next->isObjCAtKeyword(tok::objc_protected) || |
| 297 | Next->isObjCAtKeyword(tok::objc_package) || |
| 298 | Next->isObjCAtKeyword(tok::objc_private)); |
| 299 | } |
| 300 | |
| 301 | /// \brief Returns whether \p Tok is ([{ or a template opening <. |
| 302 | bool opensScope() const { |
| 303 | return isOneOf(tok::l_paren, tok::l_brace, tok::l_square) || |
| 304 | Type == TT_TemplateOpener; |
Alexander Kornienko | 4b67207 | 2013-06-03 16:45:03 +0000 | [diff] [blame] | 305 | } |
Nico Weber | 0f987a6 | 2013-06-25 19:25:12 +0000 | [diff] [blame] | 306 | /// \brief Returns whether \p Tok is )]} or a template closing >. |
Alexander Kornienko | 4b67207 | 2013-06-03 16:45:03 +0000 | [diff] [blame] | 307 | bool closesScope() const { |
| 308 | return isOneOf(tok::r_paren, tok::r_brace, tok::r_square) || |
| 309 | Type == TT_TemplateCloser; |
| 310 | } |
| 311 | |
Daniel Jasper | 4c6e005 | 2013-08-27 14:24:43 +0000 | [diff] [blame] | 312 | /// \brief Returns \c true if this is a "." or "->" accessing a member. |
| 313 | bool isMemberAccess() const { |
| 314 | return isOneOf(tok::arrow, tok::period) && |
| 315 | Type != TT_DesignatedInitializerPeriod; |
| 316 | } |
| 317 | |
Alexander Kornienko | 4b67207 | 2013-06-03 16:45:03 +0000 | [diff] [blame] | 318 | bool isUnaryOperator() const { |
| 319 | switch (Tok.getKind()) { |
| 320 | case tok::plus: |
| 321 | case tok::plusplus: |
| 322 | case tok::minus: |
| 323 | case tok::minusminus: |
| 324 | case tok::exclaim: |
| 325 | case tok::tilde: |
| 326 | case tok::kw_sizeof: |
| 327 | case tok::kw_alignof: |
| 328 | return true; |
| 329 | default: |
| 330 | return false; |
| 331 | } |
| 332 | } |
Daniel Jasper | 4c6e005 | 2013-08-27 14:24:43 +0000 | [diff] [blame] | 333 | |
Alexander Kornienko | 4b67207 | 2013-06-03 16:45:03 +0000 | [diff] [blame] | 334 | bool isBinaryOperator() const { |
| 335 | // Comma is a binary operator, but does not behave as such wrt. formatting. |
| 336 | return getPrecedence() > prec::Comma; |
| 337 | } |
Daniel Jasper | 4c6e005 | 2013-08-27 14:24:43 +0000 | [diff] [blame] | 338 | |
Alexander Kornienko | 4b67207 | 2013-06-03 16:45:03 +0000 | [diff] [blame] | 339 | bool isTrailingComment() const { |
| 340 | return is(tok::comment) && (!Next || Next->NewlinesBefore > 0); |
| 341 | } |
| 342 | |
| 343 | prec::Level getPrecedence() const { |
| 344 | return getBinOpPrecedence(Tok.getKind(), true, true); |
| 345 | } |
| 346 | |
| 347 | /// \brief Returns the previous token ignoring comments. |
Alexander Kornienko | 1efe0a0 | 2013-07-04 14:47:51 +0000 | [diff] [blame] | 348 | FormatToken *getPreviousNonComment() const { |
Alexander Kornienko | 4b67207 | 2013-06-03 16:45:03 +0000 | [diff] [blame] | 349 | FormatToken *Tok = Previous; |
| 350 | while (Tok != NULL && Tok->is(tok::comment)) |
| 351 | Tok = Tok->Previous; |
| 352 | return Tok; |
| 353 | } |
| 354 | |
| 355 | /// \brief Returns the next token ignoring comments. |
Alexander Kornienko | 1efe0a0 | 2013-07-04 14:47:51 +0000 | [diff] [blame] | 356 | const FormatToken *getNextNonComment() const { |
Alexander Kornienko | 4b67207 | 2013-06-03 16:45:03 +0000 | [diff] [blame] | 357 | const FormatToken *Tok = Next; |
| 358 | while (Tok != NULL && Tok->is(tok::comment)) |
| 359 | Tok = Tok->Next; |
| 360 | return Tok; |
| 361 | } |
| 362 | |
Daniel Jasper | b8f6168 | 2013-10-22 15:45:58 +0000 | [diff] [blame] | 363 | /// \brief Returns \c true if this tokens starts a block-type list, i.e. a |
| 364 | /// list that should be indented with a block indent. |
| 365 | bool opensBlockTypeList(const FormatStyle &Style) const { |
| 366 | return Type == TT_ArrayInitializerLSquare || |
| 367 | (is(tok::l_brace) && |
Daniel Jasper | b596fb2 | 2013-10-24 10:31:50 +0000 | [diff] [blame] | 368 | (BlockKind == BK_Block || Type == TT_DictLiteral || |
Daniel Jasper | b8f6168 | 2013-10-22 15:45:58 +0000 | [diff] [blame] | 369 | !Style.Cpp11BracedListStyle)); |
| 370 | } |
| 371 | |
| 372 | /// \brief Same as opensBlockTypeList, but for the closing token. |
Daniel Jasper | 1db6c38 | 2013-10-22 15:30:28 +0000 | [diff] [blame] | 373 | bool closesBlockTypeList(const FormatStyle &Style) const { |
Daniel Jasper | b8f6168 | 2013-10-22 15:45:58 +0000 | [diff] [blame] | 374 | return MatchingParen && MatchingParen->opensBlockTypeList(Style); |
Daniel Jasper | 1db6c38 | 2013-10-22 15:30:28 +0000 | [diff] [blame] | 375 | } |
| 376 | |
Alexander Kornienko | 4b67207 | 2013-06-03 16:45:03 +0000 | [diff] [blame] | 377 | FormatToken *MatchingParen; |
| 378 | |
| 379 | FormatToken *Previous; |
| 380 | FormatToken *Next; |
| 381 | |
Daniel Jasper | 9fe0e8d | 2013-09-05 09:29:45 +0000 | [diff] [blame] | 382 | SmallVector<AnnotatedLine *, 1> Children; |
| 383 | |
Manuel Klimek | 71814b4 | 2013-10-11 21:25:45 +0000 | [diff] [blame] | 384 | /// \brief Stores the formatting decision for the token once it was made. |
| 385 | FormatDecision Decision; |
| 386 | |
| 387 | /// \brief If \c true, this token has been fully formatted (indented and |
| 388 | /// potentially re-formatted inside), and we do not allow further formatting |
| 389 | /// changes. |
| 390 | bool Finalized; |
| 391 | |
Alexander Kornienko | 4b67207 | 2013-06-03 16:45:03 +0000 | [diff] [blame] | 392 | private: |
| 393 | // Disallow copying. |
Craig Topper | 411294d | 2013-07-01 04:07:34 +0000 | [diff] [blame] | 394 | FormatToken(const FormatToken &) LLVM_DELETED_FUNCTION; |
| 395 | void operator=(const FormatToken &) LLVM_DELETED_FUNCTION; |
Alexander Kornienko | 4b67207 | 2013-06-03 16:45:03 +0000 | [diff] [blame] | 396 | }; |
| 397 | |
Daniel Jasper | 8de9ed0 | 2013-08-22 15:00:41 +0000 | [diff] [blame] | 398 | class ContinuationIndenter; |
| 399 | struct LineState; |
| 400 | |
| 401 | class TokenRole { |
| 402 | public: |
| 403 | TokenRole(const FormatStyle &Style) : Style(Style) {} |
| 404 | virtual ~TokenRole(); |
| 405 | |
| 406 | /// \brief After the \c TokenAnnotator has finished annotating all the tokens, |
| 407 | /// this function precomputes required information for formatting. |
| 408 | virtual void precomputeFormattingInfos(const FormatToken *Token); |
| 409 | |
| 410 | /// \brief Apply the special formatting that the given role demands. |
| 411 | /// |
Daniel Jasper | 0160347 | 2014-01-09 13:42:56 +0000 | [diff] [blame] | 412 | /// Assumes that the token having this role is already formatted. |
| 413 | /// |
Daniel Jasper | 8de9ed0 | 2013-08-22 15:00:41 +0000 | [diff] [blame] | 414 | /// Continues formatting from \p State leaving indentation to \p Indenter and |
| 415 | /// returns the total penalty that this formatting incurs. |
Daniel Jasper | 0160347 | 2014-01-09 13:42:56 +0000 | [diff] [blame] | 416 | virtual unsigned formatFromToken(LineState &State, |
| 417 | ContinuationIndenter *Indenter, |
| 418 | bool DryRun) { |
| 419 | return 0; |
| 420 | } |
| 421 | |
| 422 | /// \brief Same as \c formatFromToken, but assumes that the first token has |
| 423 | /// already been set thereby deciding on the first line break. |
| 424 | virtual unsigned formatAfterToken(LineState &State, |
| 425 | ContinuationIndenter *Indenter, |
| 426 | bool DryRun) { |
Daniel Jasper | 8de9ed0 | 2013-08-22 15:00:41 +0000 | [diff] [blame] | 427 | return 0; |
| 428 | } |
| 429 | |
| 430 | /// \brief Notifies the \c Role that a comma was found. |
| 431 | virtual void CommaFound(const FormatToken *Token) {} |
| 432 | |
| 433 | protected: |
| 434 | const FormatStyle &Style; |
| 435 | }; |
| 436 | |
| 437 | class CommaSeparatedList : public TokenRole { |
| 438 | public: |
Daniel Jasper | 0160347 | 2014-01-09 13:42:56 +0000 | [diff] [blame] | 439 | CommaSeparatedList(const FormatStyle &Style) |
| 440 | : TokenRole(Style), HasNestedBracedList(false) {} |
Daniel Jasper | 8de9ed0 | 2013-08-22 15:00:41 +0000 | [diff] [blame] | 441 | |
Craig Topper | fb6b25b | 2014-03-15 04:29:04 +0000 | [diff] [blame] | 442 | void precomputeFormattingInfos(const FormatToken *Token) override; |
Daniel Jasper | 8de9ed0 | 2013-08-22 15:00:41 +0000 | [diff] [blame] | 443 | |
Craig Topper | fb6b25b | 2014-03-15 04:29:04 +0000 | [diff] [blame] | 444 | unsigned formatAfterToken(LineState &State, ContinuationIndenter *Indenter, |
| 445 | bool DryRun) override; |
Daniel Jasper | 0160347 | 2014-01-09 13:42:56 +0000 | [diff] [blame] | 446 | |
Craig Topper | fb6b25b | 2014-03-15 04:29:04 +0000 | [diff] [blame] | 447 | unsigned formatFromToken(LineState &State, ContinuationIndenter *Indenter, |
| 448 | bool DryRun) override; |
Daniel Jasper | 8de9ed0 | 2013-08-22 15:00:41 +0000 | [diff] [blame] | 449 | |
| 450 | /// \brief Adds \p Token as the next comma to the \c CommaSeparated list. |
Craig Topper | fb6b25b | 2014-03-15 04:29:04 +0000 | [diff] [blame] | 451 | void CommaFound(const FormatToken *Token) override { Commas.push_back(Token);} |
Daniel Jasper | 8de9ed0 | 2013-08-22 15:00:41 +0000 | [diff] [blame] | 452 | |
| 453 | private: |
| 454 | /// \brief A struct that holds information on how to format a given list with |
| 455 | /// a specific number of columns. |
| 456 | struct ColumnFormat { |
| 457 | /// \brief The number of columns to use. |
| 458 | unsigned Columns; |
| 459 | |
| 460 | /// \brief The total width in characters. |
| 461 | unsigned TotalWidth; |
| 462 | |
| 463 | /// \brief The number of lines required for this format. |
| 464 | unsigned LineCount; |
| 465 | |
| 466 | /// \brief The size of each column in characters. |
| 467 | SmallVector<unsigned, 8> ColumnSizes; |
| 468 | }; |
| 469 | |
| 470 | /// \brief Calculate which \c ColumnFormat fits best into |
| 471 | /// \p RemainingCharacters. |
| 472 | const ColumnFormat *getColumnFormat(unsigned RemainingCharacters) const; |
| 473 | |
| 474 | /// \brief The ordered \c FormatTokens making up the commas of this list. |
| 475 | SmallVector<const FormatToken *, 8> Commas; |
| 476 | |
| 477 | /// \brief The length of each of the list's items in characters including the |
| 478 | /// trailing comma. |
| 479 | SmallVector<unsigned, 8> ItemLengths; |
| 480 | |
| 481 | /// \brief Precomputed formats that can be used for this list. |
| 482 | SmallVector<ColumnFormat, 4> Formats; |
Daniel Jasper | 0160347 | 2014-01-09 13:42:56 +0000 | [diff] [blame] | 483 | |
| 484 | bool HasNestedBracedList; |
Daniel Jasper | 8de9ed0 | 2013-08-22 15:00:41 +0000 | [diff] [blame] | 485 | }; |
| 486 | |
Alexander Kornienko | 4b67207 | 2013-06-03 16:45:03 +0000 | [diff] [blame] | 487 | } // namespace format |
| 488 | } // namespace clang |
| 489 | |
| 490 | #endif // LLVM_CLANG_FORMAT_FORMAT_TOKEN_H |