xref: /aosp_15_r20/external/clang/test/SemaCXX/bool.cpp (revision 67e74705e28f6214e480b399dd47ea732279e315)
1*67e74705SXin Li // RUN: %clang_cc1 -fsyntax-only -verify -Wno-constant-conversion %s
2*67e74705SXin Li // RUN: %clang_cc1 -fsyntax-only -verify -Wno-constant-conversion \
3*67e74705SXin Li // RUN:     -Wno-deprecated -Wdeprecated-increment-bool %s
4*67e74705SXin Li 
5*67e74705SXin Li // Bool literals can be enum values.
6*67e74705SXin Li enum {
7*67e74705SXin Li   ReadWrite = false,
8*67e74705SXin Li   ReadOnly = true
9*67e74705SXin Li };
10*67e74705SXin Li 
11*67e74705SXin Li // bool cannot be decremented, and gives a warning on increment
test(bool b)12*67e74705SXin Li void test(bool b)
13*67e74705SXin Li {
14*67e74705SXin Li   ++b; // expected-warning {{incrementing expression of type bool is deprecated}}
15*67e74705SXin Li   b++; // expected-warning {{incrementing expression of type bool is deprecated}}
16*67e74705SXin Li   --b; // expected-error {{cannot decrement expression of type bool}}
17*67e74705SXin Li   b--; // expected-error {{cannot decrement expression of type bool}}
18*67e74705SXin Li 
19*67e74705SXin Li   bool *b1 = (int *)0; // expected-error{{cannot initialize}}
20*67e74705SXin Li }
21*67e74705SXin Li 
22*67e74705SXin Li // static_assert_arg_is_bool(x) compiles only if x is a bool.
23*67e74705SXin Li template <typename T>
static_assert_arg_is_bool(T x)24*67e74705SXin Li void static_assert_arg_is_bool(T x) {
25*67e74705SXin Li   bool* p = &x;
26*67e74705SXin Li }
27*67e74705SXin Li 
test2()28*67e74705SXin Li void test2() {
29*67e74705SXin Li   int n = 2;
30*67e74705SXin Li   static_assert_arg_is_bool(n && 4);  // expected-warning {{use of logical '&&' with constant operand}} \
31*67e74705SXin Li                                       // expected-note {{use '&' for a bitwise operation}} \
32*67e74705SXin Li                                       // expected-note {{remove constant to silence this warning}}
33*67e74705SXin Li   static_assert_arg_is_bool(n || 5);  // expected-warning {{use of logical '||' with constant operand}} \
34*67e74705SXin Li                                       // expected-note {{use '|' for a bitwise operation}}
35*67e74705SXin Li }
36