Skip to contentSkip to conversation

Matching either declaration or assignment

cocci Christoph Böhmwalder 2 participants 2 messages Jun 20, 2022

lore
Share & tools
lore
#0 lore raw

Hi,

I have two places where I want to run a similar transformation:

	struct bio *bio = bio_alloc(device->ldev->backing_bdev, 0,
			REQ_OP_FLUSH | REQ_PREFLUSH, GFP_NOIO);

and (in a different function):

	struct bio *bio;
	...
	bio = bio_alloc(device->ldev->backing_bdev, nr_pages, peer_req->opf,

GFP_NOIO);

So basically, in one variant the assignment is done in combination with the declaration, while in the other case the assignment is separate from the declaration.

I would now like to match both variants with a semantic patch, capturing "bio" either way. This is what I have intuitively tried:

@@
local idexpression struct bio *b;
expression bdev, opf, gfp_mask, nr_vecs;
@@
b =
- bio_alloc(bdev, nr_vecs, opf, gfp_mask)
+ bio_alloc(gfp_mask, nr_vecs)
;

But it only matches the separate assignment.

On the other hand, this:

@@
identifier b;
expression bdev, opf, gfp_mask, nr_vecs;
@@
struct bio *b =
- bio_alloc(bdev, nr_vecs, opf, gfp_mask)
+ bio_alloc(gfp_mask, nr_vecs)
;

Only matches the combined declaration/assignment.

Is there a way to match both with the same rule?

Signature · 3 lines
Christoph Böhmwalder
LINBIT | Keeping the Digital World Running
DRBD HA —  Disaster Recovery — Software defined Storage
#1 lore raw
Quoted 44 lines · Christoph Böhmwalder

Hi,

I have two places where I want to run a similar transformation:

	struct bio *bio = bio_alloc(device->ldev->backing_bdev, 0,
			REQ_OP_FLUSH | REQ_PREFLUSH, GFP_NOIO);

and (in a different function):

	struct bio *bio;
	...
	bio = bio_alloc(device->ldev->backing_bdev, nr_pages, peer_req->opf,

GFP_NOIO);

So basically, in one variant the assignment is done in combination with the declaration, while in the other case the assignment is separate from the declaration.

I would now like to match both variants with a semantic patch, capturing "bio" either way. This is what I have intuitively tried:

@@
local idexpression struct bio *b;
expression bdev, opf, gfp_mask, nr_vecs;
@@
b =
- bio_alloc(bdev, nr_vecs, opf, gfp_mask)
+ bio_alloc(gfp_mask, nr_vecs)
;

But it only matches the separate assignment.

On the other hand, this:

@@
identifier b;
expression bdev, opf, gfp_mask, nr_vecs;
@@
struct bio *b =
- bio_alloc(bdev, nr_vecs, opf, gfp_mask)
+ bio_alloc(gfp_mask, nr_vecs)
;

Quoted 3 lines

Only matches the combined declaration/assignment.

Is there a way to match both with the same rule?

Yes, just make the first rule, but drop the semicolon.

julia

Quoted 6 lines

--
Christoph Böhmwalder
LINBIT | Keeping the Digital World Running DRBD HA — Disaster Recovery — Software defined Storage