Skip to content

Commit

Permalink
[Sema] Diagnose more cases of static data members in local or unnamed…
Browse files Browse the repository at this point in the history
… 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
  • Loading branch information
john-brawn-arm committed May 26, 2020
1 parent 4d20e31 commit 6c906f7
Show file tree
Hide file tree
Showing 4 changed files with 64 additions and 7 deletions.
30 changes: 23 additions & 7 deletions clang/lib/Sema/SemaDecl.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -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,
Expand Down
10 changes: 10 additions & 0 deletions clang/test/OpenMP/for_loop_messages.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -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'}}
};
}
}
18 changes: 18 additions & 0 deletions clang/test/SemaCXX/anonymous-struct.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -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;
13 changes: 13 additions & 0 deletions clang/test/SemaCXX/blocks.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -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'}}
};
};
};
}

0 comments on commit 6c906f7

Please sign in to comment.