Skip to content

Resolving issue #119101 #119143

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Open
wants to merge 5 commits into
base: main
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 2 additions & 0 deletions clang/include/clang/Basic/DiagnosticParseKinds.td
Original file line number Diff line number Diff line change
Expand Up @@ -449,6 +449,8 @@ def err_typename_invalid_constexpr : Error<
"to be specified">;
def err_typename_identifiers_only : Error<
"typename is allowed for identifiers only">;
def err_storage_class_before_class_decl : Error<
"'%0' is not allowed before a class declaration">;

def err_friend_invalid_in_context : Error<
"'friend' used outside of class">;
Expand Down
12 changes: 12 additions & 0 deletions clang/lib/Parse/Parser.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -1254,6 +1254,18 @@ Parser::DeclGroupPtrTy Parser::ParseDeclarationOrFunctionDefinition(
Actions.getASTContext().getSourceManager());
});

if (DS->getStorageClassSpec() != DeclSpec::SCS_unspecified) {
// Check if the next token starts a class/struct/union/enum declaration
if (Tok.isOneOf(tok::kw_class, tok::kw_struct, tok::kw_union, tok::kw_enum)) {
// Emit an error: storage class specifiers are not allowed before class declarations
Diag(DS->getStorageClassSpecLoc(), diag::err_storage_class_before_class_decl)
<< DeclSpec::getSpecifierName(DS->getStorageClassSpec());

// Optionally, consume the storage class specifier to continue parsing
DS->ClearStorageClassSpecs();
}
}

if (DS) {
return ParseDeclOrFunctionDefInternal(Attrs, DeclSpecAttrs, *DS, AS);
} else {
Expand Down
7 changes: 7 additions & 0 deletions clang/test/SemaCXX/invalid-storage-class.cpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
// RUN: %clang_cc1 -fsyntax-only -verify %s

auto class X1 {}; // expected-error {{'auto' is not allowed before a class declaration}}

static struct X2 {}; // expected-error {{'static' is not allowed before a class declaration}}

register union X3 {}; // expected-error {{'register' is not allowed before a class declaration}}
Loading