]> granicus.if.org Git - clang/commitdiff
[modules] Allow a function to be redefined if the old definition is not visible.
authorRichard Smith <richard-llvm@metafoo.co.uk>
Fri, 27 Mar 2015 20:16:58 +0000 (20:16 +0000)
committerRichard Smith <richard-llvm@metafoo.co.uk>
Fri, 27 Mar 2015 20:16:58 +0000 (20:16 +0000)
git-svn-id: https://llvm.org/svn/llvm-project/cfe/trunk@233407 91177308-0d34-0410-b5e6-96231b3b80d8

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

index 88217b9283d0aebdfca26f22ec898fe66d188128..7078cd8cdca8b68d1bfd241410ab68b538df4c98 100644 (file)
@@ -1282,6 +1282,10 @@ public:
   /// Determine if \p D has a visible definition. If not, suggest a declaration
   /// that should be made visible to expose the definition.
   bool hasVisibleDefinition(NamedDecl *D, NamedDecl **Suggested);
+  bool hasVisibleDefinition(const NamedDecl *D) {
+    NamedDecl *Hidden;
+    return hasVisibleDefinition(const_cast<NamedDecl*>(D), &Hidden);
+  }
 
   bool RequireCompleteType(SourceLocation Loc, QualType T,
                            TypeDiagnoser &Diagnoser);
index 9bbfeaf19effebe25a800044b980d1583754a29b..3b9bb842c4e5a28bdf60590262334fbe41a4adc6 100644 (file)
@@ -10282,6 +10282,15 @@ 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.
+  //
+  // 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())
+    return;
+
   if (getLangOpts().GNUMode && Definition->isInlineSpecified() &&
       Definition->getStorageClass() == SC_Extern)
     Diag(FD->getLocation(), diag::err_redefinition_extern_inline)
index ac7376efbc4105694cc40de217db07caa5bfc663..1244829f2ba848578c0a047a98616abb2ffc2f81 100644 (file)
@@ -1226,8 +1226,7 @@ bool LookupResult::isVisibleSlow(Sema &SemaRef, NamedDecl *D) {
   DeclContext *DC = D->getLexicalDeclContext();
   if (!D->isModulePrivate() &&
       DC && !DC->isFileContext() && !isa<LinkageSpecDecl>(DC)) {
-    NamedDecl *Hidden;
-    if (SemaRef.hasVisibleDefinition(cast<NamedDecl>(DC), &Hidden)) {
+    if (SemaRef.hasVisibleDefinition(cast<NamedDecl>(DC))) {
       if (SemaRef.ActiveTemplateInstantiations.empty()) {
         // Cache the fact that this declaration is implicitly visible because
         // its parent has a visible definition.
index 16f71840d0a12ba3aaa924729a700fbc7b8a8bc4..3a9c2327c7a7048af0bf66e19e369fb70f2bf96d 100644 (file)
@@ -1,5 +1,5 @@
 struct A { int a_member; };
-namespace { inline int use_a(A a) { return a.a_member; } }
+inline int use_a(A a) { return a.a_member; }
 
 class B {
   struct Inner1 {};
@@ -17,4 +17,4 @@ 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;
-namespace { inline int use_dx(D::X dx) { return dx.dx; } }
+inline int use_dx(D::X dx) { return dx.dx; }
index cdda48ea65211b507e3105d8f7f9382e1f2a222b..86e50368a9ea153b6f32d8d4f792329f1b14d7ee 100644 (file)
@@ -5,7 +5,9 @@
 #include "empty.h"
 
 A pre_a; // expected-error {{must be imported}} expected-error {{must use 'struct'}}
-// expected-note@defs.h:1 {{here}}
+// expected-note@defs.h:1 +{{here}}
+// FIXME: We should warn that use_a is being used without being imported.
+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}}
@@ -21,13 +23,17 @@ C2 pre_c2; // expected-error +{{must be imported}} expected-error {{must use 'st
 D::X pre_dx; // expected-error +{{must be imported}}
 // expected-note@defs.h:18 +{{here}}
 // expected-note@defs.h:19 +{{here}}
+// FIXME: We should warn that use_dx is being used without being imported.
+int pre_use_dx = use_dx(pre_dx);
 
 // Make definitions from second module visible.
 #include "import-and-redefine.h"
 
 A post_a;
+int post_use_a = use_a(post_a);
 B::Inner2 post_bi;
 C_Base<1> post_cb1;
 C1 c1;
 C2 c2;
 D::X post_dx;
+int post_use_dx = use_dx(post_dx);