1be0ee875d8a91c031a085cbbd73ad9e8dc1aa8ffDavid Blaikie// RUN: %clang_cc1 -fsyntax-only -verify -Wno-constant-conversion %s
2f58443e4740539ef211f4da01be2160b2c91e1c3Richard Trieu// RUN: %clang_cc1 -fsyntax-only -verify -Wno-constant-conversion \
3f58443e4740539ef211f4da01be2160b2c91e1c3Richard Trieu// RUN:     -Wno-deprecated -Wdeprecated-increment-bool %s
4b88d45ea7eb835d36c4a4b3ea84b1260b120dd0aAnders Carlsson
5b88d45ea7eb835d36c4a4b3ea84b1260b120dd0aAnders Carlsson// Bool literals can be enum values.
6b88d45ea7eb835d36c4a4b3ea84b1260b120dd0aAnders Carlssonenum {
7b88d45ea7eb835d36c4a4b3ea84b1260b120dd0aAnders Carlsson  ReadWrite = false,
8b88d45ea7eb835d36c4a4b3ea84b1260b120dd0aAnders Carlsson  ReadOnly = true
9b88d45ea7eb835d36c4a4b3ea84b1260b120dd0aAnders Carlsson};
10e6d5a4a58346441c969d5fcc7aa053e029186f86Sebastian Redl
11e6d5a4a58346441c969d5fcc7aa053e029186f86Sebastian Redl// bool cannot be decremented, and gives a warning on increment
12e6d5a4a58346441c969d5fcc7aa053e029186f86Sebastian Redlvoid test(bool b)
13e6d5a4a58346441c969d5fcc7aa053e029186f86Sebastian Redl{
14e6d5a4a58346441c969d5fcc7aa053e029186f86Sebastian Redl  ++b; // expected-warning {{incrementing expression of type bool is deprecated}}
15e6d5a4a58346441c969d5fcc7aa053e029186f86Sebastian Redl  b++; // expected-warning {{incrementing expression of type bool is deprecated}}
16e6d5a4a58346441c969d5fcc7aa053e029186f86Sebastian Redl  --b; // expected-error {{cannot decrement expression of type bool}}
17e6d5a4a58346441c969d5fcc7aa053e029186f86Sebastian Redl  b--; // expected-error {{cannot decrement expression of type bool}}
18d249e1d1f1498b81314459ceda19d6ff25c278adDouglas Gregor
19cfdc81a83467973b14e4ea5e9e9af1690f135415Eli Friedman  bool *b1 = (int *)0; // expected-error{{cannot initialize}}
20e6d5a4a58346441c969d5fcc7aa053e029186f86Sebastian Redl}
2104905014fd854979ac12fc31a2b25fca37a93eb4Anders Carlsson
2204905014fd854979ac12fc31a2b25fca37a93eb4Anders Carlsson// static_assert_arg_is_bool(x) compiles only if x is a bool.
2304905014fd854979ac12fc31a2b25fca37a93eb4Anders Carlssontemplate <typename T>
2404905014fd854979ac12fc31a2b25fca37a93eb4Anders Carlssonvoid static_assert_arg_is_bool(T x) {
2504905014fd854979ac12fc31a2b25fca37a93eb4Anders Carlsson  bool* p = &x;
2604905014fd854979ac12fc31a2b25fca37a93eb4Anders Carlsson}
2704905014fd854979ac12fc31a2b25fca37a93eb4Anders Carlsson
2804905014fd854979ac12fc31a2b25fca37a93eb4Anders Carlssonvoid test2() {
2904905014fd854979ac12fc31a2b25fca37a93eb4Anders Carlsson  int n = 2;
309b127f3b44e685cbe513595b7e0115b0884b0604Matt Beaumont-Gay  static_assert_arg_is_bool(n && 4);  // expected-warning {{use of logical '&&' with constant operand}} \
319b127f3b44e685cbe513595b7e0115b0884b0604Matt Beaumont-Gay                                      // expected-note {{use '&' for a bitwise operation}} \
329b127f3b44e685cbe513595b7e0115b0884b0604Matt Beaumont-Gay                                      // expected-note {{remove constant to silence this warning}}
339b127f3b44e685cbe513595b7e0115b0884b0604Matt Beaumont-Gay  static_assert_arg_is_bool(n || 5);  // expected-warning {{use of logical '||' with constant operand}} \
349b127f3b44e685cbe513595b7e0115b0884b0604Matt Beaumont-Gay                                      // expected-note {{use '|' for a bitwise operation}}
3504905014fd854979ac12fc31a2b25fca37a93eb4Anders Carlsson}
36