xref: /llvm-project/clang/test/AST/ByteCode/if.cpp (revision acb7dfaa017dc11106bf97de4e8bf72d47ff54a8)
1 // RUN: %clang_cc1 -std=c++23 -fsyntax-only -fexperimental-new-constant-interpreter %s -verify=expected,both
2 // RUN: %clang_cc1 -std=c++23 -fsyntax-only %s -verify=ref,both
3 
4 namespace ConstEval {
5   constexpr int f() {
6     int i = 0;
7     if consteval {
8       i = 1;
9     }
10     return i;
11   }
12   static_assert(f() == 1, "");
13 
14   constexpr int f2() {
15     int i = 0;
16     if !consteval {
17         i = 12;
18       if consteval {
19         i = i + 1;
20       }
21     }
22     return i;
23   }
24   static_assert(f2() == 0, "");
25 };
26 
27 namespace InitDecl {
28   constexpr bool f() {
29     if (int i = 5; i != 10) {
30       return true;
31     }
32     return false;
33   }
34   static_assert(f(), "");
35 
36   constexpr bool f2() {
37     if (bool b = false; b) {
38       return true;
39     }
40     return false;
41   }
42   static_assert(!f2(), "");
43 
44 
45   constexpr int attrs() {
46     if (1) [[likely]] {}
47     return 1;
48   }
49   static_assert(attrs() == 1, "");
50 };
51 
52 /// The faulty if statement creates a RecoveryExpr with contains-errors,
53 /// but the execution will never reach that.
54 constexpr char g(char const (&x)[2]) {
55     return 'x';
56   if (auto [a, b] = x) // both-error {{an array type is not allowed here}} \
57                        // both-warning {{ISO C++17 does not permit structured binding declaration in a condition}}
58     ;
59 }
60 static_assert(g("x") == 'x');
61 
62 namespace IfScope {
63   struct Inc {
64     int &a;
65     constexpr Inc(int &a) : a(a) {}
66     constexpr ~Inc() { ++a; }
67   };
68 
69   constexpr int foo() {
70     int a= 0;
71     int b = 12;
72     if (Inc{a}; true) {
73       b += a;
74     }
75     return b;
76   }
77   static_assert(foo() == 13, "");
78 }
79 
80 namespace IfScope2 {
81   struct __bit_iterator {
82     unsigned __ctz_;
83   };
84   constexpr void __fill_n_bool(__bit_iterator) {}
85 
86   constexpr void fill_n(__bit_iterator __first) {
87     if (false)
88       __fill_n_bool(__first);
89     else
90       __fill_n_bool(__first);
91   }
92 
93   struct bitset{
94     constexpr void reset() {
95       auto m = __bit_iterator(8);
96       fill_n(m);
97     }
98   };
99   consteval bool foo() {
100     bitset v;
101     v.reset();
102     return true;
103   }
104   static_assert(foo());
105 }
106