• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1// RUN: %clang_cc1 %s -verify -Wobjc-signed-char-bool
2// RUN: %clang_cc1 -xobjective-c++ %s -verify -Wobjc-signed-char-bool
3
4typedef signed char BOOL;
5#define YES __objc_yes
6#define NO __objc_no
7
8typedef unsigned char Boolean;
9
10BOOL b;
11Boolean boolean;
12float fl;
13int i;
14int *ptr;
15
16void t1() {
17  b = boolean;
18  b = fl; // expected-warning {{implicit conversion from floating-point type 'float' to 'BOOL'}}
19  b = i; // expected-warning {{implicit conversion from integral type 'int' to 'BOOL'}}
20
21  b = 1.0;
22  b = 0.0;
23  b = 1.1; // expected-warning {{implicit conversion from 'double' to 'BOOL' (aka 'signed char') changes value from 1.1 to 1}}
24  b = 2.1; // expected-warning {{implicit conversion from constant value 2.1 to 'BOOL'; the only well defined values for 'BOOL' are YES and NO}}
25
26  b = YES;
27#ifndef __cplusplus
28  b = ptr; // expected-warning {{incompatible pointer to integer conversion assigning to 'BOOL' (aka 'signed char') from 'int *'}}
29#endif
30}
31
32@interface BoolProp
33@property BOOL p;
34@end
35
36void t2(BoolProp *bp) {
37  bp.p = YES;
38  bp.p = NO;
39  bp.p = boolean;
40  bp.p = fl; // expected-warning {{implicit conversion from floating-point type 'float' to 'BOOL'}}
41  bp.p = i; // expected-warning {{implicit conversion from integral type 'int' to 'BOOL'}}
42  bp.p = b;
43  bp.p = bp.p;
44#ifndef __cplusplus
45  bp.p = ptr; // expected-warning {{incompatible pointer to integer conversion assigning to 'BOOL' (aka 'signed char') from 'int *'}}
46#endif
47  bp.p = 1;
48  bp.p = 2; // expected-warning {{implicit conversion from constant value 2 to 'BOOL'; the only well defined values for 'BOOL' are YES and NO}}
49}
50
51struct has_bf {
52  int signed_bf1 : 1;
53  int signed_bf2 : 2;
54  unsigned unsigned_bf1 : 1;
55  unsigned unsigned_bf2 : 2;
56
57  struct has_bf *nested;
58};
59
60void t3(struct has_bf *bf) {
61  b = bf->signed_bf1; // expected-warning{{implicit conversion from integral type 'int' to 'BOOL'}}
62  b = bf->signed_bf2; // expected-warning{{implicit conversion from integral type 'int' to 'BOOL'}}
63  b = bf->unsigned_bf1; // no warning
64  b = bf->unsigned_bf2; // expected-warning{{implicit conversion from integral type 'unsigned int' to 'BOOL'}}
65  struct has_bf local;
66  b = local.unsigned_bf1;
67  b = local.unsigned_bf2; // expected-warning{{implicit conversion from integral type 'unsigned int' to 'BOOL'}}
68  b = local.nested->unsigned_bf1;
69  b = local.nested->unsigned_bf2; // expected-warning{{implicit conversion from integral type 'unsigned int' to 'BOOL'}}
70}
71
72void t4(BoolProp *bp) {
73  BOOL local = YES;
74  bp.p = 1 ? local : NO; // no warning
75}
76
77__attribute__((objc_root_class))
78@interface BFIvar {
79  struct has_bf bf;
80  unsigned unsigned_bf1 : 1;
81  unsigned unsigned_bf2 : 2;
82}
83@end
84
85@implementation BFIvar
86-(void)m {
87  b = bf.unsigned_bf1;
88  b = bf.unsigned_bf2; // expected-warning{{implicit conversion from integral type 'unsigned int' to 'BOOL'}}
89  b = unsigned_bf1;
90  b = unsigned_bf2; // expected-warning{{implicit conversion from integral type 'unsigned int' to 'BOOL'}}
91}
92@end
93
94#ifdef __cplusplus
95template <class T>
96struct S {
97  unsigned i : sizeof(T);
98};
99
100template <class T>
101void f() {
102  S<T> i;
103  BOOL b = i.i; // expected-warning{{implicit conversion from integral type 'unsigned int' to 'BOOL'}}
104}
105
106int main() {
107  f<char>();
108  f<short>(); // expected-note {{in instantiation of function template specialization 'f<short>' requested here}}
109}
110#endif
111
112void t5(BOOL b) {
113  int i;
114  b = b ?: YES; // no warning
115  b = b ?: i; // expected-warning {{implicit conversion from integral type 'int' to 'BOOL'}}
116  b = (b = i) // expected-warning {{implicit conversion from integral type 'int' to 'BOOL'}}
117               ?: YES;
118  b = (1 ? YES : i) ?: YES; // expected-warning {{implicit conversion from integral type 'int' to 'BOOL'}}
119  b = b ?: (1 ? i : i); // expected-warning 2 {{implicit conversion from integral type 'int' to 'BOOL'}}
120
121  b = b ? YES : (i ?: 0); // expected-warning {{implicit conversion from integral type 'int' to 'BOOL'}}
122}
123