Skip to contentSkip to conversation

question on parsing g_assert_cmpint() macro

Cocci Eric Blake 2 participants 4 messages Apr 4, 2017 – Apr 5, 2017

lore
Share & tools
lore

Eric Blake

author
#0 lore raw

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?

Signature · 10 lines
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>
#1 lore raw
Quoted 40 lines · Eric Blake

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.

Quoted 3 lines

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

Quoted 6 lines

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

author
#2 lore raw
Quoted 4 lines · Julia Lawall
Quoted 3 lines

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?

Quoted 8 lines

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.

Signature · 10 lines
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>
#3 lore raw
Quoted 13 lines · Eric Blake
Quoted 11 lines · Julia Lawall
Quoted 3 lines

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

Quoted 6 lines
Quoted 1 line

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

Quoted 8 lines

which indeed solves the problem.

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