Implement DR85: Redeclaration of member is forbidden
Disallow the existence of a declaration of a member class that isn't a
forward declaration before it's definition.
git-svn-id: https://llvm.org/svn/llvm-project/cfe/trunk@183722 91177308-0d34-0410-b5e6-96231b3b80d8
diff --git a/include/clang/Basic/DiagnosticSemaKinds.td b/include/clang/Basic/DiagnosticSemaKinds.td
index c476ac9..302ec8b 100644
--- a/include/clang/Basic/DiagnosticSemaKinds.td
+++ b/include/clang/Basic/DiagnosticSemaKinds.td
@@ -4241,6 +4241,8 @@
"enum %0 was explicitly specialized here">;
def err_member_redeclared : Error<"class member cannot be redeclared">;
+def ext_member_redeclared : ExtWarn<"class member cannot be redeclared">,
+ InGroup<GNU>;
def err_member_redeclared_in_instantiation : Error<
"multiple overloads of %0 instantiate to the same signature %1">;
def err_member_name_of_class : Error<"member %0 has the same name as its class">;
diff --git a/lib/Sema/SemaDecl.cpp b/lib/Sema/SemaDecl.cpp
index c17328d..c519bfa 100644
--- a/lib/Sema/SemaDecl.cpp
+++ b/lib/Sema/SemaDecl.cpp
@@ -9885,6 +9885,16 @@
return TUK == TUK_Declaration ? PrevTagDecl : 0;
}
+ // C++11 [class.mem]p1:
+ // A member shall not be declared twice in the member-speciļ¬cation,
+ // except that a nested class or member class template can be declared
+ // and then later defined.
+ if (TUK == TUK_Declaration && PrevDecl->isCXXClassMember() &&
+ S->isDeclScope(PrevDecl)) {
+ Diag(NameLoc, diag::ext_member_redeclared);
+ Diag(PrevTagDecl->getLocation(), diag::note_previous_declaration);
+ }
+
if (!Invalid) {
// If this is a use, just return the declaration we found.
diff --git a/test/CXX/drs/dr0xx.cpp b/test/CXX/drs/dr0xx.cpp
index 7ff199d..5ffc751 100644
--- a/test/CXX/drs/dr0xx.cpp
+++ b/test/CXX/drs/dr0xx.cpp
@@ -862,15 +862,31 @@
B b = a; // expected-error {{no viable}}
}
-namespace dr85 { // dr85: no
+namespace dr85 { // dr85: yes
struct A {
struct B;
- struct B {};
- // FIXME: This redeclaration is invalid. Per [class.mem]p1,
- // "A member shall not be declared twice in the member-specification,
- // except that a nested class [...] can be declared then later defined"
- // This is not that case.
- struct B;
+ struct B {}; // expected-note{{previous declaration is here}}
+ struct B; // expected-error{{class member cannot be redeclared}}
+
+ union U;
+ union U {}; // expected-note{{previous declaration is here}}
+ union U; // expected-error{{class member cannot be redeclared}}
+
+#if __cplusplus >= 201103L
+ enum E1 : int;
+ enum E1 : int { e1 }; // expected-note{{previous declaration is here}}
+ enum E1 : int; // expected-error{{class member cannot be redeclared}}
+
+ enum class E2;
+ enum class E2 { e2 }; // expected-note{{previous declaration is here}}
+ enum class E2; // expected-error{{class member cannot be redeclared}}
+#endif
+ };
+
+ template <typename T>
+ struct C {
+ struct B {}; // expected-note{{previous declaration is here}}
+ struct B; // expected-error{{class member cannot be redeclared}}
};
}
diff --git a/test/SemaCXX/access.cpp b/test/SemaCXX/access.cpp
index 50f2eff..fa89ce6 100644
--- a/test/SemaCXX/access.cpp
+++ b/test/SemaCXX/access.cpp
@@ -26,9 +26,11 @@
namespace test1 {
class A {
private:
- class X; // expected-note {{previously declared 'private' here}}
+ class X; // expected-note {{previously declared 'private' here}} \
+ // expected-note {{previous declaration is here}}
public:
- class X; // expected-error {{'X' redeclared with 'public' access}}
+ class X; // expected-error {{'X' redeclared with 'public' access}} \
+ // expected-warning {{class member cannot be redeclared}}
class X {};
};
}
diff --git a/test/SemaTemplate/dependent-names.cpp b/test/SemaTemplate/dependent-names.cpp
index fa47ef5..769584d 100644
--- a/test/SemaTemplate/dependent-names.cpp
+++ b/test/SemaTemplate/dependent-names.cpp
@@ -354,7 +354,6 @@
struct A : public B { // expected-note{{'rdar12629723::X::A' declared here}}
virtual void foo() { }
};
- struct B;
struct D : T::foo { };
struct E : D { };
diff --git a/test/SemaTemplate/instantiate-member-class.cpp b/test/SemaTemplate/instantiate-member-class.cpp
index 7b42a27..3f49606 100644
--- a/test/SemaTemplate/instantiate-member-class.cpp
+++ b/test/SemaTemplate/instantiate-member-class.cpp
@@ -91,13 +91,11 @@
typedef int X;
};
typename Foo::X x;
- class Foo;
};
template class B<int>;
template <typename T> class C {
class Foo;
- class Foo;
};
template <typename T> class C<T>::Foo {
int x;
diff --git a/www/cxx_dr_status.html b/www/cxx_dr_status.html
index e782181..7ec46ab 100644
--- a/www/cxx_dr_status.html
+++ b/www/cxx_dr_status.html
@@ -548,7 +548,7 @@
<td><a href="http://www.open-std.org/jtc1/sc22/wg21/docs/cwg_defects.html#85">85</a></td>
<td>TC1</td>
<td>Redeclaration of member class</td>
- <td class="none" align="center">No</td>
+ <td class="full" align="center">Yes</td>
</tr>
<tr>
<td><a href="http://www.open-std.org/jtc1/sc22/wg21/docs/cwg_defects.html#86">86</a></td>