https://github.com/zebullax updated 
https://github.com/llvm/llvm-project/pull/148090

>From 11909560ed6cb4e56192fbbfe4d8b1cdf58e1cb1 Mon Sep 17 00:00:00 2001
From: zebullax <zebul...@gmail.com>
Date: Fri, 11 Jul 2025 09:12:44 +0900
Subject: [PATCH 1/6] Build argument string for clang::warn_unused_result

Preserve the argument-clause for `warn-unused-result` when under clang:: scope.
We are not touching gnu:: scope for now as it's an error for GCC to have that 
string. Personally I think it would be ok to relax it here too as we are not 
introducing breakage to currently passing code, but feedback is to go slowly 
about it.
---
 clang/lib/Sema/SemaDeclAttr.cpp | 3 ++-
 1 file changed, 2 insertions(+), 1 deletion(-)

diff --git a/clang/lib/Sema/SemaDeclAttr.cpp b/clang/lib/Sema/SemaDeclAttr.cpp
index 7ebb53318702c..221197b3849e0 100644
--- a/clang/lib/Sema/SemaDeclAttr.cpp
+++ b/clang/lib/Sema/SemaDeclAttr.cpp
@@ -2902,7 +2902,8 @@ static void handleWarnUnusedResult(Sema &S, Decl *D, 
const ParsedAttr &AL) {
     }
 
   StringRef Str;
-  if (AL.isStandardAttributeSyntax() && !AL.getScopeName()) {
+  if (AL.isStandardAttributeSyntax()
+      && (!AL.getScopeName() || AL.isClangScope())) {
     // The standard attribute cannot be applied to variable declarations such
     // as a function pointer.
     if (isa<VarDecl>(D))

>From 7d1b223472514fa09574fb4ee49518f912353494 Mon Sep 17 00:00:00 2001
From: zebullax <zebul...@gmail.com>
Date: Fri, 11 Jul 2025 15:51:22 +0900
Subject: [PATCH 2/6] Add unit test to check reason string on clang scope

Signed-off-by: zebullax <zebul...@gmail.com>

Fix scope in UT

Signed-off-by: zebullax <zebul...@gmail.com>

Fix UT

Signed-off-by: zebullax <zebul...@gmail.com>

Remove warn on attaching clang scoped to var decl

Signed-off-by: zebullax <zebul...@gmail.com>

Fix EOL

Signed-off-by: zebullax <zebul...@gmail.com>
---
 clang/lib/Sema/SemaDeclAttr.cpp           |  2 +-
 clang/test/SemaCXX/warn-unused-result.cpp | 18 ++++++++++++++++++
 2 files changed, 19 insertions(+), 1 deletion(-)

diff --git a/clang/lib/Sema/SemaDeclAttr.cpp b/clang/lib/Sema/SemaDeclAttr.cpp
index 221197b3849e0..c9822369e8652 100644
--- a/clang/lib/Sema/SemaDeclAttr.cpp
+++ b/clang/lib/Sema/SemaDeclAttr.cpp
@@ -2906,7 +2906,7 @@ static void handleWarnUnusedResult(Sema &S, Decl *D, 
const ParsedAttr &AL) {
       && (!AL.getScopeName() || AL.isClangScope())) {
     // The standard attribute cannot be applied to variable declarations such
     // as a function pointer.
-    if (isa<VarDecl>(D))
+    if (!AL.isClangScope() && isa<VarDecl>(D))
       S.Diag(AL.getLoc(), diag::warn_attribute_wrong_decl_type)
           << AL << AL.isRegularKeywordAttribute()
           << ExpectedFunctionOrClassOrEnum;
diff --git a/clang/test/SemaCXX/warn-unused-result.cpp 
b/clang/test/SemaCXX/warn-unused-result.cpp
index fa7540c116e67..fe7d5ea79e9a7 100644
--- a/clang/test/SemaCXX/warn-unused-result.cpp
+++ b/clang/test/SemaCXX/warn-unused-result.cpp
@@ -364,3 +364,21 @@ void id_print_name() {
     ((int(*)())f)();
 }
 } // namespace GH117975
+
+namespace BuildStringOnClangScope {
+
+[[clang::warn_unused_result("Discarded result")]]
+bool makeClangTrue() { return true; }
+
+[[gnu::warn_unused_result("Discarded result")]]
+bool makeGccTrue() { return true; }
+
+void doClangThings() {
+  makeClangTrue(); // expected-warning {{ignoring return value of function 
declared with 'clang::warn_unused_result' attribute: Discarded result}}
+}
+
+void doGccThings() {
+  makeGccTrue(); // expected-warning {{ignoring return value of function 
declared with 'gnu::warn_unused_result' attribute}}
+}
+
+}

