[clang] [clang-tools-extra] [clang] NFC: introduce Type::getAsEnumDecl, and cast variants for all TagDecls (PR #155463)
via cfe-commits
cfe-commits at lists.llvm.org
Tue Aug 26 11:11:02 PDT 2025
llvmbot wrote:
<!--LLVM PR SUMMARY COMMENT-->
@llvm/pr-subscribers-backend-risc-v
Author: Matheus Izvekov (mizvekov)
<details>
<summary>Changes</summary>
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.
---
Patch is 157.68 KiB, truncated to 20.00 KiB below, full version: https://github.com/llvm/llvm-project/pull/155463.diff
91 Files Affected:
- (modified) clang-tools-extra/clang-tidy/bugprone/TaggedUnionMemberCountCheck.cpp (+2-9)
- (modified) clang-tools-extra/clang-tidy/utils/ExceptionSpecAnalyzer.cpp (+1-4)
- (modified) clang-tools-extra/clang-tidy/utils/FormatStringConverter.cpp (+3-4)
- (modified) clang-tools-extra/clangd/Hover.cpp (+2-3)
- (modified) clang/include/clang/AST/Decl.h (+4)
- (modified) clang/include/clang/AST/Type.h (+7)
- (modified) clang/lib/AST/APValue.cpp (+1-2)
- (modified) clang/lib/AST/ASTContext.cpp (+17-26)
- (modified) clang/lib/AST/ByteCode/Compiler.cpp (+1-2)
- (modified) clang/lib/AST/ByteCode/Context.cpp (+1-2)
- (modified) clang/lib/AST/ByteCode/Program.cpp (+1-5)
- (modified) clang/lib/AST/CXXInheritance.cpp (+3-6)
- (modified) clang/lib/AST/DeclCXX.cpp (+4-7)
- (modified) clang/lib/AST/Expr.cpp (+4-13)
- (modified) clang/lib/AST/ExprConstant.cpp (+6-18)
- (modified) clang/lib/AST/FormatString.cpp (+4-6)
- (modified) clang/lib/AST/ItaniumCXXABI.cpp (+1-2)
- (modified) clang/lib/AST/ItaniumMangle.cpp (+1-4)
- (modified) clang/lib/AST/PrintfFormatString.cpp (+2-2)
- (modified) clang/lib/AST/ScanfFormatString.cpp (+1-2)
- (modified) clang/lib/AST/TemplateBase.cpp (+2-2)
- (modified) clang/lib/AST/Type.cpp (+40-35)
- (modified) clang/lib/AST/VTTBuilder.cpp (+3-13)
- (modified) clang/lib/CIR/CodeGen/CIRGenCall.cpp (+3-8)
- (modified) clang/lib/CIR/CodeGen/CIRGenClass.cpp (+3-9)
- (modified) clang/lib/CIR/CodeGen/CIRGenExpr.cpp (+1-5)
- (modified) clang/lib/CIR/CodeGen/CIRGenExprAggregate.cpp (+1-4)
- (modified) clang/lib/CIR/CodeGen/CIRGenExprConstant.cpp (+1-3)
- (modified) clang/lib/CIR/CodeGen/CIRGenModule.cpp (+1-4)
- (modified) clang/lib/CIR/CodeGen/CIRGenTypes.cpp (+2-6)
- (modified) clang/lib/CodeGen/ABIInfoImpl.cpp (+4-4)
- (modified) clang/lib/CodeGen/CGCUDANV.cpp (+1-2)
- (modified) clang/lib/CodeGen/CGCXX.cpp (+3-13)
- (modified) clang/lib/CodeGen/CGCall.cpp (+4-12)
- (modified) clang/lib/CodeGen/CGClass.cpp (+9-37)
- (modified) clang/lib/CodeGen/CGDebugInfo.cpp (+1-2)
- (modified) clang/lib/CodeGen/CGDecl.cpp (+1-4)
- (modified) clang/lib/CodeGen/CGExpr.cpp (+4-15)
- (modified) clang/lib/CodeGen/CGExprAgg.cpp (+2-8)
- (modified) clang/lib/CodeGen/CGExprCXX.cpp (+3-7)
- (modified) clang/lib/CodeGen/CGExprConstant.cpp (+5-18)
- (modified) clang/lib/CodeGen/CGExprScalar.cpp (+2-6)
- (modified) clang/lib/CodeGen/CGNonTrivialStruct.cpp (+1-2)
- (modified) clang/lib/CodeGen/CGOpenMPRuntime.cpp (+15-26)
- (modified) clang/lib/CodeGen/CodeGenTypes.cpp (+2-6)
- (modified) clang/lib/CodeGen/HLSLBufferLayoutBuilder.cpp (+9-11)
- (modified) clang/lib/CodeGen/ItaniumCXXABI.cpp (+6-21)
- (modified) clang/lib/CodeGen/Targets/AArch64.cpp (+4-5)
- (modified) clang/lib/CodeGen/Targets/ARC.cpp (+2-2)
- (modified) clang/lib/CodeGen/Targets/ARM.cpp (+4-5)
- (modified) clang/lib/CodeGen/Targets/BPF.cpp (+4-5)
- (modified) clang/lib/CodeGen/Targets/CSKY.cpp (+2-2)
- (modified) clang/lib/CodeGen/Targets/Hexagon.cpp (+4-5)
- (modified) clang/lib/CodeGen/Targets/Lanai.cpp (+2-2)
- (modified) clang/lib/CodeGen/Targets/LoongArch.cpp (+3-6)
- (modified) clang/lib/CodeGen/Targets/Mips.cpp (+4-4)
- (modified) clang/lib/CodeGen/Targets/NVPTX.cpp (+4-4)
- (modified) clang/lib/CodeGen/Targets/PPC.cpp (+4-4)
- (modified) clang/lib/CodeGen/Targets/RISCV.cpp (+3-6)
- (modified) clang/lib/CodeGen/Targets/Sparc.cpp (+2-2)
- (modified) clang/lib/CodeGen/Targets/SystemZ.cpp (+4-6)
- (modified) clang/lib/CodeGen/Targets/WebAssembly.cpp (+2-4)
- (modified) clang/lib/CodeGen/Targets/X86.cpp (+16-26)
- (modified) clang/lib/Frontend/Rewrite/RewriteModernObjC.cpp (+20-26)
- (modified) clang/lib/Frontend/Rewrite/RewriteObjC.cpp (+1-4)
- (modified) clang/lib/Index/IndexTypeSourceInfo.cpp (+1-1)
- (modified) clang/lib/Interpreter/InterpreterValuePrinter.cpp (+4-8)
- (modified) clang/lib/Interpreter/Value.cpp (+2-2)
- (modified) clang/lib/Sema/SemaAccess.cpp (+1-4)
- (modified) clang/lib/Sema/SemaBPF.cpp (+3-4)
- (modified) clang/lib/Sema/SemaCXXScopeSpec.cpp (+2-5)
- (modified) clang/lib/Sema/SemaCast.cpp (+1-2)
- (modified) clang/lib/Sema/SemaChecking.cpp (+25-51)
- (modified) clang/lib/Sema/SemaCodeComplete.cpp (+3-10)
- (modified) clang/lib/Sema/SemaDecl.cpp (+5-11)
- (modified) clang/lib/Sema/SemaDeclCXX.cpp (+4-16)
- (modified) clang/lib/Sema/SemaExpr.cpp (+3-7)
- (modified) clang/lib/Sema/SemaExprCXX.cpp (+6-13)
- (modified) clang/lib/Sema/SemaExprObjC.cpp (+1-2)
- (modified) clang/lib/Sema/SemaHLSL.cpp (+24-25)
- (modified) clang/lib/Sema/SemaInit.cpp (+28-61)
- (modified) clang/lib/Sema/SemaLambda.cpp (+2-3)
- (modified) clang/lib/Sema/SemaLookup.cpp (+2-6)
- (modified) clang/lib/Sema/SemaOpenMP.cpp (+3-3)
- (modified) clang/lib/Sema/SemaOverload.cpp (+14-32)
- (modified) clang/lib/Sema/SemaPPC.cpp (+1-4)
- (modified) clang/lib/Sema/SemaStmt.cpp (+4-7)
- (modified) clang/lib/Sema/SemaTemplate.cpp (+6-8)
- (modified) clang/lib/Sema/SemaType.cpp (+2-5)
- (modified) clang/lib/StaticAnalyzer/Checkers/EnumCastOutOfRangeChecker.cpp (+2-9)
- (modified) clang/lib/StaticAnalyzer/Core/RegionStore.cpp (+1-3)
``````````diff
diff --git a/clang-tools-extra/clang-tidy/bugprone/TaggedUnionMemberCountCheck.cpp b/clang-tools-extra/clang-tidy/bugprone/TaggedUnionMemberCountCheck.cpp
index ddbb14e3ac62b..02f4421efdbf4 100644
--- a/clang-tools-extra/clang-tidy/bugprone/TaggedUnionMemberCountCheck.cpp
+++ b/clang-tools-extra/clang-tidy/bugprone/TaggedUnionMemberCountCheck.cpp
@@ -169,15 +169,8 @@ void TaggedUnionMemberCountCheck::check(
if (!Root || !UnionField || !TagField)
return;
- const auto *UnionDef =
- UnionField->getType().getCanonicalType().getTypePtr()->getAsRecordDecl();
- const auto *EnumDef = llvm::dyn_cast<EnumDecl>(
- TagField->getType().getCanonicalType().getTypePtr()->getAsTagDecl());
-
- assert(UnionDef && "UnionDef is missing!");
- assert(EnumDef && "EnumDef is missing!");
- if (!UnionDef || !EnumDef)
- return;
+ const auto *UnionDef = UnionField->getType()->castAsRecordDecl();
+ const auto *EnumDef = TagField->getType()->castAsEnumDecl();
const std::size_t UnionMemberCount = llvm::range_size(UnionDef->fields());
auto [TagCount, CountingEnumConstantDecl] = getNumberOfEnumValues(EnumDef);
diff --git a/clang-tools-extra/clang-tidy/utils/ExceptionSpecAnalyzer.cpp b/clang-tools-extra/clang-tidy/utils/ExceptionSpecAnalyzer.cpp
index aa6aefcf0c493..4314817e4f69d 100644
--- a/clang-tools-extra/clang-tidy/utils/ExceptionSpecAnalyzer.cpp
+++ b/clang-tools-extra/clang-tidy/utils/ExceptionSpecAnalyzer.cpp
@@ -66,10 +66,7 @@ ExceptionSpecAnalyzer::analyzeBase(const CXXBaseSpecifier &Base,
if (!RecType)
return State::Unknown;
- const auto *BaseClass =
- cast<CXXRecordDecl>(RecType->getOriginalDecl())->getDefinitionOrSelf();
-
- return analyzeRecord(BaseClass, Kind);
+ return analyzeRecord(RecType->getAsCXXRecordDecl(), Kind);
}
ExceptionSpecAnalyzer::State
diff --git a/clang-tools-extra/clang-tidy/utils/FormatStringConverter.cpp b/clang-tools-extra/clang-tidy/utils/FormatStringConverter.cpp
index 0df8e913100fc..0d0834dc38fc6 100644
--- a/clang-tools-extra/clang-tidy/utils/FormatStringConverter.cpp
+++ b/clang-tools-extra/clang-tidy/utils/FormatStringConverter.cpp
@@ -460,10 +460,9 @@ bool FormatStringConverter::emitIntegerArgument(
// be passed as its underlying type. However, printf will have forced
// the signedness based on the format string, so we need to do the
// same.
- if (const auto *ET = ArgType->getAs<EnumType>()) {
- if (const std::optional<std::string> MaybeCastType = castTypeForArgument(
- ArgKind,
- ET->getOriginalDecl()->getDefinitionOrSelf()->getIntegerType()))
+ if (const auto *ED = ArgType->getAsEnumDecl()) {
+ if (const std::optional<std::string> MaybeCastType =
+ castTypeForArgument(ArgKind, ED->getIntegerType()))
ArgFixes.emplace_back(
ArgIndex, (Twine("static_cast<") + *MaybeCastType + ">(").str());
else
diff --git a/clang-tools-extra/clangd/Hover.cpp b/clang-tools-extra/clangd/Hover.cpp
index af00c8948a215..30c70ac02205b 100644
--- a/clang-tools-extra/clangd/Hover.cpp
+++ b/clang-tools-extra/clangd/Hover.cpp
@@ -454,8 +454,7 @@ std::optional<std::string> printExprValue(const Expr *E,
Constant.Val.getInt().getSignificantBits() <= 64) {
// Compare to int64_t to avoid bit-width match requirements.
int64_t Val = Constant.Val.getInt().getExtValue();
- for (const EnumConstantDecl *ECD :
- T->castAs<EnumType>()->getOriginalDecl()->enumerators())
+ for (const EnumConstantDecl *ECD : T->castAsEnumDecl()->enumerators())
if (ECD->getInitVal() == Val)
return llvm::formatv("{0} ({1})", ECD->getNameAsString(),
printHex(Constant.Val.getInt()))
@@ -832,7 +831,7 @@ std::optional<HoverInfo> getThisExprHoverContents(const CXXThisExpr *CTE,
ASTContext &ASTCtx,
const PrintingPolicy &PP) {
QualType OriginThisType = CTE->getType()->getPointeeType();
- QualType ClassType = declaredType(OriginThisType->getAsTagDecl());
+ QualType ClassType = declaredType(OriginThisType->castAsTagDecl());
// For partial specialization class, origin `this` pointee type will be
// parsed as `InjectedClassNameType`, which will ouput template arguments
// like "type-parameter-0-0". So we retrieve user written class type in this
diff --git a/clang/include/clang/AST/Decl.h b/clang/include/clang/AST/Decl.h
index bebbde3661a33..79636a67dafba 100644
--- a/clang/include/clang/AST/Decl.h
+++ b/clang/include/clang/AST/Decl.h
@@ -3915,6 +3915,10 @@ class TagDecl : public TypeDecl,
bool isUnion() const { return getTagKind() == TagTypeKind::Union; }
bool isEnum() const { return getTagKind() == TagTypeKind::Enum; }
+ bool isStructureOrClass() const {
+ return isStruct() || isClass() || isInterface();
+ }
+
/// Is this tag type named, either directly or via being defined in
/// a typedef of this type?
///
diff --git a/clang/include/clang/AST/Type.h b/clang/include/clang/AST/Type.h
index adf5cb0462154..187e54f5cb54b 100644
--- a/clang/include/clang/AST/Type.h
+++ b/clang/include/clang/AST/Type.h
@@ -2883,14 +2883,21 @@ class alignas(TypeAlignment) Type : public ExtQualsTypeCommonBase {
/// because the type is a RecordType or because it is the injected-class-name
/// type of a class template or class template partial specialization.
CXXRecordDecl *getAsCXXRecordDecl() const;
+ CXXRecordDecl *castAsCXXRecordDecl() const;
/// Retrieves the RecordDecl this type refers to.
RecordDecl *getAsRecordDecl() const;
+ RecordDecl *castAsRecordDecl() const;
+
+ /// Retrieves the EnumDecl this type refers to.
+ EnumDecl *getAsEnumDecl() const;
+ EnumDecl *castAsEnumDecl() const;
/// Retrieves the TagDecl that this type refers to, either
/// because the type is a TagType or because it is the injected-class-name
/// type of a class template or class template partial specialization.
TagDecl *getAsTagDecl() const;
+ TagDecl *castAsTagDecl() const;
/// If this is a pointer or reference to a RecordType, return the
/// CXXRecordDecl that the type refers to.
diff --git a/clang/lib/AST/APValue.cpp b/clang/lib/AST/APValue.cpp
index 2d62209bbc28c..7173c2a0e1a2a 100644
--- a/clang/lib/AST/APValue.cpp
+++ b/clang/lib/AST/APValue.cpp
@@ -903,8 +903,7 @@ void APValue::printPretty(raw_ostream &Out, const PrintingPolicy &Policy,
case APValue::Struct: {
Out << '{';
bool First = true;
- const RecordDecl *RD =
- Ty->castAs<RecordType>()->getOriginalDecl()->getDefinitionOrSelf();
+ const auto *RD = Ty->castAsRecordDecl();
if (unsigned N = getStructNumBases()) {
const CXXRecordDecl *CD = cast<CXXRecordDecl>(RD);
CXXRecordDecl::base_class_const_iterator BI = CD->bases_begin();
diff --git a/clang/lib/AST/ASTContext.cpp b/clang/lib/AST/ASTContext.cpp
index 5fc55b2675fd2..06e7a2d5b857b 100644
--- a/clang/lib/AST/ASTContext.cpp
+++ b/clang/lib/AST/ASTContext.cpp
@@ -2001,8 +2001,7 @@ bool ASTContext::isPromotableIntegerType(QualType T) const {
// Enumerated types are promotable to their compatible integer types
// (C99 6.3.1.1) a.k.a. its underlying type (C++ [conv.prom]p2).
- if (const auto *ET = T->getAs<EnumType>()) {
- const EnumDecl *ED = ET->getOriginalDecl()->getDefinitionOrSelf();
+ if (const auto *ED = T->getAsEnumDecl()) {
if (T->isDependentType() || ED->getPromotionType().isNull() ||
ED->isScoped())
return false;
@@ -2712,11 +2711,8 @@ unsigned ASTContext::getPreferredTypeAlign(const Type *T) const {
// possible.
if (const auto *CT = T->getAs<ComplexType>())
T = CT->getElementType().getTypePtr();
- if (const auto *ET = T->getAs<EnumType>())
- T = ET->getOriginalDecl()
- ->getDefinitionOrSelf()
- ->getIntegerType()
- .getTypePtr();
+ if (const auto *ED = T->getAsEnumDecl())
+ T = ED->getIntegerType().getTypePtr();
if (T->isSpecificBuiltinType(BuiltinType::Double) ||
T->isSpecificBuiltinType(BuiltinType::LongLong) ||
T->isSpecificBuiltinType(BuiltinType::ULongLong) ||
@@ -3412,10 +3408,7 @@ static void encodeTypeForFunctionPointerAuth(const ASTContext &Ctx,
// type, or an unsigned integer type.
//
// So we have to treat enum types as integers.
- QualType UnderlyingType = cast<EnumType>(T)
- ->getOriginalDecl()
- ->getDefinitionOrSelf()
- ->getIntegerType();
+ QualType UnderlyingType = T->castAsEnumDecl()->getIntegerType();
return encodeTypeForFunctionPointerAuth(
Ctx, OS, UnderlyingType.isNull() ? Ctx.IntTy : UnderlyingType);
}
@@ -8351,8 +8344,8 @@ QualType ASTContext::isPromotableBitField(Expr *E) const {
QualType ASTContext::getPromotedIntegerType(QualType Promotable) const {
assert(!Promotable.isNull());
assert(isPromotableIntegerType(Promotable));
- if (const auto *ET = Promotable->getAs<EnumType>())
- return ET->getOriginalDecl()->getDefinitionOrSelf()->getPromotionType();
+ if (const auto *ED = Promotable->getAsEnumDecl())
+ return ED->getPromotionType();
if (const auto *BT = Promotable->getAs<BuiltinType>()) {
// C++ [conv.prom]: A prvalue of type char16_t, char32_t, or wchar_t
@@ -8571,10 +8564,9 @@ QualType ASTContext::getObjCSuperType() const {
}
void ASTContext::setCFConstantStringType(QualType T) {
- const auto *TD = T->castAs<TypedefType>();
- CFConstantStringTypeDecl = cast<TypedefDecl>(TD->getDecl());
- const auto *TagType = TD->castAs<RecordType>();
- CFConstantStringTagDecl = TagType->getOriginalDecl()->getDefinitionOrSelf();
+ const auto *TT = T->castAs<TypedefType>();
+ CFConstantStringTypeDecl = cast<TypedefDecl>(TT->getDecl());
+ CFConstantStringTagDecl = TT->castAsRecordDecl();
}
QualType ASTContext::getBlockDescriptorType() const {
@@ -11667,9 +11659,8 @@ QualType ASTContext::mergeFunctionTypes(QualType lhs, QualType rhs,
// Look at the converted type of enum types, since that is the type used
// to pass enum values.
- if (const auto *Enum = paramTy->getAs<EnumType>()) {
- paramTy =
- Enum->getOriginalDecl()->getDefinitionOrSelf()->getIntegerType();
+ if (const auto *ED = paramTy->getAsEnumDecl()) {
+ paramTy = ED->getIntegerType();
if (paramTy.isNull())
return {};
}
@@ -12260,8 +12251,8 @@ QualType ASTContext::mergeObjCGCQualifiers(QualType LHS, QualType RHS) {
//===----------------------------------------------------------------------===//
unsigned ASTContext::getIntWidth(QualType T) const {
- if (const auto *ET = T->getAs<EnumType>())
- T = ET->getOriginalDecl()->getDefinitionOrSelf()->getIntegerType();
+ if (const auto *ED = T->getAsEnumDecl())
+ T = ED->getIntegerType();
if (T->isBooleanType())
return 1;
if (const auto *EIT = T->getAs<BitIntType>())
@@ -12286,8 +12277,8 @@ QualType ASTContext::getCorrespondingUnsignedType(QualType T) const {
// For enums, get the underlying integer type of the enum, and let the general
// integer type signchanging code handle it.
- if (const auto *ETy = T->getAs<EnumType>())
- T = ETy->getOriginalDecl()->getDefinitionOrSelf()->getIntegerType();
+ if (const auto *ED = T->getAsEnumDecl())
+ T = ED->getIntegerType();
switch (T->castAs<BuiltinType>()->getKind()) {
case BuiltinType::Char_U:
@@ -12360,8 +12351,8 @@ QualType ASTContext::getCorrespondingSignedType(QualType T) const {
// For enums, get the underlying integer type of the enum, and let the general
// integer type signchanging code handle it.
- if (const auto *ETy = T->getAs<EnumType>())
- T = ETy->getOriginalDecl()->getDefinitionOrSelf()->getIntegerType();
+ if (const auto *ED = T->getAsEnumDecl())
+ T = ED->getIntegerType();
switch (T->castAs<BuiltinType>()->getKind()) {
case BuiltinType::Char_S:
diff --git a/clang/lib/AST/ByteCode/Compiler.cpp b/clang/lib/AST/ByteCode/Compiler.cpp
index e61d5e085a036..cb1a6b7eda93a 100644
--- a/clang/lib/AST/ByteCode/Compiler.cpp
+++ b/clang/lib/AST/ByteCode/Compiler.cpp
@@ -559,8 +559,7 @@ bool Compiler<Emitter>::VisitCastExpr(const CastExpr *CE) {
// Possibly diagnose casts to enum types if the target type does not
// have a fixed size.
if (Ctx.getLangOpts().CPlusPlus && CE->getType()->isEnumeralType()) {
- const auto *ET = CE->getType().getCanonicalType()->castAs<EnumType>();
- const auto *ED = ET->getOriginalDecl()->getDefinitionOrSelf();
+ const auto *ED = CE->getType()->castAsEnumDecl();
if (!ED->isFixed()) {
if (!this->emitCheckEnumValue(*FromT, ED, CE))
return false;
diff --git a/clang/lib/AST/ByteCode/Context.cpp b/clang/lib/AST/ByteCode/Context.cpp
index 36eb7607e70bf..fbbb508ed226c 100644
--- a/clang/lib/AST/ByteCode/Context.cpp
+++ b/clang/lib/AST/ByteCode/Context.cpp
@@ -364,8 +364,7 @@ OptPrimType Context::classify(QualType T) const {
return integralTypeToPrimTypeU(BT->getNumBits());
}
- if (const auto *ET = T->getAs<EnumType>()) {
- const auto *D = ET->getOriginalDecl()->getDefinitionOrSelf();
+ if (const auto *D = T->getAsEnumDecl()) {
if (!D->isComplete())
return std::nullopt;
return classify(D->getIntegerType());
diff --git a/clang/lib/AST/ByteCode/Program.cpp b/clang/lib/AST/ByteCode/Program.cpp
index 139cae7afc87e..0892d1dc36595 100644
--- a/clang/lib/AST/ByteCode/Program.cpp
+++ b/clang/lib/AST/ByteCode/Program.cpp
@@ -347,11 +347,7 @@ Record *Program::getOrCreateRecord(const RecordDecl *RD) {
}
for (const CXXBaseSpecifier &Spec : CD->vbases()) {
- const auto *RT = Spec.getType()->getAs<RecordType>();
- if (!RT)
- return nullptr;
-
- const RecordDecl *BD = RT->getOriginalDecl()->getDefinitionOrSelf();
+ const auto *BD = Spec.getType()->castAsCXXRecordDecl();
const Record *BR = getOrCreateRecord(BD);
const Descriptor *Desc = GetBaseDesc(BD, BR);
diff --git a/clang/lib/AST/CXXInheritance.cpp b/clang/lib/AST/CXXInheritance.cpp
index 0ced210900b1a..94f01c86a16ca 100644
--- a/clang/lib/AST/CXXInheritance.cpp
+++ b/clang/lib/AST/CXXInheritance.cpp
@@ -263,7 +263,7 @@ bool CXXBasePaths::lookupInBases(ASTContext &Context,
BaseRecord = nullptr;
}
} else {
- BaseRecord = cast<CXXRecordDecl>(BaseSpec.getType()->getAsRecordDecl());
+ BaseRecord = BaseSpec.getType()->castAsCXXRecordDecl();
}
if (BaseRecord &&
lookupInBases(Context, BaseRecord, BaseMatches, LookupInDependent)) {
@@ -327,10 +327,7 @@ bool CXXRecordDecl::lookupInBases(BaseMatchesCallback BaseMatches,
if (!PE.Base->isVirtual())
continue;
- CXXRecordDecl *VBase = nullptr;
- if (const RecordType *Record = PE.Base->getType()->getAs<RecordType>())
- VBase = cast<CXXRecordDecl>(Record->getOriginalDecl())
- ->getDefinitionOrSelf();
+ auto *VBase = PE.Base->getType()->getAsCXXRecordDecl();
if (!VBase)
break;
@@ -396,7 +393,7 @@ bool CXXRecordDecl::hasMemberName(DeclarationName Name) const {
CXXBasePaths Paths(false, false, false);
return lookupInBases(
[Name](const CXXBaseSpecifier *Specifier, CXXBasePath &Path) {
- return findOrdinaryMember(Specifier->getType()->getAsCXXRecordDecl(),
+ return findOrdinaryMember(Specifier->getType()->castAsCXXRecordDecl(),
Path, Name);
},
Paths);
diff --git a/clang/lib/AST/DeclCXX.cpp b/clang/lib/AST/DeclCXX.cpp
index 62eb4de8c6a96..86d3b136ce0b5 100644
--- a/clang/lib/AST/DeclCXX.cpp
+++ b/clang/lib/AST/DeclCXX.cpp
@@ -216,9 +216,7 @@ CXXRecordDecl::setBases(CXXBaseSpecifier const * const *Bases,
// Skip dependent types; we can't do any checking on them now.
if (BaseType->isDependentType())
continue;
- auto *BaseClassDecl =
- cast<CXXRecordDecl>(BaseType->castAs<RecordType>()->getOriginalDecl())
- ->getDefinitionOrSelf();
+ auto *BaseClassDecl = BaseType->castAsCXXRecordDecl();
// C++2a [class]p7:
// A standard-layout class is a class that:
@@ -3432,13 +3430,12 @@ SourceRange UsingDecl::getSourceRange() const {
void UsingEnumDecl::anchor() {}
UsingEnumDecl *UsingEnumDecl::Create(ASTContext &C, DeclContext *DC,
- SourceLocation UL,
- SourceLocation EL,
+ SourceLocation UL, SourceLocation EL,
SourceLocation NL,
TypeSourceInfo *EnumType) {
- assert(isa<EnumDecl>(EnumType->getType()->getAsTagDecl()));
return new (C, DC)
- UsingEnumDecl(DC, EnumType->getType()->getAsTagDecl()->getDeclName(), UL, EL, NL, EnumType);
+ UsingEnumDecl(DC, EnumType->getType()->castAsEnumDecl()->getDeclName(),
+ UL, EL, NL, EnumType);
}
UsingEnumDecl *UsingEnumDecl::CreateDeserialized(ASTContext &C,
diff --git a/clang/lib/AST/Expr.cpp b/clang/lib/AST/Expr.cpp
index 9d1490c2ef834..072d07cb81179 100644
--- a/clang/lib/AST/Expr.cpp
+++ b/clang/lib/AST/Expr.cpp
@@ -74,8 +74,7 @@ const CXXRecordDecl *Expr::getBestDynamicClassType() const {
if (DerivedType->isDependentType())
return nullptr;
- const RecordType *Ty = DerivedType->castAs<RecordType>();
- return cast<CXXRecordDecl>(Ty->getOriginalDecl())->getDefinitionOrSelf();
+ return DerivedType->castAsCXXRecordDecl();
}
const Expr *Expr::skipRValueSubobjectAdjustments(
@@ -90,10 +89,7 @@ const Expr *Expr::skipRValueSubobjectAdjustments(
CE->getCastKind() == CK_UncheckedDerivedToBase) &&
E->getType()->isRecordType()) {
E = CE->getSubExpr();
- const auto *Derived =
- cast<CXXRecordDecl>(
- E->getType()->castAs<RecordType>()->getOriginalDecl())
- ->getDefinitionOrSelf();
+ const auto *Derived = E->getType()->castAsCXXRecordDecl();
Adjustments.push_back(SubobjectAdjustment(CE, Derived));
continue;
}
@@ -2032,9 +2028,7 @@ CXXBaseSpecifier **CastExpr::path_buffer() {
const FieldDecl *CastExpr::getTargetFieldForToUnionCast(QualType unionType,
QualType opType) {
- auto RD =
- unionType->castAs<RecordType>()->getOriginalDecl()->getDefinitionOrSelf();
- return getTargetFieldForToUnionCast(RD, opType);
+ return getTargetFieldForToUnionCast(unionType->castAsRecordDecl(), opType);
}
const FieldDecl *CastExpr::getTargetFieldForToUnionCast(const RecordDecl *RD,
@@ -3396,10 +3390,7 @@ bool Expr::isConstantInitializer(ASTContext &Ctx, bool IsForRef,
if (ILE->getType()->isRecordType()) {
unsigned ElementNo = 0;
- RecordDecl *RD = ILE->getType()
- ->castAs<RecordType>()
- ->getOriginalDecl()
- ->getDefinitionOrSelf();
+ auto *RD = ILE->getType()->castAsRecordDecl();
// In C++17, bases were added to the list of members used by aggregate
// initialization.
diff --git a/clang/lib/AST/ExprConstant.cpp b/clang/lib/AST/ExprConstant.cpp
index ee0ac4effab0e..107ce47b8a599 100644
--- a/clang/lib/AST/ExprConstant.cpp
+++ b/clang/lib/AST/ExprConstant.cpp
@@ -2614,8 +2614,7 @@ static bool CheckEvaluationResult(CheckEvaluationResultKind CERK,
Value.getUnionValue(), Kind, Value.getUnionField(), CheckedTemps);
}
if (Value.isStruct()) {
- RecordDecl *RD =
- Type->castAs<RecordType>()->getOriginalDecl()->getDefinitionOrSelf();
+ auto *RD = Type->castAsRecordDecl();
if (const CXXRecordDecl *CD = dyn_cast<CXXRecordDecl>(RD)) {
unsigned BaseIndex = 0;
for (const CXXBaseSpecifier &BS : CD->bases()) {
@@ -10769,8 +10768,7 @@ static bool HandleClassZeroInitialization(EvalInfo &Info, const Expr *E,
}
bool RecordExprEvaluator::ZeroInitialization(const Expr *E, QualType T) {
- const RecordDecl *RD =
- T->castAs<RecordType>()->getOriginalDecl()->getDefinitionOrSelf();
+ const auto *RD = T->castAsRecordDecl();
if (RD->isInvalidDecl()) return false;
if (RD->isUnion()) {
// C++11 [dcl.init]p5: If T is a (possibly cv-qualified) union type, the
@@ -10839,10 +10837,7 @...
[truncated]
``````````
</details>
https://github.com/llvm/llvm-project/pull/155463
More information about the cfe-commits
mailing list