From e7b4d96d686d72107432d1a316522d7d91fb0cb0 Mon Sep 17 00:00:00 2001 From: George Karpenkov Date: Sat, 28 Jul 2018 02:16:13 +0000 Subject: [PATCH] [AST] Add a convenient getter from QualType to RecordDecl Differential Revision: https://reviews.llvm.org/D49951 git-svn-id: https://llvm.org/svn/llvm-project/cfe/trunk@338187 91177308-0d34-0410-b5e6-96231b3b80d8 --- include/clang/AST/Type.h | 3 +++ lib/AST/Decl.cpp | 9 +++------ lib/AST/Type.cpp | 4 ++++ lib/Analysis/BodyFarm.cpp | 2 +- lib/CodeGen/CGExprConstant.cpp | 3 +-- lib/CodeGen/CGRecordLayoutBuilder.cpp | 5 ++--- 6 files changed, 14 insertions(+), 12 deletions(-) diff --git a/include/clang/AST/Type.h b/include/clang/AST/Type.h index c692707847..81e799c3ba 100644 --- a/include/clang/AST/Type.h +++ b/include/clang/AST/Type.h @@ -2017,6 +2017,9 @@ public: /// type of a class template or class template partial specialization. CXXRecordDecl *getAsCXXRecordDecl() const; + /// Retrieves the RecordDecl this type refers to. + RecordDecl *getAsRecordDecl() 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. diff --git a/lib/AST/Decl.cpp b/lib/AST/Decl.cpp index 3b9b85a20a..b85e82d56d 100644 --- a/lib/AST/Decl.cpp +++ b/lib/AST/Decl.cpp @@ -3149,12 +3149,9 @@ SourceRange FunctionDecl::getExceptionSpecSourceRange() const { const Attr *FunctionDecl::getUnusedResultAttr() const { QualType RetType = getReturnType(); - if (RetType->isRecordType()) { - if (const auto *Ret = - dyn_cast_or_null(RetType->getAsTagDecl())) { - if (const auto *R = Ret->getAttr()) - return R; - } + if (const auto *Ret = RetType->getAsRecordDecl()) { + if (const auto *R = Ret->getAttr()) + return R; } else if (const auto *ET = RetType->getAs()) { if (const EnumDecl *ED = ET->getDecl()) { if (const auto *R = ED->getAttr()) diff --git a/lib/AST/Type.cpp b/lib/AST/Type.cpp index fad8c0d1c6..64d16c4783 100644 --- a/lib/AST/Type.cpp +++ b/lib/AST/Type.cpp @@ -1628,6 +1628,10 @@ CXXRecordDecl *Type::getAsCXXRecordDecl() const { return dyn_cast_or_null(getAsTagDecl()); } +RecordDecl *Type::getAsRecordDecl() const { + return dyn_cast_or_null(getAsTagDecl()); +} + TagDecl *Type::getAsTagDecl() const { if (const auto *TT = getAs()) return TT->getDecl(); diff --git a/lib/Analysis/BodyFarm.cpp b/lib/Analysis/BodyFarm.cpp index b9fb15b2db..05a311e5b2 100644 --- a/lib/Analysis/BodyFarm.cpp +++ b/lib/Analysis/BodyFarm.cpp @@ -342,7 +342,7 @@ static Stmt *create_call_once(ASTContext &C, const FunctionDecl *D) { // Nullable pointer, non-null iff function is a CXXRecordDecl. CXXRecordDecl *CallbackRecordDecl = CallbackType->getAsCXXRecordDecl(); QualType FlagType = Flag->getType().getNonReferenceType(); - auto *FlagRecordDecl = dyn_cast_or_null(FlagType->getAsTagDecl()); + auto *FlagRecordDecl = FlagType->getAsRecordDecl(); if (!FlagRecordDecl) { LLVM_DEBUG(llvm::dbgs() << "Flag field is not a record: " diff --git a/lib/CodeGen/CGExprConstant.cpp b/lib/CodeGen/CGExprConstant.cpp index cfd0b85923..a738ecb0a3 100644 --- a/lib/CodeGen/CGExprConstant.cpp +++ b/lib/CodeGen/CGExprConstant.cpp @@ -2064,8 +2064,7 @@ static llvm::Constant *EmitNullConstant(CodeGenModule &CGM, if (record->isUnion()) { if (Field->getIdentifier()) break; - if (const auto *FieldRD = - dyn_cast_or_null(Field->getType()->getAsTagDecl())) + if (const auto *FieldRD = Field->getType()->getAsRecordDecl()) if (FieldRD->findFirstNamedDataMember()) break; } diff --git a/lib/CodeGen/CGRecordLayoutBuilder.cpp b/lib/CodeGen/CGRecordLayoutBuilder.cpp index 4ee6c8e714..a38df508dc 100644 --- a/lib/CodeGen/CGRecordLayoutBuilder.cpp +++ b/lib/CodeGen/CGRecordLayoutBuilder.cpp @@ -313,9 +313,8 @@ void CGRecordLowering::lowerUnion() { if (!SeenNamedMember) { SeenNamedMember = Field->getIdentifier(); if (!SeenNamedMember) - if (const auto *FieldRD = - dyn_cast_or_null(Field->getType()->getAsTagDecl())) - SeenNamedMember = FieldRD->findFirstNamedDataMember(); + if (const auto *FieldRD = Field->getType()->getAsRecordDecl()) + SeenNamedMember = FieldRD->findFirstNamedDataMember(); if (SeenNamedMember && !isZeroInitializable(Field)) { IsZeroInitializable = IsZeroInitializableAsBase = false; StorageType = FieldType; -- 2.40.0