Skip to content

Commit 700ea0d

Browse files
committed
[clang][TypePrinter][NFC] Turn SuppressTagKeyword into an enum
In preparation for a follow-up patch that adds a new mode to this enum. (cherry picked from commit 8fd09a8)
1 parent 9653cd0 commit 700ea0d

File tree

17 files changed

+114
-66
lines changed

17 files changed

+114
-66
lines changed

clang-tools-extra/clang-tidy/performance/MoveConstArgCheck.cpp

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -187,7 +187,8 @@ void MoveConstArgCheck::check(const MatchFinder::MatchResult &Result) {
187187

188188
QualType NoRefType = (*InvocationParmType)->getPointeeType();
189189
PrintingPolicy PolicyWithSuppressedTag(getLangOpts());
190-
PolicyWithSuppressedTag.SuppressTagKeyword = true;
190+
PolicyWithSuppressedTag.SuppressTagKeyword = llvm::to_underlying(
191+
PrintingPolicy::SuppressTagKeywordMode::InElaboratedNames);
191192
PolicyWithSuppressedTag.SuppressUnwrittenScope = true;
192193
std::string ExpectParmTypeName =
193194
NoRefType.getAsString(PolicyWithSuppressedTag);

clang-tools-extra/clang-tidy/readability/StaticAccessedThroughInstanceCheck.cpp

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -72,7 +72,8 @@ void StaticAccessedThroughInstanceCheck::check(
7272

7373
const ASTContext *AstContext = Result.Context;
7474
PrintingPolicy PrintingPolicyWithSuppressedTag(AstContext->getLangOpts());
75-
PrintingPolicyWithSuppressedTag.SuppressTagKeyword = true;
75+
PrintingPolicyWithSuppressedTag.SuppressTagKeyword = llvm::to_underlying(
76+
PrintingPolicy::SuppressTagKeywordMode::InElaboratedNames);
7677
PrintingPolicyWithSuppressedTag.SuppressUnwrittenScope = true;
7778

7879
PrintingPolicyWithSuppressedTag.PrintAsCanonical =

clang-tools-extra/clang-tidy/utils/Matchers.cpp

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -35,7 +35,8 @@ bool MatchesAnyListedTypeNameMatcher::matches(
3535
PrintingPolicyWithSuppressedTag.PrintAsCanonical = CanonicalTypes;
3636
PrintingPolicyWithSuppressedTag.FullyQualifiedName = true;
3737
PrintingPolicyWithSuppressedTag.SuppressScope = false;
38-
PrintingPolicyWithSuppressedTag.SuppressTagKeyword = true;
38+
PrintingPolicyWithSuppressedTag.SuppressTagKeyword = llvm::to_underlying(
39+
PrintingPolicy::SuppressTagKeywordMode::InElaboratedNames);
3940
PrintingPolicyWithSuppressedTag.SuppressUnwrittenScope = true;
4041
std::string TypeName =
4142
Node.getUnqualifiedType().getAsString(PrintingPolicyWithSuppressedTag);

clang-tools-extra/clangd/AST.cpp

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -419,7 +419,8 @@ std::string printType(const QualType QT, const DeclContext &CurContext,
419419
std::string Result;
420420
llvm::raw_string_ostream OS(Result);
421421
PrintingPolicy PP(CurContext.getParentASTContext().getPrintingPolicy());
422-
PP.SuppressTagKeyword = true;
422+
PP.SuppressTagKeyword = llvm::to_underlying(
423+
PrintingPolicy::SuppressTagKeywordMode::InElaboratedNames);
423424
PP.SuppressUnwrittenScope = true;
424425
PP.FullyQualifiedName = FullyQualify;
425426

clang-tools-extra/clangd/Hover.cpp

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -176,7 +176,10 @@ HoverInfo::PrintedType printType(QualType QT, ASTContext &ASTCtx,
176176
// tag for extra clarity. This isn't very idiomatic, so don't attempt it for
177177
// complex cases, including pointers/references, template specializations,
178178
// etc.
179-
if (!QT.isNull() && !QT.hasQualifiers() && PP.SuppressTagKeyword) {
179+
if (!QT.isNull() && !QT.hasQualifiers() &&
180+
PP.SuppressTagKeyword ==
181+
llvm::to_underlying(
182+
PrintingPolicy::SuppressTagKeywordMode::InElaboratedNames)) {
180183
if (auto *TT = llvm::dyn_cast<TagType>(QT.getTypePtr());
181184
TT && TT->isCanonicalUnqualified())
182185
OS << TT->getDecl()->getKindName() << " ";

clang/include/clang/AST/PrettyPrinter.h

Lines changed: 7 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -57,11 +57,14 @@ class PrintingCallbacks {
5757
/// It is very frequently copied.
5858
struct PrintingPolicy {
5959
enum class SuppressInlineNamespaceMode : uint8_t { None, Redundant, All };
60+
enum class SuppressTagKeywordMode : uint8_t { None, InElaboratedNames };
6061

6162
/// Create a default printing policy for the specified language.
6263
PrintingPolicy(const LangOptions &LO)
6364
: Indentation(2), SuppressSpecifiers(false),
64-
SuppressTagKeyword(LO.CPlusPlus),
65+
SuppressTagKeyword(llvm::to_underlying(
66+
LO.CPlusPlus ? SuppressTagKeywordMode::InElaboratedNames
67+
: SuppressTagKeywordMode::None)),
6568
SuppressTagKeywordInAnonymousTagNames(false),
6669
IncludeTagDefinition(false), SuppressScope(false),
6770
SuppressUnwrittenScope(false),
@@ -90,7 +93,8 @@ struct PrintingPolicy {
9093
/// construct). This should not be used if a real LangOptions object is
9194
/// available.
9295
void adjustForCPlusPlus() {
93-
SuppressTagKeyword = true;
96+
SuppressTagKeyword =
97+
llvm::to_underlying(SuppressTagKeywordMode::InElaboratedNames);
9498
Bool = true;
9599
UseVoidForZeroParams = false;
96100
}
@@ -123,7 +127,7 @@ struct PrintingPolicy {
123127
/// \code
124128
/// struct Geometry::Point;
125129
/// \endcode
126-
LLVM_PREFERRED_TYPE(bool)
130+
LLVM_PREFERRED_TYPE(SuppressTagKeywordMode)
127131
unsigned SuppressTagKeyword : 1;
128132
unsigned SuppressTagKeywordInAnonymousTagNames : 1;
129133

clang/lib/AST/Expr.cpp

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -759,7 +759,10 @@ std::string PredefinedExpr::ComputeName(PredefinedIdentKind IK,
759759
PrettyCallbacks PrettyCB(Context.getLangOpts());
760760
Policy.Callbacks = &PrettyCB;
761761
if (IK == PredefinedIdentKind::Function && ForceElaboratedPrinting)
762-
Policy.SuppressTagKeyword = !LO.MSVCCompat;
762+
Policy.SuppressTagKeyword = llvm::to_underlying(
763+
LO.MSVCCompat
764+
? PrintingPolicy::SuppressTagKeywordMode::None
765+
: PrintingPolicy::SuppressTagKeywordMode::InElaboratedNames);
763766
std::string Proto;
764767
llvm::raw_string_ostream POut(Proto);
765768

clang/lib/AST/InferAlloc.cpp

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -184,7 +184,8 @@ infer_alloc::getAllocTokenMetadata(QualType T, const ASTContext &Ctx) {
184184

185185
// Get unique type name.
186186
PrintingPolicy Policy(Ctx.getLangOpts());
187-
Policy.SuppressTagKeyword = true;
187+
Policy.SuppressTagKeyword = llvm::to_underlying(
188+
PrintingPolicy::SuppressTagKeywordMode::InElaboratedNames);
188189
Policy.FullyQualifiedName = true;
189190
llvm::raw_svector_ostream TypeNameOS(ATMD.TypeName);
190191
T.getCanonicalType().print(TypeNameOS, Policy);

clang/lib/AST/NestedNameSpecifier.cpp

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -111,7 +111,8 @@ void NestedNameSpecifier::print(raw_ostream &OS, const PrintingPolicy &Policy,
111111
break;
112112
case Kind::Type: {
113113
PrintingPolicy InnerPolicy(Policy);
114-
InnerPolicy.SuppressTagKeyword = true;
114+
InnerPolicy.SuppressTagKeyword = llvm::to_underlying(
115+
PrintingPolicy::SuppressTagKeywordMode::InElaboratedNames);
115116
QualType(getAsType(), 0).print(OS, InnerPolicy);
116117
break;
117118
}

clang/lib/AST/TypePrinter.cpp

Lines changed: 9 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -93,19 +93,21 @@ class DefaultTemplateArgsPolicyRAII {
9393

9494
class ElaboratedTypePolicyRAII {
9595
PrintingPolicy &Policy;
96-
bool SuppressTagKeyword;
96+
PrintingPolicy::SuppressTagKeywordMode SuppressTagKeyword;
9797
bool SuppressScope;
9898

9999
public:
100100
explicit ElaboratedTypePolicyRAII(PrintingPolicy &Policy) : Policy(Policy) {
101-
SuppressTagKeyword = Policy.SuppressTagKeyword;
101+
SuppressTagKeyword = static_cast<PrintingPolicy::SuppressTagKeywordMode>(
102+
Policy.SuppressTagKeyword);
102103
SuppressScope = Policy.SuppressScope;
103-
Policy.SuppressTagKeyword = true;
104+
Policy.SuppressTagKeyword = llvm::to_underlying(
105+
PrintingPolicy::SuppressTagKeywordMode::InElaboratedNames);
104106
Policy.SuppressScope = true;
105107
}
106108

107109
~ElaboratedTypePolicyRAII() {
108-
Policy.SuppressTagKeyword = SuppressTagKeyword;
110+
Policy.SuppressTagKeyword = llvm::to_underlying(SuppressTagKeyword);
109111
Policy.SuppressScope = SuppressScope;
110112
}
111113
};
@@ -1521,7 +1523,9 @@ void TypePrinter::printTagType(const TagType *T, raw_ostream &OS) {
15211523

15221524
bool PrintedKindDecoration = false;
15231525
if (T->isCanonicalUnqualified()) {
1524-
if (!Policy.SuppressTagKeyword && !D->getTypedefNameForAnonDecl()) {
1526+
if (Policy.SuppressTagKeyword ==
1527+
llvm::to_underlying(PrintingPolicy::SuppressTagKeywordMode::None) &&
1528+
!D->getTypedefNameForAnonDecl()) {
15251529
PrintedKindDecoration = true;
15261530
OS << D->getKindName();
15271531
OS << ' ';

0 commit comments

Comments
 (0)