[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 x86_64-apple-darwin -emit-llvm < %s | FileCheck -check-prefix=NULL-INVALID %s
|
|
|
|
// RUN: %clang_cc1 -triple x86_64-apple-darwin -emit-llvm -fno-delete-null-pointer-checks < %s | FileCheck -check-prefix=NULL-VALID %s
|
2014-07-12 01:35:21 +08:00
|
|
|
|
2022-01-16 17:53:11 +08:00
|
|
|
// NULL-INVALID: define{{.*}} void @foo(i32* noundef nonnull %x)
|
|
|
|
// NULL-VALID: define{{.*}} void @foo(i32* noundef %x)
|
2014-07-12 01:35:21 +08:00
|
|
|
void foo(int * __attribute__((nonnull)) x) {
|
|
|
|
*x = 0;
|
|
|
|
}
|
|
|
|
|
2022-01-16 17:53:11 +08:00
|
|
|
// NULL-INVALID: define{{.*}} void @bar(i32* noundef nonnull %x)
|
|
|
|
// NULL-VALID: define{{.*}} void @bar(i32* noundef %x)
|
2014-07-12 01:35:21 +08:00
|
|
|
void bar(int * x) __attribute__((nonnull(1))) {
|
|
|
|
*x = 0;
|
|
|
|
}
|
|
|
|
|
2022-01-16 17:53:11 +08:00
|
|
|
// NULL-INVALID: define{{.*}} void @bar2(i32* noundef %x, i32* noundef nonnull %y)
|
|
|
|
// NULL-VALID: define{{.*}} void @bar2(i32* noundef %x, i32* noundef %y)
|
2014-07-12 01:35:21 +08:00
|
|
|
void bar2(int * x, int * y) __attribute__((nonnull(2))) {
|
|
|
|
*x = 0;
|
|
|
|
}
|
|
|
|
|
2014-07-12 12:51:04 +08:00
|
|
|
static int a;
|
2020-12-31 12:45:56 +08:00
|
|
|
// NULL-INVALID: define{{.*}} nonnull i32* @bar3()
|
|
|
|
// NULL-VALID: define{{.*}} i32* @bar3()
|
2022-02-16 05:06:01 +08:00
|
|
|
int * bar3(void) __attribute__((returns_nonnull)) {
|
2014-07-12 12:51:04 +08:00
|
|
|
return &a;
|
|
|
|
}
|
|
|
|
|
2022-01-16 17:53:11 +08:00
|
|
|
// NULL-INVALID: define{{.*}} i32 @bar4(i32 noundef %n, i32* noundef nonnull %p)
|
|
|
|
// NULL-VALID: define{{.*}} i32 @bar4(i32 noundef %n, i32* noundef %p)
|
2014-08-28 02:56:18 +08:00
|
|
|
int bar4(int n, int *p) __attribute__((nonnull)) {
|
|
|
|
return n + *p;
|
|
|
|
}
|
|
|
|
|
2022-01-16 17:53:11 +08:00
|
|
|
// NULL-INVALID: define{{.*}} i32 @bar5(i32 noundef %n, i32* noundef nonnull %p)
|
|
|
|
// NULL-VALID: define{{.*}} i32 @bar5(i32 noundef %n, i32* noundef %p)
|
2014-08-28 02:56:18 +08:00
|
|
|
int bar5(int n, int *p) __attribute__((nonnull(1, 2))) {
|
|
|
|
return n + *p;
|
|
|
|
}
|
|
|
|
|
|
|
|
typedef union {
|
|
|
|
unsigned long long n;
|
|
|
|
int *p;
|
|
|
|
double d;
|
|
|
|
} TransparentUnion __attribute__((transparent_union));
|
|
|
|
|
2020-12-31 12:45:56 +08:00
|
|
|
// NULL-INVALID: define{{.*}} i32 @bar6(i64 %
|
|
|
|
// NULL-VALID: define{{.*}} i32 @bar6(i64 %
|
2014-08-28 02:56:18 +08:00
|
|
|
int bar6(TransparentUnion tu) __attribute__((nonnull(1))) {
|
|
|
|
return *tu.p;
|
|
|
|
}
|
2014-08-28 08:53:20 +08:00
|
|
|
|
2022-01-16 17:53:11 +08:00
|
|
|
// NULL-INVALID: define{{.*}} void @bar7(i32* noundef nonnull %a, i32* noundef nonnull %b)
|
|
|
|
// NULL-VALID: define{{.*}} void @bar7(i32* noundef %a, i32* noundef %b)
|
2014-08-28 08:53:20 +08:00
|
|
|
void bar7(int *a, int *b) __attribute__((nonnull(1)))
|
|
|
|
__attribute__((nonnull(2))) {}
|
|
|
|
|
2022-01-16 17:53:11 +08:00
|
|
|
// NULL-INVALID: define{{.*}} void @bar8(i32* noundef nonnull %a, i32* noundef nonnull %b)
|
|
|
|
// NULL-VALID: define{{.*}} void @bar8(i32* noundef %a, i32* noundef %b)
|
2014-08-28 08:53:20 +08:00
|
|
|
void bar8(int *a, int *b) __attribute__((nonnull))
|
|
|
|
__attribute__((nonnull(1))) {}
|