All of lore.kernel.org
 help / color / mirror / Atom feed
* [Cocci] question on parsing g_assert_cmpint() macro
@ 2017-04-04 22:11 Eric Blake
  2017-04-05  5:25 ` Julia Lawall
  0 siblings, 1 reply; 4+ messages in thread
From: Eric Blake @ 2017-04-04 22:11 UTC (permalink / raw)
  To: cocci

I'm trying to use the following rather-simple coccinelle patch to
simplify qemu:

@@
expression Obj, Key, E;
@@
- qdict_put(Obj, Key, qint_from_int(E));
+ qdict_put_int(Obj, Key, E);

I noticed by chance, however, that it only found some, and not all,
instances of the pattern in the file tests/check-qdict.c
(git.qemu-project.org/?p=qemu.git;a=blob;f=tests/check-qdict.c;h=81162ee).
 Running 'spatch --verbose-parsing ...' finally let me figure out why:

...
parse error
 = File "tests/check-qdict.c", line 473, column 55, charpos = 10131
  around = '==',
  whole content =     g_assert_cmpint(qdict_array_entries(dict, "foo."),
==, 0);
badcount: 44
bad: }
bad:
bad: static void qdict_array_entries_test(void)
bad: {
bad:     QDict *dict = qdict_new();
bad:
BAD:!!!!!     g_assert_cmpint(qdict_array_entries(dict, "foo."), ==, 0);
bad:
bad:     qdict_put(dict, "bar", qint_from_int(0));
bad:     qdict_put(dict, "baz.0", qint_from_int(0));
...

So the fact that the g_assert_cmpint() macro (from glib,
https://developer.gnome.org/glib/stable/glib-Testing.html#g-assert-cmpint)
has unusual semantics that make it called differently than a normal C
function (the second argument '==' is what the parser chokes on) means
that coccinelle is then ignoring the ENTIRE function where the parse
problem occurred, and missing the instance of my pattern just afterwards.

What is the trick for teaching coccinelle about what g_assert_cmpint()
expands to, and/or completely ignoring the use of that macro, so that I
don't have to manually look for spots that the cleanup missed?

-- 
Eric Blake   eblake redhat com    +1-919-301-3266
Libvirt virtualization library http://libvirt.org

-------------- next part --------------
A non-text attachment was scrubbed...
Name: signature.asc
Type: application/pgp-signature
Size: 604 bytes
Desc: OpenPGP digital signature
URL: <https://systeme.lip6.fr/pipermail/cocci/attachments/20170404/45f19db4/attachment.asc>

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

* [Cocci] question on parsing g_assert_cmpint() macro
  2017-04-04 22:11 [Cocci] question on parsing g_assert_cmpint() macro Eric Blake
@ 2017-04-05  5:25 ` Julia Lawall
  2017-04-05 14:14   ` Eric Blake
  0 siblings, 1 reply; 4+ messages in thread
From: Julia Lawall @ 2017-04-05  5:25 UTC (permalink / raw)
  To: cocci



On Tue, 4 Apr 2017, Eric Blake wrote:

> I'm trying to use the following rather-simple coccinelle patch to
> simplify qemu:
>
> @@
> expression Obj, Key, E;
> @@
> - qdict_put(Obj, Key, qint_from_int(E));
> + qdict_put_int(Obj, Key, E);
>
> I noticed by chance, however, that it only found some, and not all,
> instances of the pattern in the file tests/check-qdict.c
> (git.qemu-project.org/?p=qemu.git;a=blob;f=tests/check-qdict.c;h=81162ee).
>  Running 'spatch --verbose-parsing ...' finally let me figure out why:
>
> ...
> parse error
>  = File "tests/check-qdict.c", line 473, column 55, charpos = 10131
>   around = '==',
>   whole content =     g_assert_cmpint(qdict_array_entries(dict, "foo."),
> ==, 0);
> badcount: 44
> bad: }
> bad:
> bad: static void qdict_array_entries_test(void)
> bad: {
> bad:     QDict *dict = qdict_new();
> bad:
> BAD:!!!!!     g_assert_cmpint(qdict_array_entries(dict, "foo."), ==, 0);
> bad:
> bad:     qdict_put(dict, "bar", qint_from_int(0));
> bad:     qdict_put(dict, "baz.0", qint_from_int(0));
> ...
>
> So the fact that the g_assert_cmpint() macro (from glib,
> https://developer.gnome.org/glib/stable/glib-Testing.html#g-assert-cmpint)
> has unusual semantics that make it called differently than a normal C
> function (the second argument '==' is what the parser chokes on) means
> that coccinelle is then ignoring the ENTIRE function where the parse
> problem occurred, and missing the instance of my pattern just afterwards.
>
> What is the trick for teaching coccinelle about what g_assert_cmpint()
> expands to, and/or completely ignoring the use of that macro, so that I
> don't have to manually look for spots that the cleanup missed?

Try adding

#define g_assert_cmpint(x,y,z)

to the macro definition file standard.h.  Or you can make your own macro
definition file for qemu and give it as an argument --macro-file file.h

Write back if that does not solve the problem.

julia


>
> --
> Eric Blake   eblake redhat com    +1-919-301-3266
> Libvirt virtualization library http://libvirt.org
>
>

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

* [Cocci] question on parsing g_assert_cmpint() macro
  2017-04-05  5:25 ` Julia Lawall
