On Thu, 26 Jan 2012, Joe Perches wrote:

I'm trying to elide unnecessary initializations of
variable declarations when the variables are later
overwritten with a direct set.

I'm using:

$ cat multi_set.cocci
@@
type T;
identifier x;
expression y;
expression z;
@@
-T x = y;

This is much too general. You don't want to get rid of the assignment if y is an arbitrary expression, such as a function call. You could just put constant y. Or you could allow it to be eg a constant or an identifier, but identifier seems unlikely enough that it is probably not worth bothering with. I guess there could be a casted identifier. But in that case, it seems unlikely that one would do that and then do nothing with the value afterwards.

+T x;
...
x = z
$

This doesn't work appropriately when the elided variable
declaration is used in expression z.

You could try:

(
x = <+...x...+>
|
x = z
)

I would put ? in front of x = z, because there might be some execution paths where x is not reassigned. Then there should be when != x on the dots, and probably when strict as well, because you want the constraints to be satisfied in error handling code as well.

julia

For instance, using current linux-kernel, the .cocci file above
generates this diff:
-----------------------
diff -u -p a/broadcom/bnx2x/bnx2x_stats.c b/broadcom/bnx2x/bnx2x_stats.c
--- a/broadcom/bnx2x/bnx2x_stats.c
+++ b/broadcom/bnx2x/bnx2x_stats.c
@@ -104,8 +104,7 @@ static void bnx2x_hw_stats_post(struct b
        /* loader */
        if (bp->executer_idx) {
                int loader_idx = PMF_DMAE_C(bp);
-               u32 opcode =  bnx2x_dmae_opcode(bp, DMAE_SRC_PCI, DMAE_DST_GRC,
-                                                true, DMAE_COMP_GRC);
+               u32 opcode;
                opcode = bnx2x_dmae_opcode_clr_src_reset(opcode);

                memset(dmae, 0, sizeof(struct dmae_command));
-----------------------

How can cocci be told to make expression z not allow use
of identifier x?

Is there a better way to write this?


_______________________________________________
Cocci mailing list
[email protected]
http://lists.diku.dk/mailman/listinfo/cocci
(Web access from inside DIKUs LAN only)

Reply via email to