If an instantiation of a template is required to be a complete type, check
whether the definition of the template is visible rather than checking whether
the instantiated definition happens to be in an imported module.

llvm-svn: 208150
diff --git a/clang/test/Modules/Inputs/cxx-templates-b-impl.h b/clang/test/Modules/Inputs/cxx-templates-b-impl.h
index fdf4a4f..93d0574 100644
--- a/clang/test/Modules/Inputs/cxx-templates-b-impl.h
+++ b/clang/test/Modules/Inputs/cxx-templates-b-impl.h
@@ -3,3 +3,10 @@
   struct Inner {};
   friend void FoundByADL(DefinedInBImpl);
 };
+
+@import cxx_templates_common;
+template struct TemplateInstantiationVisibility<char[1]>;
+extern template struct TemplateInstantiationVisibility<char[2]>;
+template<> struct TemplateInstantiationVisibility<char[3]> {};
+extern TemplateInstantiationVisibility<char[4]>::type
+    TemplateInstantiationVisibility_ImplicitInstantiation;
diff --git a/clang/test/Modules/Inputs/cxx-templates-common.h b/clang/test/Modules/Inputs/cxx-templates-common.h
index 9b46539..efbda2b 100644
--- a/clang/test/Modules/Inputs/cxx-templates-common.h
+++ b/clang/test/Modules/Inputs/cxx-templates-common.h
@@ -21,3 +21,5 @@
     extern T g();
   }
 }
+
+template<typename T> struct TemplateInstantiationVisibility { typedef int type; };
diff --git a/clang/test/Modules/cxx-templates.cpp b/clang/test/Modules/cxx-templates.cpp
index 82ecca3..ab65e3d 100644
--- a/clang/test/Modules/cxx-templates.cpp
+++ b/clang/test/Modules/cxx-templates.cpp
@@ -1,8 +1,8 @@
 // RUN: rm -rf %t
-// RUN: not %clang_cc1 -x objective-c++ -fmodules -fmodules-cache-path=%t -I %S/Inputs %s -std=c++11 -ast-dump -ast-dump-lookups | FileCheck %s --check-prefix=CHECK-GLOBAL
-// RUN: not %clang_cc1 -x objective-c++ -fmodules -fmodules-cache-path=%t -I %S/Inputs %s -std=c++11 -ast-dump -ast-dump-lookups -ast-dump-filter N | FileCheck %s --check-prefix=CHECK-NAMESPACE-N
-// RUN: not %clang_cc1 -x objective-c++ -fmodules -fmodules-cache-path=%t -I %S/Inputs %s -std=c++11 -ast-dump | FileCheck %s --check-prefix=CHECK-DUMP
-// RUN: %clang_cc1 -x objective-c++ -fmodules -fmodules-cache-path=%t -I %S/Inputs %s -verify -std=c++11
+// RUN: not %clang_cc1 -x objective-c++ -fmodules -fno-modules-error-recovery -fmodules-cache-path=%t -I %S/Inputs %s -std=c++11 -ast-dump -ast-dump-lookups | FileCheck %s --check-prefix=CHECK-GLOBAL
+// RUN: not %clang_cc1 -x objective-c++ -fmodules -fno-modules-error-recovery -fmodules-cache-path=%t -I %S/Inputs %s -std=c++11 -ast-dump -ast-dump-lookups -ast-dump-filter N | FileCheck %s --check-prefix=CHECK-NAMESPACE-N
+// RUN: not %clang_cc1 -x objective-c++ -fmodules -fno-modules-error-recovery -fmodules-cache-path=%t -I %S/Inputs %s -std=c++11 -ast-dump | FileCheck %s --check-prefix=CHECK-DUMP
+// RUN: %clang_cc1 -x objective-c++ -fmodules -fno-modules-error-recovery -fmodules-cache-path=%t -I %S/Inputs %s -verify -std=c++11
 
 @import cxx_templates_a;
 @import cxx_templates_b;
@@ -71,8 +71,15 @@
   // Trigger the instantiation of a template in 'a' that uses a type defined in
   // 'b_impl'. That type is not visible here, nor in 'a'. This fails; there is
   // no reason why DefinedInBImpl should be visible here.
