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