Store a pointer to the return value in a static alloca and let the debugger use that
as the variable address for NRVO variables.

Subscribers: hiraditya, cfe-commits, llvm-commits

Tags: #clang, #llvm

Differential Revision: https://reviews.llvm.org/D63361

llvm-svn: 363952
GitOrigin-RevId: 7fac5c8d940c91e1e7b8b704186b4649170b029f
diff --git a/nrvo-string.cpp b/nrvo-string.cpp
index 18acebb..e27d4d3 100644
--- a/nrvo-string.cpp
+++ b/nrvo-string.cpp
@@ -17,11 +17,32 @@
 string get_string() {
   string unused;
   string result = 3;
-// DEBUGGER: break 21
+  // DEBUGGER: break 21
   return result;
 }
-int main() { get_string(); }
+void some_function(int) {}
+struct string2 {
+  string2() = default;
+  string2(string2 &&other) { i = other.i; }
+  int i;
+};
+string2 get_string2() {
+  string2 result;
+  result.i = 5;
+  some_function(result.i);
+  // Test that the debugger can get the value of result after another
+  // function is called.
+  // DEBUGGER: break 35
+  return result;
+}
+int main() {
+  get_string();
+  get_string2();
+}
 
 // DEBUGGER: r
 // DEBUGGER: print result.i
 // CHECK:  = 3
+// DEBUGGER: c
+// DEBUGGER: print result.i
+// CHECK:  = 5
diff --git a/win_cdb/nrvo.cpp b/win_cdb/nrvo.cpp
new file mode 100644
index 0000000..5712118
--- /dev/null
+++ b/win_cdb/nrvo.cpp
@@ -0,0 +1,49 @@
+// This ensures that DW_OP_deref is inserted when necessary, such as when NRVO
+// of a string object occurs in C++.
+//
+// RUN: %clang_cl %s -o %t.exe -fuse-ld=lld -Z7
+// RUN: grep DE[B]UGGER: %s | sed -e 's/.*DE[B]UGGER: //' > %t.script
+// RUN: %cdb -cf %t.script %t.exe | FileCheck %s --check-prefixes=DEBUGGER,CHECK
+//
+
+struct string {
+  string() {}
+  string(int i) : i(i) {}
+  ~string() {}
+  int i = 0;
+};
+string get_string() {
+  string unused;
+  string result = 3;
+  __debugbreak();
+  return result;
+}
+void some_function(int) {}
+struct string2 {
+  string2() = default;
+  string2(string2 &&other) { i = other.i; }
+  int i;
+};
+string2 get_string2() {
+  string2 result;
+  result.i = 5;
+  some_function(result.i);
+  // Test that the debugger can get the value of result after another
+  // function is called.
+  __debugbreak();
+  return result;
+}
+int main() {
+  get_string();
+  get_string2();
+}
+
+// DEBUGGER: g
+// DEBUGGER: ?? result
+// CHECK: struct string *
+// CHECK:    +0x000 i : 0n3
+// DEBUGGER: g
+// DEBUGGER: ?? result
+// CHECK: struct string2 *
+// CHECK:    +0x000 i : 0n5
+// DEBUGGER: q