[clang] Resolving issue #119101 (PR #119143)

Rounaq Khan via cfe-commits cfe-commits at lists.llvm.org
Sun Dec 8 11:46:20 PST 2024


https://github.com/rkmaxhero created https://github.com/llvm/llvm-project/pull/119143

CLANG parser previously allowed for invalid C/C++ auto classes declaration due to a lack of logic addressing this case. Logic comparing with a valid case was added to the ParseDeclarationOrFunctionDefinition() function to account for this. Test cases where added to address possible scenarios of auto class declaration. the parser diagnostic file will now detect invalid auto class definitions and output appropriately. 

>From ab8c853a3bfda20beb82bc2d27859b6dcf3cdc7e Mon Sep 17 00:00:00 2001
From: Rounaq Khan <khanru at umich.edu>
Date: Sun, 8 Dec 2024 13:49:06 -0500
Subject: [PATCH 1/4] add check logic for auto class type

---
 clang/lib/Parse/Parser.cpp | 12 ++++++++++++
 1 file changed, 12 insertions(+)

diff --git a/clang/lib/Parse/Parser.cpp b/clang/lib/Parse/Parser.cpp
index 04c2f1d380bc48..2145f78f4b2749 100644
--- a/clang/lib/Parse/Parser.cpp
+++ b/clang/lib/Parse/Parser.cpp
@@ -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 {

>From e9d9b5aa24a775e7cd99fc7c1391859d8ff61dd8 Mon Sep 17 00:00:00 2001
From: Rounaq Khan <khanru at umich.edu>
Date: Sun, 8 Dec 2024 13:50:40 -0500
Subject: [PATCH 2/4] add diagnostic message for parser issue auto class
 declaration for CLANG

---
 clang/include/clang/Basic/DiagnosticParseKinds.td | 2 ++
 1 file changed, 2 insertions(+)

diff --git a/clang/include/clang/Basic/DiagnosticParseKinds.td b/clang/include/clang/Basic/DiagnosticParseKinds.td
index 0da509280068ad..99d698f57982fc 100644
--- a/clang/include/clang/Basic/DiagnosticParseKinds.td
+++ b/clang/include/clang/Basic/DiagnosticParseKinds.td
@@ -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">;

>From 8680d99633bbf72dddfc3b9d4718a4d7e3fae91e Mon Sep 17 00:00:00 2001
From: Rounaq Khan <45477562+rkmaxhero at users.noreply.github.com>
Date: Sun, 8 Dec 2024 14:05:25 -0500
Subject: [PATCH 3/4] Update DiagnosticParseKinds.td in accordance to error
 related to auto class declarations

---
 clang/include/clang/Basic/DiagnosticParseKinds.td | 2 ++
 1 file changed, 2 insertions(+)

diff --git a/clang/include/clang/Basic/DiagnosticParseKinds.td b/clang/include/clang/Basic/DiagnosticParseKinds.td
index 0da509280068ad..99d698f57982fc 100644
--- a/clang/include/clang/Basic/DiagnosticParseKinds.td
+++ b/clang/include/clang/Basic/DiagnosticParseKinds.td
@@ -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">;

>From 871187e20647c54f7265b9b71fdfcf4b8c5daecc Mon Sep 17 00:00:00 2001
From: Rounaq Khan <khanru at umich.edu>
Date: Sun, 8 Dec 2024 14:38:04 -0500
Subject: [PATCH 4/4] add test cases for invalid auto storage declaration

---
 clang/test/SemaCXX/invalid-storage-class.cpp | 7 +++++++
 1 file changed, 7 insertions(+)
 create mode 100644 clang/test/SemaCXX/invalid-storage-class.cpp

diff --git a/clang/test/SemaCXX/invalid-storage-class.cpp b/clang/test/SemaCXX/invalid-storage-class.cpp
new file mode 100644
index 00000000000000..ce0dee3711ac03
--- /dev/null
+++ b/clang/test/SemaCXX/invalid-storage-class.cpp
@@ -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}}
\ No newline at end of file



More information about the cfe-commits mailing list