[modules] Allow a function template definition if we have a pre-existing but not...
authorRichard Smith <richard-llvm@metafoo.co.uk>
Fri, 27 Mar 2015 21:57:41 +0000 (21:57 +0000)
committerRichard Smith <richard-llvm@metafoo.co.uk>
Fri, 27 Mar 2015 21:57:41 +0000 (21:57 +0000)
llvm-svn: 233430

clang/lib/Sema/SemaDecl.cpp
clang/test/Modules/Inputs/submodules-merge-defs/defs.h
clang/test/Modules/submodules-merge-defs.cpp

index 3b9bb842c4e5a28bdf60590262334fbe41a4adc6..ce183d0adf43a4b4f6413aa05d8f20fdc26aa9d1 100644 (file)
@@ -10282,13 +10282,16 @@ Sema::CheckForFunctionRedefinition(FunctionDecl *FD,
   if (canRedefineFunction(Definition, getLangOpts()))
     return;
 
-  // If we don't have a visible definition of the function, and it's inline,
-  // it's OK to form another definition of it.
+  // If we don't have a visible definition of the function, and it's inline or
+  // a template, it's OK to form another definition of it.
   //
   // FIXME: Should we skip the body of the function and use the old definition
   // in this case? That may be necessary for functions that return local types
   // through a deduced return type, or instantiate templates with local types.
-  if (!hasVisibleDefinition(Definition) && Definition->isInlineSpecified())
+  if (!hasVisibleDefinition(Definition) &&
+      (Definition->isInlineSpecified() ||
+       Definition->getDescribedFunctionTemplate() ||
+       Definition->getNumTemplateParameterLists()))
     return;
 
   if (getLangOpts().GNUMode && Definition->isInlineSpecified() &&
index 3a9c2327c7a7048af0bf66e19e369fb70f2bf96d..89e4b2319c9dbf1c3c33c6fe5af348e571050375 100644 (file)
@@ -5,9 +5,11 @@ class B {
   struct Inner1 {};
 public:
   struct Inner2;
+  template<typename T> void f();
 };
 // Check that lookup and access checks are performed in the right context.
 struct B::Inner2 : Inner1 {};
+template<typename T> void B::f() {}
 
 // Check that base-specifiers are correctly disambiguated.
 template<int N> struct C_Base { struct D { constexpr operator int() const { return 0; } }; };
@@ -18,3 +20,12 @@ struct C2 : C_Base<C_Const<0>::D{} extern c2;
 typedef struct { int a; void f(); struct X; } D;
 struct D::X { int dx; } extern dx;
 inline int use_dx(D::X dx) { return dx.dx; }
+
+template<typename T> int E(T t) { return t; }
+
+template<typename T> struct F {
+  int f();
+  template<typename U> int g();
+};
+template<typename T> int F<T>::f() { return 0; }
+template<typename T> template<typename U> int F<T>::g() { return 0; }
index 3bc52fee828f3bc487cf52aef58f032e6abf9c66..79213cae8fa26d4e6ec09f879d502cafb4dd3d4e 100644 (file)
@@ -12,21 +12,28 @@ int pre_use_a = use_a(pre_a); // expected-error {{'A' must be imported}}
 
 B::Inner2 pre_bi; // expected-error +{{must be imported}}
 // expected-note@defs.h:4 +{{here}}
-// expected-note@defs.h:10 +{{here}}
+// expected-note@defs.h:11 +{{here}}
 
 C_Base<1> pre_cb1; // expected-error +{{must be imported}}
-// expected-note@defs.h:13 +{{here}}
-C1 pre_c1; // expected-error +{{must be imported}} expected-error {{must use 'struct'}}
 // expected-note@defs.h:15 +{{here}}
+C1 pre_c1; // expected-error +{{must be imported}} expected-error {{must use 'struct'}}
+// expected-note@defs.h:17 +{{here}}
 C2 pre_c2; // expected-error +{{must be imported}} expected-error {{must use 'struct'}}
-// expected-note@defs.h:16 +{{here}}
+// expected-note@defs.h:18 +{{here}}
 
 D::X pre_dx; // expected-error +{{must be imported}}
-// expected-note@defs.h:18 +{{here}}
-// expected-note@defs.h:19 +{{here}}
+// expected-note@defs.h:20 +{{here}}
+// expected-note@defs.h:21 +{{here}}
 // FIXME: We should warn that use_dx is being used without being imported.
 int pre_use_dx = use_dx(pre_dx);
 
+int pre_e = E(0); // expected-error {{must be imported}}
+// expected-note@defs.h:24 +{{here}}
+
+int pre_ff = F<int>().f(); // expected-error +{{must be imported}}
+int pre_fg = F<int>().g<int>(); // expected-error +{{must be imported}}
+// expected-note@defs.h:26 +{{here}}
+
 // Make definitions from second module visible.
 #ifdef TEXTUAL
 #include "import-and-redefine.h"
@@ -42,3 +49,6 @@ C1 c1;
 C2 c2;
 D::X post_dx;
 int post_use_dx = use_dx(post_dx);
+int post_e = E(0);
+int post_ff = F<char>().f();
+int post_fg = F<char>().g<int>();