1 // RUN: %clang_cc1 -std=c++20 -Wunsafe-buffer-usage \
2 // RUN: -fsafe-buffer-usage-suggestions \
3 // RUN: -Wno-unused-value -verify %s
5 void basic(int * x
) { // expected-warning{{'x' is an unsafe pointer used for buffer access}}
6 int *p1
= new int[10]; // not to warn
7 int *p2
= new int[10]; // expected-warning{{'p2' is an unsafe pointer used for buffer access}}
9 #pragma clang unsafe_buffer_usage begin
12 #define _INCLUDE_NO_WARN
13 #include "warn-unsafe-buffer-usage-pragma.h" // increment p1 in header
15 int *p3
= new int[10]; // expected-warning{{'p3' is an unsafe pointer used for buffer access}}
17 #pragma clang unsafe_buffer_usage end
18 p2
[5]; //expected-note{{used in buffer access here}}
19 p3
[5]; //expected-note{{used in buffer access here}}
20 x
++; //expected-note{{used in pointer arithmetic here}}
22 #include "warn-unsafe-buffer-usage-pragma.h" // increment p2 in header
26 void withDiagnosticWarning() {
27 int *p1
= new int[10]; // not to warn
28 int *p2
= new int[10]; // expected-warning{{'p2' is an unsafe pointer used for buffer access}}
30 // diagnostics in opt-out region
31 #pragma clang unsafe_buffer_usage begin
34 #pragma clang diagnostic push
35 #pragma clang diagnostic warning "-Wunsafe-buffer-usage"
38 #pragma clang diagnostic warning "-Weverything"
39 p1
[5]; // not to warn expected-warning{{expression result unused}}
40 p2
[5]; // not to warn expected-warning{{expression result unused}}
41 #pragma clang diagnostic pop
42 #pragma clang unsafe_buffer_usage end
44 // opt-out region under diagnostic warning
45 #pragma clang diagnostic push
46 #pragma clang diagnostic warning "-Wunsafe-buffer-usage"
47 #pragma clang unsafe_buffer_usage begin
50 #pragma clang unsafe_buffer_usage end
51 #pragma clang diagnostic pop
53 p2
[5]; // expected-note{{used in buffer access here}}
57 void withDiagnosticIgnore() {
58 int *p1
= new int[10]; // not to warn
59 int *p2
= new int[10]; // expected-warning{{'p2' is an unsafe pointer used for buffer access}}
60 int *p3
= new int[10]; // expected-warning{{'p3' is an unsafe pointer used for buffer access}}
62 #pragma clang unsafe_buffer_usage begin
65 #pragma clang diagnostic push
66 #pragma clang diagnostic ignored "-Wunsafe-buffer-usage"
69 #pragma clang diagnostic ignored "-Weverything"
72 #pragma clang diagnostic pop
73 #pragma clang unsafe_buffer_usage end
75 #pragma clang diagnostic push
76 #pragma clang diagnostic ignored "-Wunsafe-buffer-usage"
77 #pragma clang unsafe_buffer_usage begin
80 #pragma clang unsafe_buffer_usage end
81 #pragma clang diagnostic pop
83 p2
[5]; // expected-note{{used in buffer access here}}
85 #pragma clang diagnostic push
86 #pragma clang diagnostic ignored "-Wunsafe-buffer-usage"
87 #pragma clang unsafe_buffer_usage begin
90 #pragma clang unsafe_buffer_usage end
91 p3
[5]; // expected-note{{used in buffer access here}}
92 #pragma clang diagnostic pop
95 void noteGoesWithVarDeclWarning() {
96 #pragma clang diagnostic push
97 #pragma clang diagnostic ignored "-Wunsafe-buffer-usage"
98 int *p
= new int[10]; // not to warn
99 #pragma clang diagnostic pop
101 p
[5]; // not to note since the associated warning is suppressed