1 // RUN: %clang_analyze_cc1 -analyzer-checker=core -analyzer-checker=debug.ExprInspection -verify %s
3 typedef unsigned long size_t;
4 size_t clang_analyzer_getExtent(void *);
5 void clang_analyzer_eval(int);
8 void check_zero_sized_VLA(int x
) {
12 int vla
[x
]; // expected-warning{{Declared variable-length array (VLA) has zero size}}
15 void check_uninit_sized_VLA(void) {
17 int vla
[x
]; // expected-warning{{Declared variable-length array (VLA) uses a garbage value as its size}}
21 static void vla_allocate_signed(short x
) {
22 int vla
[x
]; // expected-warning{{Declared variable-length array (VLA) has negative size}}
25 static void vla_allocate_unsigned(unsigned short x
) {
26 int vla
[x
]; // no-warning
29 void check_negative_sized_VLA_1(void) {
30 vla_allocate_signed(-1);
33 void check_negative_sized_VLA_2(void) {
34 vla_allocate_unsigned(-1);
37 void check_negative_sized_VLA_3(void) {
39 int vla
[x
]; // expected-warning{{Declared variable-length array (VLA) has negative size}}
42 void check_negative_sized_VLA_4(void) {
43 unsigned short x
= -1;
44 int vla
[x
]; // no-warning
47 void check_negative_sized_VLA_5(void) {
49 int vla
[x
]; // expected-warning{{Declared variable-length array (VLA) has negative size}}
52 void check_negative_sized_VLA_6(void) {
54 int vla
[x
]; // no-warning
57 void check_negative_sized_VLA_7(void) {
59 int vla
[x
+ 2]; // no-warning
62 void check_negative_sized_VLA_8(void) {
64 int vla
[x
- 2]; // expected-warning{{Declared variable-length array (VLA) has negative size}}
67 void check_negative_sized_VLA_9(void) {
69 int vla
[x
]; // no-warning
72 static void check_negative_sized_VLA_10_sub(int x
)
74 int vla
[x
]; // expected-warning{{Declared variable-length array (VLA) has negative size}}
77 void check_negative_sized_VLA_10(int x
) {
79 check_negative_sized_VLA_10_sub(x
);
82 static void check_negative_sized_VLA_11_sub(short x
)
84 int vla
[x
]; // no-warning
87 void check_negative_sized_VLA_11(short x
) {
89 check_negative_sized_VLA_11_sub(x
);
92 void check_VLA_typedef(void) {
94 typedef int VLA
[x
]; // expected-warning{{Declared variable-length array (VLA) has negative size}}
97 size_t check_VLA_sizeof(void) {
99 size_t s
= sizeof(int[x
]); // expected-warning{{Declared variable-length array (VLA) has negative size}}
103 // Multi-dimensional arrays.
105 void check_zero_sized_VLA_multi1(int x
) {
109 int vla
[10][x
]; // expected-warning{{Declared variable-length array (VLA) has zero size}}
112 void check_zero_sized_VLA_multi2(int x
, int y
) {
116 int vla
[y
][x
]; // expected-warning{{Declared variable-length array (VLA) has zero size}}
121 void check_VLA_extent(void) {
125 clang_analyzer_eval(clang_analyzer_getExtent(&vla1
) == x
* sizeof(int));
126 // expected-warning@-1{{TRUE}}
129 clang_analyzer_eval(clang_analyzer_getExtent(&vla2
) == x
* 2 * sizeof(int));
130 // expected-warning@-1{{TRUE}}
133 clang_analyzer_eval(clang_analyzer_getExtent(&vla2m
) == 2 * x
* sizeof(int));
134 // expected-warning@-1{{TRUE}}
137 clang_analyzer_eval(clang_analyzer_getExtent(&vla3m
) == 2 * x
* 4 * sizeof(int));
138 // expected-warning@-1{{TRUE}}
141 // https://bugs.llvm.org/show_bug.cgi?id=46128
142 // analyzer doesn't handle more than simple symbolic expressions.
144 extern void foo(void);