| 1 | // RUN: %clang_cc1 -fsyntax-only -triple x86_64-unknown-unknown -verify -std=c++11 -fcxx-exceptions %s |
| 2 | // RUN: %clang_cc1 -fsyntax-only -triple x86_64-unknown-unknown -verify -std=c++14 -fcxx-exceptions %s |
| 3 | // RUN: %clang_cc1 -fsyntax-only -triple x86_64-unknown-unknown -std=c++11 -fcxx-exceptions -Wno-invalid-constexpr %s -DNO_INVALID_CONSTEXPR |
| 4 | |
| 5 | namespace StdExample { |
| 6 | |
| 7 | constexpr int f(void *) { return 0; } |
| 8 | constexpr int f(...) { return 1; } |
| 9 | constexpr int g1() { return f(0); } |
| 10 | constexpr int g2(int n) { return f(n); } |
| 11 | constexpr int g3(int n) { return f(n*0); } |
| 12 | |
| 13 | namespace N { |
| 14 | constexpr int c = 5; |
| 15 | constexpr int h() { return c; } |
| 16 | } |
| 17 | constexpr int c = 0; |
| 18 | constexpr int g4() { return N::h(); } |
| 19 | |
| 20 | static_assert(f(0) == 0, ""); |
| 21 | static_assert(f('0') == 1, ""); |
| 22 | static_assert(g1() == 0, ""); |
| 23 | static_assert(g2(0) == 1, ""); |
| 24 | static_assert(g2(1) == 1, ""); |
| 25 | static_assert(g3(0) == 1, ""); |
| 26 | static_assert(g3(1) == 1, ""); |
| 27 | static_assert(N::h() == 5, ""); |
| 28 | static_assert(g4() == 5, ""); |
| 29 | |
| 30 | |
| 31 | constexpr int f(bool b) |
| 32 | { return b ? throw 0 : 0; } // ok |
| 33 | constexpr int f() { return throw 0, 0; } // expected-error {{constexpr function never produces a constant expression}} expected-note {{subexpression}} |
| 34 | |
| 35 | struct B { |
| 36 | constexpr B(int x) : i(0) { } |
| 37 | int i; |
| 38 | }; |
| 39 | |
| 40 | int global; // expected-note {{declared here}} |
| 41 | |
| 42 | struct D : B { |
| 43 | constexpr D() : B(global) { } // expected-error {{constexpr constructor never produces a constant expression}} expected-note {{read of non-const}} |
| 44 | }; |
| 45 | |
| 46 | } |
| 47 | |
| 48 | namespace PotentialConstant { |
| 49 | |
| 50 | constexpr int Comma(int n) { return // expected-error {{constexpr function never produces a constant expression}} |
| 51 | (void)(n * 2), |
| 52 | throw 0, // expected-note {{subexpression}} |
| 53 | 0; |
| 54 | } |
| 55 | |
| 56 | int ng; // expected-note 6{{here}} |
| 57 | constexpr int BinaryOp1(int n) { return n + ng; } // expected-error {{never produces}} expected-note {{read}} |
| 58 | constexpr int BinaryOp2(int n) { return ng + n; } // expected-error {{never produces}} expected-note {{read}} |
| 59 | |
| 60 | double dg; // expected-note 2{{here}} |
| 61 | constexpr double BinaryOp1(double d) { return d + dg; } // expected-error {{never produces}} expected-note {{read}} |
| 62 | constexpr double BinaryOp2(double d) { return dg + d; } // expected-error {{never produces}} expected-note {{read}} |
| 63 | |
| 64 | constexpr int Add(int a, int b, int c) { return a + b + c; } |
| 65 | constexpr int FunctionArgs(int a) { return Add(a, ng, a); } // expected-error {{never produces}} expected-note {{read}} |
| 66 | |
| 67 | struct S { int a; int b; int c[2]; }; |
| 68 | constexpr S InitList(int a) { return { a, ng }; }; // expected-error {{never produces}} expected-note {{read}} |
| 69 | constexpr S InitList1a(int a) { return S{ a, ng }; }; // expected-error {{never produces}} expected-note {{read}} |
| 70 | constexpr S InitList2(int a) { return { a, a, { ng } }; }; // expected-error {{never produces}} expected-note {{read}} |
| 71 | constexpr S InitList3(int a) { return a ? S{ a, a } : S{ a, ng }; }; // ok |
| 72 | |
| 73 | constexpr int LogicalAnd1(int n) { return n && (throw, 0); } // ok |
| 74 | constexpr int LogicalAnd2(int n) { return 1 && (throw, 0); } // expected-error {{never produces}} expected-note {{subexpression}} |
| 75 | |
| 76 | constexpr int LogicalOr1(int n) { return n || (throw, 0); } // ok |
| 77 | constexpr int LogicalOr2(int n) { return 0 || (throw, 0); } // expected-error {{never produces}} expected-note {{subexpression}} |
| 78 | |
| 79 | constexpr int Conditional1(bool b, int n) { return b ? n : ng; } // ok |
| 80 | constexpr int Conditional2(bool b, int n) { return b ? n * ng : n + ng; } // expected-error {{never produces}} expected-note {{both arms of conditional operator are unable to produce a constant expression}} |
| 81 | |
| 82 | // __builtin_constant_p ? : is magical, and is always a potential constant. |
| 83 | constexpr bool BcpCall(int n) { |
| 84 | return __builtin_constant_p((int*)n != &n) ? (int*)n != &n : (int*)n != &n; // expected-warning 3 {{cast to 'int *' from smaller integer type 'int'}} |
| 85 | } |
| 86 | static_assert(BcpCall(0), ""); |
| 87 | |
| 88 | // DR1311: A function template which can produce a constant expression, but |
| 89 | // for which a particular specialization cannot, is ok. |
| 90 | template<typename T> constexpr T cmin(T a, T b) { |
| 91 | return a < b ? a : b; |
| 92 | } |
| 93 | int n = cmin(3, 5); // ok |
| 94 | |
| 95 | struct X { |
| 96 | constexpr X() {} |
| 97 | bool operator<(X); // not constexpr |
| 98 | }; |
| 99 | |
| 100 | X x = cmin(X(), X()); // ok, not constexpr |
| 101 | |
| 102 | // Same with other temploids. |
| 103 | template<typename T> |
| 104 | struct Y { |
| 105 | constexpr Y() {} |
| 106 | constexpr int get() { return T(); } |
| 107 | #if __cplusplus < 201402L |
| 108 | // expected-warning@-2 {{C++14}} |
| 109 | #endif |
| 110 | }; |
| 111 | struct Z { operator int(); }; |
| 112 | |
| 113 | int y1 = Y<int>().get(); // ok |
| 114 | int y2 = Y<Z>().get(); // ok |
| 115 | |
| 116 | } |
| 117 | |
| 118 | #ifndef NO_INVALID_CONSTEXPR |
| 119 | namespace PR14550 { |
| 120 | // As an "extension", we allow functions which can't produce constant |
| 121 | // expressions to be declared constexpr in system headers (libstdc++ |
| 122 | // marks some functions as constexpr which use builtins which we don't |
| 123 | // support constant folding). Ensure that we don't mark those functions |
| 124 | // as invalid after suppressing the diagnostic. |
| 125 | # 126 "p5.cpp" 1 3 |
| 126 | int n; |
| 127 | struct A { |
| 128 | static constexpr int f() { return n; } |
| 129 | }; |
| 130 | template<typename T> struct B { |
| 131 | B() { g(T::f()); } // expected-error {{undeclared identifier 'g'}} |
| 132 | }; |
| 133 | # 134 "p5.cpp" 2 |
| 134 | template class B<A>; // expected-note {{here}} |
| 135 | } |
| 136 | #endif |
| 137 | |
| 138 | #if __cplusplus >= 201402L |
| 139 | constexpr void f() { throw; } // expected-error {{never produces}} expected-note {{subexpression}} |
| 140 | #endif |
| 141 | |