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/3] 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 8b98b9e5cf0d88ffe7f2a9b8668e7ef0359c7c87 Mon Sep 17 00:00:00 2001 From: zebullax <zebul...@gmail.com> Date: Fri, 11 Jul 2025 15:51:22 +0900 Subject: [PATCH 2/3] 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> --- clang/test/SemaCXX/warn-unused-result.cpp | 20 +++++++++++++++++++- 1 file changed, 19 insertions(+), 1 deletion(-) diff --git a/clang/test/SemaCXX/warn-unused-result.cpp b/clang/test/SemaCXX/warn-unused-result.cpp index fa7540c116e67..440306a235f08 100644 --- a/clang/test/SemaCXX/warn-unused-result.cpp +++ b/clang/test/SemaCXX/warn-unused-result.cpp @@ -249,7 +249,7 @@ void g() { namespace PR45520 { [[nodiscard]] bool (*f)(); // expected-warning {{'nodiscard' attribute only applies to functions, classes, or enumerations}} -[[clang::warn_unused_result]] bool (*g)(); +[[clang::warn_unused_result]] bool (*g)(); // expected-warning {{'clang::warn_unused_result' attribute only applies to functions, classes, or enumerations}} __attribute__((warn_unused_result)) bool (*h)(); void i([[nodiscard]] bool (*fp)()); // expected-warning {{'nodiscard' attribute only applies to functions, classes, or enumerations}} @@ -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}} +} + +} \ No newline at end of file >From 5aedb435e2aeb0173234756df2ffa279f366751a Mon Sep 17 00:00:00 2001 From: zebullax <zebul...@gmail.com> Date: Fri, 11 Jul 2025 17:36:00 +0900 Subject: [PATCH 3/3] Remove warn on attaching clang scoped to var decl Signed-off-by: zebullax <zebul...@gmail.com> --- clang/lib/Sema/SemaDeclAttr.cpp | 2 +- clang/test/SemaCXX/warn-unused-result.cpp | 2 +- 2 files changed, 2 insertions(+), 2 deletions(-) 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 440306a235f08..3e6851590aa5f 100644 --- a/clang/test/SemaCXX/warn-unused-result.cpp +++ b/clang/test/SemaCXX/warn-unused-result.cpp @@ -249,7 +249,7 @@ void g() { namespace PR45520 { [[nodiscard]] bool (*f)(); // expected-warning {{'nodiscard' attribute only applies to functions, classes, or enumerations}} -[[clang::warn_unused_result]] bool (*g)(); // expected-warning {{'clang::warn_unused_result' attribute only applies to functions, classes, or enumerations}} +[[clang::warn_unused_result]] bool (*g)(); __attribute__((warn_unused_result)) bool (*h)(); void i([[nodiscard]] bool (*fp)()); // expected-warning {{'nodiscard' attribute only applies to functions, classes, or enumerations}} _______________________________________________ cfe-commits mailing list cfe-commits@lists.llvm.org https://lists.llvm.org/cgi-bin/mailman/listinfo/cfe-commits