C++11 constexpr: Add note stacks containing backtraces if constant evaluation
fails within a call to a constexpr function. Add -fconstexpr-backtrace-limit
argument to driver and frontend, to control the maximum number of notes so
produced (default 10). Fix APValue printing to be able to pretty-print all
APValue types, and move the testing for this functionality from a unittest to
a -verify test now that it's visible in clang's output.

llvm-svn: 146749
diff --git a/clang/test/CXX/expr/expr.const/p2-0x.cpp b/clang/test/CXX/expr/expr.const/p2-0x.cpp
index 2e5e7f2..0e6d69d 100644
--- a/clang/test/CXX/expr/expr.const/p2-0x.cpp
+++ b/clang/test/CXX/expr/expr.const/p2-0x.cpp
@@ -56,7 +56,7 @@
     return n; // expected-note {{reference to temporary cannot be returned from a constexpr function}}
   }
   struct NonConstExprFunction {
-    int n : id_ref( // expected-error {{constant expression}}
+    int n : id_ref( // expected-error {{constant expression}} expected-note {{in call to 'id_ref(16)'}}
         16 // expected-note {{temporary created here}}
         );
   };
@@ -64,10 +64,10 @@
     return &a; // expected-note {{pointer to 'n' cannot be returned from a constexpr function}}
   }
   constexpr const int *return_param(int n) { // expected-note {{declared here}}
-    return address_of(n);
+    return address_of(n); // expected-note {{in call to 'address_of(n)'}}
   }
   struct S {
-    int n : *return_param(0); // expected-error {{constant expression}}
+    int n : *return_param(0); // expected-error {{constant expression}} expected-note {{in call to 'return_param(0)'}}
   };
 }
 
@@ -87,7 +87,7 @@
   constexpr T t2(0); // expected-error {{must be initialized by a constant expression}}
 
   struct S {
-    int n : T(4).r; // expected-error {{constant expression}} expected-note {{temporary created here}}
+    int n : T(4).r; // expected-error {{constant expression}} expected-note {{temporary created here}} expected-note {{in call to 'T(4)'}}
   };
 }
 
@@ -95,17 +95,17 @@
 //   exceed the implementation-defined recursion limits (see Annex B);
 namespace RecursionLimits {
   constexpr int RecurseForever(int n) {
-    return n + RecurseForever(n+1); // expected-note {{constexpr evaluation exceeded maximum depth of 256 calls}}
+    return n + RecurseForever(n+1); // expected-note {{constexpr evaluation exceeded maximum depth of 256 calls}} expected-note 9{{in call to 'RecurseForever(}} expected-note {{skipping 246 calls}}
   }
   struct AlsoRecurseForever {
     constexpr AlsoRecurseForever(int n) :
-      n(AlsoRecurseForever(n+1).n) // expected-note {{constexpr evaluation exceeded maximum depth of 256 calls}}
+      n(AlsoRecurseForever(n+1).n) // expected-note {{constexpr evaluation exceeded maximum depth of 256 calls}} expected-note 9{{in call to 'AlsoRecurseForever(}} expected-note {{skipping 246 calls}}
     {}
     int n;
   };
   struct S {
-    int k : RecurseForever(0); // expected-error {{constant expression}}
-    int l : AlsoRecurseForever(0).n; // expected-error {{constant expression}}
+    int k : RecurseForever(0); // expected-error {{constant expression}} expected-note {{in call to}}
+    int l : AlsoRecurseForever(0).n; // expected-error {{constant expression}} expected-note {{in call to}}
   };
 }
 
@@ -135,7 +135,7 @@
     return q[0]; // expected-note {{dereferenced pointer past the end of subobject of 's' is not a constant expression}}
   }
   struct T {
-    int n : f(p); // expected-error {{not an integer constant expression}}
+    int n : f(p); // expected-error {{not an integer constant expression}} expected-note {{in call to 'f(&s.m + 1)'}}
   };
 }
 
