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 5namespace StdExample { 6 7constexpr int f(void *) { return 0; } 8constexpr int f(...) { return 1; } 9constexpr int g1() { return f(0); } 10constexpr int g2(int n) { return f(n); } 11constexpr int g3(int n) { return f(n*0); } 12 13namespace N { 14 constexpr int c = 5; 15 constexpr int h() { return c; } 16} 17constexpr int c = 0; 18constexpr int g4() { return N::h(); } 19 20static_assert(f(0) == 0, ""); 21static_assert(f('0') == 1, ""); 22static_assert(g1() == 0, ""); 23static_assert(g2(0) == 1, ""); 24static_assert(g2(1) == 1, ""); 25static_assert(g3(0) == 1, ""); 26static_assert(g3(1) == 1, ""); 27static_assert(N::h() == 5, ""); 28static_assert(g4() == 5, ""); 29 30 31constexpr int f(bool b) 32 { return b ? throw 0 : 0; } // ok 33constexpr int f() { return throw 0, 0; } // expected-error {{constexpr function never produces a constant expression}} expected-note {{subexpression}} 34 35struct B { 36 constexpr B(int x) : i(0) { } 37 int i; 38}; 39 40int global; // expected-note {{declared here}} 41 42struct 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 48namespace PotentialConstant { 49 50constexpr 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 56int ng; // expected-note 6{{here}} 57constexpr int BinaryOp1(int n) { return n + ng; } // expected-error {{never produces}} expected-note {{read}} 58constexpr int BinaryOp2(int n) { return ng + n; } // expected-error {{never produces}} expected-note {{read}} 59 60double dg; // expected-note 2{{here}} 61constexpr double BinaryOp1(double d) { return d + dg; } // expected-error {{never produces}} expected-note {{read}} 62constexpr double BinaryOp2(double d) { return dg + d; } // expected-error {{never produces}} expected-note {{read}} 63 64constexpr int Add(int a, int b, int c) { return a + b + c; } 65constexpr int FunctionArgs(int a) { return Add(a, ng, a); } // expected-error {{never produces}} expected-note {{read}} 66 67struct S { int a; int b; int c[2]; }; 68constexpr S InitList(int a) { return { a, ng }; }; // expected-error {{never produces}} expected-note {{read}} 69constexpr S InitList1a(int a) { return S{ a, ng }; }; // expected-error {{never produces}} expected-note {{read}} 70constexpr S InitList2(int a) { return { a, a, { ng } }; }; // expected-error {{never produces}} expected-note {{read}} 71constexpr S InitList3(int a) { return a ? S{ a, a } : S{ a, ng }; }; // ok 72 73constexpr int LogicalAnd1(int n) { return n && (throw, 0); } // ok 74constexpr int LogicalAnd2(int n) { return 1 && (throw, 0); } // expected-error {{never produces}} expected-note {{subexpression}} 75 76constexpr int LogicalOr1(int n) { return n || (throw, 0); } // ok 77constexpr int LogicalOr2(int n) { return 0 || (throw, 0); } // expected-error {{never produces}} expected-note {{subexpression}} 78 79constexpr int Conditional1(bool b, int n) { return b ? n : ng; } // ok 80constexpr 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. 83constexpr 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} 86static_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. 90template<typename T> constexpr T cmin(T a, T b) { 91 return a < b ? a : b; 92} 93int n = cmin(3, 5); // ok 94 95struct X { 96 constexpr X() {} 97 bool operator<(X); // not constexpr 98}; 99 100X x = cmin(X(), X()); // ok, not constexpr 101 102// Same with other temploids. 103template<typename T> 104struct Y { 105 constexpr Y() {} 106 constexpr int get() { return T(); } 107#if __cplusplus < 201402L 108 // expected-warning@-2 {{C++14}} 109#endif 110}; 111struct Z { operator int(); }; 112 113int y1 = Y<int>().get(); // ok 114int y2 = Y<Z>().get(); // ok 115 116} 117 118#ifndef NO_INVALID_CONSTEXPR 119namespace 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 139constexpr void f() { throw; } // expected-error {{never produces}} expected-note {{subexpression}} 140#endif 141