xref: /minix3/external/bsd/llvm/dist/clang/test/Analysis/region-store.c (revision f4a2713ac843a11c696ec80c0a5e3e5d80b4d338)
1*f4a2713aSLionel Sambuc // RUN: %clang_cc1 -analyze -analyzer-checker=core,unix,debug.ExprInspection -verify %s
2*f4a2713aSLionel Sambuc 
3*f4a2713aSLionel Sambuc int printf(const char *restrict,...);
4*f4a2713aSLionel Sambuc 
5*f4a2713aSLionel Sambuc // Testing core functionality of the region store.
6*f4a2713aSLionel Sambuc // radar://10127782
compoundLiteralTest()7*f4a2713aSLionel Sambuc int compoundLiteralTest() {
8*f4a2713aSLionel Sambuc     int index = 0;
9*f4a2713aSLionel Sambuc     for (index = 0; index < 2; index++) {
10*f4a2713aSLionel Sambuc         int thing = (int []){0, 1}[index];
11*f4a2713aSLionel Sambuc         printf("thing: %i\n", thing);
12*f4a2713aSLionel Sambuc     }
13*f4a2713aSLionel Sambuc     return 0;
14*f4a2713aSLionel Sambuc }
15*f4a2713aSLionel Sambuc 
compoundLiteralTest2()16*f4a2713aSLionel Sambuc int compoundLiteralTest2() {
17*f4a2713aSLionel Sambuc     int index = 0;
18*f4a2713aSLionel Sambuc     for (index = 0; index < 3; index++) {
19*f4a2713aSLionel Sambuc         int thing = (int [][3]){{0,0,0}, {1,1,1}, {2,2,2}}[index][index];
20*f4a2713aSLionel Sambuc         printf("thing: %i\n", thing);
21*f4a2713aSLionel Sambuc     }
22*f4a2713aSLionel Sambuc     return 0;
23*f4a2713aSLionel Sambuc }
24*f4a2713aSLionel Sambuc 
concreteOffsetBindingIsInvalidatedBySymbolicOffsetAssignment(int length,int i)25*f4a2713aSLionel Sambuc int concreteOffsetBindingIsInvalidatedBySymbolicOffsetAssignment(int length,
26*f4a2713aSLionel Sambuc                                                                  int i) {
27*f4a2713aSLionel Sambuc   int values[length];
28*f4a2713aSLionel Sambuc   values[i] = 4;
29*f4a2713aSLionel Sambuc   return values[0]; // no-warning
30*f4a2713aSLionel Sambuc }
31*f4a2713aSLionel Sambuc 
32*f4a2713aSLionel Sambuc struct X{
33*f4a2713aSLionel Sambuc   int mem;
34*f4a2713aSLionel Sambuc };
35*f4a2713aSLionel Sambuc int initStruct(struct X *st);
structOffsetBindingIsInvalidated(int length,int i)36*f4a2713aSLionel Sambuc int structOffsetBindingIsInvalidated(int length, int i){
37*f4a2713aSLionel Sambuc   struct X l;
38*f4a2713aSLionel Sambuc   initStruct(&l);
39*f4a2713aSLionel Sambuc   return l.mem; // no-warning
40*f4a2713aSLionel Sambuc }
41*f4a2713aSLionel Sambuc 
42*f4a2713aSLionel Sambuc void clang_analyzer_eval(int);
testConstraintOnRegionOffset(int * values,int length,int i)43*f4a2713aSLionel Sambuc void testConstraintOnRegionOffset(int *values, int length, int i){
44*f4a2713aSLionel Sambuc   if (values[1] == 4) {
45*f4a2713aSLionel Sambuc     values[i] = 5;
46*f4a2713aSLionel Sambuc     clang_analyzer_eval(values[1] == 4);// expected-warning {{UNKNOWN}}
47*f4a2713aSLionel Sambuc   }
48*f4a2713aSLionel Sambuc }
49*f4a2713aSLionel Sambuc 
50*f4a2713aSLionel Sambuc int initArray(int *values);
testConstraintOnRegionOffsetStack(int * values,int length,int i)51*f4a2713aSLionel Sambuc void testConstraintOnRegionOffsetStack(int *values, int length, int i) {
52*f4a2713aSLionel Sambuc   if (values[0] == 4) {
53*f4a2713aSLionel Sambuc     initArray(values);
54*f4a2713aSLionel Sambuc     clang_analyzer_eval(values[0] == 4);// expected-warning {{UNKNOWN}}
55*f4a2713aSLionel Sambuc   }
56*f4a2713aSLionel Sambuc }
57