1a4de17562d13d7a8188108243c4cfbd52f33229aPirama Arumuga Nainar// RUN: %clang_cc1 -analyze -analyzer-checker=debug.ExprInspection -verify %s
2a4de17562d13d7a8188108243c4cfbd52f33229aPirama Arumuga Nainar
3a4de17562d13d7a8188108243c4cfbd52f33229aPirama Arumuga Nainarvoid clang_analyzer_eval(int);
4a4de17562d13d7a8188108243c4cfbd52f33229aPirama Arumuga Nainarvoid clang_analyzer_warnOnDeadSymbol(int);
5a4de17562d13d7a8188108243c4cfbd52f33229aPirama Arumuga Nainar
6a4de17562d13d7a8188108243c4cfbd52f33229aPirama Arumuga Nainarint conjure_index();
7a4de17562d13d7a8188108243c4cfbd52f33229aPirama Arumuga Nainar
8a4de17562d13d7a8188108243c4cfbd52f33229aPirama Arumuga Nainarvoid test_that_expr_inspection_works() {
9a4de17562d13d7a8188108243c4cfbd52f33229aPirama Arumuga Nainar  do {
10a4de17562d13d7a8188108243c4cfbd52f33229aPirama Arumuga Nainar    int x = conjure_index();
11a4de17562d13d7a8188108243c4cfbd52f33229aPirama Arumuga Nainar    clang_analyzer_warnOnDeadSymbol(x);
12a4de17562d13d7a8188108243c4cfbd52f33229aPirama Arumuga Nainar  } while(0); // expected-warning{{SYMBOL DEAD}}
13a4de17562d13d7a8188108243c4cfbd52f33229aPirama Arumuga Nainar}
14a4de17562d13d7a8188108243c4cfbd52f33229aPirama Arumuga Nainar
15a4de17562d13d7a8188108243c4cfbd52f33229aPirama Arumuga Nainar// These tests verify the reaping of symbols that are only referenced as
16a4de17562d13d7a8188108243c4cfbd52f33229aPirama Arumuga Nainar// index values in element regions. Most of the time, depending on where
17a4de17562d13d7a8188108243c4cfbd52f33229aPirama Arumuga Nainar// the element region, as Loc value, is stored, it is possible to
18a4de17562d13d7a8188108243c4cfbd52f33229aPirama Arumuga Nainar// recover the index symbol in checker code, which is also demonstrated
19a4de17562d13d7a8188108243c4cfbd52f33229aPirama Arumuga Nainar// in the return_ptr_range.c test file.
20a4de17562d13d7a8188108243c4cfbd52f33229aPirama Arumuga Nainar
21a4de17562d13d7a8188108243c4cfbd52f33229aPirama Arumuga Nainarint arr[3];
22a4de17562d13d7a8188108243c4cfbd52f33229aPirama Arumuga Nainar
23a4de17562d13d7a8188108243c4cfbd52f33229aPirama Arumuga Nainarint *test_element_index_lifetime_in_environment_values() {
24a4de17562d13d7a8188108243c4cfbd52f33229aPirama Arumuga Nainar  int *ptr;
25a4de17562d13d7a8188108243c4cfbd52f33229aPirama Arumuga Nainar  do {
26a4de17562d13d7a8188108243c4cfbd52f33229aPirama Arumuga Nainar    int x = conjure_index();
27a4de17562d13d7a8188108243c4cfbd52f33229aPirama Arumuga Nainar    clang_analyzer_warnOnDeadSymbol(x);
28a4de17562d13d7a8188108243c4cfbd52f33229aPirama Arumuga Nainar    ptr = arr + x;
29a4de17562d13d7a8188108243c4cfbd52f33229aPirama Arumuga Nainar  } while (0);
30a4de17562d13d7a8188108243c4cfbd52f33229aPirama Arumuga Nainar  return ptr;
31a4de17562d13d7a8188108243c4cfbd52f33229aPirama Arumuga Nainar}
32a4de17562d13d7a8188108243c4cfbd52f33229aPirama Arumuga Nainar
33a4de17562d13d7a8188108243c4cfbd52f33229aPirama Arumuga Nainarvoid test_element_index_lifetime_in_store_keys() {
34a4de17562d13d7a8188108243c4cfbd52f33229aPirama Arumuga Nainar  do {
35a4de17562d13d7a8188108243c4cfbd52f33229aPirama Arumuga Nainar    int x = conjure_index();
36a4de17562d13d7a8188108243c4cfbd52f33229aPirama Arumuga Nainar    clang_analyzer_warnOnDeadSymbol(x);
37a4de17562d13d7a8188108243c4cfbd52f33229aPirama Arumuga Nainar    arr[x] = 1;
38a4de17562d13d7a8188108243c4cfbd52f33229aPirama Arumuga Nainar    if (x) {}
39a4de17562d13d7a8188108243c4cfbd52f33229aPirama Arumuga Nainar  } while (0); // no-warning
40a4de17562d13d7a8188108243c4cfbd52f33229aPirama Arumuga Nainar}
41a4de17562d13d7a8188108243c4cfbd52f33229aPirama Arumuga Nainar
42a4de17562d13d7a8188108243c4cfbd52f33229aPirama Arumuga Nainarint *ptr;
43a4de17562d13d7a8188108243c4cfbd52f33229aPirama Arumuga Nainarvoid test_element_index_lifetime_in_store_values() {
44a4de17562d13d7a8188108243c4cfbd52f33229aPirama Arumuga Nainar  do {
45a4de17562d13d7a8188108243c4cfbd52f33229aPirama Arumuga Nainar    int x = conjure_index();
46a4de17562d13d7a8188108243c4cfbd52f33229aPirama Arumuga Nainar    clang_analyzer_warnOnDeadSymbol(x);
47a4de17562d13d7a8188108243c4cfbd52f33229aPirama Arumuga Nainar    ptr = arr + x;
48a4de17562d13d7a8188108243c4cfbd52f33229aPirama Arumuga Nainar  } while (0); // no-warning
49a4de17562d13d7a8188108243c4cfbd52f33229aPirama Arumuga Nainar}
50a4de17562d13d7a8188108243c4cfbd52f33229aPirama Arumuga Nainar
51a4de17562d13d7a8188108243c4cfbd52f33229aPirama Arumuga Nainarstruct S1 {
52a4de17562d13d7a8188108243c4cfbd52f33229aPirama Arumuga Nainar  int field;
53a4de17562d13d7a8188108243c4cfbd52f33229aPirama Arumuga Nainar};
54a4de17562d13d7a8188108243c4cfbd52f33229aPirama Arumuga Nainarstruct S2 {
55a4de17562d13d7a8188108243c4cfbd52f33229aPirama Arumuga Nainar  struct S1 array[5];
56a4de17562d13d7a8188108243c4cfbd52f33229aPirama Arumuga Nainar} s2;
57a4de17562d13d7a8188108243c4cfbd52f33229aPirama Arumuga Nainar
58a4de17562d13d7a8188108243c4cfbd52f33229aPirama Arumuga Nainarvoid test_element_index_lifetime_with_complicated_hierarchy_of_regions() {
59a4de17562d13d7a8188108243c4cfbd52f33229aPirama Arumuga Nainar  do {
60a4de17562d13d7a8188108243c4cfbd52f33229aPirama Arumuga Nainar    int x = conjure_index();
61a4de17562d13d7a8188108243c4cfbd52f33229aPirama Arumuga Nainar    clang_analyzer_warnOnDeadSymbol(x);
62a4de17562d13d7a8188108243c4cfbd52f33229aPirama Arumuga Nainar    s2.array[x].field = 1;
63a4de17562d13d7a8188108243c4cfbd52f33229aPirama Arumuga Nainar    if (x) {}
64a4de17562d13d7a8188108243c4cfbd52f33229aPirama Arumuga Nainar  } while (0); // no-warning
65a4de17562d13d7a8188108243c4cfbd52f33229aPirama Arumuga Nainar}
66a4de17562d13d7a8188108243c4cfbd52f33229aPirama Arumuga Nainar
67a4de17562d13d7a8188108243c4cfbd52f33229aPirama Arumuga Nainar// Test below checks lifetime of SymbolRegionValue in certain conditions.
68a4de17562d13d7a8188108243c4cfbd52f33229aPirama Arumuga Nainar
69a4de17562d13d7a8188108243c4cfbd52f33229aPirama Arumuga Nainarint **ptrptr;
70a4de17562d13d7a8188108243c4cfbd52f33229aPirama Arumuga Nainarvoid test_region_lifetime_as_store_value(int *x) {
71a4de17562d13d7a8188108243c4cfbd52f33229aPirama Arumuga Nainar  clang_analyzer_warnOnDeadSymbol((int) x);
72a4de17562d13d7a8188108243c4cfbd52f33229aPirama Arumuga Nainar  *x = 1;
73a4de17562d13d7a8188108243c4cfbd52f33229aPirama Arumuga Nainar  ptrptr = &x;
74a4de17562d13d7a8188108243c4cfbd52f33229aPirama Arumuga Nainar  (void)0; // No-op; make sure the environment forgets things and the GC runs.
75a4de17562d13d7a8188108243c4cfbd52f33229aPirama Arumuga Nainar  clang_analyzer_eval(**ptrptr); // expected-warning{{TRUE}}
76a4de17562d13d7a8188108243c4cfbd52f33229aPirama Arumuga Nainar} // no-warning
77