xref: /llvm-project/clang/test/CodeGen/nonnull.c (revision 39db5e1ed87363a9ffea81e53520b542201b3262)
1 // RUN: %clang_cc1 -triple x86_64-apple-darwin -emit-llvm < %s | FileCheck -check-prefix=NULL-INVALID %s
2 // RUN: %clang_cc1 -triple x86_64-apple-darwin -emit-llvm -fno-delete-null-pointer-checks < %s | FileCheck -check-prefix=NULL-VALID %s
3 
4 // NULL-INVALID: define{{.*}} void @foo(ptr noundef nonnull %x)
5 // NULL-VALID: define{{.*}} void @foo(ptr noundef %x)
foo(int * x)6 void foo(int * __attribute__((nonnull)) x) {
7   *x = 0;
8 }
9 
10 // NULL-INVALID: define{{.*}} void @bar(ptr noundef nonnull %x)
11 // NULL-VALID: define{{.*}} void @bar(ptr noundef %x)
bar(int * x)12 void bar(int * x) __attribute__((nonnull(1)))  {
13   *x = 0;
14 }
15 
16 // NULL-INVALID: define{{.*}} void @bar2(ptr noundef %x, ptr noundef nonnull %y)
17 // NULL-VALID: define{{.*}} void @bar2(ptr noundef %x, ptr noundef %y)
bar2(int * x,int * y)18 void bar2(int * x, int * y) __attribute__((nonnull(2)))  {
19   *x = 0;
20 }
21 
22 static int a;
23 // NULL-INVALID: define{{.*}} nonnull ptr @bar3()
24 // NULL-VALID: define{{.*}} ptr @bar3()
bar3(void)25 int * bar3(void) __attribute__((returns_nonnull))  {
26   return &a;
27 }
28 
29 // NULL-INVALID: define{{.*}} i32 @bar4(i32 noundef %n, ptr noundef nonnull %p)
30 // NULL-VALID: define{{.*}} i32 @bar4(i32 noundef %n, ptr noundef %p)
bar4(int n,int * p)31 int bar4(int n, int *p) __attribute__((nonnull)) {
32   return n + *p;
33 }
34 
35 // NULL-INVALID: define{{.*}} i32 @bar5(i32 noundef %n, ptr noundef nonnull %p)
36 // NULL-VALID: define{{.*}} i32 @bar5(i32 noundef %n, ptr noundef %p)
bar5(int n,int * p)37 int bar5(int n, int *p) __attribute__((nonnull(1, 2))) {
38   return n + *p;
39 }
40 
41 typedef union {
42   unsigned long long n;
43   int *p;
44   double d;
45 } TransparentUnion __attribute__((transparent_union));
46 
47 // NULL-INVALID: define{{.*}} i32 @bar6(i64 %
48 // NULL-VALID: define{{.*}} i32 @bar6(i64 %
bar6(TransparentUnion tu)49 int bar6(TransparentUnion tu) __attribute__((nonnull(1))) {
50   return *tu.p;
51 }
52 
53 // NULL-INVALID: define{{.*}} void @bar7(ptr noundef nonnull %a, ptr noundef nonnull %b)
54 // NULL-VALID: define{{.*}} void @bar7(ptr noundef %a, ptr noundef %b)
bar7(int * a,int * b)55 void bar7(int *a, int *b) __attribute__((nonnull(1)))
56 __attribute__((nonnull(2))) {}
57 
58 // NULL-INVALID: define{{.*}} void @bar8(ptr noundef nonnull %a, ptr noundef nonnull %b)
59 // NULL-VALID: define{{.*}} void @bar8(ptr noundef %a, ptr noundef %b)
bar8(int * a,int * b)60 void bar8(int *a, int *b) __attribute__((nonnull))
61 __attribute__((nonnull(1))) {}
62