Run DCE after a LoopFlatten test to reduce spurious output [nfc]
[llvm-project.git] / clang / test / SemaCXX / warn-unsafe-buffer-usage-pragma.cpp
blobd8ee9bb16c329a93d0a65a9fc2de08ec853bf915
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
10 p1[5]; // not to warn
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}}
21 #define _INCLUDE_WARN
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
32 p1[5]; // not to warn
33 p2[5]; // not to warn
34 #pragma clang diagnostic push
35 #pragma clang diagnostic warning "-Wunsafe-buffer-usage"
36 p1[5]; // not to warn
37 p2[5]; // not to warn
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
48 p1[5]; // not to warn
49 p2[5]; // not to warn
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
63 p1[5]; // not to warn
64 p2[5]; // not to warn
65 #pragma clang diagnostic push
66 #pragma clang diagnostic ignored "-Wunsafe-buffer-usage"
67 p1[5]; // not to warn
68 p2[5]; // not to warn
69 #pragma clang diagnostic ignored "-Weverything"
70 p1[5]; // not to warn
71 p2[5]; // not to warn
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
78 p1[5]; // not to warn
79 p2[5]; // not to warn
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
88 p1[5]; // not to warn
89 p2[5]; // not to warn
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