>From b110d634b6b64b8d96d63e0a43e3283809e46af0 Mon Sep 17 00:00:00 2001
From: zebullax <zebul...@gmail.com>
Date: Fri, 11 Jul 2025 18:02:53 +0900
Subject: [PATCH 3/6] Update release notes

Signed-off-by: zebullax <zebul...@gmail.com>
---
 clang/docs/ReleaseNotes.rst | 3 +++
 1 file changed, 3 insertions(+)

diff --git a/clang/docs/ReleaseNotes.rst b/clang/docs/ReleaseNotes.rst
index 9e16613826a77..7797b14e49f55 100644
--- a/clang/docs/ReleaseNotes.rst
+++ b/clang/docs/ReleaseNotes.rst
@@ -464,6 +464,9 @@ related warnings within the method body.
 - Clang now disallows the use of attributes applied before an
   ``extern template`` declaration (#GH79893).
 
+- Clang will print the "reason" string argument passed on to
+  `[[clang::warn_unused_result("reason")]]` as part of the warning diagnostic
+
 Improvements to Clang's diagnostics
 -----------------------------------
 

>From 0cbcc955ccc9d9be8e15cbc094a8eaa3235e0bfd Mon Sep 17 00:00:00 2001
From: zebullax <zebul...@gmail.com>
Date: Fri, 11 Jul 2025 20:36:53 +0900
Subject: [PATCH 4/6] Update clang/docs/ReleaseNotes.rst

Co-authored-by: Aaron Ballman <aa...@aaronballman.com>
---
 clang/docs/ReleaseNotes.rst | 2 +-
 1 file changed, 1 insertion(+), 1 deletion(-)

diff --git a/clang/docs/ReleaseNotes.rst b/clang/docs/ReleaseNotes.rst
index 7797b14e49f55..edad96c15ca9f 100644
--- a/clang/docs/ReleaseNotes.rst
+++ b/clang/docs/ReleaseNotes.rst
@@ -465,7 +465,7 @@ related warnings within the method body.
   ``extern template`` declaration (#GH79893).
 
 - Clang will print the "reason" string argument passed on to
-  `[[clang::warn_unused_result("reason")]]` as part of the warning diagnostic
+  ``[[clang::warn_unused_result("reason")]]`` as part of the warning 
diagnostic.
 
 Improvements to Clang's diagnostics
 -----------------------------------

>From 04c46dab4a249d018632b0bba04c1224c0b93096 Mon Sep 17 00:00:00 2001
From: zebullax <zebul...@gmail.com>
Date: Fri, 11 Jul 2025 20:37:24 +0900
Subject: [PATCH 5/6] Fix format

Signed-off-by: zebullax <zebul...@gmail.com>
---
 clang/lib/Sema/SemaDeclAttr.cpp | 4 ++--
 1 file changed, 2 insertions(+), 2 deletions(-)

diff --git a/clang/lib/Sema/SemaDeclAttr.cpp b/clang/lib/Sema/SemaDeclAttr.cpp
index c9822369e8652..f4b0f9fa74b77 100644
--- a/clang/lib/Sema/SemaDeclAttr.cpp
+++ b/clang/lib/Sema/SemaDeclAttr.cpp
@@ -2902,8 +2902,8 @@ static void handleWarnUnusedResult(Sema &S, Decl *D, 
const ParsedAttr &AL) {
     }
 
   StringRef Str;
-  if (AL.isStandardAttributeSyntax()
-      && (!AL.getScopeName() || AL.isClangScope())) {
+  if (AL.isStandardAttributeSyntax() &&
+      (!AL.getScopeName() || AL.isClangScope())) {
     // The standard attribute cannot be applied to variable declarations such
     // as a function pointer.
     if (!AL.isClangScope() && isa<VarDecl>(D))

>From e70bba4cdf1093a7c80463833d0dbf99916aa8b8 Mon Sep 17 00:00:00 2001
From: zebullax <zebul...@gmail.com>
Date: Fri, 11 Jul 2025 22:14:21 +0900
Subject: [PATCH 6/6] Extract the clang scope branch to bypass cxx17,20
 standard verification

Signed-off-by: zebullax <zebul...@gmail.com>
---
 clang/lib/Sema/SemaDeclAttr.cpp | 60 +++++++++++++++++++--------------
 1 file changed, 34 insertions(+), 26 deletions(-)

diff --git a/clang/lib/Sema/SemaDeclAttr.cpp b/clang/lib/Sema/SemaDeclAttr.cpp
index f4b0f9fa74b77..f28ecc9738fc6 100644
--- a/clang/lib/Sema/SemaDeclAttr.cpp
+++ b/clang/lib/Sema/SemaDeclAttr.cpp
@@ -2902,33 +2902,41 @@ static void handleWarnUnusedResult(Sema &S, Decl *D, 
const ParsedAttr &AL) {
     }
 
   StringRef Str;
-  if (AL.isStandardAttributeSyntax() &&
-      (!AL.getScopeName() || AL.isClangScope())) {
-    // The standard attribute cannot be applied to variable declarations such
-    // as a function pointer.
-    if (!AL.isClangScope() && isa<VarDecl>(D))
-      S.Diag(AL.getLoc(), diag::warn_attribute_wrong_decl_type)
-          << AL << AL.isRegularKeywordAttribute()
-          << ExpectedFunctionOrClassOrEnum;
-
-    // If this is spelled as the standard C++17 attribute, but not in C++17,
-    // warn about using it as an extension. If there are attribute arguments,
-    // then claim it's a C++20 extension instead.
-    // FIXME: If WG14 does not seem likely to adopt the same feature, add an
-    // extension warning for C23 mode.
-    const LangOptions &LO = S.getLangOpts();
-    if (AL.getNumArgs() == 1) {
-      if (LO.CPlusPlus && !LO.CPlusPlus20)
-        S.Diag(AL.getLoc(), diag::ext_cxx20_attr) << AL;
-
-      // Since this is spelled [[nodiscard]], get the optional string
-      // literal. If in C++ mode, but not in C++20 mode, diagnose as an
-      // extension.
-      // FIXME: C23 should support this feature as well, even as an extension.
-      if (!S.checkStringLiteralArgumentAttr(AL, 0, Str, nullptr))
+  if (AL.isStandardAttributeSyntax()) {
+    // If this is spelled [[clang::warn_unused_result]] we look for an optional
+    // string literal. This is not gated behind any specific version of the
+    // standard.
+    if (AL.isClangScope()) {
+      if (AL.getNumArgs() == 1 &&
+          !S.checkStringLiteralArgumentAttr(AL, 0, Str, nullptr))
         return;
-    } else if (LO.CPlusPlus && !LO.CPlusPlus17)
-      S.Diag(AL.getLoc(), diag::ext_cxx17_attr) << AL;
+    } else if (!AL.getScopeName()) {
+      // The standard attribute cannot be applied to variable declarations such
+      // as a function pointer.
+      if (isa<VarDecl>(D))
+        S.Diag(AL.getLoc(), diag::warn_attribute_wrong_decl_type)
+            << AL << AL.isRegularKeywordAttribute()
+            << ExpectedFunctionOrClassOrEnum;
+
+      // If this is spelled as the standard C++17 attribute, but not in C++17,
+      // warn about using it as an extension. If there are attribute arguments,
+      // then claim it's a C++20 extension instead.
+      // FIXME: If WG14 does not seem likely to adopt the same feature, add an
+      // extension warning for C23 mode.
+      const LangOptions &LO = S.getLangOpts();
+      if (AL.getNumArgs() == 1) {
+        if (LO.CPlusPlus && !LO.CPlusPlus20)
+          S.Diag(AL.getLoc(), diag::ext_cxx20_attr) << AL;
+
+        // Since this is spelled [[nodiscard]], get the optional string
+        // literal. If in C++ mode, but not in C++20 mode, diagnose as an
+        // extension.
+        // FIXME: C23 should support this feature as well, even as an 
extension.
+        if (!S.checkStringLiteralArgumentAttr(AL, 0, Str, nullptr))
+          return;
+      } else if (LO.CPlusPlus && !LO.CPlusPlus17)
+        S.Diag(AL.getLoc(), diag::ext_cxx17_attr) << AL;
+    }
   }
 
   if ((!AL.isGNUAttribute() &&

_______________________________________________
cfe-commits mailing list
cfe-commits@lists.llvm.org
https://lists.llvm.org/cgi-bin/mailman/listinfo/cfe-commits

Reply via email to