From a5bf7f13d7772b164750997f95ab18487bbc4114 Mon Sep 17 00:00:00 2001 From: Douglas Gregor Date: Fri, 28 Aug 2009 22:03:51 +0000 Subject: [PATCH] Don't crash when instantiating templates containing anonymous structs/unions git-svn-id: https://llvm.org/svn/llvm-project/cfe/trunk@80397 91177308-0d34-0410-b5e6-96231b3b80d8 --- lib/Sema/SemaTemplateInstantiateDecl.cpp | 40 ++++++++++++------- .../instantiate-anonymous-union.cpp | 8 ++++ 2 files changed, 34 insertions(+), 14 deletions(-) create mode 100644 test/SemaTemplate/instantiate-anonymous-union.cpp diff --git a/lib/Sema/SemaTemplateInstantiateDecl.cpp b/lib/Sema/SemaTemplateInstantiateDecl.cpp index 732394d867..7b06bf909a 100644 --- a/lib/Sema/SemaTemplateInstantiateDecl.cpp +++ b/lib/Sema/SemaTemplateInstantiateDecl.cpp @@ -1117,23 +1117,35 @@ static bool isInstantiationOf(ASTContext &Ctx, NamedDecl *D, Decl *Other) { if (D->getKind() != Other->getKind()) return false; - if (CXXRecordDecl *Record = dyn_cast(Other)) - return Record->getInstantiatedFromMemberClass()->getCanonicalDecl() - == D->getCanonicalDecl(); - - if (FunctionDecl *Function = dyn_cast(Other)) - return Function->getInstantiatedFromMemberFunction()->getCanonicalDecl() - == D->getCanonicalDecl(); + if (CXXRecordDecl *Record = dyn_cast(Other)) { + if (CXXRecordDecl *Pattern = Record->getInstantiatedFromMemberClass()) + return Pattern->getCanonicalDecl() == D->getCanonicalDecl(); + else + return false; + } + + if (FunctionDecl *Function = dyn_cast(Other)) { + if (FunctionDecl *Pattern = Function->getInstantiatedFromMemberFunction()) + return Pattern->getCanonicalDecl() == D->getCanonicalDecl(); + else + return false; + } - if (EnumDecl *Enum = dyn_cast(Other)) - return Enum->getInstantiatedFromMemberEnum()->getCanonicalDecl() - == D->getCanonicalDecl(); + if (EnumDecl *Enum = dyn_cast(Other)) { + if (EnumDecl *Pattern = Enum->getInstantiatedFromMemberEnum()) + return Pattern->getCanonicalDecl() == D->getCanonicalDecl(); + else + return false; + } if (VarDecl *Var = dyn_cast(Other)) - if (Var->isStaticDataMember()) - return Var->getInstantiatedFromStaticDataMember()->getCanonicalDecl() - == D->getCanonicalDecl(); - + if (Var->isStaticDataMember()) { + if (VarDecl *Pattern = Var->getInstantiatedFromStaticDataMember()) + return Pattern->getCanonicalDecl() == D->getCanonicalDecl(); + else + return false; + } + // FIXME: How can we find instantiations of anonymous unions? return D->getDeclName() && isa(Other) && diff --git a/test/SemaTemplate/instantiate-anonymous-union.cpp b/test/SemaTemplate/instantiate-anonymous-union.cpp new file mode 100644 index 0000000000..4eb5b0c24c --- /dev/null +++ b/test/SemaTemplate/instantiate-anonymous-union.cpp @@ -0,0 +1,8 @@ +// RUN: clang-cc -fsyntax-only %s + +// FIXME: We need to test anonymous structs/unions in templates for real. + +template class A { struct { }; }; + +A a0; + -- 2.40.0