qemu-devel.nongnu.org archive mirror
 help / color / mirror / Atom feed
* Function-like macro with the same name as a typedef confuses Coccinelle
@ 2020-04-02 12:06 Markus Armbruster
  2020-04-02 12:21 ` Peter Maydell
  2020-04-02 12:29 ` Philippe Mathieu-Daudé
  0 siblings, 2 replies; 7+ messages in thread
From: Markus Armbruster @ 2020-04-02 12:06 UTC (permalink / raw)
  To: qemu-devel; +Cc: Peter Maydell, Vladimir Sementsov-Ogievskiy, Paolo Bonzini

I discovered that Vladimir's auto-propagated-errp.cocci leaves
hw/arm/armsse.c unchanged, even though it clearly should change it.
Running spatch with --debug prints (among lots of other things)

    let's go
    -----------------------------------------------------------------------
    -----------------------------------------------------------------------
    parse error 
     = error in hw/arm/armsse.c; set verbose_parsing for more info
    badcount: 7
    bad: }
    bad: 
    bad: static void nsccfg_handler(void *opaque, int n, int level)
    bad: {
    BAD:!!!!!     ARMSSE *s = ARMSSE(opaque);
    bad: 
    bad:     s->nsccfg = level;
    bad: }
    parse error 

Alright, what's wrong with this?  ARMSSE is both a function-like macro
and a typedef:

    #define ARMSSE(obj) OBJECT_CHECK(ARMSSE, (obj), TYPE_ARMSSE)

    typedef struct ARMSSE {
        ...
    } ARMSSE;

This appears to spook Coccinelle badly enough to skip code using ARMSSE.

If I rename the macro to ARMSSE_() just for testing, it produces the
transformation I'd expect.

Grepping for typedef names is bothersome, so I used ctags -x to build a
cross reference table, and searched that for function-like macros
clashing with typedefs.  Result:

include/hw/arm/armsse.h:#define ARMSSE(obj) OBJECT_CHECK(ARMSSE, (obj), TYPE_ARMSSE)
include/hw/arm/armsse.h:} ARMSSE;
include/hw/block/swim.h:#define SWIM(obj) OBJECT_CHECK(SWIM, (obj), TYPE_SWIM)
include/hw/block/swim.h:} SWIM;
target/rx/cpu-qom.h:#define RXCPU(obj) \
target/rx/cpu.h:typedef struct RXCPU RXCPU;
target/s390x/translate.c:#define BD(N, BB, BD) { BB,  4, 0, FLD_C_b##N, FLD_O_b##N }, \
hw/audio/ac97.c:} BD;

The last one is a name clash in unrelated files; should not bother
Coccinelle.

The first three are due to QOM.  By convention, the name of the
function-like macro to convert to the QOM type is the QOM type in
ALL_CAPS.  Clash when the QOM type is ALL_CAPS already.

Clearly, Coccinelle is getting spooked to easily.  Regardless, three
questions:

1. Are ALL_CAPS typedef names a good idea?  We shout macros to tell
readers "beware, possibly magic".  Shouting other stuff as well
undermines that.

2. The compiler is quite cool with us using the same name for a
function-like macro and a not-function-like non-macro.  But is it a good
idea?

3. Do we want to work around Coccinelle's parsing flaw?  Having it skip
so much code is clearly suboptimal, and potentially dangerous.  The
obvious work-around is to rename the three problematic types so they
contain at least one lower-case letter.



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

end of thread, other threads:[~2020-04-07 13:49 UTC | newest]

Thread overview: 7+ messages (download: mbox.gz follow: Atom feed
-- links below jump to the message on this page --
2020-04-02 12:06 Function-like macro with the same name as a typedef confuses Coccinelle Markus Armbruster
2020-04-02 12:21 ` Peter Maydell
2020-04-02 13:30   ` Markus Armbruster
2020-04-07 11:58     ` Markus Armbruster
2020-04-07 13:47       ` Eric Blake
2020-04-02 12:29 ` Philippe Mathieu-Daudé
2020-04-02 13:47   ` Markus Armbruster

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).