[clang] [clang] Ignore GCC 11 `[[malloc(x)]]` attribute (PR #68059)

Alois Klink via cfe-commits cfe-commits at lists.llvm.org
Tue Oct 3 15:41:46 PDT 2023


================
@@ -4122,6 +4122,9 @@ def RestrictDocs : Documentation {
 The ``malloc`` attribute indicates that the function acts like a system memory
 allocation function, returning a pointer to allocated storage disjoint from the
 storage for any other object accessible to the caller.
+
+The form of ``malloc`` with one or two arguments (supported by GCC 11) is
+currently ignored by Clang.
----------------
aloisklink wrote:

In this PR, it's completely ignored, it's not even added to the Clang AST.

It just reaches the end of this function where nothing happens (let me know if I can improve the comment!):

https://github.com/llvm/llvm-project/blob/f9c914729a5f5ac7f8b61ea2d39509ff0236a228/clang/lib/Sema/SemaDeclAttr.cpp#L2084-L2087

We can't treat it as if has no arguments because in GCC it means two different things:
  - `__attribute__((malloc))` with no args means the return value is guaranteed not to alias to any other pointer (aka like [`__declspec(restrict)` in MSVC](https://learn.microsoft.com/en-us/cpp/cpp/restrict?view=msvc-170)) and that the function will normally return non-`NULL`, allowing optimizations,
  - `__attribute__((malloc(deallocator)))` instead says the returned pointer should be deallocated by the specified deallocator, allowing a static analyzer to print warnings.

See the `malloc` section of https://gcc.gnu.org/onlinedocs/gcc/Common-Function-Attributes.html for more info.

To be honest, I feel like GCC should have given the one/two argument form a different attribute name to make things less confusing, but GCC 11 has already been out for years unfortunately.

https://github.com/llvm/llvm-project/pull/68059


More information about the cfe-commits mailing list