Structures from -include are "weak"
Hello!
I'm trying to use sparse on standalone kernel drivers that use -include to include compatibility code for slightly older kernels, so that it's possible to have exactly the same driver sources in the kernel and in the standalone project.
Support for multiple -include switches has been very helpful, and things have been working fine for some time, but now I'm seeing massive error messages about dereferencing members of incomplete structures.
I've been able to reduce the problem to the following test case.
test.h:
struct st {
int len;
};test.c:
struct st;
int test(struct st *s);
int test(struct st *s)
{
return s->len;
}Commands to run:
$ sparse -include test.h test.c test.c:5:10: error: using member 'len' in incomplete struct st $ gcc -c -include test.h test.c $
It looks like the definition of struct st is sort of "weak" when it's read from the header included on the command line, so that the forward declaration from test.c removes the original definition.
It's also possible to define struct st to something else, and sparse won't warn about it. But omitting "struct st;" fixes the error, so the original definition is known to sparse.
Signature · 2 lines
Regards, Pavel Roskin
Quoted 7 lines · Pavel Roskin
It looks like the definition of struct st is sort of "weak" when it's read from the header included on the command line, so that the forward declaration from
test.cremoves the original definition.It's also possible to define struct st to something else, and sparse won't warn about it. But omitting "struct st;" fixes the error, so the original definition is known to sparse.
Looks like it gets a scope boundary between those. There is one case when presense of struct st; changes things: struct st; in
struct st {....};
....
{
struct st;
struct st *p;
...
struct st {....} x;
...
}will make p a pointer to struct st from that scope. If you omit it, p will be a pointer to struct st from the outer scope and x will have a different type. Too late beginning of file scope, perhaps?
Quoted 11 lines · Al Viro
{ struct st; struct st *p; ... struct st {....} x; ... }will make p a pointer to struct st from that scope. If you omit it, p will be a pointer to struct st from the outer scope and x will have a different type. Too late beginning of file scope, perhaps?
Gyah... So it is. We put the stuff from -include into builtin_scope and start the file scope only in __sparse().
See if adding
int is_outer_scope(struct scope *scope)
{
if (scope == block_scope)
return 0;
if (scope == &builtin_scope && block_scope->next == &builtin_scope)
return 0;
return 1;
}
to scope.c and replacing
sym = lookup_symbol(token->ident, NS_STRUCT);
if (!sym ||
(sym->scope != block_scope &&
(match_op(token->next,';') || match_op(token->next,'{')))) {
with
sym = lookup_symbol(token->ident, NS_STRUCT);
if (!sym ||
(is_outer_scope(sym->scope) &&
(match_op(token->next,';') || match_op(token->next,'{')))) {in parse.c:struct_union_enum_specifier() would fix all problems of that kind.
Quoted 4 lines · Al Viro
Gyah... So it is. We put the stuff from -include into
builtin_scopeand start the file scope only in__sparse().See if adding
...
Quoted 1 line
in parse.c:struct_union_enum_specifier() would fix all problems of that kind.
Yes, it's working like a charm! Tested on 3 projects: madwifi, at76_usb and orinoco_usb - all are fine. Thank you!
Signature · 2 lines
Regards, Pavel Roskin