Skip to content

Commit 2ec71d9

Browse files
authored
[clang] NFC: introduce Type::getAsEnumDecl, and cast variants for all TagDecls (#155463)
And make use of those. These changes are split from prior PR #155028, in order to decrease the size of that PR and facilitate review.
1 parent 2bfbae9 commit 2ec71d9

File tree

91 files changed

+430
-820
lines changed

Some content is hidden

Large Commits have some content hidden by default. Use the searchbox below for content that may be hidden.

91 files changed

+430
-820
lines changed

clang-tools-extra/clang-tidy/bugprone/TaggedUnionMemberCountCheck.cpp

Lines changed: 2 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -169,15 +169,8 @@ void TaggedUnionMemberCountCheck::check(
169169
if (!Root || !UnionField || !TagField)
170170
return;
171171

172-
const auto *UnionDef =
173-
UnionField->getType().getCanonicalType().getTypePtr()->getAsRecordDecl();
174-
const auto *EnumDef = llvm::dyn_cast<EnumDecl>(
175-
TagField->getType().getCanonicalType().getTypePtr()->getAsTagDecl());
176-
177-
assert(UnionDef && "UnionDef is missing!");
178-
assert(EnumDef && "EnumDef is missing!");
179-
if (!UnionDef || !EnumDef)
180-
return;
172+
const auto *UnionDef = UnionField->getType()->castAsRecordDecl();
173+
const auto *EnumDef = TagField->getType()->castAsEnumDecl();
181174

182175
const std::size_t UnionMemberCount = llvm::range_size(UnionDef->fields());
183176
auto [TagCount, CountingEnumConstantDecl] = getNumberOfEnumValues(EnumDef);

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

Lines changed: 1 addition & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -66,10 +66,7 @@ ExceptionSpecAnalyzer::analyzeBase(const CXXBaseSpecifier &Base,
6666
if (!RecType)
6767
return State::Unknown;
6868

69-
const auto *BaseClass =
70-
cast<CXXRecordDecl>(RecType->getOriginalDecl())->getDefinitionOrSelf();
71-
72-
return analyzeRecord(BaseClass, Kind);
69+
return analyzeRecord(RecType->getAsCXXRecordDecl(), Kind);
7370
}
7471

7572
ExceptionSpecAnalyzer::State

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

Lines changed: 3 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -460,10 +460,9 @@ bool FormatStringConverter::emitIntegerArgument(
460460
// be passed as its underlying type. However, printf will have forced
461461
// the signedness based on the format string, so we need to do the
462462
// same.
463-
if (const auto *ET = ArgType->getAs<EnumType>()) {
464-
if (const std::optional<std::string> MaybeCastType = castTypeForArgument(
465-
ArgKind,
466-
ET->getOriginalDecl()->getDefinitionOrSelf()->getIntegerType()))
463+
if (const auto *ED = ArgType->getAsEnumDecl()) {
464+
if (const std::optional<std::string> MaybeCastType =
465+
castTypeForArgument(ArgKind, ED->getIntegerType()))
467466
ArgFixes.emplace_back(
468467
ArgIndex, (Twine("static_cast<") + *MaybeCastType + ">(").str());
469468
else

clang-tools-extra/clangd/Hover.cpp

Lines changed: 2 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -454,8 +454,7 @@ std::optional<std::string> printExprValue(const Expr *E,
454454
Constant.Val.getInt().getSignificantBits() <= 64) {
455455
// Compare to int64_t to avoid bit-width match requirements.
456456
int64_t Val = Constant.Val.getInt().getExtValue();
457-
for (const EnumConstantDecl *ECD :
458-
T->castAs<EnumType>()->getOriginalDecl()->enumerators())
457+
for (const EnumConstantDecl *ECD : T->castAsEnumDecl()->enumerators())
459458
if (ECD->getInitVal() == Val)
460459
return llvm::formatv("{0} ({1})", ECD->getNameAsString(),
461460
printHex(Constant.Val.getInt()))
@@ -832,7 +831,7 @@ std::optional<HoverInfo> getThisExprHoverContents(const CXXThisExpr *CTE,
832831
ASTContext &ASTCtx,
833832
const PrintingPolicy &PP) {
834833
QualType OriginThisType = CTE->getType()->getPointeeType();
835-
QualType ClassType = declaredType(OriginThisType->getAsTagDecl());
834+
QualType ClassType = declaredType(OriginThisType->castAsTagDecl());
836835
// For partial specialization class, origin `this` pointee type will be
837836
// parsed as `InjectedClassNameType`, which will ouput template arguments
838837
// like "type-parameter-0-0". So we retrieve user written class type in this

clang/include/clang/AST/Decl.h

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -3915,6 +3915,10 @@ class TagDecl : public TypeDecl,
39153915
bool isUnion() const { return getTagKind() == TagTypeKind::Union; }
39163916
bool isEnum() const { return getTagKind() == TagTypeKind::Enum; }
39173917

3918+
bool isStructureOrClass() const {
3919+
return isStruct() || isClass() || isInterface();
3920+
}
3921+
39183922
/// Is this tag type named, either directly or via being defined in
39193923
/// a typedef of this type?
39203924
///

clang/include/clang/AST/Type.h

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2883,14 +2883,21 @@ class alignas(TypeAlignment) Type : public ExtQualsTypeCommonBase {
28832883
/// because the type is a RecordType or because it is the injected-class-name
28842884
/// type of a class template or class template partial specialization.
28852885
CXXRecordDecl *getAsCXXRecordDecl() const;
2886+
CXXRecordDecl *castAsCXXRecordDecl() const;
28862887

28872888
/// Retrieves the RecordDecl this type refers to.
28882889
RecordDecl *getAsRecordDecl() const;
2890+
RecordDecl *castAsRecordDecl() const;
2891+
2892+
/// Retrieves the EnumDecl this type refers to.
2893+
EnumDecl *getAsEnumDecl() const;
2894+
EnumDecl *castAsEnumDecl() const;
28892895

28902896
/// Retrieves the TagDecl that this type refers to, either
28912897
/// because the type is a TagType or because it is the injected-class-name
28922898
/// type of a class template or class template partial specialization.
28932899
TagDecl *getAsTagDecl() const;
2900+
TagDecl *castAsTagDecl() const;
28942901

28952902
/// If this is a pointer or reference to a RecordType, return the
28962903
/// CXXRecordDecl that the type refers to.

clang/lib/AST/APValue.cpp

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -903,8 +903,7 @@ void APValue::printPretty(raw_ostream &Out, const PrintingPolicy &Policy,
903903
case APValue::Struct: {
904904
Out << '{';
905905
bool First = true;
906-
const RecordDecl *RD =
907-
Ty->castAs<RecordType>()->getOriginalDecl()->getDefinitionOrSelf();
906+
const auto *RD = Ty->castAsRecordDecl();
908907
if (unsigned N = getStructNumBases()) {
909908
const CXXRecordDecl *CD = cast<CXXRecordDecl>(RD);
910909
CXXRecordDecl::base_class_const_iterator BI = CD->bases_begin();

clang/lib/AST/ASTContext.cpp

Lines changed: 17 additions & 26 deletions
Original file line numberDiff line numberDiff line change
@@ -2001,8 +2001,7 @@ bool ASTContext::isPromotableIntegerType(QualType T) const {
20012001

20022002
// Enumerated types are promotable to their compatible integer types
20032003
// (C99 6.3.1.1) a.k.a. its underlying type (C++ [conv.prom]p2).
2004-
if (const auto *ET = T->getAs<EnumType>()) {
2005-
const EnumDecl *ED = ET->getOriginalDecl()->getDefinitionOrSelf();
2004+
if (const auto *ED = T->getAsEnumDecl()) {
20062005
if (T->isDependentType() || ED->getPromotionType().isNull() ||
20072006
ED->isScoped())
20082007
return false;
@@ -2712,11 +2711,8 @@ unsigned ASTContext::getPreferredTypeAlign(const Type *T) const {
27122711
// possible.
27132712
if (const auto *CT = T->getAs<ComplexType>())
27142713
T = CT->getElementType().getTypePtr();
2715-
if (const auto *ET = T->getAs<EnumType>())
2716-
T = ET->getOriginalDecl()
2717-
->getDefinitionOrSelf()
2718-
->getIntegerType()
2719-
.getTypePtr();
2714+
if (const auto *ED = T->getAsEnumDecl())
2715+
T = ED->getIntegerType().getTypePtr();
27202716
if (T->isSpecificBuiltinType(BuiltinType::Double) ||
27212717
T->isSpecificBuiltinType(BuiltinType::LongLong) ||
27222718
T->isSpecificBuiltinType(BuiltinType::ULongLong) ||
@@ -3412,10 +3408,7 @@ static void encodeTypeForFunctionPointerAuth(const ASTContext &Ctx,
34123408
// type, or an unsigned integer type.
34133409
//
34143410
// So we have to treat enum types as integers.
3415-
QualType UnderlyingType = cast<EnumType>(T)
3416-
->getOriginalDecl()
3417-
->getDefinitionOrSelf()
3418-
->getIntegerType();
3411+
QualType UnderlyingType = T->castAsEnumDecl()->getIntegerType();
34193412
return encodeTypeForFunctionPointerAuth(
34203413
Ctx, OS, UnderlyingType.isNull() ? Ctx.IntTy : UnderlyingType);
34213414
}
@@ -8351,8 +8344,8 @@ QualType ASTContext::isPromotableBitField(Expr *E) const {
83518344
QualType ASTContext::getPromotedIntegerType(QualType Promotable) const {
83528345
assert(!Promotable.isNull());
83538346
assert(isPromotableIntegerType(Promotable));
8354-
if (const auto *ET = Promotable->getAs<EnumType>())
8355-
return ET->getOriginalDecl()->getDefinitionOrSelf()->getPromotionType();
8347+
if (const auto *ED = Promotable->getAsEnumDecl())
8348+
return ED->getPromotionType();
83568349

83578350
if (const auto *BT = Promotable->getAs<BuiltinType>()) {
83588351
// C++ [conv.prom]: A prvalue of type char16_t, char32_t, or wchar_t
@@ -8571,10 +8564,9 @@ QualType ASTContext::getObjCSuperType() const {
85718564
}
85728565

85738566
void ASTContext::setCFConstantStringType(QualType T) {
8574-
const auto *TD = T->castAs<TypedefType>();
8575-
CFConstantStringTypeDecl = cast<TypedefDecl>(TD->getDecl());
8576-
const auto *TagType = TD->castAs<RecordType>();
8577-
CFConstantStringTagDecl = TagType->getOriginalDecl()->getDefinitionOrSelf();
8567+
const auto *TT = T->castAs<TypedefType>();
8568+
CFConstantStringTypeDecl = cast<TypedefDecl>(TT->getDecl());
8569+
CFConstantStringTagDecl = TT->castAsRecordDecl();
85788570
}
85798571

85808572
QualType ASTContext::getBlockDescriptorType() const {
@@ -11667,9 +11659,8 @@ QualType ASTContext::mergeFunctionTypes(QualType lhs, QualType rhs,
1166711659

1166811660
// Look at the converted type of enum types, since that is the type used
1166911661
// to pass enum values.
11670-
if (const auto *Enum = paramTy->getAs<EnumType>()) {
11671-
paramTy =
11672-
Enum->getOriginalDecl()->getDefinitionOrSelf()->getIntegerType();
11662+
if (const auto *ED = paramTy->getAsEnumDecl()) {
11663+
paramTy = ED->getIntegerType();
1167311664
if (paramTy.isNull())
1167411665
return {};
1167511666
}
@@ -12260,8 +12251,8 @@ QualType ASTContext::mergeObjCGCQualifiers(QualType LHS, QualType RHS) {
1226012251
//===----------------------------------------------------------------------===//
1226112252

1226212253
unsigned ASTContext::getIntWidth(QualType T) const {
12263-
if (const auto *ET = T->getAs<EnumType>())
12264-
T = ET->getOriginalDecl()->getDefinitionOrSelf()->getIntegerType();
12254+
if (const auto *ED = T->getAsEnumDecl())
12255+
T = ED->getIntegerType();
1226512256
if (T->isBooleanType())
1226612257
return 1;
1226712258
if (const auto *EIT = T->getAs<BitIntType>())
@@ -12286,8 +12277,8 @@ QualType ASTContext::getCorrespondingUnsignedType(QualType T) const {
1228612277

1228712278
// For enums, get the underlying integer type of the enum, and let the general
1228812279
// integer type signchanging code handle it.
12289-
if (const auto *ETy = T->getAs<EnumType>())
12290-
T = ETy->getOriginalDecl()->getDefinitionOrSelf()->getIntegerType();
12280+
if (const auto *ED = T->getAsEnumDecl())
12281+
T = ED->getIntegerType();
1229112282

1229212283
switch (T->castAs<BuiltinType>()->getKind()) {
1229312284
case BuiltinType::Char_U:
@@ -12360,8 +12351,8 @@ QualType ASTContext::getCorrespondingSignedType(QualType T) const {
1236012351

1236112352
// For enums, get the underlying integer type of the enum, and let the general
1236212353
// integer type signchanging code handle it.
12363-
if (const auto *ETy = T->getAs<EnumType>())
12364-
T = ETy->getOriginalDecl()->getDefinitionOrSelf()->getIntegerType();
12354+
if (const auto *ED = T->getAsEnumDecl())
12355+
T = ED->getIntegerType();
1236512356

1236612357
switch (T->castAs<BuiltinType>()->getKind()) {
1236712358
case BuiltinType::Char_S:

clang/lib/AST/ByteCode/Compiler.cpp

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -559,8 +559,7 @@ bool Compiler<Emitter>::VisitCastExpr(const CastExpr *CE) {
559559
// Possibly diagnose casts to enum types if the target type does not
560560
// have a fixed size.
561561
if (Ctx.getLangOpts().CPlusPlus && CE->getType()->isEnumeralType()) {
562-
const auto *ET = CE->getType().getCanonicalType()->castAs<EnumType>();
563-
const auto *ED = ET->getOriginalDecl()->getDefinitionOrSelf();
562+
const auto *ED = CE->getType()->castAsEnumDecl();
564563
if (!ED->isFixed()) {
565564
if (!this->emitCheckEnumValue(*FromT, ED, CE))
566565
return false;

clang/lib/AST/ByteCode/Context.cpp

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -364,8 +364,7 @@ OptPrimType Context::classify(QualType T) const {
364364
return integralTypeToPrimTypeU(BT->getNumBits());
365365
}
366366

367-
if (const auto *ET = T->getAs<EnumType>()) {
368-
const auto *D = ET->getOriginalDecl()->getDefinitionOrSelf();
367+
if (const auto *D = T->getAsEnumDecl()) {
369368
if (!D->isComplete())
370369
return std::nullopt;
371370
return classify(D->getIntegerType());

0 commit comments

Comments
 (0)