When performing name lookup for a redeclaration, ignore module
visibility restrictions. This ensures that all declarations of the
same entity end up in the same redeclaration chain, even if some of
those declarations aren't visible. While this may seem unfortunate to
some---why can't two C modules have different functions named
'f'?---it's an acknowedgment that a module does not introduce a new
"namespace" of names.
As part of this, stop merging the 'module-private' bit from previous
declarations to later declarations, because we want each declaration
in a module to stand on its own because this can effect, for example,
submodule visibility.
Note that this notion of names that are invisible to normal name
lookup but are available for redeclaration lookups is how we should
implement friend declarations and extern declarations within local
function scopes. I'm not tackling that problem now.
git-svn-id: https://llvm.org/svn/llvm-project/cfe/trunk@146980 91177308-0d34-0410-b5e6-96231b3b80d8
diff --git a/include/clang/Basic/DiagnosticSemaKinds.td b/include/clang/Basic/DiagnosticSemaKinds.td
index c0676dd..7c2e1f8 100644
--- a/include/clang/Basic/DiagnosticSemaKinds.td
+++ b/include/clang/Basic/DiagnosticSemaKinds.td
@@ -5154,8 +5154,6 @@
}
let CategoryName = "Modules Issue" in {
-def err_module_private_follows_public : Error<
- "__module_private__ declaration of %0 follows public declaration">;
def err_module_private_specialization : Error<
"%select{template|partial|member}0 specialization cannot be "
"declared __module_private__">;
diff --git a/include/clang/Sema/Lookup.h b/include/clang/Sema/Lookup.h
index 9f8a239..1b991cb 100644
--- a/include/clang/Sema/Lookup.h
+++ b/include/clang/Sema/Lookup.h
@@ -286,7 +286,7 @@
if (!D->isInIdentifierNamespace(IDNS))
return 0;
- if (isVisible(D))
+ if (Redecl == Sema::ForRedeclaration || isVisible(D))
return D;
return getAcceptableDeclSlow(D);
diff --git a/include/clang/Sema/Sema.h b/include/clang/Sema/Sema.h
index fd87d6b..9b244cd 100644
--- a/include/clang/Sema/Sema.h
+++ b/include/clang/Sema/Sema.h
@@ -1119,12 +1119,6 @@
/// \param Path The module access path.
DeclResult ActOnModuleImport(SourceLocation ImportLoc, ModuleIdPath Path);
- /// \brief Diagnose that \p New is a module-private redeclaration of
- /// \p Old.
- void diagnoseModulePrivateRedeclaration(NamedDecl *New, NamedDecl *Old,
- SourceLocation ModulePrivateKeyword
- = SourceLocation());
-
/// \brief Retrieve a suitable printing policy.
PrintingPolicy getPrintingPolicy() const;
diff --git a/lib/Sema/SemaDecl.cpp b/lib/Sema/SemaDecl.cpp
index c318503..ebcdcf5 100644
--- a/lib/Sema/SemaDecl.cpp
+++ b/lib/Sema/SemaDecl.cpp
@@ -1456,12 +1456,6 @@
if (TypedefNameDecl *Typedef = dyn_cast<TypedefNameDecl>(Old))
New->setPreviousDeclaration(Typedef);
- // __module_private__ is propagated to later declarations.
- if (Old->isModulePrivate())
- New->setModulePrivate();
- else if (New->isModulePrivate())
- diagnoseModulePrivateRedeclaration(New, Old);
-
if (getLangOptions().MicrosoftExt)
return;
@@ -2047,12 +2041,6 @@
if (Old->isPure())
New->setPure();
- // __module_private__ is propagated to later declarations.
- if (Old->isModulePrivate())
- New->setModulePrivate();
- else if (New->isModulePrivate())
- diagnoseModulePrivateRedeclaration(New, Old);
-
// Merge attributes from the parameters. These can mismatch with K&R
// declarations.
if (New->getNumParams() == Old->getNumParams())
@@ -2237,12 +2225,6 @@
return New->setInvalidDecl();
}
- // __module_private__ is propagated to later declarations.
- if (Old->isModulePrivate())
- New->setModulePrivate();
- else if (New->isModulePrivate())
- diagnoseModulePrivateRedeclaration(New, Old);
-
// Variables with external linkage are analyzed in FinalizeDeclaratorGroup.
// FIXME: The test for external storage here seems wrong? We still
@@ -5627,9 +5609,6 @@
assert(OldTemplateDecl->isMemberSpecialization());
}
- if (OldTemplateDecl->isModulePrivate())
- NewTemplateDecl->setModulePrivate();
-
} else {
if (isa<CXXMethodDecl>(NewFD)) // Set access for out-of-line definitions
NewFD->setAccess(OldDecl->getAccess());
@@ -8212,19 +8191,14 @@
AddMsStructLayoutForRecord(RD);
}
- if (PrevDecl && PrevDecl->isModulePrivate())
- New->setModulePrivate();
- else if (ModulePrivateLoc.isValid()) {
+ if (ModulePrivateLoc.isValid()) {
if (isExplicitSpecialization)
Diag(New->getLocation(), diag::err_module_private_specialization)
<< 2
<< FixItHint::CreateRemoval(ModulePrivateLoc);
- else if (PrevDecl && !PrevDecl->isModulePrivate())
- diagnoseModulePrivateRedeclaration(New, PrevDecl, ModulePrivateLoc);
// __module_private__ does not apply to local classes. However, we only
// diagnose this as an error when the declaration specifiers are
// freestanding. Here, we just ignore the __module_private__.
- // foobar
else if (!SearchDC->isFunctionOrMethod())
New->setModulePrivate();
}
@@ -9969,20 +9943,6 @@
return Import;
}
-void
-Sema::diagnoseModulePrivateRedeclaration(NamedDecl *New, NamedDecl *Old,
- SourceLocation ModulePrivateKeyword) {
- assert(!Old->isModulePrivate() && "Old is module-private!");
-
- Diag(New->getLocation(), diag::err_module_private_follows_public)
- << New->getDeclName() << SourceRange(ModulePrivateKeyword);
- Diag(Old->getLocation(), diag::note_previous_declaration)
- << Old->getDeclName();
-
- // Drop the __module_private__ from the new declaration, since it's invalid.
- New->setModulePrivate(false);
-}
-
void Sema::ActOnPragmaWeakID(IdentifierInfo* Name,
SourceLocation PragmaLoc,
SourceLocation NameLoc) {
diff --git a/lib/Sema/SemaLookup.cpp b/lib/Sema/SemaLookup.cpp
index bc1cb3f..a5fc682 100644
--- a/lib/Sema/SemaLookup.cpp
+++ b/lib/Sema/SemaLookup.cpp
@@ -1169,7 +1169,7 @@
// If this declaration is module-private and it came from an AST
// file, we can't see it.
- NamedDecl *D = getVisibleDecl(*I);
+ NamedDecl *D = R.isForRedeclaration()? *I : getVisibleDecl(*I);
if (!D)
continue;
diff --git a/lib/Sema/SemaTemplate.cpp b/lib/Sema/SemaTemplate.cpp
index 8ec4c19..d0c0f0b 100644
--- a/lib/Sema/SemaTemplate.cpp
+++ b/lib/Sema/SemaTemplate.cpp
@@ -1013,15 +1013,8 @@
NewClass, PrevClassTemplate);
NewClass->setDescribedClassTemplate(NewTemplate);
- if (PrevClassTemplate && PrevClassTemplate->isModulePrivate()) {
+ if (ModulePrivateLoc.isValid())
NewTemplate->setModulePrivate();
- } else if (ModulePrivateLoc.isValid()) {
- if (PrevClassTemplate && !PrevClassTemplate->isModulePrivate())
- diagnoseModulePrivateRedeclaration(NewTemplate, PrevClassTemplate,
- ModulePrivateLoc);
- else
- NewTemplate->setModulePrivate();
- }
// Build the type for the class template declaration now.
QualType T = NewTemplate->getInjectedClassNameSpecialization();
diff --git a/test/Modules/Inputs/module.map b/test/Modules/Inputs/module.map
index b101c01..13a44eb 100644
--- a/test/Modules/Inputs/module.map
+++ b/test/Modules/Inputs/module.map
@@ -44,7 +44,8 @@
}
module redecl_merge_top {
- header "redecl-merge-top.h"
+ header "redecl-merge-top.h"
+ explicit module Explicit { header "redecl-merge-top-explicit.h" }
}
module redecl_merge_left {
header "redecl-merge-left.h"
diff --git a/test/Modules/Inputs/module_private_left.h b/test/Modules/Inputs/module_private_left.h
index 617a7ab..ff33999 100644
--- a/test/Modules/Inputs/module_private_left.h
+++ b/test/Modules/Inputs/module_private_left.h
@@ -1,6 +1,6 @@
__module_private__ struct HiddenStruct;
-struct HiddenStruct {
+__module_private__ struct HiddenStruct {
};
@@ -10,17 +10,17 @@
__module_private__ void f1(T*);
template<typename T>
-void f1(T*);
+__module_private__ void f1(T*);
template<typename T>
__module_private__ class vector;
template<typename T>
-class vector {
+__module_private__ class vector {
};
vector<float> vec_float;
typedef __module_private__ int Integer;
-typedef int Integer;
+typedef __module_private__ int Integer;
diff --git a/test/Modules/Inputs/module_private_right.h b/test/Modules/Inputs/module_private_right.h
index e7c1bb8..53efe25 100644
--- a/test/Modules/Inputs/module_private_right.h
+++ b/test/Modules/Inputs/module_private_right.h
@@ -1,5 +1,5 @@
__module_private__ double &f0(double);
-double &f0(double);
+__module_private__ double &f0(double);
__module_private__ int hidden_var;
diff --git a/test/Modules/Inputs/redecl-merge-left.h b/test/Modules/Inputs/redecl-merge-left.h
index 8e2fa53..4e00959 100644
--- a/test/Modules/Inputs/redecl-merge-left.h
+++ b/test/Modules/Inputs/redecl-merge-left.h
@@ -10,6 +10,12 @@
@class A;
+@class Explicit;
+
+int *explicit_func(void);
+
+struct explicit_struct;
+
#ifdef __cplusplus
template<typename T> class Vector;
diff --git a/test/Modules/Inputs/redecl-merge-right.h b/test/Modules/Inputs/redecl-merge-right.h
index 6d69734..f0bfd35 100644
--- a/test/Modules/Inputs/redecl-merge-right.h
+++ b/test/Modules/Inputs/redecl-merge-right.h
@@ -9,6 +9,12 @@
@class B;
+@class Explicit;
+
+int *explicit_func(void);
+
+struct explicit_struct;
+
#ifdef __cplusplus
template<typename T> class Vector {
public:
diff --git a/test/Modules/Inputs/redecl-merge-top-explicit.h b/test/Modules/Inputs/redecl-merge-top-explicit.h
new file mode 100644
index 0000000..c8f5158
--- /dev/null
+++ b/test/Modules/Inputs/redecl-merge-top-explicit.h
@@ -0,0 +1,5 @@
+@class Explicit;
+
+int *explicit_func(void);
+
+struct explicit_struct { int member; };
diff --git a/test/Modules/module-private.cpp b/test/Modules/module-private.cpp
index eb932a8..bdfa268 100644
--- a/test/Modules/module-private.cpp
+++ b/test/Modules/module-private.cpp
@@ -31,28 +31,28 @@
// Check for private redeclarations of public entities.
template<typename T>
-class public_class_template; // expected-note{{previous declaration is here}}
+class public_class_template;
template<typename T>
-__module_private__ class public_class_template; // expected-error{{__module_private__ declaration of 'public_class_template' follows public declaration}}
+__module_private__ class public_class_template;
-typedef int public_typedef; // expected-note{{previous declaration is here}}
-typedef __module_private__ int public_typedef; // expected-error{{__module_private__ declaration of 'public_typedef' follows public declaration}}
+typedef int public_typedef;
+typedef __module_private__ int public_typedef;
-extern int public_var; // expected-note{{previous declaration is here}}
-extern __module_private__ int public_var; // expected-error{{__module_private__ declaration of 'public_var' follows public declaration}}
+extern int public_var;
+extern __module_private__ int public_var;
-void public_func(); // expected-note{{previous declaration is here}}
-__module_private__ void public_func(); // expected-error{{__module_private__ declaration of 'public_func' follows public declaration}}
+void public_func();
+__module_private__ void public_func();
template<typename T>
-void public_func_template(); // expected-note{{previous declaration is here}}
+void public_func_template();
template<typename T>
-__module_private__ void public_func_template(); // expected-error{{__module_private__ declaration of 'public_func_template' follows public declaration}}
+__module_private__ void public_func_template();
-struct public_struct; // expected-note{{previous declaration is here}}
-__module_private__ struct public_struct; // expected-error{{__module_private__ declaration of 'public_struct' follows public declaration}}
+struct public_struct;
+__module_private__ struct public_struct;
// Check for attempts to make specializations private
template<> __module_private__ void public_func_template<int>(); // expected-error{{template specialization cannot be declared __module_private__}}
diff --git a/test/Modules/redecl-merge.m b/test/Modules/redecl-merge.m
index 6497268..7021d14 100644
--- a/test/Modules/redecl-merge.m
+++ b/test/Modules/redecl-merge.m
@@ -20,6 +20,18 @@
@class B;
+// Test redeclarations of entities in explicit submodules, to make
+// sure we're maintaining the declaration chains even when normal name
+// lookup can't see what we're looking for.
+void testExplicit() {
+ Explicit *e;
+ int *(*fp)(void) = &explicit_func;
+ int *ip = explicit_func();
+
+ // FIXME: Should complain about definition not having been imported.
+ struct explicit_struct es = { 0 };
+}
+
__import_module__ redecl_merge_bottom;
@implementation B