220 lines · c
1// RUN: %clang_cc1 -Wno-error=incompatible-pointer-types -Wno-int-conversion -triple i386-unknown-unknown %s -emit-llvm -o - | FileCheck %s -check-prefixes=CHECK,NULL-INVALID2// RUN: %clang_cc1 -Wno-error=incompatible-pointer-types -Wno-int-conversion -triple i386-unknown-unknown %s -emit-llvm -fno-delete-null-pointer-checks -o - | FileCheck %s -check-prefixes=CHECK,NULL-VALID3 4int b(char* x);5 6// Extremely basic VLA test7void a(int x) {8 char arry[x];9 arry[0] = 10;10 b(arry);11}12 13int c(int n)14{15 return sizeof(int[n]);16}17 18int f0(int x) {19 int vla[x];20 return vla[x-1];21}22 23void24f(int count)25{26 int a[count];27 28 do { } while (0);29 30 if (a[0] != 3) {31 }32}33 34void g(int count) {35 // Make sure we emit sizes correctly in some obscure cases36 int (*a[5])[count];37 int (*b)[][count];38}39 40// CHECK-LABEL: define{{.*}} void @f_840310841void f_8403108(unsigned x) {42 // CHECK: call ptr @llvm.stacksave.p0()43 char s1[x];44 while (1) {45 // CHECK: call ptr @llvm.stacksave.p0()46 char s2[x];47 if (1)48 break;49 // CHECK: call void @llvm.stackrestore.p0(ptr50 }51 // CHECK: call void @llvm.stackrestore.p0(ptr52}53 54// pr782755void function(short width, int data[][width]) {} // expected-note {{passing argument to parameter 'data' here}}56 57void test(void) {58 int bork[4][13];59 // CHECK: call void @function(i16 noundef signext 1, ptr noundef null)60 function(1, 0);61 // CHECK: call void @function(i16 noundef signext 1, ptr noundef inttoptr62 function(1, 0xbadbeef); // expected-warning {{incompatible integer to pointer conversion passing}}63 // CHECK: call void @function(i16 noundef signext 1, ptr noundef {{.*}})64 function(1, bork);65}66 67void function1(short width, int data[][width][width]) {}68void test1(void) {69 int bork[4][13][15];70 // CHECK: call void @function1(i16 noundef signext 1, ptr noundef {{.*}})71 function1(1, bork);72 // CHECK: call void @function(i16 noundef signext 1, ptr noundef {{.*}})73 function(1, bork[2]);74}75 76static int GLOB;77int test2(int n)78{79 GLOB = 0;80 char b[1][n+3]; /* Variable length array. */81 // CHECK: [[tmp_1:%.*]] = load i32, ptr @GLOB, align 482 // CHECK-NEXT: add nsw i32 [[tmp_1]], 183 __typeof__(b[GLOB++]) c;84 return GLOB;85}86 87// http://llvm.org/PR856788// CHECK-LABEL: define{{.*}} double @test_PR856789double test_PR8567(int n, double (*p)[n][5]) {90 // CHECK: [[NV:%.*]] = alloca i32, align 491 // CHECK-NEXT: [[PV:%.*]] = alloca ptr, align 492 // CHECK-NEXT: store93 // CHECK-NEXT: store94 // CHECK-NEXT: [[N:%.*]] = load i32, ptr [[NV]], align 495 // CHECK-NEXT: [[P:%.*]] = load ptr, ptr [[PV]], align 496 // CHECK-NEXT: [[T0:%.*]] = mul nsw i32 1, [[N]]97 // CHECK-NEXT: [[T1:%.*]] = getelementptr inbounds [5 x double], ptr [[P]], i32 [[T0]]98 // CHECK-NEXT: [[T2:%.*]] = getelementptr inbounds [5 x double], ptr [[T1]], i32 299 // CHECK-NEXT: [[T3:%.*]] = getelementptr inbounds [5 x double], ptr [[T2]], i32 0, i32 3100 // CHECK-NEXT: [[T4:%.*]] = load double, ptr [[T3]]101 // CHECK-NEXT: ret double [[T4]]102 return p[1][2][3];103}104 105int test4(unsigned n, char (*p)[n][n+1][6]) {106 // CHECK-LABEL: define{{.*}} i32 @test4(107 // CHECK: [[N:%.*]] = alloca i32, align 4108 // CHECK-NEXT: [[P:%.*]] = alloca ptr, align 4109 // CHECK-NEXT: [[P2:%.*]] = alloca ptr, align 4110 // CHECK-NEXT: store i32111 // CHECK-NEXT: store ptr112 113 // VLA captures.114 // CHECK-NEXT: [[DIM0:%.*]] = load i32, ptr [[N]], align 4115 // CHECK-NEXT: [[T0:%.*]] = load i32, ptr [[N]], align 4116 // CHECK-NEXT: [[DIM1:%.*]] = add i32 [[T0]], 1117 118 // CHECK-NEXT: [[T0:%.*]] = load ptr, ptr [[P]], align 4119 // CHECK-NEXT: [[T1:%.*]] = load i32, ptr [[N]], align 4120 // CHECK-NEXT: [[T2:%.*]] = udiv i32 [[T1]], 2121 // CHECK-NEXT: [[T3:%.*]] = mul nuw i32 [[DIM0]], [[DIM1]]122 // CHECK-NEXT: [[T4:%.*]] = mul nsw i32 [[T2]], [[T3]]123 // CHECK-NEXT: [[T5:%.*]] = getelementptr inbounds nuw [6 x i8], ptr [[T0]], i32 [[T4]]124 // CHECK-NEXT: [[T6:%.*]] = load i32, ptr [[N]], align 4125 // CHECK-NEXT: [[T7:%.*]] = udiv i32 [[T6]], 4126 // CHECK-NEXT: [[T8:%.*]] = sub i32 0, [[T7]]127 // CHECK-NEXT: [[T9:%.*]] = mul nuw i32 [[DIM0]], [[DIM1]]128 // CHECK-NEXT: [[T10:%.*]] = mul nsw i32 [[T8]], [[T9]]129 // CHECK-NEXT: [[T11:%.*]] = getelementptr inbounds [6 x i8], ptr [[T5]], i32 [[T10]]130 // CHECK-NEXT: store ptr [[T11]], ptr [[P2]], align 4131 __typeof(p) p2 = (p + n/2) - n/4;132 133 // CHECK-NEXT: [[T0:%.*]] = load ptr, ptr [[P2]], align 4134 // CHECK-NEXT: [[T1:%.*]] = load ptr, ptr [[P]], align 4135 // CHECK-NEXT: [[T2:%.*]] = ptrtoint ptr [[T0]] to i32136 // CHECK-NEXT: [[T3:%.*]] = ptrtoint ptr [[T1]] to i32137 // CHECK-NEXT: [[T4:%.*]] = sub i32 [[T2]], [[T3]]138 // CHECK-NEXT: [[T5:%.*]] = mul nuw i32 [[DIM0]], [[DIM1]]139 // CHECK-NEXT: [[T6:%.*]] = mul nuw i32 6, [[T5]]140 // CHECK-NEXT: [[T7:%.*]] = sdiv exact i32 [[T4]], [[T6]]141 // CHECK-NEXT: ret i32 [[T7]]142 return p2 - p;143}144 145void test5(void)146{147 // CHECK-LABEL: define{{.*}} void @test5(148 int a[5], i = 0;149 // CHECK: [[A:%.*]] = alloca [5 x i32], align 4150 // CHECK-NEXT: [[I:%.*]] = alloca i32, align 4151 // CHECK-NEXT: [[CL:%.*]] = alloca ptr, align 4152 // CHECK-NEXT: store i32 0, ptr [[I]], align 4153 154 (typeof(++i, (int (*)[i])a)){&a} += 0;155 // CHECK-NEXT: [[Z:%.*]] = load i32, ptr [[I]], align 4156 // CHECK-NEXT: [[INC:%.*]] = add nsw i32 [[Z]], 1157 // CHECK-NEXT: store i32 [[INC]], ptr [[I]], align 4158 // CHECK-NEXT: [[O:%.*]] = load i32, ptr [[I]], align 4159 // CHECK-NEXT: [[AR:%.*]] = getelementptr inbounds [5 x i32], ptr [[A]], i32 0, i32 0160 // CHECK-NEXT: store ptr [[A]], ptr [[CL]]161 // CHECK-NEXT: [[TH:%.*]] = load ptr, ptr [[CL]]162 // CHECK-NEXT: [[VLAIX:%.*]] = mul nsw i32 0, [[O]]163 // CHECK-NEXT: [[ADDPTR:%.*]] = getelementptr inbounds i32, ptr [[TH]], i32 [[VLAIX]]164 // CHECK-NEXT: store ptr [[ADDPTR]], ptr [[CL]]165}166 167void test6(void)168{169 // CHECK-LABEL: define{{.*}} void @test6(170 int n = 20, **a, i=0;171 // CHECK: [[N:%.*]] = alloca i32, align 4172 // CHECK-NEXT: [[A:%.*]] = alloca ptr, align 4173 // CHECK-NEXT: [[I:%.*]] = alloca i32, align 4174 (int (**)[i]){&a}[0][1][5] = 0;175 // CHECK-NEXT: [[CL:%.*]] = alloca ptr, align 4176 // CHECK-NEXT: store i32 20, ptr [[N]], align 4177 // CHECK-NEXT: store i32 0, ptr [[I]], align 4178 // CHECK-NEXT: [[Z:%.*]] = load i32, ptr [[I]], align 4179 // CHECK-NEXT: store ptr [[A]], ptr [[CL]]180 // CHECK-NEXT: [[T:%.*]] = load ptr, ptr [[CL]]181 // CHECK-NEXT: [[IX:%.*]] = getelementptr inbounds ptr, ptr [[T]], i32 0182 // CHECK-NEXT: [[TH:%.*]] = load ptr, ptr [[IX]], align 4183 // CHECK-NEXT: [[F:%.*]] = mul nsw i32 1, [[Z]]184 // CHECK-NEXT: [[IX1:%.*]] = getelementptr inbounds i32, ptr [[TH]], i32 [[F]]185 // CHECK-NEXT: [[IX2:%.*]] = getelementptr inbounds i32, ptr [[IX1]], i32 5186 // CHECK-NEXT: store i32 0, ptr [[IX2]], align 4187}188 189// Follow gcc's behavior for VLAs in parameter lists. PR9559.190void test7(int a[b(0)]) {191 // CHECK-LABEL: define{{.*}} void @test7(192 // CHECK: call i32 @b(ptr noundef null)193}194 195// Make sure we emit dereferenceable or nonnull when the static keyword is196// provided.197void test8(int a[static 3]) { }198// CHECK: define{{.*}} void @test8(ptr noundef align 4 dereferenceable(12) %a)199 200void test9(int n, int a[static n]) { }201// NULL-INVALID: define{{.*}} void @test9(i32 noundef %n, ptr noundef nonnull align 4 %a)202// NULL-VALID: define{{.*}} void @test9(i32 noundef %n, ptr noundef align 4 %a)203 204// Make sure a zero-sized static array extent is still required to be nonnull.205void test10(int a[static 0]) {}206// NULL-INVALID: define{{.*}} void @test10(ptr noundef nonnull align 4 %a)207// NULL-VALID: define{{.*}} void @test10(ptr noundef align 4 %a)208 209const int constant = 32;210// CHECK: define {{.*}}pr44406(211int pr44406(void) {212 int n = 0;213 // Do not fold this VLA to an array of constant bound; that would miscompile214 // this testcase.215 char c[1][(constant - constant) + 3];216 // CHECK: store i32 1,217 sizeof(c[n = 1]);218 return n;219}220