1 // RUN: %clang_cc1 -fsanitize=implicit-integer-sign-change -fsanitize-recover=implicit-integer-sign-change -emit-llvm %s -o - -triple x86_64-linux-gnu | FileCheck %s -implicit-check-not="call void @__ubsan_handle_implicit_conversion" --check-prefixes=CHECK
4 // * Two types - int and char
5 // * Two signs - signed and unsigned
6 // * Square that - we have input and output types.
7 // Thus, there are total of (2*2)^2 == 16 tests.
8 // These are all the possible variations/combinations of casts.
9 // However, not all of them should result in the check.
10 // So here, we *only* check which should and which should not result in checks.
12 // CHECK-DAG: @[[LINE_900_SIGN_CHANGE:.*]] = {{.*}}, i32 900, i32 10 }, {{.*}}, {{.*}}, i8 3 }
13 // CHECK-DAG: @[[LINE_1000_SIGN_CHANGE:.*]] = {{.*}}, i32 1000, i32 10 }, {{.*}}, {{.*}}, i8 3 }
14 // CHECK-DAG: @[[LINE_1100_SIGN_CHANGE:.*]] = {{.*}}, i32 1100, i32 10 }, {{.*}}, {{.*}}, i8 3 }
15 // CHECK-DAG: @[[LINE_1200_SIGN_CHANGE:.*]] = {{.*}}, i32 1200, i32 10 }, {{.*}}, {{.*}}, i8 3 }
16 // CHECK-DAG: @[[LINE_1300_SIGN_CHANGE:.*]] = {{.*}}, i32 1300, i32 10 }, {{.*}}, {{.*}}, i8 3 }
17 // CHECK-DAG: @[[LINE_1400_SIGN_CHANGE:.*]] = {{.*}}, i32 1400, i32 10 }, {{.*}}, {{.*}}, i8 3 }
18 // CHECK-DAG: @[[LINE_1500_SIGN_CHANGE:.*]] = {{.*}}, i32 1500, i32 10 }, {{.*}}, {{.*}}, i8 3 }
19 // CHECK-DAG: @[[LINE_1600_SIGN_CHANGE:.*]] = {{.*}}, i32 1600, i32 10 }, {{.*}}, {{.*}}, i8 3 }
21 //============================================================================//
22 // Half of the cases do not need the check. //
23 //============================================================================//
25 //----------------------------------------------------------------------------//
26 // No cast happens at all. No check needed.
27 //----------------------------------------------------------------------------//
29 // CHECK-LABEL: @convert_unsigned_int_to_unsigned_int
30 unsigned int convert_unsigned_int_to_unsigned_int(unsigned int x
) {
35 // CHECK-LABEL: @convert_unsigned_char_to_unsigned_char
36 unsigned char convert_unsigned_char_to_unsigned_char(unsigned char x
) {
41 // CHECK-LABEL: @convert_signed_int_to_signed_int
42 signed int convert_signed_int_to_signed_int(signed int x
) {
47 // CHECK-LABEL: @convert_signed_char_to_signed_char
48 signed char convert_signed_char_to_signed_char(signed char x
) {
53 //----------------------------------------------------------------------------//
54 // Both types are unsigned. No check needed.
55 //----------------------------------------------------------------------------//
57 // CHECK-LABEL: @convert_unsigned_int_to_unsigned_char
58 unsigned char convert_unsigned_int_to_unsigned_char(unsigned int x
) {
63 // CHECK-LABEL: @convert_unsigned_char_to_unsigned_int
64 unsigned int convert_unsigned_char_to_unsigned_int(unsigned char x
) {
69 //----------------------------------------------------------------------------//
70 // Source type was unsigned, destination type is signed, but non-negative.
71 // Because zero-extension happens - the sign bit will be 0. No check needed.
72 //----------------------------------------------------------------------------//
74 // CHECK-LABEL: @convert_unsigned_char_to_signed_int
75 signed int convert_unsigned_char_to_signed_int(unsigned char x
) {
80 //----------------------------------------------------------------------------//
81 // Both types are signed, and have the same sign, since sign-extension happens,
82 // i.e. the sign bit will be propagated. No check needed.
83 //----------------------------------------------------------------------------//
85 // CHECK-LABEL: @convert_signed_char_to_signed_int
86 signed int convert_signed_char_to_signed_int(signed char x
) {
91 //============================================================================//
92 // The remaining 8 cases *do* need the check. //
93 //============================================================================//
95 // These 3 result in simple 'icmp sge i32 %x, 0'
97 // CHECK-LABEL: @convert_unsigned_int_to_signed_int
98 signed int convert_unsigned_int_to_signed_int(unsigned int x
) {
99 // CHECK: call void @__ubsan_handle_implicit_conversion(ptr @[[LINE_900_SIGN_CHANGE]]
104 // CHECK-LABEL: @convert_signed_int_to_unsigned_int
105 unsigned int convert_signed_int_to_unsigned_int(signed int x
) {
106 // CHECK: call void @__ubsan_handle_implicit_conversion(ptr @[[LINE_1000_SIGN_CHANGE]]
111 // CHECK-LABEL: @convert_signed_int_to_unsigned_char
112 unsigned char convert_signed_int_to_unsigned_char(signed int x
) {
113 // CHECK: call void @__ubsan_handle_implicit_conversion(ptr @[[LINE_1100_SIGN_CHANGE]]
118 // These 3 result in simple 'icmp sge i8 %x, 0'
120 // CHECK-LABEL: @convert_signed_char_to_unsigned_char
121 unsigned char convert_signed_char_to_unsigned_char(signed char x
) {
122 // CHECK: call void @__ubsan_handle_implicit_conversion(ptr @[[LINE_1200_SIGN_CHANGE]]
127 // CHECK-LABEL: @convert_unsigned_char_to_signed_char
128 signed char convert_unsigned_char_to_signed_char(unsigned char x
) {
129 // CHECK: call void @__ubsan_handle_implicit_conversion(ptr @[[LINE_1300_SIGN_CHANGE]]
134 // CHECK-LABEL: @convert_signed_char_to_unsigned_int
135 unsigned int convert_signed_char_to_unsigned_int(signed char x
) {
136 // CHECK: call void @__ubsan_handle_implicit_conversion(ptr @[[LINE_1400_SIGN_CHANGE]]
141 // 'icmp sge i8 (trunc i32 %x), 0'
143 // CHECK-LABEL: @convert_unsigned_int_to_signed_char
144 signed char convert_unsigned_int_to_signed_char(unsigned int x
) {
145 // CHECK: call void @__ubsan_handle_implicit_conversion(ptr @[[LINE_1500_SIGN_CHANGE]]
150 // 'xor i1 (icmp sge i8 (trunc i32 %x), 0), (icmp sge i32 %x, 0)'
152 // CHECK-LABEL: @convert_signed_int_to_signed_char
153 signed char convert_signed_int_to_signed_char(signed int x
) {
154 // CHECK: call void @__ubsan_handle_implicit_conversion(ptr @[[LINE_1600_SIGN_CHANGE]]