1 // RUN: %clang_cc1 -std=c++20 -Wunsafe-buffer-usage -Wno-unused-value -verify %s
3 void basic(int * x
) { // expected-warning{{'x' is an unsafe pointer used for buffer access}}
4 int *p1
= new int[10]; // not to warn
5 int *p2
= new int[10]; // expected-warning{{'p2' is an unsafe pointer used for buffer access}}
7 #pragma clang unsafe_buffer_usage begin
10 #define _INCLUDE_NO_WARN
11 #include "warn-unsafe-buffer-usage-pragma.h" // increment p1 in header
13 int *p3
= new int[10]; // expected-warning{{'p3' is an unsafe pointer used for buffer access}}
15 #pragma clang unsafe_buffer_usage end
16 p2
[5]; //expected-note{{used in buffer access here}}
17 p3
[5]; //expected-note{{used in buffer access here}}
18 x
++; //expected-note{{used in pointer arithmetic here}}
20 #include "warn-unsafe-buffer-usage-pragma.h" // increment p2 in header
24 void withDiagnosticWarning() {
25 int *p1
= new int[10]; // not to warn
26 int *p2
= new int[10]; // expected-warning{{'p2' is an unsafe pointer used for buffer access}}
28 // diagnostics in opt-out region
29 #pragma clang unsafe_buffer_usage begin
32 #pragma clang diagnostic push
33 #pragma clang diagnostic warning "-Wunsafe-buffer-usage"
36 #pragma clang diagnostic warning "-Weverything"
37 p1
[5]; // not to warn expected-warning{{expression result unused}}
38 p2
[5]; // not to warn expected-warning{{expression result unused}}
39 #pragma clang diagnostic pop
40 #pragma clang unsafe_buffer_usage end
42 // opt-out region under diagnostic warning
43 #pragma clang diagnostic push
44 #pragma clang diagnostic warning "-Wunsafe-buffer-usage"
45 #pragma clang unsafe_buffer_usage begin
48 #pragma clang unsafe_buffer_usage end
49 #pragma clang diagnostic pop
51 p2
[5]; // expected-note{{used in buffer access here}}
55 void withDiagnosticIgnore() {
56 int *p1
= new int[10]; // not to warn
57 int *p2
= new int[10]; // expected-warning{{'p2' is an unsafe pointer used for buffer access}}
58 int *p3
= new int[10]; // expected-warning{{'p3' is an unsafe pointer used for buffer access}}
60 #pragma clang unsafe_buffer_usage begin
63 #pragma clang diagnostic push
64 #pragma clang diagnostic ignored "-Wunsafe-buffer-usage"
67 #pragma clang diagnostic ignored "-Weverything"
70 #pragma clang diagnostic pop
71 #pragma clang unsafe_buffer_usage end
73 #pragma clang diagnostic push
74 #pragma clang diagnostic ignored "-Wunsafe-buffer-usage"
75 #pragma clang unsafe_buffer_usage begin
78 #pragma clang unsafe_buffer_usage end
79 #pragma clang diagnostic pop
81 p2
[5]; // expected-note{{used in buffer access here}}
83 #pragma clang diagnostic push
84 #pragma clang diagnostic ignored "-Wunsafe-buffer-usage"
85 #pragma clang unsafe_buffer_usage begin
88 #pragma clang unsafe_buffer_usage end
89 p3
[5]; // expected-note{{used in buffer access here}}
90 #pragma clang diagnostic pop
93 void noteGoesWithVarDeclWarning() {
94 #pragma clang diagnostic push
95 #pragma clang diagnostic ignored "-Wunsafe-buffer-usage"
96 int *p
= new int[10]; // not to warn
97 #pragma clang diagnostic pop
99 p
[5]; // not to note since the associated warning is suppressed