-
Notifications
You must be signed in to change notification settings - Fork 14.8k
[clang] NFC: introduce Type::getAsEnumDecl, and cast variants for all TagDecls #155463
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Conversation
… TagDecls 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.
@llvm/pr-subscribers-backend-webassembly @llvm/pr-subscribers-backend-arm Author: Matheus Izvekov (mizvekov) ChangesAnd 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:
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]
|
@llvm/pr-subscribers-clangd Author: Matheus Izvekov (mizvekov) ChangesAnd 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:
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]
|
@llvm/pr-subscribers-debuginfo Author: Matheus Izvekov (mizvekov) ChangesAnd 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:
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]
|
@llvm/pr-subscribers-backend-aarch64 Author: Matheus Izvekov (mizvekov) ChangesAnd 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:
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]
|
@llvm/pr-subscribers-clang Author: Matheus Izvekov (mizvekov) ChangesAnd 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:
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]
|
@llvm/pr-subscribers-backend-mips Author: Matheus Izvekov (mizvekov) ChangesAnd 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:
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]
|
@llvm/pr-subscribers-backend-risc-v Author: Matheus Izvekov (mizvekov) ChangesAnd 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:
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]
|
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.