cocci.inria.fr archive mirror
 help / color / mirror / Atom feed
From: George McCollister <george.mccollister@gmail.com>
To: cocci@systeme.lip6.fr
Subject: [Cocci] using cocci to switch to formatted log/print function
Date: Fri, 27 Mar 2020 13:19:27 -0500	[thread overview]
Message-ID: <CAFSKS=NXVUcp1KwpidZoFTfyUCmh+8ij2njaGZ7g6h9LvEiYrA@mail.gmail.com> (raw)

Hello,

I'm working with an old code base that makes excessive use of the
following sort of construct:

char display[128];

...

snprintf(display, sizeof(display), "example log message %d", i);
log_buffer(level, strlen(display)+1, display);

I'd like to replace this mess with a single call to a function named
log_formatted(). This involves moving the format string and arguments
passed to snprintf into the log_formatted() call, removing snprintf
call and (here's where it gets a bit tricky) remove the buffer if it
isn't used for anything else.

I have this all working with the following script with the caveat that
running it on moderately complicated source files makes it never
finish (after an hour or so the spatch process crashes with a stack
overflow error).
I've tried --no-loops which seem to speed things up but complicated
source files still result in it never finishing.

@r1@
type T;
identifier disp;
expression level;
expression list prnt;
@@

{
... when any
(
T disp[...];
|
T disp[...]="";
)
<+...
  snprintf(disp, sizeof(disp), prnt);
... when != disp
- log_buffer(level, strlen(disp)+1, disp);
+ log_formatted(level, prnt);
...+>
}

// Only remove the display variable and snprintf if there are no
// other references to the variable.

@r2 depends on r1@
type r1.T;
identifier r1.disp;
expression list r1.prnt;
@@

{
... when any
(
- T disp[...];
|
- T disp[...]="";
)
<+... when != disp
- snprintf(disp, sizeof(disp), prnt);
...+>
}


Any suggestions on changes to my script that would make this work on
lengthy source files would be greatly appreciated!

Thanks,
George McCollister
_______________________________________________
Cocci mailing list
Cocci@systeme.lip6.fr
https://systeme.lip6.fr/mailman/listinfo/cocci

             reply	other threads:[~2020-03-27 18:24 UTC|newest]

Thread overview: 3+ messages / expand[flat|nested]  mbox.gz  Atom feed  top
2020-03-27 18:19 George McCollister [this message]
2020-03-27 18:37 ` [Cocci] using cocci to switch to formatted log/print function Julia Lawall
2020-03-27 22:54   ` George McCollister

Reply instructions:

You may reply publicly to this message via plain-text email
using any one of the following methods:

* Save the following mbox file, import it into your mail client,
  and reply-to-all from there: mbox

  Avoid top-posting and favor interleaved quoting:
  https://en.wikipedia.org/wiki/Posting_style#Interleaved_style

* Reply using the --to, --cc, and --in-reply-to
  switches of git-send-email(1):

  git send-email \
    --in-reply-to='CAFSKS=NXVUcp1KwpidZoFTfyUCmh+8ij2njaGZ7g6h9LvEiYrA@mail.gmail.com' \
    --to=george.mccollister@gmail.com \
    --cc=cocci@systeme.lip6.fr \
    /path/to/YOUR_REPLY

  https://kernel.org/pub/software/scm/git/docs/git-send-email.html

* If your mail client supports setting the In-Reply-To header
  via mailto: links, try the mailto: link
Be sure your reply has a Subject: header at the top and a blank line before the message body.
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).