xref: /llvm-project/clang/test/SemaCXX/warn-unsafe-buffer-usage-pragma.cpp (revision b7bdf1996fd195b20b9a2916c9183167650806f1)
1 // RUN: %clang_cc1 -std=c++20 -Wunsafe-buffer-usage \
2 // RUN:            -fsafe-buffer-usage-suggestions \
3 // RUN:            -Wno-unused-value -verify %s
4 
basic(int * x)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}}
8 
9 #pragma clang unsafe_buffer_usage begin
10   p1[5];  // not to warn
11 
12 #define _INCLUDE_NO_WARN
13 #include "warn-unsafe-buffer-usage-pragma.h" // increment p1 in header
14 
15   int *p3 = new int[10]; // expected-warning{{'p3' is an unsafe pointer used for buffer access}}
16 
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
23 }
24 
25 
withDiagnosticWarning()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}}
29 
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
43 
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
52 
53   p2[5]; // expected-note{{used in buffer access here}}
54 }
55 
56 
withDiagnosticIgnore()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}}
61 
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
74 
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
82 
83   p2[5]; // expected-note{{used in buffer access here}}
84 
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
93 }
94 
noteGoesWithVarDeclWarning()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
100 
101   p[5]; // not to note since the associated warning is suppressed
102 }
103