ADDED   tools/clang/test/SemaCXX/constexpr-circulardep.cpp
Index: tools/clang/test/SemaCXX/constexpr-circulardep.cpp
==================================================================
--- tools/clang/test/SemaCXX/constexpr-circulardep.cpp
+++ tools/clang/test/SemaCXX/constexpr-circulardep.cpp
@@ -0,0 +1,41 @@
+// RUN: %clang_cc1 -std=c++11 -fsyntax-only -verify %s
+// RUN: %clang_cc1 -std=c++11 -fsyntax-only -Wno-invalid-constexpr -verify %s
+// RUN: %clang_cc1 -std=c++11 -fsyntax-only -DWITH_LIMIT -fconstexpr-depth 3 -verify %s
+
+constexpr int fn(int x) {
+  return x == 255 ? x : fn(x ^ (x << 1) & 0xFF);
+}
+
+constexpr int valid = fn(1);
+#ifdef WITH_LIMIT
+// expected-error@9{{constexpr variable 'valid' must be initialized by a constant expression}}
+// expected-note@6{{constexpr evaluation exceeded maximum depth of 3 calls}}
+// expected-note@6{{in call to 'fn(5)'}}
+// expected-note@6{{in call to 'fn(3)'}}
+// expected-note@9{{in call to 'fn(1)'}}
+#endif
+
+constexpr int circular = fn(2);
+// expected-error@18{{constexpr variable 'circular' must be initialized by a constant expression}}
+#ifdef WITH_LIMIT
+// expected-note@6{{constexpr evaluation exceeded maximum depth of 3 calls}}
+#else
+// expected-note@6{{circular dependency detected}}
+// expected-note@6{{in call to 'fn(254)'}}
+// expected-note@6{{in call to 'fn(170)'}}
+// expected-note@6{{in call to 'fn(102)'}}
+// expected-note@6{{in call to 'fn(34)'}}
+// expected-note@6{{in call to 'fn(30)'}}
+#endif
+// expected-note@6{{in call to 'fn(10)'}}
+// expected-note@6{{in call to 'fn(6)'}}
+// expected-note@18{{in call to 'fn(2)'}}
+
+constexpr int loop() {
+  return loop();
+}
+
+static_assert(loop() == 0, "");
+// expected-error@38{{static_assert expression is not an integral constant expression}}
+// expected-note@35{{circular dependency detected}}
+// expected-note@38{{in call to 'loop()'}}

