Coccinelle archive on lore.kernel.org
 help / color / Atom feed
* [Cocci] Concatenating parameters into a string
@ 2020-01-13 22:21 Timur Tabi
  2020-01-13 22:33 ` Julia Lawall
  0 siblings, 1 reply; 6+ messages in thread
From: Timur Tabi @ 2020-01-13 22:21 UTC (permalink / raw)
  To: cocci

I don't know how to properly describe what I'm trying to do, so I'll
just show an example.

The code I'm working on has dozens of annoying macros that concatenate
parameters into strings.  For example,

#define DRF_DEF(d,r,f,c)  \
       (((NvU32)(NV ## d ## r ## f ## c))<<DRF_SHIFT(NV ## d ## r ## f))

So given DRF_DEF(one, two, three, four), it will generated:

(((NvU32)(NV_one_two_three_four))<<DRF_SHIFT(NV_one_two_three))

I'm trying to create a cocci rule that will replace all of these
macros with their expanded equivalents, but I don't know how to handle
the ## feature of macros.  So far I have this:

@@
expression d, r, f, n, v;
@@
-FLD_SET_DRF_NUM(d, r, f, n, v)
+((v) & ~DRF_SHIFTMASK(NV d r f)) | DRF_NUM(d, r, f, n)

But this gives an error because "NV d r f" is not valid C.
_______________________________________________
Cocci mailing list
Cocci@systeme.lip6.fr
https://systeme.lip6.fr/mailman/listinfo/cocci

^ permalink raw reply	[flat|nested] 6+ messages in thread

* Re: [Cocci] Concatenating parameters into a string
  2020-01-13 22:21 [Cocci] Concatenating parameters into a string Timur Tabi
@ 2020-01-13 22:33 ` Julia Lawall
  2020-01-13 22:46   ` Timur Tabi
  0 siblings, 1 reply; 6+ messages in thread
From: Julia Lawall @ 2020-01-13 22:33 UTC (permalink / raw)
  To: Timur Tabi; +Cc: cocci

On Mon, 13 Jan 2020, Timur Tabi wrote:

> I don't know how to properly describe what I'm trying to do, so I'll
> just show an example.
>
> The code I'm working on has dozens of annoying macros that concatenate
> parameters into strings.  For example,
>
> #define DRF_DEF(d,r,f,c)  \
>        (((NvU32)(NV ## d ## r ## f ## c))<<DRF_SHIFT(NV ## d ## r ## f))
>
> So given DRF_DEF(one, two, three, four), it will generated:
>
> (((NvU32)(NV_one_two_three_four))<<DRF_SHIFT(NV_one_two_three))
>
> I'm trying to create a cocci rule that will replace all of these
> macros with their expanded equivalents, but I don't know how to handle
> the ## feature of macros.  So far I have this:
>
> @@
> expression d, r, f, n, v;
> @@
> -FLD_SET_DRF_NUM(d, r, f, n, v)
> +((v) & ~DRF_SHIFTMASK(NV d r f)) | DRF_NUM(d, r, f, n)

Sorry, I don't follow what you are trying to do here.  Could you send an
example of the desired before after C code?  I see what you have above,
but I don't see the connection to the rule you have tried to write.

I have the feeling that you will want to use some python code to do
something with the ##s, but I'm not sure.

julia
_______________________________________________
Cocci mailing list
Cocci@systeme.lip6.fr
https://systeme.lip6.fr/mailman/listinfo/cocci

^ permalink raw reply	[flat|nested] 6+ messages in thread

* Re: [Cocci] Concatenating parameters into a string
  2020-01-13 22:33 ` Julia Lawall
@ 2020-01-13 22:46   ` Timur Tabi
  2020-01-13 23:04     ` Julia Lawall
  0 siblings, 1 reply; 6+ messages in thread
From: Timur Tabi @ 2020-01-13 22:46 UTC (permalink / raw)
  To: Julia Lawall; +Cc: cocci, Timur Tabi

On Mon, Jan 13, 2020 at 4:33 PM Julia Lawall <julia.lawall@inria.fr> wrote:
>
> Sorry, I don't follow what you are trying to do here.  Could you send an
> example of the desired before after C code?  I see what you have above,
> but I don't see the connection to the rule you have tried to write.
>
> I have the feeling that you will want to use some python code to do
> something with the ##s, but I'm not sure.

I'm hoping to avoid Python.

I'm basically trying to recreate the ## feature of C macros.  For
example, given:

#define MACRO(a, b)   a##b

I'm looking for a cocci script that replaces

   x = 1 << MACRO(HELLO, _THERE);

with

  x = 1 << HELLO_THERE;
_______________________________________________
Cocci mailing list
Cocci@systeme.lip6.fr
https://systeme.lip6.fr/mailman/listinfo/cocci

