1 // RUN: %clang_cc1 -emit-llvm %s -o - -triple x86_64-linux-gnu | FileCheck %s --check-prefix=CHECK
2 // RUN: %clang_cc1 -fsanitize=implicit-unsigned-integer-truncation,implicit-signed-integer-truncation -fno-sanitize-recover=implicit-unsigned-integer-truncation,implicit-signed-integer-truncation -emit-llvm %s -o - -triple x86_64-linux-gnu | FileCheck %s --check-prefixes=CHECK,CHECK-SANITIZE
3 // RUN: %clang_cc1 -fsanitize=implicit-unsigned-integer-truncation,implicit-signed-integer-truncation -fsanitize-recover=implicit-unsigned-integer-truncation,implicit-signed-integer-truncation -emit-llvm %s -o - -triple x86_64-linux-gnu | FileCheck %s --check-prefixes=CHECK,CHECK-SANITIZE
4 // RUN: %clang_cc1 -fsanitize=implicit-unsigned-integer-truncation,implicit-signed-integer-truncation -fsanitize-trap=implicit-unsigned-integer-truncation,implicit-signed-integer-truncation -emit-llvm %s -o - -triple x86_64-linux-gnu | FileCheck %s --check-prefixes=CHECK,CHECK-SANITIZE
6 extern "C" { // Disable name mangling.
8 // ========================================================================== //
9 // Check that explicit cast does not interfere with implicit conversion
10 // ========================================================================== //
11 // These contain one implicit truncating conversion, and one explicit truncating cast.
12 // We want to make sure that we still diagnose the implicit conversion.
14 // Implicit truncation after explicit truncation.
15 // CHECK-LABEL: @explicit_cast_interference0
16 unsigned char explicit_cast_interference0(unsigned int c
) {
17 // CHECK-SANITIZE: %[[ANYEXT:.*]] = zext i8 %[[DST:.*]] to i16, !nosanitize
18 // CHECK-SANITIZE: call
19 // CHECK-SANITIZE-NOT: call
21 return (unsigned short)c
;
24 // Implicit truncation before explicit truncation.
25 // CHECK-LABEL: @explicit_cast_interference1
26 unsigned char explicit_cast_interference1(unsigned int c
) {
27 // CHECK-SANITIZE: %[[ANYEXT:.*]] = zext i16 %[[DST:.*]] to i32, !nosanitize
28 // CHECK-SANITIZE: call
29 // CHECK-SANITIZE-NOT: call
32 return (unsigned char)(b
= c
);
35 // ========================================================================== //
36 // The expected true-negatives.
37 // ========================================================================== //
39 // Explicit truncating casts.
40 // ========================================================================== //
42 // CHECK-LABEL: @explicit_unsigned_int_to_unsigned_char
43 unsigned char explicit_unsigned_int_to_unsigned_char(unsigned int src
) {
44 // CHECK-SANITIZE-NOT: call
46 return (unsigned char)src
;
49 // CHECK-LABEL: @explicit_signed_int_to_unsigned_char
50 unsigned char explicit_signed_int_to_unsigned_char(signed int src
) {
51 // CHECK-SANITIZE-NOT: call
53 return (unsigned char)src
;
56 // CHECK-LABEL: @explicit_unsigned_int_to_signed_char
57 signed char explicit_unsigned_int_to_signed_char(unsigned int src
) {
58 // CHECK-SANITIZE-NOT: call
60 return (signed char)src
;
63 // CHECK-LABEL: @explicit_signed_int_to_signed_char
64 signed char explicit_signed_int_to_signed_char(signed int src
) {
65 // CHECK-SANITIZE-NOT: call
67 return (signed char)src
;
70 // Explicit NOP casts.
71 // ========================================================================== //
73 // CHECK-LABEL: @explicit_unsigned_int_to_unsigned_int
74 unsigned int explicit_unsigned_int_to_unsigned_int(unsigned int src
) {
75 // CHECK-SANITIZE-NOT: call
77 return (unsigned int)src
;
80 // CHECK-LABEL: @explicit_signed_int_to_signed_int
81 signed int explicit_signed_int_to_signed_int(signed int src
) {
82 // CHECK-SANITIZE-NOT: call
84 return (signed int)src
;
87 // CHECK-LABEL: @explicit_unsigned_char_to_signed_char
88 unsigned char explicit_unsigned_char_to_signed_char(unsigned char src
) {
89 // CHECK-SANITIZE-NOT: call
91 return (unsigned char)src
;
94 // CHECK-LABEL: @explicit_signed_char_to_signed_char
95 signed char explicit_signed_char_to_signed_char(signed char src
) {
96 // CHECK-SANITIZE-NOT: call
98 return (signed char)src
;
101 // Explicit functional truncating casts.
102 // ========================================================================== //
104 using UnsignedChar
= unsigned char;
105 using SignedChar
= signed char;
106 using UnsignedInt
= unsigned int;
107 using SignedInt
= signed int;
109 // CHECK-LABEL: @explicit_functional_unsigned_int_to_unsigned_char
110 unsigned char explicit_functional_unsigned_int_to_unsigned_char(unsigned int src
) {
111 // CHECK-SANITIZE-NOT: call
113 return UnsignedChar(src
);
116 // CHECK-LABEL: @explicit_functional_signed_int_to_unsigned_char
117 unsigned char explicit_functional_signed_int_to_unsigned_char(signed int src
) {
118 // CHECK-SANITIZE-NOT: call
120 return UnsignedChar(src
);
123 // CHECK-LABEL: @explicit_functional_unsigned_int_to_signed_char
124 signed char explicit_functional_unsigned_int_to_signed_char(unsigned int src
) {
125 // CHECK-SANITIZE-NOT: call
127 return SignedChar(src
);
130 // CHECK-LABEL: @explicit_functional_signed_int_to_signed_char
131 signed char explicit_functional_signed_int_to_signed_char(signed int src
) {
132 // CHECK-SANITIZE-NOT: call
134 return SignedChar(src
);
137 // Explicit functional NOP casts.
138 // ========================================================================== //
140 // CHECK-LABEL: @explicit_functional_unsigned_int_to_unsigned_int
141 unsigned int explicit_functional_unsigned_int_to_unsigned_int(unsigned int src
) {
142 // CHECK-SANITIZE-NOT: call
144 return UnsignedInt(src
);
147 // CHECK-LABEL: @explicit_functional_signed_int_to_signed_int
148 signed int explicit_functional_signed_int_to_signed_int(signed int src
) {
149 // CHECK-SANITIZE-NOT: call
151 return SignedInt(src
);
154 // CHECK-LABEL: @explicit_functional_unsigned_char_to_signed_char
155 unsigned char explicit_functional_unsigned_char_to_signed_char(unsigned char src
) {
156 // CHECK-SANITIZE-NOT: call
158 return UnsignedChar(src
);
161 // CHECK-LABEL: @explicit_functional_signed_char_to_signed_char
162 signed char explicit_functional_signed_char_to_signed_char(signed char src
) {
163 // CHECK-SANITIZE-NOT: call
165 return SignedChar(src
);
168 // Explicit C++-style casts truncating casts.
169 // ========================================================================== //
171 // CHECK-LABEL: @explicit_cppstyleunsigned_int_to_unsigned_char
172 unsigned char explicit_cppstyleunsigned_int_to_unsigned_char(unsigned int src
) {
173 // CHECK-SANITIZE-NOT: call
175 return static_cast<unsigned char>(src
);
178 // CHECK-LABEL: @explicit_cppstylesigned_int_to_unsigned_char
179 unsigned char explicit_cppstylesigned_int_to_unsigned_char(signed int src
) {
180 // CHECK-SANITIZE-NOT: call
182 return static_cast<unsigned char>(src
);
185 // CHECK-LABEL: @explicit_cppstyleunsigned_int_to_signed_char
186 signed char explicit_cppstyleunsigned_int_to_signed_char(unsigned int src
) {
187 // CHECK-SANITIZE-NOT: call
189 return static_cast<signed char>(src
);
192 // CHECK-LABEL: @explicit_cppstylesigned_int_to_signed_char
193 signed char explicit_cppstylesigned_int_to_signed_char(signed int src
) {
194 // CHECK-SANITIZE-NOT: call
196 return static_cast<signed char>(src
);
199 // Explicit C++-style casts NOP casts.
200 // ========================================================================== //
202 // CHECK-LABEL: @explicit_cppstyleunsigned_int_to_unsigned_int
203 unsigned int explicit_cppstyleunsigned_int_to_unsigned_int(unsigned int src
) {
204 // CHECK-SANITIZE-NOT: call
206 return static_cast<unsigned int>(src
);
209 // CHECK-LABEL: @explicit_cppstylesigned_int_to_signed_int
210 signed int explicit_cppstylesigned_int_to_signed_int(signed int src
) {
211 // CHECK-SANITIZE-NOT: call
213 return static_cast<signed int>(src
);
216 // CHECK-LABEL: @explicit_cppstyleunsigned_char_to_signed_char
217 unsigned char explicit_cppstyleunsigned_char_to_signed_char(unsigned char src
) {
218 // CHECK-SANITIZE-NOT: call
220 return static_cast<unsigned char>(src
);
223 // CHECK-LABEL: @explicit_cppstylesigned_char_to_signed_char
224 signed char explicit_cppstylesigned_char_to_signed_char(signed char src
) {
225 // CHECK-SANITIZE-NOT: call
227 return static_cast<signed char>(src
);
232 // ---------------------------------------------------------------------------//
233 // A problematic true-negative involving simple C++ code.
234 // The problem is tha the NoOp ExplicitCast is directly within MaterializeTemporaryExpr(),
235 // so a special care is neeeded.
236 // See https://reviews.llvm.org/D48958#1161345
237 template <typename a
>
238 a
b(a c
, const a
&d
) {
244 extern "C" { // Disable name mangling.
246 // CHECK-LABEL: @false_positive_with_MaterializeTemporaryExpr
247 int false_positive_with_MaterializeTemporaryExpr() {
248 // CHECK-SANITIZE-NOT: call{{.*}}ubsan
250 int e
= b
<unsigned>(4, static_cast<unsigned>(4294967296));
254 // ---------------------------------------------------------------------------//