Discussion:
[Cocci] Search/replace inside string constants?
Timur Tabi
2018-07-26 22:19:42 UTC
Permalink
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.
Julia Lawall
2018-07-26 22:36:38 UTC
Permalink
Post by Timur Tabi
Is there a way to edit string constants? I need to change
"NVRM: ...."
into
"..."
@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
Timur Tabi
2018-07-26 22:44:48 UTC
Permalink
Post by Julia Lawall
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.
Julia Lawall
2018-07-26 22:54:28 UTC
Permalink
Post by Timur Tabi
Post by Julia Lawall
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
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
Timur Tabi
2018-07-27 16:38:40 UTC
Permalink
Post by Julia Lawall
@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");
Julia Lawall
2018-07-27 16:46:16 UTC
Permalink
Post by Timur Tabi
Post by Julia Lawall
@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;
@@
coccinelle.c2 = '"' + c[7:];
coccinelle.c2 = c;
- 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);
- 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
Julia Lawall
2018-07-27 19:40:30 UTC
Permalink
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
Timur Tabi
2018-07-27 20:25:46 UTC
Permalink
Post by Julia Lawall
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. :-)
Timur Tabi
2018-07-27 21:28:45 UTC
Permalink
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,
SF Markus Elfring
2018-07-27 17:09:35 UTC
Permalink
Post by Julia Lawall
@script:python s@
c << r.c;
c2;
@@
coccinelle.c2 = '"' + c[7:];
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?
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
Julia Lawall
2018-07-27 17:14:37 UTC
Permalink
Post by SF Markus Elfring
Post by Julia Lawall
@script:python s@
c << r.c;
c2;
@@
coccinelle.c2 = '"' + c[7:];
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
SF Markus Elfring
2018-07-28 06:42:59 UTC
Permalink
Post by Julia Lawall
@script:python s@
c << r.c;
c2;
@@
coccinelle.c2 = '"' + c[7:];
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
Julia Lawall
2018-07-28 06:54:01 UTC
Permalink
Post by SF Markus Elfring
Post by Julia Lawall
@script:python s@
c << r.c;
c2;
@@
coccinelle.c2 = '"' + c[7:];
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
Post by SF Markus Elfring
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
https://systeme.lip6.fr/mailman/listinfo/cocci
Timur Tabi
2018-07-26 22:39:07 UTC
Permalink
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.
Julia Lawall
2018-07-26 22:51:03 UTC
Permalink
Post by Timur Tabi
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
@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
Post by Timur Tabi
_______________________________________________
Cocci mailing list
https://systeme.lip6.fr/mailman/listinfo/cocci
Loading...