[clang] [Clang] handle both gnu and cpp11 attributes to ensure correct parsing inside extern block (PR #102864)

Oleksandr T. via cfe-commits cfe-commits at lists.llvm.org
Wed Aug 14 12:18:35 PDT 2024


https://github.com/a-tarasyuk updated https://github.com/llvm/llvm-project/pull/102864

>From b0e53b1c8a687165fa28bd21200f83bf1b1a9234 Mon Sep 17 00:00:00 2001
From: Oleksandr T <oleksandr.tarasiuk at outlook.com>
Date: Mon, 12 Aug 2024 13:56:30 +0300
Subject: [PATCH 1/2] [Clang] handle both gnu and cpp11 attributes to ensure
 correct parsing inside extern block

---
 clang/docs/ReleaseNotes.rst      | 1 +
 clang/lib/Parse/ParseDeclCXX.cpp | 5 ++++-
 clang/test/Parser/attr-order.cpp | 8 ++++++++
 3 files changed, 13 insertions(+), 1 deletion(-)

diff --git a/clang/docs/ReleaseNotes.rst b/clang/docs/ReleaseNotes.rst
index 6796a619ba97f8..00a663fdd23454 100644
--- a/clang/docs/ReleaseNotes.rst
+++ b/clang/docs/ReleaseNotes.rst
@@ -217,6 +217,7 @@ Bug Fixes to C++ Support
 - Clang now preserves the unexpanded flag in a lambda transform used for pack expansion. (#GH56852), (#GH85667),
   (#GH99877).
 - Fixed a bug when diagnosing ambiguous explicit specializations of constrained member functions.
+- Clang now properly handles the order of attributes in `extern` blocks. (#GH101990).
 
 Bug Fixes to AST Handling
 ^^^^^^^^^^^^^^^^^^^^^^^^^
diff --git a/clang/lib/Parse/ParseDeclCXX.cpp b/clang/lib/Parse/ParseDeclCXX.cpp
index aac89d910bbc83..d45a738fe4c596 100644
--- a/clang/lib/Parse/ParseDeclCXX.cpp
+++ b/clang/lib/Parse/ParseDeclCXX.cpp
@@ -425,7 +425,10 @@ Decl *Parser::ParseLinkage(ParsingDeclSpec &DS, DeclaratorContext Context) {
       [[fallthrough]];
     default:
       ParsedAttributes DeclAttrs(AttrFactory);
-      MaybeParseCXX11Attributes(DeclAttrs);
+      ParsedAttributes DeclSpecAttrs(AttrFactory);
+      while (MaybeParseCXX11Attributes(DeclAttrs) ||
+             MaybeParseGNUAttributes(DeclSpecAttrs))
+        ;
       ParseExternalDeclaration(DeclAttrs, DeclSpecAttrs);
       continue;
     }
diff --git a/clang/test/Parser/attr-order.cpp b/clang/test/Parser/attr-order.cpp
index 10bad38cac6447..369941ab24ee69 100644
--- a/clang/test/Parser/attr-order.cpp
+++ b/clang/test/Parser/attr-order.cpp
@@ -31,3 +31,11 @@ template <int a>
 
 template <int a>
 [[noreturn]] __declspec(dllexport) __attribute__((cdecl)) void k(); // ok
+
+extern "C" {
+  __attribute__ ((__warn_unused_result__)) [[__maybe_unused__]] int l(int); // ok
+  [[__maybe_unused__]] __attribute__ ((__warn_unused_result__)) int m(int); // ok
+}
+
+extern "C" __attribute__ ((__warn_unused_result__)) [[__maybe_unused__]] int n(int); // ok
+extern "C" [[__maybe_unused__]] __attribute__ ((__warn_unused_result__)) int o(int); // ok

>From 7adea6685eb9409866088b20b64201c376871b2a Mon Sep 17 00:00:00 2001
From: Oleksandr T <oleksandr.tarasiuk at outlook.com>
Date: Tue, 13 Aug 2024 22:20:42 +0300
Subject: [PATCH 2/2] add more tests

---
 clang/test/Parser/attr-order.cpp | 9 +++++++--
 1 file changed, 7 insertions(+), 2 deletions(-)

diff --git a/clang/test/Parser/attr-order.cpp b/clang/test/Parser/attr-order.cpp
index 369941ab24ee69..21e30e5b6f9df1 100644
--- a/clang/test/Parser/attr-order.cpp
+++ b/clang/test/Parser/attr-order.cpp
@@ -37,5 +37,10 @@ extern "C" {
   [[__maybe_unused__]] __attribute__ ((__warn_unused_result__)) int m(int); // ok
 }
 
-extern "C" __attribute__ ((__warn_unused_result__)) [[__maybe_unused__]] int n(int); // ok
-extern "C" [[__maybe_unused__]] __attribute__ ((__warn_unused_result__)) int o(int); // ok
+extern "C" {
+  __attribute__ ((__warn_unused_result__)) [[__maybe_unused__]] int n (int); // ok
+  __attribute__ ((__warn_unused_result__)) [[__maybe_unused__]] static int o (int x) { return x; }; // ok
+}
+
+extern "C" __attribute__ ((__warn_unused_result__)) [[__maybe_unused__]] int p(int); // ok
+extern "C" [[__maybe_unused__]] __attribute__ ((__warn_unused_result__)) int q(int); // ok



More information about the cfe-commits mailing list