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