From f091e129dbf75f5e52bf5ff000d4fd049cc9c06c Mon Sep 17 00:00:00 2001 From: Richard Smith Date: Tue, 15 Sep 2015 01:28:55 +0000 Subject: [PATCH] [modules] A using-declaration doesn't introduce a new entity, just a new name for an existing entity, and as such a using-declaration doesn't need to conflict with a hidden entity (nor vice versa). llvm-svn: 247654 --- clang/include/clang/Sema/Lookup.h | 5 ++ clang/lib/Sema/SemaDeclCXX.cpp | 3 +- clang/lib/Sema/SemaOverload.cpp | 5 ++ clang/test/Modules/Inputs/using-decl-b.h | 26 ++++++++ clang/test/Modules/using-decl.cpp | 76 +++++++++++++++++++++++- 5 files changed, 112 insertions(+), 3 deletions(-) diff --git a/clang/include/clang/Sema/Lookup.h b/clang/include/clang/Sema/Lookup.h index bc8574c20167..ed447ba14975 100644 --- a/clang/include/clang/Sema/Lookup.h +++ b/clang/include/clang/Sema/Lookup.h @@ -237,6 +237,11 @@ public: /// \brief Determine whether this lookup is permitted to see hidden /// declarations, such as those in modules that have not yet been imported. bool isHiddenDeclarationVisible(NamedDecl *ND) const { + // If a using-shadow declaration is hidden, it's never visible, not + // even to redeclaration lookup. + // FIXME: Should this apply to typedefs and namespace aliases too? + if (isa(ND)) + return false; return (AllowHidden && (AllowHiddenInternal || ND->isExternallyVisible())) || LookupKind == Sema::LookupTagName; diff --git a/clang/lib/Sema/SemaDeclCXX.cpp b/clang/lib/Sema/SemaDeclCXX.cpp index e80cde76e10c..ff9c93d2f696 100644 --- a/clang/lib/Sema/SemaDeclCXX.cpp +++ b/clang/lib/Sema/SemaDeclCXX.cpp @@ -7821,7 +7821,8 @@ bool Sema::CheckUsingShadowDecl(UsingDecl *Using, NamedDecl *Orig, FoundEquivalentDecl = true; } - (isa(D) ? Tag : NonTag) = D; + if (isVisible(D)) + (isa(D) ? Tag : NonTag) = D; } if (FoundEquivalentDecl) diff --git a/clang/lib/Sema/SemaOverload.cpp b/clang/lib/Sema/SemaOverload.cpp index 34a03abb96fb..0ba559830151 100644 --- a/clang/lib/Sema/SemaOverload.cpp +++ b/clang/lib/Sema/SemaOverload.cpp @@ -896,6 +896,11 @@ Sema::CheckOverload(Scope *S, FunctionDecl *New, const LookupResult &Old, OldD = cast(OldD)->getTargetDecl(); } + // A using-declaration does not conflict with another declaration + // if one of them is hidden. + if ((OldIsUsingDecl || NewIsUsingDecl) && !isVisible(*I)) + continue; + // If either declaration was introduced by a using declaration, // we'll need to use slightly different rules for matching. // Essentially, these rules are the normal rules, except that diff --git a/clang/test/Modules/Inputs/using-decl-b.h b/clang/test/Modules/Inputs/using-decl-b.h index b82526f39ff4..b4f206fa4431 100644 --- a/clang/test/Modules/Inputs/using-decl-b.h +++ b/clang/test/Modules/Inputs/using-decl-b.h @@ -9,3 +9,29 @@ namespace UsingDecl { using ::using_decl_type; using ::using_decl_var; } + +namespace X { + int conflicting_hidden_using_decl; + int conflicting_hidden_using_decl_fn(); + int conflicting_hidden_using_decl_var; + struct conflicting_hidden_using_decl_struct; + + int conflicting_hidden_using_decl_mixed_1; + int conflicting_hidden_using_decl_mixed_2(); + struct conflicting_hidden_using_decl_mixed_3 {}; +} + +using X::conflicting_hidden_using_decl; +using X::conflicting_hidden_using_decl_fn; +using X::conflicting_hidden_using_decl_var; +using X::conflicting_hidden_using_decl_struct; +int conflicting_hidden_using_decl_fn_2(); +int conflicting_hidden_using_decl_var_2; +struct conflicting_hidden_using_decl_struct_2 {}; + +using X::conflicting_hidden_using_decl_mixed_1; +using X::conflicting_hidden_using_decl_mixed_2; +using X::conflicting_hidden_using_decl_mixed_3; +int conflicting_hidden_using_decl_mixed_4; +int conflicting_hidden_using_decl_mixed_5(); +struct conflicting_hidden_using_decl_mixed_6 {}; diff --git a/clang/test/Modules/using-decl.cpp b/clang/test/Modules/using-decl.cpp index a388a5b7e711..f01688498c55 100644 --- a/clang/test/Modules/using-decl.cpp +++ b/clang/test/Modules/using-decl.cpp @@ -1,8 +1,80 @@ // RUN: rm -rf %t -// RUN: %clang_cc1 -x objective-c++ -fmodules -fimplicit-module-maps -fmodules-cache-path=%t -I %S/Inputs %s -verify +// RUN: %clang_cc1 -x objective-c++ -std=c++11 -fmodules -fimplicit-module-maps -fmodules-cache-path=%t -I %S/Inputs %s -verify -DEARLY_IMPORT +// RUN: %clang_cc1 -x objective-c++ -std=c++11 -fmodules -fimplicit-module-maps -fmodules-cache-path=%t -I %S/Inputs %s -verify -UEARLY_IMPORT +// RUN: %clang_cc1 -x objective-c++ -std=c++11 -fmodules -fimplicit-module-maps -fmodules-cache-path=%t -I %S/Inputs %s -verify -DEARLY_IMPORT -fno-modules-hide-internal-linkage +// RUN: %clang_cc1 -x objective-c++ -std=c++11 -fmodules -fimplicit-module-maps -fmodules-cache-path=%t -I %S/Inputs %s -verify -UEARLY_IMPORT -fno-modules-hide-internal-linkage +#ifdef EARLY_IMPORT @import using_decl.a; +#endif + +namespace Y { + int conflicting_hidden_using_decl; + int conflicting_hidden_using_decl_fn_2(); + int conflicting_hidden_using_decl_var_2; + struct conflicting_hidden_using_decl_struct_2; + + struct conflicting_hidden_using_decl_mixed_4 {}; + int conflicting_hidden_using_decl_mixed_5; + int conflicting_hidden_using_decl_mixed_6(); +} + +using Y::conflicting_hidden_using_decl; +int conflicting_hidden_using_decl_fn(); +int conflicting_hidden_using_decl_var; +struct conflicting_hidden_using_decl_struct {}; +using Y::conflicting_hidden_using_decl_fn_2; +using Y::conflicting_hidden_using_decl_var_2; +using Y::conflicting_hidden_using_decl_struct_2; + +struct conflicting_hidden_using_decl_mixed_1 {}; +int conflicting_hidden_using_decl_mixed_2; +int conflicting_hidden_using_decl_mixed_3(); +using Y::conflicting_hidden_using_decl_mixed_4; +using Y::conflicting_hidden_using_decl_mixed_5; +using Y::conflicting_hidden_using_decl_mixed_6; + +template int use(T); +void test_conflicting() { + use(conflicting_hidden_using_decl); + use(conflicting_hidden_using_decl_fn()); + use(conflicting_hidden_using_decl_var); + use(conflicting_hidden_using_decl_fn_2()); + use(conflicting_hidden_using_decl_var_2); + use(conflicting_hidden_using_decl_mixed_1()); + use(conflicting_hidden_using_decl_mixed_2); + use(conflicting_hidden_using_decl_mixed_3()); + use(conflicting_hidden_using_decl_mixed_4()); + use(conflicting_hidden_using_decl_mixed_5); + use(conflicting_hidden_using_decl_mixed_6()); +} + +#ifndef EARLY_IMPORT +@import using_decl.a; +#endif -// expected-no-diagnostics UsingDecl::using_decl_type x = UsingDecl::using_decl_var; UsingDecl::inner y = x; + +@import using_decl.b; + +void test_conflicting_2() { + use(conflicting_hidden_using_decl); // expected-error {{ambiguous}} + use(conflicting_hidden_using_decl_fn()); // expected-error {{ambiguous}} + use(conflicting_hidden_using_decl_var); // expected-error {{ambiguous}} + use(conflicting_hidden_using_decl_fn_2()); // expected-error {{ambiguous}} + use(conflicting_hidden_using_decl_var_2); // expected-error {{ambiguous}} + use(conflicting_hidden_using_decl_mixed_1); // ok, struct hidden + use(conflicting_hidden_using_decl_mixed_2); // expected-error {{ambiguous}} + use(conflicting_hidden_using_decl_mixed_3); // ok, struct hidden + use(conflicting_hidden_using_decl_mixed_4); // ok, struct hidden + use(conflicting_hidden_using_decl_mixed_5); // expected-error {{ambiguous}} + use(conflicting_hidden_using_decl_mixed_6); // ok, struct hidden + // expected-note@using-decl.cpp:* 7{{candidate}} + // expected-note@using-decl-b.h:* 7{{candidate}} + + int conflicting_hidden_using_decl_mixed_1::*p1; + int conflicting_hidden_using_decl_mixed_3::*p3; + int conflicting_hidden_using_decl_mixed_4::*p4; + int conflicting_hidden_using_decl_mixed_6::*p6; +}