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.

Copy link
Contributor

Choose a reason for hiding this comment

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

Do we have precedent for that? Users usually don't set warning flags per language in a project that mixes both.
We probably should just gate warn_not_eliding_copy_on_return on C++ (rather than the whole function), and not try to be more clever

Copy link
Member

Choose a reason for hiding this comment

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

That’s probably the easiest solution, yeah

@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.

@bjosv
Copy link
Contributor

bjosv commented Sep 8, 2025

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.

I guess rejecting the flag would make it more similar to GCC, but as I understand it, GCC don't have the -Weverything flag. This will make it a bit harder for us..
The -Weverything flag seems to just lift any found warning-severity from ignore to a warning (or maybe I'm missing something..)

Maybe we still need to have the C++ check in computeNRVO(), but moving it to just avoid emitting the diagnostic error?

Change-Id: Iae9d550fdb7adc0f7332c28dbb817f1a6d585d78
@javiermunozkirschberg
Copy link
Author

Hi @Sirraide, @cor3ntin, @bjosv

Thanks a lot for your comments. I've tried to reflect them with a further commit, where I've moved the check to only the warning and I've modified the release notes so the change is reflected in the "improved diagnostics".

Thanks again for all your comments.

@@ -16174,8 +16174,10 @@ void Sema::computeNRVO(Stmt *Body, FunctionScopeInfo *Scope) {
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) {
Copy link
Member

Choose a reason for hiding this comment

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

Instead of using a nested if, just merge the two conditions using &&.

Copy link
Author

@javiermunozkirschberg javiermunozkirschberg Sep 8, 2025

Choose a reason for hiding this comment

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

Thanks!

One question, after the Diag, and as part of if(!NRVOCandidate) there is a
Returns[I]->setNRVOCandidate(nullptr);

If I merge the two ifs(), then diag will be skip, but returns[i]->setNRVOCandidate(nullptr) will be also skipped. Is that ok?

Copy link
Member

Choose a reason for hiding this comment

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

Ah, no, disregard that. I misread that because of how the diff was formatted; we do in fact need to keep them separate yes.

@@ -0,0 +1,41 @@
// RUN: %clang -std=c23 -Wnrvo -Xclang -verify %s
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
// RUN: %clang -std=c23 -Wnrvo -Xclang -verify %s
// RUN: %clang_cc1 -std=c23 -Wnrvo -verify %s

Comment on lines +35 to +41
int main(int argc, char** argv)
{
String s= randomString();
clean(&s);

return 0;
}
Copy link
Contributor

Choose a reason for hiding this comment

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

nit: lets call that functions something else void test() would do - clang test files are never executed and having main functions is confusing

Choose a reason for hiding this comment

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

I'll fix this, thanks!

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.

5 participants