Run DCE after a LoopFlatten test to reduce spurious output [nfc]
[llvm-project.git] / clang / test / CodeGenCXX / cfi-cast.cpp
blob6801e043b81fba870d76367da9878dd52847853e
1 // RUN: %clang_cc1 -triple x86_64-unknown-linux -fvisibility=hidden -std=c++11 -fsanitize=cfi-derived-cast -fsanitize-trap=cfi-derived-cast -emit-llvm -o - %s | FileCheck -check-prefix=CHECK-DCAST %s
2 // RUN: %clang_cc1 -triple x86_64-unknown-linux -fvisibility=hidden -std=c++11 -fsanitize=cfi-unrelated-cast -fsanitize-trap=cfi-unrelated-cast -emit-llvm -o - %s | FileCheck -check-prefix=CHECK-UCAST %s
3 // RUN: %clang_cc1 -triple x86_64-unknown-linux -fvisibility=hidden -std=c++11 -fsanitize=cfi-unrelated-cast,cfi-cast-strict -fsanitize-trap=cfi-unrelated-cast,cfi-cast-strict -emit-llvm -o - %s | FileCheck -check-prefix=CHECK-UCAST-STRICT %s
5 // In this test the main thing we are searching for is something like
6 // 'metadata !"1B"' where "1B" is the mangled name of the class we are
7 // casting to (or maybe its base class in non-strict mode).
9 struct A {
10 virtual void f();
11 int i() const;
14 struct B : A {
15 virtual void f();
18 struct C : A {};
20 // CHECK-DCAST-LABEL: define hidden void @_Z3abpP1A
21 void abp(A *a) {
22 // CHECK-DCAST: [[P:%[^ ]*]] = call i1 @llvm.type.test(ptr {{%[^ ]*}}, metadata !"_ZTS1B")
23 // CHECK-DCAST-NEXT: br i1 [[P]], label %[[CONTBB:[^ ]*]], label %[[TRAPBB:[^ ,]*]]
25 // CHECK-DCAST: [[TRAPBB]]
26 // CHECK-DCAST-NEXT: call void @llvm.ubsantrap(i8 2)
27 // CHECK-DCAST-NEXT: unreachable
29 // CHECK-DCAST: [[CONTBB]]
30 // CHECK-DCAST: ret
31 (void)static_cast<B*>(a);
34 // CHECK-DCAST-LABEL: define hidden void @_Z3abrR1A
35 void abr(A &a) {
36 // CHECK-DCAST: [[P:%[^ ]*]] = call i1 @llvm.type.test(ptr {{%[^ ]*}}, metadata !"_ZTS1B")
37 // CHECK-DCAST-NEXT: br i1 [[P]], label %[[CONTBB:[^ ]*]], label %[[TRAPBB:[^ ,]*]]
39 // CHECK-DCAST: [[TRAPBB]]
40 // CHECK-DCAST-NEXT: call void @llvm.ubsantrap(i8 2)
41 // CHECK-DCAST-NEXT: unreachable
43 // CHECK-DCAST: [[CONTBB]]
44 // CHECK-DCAST: ret
45 (void)static_cast<B&>(a);
48 // CHECK-DCAST-LABEL: define hidden void @_Z4abrrO1A
49 void abrr(A &&a) {
50 // CHECK-DCAST: [[P:%[^ ]*]] = call i1 @llvm.type.test(ptr {{%[^ ]*}}, metadata !"_ZTS1B")
51 // CHECK-DCAST-NEXT: br i1 [[P]], label %[[CONTBB:[^ ]*]], label %[[TRAPBB:[^ ,]*]]
53 // CHECK-DCAST: [[TRAPBB]]
54 // CHECK-DCAST-NEXT: call void @llvm.ubsantrap(i8 2)
55 // CHECK-DCAST-NEXT: unreachable
57 // CHECK-DCAST: [[CONTBB]]
58 // CHECK-DCAST: ret
59 (void)static_cast<B&&>(a);
62 // CHECK-UCAST-LABEL: define hidden void @_Z3vbpPv
63 void vbp(void *p) {
64 // CHECK-UCAST: [[P:%[^ ]*]] = call i1 @llvm.type.test(ptr {{%[^ ]*}}, metadata !"_ZTS1B")
65 // CHECK-UCAST-NEXT: br i1 [[P]], label %[[CONTBB:[^ ]*]], label %[[TRAPBB:[^ ,]*]]
67 // CHECK-UCAST: [[TRAPBB]]
68 // CHECK-UCAST-NEXT: call void @llvm.ubsantrap(i8 2)
69 // CHECK-UCAST-NEXT: unreachable
71 // CHECK-UCAST: [[CONTBB]]
72 // CHECK-UCAST: ret
73 (void)static_cast<B*>(p);
76 // CHECK-UCAST-LABEL: define hidden void @_Z3vbrRc
77 void vbr(char &r) {
78 // CHECK-UCAST: [[P:%[^ ]*]] = call i1 @llvm.type.test(ptr {{%[^ ]*}}, metadata !"_ZTS1B")
79 // CHECK-UCAST-NEXT: br i1 [[P]], label %[[CONTBB:[^ ]*]], label %[[TRAPBB:[^ ,]*]]
81 // CHECK-UCAST: [[TRAPBB]]
82 // CHECK-UCAST-NEXT: call void @llvm.ubsantrap(i8 2)
83 // CHECK-UCAST-NEXT: unreachable
85 // CHECK-UCAST: [[CONTBB]]
86 // CHECK-UCAST: ret
87 (void)reinterpret_cast<B&>(r);
90 // CHECK-UCAST-LABEL: define hidden void @_Z4vbrrOc
91 void vbrr(char &&r) {
92 // CHECK-UCAST: [[P:%[^ ]*]] = call i1 @llvm.type.test(ptr {{%[^ ]*}}, metadata !"_ZTS1B")
93 // CHECK-UCAST-NEXT: br i1 [[P]], label %[[CONTBB:[^ ]*]], label %[[TRAPBB:[^ ,]*]]
95 // CHECK-UCAST: [[TRAPBB]]
96 // CHECK-UCAST-NEXT: call void @llvm.ubsantrap(i8 2)
97 // CHECK-UCAST-NEXT: unreachable
99 // CHECK-UCAST: [[CONTBB]]
100 // CHECK-UCAST: ret
101 (void)reinterpret_cast<B&&>(r);
104 // CHECK-UCAST-LABEL: define hidden void @_Z3vcpPv
105 // CHECK-UCAST-STRICT-LABEL: define hidden void @_Z3vcpPv
106 void vcp(void *p) {
107 // CHECK-UCAST: call i1 @llvm.type.test(ptr {{%[^ ]*}}, metadata !"_ZTS1A")
108 // CHECK-UCAST-STRICT: call i1 @llvm.type.test(ptr {{%[^ ]*}}, metadata !"_ZTS1C")
109 (void)static_cast<C*>(p);
112 // CHECK-UCAST-LABEL: define hidden void @_Z3bcpP1B
113 // CHECK-UCAST-STRICT-LABEL: define hidden void @_Z3bcpP1B
114 void bcp(B *p) {
115 // CHECK-UCAST: call i1 @llvm.type.test(ptr {{%[^ ]*}}, metadata !"_ZTS1A")
116 // CHECK-UCAST-STRICT: call i1 @llvm.type.test(ptr {{%[^ ]*}}, metadata !"_ZTS1C")
117 (void)(C *)p;
120 // CHECK-UCAST-LABEL: define hidden void @_Z8bcp_callP1B
121 // CHECK-UCAST-STRICT-LABEL: define hidden void @_Z8bcp_callP1B
122 void bcp_call(B *p) {
123 // CHECK-UCAST: call i1 @llvm.type.test(ptr {{%[^ ]*}}, metadata !"_ZTS1A")
124 // CHECK-UCAST-STRICT: call i1 @llvm.type.test(ptr {{%[^ ]*}}, metadata !"_ZTS1C")
125 ((C *)p)->f();
128 // CHECK-UCAST-LABEL: define hidden noundef i32 @_Z6a_callP1A
129 // CHECK-UCAST-STRICT-LABEL: define hidden noundef i32 @_Z6a_callP1A
130 int a_call(A *a) {
131 // CHECK-UCAST-NOT: @llvm.type.test
132 // CHECK-UCAST-STRICT-NOT: @llvm.type.test
133 return a->i();