[clang] Implement lifetime analysis for lifetime_capture_by(X) (#115921)
[llvm-project.git] / clang / test / SemaCXX / warn-unsafe-buffer-usage-libc-functions-inline-namespace.cpp
blob2bd12db93fd5268e99e0d35584af64436c9eb8bc
1 // RUN: %clang_cc1 -std=c++20 -Wno-all -Wunsafe-buffer-usage \
2 // RUN: -verify %s
4 namespace std {
5 inline namespace __1 {
6 template< class InputIt, class OutputIt >
7 OutputIt copy( InputIt first, InputIt last,
8 OutputIt d_first );
10 struct iterator{};
11 template<typename T>
12 struct span {
13 T * ptr;
14 T * data();
15 unsigned size_bytes();
16 unsigned size();
17 iterator begin() const noexcept;
18 iterator end() const noexcept;
21 template<typename T>
22 struct basic_string {
23 T* p;
24 T *c_str();
25 T *data();
26 unsigned size_bytes();
29 typedef basic_string<char> string;
30 typedef basic_string<wchar_t> wstring;
32 // C function under std:
33 void memcpy();
34 void strcpy();
35 int snprintf( char* buffer, unsigned buf_size, const char* format, ... );
39 void f(char * p, char * q, std::span<char> s) {
40 std::memcpy(); // expected-warning{{function 'memcpy' is unsafe}}
41 std::strcpy(); // expected-warning{{function 'strcpy' is unsafe}}
42 std::__1::memcpy(); // expected-warning{{function 'memcpy' is unsafe}}
43 std::__1::strcpy(); // expected-warning{{function 'strcpy' is unsafe}}
45 /* Test printfs */
46 std::snprintf(s.data(), 10, "%s%d", "hello", *p); // expected-warning{{function 'snprintf' is unsafe}} expected-note{{buffer pointer and size may not match}}
47 std::__1::snprintf(s.data(), 10, "%s%d", "hello", *p); // expected-warning{{function 'snprintf' is unsafe}} expected-note{{buffer pointer and size may not match}}
48 std::snprintf(s.data(), s.size_bytes(), "%s%d", "hello", *p); // no warn
49 std::__1::snprintf(s.data(), s.size_bytes(), "%s%d", "hello", *p); // no warn
52 void v(std::string s1) {
53 std::snprintf(s1.data(), s1.size_bytes(), "%s%d", s1.c_str(), 0); // no warn
54 std::__1::snprintf(s1.data(), s1.size_bytes(), "%s%d", s1.c_str(), 0); // no warn
57 void g(char *begin, char *end, char *p, std::span<char> s) {
58 std::copy(begin, end, p); // no warn
59 std::copy(s.begin(), s.end(), s.begin()); // no warn