Skip to content

Commit 4b34c65

Browse files
committed
Adding new warning: not eliding copy on return
1 parent a4eb0db commit 4b34c65

File tree

3 files changed

+24
-1
lines changed

3 files changed

+24
-1
lines changed

clang/include/clang/Basic/DiagnosticSemaKinds.td

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -12445,6 +12445,10 @@ def warn_zero_as_null_pointer_constant : Warning<
1244512445
"zero as null pointer constant">,
1244612446
InGroup<DiagGroup<"zero-as-null-pointer-constant">>, DefaultIgnore;
1244712447

12448+
def warn_not_eliding_copy_on_return : Warning<
12449+
"not eliding copy on return">,
12450+
InGroup<DiagGroup<"nrvo">>, DefaultIgnore;
12451+
1244812452
def err_nullability_cs_multilevel : Error<
1244912453
"nullability keyword %0 cannot be applied to multi-level pointer type %1">;
1245012454
def note_nullability_type_specifier : Note<

clang/lib/Sema/SemaDecl.cpp

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -16093,8 +16093,11 @@ void Sema::computeNRVO(Stmt *Body, FunctionScopeInfo *Scope) {
1609316093

1609416094
for (unsigned I = 0, E = Scope->Returns.size(); I != E; ++I) {
1609516095
if (const VarDecl *NRVOCandidate = Returns[I]->getNRVOCandidate()) {
16096-
if (!NRVOCandidate->isNRVOVariable())
16096+
if (!NRVOCandidate->isNRVOVariable()) {
16097+
Diag(Returns[I]->getRetValue()->getExprLoc(),
16098+
diag::warn_not_eliding_copy_on_return);
1609716099
Returns[I]->setNRVOCandidate(nullptr);
16100+
}
1609816101
}
1609916102
}
1610016103
}

clang/test/SemaCXX/warn-nrvo.cpp

Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,16 @@
1+
// RUN: %clang_cc1 -fsyntax-only -Wnrvo -verify %s
2+
struct MyClass {
3+
int value;
4+
int c;
5+
MyClass(int v) : value(v), c(0) {}
6+
MyClass(const MyClass& other) : value(other.value) { c++; }
7+
};
8+
9+
MyClass create_object(bool condition) {
10+
MyClass obj1(1);
11+
MyClass obj2(2);
12+
if (condition) {
13+
return obj1; // expected-warning{{not eliding copy on return}}
14+
}
15+
return obj2; // expected-warning{{not eliding copy on return}}
16+
}

0 commit comments

Comments
 (0)