[Cocci] cocci: Avoid initializing variables?

Joe Perches joe at perches.com
Fri Jan 27 08:27:38 CET 2012


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;
+T x;
...
x = z
$

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

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?



More information about the Cocci mailing list