Summary: | -Wsizeof-pointer-div not triggered by system header macros | ||
---|---|---|---|
Product: | gcc | Reporter: | Alejandro Colomar <alx> |
Component: | c | Assignee: | Not yet assigned to anyone <unassigned> |
Status: | RESOLVED WONTFIX | ||
Severity: | normal | CC: | asolokha |
Priority: | P3 | Keywords: | diagnostic |
Version: | 9.3.0 | ||
Target Milestone: | --- | ||
Host: | Target: | ||
Build: | Known to work: | ||
Known to fail: | Last reconfirmed: |
Description
Alejandro Colomar
2020-04-24 13:39:30 UTC
I'm not a GCC developer, but this is by design. https://gcc.gnu.org/onlinedocs/gcc/Warning-Options.html#index-Wsystem-headers Maybe the design is not perfect. Maybe some special warnings should still be warned about when they are used in user's code. I don't think there are any possible false positives with this warning. But still I don't know. Maybe I should add a pragma in the system header to enable `-Wsystem-headers` for that macro. I tried to use ``#pragma GCC diagnostic`` to enable ``-Wsystem-headers`` only for that macro. It bloated me with completely unrelated errors from libraries. So it is not an option. The only workaround right now is to use a ``_Static_assert``: .. code-block:: c #include <sys/cdefs.h> #define is_same_type(a, b) \ __builtin_types_compatible_p(__typeof__(a), __typeof__(b)) #define is_array(a) (!is_same_type((a), &(a)[0])) #define Static_assert_array(a) \ _Static_assert(is_array(a), "Not a `[]` !") #define ARRAY_SIZE(arr) __extension__( \ { \ Static_assert_array(arr); \ __arraycount((arr)); \ } \ ) This macro is safe no matter which warnings are enabled. There is no other way to write a safe macro in a system library for calculating the size of an array. I would call that a bug. The warning is completely useless, unless you keep copy&pasting that macro for each and every project, which is of course *wrong*. A static_assert(3) is indeed the right solution. As Arseny said, this is by design, so I'll close it as WONTFIX. |