Skip to content

Conversation

javiermunozkirschberg
Copy link

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

Change-Id: Iaadd60f072c176972a5210c1fd1a6f0499d3ff39
Copy link

github-actions bot commented Sep 5, 2025

Thank you for submitting a Pull Request (PR) to the LLVM Project!

This PR will be automatically labeled and the relevant teams will be notified.

If you wish to, you can add reviewers by using the "Reviewers" section on this page.

If this is not working for you, it is probably because you do not have write permissions for the repository. In which case you can instead tag reviewers by name in a comment by using @ followed by their GitHub username.

If you have received no comments on your PR for a week, you can request a review by "ping"ing the PR by adding a comment “Ping”. The common courtesy "ping" rate is once a week. Please remember that you are asking for valuable time from other developers.

If you have further questions, they may be answered by the LLVM GitHub User Guide.

You can also ask questions in a comment on this PR, on the LLVM Discord or on the forums.

@llvmbot llvmbot added clang Clang issues not falling into any other category clang:frontend Language frontend issues, e.g. anything involving "Sema" labels Sep 5, 2025
@llvmbot
Copy link
Member

llvmbot commented Sep 5, 2025

@llvm/pr-subscribers-clang

Author: None (javiermunozkirschberg)

Changes

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


Full diff: https://github.com/llvm/llvm-project/pull/157059.diff

3 Files Affected:

  • (modified) clang/docs/ReleaseNotes.rst (+1)
  • (modified) clang/lib/Sema/SemaDecl.cpp (+2)
  • (added) clang/test/SemaCXX/no-warn-nrvo-on-c.c (+41)
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;
+}

@javiermunozkirschberg
Copy link
Author

If I understood everything correctly, and please accept my apologies if I did not (it's my first contribution), I should copy here the maintainers. So thanks a lot in advance @jansvoboda11 for your review, and my apologies for anything I could have done wrong :)

Copy link
Member

@Sirraide Sirraide left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I think it makes sense not to issue the warning considering that C doesn’t have a concept of NRVO from what I know. That said, I don’t think the current approach is right because it just prevents NRVO in C entirely; we still want to perform NRVO, we just don’t want to warn if we can’t.

What I think we should do instead is the same thing that GCC does, i.e. reject the flag in the driver if we’re compiling C.

@@ -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.
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Suggested change
- The `-Wnrvo` compiler flag will not apply for C language.
- ``-Wnrvo`` is now ignored in C mode.

Maybe something like this? Also, I’d move this into the ‘Improvement to Clang’s Diagnostics’ section below.

(wrt the double backticks, yes, the release note above this one is also wrong)

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Actually, if we’re disallowing it, it should probably say ‘is no longer permitted in C mode’ or sth like that, and to elaborate, the driver should issue a warning about that when it encounters it and just ignore it.

@Sirraide
Copy link
Member

Sirraide commented Sep 5, 2025

I think it makes sense not to issue the warning

Doubly so because the point of adding this warning was GCC compatibility from what I can tell.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
clang:frontend Language frontend issues, e.g. anything involving "Sema" clang Clang issues not falling into any other category
Projects
None yet
Development

Successfully merging this pull request may close these issues.

3 participants