https://github.com/javiermunozkirschberg updated https://github.com/llvm/llvm-project/pull/157059
From b81368b2a7dcc8e998e9ffae8109c622faee6bcc Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Javier=20Mu=C3=B1oz=20Kirschberg?= <[email protected]> Date: Fri, 5 Sep 2025 10:57:24 +0200 Subject: [PATCH 1/2] [clang] Restrict -Wnrvo to C++ code only. Change-Id: Iaadd60f072c176972a5210c1fd1a6f0499d3ff39 --- clang/docs/ReleaseNotes.rst | 1 + clang/lib/Sema/SemaDecl.cpp | 2 ++ clang/test/SemaCXX/no-warn-nrvo-on-c.c | 41 ++++++++++++++++++++++++++ 3 files changed, 44 insertions(+) create mode 100644 clang/test/SemaCXX/no-warn-nrvo-on-c.c diff --git a/clang/docs/ReleaseNotes.rst b/clang/docs/ReleaseNotes.rst index 780e8a31cae6d..1a4d1b9e26d4d 100644 --- a/clang/docs/ReleaseNotes.rst +++ b/clang/docs/ReleaseNotes.rst @@ -223,6 +223,7 @@ Deprecated Compiler Flags Modified Compiler Flags ----------------------- - The `-gkey-instructions` compiler flag is now enabled by default when DWARF is emitted for plain C/C++ and optimizations are enabled. (#GH149509) +- The `-Wnrvo` compiler flag will not apply for C language. Removed Compiler Flags ------------------------- diff --git a/clang/lib/Sema/SemaDecl.cpp b/clang/lib/Sema/SemaDecl.cpp index 12bedae05f6f3..0c96e9dd16b07 100644 --- a/clang/lib/Sema/SemaDecl.cpp +++ b/clang/lib/Sema/SemaDecl.cpp @@ -16169,6 +16169,8 @@ void Sema::applyFunctionAttributesBeforeParsingBody(Decl *FD) { } void Sema::computeNRVO(Stmt *Body, FunctionScopeInfo *Scope) { + if (!getLangOpts().CPlusPlus) + return; ReturnStmt **Returns = Scope->Returns.data(); for (unsigned I = 0, E = Scope->Returns.size(); I != E; ++I) { diff --git a/clang/test/SemaCXX/no-warn-nrvo-on-c.c b/clang/test/SemaCXX/no-warn-nrvo-on-c.c new file mode 100644 index 0000000000000..a8173e51faceb --- /dev/null +++ b/clang/test/SemaCXX/no-warn-nrvo-on-c.c @@ -0,0 +1,41 @@ +// RUN: %clang -std=c23 -Wnrvo -Xclang -verify %s +// expected-no-diagnostics + +#include <stdlib.h> + +#define SIZE 20 + +typedef struct String_s { + char* buf; + size_t len; +} String; + + +void clean(String* s) { + free(s->buf); +} + +String randomString() { + String s = {}; + + s.buf = malloc(SIZE); + s.len = SIZE; + + if (!s.buf) { + goto fail; + } + + return s; + +fail: + clean(&s); + return (String){}; +} + +int main(int argc, char** argv) +{ + String s= randomString(); + clean(&s); + + return 0; +} From 47565bea9591a5dd61a5c70463a7341a58c8310f Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Javier=20Mu=C3=B1oz=20Kirschberg?= <[email protected]> Date: Mon, 8 Sep 2025 23:30:59 +0200 Subject: [PATCH 2/2] Code review adjustments Change-Id: Iae9d550fdb7adc0f7332c28dbb817f1a6d585d78 --- clang/docs/ReleaseNotes.rst | 3 ++- clang/lib/Sema/SemaDecl.cpp | 8 ++++---- 2 files changed, 6 insertions(+), 5 deletions(-) diff --git a/clang/docs/ReleaseNotes.rst b/clang/docs/ReleaseNotes.rst index 1a4d1b9e26d4d..cf7224ee6f46b 100644 --- a/clang/docs/ReleaseNotes.rst +++ b/clang/docs/ReleaseNotes.rst @@ -223,7 +223,6 @@ Deprecated Compiler Flags Modified Compiler Flags ----------------------- - The `-gkey-instructions` compiler flag is now enabled by default when DWARF is emitted for plain C/C++ and optimizations are enabled. (#GH149509) -- The `-Wnrvo` compiler flag will not apply for C language. Removed Compiler Flags ------------------------- @@ -280,6 +279,8 @@ Improvements to Clang's diagnostics ``ACQUIRED_AFTER(...)`` have been moved to the stable feature set and no longer require ``-Wthread-safety-beta`` to be used. +- The `-Wnrvo` compiler flag is now ignored in C mode. + Improvements to Clang's time-trace ---------------------------------- diff --git a/clang/lib/Sema/SemaDecl.cpp b/clang/lib/Sema/SemaDecl.cpp index 0c96e9dd16b07..f89a2c36484cb 100644 --- a/clang/lib/Sema/SemaDecl.cpp +++ b/clang/lib/Sema/SemaDecl.cpp @@ -16169,15 +16169,15 @@ void Sema::applyFunctionAttributesBeforeParsingBody(Decl *FD) { } void Sema::computeNRVO(Stmt *Body, FunctionScopeInfo *Scope) { - if (!getLangOpts().CPlusPlus) - return; ReturnStmt **Returns = Scope->Returns.data(); for (unsigned I = 0, E = Scope->Returns.size(); I != E; ++I) { if (const VarDecl *NRVOCandidate = Returns[I]->getNRVOCandidate()) { if (!NRVOCandidate->isNRVOVariable()) { - Diag(Returns[I]->getRetValue()->getExprLoc(), - diag::warn_not_eliding_copy_on_return); + if (getLangOpts().CPlusPlus) { + Diag(Returns[I]->getRetValue()->getExprLoc(), + diag::warn_not_eliding_copy_on_return); + } Returns[I]->setNRVOCandidate(nullptr); } } _______________________________________________ cfe-commits mailing list [email protected] https://lists.llvm.org/cgi-bin/mailman/listinfo/cfe-commits