ADDED   tools/clang/test/SemaCXX/constexpr-torture.cpp
Index: tools/clang/test/SemaCXX/constexpr-torture.cpp
==================================================================
--- tools/clang/test/SemaCXX/constexpr-torture.cpp
+++ tools/clang/test/SemaCXX/constexpr-torture.cpp
@@ -0,0 +1,288 @@
+// RUN: %clang_cc1 -std=c++11 -fsyntax-only -verify %s
+
+// The following test case includes a regression check on how many
+// hits are achieved per function call.  As function caching improves,
+// the number of hits may go up and the number of cached calls may
+// go up or down.  The number of hits should not go down.
+
+// This will take several hours if caching of constexpr functions
+// is not working... (you have been warned!)
+
+// The following macro ensures that clang does not consume comments
+// until *after* the statement is evaluated.  Clang parses all the
+// way to the end of any comments which follow the terminating ; of
+// a statement.  This causes 'verıfy-fncache-stats' to pick up old
+// statistics.
+#define E(Stmt) Stmt;
+
+template <unsigned A, unsigned B> struct match;
+template <unsigned A> struct match<A,A> { enum { value = true }; };
+
+int invalid = 0; // expected-note{{declared here}}
+template <typename X>
+constexpr X fn0(X i) {
+  return i < -2 ? X(invalid) : i; // expected-note{{read of non-const variable 'invalid'}}
+}
+
+static_assert(fn0(-3) == 0, "oops"); // expected-error{{not an integral constant expression}} \
+                                     // expected-note{{in call to 'fn0(-3)'}}
+
+template <typename X>
+constexpr X fn1(X i) {
+  return i < 0 ? 0 : (i + fn1(fn0<X>(i-1)) + fn1(fn0<X>(i-2)));
+}
+
+E();
+// verify-fncache-stats expected-note{{0 hits from 0 cached calls}}
+E(static_assert(fn1<int>(5)  == 26, "oops"));
+// verify-fncache-stats expected-note{{10 hits from 15 cached calls}}
+E(static_assert(fn1<int>(5)  == 26, "oops"));
+// verify-fncache-stats expected-note{{11 hits from 15 cached calls}}
+E(static_assert(fn1<int>(10) == 364, "oops"));
+// verify-fncache-stats expected-note{{22 hits from 25 cached calls}}
+E(static_assert(fn1<int>(15) == 4163, "oops"));
+// verify-fncache-stats expected-note{{33 hits from 35 cached calls}}
+E(static_assert(fn1<int>(20) == 46345, "oops"));
+// verify-fncache-stats expected-note{{44 hits from 45 cached calls}}
+E(static_assert(fn1<int>(25) == 514201, "oops"));
+// verify-fncache-stats expected-note{{55 hits from 55 cached calls}}
+E(static_assert(fn1<int>(30) == 5702854, "oops"));
+// verify-fncache-stats expected-note{{66 hits from 65 cached calls}}
+E(static_assert(fn1<int>(35) == 63245948, "oops"));
+// verify-fncache-stats expected-note{{77 hits from 75 cached calls}}
+E(static_assert(fn1<int>(40) == 701408690, "oops"));
+// verify-fncache-stats expected-note{{88 hits from 85 cached calls}}
+
+E(static_assert(fn1<double>(5)  == 26, "oops"));
+// verify-fncache-stats expected-note{{98 hits from 100 cached calls}}
+E(static_assert(fn1<double>(10) == 364, "oops"));
+// verify-fncache-stats expected-note{{109 hits from 110 cached calls}}
+E(static_assert(fn1<double>(15) == 4163, "oops"));
+// verify-fncache-stats expected-note{{120 hits from 120 cached calls}}
+E(static_assert(fn1<double>(20) == 46345, "oops"));
+// verify-fncache-stats expected-note{{131 hits from 130 cached calls}}
+E(static_assert(fn1<double>(25) == 514201, "oops"));
+// verify-fncache-stats expected-note{{142 hits from 140 cached calls}}
+E(static_assert(fn1<double>(30) == 5702854, "oops"));
+// verify-fncache-stats expected-note{{153 hits from 150 cached calls}}
+E(static_assert(fn1<double>(35) == 63245948, "oops"));
+// verify-fncache-stats expected-note{{164 hits from 160 cached calls}}
+E(static_assert(fn1<double>(40) == 701408690, "oops"));
+// verify-fncache-stats expected-note{{175 hits from 170 cached calls}}
+
+struct A1 {
+private:
+  int i;
+public:
+  constexpr A1(int i) : i(i) { }
+  constexpr operator int() const { return i; }
+  virtual void a() { }
+};
+
+E();
+// verify-fncache-stats expected-note{{175 hits from 170 cached calls}}
+E(static_assert(fn1<A1>(5)  == 26, "oops"));
+// verify-fncache-stats expected-note{{185 hits from 185 cached calls}}
+E(static_assert(fn1<A1>(10) == 364, "oops"));
+// verify-fncache-stats expected-note{{196 hits from 195 cached calls}}
+E(static_assert(fn1<A1>(15) == 4163, "oops"));
+// verify-fncache-stats expected-note{{207 hits from 205 cached calls}}
+E(static_assert(fn1<A1>(20) == 46345, "oops"));
+// verify-fncache-stats expected-note{{218 hits from 215 cached calls}}
+E(static_assert(fn1<A1>(25) == 514201, "oops"));
+// verify-fncache-stats expected-note{{229 hits from 225 cached calls}}
+E(static_assert(fn1<A1>(30) == 5702854, "oops"));
+// verify-fncache-stats expected-note{{240 hits from 235 cached calls}}
+E(static_assert(fn1<A1>(35) == 63245948, "oops"));
+// verify-fncache-stats expected-note{{251 hits from 245 cached calls}}
+E(static_assert(fn1<A1>(40) == 701408690, "oops"));
+// verify-fncache-stats expected-note{{262 hits from 255 cached calls}}
+
+struct A2 : A1 {
+    constexpr A2(int i) : A1(i) { }
+  };
+
+E();
+// verify-fncache-stats expected-note{{262 hits from 255 cached calls}}
+E(static_assert(fn1<A2>(5)  == 26, "oops"));
+// verify-fncache-stats expected-note{{272 hits from 270 cached calls}}
+E(static_assert(fn1<A2>(10) == 364, "oops"));
+// verify-fncache-stats expected-note{{283 hits from 280 cached calls}}
+E(static_assert(fn1<A2>(15) == 4163, "oops"));
+// verify-fncache-stats expected-note{{294 hits from 290 cached calls}}
+E(static_assert(fn1<A2>(20) == 46345, "oops"));
+// verify-fncache-stats expected-note{{305 hits from 300 cached calls}}
+E(static_assert(fn1<A2>(25) == 514201, "oops"));
+// verify-fncache-stats expected-note{{316 hits from 310 cached calls}}
+E(static_assert(fn1<A2>(30) == 5702854, "oops"));
+// verify-fncache-stats expected-note{{327 hits from 320 cached calls}}
+E(static_assert(fn1<A2>(35) == 63245948, "oops"));
+// verify-fncache-stats expected-note{{338 hits from 330 cached calls}}
+E(static_assert(fn1<A2>(40) == 701408690, "oops"));
+// verify-fncache-stats expected-note{{349 hits from 340 cached calls}}
+E(static_assert(fn1<A2>(40) == 701408690, "oops"));
+// verify-fncache-stats expected-note{{350 hits from 340 cached calls}}
+
+struct B {
+  int a,b;
+  constexpr int operator[](int i) const { return i ? b : a; }
+  constexpr int sum() const { return a+b; }
+};
+
+constexpr B fn2(B i) {
+  return {
+    (i[0] < 0 ? 0 : i[0] + fn2(B{i[0]-1, i[1]-2})[0] + fn2(B{i[0]-2, i[1]-1})[1]),
+    (i[1] < 0 ? 0 : i[1] + fn2(B{i[0]-2, i[1]-1})[1] + fn2(B{i[0]-1, i[1]-2})[0])
+  };
+}
+
+E();
+// verify-fncache-stats expected-note{{350 hits from 340 cached calls}}
+E(static_assert(match<fn2({30,40}).sum(), 118428136>::value, "oops"));
+// verify-fncache-stats expected-note{{1216 hits from 870 cached calls}}
+E(static_assert(match<fn2({45,35}).sum(), 1173866868>::value, "oops"));
+// verify-fncache-stats expected-note{{2338 hits from 1538 cached calls}}
+E(static_assert(match<fn2({45,35}).sum(), 1173866868>::value, "oops"));
+// verify-fncache-stats expected-note{{2340 hits from 1538 cached calls}}
+
+constexpr const char* inc(const char* s, unsigned n, unsigned m) {
+  return (m > n) ? s : (s + m);
+}
+
+constexpr unsigned dec(unsigned n, unsigned m) {
+  return (m > n) ? 0 : (n - m);
+}
+
+constexpr unsigned fn3(const char* s, unsigned len) {
+  return len == 0 ? 0
+                  : (*s ^ (fn3(inc(s,len,1), dec(len,1)) << 1)
+                        ^ (~fn3(inc(s,len,2), dec(len,2)) >> 1));
+}
+
+#define STR(X) X, __builtin_strlen(X)
+
+E();
+// verify-fncache-stats expected-note{{2340 hits from 1538 cached calls}}
+E(static_assert(match<fn3(STR("A long string")), 1845171819>::value, "oops"));
+// verify-fncache-stats expected-note{{2352 hits from 1579 cached calls}}
+E(static_assert(match<fn3(STR("A long string")), 1845171819>::value, "oops"));
+// verify-fncache-stats expected-note{{2353 hits from 1579 cached calls}}
+
+E(static_assert(match<fn3(STR("A very very very very very "
+                                "very very very very very "
+                                "very very very very very "
+                                "very very very very very "
+                                "very very very very very "
+                                "very very very very very "
+                                "very very very very very "
+                                "very very very very very "
+                                "very very very very very "
+                                "very very very very very "
+                                "very very very very very "
+                                "very very very very very "
+                                "long string")), 260557491>::value, "oops"));
+// verify-fncache-stats expected-note{{2691 hits from 2494 cached calls}}
+E(static_assert(match<fn3(STR("A very very very very very "
+                                "very very very very very "
+                                "very very very very very "
+                                "very very very very very "
+                                "very very very very very "
+                                "very very very very very "
+                                "very very very very very "
+                                "very very very very very "
+                                "very very very very very "
+                                "very very very very very "
+                                "very very very very very "
+                                "very very very very very "
+                                "long string")), 260557491>::value, "oops"));
+// verify-fncache-stats expected-note{{2692 hits from 2494 cached calls}}
+
+constexpr const char str[13] = { 'A', ' ', 'l', 'o', 'n', 'g', ' ', 's', 't', 'r', 'i', 'n', 'g' };
+E(static_assert(match<fn3(str, 13), 1845171819>::value, "oops"));
+// verify-fncache-stats expected-note{{2730 hits from 2509 cached calls}}
+E(static_assert(match<fn3(str, 13), 1845171819>::value, "oops"));
+// verify-fncache-stats expected-note{{2731 hits from 2509 cached calls}}
+
+template <char... String>
+struct LiteralData {
+  enum { length = sizeof...(String) };
+  static constexpr char string[length+1] = { String..., 0 };
+
+  template <char... S2>
+  constexpr LiteralData<String..., S2...> operator+(LiteralData<S2...>) const {
+    return LiteralData<String..., S2...>();
+  }
+};
+
+template <char... String> constexpr char LiteralData<String...>::string[];
+
+template <char... String>
+constexpr LiteralData<String...> operator"" _str() {
+  return LiteralData<String...>();
+}
+
+#define LITERAL 1234567890123456789012345678901234567890_str \
+              + 1234567890123456789012345678901234567890_str \
+              + 1234567890123456789012345678901234567890_str \
+              + 1234567890123456789012345678901234567890_str \
+              + 1234567890123456789012345678901234567890_str \
+              + 1234567890123456789012345678901234567890_str \
+              + 1234567890123456789012345678901234567890_str \
+              + 1234567890123456789012345678901234567890_str \
+              + 1234567890123456789012345678901234567890_str \
+              + 1234567890123456789012345678901234567890_str
+
+// verify-fncache-stats expected-note{{2731 hits from 2509 cached calls}}
+E(static_assert(match<fn3((LITERAL).string, (LITERAL).length), 2622881326>::value, "oops"));
+// verify-fncache-stats expected-note{{3775 hits from 3086 cached calls}}
+E(static_assert(match<fn3((LITERAL).string, (LITERAL).length), 2622881326>::value, "oops"));
+// verify-fncache-stats expected-note{{4994 hits from 3488 cached calls}}
+// FIXME: it would be nice if the above could pick straight out of the cache
+
+constexpr struct Literal {
+  template <char... String>
+  constexpr Literal(LiteralData<String...> Data)
+    : string(Data.string), length(Data.length) { }
+
+  const char* const string;
+  const unsigned length;
+} S = LITERAL;
+
+// verify-fncache-stats expected-note{{5004 hits from 3488 cached calls}}
+E(static_assert(match<fn3(S.string, S.length), 2622881326>::value, "oops"));
+// verify-fncache-stats expected-note{{5005 hits from 3488 cached calls}}
+
+constexpr unsigned fn4(const Literal& literal, unsigned len) {
+  return len == 0 ? 0
+                  : (literal.string[literal.length - len] ^
+                       (fn4(literal, dec(len,1)) << 1) ^
+                       (~fn4(literal, dec(len,2)) >> 1));
+}
+
+// verify-fncache-stats expected-note{{5005 hits from 3488 cached calls}}
+E(static_assert(match<fn4(S, S.length), 2622881326>::value, "oops"));
+// verify-fncache-stats expected-note{{6205 hits from 3889 cached calls}}
+E(static_assert(match<fn4(S, S.length), 2622881326>::value, "oops"));
+// verify-fncache-stats expected-note{{6206 hits from 3889 cached calls}}
+
+// FIXME: clang crashes due to stack overflow on recursive constexpr
+// template functions, so LITERAL has been shortened (see bug #12896)
+#undef LITERAL
+#define LITERAL 1234567890123456789012345678901234567890_str \
+              + 1234567890123456789012345678901234567890_str
+
+// Uses local cache rather than global cache
+template <typename T, unsigned Len = T::length>
+constexpr unsigned fn5(const T& literal) {
+  return Len == 0 ? 0
+                  : (literal.string[T::length - Len] ^
+                       (fn5<T,dec(Len,1)>(literal) << 1) ^
+                       (~fn5<T,dec(Len,2)>(literal) >> 1));
+}
+
+// verify-fncache-stats expected-note-re{{6206 hits from 3889 cached calls .*/0kB\)}}
+E(static_assert(match<fn5(LITERAL), 3563564404>::value, "oops"));
+// verify-fncache-stats expected-note-re{{6448 hits from 3972 cached calls .*/[1-9][0-9]*kB\)}}
+E(static_assert(match<fn5(LITERAL), 3563564404>::value, "oops"));
+// verify-fncache-stats expected-note{{6530 hits from 4053 cached calls}}
+// FIXME: it would be nice if the above could pick straight out of the cache

