1 // RUN: %clang_cc1 -std=c++20 -Wunsafe-buffer-usage \
2 // RUN: -fsafe-buffer-usage-suggestions \
3 // RUN: -fdiagnostics-parseable-fixits %s 2>&1 | FileCheck %s
4 typedef int * Int_ptr_t
;
7 void local_array_subscript_simple() {
10 // CHECK: fix-it:"{{.*}}":{[[@LINE-1]]:3-[[@LINE-1]]:8}:"std::span<int> "
11 // CHECK: fix-it:"{{.*}}":{[[@LINE-2]]:12-[[@LINE-2]]:12}:"{"
12 // CHECK: fix-it:"{{.*}}":{[[@LINE-3]]:23-[[@LINE-3]]:23}:", 10}"
13 const int *q
= new int[10];
14 // CHECK: fix-it:"{{.*}}":{[[@LINE-1]]:3-[[@LINE-1]]:14}:"std::span<int const> "
15 // CHECK: fix-it:"{{.*}}":{[[@LINE-2]]:18-[[@LINE-2]]:18}:"{"
16 // CHECK: fix-it:"{{.*}}":{[[@LINE-3]]:29-[[@LINE-3]]:29}:", 10}"
20 // We do not fix the following declaration. Because if the
21 // definition of `Int_ptr_t` gets changed, the fixed code becomes
22 // incorrect and may NOT be noticed.
23 // FIXME: Fix with std::span<std::remove_pointer_t<Int_ptr_t>>?
24 Int_ptr_t x
= new int[10];
25 // CHECK-NOT: fix-it:"{{.*}}":{[[@LINE-1]]
26 Int_t
* z
= new int[10];
27 // CHECK: fix-it:"{{.*}}":{[[@LINE-1]]:3-[[@LINE-1]]:10}:"std::span<Int_t>"
28 // CHECK: fix-it:"{{.*}}":{[[@LINE-2]]:15-[[@LINE-2]]:15}:"{"
29 // CHECK: fix-it:"{{.*}}":{[[@LINE-3]]:26-[[@LINE-3]]:26}:", 10}"
30 Int_t
* w
= new Int_t
[10];
31 // CHECK: fix-it:"{{.*}}":{[[@LINE-1]]:3-[[@LINE-1]]:10}:"std::span<Int_t>"
32 // CHECK: fix-it:"{{.*}}":{[[@LINE-2]]:15-[[@LINE-2]]:15}:"{"
33 // CHECK: fix-it:"{{.*}}":{[[@LINE-3]]:28-[[@LINE-3]]:28}:", 10}"
40 void local_array_subscript_auto() {
42 // We do not fix the following declaration because
43 // that'd cause us to hardcode the element type.
44 // FIXME: Can we use the C++17 class template argument deduction
45 // to avoid spelling out the element type?
47 // CHECK-NOT: fix-it:"{{.*}}":{[[@LINE-1]]
51 void local_variable_qualifiers_specifiers() {
54 // CHECK: fix-it:"{{.*}}":{[[@LINE-1]]:3-[[@LINE-1]]:14}:"std::span<int const>"
55 const int * const q
= a
;
56 // CHECK: fix-it:"{{.*}}":{[[@LINE-1]]:3-[[@LINE-1]]:14}:"std::span<int const>"
61 [[deprecated
]] const int * x
= a
;
62 // CHECK: fix-it:"{{.*}}":{[[@LINE-1]]:18-[[@LINE-1]]:29}:"std::span<int const>"
63 const int * y
[[deprecated
]];
64 // CHECK: fix-it:"{{.*}}":{[[@LINE-1]]:3-[[@LINE-1]]:14}:"std::span<int const>"
69 void local_variable_unsupported_specifiers() {
71 const int * p
[[deprecated
]] = a
; // not supported because the attribute overlaps the source range of the declaration
72 // CHECK-NOT: fix-it:"{{.*}}":{[[@LINE-1]]:
74 static const int * q
= a
; // storage specifier not supported yet
75 // CHECK-NOT: fix-it:"{{.*}}":{[[@LINE-1]]:
77 extern int * x
; // storage specifier not supported yet
78 // CHECK-NOT: fix-it:"{{.*}}":{[[@LINE-1]]:
80 constexpr int * y
= 0; // `constexpr` specifier not supported yet
81 // CHECK-NOT: fix-it:"{{.*}}":{[[@LINE-1]]:
91 void local_array_subscript_variable_extent() {
95 // CHECK-DAG: fix-it:"{{.*}}":{[[@LINE-1]]:3-[[@LINE-1]]:8}:"std::span<int> "
96 // CHECK-DAG: fix-it:"{{.*}}":{[[@LINE-2]]:12-[[@LINE-2]]:12}:"{"
97 // CHECK-DAG: fix-it:"{{.*}}":{[[@LINE-3]]:22-[[@LINE-3]]:22}:", n}"
98 // If the extent expression does not have a constant value, we cannot fill the extent for users...
99 int *q
= new int[n
++];
100 // CHECK-DAG: fix-it:"{{.*}}":{[[@LINE-1]]:3-[[@LINE-1]]:8}:"std::span<int> "
101 // CHECK-DAG: fix-it:"{{.*}}":{[[@LINE-2]]:12-[[@LINE-2]]:12}:"{"
102 // CHECK-DAG: fix-it:"{{.*}}":{[[@LINE-3]]:24-[[@LINE-3]]:24}:", <# placeholder #>}"
108 void local_ptr_to_array() {
112 int b
[n
]; // If the extent expression does not have a constant value, we cannot fill the extent for users...
114 // CHECK-DAG: fix-it:"{{.*}}":{[[@LINE-1]]:3-[[@LINE-1]]:8}:"std::span<int> "
116 // CHECK-DAG: fix-it:"{{.*}}":{[[@LINE-1]]:3-[[@LINE-1]]:8}:"std::span<int> "
117 // No way to know if `n` is ever mutated since `int b[n];`, so no way to figure out the extent
122 void local_ptr_addrof_init() {
125 // CHECK-DAG: fix-it:"{{.*}}":{[[@LINE-1]]:3-[[@LINE-1]]:8}:"std::span<int>"
126 // CHECK-DAG: fix-it:"{{.*}}":{[[@LINE-2]]:13-[[@LINE-2]]:13}:"{"
127 // CHECK-DAG: fix-it:"{{.*}}":{[[@LINE-3]]:17-[[@LINE-3]]:17}:", 1}"
128 // This expression involves unsafe buffer accesses, which will crash
129 // at runtime after applying the fix-it,
133 void decl_without_init() {
136 // CHECK-DAG: fix-it:"{{.*}}":{[[@LINE-1]]:3-[[@LINE-1]]:8}:"std::span<int>"
137 // CHECK-NOT: fix-it:"{{.*}}":{[[@LINE-2]]:{{^3}}
139 // CHECK-DAG: fix-it:"{{.*}}":{[[@LINE-1]]:3-[[@LINE-1]]:10}:"std::span<Int_t>"
140 // CHECK-NOT: fix-it:"{{.*}}":{[[@LINE-2]]:{{^3}}
145 // Explicit casts are required in the following cases. No way to
146 // figure out span extent for them automatically.
147 void explict_cast() {
149 int * p
= (int*) new int[10][10];
150 // CHECK-DAG: fix-it:"{{.*}}":{[[@LINE-1]]:3-[[@LINE-1]]:8}:"std::span<int>"
151 // CHECK-DAG: fix-it:"{{.*}}":{[[@LINE-2]]:13-[[@LINE-2]]:13}:"{"
152 // CHECK-DAG: fix-it:"{{.*}}":{[[@LINE-3]]:35-[[@LINE-3]]:35}:", <# placeholder #>}"
156 char * q
= (char *)&a
;
157 // CHECK-DAG: fix-it:"{{.*}}":{[[@LINE-1]]:3-[[@LINE-1]]:9}:"std::span<char>"
158 // CHECK-DAG: fix-it:"{{.*}}":{[[@LINE-2]]:14-[[@LINE-2]]:14}:"{"
159 // CHECK-DAG: fix-it:"{{.*}}":{[[@LINE-3]]:24-[[@LINE-3]]:24}:", <# placeholder #>}"
163 char * s
= (char *) r
;
164 // CHECK-DAG: fix-it:"{{.*}}":{[[@LINE-1]]:3-[[@LINE-1]]:9}:"std::span<char>"
165 // CHECK-DAG: fix-it:"{{.*}}":{[[@LINE-2]]:14-[[@LINE-2]]:14}:"{"
166 // CHECK-DAG: fix-it:"{{.*}}":{[[@LINE-3]]:24-[[@LINE-3]]:24}:", <# placeholder #>}"
175 // CHECK: fix-it:"{{.*}}":{[[@LINE-1]]:3-[[@LINE-1]]:8}:"std::span<int>"
176 // CHECK-NOT: fix-it:"{{.*}}":{[[@LINE-2]]:{{^3}}
177 int * g
= NULL
; // cannot handle fix-its involving macros for now
178 // CHECK-NOT: fix-it:"{{.*}}":{[[@LINE-1]]:
180 // CHECK: fix-it:"{{.*}}":{[[@LINE-1]]:3-[[@LINE-1]]:8}:"std::span<int>"
181 // CHECK-NOT: fix-it:"{{.*}}":{[[@LINE-2]]:{{^3}}
183 // In case of value dependencies, we give up
185 // CHECK: fix-it:"{{.*}}":{[[@LINE-1]]:3-[[@LINE-1]]:8}:"std::span<int>"
186 // CHECK: fix-it:"{{.*}}":{[[@LINE-2]]:13-[[@LINE-2]]:13}:"{"
187 // CHECK: fix-it:"{{.*}}":{[[@LINE-3]]:20-[[@LINE-3]]:20}:", <# placeholder #>}"
188 int * r
= my_null
+ 0;
189 // CHECK: fix-it:"{{.*}}":{[[@LINE-1]]:3-[[@LINE-1]]:8}:"std::span<int>"
190 // CHECK: fix-it:"{{.*}}":{[[@LINE-2]]:13-[[@LINE-2]]:13}:"{"
191 // CHECK: fix-it:"{{.*}}":{[[@LINE-3]]:24-[[@LINE-3]]:24}:", <# placeholder #>}"
193 tmp
= p
[5]; // `p[5]` will cause crash after `p` being transformed to be a `std::span`
194 tmp
= q
[5]; // Similar for the rests.
202 void unsupported_multi_decl(int * x
) {
203 int * p
= x
, * q
= new int[10];
204 // CHECK-NOT: fix-it:"{{.*}}":{[[@LINE-1]]
208 void macroVariableIdentifier() {
210 #define MY_NAME_ARG(x) q
212 // Although fix-its include macros, the macros do not overlap with
213 // the bounds of the source range of these fix-its. So these fix-its
216 int * MY_NAME
= new int[10];
217 // CHECK: fix-it:"{{.*}}":{[[@LINE-1]]:3-[[@LINE-1]]:8}:"std::span<int>"
218 // CHECK: fix-it:"{{.*}}":{[[@LINE-2]]:19-[[@LINE-2]]:19}:"{"
219 // CHECK: fix-it:"{{.*}}":{[[@LINE-3]]:30-[[@LINE-3]]:30}:", 10}"
220 int * MY_NAME_ARG( 'x' ) = new int[10];
221 // CHECK: fix-it:"{{.*}}":{[[@LINE-1]]:3-[[@LINE-1]]:8}:"std::span<int>"
222 // CHECK: fix-it:"{{.*}}":{[[@LINE-2]]:30-[[@LINE-2]]:30}:"{"
223 // CHECK: fix-it:"{{.*}}":{[[@LINE-3]]:41-[[@LINE-3]]:41}:", 10}"
230 void unsupported_fixit_overlapping_macro(int * x
) {
232 // In the case below, a tentative fix-it replaces `MY_INT * p =` with `std::span<MY_INT> p `.
233 // The bounds of the source range of the fix-it overlap with the use of the macro
234 // `MY_INT`. The fix-it is discarded then.
236 // FIXME: we do not have to discard a fix-it if its begin location
237 // overlaps with the begin location of a macro. Similar for end
241 MY_INT
* p
= new int[10];
242 // CHECK-NOT: fix-it:"{{.*}}":{[[@LINE-1]]
245 #define MY_VAR(name) int * name
246 MY_VAR(q
) = new int[10];
247 // CHECK-NOT: fix-it:"{{.*}}":{[[@LINE-1]]
250 // In cases where fix-its do not change the original code where
251 // macros are used, those fix-its will be emitted. For example,
252 // fixits are inserted before and after `new MY_INT[MY_TEN]` below.
254 int * g
= new MY_INT
[MY_TEN
];
255 // CHECK: fix-it:"{{.*}}":{[[@LINE-1]]:3-[[@LINE-1]]:8}:"std::span<int>"
256 // CHECK: fix-it:"{{.*}}":{[[@LINE-2]]:13-[[@LINE-2]]:13}:"{"
257 // CHECK: fix-it:"{{.*}}":{[[@LINE-3]]:31-[[@LINE-3]]:31}:", MY_TEN}"
265 void unsupported_subscript_negative(int i
, unsigned j
, unsigned long k
) {
267 int * p
= new int[10];
268 // CHECK-NOT: fix-it:"{{.*}}":{[[@LINE-1]]
270 tmp
= p
[-1]; // If `p` is made a span, this `[]` operation is wrong,
271 // so no fix-it emitted.
273 int * q
= new int[10];
274 // CHECK-NOT: fix-it:"{{.*}}":{[[@LINE-1]]
277 tmp
= q
[i
]; // If `q` is made a span, this `[]` operation may be
278 // wrong as we do not know if `i` is non-negative, so
279 // no fix-it emitted.
281 int * r
= new int[10];
282 // CHECK: fix-it:"{{.*}}":{[[@LINE-1]]:3-[[@LINE-1]]:8}:"std::span<int>"
283 // CHECK: fix-it:"{{.*}}":{[[@LINE-2]]:13-[[@LINE-2]]:13}:"{"
284 // CHECK: fix-it:"{{.*}}":{[[@LINE-3]]:24-[[@LINE-3]]:24}:", 10}"
286 tmp
= r
[j
] + r
[k
]; // both `j` and `k` are unsigned so they must be non-negative
287 tmp
= r
[(unsigned int)-1]; // a cast-to-unsigned-expression is also non-negative
290 #define DEFINE_PTR(X) int* ptr = (X);
292 void all_vars_in_macro() {
298 void few_vars_in_macro() {
304 int * p
= new int[10];
305 // CHECK-DAG: fix-it:"{{.*}}":{[[@LINE-1]]:3-[[@LINE-1]]:8}:"std::span<int>"
306 // CHECK-DAG: fix-it:"{{.*}}":{[[@LINE-2]]:13-[[@LINE-2]]:13}:"{"
307 // CHECK-DAG: fix-it:"{{.*}}":{[[@LINE-3]]:24-[[@LINE-3]]:24}:", 10}"
310 // CHECK-DAG: fix-it:"{{.*}}":{[[@LINE-1]]:13-[[@LINE-1]]:14}:""
311 // CHECK-DAG: fix-it:"{{.*}}":{[[@LINE-2]]:15-[[@LINE-2]]:15}:"[0]"
313 // CHECK-DAG: fix-it:"{{.*}}":{[[@LINE-1]]:9-[[@LINE-1]]:10}:""
314 // CHECK-DAG: fix-it:"{{.*}}":{[[@LINE-2]]:11-[[@LINE-2]]:11}:"[0]"