-
Notifications
You must be signed in to change notification settings - Fork 15.4k
Description
Please consider the following offsetof.c file:
~ $ cat offsetof.c
/*
* Compiler:
~ $ gcc -v
clang version 19.1.4
Target: aarch64-unknown-linux-android24
Thread model: posix
InstalledDir: /data/data/com.termux/files/usr/bin
* Error:
~ $ gcc -ansi -pedantic -Wall -Wextra -Werror -o offsetof offsetof.c 2>&1
offsetof.c:27:21: error: defining a type within 'offsetof' is a C23 extension [-Werror,-Wc23-extensions]
27 | return offsetof(struct { char c; }, c);
| ^~~~~~
/data/data/com.termux/files/usr/lib/clang/19/include/__stddef_offsetof.h:16:43: note: expanded from macro 'offsetof'
16 | #define offsetof(t, d) __builtin_offsetof(t, d)
| ^
1 error generated.
* Why?
*/
#include <stddef.h>
int main(void)
{
return offsetof(struct { char c; }, c);
}
A relevant portion of C89 for stddef.h is:
[...] and
offsetof( type, member-designator)
which expands to an integral constant expression that has type size_t,
the value of which is the offset in bytes, to the structure member
(designated by member-designator ), from the beginning of its
structure (designated by type ). The member-designator shall be such
that given
static type t;
then the expression &(t. member-designator ) evaluates to an address
constant. (If the specified member is a bit-field, the behavior is
undefined.)
[...]
The clang error-message reports a "C23 extension." It is now December, 2024. At least 12 years ago, this "code-snippet" was produced:
- https://ideone.com/wL6wm
- (linked to from the https://www.iso-9899.info/wiki/Code_snippets page)
It uses an alignof macro shared by Mr. Chris M. Thomasson in Usenet (probably the comp.lang.c section) from before that time, which is something like:
#define alignof(type) (offsetof(struct {char c; type t;}, t))
I suspect that similar macros have been in use for much, much longer, but I could be mistaken.
If we are invoking clang in a C89 mode and we interpret the quotation above as authoritative, then I perceive that the type used within the offsetof macro in the code-file (shared far above) meets the "static type t;" condition and I do not perceive a C23 extension being relevant to such old usage.
Is this an opportunity for improvement?