hack: make flexible union members work on gcc≤14

In GCC <= 14, flexible arrays are not supported in unions because of a parserlimitation.

This is a hack that declares an anonymous struct, which itself can contain a flexible array but still be a member of a union. Another empty element is included because structs that only contain a flexible array are also invalid.
This commit is contained in:
Mohamed Gaber 2026-06-30 02:25:11 +03:00 committed by R. Timothy Edwards
parent 6638949233
commit 2f23be9dc3
3 changed files with 21 additions and 8 deletions

View File

@ -735,10 +735,10 @@ typedef struct
int prop_len; /* String length or number of values */
union {
Plane *prop_plane; /* For PROPERTY_TYPE_PLANE */
int prop_integer[]; /* For PROPERTY_TYPE_INTEGER or _DIMENSION */
dlong prop_double[]; /* For PROPERTY_TYPE_DOUBLE */
char prop_string[]; /* For PROPERTY_TYPE_STRING, must be last in
* struct and union */
FLEX_UNION_MEMBER(int prop_integer[], 0); /* For PROPERTY_TYPE_INTEGER or _DIMENSION */
FLEX_UNION_MEMBER(dlong prop_double[], 1); /* For PROPERTY_TYPE_DOUBLE */
FLEX_UNION_MEMBER(char prop_string[], 2); /* For PROPERTY_TYPE_STRING, must be last in
* struct and union */
} prop_value;
} PropertyRecord;

View File

@ -34,10 +34,10 @@ typedef struct h1
char *h_pointer; /* Pointer to anything. */
struct h1 *h_next; /* Next element, zero for end. */
union {
const char *h_ptr; /* One-word key value to identify entry. */
/* Following members must be the last part of the struct and union:*/
unsigned h_words[]; /* N-word key value. */
char h_name[]; /* Text name of this entry. */
const char *h_ptr; /* One-word key value to identify entry. */
/* Following members must be the last part of the struct and union:*/
FLEX_UNION_MEMBER(unsigned h_words[], 0); /* N-word key value. */
FLEX_UNION_MEMBER(char h_name[], 1); /* Text name of this entry. */
} h_key;
} HashEntry;

View File

@ -286,6 +286,19 @@ extern char AbortMessage[];
#endif
#endif
/*
* In GCC <= 14, flexible arrays are not supported in unions because of a parser
* limitation.
*
* This is a hack that declares an anonymous struct, which itself can contain
* a flexible array but still be a member of a union. Another empty element is
* included because structs that only contain a flexible array are also invalid.
*/
#define FLEX_UNION_MEMBER(member, member_no) struct {\
char __empty ## member_no [0]; member;\
}
/* ------------------ End of Machine Configuration Section ----------------- */
#endif /* _MAGIC__UTILS__MAGIC_H */