On 2/15/19 10:04 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 const is now propagated to the fields. Let me know if there are > further problems. Many thanks!
I tested it and it removed all but 1 false positive. I have also validated that it removed only false positives. There's a qualifier propagation issue left in the case the field is an array. I have attached a patch that extends the tests/qualifier.c test case to demonstrate the problem. But actually I am happy about that leftover false positive as that gave me a new idea of how to remove some more casts: https://source.winehq.org/patches/data/158631 Thanks again! bye michael
>From 7000ce04c9e11a1815783a93dbe9082d22b53a30 Mon Sep 17 00:00:00 2001 From: Michael Stefaniuc <[email protected]> Date: Fri, 15 Feb 2019 23:09:18 +0100 Subject: [PATCH] Expand the qualifier propagation test with array fields Signed-off-by: Michael Stefaniuc <[email protected]> --- tests/qualifier.c | 11 +++++++++-- 1 file changed, 9 insertions(+), 2 deletions(-) diff --git a/tests/qualifier.c b/tests/qualifier.c index 05af05f1..149f67f3 100644 --- a/tests/qualifier.c +++ b/tests/qualifier.c @@ -10,14 +10,21 @@ int *foo2(int *i) struct bar { int i; + int j[2]; }; int *baz(const struct bar *b) { - return (int *)&b->i; + if (b->i) + return (int *)&b->i; + else + return (int *)&b->j[0]; } int *baz2(struct bar *b) { - return (int *)&b->i; + if (b->i) + return (int *)&b->i; + else + return (int *)&b->j[0]; } -- 2.20.1
_______________________________________________ Cocci mailing list [email protected] https://systeme.lip6.fr/mailman/listinfo/cocci
