xref: /llvm-project/clang/test/SemaCXX/warn-unsafe-buffer-usage-pragma.cpp (revision b7bdf1996fd195b20b9a2916c9183167650806f1)
1*b7bdf199SArtem Dergachev // RUN: %clang_cc1 -std=c++20 -Wunsafe-buffer-usage \
2*b7bdf199SArtem Dergachev // RUN:            -fsafe-buffer-usage-suggestions \
3*b7bdf199SArtem Dergachev // RUN:            -Wno-unused-value -verify %s
4829bcb06SZiqing Luo 
basic(int * x)5829bcb06SZiqing Luo void basic(int * x) {    // expected-warning{{'x' is an unsafe pointer used for buffer access}}
6829bcb06SZiqing Luo   int *p1 = new int[10]; // not to warn
7829bcb06SZiqing Luo   int *p2 = new int[10]; // expected-warning{{'p2' is an unsafe pointer used for buffer access}}
8829bcb06SZiqing Luo 
9829bcb06SZiqing Luo #pragma clang unsafe_buffer_usage begin
10829bcb06SZiqing Luo   p1[5];  // not to warn
11829bcb06SZiqing Luo 
12829bcb06SZiqing Luo #define _INCLUDE_NO_WARN
13829bcb06SZiqing Luo #include "warn-unsafe-buffer-usage-pragma.h" // increment p1 in header
14829bcb06SZiqing Luo 
15829bcb06SZiqing Luo   int *p3 = new int[10]; // expected-warning{{'p3' is an unsafe pointer used for buffer access}}
16829bcb06SZiqing Luo 
17829bcb06SZiqing Luo #pragma clang unsafe_buffer_usage end
18829bcb06SZiqing Luo   p2[5]; //expected-note{{used in buffer access here}}
19829bcb06SZiqing Luo   p3[5]; //expected-note{{used in buffer access here}}
20829bcb06SZiqing Luo   x++;   //expected-note{{used in pointer arithmetic here}}
21829bcb06SZiqing Luo #define _INCLUDE_WARN
22829bcb06SZiqing Luo #include "warn-unsafe-buffer-usage-pragma.h" // increment p2 in header
23829bcb06SZiqing Luo }
24829bcb06SZiqing Luo 
25829bcb06SZiqing Luo 
withDiagnosticWarning()26829bcb06SZiqing Luo void withDiagnosticWarning() {
27829bcb06SZiqing Luo   int *p1 = new int[10]; // not to warn
28829bcb06SZiqing Luo   int *p2 = new int[10]; // expected-warning{{'p2' is an unsafe pointer used for buffer access}}
29829bcb06SZiqing Luo 
30829bcb06SZiqing Luo   // diagnostics in opt-out region
31829bcb06SZiqing Luo #pragma clang unsafe_buffer_usage begin
32829bcb06SZiqing Luo   p1[5];  // not to warn
33829bcb06SZiqing Luo   p2[5];  // not to warn
34829bcb06SZiqing Luo #pragma clang diagnostic push
35829bcb06SZiqing Luo #pragma clang diagnostic warning "-Wunsafe-buffer-usage"
36829bcb06SZiqing Luo   p1[5];  // not to warn
37829bcb06SZiqing Luo   p2[5];  // not to warn
38829bcb06SZiqing Luo #pragma clang diagnostic warning "-Weverything"
39829bcb06SZiqing Luo   p1[5];  // not to warn expected-warning{{expression result unused}}
40829bcb06SZiqing Luo   p2[5];  // not to warn expected-warning{{expression result unused}}
41829bcb06SZiqing Luo #pragma clang diagnostic pop
42829bcb06SZiqing Luo #pragma clang unsafe_buffer_usage end
43829bcb06SZiqing Luo 
44829bcb06SZiqing Luo   // opt-out region under diagnostic warning
45829bcb06SZiqing Luo #pragma clang diagnostic push
46829bcb06SZiqing Luo #pragma clang diagnostic warning "-Wunsafe-buffer-usage"
47829bcb06SZiqing Luo #pragma clang unsafe_buffer_usage begin
48829bcb06SZiqing Luo   p1[5];  // not to warn
49829bcb06SZiqing Luo   p2[5];  // not to warn
50829bcb06SZiqing Luo #pragma clang unsafe_buffer_usage end
51829bcb06SZiqing Luo #pragma clang diagnostic pop
52829bcb06SZiqing Luo 
53829bcb06SZiqing Luo   p2[5]; // expected-note{{used in buffer access here}}
54829bcb06SZiqing Luo }
55829bcb06SZiqing Luo 
56829bcb06SZiqing Luo 
withDiagnosticIgnore()57829bcb06SZiqing Luo void withDiagnosticIgnore() {
58829bcb06SZiqing Luo   int *p1 = new int[10]; // not to warn
59829bcb06SZiqing Luo   int *p2 = new int[10]; // expected-warning{{'p2' is an unsafe pointer used for buffer access}}
60829bcb06SZiqing Luo   int *p3 = new int[10]; // expected-warning{{'p3' is an unsafe pointer used for buffer access}}
61829bcb06SZiqing Luo 
62829bcb06SZiqing Luo #pragma clang unsafe_buffer_usage begin
63829bcb06SZiqing Luo   p1[5];  // not to warn
64829bcb06SZiqing Luo   p2[5];  // not to warn
65829bcb06SZiqing Luo #pragma clang diagnostic push
66829bcb06SZiqing Luo #pragma clang diagnostic ignored "-Wunsafe-buffer-usage"
67829bcb06SZiqing Luo   p1[5];  // not to warn
68829bcb06SZiqing Luo   p2[5];  // not to warn
69829bcb06SZiqing Luo #pragma clang diagnostic ignored "-Weverything"
70829bcb06SZiqing Luo   p1[5];  // not to warn
71829bcb06SZiqing Luo   p2[5];  // not to warn
72829bcb06SZiqing Luo #pragma clang diagnostic pop
73829bcb06SZiqing Luo #pragma clang unsafe_buffer_usage end
74829bcb06SZiqing Luo 
75829bcb06SZiqing Luo #pragma clang diagnostic push
76829bcb06SZiqing Luo #pragma clang diagnostic ignored "-Wunsafe-buffer-usage"
77829bcb06SZiqing Luo #pragma clang unsafe_buffer_usage begin
78829bcb06SZiqing Luo   p1[5];  // not to warn
79829bcb06SZiqing Luo   p2[5];  // not to warn
80829bcb06SZiqing Luo #pragma clang unsafe_buffer_usage end
81829bcb06SZiqing Luo #pragma clang diagnostic pop
82829bcb06SZiqing Luo 
83829bcb06SZiqing Luo   p2[5]; // expected-note{{used in buffer access here}}
84829bcb06SZiqing Luo 
85829bcb06SZiqing Luo #pragma clang diagnostic push
86829bcb06SZiqing Luo #pragma clang diagnostic ignored "-Wunsafe-buffer-usage"
87829bcb06SZiqing Luo #pragma clang unsafe_buffer_usage begin
88829bcb06SZiqing Luo   p1[5];  // not to warn
89829bcb06SZiqing Luo   p2[5];  // not to warn
90829bcb06SZiqing Luo #pragma clang unsafe_buffer_usage end
91829bcb06SZiqing Luo   p3[5];  // expected-note{{used in buffer access here}}
92829bcb06SZiqing Luo #pragma clang diagnostic pop
93829bcb06SZiqing Luo }
94829bcb06SZiqing Luo 
noteGoesWithVarDeclWarning()95829bcb06SZiqing Luo void noteGoesWithVarDeclWarning() {
96829bcb06SZiqing Luo #pragma clang diagnostic push
97829bcb06SZiqing Luo #pragma clang diagnostic ignored "-Wunsafe-buffer-usage"
98829bcb06SZiqing Luo   int *p = new int[10]; // not to warn
99829bcb06SZiqing Luo #pragma clang diagnostic pop
100829bcb06SZiqing Luo 
101829bcb06SZiqing Luo   p[5]; // not to note since the associated warning is suppressed
102829bcb06SZiqing Luo }
103