]> granicus.if.org Git - clang/commitdiff
Fix implementation of C++'s restrictions on using-declarations referring to enumerators:
authorRichard Smith <richard-llvm@metafoo.co.uk>
Thu, 5 May 2016 02:13:49 +0000 (02:13 +0000)
committerRichard Smith <richard-llvm@metafoo.co.uk>
Thu, 5 May 2016 02:13:49 +0000 (02:13 +0000)
 * 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.

git-svn-id: https://llvm.org/svn/llvm-project/cfe/trunk@268594 91177308-0d34-0410-b5e6-96231b3b80d8

include/clang/Basic/DiagnosticSemaKinds.td
lib/Sema/SemaDeclCXX.cpp
test/CXX/dcl.dcl/basic.namespace/namespace.udecl/p3-cxx0x.cpp [deleted file]
test/CXX/dcl.dcl/basic.namespace/namespace.udecl/p3.cpp [new file with mode: 0644]
test/CXX/dcl.dcl/basic.namespace/namespace.udecl/p6-cxx11.cpp [moved from test/CXX/dcl.dcl/basic.namespace/namespace.udecl/p6-cxx0x.cpp with 83% similarity]
test/CXX/drs/dr4xx.cpp
test/SemaCXX/enum-scoped.cpp

index 413d5939abdf4ca4adf0f1bc9a9f1cce71f96574..b0b6004ac0a2714e4610c4564ca9b4b8e1ae711e 100644 (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<
index 21db3be3ccf25a8b4db7b0fe896b9dff94264a86..f478f0419b2223de2900ed70e1ea7ee36ccf268a 100644 (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.
diff --git a/test/CXX/dcl.dcl/basic.namespace/namespace.udecl/p3-cxx0x.cpp b/test/CXX/dcl.dcl/basic.namespace/namespace.udecl/p3-cxx0x.cpp
deleted file mode 100644 (file)
index f61437e..0000000
+++ /dev/null
@@ -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}}
-  };
-}
diff --git a/test/CXX/dcl.dcl/basic.namespace/namespace.udecl/p3.cpp b/test/CXX/dcl.dcl/basic.namespace/namespace.udecl/p3.cpp
new file mode 100644 (file)
index 0000000..6c505a5
--- /dev/null
@@ -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
+  };
+}
similarity index 83%
rename from test/CXX/dcl.dcl/basic.namespace/namespace.udecl/p6-cxx0x.cpp
rename to test/CXX/dcl.dcl/basic.namespace/namespace.udecl/p6-cxx11.cpp
index c2fb95902454d7e1e6ec1598bb3234bf6d7dfc73..97b2953b903123c2529f5b8882702db3a009cdd4 100644 (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 namespace}}
index 09298cb0108c819bb74dc782839f1944cc1a6f96..b1c21f8631dd1a320995380f7d10e7dd73e3c36c 100644 (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 namespace}}
+    using X::Q; // expected-error {{cannot refer to namespace}}
   }
 }
 
index 909802335e46adfaf8f773fa4c1ab115a5b60eb2..142edd3893aae690e366e33beb34cf1f8ab50215 100644 (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 {