1 // RUN: %clang_analyze_cc1 -analyzer-checker=debug.ExprInspection -Wno-pointer-to-int-cast -verify %s
3 void clang_analyzer_eval(int);
4 void clang_analyzer_warnOnDeadSymbol(int);
5 void clang_analyzer_numTimesReached(void);
6 void clang_analyzer_warnIfReached(void);
10 int conjure_index(void);
12 void test_that_expr_inspection_works(void) {
14 int x
= conjure_index();
15 clang_analyzer_warnOnDeadSymbol(x
);
16 } while(0); // expected-warning{{SYMBOL DEAD}}
18 // Make sure we don't accidentally split state in ExprInspection.
19 clang_analyzer_numTimesReached(); // expected-warning{{1}}
22 // These tests verify the reaping of symbols that are only referenced as
23 // index values in element regions. Most of the time, depending on where
24 // the element region, as Loc value, is stored, it is possible to
25 // recover the index symbol in checker code, which is also demonstrated
26 // in the return_ptr_range.c test file.
30 int *test_element_index_lifetime_in_environment_values(void) {
33 int x
= conjure_index();
34 clang_analyzer_warnOnDeadSymbol(x
);
40 void test_element_index_lifetime_in_store_keys(void) {
42 int x
= conjure_index();
43 clang_analyzer_warnOnDeadSymbol(x
);
46 } while (0); // no-warning
50 void test_element_index_lifetime_in_store_values(void) {
52 int x
= conjure_index();
53 clang_analyzer_warnOnDeadSymbol(x
);
55 } while (0); // no-warning
68 struct S1
*conjure_S1(void);
69 struct S3
*conjure_S3(void);
71 void test_element_index_lifetime_with_complicated_hierarchy_of_regions(void) {
73 int x
= conjure_index();
74 clang_analyzer_warnOnDeadSymbol(x
);
75 s2
.array
[x
].field
= 1;
77 } while (0); // no-warning
80 void test_loc_as_integer_element_index_lifetime(void) {
83 struct S3
*s
= conjure_S3();
84 clang_analyzer_warnOnDeadSymbol((int)s
);
91 // Test below checks lifetime of SymbolRegionValue in certain conditions.
94 void test_region_lifetime_as_store_value(int *x
) {
95 clang_analyzer_warnOnDeadSymbol((int) x
);
98 (void)0; // No-op; make sure the environment forgets things and the GC runs.
99 clang_analyzer_eval(**ptrptr
); // expected-warning{{TRUE}}
102 int *produce_region_referenced_only_through_field_in_environment_value(void) {
103 struct S1
*s
= conjure_S1();
104 clang_analyzer_warnOnDeadSymbol((int) s
);
109 void test_region_referenced_only_through_field_in_environment_value(void) {
110 produce_region_referenced_only_through_field_in_environment_value();
111 } // expected-warning{{SYMBOL DEAD}}
113 void test_region_referenced_only_through_field_in_store_value(void) {
114 struct S1
*s
= conjure_S1();
115 clang_analyzer_warnOnDeadSymbol((int) s
);
116 ptr
= &s
->field
; // Write the symbol into a global. It should live forever.
118 exit(0); // no-warning (symbol should not die here)
119 // exit() is noreturn.
120 clang_analyzer_warnIfReached(); // no-warning
122 if (!ptr
) { // no-warning (symbol should not die here)
123 // We exit()ed under these constraints earlier.
124 clang_analyzer_warnIfReached(); // no-warning
126 // The exit() call invalidates globals. The symbol will die here because
127 // the exit() statement itself is already over and there's no better statement
128 // to put the diagnostic on.
129 } // expected-warning{{SYMBOL DEAD}}
131 void test_zombie_referenced_only_through_field_in_store_value(void) {
132 struct S1
*s
= conjure_S1();
133 clang_analyzer_warnOnDeadSymbol((int) s
);
135 } // expected-warning{{SYMBOL DEAD}}
137 void double_dereference_of_implicit_value_aux1(int *p
) {
141 void double_dereference_of_implicit_value_aux2(int *p
) {
143 clang_analyzer_warnIfReached(); // no-warning
146 void test_double_dereference_of_implicit_value(int **x
) {
147 clang_analyzer_warnOnDeadSymbol(**x
);
150 double_dereference_of_implicit_value_aux1(*y
);
151 // Give time for symbol reaping to happen.
153 // The symbol for **y was cleaned up from the Store at this point,
154 // even though it was not perceived as dead when asked explicitly.
155 // For that reason the SYMBOL DEAD warning never appeared at this point.
156 double_dereference_of_implicit_value_aux2(*y
);
158 // The symbol is generally reaped here regardless.
159 ((void)0); // expected-warning{{SYMBOL DEAD}}