xref: /minix3/external/bsd/llvm/dist/clang/test/Analysis/bool-assignment.c (revision f4a2713ac843a11c696ec80c0a5e3e5d80b4d338)
1*f4a2713aSLionel Sambuc // RUN: %clang_cc1 -analyze -analyzer-checker=core,alpha.core.BoolAssignment -analyzer-store=region -verify -std=c99 -Dbool=_Bool %s
2*f4a2713aSLionel Sambuc // RUN: %clang_cc1 -analyze -analyzer-checker=core,alpha.core.BoolAssignment -analyzer-store=region -verify -x c++ %s
3*f4a2713aSLionel Sambuc 
4*f4a2713aSLionel Sambuc // Test C++'s bool and C's _Bool.
5*f4a2713aSLionel Sambuc // FIXME: We stopped warning on these when SValBuilder got smarter about
6*f4a2713aSLionel Sambuc // casts to bool. Arguably, however, these conversions are okay; the result
7*f4a2713aSLionel Sambuc // is always 'true' or 'false'.
8*f4a2713aSLionel Sambuc 
test_stdbool_initialization(int y)9*f4a2713aSLionel Sambuc void test_stdbool_initialization(int y) {
10*f4a2713aSLionel Sambuc   bool constant = 2; // no-warning
11*f4a2713aSLionel Sambuc   if (y < 0) {
12*f4a2713aSLionel Sambuc     bool x = y; // no-warning
13*f4a2713aSLionel Sambuc     return;
14*f4a2713aSLionel Sambuc   }
15*f4a2713aSLionel Sambuc   if (y > 1) {
16*f4a2713aSLionel Sambuc     bool x = y; // no-warning
17*f4a2713aSLionel Sambuc     return;
18*f4a2713aSLionel Sambuc   }
19*f4a2713aSLionel Sambuc   bool x = y; // no-warning
20*f4a2713aSLionel Sambuc }
21*f4a2713aSLionel Sambuc 
test_stdbool_assignment(int y)22*f4a2713aSLionel Sambuc void test_stdbool_assignment(int y) {
23*f4a2713aSLionel Sambuc   bool x = 0; // no-warning
24*f4a2713aSLionel Sambuc   if (y < 0) {
25*f4a2713aSLionel Sambuc     x = y; // no-warning
26*f4a2713aSLionel Sambuc     return;
27*f4a2713aSLionel Sambuc   }
28*f4a2713aSLionel Sambuc   if (y > 1) {
29*f4a2713aSLionel Sambuc     x = y; // no-warning
30*f4a2713aSLionel Sambuc     return;
31*f4a2713aSLionel Sambuc   }
32*f4a2713aSLionel Sambuc   x = y; // no-warning
33*f4a2713aSLionel Sambuc }
34*f4a2713aSLionel Sambuc 
35*f4a2713aSLionel Sambuc // Test Objective-C's BOOL
36*f4a2713aSLionel Sambuc 
37*f4a2713aSLionel Sambuc typedef signed char BOOL;
38*f4a2713aSLionel Sambuc 
test_BOOL_initialization(int y)39*f4a2713aSLionel Sambuc void test_BOOL_initialization(int y) {
40*f4a2713aSLionel Sambuc   BOOL constant = 2; // expected-warning {{Assignment of a non-Boolean value}}
41*f4a2713aSLionel Sambuc   if (y < 0) {
42*f4a2713aSLionel Sambuc     BOOL x = y; // expected-warning {{Assignment of a non-Boolean value}}
43*f4a2713aSLionel Sambuc     return;
44*f4a2713aSLionel Sambuc   }
45*f4a2713aSLionel Sambuc   if (y > 1) {
46*f4a2713aSLionel Sambuc     BOOL x = y; // expected-warning {{Assignment of a non-Boolean value}}
47*f4a2713aSLionel Sambuc     return;
48*f4a2713aSLionel Sambuc   }
49*f4a2713aSLionel Sambuc   BOOL x = y; // no-warning
50*f4a2713aSLionel Sambuc }
51*f4a2713aSLionel Sambuc 
test_BOOL_assignment(int y)52*f4a2713aSLionel Sambuc void test_BOOL_assignment(int y) {
53*f4a2713aSLionel Sambuc   BOOL x = 0; // no-warning
54*f4a2713aSLionel Sambuc   if (y < 0) {
55*f4a2713aSLionel Sambuc     x = y; // expected-warning {{Assignment of a non-Boolean value}}
56*f4a2713aSLionel Sambuc     return;
57*f4a2713aSLionel Sambuc   }
58*f4a2713aSLionel Sambuc   if (y > 1) {
59*f4a2713aSLionel Sambuc     x = y; // expected-warning {{Assignment of a non-Boolean value}}
60*f4a2713aSLionel Sambuc     return;
61*f4a2713aSLionel Sambuc   }
62*f4a2713aSLionel Sambuc   x = y; // no-warning
63*f4a2713aSLionel Sambuc }
64*f4a2713aSLionel Sambuc 
65*f4a2713aSLionel Sambuc 
66*f4a2713aSLionel Sambuc // Test MacTypes.h's Boolean
67*f4a2713aSLionel Sambuc 
68*f4a2713aSLionel Sambuc typedef unsigned char Boolean;
69*f4a2713aSLionel Sambuc 
test_Boolean_initialization(int y)70*f4a2713aSLionel Sambuc void test_Boolean_initialization(int y) {
71*f4a2713aSLionel Sambuc   Boolean constant = 2; // expected-warning {{Assignment of a non-Boolean value}}
72*f4a2713aSLionel Sambuc   if (y < 0) {
73*f4a2713aSLionel Sambuc     Boolean x = y; // expected-warning {{Assignment of a non-Boolean value}}
74*f4a2713aSLionel Sambuc     return;
75*f4a2713aSLionel Sambuc   }
76*f4a2713aSLionel Sambuc   if (y > 1) {
77*f4a2713aSLionel Sambuc     Boolean x = y; // expected-warning {{Assignment of a non-Boolean value}}
78*f4a2713aSLionel Sambuc     return;
79*f4a2713aSLionel Sambuc   }
80*f4a2713aSLionel Sambuc   Boolean x = y; // no-warning
81*f4a2713aSLionel Sambuc }
82*f4a2713aSLionel Sambuc 
test_Boolean_assignment(int y)83*f4a2713aSLionel Sambuc void test_Boolean_assignment(int y) {
84*f4a2713aSLionel Sambuc   Boolean x = 0; // no-warning
85*f4a2713aSLionel Sambuc   if (y < 0) {
86*f4a2713aSLionel Sambuc     x = y; // expected-warning {{Assignment of a non-Boolean value}}
87*f4a2713aSLionel Sambuc     return;
88*f4a2713aSLionel Sambuc   }
89*f4a2713aSLionel Sambuc   if (y > 1) {
90*f4a2713aSLionel Sambuc     x = y; // expected-warning {{Assignment of a non-Boolean value}}
91*f4a2713aSLionel Sambuc     return;
92*f4a2713aSLionel Sambuc   }
93*f4a2713aSLionel Sambuc   x = y; // no-warning
94*f4a2713aSLionel Sambuc }
95