Fix implementation of C++'s restrictions on using-declarations referring to enumerators:

* an unscoped enumerator whose enumeration is a class member is itself a class
   member, so can only be the subject of a class-scope using-declaration.

 * a scoped enumerator cannot be the subject of a class-scope using-declaration.

llvm-svn: 268594
This commit is contained in:
Richard Smith 2016-05-05 02:13:49 +00:00
parent 5ee5408625
commit 5cbeb75a99
7 changed files with 107 additions and 58 deletions

View File

@ -396,7 +396,9 @@ def note_using_decl_class_member_workaround : Note<
"use %select{an alias declaration|a typedef declaration|a reference}0 "
"instead">;
def err_using_decl_can_not_refer_to_namespace : Error<
"using declaration cannot refer to namespace">;
"using declaration cannot refer to a namespace">;
def err_using_decl_can_not_refer_to_scoped_enum : Error<
"using declaration cannot refer to a scoped enumerator">;
def err_using_decl_constructor : Error<
"using declaration cannot refer to a constructor">;
def warn_cxx98_compat_using_decl_constructor : Warning<

View File

@ -7738,7 +7738,7 @@ bool Sema::CheckUsingShadowDecl(UsingDecl *Using, NamedDecl *Orig,
// function will silently decide not to build a shadow decl, which
// will pre-empt further diagnostics.
//
// We don't need to do this in C++0x because we do the check once on
// We don't need to do this in C++11 because we do the check once on
// the qualifier.
//
// FIXME: diagnose the following if we care enough:
@ -8227,7 +8227,7 @@ NamedDecl *Sema::BuildUsingDeclaration(Scope *S, AccessSpecifier AS,
}
}
// C++0x N2914 [namespace.udecl]p6:
// C++14 [namespace.udecl]p6:
// A using-declaration shall not name a namespace.
if (R.getAsSingle<NamespaceDecl>()) {
Diag(IdentLoc, diag::err_using_decl_can_not_refer_to_namespace)
@ -8235,6 +8235,16 @@ NamedDecl *Sema::BuildUsingDeclaration(Scope *S, AccessSpecifier AS,
return BuildInvalid();
}
// C++14 [namespace.udecl]p7:
// A using-declaration shall not name a scoped enumerator.
if (auto *ED = R.getAsSingle<EnumConstantDecl>()) {
if (cast<EnumDecl>(ED->getDeclContext())->isScoped()) {
Diag(IdentLoc, diag::err_using_decl_can_not_refer_to_scoped_enum)
<< SS.getRange();
return BuildInvalid();
}
}
UsingDecl *UD = BuildValid();
// The normal rules do not apply to inheriting constructor declarations.
@ -8359,8 +8369,10 @@ bool Sema::CheckUsingDeclQualifier(SourceLocation UsingLoc,
// If we weren't able to compute a valid scope, it must be a
// dependent class scope.
if (!NamedContext || NamedContext->isRecord()) {
auto *RD = dyn_cast_or_null<CXXRecordDecl>(NamedContext);
if (!NamedContext || NamedContext->getRedeclContext()->isRecord()) {
auto *RD = NamedContext
? cast<CXXRecordDecl>(NamedContext->getRedeclContext())
: nullptr;
if (RD && RequireCompleteDeclContext(const_cast<CXXScopeSpec&>(SS), RD))
RD = nullptr;
@ -8444,7 +8456,7 @@ bool Sema::CheckUsingDeclQualifier(SourceLocation UsingLoc,
return true;
if (getLangOpts().CPlusPlus11) {
// C++0x [namespace.udecl]p3:
// C++11 [namespace.udecl]p3:
// In a using-declaration used as a member-declaration, the
// nested-name-specifier shall name a base class of the class
// being defined.

View File

@ -1,46 +0,0 @@
// RUN: %clang_cc1 -std=c++11 -fsyntax-only -verify %s
// C++0x N2914.
struct B {
void f(char);
void g(char);
enum E { e };
union { int x; };
};
class C {
int g();
};
class D2 : public B {
using B::f;
using B::e;
using B::x;
using C::g; // expected-error{{using declaration refers into 'C::', which is not a base class of 'D2'}}
};
namespace test1 {
struct Base {
int foo();
};
struct Unrelated {
int foo();
};
struct Subclass : Base {
};
namespace InnerNS {
int foo();
}
// We should be able to diagnose these without instantiation.
template <class T> struct C : Base {
using InnerNS::foo; // expected-error {{not a class}}
using Base::bar; // expected-error {{no member named 'bar'}}
using Unrelated::foo; // expected-error {{not a base class}}
using C::foo; // expected-error {{refers to its own class}}
using Subclass::foo; // expected-error {{not a base class}}
};
}

View File

@ -0,0 +1,82 @@
// RUN: %clang_cc1 -std=c++98 -fsyntax-only -verify %s
// RUN: %clang_cc1 -std=c++11 -fsyntax-only -verify %s
// RUN: %clang_cc1 -fsyntax-only -verify %s
struct B {
void f(char);
void g(char);
enum E { e };
union { int x; };
enum class EC { ec }; // expected-warning 0-1 {{C++11}}
void f2(char);
void g2(char);
enum E2 { e2 };
union { int x2; };
};
class C {
int g();
};
struct D : B {};
class D2 : public B {
using B::f;
using B::E;
using B::e;
using B::x;
using C::g; // expected-error{{using declaration refers into 'C::', which is not a base class of 'D2'}}
// These are valid in C++98 but not in C++11.
using D::f2;
using D::E2;
using D::e2;
using D::x2;
#if __cplusplus >= 201103L
// expected-error@-5 {{using declaration refers into 'D::', which is not a base class of 'D2'}}
// expected-error@-5 {{using declaration refers into 'D::', which is not a base class of 'D2'}}
// expected-error@-5 {{using declaration refers into 'D::', which is not a base class of 'D2'}}
// expected-error@-5 {{using declaration refers into 'D::', which is not a base class of 'D2'}}
#endif
using B::EC;
using B::EC::ec; // expected-error {{not a class}} expected-warning 0-1 {{C++11}}
};
namespace test1 {
struct Base {
int foo();
};
struct Unrelated {
int foo();
};
struct Subclass : Base {
};
namespace InnerNS {
int foo();
}
struct B : Base {
};
// We should be able to diagnose these without instantiation.
template <class T> struct C : Base {
using InnerNS::foo; // expected-error {{not a class}}
using Base::bar; // expected-error {{no member named 'bar'}}
using Unrelated::foo; // expected-error {{not a base class}}
// In C++98, it's hard to see that these are invalid, because indirect
// references to base class members are permitted.
using C::foo;
using Subclass::foo;
#if __cplusplus >= 201103L
// expected-error@-3 {{refers to its own class}}
// expected-error@-3 {{not a base class}}
#endif
};
}

View File

@ -1,8 +1,7 @@
// RUN: %clang_cc1 -fsyntax-only -verify %s
// C++0x N2914.
namespace A {
namespace B { }
}
using A::B; // expected-error{{using declaration cannot refer to namespace}}
using A::B; // expected-error{{using declaration cannot refer to a namespace}}

View File

@ -702,8 +702,8 @@ namespace dr460 { // dr460: yes
namespace X { namespace Q { int n; } }
namespace Y {
using X; // expected-error {{requires a qualified name}}
using dr460::X; // expected-error {{cannot refer to namespace}}
using X::Q; // expected-error {{cannot refer to namespace}}
using dr460::X; // expected-error {{cannot refer to a namespace}}
using X::Q; // expected-error {{cannot refer to a namespace}}
}
}

View File

@ -298,8 +298,8 @@ namespace PR18044 {
int E::*p; // expected-error {{does not point into a class}}
using E::f; // expected-error {{no member named 'f'}}
using E::a; // ok!
E b = a;
using E::a; // expected-error {{using declaration cannot refer to a scoped enumerator}}
E b = a; // expected-error {{undeclared}}
}
namespace test11 {