[clang]: Add support for "-fno-delete-null-pointer-checks"
Summary:
Support for this option is needed for building Linux kernel.
This is a very frequently requested feature by kernel developers.
More details : https://lkml.org/lkml/2018/4/4/601
GCC option description for -fdelete-null-pointer-checks:
This Assume that programs cannot safely dereference null pointers,
and that no code or data element resides at address zero.
-fno-delete-null-pointer-checks is the inverse of this implying that
null pointer dereferencing is not undefined.
This feature is implemented in as the function attribute
"null-pointer-is-valid"="true".
This CL only adds the attribute on the function.
It also strips "nonnull" attributes from function arguments but
keeps the related warnings unchanged.
Corresponding LLVM change rL336613 already updated the
optimizations to not treat null pointer dereferencing
as undefined if the attribute is present.
Reviewers: t.p.northover, efriedma, jyknight, chandlerc, rnk, srhines, void, george.burgess.iv
Reviewed By: jyknight
Subscribers: drinkcat, xbolva00, cfe-commits
Differential Revision: https://reviews.llvm.org/D47894
llvm-svn: 337433
2018-07-19 08:44:52 +08:00
|
|
|
// RUN: %clang_cc1 -triple i386-unknown-unknown %s -emit-llvm -o - | FileCheck %s -check-prefixes=CHECK,NULL-INVALID
|
|
|
|
// RUN: %clang_cc1 -triple i386-unknown-unknown %s -emit-llvm -fno-delete-null-pointer-checks -o - | FileCheck %s -check-prefixes=CHECK,NULL-VALID
|
2008-12-21 07:11:59 +08:00
|
|
|
|
|
|
|
int b(char* x);
|
|
|
|
|
|
|
|
// Extremely basic VLA test
|
|
|
|
void a(int x) {
|
|
|
|
char arry[x];
|
|
|
|
arry[0] = 10;
|
|
|
|
b(arry);
|
|
|
|
}
|
2008-12-21 11:33:21 +08:00
|
|
|
|
2008-12-21 11:40:32 +08:00
|
|
|
int c(int n)
|
2008-12-21 11:33:21 +08:00
|
|
|
{
|
2008-12-21 11:40:32 +08:00
|
|
|
return sizeof(int[n]);
|
2008-12-21 11:33:21 +08:00
|
|
|
}
|
2009-02-10 05:48:07 +08:00
|
|
|
|
|
|
|
int f0(int x) {
|
|
|
|
int vla[x];
|
|
|
|
return vla[x-1];
|
|
|
|
}
|
2009-02-11 06:50:24 +08:00
|
|
|
|
|
|
|
void
|
|
|
|
f(int count)
|
|
|
|
{
|
|
|
|
int a[count];
|
|
|
|
|
|
|
|
do { } while (0);
|
|
|
|
|
|
|
|
if (a[0] != 3) {
|
|
|
|
}
|
|
|
|
}
|
2009-05-30 03:23:46 +08:00
|
|
|
|
|
|
|
void g(int count) {
|
|
|
|
// Make sure we emit sizes correctly in some obscure cases
|
|
|
|
int (*a[5])[count];
|
|
|
|
int (*b)[][count];
|
|
|
|
}
|
2010-09-14 08:42:34 +08:00
|
|
|
|
|
|
|
// rdar://8403108
|
2020-02-04 02:09:39 +08:00
|
|
|
// CHECK-LABEL: define void @f_8403108
|
2010-09-14 08:42:34 +08:00
|
|
|
void f_8403108(unsigned x) {
|
|
|
|
// CHECK: call i8* @llvm.stacksave()
|
|
|
|
char s1[x];
|
|
|
|
while (1) {
|
|
|
|
// CHECK: call i8* @llvm.stacksave()
|
|
|
|
char s2[x];
|
|
|
|
if (1)
|
|
|
|
break;
|
|
|
|
// CHECK: call void @llvm.stackrestore(i8*
|
|
|
|
}
|
|
|
|
// CHECK: call void @llvm.stackrestore(i8*
|
|
|
|
}
|
2010-09-22 06:53:33 +08:00
|
|
|
|
|
|
|
// pr7827
|
2010-09-25 01:30:16 +08:00
|
|
|
void function(short width, int data[][width]) {} // expected-note {{passing argument to parameter 'data' here}}
|
2010-09-22 06:53:33 +08:00
|
|
|
|
|
|
|
void test() {
|
2010-09-25 01:30:16 +08:00
|
|
|
int bork[4][13];
|
2010-09-22 06:53:33 +08:00
|
|
|
// CHECK: call void @function(i16 signext 1, i32* null)
|
|
|
|
function(1, 0);
|
2010-09-25 01:30:16 +08:00
|
|
|
// CHECK: call void @function(i16 signext 1, i32* inttoptr
|
|
|
|
function(1, 0xbadbeef); // expected-warning {{incompatible integer to pointer conversion passing}}
|
|
|
|
// CHECK: call void @function(i16 signext 1, i32* {{.*}})
|
|
|
|
function(1, bork);
|
|
|
|
}
|
|
|
|
|
|
|
|
void function1(short width, int data[][width][width]) {}
|
|
|
|
void test1() {
|
|
|
|
int bork[4][13][15];
|
|
|
|
// CHECK: call void @function1(i16 signext 1, i32* {{.*}})
|
|
|
|
function1(1, bork);
|
|
|
|
// CHECK: call void @function(i16 signext 1, i32* {{.*}})
|
|
|
|
function(1, bork[2]);
|
2010-09-22 06:53:33 +08:00
|
|
|
}
|
|
|
|
|
2010-09-29 04:42:35 +08:00
|
|
|
// rdar://8476159
|
|
|
|
static int GLOB;
|
|
|
|
int test2(int n)
|
|
|
|
{
|
|
|
|
GLOB = 0;
|
|
|
|
char b[1][n+3]; /* Variable length array. */
|
2015-02-28 05:19:58 +08:00
|
|
|
// CHECK: [[tmp_1:%.*]] = load i32, i32* @GLOB, align 4
|
2010-09-29 04:42:35 +08:00
|
|
|
// CHECK-NEXT: add nsw i32 [[tmp_1]], 1
|
|
|
|
__typeof__(b[GLOB++]) c;
|
|
|
|
return GLOB;
|
|
|
|
}
|
|
|
|
|
2010-11-09 09:30:48 +08:00
|
|
|
// http://llvm.org/PR8567
|
2020-02-04 02:09:39 +08:00
|
|
|
// CHECK-LABEL: define double @test_PR8567
|
2010-11-09 09:30:48 +08:00
|
|
|
double test_PR8567(int n, double (*p)[n][5]) {
|
2011-06-25 05:55:10 +08:00
|
|
|
// CHECK: [[NV:%.*]] = alloca i32, align 4
|
|
|
|
// CHECK-NEXT: [[PV:%.*]] = alloca [5 x double]*, align 4
|
|
|
|
// CHECK-NEXT: store
|
|
|
|
// CHECK-NEXT: store
|
2015-02-28 05:19:58 +08:00
|
|
|
// CHECK-NEXT: [[N:%.*]] = load i32, i32* [[NV]], align 4
|
|
|
|
// CHECK-NEXT: [[P:%.*]] = load [5 x double]*, [5 x double]** [[PV]], align 4
|
2011-06-25 09:32:37 +08:00
|
|
|
// CHECK-NEXT: [[T0:%.*]] = mul nsw i32 1, [[N]]
|
2015-02-28 03:18:17 +08:00
|
|
|
// CHECK-NEXT: [[T1:%.*]] = getelementptr inbounds [5 x double], [5 x double]* [[P]], i32 [[T0]]
|
|
|
|
// CHECK-NEXT: [[T2:%.*]] = getelementptr inbounds [5 x double], [5 x double]* [[T1]], i32 2
|
|
|
|
// CHECK-NEXT: [[T3:%.*]] = getelementptr inbounds [5 x double], [5 x double]* [[T2]], i32 0, i32 3
|
2015-02-28 05:19:58 +08:00
|
|
|
// CHECK-NEXT: [[T4:%.*]] = load double, double* [[T3]]
|
2011-06-25 05:55:10 +08:00
|
|
|
// CHECK-NEXT: ret double [[T4]]
|
2010-11-09 09:30:48 +08:00
|
|
|
return p[1][2][3];
|
|
|
|
}
|
2011-06-25 09:32:37 +08:00
|
|
|
|
|
|
|
int test4(unsigned n, char (*p)[n][n+1][6]) {
|
2020-02-04 02:09:39 +08:00
|
|
|
// CHECK-LABEL: define i32 @test4(
|
2011-06-25 09:32:37 +08:00
|
|
|
// CHECK: [[N:%.*]] = alloca i32, align 4
|
|
|
|
// CHECK-NEXT: [[P:%.*]] = alloca [6 x i8]*, align 4
|
|
|
|
// CHECK-NEXT: [[P2:%.*]] = alloca [6 x i8]*, align 4
|
|
|
|
// CHECK-NEXT: store i32
|
|
|
|
// CHECK-NEXT: store [6 x i8]*
|
|
|
|
|
|
|
|
// VLA captures.
|
2015-02-28 05:19:58 +08:00
|
|
|
// CHECK-NEXT: [[DIM0:%.*]] = load i32, i32* [[N]], align 4
|
|
|
|
// CHECK-NEXT: [[T0:%.*]] = load i32, i32* [[N]], align 4
|
2011-06-25 09:32:37 +08:00
|
|
|
// CHECK-NEXT: [[DIM1:%.*]] = add i32 [[T0]], 1
|
|
|
|
|
2015-02-28 05:19:58 +08:00
|
|
|
// CHECK-NEXT: [[T0:%.*]] = load [6 x i8]*, [6 x i8]** [[P]], align 4
|
|
|
|
// CHECK-NEXT: [[T1:%.*]] = load i32, i32* [[N]], align 4
|
2011-06-25 09:32:37 +08:00
|
|
|
// CHECK-NEXT: [[T2:%.*]] = udiv i32 [[T1]], 2
|
|
|
|
// CHECK-NEXT: [[T3:%.*]] = mul nuw i32 [[DIM0]], [[DIM1]]
|
|
|
|
// CHECK-NEXT: [[T4:%.*]] = mul nsw i32 [[T2]], [[T3]]
|
2015-02-28 03:18:17 +08:00
|
|
|
// CHECK-NEXT: [[T5:%.*]] = getelementptr inbounds [6 x i8], [6 x i8]* [[T0]], i32 [[T4]]
|
2015-02-28 05:19:58 +08:00
|
|
|
// CHECK-NEXT: [[T6:%.*]] = load i32, i32* [[N]], align 4
|
2011-06-25 09:32:37 +08:00
|
|
|
// CHECK-NEXT: [[T7:%.*]] = udiv i32 [[T6]], 4
|
|
|
|
// CHECK-NEXT: [[T8:%.*]] = sub i32 0, [[T7]]
|
|
|
|
// CHECK-NEXT: [[T9:%.*]] = mul nuw i32 [[DIM0]], [[DIM1]]
|
|
|
|
// CHECK-NEXT: [[T10:%.*]] = mul nsw i32 [[T8]], [[T9]]
|
2015-02-28 03:18:17 +08:00
|
|
|
// CHECK-NEXT: [[T11:%.*]] = getelementptr inbounds [6 x i8], [6 x i8]* [[T5]], i32 [[T10]]
|
2011-06-25 09:32:37 +08:00
|
|
|
// CHECK-NEXT: store [6 x i8]* [[T11]], [6 x i8]** [[P2]], align 4
|
|
|
|
__typeof(p) p2 = (p + n/2) - n/4;
|
|
|
|
|
2015-02-28 05:19:58 +08:00
|
|
|
// CHECK-NEXT: [[T0:%.*]] = load [6 x i8]*, [6 x i8]** [[P2]], align 4
|
|
|
|
// CHECK-NEXT: [[T1:%.*]] = load [6 x i8]*, [6 x i8]** [[P]], align 4
|
2011-06-25 09:32:37 +08:00
|
|
|
// CHECK-NEXT: [[T2:%.*]] = ptrtoint [6 x i8]* [[T0]] to i32
|
|
|
|
// CHECK-NEXT: [[T3:%.*]] = ptrtoint [6 x i8]* [[T1]] to i32
|
|
|
|
// CHECK-NEXT: [[T4:%.*]] = sub i32 [[T2]], [[T3]]
|
|
|
|
// CHECK-NEXT: [[T5:%.*]] = mul nuw i32 [[DIM0]], [[DIM1]]
|
|
|
|
// CHECK-NEXT: [[T6:%.*]] = mul nuw i32 6, [[T5]]
|
|
|
|
// CHECK-NEXT: [[T7:%.*]] = sdiv exact i32 [[T4]], [[T6]]
|
|
|
|
// CHECK-NEXT: ret i32 [[T7]]
|
|
|
|
return p2 - p;
|
|
|
|
}
|
2012-06-08 01:07:15 +08:00
|
|
|
|
|
|
|
// rdar://11485774
|
|
|
|
void test5(void)
|
|
|
|
{
|
2020-02-04 02:09:39 +08:00
|
|
|
// CHECK-LABEL: define void @test5(
|
2012-06-08 01:07:15 +08:00
|
|
|
int a[5], i = 0;
|
|
|
|
// CHECK: [[A:%.*]] = alloca [5 x i32], align 4
|
|
|
|
// CHECK-NEXT: [[I:%.*]] = alloca i32, align 4
|
|
|
|
// CHECK-NEXT: [[CL:%.*]] = alloca i32*, align 4
|
|
|
|
// CHECK-NEXT: store i32 0, i32* [[I]], align 4
|
|
|
|
|
|
|
|
(typeof(++i, (int (*)[i])a)){&a} += 0;
|
2015-02-28 05:19:58 +08:00
|
|
|
// CHECK-NEXT: [[Z:%.*]] = load i32, i32* [[I]], align 4
|
2012-06-08 02:15:55 +08:00
|
|
|
// CHECK-NEXT: [[INC:%.*]] = add nsw i32 [[Z]], 1
|
|
|
|
// CHECK-NEXT: store i32 [[INC]], i32* [[I]], align 4
|
2015-02-28 05:19:58 +08:00
|
|
|
// CHECK-NEXT: [[O:%.*]] = load i32, i32* [[I]], align 4
|
2015-02-28 03:18:17 +08:00
|
|
|
// CHECK-NEXT: [[AR:%.*]] = getelementptr inbounds [5 x i32], [5 x i32]* [[A]], i32 0, i32 0
|
2012-06-08 02:15:55 +08:00
|
|
|
// CHECK-NEXT: [[T:%.*]] = bitcast [5 x i32]* [[A]] to i32*
|
|
|
|
// CHECK-NEXT: store i32* [[T]], i32** [[CL]]
|
2015-02-28 05:19:58 +08:00
|
|
|
// CHECK-NEXT: [[TH:%.*]] = load i32*, i32** [[CL]]
|
2012-06-08 02:15:55 +08:00
|
|
|
// CHECK-NEXT: [[VLAIX:%.*]] = mul nsw i32 0, [[O]]
|
2015-02-28 03:18:17 +08:00
|
|
|
// CHECK-NEXT: [[ADDPTR:%.*]] = getelementptr inbounds i32, i32* [[TH]], i32 [[VLAIX]]
|
2012-06-08 01:07:15 +08:00
|
|
|
// CHECK-NEXT: store i32* [[ADDPTR]], i32** [[CL]]
|
|
|
|
}
|
2012-06-08 02:15:55 +08:00
|
|
|
|
|
|
|
void test6(void)
|
|
|
|
{
|
2020-02-04 02:09:39 +08:00
|
|
|
// CHECK-LABEL: define void @test6(
|
2012-06-08 02:15:55 +08:00
|
|
|
int n = 20, **a, i=0;
|
|
|
|
// CHECK: [[N:%.*]] = alloca i32, align 4
|
|
|
|
// CHECK-NEXT: [[A:%.*]] = alloca i32**, align 4
|
|
|
|
// CHECK-NEXT: [[I:%.*]] = alloca i32, align 4
|
|
|
|
(int (**)[i]){&a}[0][1][5] = 0;
|
|
|
|
// CHECK-NEXT: [[CL:%.*]] = alloca i32**, align 4
|
|
|
|
// CHECK-NEXT: store i32 20, i32* [[N]], align 4
|
|
|
|
// CHECK-NEXT: store i32 0, i32* [[I]], align 4
|
2015-02-28 05:19:58 +08:00
|
|
|
// CHECK-NEXT: [[Z:%.*]] = load i32, i32* [[I]], align 4
|
2012-06-08 02:15:55 +08:00
|
|
|
// CHECK-NEXT: [[O:%.*]] = bitcast i32*** [[A]] to i32**
|
|
|
|
// CHECK-NEXT: store i32** [[O]], i32*** [[CL]]
|
2015-02-28 05:19:58 +08:00
|
|
|
// CHECK-NEXT: [[T:%.*]] = load i32**, i32*** [[CL]]
|
2015-02-28 03:18:17 +08:00
|
|
|
// CHECK-NEXT: [[IX:%.*]] = getelementptr inbounds i32*, i32** [[T]], i32 0
|
2015-02-28 05:19:58 +08:00
|
|
|
// CHECK-NEXT: [[TH:%.*]] = load i32*, i32** [[IX]], align 4
|
2012-06-08 02:15:55 +08:00
|
|
|
// CHECK-NEXT: [[F:%.*]] = mul nsw i32 1, [[Z]]
|
2015-02-28 03:18:17 +08:00
|
|
|
// CHECK-NEXT: [[IX1:%.*]] = getelementptr inbounds i32, i32* [[TH]], i32 [[F]]
|
|
|
|
// CHECK-NEXT: [[IX2:%.*]] = getelementptr inbounds i32, i32* [[IX1]], i32 5
|
2012-06-08 02:15:55 +08:00
|
|
|
// CHECK-NEXT: store i32 0, i32* [[IX2]], align 4
|
|
|
|
}
|
|
|
|
|
2012-11-15 06:09:59 +08:00
|
|
|
// Follow gcc's behavior for VLAs in parameter lists. PR9559.
|
|
|
|
void test7(int a[b(0)]) {
|
2020-02-04 02:09:39 +08:00
|
|
|
// CHECK-LABEL: define void @test7(
|
2012-11-15 06:09:59 +08:00
|
|
|
// CHECK: call i32 @b(i8* null)
|
|
|
|
}
|
2014-07-19 09:41:07 +08:00
|
|
|
|
|
|
|
// Make sure we emit dereferenceable or nonnull when the static keyword is
|
|
|
|
// provided.
|
|
|
|
void test8(int a[static 3]) { }
|
2020-02-04 02:09:39 +08:00
|
|
|
// CHECK: define void @test8(i32* dereferenceable(12) %a)
|
2014-07-19 09:41:07 +08:00
|
|
|
|
|
|
|
void test9(int n, int a[static n]) { }
|
2020-02-04 02:09:39 +08:00
|
|
|
// NULL-INVALID: define void @test9(i32 %n, i32* nonnull %a)
|
|
|
|
// NULL-VALID: define void @test9(i32 %n, i32* %a)
|
2014-07-19 09:41:07 +08:00
|
|
|
|
2020-07-11 03:54:57 +08:00
|
|
|
// Make sure a zero-sized static array extent is still required to be nonnull.
|
|
|
|
void test10(int a[static 0]) {}
|
|
|
|
// NULL-INVALID: define void @test10(i32* nonnull %a)
|
|
|
|
// NULL-VALID: define void @test10(i32* %a)
|