mirror of
https://github.com/capstone-engine/llvm-capstone.git
synced 2025-01-24 18:20:38 +00:00
[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
This commit is contained in:
parent
4d20e31f73
commit
6c906f7785
@ -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,
|
||||
|
@ -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'}}
|
||||
};
|
||||
}
|
||||
}
|
||||
|
@ -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;
|
||||
|
@ -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'}}
|
||||
};
|
||||
};
|
||||
};
|
||||
}
|
||||
|
Loading…
x
Reference in New Issue
Block a user