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