On 2/14/19 10:38 PM, Julia Lawall wrote: > > > On Thu, 14 Feb 2019, Michael Stefaniuc wrote: > >> Hello, >> >> I have this trivial script to remove useless casts to self: >> @ disable drop_cast @ >> type T; >> T E; >> @@ >> - (T) >> E >> >> It works but I'm hitting false positives when the code casts away >> qualifiers for field types. Please see the attached test case: >> - Good: Cast is kept in foo() >> - Bad: Cast is dropped in baz() >> >> Applying the generated diff will lead to a gcc warning: >> $ gcc -c qualifier.c >> qualifier.c: In function ‘baz’: >> qualifier.c:12:12: warning: return discards ‘const’ qualifier from >> pointer target type [-Wdiscarded-qualifiers] >> return &b->i; >> ^~~~~ >> >> I tried disabling the optional_qualifier iso but that has no effect: >> - Not needed for the foo() case >> - No effect for the baz() case >> >> I even tried prepending an alternation with "const T good;" but the cast >> still gets removed. > > The following is better, but not perfect, in that it requires that the > const thing have type pointer to a structure. It could be possible to > explicitly consider more type patterns. Well, it doesn't fixes the problem that the qualifier is not propagated from the struct to its fields. Code like this isn't matched with or without the cocci modification.
const int *zzz(const struct bar *b) { return (const int *)&b->i; } That's because "&b->i" has the type of just "int*" instead of the correct "const int*". bye michael > > @disable drop_cast@ > type T,T1; > T E; > identifier i; > const struct i *c; > @@ > > ( > c > | > - (T) > E > ) > > julia > _______________________________________________ Cocci mailing list Cocci@systeme.lip6.fr https://systeme.lip6.fr/mailman/listinfo/cocci