r233407 - [modules] Allow a function to be redefined if the old definition is not visible.

Richard Smith richard-llvm at metafoo.co.uk
Fri Mar 27 13:16:58 PDT 2015


Author: rsmith
Date: Fri Mar 27 15:16:58 2015
New Revision: 233407

URL: http://llvm.org/viewvc/llvm-project?rev=233407&view=rev
Log:
[modules] Allow a function to be redefined if the old definition is not visible.

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

Modified: cfe/trunk/include/clang/Sema/Sema.h
URL: http://llvm.org/viewvc/llvm-project/cfe/trunk/include/clang/Sema/Sema.h?rev=233407&r1=233406&r2=233407&view=diff
==============================================================================
--- cfe/trunk/include/clang/Sema/Sema.h (original)
+++ cfe/trunk/include/clang/Sema/Sema.h Fri Mar 27 15:16:58 2015
@@ -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);

Modified: cfe/trunk/lib/Sema/SemaDecl.cpp
URL: http://llvm.org/viewvc/llvm-project/cfe/trunk/lib/Sema/SemaDecl.cpp?rev=233407&r1=233406&r2=233407&view=diff
==============================================================================
--- cfe/trunk/lib/Sema/SemaDecl.cpp (original)
+++ cfe/trunk/lib/Sema/SemaDecl.cpp Fri Mar 27 15:16:58 2015
@@ -10282,6 +10282,15 @@ Sema::CheckForFunctionRedefinition(Funct
   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)

Modified: cfe/trunk/lib/Sema/SemaLookup.cpp
URL: http://llvm.org/viewvc/llvm-project/cfe/trunk/lib/Sema/SemaLookup.cpp?rev=233407&r1=233406&r2=233407&view=diff
==============================================================================
--- cfe/trunk/lib/Sema/SemaLookup.cpp (original)
+++ cfe/trunk/lib/Sema/SemaLookup.cpp Fri Mar 27 15:16:58 2015
@@ -1226,8 +1226,7 @@ bool LookupResult::isVisibleSlow(Sema &S
   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.

Modified: cfe/trunk/test/Modules/Inputs/submodules-merge-defs/defs.h
URL: http://llvm.org/viewvc/llvm-project/cfe/trunk/test/Modules/Inputs/submodules-merge-defs/defs.h?rev=233407&r1=233406&r2=233407&view=diff
==============================================================================
--- cfe/trunk/test/Modules/Inputs/submodules-merge-defs/defs.h (original)
+++ cfe/trunk/test/Modules/Inputs/submodules-merge-defs/defs.h Fri Mar 27 15:16:58 2015
@@ -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{} exter
 
 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; }

Modified: cfe/trunk/test/Modules/submodules-merge-defs.cpp
URL: http://llvm.org/viewvc/llvm-project/cfe/trunk/test/Modules/submodules-merge-defs.cpp?rev=233407&r1=233406&r2=233407&view=diff
==============================================================================
--- cfe/trunk/test/Modules/submodules-merge-defs.cpp (original)
+++ cfe/trunk/test/Modules/submodules-merge-defs.cpp Fri Mar 27 15:16:58 2015
@@ -5,7 +5,9 @@
 #include "empty.h"
 
 A pre_a; // expected-error {{must be imported}} expected-error {{must use 'struct'}}
-// expected-note at defs.h:1 {{here}}
+// expected-note at 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 at defs.h:4 +{{here}}
@@ -21,13 +23,17 @@ C2 pre_c2; // expected-error +{{must be
 D::X pre_dx; // expected-error +{{must be imported}}
 // expected-note at defs.h:18 +{{here}}
 // expected-note at 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);





More information about the cfe-commits mailing list