[clang] 6c906f7 - [Sema] Diagnose more cases of static data members in local or unnamed classes
John Brawn via cfe-commits
cfe-commits at lists.llvm.org
Tue May 26 05:38:28 PDT 2020
Author: John Brawn
Date: 2020-05-26T13:29:59+01:00
New Revision: 6c906f7785dad3a1dea5357cfde0762952c2a2bd
URL: https://github.com/llvm/llvm-project/commit/6c906f7785dad3a1dea5357cfde0762952c2a2bd
DIFF: https://github.com/llvm/llvm-project/commit/6c906f7785dad3a1dea5357cfde0762952c2a2bd.diff
LOG: [Sema] Diagnose more cases of static data members in local or unnamed classes
We currently diagnose static data members directly contained in unnamed classes,
but we should also diagnose when they're in a class that is nested (directly or
indirectly) in an unnamed class. Do this by iterating up the list of parent
DeclContexts and checking if any is an unnamed class.
Similarly also check for function or method DeclContexts (which includes things
like blocks and openmp captured statements) as then the class is considered to
be a local class, which means static data members aren't allowed.
Differential Revision: https://reviews.llvm.org/D80295
Added:
Modified:
clang/lib/Sema/SemaDecl.cpp
clang/test/OpenMP/for_loop_messages.cpp
clang/test/SemaCXX/anonymous-struct.cpp
clang/test/SemaCXX/blocks.cpp
Removed:
################################################################################
diff --git a/clang/lib/Sema/SemaDecl.cpp b/clang/lib/Sema/SemaDecl.cpp
index 74a4fd8a06de..6fe48c860864 100644
--- a/clang/lib/Sema/SemaDecl.cpp
+++ b/clang/lib/Sema/SemaDecl.cpp
@@ -6885,18 +6885,34 @@ NamedDecl *Sema::ActOnVariableDeclarator(
if (SC == SC_Static && CurContext->isRecord()) {
if (const CXXRecordDecl *RD = dyn_cast<CXXRecordDecl>(DC)) {
- // C++ [class.static.data]p2:
- // A static data member shall not be a direct member of an unnamed
- // or local class
- // FIXME: or of a (possibly indirectly) nested class thereof.
- if (RD->isLocalClass()) {
+ // Walk up the enclosing DeclContexts to check for any that are
+ // incompatible with static data members.
+ const DeclContext *FunctionOrMethod = nullptr;
+ const CXXRecordDecl *AnonStruct = nullptr;
+ for (DeclContext *Ctxt = DC; Ctxt; Ctxt = Ctxt->getParent()) {
+ if (Ctxt->isFunctionOrMethod()) {
+ FunctionOrMethod = Ctxt;
+ break;
+ }
+ const CXXRecordDecl *ParentDecl = dyn_cast<CXXRecordDecl>(Ctxt);
+ if (ParentDecl && !ParentDecl->getDeclName()) {
+ AnonStruct = ParentDecl;
+ break;
+ }
+ }
+ if (FunctionOrMethod) {
+ // C++ [class.static.data]p5: A local class shall not have static data
+ // members.
Diag(D.getIdentifierLoc(),
diag::err_static_data_member_not_allowed_in_local_class)
<< Name << RD->getDeclName() << RD->getTagKind();
- } else if (!RD->getDeclName()) {
+ } else if (AnonStruct) {
+ // C++ [class.static.data]p4: Unnamed classes and classes contained
+ // directly or indirectly within unnamed classes shall not contain
+ // static data members.
Diag(D.getIdentifierLoc(),
diag::err_static_data_member_not_allowed_in_anon_struct)
- << Name << RD->getTagKind();
+ << Name << AnonStruct->getTagKind();
Invalid = true;
} else if (RD->isUnion()) {
// C++98 [class.union]p1: If a union contains a static data member,
diff --git a/clang/test/OpenMP/for_loop_messages.cpp b/clang/test/OpenMP/for_loop_messages.cpp
index 73c69ede6d12..087db755273a 100644
--- a/clang/test/OpenMP/for_loop_messages.cpp
+++ b/clang/test/OpenMP/for_loop_messages.cpp
@@ -831,3 +831,13 @@ void test_nowait() {
for (int i = 0; i < 16; ++i)
;
}
+
+void test_static_data_member() {
+#pragma omp parallel
+#pragma omp for
+ for (int i = 0; i < 16; ++i) {
+ class X {
+ static int x; // expected-error {{static data member 'x' not allowed in local class 'X'}}
+ };
+ }
+}
diff --git a/clang/test/SemaCXX/anonymous-struct.cpp b/clang/test/SemaCXX/anonymous-struct.cpp
index 10f6711dd340..333b8f724f4e 100644
--- a/clang/test/SemaCXX/anonymous-struct.cpp
+++ b/clang/test/SemaCXX/anonymous-struct.cpp
@@ -153,3 +153,21 @@ typedef struct {
const Empty E;
} C;
} // namespace ImplicitDecls
+
+struct {
+ static int x; // expected-error {{static data member 'x' not allowed in anonymous struct}}
+} static_member_1;
+
+class {
+ struct A {
+ static int x; // expected-error {{static data member 'x' not allowed in anonymous class}}
+ } x;
+} static_member_2;
+
+union {
+ struct A {
+ struct B {
+ static int x; // expected-error {{static data member 'x' not allowed in anonymous union}}
+ } x;
+ } x;
+} static_member_3;
diff --git a/clang/test/SemaCXX/blocks.cpp b/clang/test/SemaCXX/blocks.cpp
index aacf63cfab42..5d0aa2af7360 100644
--- a/clang/test/SemaCXX/blocks.cpp
+++ b/clang/test/SemaCXX/blocks.cpp
@@ -153,3 +153,16 @@ void f() {
auto some_block = ^{ (void)s; };
}
}
+
+void static_data_member() {
+ auto block = ^{
+ class X {
+ static int x; // expected-error {{static data member 'x' not allowed in local class 'X'}}
+ };
+ class Y {
+ struct Z {
+ static int z; // expected-error {{static data member 'z' not allowed in local struct 'Z'}}
+ };
+ };
+ };
+}
More information about the cfe-commits
mailing list