Jordan Rose | 48088ed | 2012-07-06 22:10:39 +0000 | [diff] [blame] | 1 | // RUN: %clang_cc1 -analyze -analyzer-checker=core,experimental.core,debug.ExprInspection -analyzer-store=region -analyzer-constraints=basic -analyzer-ipa=inlining -verify %s |
| 2 | // RUN: %clang_cc1 -analyze -analyzer-checker=core,experimental.core,debug.ExprInspection -analyzer-store=region -analyzer-constraints=range -analyzer-ipa=inlining -verify %s |
Jordy Rose | 43d9f0d | 2012-05-16 16:01:10 +0000 | [diff] [blame] | 3 | |
| 4 | void clang_analyzer_eval(int); |
Jordy Rose | 23b736e | 2010-07-29 07:11:59 +0000 | [diff] [blame] | 5 | |
| 6 | int string_literal_init() { |
| 7 | char a[] = "abc"; |
| 8 | char b[2] = "abc"; // expected-warning{{too long}} |
| 9 | char c[5] = "abc"; |
| 10 | |
Jordy Rose | 43d9f0d | 2012-05-16 16:01:10 +0000 | [diff] [blame] | 11 | clang_analyzer_eval(a[1] == 'b'); // expected-warning{{TRUE}} |
| 12 | clang_analyzer_eval(b[1] == 'b'); // expected-warning{{TRUE}} |
| 13 | clang_analyzer_eval(c[1] == 'b'); // expected-warning{{TRUE}} |
Jordy Rose | 23b736e | 2010-07-29 07:11:59 +0000 | [diff] [blame] | 14 | |
Jordy Rose | 43d9f0d | 2012-05-16 16:01:10 +0000 | [diff] [blame] | 15 | clang_analyzer_eval(a[3] == 0); // expected-warning{{TRUE}} |
| 16 | clang_analyzer_eval(c[3] == 0); // expected-warning{{TRUE}} |
Jordy Rose | 23b736e | 2010-07-29 07:11:59 +0000 | [diff] [blame] | 17 | |
Jordy Rose | 43d9f0d | 2012-05-16 16:01:10 +0000 | [diff] [blame] | 18 | clang_analyzer_eval(c[4] == 0); // expected-warning{{TRUE}} |
Jordy Rose | 23b736e | 2010-07-29 07:11:59 +0000 | [diff] [blame] | 19 | |
| 20 | return 42; |
| 21 | } |
Jordy Rose | 59b6dca | 2010-08-20 01:05:59 +0000 | [diff] [blame] | 22 | |
| 23 | void nested_compound_literals(int rad) { |
David Blaikie | be0ee87 | 2012-05-15 16:56:36 +0000 | [diff] [blame] | 24 | int vec[6][2] = {{0.195, 0.02}, {0.383, 0.067}, {0.55, 0.169}, // expected-warning 6 {{implicit conversion from 'double' to 'int' changes value from}} |
| 25 | {0.831, 0.45}, {0.924, 0.617}, {0.98, 0.805}}; // expected-warning 6 {{implicit conversion from 'double' to 'int' changes value from}} |
Jordy Rose | 59b6dca | 2010-08-20 01:05:59 +0000 | [diff] [blame] | 26 | int a; |
| 27 | |
| 28 | for (a = 0; a < 6; ++a) { |
| 29 | vec[a][0] *= rad; // no-warning |
| 30 | vec[a][1] *= rad; // no-warning |
| 31 | } |
| 32 | } |
| 33 | |
| 34 | void nested_compound_literals_float(float rad) { |
| 35 | float vec[6][2] = {{0.195, 0.02}, {0.383, 0.067}, {0.55, 0.169}, |
| 36 | {0.831, 0.45}, {0.924, 0.617}, {0.98, 0.805}}; |
| 37 | int a; |
| 38 | |
| 39 | for (a = 0; a < 6; ++a) { |
| 40 | vec[a][0] *= rad; // no-warning |
| 41 | vec[a][1] *= rad; // no-warning |
| 42 | } |
| 43 | } |
Jordy Rose | f113940 | 2012-05-12 17:32:59 +0000 | [diff] [blame] | 44 | |
| 45 | |
| 46 | void struct_as_array() { |
Jordy Rose | 43d9f0d | 2012-05-16 16:01:10 +0000 | [diff] [blame] | 47 | struct simple { int x; int y; }; |
Jordy Rose | f113940 | 2012-05-12 17:32:59 +0000 | [diff] [blame] | 48 | struct simple a; |
| 49 | struct simple *p = &a; |
Jordy Rose | 43d9f0d | 2012-05-16 16:01:10 +0000 | [diff] [blame] | 50 | |
Jordy Rose | f113940 | 2012-05-12 17:32:59 +0000 | [diff] [blame] | 51 | p->x = 5; |
Jordy Rose | 43d9f0d | 2012-05-16 16:01:10 +0000 | [diff] [blame] | 52 | clang_analyzer_eval(a.x == 5); // expected-warning{{TRUE}} |
| 53 | clang_analyzer_eval(p[0].x == 5); // expected-warning{{TRUE}} |
| 54 | |
| 55 | p[0].y = 5; |
| 56 | clang_analyzer_eval(a.y == 5); // expected-warning{{TRUE}} |
| 57 | clang_analyzer_eval(p->y == 5); // expected-warning{{TRUE}} |
Jordy Rose | f113940 | 2012-05-12 17:32:59 +0000 | [diff] [blame] | 58 | } |
| 59 | |
Jordan Rose | 752bee2 | 2012-07-06 21:59:56 +0000 | [diff] [blame] | 60 | |
| 61 | // PR13264 / <rdar://problem/11802440> |
| 62 | struct point { int x; int y; }; |
| 63 | struct circle { struct point o; int r; }; |
| 64 | struct circle get_circle() { |
| 65 | struct circle result; |
| 66 | result.r = 5; |
| 67 | result.o = (struct point){0, 0}; |
| 68 | return result; |
| 69 | } |
| 70 | |
| 71 | void struct_in_struct() { |
| 72 | struct circle c; |
| 73 | c = get_circle(); |
| 74 | // This used to think c.r was undefined because c.o is a LazyCompoundVal. |
| 75 | clang_analyzer_eval(c.r == 5); // expected-warning{{TRUE}} |
| 76 | } |
| 77 | |
| 78 | // We also test with floats because we don't model floats right now, |
| 79 | // and the original bug report used a float. |
| 80 | struct circle_f { struct point o; float r; }; |
| 81 | struct circle_f get_circle_f() { |
| 82 | struct circle_f result; |
| 83 | result.r = 5.0; |
| 84 | result.o = (struct point){0, 0}; |
| 85 | return result; |
| 86 | } |
| 87 | |
| 88 | float struct_in_struct_f() { |
| 89 | struct circle_f c; |
| 90 | c = get_circle_f(); |
| 91 | |
| 92 | return c.r; // no-warning |
| 93 | } |
| 94 | |
Jordan Rose | e0d24eb | 2012-08-08 18:23:27 +0000 | [diff] [blame] | 95 | |
| 96 | int randomInt(); |
| 97 | |
| 98 | int testSymbolicInvalidation(int index) { |
| 99 | int vals[10]; |
| 100 | |
| 101 | vals[0] = 42; |
| 102 | clang_analyzer_eval(vals[0] == 42); // expected-warning{{TRUE}} |
| 103 | |
| 104 | vals[index] = randomInt(); |
| 105 | clang_analyzer_eval(vals[0] == 42); // expected-warning{{UNKNOWN}} |
| 106 | |
| 107 | return vals[index]; // no-warning |
| 108 | } |
| 109 | |
| 110 | int testConcreteInvalidation(int index) { |
| 111 | int vals[10]; |
| 112 | |
| 113 | vals[index] = 42; |
| 114 | clang_analyzer_eval(vals[index] == 42); // expected-warning{{TRUE}} |
| 115 | vals[0] = randomInt(); |
| 116 | clang_analyzer_eval(vals[index] == 42); // expected-warning{{UNKNOWN}} |
| 117 | |
| 118 | return vals[0]; // no-warning |
| 119 | } |
| 120 | |
| 121 | |
| 122 | typedef struct { |
| 123 | int x, y, z; |
| 124 | } S; |
| 125 | |
| 126 | S makeS(); |
| 127 | |
| 128 | int testSymbolicInvalidationStruct(int index) { |
| 129 | S vals[10]; |
| 130 | |
| 131 | vals[0].x = 42; |
| 132 | clang_analyzer_eval(vals[0].x == 42); // expected-warning{{TRUE}} |
| 133 | |
| 134 | vals[index] = makeS(); |
| 135 | clang_analyzer_eval(vals[0].x == 42); // expected-warning{{UNKNOWN}} |
| 136 | |
| 137 | return vals[index].x; // no-warning |
| 138 | } |
| 139 | |
| 140 | int testConcreteInvalidationStruct(int index) { |
| 141 | S vals[10]; |
| 142 | |
| 143 | vals[index].x = 42; |
| 144 | clang_analyzer_eval(vals[index].x == 42); // expected-warning{{TRUE}} |
| 145 | vals[0] = makeS(); |
| 146 | clang_analyzer_eval(vals[index].x == 42); // expected-warning{{UNKNOWN}} |
| 147 | |
| 148 | return vals[0].x; // no-warning |
| 149 | } |
| 150 | |
| 151 | typedef struct { |
| 152 | S a[5]; |
| 153 | S b[5]; |
| 154 | } SS; |
| 155 | |
| 156 | int testSymbolicInvalidationDoubleStruct(int index) { |
| 157 | SS vals; |
| 158 | |
| 159 | vals.a[0].x = 42; |
| 160 | vals.b[0].x = 42; |
| 161 | clang_analyzer_eval(vals.a[0].x == 42); // expected-warning{{TRUE}} |
| 162 | clang_analyzer_eval(vals.b[0].x == 42); // expected-warning{{TRUE}} |
| 163 | |
| 164 | vals.a[index] = makeS(); |
| 165 | clang_analyzer_eval(vals.a[0].x == 42); // expected-warning{{UNKNOWN}} |
| 166 | clang_analyzer_eval(vals.b[0].x == 42); // expected-warning{{TRUE}} |
| 167 | |
| 168 | return vals.b[index].x; // no-warning |
| 169 | } |
| 170 | |
| 171 | int testConcreteInvalidationDoubleStruct(int index) { |
| 172 | SS vals; |
| 173 | |
| 174 | vals.a[index].x = 42; |
| 175 | vals.b[index].x = 42; |
| 176 | clang_analyzer_eval(vals.a[index].x == 42); // expected-warning{{TRUE}} |
| 177 | clang_analyzer_eval(vals.b[index].x == 42); // expected-warning{{TRUE}} |
| 178 | |
| 179 | vals.a[0] = makeS(); |
| 180 | clang_analyzer_eval(vals.a[index].x == 42); // expected-warning{{UNKNOWN}} |
| 181 | clang_analyzer_eval(vals.b[index].x == 42); // expected-warning{{TRUE}} |
| 182 | |
| 183 | return vals.b[0].x; // no-warning |
| 184 | } |
| 185 | |
| 186 | |