Issue 244189
Summary Add support for declaration attributes in compound literals
Labels new issue
Assignees
Reporter alejandro-colomar
    It's good to have macros that make certain void* operations type-safe.  For example, glibc is considering the addition of a memset_T() macro (Cc: @codonell).

The implementation would be something like the following:

```c
        #define typeof_typename(T) typeof(*(typeof(T) *){_Generic(0, T: NULL, default: NULL)})

        // memset_T - memory set type-safe
        #define memset_T(p, val, n, T) memset_T_(p, val, n, typeof_typename(T))
        #define memset_T_(p, val, n, T)                               \
        ( \
                _Generic(p, T *: (void)0),                            \
                memset_T__(p, val, n, T)                              \
        )
        #define memset_T__(p, val, n, T)                              \
        ( \
                (T *) memsetarray(p, (const T [1]){(val)}, n, sizeof(T))\
        )

 void *
        memsetarray(void *p, const void *elt, size_t n, size_t size)
        {
                for (size_t i = 0; i < n; i++)
 memcpy(p + i * size, elt, size);
                return p;
 }
```

Using casts is well-known to be dangerous, and so one may want to replace the cast with a compound literal.  However, compound literals produce diagnostics due to unused code, and thus one will want to use `[[maybe_unused]]` in it:

```c
        #define memset_T__(p, val, n, T) \
        ( \
                (void)0, \
                ([[maybe_unused]] T *){memsetarray(p, (const T [1]){(val)}, n, sizeof(T))}\
        )
```

Cc: @AaronBallman 
_______________________________________________
llvm-bugs mailing list
[email protected]
https://lists.llvm.org/cgi-bin/mailman/listinfo/llvm-bugs

Reply via email to