| 1 | //===--- FormatToken.h - Format C++ code ------------------------*- C++ -*-===// |
| 2 | // |
| 3 | // Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions. |
| 4 | // See https://llvm.org/LICENSE.txt for license information. |
| 5 | // SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception |
| 6 | // |
| 7 | //===----------------------------------------------------------------------===// |
| 8 | /// |
| 9 | /// \file |
| 10 | /// This file contains the declaration of the FormatToken, a wrapper |
| 11 | /// around Token with additional information related to formatting. |
| 12 | /// |
| 13 | //===----------------------------------------------------------------------===// |
| 14 | |
| 15 | #ifndef LLVM_CLANG_LIB_FORMAT_FORMATTOKEN_H |
| 16 | #define LLVM_CLANG_LIB_FORMAT_FORMATTOKEN_H |
| 17 | |
| 18 | #include "clang/Basic/IdentifierTable.h" |
| 19 | #include "clang/Basic/OperatorPrecedence.h" |
| 20 | #include "clang/Format/Format.h" |
| 21 | #include "clang/Lex/Lexer.h" |
| 22 | #include <unordered_set> |
| 23 | |
| 24 | namespace clang { |
| 25 | namespace format { |
| 26 | |
| 27 | #define LIST_TOKEN_TYPES \ |
| 28 | TYPE(AfterPPDirective) \ |
| 29 | TYPE(ArrayInitializerLSquare) \ |
| 30 | TYPE(ArraySubscriptLSquare) \ |
| 31 | TYPE(AttributeColon) \ |
| 32 | TYPE(AttributeLParen) \ |
| 33 | TYPE(AttributeMacro) \ |
| 34 | TYPE(AttributeRParen) \ |
| 35 | TYPE(AttributeSquare) \ |
| 36 | TYPE(BinaryOperator) \ |
| 37 | TYPE(BitFieldColon) \ |
| 38 | TYPE(BlockComment) \ |
| 39 | /* l_brace of a block that is not the body of a (e.g. loop) statement. */ \ |
| 40 | TYPE(BlockLBrace) \ |
| 41 | TYPE(BracedListLBrace) \ |
| 42 | TYPE(CaseLabelArrow) \ |
| 43 | /* The colon at the end of a case label. */ \ |
| 44 | TYPE(CaseLabelColon) \ |
| 45 | TYPE(CastRParen) \ |
| 46 | TYPE(ClassLBrace) \ |
| 47 | /* Name of class/struct/union/interface definition. */ \ |
| 48 | TYPE(ClassHeadName) \ |
| 49 | TYPE(ClassRBrace) \ |
| 50 | TYPE(CompoundRequirementLBrace) \ |
| 51 | /* ternary ?: expression */ \ |
| 52 | TYPE(ConditionalExpr) \ |
| 53 | /* the condition in an if statement */ \ |
| 54 | TYPE(ConditionLParen) \ |
| 55 | TYPE(ConflictAlternative) \ |
| 56 | TYPE(ConflictEnd) \ |
| 57 | TYPE(ConflictStart) \ |
| 58 | /* l_brace of if/for/while */ \ |
| 59 | TYPE(ControlStatementLBrace) \ |
| 60 | TYPE(ControlStatementRBrace) \ |
| 61 | TYPE(CppCastLParen) \ |
| 62 | TYPE(CSharpGenericTypeConstraint) \ |
| 63 | TYPE(CSharpGenericTypeConstraintColon) \ |
| 64 | TYPE(CSharpGenericTypeConstraintComma) \ |
| 65 | TYPE(CSharpNamedArgumentColon) \ |
| 66 | TYPE(CSharpNullable) \ |
| 67 | TYPE(CSharpNullConditionalLSquare) \ |
| 68 | TYPE(CSharpStringLiteral) \ |
| 69 | TYPE(CtorInitializerColon) \ |
| 70 | TYPE(CtorInitializerComma) \ |
| 71 | TYPE(CtorDtorDeclName) \ |
| 72 | TYPE(DesignatedInitializerLSquare) \ |
| 73 | TYPE(DesignatedInitializerPeriod) \ |
| 74 | TYPE(DictLiteral) \ |
| 75 | TYPE(DoWhile) \ |
| 76 | TYPE(ElseLBrace) \ |
| 77 | TYPE(ElseRBrace) \ |
| 78 | TYPE(EnumLBrace) \ |
| 79 | TYPE(EnumRBrace) \ |
| 80 | TYPE(FatArrow) \ |
| 81 | TYPE(ForEachMacro) \ |
| 82 | TYPE(FunctionAnnotationRParen) \ |
| 83 | TYPE(FunctionDeclarationName) \ |
| 84 | TYPE(FunctionDeclarationLParen) \ |
| 85 | TYPE(FunctionLBrace) \ |
| 86 | TYPE(FunctionLikeOrFreestandingMacro) \ |
| 87 | TYPE(FunctionTypeLParen) \ |
| 88 | /* The colons as part of a C11 _Generic selection */ \ |
| 89 | TYPE(GenericSelectionColon) \ |
| 90 | /* The colon at the end of a goto label. */ \ |
| 91 | TYPE(GotoLabelColon) \ |
| 92 | TYPE(IfMacro) \ |
| 93 | TYPE(ImplicitStringLiteral) \ |
| 94 | TYPE(InheritanceColon) \ |
| 95 | TYPE(InheritanceComma) \ |
| 96 | TYPE(InlineASMBrace) \ |
| 97 | TYPE(InlineASMColon) \ |
| 98 | TYPE(InlineASMSymbolicNameLSquare) \ |
| 99 | TYPE(JavaAnnotation) \ |
| 100 | TYPE(JsAndAndEqual) \ |
| 101 | TYPE(JsComputedPropertyName) \ |
| 102 | TYPE(JsExponentiation) \ |
| 103 | TYPE(JsExponentiationEqual) \ |
| 104 | TYPE(JsPipePipeEqual) \ |
| 105 | TYPE(JsPrivateIdentifier) \ |
| 106 | TYPE(JsTypeColon) \ |
| 107 | TYPE(JsTypeOperator) \ |
| 108 | TYPE(JsTypeOptionalQuestion) \ |
| 109 | TYPE(LambdaArrow) \ |
| 110 | TYPE(LambdaDefinitionLParen) \ |
| 111 | TYPE(LambdaLBrace) \ |
| 112 | TYPE(LambdaLSquare) \ |
| 113 | TYPE(LeadingJavaAnnotation) \ |
| 114 | TYPE(LineComment) \ |
| 115 | TYPE(MacroBlockBegin) \ |
| 116 | TYPE(MacroBlockEnd) \ |
| 117 | TYPE(ModulePartitionColon) \ |
| 118 | TYPE(NamespaceLBrace) \ |
| 119 | TYPE(NamespaceMacro) \ |
| 120 | TYPE(NamespaceRBrace) \ |
| 121 | TYPE(NonNullAssertion) \ |
| 122 | TYPE(NullCoalescingEqual) \ |
| 123 | TYPE(NullCoalescingOperator) \ |
| 124 | TYPE(NullPropagatingOperator) \ |
| 125 | TYPE(ObjCBlockLBrace) \ |
| 126 | TYPE(ObjCBlockLParen) \ |
| 127 | TYPE(ObjCDecl) \ |
| 128 | TYPE(ObjCForIn) \ |
| 129 | TYPE(ObjCMethodExpr) \ |
| 130 | TYPE(ObjCMethodSpecifier) \ |
| 131 | TYPE(ObjCProperty) \ |
| 132 | TYPE(ObjCStringLiteral) \ |
| 133 | TYPE(OverloadedOperator) \ |
| 134 | TYPE(OverloadedOperatorLParen) \ |
| 135 | TYPE(PointerOrReference) \ |
| 136 | TYPE(ProtoExtensionLSquare) \ |
| 137 | TYPE(PureVirtualSpecifier) \ |
| 138 | TYPE(RangeBasedForLoopColon) \ |
| 139 | TYPE(RecordLBrace) \ |
| 140 | TYPE(RecordRBrace) \ |
| 141 | TYPE(RegexLiteral) \ |
| 142 | TYPE(RequiresClause) \ |
| 143 | TYPE(RequiresClauseInARequiresExpression) \ |
| 144 | TYPE(RequiresExpression) \ |
| 145 | TYPE(RequiresExpressionLBrace) \ |
| 146 | TYPE(RequiresExpressionLParen) \ |
| 147 | TYPE(SelectorName) \ |
| 148 | TYPE(StartOfName) \ |
| 149 | TYPE(StatementAttributeLikeMacro) \ |
| 150 | TYPE(StatementMacro) \ |
| 151 | /* A string that is part of a string concatenation. For C#, JavaScript, and \ |
| 152 | * Java, it is used for marking whether a string needs parentheses around it \ |
| 153 | * if it is to be split into parts joined by `+`. For Verilog, whether \ |
| 154 | * braces need to be added to split it. Not used for other languages. */ \ |
| 155 | TYPE(StringInConcatenation) \ |
| 156 | TYPE(StructLBrace) \ |
| 157 | TYPE(StructRBrace) \ |
| 158 | TYPE(StructuredBindingLSquare) \ |
| 159 | TYPE(SwitchExpressionLabel) \ |
| 160 | TYPE(SwitchExpressionLBrace) \ |
| 161 | TYPE(TableGenBangOperator) \ |
| 162 | TYPE(TableGenCondOperator) \ |
| 163 | TYPE(TableGenCondOperatorColon) \ |
| 164 | TYPE(TableGenCondOperatorComma) \ |
| 165 | TYPE(TableGenDAGArgCloser) \ |
| 166 | TYPE(TableGenDAGArgListColon) \ |
| 167 | TYPE(TableGenDAGArgListColonToAlign) \ |
| 168 | TYPE(TableGenDAGArgListComma) \ |
| 169 | TYPE(TableGenDAGArgListCommaToBreak) \ |
| 170 | TYPE(TableGenDAGArgOpener) \ |
| 171 | TYPE(TableGenDAGArgOpenerToBreak) \ |
| 172 | TYPE(TableGenDAGArgOperatorID) \ |
| 173 | TYPE(TableGenDAGArgOperatorToBreak) \ |
| 174 | TYPE(TableGenListCloser) \ |
| 175 | TYPE(TableGenListOpener) \ |
| 176 | TYPE(TableGenMultiLineString) \ |
| 177 | TYPE(TableGenTrailingPasteOperator) \ |
| 178 | TYPE(TableGenValueSuffix) \ |
| 179 | TYPE(TemplateCloser) \ |
| 180 | TYPE(TemplateOpener) \ |
| 181 | TYPE(TemplateString) \ |
| 182 | TYPE(TrailingAnnotation) \ |
| 183 | TYPE(TrailingReturnArrow) \ |
| 184 | TYPE(TrailingUnaryOperator) \ |
| 185 | TYPE(TypeDeclarationParen) \ |
| 186 | TYPE(TemplateName) \ |
| 187 | TYPE(TypeName) \ |
| 188 | TYPE(TypenameMacro) \ |
| 189 | TYPE(UnaryOperator) \ |
| 190 | TYPE(UnionLBrace) \ |
| 191 | TYPE(UnionRBrace) \ |
| 192 | TYPE(UntouchableMacroFunc) \ |
| 193 | TYPE(VariableTemplate) \ |
| 194 | /* Like in 'assign x = 0, y = 1;' . */ \ |
| 195 | TYPE(VerilogAssignComma) \ |
| 196 | /* like in begin : block */ \ |
| 197 | TYPE(VerilogBlockLabelColon) \ |
| 198 | /* The square bracket for the dimension part of the type name. \ |
| 199 | * In 'logic [1:0] x[1:0]', only the first '['. This way we can have space \ |
| 200 | * before the first bracket but not the second. */ \ |
| 201 | TYPE(VerilogDimensionedTypeName) \ |
| 202 | /* list of port connections or parameters in a module instantiation */ \ |
| 203 | TYPE(VerilogInstancePortComma) \ |
| 204 | TYPE(VerilogInstancePortLParen) \ |
| 205 | /* A parenthesized list within which line breaks are inserted by the \ |
| 206 | * formatter, for example the list of ports in a module header. */ \ |
| 207 | TYPE(VerilogMultiLineListLParen) \ |
| 208 | /* for the base in a number literal, not including the quote */ \ |
| 209 | TYPE(VerilogNumberBase) \ |
| 210 | /* like `(strong1, pull0)` */ \ |
| 211 | TYPE(VerilogStrength) \ |
| 212 | /* Things inside the table in user-defined primitives. */ \ |
| 213 | TYPE(VerilogTableItem) \ |
| 214 | /* those that separate ports of different types */ \ |
| 215 | TYPE(VerilogTypeComma) \ |
| 216 | TYPE(Unknown) |
| 217 | |
| 218 | /// Determines the semantic type of a syntactic token, e.g. whether "<" is a |
| 219 | /// template opener or binary operator. |
| 220 | enum TokenType : uint8_t { |
| 221 | #define TYPE(X) TT_##X, |
| 222 | LIST_TOKEN_TYPES |
| 223 | #undef TYPE |
| 224 | NUM_TOKEN_TYPES |
| 225 | }; |
| 226 | |
| 227 | /// Determines the name of a token type. |
| 228 | const char *getTokenTypeName(TokenType Type); |
| 229 | |
| 230 | // Represents what type of block a set of braces open. |
| 231 | enum BraceBlockKind { BK_Unknown, BK_Block, BK_BracedInit }; |
| 232 | |
| 233 | // The packing kind of a function's parameters. |
| 234 | enum ParameterPackingKind { PPK_BinPacked, PPK_OnePerLine, PPK_Inconclusive }; |
| 235 | |
| 236 | enum FormatDecision { FD_Unformatted, FD_Continue, FD_Break }; |
| 237 | |
| 238 | /// Roles a token can take in a configured macro expansion. |
| 239 | enum MacroRole { |
| 240 | /// The token was expanded from a macro argument when formatting the expanded |
| 241 | /// token sequence. |
| 242 | MR_ExpandedArg, |
| 243 | /// The token is part of a macro argument that was previously formatted as |
| 244 | /// expansion when formatting the unexpanded macro call. |
| 245 | MR_UnexpandedArg, |
| 246 | /// The token was expanded from a macro definition, and is not visible as part |
| 247 | /// of the macro call. |
| 248 | MR_Hidden, |
| 249 | }; |
| 250 | |
| 251 | struct FormatToken; |
| 252 | |
| 253 | /// Contains information on the token's role in a macro expansion. |
| 254 | /// |
| 255 | /// Given the following definitions: |
| 256 | /// A(X) = [ X ] |
| 257 | /// B(X) = < X > |
| 258 | /// C(X) = X |
| 259 | /// |
| 260 | /// Consider the macro call: |
| 261 | /// A({B(C(C(x)))}) -> [{<x>}] |
| 262 | /// |
| 263 | /// In this case, the tokens of the unexpanded macro call will have the |
| 264 | /// following relevant entries in their macro context (note that formatting |
| 265 | /// the unexpanded macro call happens *after* formatting the expanded macro |
| 266 | /// call): |
| 267 | /// A( { B( C( C(x) ) ) } ) |
| 268 | /// Role: NN U NN NN NNUN N N U N (N=None, U=UnexpandedArg) |
| 269 | /// |
| 270 | /// [ { < x > } ] |
| 271 | /// Role: H E H E H E H (H=Hidden, E=ExpandedArg) |
| 272 | /// ExpandedFrom[0]: A A A A A A A |
| 273 | /// ExpandedFrom[1]: B B B |
| 274 | /// ExpandedFrom[2]: C |
| 275 | /// ExpandedFrom[3]: C |
| 276 | /// StartOfExpansion: 1 0 1 2 0 0 0 |
| 277 | /// EndOfExpansion: 0 0 0 2 1 0 1 |
| 278 | struct MacroExpansion { |
| 279 | MacroExpansion(MacroRole Role) : Role(Role) {} |
| 280 | |
| 281 | /// The token's role in the macro expansion. |
| 282 | /// When formatting an expanded macro, all tokens that are part of macro |
| 283 | /// arguments will be MR_ExpandedArg, while all tokens that are not visible in |
| 284 | /// the macro call will be MR_Hidden. |
| 285 | /// When formatting an unexpanded macro call, all tokens that are part of |
| 286 | /// macro arguments will be MR_UnexpandedArg. |
| 287 | MacroRole Role; |
| 288 | |
| 289 | /// The stack of macro call identifier tokens this token was expanded from. |
| 290 | llvm::SmallVector<FormatToken *, 1> ExpandedFrom; |
| 291 | |
| 292 | /// The number of expansions of which this macro is the first entry. |
| 293 | unsigned StartOfExpansion = 0; |
| 294 | |
| 295 | /// The number of currently open expansions in \c ExpandedFrom this macro is |
| 296 | /// the last token in. |
| 297 | unsigned EndOfExpansion = 0; |
| 298 | }; |
| 299 | |
| 300 | class TokenRole; |
| 301 | class AnnotatedLine; |
| 302 | |
| 303 | /// A wrapper around a \c Token storing information about the |
| 304 | /// whitespace characters preceding it. |
| 305 | struct FormatToken { |
| 306 | FormatToken() |
| 307 | : HasUnescapedNewline(false), IsMultiline(false), IsFirst(false), |
| 308 | MustBreakBefore(false), MustBreakBeforeFinalized(false), |
| 309 | IsUnterminatedLiteral(false), CanBreakBefore(false), |
| 310 | ClosesTemplateDeclaration(false), StartsBinaryExpression(false), |
| 311 | EndsBinaryExpression(false), PartOfMultiVariableDeclStmt(false), |
| 312 | ContinuesLineCommentSection(false), Finalized(false), |
| 313 | ClosesRequiresClause(false), EndsCppAttributeGroup(false), |
| 314 | BlockKind(BK_Unknown), Decision(FD_Unformatted), |
| 315 | PackingKind(PPK_Inconclusive), TypeIsFinalized(false), |
| 316 | Type(TT_Unknown) {} |
| 317 | |
| 318 | /// The \c Token. |
| 319 | Token Tok; |
| 320 | |
| 321 | /// The raw text of the token. |
| 322 | /// |
| 323 | /// Contains the raw token text without leading whitespace and without leading |
| 324 | /// escaped newlines. |
| 325 | StringRef TokenText; |
| 326 | |
| 327 | /// A token can have a special role that can carry extra information |
| 328 | /// about the token's formatting. |
| 329 | /// FIXME: Make FormatToken for parsing and AnnotatedToken two different |
| 330 | /// classes and make this a unique_ptr in the AnnotatedToken class. |
| 331 | std::shared_ptr<TokenRole> Role; |
| 332 | |
| 333 | /// The range of the whitespace immediately preceding the \c Token. |
| 334 | SourceRange WhitespaceRange; |
| 335 | |
| 336 | /// Whether there is at least one unescaped newline before the \c |
| 337 | /// Token. |
| 338 | unsigned HasUnescapedNewline : 1; |
| 339 | |
| 340 | /// Whether the token text contains newlines (escaped or not). |
| 341 | unsigned IsMultiline : 1; |
| 342 | |
| 343 | /// Indicates that this is the first token of the file. |
| 344 | unsigned IsFirst : 1; |
| 345 | |
| 346 | /// Whether there must be a line break before this token. |
| 347 | /// |
| 348 | /// This happens for example when a preprocessor directive ended directly |
| 349 | /// before the token. |
| 350 | unsigned MustBreakBefore : 1; |
| 351 | |
| 352 | /// Whether MustBreakBefore is finalized during parsing and must not |
| 353 | /// be reset between runs. |
| 354 | unsigned MustBreakBeforeFinalized : 1; |
| 355 | |
| 356 | /// Set to \c true if this token is an unterminated literal. |
| 357 | unsigned IsUnterminatedLiteral : 1; |
| 358 | |
| 359 | /// \c true if it is allowed to break before this token. |
| 360 | unsigned CanBreakBefore : 1; |
| 361 | |
| 362 | /// \c true if this is the ">" of "template<..>". |
| 363 | unsigned ClosesTemplateDeclaration : 1; |
| 364 | |
| 365 | /// \c true if this token starts a binary expression, i.e. has at least |
| 366 | /// one fake l_paren with a precedence greater than prec::Unknown. |
| 367 | unsigned StartsBinaryExpression : 1; |
| 368 | /// \c true if this token ends a binary expression. |
| 369 | unsigned EndsBinaryExpression : 1; |
| 370 | |
| 371 | /// Is this token part of a \c DeclStmt defining multiple variables? |
| 372 | /// |
| 373 | /// Only set if \c Type == \c TT_StartOfName. |
| 374 | unsigned PartOfMultiVariableDeclStmt : 1; |
| 375 | |
| 376 | /// Does this line comment continue a line comment section? |
| 377 | /// |
| 378 | /// Only set to true if \c Type == \c TT_LineComment. |
| 379 | unsigned : 1; |
| 380 | |
| 381 | /// If \c true, this token has been fully formatted (indented and |
| 382 | /// potentially re-formatted inside), and we do not allow further formatting |
| 383 | /// changes. |
| 384 | unsigned Finalized : 1; |
| 385 | |
| 386 | /// \c true if this is the last token within requires clause. |
| 387 | unsigned ClosesRequiresClause : 1; |
| 388 | |
| 389 | /// \c true if this token ends a group of C++ attributes. |
| 390 | unsigned EndsCppAttributeGroup : 1; |
| 391 | |
| 392 | private: |
| 393 | /// Contains the kind of block if this token is a brace. |
| 394 | unsigned BlockKind : 2; |
| 395 | |
| 396 | public: |
| 397 | BraceBlockKind getBlockKind() const { |
| 398 | return static_cast<BraceBlockKind>(BlockKind); |
| 399 | } |
| 400 | void setBlockKind(BraceBlockKind BBK) { |
| 401 | BlockKind = BBK; |
| 402 | assert(getBlockKind() == BBK && "BraceBlockKind overflow!" ); |
| 403 | } |
| 404 | |
| 405 | private: |
| 406 | /// Stores the formatting decision for the token once it was made. |
| 407 | unsigned Decision : 2; |
| 408 | |
| 409 | public: |
| 410 | FormatDecision getDecision() const { |
| 411 | return static_cast<FormatDecision>(Decision); |
| 412 | } |
| 413 | void setDecision(FormatDecision D) { |
| 414 | Decision = D; |
| 415 | assert(getDecision() == D && "FormatDecision overflow!" ); |
| 416 | } |
| 417 | |
| 418 | private: |
| 419 | /// If this is an opening parenthesis, how are the parameters packed? |
| 420 | unsigned PackingKind : 2; |
| 421 | |
| 422 | public: |
| 423 | ParameterPackingKind getPackingKind() const { |
| 424 | return static_cast<ParameterPackingKind>(PackingKind); |
| 425 | } |
| 426 | void setPackingKind(ParameterPackingKind K) { |
| 427 | PackingKind = K; |
| 428 | assert(getPackingKind() == K && "ParameterPackingKind overflow!" ); |
| 429 | } |
| 430 | |
| 431 | private: |
| 432 | unsigned TypeIsFinalized : 1; |
| 433 | TokenType Type; |
| 434 | |
| 435 | public: |
| 436 | /// Returns the token's type, e.g. whether "<" is a template opener or |
| 437 | /// binary operator. |
| 438 | TokenType getType() const { return Type; } |
| 439 | void setType(TokenType T) { |
| 440 | // If this token is a macro argument while formatting an unexpanded macro |
| 441 | // call, we do not change its type any more - the type was deduced from |
| 442 | // formatting the expanded macro stream already. |
| 443 | if (MacroCtx && MacroCtx->Role == MR_UnexpandedArg) |
| 444 | return; |
| 445 | assert((!TypeIsFinalized || T == Type) && |
| 446 | "Please use overwriteFixedType to change a fixed type." ); |
| 447 | Type = T; |
| 448 | } |
| 449 | /// Sets the type and also the finalized flag. This prevents the type to be |
| 450 | /// reset in TokenAnnotator::resetTokenMetadata(). If the type needs to be set |
| 451 | /// to another one please use overwriteFixedType, or even better remove the |
| 452 | /// need to reassign the type. |
| 453 | void setFinalizedType(TokenType T) { |
| 454 | if (MacroCtx && MacroCtx->Role == MR_UnexpandedArg) |
| 455 | return; |
| 456 | Type = T; |
| 457 | TypeIsFinalized = true; |
| 458 | } |
| 459 | void overwriteFixedType(TokenType T) { |
| 460 | if (MacroCtx && MacroCtx->Role == MR_UnexpandedArg) |
| 461 | return; |
| 462 | TypeIsFinalized = false; |
| 463 | setType(T); |
| 464 | } |
| 465 | bool isTypeFinalized() const { return TypeIsFinalized; } |
| 466 | |
| 467 | /// Used to set an operator precedence explicitly. |
| 468 | prec::Level ForcedPrecedence = prec::Unknown; |
| 469 | |
| 470 | /// The number of newlines immediately before the \c Token. |
| 471 | /// |
| 472 | /// This can be used to determine what the user wrote in the original code |
| 473 | /// and thereby e.g. leave an empty line between two function definitions. |
| 474 | unsigned NewlinesBefore = 0; |
| 475 | |
| 476 | /// The number of newlines immediately before the \c Token after formatting. |
| 477 | /// |
| 478 | /// This is used to avoid overlapping whitespace replacements when \c Newlines |
| 479 | /// is recomputed for a finalized preprocessor branching directive. |
| 480 | int Newlines = -1; |
| 481 | |
| 482 | /// The offset just past the last '\n' in this token's leading |
| 483 | /// whitespace (relative to \c WhiteSpaceStart). 0 if there is no '\n'. |
| 484 | unsigned LastNewlineOffset = 0; |
| 485 | |
| 486 | /// The width of the non-whitespace parts of the token (or its first |
| 487 | /// line for multi-line tokens) in columns. |
| 488 | /// We need this to correctly measure number of columns a token spans. |
| 489 | unsigned ColumnWidth = 0; |
| 490 | |
| 491 | /// Contains the width in columns of the last line of a multi-line |
| 492 | /// token. |
| 493 | unsigned LastLineColumnWidth = 0; |
| 494 | |
| 495 | /// The number of spaces that should be inserted before this token. |
| 496 | unsigned SpacesRequiredBefore = 0; |
| 497 | |
| 498 | /// Number of parameters, if this is "(", "[" or "<". |
| 499 | unsigned ParameterCount = 0; |
| 500 | |
| 501 | /// Number of parameters that are nested blocks, |
| 502 | /// if this is "(", "[" or "<". |
| 503 | unsigned BlockParameterCount = 0; |
| 504 | |
| 505 | /// If this is a bracket ("<", "(", "[" or "{"), contains the kind of |
| 506 | /// the surrounding bracket. |
| 507 | tok::TokenKind ParentBracket = tok::unknown; |
| 508 | |
| 509 | /// The total length of the unwrapped line up to and including this |
| 510 | /// token. |
| 511 | unsigned TotalLength = 0; |
| 512 | |
| 513 | /// The original 0-based column of this token, including expanded tabs. |
| 514 | /// The configured TabWidth is used as tab width. |
| 515 | unsigned OriginalColumn = 0; |
| 516 | |
| 517 | /// The length of following tokens until the next natural split point, |
| 518 | /// or the next token that can be broken. |
| 519 | unsigned UnbreakableTailLength = 0; |
| 520 | |
| 521 | // FIXME: Come up with a 'cleaner' concept. |
| 522 | /// The binding strength of a token. This is a combined value of |
| 523 | /// operator precedence, parenthesis nesting, etc. |
| 524 | unsigned BindingStrength = 0; |
| 525 | |
| 526 | /// The nesting level of this token, i.e. the number of surrounding (), |
| 527 | /// [], {} or <>. |
| 528 | unsigned NestingLevel = 0; |
| 529 | |
| 530 | /// The indent level of this token. Copied from the surrounding line. |
| 531 | unsigned IndentLevel = 0; |
| 532 | |
| 533 | /// Penalty for inserting a line break before this token. |
| 534 | unsigned SplitPenalty = 0; |
| 535 | |
| 536 | /// If this is the first ObjC selector name in an ObjC method |
| 537 | /// definition or call, this contains the length of the longest name. |
| 538 | /// |
| 539 | /// This being set to 0 means that the selectors should not be colon-aligned, |
| 540 | /// e.g. because several of them are block-type. |
| 541 | unsigned LongestObjCSelectorName = 0; |
| 542 | |
| 543 | /// If this is the first ObjC selector name in an ObjC method |
| 544 | /// definition or call, this contains the number of parts that the whole |
| 545 | /// selector consist of. |
| 546 | unsigned ObjCSelectorNameParts = 0; |
| 547 | |
| 548 | /// The 0-based index of the parameter/argument. For ObjC it is set |
| 549 | /// for the selector name token. |
| 550 | /// For now calculated only for ObjC. |
| 551 | unsigned ParameterIndex = 0; |
| 552 | |
| 553 | /// Stores the number of required fake parentheses and the |
| 554 | /// corresponding operator precedence. |
| 555 | /// |
| 556 | /// If multiple fake parentheses start at a token, this vector stores them in |
| 557 | /// reverse order, i.e. inner fake parenthesis first. |
| 558 | SmallVector<prec::Level, 4> FakeLParens; |
| 559 | /// Insert this many fake ) after this token for correct indentation. |
| 560 | unsigned FakeRParens = 0; |
| 561 | |
| 562 | /// If this is an operator (or "."/"->") in a sequence of operators |
| 563 | /// with the same precedence, contains the 0-based operator index. |
| 564 | unsigned OperatorIndex = 0; |
| 565 | |
| 566 | /// If this is an operator (or "."/"->") in a sequence of operators |
| 567 | /// with the same precedence, points to the next operator. |
| 568 | FormatToken *NextOperator = nullptr; |
| 569 | |
| 570 | /// If this is a bracket, this points to the matching one. |
| 571 | FormatToken *MatchingParen = nullptr; |
| 572 | |
| 573 | /// The previous token in the unwrapped line. |
| 574 | FormatToken *Previous = nullptr; |
| 575 | |
| 576 | /// The next token in the unwrapped line. |
| 577 | FormatToken *Next = nullptr; |
| 578 | |
| 579 | /// The first token in set of column elements. |
| 580 | bool StartsColumn = false; |
| 581 | |
| 582 | /// This notes the start of the line of an array initializer. |
| 583 | bool ArrayInitializerLineStart = false; |
| 584 | |
| 585 | /// This starts an array initializer. |
| 586 | bool IsArrayInitializer = false; |
| 587 | |
| 588 | /// Is optional and can be removed. |
| 589 | bool Optional = false; |
| 590 | |
| 591 | /// Might be function declaration open/closing paren. |
| 592 | bool MightBeFunctionDeclParen = false; |
| 593 | |
| 594 | /// Has "\n\f\n" or "\n\f\r\n" before TokenText. |
| 595 | bool HasFormFeedBefore = false; |
| 596 | |
| 597 | /// Is the first token after a preprocessor line. |
| 598 | bool FirstAfterPPLine = false; |
| 599 | |
| 600 | /// Number of optional braces to be inserted after this token: |
| 601 | /// -1: a single left brace |
| 602 | /// 0: no braces |
| 603 | /// >0: number of right braces |
| 604 | int8_t BraceCount = 0; |
| 605 | |
| 606 | /// If this token starts a block, this contains all the unwrapped lines |
| 607 | /// in it. |
| 608 | SmallVector<AnnotatedLine *, 1> Children; |
| 609 | |
| 610 | // Contains all attributes related to how this token takes part |
| 611 | // in a configured macro expansion. |
| 612 | std::optional<MacroExpansion> MacroCtx; |
| 613 | |
| 614 | /// When macro expansion introduces nodes with children, those are marked as |
| 615 | /// \c MacroParent. |
| 616 | /// FIXME: The formatting code currently hard-codes the assumption that |
| 617 | /// child nodes are introduced by blocks following an opening brace. |
| 618 | /// This is deeply baked into the code and disentangling this will require |
| 619 | /// signficant refactorings. \c MacroParent allows us to special-case the |
| 620 | /// cases in which we treat parents as block-openers for now. |
| 621 | bool MacroParent = false; |
| 622 | |
| 623 | bool is(tok::TokenKind Kind) const { return Tok.is(K: Kind); } |
| 624 | bool is(tok::ObjCKeywordKind Kind) const { |
| 625 | return Tok.getObjCKeywordID() == Kind; |
| 626 | } |
| 627 | bool is(TokenType TT) const { return getType() == TT; } |
| 628 | bool is(const IdentifierInfo *II) const { |
| 629 | return II && II == Tok.getIdentifierInfo(); |
| 630 | } |
| 631 | bool is(tok::PPKeywordKind Kind) const { |
| 632 | return Tok.getIdentifierInfo() && |
| 633 | Tok.getIdentifierInfo()->getPPKeywordID() == Kind; |
| 634 | } |
| 635 | bool is(BraceBlockKind BBK) const { return getBlockKind() == BBK; } |
| 636 | bool is(ParameterPackingKind PPK) const { return getPackingKind() == PPK; } |
| 637 | |
| 638 | template <typename A, typename B> bool isOneOf(A K1, B K2) const { |
| 639 | return is(K1) || is(K2); |
| 640 | } |
| 641 | template <typename A, typename B, typename... Ts> |
| 642 | bool isOneOf(A K1, B K2, Ts... Ks) const { |
| 643 | return is(K1) || isOneOf(K2, Ks...); |
| 644 | } |
| 645 | template <typename T> bool isNot(T Kind) const { return !is(Kind); } |
| 646 | |
| 647 | bool isIf(bool AllowConstexprMacro = true) const { |
| 648 | return is(Kind: tok::kw_if) || endsSequence(K1: tok::kw_constexpr, Tokens: tok::kw_if) || |
| 649 | (endsSequence(K1: tok::identifier, Tokens: tok::kw_if) && AllowConstexprMacro); |
| 650 | } |
| 651 | |
| 652 | bool closesScopeAfterBlock() const { |
| 653 | if (getBlockKind() == BK_Block) |
| 654 | return true; |
| 655 | if (closesScope()) |
| 656 | return Previous->closesScopeAfterBlock(); |
| 657 | return false; |
| 658 | } |
| 659 | |
| 660 | /// \c true if this token starts a sequence with the given tokens in order, |
| 661 | /// following the ``Next`` pointers, ignoring comments. |
| 662 | template <typename A, typename... Ts> |
| 663 | bool startsSequence(A K1, Ts... Tokens) const { |
| 664 | return startsSequenceInternal(K1, Tokens...); |
| 665 | } |
| 666 | |
| 667 | /// \c true if this token ends a sequence with the given tokens in order, |
| 668 | /// following the ``Previous`` pointers, ignoring comments. |
| 669 | /// For example, given tokens [T1, T2, T3], the function returns true if |
| 670 | /// 3 tokens ending at this (ignoring comments) are [T3, T2, T1]. In other |
| 671 | /// words, the tokens passed to this function need to the reverse of the |
| 672 | /// order the tokens appear in code. |
| 673 | template <typename A, typename... Ts> |
| 674 | bool endsSequence(A K1, Ts... Tokens) const { |
| 675 | return endsSequenceInternal(K1, Tokens...); |
| 676 | } |
| 677 | |
| 678 | bool isStringLiteral() const { return tok::isStringLiteral(K: Tok.getKind()); } |
| 679 | |
| 680 | bool isAttribute() const { |
| 681 | return isOneOf(K1: tok::kw___attribute, K2: tok::kw___declspec, Ks: TT_AttributeMacro); |
| 682 | } |
| 683 | |
| 684 | bool isAccessSpecifierKeyword() const { |
| 685 | return isOneOf(K1: tok::kw_public, K2: tok::kw_protected, Ks: tok::kw_private); |
| 686 | } |
| 687 | |
| 688 | bool isAccessSpecifier(bool ColonRequired = true) const { |
| 689 | if (!isAccessSpecifierKeyword()) |
| 690 | return false; |
| 691 | if (!ColonRequired) |
| 692 | return true; |
| 693 | const auto * = getNextNonComment(); |
| 694 | return NextNonComment && NextNonComment->is(Kind: tok::colon); |
| 695 | } |
| 696 | |
| 697 | bool canBePointerOrReferenceQualifier() const { |
| 698 | return isOneOf(K1: tok::kw_const, K2: tok::kw_restrict, Ks: tok::kw_volatile, |
| 699 | Ks: tok::kw__Nonnull, Ks: tok::kw__Nullable, |
| 700 | Ks: tok::kw__Null_unspecified, Ks: tok::kw___ptr32, Ks: tok::kw___ptr64, |
| 701 | Ks: tok::kw___funcref) || |
| 702 | isAttribute(); |
| 703 | } |
| 704 | |
| 705 | [[nodiscard]] bool isTypeName(const LangOptions &LangOpts) const; |
| 706 | [[nodiscard]] bool isTypeOrIdentifier(const LangOptions &LangOpts) const; |
| 707 | |
| 708 | bool isObjCAccessSpecifier() const { |
| 709 | return is(Kind: tok::at) && Next && |
| 710 | Next->isOneOf(K1: tok::objc_public, K2: tok::objc_protected, |
| 711 | Ks: tok::objc_package, Ks: tok::objc_private); |
| 712 | } |
| 713 | |
| 714 | bool isObjCLifetimeQualifier(const FormatStyle &Style) const { |
| 715 | if (Style.Language != FormatStyle::LK_ObjC || isNot(Kind: tok::identifier) || |
| 716 | !TokenText.starts_with(Prefix: "__" )) { |
| 717 | return false; |
| 718 | } |
| 719 | const auto Qualifier = TokenText.substr(Start: 2); |
| 720 | return Qualifier == "autoreleasing" || Qualifier == "strong" || |
| 721 | Qualifier == "weak" || Qualifier == "unsafe_unretained" ; |
| 722 | } |
| 723 | |
| 724 | /// Returns whether \p Tok is ([{ or an opening < of a template or in |
| 725 | /// protos. |
| 726 | bool opensScope() const { |
| 727 | if (is(TT: TT_TemplateString) && TokenText.ends_with(Suffix: "${" )) |
| 728 | return true; |
| 729 | if (is(TT: TT_DictLiteral) && is(Kind: tok::less)) |
| 730 | return true; |
| 731 | return isOneOf(K1: tok::l_paren, K2: tok::l_brace, Ks: tok::l_square, |
| 732 | Ks: TT_TemplateOpener); |
| 733 | } |
| 734 | /// Returns whether \p Tok is )]} or a closing > of a template or in |
| 735 | /// protos. |
| 736 | bool closesScope() const { |
| 737 | if (is(TT: TT_TemplateString) && TokenText.starts_with(Prefix: "}" )) |
| 738 | return true; |
| 739 | if (is(TT: TT_DictLiteral) && is(Kind: tok::greater)) |
| 740 | return true; |
| 741 | return isOneOf(K1: tok::r_paren, K2: tok::r_brace, Ks: tok::r_square, |
| 742 | Ks: TT_TemplateCloser); |
| 743 | } |
| 744 | |
| 745 | /// Returns \c true if this is a "." or "->" accessing a member. |
| 746 | bool isMemberAccess() const { |
| 747 | return isOneOf(K1: tok::arrow, K2: tok::period, Ks: tok::arrowstar) && |
| 748 | !isOneOf(K1: TT_DesignatedInitializerPeriod, K2: TT_TrailingReturnArrow, |
| 749 | Ks: TT_LambdaArrow, Ks: TT_LeadingJavaAnnotation); |
| 750 | } |
| 751 | |
| 752 | bool isPointerOrReference() const { |
| 753 | return isOneOf(K1: tok::star, K2: tok::amp, Ks: tok::ampamp); |
| 754 | } |
| 755 | |
| 756 | bool isPlacementOperator() const { |
| 757 | return isOneOf(K1: tok::kw_new, K2: tok::kw_delete); |
| 758 | } |
| 759 | |
| 760 | bool isUnaryOperator() const { |
| 761 | switch (Tok.getKind()) { |
| 762 | case tok::plus: |
| 763 | case tok::plusplus: |
| 764 | case tok::minus: |
| 765 | case tok::minusminus: |
| 766 | case tok::exclaim: |
| 767 | case tok::tilde: |
| 768 | case tok::kw_sizeof: |
| 769 | case tok::kw_alignof: |
| 770 | return true; |
| 771 | default: |
| 772 | return false; |
| 773 | } |
| 774 | } |
| 775 | |
| 776 | bool isBinaryOperator() const { |
| 777 | // Comma is a binary operator, but does not behave as such wrt. formatting. |
| 778 | return getPrecedence() > prec::Comma; |
| 779 | } |
| 780 | |
| 781 | bool () const { |
| 782 | return is(Kind: tok::comment) && |
| 783 | (is(TT: TT_LineComment) || !Next || Next->NewlinesBefore > 0); |
| 784 | } |
| 785 | |
| 786 | /// Returns \c true if this is a keyword that can be used |
| 787 | /// like a function call (e.g. sizeof, typeid, ...). |
| 788 | bool isFunctionLikeKeyword() const { |
| 789 | if (isAttribute()) |
| 790 | return true; |
| 791 | |
| 792 | return isOneOf(K1: tok::kw_throw, K2: tok::kw_typeid, Ks: tok::kw_return, |
| 793 | Ks: tok::kw_sizeof, Ks: tok::kw_alignof, Ks: tok::kw_alignas, |
| 794 | Ks: tok::kw_decltype, Ks: tok::kw_noexcept, Ks: tok::kw_static_assert, |
| 795 | Ks: tok::kw__Atomic, |
| 796 | #define TRANSFORM_TYPE_TRAIT_DEF(_, Trait) tok::kw___##Trait, |
| 797 | #include "clang/Basic/TransformTypeTraits.def" |
| 798 | Ks: tok::kw_requires); |
| 799 | } |
| 800 | |
| 801 | /// Returns \c true if this is a string literal that's like a label, |
| 802 | /// e.g. ends with "=" or ":". |
| 803 | bool isLabelString() const { |
| 804 | if (isNot(Kind: tok::string_literal)) |
| 805 | return false; |
| 806 | StringRef Content = TokenText; |
| 807 | if (Content.starts_with(Prefix: "\"" ) || Content.starts_with(Prefix: "'" )) |
| 808 | Content = Content.drop_front(N: 1); |
| 809 | if (Content.ends_with(Suffix: "\"" ) || Content.ends_with(Suffix: "'" )) |
| 810 | Content = Content.drop_back(N: 1); |
| 811 | Content = Content.trim(); |
| 812 | return Content.size() > 1 && |
| 813 | (Content.back() == ':' || Content.back() == '='); |
| 814 | } |
| 815 | |
| 816 | /// Returns actual token start location without leading escaped |
| 817 | /// newlines and whitespace. |
| 818 | /// |
| 819 | /// This can be different to Tok.getLocation(), which includes leading escaped |
| 820 | /// newlines. |
| 821 | SourceLocation getStartOfNonWhitespace() const { |
| 822 | return WhitespaceRange.getEnd(); |
| 823 | } |
| 824 | |
| 825 | /// Returns \c true if the range of whitespace immediately preceding the \c |
| 826 | /// Token is not empty. |
| 827 | bool hasWhitespaceBefore() const { |
| 828 | return WhitespaceRange.getBegin() != WhitespaceRange.getEnd(); |
| 829 | } |
| 830 | |
| 831 | prec::Level getPrecedence() const { |
| 832 | if (ForcedPrecedence != prec::Unknown) |
| 833 | return ForcedPrecedence; |
| 834 | return getBinOpPrecedence(Kind: Tok.getKind(), /*GreaterThanIsOperator=*/GreaterThanIsOperator: true, |
| 835 | /*CPlusPlus11=*/CPlusPlus11: true); |
| 836 | } |
| 837 | |
| 838 | /// Returns the previous token ignoring comments. |
| 839 | [[nodiscard]] FormatToken *() const { |
| 840 | FormatToken *Tok = Previous; |
| 841 | while (Tok && Tok->is(Kind: tok::comment)) |
| 842 | Tok = Tok->Previous; |
| 843 | return Tok; |
| 844 | } |
| 845 | |
| 846 | /// Returns the next token ignoring comments. |
| 847 | [[nodiscard]] FormatToken *() const { |
| 848 | FormatToken *Tok = Next; |
| 849 | while (Tok && Tok->is(Kind: tok::comment)) |
| 850 | Tok = Tok->Next; |
| 851 | return Tok; |
| 852 | } |
| 853 | |
| 854 | /// Returns \c true if this token ends a block indented initializer list. |
| 855 | [[nodiscard]] bool isBlockIndentedInitRBrace(const FormatStyle &Style) const; |
| 856 | |
| 857 | /// Returns \c true if this tokens starts a block-type list, i.e. a |
| 858 | /// list that should be indented with a block indent. |
| 859 | [[nodiscard]] bool opensBlockOrBlockTypeList(const FormatStyle &Style) const; |
| 860 | |
| 861 | /// Returns whether the token is the left square bracket of a C++ |
| 862 | /// structured binding declaration. |
| 863 | bool isCppStructuredBinding(bool IsCpp) const { |
| 864 | if (!IsCpp || isNot(Kind: tok::l_square)) |
| 865 | return false; |
| 866 | const FormatToken *T = this; |
| 867 | do { |
| 868 | T = T->getPreviousNonComment(); |
| 869 | } while (T && T->isOneOf(K1: tok::kw_const, K2: tok::kw_volatile, Ks: tok::amp, |
| 870 | Ks: tok::ampamp)); |
| 871 | return T && T->is(Kind: tok::kw_auto); |
| 872 | } |
| 873 | |
| 874 | /// Same as opensBlockOrBlockTypeList, but for the closing token. |
| 875 | bool closesBlockOrBlockTypeList(const FormatStyle &Style) const { |
| 876 | if (is(TT: TT_TemplateString) && closesScope()) |
| 877 | return true; |
| 878 | return MatchingParen && MatchingParen->opensBlockOrBlockTypeList(Style); |
| 879 | } |
| 880 | |
| 881 | /// Return the actual namespace token, if this token starts a namespace |
| 882 | /// block. |
| 883 | const FormatToken *getNamespaceToken() const { |
| 884 | const FormatToken *NamespaceTok = this; |
| 885 | if (is(Kind: tok::comment)) |
| 886 | NamespaceTok = NamespaceTok->getNextNonComment(); |
| 887 | // Detect "(inline|export)? namespace" in the beginning of a line. |
| 888 | if (NamespaceTok && NamespaceTok->isOneOf(K1: tok::kw_inline, K2: tok::kw_export)) |
| 889 | NamespaceTok = NamespaceTok->getNextNonComment(); |
| 890 | return NamespaceTok && |
| 891 | NamespaceTok->isOneOf(K1: tok::kw_namespace, K2: TT_NamespaceMacro) |
| 892 | ? NamespaceTok |
| 893 | : nullptr; |
| 894 | } |
| 895 | |
| 896 | void copyFrom(const FormatToken &Tok) { *this = Tok; } |
| 897 | |
| 898 | private: |
| 899 | // Only allow copying via the explicit copyFrom method. |
| 900 | FormatToken(const FormatToken &) = delete; |
| 901 | FormatToken &operator=(const FormatToken &) = default; |
| 902 | |
| 903 | template <typename A, typename... Ts> |
| 904 | bool startsSequenceInternal(A K1, Ts... Tokens) const { |
| 905 | if (is(Kind: tok::comment) && Next) |
| 906 | return Next->startsSequenceInternal(K1, Tokens...); |
| 907 | return is(K1) && Next && Next->startsSequenceInternal(Tokens...); |
| 908 | } |
| 909 | |
| 910 | template <typename A> bool startsSequenceInternal(A K1) const { |
| 911 | if (is(Kind: tok::comment) && Next) |
| 912 | return Next->startsSequenceInternal(K1); |
| 913 | return is(K1); |
| 914 | } |
| 915 | |
| 916 | template <typename A, typename... Ts> bool endsSequenceInternal(A K1) const { |
| 917 | if (is(Kind: tok::comment) && Previous) |
| 918 | return Previous->endsSequenceInternal(K1); |
| 919 | return is(K1); |
| 920 | } |
| 921 | |
| 922 | template <typename A, typename... Ts> |
| 923 | bool endsSequenceInternal(A K1, Ts... Tokens) const { |
| 924 | if (is(Kind: tok::comment) && Previous) |
| 925 | return Previous->endsSequenceInternal(K1, Tokens...); |
| 926 | return is(K1) && Previous && Previous->endsSequenceInternal(Tokens...); |
| 927 | } |
| 928 | }; |
| 929 | |
| 930 | class ContinuationIndenter; |
| 931 | struct LineState; |
| 932 | |
| 933 | class TokenRole { |
| 934 | public: |
| 935 | TokenRole(const FormatStyle &Style) : Style(Style) {} |
| 936 | virtual ~TokenRole(); |
| 937 | |
| 938 | /// After the \c TokenAnnotator has finished annotating all the tokens, |
| 939 | /// this function precomputes required information for formatting. |
| 940 | virtual void precomputeFormattingInfos(const FormatToken *Token); |
| 941 | |
| 942 | /// Apply the special formatting that the given role demands. |
| 943 | /// |
| 944 | /// Assumes that the token having this role is already formatted. |
| 945 | /// |
| 946 | /// Continues formatting from \p State leaving indentation to \p Indenter and |
| 947 | /// returns the total penalty that this formatting incurs. |
| 948 | virtual unsigned formatFromToken(LineState &State, |
| 949 | ContinuationIndenter *Indenter, |
| 950 | bool DryRun) { |
| 951 | return 0; |
| 952 | } |
| 953 | |
| 954 | /// Same as \c formatFromToken, but assumes that the first token has |
| 955 | /// already been set thereby deciding on the first line break. |
| 956 | virtual unsigned formatAfterToken(LineState &State, |
| 957 | ContinuationIndenter *Indenter, |
| 958 | bool DryRun) { |
| 959 | return 0; |
| 960 | } |
| 961 | |
| 962 | /// Notifies the \c Role that a comma was found. |
| 963 | virtual void CommaFound(const FormatToken *Token) {} |
| 964 | |
| 965 | virtual const FormatToken *lastComma() { return nullptr; } |
| 966 | |
| 967 | protected: |
| 968 | const FormatStyle &Style; |
| 969 | }; |
| 970 | |
| 971 | class CommaSeparatedList : public TokenRole { |
| 972 | public: |
| 973 | CommaSeparatedList(const FormatStyle &Style) |
| 974 | : TokenRole(Style), HasNestedBracedList(false) {} |
| 975 | |
| 976 | void precomputeFormattingInfos(const FormatToken *Token) override; |
| 977 | |
| 978 | unsigned formatAfterToken(LineState &State, ContinuationIndenter *Indenter, |
| 979 | bool DryRun) override; |
| 980 | |
| 981 | unsigned formatFromToken(LineState &State, ContinuationIndenter *Indenter, |
| 982 | bool DryRun) override; |
| 983 | |
| 984 | /// Adds \p Token as the next comma to the \c CommaSeparated list. |
| 985 | void CommaFound(const FormatToken *Token) override { |
| 986 | Commas.push_back(Elt: Token); |
| 987 | } |
| 988 | |
| 989 | const FormatToken *lastComma() override { |
| 990 | if (Commas.empty()) |
| 991 | return nullptr; |
| 992 | return Commas.back(); |
| 993 | } |
| 994 | |
| 995 | private: |
| 996 | /// A struct that holds information on how to format a given list with |
| 997 | /// a specific number of columns. |
| 998 | struct ColumnFormat { |
| 999 | /// The number of columns to use. |
| 1000 | unsigned Columns; |
| 1001 | |
| 1002 | /// The total width in characters. |
| 1003 | unsigned TotalWidth; |
| 1004 | |
| 1005 | /// The number of lines required for this format. |
| 1006 | unsigned LineCount; |
| 1007 | |
| 1008 | /// The size of each column in characters. |
| 1009 | SmallVector<unsigned, 8> ColumnSizes; |
| 1010 | }; |
| 1011 | |
| 1012 | /// Calculate which \c ColumnFormat fits best into |
| 1013 | /// \p RemainingCharacters. |
| 1014 | const ColumnFormat *getColumnFormat(unsigned RemainingCharacters) const; |
| 1015 | |
| 1016 | /// The ordered \c FormatTokens making up the commas of this list. |
| 1017 | SmallVector<const FormatToken *, 8> Commas; |
| 1018 | |
| 1019 | /// The length of each of the list's items in characters including the |
| 1020 | /// trailing comma. |
| 1021 | SmallVector<unsigned, 8> ItemLengths; |
| 1022 | |
| 1023 | /// Precomputed formats that can be used for this list. |
| 1024 | SmallVector<ColumnFormat, 4> Formats; |
| 1025 | |
| 1026 | bool HasNestedBracedList; |
| 1027 | }; |
| 1028 | |
| 1029 | /// Encapsulates keywords that are context sensitive or for languages not |
| 1030 | /// properly supported by Clang's lexer. |
| 1031 | struct AdditionalKeywords { |
| 1032 | AdditionalKeywords(IdentifierTable &IdentTable) { |
| 1033 | kw_final = &IdentTable.get(Name: "final" ); |
| 1034 | kw_override = &IdentTable.get(Name: "override" ); |
| 1035 | kw_in = &IdentTable.get(Name: "in" ); |
| 1036 | kw_of = &IdentTable.get(Name: "of" ); |
| 1037 | kw_CF_CLOSED_ENUM = &IdentTable.get(Name: "CF_CLOSED_ENUM" ); |
| 1038 | kw_CF_ENUM = &IdentTable.get(Name: "CF_ENUM" ); |
| 1039 | kw_CF_OPTIONS = &IdentTable.get(Name: "CF_OPTIONS" ); |
| 1040 | kw_NS_CLOSED_ENUM = &IdentTable.get(Name: "NS_CLOSED_ENUM" ); |
| 1041 | kw_NS_ENUM = &IdentTable.get(Name: "NS_ENUM" ); |
| 1042 | kw_NS_ERROR_ENUM = &IdentTable.get(Name: "NS_ERROR_ENUM" ); |
| 1043 | kw_NS_OPTIONS = &IdentTable.get(Name: "NS_OPTIONS" ); |
| 1044 | |
| 1045 | kw_as = &IdentTable.get(Name: "as" ); |
| 1046 | kw_async = &IdentTable.get(Name: "async" ); |
| 1047 | kw_await = &IdentTable.get(Name: "await" ); |
| 1048 | kw_declare = &IdentTable.get(Name: "declare" ); |
| 1049 | kw_finally = &IdentTable.get(Name: "finally" ); |
| 1050 | kw_from = &IdentTable.get(Name: "from" ); |
| 1051 | kw_function = &IdentTable.get(Name: "function" ); |
| 1052 | kw_get = &IdentTable.get(Name: "get" ); |
| 1053 | kw_import = &IdentTable.get(Name: "import" ); |
| 1054 | kw_infer = &IdentTable.get(Name: "infer" ); |
| 1055 | kw_is = &IdentTable.get(Name: "is" ); |
| 1056 | kw_let = &IdentTable.get(Name: "let" ); |
| 1057 | kw_module = &IdentTable.get(Name: "module" ); |
| 1058 | kw_readonly = &IdentTable.get(Name: "readonly" ); |
| 1059 | kw_set = &IdentTable.get(Name: "set" ); |
| 1060 | kw_type = &IdentTable.get(Name: "type" ); |
| 1061 | kw_typeof = &IdentTable.get(Name: "typeof" ); |
| 1062 | kw_var = &IdentTable.get(Name: "var" ); |
| 1063 | kw_yield = &IdentTable.get(Name: "yield" ); |
| 1064 | |
| 1065 | kw_abstract = &IdentTable.get(Name: "abstract" ); |
| 1066 | kw_assert = &IdentTable.get(Name: "assert" ); |
| 1067 | kw_extends = &IdentTable.get(Name: "extends" ); |
| 1068 | kw_implements = &IdentTable.get(Name: "implements" ); |
| 1069 | kw_instanceof = &IdentTable.get(Name: "instanceof" ); |
| 1070 | kw_interface = &IdentTable.get(Name: "interface" ); |
| 1071 | kw_native = &IdentTable.get(Name: "native" ); |
| 1072 | kw_package = &IdentTable.get(Name: "package" ); |
| 1073 | kw_record = &IdentTable.get(Name: "record" ); |
| 1074 | kw_synchronized = &IdentTable.get(Name: "synchronized" ); |
| 1075 | kw_throws = &IdentTable.get(Name: "throws" ); |
| 1076 | kw___except = &IdentTable.get(Name: "__except" ); |
| 1077 | kw___has_include = &IdentTable.get(Name: "__has_include" ); |
| 1078 | kw___has_include_next = &IdentTable.get(Name: "__has_include_next" ); |
| 1079 | |
| 1080 | kw_mark = &IdentTable.get(Name: "mark" ); |
| 1081 | kw_region = &IdentTable.get(Name: "region" ); |
| 1082 | |
| 1083 | kw_extend = &IdentTable.get(Name: "extend" ); |
| 1084 | kw_option = &IdentTable.get(Name: "option" ); |
| 1085 | kw_optional = &IdentTable.get(Name: "optional" ); |
| 1086 | kw_repeated = &IdentTable.get(Name: "repeated" ); |
| 1087 | kw_required = &IdentTable.get(Name: "required" ); |
| 1088 | kw_returns = &IdentTable.get(Name: "returns" ); |
| 1089 | |
| 1090 | kw_signals = &IdentTable.get(Name: "signals" ); |
| 1091 | kw_qsignals = &IdentTable.get(Name: "Q_SIGNALS" ); |
| 1092 | kw_slots = &IdentTable.get(Name: "slots" ); |
| 1093 | kw_qslots = &IdentTable.get(Name: "Q_SLOTS" ); |
| 1094 | |
| 1095 | // For internal clang-format use. |
| 1096 | kw_internal_ident_after_define = |
| 1097 | &IdentTable.get(Name: "__CLANG_FORMAT_INTERNAL_IDENT_AFTER_DEFINE__" ); |
| 1098 | |
| 1099 | // C# keywords |
| 1100 | kw_dollar = &IdentTable.get(Name: "dollar" ); |
| 1101 | kw_base = &IdentTable.get(Name: "base" ); |
| 1102 | kw_byte = &IdentTable.get(Name: "byte" ); |
| 1103 | kw_checked = &IdentTable.get(Name: "checked" ); |
| 1104 | kw_decimal = &IdentTable.get(Name: "decimal" ); |
| 1105 | kw_delegate = &IdentTable.get(Name: "delegate" ); |
| 1106 | kw_event = &IdentTable.get(Name: "event" ); |
| 1107 | kw_fixed = &IdentTable.get(Name: "fixed" ); |
| 1108 | kw_foreach = &IdentTable.get(Name: "foreach" ); |
| 1109 | kw_init = &IdentTable.get(Name: "init" ); |
| 1110 | kw_implicit = &IdentTable.get(Name: "implicit" ); |
| 1111 | kw_internal = &IdentTable.get(Name: "internal" ); |
| 1112 | kw_lock = &IdentTable.get(Name: "lock" ); |
| 1113 | kw_null = &IdentTable.get(Name: "null" ); |
| 1114 | kw_object = &IdentTable.get(Name: "object" ); |
| 1115 | kw_out = &IdentTable.get(Name: "out" ); |
| 1116 | kw_params = &IdentTable.get(Name: "params" ); |
| 1117 | kw_ref = &IdentTable.get(Name: "ref" ); |
| 1118 | kw_string = &IdentTable.get(Name: "string" ); |
| 1119 | kw_stackalloc = &IdentTable.get(Name: "stackalloc" ); |
| 1120 | kw_sbyte = &IdentTable.get(Name: "sbyte" ); |
| 1121 | kw_sealed = &IdentTable.get(Name: "sealed" ); |
| 1122 | kw_uint = &IdentTable.get(Name: "uint" ); |
| 1123 | kw_ulong = &IdentTable.get(Name: "ulong" ); |
| 1124 | kw_unchecked = &IdentTable.get(Name: "unchecked" ); |
| 1125 | kw_unsafe = &IdentTable.get(Name: "unsafe" ); |
| 1126 | kw_ushort = &IdentTable.get(Name: "ushort" ); |
| 1127 | kw_when = &IdentTable.get(Name: "when" ); |
| 1128 | kw_where = &IdentTable.get(Name: "where" ); |
| 1129 | |
| 1130 | // Verilog keywords |
| 1131 | kw_always = &IdentTable.get(Name: "always" ); |
| 1132 | kw_always_comb = &IdentTable.get(Name: "always_comb" ); |
| 1133 | kw_always_ff = &IdentTable.get(Name: "always_ff" ); |
| 1134 | kw_always_latch = &IdentTable.get(Name: "always_latch" ); |
| 1135 | kw_assign = &IdentTable.get(Name: "assign" ); |
| 1136 | kw_assume = &IdentTable.get(Name: "assume" ); |
| 1137 | kw_automatic = &IdentTable.get(Name: "automatic" ); |
| 1138 | kw_before = &IdentTable.get(Name: "before" ); |
| 1139 | kw_begin = &IdentTable.get(Name: "begin" ); |
| 1140 | kw_begin_keywords = &IdentTable.get(Name: "begin_keywords" ); |
| 1141 | kw_bins = &IdentTable.get(Name: "bins" ); |
| 1142 | kw_binsof = &IdentTable.get(Name: "binsof" ); |
| 1143 | kw_casex = &IdentTable.get(Name: "casex" ); |
| 1144 | kw_casez = &IdentTable.get(Name: "casez" ); |
| 1145 | kw_celldefine = &IdentTable.get(Name: "celldefine" ); |
| 1146 | kw_checker = &IdentTable.get(Name: "checker" ); |
| 1147 | kw_clocking = &IdentTable.get(Name: "clocking" ); |
| 1148 | kw_constraint = &IdentTable.get(Name: "constraint" ); |
| 1149 | kw_cover = &IdentTable.get(Name: "cover" ); |
| 1150 | kw_covergroup = &IdentTable.get(Name: "covergroup" ); |
| 1151 | kw_coverpoint = &IdentTable.get(Name: "coverpoint" ); |
| 1152 | kw_default_decay_time = &IdentTable.get(Name: "default_decay_time" ); |
| 1153 | kw_default_nettype = &IdentTable.get(Name: "default_nettype" ); |
| 1154 | kw_default_trireg_strength = &IdentTable.get(Name: "default_trireg_strength" ); |
| 1155 | kw_delay_mode_distributed = &IdentTable.get(Name: "delay_mode_distributed" ); |
| 1156 | kw_delay_mode_path = &IdentTable.get(Name: "delay_mode_path" ); |
| 1157 | kw_delay_mode_unit = &IdentTable.get(Name: "delay_mode_unit" ); |
| 1158 | kw_delay_mode_zero = &IdentTable.get(Name: "delay_mode_zero" ); |
| 1159 | kw_disable = &IdentTable.get(Name: "disable" ); |
| 1160 | kw_dist = &IdentTable.get(Name: "dist" ); |
| 1161 | kw_edge = &IdentTable.get(Name: "edge" ); |
| 1162 | kw_elsif = &IdentTable.get(Name: "elsif" ); |
| 1163 | kw_end = &IdentTable.get(Name: "end" ); |
| 1164 | kw_end_keywords = &IdentTable.get(Name: "end_keywords" ); |
| 1165 | kw_endcase = &IdentTable.get(Name: "endcase" ); |
| 1166 | kw_endcelldefine = &IdentTable.get(Name: "endcelldefine" ); |
| 1167 | kw_endchecker = &IdentTable.get(Name: "endchecker" ); |
| 1168 | kw_endclass = &IdentTable.get(Name: "endclass" ); |
| 1169 | kw_endclocking = &IdentTable.get(Name: "endclocking" ); |
| 1170 | kw_endfunction = &IdentTable.get(Name: "endfunction" ); |
| 1171 | kw_endgenerate = &IdentTable.get(Name: "endgenerate" ); |
| 1172 | kw_endgroup = &IdentTable.get(Name: "endgroup" ); |
| 1173 | kw_endinterface = &IdentTable.get(Name: "endinterface" ); |
| 1174 | kw_endmodule = &IdentTable.get(Name: "endmodule" ); |
| 1175 | kw_endpackage = &IdentTable.get(Name: "endpackage" ); |
| 1176 | kw_endprimitive = &IdentTable.get(Name: "endprimitive" ); |
| 1177 | kw_endprogram = &IdentTable.get(Name: "endprogram" ); |
| 1178 | kw_endproperty = &IdentTable.get(Name: "endproperty" ); |
| 1179 | kw_endsequence = &IdentTable.get(Name: "endsequence" ); |
| 1180 | kw_endspecify = &IdentTable.get(Name: "endspecify" ); |
| 1181 | kw_endtable = &IdentTable.get(Name: "endtable" ); |
| 1182 | kw_endtask = &IdentTable.get(Name: "endtask" ); |
| 1183 | kw_forever = &IdentTable.get(Name: "forever" ); |
| 1184 | kw_fork = &IdentTable.get(Name: "fork" ); |
| 1185 | kw_generate = &IdentTable.get(Name: "generate" ); |
| 1186 | kw_highz0 = &IdentTable.get(Name: "highz0" ); |
| 1187 | kw_highz1 = &IdentTable.get(Name: "highz1" ); |
| 1188 | kw_iff = &IdentTable.get(Name: "iff" ); |
| 1189 | kw_ifnone = &IdentTable.get(Name: "ifnone" ); |
| 1190 | kw_ignore_bins = &IdentTable.get(Name: "ignore_bins" ); |
| 1191 | kw_illegal_bins = &IdentTable.get(Name: "illegal_bins" ); |
| 1192 | kw_initial = &IdentTable.get(Name: "initial" ); |
| 1193 | kw_inout = &IdentTable.get(Name: "inout" ); |
| 1194 | kw_input = &IdentTable.get(Name: "input" ); |
| 1195 | kw_inside = &IdentTable.get(Name: "inside" ); |
| 1196 | kw_interconnect = &IdentTable.get(Name: "interconnect" ); |
| 1197 | kw_intersect = &IdentTable.get(Name: "intersect" ); |
| 1198 | kw_join = &IdentTable.get(Name: "join" ); |
| 1199 | kw_join_any = &IdentTable.get(Name: "join_any" ); |
| 1200 | kw_join_none = &IdentTable.get(Name: "join_none" ); |
| 1201 | kw_large = &IdentTable.get(Name: "large" ); |
| 1202 | kw_local = &IdentTable.get(Name: "local" ); |
| 1203 | kw_localparam = &IdentTable.get(Name: "localparam" ); |
| 1204 | kw_macromodule = &IdentTable.get(Name: "macromodule" ); |
| 1205 | kw_matches = &IdentTable.get(Name: "matches" ); |
| 1206 | kw_medium = &IdentTable.get(Name: "medium" ); |
| 1207 | kw_negedge = &IdentTable.get(Name: "negedge" ); |
| 1208 | kw_nounconnected_drive = &IdentTable.get(Name: "nounconnected_drive" ); |
| 1209 | kw_output = &IdentTable.get(Name: "output" ); |
| 1210 | kw_packed = &IdentTable.get(Name: "packed" ); |
| 1211 | kw_parameter = &IdentTable.get(Name: "parameter" ); |
| 1212 | kw_posedge = &IdentTable.get(Name: "posedge" ); |
| 1213 | kw_primitive = &IdentTable.get(Name: "primitive" ); |
| 1214 | kw_priority = &IdentTable.get(Name: "priority" ); |
| 1215 | kw_program = &IdentTable.get(Name: "program" ); |
| 1216 | kw_property = &IdentTable.get(Name: "property" ); |
| 1217 | kw_pull0 = &IdentTable.get(Name: "pull0" ); |
| 1218 | kw_pull1 = &IdentTable.get(Name: "pull1" ); |
| 1219 | kw_pure = &IdentTable.get(Name: "pure" ); |
| 1220 | kw_rand = &IdentTable.get(Name: "rand" ); |
| 1221 | kw_randc = &IdentTable.get(Name: "randc" ); |
| 1222 | kw_randcase = &IdentTable.get(Name: "randcase" ); |
| 1223 | kw_randsequence = &IdentTable.get(Name: "randsequence" ); |
| 1224 | kw_repeat = &IdentTable.get(Name: "repeat" ); |
| 1225 | kw_resetall = &IdentTable.get(Name: "resetall" ); |
| 1226 | kw_sample = &IdentTable.get(Name: "sample" ); |
| 1227 | kw_scalared = &IdentTable.get(Name: "scalared" ); |
| 1228 | kw_sequence = &IdentTable.get(Name: "sequence" ); |
| 1229 | kw_small = &IdentTable.get(Name: "small" ); |
| 1230 | kw_soft = &IdentTable.get(Name: "soft" ); |
| 1231 | kw_solve = &IdentTable.get(Name: "solve" ); |
| 1232 | kw_specify = &IdentTable.get(Name: "specify" ); |
| 1233 | kw_specparam = &IdentTable.get(Name: "specparam" ); |
| 1234 | kw_strong0 = &IdentTable.get(Name: "strong0" ); |
| 1235 | kw_strong1 = &IdentTable.get(Name: "strong1" ); |
| 1236 | kw_supply0 = &IdentTable.get(Name: "supply0" ); |
| 1237 | kw_supply1 = &IdentTable.get(Name: "supply1" ); |
| 1238 | kw_table = &IdentTable.get(Name: "table" ); |
| 1239 | kw_tagged = &IdentTable.get(Name: "tagged" ); |
| 1240 | kw_task = &IdentTable.get(Name: "task" ); |
| 1241 | kw_timescale = &IdentTable.get(Name: "timescale" ); |
| 1242 | kw_tri = &IdentTable.get(Name: "tri" ); |
| 1243 | kw_tri0 = &IdentTable.get(Name: "tri0" ); |
| 1244 | kw_tri1 = &IdentTable.get(Name: "tri1" ); |
| 1245 | kw_triand = &IdentTable.get(Name: "triand" ); |
| 1246 | kw_trior = &IdentTable.get(Name: "trior" ); |
| 1247 | kw_trireg = &IdentTable.get(Name: "trireg" ); |
| 1248 | kw_unconnected_drive = &IdentTable.get(Name: "unconnected_drive" ); |
| 1249 | kw_undefineall = &IdentTable.get(Name: "undefineall" ); |
| 1250 | kw_unique = &IdentTable.get(Name: "unique" ); |
| 1251 | kw_unique0 = &IdentTable.get(Name: "unique0" ); |
| 1252 | kw_uwire = &IdentTable.get(Name: "uwire" ); |
| 1253 | kw_vectored = &IdentTable.get(Name: "vectored" ); |
| 1254 | kw_wait = &IdentTable.get(Name: "wait" ); |
| 1255 | kw_wand = &IdentTable.get(Name: "wand" ); |
| 1256 | kw_weak0 = &IdentTable.get(Name: "weak0" ); |
| 1257 | kw_weak1 = &IdentTable.get(Name: "weak1" ); |
| 1258 | kw_wildcard = &IdentTable.get(Name: "wildcard" ); |
| 1259 | kw_wire = &IdentTable.get(Name: "wire" ); |
| 1260 | kw_with = &IdentTable.get(Name: "with" ); |
| 1261 | kw_wor = &IdentTable.get(Name: "wor" ); |
| 1262 | |
| 1263 | // Symbols that are treated as keywords. |
| 1264 | kw_verilogHash = &IdentTable.get(Name: "#" ); |
| 1265 | kw_verilogHashHash = &IdentTable.get(Name: "##" ); |
| 1266 | kw_apostrophe = &IdentTable.get(Name: "\'" ); |
| 1267 | |
| 1268 | // TableGen keywords. |
| 1269 | kw_bit = &IdentTable.get(Name: "bit" ); |
| 1270 | kw_bits = &IdentTable.get(Name: "bits" ); |
| 1271 | kw_code = &IdentTable.get(Name: "code" ); |
| 1272 | kw_dag = &IdentTable.get(Name: "dag" ); |
| 1273 | kw_def = &IdentTable.get(Name: "def" ); |
| 1274 | kw_defm = &IdentTable.get(Name: "defm" ); |
| 1275 | kw_defset = &IdentTable.get(Name: "defset" ); |
| 1276 | kw_defvar = &IdentTable.get(Name: "defvar" ); |
| 1277 | kw_dump = &IdentTable.get(Name: "dump" ); |
| 1278 | kw_include = &IdentTable.get(Name: "include" ); |
| 1279 | kw_list = &IdentTable.get(Name: "list" ); |
| 1280 | kw_multiclass = &IdentTable.get(Name: "multiclass" ); |
| 1281 | kw_then = &IdentTable.get(Name: "then" ); |
| 1282 | |
| 1283 | // Keep this at the end of the constructor to make sure everything here is |
| 1284 | // already initialized. |
| 1285 | JsExtraKeywords = std::unordered_set<IdentifierInfo *>( |
| 1286 | {kw_as, kw_async, kw_await, kw_declare, kw_finally, kw_from, |
| 1287 | kw_function, kw_get, kw_import, kw_is, kw_let, kw_module, kw_override, |
| 1288 | kw_readonly, kw_set, kw_type, kw_typeof, kw_var, kw_yield, |
| 1289 | // Keywords from the Java section. |
| 1290 | kw_abstract, kw_extends, kw_implements, kw_instanceof, kw_interface}); |
| 1291 | |
| 1292 | CSharpExtraKeywords = JsExtraKeywords; |
| 1293 | CSharpExtraKeywords.insert( |
| 1294 | l: {kw_base, kw_byte, kw_checked, kw_decimal, kw_delegate, |
| 1295 | kw_event, kw_fixed, kw_foreach, kw_implicit, kw_in, |
| 1296 | kw_init, kw_internal, kw_lock, kw_null, kw_object, |
| 1297 | kw_out, kw_params, kw_ref, kw_string, kw_stackalloc, |
| 1298 | kw_sbyte, kw_sealed, kw_uint, kw_ulong, kw_unchecked, |
| 1299 | kw_unsafe, kw_ushort, kw_when, kw_where}); |
| 1300 | |
| 1301 | // Some keywords are not included here because they don't need special |
| 1302 | // treatment like `showcancelled` or they should be treated as identifiers |
| 1303 | // like `int` and `logic`. |
| 1304 | VerilogExtraKeywords = std::unordered_set<IdentifierInfo *>( |
| 1305 | {kw_always, kw_always_comb, kw_always_ff, |
| 1306 | kw_always_latch, kw_assert, kw_assign, |
| 1307 | kw_assume, kw_automatic, kw_before, |
| 1308 | kw_begin, kw_bins, kw_binsof, |
| 1309 | kw_casex, kw_casez, kw_celldefine, |
| 1310 | kw_checker, kw_clocking, kw_constraint, |
| 1311 | kw_cover, kw_covergroup, kw_coverpoint, |
| 1312 | kw_disable, kw_dist, kw_edge, |
| 1313 | kw_end, kw_endcase, kw_endchecker, |
| 1314 | kw_endclass, kw_endclocking, kw_endfunction, |
| 1315 | kw_endgenerate, kw_endgroup, kw_endinterface, |
| 1316 | kw_endmodule, kw_endpackage, kw_endprimitive, |
| 1317 | kw_endprogram, kw_endproperty, kw_endsequence, |
| 1318 | kw_endspecify, kw_endtable, kw_endtask, |
| 1319 | kw_extends, kw_final, kw_foreach, |
| 1320 | kw_forever, kw_fork, kw_function, |
| 1321 | kw_generate, kw_highz0, kw_highz1, |
| 1322 | kw_iff, kw_ifnone, kw_ignore_bins, |
| 1323 | kw_illegal_bins, kw_implements, kw_import, |
| 1324 | kw_initial, kw_inout, kw_input, |
| 1325 | kw_inside, kw_interconnect, kw_interface, |
| 1326 | kw_intersect, kw_join, kw_join_any, |
| 1327 | kw_join_none, kw_large, kw_let, |
| 1328 | kw_local, kw_localparam, kw_macromodule, |
| 1329 | kw_matches, kw_medium, kw_negedge, |
| 1330 | kw_output, kw_package, kw_packed, |
| 1331 | kw_parameter, kw_posedge, kw_primitive, |
| 1332 | kw_priority, kw_program, kw_property, |
| 1333 | kw_pull0, kw_pull1, kw_pure, |
| 1334 | kw_rand, kw_randc, kw_randcase, |
| 1335 | kw_randsequence, kw_ref, kw_repeat, |
| 1336 | kw_sample, kw_scalared, kw_sequence, |
| 1337 | kw_small, kw_soft, kw_solve, |
| 1338 | kw_specify, kw_specparam, kw_strong0, |
| 1339 | kw_strong1, kw_supply0, kw_supply1, |
| 1340 | kw_table, kw_tagged, kw_task, |
| 1341 | kw_tri, kw_tri0, kw_tri1, |
| 1342 | kw_triand, kw_trior, kw_trireg, |
| 1343 | kw_unique, kw_unique0, kw_uwire, |
| 1344 | kw_var, kw_vectored, kw_wait, |
| 1345 | kw_wand, kw_weak0, kw_weak1, |
| 1346 | kw_wildcard, kw_wire, kw_with, |
| 1347 | kw_wor, kw_verilogHash, kw_verilogHashHash}); |
| 1348 | |
| 1349 | TableGenExtraKeywords = std::unordered_set<IdentifierInfo *>({ |
| 1350 | kw_assert, |
| 1351 | kw_bit, |
| 1352 | kw_bits, |
| 1353 | kw_code, |
| 1354 | kw_dag, |
| 1355 | kw_def, |
| 1356 | kw_defm, |
| 1357 | kw_defset, |
| 1358 | kw_defvar, |
| 1359 | kw_dump, |
| 1360 | kw_foreach, |
| 1361 | kw_in, |
| 1362 | kw_include, |
| 1363 | kw_let, |
| 1364 | kw_list, |
| 1365 | kw_multiclass, |
| 1366 | kw_string, |
| 1367 | kw_then, |
| 1368 | }); |
| 1369 | } |
| 1370 | |
| 1371 | // Context sensitive keywords. |
| 1372 | IdentifierInfo *kw_final; |
| 1373 | IdentifierInfo *kw_override; |
| 1374 | IdentifierInfo *kw_in; |
| 1375 | IdentifierInfo *kw_of; |
| 1376 | IdentifierInfo *kw_CF_CLOSED_ENUM; |
| 1377 | IdentifierInfo *kw_CF_ENUM; |
| 1378 | IdentifierInfo *kw_CF_OPTIONS; |
| 1379 | IdentifierInfo *kw_NS_CLOSED_ENUM; |
| 1380 | IdentifierInfo *kw_NS_ENUM; |
| 1381 | IdentifierInfo *kw_NS_ERROR_ENUM; |
| 1382 | IdentifierInfo *kw_NS_OPTIONS; |
| 1383 | IdentifierInfo *kw___except; |
| 1384 | IdentifierInfo *kw___has_include; |
| 1385 | IdentifierInfo *kw___has_include_next; |
| 1386 | |
| 1387 | // JavaScript keywords. |
| 1388 | IdentifierInfo *kw_as; |
| 1389 | IdentifierInfo *kw_async; |
| 1390 | IdentifierInfo *kw_await; |
| 1391 | IdentifierInfo *kw_declare; |
| 1392 | IdentifierInfo *kw_finally; |
| 1393 | IdentifierInfo *kw_from; |
| 1394 | IdentifierInfo *kw_function; |
| 1395 | IdentifierInfo *kw_get; |
| 1396 | IdentifierInfo *kw_import; |
| 1397 | IdentifierInfo *kw_infer; |
| 1398 | IdentifierInfo *kw_is; |
| 1399 | IdentifierInfo *kw_let; |
| 1400 | IdentifierInfo *kw_module; |
| 1401 | IdentifierInfo *kw_readonly; |
| 1402 | IdentifierInfo *kw_set; |
| 1403 | IdentifierInfo *kw_type; |
| 1404 | IdentifierInfo *kw_typeof; |
| 1405 | IdentifierInfo *kw_var; |
| 1406 | IdentifierInfo *kw_yield; |
| 1407 | |
| 1408 | // Java keywords. |
| 1409 | IdentifierInfo *kw_abstract; |
| 1410 | IdentifierInfo *kw_assert; |
| 1411 | IdentifierInfo *kw_extends; |
| 1412 | IdentifierInfo *kw_implements; |
| 1413 | IdentifierInfo *kw_instanceof; |
| 1414 | IdentifierInfo *kw_interface; |
| 1415 | IdentifierInfo *kw_native; |
| 1416 | IdentifierInfo *kw_package; |
| 1417 | IdentifierInfo *kw_record; |
| 1418 | IdentifierInfo *kw_synchronized; |
| 1419 | IdentifierInfo *kw_throws; |
| 1420 | |
| 1421 | // Pragma keywords. |
| 1422 | IdentifierInfo *kw_mark; |
| 1423 | IdentifierInfo *kw_region; |
| 1424 | |
| 1425 | // Proto keywords. |
| 1426 | IdentifierInfo *kw_extend; |
| 1427 | IdentifierInfo *kw_option; |
| 1428 | IdentifierInfo *kw_optional; |
| 1429 | IdentifierInfo *kw_repeated; |
| 1430 | IdentifierInfo *kw_required; |
| 1431 | IdentifierInfo *kw_returns; |
| 1432 | |
| 1433 | // QT keywords. |
| 1434 | IdentifierInfo *kw_signals; |
| 1435 | IdentifierInfo *kw_qsignals; |
| 1436 | IdentifierInfo *kw_slots; |
| 1437 | IdentifierInfo *kw_qslots; |
| 1438 | |
| 1439 | // For internal use by clang-format. |
| 1440 | IdentifierInfo *kw_internal_ident_after_define; |
| 1441 | |
| 1442 | // C# keywords |
| 1443 | IdentifierInfo *kw_dollar; |
| 1444 | IdentifierInfo *kw_base; |
| 1445 | IdentifierInfo *kw_byte; |
| 1446 | IdentifierInfo *kw_checked; |
| 1447 | IdentifierInfo *kw_decimal; |
| 1448 | IdentifierInfo *kw_delegate; |
| 1449 | IdentifierInfo *kw_event; |
| 1450 | IdentifierInfo *kw_fixed; |
| 1451 | IdentifierInfo *kw_foreach; |
| 1452 | IdentifierInfo *kw_implicit; |
| 1453 | IdentifierInfo *kw_init; |
| 1454 | IdentifierInfo *kw_internal; |
| 1455 | |
| 1456 | IdentifierInfo *kw_lock; |
| 1457 | IdentifierInfo *kw_null; |
| 1458 | IdentifierInfo *kw_object; |
| 1459 | IdentifierInfo *kw_out; |
| 1460 | |
| 1461 | IdentifierInfo *kw_params; |
| 1462 | |
| 1463 | IdentifierInfo *kw_ref; |
| 1464 | IdentifierInfo *kw_string; |
| 1465 | IdentifierInfo *kw_stackalloc; |
| 1466 | IdentifierInfo *kw_sbyte; |
| 1467 | IdentifierInfo *kw_sealed; |
| 1468 | IdentifierInfo *kw_uint; |
| 1469 | IdentifierInfo *kw_ulong; |
| 1470 | IdentifierInfo *kw_unchecked; |
| 1471 | IdentifierInfo *kw_unsafe; |
| 1472 | IdentifierInfo *kw_ushort; |
| 1473 | IdentifierInfo *kw_when; |
| 1474 | IdentifierInfo *kw_where; |
| 1475 | |
| 1476 | // Verilog keywords |
| 1477 | IdentifierInfo *kw_always; |
| 1478 | IdentifierInfo *kw_always_comb; |
| 1479 | IdentifierInfo *kw_always_ff; |
| 1480 | IdentifierInfo *kw_always_latch; |
| 1481 | IdentifierInfo *kw_assign; |
| 1482 | IdentifierInfo *kw_assume; |
| 1483 | IdentifierInfo *kw_automatic; |
| 1484 | IdentifierInfo *kw_before; |
| 1485 | IdentifierInfo *kw_begin; |
| 1486 | IdentifierInfo *kw_begin_keywords; |
| 1487 | IdentifierInfo *kw_bins; |
| 1488 | IdentifierInfo *kw_binsof; |
| 1489 | IdentifierInfo *kw_casex; |
| 1490 | IdentifierInfo *kw_casez; |
| 1491 | IdentifierInfo *kw_celldefine; |
| 1492 | IdentifierInfo *kw_checker; |
| 1493 | IdentifierInfo *kw_clocking; |
| 1494 | IdentifierInfo *kw_constraint; |
| 1495 | IdentifierInfo *kw_cover; |
| 1496 | IdentifierInfo *kw_covergroup; |
| 1497 | IdentifierInfo *kw_coverpoint; |
| 1498 | IdentifierInfo *kw_default_decay_time; |
| 1499 | IdentifierInfo *kw_default_nettype; |
| 1500 | IdentifierInfo *kw_default_trireg_strength; |
| 1501 | IdentifierInfo *kw_delay_mode_distributed; |
| 1502 | IdentifierInfo *kw_delay_mode_path; |
| 1503 | IdentifierInfo *kw_delay_mode_unit; |
| 1504 | IdentifierInfo *kw_delay_mode_zero; |
| 1505 | IdentifierInfo *kw_disable; |
| 1506 | IdentifierInfo *kw_dist; |
| 1507 | IdentifierInfo *kw_elsif; |
| 1508 | IdentifierInfo *kw_edge; |
| 1509 | IdentifierInfo *kw_end; |
| 1510 | IdentifierInfo *kw_end_keywords; |
| 1511 | IdentifierInfo *kw_endcase; |
| 1512 | IdentifierInfo *kw_endcelldefine; |
| 1513 | IdentifierInfo *kw_endchecker; |
| 1514 | IdentifierInfo *kw_endclass; |
| 1515 | IdentifierInfo *kw_endclocking; |
| 1516 | IdentifierInfo *kw_endfunction; |
| 1517 | IdentifierInfo *kw_endgenerate; |
| 1518 | IdentifierInfo *kw_endgroup; |
| 1519 | IdentifierInfo *kw_endinterface; |
| 1520 | IdentifierInfo *kw_endmodule; |
| 1521 | IdentifierInfo *kw_endpackage; |
| 1522 | IdentifierInfo *kw_endprimitive; |
| 1523 | IdentifierInfo *kw_endprogram; |
| 1524 | IdentifierInfo *kw_endproperty; |
| 1525 | IdentifierInfo *kw_endsequence; |
| 1526 | IdentifierInfo *kw_endspecify; |
| 1527 | IdentifierInfo *kw_endtable; |
| 1528 | IdentifierInfo *kw_endtask; |
| 1529 | IdentifierInfo *kw_forever; |
| 1530 | IdentifierInfo *kw_fork; |
| 1531 | IdentifierInfo *kw_generate; |
| 1532 | IdentifierInfo *kw_highz0; |
| 1533 | IdentifierInfo *kw_highz1; |
| 1534 | IdentifierInfo *kw_iff; |
| 1535 | IdentifierInfo *kw_ifnone; |
| 1536 | IdentifierInfo *kw_ignore_bins; |
| 1537 | IdentifierInfo *kw_illegal_bins; |
| 1538 | IdentifierInfo *kw_initial; |
| 1539 | IdentifierInfo *kw_inout; |
| 1540 | IdentifierInfo *kw_input; |
| 1541 | IdentifierInfo *kw_inside; |
| 1542 | IdentifierInfo *kw_interconnect; |
| 1543 | IdentifierInfo *kw_intersect; |
| 1544 | IdentifierInfo *kw_join; |
| 1545 | IdentifierInfo *kw_join_any; |
| 1546 | IdentifierInfo *kw_join_none; |
| 1547 | IdentifierInfo *kw_large; |
| 1548 | IdentifierInfo *kw_local; |
| 1549 | IdentifierInfo *kw_localparam; |
| 1550 | IdentifierInfo *kw_macromodule; |
| 1551 | IdentifierInfo *kw_matches; |
| 1552 | IdentifierInfo *kw_medium; |
| 1553 | IdentifierInfo *kw_negedge; |
| 1554 | IdentifierInfo *kw_nounconnected_drive; |
| 1555 | IdentifierInfo *kw_output; |
| 1556 | IdentifierInfo *kw_packed; |
| 1557 | IdentifierInfo *kw_parameter; |
| 1558 | IdentifierInfo *kw_posedge; |
| 1559 | IdentifierInfo *kw_primitive; |
| 1560 | IdentifierInfo *kw_priority; |
| 1561 | IdentifierInfo *kw_program; |
| 1562 | IdentifierInfo *kw_property; |
| 1563 | IdentifierInfo *kw_pull0; |
| 1564 | IdentifierInfo *kw_pull1; |
| 1565 | IdentifierInfo *kw_pure; |
| 1566 | IdentifierInfo *kw_rand; |
| 1567 | IdentifierInfo *kw_randc; |
| 1568 | IdentifierInfo *kw_randcase; |
| 1569 | IdentifierInfo *kw_randsequence; |
| 1570 | IdentifierInfo *kw_repeat; |
| 1571 | IdentifierInfo *kw_resetall; |
| 1572 | IdentifierInfo *kw_sample; |
| 1573 | IdentifierInfo *kw_scalared; |
| 1574 | IdentifierInfo *kw_sequence; |
| 1575 | IdentifierInfo *kw_small; |
| 1576 | IdentifierInfo *kw_soft; |
| 1577 | IdentifierInfo *kw_solve; |
| 1578 | IdentifierInfo *kw_specify; |
| 1579 | IdentifierInfo *kw_specparam; |
| 1580 | IdentifierInfo *kw_strong0; |
| 1581 | IdentifierInfo *kw_strong1; |
| 1582 | IdentifierInfo *kw_supply0; |
| 1583 | IdentifierInfo *kw_supply1; |
| 1584 | IdentifierInfo *kw_table; |
| 1585 | IdentifierInfo *kw_tagged; |
| 1586 | IdentifierInfo *kw_task; |
| 1587 | IdentifierInfo *kw_timescale; |
| 1588 | IdentifierInfo *kw_tri0; |
| 1589 | IdentifierInfo *kw_tri1; |
| 1590 | IdentifierInfo *kw_tri; |
| 1591 | IdentifierInfo *kw_triand; |
| 1592 | IdentifierInfo *kw_trior; |
| 1593 | IdentifierInfo *kw_trireg; |
| 1594 | IdentifierInfo *kw_unconnected_drive; |
| 1595 | IdentifierInfo *kw_undefineall; |
| 1596 | IdentifierInfo *kw_unique; |
| 1597 | IdentifierInfo *kw_unique0; |
| 1598 | IdentifierInfo *kw_uwire; |
| 1599 | IdentifierInfo *kw_vectored; |
| 1600 | IdentifierInfo *kw_wait; |
| 1601 | IdentifierInfo *kw_wand; |
| 1602 | IdentifierInfo *kw_weak0; |
| 1603 | IdentifierInfo *kw_weak1; |
| 1604 | IdentifierInfo *kw_wildcard; |
| 1605 | IdentifierInfo *kw_wire; |
| 1606 | IdentifierInfo *kw_with; |
| 1607 | IdentifierInfo *kw_wor; |
| 1608 | |
| 1609 | // Workaround for hashes and backticks in Verilog. |
| 1610 | IdentifierInfo *kw_verilogHash; |
| 1611 | IdentifierInfo *kw_verilogHashHash; |
| 1612 | |
| 1613 | // Symbols in Verilog that don't exist in C++. |
| 1614 | IdentifierInfo *kw_apostrophe; |
| 1615 | |
| 1616 | // TableGen keywords |
| 1617 | IdentifierInfo *kw_bit; |
| 1618 | IdentifierInfo *kw_bits; |
| 1619 | IdentifierInfo *kw_code; |
| 1620 | IdentifierInfo *kw_dag; |
| 1621 | IdentifierInfo *kw_def; |
| 1622 | IdentifierInfo *kw_defm; |
| 1623 | IdentifierInfo *kw_defset; |
| 1624 | IdentifierInfo *kw_defvar; |
| 1625 | IdentifierInfo *kw_dump; |
| 1626 | IdentifierInfo *kw_include; |
| 1627 | IdentifierInfo *kw_list; |
| 1628 | IdentifierInfo *kw_multiclass; |
| 1629 | IdentifierInfo *kw_then; |
| 1630 | |
| 1631 | /// Returns \c true if \p Tok is a keyword or an identifier. |
| 1632 | bool isWordLike(const FormatToken &Tok, bool IsVerilog = true) const { |
| 1633 | // getIdentifierinfo returns non-null for keywords as well as identifiers. |
| 1634 | return Tok.Tok.getIdentifierInfo() && |
| 1635 | (!IsVerilog || !isVerilogKeywordSymbol(Tok)); |
| 1636 | } |
| 1637 | |
| 1638 | /// Returns \c true if \p Tok is a true JavaScript identifier, returns |
| 1639 | /// \c false if it is a keyword or a pseudo keyword. |
| 1640 | /// If \c AcceptIdentifierName is true, returns true not only for keywords, |
| 1641 | // but also for IdentifierName tokens (aka pseudo-keywords), such as |
| 1642 | // ``yield``. |
| 1643 | bool isJavaScriptIdentifier(const FormatToken &Tok, |
| 1644 | bool AcceptIdentifierName = true) const { |
| 1645 | // Based on the list of JavaScript & TypeScript keywords here: |
| 1646 | // https://github.com/microsoft/TypeScript/blob/main/src/compiler/scanner.ts#L74 |
| 1647 | if (Tok.isAccessSpecifierKeyword()) |
| 1648 | return false; |
| 1649 | switch (Tok.Tok.getKind()) { |
| 1650 | case tok::kw_break: |
| 1651 | case tok::kw_case: |
| 1652 | case tok::kw_catch: |
| 1653 | case tok::kw_class: |
| 1654 | case tok::kw_continue: |
| 1655 | case tok::kw_const: |
| 1656 | case tok::kw_default: |
| 1657 | case tok::kw_delete: |
| 1658 | case tok::kw_do: |
| 1659 | case tok::kw_else: |
| 1660 | case tok::kw_enum: |
| 1661 | case tok::kw_export: |
| 1662 | case tok::kw_false: |
| 1663 | case tok::kw_for: |
| 1664 | case tok::kw_if: |
| 1665 | case tok::kw_import: |
| 1666 | case tok::kw_module: |
| 1667 | case tok::kw_new: |
| 1668 | case tok::kw_return: |
| 1669 | case tok::kw_static: |
| 1670 | case tok::kw_switch: |
| 1671 | case tok::kw_this: |
| 1672 | case tok::kw_throw: |
| 1673 | case tok::kw_true: |
| 1674 | case tok::kw_try: |
| 1675 | case tok::kw_typeof: |
| 1676 | case tok::kw_void: |
| 1677 | case tok::kw_while: |
| 1678 | // These are JS keywords that are lexed by LLVM/clang as keywords. |
| 1679 | return false; |
| 1680 | case tok::identifier: { |
| 1681 | // For identifiers, make sure they are true identifiers, excluding the |
| 1682 | // JavaScript pseudo-keywords (not lexed by LLVM/clang as keywords). |
| 1683 | bool IsPseudoKeyword = |
| 1684 | JsExtraKeywords.find(x: Tok.Tok.getIdentifierInfo()) != |
| 1685 | JsExtraKeywords.end(); |
| 1686 | return AcceptIdentifierName || !IsPseudoKeyword; |
| 1687 | } |
| 1688 | default: |
| 1689 | // Other keywords are handled in the switch below, to avoid problems due |
| 1690 | // to duplicate case labels when using the #include trick. |
| 1691 | break; |
| 1692 | } |
| 1693 | |
| 1694 | switch (Tok.Tok.getKind()) { |
| 1695 | // Handle C++ keywords not included above: these are all JS identifiers. |
| 1696 | #define KEYWORD(X, Y) case tok::kw_##X: |
| 1697 | #include "clang/Basic/TokenKinds.def" |
| 1698 | // #undef KEYWORD is not needed -- it's #undef-ed at the end of |
| 1699 | // TokenKinds.def |
| 1700 | return true; |
| 1701 | default: |
| 1702 | // All other tokens (punctuation etc) are not JS identifiers. |
| 1703 | return false; |
| 1704 | } |
| 1705 | } |
| 1706 | |
| 1707 | /// Returns \c true if \p Tok is a C# keyword, returns \c false if it is |
| 1708 | /// anything else. |
| 1709 | bool isCSharpKeyword(const FormatToken &Tok) const { |
| 1710 | if (Tok.isAccessSpecifierKeyword()) |
| 1711 | return true; |
| 1712 | switch (Tok.Tok.getKind()) { |
| 1713 | case tok::kw_bool: |
| 1714 | case tok::kw_break: |
| 1715 | case tok::kw_case: |
| 1716 | case tok::kw_catch: |
| 1717 | case tok::kw_char: |
| 1718 | case tok::kw_class: |
| 1719 | case tok::kw_const: |
| 1720 | case tok::kw_continue: |
| 1721 | case tok::kw_default: |
| 1722 | case tok::kw_do: |
| 1723 | case tok::kw_double: |
| 1724 | case tok::kw_else: |
| 1725 | case tok::kw_enum: |
| 1726 | case tok::kw_explicit: |
| 1727 | case tok::kw_extern: |
| 1728 | case tok::kw_false: |
| 1729 | case tok::kw_float: |
| 1730 | case tok::kw_for: |
| 1731 | case tok::kw_goto: |
| 1732 | case tok::kw_if: |
| 1733 | case tok::kw_int: |
| 1734 | case tok::kw_long: |
| 1735 | case tok::kw_namespace: |
| 1736 | case tok::kw_new: |
| 1737 | case tok::kw_operator: |
| 1738 | case tok::kw_return: |
| 1739 | case tok::kw_short: |
| 1740 | case tok::kw_sizeof: |
| 1741 | case tok::kw_static: |
| 1742 | case tok::kw_struct: |
| 1743 | case tok::kw_switch: |
| 1744 | case tok::kw_this: |
| 1745 | case tok::kw_throw: |
| 1746 | case tok::kw_true: |
| 1747 | case tok::kw_try: |
| 1748 | case tok::kw_typeof: |
| 1749 | case tok::kw_using: |
| 1750 | case tok::kw_virtual: |
| 1751 | case tok::kw_void: |
| 1752 | case tok::kw_volatile: |
| 1753 | case tok::kw_while: |
| 1754 | return true; |
| 1755 | default: |
| 1756 | return Tok.is(Kind: tok::identifier) && |
| 1757 | CSharpExtraKeywords.find(x: Tok.Tok.getIdentifierInfo()) == |
| 1758 | CSharpExtraKeywords.end(); |
| 1759 | } |
| 1760 | } |
| 1761 | |
| 1762 | bool isVerilogKeywordSymbol(const FormatToken &Tok) const { |
| 1763 | return Tok.isOneOf(K1: kw_verilogHash, K2: kw_verilogHashHash, Ks: kw_apostrophe); |
| 1764 | } |
| 1765 | |
| 1766 | bool isVerilogWordOperator(const FormatToken &Tok) const { |
| 1767 | return Tok.isOneOf(K1: kw_before, K2: kw_intersect, Ks: kw_dist, Ks: kw_iff, Ks: kw_inside, |
| 1768 | Ks: kw_with); |
| 1769 | } |
| 1770 | |
| 1771 | bool isVerilogIdentifier(const FormatToken &Tok) const { |
| 1772 | switch (Tok.Tok.getKind()) { |
| 1773 | case tok::kw_case: |
| 1774 | case tok::kw_class: |
| 1775 | case tok::kw_const: |
| 1776 | case tok::kw_continue: |
| 1777 | case tok::kw_default: |
| 1778 | case tok::kw_do: |
| 1779 | case tok::kw_extern: |
| 1780 | case tok::kw_else: |
| 1781 | case tok::kw_enum: |
| 1782 | case tok::kw_for: |
| 1783 | case tok::kw_if: |
| 1784 | case tok::kw_restrict: |
| 1785 | case tok::kw_signed: |
| 1786 | case tok::kw_static: |
| 1787 | case tok::kw_struct: |
| 1788 | case tok::kw_typedef: |
| 1789 | case tok::kw_union: |
| 1790 | case tok::kw_unsigned: |
| 1791 | case tok::kw_virtual: |
| 1792 | case tok::kw_while: |
| 1793 | return false; |
| 1794 | case tok::identifier: |
| 1795 | return isWordLike(Tok) && |
| 1796 | VerilogExtraKeywords.find(x: Tok.Tok.getIdentifierInfo()) == |
| 1797 | VerilogExtraKeywords.end(); |
| 1798 | default: |
| 1799 | // getIdentifierInfo returns non-null for both identifiers and keywords. |
| 1800 | return Tok.Tok.getIdentifierInfo(); |
| 1801 | } |
| 1802 | } |
| 1803 | |
| 1804 | /// Returns whether \p Tok is a Verilog preprocessor directive. This is |
| 1805 | /// needed because macro expansions start with a backtick as well and they |
| 1806 | /// need to be treated differently. |
| 1807 | bool isVerilogPPDirective(const FormatToken &Tok) const { |
| 1808 | auto Info = Tok.Tok.getIdentifierInfo(); |
| 1809 | if (!Info) |
| 1810 | return false; |
| 1811 | switch (Info->getPPKeywordID()) { |
| 1812 | case tok::pp_define: |
| 1813 | case tok::pp_else: |
| 1814 | case tok::pp_endif: |
| 1815 | case tok::pp_ifdef: |
| 1816 | case tok::pp_ifndef: |
| 1817 | case tok::pp_include: |
| 1818 | case tok::pp_line: |
| 1819 | case tok::pp_pragma: |
| 1820 | case tok::pp_undef: |
| 1821 | return true; |
| 1822 | default: |
| 1823 | return Tok.isOneOf(K1: kw_begin_keywords, K2: kw_celldefine, |
| 1824 | Ks: kw_default_decay_time, Ks: kw_default_nettype, |
| 1825 | Ks: kw_default_trireg_strength, Ks: kw_delay_mode_distributed, |
| 1826 | Ks: kw_delay_mode_path, Ks: kw_delay_mode_unit, |
| 1827 | Ks: kw_delay_mode_zero, Ks: kw_elsif, Ks: kw_end_keywords, |
| 1828 | Ks: kw_endcelldefine, Ks: kw_nounconnected_drive, Ks: kw_resetall, |
| 1829 | Ks: kw_timescale, Ks: kw_unconnected_drive, Ks: kw_undefineall); |
| 1830 | } |
| 1831 | } |
| 1832 | |
| 1833 | /// Returns whether \p Tok is a Verilog keyword that opens a block. |
| 1834 | bool isVerilogBegin(const FormatToken &Tok) const { |
| 1835 | // `table` is not included since it needs to be treated specially. |
| 1836 | if (Tok.isOneOf(K1: kw_begin, K2: kw_generate, Ks: kw_specify)) |
| 1837 | return true; |
| 1838 | if (Tok.isNot(Kind: kw_fork)) |
| 1839 | return false; |
| 1840 | const auto *Prev = Tok.getPreviousNonComment(); |
| 1841 | return !(Prev && Prev->isOneOf(K1: kw_disable, K2: kw_wait)); |
| 1842 | } |
| 1843 | |
| 1844 | /// Returns whether \p Tok is a Verilog keyword that closes a block. |
| 1845 | bool isVerilogEnd(const FormatToken &Tok) const { |
| 1846 | return !Tok.endsSequence(K1: kw_join, Tokens: kw_rand) && |
| 1847 | Tok.isOneOf(K1: TT_MacroBlockEnd, K2: kw_end, Ks: kw_endcase, Ks: kw_endclass, |
| 1848 | Ks: kw_endclocking, Ks: kw_endchecker, Ks: kw_endfunction, |
| 1849 | Ks: kw_endgenerate, Ks: kw_endgroup, Ks: kw_endinterface, |
| 1850 | Ks: kw_endmodule, Ks: kw_endpackage, Ks: kw_endprimitive, |
| 1851 | Ks: kw_endprogram, Ks: kw_endproperty, Ks: kw_endsequence, |
| 1852 | Ks: kw_endspecify, Ks: kw_endtable, Ks: kw_endtask, Ks: kw_join, |
| 1853 | Ks: kw_join_any, Ks: kw_join_none); |
| 1854 | } |
| 1855 | |
| 1856 | /// Returns whether \p Tok is a Verilog keyword that opens a module, etc. |
| 1857 | bool isVerilogHierarchy(const FormatToken &Tok) const { |
| 1858 | if (Tok.endsSequence(K1: kw_function, Tokens: kw_with)) |
| 1859 | return false; |
| 1860 | if (Tok.is(II: kw_property)) { |
| 1861 | const FormatToken *Prev = Tok.getPreviousNonComment(); |
| 1862 | return !(Prev && |
| 1863 | Prev->isOneOf(K1: tok::kw_restrict, K2: kw_assert, Ks: kw_assume, Ks: kw_cover)); |
| 1864 | } |
| 1865 | return Tok.isOneOf(K1: tok::kw_case, K2: tok::kw_class, Ks: kw_function, Ks: kw_module, |
| 1866 | Ks: kw_interface, Ks: kw_package, Ks: kw_casex, Ks: kw_casez, Ks: kw_checker, |
| 1867 | Ks: kw_clocking, Ks: kw_covergroup, Ks: kw_macromodule, Ks: kw_primitive, |
| 1868 | Ks: kw_program, Ks: kw_property, Ks: kw_randcase, Ks: kw_randsequence, |
| 1869 | Ks: kw_task); |
| 1870 | } |
| 1871 | |
| 1872 | bool isVerilogEndOfLabel(const FormatToken &Tok) const { |
| 1873 | const FormatToken *Next = Tok.getNextNonComment(); |
| 1874 | // In Verilog the colon in a default label is optional. |
| 1875 | return Tok.is(TT: TT_CaseLabelColon) || |
| 1876 | (Tok.is(Kind: tok::kw_default) && |
| 1877 | !(Next && Next->isOneOf(K1: tok::colon, K2: tok::semi, Ks: kw_clocking, Ks: kw_iff, |
| 1878 | Ks: kw_input, Ks: kw_output, Ks: kw_sequence))); |
| 1879 | } |
| 1880 | |
| 1881 | /// Returns whether \p Tok is a Verilog keyword that starts a |
| 1882 | /// structured procedure like 'always'. |
| 1883 | bool isVerilogStructuredProcedure(const FormatToken &Tok) const { |
| 1884 | return Tok.isOneOf(K1: kw_always, K2: kw_always_comb, Ks: kw_always_ff, Ks: kw_always_latch, |
| 1885 | Ks: kw_final, Ks: kw_forever, Ks: kw_initial); |
| 1886 | } |
| 1887 | |
| 1888 | bool isVerilogQualifier(const FormatToken &Tok) const { |
| 1889 | switch (Tok.Tok.getKind()) { |
| 1890 | case tok::kw_extern: |
| 1891 | case tok::kw_signed: |
| 1892 | case tok::kw_static: |
| 1893 | case tok::kw_unsigned: |
| 1894 | case tok::kw_virtual: |
| 1895 | return true; |
| 1896 | case tok::identifier: |
| 1897 | return Tok.isOneOf( |
| 1898 | K1: kw_let, K2: kw_var, Ks: kw_ref, Ks: kw_automatic, Ks: kw_bins, Ks: kw_coverpoint, |
| 1899 | Ks: kw_ignore_bins, Ks: kw_illegal_bins, Ks: kw_inout, Ks: kw_input, Ks: kw_interconnect, |
| 1900 | Ks: kw_local, Ks: kw_localparam, Ks: kw_output, Ks: kw_parameter, Ks: kw_pure, Ks: kw_rand, |
| 1901 | Ks: kw_randc, Ks: kw_scalared, Ks: kw_specparam, Ks: kw_tri, Ks: kw_tri0, Ks: kw_tri1, |
| 1902 | Ks: kw_triand, Ks: kw_trior, Ks: kw_trireg, Ks: kw_uwire, Ks: kw_vectored, Ks: kw_wand, |
| 1903 | Ks: kw_wildcard, Ks: kw_wire, Ks: kw_wor); |
| 1904 | default: |
| 1905 | return false; |
| 1906 | } |
| 1907 | } |
| 1908 | |
| 1909 | bool isTableGenDefinition(const FormatToken &Tok) const { |
| 1910 | return Tok.isOneOf(K1: kw_def, K2: kw_defm, Ks: kw_defset, Ks: kw_defvar, Ks: kw_multiclass, |
| 1911 | Ks: kw_let, Ks: tok::kw_class); |
| 1912 | } |
| 1913 | |
| 1914 | bool isTableGenKeyword(const FormatToken &Tok) const { |
| 1915 | switch (Tok.Tok.getKind()) { |
| 1916 | case tok::kw_class: |
| 1917 | case tok::kw_else: |
| 1918 | case tok::kw_false: |
| 1919 | case tok::kw_if: |
| 1920 | case tok::kw_int: |
| 1921 | case tok::kw_true: |
| 1922 | return true; |
| 1923 | default: |
| 1924 | return Tok.is(Kind: tok::identifier) && |
| 1925 | TableGenExtraKeywords.find(x: Tok.Tok.getIdentifierInfo()) != |
| 1926 | TableGenExtraKeywords.end(); |
| 1927 | } |
| 1928 | } |
| 1929 | |
| 1930 | private: |
| 1931 | /// The JavaScript keywords beyond the C++ keyword set. |
| 1932 | std::unordered_set<IdentifierInfo *> ; |
| 1933 | |
| 1934 | /// The C# keywords beyond the C++ keyword set. |
| 1935 | std::unordered_set<IdentifierInfo *> ; |
| 1936 | |
| 1937 | /// The Verilog keywords beyond the C++ keyword set. |
| 1938 | std::unordered_set<IdentifierInfo *> ; |
| 1939 | |
| 1940 | /// The TableGen keywords beyond the C++ keyword set. |
| 1941 | std::unordered_set<IdentifierInfo *> ; |
| 1942 | }; |
| 1943 | |
| 1944 | inline bool (const FormatToken &FormatTok) { |
| 1945 | return FormatTok.is(Kind: tok::comment) && !FormatTok.TokenText.starts_with(Prefix: "/*" ); |
| 1946 | } |
| 1947 | |
| 1948 | // Checks if \p FormatTok is a line comment that continues the line comment |
| 1949 | // \p Previous. The original column of \p MinColumnToken is used to determine |
| 1950 | // whether \p FormatTok is indented enough to the right to continue \p Previous. |
| 1951 | inline bool (const FormatToken &FormatTok, |
| 1952 | const FormatToken *Previous, |
| 1953 | const FormatToken *MinColumnToken) { |
| 1954 | if (!Previous || !MinColumnToken) |
| 1955 | return false; |
| 1956 | unsigned MinContinueColumn = |
| 1957 | MinColumnToken->OriginalColumn + (isLineComment(FormatTok: *MinColumnToken) ? 0 : 1); |
| 1958 | return isLineComment(FormatTok) && FormatTok.NewlinesBefore == 1 && |
| 1959 | isLineComment(FormatTok: *Previous) && |
| 1960 | FormatTok.OriginalColumn >= MinContinueColumn; |
| 1961 | } |
| 1962 | |
| 1963 | // Returns \c true if \c Current starts a new parameter. |
| 1964 | bool startsNextParameter(const FormatToken &Current, const FormatStyle &Style); |
| 1965 | |
| 1966 | } // namespace format |
| 1967 | } // namespace clang |
| 1968 | |
| 1969 | #endif |
| 1970 | |