annotate clang/test/CodeGenCXX/catch-implicit-integer-truncations.cpp @ 206:f17a3b42b08b

Added tag before-12 for changeset b7591485f4cd
author Shinji KONO <kono@ie.u-ryukyu.ac.jp>
date Mon, 07 Jun 2021 21:25:57 +0900
parents 1d019706d866
children 2e18cbf3894f
Ignore whitespace changes - Everywhere: Within whitespace: At end of lines:
rev   line source
150
anatofuz
parents:
diff changeset
1 // RUN: %clang_cc1 -emit-llvm %s -o - -triple x86_64-linux-gnu | FileCheck %s --check-prefix=CHECK
anatofuz
parents:
diff changeset
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,CHECK-SANITIZE-ANYRECOVER,CHECK-SANITIZE-NORECOVER
anatofuz
parents:
diff changeset
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,CHECK-SANITIZE-ANYRECOVER,CHECK-SANITIZE-RECOVER
anatofuz
parents:
diff changeset
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,CHECK-SANITIZE-TRAP
anatofuz
parents:
diff changeset
5
anatofuz
parents:
diff changeset
6 extern "C" { // Disable name mangling.
anatofuz
parents:
diff changeset
7
anatofuz
parents:
diff changeset
8 // ========================================================================== //
anatofuz
parents:
diff changeset
9 // Check that explicit cast does not interfere with implicit conversion
anatofuz
parents:
diff changeset
10 // ========================================================================== //
anatofuz
parents:
diff changeset
11 // These contain one implicit truncating conversion, and one explicit truncating cast.
anatofuz
parents:
diff changeset
12 // We want to make sure that we still diagnose the implicit conversion.
anatofuz
parents:
diff changeset
13
anatofuz
parents:
diff changeset
14 // Implicit truncation after explicit truncation.
anatofuz
parents:
diff changeset
15 // CHECK-LABEL: @explicit_cast_interference0
anatofuz
parents:
diff changeset
16 unsigned char explicit_cast_interference0(unsigned int c) {
anatofuz
parents:
diff changeset
17 // CHECK-SANITIZE: %[[ANYEXT:.*]] = zext i8 %[[DST:.*]] to i16, !nosanitize
anatofuz
parents:
diff changeset
18 // CHECK-SANITIZE: call
anatofuz
parents:
diff changeset
19 // CHECK-SANITIZE-NOT: call
anatofuz
parents:
diff changeset
20 // CHECK: }
anatofuz
parents:
diff changeset
21 return (unsigned short)c;
anatofuz
parents:
diff changeset
22 }
anatofuz
parents:
diff changeset
23
anatofuz
parents:
diff changeset
24 // Implicit truncation before explicit truncation.
anatofuz
parents:
diff changeset
25 // CHECK-LABEL: @explicit_cast_interference1
anatofuz
parents:
diff changeset
26 unsigned char explicit_cast_interference1(unsigned int c) {
anatofuz
parents:
diff changeset
27 // CHECK-SANITIZE: %[[ANYEXT:.*]] = zext i16 %[[DST:.*]] to i32, !nosanitize
anatofuz
parents:
diff changeset
28 // CHECK-SANITIZE: call
anatofuz
parents:
diff changeset
29 // CHECK-SANITIZE-NOT: call
anatofuz
parents:
diff changeset
30 // CHECK: }
anatofuz
parents:
diff changeset
31 unsigned short b;
anatofuz
parents:
diff changeset
32 return (unsigned char)(b = c);
anatofuz
parents:
diff changeset
33 }
anatofuz
parents:
diff changeset
34
anatofuz
parents:
diff changeset
35 // ========================================================================== //
anatofuz
parents:
diff changeset
36 // The expected true-negatives.
anatofuz
parents:
diff changeset
37 // ========================================================================== //
anatofuz
parents:
diff changeset
38
anatofuz
parents:
diff changeset
39 // Explicit truncating casts.
anatofuz
parents:
diff changeset
40 // ========================================================================== //
anatofuz
parents:
diff changeset
41
anatofuz
parents:
diff changeset
42 // CHECK-LABEL: @explicit_unsigned_int_to_unsigned_char
anatofuz
parents:
diff changeset
43 unsigned char explicit_unsigned_int_to_unsigned_char(unsigned int src) {
anatofuz
parents:
diff changeset
44 // CHECK-SANITIZE-NOT: call
anatofuz
parents:
diff changeset
45 // CHECK: }
anatofuz
parents:
diff changeset
46 return (unsigned char)src;
anatofuz
parents:
diff changeset
47 }
anatofuz
parents:
diff changeset
48
anatofuz
parents:
diff changeset
49 // CHECK-LABEL: @explicit_signed_int_to_unsigned_char
anatofuz
parents:
diff changeset
50 unsigned char explicit_signed_int_to_unsigned_char(signed int src) {
anatofuz
parents:
diff changeset
51 // CHECK-SANITIZE-NOT: call
anatofuz
parents:
diff changeset
52 // CHECK: }
anatofuz
parents:
diff changeset
53 return (unsigned char)src;
anatofuz
parents:
diff changeset
54 }
anatofuz
parents:
diff changeset
55
anatofuz
parents:
diff changeset
56 // CHECK-LABEL: @explicit_unsigned_int_to_signed_char
anatofuz
parents:
diff changeset
57 signed char explicit_unsigned_int_to_signed_char(unsigned int src) {
anatofuz
parents:
diff changeset
58 // CHECK-SANITIZE-NOT: call
anatofuz
parents:
diff changeset
59 // CHECK: }
anatofuz
parents:
diff changeset
60 return (signed char)src;
anatofuz
parents:
diff changeset
61 }
anatofuz
parents:
diff changeset
62
anatofuz
parents:
diff changeset
63 // CHECK-LABEL: @explicit_signed_int_to_signed_char
anatofuz
parents:
diff changeset
64 signed char explicit_signed_int_to_signed_char(signed int src) {
anatofuz
parents:
diff changeset
65 // CHECK-SANITIZE-NOT: call
anatofuz
parents:
diff changeset
66 // CHECK: }
anatofuz
parents:
diff changeset
67 return (signed char)src;
anatofuz
parents:
diff changeset
68 }
anatofuz
parents:
diff changeset
69
anatofuz
parents:
diff changeset
70 // Explicit NOP casts.
anatofuz
parents:
diff changeset
71 // ========================================================================== //
anatofuz
parents:
diff changeset
72
anatofuz
parents:
diff changeset
73 // CHECK-LABEL: @explicit_unsigned_int_to_unsigned_int
anatofuz
parents:
diff changeset
74 unsigned int explicit_unsigned_int_to_unsigned_int(unsigned int src) {
anatofuz
parents:
diff changeset
75 // CHECK-SANITIZE-NOT: call
anatofuz
parents:
diff changeset
76 // CHECK: }
anatofuz
parents:
diff changeset
77 return (unsigned int)src;
anatofuz
parents:
diff changeset
78 }
anatofuz
parents:
diff changeset
79
anatofuz
parents:
diff changeset
80 // CHECK-LABEL: @explicit_signed_int_to_signed_int
anatofuz
parents:
diff changeset
81 signed int explicit_signed_int_to_signed_int(signed int src) {
anatofuz
parents:
diff changeset
82 // CHECK-SANITIZE-NOT: call
anatofuz
parents:
diff changeset
83 // CHECK: }
anatofuz
parents:
diff changeset
84 return (signed int)src;
anatofuz
parents:
diff changeset
85 }
anatofuz
parents:
diff changeset
86
anatofuz
parents:
diff changeset
87 // CHECK-LABEL: @explicit_unsigned_char_to_signed_char
anatofuz
parents:
diff changeset
88 unsigned char explicit_unsigned_char_to_signed_char(unsigned char src) {
anatofuz
parents:
diff changeset
89 // CHECK-SANITIZE-NOT: call
anatofuz
parents:
diff changeset
90 // CHECK: }
anatofuz
parents:
diff changeset
91 return (unsigned char)src;
anatofuz
parents:
diff changeset
92 }
anatofuz
parents:
diff changeset
93
anatofuz
parents:
diff changeset
94 // CHECK-LABEL: @explicit_signed_char_to_signed_char
anatofuz
parents:
diff changeset
95 signed char explicit_signed_char_to_signed_char(signed char src) {
anatofuz
parents:
diff changeset
96 // CHECK-SANITIZE-NOT: call
anatofuz
parents:
diff changeset
97 // CHECK: }
anatofuz
parents:
diff changeset
98 return (signed char)src;
anatofuz
parents:
diff changeset
99 }
anatofuz
parents:
diff changeset
100
anatofuz
parents:
diff changeset
101 // Explicit functional truncating casts.
anatofuz
parents:
diff changeset
102 // ========================================================================== //
anatofuz
parents:
diff changeset
103
anatofuz
parents:
diff changeset
104 using UnsignedChar = unsigned char;
anatofuz
parents:
diff changeset
105 using SignedChar = signed char;
anatofuz
parents:
diff changeset
106 using UnsignedInt = unsigned int;
anatofuz
parents:
diff changeset
107 using SignedInt = signed int;
anatofuz
parents:
diff changeset
108
anatofuz
parents:
diff changeset
109 // CHECK-LABEL: @explicit_functional_unsigned_int_to_unsigned_char
anatofuz
parents:
diff changeset
110 unsigned char explicit_functional_unsigned_int_to_unsigned_char(unsigned int src) {
anatofuz
parents:
diff changeset
111 // CHECK-SANITIZE-NOT: call
anatofuz
parents:
diff changeset
112 // CHECK: }
anatofuz
parents:
diff changeset
113 return UnsignedChar(src);
anatofuz
parents:
diff changeset
114 }
anatofuz
parents:
diff changeset
115
anatofuz
parents:
diff changeset
116 // CHECK-LABEL: @explicit_functional_signed_int_to_unsigned_char
anatofuz
parents:
diff changeset
117 unsigned char explicit_functional_signed_int_to_unsigned_char(signed int src) {
anatofuz
parents:
diff changeset
118 // CHECK-SANITIZE-NOT: call
anatofuz
parents:
diff changeset
119 // CHECK: }
anatofuz
parents:
diff changeset
120 return UnsignedChar(src);
anatofuz
parents:
diff changeset
121 }
anatofuz
parents:
diff changeset
122
anatofuz
parents:
diff changeset
123 // CHECK-LABEL: @explicit_functional_unsigned_int_to_signed_char
anatofuz
parents:
diff changeset
124 signed char explicit_functional_unsigned_int_to_signed_char(unsigned int src) {
anatofuz
parents:
diff changeset
125 // CHECK-SANITIZE-NOT: call
anatofuz
parents:
diff changeset
126 // CHECK: }
anatofuz
parents:
diff changeset
127 return SignedChar(src);
anatofuz
parents:
diff changeset
128 }
anatofuz
parents:
diff changeset
129
anatofuz
parents:
diff changeset
130 // CHECK-LABEL: @explicit_functional_signed_int_to_signed_char
anatofuz
parents:
diff changeset
131 signed char explicit_functional_signed_int_to_signed_char(signed int src) {
anatofuz
parents:
diff changeset
132 // CHECK-SANITIZE-NOT: call
anatofuz
parents:
diff changeset
133 // CHECK: }
anatofuz
parents:
diff changeset
134 return SignedChar(src);
anatofuz
parents:
diff changeset
135 }
anatofuz
parents:
diff changeset
136
anatofuz
parents:
diff changeset
137 // Explicit functional NOP casts.
anatofuz
parents:
diff changeset
138 // ========================================================================== //
anatofuz
parents:
diff changeset
139
anatofuz
parents:
diff changeset
140 // CHECK-LABEL: @explicit_functional_unsigned_int_to_unsigned_int
anatofuz
parents:
diff changeset
141 unsigned int explicit_functional_unsigned_int_to_unsigned_int(unsigned int src) {
anatofuz
parents:
diff changeset
142 // CHECK-SANITIZE-NOT: call
anatofuz
parents:
diff changeset
143 // CHECK: }
anatofuz
parents:
diff changeset
144 return UnsignedInt(src);
anatofuz
parents:
diff changeset
145 }
anatofuz
parents:
diff changeset
146
anatofuz
parents:
diff changeset
147 // CHECK-LABEL: @explicit_functional_signed_int_to_signed_int
anatofuz
parents:
diff changeset
148 signed int explicit_functional_signed_int_to_signed_int(signed int src) {
anatofuz
parents:
diff changeset
149 // CHECK-SANITIZE-NOT: call
anatofuz
parents:
diff changeset
150 // CHECK: }
anatofuz
parents:
diff changeset
151 return SignedInt(src);
anatofuz
parents:
diff changeset
152 }
anatofuz
parents:
diff changeset
153
anatofuz
parents:
diff changeset
154 // CHECK-LABEL: @explicit_functional_unsigned_char_to_signed_char
anatofuz
parents:
diff changeset
155 unsigned char explicit_functional_unsigned_char_to_signed_char(unsigned char src) {
anatofuz
parents:
diff changeset
156 // CHECK-SANITIZE-NOT: call
anatofuz
parents:
diff changeset
157 // CHECK: }
anatofuz
parents:
diff changeset
158 return UnsignedChar(src);
anatofuz
parents:
diff changeset
159 }
anatofuz
parents:
diff changeset
160
anatofuz
parents:
diff changeset
161 // CHECK-LABEL: @explicit_functional_signed_char_to_signed_char
anatofuz
parents:
diff changeset
162 signed char explicit_functional_signed_char_to_signed_char(signed char src) {
anatofuz
parents:
diff changeset
163 // CHECK-SANITIZE-NOT: call
anatofuz
parents:
diff changeset
164 // CHECK: }
anatofuz
parents:
diff changeset
165 return SignedChar(src);
anatofuz
parents:
diff changeset
166 }
anatofuz
parents:
diff changeset
167
anatofuz
parents:
diff changeset
168 // Explicit C++-style casts truncating casts.
anatofuz
parents:
diff changeset
169 // ========================================================================== //
anatofuz
parents:
diff changeset
170
anatofuz
parents:
diff changeset
171 // CHECK-LABEL: @explicit_cppstyleunsigned_int_to_unsigned_char
anatofuz
parents:
diff changeset
172 unsigned char explicit_cppstyleunsigned_int_to_unsigned_char(unsigned int src) {
anatofuz
parents:
diff changeset
173 // CHECK-SANITIZE-NOT: call
anatofuz
parents:
diff changeset
174 // CHECK: }
anatofuz
parents:
diff changeset
175 return static_cast<unsigned char>(src);
anatofuz
parents:
diff changeset
176 }
anatofuz
parents:
diff changeset
177
anatofuz
parents:
diff changeset
178 // CHECK-LABEL: @explicit_cppstylesigned_int_to_unsigned_char
anatofuz
parents:
diff changeset
179 unsigned char explicit_cppstylesigned_int_to_unsigned_char(signed int src) {
anatofuz
parents:
diff changeset
180 // CHECK-SANITIZE-NOT: call
anatofuz
parents:
diff changeset
181 // CHECK: }
anatofuz
parents:
diff changeset
182 return static_cast<unsigned char>(src);
anatofuz
parents:
diff changeset
183 }
anatofuz
parents:
diff changeset
184
anatofuz
parents:
diff changeset
185 // CHECK-LABEL: @explicit_cppstyleunsigned_int_to_signed_char
anatofuz
parents:
diff changeset
186 signed char explicit_cppstyleunsigned_int_to_signed_char(unsigned int src) {
anatofuz
parents:
diff changeset
187 // CHECK-SANITIZE-NOT: call
anatofuz
parents:
diff changeset
188 // CHECK: }
anatofuz
parents:
diff changeset
189 return static_cast<signed char>(src);
anatofuz
parents:
diff changeset
190 }
anatofuz
parents:
diff changeset
191
anatofuz
parents:
diff changeset
192 // CHECK-LABEL: @explicit_cppstylesigned_int_to_signed_char
anatofuz
parents:
diff changeset
193 signed char explicit_cppstylesigned_int_to_signed_char(signed int src) {
anatofuz
parents:
diff changeset
194 // CHECK-SANITIZE-NOT: call
anatofuz
parents:
diff changeset
195 // CHECK: }
anatofuz
parents:
diff changeset
196 return static_cast<signed char>(src);
anatofuz
parents:
diff changeset
197 }
anatofuz
parents:
diff changeset
198
anatofuz
parents:
diff changeset
199 // Explicit C++-style casts NOP casts.
anatofuz
parents:
diff changeset
200 // ========================================================================== //
anatofuz
parents:
diff changeset
201
anatofuz
parents:
diff changeset
202 // CHECK-LABEL: @explicit_cppstyleunsigned_int_to_unsigned_int
anatofuz
parents:
diff changeset
203 unsigned int explicit_cppstyleunsigned_int_to_unsigned_int(unsigned int src) {
anatofuz
parents:
diff changeset
204 // CHECK-SANITIZE-NOT: call
anatofuz
parents:
diff changeset
205 // CHECK: }
anatofuz
parents:
diff changeset
206 return static_cast<unsigned int>(src);
anatofuz
parents:
diff changeset
207 }
anatofuz
parents:
diff changeset
208
anatofuz
parents:
diff changeset
209 // CHECK-LABEL: @explicit_cppstylesigned_int_to_signed_int
anatofuz
parents:
diff changeset
210 signed int explicit_cppstylesigned_int_to_signed_int(signed int src) {
anatofuz
parents:
diff changeset
211 // CHECK-SANITIZE-NOT: call
anatofuz
parents:
diff changeset
212 // CHECK: }
anatofuz
parents:
diff changeset
213 return static_cast<signed int>(src);
anatofuz
parents:
diff changeset
214 }
anatofuz
parents:
diff changeset
215
anatofuz
parents:
diff changeset
216 // CHECK-LABEL: @explicit_cppstyleunsigned_char_to_signed_char
anatofuz
parents:
diff changeset
217 unsigned char explicit_cppstyleunsigned_char_to_signed_char(unsigned char src) {
anatofuz
parents:
diff changeset
218 // CHECK-SANITIZE-NOT: call
anatofuz
parents:
diff changeset
219 // CHECK: }
anatofuz
parents:
diff changeset
220 return static_cast<unsigned char>(src);
anatofuz
parents:
diff changeset
221 }
anatofuz
parents:
diff changeset
222
anatofuz
parents:
diff changeset
223 // CHECK-LABEL: @explicit_cppstylesigned_char_to_signed_char
anatofuz
parents:
diff changeset
224 signed char explicit_cppstylesigned_char_to_signed_char(signed char src) {
anatofuz
parents:
diff changeset
225 // CHECK-SANITIZE-NOT: call
anatofuz
parents:
diff changeset
226 // CHECK: }
anatofuz
parents:
diff changeset
227 return static_cast<signed char>(src);
anatofuz
parents:
diff changeset
228 }
anatofuz
parents:
diff changeset
229
anatofuz
parents:
diff changeset
230 } // extern "C"
anatofuz
parents:
diff changeset
231
anatofuz
parents:
diff changeset
232 // ---------------------------------------------------------------------------//
anatofuz
parents:
diff changeset
233 // A problematic true-negative involving simple C++ code.
anatofuz
parents:
diff changeset
234 // The problem is tha the NoOp ExplicitCast is directly within MaterializeTemporaryExpr(),
anatofuz
parents:
diff changeset
235 // so a special care is neeeded.
anatofuz
parents:
diff changeset
236 // See https://reviews.llvm.org/D48958#1161345
anatofuz
parents:
diff changeset
237 template <typename a>
anatofuz
parents:
diff changeset
238 a b(a c, const a &d) {
anatofuz
parents:
diff changeset
239 if (d)
anatofuz
parents:
diff changeset
240 ;
anatofuz
parents:
diff changeset
241 return c;
anatofuz
parents:
diff changeset
242 }
anatofuz
parents:
diff changeset
243
anatofuz
parents:
diff changeset
244 extern "C" { // Disable name mangling.
anatofuz
parents:
diff changeset
245
anatofuz
parents:
diff changeset
246 // CHECK-LABEL: @false_positive_with_MaterializeTemporaryExpr
anatofuz
parents:
diff changeset
247 int false_positive_with_MaterializeTemporaryExpr() {
anatofuz
parents:
diff changeset
248 // CHECK-SANITIZE-NOT: call{{.*}}ubsan
anatofuz
parents:
diff changeset
249 // CHECK: }
anatofuz
parents:
diff changeset
250 int e = b<unsigned>(4, static_cast<unsigned>(4294967296));
anatofuz
parents:
diff changeset
251 return e;
anatofuz
parents:
diff changeset
252 }
anatofuz
parents:
diff changeset
253
anatofuz
parents:
diff changeset
254 // ---------------------------------------------------------------------------//
anatofuz
parents:
diff changeset
255
anatofuz
parents:
diff changeset
256 } // extern "C"