xref: /llvm-project/clang/test/CodeGenCXX/lifetime-sanitizer.cpp (revision 3512721d52b3380ea4d3f5b2419d0b7b072e7797)
1 // RUN: %clang -w -target x86_64-linux-gnu -S -emit-llvm -o - -fno-exceptions -O0 \
2 // RUN:     -Xclang -disable-llvm-passes %s | FileCheck %s -check-prefixes=CHECK \
3 // RUN:      --implicit-check-not=llvm.lifetime
4 // RUN: %clang -w -target x86_64-linux-gnu -S -emit-llvm -o - -fno-exceptions -O0 \
5 // RUN:     -fsanitize=address -fsanitize-address-use-after-scope \
6 // RUN:     -Xclang -disable-llvm-passes %s | FileCheck %s -check-prefixes=CHECK,LIFETIME
7 // RUN: %clang -w -target x86_64-linux-gnu -S -emit-llvm -o - -fno-exceptions -O0 \
8 // RUN:     -fsanitize=memory -Xclang -disable-llvm-passes %s | \
9 // RUN:     FileCheck %s -check-prefixes=CHECK,LIFETIME
10 // RUN: %clang -w -target aarch64-linux-gnu -S -emit-llvm -o - -fno-exceptions -O0 \
11 // RUN:     -fsanitize=hwaddress -Xclang -disable-llvm-passes %s | \
12 // RUN:     FileCheck %s -check-prefixes=CHECK,LIFETIME
13 
14 extern int bar(char *A, int n);
15 
16 struct X {
17   X();
18   ~X();
19   int *p;
20 };
21 struct Y {
22   Y();
23   int *p;
24 };
25 
26 extern "C" void a(), b(), c(), d();
27 
28 // CHECK: define dso_local void @_Z3fooi(i32 noundef %[[N:[^)]+]])
foo(int n)29 void foo(int n) {
30   // CHECK: store i32 %[[N]], ptr %[[NADDR:[^,]+]]
31   // CHECK-LABEL: call void @a()
32   a();
33 
34   // CHECK-LABEL: call void @b()
35   // CHECK: [[NARG:%[^ ]+]] = load i32, ptr %[[NADDR]]
36   // CHECK: [[BOOL:%[^ ]+]] = icmp ne i32 [[NARG]], 0
37   // CHECK: store i1 false
38   // CHECK: br i1 [[BOOL]], label %[[ONTRUE:[^,]+]], label %[[ONFALSE:[^,]+]]
39   //
40   // CHECK: [[ONTRUE]]:
41   // LIFETIME: @llvm.lifetime.start
42   // LIFETIME: store i1 true
43   // LIFETIME: call void @_ZN1XC
44   // CHECK: br label %[[END:[^,]+]]
45   //
46   // CHECK: [[ONFALSE]]:
47   // LIFETIME: @llvm.lifetime.start
48   // LIFETIME: store i1 true
49   // LIFETIME: call void @_ZN1YC
50   // CHECK: br label %[[END]]
51   //
52   // CHECK: [[END]]:
53   // CHECK: call void @c()
54   // LIFETIME: @llvm.lifetime.end
55   // LIFETIME: @llvm.lifetime.end
56   b(), (n ? X().p : Y().p), c();
57 
58   // CHECK: call void @d()
59   d();
60 }
61