Hi Julia,

Thanks for the suggestion. But, It is working partially... I am not sure if
Coccinelle is able to filter the %fmt e.g: %s only called with a parameter
of specific data-type. In my case, *{struct mydata }.name* and *{ struct
mydata *}->name*.

##### * Cocci*
@r1@
format list d;
identifier fn;
@@

fn("%@d@", ...)

@script:ocaml s1@
d << r1.d;
res;
@@
res := make_expr ("\""^(String.concat "%m"
(Str.split_delim(Str.regexp_string "%s") d))^"\"")

@main depends on s1 && r1@
format list r1.d;
expression s1.res;
identifier r1.fn;
struct mydata SMD;
struct mydata* SMDP;
@@

 fn(
-"%@d@"
+res
,
(
+ &
  SMD
- .name
|
  SMDP
- ->name
)
 );

*#####  .c Code sample*
int foo() {
int id;
struct mydata h1, *h2, s1, *s2;
char *city;

// works fine
my_printf("%s", s2->name);

// works without any criterions about the data type of %fmt
my_printf("sss %s gggg", h1.name);
my_printf("33131231313 %d %s %d %s hhhhh", id, s1.name, (*h2)->name,
h2->name);
my_printf("aaaa %s hhhhh", h2->name);
my_printf("%s", s2->name);

// should do nothing
my_printf("%s");
my_printf("%s", city);

// don't match.
my_printf("a %s %d", h2->name, id);
my_printf("ddddd %s %s %s", h2->name, city, h2->name);
my_printf("%d it would work but dunno mydata=%m\n", id, h2);
my_printf("%d here also, tt=%s | %s and %m\n", id, h2->name, h2->name, s2);
}

*#### Result*
HANDLING: /Volumes/Users/test/Coccinella/sample.c
diff =
--- /Volumes/Users/test/Coccinella/sample.c
+++ /tmp/cocci-output-99329-3a9829-sample.c
@@ -5,13 +5,13 @@ int foo() {
  char *city;

  // works fine
- my_printf("%s", s2->name);
+ my_printf("%m", s2);

  // works without any criterious about the data type of %fmt
- my_printf("sss %s gggg", h1.name);
+ my_printf("sss %m gggg", &h1);
  my_printf("33131231313 %d %s %d %s hhhhh", id, s1.name, (*h2)->name,
h2->name);
- my_printf("aaaa %s hhhhh", h2->name);
- my_printf("%s", s2->name);
+ my_printf("aaaa %m hhhhh", h2);
+ my_printf("%m", s2);

  // should do nothing
  my_printf("%s");


Basically, I intend to replace alls "%s" called with "mydata->name" by "%m"
with "mydata" or "&mydata"





On Fri, Nov 29, 2019 at 6:55 PM Julia Lawall <julia.law...@inria.fr> wrote:

> Maybe this will help you:
>
> @r@
> format list d;
> @@
>
> "%@d@"
>
> @script:ocaml s@
> d << r.d;
> res;
> @@
> res := make_expr ("\""^(String.concat "%s" (Str.split_delim
> (Str.regexp_string "%d") d))^"\"")
>
>
> @@
> format list r.d;
> expression s.res;
> @@
>
> -"%@d@"
> +res
>
> ---------------
>
> Example:
>
> int main() {
>   printf("some %d more\n", 12);
> }
>
> int main() {
>   printf("%d more\n", 12);
> }
>
> int main() {
>   printf("more %d\n", 12);
> }
>
> int main() {
>   printf("%d more %d\n", 12);
> }
>
> julia
>
_______________________________________________
Cocci mailing list
Cocci@systeme.lip6.fr
https://systeme.lip6.fr/mailman/listinfo/cocci

Reply via email to