llvm-project/clang/test/SemaCXX/warn-unsafe-buffer-usage-pragma-pch.cpp
Ziqing Luo 2e7b95e4c0
[Safe Buffers] Serialize unsafe_buffer_usage pragmas (#92031)
The commit adds serialization and de-serialization implementations for
the stored regions. Basically, the serialized representation of the
regions of a PP is a (ordered) sequence of source location encodings.
For de-serialization, regions from loaded files are stored by their ASTs.
When later one queries if a loaded location L is in an opt-out
region, PP looks up the regions of the loaded AST where L is at.

(Background if helps: a pair of `#pragma clang unsafe_buffer_usage begin/end` pragmas marks a
warning-opt-out region. The begin and end locations (opt-out regions)
are stored in preprocessor instances (PP) and will be queried by the
`-Wunsafe-buffer-usage` analyzer.)

The reported issue at upstream: https://github.com/llvm/llvm-project/issues/90501
rdar://124035402
2024-06-13 22:44:24 -07:00

28 lines
761 B
C++

// The original example from https://github.com/llvm/llvm-project/issues/90501
// Test without PCH
// RUN: %clang_cc1 -Wno-unused-value -Wunsafe-buffer-usage -std=c++20 -include %s -verify %s
// Test with PCH
// RUN: %clang_cc1 -Wno-unused-value -std=c++20 -emit-pch -o %t %s
// RUN: %clang_cc1 -Wno-unused-value -Wunsafe-buffer-usage -std=c++20 -include-pch %t -verify %s
#ifndef A_H
#define A_H
int a(int *s) {
s[2]; // <- expected warning here
#pragma clang unsafe_buffer_usage begin
return s[1];
#pragma clang unsafe_buffer_usage end
}
#else
// expected-warning@-7{{unsafe buffer access}}
// expected-note@-8{{pass -fsafe-buffer-usage-suggestions to receive code hardening suggestions}}
int main() {
int s[] = {1, 2, 3};
return a(s);
}
#endif