1 // RUN: %clang_analyze_cc1 -analyzer-checker=core,unix.Malloc,debug.ExprInspection -analyzer-max-loop 4 -analyzer-config widen-loops=true -verify -analyzer-config eagerly-assume=false %s
2 // RUN: %clang_analyze_cc1 -DTEST_NULL_TERM -analyzer-checker=core,unix.Malloc,debug.ExprInspection,alpha.cplusplus.IteratorRange -analyzer-max-loop 4 -analyzer-config widen-loops=true -verify -analyzer-config eagerly-assume=false %s
4 void clang_analyzer_eval(int);
5 void clang_analyzer_warnIfReached(void);
7 typedef __typeof(sizeof(int)) size_t;
11 void loop_which_iterates_limit_times_not_widened(void) {
14 // Check loop isn't widened by checking x isn't invalidated
15 for (i
= 0; i
< 1; ++i
) {}
16 clang_analyzer_eval(x
== 1); // expected-warning {{TRUE}}
17 for (i
= 0; i
< 2; ++i
) {}
18 clang_analyzer_eval(x
== 1); // expected-warning {{TRUE}}
19 for (i
= 0; i
< 3; ++i
) {}
20 // FIXME loss of precision as a result of evaluating the widened loop body
21 // *instead* of the last iteration.
22 clang_analyzer_eval(x
== 1); // expected-warning {{UNKNOWN}}
27 void loop_evaluated_before_widening(void) {
30 for (i
= 0; i
< 10; ++i
) {
32 // True before widening then unknown after.
33 clang_analyzer_eval(a_global
== 1); // expected-warning{{TRUE}} expected-warning{{UNKNOWN}}
36 clang_analyzer_warnIfReached(); // expected-warning{{REACHABLE}}
39 void warnings_after_loop(void) {
41 for (i
= 0; i
< 10; ++i
) {}
42 char *m
= (char*)malloc(12);
43 } // expected-warning {{Potential leak of memory pointed to by 'm'}}
45 void for_loop_exits(void) {
47 for (i
= 0; i
< 10; ++i
) {}
48 clang_analyzer_warnIfReached(); // expected-warning{{REACHABLE}}
51 void while_loop_exits(void) {
54 clang_analyzer_warnIfReached(); // expected-warning{{REACHABLE}}
57 void do_while_loop_exits(void) {
59 do {++i
;} while (i
< 10);
60 clang_analyzer_warnIfReached(); // expected-warning{{REACHABLE}}
63 void loop_body_is_widened(void) {
67 clang_analyzer_warnIfReached(); // expected-warning{{REACHABLE}}
71 clang_analyzer_warnIfReached(); // expected-warning{{REACHABLE}}
74 void invariably_infinite_loop(void) {
77 clang_analyzer_warnIfReached(); // no-warning
80 void invariably_infinite_break_loop(void) {
87 clang_analyzer_warnIfReached(); // no-warning
90 void reachable_break_loop(void) {
96 clang_analyzer_warnIfReached(); // expected-warning{{REACHABLE}}
99 void condition_constrained_true_in_loop(void) {
102 clang_analyzer_eval(i
< 50); // expected-warning {{TRUE}}
105 clang_analyzer_warnIfReached(); // expected-warning{{REACHABLE}}
108 void condition_constrained_false_after_loop(void) {
113 clang_analyzer_eval(i
>= 50); // expected-warning {{TRUE}}
114 clang_analyzer_warnIfReached(); // expected-warning{{REACHABLE}}
117 void multiple_exit_test(void) {
127 // Reachable by 'normal' exit
128 if (i
== 50) clang_analyzer_warnIfReached(); // expected-warning{{REACHABLE}}
129 // Reachable by break point
130 if (i
== 10) clang_analyzer_warnIfReached(); // expected-warning{{REACHABLE}}
132 if (i
< 10) clang_analyzer_warnIfReached(); // no-warning
133 if (i
> 10 && i
< 50) clang_analyzer_warnIfReached(); // no-warning
136 void pointer_doesnt_leak_from_loop(void) {
137 int *h_ptr
= (int *) malloc(sizeof(int));
138 for (int i
= 0; i
< 2; ++i
) {}
139 for (int i
= 0; i
< 10; ++i
) {} // no-warning
145 void unknown_after_loop(int s_arg
) {
149 int *h_ptr
= malloc(sizeof(int));
151 for (int i
= 0; i
< 10; ++i
) {}
153 clang_analyzer_eval(g_global
); // expected-warning {{UNKNOWN}}
154 clang_analyzer_eval(s_arg
); // expected-warning {{UNKNOWN}}
155 clang_analyzer_eval(s_local
); // expected-warning {{UNKNOWN}}
156 clang_analyzer_eval(h_ptr
== 0); // expected-warning {{UNKNOWN}}
160 void variable_bound_exiting_loops_widened(int x
) {
166 clang_analyzer_eval(t
== 1); // expected-warning {{TRUE}} // expected-warning {{UNKNOWN}}
169 void nested_loop_outer_widen(void) {
171 for (i
= 0; i
< 10; i
++) {
172 clang_analyzer_eval(i
< 10); // expected-warning {{TRUE}}
173 for (j
= 0; j
< 2; j
++) {
174 clang_analyzer_eval(j
< 2); // expected-warning {{TRUE}}
176 clang_analyzer_eval(j
>= 2); // expected-warning {{TRUE}}
178 clang_analyzer_eval(i
>= 10); // expected-warning {{TRUE}}
181 void nested_loop_inner_widen(void) {
183 for (i
= 0; i
< 2; i
++) {
184 clang_analyzer_eval(i
< 2); // expected-warning {{TRUE}}
185 for (j
= 0; j
< 10; j
++) {
186 clang_analyzer_eval(j
< 10); // expected-warning {{TRUE}}
188 clang_analyzer_eval(j
>= 10); // expected-warning {{TRUE}}
190 clang_analyzer_eval(i
>= 2); // expected-warning {{TRUE}}
193 #ifdef TEST_NULL_TERM
194 void null_terminator_loop_widen(int *a
) {
196 // Loop widening will call 'invalidateRegions()' and 'invalidateRegions()'
197 // will construct the SymbolConjured with null Stmt because of the null
198 // terminator statement. Accessing the null Stmt will cause a crash.