blob: c1eddcdd217f04874250bac9435a69d594bfe4d8 [file] [log] [blame]
Jordy Rose43d9f0d2012-05-16 16:01:10 +00001// RUN: %clang_cc1 -analyze -analyzer-checker=core,experimental.core,debug.ExprInspection -analyzer-store=region -analyzer-constraints=basic -verify %s
2// RUN: %clang_cc1 -analyze -analyzer-checker=core,experimental.core,debug.ExprInspection -analyzer-store=region -analyzer-constraints=range -verify %s
3
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