blob: ddb9f4b1167cc529eb3c4e374dbd7d94a50bfd79 [file] [log] [blame]
Jordan Rose752bee22012-07-06 21:59:56 +00001// 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
Jordy Rose43d9f0d2012-05-16 16:01:10 +00003
4void clang_analyzer_eval(int);
Jordy Rose23b736e2010-07-29 07:11:59 +00005
6int string_literal_init() {
7 char a[] = "abc";
8 char b[2] = "abc"; // expected-warning{{too long}}
9 char c[5] = "abc";
10
Jordy Rose43d9f0d2012-05-16 16:01:10 +000011 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 Rose23b736e2010-07-29 07:11:59 +000014
Jordy Rose43d9f0d2012-05-16 16:01:10 +000015 clang_analyzer_eval(a[3] == 0); // expected-warning{{TRUE}}
16 clang_analyzer_eval(c[3] == 0); // expected-warning{{TRUE}}
Jordy Rose23b736e2010-07-29 07:11:59 +000017
Jordy Rose43d9f0d2012-05-16 16:01:10 +000018 clang_analyzer_eval(c[4] == 0); // expected-warning{{TRUE}}
Jordy Rose23b736e2010-07-29 07:11:59 +000019
20 return 42;
21}
Jordy Rose59b6dca2010-08-20 01:05:59 +000022
23void nested_compound_literals(int rad) {
David Blaikiebe0ee872012-05-15 16:56:36 +000024 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 Rose59b6dca2010-08-20 01:05:59 +000026 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}
Jordy Rosef1139402012-05-12 17:32:59 +000044
45
46void struct_as_array() {
Jordy Rose43d9f0d2012-05-16 16:01:10 +000047 struct simple { int x; int y; };
Jordy Rosef1139402012-05-12 17:32:59 +000048 struct simple a;
49 struct simple *p = &a;
Jordy Rose43d9f0d2012-05-16 16:01:10 +000050
Jordy Rosef1139402012-05-12 17:32:59 +000051 p->x = 5;
Jordy Rose43d9f0d2012-05-16 16:01:10 +000052 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 Rosef1139402012-05-12 17:32:59 +000058}
59
Jordan Rose752bee22012-07-06 21:59:56 +000060
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