array-struct-region.c revision 752bee2493ec2931bd18899753552e3a47dc85fe
1// RUN: %clang_cc1 -analyze -analyzer-checker=core,experimental.core,debug.ExprInspection -analyzer-store=region -analyzer-constraints=basic -analyzer-ipa=all -verify %s
2// RUN: %clang_cc1 -analyze -analyzer-checker=core,experimental.core,debug.ExprInspection -analyzer-store=region -analyzer-constraints=range -analyzer-ipa=all -verify %s
3
4void clang_analyzer_eval(int);
5
6int string_literal_init() {
7  char a[] = "abc";
8  char b[2] = "abc"; // expected-warning{{too long}}
9  char c[5] = "abc";
10
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}}
14
15  clang_analyzer_eval(a[3] == 0); // expected-warning{{TRUE}}
16  clang_analyzer_eval(c[3] == 0); // expected-warning{{TRUE}}
17
18  clang_analyzer_eval(c[4] == 0); // expected-warning{{TRUE}}
19
20  return 42;
21}
22
23void nested_compound_literals(int rad) {
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}}
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
34void 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}
44
45
46void struct_as_array() {
47  struct simple { int x; int y; };
48  struct simple a;
49  struct simple *p = &a;
50
51  p->x = 5;
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}}
58}
59
60
61// PR13264 / <rdar://problem/11802440>
62struct point { int x; int y; };
63struct circle { struct point o; int r; };
64struct circle get_circle() {
65  struct circle result;
66  result.r = 5;
67  result.o = (struct point){0, 0};
68  return result;
69}
70
71void 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.
80struct circle_f { struct point o; float r; };
81struct 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
88float struct_in_struct_f() {
89  struct circle_f c;
90  c = get_circle_f();
91
92  return c.r; // no-warning
93}
94
95