@ 2017-04-05 14:14   ` Eric Blake
  2017-04-05 14:18     ` Julia Lawall
  0 siblings, 1 reply; 4+ messages in thread
From: Eric Blake @ 2017-04-05 14:14 UTC (permalink / raw)
  To: cocci

On 04/05/2017 12:25 AM, Julia Lawall wrote:

>> What is the trick for teaching coccinelle about what g_assert_cmpint()
>> expands to, and/or completely ignoring the use of that macro, so that I
>> don't have to manually look for spots that the cleanup missed?
> 
> Try adding
> 
> #define g_assert_cmpint(x,y,z)
> 
> to the macro definition file standard.h.  Or you can make your own macro
> definition file for qemu and give it as an argument --macro-file file.h
> 
> Write back if that does not solve the problem.

Thanks. I didn't realize qemu already had a scripts/cocci-macro-file.h
designed just for this purpose, and it includes

#define g_assert_cmpint(a, op, b)   g_assert(a op b)

which indeed solves the problem.

-- 
Eric Blake   eblake redhat com    +1-919-301-3266
Libvirt virtualization library http://libvirt.org

-------------- next part --------------
A non-text attachment was scrubbed...
Name: signature.asc
Type: application/pgp-signature
Size: 604 bytes
Desc: OpenPGP digital signature
URL: <https://systeme.lip6.fr/pipermail/cocci/attachments/20170405/ec5b3db3/attachment.asc>

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

* [Cocci] question on parsing g_assert_cmpint() macro
  2017-04-05 14:14   ` Eric Blake
@ 2017-04-05 14:18     ` Julia Lawall
  0 siblings, 0 replies; 4+ messages in thread
From: Julia Lawall @ 2017-04-05 14:18 UTC (permalink / raw)
  To: cocci



On Wed, 5 Apr 2017, Eric Blake wrote:

> On 04/05/2017 12:25 AM, Julia Lawall wrote:
>
> >> What is the trick for teaching coccinelle about what g_assert_cmpint()
> >> expands to, and/or completely ignoring the use of that macro, so that I
> >> don't have to manually look for spots that the cleanup missed?
> >
> > Try adding
> >
> > #define g_assert_cmpint(x,y,z)
> >
> > to the macro definition file standard.h.  Or you can make your own macro
> > definition file for qemu and give it as an argument --macro-file file.h
> >
> > Write back if that does not solve the problem.
>
> Thanks. I didn't realize qemu already had a scripts/cocci-macro-file.h
> designed just for this purpose, and it includes
>
> #define g_assert_cmpint(a, op, b)   g_assert(a op b)

This definition looks fine too.

julia

>
> which indeed solves the problem.
>
> --
> Eric Blake   eblake redhat com    +1-919-301-3266
> Libvirt virtualization library http://libvirt.org
>
>

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

end of thread, other threads:[~2017-04-05 14:18 UTC | newest]

Thread overview: 4+ messages (download: mbox.gz / follow: Atom feed)
-- links below jump to the message on this page --
2017-04-04 22:11 [Cocci] question on parsing g_assert_cmpint() macro Eric Blake
2017-04-05  5:25 ` Julia Lawall
2017-04-05 14:14   ` Eric Blake
2017-04-05 14:18     ` Julia Lawall

This is an external index of several public inboxes,
see mirroring instructions on how to clone and mirror
all data and code used by this external index.