]> granicus.if.org Git - clang/commitdiff
Don't crash when instantiating templates containing anonymous structs/unions
authorDouglas Gregor <dgregor@apple.com>
Fri, 28 Aug 2009 22:03:51 +0000 (22:03 +0000)
committerDouglas Gregor <dgregor@apple.com>
Fri, 28 Aug 2009 22:03:51 +0000 (22:03 +0000)
git-svn-id: https://llvm.org/svn/llvm-project/cfe/trunk@80397 91177308-0d34-0410-b5e6-96231b3b80d8

lib/Sema/SemaTemplateInstantiateDecl.cpp
test/SemaTemplate/instantiate-anonymous-union.cpp [new file with mode: 0644]

index 732394d867d8038cee9be960e328ce03ac0dc058..7b06bf909aa731d9b0929117ba5d44a6a3cde6ba 100644 (file)
@@ -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<CXXRecordDecl>(Other))
-    return Record->getInstantiatedFromMemberClass()->getCanonicalDecl()
-             == D->getCanonicalDecl();
-
-  if (FunctionDecl *Function = dyn_cast<FunctionDecl>(Other))
-    return Function->getInstantiatedFromMemberFunction()->getCanonicalDecl()
-             == D->getCanonicalDecl();
+  if (CXXRecordDecl *Record = dyn_cast<CXXRecordDecl>(Other)) {
+    if (CXXRecordDecl *Pattern = Record->getInstantiatedFromMemberClass())
+      return Pattern->getCanonicalDecl() == D->getCanonicalDecl();
+    else
+      return false;
+  }
+  
+  if (FunctionDecl *Function = dyn_cast<FunctionDecl>(Other)) {
+    if (FunctionDecl *Pattern = Function->getInstantiatedFromMemberFunction())
+      return Pattern->getCanonicalDecl() == D->getCanonicalDecl();
+    else
+      return false;
+  }
 
-  if (EnumDecl *Enum = dyn_cast<EnumDecl>(Other))
-    return Enum->getInstantiatedFromMemberEnum()->getCanonicalDecl()
-             == D->getCanonicalDecl();
+  if (EnumDecl *Enum = dyn_cast<EnumDecl>(Other)) {
+    if (EnumDecl *Pattern = Enum->getInstantiatedFromMemberEnum())
+      return Pattern->getCanonicalDecl() == D->getCanonicalDecl();
+    else
+      return false;
+  }
 
   if (VarDecl *Var = dyn_cast<VarDecl>(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<NamedDecl>(Other) &&
diff --git a/test/SemaTemplate/instantiate-anonymous-union.cpp b/test/SemaTemplate/instantiate-anonymous-union.cpp
new file mode 100644 (file)
index 0000000..4eb5b0c
--- /dev/null
@@ -0,0 +1,8 @@
+// RUN: clang-cc -fsyntax-only %s
+
+// FIXME: We need to test anonymous structs/unions in templates for real.
+
+template <typename T> class A { struct { }; };
+
+A<int> a0;
+