xref: /llvm-project/clang/test/PCH/unsafe-buffer-usage-pragma-pch-complex.cpp (revision 2e7b95e4c080426e5085c38cec01176b56798534)
1*2e7b95e4SZiqing Luo // Test PCHs:
2*2e7b95e4SZiqing Luo //   MAIN - includes textual_1.h
3*2e7b95e4SZiqing Luo //        \ loads    pch_1.h - includes textual_2.h
4*2e7b95e4SZiqing Luo //                           \ loads    pch_2.h
5*2e7b95e4SZiqing Luo 
6*2e7b95e4SZiqing Luo // RUN: rm -rf %t
7*2e7b95e4SZiqing Luo // RUN: mkdir -p %t
8*2e7b95e4SZiqing Luo // RUN: split-file %s %t
9*2e7b95e4SZiqing Luo 
10*2e7b95e4SZiqing Luo // RUN: %clang_cc1 -Wno-unused-value -std=c++20 -emit-pch -o %t/pch_2.h.pch %t/pch_2.h -x c++
11*2e7b95e4SZiqing Luo // RUN: %clang_cc1 -Wno-unused-value -std=c++20 -include-pch %t/pch_2.h.pch -emit-pch -o %t/pch_1.h.pch %t/pch_1.h -x c++
12*2e7b95e4SZiqing Luo // RUN: %clang_cc1 -Wno-unused-value -std=c++20 -include-pch %t/pch_1.h.pch -verify %t/main.cpp -Wunsafe-buffer-usage
13*2e7b95e4SZiqing Luo 
14*2e7b95e4SZiqing Luo 
15*2e7b95e4SZiqing Luo //--- textual_1.h
a(int * s)16*2e7b95e4SZiqing Luo int a(int *s) {
17*2e7b95e4SZiqing Luo   s[2];  // <- expected warning here
18*2e7b95e4SZiqing Luo #pragma clang unsafe_buffer_usage begin
19*2e7b95e4SZiqing Luo   return s[1];
20*2e7b95e4SZiqing Luo #pragma clang unsafe_buffer_usage end
21*2e7b95e4SZiqing Luo }
22*2e7b95e4SZiqing Luo 
23*2e7b95e4SZiqing Luo //--- textual_2.h
b(int * s)24*2e7b95e4SZiqing Luo int b(int *s) {
25*2e7b95e4SZiqing Luo   s[2];  // <- expected warning here
26*2e7b95e4SZiqing Luo #pragma clang unsafe_buffer_usage begin
27*2e7b95e4SZiqing Luo   return s[1];
28*2e7b95e4SZiqing Luo #pragma clang unsafe_buffer_usage end
29*2e7b95e4SZiqing Luo }
30*2e7b95e4SZiqing Luo 
31*2e7b95e4SZiqing Luo //--- pch_1.h
32*2e7b95e4SZiqing Luo #include "textual_2.h"
33*2e7b95e4SZiqing Luo 
c(int * s)34*2e7b95e4SZiqing Luo int c(int *s) {
35*2e7b95e4SZiqing Luo   s[2];  // <- expected warning here
36*2e7b95e4SZiqing Luo #pragma clang unsafe_buffer_usage begin
37*2e7b95e4SZiqing Luo   return s[1];
38*2e7b95e4SZiqing Luo #pragma clang unsafe_buffer_usage end
39*2e7b95e4SZiqing Luo }
40*2e7b95e4SZiqing Luo 
41*2e7b95e4SZiqing Luo //--- pch_2.h
d(int * s)42*2e7b95e4SZiqing Luo int d(int *s) {
43*2e7b95e4SZiqing Luo   s[2];  // <- expected warning here
44*2e7b95e4SZiqing Luo #pragma clang unsafe_buffer_usage begin
45*2e7b95e4SZiqing Luo   return s[1];
46*2e7b95e4SZiqing Luo #pragma clang unsafe_buffer_usage end
47*2e7b95e4SZiqing Luo }
48*2e7b95e4SZiqing Luo 
49*2e7b95e4SZiqing Luo 
50*2e7b95e4SZiqing Luo //--- main.cpp
51*2e7b95e4SZiqing Luo #include "textual_1.h"
52*2e7b95e4SZiqing Luo // expected-warning@textual_1.h:2{{unsafe buffer access}} \
53*2e7b95e4SZiqing Luo    expected-note@textual_1.h:2{{pass -fsafe-buffer-usage-suggestions to receive code hardening suggestions}}
54*2e7b95e4SZiqing Luo // expected-warning@textual_2.h:2{{unsafe buffer access}} \
55*2e7b95e4SZiqing Luo    expected-note@textual_2.h:2{{pass -fsafe-buffer-usage-suggestions to receive code hardening suggestions}}
56*2e7b95e4SZiqing Luo // expected-warning@pch_1.h:4{{unsafe buffer access}} \
57*2e7b95e4SZiqing Luo    expected-note@pch_1.h:4{{pass -fsafe-buffer-usage-suggestions to receive code hardening suggestions}}
58*2e7b95e4SZiqing Luo // expected-warning@pch_2.h:2{{unsafe buffer access}} \
59*2e7b95e4SZiqing Luo    expected-note@pch_2.h:2{{pass -fsafe-buffer-usage-suggestions to receive code hardening suggestions}}
main()60*2e7b95e4SZiqing Luo int main() {
61*2e7b95e4SZiqing Luo   int s[] = {1, 2, 3};
62*2e7b95e4SZiqing Luo   return a(s) + b(s) + c(s) + d(s);
63*2e7b95e4SZiqing Luo }
64