Skip to content

Commit dc8596d

Browse files
authored
[clang] NFC: change more places to use Type::getAsTagDecl and friends (#155313)
This changes a bunch of places which use getAs<TagType>, including derived types, just to obtain the tag definition. This is preparation for #155028, offloading all the changes that PR used to introduce which don't depend on any new helpers.
1 parent 1ba8b36 commit dc8596d

File tree

100 files changed

+613
-945
lines changed

Some content is hidden

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

100 files changed

+613
-945
lines changed

clang-tools-extra/clang-doc/Serialize.cpp

Lines changed: 2 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -901,9 +901,8 @@ parseBases(RecordInfo &I, const CXXRecordDecl *D, bool IsFileInRootDir,
901901
if (!D->isThisDeclarationADefinition())
902902
return;
903903
for (const CXXBaseSpecifier &B : D->bases()) {
904-
if (const RecordType *Ty = B.getType()->getAs<RecordType>()) {
905-
if (const CXXRecordDecl *Base = cast_or_null<CXXRecordDecl>(
906-
Ty->getOriginalDecl()->getDefinition())) {
904+
if (const auto *Base = B.getType()->getAsCXXRecordDecl()) {
905+
if (Base->isCompleteDefinition()) {
907906
// Initialized without USR and name, this will be set in the following
908907
// if-else stmt.
909908
BaseRecordInfo BI(

clang-tools-extra/clang-tidy/cert/DefaultOperatorNewAlignmentCheck.cpp

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -31,7 +31,7 @@ void DefaultOperatorNewAlignmentCheck::check(
3131
return;
3232
const TagDecl *D = T->getAsTagDecl();
3333
// Alignment can not be obtained for undefined type.
34-
if (!D || !D->getDefinition() || !D->isCompleteDefinition())
34+
if (!D || !D->isCompleteDefinition())
3535
return;
3636

3737
ASTContext &Context = D->getASTContext();

clang-tools-extra/clang-tidy/cppcoreguidelines/SlicingCheck.cpp

Lines changed: 2 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -90,9 +90,8 @@ void SlicingCheck::diagnoseSlicedOverriddenMethods(
9090
}
9191
// Recursively process bases.
9292
for (const auto &Base : DerivedDecl.bases()) {
93-
if (const auto *BaseRecordType = Base.getType()->getAs<RecordType>()) {
94-
if (const auto *BaseRecord = cast_or_null<CXXRecordDecl>(
95-
BaseRecordType->getOriginalDecl()->getDefinition()))
93+
if (const auto *BaseRecord = Base.getType()->getAsCXXRecordDecl()) {
94+
if (BaseRecord->isCompleteDefinition())
9695
diagnoseSlicedOverriddenMethods(Call, *BaseRecord, BaseDecl);
9796
}
9897
}

clang-tools-extra/clang-tidy/fuchsia/MultipleInheritanceCheck.cpp

Lines changed: 9 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -71,13 +71,10 @@ bool MultipleInheritanceCheck::isInterface(const CXXRecordDecl *Node) {
7171
for (const auto &I : Node->bases()) {
7272
if (I.isVirtual())
7373
continue;
74-
const auto *Ty = I.getType()->getAs<RecordType>();
75-
if (!Ty)
74+
const auto *Base = I.getType()->getAsCXXRecordDecl();
75+
if (!Base)
7676
continue;
77-
const RecordDecl *D = Ty->getOriginalDecl()->getDefinition();
78-
if (!D)
79-
continue;
80-
const auto *Base = cast<CXXRecordDecl>(D);
77+
assert(Base->isCompleteDefinition());
8178
if (!isInterface(Base)) {
8279
addNodeToInterfaceMap(Node, false);
8380
return false;
@@ -103,23 +100,21 @@ void MultipleInheritanceCheck::check(const MatchFinder::MatchResult &Result) {
103100
for (const auto &I : D->bases()) {
104101
if (I.isVirtual())
105102
continue;
106-
const auto *Ty = I.getType()->getAs<RecordType>();
107-
if (!Ty)
103+
const auto *Base = I.getType()->getAsCXXRecordDecl();
104+
if (!Base)
108105
continue;
109-
const auto *Base =
110-
cast<CXXRecordDecl>(Ty->getOriginalDecl()->getDefinition());
106+
assert(Base->isCompleteDefinition());
111107
if (!isInterface(Base))
112108
NumConcrete++;
113109
}
114110

115111
// Check virtual bases to see if there is more than one concrete
116112
// non-virtual base.
117113
for (const auto &V : D->vbases()) {
118-
const auto *Ty = V.getType()->getAs<RecordType>();
119-
if (!Ty)
114+
const auto *Base = V.getType()->getAsCXXRecordDecl();
115+
if (!Base)
120116
continue;
121-
const auto *Base =
122-
cast<CXXRecordDecl>(Ty->getOriginalDecl()->getDefinition());
117+
assert(Base->isCompleteDefinition());
123118
if (!isInterface(Base))
124119
NumConcrete++;
125120
}

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

Lines changed: 2 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -119,9 +119,8 @@ bool isTriviallyDefaultConstructible(QualType Type, const ASTContext &Context) {
119119
if (CanonicalType->isScalarType() || CanonicalType->isVectorType())
120120
return true;
121121

122-
if (const auto *RT = CanonicalType->getAs<RecordType>()) {
123-
return recordIsTriviallyDefaultConstructible(
124-
*RT->getOriginalDecl()->getDefinitionOrSelf(), Context);
122+
if (const auto *RD = CanonicalType->getAsRecordDecl()) {
123+
return recordIsTriviallyDefaultConstructible(*RD, Context);
125124
}
126125

127126
// No other types can match.

clang/include/clang/AST/AbstractBasicReader.h

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -193,7 +193,7 @@ class DataStreamBasicReader : public BasicReaderBase<Impl> {
193193
auto elemTy = origTy;
194194
unsigned pathLength = asImpl().readUInt32();
195195
for (unsigned i = 0; i < pathLength; ++i) {
196-
if (elemTy->template getAs<RecordType>()) {
196+
if (elemTy->isRecordType()) {
197197
unsigned int_ = asImpl().readUInt32();
198198
Decl *decl = asImpl().template readDeclAs<Decl>();
199199
if (auto *recordDecl = dyn_cast<CXXRecordDecl>(decl))

clang/include/clang/AST/AbstractBasicWriter.h

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -176,7 +176,7 @@ class DataStreamBasicWriter : public BasicWriterBase<Impl> {
176176
asImpl().writeUInt32(path.size());
177177
auto &ctx = ((BasicWriterBase<Impl> *)this)->getASTContext();
178178
for (auto elem : path) {
179-
if (elemTy->getAs<RecordType>()) {
179+
if (elemTy->isRecordType()) {
180180
asImpl().writeUInt32(elem.getAsBaseOrMember().getInt());
181181
const Decl *baseOrMember = elem.getAsBaseOrMember().getPointer();
182182
if (const auto *recordDecl = dyn_cast<CXXRecordDecl>(baseOrMember)) {

clang/include/clang/AST/DeclCXX.h

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -3825,7 +3825,9 @@ class UsingEnumDecl : public BaseUsingDecl, public Mergeable<UsingEnumDecl> {
38253825
void setEnumType(TypeSourceInfo *TSI) { EnumType = TSI; }
38263826

38273827
public:
3828-
EnumDecl *getEnumDecl() const { return cast<EnumDecl>(EnumType->getType()->getAsTagDecl()); }
3828+
EnumDecl *getEnumDecl() const {
3829+
return cast<clang::EnumType>(EnumType->getType())->getOriginalDecl();
3830+
}
38293831

38303832
static UsingEnumDecl *Create(ASTContext &C, DeclContext *DC,
38313833
SourceLocation UsingL, SourceLocation EnumL,

clang/include/clang/Sema/Sema.h

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -5404,7 +5404,7 @@ class Sema final : public SemaBase {
54045404

54055405
/// FinalizeVarWithDestructor - Prepare for calling destructor on the
54065406
/// constructed variable.
5407-
void FinalizeVarWithDestructor(VarDecl *VD, const RecordType *DeclInitType);
5407+
void FinalizeVarWithDestructor(VarDecl *VD, CXXRecordDecl *DeclInit);
54085408

54095409
/// Helper class that collects exception specifications for
54105410
/// implicitly-declared special member functions.

clang/lib/AST/ASTContext.cpp

Lines changed: 12 additions & 22 deletions
Original file line numberDiff line numberDiff line change
@@ -654,9 +654,9 @@ comments::FullComment *ASTContext::getCommentForDecl(
654654
// does not have one of its own.
655655
QualType QT = TD->getUnderlyingType();
656656
if (const auto *TT = QT->getAs<TagType>())
657-
if (const Decl *TD = TT->getOriginalDecl())
658-
if (comments::FullComment *FC = getCommentForDecl(TD, PP))
659-
return cloneFullComment(FC, D);
657+
if (comments::FullComment *FC =
658+
getCommentForDecl(TT->getOriginalDecl(), PP))
659+
return cloneFullComment(FC, D);
660660
}
661661
else if (const auto *IC = dyn_cast<ObjCInterfaceDecl>(D)) {
662662
while (IC->getSuperClass()) {
@@ -1933,12 +1933,9 @@ TypeInfoChars ASTContext::getTypeInfoDataSizeInChars(QualType T) const {
19331933
// of a base-class subobject. We decide whether that's possible
19341934
// during class layout, so here we can just trust the layout results.
19351935
if (getLangOpts().CPlusPlus) {
1936-
if (const auto *RT = T->getAs<RecordType>()) {
1937-
const auto *RD = RT->getOriginalDecl()->getDefinitionOrSelf();
1938-
if (!RD->isInvalidDecl()) {
1939-
const ASTRecordLayout &layout = getASTRecordLayout(RD);
1940-
Info.Width = layout.getDataSize();
1941-
}
1936+
if (const auto *RD = T->getAsCXXRecordDecl(); RD && !RD->isInvalidDecl()) {
1937+
const ASTRecordLayout &layout = getASTRecordLayout(RD);
1938+
Info.Width = layout.getDataSize();
19421939
}
19431940
}
19441941

@@ -2694,9 +2691,7 @@ unsigned ASTContext::getPreferredTypeAlign(const Type *T) const {
26942691
if (!Target->allowsLargerPreferedTypeAlignment())
26952692
return ABIAlign;
26962693

2697-
if (const auto *RT = T->getAs<RecordType>()) {
2698-
const RecordDecl *RD = RT->getOriginalDecl()->getDefinitionOrSelf();
2699-
2694+
if (const auto *RD = T->getAsRecordDecl()) {
27002695
// When used as part of a typedef, or together with a 'packed' attribute,
27012696
// the 'aligned' attribute can be used to decrease alignment. Note that the
27022697
// 'packed' case is already taken into consideration when computing the
@@ -2887,12 +2882,10 @@ structHasUniqueObjectRepresentations(const ASTContext &Context,
28872882
static std::optional<int64_t>
28882883
getSubobjectSizeInBits(const FieldDecl *Field, const ASTContext &Context,
28892884
bool CheckIfTriviallyCopyable) {
2890-
if (Field->getType()->isRecordType()) {
2891-
const RecordDecl *RD = Field->getType()->getAsRecordDecl();
2892-
if (!RD->isUnion())
2893-
return structHasUniqueObjectRepresentations(Context, RD,
2894-
CheckIfTriviallyCopyable);
2895-
}
2885+
if (const auto *RD = Field->getType()->getAsRecordDecl();
2886+
RD && !RD->isUnion())
2887+
return structHasUniqueObjectRepresentations(Context, RD,
2888+
CheckIfTriviallyCopyable);
28962889

28972890
// A _BitInt type may not be unique if it has padding bits
28982891
// but if it is a bitfield the padding bits are not used.
@@ -3047,10 +3040,7 @@ bool ASTContext::hasUniqueObjectRepresentations(
30473040
if (const auto *MPT = Ty->getAs<MemberPointerType>())
30483041
return !ABI->getMemberPointerInfo(MPT).HasPadding;
30493042

3050-
if (Ty->isRecordType()) {
3051-
const RecordDecl *Record =
3052-
Ty->castAs<RecordType>()->getOriginalDecl()->getDefinitionOrSelf();
3053-
3043+
if (const auto *Record = Ty->getAsRecordDecl()) {
30543044
if (Record->isInvalidDecl())
30553045
return false;
30563046

0 commit comments

Comments
 (0)