cocci.inria.fr archive mirror
 help / color / mirror / Atom feed
* [Cocci] Search/replace inside string constants?
@ 2018-07-26 22:19 Timur Tabi
  2018-07-26 22:36 ` Julia Lawall
  2018-07-26 22:39 ` Timur Tabi
  0 siblings, 2 replies; 15+ messages in thread
From: Timur Tabi @ 2018-07-26 22:19 UTC (permalink / raw)
  To: cocci

Is there a way to edit string constants?  I need to change

"NVRM: ...."

into

"..."

That is, I need to remove the "NVRM: " from a string.  I tried this:

@depends on rule1@
@@
-"NVRM: "
+""

But it doesn't do anything.

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

* [Cocci] Search/replace inside string constants?
  2018-07-26 22:19 [Cocci] Search/replace inside string constants? Timur Tabi
@ 2018-07-26 22:36 ` Julia Lawall
  2018-07-26 22:44   ` Timur Tabi
  2018-07-26 22:39 ` Timur Tabi
  1 sibling, 1 reply; 15+ messages in thread
From: Julia Lawall @ 2018-07-26 22:36 UTC (permalink / raw)
  To: cocci



On Thu, 26 Jul 2018, Timur Tabi wrote:

> Is there a way to edit string constants?  I need to change
>
> "NVRM: ...."
>
> into
>
> "..."
>
> That is, I need to remove the "NVRM: " from a string.  I tried this:
>
> @depends on rule1@
> @@
> -"NVRM: "
> +""
>
> But it doesn't do anything.

The easiest thing would be to use python.  You can look at
demos/pythontococci.cocci for an example.  If you send a complete change
that you would like to make, I can write an example rule.

julia

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

* [Cocci] Search/replace inside string constants?
  2018-07-26 22:19 [Cocci] Search/replace inside string constants? Timur Tabi
  2018-07-26 22:36 ` Julia Lawall
@ 2018-07-26 22:39 ` Timur Tabi
  2018-07-26 22:51   ` Julia Lawall
  1 sibling, 1 reply; 15+ messages in thread
From: Timur Tabi @ 2018-07-26 22:39 UTC (permalink / raw)
  To: cocci

Also, while I have you attention, is this correct?

@rule1@
expression x;
expression list y;
@@
-DBG_PRINTF(x, y);
+NV_PRINTF(y);

@depends on rule1@
@@
-DBG_LEVEL_ERRORS
+LEVEL_ERROR

This appears to work, but I think the "depends on" is just saying that
if rule1 succeeds, then go ahead and replace all DBG_LEVEL_ERRORS with
LEVEL_ERROR.  However, I really only want that to happen inside an
NV_PRINTF.  I tried doing this:

@depends on rule1@
@@
NV_PRINTF(
-DBG_LEVEL_ERRORS
+LEVEL_ERROR
 );

That that doesn't do anything.

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

* [Cocci] Search/replace inside string constants?
  2018-07-26 22:36 ` Julia Lawall
@ 2018-07-26 22:44   ` Timur Tabi
  2018-07-26 22:54     ` Julia Lawall
  0 siblings, 1 reply; 15+ messages in thread
From: Timur Tabi @ 2018-07-26 22:44 UTC (permalink / raw)
  To: cocci

On Thu, Jul 26, 2018 at 5:36 PM, Julia Lawall <julia.lawall@lip6.fr> wrote:
>
> The easiest thing would be to use python.  You can look at
> demos/pythontococci.cocci for an example.  If you send a complete change
> that you would like to make, I can write an example rule.

My goal is to replace usage of a deprecated macro with a new one, but
some of the parameters has changed.

So

DBG_PRINTF((DBG_MODULE_OS, DBG_LEVEL_ERRORS, "NVRM: error status:
0x%x\n", status));

becomes

NV_PRINTF(LEVEL_ERROR, "failed to blacklist GPU: 0x%x\n", status);

So I think I've got everything except the removal of "NVRM: " from the
beginning of any strings where the macro has been changed.

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

* [Cocci] Search/replace inside string constants?
  2018-07-26 22:39 ` Timur Tabi