diff --git a/clang/test/SemaCXX/constant-expression-cxx11.cpp b/clang/test/SemaCXX/constant-expression-cxx11.cpp
index 4b59157..5105418 100644
--- a/clang/test/SemaCXX/constant-expression-cxx11.cpp
+++ b/clang/test/SemaCXX/constant-expression-cxx11.cpp
@@ -416,7 +416,11 @@
                        const int *xs, const int *ys, int c) {
   return n ? F(
                *xs, // expected-note {{subexpression not valid}}
-               *ys, ZipFoldR(F, n-1, xs+1, ys+1, c)) : c;
+               *ys,
+               ZipFoldR(F, n-1, xs+1, ys+1, c)) // \
+      expected-note {{in call to 'ZipFoldR(&SubMul, 2, &xs[4], &ys[4], 1)'}} \
+      expected-note {{in call to 'ZipFoldR(&SubMul, 1, &xs[5], &ys[5], 1)'}}
+           : c;
 }
 constexpr int MulAdd(int x, int y, int c) { return x * y + c; }
 constexpr int InnerProduct = ZipFoldR(MulAdd, 5, xs, ys, 0);
@@ -425,7 +429,9 @@
 constexpr int SubMul(int x, int y, int c) { return (x - y) * c; }
 constexpr int DiffProd = ZipFoldR(SubMul, 2, xs+3, ys+3, 1);
 static_assert(DiffProd == 8, "");
-static_assert(ZipFoldR(SubMul, 3, xs+3, ys+3, 1), ""); // expected-error {{constant expression}}
+static_assert(ZipFoldR(SubMul, 3, xs+3, ys+3, 1), ""); // \
+      expected-error {{constant expression}} \
+      expected-note {{in call to 'ZipFoldR(&SubMul, 3, &xs[3], &ys[3], 1)'}}
 
 constexpr const int *p = xs + 3;
 constexpr int xs4 = p[1]; // ok
@@ -441,6 +447,13 @@
 static_assert(**(**(zs + 1) + 1) == 11, "");
 static_assert(*(&(&(*(*&(&zs[2] - 1)[0] + 2 - 2))[2])[-1][-1] + 1) == 11, "");
 
