diff --git a/clang/lib/Sema/SemaTemplateInstantiateDecl.cpp b/clang/lib/Sema/SemaTemplateInstantiateDecl.cpp index 732394d867d8..7b06bf909aa7 100644 --- a/clang/lib/Sema/SemaTemplateInstantiateDecl.cpp +++ b/clang/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 (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 (FunctionDecl *Function = dyn_cast(Other)) - return Function->getInstantiatedFromMemberFunction()->getCanonicalDecl() - == D->getCanonicalDecl(); - - 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/clang/test/SemaTemplate/instantiate-anonymous-union.cpp b/clang/test/SemaTemplate/instantiate-anonymous-union.cpp new file mode 100644 index 000000000000..4eb5b0c24cb0 --- /dev/null +++ b/clang/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; +