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