[clang] [Clang] Introduce malloc_span attribute (PR #167010)
Marco Elver via cfe-commits
cfe-commits at lists.llvm.org
Mon Nov 17 06:45:04 PST 2025
================
@@ -0,0 +1,102 @@
+// RUN: %clang_cc1 -std=c++20 -fsyntax-only -verify %s
+
+class SomeClass {
+public:
+ int Data;
+};
+
+// Returning pointers to data members is not allowed.
+struct DataMemberSpan {
+ int SomeClass::* member_ptr;
+ int n;
+};
+
+// expected-warning at +2 {{attribute only applies to functions that return span-like structures}}
+// expected-note at +1 {{returned struct/class fields are not a supported combination}}
+DataMemberSpan returns_data_member_span(void) __attribute((malloc_span)) {
+ return DataMemberSpan{};
+}
+
+// Returning pointers to member functions is not allowed.
+struct MemberFuncSpan {
+ void (SomeClass::*member_func_ptr)();
+ int n;
+};
+
+// expected-warning at +2 {{attribute only applies to functions that return span-like structures}}
+// expected-note at +1 {{returned struct/class fields are not a supported combination}}
+MemberFuncSpan returns_member_func_span(void) __attribute((malloc_span)) {
+ return MemberFuncSpan{};
+}
+
+template<typename FirstType, typename SecondType>
+struct Pair {
+ FirstType first;
+ SecondType second;
+};
+
+Pair<int*, int> returns_templated_span1(void) __attribute((malloc_span)) { // no-warning
+ return Pair<int*, int>{};
+}
+
+Pair<int*, int*> returns_templated_span2(void) __attribute((malloc_span)) { // no-warning
+ return Pair<int*, int*>{};
+}
+
+// expected-warning at +2 {{attribute only applies to functions that return span-like structures}}
+// expected-note at +1 {{returned struct/class fields are not a supported combination for a span-like type}}
+Pair<int, int> returns_templated_span3(void) __attribute((malloc_span)) {
+ return Pair<int, int>{};
+}
+
+// Verify that semantic checks are done on dependent types.
+
+struct GoodSpan {
+ void *ptr;
+ int n;
+};
+
+struct BadSpan {
+ int n;
+};
+
+template <typename T>
+// expected-warning at +2 {{'malloc_span' attribute only applies to functions that return span-like structures}}
+// expected-note at +1 {{returned struct/class has 1 fields, expected 2}}
+T produce_span() __attribute((malloc_span)) {
+ return T{};
+}
+
+void TestGoodBadSpan() {
+ produce_span<GoodSpan>(); // no-warnings
+ // expected-note at +1 {{in instantiation of function template specialization 'produce_span<BadSpan>' requested here}}
+ produce_span<BadSpan>();
+}
+
+// Ensure that trailing return types are also supported.
+__attribute__((malloc_span)) auto trailling_return_type(int size) -> GoodSpan { // no-warning
+ return GoodSpan{};
+}
+
+template<typename T>
+// expected-warning at +2 {{'malloc_span' attribute only applies to functions that return span-like structures}}
+// expected-note at +1 {{returned struct/class has 1 fields, expected 2}}
+__attribute__((malloc_span)) auto templated_trailling_return_type() -> T { // no-warning
----------------
melver wrote:
s/trailling/trailing/
https://github.com/llvm/llvm-project/pull/167010
More information about the cfe-commits
mailing list