blob: 57a7f0b5dc1b0ab8241bca96f16068de3238afde [file] [log] [blame]
Dominic Chen184c6242017-03-03 18:02:02 +00001// RUN: %clang_analyze_cc1 -analyzer-checker=core,alpha.core.BoolAssignment -analyzer-store=region -verify -std=c99 -Dbool=_Bool %s
2// RUN: %clang_analyze_cc1 -analyzer-checker=core,alpha.core.BoolAssignment -analyzer-store=region -verify -x c++ %s
Ryan Govostes55011c02012-02-11 16:32:09 +00003
Jordan Rosedc166282013-05-01 18:19:59 +00004// Test C++'s bool and C's _Bool.
5// FIXME: We stopped warning on these when SValBuilder got smarter about
6// casts to bool. Arguably, however, these conversions are okay; the result
7// is always 'true' or 'false'.
Ryan Govostes55011c02012-02-11 16:32:09 +00008
Jordan Rose05411592013-04-26 21:42:47 +00009void test_stdbool_initialization(int y) {
Jordan Rosedc166282013-05-01 18:19:59 +000010 bool constant = 2; // no-warning
Ryan Govostes55011c02012-02-11 16:32:09 +000011 if (y < 0) {
Jordan Rosedc166282013-05-01 18:19:59 +000012 bool x = y; // no-warning
Ryan Govostes55011c02012-02-11 16:32:09 +000013 return;
14 }
15 if (y > 1) {
Jordan Rosedc166282013-05-01 18:19:59 +000016 bool x = y; // no-warning
Ryan Govostes55011c02012-02-11 16:32:09 +000017 return;
18 }
19 bool x = y; // no-warning
20}
21
Jordan Rose05411592013-04-26 21:42:47 +000022void test_stdbool_assignment(int y) {
Ryan Govostes55011c02012-02-11 16:32:09 +000023 bool x = 0; // no-warning
24 if (y < 0) {
Jordan Rosedc166282013-05-01 18:19:59 +000025 x = y; // no-warning
Ryan Govostes55011c02012-02-11 16:32:09 +000026 return;
27 }
28 if (y > 1) {
Jordan Rosedc166282013-05-01 18:19:59 +000029 x = y; // no-warning
Ryan Govostes55011c02012-02-11 16:32:09 +000030 return;
31 }
32 x = y; // no-warning
33}
34
35// Test Objective-C's BOOL
36
37typedef signed char BOOL;
38
39void test_BOOL_initialization(int y) {
Jordan Rosedc166282013-05-01 18:19:59 +000040 BOOL constant = 2; // expected-warning {{Assignment of a non-Boolean value}}
Ryan Govostes55011c02012-02-11 16:32:09 +000041 if (y < 0) {
42 BOOL x = y; // expected-warning {{Assignment of a non-Boolean value}}
43 return;
44 }
Artem Dergachev91c45e82016-01-18 10:17:16 +000045 if (y > 200 && y < 250) {
Dominic Chen184c6242017-03-03 18:02:02 +000046#ifdef ANALYZER_CM_Z3
47 BOOL x = y; // expected-warning {{Assignment of a non-Boolean value}}
48#else
Artem Dergachev91c45e82016-01-18 10:17:16 +000049 BOOL x = y; // no-warning
Dominic Chen184c6242017-03-03 18:02:02 +000050#endif
Artem Dergachev91c45e82016-01-18 10:17:16 +000051 return;
52 }
53 if (y >= 127 && y < 150) {
54 BOOL x = y; // expected-warning{{Assignment of a non-Boolean value}}
55 return;
56 }
Ryan Govostes55011c02012-02-11 16:32:09 +000057 if (y > 1) {
58 BOOL x = y; // expected-warning {{Assignment of a non-Boolean value}}
59 return;
60 }
61 BOOL x = y; // no-warning
62}
63
64void test_BOOL_assignment(int y) {
65 BOOL x = 0; // no-warning
66 if (y < 0) {
67 x = y; // expected-warning {{Assignment of a non-Boolean value}}
68 return;
69 }
70 if (y > 1) {
71 x = y; // expected-warning {{Assignment of a non-Boolean value}}
72 return;
73 }
74 x = y; // no-warning
75}
76
77
78// Test MacTypes.h's Boolean
79
80typedef unsigned char Boolean;
81
82void test_Boolean_initialization(int y) {
Jordan Rosedc166282013-05-01 18:19:59 +000083 Boolean constant = 2; // expected-warning {{Assignment of a non-Boolean value}}
Ryan Govostes55011c02012-02-11 16:32:09 +000084 if (y < 0) {
85 Boolean x = y; // expected-warning {{Assignment of a non-Boolean value}}
86 return;
87 }
88 if (y > 1) {
89 Boolean x = y; // expected-warning {{Assignment of a non-Boolean value}}
90 return;
91 }
92 Boolean x = y; // no-warning
93}
94
95void test_Boolean_assignment(int y) {
96 Boolean x = 0; // no-warning
97 if (y < 0) {
98 x = y; // expected-warning {{Assignment of a non-Boolean value}}
99 return;
100 }
101 if (y > 1) {
102 x = y; // expected-warning {{Assignment of a non-Boolean value}}
103 return;
104 }
105 x = y; // no-warning
106}