Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -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);
Expand Down
5 changes: 1 addition & 4 deletions clang-tools-extra/clang-tidy/utils/ExceptionSpecAnalyzer.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
7 changes: 3 additions & 4 deletions clang-tools-extra/clang-tidy/utils/FormatStringConverter.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
5 changes: 2 additions & 3 deletions clang-tools-extra/clangd/Hover.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -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()))
Expand Down Expand Up @@ -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
Expand Down
4 changes: 4 additions & 0 deletions clang/include/clang/AST/Decl.h
Original file line number Diff line number Diff line change
Expand Up @@ -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?
///
Expand Down
7 changes: 7 additions & 0 deletions clang/include/clang/AST/Type.h
Original file line number Diff line number Diff line change
Expand Up @@ -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.
Expand Down
3 changes: 1 addition & 2 deletions clang/lib/AST/APValue.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -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();
Expand Down
43 changes: 17 additions & 26 deletions clang/lib/AST/ASTContext.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down Expand Up @@ -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) ||
Expand Down Expand Up @@ -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);
}
Expand Down Expand Up @@ -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
Expand Down Expand Up @@ -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 {
Expand Down Expand Up @@ -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 {};
}
Expand Down Expand Up @@ -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>())
Expand All @@ -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:
Expand Down Expand Up @@ -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:
Expand Down
3 changes: 1 addition & 2 deletions clang/lib/AST/ByteCode/Compiler.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down
3 changes: 1 addition & 2 deletions clang/lib/AST/ByteCode/Context.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -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());
Expand Down
6 changes: 1 addition & 5 deletions clang/lib/AST/ByteCode/Program.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -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);
Expand Down
9 changes: 3 additions & 6 deletions clang/lib/AST/CXXInheritance.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -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)) {
Expand Down Expand Up @@ -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;

Expand Down Expand Up @@ -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);
Expand Down
11 changes: 4 additions & 7 deletions clang/lib/AST/DeclCXX.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -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:
Expand Down Expand Up @@ -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,
Expand Down
17 changes: 4 additions & 13 deletions clang/lib/AST/Expr.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -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(
Expand All @@ -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;
}
Expand Down Expand Up @@ -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,
Expand Down Expand Up @@ -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.
Expand Down
Loading
Loading