+constexpr int fail(const int &p) {
+  return (&p)[64]; // expected-note {{subexpression}}
+}
+static_assert(fail(*(&(&(*(*&(&zs[2] - 1)[0] + 2 - 2))[2])[-1][-1] + 1)) == 11, ""); // \
+expected-error {{static_assert expression is not an integral constant expression}} \
+expected-note {{in call to 'fail(zs[1][0][1][0])'}}
+
 constexpr int arr[40] = { 1, 2, 3, [8] = 4 }; // expected-warning {{extension}}
 constexpr int SumNonzero(const int *p) {
   return *p + (*p ? SumNonzero(p+1) : 0);
diff --git a/clang/test/SemaCXX/constexpr-backtrace-limit.cpp b/clang/test/SemaCXX/constexpr-backtrace-limit.cpp
new file mode 100644
index 0000000..d2923ab
--- /dev/null
+++ b/clang/test/SemaCXX/constexpr-backtrace-limit.cpp
@@ -0,0 +1,34 @@
+// RUN: %clang_cc1 -std=c++11 -fsyntax-only %s -fconstexpr-backtrace-limit 0 -fconstexpr-depth 4 -fno-caret-diagnostics 2>&1 | FileCheck %s -check-prefix=TEST1
+// TEST1: constant expression
+// TEST1-NEXT: exceeded maximum depth of 4
+// TEST1-NEXT: in call to 'recurse(2)'
+// TEST1-NEXT: in call to 'recurse(3)'
+// TEST1-NEXT: in call to 'recurse(4)'
+// TEST1-NEXT: in call to 'recurse(5)'
+
+// RUN: %clang_cc1 -std=c++11 -fsyntax-only %s -fconstexpr-backtrace-limit 2 -fconstexpr-depth 4 -fno-caret-diagnostics 2>&1 | FileCheck %s -check-prefix=TEST2
+// TEST2: constant expression
+// TEST2-NEXT: exceeded maximum depth of 4
+// TEST2-NEXT: in call to 'recurse(2)'
+// TEST2-NEXT: skipping 2 calls
+// TEST2-NEXT: in call to 'recurse(5)'
+
+// RUN: %clang_cc1 -std=c++11 -fsyntax-only %s -fconstexpr-backtrace-limit 2 -fconstexpr-depth 8 -fno-caret-diagnostics 2>&1 | FileCheck %s -check-prefix=TEST3
+// TEST3: constant expression
+// TEST3-NEXT: subexpression
+// TEST3-NEXT: in call to 'recurse(0)'
+// TEST3-NEXT: skipping 4 calls
+// TEST3-NEXT: in call to 'recurse(5)'
+
+// RUN: %clang_cc1 -std=c++11 -fsyntax-only %s -fconstexpr-backtrace-limit 8 -fconstexpr-depth 8 -fno-caret-diagnostics 2>&1 | FileCheck %s -check-prefix=TEST4
+// TEST4: constant expression
+// TEST4-NEXT: subexpression
+// TEST4-NEXT: in call to 'recurse(0)'
+// TEST4-NEXT: in call to 'recurse(1)'
+// TEST4-NEXT: in call to 'recurse(2)'
+// TEST4-NEXT: in call to 'recurse(3)'
+// TEST4-NEXT: in call to 'recurse(4)'
+// TEST4-NEXT: in call to 'recurse(5)'
+
+constexpr int recurse(int n) { return n ? recurse(n-1) : *(int*)n; }
+static_assert(recurse(5), "");
diff --git a/clang/test/SemaCXX/constexpr-printing.cpp b/clang/test/SemaCXX/constexpr-printing.cpp
new file mode 100644
index 0000000..341495c
--- /dev/null
+++ b/clang/test/SemaCXX/constexpr-printing.cpp
@@ -0,0 +1,73 @@
+// RUN: %clang_cc1 %s -std=c++11 -fsyntax-only -verify
+
+constexpr int extract(struct S &s);
+
+struct S {
+  constexpr S() : n(extract(*this)), m(0) {}
+  constexpr S(int k) : n(k), m(extract(*this)) {}
+  int n, m;
+};
+
+constexpr int extract(S &s) { return s.n; }
+
+// FIXME: once we produce notes for constexpr variable declarations, this should
+// produce a note indicating that S.n is used uninitialized.
+constexpr S s1; // expected-error {{constant expression}}
+constexpr S s2(10);
+
+typedef __attribute__((vector_size(16))) int vector_int;
+
+struct T {
+  constexpr T() : arr() {}
+  int arr[4];
+};
+struct U : T {
+  constexpr U(const int *p) : T(), another(), p(p) {}
+  constexpr U(const U &u) : T(), another(), p(u.p) {}
+  T another;
+  const int *p;
+};
+constexpr U u1(&u1.arr[2]);
+
+constexpr int test_printing(int a, float b, _Complex int c, _Complex float d,
+                            int *e, int &f, vector_int g, U h) {
+  return *e; // expected-note {{subexpression}}
+}
+U u2(0);
+static_assert(test_printing(12, 39.762, 3 + 4i, 12.9 + 3.6i, &u2.arr[4], u2.another.arr[2], (vector_int){5, 1, 2, 3}, u1) == 0, ""); // \
+expected-error {{constant expression}} \
+expected-note {{in call to 'test_printing(12, 3.976200e+01, 3+4i, 1.290000e+01+3.600000e+00i, &u2.T::arr[4], u2.another.arr[2], {5, 1, 2, 3}, {{{}}, {{}}, &u1.T::arr[2]})'}}
+
+struct V {
+  // FIXME: when we can generate these as constexpr constructors, remove the
+  // explicit definitions.
+  constexpr V() : arr{[255] = 42} {}
+  constexpr V(const V &v) : arr{[255] = 42} {}
+  int arr[256];
+};
+constexpr V v;
+constexpr int get(const int *p) { return *p; } // expected-note {{subexpression}}
+constexpr int passLargeArray(V v) { return get(v.arr+256); } // expected-note {{in call to 'get(&v.arr[256])'}}
+static_assert(passLargeArray(v) == 0, ""); // expected-error {{constant expression}} expected-note {{in call to 'passLargeArray({{0, 0, 0, 0, 0, 0, 0, 0, 0, 0, ...}})'}}
+
+union Union {
+  constexpr Union(int n) : b(n) {}
+  constexpr Union(const Union &u) : b(u.b) {}
+  int a, b;
+};
+constexpr Union myUnion = 76;
+
+constexpr int badness(Union u) { return u.a + u.b; } // expected-note {{subexpression}}
+static_assert(badness(myUnion), ""); // expected-error {{constant expression}} \
+        expected-note {{in call to 'badness({.b = 76})'}}
+
+struct MemPtrTest {
+  int n;
+  void f();
+};
+MemPtrTest mpt;
+constexpr int MemPtr(int (MemPtrTest::*a), void (MemPtrTest::*b)(), int &c) {
+  return c; // expected-note {{subexpression}}
+}
+static_assert(MemPtr(&MemPtrTest::n, &MemPtrTest::f, mpt.*&MemPtrTest::n), ""); // expected-error {{constant expression}} \
+expected-note {{in call to 'MemPtr(&MemPtrTest::n, &MemPtrTest::f, mpt.n)'}}