The kernel is trying to use a unnamed struct to mark a group of fields as being 'const' and get a warning if one of its member is assigned. GCC gives indeed a warning but Sparse and clang don't. So, the type qualifiers of the anonymous struct need to be propagate to the members. An easy solution is to handle this inside find_identifier(), where access to are recursively searched inside sub-structures. It's very easy but it feels wrong to do semantics inside this function. Worse, it's only working for fields that are effectively accessed, doing a type evaluation on the anonymous struct (or its parent) would not by itself handle this. So, the solution chosen here is to handle this during type examination, more precisely, inside examine_struct_union_type(), where things are a bit more complicated (it can't be done when examining the members themselves because only the parent SYM_STRUCT is accessible and the qualifiers are in the SYM_NODE, so it needs to be done when examining the anonymous struct itself) but can be done for all members. Note: It would seems to logical to also handle at least all qualifier-like attributes but GCC seems to only bother with the true qualifiers and ignore things like attributes and alignment specifiers. Link: lore.kernel.org/r/CAHk-=wj4Kviw8q2Sx9vrrvyn3uWK-zNi4uGRx=5bzde0Cio8uQ@xxxxxxxxxxxxxx Link: lore.kernel.org/r/CAHk-=wjdJmL22+zk3_rWAfEJJCf=oDxiJ530qk-WNk_Ji0qhxw@xxxxxxxxxxxxxx Signed-off-by: Luc Van Oostenryck <luc.vanoostenryck@xxxxxxxxx> --- symbol.c | 26 ++++++++++++++++++++++++++ 1 file changed, 26 insertions(+) diff --git a/symbol.c b/symbol.c index aa02c8c5ad80..0030afb5f3f5 100644 --- a/symbol.c +++ b/symbol.c @@ -175,6 +175,30 @@ static void lay_out_struct(struct symbol *sym, struct struct_union_info *info) // warning (sym->pos, "regular: offset=%d", sym->offset); } +/// +// propagate properties of anonymous structs or unions into their members. +// +// :note: GCC seems to only propagate the qualifiers. +// :note: clang doesn't propagate anything at all. +static void examine_anonymous_member(struct symbol *sym) +{ + struct symbol *parent = sym; + struct symbol *sub; + + if (parent->type == SYM_NODE) + parent = sym->ctype.base_type; + if (parent->type != SYM_STRUCT && parent->type != SYM_UNION) + return; + + FOR_EACH_PTR(parent->symbol_list, sub) { + sub->ctype.modifiers |= sym->ctype.modifiers & MOD_QUALIFIER; + + // if nested, propagate all the way down + if (!sub->ident) + examine_anonymous_member(sub); + } END_FOR_EACH_PTR(sub); +} + static struct symbol * examine_struct_union_type(struct symbol *sym, int advance) { struct struct_union_info info = { @@ -196,6 +220,8 @@ static struct symbol * examine_struct_union_type(struct symbol *sym, int advance if (info.flex_array) sparse_error(info.flex_array->pos, "flexible array member '%s' is not last", show_ident(info.flex_array->ident)); examine_symbol_type(member); + if (!member->ident) + examine_anonymous_member(member); if (member->ctype.alignment > info.max_align && !sym->packed) { // Unnamed bitfields do not affect alignment. -- 2.30.0