Debian Codegraph @

clang/test/SemaCXX/attr-malloc_span.cpp

raw
1 // RUN: %clang_cc1 -std=c++20 -fsyntax-only -verify %s
2
3 struct span_with_static {
4 void *ptr;
5 int n;
6 static int static_field;
7 };
8
9 span_with_static returns_span_with_static (void) __attribute((malloc_span)); // no-warning
10
11 class SomeClass {
12 public:
13 int Data;
14 };
15
16 // Returning pointers to data members is not allowed.
17 struct DataMemberSpan {
18 int SomeClass::* member_ptr;
19 int n;
20 };
21
22 // expected-warning@+2 {{attribute only applies to functions that return span-like structures}}
23 // expected-note@+1 {{returned struct fields are not a supported combination}}
24 DataMemberSpan returns_data_member_span(void) __attribute((malloc_span)) {
25 return DataMemberSpan{};
26 }
27
28 // Returning pointers to member functions is not allowed.
29 struct MemberFuncSpan {
30 void (SomeClass::*member_func_ptr)();
31 int n;
32 };
33
34 // expected-warning@+2 {{attribute only applies to functions that return span-like structures}}
35 // expected-note@+1 {{returned struct fields are not a supported combination}}
36 MemberFuncSpan returns_member_func_span(void) __attribute((malloc_span)) {
37 return MemberFuncSpan{};
38 }
39
40 template<typename FirstType, typename SecondType>
41 struct Pair {
42 FirstType first;
43 SecondType second;
44 };
45
46 Pair<int*, int> returns_templated_span1(void) __attribute((malloc_span)) { // no-warning
47 return Pair<int*, int>{};
48 }
49
50 Pair<int*, int*> returns_templated_span2(void) __attribute((malloc_span)) { // no-warning
51 return Pair<int*, int*>{};
52 }
53
54 // expected-warning@+2 {{attribute only applies to functions that return span-like structures}}
55 // expected-note@+1 {{returned struct fields are not a supported combination for a span-like type}}
56 Pair<int, int> returns_templated_span3(void) __attribute((malloc_span)) {
57 return Pair<int, int>{};
58 }
59
60 // Verify that semantic checks are done on dependent types.
61
62 struct GoodSpan {
63 void *ptr;
64 int n;
65 };
66
67 struct BadSpan {
68 int n;
69 };
70
71 template <typename T>
72 // expected-warning@+2 {{'malloc_span' attribute only applies to functions that return span-like structures}}
73 // expected-note@+1 {{returned struct has 1 fields, expected 2}}
74 T produce_span() __attribute((malloc_span)) {
75 return T{};
76 }
77
78 void TestGoodBadSpan() {
79 produce_span<GoodSpan>(); // no-warnings
80 // expected-note@+1 {{in instantiation of function template specialization 'produce_span<BadSpan>' requested here}}
81 produce_span<BadSpan>();
82 }
83
84 // Ensure that trailing return types are also supported.
85 __attribute__((malloc_span)) auto trailing_return_type(int size) -> GoodSpan { // no-warning
86 return GoodSpan{};
87 }
88
89 template<typename T>
90 // expected-warning@+2 {{'malloc_span' attribute only applies to functions that return span-like structures}}
91 // expected-note@+1 {{returned struct has 1 fields, expected 2}}
92 __attribute__((malloc_span)) auto templated_trailing_return_type() -> T {
93 return T{};
94 }
95
96 void TestGoodBadTrailingReturnType() {
97 templated_trailing_return_type<GoodSpan>(); // no-warnings
98 // expected-note@+1 {{in instantiation of function template specialization 'templated_trailing_return_type<BadSpan>' requested here}}
99 templated_trailing_return_type<BadSpan>();
100 }
101
102 __attribute((malloc_span)) auto trailing_return_temmplate_good(void) -> Pair<int*, int> { // no-warning
103 return Pair<int*, int>{};
104 }
105
106 // expected-warning@+2 {{attribute only applies to functions that return span-like structures}}
107 // expected-note@+1 {{returned struct fields are not a supported combination for a span-like type}}
108 __attribute((malloc_span)) auto trailing_return_temmplate_bad(void) -> Pair<int, int> {
109 return Pair<int, int>{};
110 }
111
112 struct Base {
113 void *other_p;
114 };
115
116 struct ChildSpan : Base {
117 void *p;
118 int n;
119 };
120
121 // expected-warning@+2 {{attribute only applies to functions that return span-like structures}}
122 // expected-note@+1 {{returned type inherits from a base class}}
123 __attribute((malloc_span)) ChildSpan return_child_span(void);
124
125 class VirtualBaseSpan : public virtual Base {
126 void *p;
127 int n;
128 };
129
130 // expected-warning@+2 {{attribute only applies to functions that return span-like structures}}
131 // expected-note@+1 {{returned type inherits from a base class}}
132 __attribute((malloc_span)) VirtualBaseSpan return_virtual_base_span(void);