@ 2018-07-26 22:51   ` Julia Lawall
  0 siblings, 0 replies; 15+ messages in thread
From: Julia Lawall @ 2018-07-26 22:51 UTC (permalink / raw)
  To: cocci



On Thu, 26 Jul 2018, Timur Tabi wrote:

> Also, while I have you attention, is this correct?
>
> @rule1@
> expression x;
> expression list y;
> @@
> -DBG_PRINTF(x, y);
> +NV_PRINTF(y);
>
> @depends on rule1@
> @@
> -DBG_LEVEL_ERRORS
> +LEVEL_ERROR
>
> This appears to work, but I think the "depends on" is just saying that
> if rule1 succeeds, then go ahead and replace all DBG_LEVEL_ERRORS with
> LEVEL_ERROR.  However, I really only want that to happen inside an
> NV_PRINTF.  I tried doing this:
>
> @depends on rule1@
> @@
> NV_PRINTF(
> -DBG_LEVEL_ERRORS
> +LEVEL_ERROR
>  );
>
> That that doesn't do anything.

I don't know what the NV_PRINTF call looks like.  You can say

NV_PRINTF(...,
- DBG_LEVEL_ERRORS,
+ LEVEL_ERROR,
  ...);

If DBG_LEVEL_ERRORS can come at an unknown argument position.

julia

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

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

* [Cocci] Search/replace inside string constants?
  2018-07-26 22:44   ` Timur Tabi
@ 2018-07-26 22:54     ` Julia Lawall
  2018-07-27 16:38       ` Timur Tabi
  0 siblings, 1 reply; 15+ messages in thread
From: Julia Lawall @ 2018-07-26 22:54 UTC (permalink / raw)
  To: cocci



On Thu, 26 Jul 2018, Timur Tabi wrote:

> On Thu, Jul 26, 2018 at 5:36 PM, Julia Lawall <julia.lawall@lip6.fr> wrote:
> >
> > The easiest thing would be to use python.  You can look at
> > demos/pythontococci.cocci for an example.  If you send a complete change
> > that you would like to make, I can write an example rule.
>
> My goal is to replace usage of a deprecated macro with a new one, but
> some of the parameters has changed.
>
> So
>
> DBG_PRINTF((DBG_MODULE_OS, DBG_LEVEL_ERRORS, "NVRM: error status:
> 0x%x\n", status));
>
> becomes
>
> NV_PRINTF(LEVEL_ERROR, "failed to blacklist GPU: 0x%x\n", status);
>
> So I think I've got everything except the removal of "NVRM: " from the
> beginning of any strings where the macro has been changed.

@r@
constant char[] c;
expression list[n] es;
@@

foo(es,c,...)

@script:python s@
c << r.c;
c2;
@@
coccinelle.c2 = "desired change in c with extra double quotes on the outside"

@@
expression list[r.n] r.es;
constant char[] r.c;
identifier s.c2;
@@

foo(es,
- c,
+ c2,
  ...)

julia

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

* [Cocci] Search/replace inside string constants?
  2018-07-26 22:54     ` Julia Lawall
@ 2018-07-27 16:38       ` Timur Tabi
  2018-07-27 16:46         ` Julia Lawall
                           ` (2 more replies)
  0 siblings, 3 replies; 15+ messages in thread
From: Timur Tabi @ 2018-07-27 16:38 UTC (permalink / raw)
  To: cocci

On Thu, Jul 26, 2018 at 5:54 PM, Julia Lawall <julia.lawall@lip6.fr> wrote:

> @script:python s@
> c << r.c;
> c2;
> @@
> coccinelle.c2 = "desired change in c with extra double quotes on the outside"

Ok, I almost have it working.  It seems to trigger some of the time.

@script:python s@
c << r.c;
c2;
@@
if c.startswith('"NVRM: '):
    coccinelle.c2 = '"' + c[7:];
else:
    coccinelle.c2 = c;

So this works:

-        DBG_PRINTF((DBG_MODULE_OS, DBG_LEVEL_ERRORS,
-            "NVRM: failed to blacklist GPU: 0x%x\n", rmStatus));
+        NV_PRINTF(LEVEL_ERROR, "failed to blacklist GPU: 0x%x\n",
+                  rmStatus);

But this didn't:

-        DBG_PRINTF((DBG_MODULE_OS, DBG_LEVEL_ERRORS,
-            "NVRM: fifoGetUserdBar1MapInfo failed, bailing out of
RmInitAdapter\n"));
+        NV_PRINTF(LEVEL_ERROR,
+                  "NVRM: fifoGetUserdBar1MapInfo failed, bailing out
of RmInitAdapter\n");

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

* [Cocci] Search/replace inside string constants?
  2018-07-27 16:38       ` Timur Tabi
@ 2018-07-27 16:46         ` Julia Lawall
       [not found]           ` <69450411-cc54-ccad-2dfa-7dfbd439c0ba@nvidia.com>
  2018-07-27 17:09         ` SF Markus Elfring
  2018-07-28  6:42         ` SF Markus Elfring
  2 siblings, 1 reply; 15+ messages in thread
From: Julia Lawall @ 2018-07-27 16:46 UTC (permalink / raw)
  To: cocci



On Fri, 27 Jul 2018, Timur Tabi wrote:

> On Thu, Jul 26, 2018 at 5:54 PM, Julia Lawall <julia.lawall@lip6.fr> wrote:
>
> > @script:python s@
> > c << r.c;
> > c2;
> > @@
> > coccinelle.c2 = "desired change in c with extra double quotes on the outside"
>
> Ok, I almost have it working.  It seems to trigger some of the time.
>
> @script:python s@
> c << r.c;
> c2;
> @@
> if c.startswith('"NVRM: '):
>     coccinelle.c2 = '"' + c[7:];
> else:
>     coccinelle.c2 = c;
>
> So this works:
>
> -        DBG_PRINTF((DBG_MODULE_OS, DBG_LEVEL_ERRORS,
> -            "NVRM: failed to blacklist GPU: 0x%x\n", rmStatus));
> +        NV_PRINTF(LEVEL_ERROR, "failed to blacklist GPU: 0x%x\n",
> +                  rmStatus);
>
> But this didn't:
>
> -        DBG_PRINTF((DBG_MODULE_OS, DBG_LEVEL_ERRORS,
> -            "NVRM: fifoGetUserdBar1MapInfo failed, bailing out of
> RmInitAdapter\n"));
> +        NV_PRINTF(LEVEL_ERROR,
> +                  "NVRM: fifoGetUserdBar1MapInfo failed, bailing out
> of RmInitAdapter\n");

Do you know what the problem is?  If not, please send the complete
semantic patch and an appropriate .c file so that I can test it.

thanks,
julia

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

* [Cocci] Search/replace inside string constants?
  2018-07-27 16:38       ` Timur Tabi
  2018-07-27 16:46         ` Julia Lawall
@ 2018-07-27 17:09         ` SF Markus Elfring
  2018-07-27 17:14           ` Julia Lawall
  2018-07-28  6:42         ` SF Markus Elfring
  2 siblings, 1 reply; 15+ messages in thread
From: SF Markus Elfring @ 2018-07-27 17:09 UTC (permalink / raw)
  To: cocci

> @script:python s@
> c << r.c;
> c2;
> @@
> if c.startswith('"NVRM: '):
>     coccinelle.c2 = '"' + c[7:];
> else:
>    coccinelle.c2 = c;

I suggest to reconsider the action if no modification should be performed finally.
How do you think about to use the statement ?cocci.include_match(False)?
in the else branch instead?


> But this didn't:

I wonder also about the shown source code situation that an unwanted prefix
could be still preserved in the error message from such a transformation example.

Would you like to show your final SmPL replacement rule here?

Regards,
Markus

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

* [Cocci] Search/replace inside string constants?
  2018-07-27 17:09         ` SF Markus Elfring
@ 2018-07-27 17:14           ` Julia Lawall
  0 siblings, 0 replies; 15+ messages in thread
From: Julia Lawall @ 2018-07-27 17:14 UTC (permalink / raw)
  To: cocci



On Fri, 27 Jul 2018, SF Markus Elfring wrote:

> > @script:python s@
> > c << r.c;
> > c2;
> > @@
> > if c.startswith('"NVRM: '):
> >     coccinelle.c2 = '"' + c[7:];
> > else:
> >    coccinelle.c2 = c;
>
> I suggest to reconsider the action if no modification should be performed finally.
> How do you think about to use the statement ?cocci.include_match(False)?
> in the else branch instead?

This is also a reasonable strategy.

julia

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

* [Cocci] Search/replace inside string constants?
       [not found]               ` <6ca5500f-039e-e586-9366-719f847f3352@nvidia.com>
@ 2018-07-27 19:40                 ` Julia Lawall
  2018-07-27 20:25                   ` Timur Tabi
  0 siblings, 1 reply; 15+ messages in thread
From: Julia Lawall @ 2018-07-27 19:40 UTC (permalink / raw)
  To: cocci

It seems that the last rule would be better as:

@@
expression list[r.n] r.es;
constant char[] r.c;
identifier s.c2;
@@

NV_PRINTF(es,
- c
+ c2
  ,...)

It seems that when you remove and add back a comma, it thinks you really
want a comma to be there, and there is none if the string is the last
argument.

julia

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

* [Cocci] Search/replace inside string constants?
  2018-07-27 19:40                 ` Julia Lawall
@ 2018-07-27 20:25                   ` Timur Tabi
  2018-07-27 21:28                     ` Timur Tabi
  0 siblings, 1 reply; 15+ messages in thread
From: Timur Tabi @ 2018-07-27 20:25 UTC (permalink / raw)
  To: cocci

On 07/27/2018 02:40 PM, Julia Lawall wrote:
> 
> It seems that when you remove and add back a comma, it thinks you really
> want a comma to be there, and there is none if the string is the last
> argument.

That was it, thanks!  It all works now.  You can add me to the list of 
coccinelle fans. :-)

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

* [Cocci] Search/replace inside string constants?
  2018-07-27 20:25                   ` Timur Tabi
@ 2018-07-27 21:28                     ` Timur Tabi
  0 siblings, 0 replies; 15+ messages in thread
From: Timur Tabi @ 2018-07-27 21:28 UTC (permalink / raw)
  To: cocci

On 07/27/2018 03:25 PM, Timur Tabi wrote:
> 
> That was it, thanks!? It all works now.? You can add me to the list of 
> coccinelle fans. :-)

Looks like I found one particular failure:

-        DBG_PRINTF((DBG_MODULE_OS, DBG_LEVEL_INFO,
-            "NVRM: %s: Current security token doesn't match the one in 
the client database. \
-            Current EUID: %d, PID: %d; Client DB EUID: %d, PID: %d\n",
-            __FUNCTION__, pCurrentTokenUser->euid, pCurrentTokenUser->pid,
-            pClientTokenUser->euid, pClientTokenUser->pid));
+        DBG_PRINTF( DBG_MODULE_OS, DBG_LEVEL_INFO,
+                   "NVRM: %s: Current security token doesn't match the 
one in the client database. \
+                   ,
+                   __FUNCTION__, pCurrentTokenUser->euid,
+                   pCurrentTokenUser->pid,
+                   pClientTokenUser->euid, pClientTokenUser->pid);

It didn't rename DBG_PRINTF to NV_PRINTF, and it screwed up the string 
literal.  I'm guessing the \ in the middle of it is the problem.  I'll 
just fix it by hand,

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

* [Cocci] Search/replace inside string constants?
  2018-07-27 16:38       ` Timur Tabi
  2018-07-27 16:46         ` Julia Lawall
  2018-07-27 17:09         ` SF Markus Elfring
@ 2018-07-28  6:42         ` SF Markus Elfring
  2018-07-28  6:54           ` Julia Lawall
  2 siblings, 1 reply; 15+ messages in thread
From: SF Markus Elfring @ 2018-07-28  6:42 UTC (permalink / raw)
  To: cocci

> @script:python s@
> c << r.c;
> c2;
> @@
> if c.startswith('"NVRM: '):
>     coccinelle.c2 = '"' + c[7:];
> else:
>     coccinelle.c2 = c;

I have got another software development idea for this transformation approach.
The detection of unwanted prefixes could be moved into a regular expression
like ?^"NVRM: ? for the constraint of the metavariable for which SmPL inheritance
is used here, couldn't it?
The shown SmPL rule ?s? needs to work only with matching data then so that
the Python code could be reduced to the statement ?coccinelle.c2 = '"' + c[7:]?.

How do you think about to try such a script variant out besides adjustments
for comma positions?
https://systeme.lip6.fr/pipermail/cocci/2018-July/005212.html

Regards,
Markus

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

* [Cocci] Search/replace inside string constants?
  2018-07-28  6:42         ` SF Markus Elfring
@ 2018-07-28  6:54           ` Julia Lawall
  0 siblings, 0 replies; 15+ messages in thread
From: Julia Lawall @ 2018-07-28  6:54 UTC (permalink / raw)
  To: cocci



On Sat, 28 Jul 2018, SF Markus Elfring wrote:

> > @script:python s@
> > c << r.c;
> > c2;
> > @@
> > if c.startswith('"NVRM: '):
> >     coccinelle.c2 = '"' + c[7:];
> > else:
> >     coccinelle.c2 = c;
>
> I have got another software development idea for this transformation approach.
> The detection of unwanted prefixes could be moved into a regular expression
> like ?^"NVRM: ? for the constraint of the metavariable for which SmPL inheritance
> is used here, couldn't it?
> The shown SmPL rule ?s? needs to work only with matching data then so that
> the Python code could be reduced to the statement ?coccinelle.c2 = '"' + c[7:]?.

This could be reasonable.

julia

>
> How do you think about to try such a script variant out besides adjustments
> for comma positions?
> https://systeme.lip6.fr/pipermail/cocci/2018-July/005212.html
>
> Regards,
> Markus
> _______________________________________________
> Cocci mailing list
> Cocci at systeme.lip6.fr
> https://systeme.lip6.fr/mailman/listinfo/cocci
>

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

end of thread, other threads:[~2018-07-28  6:54 UTC | newest]

Thread overview: 15+ messages (download: mbox.gz / follow: Atom feed)
-- links below jump to the message on this page --
2018-07-26 22:19 [Cocci] Search/replace inside string constants? Timur Tabi
2018-07-26 22:36 ` Julia Lawall
2018-07-26 22:44   ` Timur Tabi
2018-07-26 22:54     ` Julia Lawall
2018-07-27 16:38       ` Timur Tabi
2018-07-27 16:46         ` Julia Lawall
     [not found]           ` <69450411-cc54-ccad-2dfa-7dfbd439c0ba@nvidia.com>
     [not found]             ` <alpine.DEB.2.20.1807271910330.12787@hadrien>
     [not found]               ` <6ca5500f-039e-e586-9366-719f847f3352@nvidia.com>
2018-07-27 19:40                 ` Julia Lawall
2018-07-27 20:25                   ` Timur Tabi
2018-07-27 21:28                     ` Timur Tabi
2018-07-27 17:09         ` SF Markus Elfring
2018-07-27 17:14           ` Julia Lawall
2018-07-28  6:42         ` SF Markus Elfring
2018-07-28  6:54           ` Julia Lawall
2018-07-26 22:39 ` Timur Tabi
2018-07-26 22:51   ` Julia Lawall

This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox;
as well as URLs for NNTP newsgroup(s).