xref: /minix3/external/bsd/llvm/dist/clang/test/Analysis/test-after-div-zero.c (revision 0a6a1f1d05b60e214de2f05a7310ddd1f0e590e7)
1*0a6a1f1dSLionel Sambuc // RUN: %clang_cc1 -std=c99 -Dbool=_Bool -analyze -analyzer-checker=core,alpha.core.TestAfterDivZero -analyzer-output=text -verify %s
2*0a6a1f1dSLionel Sambuc // RUN: %clang_cc1 -x c++ -analyze -analyzer-checker=core,alpha.core.TestAfterDivZero -analyzer-output=text -verify %s
3*0a6a1f1dSLionel Sambuc 
4*0a6a1f1dSLionel Sambuc int var;
5*0a6a1f1dSLionel Sambuc 
err_eq(int x)6*0a6a1f1dSLionel Sambuc void err_eq(int x) {
7*0a6a1f1dSLionel Sambuc   var = 77 / x; // expected-note {{Division with compared value made here}}
8*0a6a1f1dSLionel Sambuc   if (x == 0) { } // expected-warning {{Value being compared against zero has already been used for division}}
9*0a6a1f1dSLionel Sambuc } // expected-note@-1 {{Value being compared against zero has already been used for division}}
10*0a6a1f1dSLionel Sambuc 
err_eq2(int x)11*0a6a1f1dSLionel Sambuc void err_eq2(int x) {
12*0a6a1f1dSLionel Sambuc   var = 77 / x; // expected-note {{Division with compared value made here}}
13*0a6a1f1dSLionel Sambuc   if (0 == x) { } // expected-warning {{Value being compared against zero has already been used for division}}
14*0a6a1f1dSLionel Sambuc } // expected-note@-1 {{Value being compared against zero has already been used for division}}
15*0a6a1f1dSLionel Sambuc 
err_ne(int x)16*0a6a1f1dSLionel Sambuc void err_ne(int x) {
17*0a6a1f1dSLionel Sambuc   var = 77 / x; // expected-note {{Division with compared value made here}}
18*0a6a1f1dSLionel Sambuc   if (x != 0) { } // expected-warning {{Value being compared against zero has already been used for division}}
19*0a6a1f1dSLionel Sambuc } // expected-note@-1 {{Value being compared against zero has already been used for division}}
20*0a6a1f1dSLionel Sambuc 
err_ge(int x)21*0a6a1f1dSLionel Sambuc void err_ge(int x) {
22*0a6a1f1dSLionel Sambuc   var = 77 / x; // expected-note {{Division with compared value made here}}
23*0a6a1f1dSLionel Sambuc   if (x >= 0) { } // expected-warning {{Value being compared against zero has already been used for division}}
24*0a6a1f1dSLionel Sambuc } // expected-note@-1 {{Value being compared against zero has already been used for division}}
25*0a6a1f1dSLionel Sambuc 
err_le(int x)26*0a6a1f1dSLionel Sambuc void err_le(int x) {
27*0a6a1f1dSLionel Sambuc   var = 77 / x; // expected-note {{Division with compared value made here}}
28*0a6a1f1dSLionel Sambuc   if (x <= 0) {} // expected-warning {{Value being compared against zero has already been used for division}}
29*0a6a1f1dSLionel Sambuc } // expected-note@-1 {{Value being compared against zero has already been used for division}}
30*0a6a1f1dSLionel Sambuc 
err_yes(int x)31*0a6a1f1dSLionel Sambuc void err_yes(int x) {
32*0a6a1f1dSLionel Sambuc   var = 77 / x; // expected-note {{Division with compared value made here}}
33*0a6a1f1dSLionel Sambuc   if (x) {} // expected-warning {{Value being compared against zero has already been used for division}}
34*0a6a1f1dSLionel Sambuc } // expected-note@-1 {{Value being compared against zero has already been used for division}}
err_not(int x)35*0a6a1f1dSLionel Sambuc void err_not(int x) {
36*0a6a1f1dSLionel Sambuc   var = 77 / x; // expected-note {{Division with compared value made here}}
37*0a6a1f1dSLionel Sambuc   if (!x) {} // expected-warning {{Value being compared against zero has already been used for division}}
38*0a6a1f1dSLionel Sambuc } // expected-note@-1 {{Value being compared against zero has already been used for division}}
39*0a6a1f1dSLionel Sambuc 
err_pnot(int x)40*0a6a1f1dSLionel Sambuc void err_pnot(int x) {
41*0a6a1f1dSLionel Sambuc   int *y = &x;
42*0a6a1f1dSLionel Sambuc   var = 77 / *y; // expected-note {{Division with compared value made here}}
43*0a6a1f1dSLionel Sambuc   if (!x) {} // expected-warning {{Value being compared against zero has already been used for division}}
44*0a6a1f1dSLionel Sambuc } // expected-note@-1 {{Value being compared against zero has already been used for division}}
45*0a6a1f1dSLionel Sambuc 
err_pnot2(int x)46*0a6a1f1dSLionel Sambuc void err_pnot2(int x) {
47*0a6a1f1dSLionel Sambuc   int *y = &x;
48*0a6a1f1dSLionel Sambuc   var = 77 / x; // expected-note {{Division with compared value made here}}
49*0a6a1f1dSLionel Sambuc   if (!*y) {} // expected-warning {{Value being compared against zero has already been used for division}}
50*0a6a1f1dSLionel Sambuc } // expected-note@-1 {{Value being compared against zero has already been used for division}}
51*0a6a1f1dSLionel Sambuc 
err_ppnot(int x)52*0a6a1f1dSLionel Sambuc void err_ppnot(int x) {
53*0a6a1f1dSLionel Sambuc   int *y = &x;
54*0a6a1f1dSLionel Sambuc   int **z = &y;
55*0a6a1f1dSLionel Sambuc   var = 77 / **z; // expected-note {{Division with compared value made here}}
56*0a6a1f1dSLionel Sambuc   if (!x) {} // expected-warning {{Value being compared against zero has already been used for division}}
57*0a6a1f1dSLionel Sambuc } // expected-note@-1 {{Value being compared against zero has already been used for division}}
58*0a6a1f1dSLionel Sambuc 
err_orig_checker(int x)59*0a6a1f1dSLionel Sambuc void err_orig_checker(int x) {
60*0a6a1f1dSLionel Sambuc   if (x != 0) // expected-note {{Assuming 'x' is equal to 0}} expected-note {{Taking false branch}}
61*0a6a1f1dSLionel Sambuc     return;
62*0a6a1f1dSLionel Sambuc   var = 77 / x; // expected-warning {{Division by zero}} expected-note {{Division by zero}}
63*0a6a1f1dSLionel Sambuc   if (!x) {} // no-warning
64*0a6a1f1dSLionel Sambuc }
65*0a6a1f1dSLionel Sambuc 
ok_other(int x,int y)66*0a6a1f1dSLionel Sambuc void ok_other(int x, int y) {
67*0a6a1f1dSLionel Sambuc   var = 77 / y;
68*0a6a1f1dSLionel Sambuc   if (x == 0) {
69*0a6a1f1dSLionel Sambuc   }
70*0a6a1f1dSLionel Sambuc }
71*0a6a1f1dSLionel Sambuc 
ok_assign(int x)72*0a6a1f1dSLionel Sambuc void ok_assign(int x) {
73*0a6a1f1dSLionel Sambuc   var = 77 / x;
74*0a6a1f1dSLionel Sambuc   x = var / 77; // <- assignment => don't warn
75*0a6a1f1dSLionel Sambuc   if (x == 0) {
76*0a6a1f1dSLionel Sambuc   }
77*0a6a1f1dSLionel Sambuc }
78*0a6a1f1dSLionel Sambuc 
ok_assign2(int x)79*0a6a1f1dSLionel Sambuc void ok_assign2(int x) {
80*0a6a1f1dSLionel Sambuc   var = 77 / x;
81*0a6a1f1dSLionel Sambuc   x = var / 77; // <- assignment => don't warn
82*0a6a1f1dSLionel Sambuc   if (0 == x) {
83*0a6a1f1dSLionel Sambuc   }
84*0a6a1f1dSLionel Sambuc }
85*0a6a1f1dSLionel Sambuc 
ok_dec(int x)86*0a6a1f1dSLionel Sambuc void ok_dec(int x) {
87*0a6a1f1dSLionel Sambuc   var = 77 / x;
88*0a6a1f1dSLionel Sambuc   x--; // <- assignment => don't warn
89*0a6a1f1dSLionel Sambuc   if (x == 0) {
90*0a6a1f1dSLionel Sambuc   }
91*0a6a1f1dSLionel Sambuc }
92*0a6a1f1dSLionel Sambuc 
ok_inc(int x)93*0a6a1f1dSLionel Sambuc void ok_inc(int x) {
94*0a6a1f1dSLionel Sambuc   var = 77 / x;
95*0a6a1f1dSLionel Sambuc   x++; // <- assignment => don't warn
96*0a6a1f1dSLionel Sambuc   if (x == 0) {
97*0a6a1f1dSLionel Sambuc   }
98*0a6a1f1dSLionel Sambuc }
99*0a6a1f1dSLionel Sambuc 
100*0a6a1f1dSLionel Sambuc void do_something_ptr(int *x);
ok_callfunc_ptr(int x)101*0a6a1f1dSLionel Sambuc void ok_callfunc_ptr(int x) {
102*0a6a1f1dSLionel Sambuc   var = 77 / x;
103*0a6a1f1dSLionel Sambuc   do_something_ptr(&x); // <- pass address of x to function => don't warn
104*0a6a1f1dSLionel Sambuc   if (x == 0) {
105*0a6a1f1dSLionel Sambuc   }
106*0a6a1f1dSLionel Sambuc }
107*0a6a1f1dSLionel Sambuc 
108*0a6a1f1dSLionel Sambuc void do_something(int x);
nok_callfunc(int x)109*0a6a1f1dSLionel Sambuc void nok_callfunc(int x) {
110*0a6a1f1dSLionel Sambuc   var = 77 / x; // expected-note {{Division with compared value made here}}
111*0a6a1f1dSLionel Sambuc   do_something(x);
112*0a6a1f1dSLionel Sambuc   if (x == 0) {} // expected-warning {{Value being compared against zero has already been used for division}}
113*0a6a1f1dSLionel Sambuc } // expected-note@-1 {{Value being compared against zero has already been used for division}}
114*0a6a1f1dSLionel Sambuc 
ok_if(int x)115*0a6a1f1dSLionel Sambuc void ok_if(int x) {
116*0a6a1f1dSLionel Sambuc   if (x > 3)
117*0a6a1f1dSLionel Sambuc     var = 77 / x;
118*0a6a1f1dSLionel Sambuc   if (x == 0) {
119*0a6a1f1dSLionel Sambuc   }
120*0a6a1f1dSLionel Sambuc }
121*0a6a1f1dSLionel Sambuc 
ok_if2(int x)122*0a6a1f1dSLionel Sambuc void ok_if2(int x) {
123*0a6a1f1dSLionel Sambuc   if (x < 3)
124*0a6a1f1dSLionel Sambuc     var = 77 / x;
125*0a6a1f1dSLionel Sambuc   if (x == 0) {
126*0a6a1f1dSLionel Sambuc   } // TODO warn here
127*0a6a1f1dSLionel Sambuc }
128*0a6a1f1dSLionel Sambuc 
ok_pif(int x)129*0a6a1f1dSLionel Sambuc void ok_pif(int x) {
130*0a6a1f1dSLionel Sambuc   int *y = &x;
131*0a6a1f1dSLionel Sambuc   if (x < 3)
132*0a6a1f1dSLionel Sambuc     var = 77 / *y;
133*0a6a1f1dSLionel Sambuc   if (x == 0) {
134*0a6a1f1dSLionel Sambuc   } // TODO warn here
135*0a6a1f1dSLionel Sambuc }
136*0a6a1f1dSLionel Sambuc 
137*0a6a1f1dSLionel Sambuc int getValue(bool *isPositive);
138*0a6a1f1dSLionel Sambuc void use(int a);
foo()139*0a6a1f1dSLionel Sambuc void foo() {
140*0a6a1f1dSLionel Sambuc   bool isPositive;
141*0a6a1f1dSLionel Sambuc   int x = getValue(&isPositive);
142*0a6a1f1dSLionel Sambuc   if (isPositive) {
143*0a6a1f1dSLionel Sambuc     use(5 / x);
144*0a6a1f1dSLionel Sambuc   }
145*0a6a1f1dSLionel Sambuc 
146*0a6a1f1dSLionel Sambuc   if (x == 0) {
147*0a6a1f1dSLionel Sambuc   }
148*0a6a1f1dSLionel Sambuc }
149*0a6a1f1dSLionel Sambuc 
150*0a6a1f1dSLionel Sambuc int getValue2();
foo2()151*0a6a1f1dSLionel Sambuc void foo2() {
152*0a6a1f1dSLionel Sambuc   int x = getValue2();
153*0a6a1f1dSLionel Sambuc   int y = x;
154*0a6a1f1dSLionel Sambuc 
155*0a6a1f1dSLionel Sambuc   use(5 / x); // expected-note {{Division with compared value made here}}
156*0a6a1f1dSLionel Sambuc   if (y == 0) {} // expected-warning {{Value being compared against zero has already been used for division}}
157*0a6a1f1dSLionel Sambuc } // expected-note@-1 {{Value being compared against zero has already been used for division}}
158*0a6a1f1dSLionel Sambuc 
ok_while(int x)159*0a6a1f1dSLionel Sambuc void ok_while(int x) {
160*0a6a1f1dSLionel Sambuc   int n = 100 / x;
161*0a6a1f1dSLionel Sambuc   while (x != 0) { // <- do not warn
162*0a6a1f1dSLionel Sambuc     x--;
163*0a6a1f1dSLionel Sambuc   }
164*0a6a1f1dSLionel Sambuc }
165*0a6a1f1dSLionel Sambuc 
err_not2(int x,int y)166*0a6a1f1dSLionel Sambuc void err_not2(int x, int y) {
167*0a6a1f1dSLionel Sambuc   int v;
168*0a6a1f1dSLionel Sambuc   var = 77 / x;
169*0a6a1f1dSLionel Sambuc 
170*0a6a1f1dSLionel Sambuc   if (y)
171*0a6a1f1dSLionel Sambuc     v = 0;
172*0a6a1f1dSLionel Sambuc 
173*0a6a1f1dSLionel Sambuc   if (!x) {
174*0a6a1f1dSLionel Sambuc   } // TODO warn here
175*0a6a1f1dSLionel Sambuc }
176*0a6a1f1dSLionel Sambuc 
inline_func(int x)177*0a6a1f1dSLionel Sambuc inline void inline_func(int x) {
178*0a6a1f1dSLionel Sambuc   var = 77 / x; // expected-note {{Division with compared value made here}}
179*0a6a1f1dSLionel Sambuc   if (x == 0) {} // expected-warning {{Value being compared against zero has already been used for division}}
180*0a6a1f1dSLionel Sambuc } // expected-note@-1 {{Value being compared against zero has already been used for division}}
181*0a6a1f1dSLionel Sambuc 
err_inline(int x)182*0a6a1f1dSLionel Sambuc void err_inline(int x) {
183*0a6a1f1dSLionel Sambuc   var = 77 / x;
184*0a6a1f1dSLionel Sambuc   inline_func(x); // expected-note {{Calling 'inline_func'}}
185*0a6a1f1dSLionel Sambuc   if (x == 0) {
186*0a6a1f1dSLionel Sambuc   }
187*0a6a1f1dSLionel Sambuc }
188*0a6a1f1dSLionel Sambuc 
inline_func2(int x)189*0a6a1f1dSLionel Sambuc inline void inline_func2(int x) {}
190*0a6a1f1dSLionel Sambuc 
err_inline2(int x)191*0a6a1f1dSLionel Sambuc void err_inline2(int x) {
192*0a6a1f1dSLionel Sambuc   var = 77 / x; // expected-note {{Division with compared value made here}}
193*0a6a1f1dSLionel Sambuc   inline_func2(x);
194*0a6a1f1dSLionel Sambuc   if (x == 0) {} // expected-warning {{Value being compared against zero has already been used for division}}
195*0a6a1f1dSLionel Sambuc } // expected-note@-1 {{Value being compared against zero has already been used for division}}
196*0a6a1f1dSLionel Sambuc 
inline_func3(int x)197*0a6a1f1dSLionel Sambuc inline void inline_func3(int x) {
198*0a6a1f1dSLionel Sambuc   var = 77 / x;
199*0a6a1f1dSLionel Sambuc }
ok_inline(int x)200*0a6a1f1dSLionel Sambuc void ok_inline(int x) {
201*0a6a1f1dSLionel Sambuc   var = 77 / x; // expected-note {{Division with compared value made here}}
202*0a6a1f1dSLionel Sambuc   inline_func3(x);
203*0a6a1f1dSLionel Sambuc   if (x == 0) {} // expected-warning {{Value being compared against zero has already been used for division}}
204*0a6a1f1dSLionel Sambuc } // expected-note@-1 {{Value being compared against zero has already been used for division}}
205