^ permalink raw reply	[flat|nested] 6+ messages in thread

* Re: [Cocci] Concatenating parameters into a string
  2020-01-13 22:46   ` Timur Tabi
@ 2020-01-13 23:04     ` Julia Lawall
  2020-01-14  0:01       ` Timur Tabi
  0 siblings, 1 reply; 6+ messages in thread
From: Julia Lawall @ 2020-01-13 23:04 UTC (permalink / raw)
  To: Timur Tabi; +Cc: cocci


On Mon, 13 Jan 2020, Timur Tabi wrote:

> On Mon, Jan 13, 2020 at 4:33 PM Julia Lawall <julia.lawall@inria.fr> wrote:
> >
> > Sorry, I don't follow what you are trying to do here.  Could you send an
> > example of the desired before after C code?  I see what you have above,
> > but I don't see the connection to the rule you have tried to write.
> >
> > I have the feeling that you will want to use some python code to do
> > something with the ##s, but I'm not sure.
>
> I'm hoping to avoid Python.
>
> I'm basically trying to recreate the ## feature of C macros.  For
> example, given:
>
> #define MACRO(a, b)   a##b
>
> I'm looking for a cocci script that replaces
>
>    x = 1 << MACRO(HELLO, _THERE);
>
> with
>
>   x = 1 << HELLO_THERE;

It might be possible to use

@@
identifier a,b;
fresh identifier x = a ## "_" ## b;
@@

- MACRO(a,b)
+ x

julia

_______________________________________________
Cocci mailing list
Cocci@systeme.lip6.fr
https://systeme.lip6.fr/mailman/listinfo/cocci

^ permalink raw reply	[flat|nested] 6+ messages in thread

* Re: [Cocci] Concatenating parameters into a string
  2020-01-13 23:04     ` Julia Lawall
@ 2020-01-14  0:01       ` Timur Tabi
  2020-01-14  7:16         ` Julia Lawall
  0 siblings, 1 reply; 6+ messages in thread
From: Timur Tabi @ 2020-01-14  0:01 UTC (permalink / raw)
  To: Julia Lawall; +Cc: cocci, Timur Tabi

On Mon, Jan 13, 2020 at 5:04 PM Julia Lawall <julia.lawall@inria.fr> wrote:
>
> It might be possible to use
>
> @@
> identifier a,b;
> fresh identifier x = a ## "_" ## b;
> @@
>
> - MACRO(a,b)
> + x

That worked, thanks.
_______________________________________________
Cocci mailing list
Cocci@systeme.lip6.fr
https://systeme.lip6.fr/mailman/listinfo/cocci

^ permalink raw reply	[flat|nested] 6+ messages in thread

* Re: [Cocci] Concatenating parameters into a string
  2020-01-14  0:01       ` Timur Tabi
@ 2020-01-14  7:16         ` Julia Lawall
  0 siblings, 0 replies; 6+ messages in thread
From: Julia Lawall @ 2020-01-14  7:16 UTC (permalink / raw)
  To: Timur Tabi; +Cc: cocci

On Mon, 13 Jan 2020, Timur Tabi wrote:

> On Mon, Jan 13, 2020 at 5:04 PM Julia Lawall <julia.lawall@inria.fr> wrote:
> >
> > It might be possible to use
> >
> > @@
> > identifier a,b;
> > fresh identifier x = a ## "_" ## b;
> > @@
> >
> > - MACRO(a,b)
> > + x
>
> That worked, thanks.

Great :)

julia
_______________________________________________
Cocci mailing list
Cocci@systeme.lip6.fr
https://systeme.lip6.fr/mailman/listinfo/cocci

^ permalink raw reply	[flat|nested] 6+ messages in thread

end of thread, back to index

Thread overview: 6+ messages (download: mbox.gz / follow: Atom feed)
-- links below jump to the message on this page --
2020-01-13 22:21 [Cocci] Concatenating parameters into a string Timur Tabi
2020-01-13 22:33 ` Julia Lawall
2020-01-13 22:46   ` Timur Tabi
2020-01-13 23:04     ` Julia Lawall
2020-01-14  0:01       ` Timur Tabi
2020-01-14  7:16         ` Julia Lawall

Coccinelle archive on lore.kernel.org

Archives are clonable:
	git clone --mirror https://lore.kernel.org/cocci/0 cocci/git/0.git

	# If you have public-inbox 1.1+ installed, you may
	# initialize and index your mirror using the following commands:
	public-inbox-init -V2 cocci cocci/ https://lore.kernel.org/cocci \
		cocci@systeme.lip6.fr
	public-inbox-index cocci

Example config snippet for mirrors

Newsgroup available over NNTP:
	nntp://nntp.lore.kernel.org/fr.lip6.systeme.cocci


AGPL code for this site: git clone https://public-inbox.org/public-inbox.git