[clang] [clang] Build argument string for clang::warn_unused_result (PR #148090)
via cfe-commits
cfe-commits at lists.llvm.org
Thu Jul 10 23:51:41 PDT 2025
https://github.com/zebullax updated https://github.com/llvm/llvm-project/pull/148090
>From 11909560ed6cb4e56192fbbfe4d8b1cdf58e1cb1 Mon Sep 17 00:00:00 2001
From: zebullax <zebullax at gmail.com>
Date: Fri, 11 Jul 2025 09:12:44 +0900
Subject: [PATCH 1/2] 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 75e585703826f50f704be099f550346a06223cb5 Mon Sep 17 00:00:00 2001
From: zebullax <zebullax at gmail.com>
Date: Fri, 11 Jul 2025 15:51:22 +0900
Subject: [PATCH 2/2] Add unit test to check reason string on clang scope
Signed-off-by: zebullax <zebullax at gmail.com>
---
clang/test/SemaCXX/warn-unused-result.cpp | 18 ++++++++++++++++++
1 file changed, 18 insertions(+)
diff --git a/clang/test/SemaCXX/warn-unused-result.cpp b/clang/test/SemaCXX/warn-unused-result.cpp
index fa7540c116e67..1b9370765b377 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 'warn_unused_result' attribute: Discarded result}}
+}
+
+void doGccThings() {
+ makeGccTrue(); // expected-warning {{ignoring return value of function declared with 'warn_unused_result' attribute}}
+}
+
+}
\ No newline at end of file
More information about the cfe-commits
mailing list