From: Douglas Gregor Date: Mon, 9 Nov 2009 22:08:55 +0000 (+0000) Subject: Make sure that Type::getAs() (or Type::getAs() (or Type::getAs()) does not instantiate. Update all callers that used this unsafe feature to use the appropriate ASTContext::getAs*ArrayType method. git-svn-id: https://llvm.org/svn/llvm-project/cfe/trunk@86596 91177308-0d34-0410-b5e6-96231b3b80d8 --- diff --git a/include/clang/AST/Type.h b/include/clang/AST/Type.h index e53b9787af..8727650031 100644 --- a/include/clang/AST/Type.h +++ b/include/clang/AST/Type.h @@ -2927,8 +2927,21 @@ inline const DiagnosticBuilder &operator<<(const DiagnosticBuilder &DB, return DB; } +// Helper class template that is used by Type::getAs to ensure that one does +// not try to look through a qualified type to get to an array type. +template::value || + llvm::is_base_of::value)> +struct ArrayType_cannot_be_used_with_getAs { }; + +template +struct ArrayType_cannot_be_used_with_getAs; + /// Member-template getAs'. template const T *Type::getAs() const { + ArrayType_cannot_be_used_with_getAs at; + (void)at; + // If this is directly a T type, return it. if (const T *Ty = dyn_cast(this)) return Ty; diff --git a/lib/Analysis/RegionStore.cpp b/lib/Analysis/RegionStore.cpp index dbf8c42d27..45c42281ab 100644 --- a/lib/Analysis/RegionStore.cpp +++ b/lib/Analysis/RegionStore.cpp @@ -1109,7 +1109,7 @@ SVal RegionStoreManager::RetrieveElement(const GRState* state, // FIXME: Handle loads from strings where the literal is treated as // an integer, e.g., *((unsigned int*)"hello") ASTContext &Ctx = getContext(); - QualType T = StrR->getValueType(Ctx)->getAs()->getElementType(); + QualType T = Ctx.getAsArrayType(StrR->getValueType(Ctx))->getElementType(); if (T != Ctx.getCanonicalType(R->getElementType())) return UnknownVal(); diff --git a/lib/Analysis/VLASizeChecker.cpp b/lib/Analysis/VLASizeChecker.cpp index 98b755be53..5cb700ed67 100644 --- a/lib/Analysis/VLASizeChecker.cpp +++ b/lib/Analysis/VLASizeChecker.cpp @@ -43,7 +43,8 @@ void VLASizeChecker::PreVisitDeclStmt(CheckerContext &C, const DeclStmt *DS) { if (!VD) return; - const VariableArrayType *VLA = VD->getType()->getAs(); + const VariableArrayType *VLA + = C.getASTContext().getAsVariableArrayType(VD->getType()); if (!VLA) return; diff --git a/lib/CodeGen/CGDecl.cpp b/lib/CodeGen/CGDecl.cpp index 2021ced316..2a28ef08a0 100644 --- a/lib/CodeGen/CGDecl.cpp +++ b/lib/CodeGen/CGDecl.cpp @@ -507,7 +507,7 @@ void CodeGenFunction::EmitLocalBlockVarDecl(const VarDecl &D) { // Handle CXX destruction of variables. QualType DtorTy(Ty); - if (const ArrayType *Array = DtorTy->getAs()) + while (const ArrayType *Array = getContext().getAsArrayType(DtorTy)) DtorTy = getContext().getBaseElementType(Array); if (const RecordType *RT = DtorTy->getAs()) if (CXXRecordDecl *ClassDecl = dyn_cast(RT->getDecl())) { diff --git a/lib/Sema/SemaDecl.cpp b/lib/Sema/SemaDecl.cpp index cfea66bf5a..8bd934a561 100644 --- a/lib/Sema/SemaDecl.cpp +++ b/lib/Sema/SemaDecl.cpp @@ -3596,7 +3596,7 @@ Sema::DeclGroupPtrTy Sema::FinalizeDeclaratorGroup(Scope *S, const DeclSpec &DS, // template void f(Args... args) { // int vals[] = { args }; // } - const IncompleteArrayType *IAT = T->getAs(); + const IncompleteArrayType *IAT = Context.getAsIncompleteArrayType(T); Expr *Init = IDecl->getInit(); if (IAT && Init && (Init->isTypeDependent() || Init->isValueDependent())) { diff --git a/lib/Sema/SemaOverload.cpp b/lib/Sema/SemaOverload.cpp index 69dac7d33e..69e5a52307 100644 --- a/lib/Sema/SemaOverload.cpp +++ b/lib/Sema/SemaOverload.cpp @@ -2982,7 +2982,7 @@ BuiltinCandidateTypeSet::AddPointerWithMoreQualifiedTypeVariants(QualType Ty, QualType PointeeTy = PointerTy->getPointeeType(); unsigned BaseCVR = PointeeTy.getCVRQualifiers(); - if (const ConstantArrayType *Array = PointeeTy->getAs()) + if (const ConstantArrayType *Array =Context.getAsConstantArrayType(PointeeTy)) BaseCVR = Array->getElementType().getCVRQualifiers(); bool hasVolatile = VisibleQuals.hasVolatile(); bool hasRestrict = VisibleQuals.hasRestrict(); diff --git a/lib/Sema/SemaType.cpp b/lib/Sema/SemaType.cpp index 93ef1ea2ea..00dc809f51 100644 --- a/lib/Sema/SemaType.cpp +++ b/lib/Sema/SemaType.cpp @@ -1704,7 +1704,7 @@ bool Sema::RequireCompleteType(SourceLocation Loc, QualType T, // class template specialization, or an array with known size of such, // try to instantiate it. QualType MaybeTemplate = T; - if (const ConstantArrayType *Array = T->getAs()) + if (const ConstantArrayType *Array = Context.getAsConstantArrayType(T)) MaybeTemplate = Array->getElementType(); if (const RecordType *Record = MaybeTemplate->getAs()) { if (ClassTemplateSpecializationDecl *ClassTemplateSpec