+  //
+  // We turn off error recovery for modules in this test (so we don't get an
+  // implicit import of cxx_templates_b_impl), and that results in us producing
+  // a big spew of errors here.
+  //
   // expected-error@Inputs/cxx-templates-a.h:19 {{definition of 'DefinedInBImpl' must be imported}}
-  // expected-note@Inputs/cxx-templates-b-impl.h:1 {{definition is here}}
+  // expected-note@Inputs/cxx-templates-b-impl.h:1 +{{definition is here}}
+  // expected-error@Inputs/cxx-templates-a.h:19 +{{}}
+  // expected-error@Inputs/cxx-templates-a.h:20 +{{}}
   PerformDelayedLookup(defined_in_b_impl); // expected-note {{in instantiation of}}
 
   merge_templates_a = merge_templates_b; // ok, same type
@@ -89,6 +96,12 @@
   static_assert(enum_c_from_a == enum_c_from_b, "");
   CommonTemplate<int> cti;
   CommonTemplate<int>::E eee = CommonTemplate<int>::c;
+
+  TemplateInstantiationVisibility<char[1]> tiv1;
+  TemplateInstantiationVisibility<char[2]> tiv2;
+  TemplateInstantiationVisibility<char[3]> tiv3; // expected-error {{must be imported from module 'cxx_templates_b_impl'}}
+  // expected-note@cxx-templates-b-impl.h:10 {{previous definition is here}}
+  TemplateInstantiationVisibility<char[4]> tiv4;
 }
 
 RedeclaredAsFriend<int> raf1;
diff --git a/clang/test/Modules/template-specialization-visibility.cpp b/clang/test/Modules/template-specialization-visibility.cpp
index cc11a17..efcfd93 100644
--- a/clang/test/Modules/template-specialization-visibility.cpp
+++ b/clang/test/Modules/template-specialization-visibility.cpp
@@ -1,43 +1,26 @@
 // RUN: rm -rf %t
 // RUN: %clang_cc1 -fmodules -verify -fmodules-cache-path=%t -I %S/Inputs/template-specialization-visibility -std=c++11 %s
 //
-// FIXME: We should accept the explicit instantiation cases below too.
-// Note, errors trigger implicit imports, so only enable one error at a time.
-// RUN: %clang_cc1 -fmodules -verify -fmodules-cache-path=%t -I %S/Inputs/template-specialization-visibility -std=c++11 -DERR1 %s
-// RUN: %clang_cc1 -fmodules -verify -fmodules-cache-path=%t -I %S/Inputs/template-specialization-visibility -std=c++11 -DERR2 %s
-// RUN: %clang_cc1 -fmodules -verify -fmodules-cache-path=%t -I %S/Inputs/template-specialization-visibility -std=c++11 -DERR3 %s
+// expected-no-diagnostics
 
 #include "c.h"
 
 S<int> implicit_inst_class_template;
 int k1 = implicit_inst_class_template.n;
 
-#ifdef ERR1
-S<char> explicit_inst_class_template; // expected-error {{must be imported from module 'tsv.e'}}
-// expected-note@e.h:4 {{previous}}
+S<char> explicit_inst_class_template;
 int k2 = explicit_inst_class_template.n;
-#endif
 
 #include "a.h"
 
 T<int>::S implicit_inst_member_class_template;
 int k3 = implicit_inst_member_class_template.n;
 
-#ifdef ERR2
-T<char>::S explicit_inst_member_class_template; // expected-error {{must be imported from module 'tsv.e'}}
-// expected-note@e.h:5 {{previous}}
+T<char>::S explicit_inst_member_class_template;
 int k4 = explicit_inst_member_class_template.n;
-#endif
 
 T<int>::E implicit_inst_member_enum_template;
 int k5 = decltype(implicit_inst_member_enum_template)::e;
 
-#ifdef ERR3
-T<char>::E explicit_inst_member_enum_template; // expected-error {{must be imported from module 'tsv.e'}}
-// expected-note@e.h:5 {{previous}}
+T<char>::E explicit_inst_member_enum_template;
 int k6 = decltype(explicit_inst_member_enum_template)::e;
-#endif
-
-#if ERR1 + ERR2 + ERR3 == 0
-// expected-no-diagnostics
-#endif