xref: /llvm-project/clang/test/CodeGen/nullptr.c (revision 0d501f38f348cf046d40c9baee12f0c5145b6d8c)
1 // RUN: %clang_cc1 %s -std=c2x -emit-llvm -o - | FileCheck %s
2 
3 // Test that null <-> nullptr_t conversions work as expected.
4 typedef typeof(nullptr) nullptr_t;
5 
6 nullptr_t nullptr_t_val;
7 
8 void bool_func(bool);
9 void nullptr_func(nullptr_t);
10 
test()11 void test() {
12   // Test initialization
13   bool bool_from_nullptr_t = nullptr_t_val;
14   nullptr_t nullptr_t_from_nullptr = nullptr;
15   void *vp_from_nullptr_t = nullptr_t_val;
16   nullptr_t nullptr_t_from_vp = (void *)0;
17   nullptr_t nullptr_t_from_int = 0;
18 
19   // Test assignment
20   bool_from_nullptr_t = nullptr_t_val;
21   nullptr_t_from_nullptr = nullptr;
22   vp_from_nullptr_t = nullptr_t_val;
23   nullptr_t_from_vp = (void *)0;
24   nullptr_t_from_int = 0;
25 
26   // Test calls
27   bool_func(nullptr_t_from_nullptr);
28   nullptr_func(nullptr_t_from_nullptr);
29   nullptr_func(0);
30   nullptr_func((void *)0);
31   nullptr_func(nullptr);
32   nullptr_func(false);
33 
34   // Allocation of locals
35   // CHECK: %[[bool_from_nullptr_t:.*]] = alloca i8
36   // CHECK: %[[nullptr_t_from_nullptr:.*]] = alloca ptr
37   // CHECK: %[[vp_from_nullptr_t:.*]] = alloca ptr
38   // CHECK: %[[nullptr_t_from_vp:.*]] = alloca ptr
39   // CHECK: %[[nullptr_t_from_int:.*]] = alloca ptr
40 
41   // Initialization of locals
42   // CHECK: store i8 0, ptr %[[bool_from_nullptr_t]]
43   // CHECK: store ptr null, ptr %[[nullptr_t_from_nullptr]]
44   // CHECK: store ptr null, ptr %[[vp_from_nullptr_t]]
45   // CHECK: store ptr null, ptr %[[nullptr_t_from_vp]]
46   // CHECK: store ptr null, ptr %[[nullptr_t_from_int]]
47 
48   // Assignment expressions
49   // CHECK: store i8 0, ptr %[[bool_from_nullptr_t]]
50   // CHECK: store ptr null, ptr %[[nullptr_t_from_nullptr]]
51   // CHECK: store ptr null, ptr %[[vp_from_nullptr_t]]
52   // CHECK: store ptr null, ptr %[[nullptr_t_from_vp]]
53   // CHECK: store ptr null, ptr %[[nullptr_t_from_int]]
54 
55   // Calls
56   // CHECK: call void @bool_func(i1 noundef {{(zeroext )?}}false)
57   // CHECK: call void @nullptr_func(ptr null)
58   // CHECK: call void @nullptr_func(ptr null)
59   // CHECK: call void @nullptr_func(ptr null)
60   // CHECK: call void @nullptr_func(ptr null)
61   // CHECK: call void @nullptr_func(ptr null)
62 }
63 
64