[clang] [clang] Restrict -Wnrvo to C++ code only. (PR #157059)
via cfe-commits
cfe-commits at lists.llvm.org
Fri Sep 5 02:18:45 PDT 2025
https://github.com/javiermunozkirschberg created https://github.com/llvm/llvm-project/pull/157059
Pull request #139973 created a very useful warning for detecting not return value optimizations. This can be seen as useful for two different audiences: _compiler developers_, who may see cases where no optimization is done, and _compiler users_ who can detect when there is no copy elision. In C++ this makes a lot of sense - the effect of not having copy elision is not only performance impact, but it can be seen in the execution of code (like the copy constructor or the assignment operator) that may not be executed otherwise.
However, the value for compiler users with regards to plain C code is more difficult for me to assert. Specifically, in C it's only about a missing optimization - and can pollute your code with this new warning. GCC, for instance, restrict -Wnrvo to C++.
The following pull request will restrict Wnrvo so it does not warn on plain C-code.
Change-Id: Iaadd60f072c176972a5210c1fd1a6f0499d3ff39
>From b81368b2a7dcc8e998e9ffae8109c622faee6bcc Mon Sep 17 00:00:00 2001
From: =?UTF-8?q?Javier=20Mu=C3=B1oz=20Kirschberg?=
<javier.munoz.kirschberg at ericsson.com>
Date: Fri, 5 Sep 2025 10:57:24 +0200
Subject: [PATCH] [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;
+}
More information about the cfe-commits
mailing list