File: signed-char-bool-conversion.m

package info (click to toggle)
llvm-toolchain-14 1%3A14.0.6-16
  • links: PTS, VCS
  • area: main
  • in suites: sid
  • size: 1,496,368 kB
  • sloc: cpp: 5,593,980; ansic: 986,873; asm: 585,869; python: 184,223; objc: 72,530; lisp: 31,119; f90: 27,793; javascript: 9,780; pascal: 9,762; sh: 9,482; perl: 7,468; ml: 5,432; awk: 3,523; makefile: 2,547; xml: 953; cs: 573; fortran: 567
file content (122 lines) | stat: -rw-r--r-- 3,946 bytes parent folder | download | duplicates (8)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
// RUN: %clang_cc1 %s -verify -Wobjc-signed-char-bool
// RUN: %clang_cc1 -xobjective-c++ %s -verify -Wobjc-signed-char-bool

typedef signed char BOOL;
#define YES __objc_yes
#define NO __objc_no

typedef unsigned char Boolean;

BOOL b;
Boolean boolean;
float fl;
int i;
int *ptr;

void t1() {
  b = boolean;
  b = fl; // expected-warning {{implicit conversion from floating-point type 'float' to 'BOOL'}}
  b = i; // expected-warning {{implicit conversion from integral type 'int' to 'BOOL'}}

  b = 1.0;
  b = 0.0;
  b = 1.1; // expected-warning {{implicit conversion from 'double' to 'BOOL' (aka 'signed char') changes value from 1.1 to 1}}
  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}}

  b = YES;
#ifndef __cplusplus
  b = ptr; // expected-warning {{incompatible pointer to integer conversion assigning to 'BOOL' (aka 'signed char') from 'int *'}}
#endif
}

@interface BoolProp
@property BOOL p;
@end

void t2(BoolProp *bp) {
  bp.p = YES;
  bp.p = NO;
  bp.p = boolean;
  bp.p = fl; // expected-warning {{implicit conversion from floating-point type 'float' to 'BOOL'}}
  bp.p = i; // expected-warning {{implicit conversion from integral type 'int' to 'BOOL'}}
  bp.p = b;
  bp.p = bp.p;
#ifndef __cplusplus
  bp.p = ptr; // expected-warning {{incompatible pointer to integer conversion assigning to 'BOOL' (aka 'signed char') from 'int *'}}
#endif
  bp.p = 1;
  bp.p = 2; // expected-warning {{implicit conversion from constant value 2 to 'BOOL'; the only well defined values for 'BOOL' are YES and NO}}
}

struct has_bf {
  int signed_bf1 : 1;
  int signed_bf2 : 2;
  unsigned unsigned_bf1 : 1;
  unsigned unsigned_bf2 : 2;

  struct has_bf *nested;
};

void t3(struct has_bf *bf) {
  b = bf->signed_bf1; // expected-warning{{implicit conversion from integral type 'int' to 'BOOL'}}
  b = bf->signed_bf2; // expected-warning{{implicit conversion from integral type 'int' to 'BOOL'}}
  b = bf->unsigned_bf1; // no warning
  b = bf->unsigned_bf2; // expected-warning{{implicit conversion from integral type 'unsigned int' to 'BOOL'}}
  struct has_bf local;
  b = local.unsigned_bf1;
  b = local.unsigned_bf2; // expected-warning{{implicit conversion from integral type 'unsigned int' to 'BOOL'}}
  b = local.nested->unsigned_bf1;
  b = local.nested->unsigned_bf2; // expected-warning{{implicit conversion from integral type 'unsigned int' to 'BOOL'}}
}

void t4(BoolProp *bp) {
  BOOL local = YES;
  bp.p = 1 ? local : NO; // no warning
}

__attribute__((objc_root_class))
@interface BFIvar {
  struct has_bf bf;
  unsigned unsigned_bf1 : 1;
  unsigned unsigned_bf2 : 2;
}
@end

@implementation BFIvar
-(void)m {
  b = bf.unsigned_bf1;
  b = bf.unsigned_bf2; // expected-warning{{implicit conversion from integral type 'unsigned int' to 'BOOL'}}
  b = unsigned_bf1;
  b = unsigned_bf2; // expected-warning{{implicit conversion from integral type 'unsigned int' to 'BOOL'}}
}
@end

#ifdef __cplusplus
template <class T>
struct S {
  unsigned i : sizeof(T);
};

template <class T>
void f() {
  S<T> i;
  BOOL b = i.i; // expected-warning{{implicit conversion from integral type 'unsigned int' to 'BOOL'}}
}

int main() {
  f<char>();
  f<short>(); // expected-note {{in instantiation of function template specialization 'f<short>' requested here}}
}
#endif

void t5(BOOL b) {
  int i;
  b = b ?: YES; // no warning
  b = b ?: i; // expected-warning {{implicit conversion from integral type 'int' to 'BOOL'}}
  b = (b = i) // expected-warning {{implicit conversion from integral type 'int' to 'BOOL'}}
               ?: YES;
  b = (1 ? YES : i) ?: YES; // expected-warning {{implicit conversion from integral type 'int' to 'BOOL'}}
  b = b ?: (1 ? i : i); // expected-warning 2 {{implicit conversion from integral type 'int' to 'BOOL'}}

  b = b ? YES : (i ?: 0); // expected-warning {{implicit conversion from integral type 'int' to 'BOOL'}}
}