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