xref: /llvm-project/clang/test/SemaCXX/constant-conversion.cpp (revision 880fa7faa97bad63e403c924263b01fb81783227)
1 // RUN: %clang_cc1 -fsyntax-only -verify -triple x86_64-apple-darwin %s
2 
3 // This file tests -Wconstant-conversion, a subcategory of -Wconversion
4 // which is on by default.
5 
nines()6 constexpr int nines() { return 99999; }
7 
too_big_for_char(int param)8 void too_big_for_char(int param) {
9   char warn1 = false ? 0 : 99999;
10   // expected-warning@-1 {{implicit conversion from 'int' to 'char' changes value from 99999 to -97}}
11   char warn2 = false ? 0 : nines();
12   // expected-warning@-1 {{implicit conversion from 'int' to 'char' changes value from 99999 to -97}}
13 
14   char warn3 = param > 0 ? 0 : 99999;
15   // expected-warning@-1 {{implicit conversion from 'int' to 'char' changes value from 99999 to -97}}
16   char warn4 = param > 0 ? 0 : nines();
17   // expected-warning@-1 {{implicit conversion from 'int' to 'char' changes value from 99999 to -97}}
18 
19   char ok1 = true ? 0 : 99999;
20   char ok2 = true ? 0 : nines();
21 
22   char ok3 = true ? 0 : 99999 + 1;
23   char ok4 = true ? 0 : nines() + 1;
24 }
25 
test_bitfield()26 void test_bitfield() {
27   struct S {
28     int one_bit : 1;
29   } s;
30 
31   s.one_bit = 1;    // expected-warning {{implicit truncation from 'int' to a one-bit wide bit-field changes value from 1 to -1}}
32   s.one_bit = true; // no-warning
33 }
34