xref: /llvm-project/clang/test/SemaCXX/warn-unsafe-buffer-usage-fixits-unevaluated-context.cpp (revision fde4b80cb772897a8cf0b3d022f3041e10b6e816)
1 // RUN: %clang_cc1 -std=c++20 -Wunsafe-buffer-usage \
2 // RUN:            -fsafe-buffer-usage-suggestions \
3 // RUN:            -fdiagnostics-parseable-fixits \
4 // RUN:            -fsyntax-only %s 2>&1 | FileCheck %s
5 
6 namespace std {
7   class type_info;
8   class bad_cast;
9   class bad_typeid;
10 }
11 using size_t = __typeof(sizeof(int));
12 void *malloc(size_t);
13 
14 void foo(...);
15 int bar(int *ptr);
16 
uneval_context_fix_pointer_dereference()17 void uneval_context_fix_pointer_dereference() {
18   int* p = new int[10];
19   // CHECK: fix-it:"{{.*}}":{[[@LINE-1]]:3-[[@LINE-1]]:7}:"std::span<int>"
20   // CHECK: fix-it:"{{.*}}":{[[@LINE-2]]:12-[[@LINE-2]]:12}:"{"
21   // CHECK: fix-it:"{{.*}}":{[[@LINE-3]]:23-[[@LINE-3]]:23}:", 10}"
22 
23   int tmp = p[5];
24   typeid(foo(*p));
25   // CHECK: fix-it:"{{.*}}":{[[@LINE-1]]:14-[[@LINE-1]]:15}:""
26   // CHECK: fix-it:"{{.*}}":{[[@LINE-2]]:16-[[@LINE-2]]:16}:"[0]"
27   _Generic(*p, int: 2, float: 3);
28   // CHECK: fix-it:"{{.*}}":{[[@LINE-1]]:12-[[@LINE-1]]:13}:""
29   // CHECK: fix-it:"{{.*}}":{[[@LINE-2]]:14-[[@LINE-2]]:14}:"[0]"
30 }
31 
uneval_context_fix_pointer_array_access()32 void uneval_context_fix_pointer_array_access() {
33   int* p = new int[10];
34   // CHECK: fix-it:"{{.*}}":{[[@LINE-1]]:3-[[@LINE-1]]:7}:"std::span<int>"
35   // CHECK: fix-it:"{{.*}}":{[[@LINE-2]]:12-[[@LINE-2]]:12}:"{"
36   // CHECK: fix-it:"{{.*}}":{[[@LINE-3]]:23-[[@LINE-3]]:23}:", 10}"
37 
38   int tmp = p[5];
39   typeid(foo(p[5]));
40   _Generic(p[2], int: 2, float: 3);
41 }
42 
uneval_context_fix_pointer_reference()43 void uneval_context_fix_pointer_reference() {
44   int* p = new int[10];
45   // CHECK: fix-it:"{{.*}}":{[[@LINE-1]]:3-[[@LINE-1]]:7}:"std::span<int>"
46   // CHECK: fix-it:"{{.*}}":{[[@LINE-2]]:12-[[@LINE-2]]:12}:"{"
47   // CHECK: fix-it:"{{.*}}":{[[@LINE-3]]:23-[[@LINE-3]]:23}:", 10}"
48 
49   int tmp = p[5];
50   typeid(bar(p));
51   // CHECK: fix-it:"{{.*}}":{[[@LINE-1]]:15-[[@LINE-1]]:15}:".data()"
52 }
53 
54 // The FixableGagdtes are not working in the following scenarios:
55 // 1. sizeof(DRE)
56 // 2. typeid(DRE)
57 // 3. __typeof(DRE)
58 // 4. _Generic(expr, type_1: DRE, type_2:)
59 // 5. decltype(DRE) var = y;
60 // 6. noexcept(DRE);
61 // This is becauste the UPC and ULC context matchers do not handle these contexts
62 // and almost all FixableGagdets currently depend on these matchers.
63 
64 // FIXME: Emit fixits for each of the below use.
uneval_context_fix_pointer_dereference_not_handled()65 void uneval_context_fix_pointer_dereference_not_handled() {
66   int* p = new int[10];
67   int tmp = p[5];
68 
69   foo(sizeof(*p), sizeof(decltype(*p)));
70   __typeof(*p) x;
71   int *q = (int *)malloc(sizeof(*p));
72   int y = sizeof(*p);
73   __is_pod(__typeof(*p));
74   __is_trivially_constructible(__typeof(*p), decltype(*p));
75   _Generic(*p, int: 2, float: 3);
76   _Generic(1, int: *p, float: 3);
77   _Generic(1, int: 2, float: *p);
78   decltype(*p) var = y;
79   noexcept(*p);
80   typeid(*p);
81 }
82 
83