Use the canonical template decl when trying to find if it has a visibility
attribute.

git-svn-id: https://llvm.org/svn/llvm-project/cfe/trunk@160139 91177308-0d34-0410-b5e6-96231b3b80d8
diff --git a/lib/AST/Decl.cpp b/lib/AST/Decl.cpp
index 33826cb..2066a98 100644
--- a/lib/AST/Decl.cpp
+++ b/lib/AST/Decl.cpp
@@ -702,8 +702,10 @@
   // specialization of a class template, check for visibility
   // on the pattern.
   if (const ClassTemplateSpecializationDecl *spec
-        = dyn_cast<ClassTemplateSpecializationDecl>(this))
-    return getVisibilityOf(spec->getSpecializedTemplate()->getTemplatedDecl());
+      = dyn_cast<ClassTemplateSpecializationDecl>(this)) {
+    ClassTemplateDecl *TD = spec->getSpecializedTemplate()->getCanonicalDecl();
+    return getVisibilityOf(TD->getTemplatedDecl());
+  }
 
   // If this is a member class of a specialization of a class template
   // and the corresponding decl has explicit visibility, use that.
diff --git a/test/CodeGenCXX/visibility.cpp b/test/CodeGenCXX/visibility.cpp
index 583293b..94fb290 100644
--- a/test/CodeGenCXX/visibility.cpp
+++ b/test/CodeGenCXX/visibility.cpp
@@ -1018,3 +1018,16 @@
   // CHECK: declare hidden void @_ZN6test543fooINS_3zedEE3barEv
   // CHECK-HIDDEN: declare hidden void @_ZN6test543fooINS_3zedEE3barEv
 }
+
+namespace test55 {
+  template <class T>
+  struct __attribute__((visibility("hidden"))) foo {
+    static void bar();
+  };
+  template <class T> struct foo;
+  void foobar() {
+    foo<int>::bar();
+  }
+  // CHECK: declare hidden void @_ZN6test553fooIiE3barEv
+  // CHECK-HIDDEN: declare hidden void @_ZN6test553fooIiE3barEv
+}