• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1 // RUN: %clang_cc1 -analyze -analyzer-checker=core,alpha.core.BoolAssignment -analyzer-store=region -verify -std=c99 -Dbool=_Bool %s
2 // RUN: %clang_cc1 -analyze -analyzer-checker=core,alpha.core.BoolAssignment -analyzer-store=region -verify -x c++ %s
3 
4 // 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'.
8 
test_stdbool_initialization(int y)9 void test_stdbool_initialization(int y) {
10   bool constant = 2; // no-warning
11   if (y < 0) {
12     bool x = y; // no-warning
13     return;
14   }
15   if (y > 1) {
16     bool x = y; // no-warning
17     return;
18   }
19   bool x = y; // no-warning
20 }
21 
test_stdbool_assignment(int y)22 void test_stdbool_assignment(int y) {
23   bool x = 0; // no-warning
24   if (y < 0) {
25     x = y; // no-warning
26     return;
27   }
28   if (y > 1) {
29     x = y; // no-warning
30     return;
31   }
32   x = y; // no-warning
33 }
34 
35 // Test Objective-C's BOOL
36 
37 typedef signed char BOOL;
38 
test_BOOL_initialization(int y)39 void test_BOOL_initialization(int y) {
40   BOOL constant = 2; // expected-warning {{Assignment of a non-Boolean value}}
41   if (y < 0) {
42     BOOL x = y; // expected-warning {{Assignment of a non-Boolean value}}
43     return;
44   }
45   if (y > 1) {
46     BOOL x = y; // expected-warning {{Assignment of a non-Boolean value}}
47     return;
48   }
49   BOOL x = y; // no-warning
50 }
51 
test_BOOL_assignment(int y)52 void test_BOOL_assignment(int y) {
53   BOOL x = 0; // no-warning
54   if (y < 0) {
55     x = y; // expected-warning {{Assignment of a non-Boolean value}}
56     return;
57   }
58   if (y > 1) {
59     x = y; // expected-warning {{Assignment of a non-Boolean value}}
60     return;
61   }
62   x = y; // no-warning
63 }
64 
65 
66 // Test MacTypes.h's Boolean
67 
68 typedef unsigned char Boolean;
69 
test_Boolean_initialization(int y)70 void test_Boolean_initialization(int y) {
71   Boolean constant = 2; // expected-warning {{Assignment of a non-Boolean value}}
72   if (y < 0) {
73     Boolean x = y; // expected-warning {{Assignment of a non-Boolean value}}
74     return;
75   }
76   if (y > 1) {
77     Boolean x = y; // expected-warning {{Assignment of a non-Boolean value}}
78     return;
79   }
80   Boolean x = y; // no-warning
81 }
82 
test_Boolean_assignment(int y)83 void test_Boolean_assignment(int y) {
84   Boolean x = 0; // no-warning
85   if (y < 0) {
86     x = y; // expected-warning {{Assignment of a non-Boolean value}}
87     return;
88   }
89   if (y > 1) {
90     x = y; // expected-warning {{Assignment of a non-Boolean value}}
91     return;
92   }
93   x = y; // no-warning
94 }
95