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