1 // RUN: %clang_cc1 -fsyntax-only -verify %s
2 extern int global;
3 
4 struct S {
5   static constexpr float prob = 0.7;
6 };
7 
8 template<typename T>
9 void expect_taken(int x) {
10   if (__builtin_expect_with_probability(x > 0, 1, T::prob)) {
11     global++;
12   }
13 }
14 
15 void test(int x, double p) { // expected-note {{declared here}}
16   bool dummy;
17   dummy = __builtin_expect_with_probability(x > 0, 1, 0.9);
18   dummy = __builtin_expect_with_probability(x > 0, 1, 1.1); // expected-error {{probability argument to __builtin_expect_with_probability is outside the range [0.0, 1.0]}}
19   dummy = __builtin_expect_with_probability(x > 0, 1, -1); // expected-error {{probability argument to __builtin_expect_with_probability is outside the range [0.0, 1.0]}}
20   dummy = __builtin_expect_with_probability(x > 0, 1, p); // expected-error {{probability argument to __builtin_expect_with_probability must be constant floating-point expression}} expected-note {{read of non-constexpr variable 'p' is not allowed in a constant expression}}
21   dummy = __builtin_expect_with_probability(x > 0, 1, "aa"); // expected-error {{cannot initialize a parameter of type 'double' with an lvalue of type 'const char [3]'}}
22   dummy = __builtin_expect_with_probability(x > 0, 1, __builtin_nan("")); // expected-error {{probability argument to __builtin_expect_with_probability is outside the range [0.0, 1.0]}}
23   dummy = __builtin_expect_with_probability(x > 0, 1, __builtin_inf()); // expected-error {{probability argument to __builtin_expect_with_probability is outside the range [0.0, 1.0]}}
24   dummy = __builtin_expect_with_probability(x > 0, 1, -0.0);
25   dummy = __builtin_expect_with_probability(x > 0, 1, 1.0 + __DBL_EPSILON__); // expected-error {{probability argument to __builtin_expect_with_probability is outside the range [0.0, 1.0]}}
26   dummy = __builtin_expect_with_probability(x > 0, 1, -__DBL_DENORM_MIN__); // expected-error {{probability argument to __builtin_expect_with_probability is outside the range [0.0, 1.0]}}
27   constexpr double pd = 0.7;
28   dummy = __builtin_expect_with_probability(x > 0, 1, pd);
29   constexpr int pi = 1;
30   dummy = __builtin_expect_with_probability(x > 0, 1, pi);
31   expect_taken<S>(x);
32 }
33