xref: /llvm-project/clang/test/SemaCXX/warn-unsafe-buffer-usage-pragma-fixit.cpp (revision 829bcb06ec43ab4b56b95ff040ec9d36feeaf06a)
1 // RUN: %clang_cc1 -std=c++20 -Wunsafe-buffer-usage -fdiagnostics-parseable-fixits %s 2>&1 | FileCheck %s
2 
3 void basic(int * x) {
4   int tmp;
5   int *p1 = new int[10];  // no fix
6   // CHECK-NOT: fix-it:"{{.*}}":{[[@LINE-1]]:
7   int *p2 = new int[10];
8   // CHECK-DAG: fix-it:"{{.*}}":{[[@LINE-1]]:3-[[@LINE-1]]:12}:"std::span<int> p2"
9   // CHECK-DAG: fix-it:"{{.*}}":{[[@LINE-2]]:13-[[@LINE-2]]:13}:"{"
10   // CHECK-DAG: fix-it:"{{.*}}":{[[@LINE-3]]:24-[[@LINE-3]]:24}:", 10}"
11 #pragma clang unsafe_buffer_usage begin
12   tmp = p1[5];
13 #pragma clang unsafe_buffer_usage end
14   tmp = p2[5];
15 }
16 
17 void withDiagnosticWarning() {
18   int tmp;
19   int *p1 = new int[10]; // no fix
20   // CHECK-NOT: fix-it:"{{.*}}":{[[@LINE-1]]:
21   int *p2 = new int[10];
22   // CHECK-DAG: fix-it:"{{.*}}":{[[@LINE-1]]:3-[[@LINE-1]]:12}:"std::span<int> p2"
23   // CHECK-DAG: fix-it:"{{.*}}":{[[@LINE-2]]:13-[[@LINE-2]]:13}:"{"
24   // CHECK-DAG: fix-it:"{{.*}}":{[[@LINE-3]]:24-[[@LINE-3]]:24}:", 10}"
25 
26   // diagnostics in opt-out region
27 #pragma clang unsafe_buffer_usage begin
28   tmp = p1[5];  // not to warn
29   tmp = p2[5];  // not to warn
30 #pragma clang diagnostic push
31 #pragma clang diagnostic warning "-Wunsafe-buffer-usage"
32   tmp = p1[5];  // not to warn
33   tmp = p2[5];  // not to warn
34 #pragma clang diagnostic warning "-Weverything"
35   tmp = p1[5];  // not to warn
36   tmp = p2[5];  // not to warn
37 #pragma clang diagnostic pop
38 #pragma clang unsafe_buffer_usage end
39 
40   // opt-out region under diagnostic warning
41 #pragma clang diagnostic push
42 #pragma clang diagnostic warning "-Wunsafe-buffer-usage"
43 #pragma clang unsafe_buffer_usage begin
44   tmp = p1[5];  // not to warn
45   tmp = p2[5];  // not to warn
46 #pragma clang unsafe_buffer_usage end
47 #pragma clang diagnostic pop
48 
49   tmp = p2[5];
50 }
51 
52 
53 void withDiagnosticIgnore() {
54   int tmp;
55   int *p1 = new int[10];
56   // CHECK-NOT: fix-it:"{{.*}}":{[[@LINE-1]]:
57   int *p2 = new int[10];
58   // CHECK-DAG: fix-it:"{{.*}}":{[[@LINE-1]]:3-[[@LINE-1]]:12}:"std::span<int> p2"
59   // CHECK-DAG: fix-it:"{{.*}}":{[[@LINE-2]]:13-[[@LINE-2]]:13}:"{"
60   // CHECK-DAG: fix-it:"{{.*}}":{[[@LINE-3]]:24-[[@LINE-3]]:24}:", 10}"
61   int *p3 = new int[10];
62   // CHECK-DAG: fix-it:"{{.*}}":{[[@LINE-1]]:3-[[@LINE-1]]:12}:"std::span<int> p3"
63   // CHECK-DAG: fix-it:"{{.*}}":{[[@LINE-2]]:13-[[@LINE-2]]:13}:"{"
64   // CHECK-DAG: fix-it:"{{.*}}":{[[@LINE-3]]:24-[[@LINE-3]]:24}:", 10}"
65 
66 #pragma clang unsafe_buffer_usage begin
67   tmp = p1[5];  // not to warn
68   tmp = p2[5];  // not to warn
69 #pragma clang diagnostic push
70 #pragma clang diagnostic ignored "-Wunsafe-buffer-usage"
71   tmp = p1[5];  // not to warn
72   tmp = p2[5];  // not to warn
73 #pragma clang diagnostic ignored "-Weverything"
74   tmp = p1[5];  // not to warn
75   tmp = p2[5];  // not to warn
76 #pragma clang diagnostic pop
77 #pragma clang unsafe_buffer_usage end
78 
79 #pragma clang diagnostic push
80 #pragma clang diagnostic ignored "-Wunsafe-buffer-usage"
81 #pragma clang unsafe_buffer_usage begin
82   tmp = p1[5];  // not to warn
83   tmp = p2[5];  // not to warn
84 #pragma clang unsafe_buffer_usage end
85 #pragma clang diagnostic pop
86 
87   tmp = p2[5];
88 
89 #pragma clang diagnostic push
90 #pragma clang diagnostic ignored "-Wunsafe-buffer-usage"
91 #pragma clang unsafe_buffer_usage begin
92   tmp = p1[5];  // not to warn
93   tmp = p2[5];  // not to warn
94 #pragma clang unsafe_buffer_usage end
95   tmp = p3[5];  // expected-note{{used in buffer access here}}
96 #pragma clang diagnostic pop
97 }
98 
99 void noteGoesWithVarDeclWarning() {
100 #pragma clang diagnostic push
101 #pragma clang diagnostic ignored "-Wunsafe-buffer-usage"
102   int *p = new int[10]; // not to warn
103   // CHECK-NOT: fix-it:"{{.*}}":{[[@LINE-1]]:
104 #pragma clang diagnostic pop
105 
106   p[5]; // not to note since the associated warning is suppressed
107 }
108