-
Notifications
You must be signed in to change notification settings - Fork 15k
[clang] Restrict -Wnrvo to C++ code only. #157059
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
base: main
Are you sure you want to change the base?
Conversation
Change-Id: Iaadd60f072c176972a5210c1fd1a6f0499d3ff39
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 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. |
@llvm/pr-subscribers-clang Author: None (javiermunozkirschberg) ChangesPull 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:
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;
+}
|
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 :) |
There was a problem hiding this 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. |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
- 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)
There was a problem hiding this comment.
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.
Doubly so because the point of adding this warning was GCC compatibility from what I can tell. |
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