[clang] Implement lifetime analysis for lifetime_capture_by(X) (#115921)
[llvm-project.git] / clang / test / SemaCXX / warn-unsafe-buffer-usage-function-attr.cpp
blobbfc34b55c1f66785d627c41a0de888d09cee505c
1 // RUN: %clang_cc1 -std=c++20 -Wunsafe-buffer-usage \
2 // RUN: -fsafe-buffer-usage-suggestions -verify %s
4 [[clang::unsafe_buffer_usage]]
5 void deprecatedFunction3();
7 void deprecatedFunction4(int z);
9 void someFunction();
11 [[clang::unsafe_buffer_usage]]
12 void overloading(int* x);
14 void overloading(char c[]);
16 void overloading(int* x, int size);
18 [[clang::unsafe_buffer_usage]]
19 void deprecatedFunction4(int z);
21 void caller(int z, int* x, int size, char c[]) {
22 deprecatedFunction3(); // expected-warning{{function introduces unsafe buffer manipulation}}
23 deprecatedFunction4(z); // expected-warning{{function introduces unsafe buffer manipulation}}
24 someFunction();
26 overloading(x); // expected-warning{{function introduces unsafe buffer manipulation}}
27 overloading(x, size);
28 overloading(c);
31 [[clang::unsafe_buffer_usage]]
32 void overloading(char c[]);
34 // Test variadics
35 [[clang::unsafe_buffer_usage]]
36 void testVariadics(int *ptr, ...);
38 template<typename T, typename... Args>
39 [[clang::unsafe_buffer_usage]]
40 T adder(T first, Args... args);
42 template <typename T>
43 void foo(T t) {}
45 template<>
46 [[clang::unsafe_buffer_usage]]
47 void foo<int *>(int *t) {}
49 void caller1(int *p, int *q) {
50 testVariadics(p, q); // expected-warning{{function introduces unsafe buffer manipulation}}
51 adder(p, q); // expected-warning{{function introduces unsafe buffer manipulation}}
53 int x;
54 foo(x);
55 foo(&x); // expected-warning{{function introduces unsafe buffer manipulation}}
58 // Test virtual functions
59 class BaseClass {
60 public:
61 [[clang::unsafe_buffer_usage]]
62 virtual void func() {}
64 virtual void func1() {}
67 class DerivedClass : public BaseClass {
68 public:
69 void func() {}
71 [[clang::unsafe_buffer_usage]]
72 void func1() {}
75 void testInheritance() {
76 DerivedClass DC;
77 DC.func();
78 DC.func1(); // expected-warning{{function introduces unsafe buffer manipulation}}
80 BaseClass *BC;
81 BC->func(); // expected-warning{{function introduces unsafe buffer manipulation}}
82 BC->func1();
84 BC = &DC;
85 BC->func(); // expected-warning{{function introduces unsafe buffer manipulation}}
86 BC->func1();
89 class UnsafeMembers {
90 public:
91 UnsafeMembers() {}
93 [[clang::unsafe_buffer_usage]]
94 UnsafeMembers(int) {}
96 [[clang::unsafe_buffer_usage]]
97 explicit operator int() { return 0; }
99 [[clang::unsafe_buffer_usage]]
100 void Method() {}
102 [[clang::unsafe_buffer_usage]]
103 void operator()() {}
105 [[clang::unsafe_buffer_usage]]
106 int operator+(UnsafeMembers) { return 0; }
109 template <class... Vs>
110 int testFoldExpression(Vs&&... v) {
111 return (... + v); // expected-warning{{function introduces unsafe buffer manipulation}}
114 // https://github.com/llvm/llvm-project/issues/80482
115 void testClassMembers() {
116 UnsafeMembers(3); // expected-warning{{function introduces unsafe buffer manipulation}}
118 (void)static_cast<int>(UnsafeMembers()); // expected-warning{{function introduces unsafe buffer manipulation}}
120 UnsafeMembers().Method(); // expected-warning{{function introduces unsafe buffer manipulation}}
122 UnsafeMembers()(); // expected-warning{{function introduces unsafe buffer manipulation}}
124 testFoldExpression(UnsafeMembers(), UnsafeMembers());