2017-03-04 02:02:02 +08:00
|
|
|
// RUN: %clang_analyze_cc1 -analyzer-checker=core,alpha.core.BoolAssignment -analyzer-store=region -verify -std=c99 -Dbool=_Bool %s
|
|
|
|
// RUN: %clang_analyze_cc1 -analyzer-checker=core,alpha.core.BoolAssignment -analyzer-store=region -verify -x c++ %s
|
2012-02-12 00:32:09 +08:00
|
|
|
|
2013-05-02 02:19:59 +08:00
|
|
|
// Test C++'s bool and C's _Bool.
|
|
|
|
// FIXME: We stopped warning on these when SValBuilder got smarter about
|
|
|
|
// casts to bool. Arguably, however, these conversions are okay; the result
|
|
|
|
// is always 'true' or 'false'.
|
2012-02-12 00:32:09 +08:00
|
|
|
|
2013-04-27 05:42:47 +08:00
|
|
|
void test_stdbool_initialization(int y) {
|
2013-05-02 02:19:59 +08:00
|
|
|
bool constant = 2; // no-warning
|
2012-02-12 00:32:09 +08:00
|
|
|
if (y < 0) {
|
2013-05-02 02:19:59 +08:00
|
|
|
bool x = y; // no-warning
|
2012-02-12 00:32:09 +08:00
|
|
|
return;
|
|
|
|
}
|
|
|
|
if (y > 1) {
|
2013-05-02 02:19:59 +08:00
|
|
|
bool x = y; // no-warning
|
2012-02-12 00:32:09 +08:00
|
|
|
return;
|
|
|
|
}
|
|
|
|
bool x = y; // no-warning
|
|
|
|
}
|
|
|
|
|
2013-04-27 05:42:47 +08:00
|
|
|
void test_stdbool_assignment(int y) {
|
2012-02-12 00:32:09 +08:00
|
|
|
bool x = 0; // no-warning
|
|
|
|
if (y < 0) {
|
2013-05-02 02:19:59 +08:00
|
|
|
x = y; // no-warning
|
2012-02-12 00:32:09 +08:00
|
|
|
return;
|
|
|
|
}
|
|
|
|
if (y > 1) {
|
2013-05-02 02:19:59 +08:00
|
|
|
x = y; // no-warning
|
2012-02-12 00:32:09 +08:00
|
|
|
return;
|
|
|
|
}
|
|
|
|
x = y; // no-warning
|
|
|
|
}
|
|
|
|
|
|
|
|
// Test Objective-C's BOOL
|
|
|
|
|
|
|
|
typedef signed char BOOL;
|
|
|
|
|
|
|
|
void test_BOOL_initialization(int y) {
|
2013-05-02 02:19:59 +08:00
|
|
|
BOOL constant = 2; // expected-warning {{Assignment of a non-Boolean value}}
|
2012-02-12 00:32:09 +08:00
|
|
|
if (y < 0) {
|
|
|
|
BOOL x = y; // expected-warning {{Assignment of a non-Boolean value}}
|
|
|
|
return;
|
|
|
|
}
|
2016-01-18 18:17:16 +08:00
|
|
|
if (y > 200 && y < 250) {
|
2017-03-04 02:02:02 +08:00
|
|
|
#ifdef ANALYZER_CM_Z3
|
|
|
|
BOOL x = y; // expected-warning {{Assignment of a non-Boolean value}}
|
|
|
|
#else
|
2016-01-18 18:17:16 +08:00
|
|
|
BOOL x = y; // no-warning
|
2017-03-04 02:02:02 +08:00
|
|
|
#endif
|
2016-01-18 18:17:16 +08:00
|
|
|
return;
|
|
|
|
}
|
|
|
|
if (y >= 127 && y < 150) {
|
|
|
|
BOOL x = y; // expected-warning{{Assignment of a non-Boolean value}}
|
|
|
|
return;
|
|
|
|
}
|
2012-02-12 00:32:09 +08:00
|
|
|
if (y > 1) {
|
|
|
|
BOOL x = y; // expected-warning {{Assignment of a non-Boolean value}}
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
BOOL x = y; // no-warning
|
|
|
|
}
|
|
|
|
|
|
|
|
void test_BOOL_assignment(int y) {
|
|
|
|
BOOL x = 0; // no-warning
|
|
|
|
if (y < 0) {
|
|
|
|
x = y; // expected-warning {{Assignment of a non-Boolean value}}
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
if (y > 1) {
|
|
|
|
x = y; // expected-warning {{Assignment of a non-Boolean value}}
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
x = y; // no-warning
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
// Test MacTypes.h's Boolean
|
|
|
|
|
|
|
|
typedef unsigned char Boolean;
|
|
|
|
|
|
|
|
void test_Boolean_initialization(int y) {
|
2013-05-02 02:19:59 +08:00
|
|
|
Boolean constant = 2; // expected-warning {{Assignment of a non-Boolean value}}
|
2012-02-12 00:32:09 +08:00
|
|
|
if (y < 0) {
|
|
|
|
Boolean x = y; // expected-warning {{Assignment of a non-Boolean value}}
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
if (y > 1) {
|
|
|
|
Boolean x = y; // expected-warning {{Assignment of a non-Boolean value}}
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
Boolean x = y; // no-warning
|
|
|
|
}
|
|
|
|
|
|
|
|
void test_Boolean_assignment(int y) {
|
|
|
|
Boolean x = 0; // no-warning
|
|
|
|
if (y < 0) {
|
|
|
|
x = y; // expected-warning {{Assignment of a non-Boolean value}}
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
if (y > 1) {
|
|
|
|
x = y; // expected-warning {{Assignment of a non-Boolean value}}
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
x = y; // no-warning
|
|
|
|
}
|