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 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/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> 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/3] 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 ----------------------------------- _______________________________________________ cfe-commits mailing list cfe-commits@lists.llvm.org https://lists.llvm.org/cgi-bin/mailman/listinfo/cfe-commits