133 lines · cpp
1// RUN: %clang_cc1 -std=c++20 -fsyntax-only -verify %s2 3struct span_with_static {4 void *ptr;5 int n;6 static int static_field;7};8 9span_with_static returns_span_with_static (void) __attribute((malloc_span)); // no-warning10 11class SomeClass {12public:13 int Data;14};15 16// Returning pointers to data members is not allowed.17struct 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}}24DataMemberSpan returns_data_member_span(void) __attribute((malloc_span)) {25 return DataMemberSpan{};26}27 28// Returning pointers to member functions is not allowed.29struct 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}}36MemberFuncSpan returns_member_func_span(void) __attribute((malloc_span)) {37 return MemberFuncSpan{};38}39 40template<typename FirstType, typename SecondType>41struct Pair {42 FirstType first;43 SecondType second;44};45 46Pair<int*, int> returns_templated_span1(void) __attribute((malloc_span)) { // no-warning47 return Pair<int*, int>{};48}49 50Pair<int*, int*> returns_templated_span2(void) __attribute((malloc_span)) { // no-warning51 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}}56Pair<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 62struct GoodSpan {63 void *ptr;64 int n;65};66 67struct BadSpan {68 int n;69};70 71template <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}}74T produce_span() __attribute((malloc_span)) {75 return T{};76}77 78void TestGoodBadSpan() {79 produce_span<GoodSpan>(); // no-warnings80 // 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-warning86 return GoodSpan{};87}88 89template<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 96void TestGoodBadTrailingReturnType() {97 templated_trailing_return_type<GoodSpan>(); // no-warnings98 // 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-warning103 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 112struct Base {113 void *other_p;114};115 116struct 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 125class 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);133