xref: /llvm-project/clang/test/SemaCXX/warn-unsafe-buffer-usage-pragma.cpp (revision 829bcb06ec43ab4b56b95ff040ec9d36feeaf06a)
1 // RUN: %clang_cc1 -std=c++20 -Wunsafe-buffer-usage -Wno-unused-value -verify %s
2 
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}}
6 
7 #pragma clang unsafe_buffer_usage begin
8   p1[5];  // not to warn
9 
10 #define _INCLUDE_NO_WARN
11 #include "warn-unsafe-buffer-usage-pragma.h" // increment p1 in header
12 
13   int *p3 = new int[10]; // expected-warning{{'p3' is an unsafe pointer used for buffer access}}
14 
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}}
19 #define _INCLUDE_WARN
20 #include "warn-unsafe-buffer-usage-pragma.h" // increment p2 in header
21 }
22 
23 
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}}
27 
28   // diagnostics in opt-out region
29 #pragma clang unsafe_buffer_usage begin
30   p1[5];  // not to warn
31   p2[5];  // not to warn
32 #pragma clang diagnostic push
33 #pragma clang diagnostic warning "-Wunsafe-buffer-usage"
34   p1[5];  // not to warn
35   p2[5];  // not to warn
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
41 
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
46   p1[5];  // not to warn
47   p2[5];  // not to warn
48 #pragma clang unsafe_buffer_usage end
49 #pragma clang diagnostic pop
50 
51   p2[5]; // expected-note{{used in buffer access here}}
52 }
53 
54 
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}}
59 
60 #pragma clang unsafe_buffer_usage begin
61   p1[5];  // not to warn
62   p2[5];  // not to warn
63 #pragma clang diagnostic push
64 #pragma clang diagnostic ignored "-Wunsafe-buffer-usage"
65   p1[5];  // not to warn
66   p2[5];  // not to warn
67 #pragma clang diagnostic ignored "-Weverything"
68   p1[5];  // not to warn
69   p2[5];  // not to warn
70 #pragma clang diagnostic pop
71 #pragma clang unsafe_buffer_usage end
72 
73 #pragma clang diagnostic push
74 #pragma clang diagnostic ignored "-Wunsafe-buffer-usage"
75 #pragma clang unsafe_buffer_usage begin
76   p1[5];  // not to warn
77   p2[5];  // not to warn
78 #pragma clang unsafe_buffer_usage end
79 #pragma clang diagnostic pop
80 
81   p2[5]; // expected-note{{used in buffer access here}}
82 
83 #pragma clang diagnostic push
84 #pragma clang diagnostic ignored "-Wunsafe-buffer-usage"
85 #pragma clang unsafe_buffer_usage begin
86   p1[5];  // not to warn
87   p2[5];  // not to warn
88 #pragma clang unsafe_buffer_usage end
89   p3[5];  // expected-note{{used in buffer access here}}
90 #pragma clang diagnostic pop
91 }
92 
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
98 
99   p[5]; // not to note since the associated warning is suppressed
100 }
101