davide created this revision.
davide added reviewers: rsmith, rtrieu.
davide added a subscriber: cfe-commits.
davide set the repository for this revision to rL LLVM.

As Richard notes in PR23819, if we're returning a function parameter, copy 
elision isn't possible.

Repository:
  rL LLVM

http://reviews.llvm.org/D11305

Files:
  lib/Sema/SemaInit.cpp
  test/SemaCXX/warn-pessmizing-move.cpp
  test/SemaCXX/warn-redundant-move.cpp

Index: test/SemaCXX/warn-redundant-move.cpp
===================================================================
--- test/SemaCXX/warn-redundant-move.cpp
+++ test/SemaCXX/warn-redundant-move.cpp
@@ -90,3 +90,10 @@
   return std::move(b1);
   return std::move(b2);
 }
+
+//PR23819
+struct X {};
+X g();
+void h(X&&);
+X f(X x) { return std::move(x); } //expected-warning{{redundant move in return statement}} \
+                                  //expected-note{{remove std::move call here}}
Index: test/SemaCXX/warn-pessmizing-move.cpp
===================================================================
--- test/SemaCXX/warn-pessmizing-move.cpp
+++ test/SemaCXX/warn-pessmizing-move.cpp
@@ -23,10 +23,6 @@
   return a1;
   return a2;
   return std::move(a1);
-  // expected-warning@-1{{prevents copy elision}}
-  // expected-note@-2{{remove std::move call}}
-  // CHECK: fix-it:"{{.*}}":{[[@LINE-3]]:10-[[@LINE-3]]:20}:""
-  // CHECK: fix-it:"{{.*}}":{[[@LINE-4]]:22-[[@LINE-4]]:23}:""
   return std::move(a2);
   // expected-warning@-1{{prevents copy elision}}
   // expected-note@-2{{remove std::move call}}
@@ -46,10 +42,6 @@
   return b1;
   return b2;
   return std::move(b1);
-  // expected-warning@-1{{prevents copy elision}}
-  // expected-note@-2{{remove std::move call}}
-  // CHECK: fix-it:"{{.*}}":{[[@LINE-3]]:10-[[@LINE-3]]:20}:""
-  // CHECK: fix-it:"{{.*}}":{[[@LINE-4]]:22-[[@LINE-4]]:23}:""
   return std::move(b2);
   // expected-warning@-1{{prevents copy elision}}
   // expected-note@-2{{remove std::move call}}
@@ -163,41 +155,23 @@
 #define wrap1(x) x
 #define wrap2(x) x
 
-// Macro test.  Since the std::move call is outside the macro, it is
-// safe to suggest a fix-it.
+// Macro tests.
 A test8(A a) {
   return std::move(a);
-  // expected-warning@-1{{prevents copy elision}}
-  // expected-note@-2{{remove std::move call}}
-  // CHECK: fix-it:"{{.*}}":{[[@LINE-3]]:10-[[@LINE-3]]:20}:""
-  // CHECK: fix-it:"{{.*}}":{[[@LINE-4]]:21-[[@LINE-4]]:22}:""
   return std::move(wrap1(a));
-  // expected-warning@-1{{prevents copy elision}}
-  // expected-note@-2{{remove std::move call}}
-  // CHECK: fix-it:"{{.*}}":{[[@LINE-3]]:10-[[@LINE-3]]:20}:""
-  // CHECK: fix-it:"{{.*}}":{[[@LINE-4]]:28-[[@LINE-4]]:29}:""
   return std::move(wrap1(wrap2(a)));
-  // expected-warning@-1{{prevents copy elision}}
-  // expected-note@-2{{remove std::move call}}
-  // CHECK: fix-it:"{{.*}}":{[[@LINE-3]]:10-[[@LINE-3]]:20}:""
-  // CHECK: fix-it:"{{.*}}":{[[@LINE-4]]:35-[[@LINE-4]]:36}:""
 }
 
 #define test9            \
   A test9(A a) {         \
     return std::move(a); \
   }
 
-// Macro test.  The std::call is inside the macro, so no fix-it is suggested.
 test9
-// expected-warning@-1{{prevents copy elision}}
-// CHECK-NOT: fix-it
 
 #define return_a return std::move(a)
 
 // Macro test.  The std::call is inside the macro, so no fix-it is suggested.
 A test10(A a) {
   return_a;
-  // expected-warning@-1{{prevents copy elision}}
-  // CHECK-NOT: fix-it
 }
Index: lib/Sema/SemaInit.cpp
===================================================================
--- lib/Sema/SemaInit.cpp
+++ lib/Sema/SemaInit.cpp
@@ -5988,6 +5988,18 @@
     if (!VD->getType()->isRecordType())
       return;
 
+    // If we're returning a function parameter, copy elision
+    // is not possible.
+    if (const FunctionDecl *FD =
+        dyn_cast_or_null<FunctionDecl>(VD->getParentFunctionOrMethod())) {
+      for (const auto *Param : FD->params()) {
+        if (Param == VD) {
+          DiagID = diag::warn_redundant_move_on_return;
+          break;
+        }
+      }
+    }
+
     if (DiagID == 0) {
       DiagID = S.Context.hasSameUnqualifiedType(DestType, VD->getType())
                    ? diag::warn_pessimizing_move_on_return
_______________________________________________
cfe-commits mailing list
cfe-commits@cs.uiuc.edu
http://lists.cs.uiuc.edu/mailman/listinfo/cfe-commits

Reply via email to