qemu-devel.nongnu.org archive mirror
 help / color / mirror / Atom feed
* [Qemu-devel] [RFC, PATCH] Add -Wstrict-prototypes, maybe later -Wmissing-prototypes
@ 2008-08-08 17:22 Blue Swirl
  2008-08-09 18:24 ` Anthony Liguori
  0 siblings, 1 reply; 23+ messages in thread
From: Blue Swirl @ 2008-08-08 17:22 UTC (permalink / raw)
  To: qemu-devel

[-- Attachment #1: Type: text/plain, Size: 642 bytes --]

Hi,

I made a series of patches that add -Wstrict-prototypes to the CFLAGS
and then -Wmissing-prototypes, both of which are enabled by Xen. I
also fixed most warnings generated -Wstrict-prototypes and some of
them for the -Wmissing-prototypes case.

Compiling with -Wstrict-prototypes produces only one extra warning. I
think this flag should be enabled.

But dyngen targets spew a lot of noise with -Wmissing-prototypes so
I'm not proposing to add that (or maybe that should be coupled with
CONFIG_DYNGEN_OP). This also meant that I did not care enough to fix
all warnings. The current warning fix should be safe to apply anyway.

Comments?

[-- Attachment #2: wstrict_prototypes.diff --]
[-- Type: plain/text, Size: 1007 bytes --]

[-- Attachment #3: fix_wstrict_prototypes_warnings.diff --]
[-- Type: plain/text, Size: 2876 bytes --]

[-- Attachment #4: wmissing_prototypes.diff --]
[-- Type: plain/text, Size: 1089 bytes --]

[-- Attachment #5: fix_wmissing_prototypes_warnings.diff --]
[-- Type: plain/text, Size: 15100 bytes --]

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

* Re: [Qemu-devel] [RFC, PATCH] Add -Wstrict-prototypes, maybe later -Wmissing-prototypes
  2008-08-08 17:22 [Qemu-devel] [RFC, PATCH] Add -Wstrict-prototypes, maybe later -Wmissing-prototypes Blue Swirl
@ 2008-08-09 18:24 ` Anthony Liguori
  2008-08-10 18:33   ` Blue Swirl
  0 siblings, 1 reply; 23+ messages in thread
From: Anthony Liguori @ 2008-08-09 18:24 UTC (permalink / raw)
  To: qemu-devel

Blue Swirl wrote:
> Hi,
>
> I made a series of patches that add -Wstrict-prototypes to the CFLAGS
> and then -Wmissing-prototypes, both of which are enabled by Xen. I
> also fixed most warnings generated -Wstrict-prototypes and some of
> them for the -Wmissing-prototypes case.
>
> Compiling with -Wstrict-prototypes produces only one extra warning. I
> think this flag should be enabled.
>   

As long as the plan is to fix all of those warnings, I think it's a good 
idea.

Regards,

Anthony Liguori

> But dyngen targets spew a lot of noise with -Wmissing-prototypes so
> I'm not proposing to add that (or maybe that should be coupled with
> CONFIG_DYNGEN_OP). This also meant that I did not care enough to fix
> all warnings. The current warning fix should be safe to apply anyway.
>
> Comments?
>   

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

* Re: [Qemu-devel] [RFC, PATCH] Add -Wstrict-prototypes, maybe later -Wmissing-prototypes
  2008-08-09 18:24 ` Anthony Liguori
@ 2008-08-10 18:33   ` Blue Swirl
  2008-08-11  7:52     ` Gerd Hoffmann
  2008-08-11 14:21     ` Anthony Liguori
  0 siblings, 2 replies; 23+ messages in thread
From: Blue Swirl @ 2008-08-10 18:33 UTC (permalink / raw)
  To: qemu-devel

On 8/9/08, Anthony Liguori <anthony@codemonkey.ws> wrote:
> Blue Swirl wrote:
>
> > Hi,
> >
> > I made a series of patches that add -Wstrict-prototypes to the CFLAGS
> > and then -Wmissing-prototypes, both of which are enabled by Xen. I
> > also fixed most warnings generated -Wstrict-prototypes and some of
> > them for the -Wmissing-prototypes case.
> >
> > Compiling with -Wstrict-prototypes produces only one extra warning. I
> > think this flag should be enabled.
> >
> >
>
>  As long as the plan is to fix all of those warnings, I think it's a good
> idea.

The extra unfixed warning comes from monitor.c:
typedef struct term_cmd_t {
    const char *name;
    const char *args_type;
    void (*handler)();
    const char *params;
    const char *help;
} term_cmd_t;

The warning is generated because the definition of "handler" should
also describe the parameters and not use the old () style. But in this
case, they can vary:
static void do_help(const char *name)
static void do_quit(void)
static void do_eject(int force, const char *filename)
static void do_change(const char *device, const char *target, const char *fmt)
static void do_screen_dump(const char *filename)
static void do_memory_dump(int count, int format, int size,
                           uint32_t addrh, uint32_t addrl)
static void do_print(int count, int format, int size, unsigned int
valh, unsigned int vall)
static void do_ioport_read(int count, int format, int size, int addr,
int has_index, int index)
etc.

I don't have a good plan how to fix this, proposals are welcome.
Changing all handlers to use va_args to just silence a gcc warning
sounds like overkill.

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

* Re: [Qemu-devel] [RFC, PATCH] Add -Wstrict-prototypes, maybe later -Wmissing-prototypes
  2008-08-10 18:33   ` Blue Swirl
@ 2008-08-11  7:52     ` Gerd Hoffmann
  2008-08-11  9:54       ` Samuel Thibault
  2008-08-11 13:30       ` M. Warner Losh
  2008-08-11 14:21     ` Anthony Liguori
  1 sibling, 2 replies; 23+ messages in thread
From: Gerd Hoffmann @ 2008-08-11  7:52 UTC (permalink / raw)
  To: qemu-devel

Blue Swirl wrote:
> On 8/9/08, Anthony Liguori <anthony@codemonkey.ws> wrote:
>>  As long as the plan is to fix all of those warnings, I think it's a good
>> idea.
> 
> The extra unfixed warning comes from monitor.c:
> typedef struct term_cmd_t {
>     const char *name;
>     const char *args_type;
>     void (*handler)();
>     const char *params;
>     const char *help;
> } term_cmd_t;
> 
> The warning is generated because the definition of "handler" should
> also describe the parameters and not use the old () style. But in this
> case, they can vary:
> static void do_help(const char *name)
> static void do_quit(void)
[ ... ]

> I don't have a good plan how to fix this, proposals are welcome.
> Changing all handlers to use va_args to just silence a gcc warning
> sounds like overkill.

Using a union maybe?

typedef struct term_cmd_t {
      [ ... ]
      union {
          void (*help)(const char name);
          void (*quit)(void);
          [ ... ]
     } handlers;
     [ ... ]
};

cheers,
  Gerd


-- 
http://kraxel.fedorapeople.org/xenner/

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

* Re: [Qemu-devel] [RFC, PATCH] Add -Wstrict-prototypes, maybe later -Wmissing-prototypes
  2008-08-11  7:52     ` Gerd Hoffmann
@ 2008-08-11  9:54       ` Samuel Thibault
  2008-08-11 13:32         ` M. Warner Losh
  2008-08-11 13:30       ` M. Warner Losh
  1 sibling, 1 reply; 23+ messages in thread
From: Samuel Thibault @ 2008-08-11  9:54 UTC (permalink / raw)
  To: qemu-devel

Gerd Hoffmann, le Mon 11 Aug 2008 09:52:03 +0200, a écrit :
> > The warning is generated because the definition of "handler" should
> > also describe the parameters and not use the old () style. But in this
> > case, they can vary:
> > static void do_help(const char *name)
> > static void do_quit(void)
> [ ... ]
> 
> > I don't have a good plan how to fix this, proposals are welcome.
> > Changing all handlers to use va_args to just silence a gcc warning
> > sounds like overkill.
> 
> Using a union maybe?

That should be the right thing yes.

Samuel

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

* Re: [Qemu-devel] [RFC, PATCH] Add -Wstrict-prototypes, maybe later -Wmissing-prototypes
  2008-08-11  7:52     ` Gerd Hoffmann
  2008-08-11  9:54       ` Samuel Thibault
@ 2008-08-11 13:30       ` M. Warner Losh
  1 sibling, 0 replies; 23+ messages in thread
From: M. Warner Losh @ 2008-08-11 13:30 UTC (permalink / raw)
  To: qemu-devel, kraxel

In message: <489FEFA3.3040105@redhat.com>
            Gerd Hoffmann <kraxel@redhat.com> writes:
: Blue Swirl wrote:
: > On 8/9/08, Anthony Liguori <anthony@codemonkey.ws> wrote:
: >>  As long as the plan is to fix all of those warnings, I think it's a good
: >> idea.
: > 
: > The extra unfixed warning comes from monitor.c:
: > typedef struct term_cmd_t {
: >     const char *name;
: >     const char *args_type;
: >     void (*handler)();
: >     const char *params;
: >     const char *help;
: > } term_cmd_t;
: > 
: > The warning is generated because the definition of "handler" should
: > also describe the parameters and not use the old () style. But in this
: > case, they can vary:
: > static void do_help(const char *name)
: > static void do_quit(void)
: [ ... ]
: 
: > I don't have a good plan how to fix this, proposals are welcome.
: > Changing all handlers to use va_args to just silence a gcc warning
: > sounds like overkill.
: 
: Using a union maybe?
: 
: typedef struct term_cmd_t {
:       [ ... ]
:       union {
:           void (*help)(const char name);
:           void (*quit)(void);
:           [ ... ]
:      } handlers;
:      [ ... ]
: };

Or just have all the commands use the same arguments?  That's usually
how this is done.  (*handler) should take a void *, which the callee
can do whatever it wants with...

Warner

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

* Re: [Qemu-devel] [RFC, PATCH] Add -Wstrict-prototypes, maybe later -Wmissing-prototypes
  2008-08-11  9:54       ` Samuel Thibault
@ 2008-08-11 13:32         ` M. Warner Losh
  0 siblings, 0 replies; 23+ messages in thread
From: M. Warner Losh @ 2008-08-11 13:32 UTC (permalink / raw)
  To: qemu-devel, samuel.thibault

In message: <20080811095457.GA4499@implementation.uk.xensource.com>
            Samuel Thibault <samuel.thibault@ens-lyon.org> writes:
: Gerd Hoffmann, le Mon 11 Aug 2008 09:52:03 +0200, a écrit :
: > > The warning is generated because the definition of "handler" should
: > > also describe the parameters and not use the old () style. But in this
: > > case, they can vary:
: > > static void do_help(const char *name)
: > > static void do_quit(void)
: > [ ... ]
: > 
: > > I don't have a good plan how to fix this, proposals are welcome.
: > > Changing all handlers to use va_args to just silence a gcc warning
: > > sounds like overkill.
: > 
: > Using a union maybe?
: 
: That should be the right thing yes.

Except it becomes unmanageable over time...  It also doesn't enhance
type safety...

Warner

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

* Re: [Qemu-devel] [RFC, PATCH] Add -Wstrict-prototypes, maybe later -Wmissing-prototypes
  2008-08-10 18:33   ` Blue Swirl
  2008-08-11  7:52     ` Gerd Hoffmann
@ 2008-08-11 14:21     ` Anthony Liguori
  2008-08-11 14:48       ` Avi Kivity
  1 sibling, 1 reply; 23+ messages in thread
From: Anthony Liguori @ 2008-08-11 14:21 UTC (permalink / raw)
  To: qemu-devel

Blue Swirl wrote:
> On 8/9/08, Anthony Liguori <anthony@codemonkey.ws> wrote:
>   
>> Blue Swirl wrote:
>>
>>     
>>> Hi,
>>>
>>> I made a series of patches that add -Wstrict-prototypes to the CFLAGS
>>> and then -Wmissing-prototypes, both of which are enabled by Xen. I
>>> also fixed most warnings generated -Wstrict-prototypes and some of
>>> them for the -Wmissing-prototypes case.
>>>
>>> Compiling with -Wstrict-prototypes produces only one extra warning. I
>>> think this flag should be enabled.
>>>
>>>
>>>       
>>  As long as the plan is to fix all of those warnings, I think it's a good
>> idea.
>>     
>
> The extra unfixed warning comes from monitor.c:
> typedef struct term_cmd_t {
>     const char *name;
>     const char *args_type;
>     void (*handler)();
>     const char *params;
>     const char *help;
> } term_cmd_t;
>
> The warning is generated because the definition of "handler" should
> also describe the parameters and not use the old () style. But in this
> case, they can vary:
>   

You could just switch void (*handler)() to void *handler.

Regards,

Anthony Liguori

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

* Re: [Qemu-devel] [RFC, PATCH] Add -Wstrict-prototypes, maybe later -Wmissing-prototypes
  2008-08-11 14:21     ` Anthony Liguori
@ 2008-08-11 14:48       ` Avi Kivity
  2008-08-11 14:53         ` Paul Brook
                           ` (2 more replies)
  0 siblings, 3 replies; 23+ messages in thread
From: Avi Kivity @ 2008-08-11 14:48 UTC (permalink / raw)
  To: qemu-devel

Anthony Liguori wrote:
> Blue Swirl wrote:
>> On 8/9/08, Anthony Liguori <anthony@codemonkey.ws> wrote:
>>  
>>> Blue Swirl wrote:
>>>
>>>    
>>>> Hi,
>>>>
>>>> I made a series of patches that add -Wstrict-prototypes to the CFLAGS
>>>> and then -Wmissing-prototypes, both of which are enabled by Xen. I
>>>> also fixed most warnings generated -Wstrict-prototypes and some of
>>>> them for the -Wmissing-prototypes case.
>>>>
>>>> Compiling with -Wstrict-prototypes produces only one extra warning. I
>>>> think this flag should be enabled.
>>>>
>>>>
>>>>       
>>>  As long as the plan is to fix all of those warnings, I think it's a 
>>> good
>>> idea.
>>>     
>>
>> The extra unfixed warning comes from monitor.c:
>> typedef struct term_cmd_t {
>>     const char *name;
>>     const char *args_type;
>>     void (*handler)();
>>     const char *params;
>>     const char *help;
>> } term_cmd_t;
>>
>> The warning is generated because the definition of "handler" should
>> also describe the parameters and not use the old () style. But in this
>> case, they can vary:
>>   
>
> You could just switch void (*handler)() to void *handler. 

Function pointers might be wider than data pointers.

Looking at the usage, though, it looks like the best thing is to have a

  void (*handler)(const char **args)

So we pass the array of arguments (could be zero length) to the handler.

-- 
error compiling committee.c: too many arguments to function

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

* Re: [Qemu-devel] [RFC, PATCH] Add -Wstrict-prototypes, maybe later -Wmissing-prototypes
  2008-08-11 14:48       ` Avi Kivity
@ 2008-08-11 14:53         ` Paul Brook
  2008-08-11 14:56         ` Laurent Vivier
  2008-08-11 14:56         ` Anthony Liguori
  2 siblings, 0 replies; 23+ messages in thread
From: Paul Brook @ 2008-08-11 14:53 UTC (permalink / raw)
  To: qemu-devel

> > You could just switch void (*handler)() to void *handler.
>
> Function pointers might be wider than data pointers.

I'm pretty sure this isn't true on any current system. On some targets you end 
up using a function descriptor, but it should still work.

> void (*handler)(const char **args)

This seems like a reasonable idea. None of this code is performance critical.

Paul

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

* Re: [Qemu-devel] [RFC, PATCH] Add -Wstrict-prototypes, maybe later -Wmissing-prototypes
  2008-08-11 14:48       ` Avi Kivity
  2008-08-11 14:53         ` Paul Brook
@ 2008-08-11 14:56         ` Laurent Vivier
  2008-08-11 14:56         ` Anthony Liguori
  2 siblings, 0 replies; 23+ messages in thread
From: Laurent Vivier @ 2008-08-11 14:56 UTC (permalink / raw)
  To: qemu-devel

Le lundi 11 août 2008 à 17:48 +0300, Avi Kivity a écrit :
> Anthony Liguori wrote:
> > Blue Swirl wrote:
> >> On 8/9/08, Anthony Liguori <anthony@codemonkey.ws> wrote:
> >>  
> >>> Blue Swirl wrote:
> >>>
> >>>    
> >>>> Hi,
> >>>>
> >>>> I made a series of patches that add -Wstrict-prototypes to the CFLAGS
> >>>> and then -Wmissing-prototypes, both of which are enabled by Xen. I
> >>>> also fixed most warnings generated -Wstrict-prototypes and some of
> >>>> them for the -Wmissing-prototypes case.
> >>>>
> >>>> Compiling with -Wstrict-prototypes produces only one extra warning. I
> >>>> think this flag should be enabled.
> >>>>
> >>>>
> >>>>       
> >>>  As long as the plan is to fix all of those warnings, I think it's a 
> >>> good
> >>> idea.
> >>>     
> >>
> >> The extra unfixed warning comes from monitor.c:
> >> typedef struct term_cmd_t {
> >>     const char *name;
> >>     const char *args_type;
> >>     void (*handler)();
> >>     const char *params;
> >>     const char *help;
> >> } term_cmd_t;
> >>
> >> The warning is generated because the definition of "handler" should
> >> also describe the parameters and not use the old () style. But in this
> >> case, they can vary:
> >>   
> >
> > You could just switch void (*handler)() to void *handler. 
> 
> Function pointers might be wider than data pointers.
> 
> Looking at the usage, though, it looks like the best thing is to have a
> 
>   void (*handler)(const char **args)
> 
> So we pass the array of arguments (could be zero length) to the handler.

I agree with that.

Each command can be seen like a shell command and "handler" is like the
"int main(int argc, char **argv)".

Perhaps we can have an "int (*handler)(int argc, char **argv)" ?

Regards,
Laurent
-- 
----------------- Laurent.Vivier@bull.net  ------------------
  "La perfection est atteinte non quand il ne reste rien à
ajouter mais quand il ne reste rien à enlever." Saint Exupéry

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

* Re: [Qemu-devel] [RFC, PATCH] Add -Wstrict-prototypes, maybe later -Wmissing-prototypes
  2008-08-11 14:48       ` Avi Kivity
  2008-08-11 14:53         ` Paul Brook
  2008-08-11 14:56         ` Laurent Vivier
@ 2008-08-11 14:56         ` Anthony Liguori
  2008-08-11 16:38           ` Avi Kivity
  2 siblings, 1 reply; 23+ messages in thread
From: Anthony Liguori @ 2008-08-11 14:56 UTC (permalink / raw)
  To: qemu-devel

Avi Kivity wrote:
> Anthony Liguori wrote:
>> Blue Swirl wrote:
>>> On 8/9/08, Anthony Liguori <anthony@codemonkey.ws> wrote:
>>>  
>>>> Blue Swirl wrote:
>>>>
>>>>   
>>>>> Hi,
>>>>>
>>>>> I made a series of patches that add -Wstrict-prototypes to the CFLAGS
>>>>> and then -Wmissing-prototypes, both of which are enabled by Xen. I
>>>>> also fixed most warnings generated -Wstrict-prototypes and some of
>>>>> them for the -Wmissing-prototypes case.
>>>>>
>>>>> Compiling with -Wstrict-prototypes produces only one extra warning. I
>>>>> think this flag should be enabled.
>>>>>
>>>>>
>>>>>       
>>>>  As long as the plan is to fix all of those warnings, I think it's 
>>>> a good
>>>> idea.
>>>>     
>>>
>>> The extra unfixed warning comes from monitor.c:
>>> typedef struct term_cmd_t {
>>>     const char *name;
>>>     const char *args_type;
>>>     void (*handler)();
>>>     const char *params;
>>>     const char *help;
>>> } term_cmd_t;
>>>
>>> The warning is generated because the definition of "handler" should
>>> also describe the parameters and not use the old () style. But in this
>>> case, they can vary:
>>>   
>>
>> You could just switch void (*handler)() to void *handler. 
>
> Function pointers might be wider than data pointers.

On what platforms?  void (*)() was deprecated as a generic function 
pointer.  IIUC, there is no replacement and the rationale for that is 
that the vast majority of people can just use 'void *'.  The general 
technique is discouraged though.

> Looking at the usage, though, it looks like the best thing is to have a
>
>  void (*handler)(const char **args)
>
> So we pass the array of arguments (could be zero length) to the handler.

Yeah, it uglifies things quite a bit though.  You lose all of the nice 
parsing and lack of casting.

Regards,

Anthony Liguori

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

* Re: [Qemu-devel] [RFC, PATCH] Add -Wstrict-prototypes, maybe later -Wmissing-prototypes
  2008-08-11 14:56         ` Anthony Liguori
@ 2008-08-11 16:38           ` Avi Kivity
  2008-08-11 16:47             ` Anthony Liguori
  0 siblings, 1 reply; 23+ messages in thread
From: Avi Kivity @ 2008-08-11 16:38 UTC (permalink / raw)
  To: qemu-devel

Anthony Liguori wrote:
>>>
>>> You could just switch void (*handler)() to void *handler. 
>>
>> Function pointers might be wider than data pointers.
>
> On what platforms?  void (*)() was deprecated as a generic function 
> pointer.  IIUC, there is no replacement and the rationale for that is 
> that the vast majority of people can just use 'void *'.  The general 
> technique is discouraged though.
>

Segmented x86.  You have function pointers consisting of a 
segment:offset, and data pointers consisting of an offset into the 
implied data segment.  This was one out of five available data models.

As Paul mentioned, no modern machine does that anymore, but the 
distinction lives on in the standard.

>> Looking at the usage, though, it looks like the best thing is to have a
>>
>>  void (*handler)(const char **args)
>>
>> So we pass the array of arguments (could be zero length) to the handler.
>
> Yeah, it uglifies things quite a bit though.  You lose all of the nice 
> parsing and lack of casting. 

You lose the switch(nb_args) in monitor_handle_command() so it comes out 
even.  You don't need to cast anything -- though you do need to change 
all the callbacks to accept an argument array.

-- 
error compiling committee.c: too many arguments to function

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

* Re: [Qemu-devel] [RFC, PATCH] Add -Wstrict-prototypes, maybe later -Wmissing-prototypes
  2008-08-11 16:38           ` Avi Kivity
@ 2008-08-11 16:47             ` Anthony Liguori
  2008-08-11 18:52               ` Blue Swirl
  0 siblings, 1 reply; 23+ messages in thread
From: Anthony Liguori @ 2008-08-11 16:47 UTC (permalink / raw)
  To: qemu-devel

Avi Kivity wrote:
>
> You lose the switch(nb_args) in monitor_handle_command() so it comes 
> out even.  You don't need to cast anything -- though you do need to 
> change all the callbacks to accept an argument array.

And to either explicitly cast each argument or explicitly parse each 
argument.  It touches quite a bit of places in the code and makes the 
callbacks quite a bit more complicated.

Using a void * is a one line change and works on all modern systems.  
Seems like a winner to me :-)

Regards,

Anthony Liguori

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

* Re: [Qemu-devel] [RFC, PATCH] Add -Wstrict-prototypes, maybe later -Wmissing-prototypes
  2008-08-11 16:47             ` Anthony Liguori
@ 2008-08-11 18:52               ` Blue Swirl
  2008-08-11 18:57                 ` Laurent Vivier
  0 siblings, 1 reply; 23+ messages in thread
From: Blue Swirl @ 2008-08-11 18:52 UTC (permalink / raw)
  To: qemu-devel

On 8/11/08, Anthony Liguori <anthony@codemonkey.ws> wrote:
> Avi Kivity wrote:
>
> >
> > You lose the switch(nb_args) in monitor_handle_command() so it comes out
> even.  You don't need to cast anything -- though you do need to change all
> the callbacks to accept an argument array.
> >
>
>  And to either explicitly cast each argument or explicitly parse each
> argument.  It touches quite a bit of places in the code and makes the
> callbacks quite a bit more complicated.
>
>  Using a void * is a one line change and works on all modern systems.  Seems
> like a winner to me :-)

I vote for this. The other solutions do not improve type safety
(except for union) and they are more complex.

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

* Re: [Qemu-devel] [RFC, PATCH] Add -Wstrict-prototypes, maybe later -Wmissing-prototypes
  2008-08-11 18:52               ` Blue Swirl
@ 2008-08-11 18:57                 ` Laurent Vivier
  2008-08-11 19:11                   ` Blue Swirl
  2008-08-11 19:22                   ` Anthony Liguori
  0 siblings, 2 replies; 23+ messages in thread
From: Laurent Vivier @ 2008-08-11 18:57 UTC (permalink / raw)
  To: qemu-devel; +Cc: Blue Swirl


Le 11 août 08 à 20:52, Blue Swirl a écrit :

> On 8/11/08, Anthony Liguori <anthony@codemonkey.ws> wrote:
>> Avi Kivity wrote:
>>
>>>
>>> You lose the switch(nb_args) in monitor_handle_command() so it  
>>> comes out
>> even.  You don't need to cast anything -- though you do need to  
>> change all
>> the callbacks to accept an argument array.
>>>
>>
>> And to either explicitly cast each argument or explicitly parse each
>> argument.  It touches quite a bit of places in the code and makes the
>> callbacks quite a bit more complicated.
>>
>> Using a void * is a one line change and works on all modern  
>> systems.  Seems
>> like a winner to me :-)
>
> I vote for this. The other solutions do not improve type safety
> (except for union) and they are more complex.

But I think to call the function you have to cast the pointer, this  
doesn't improve type safety too...

Regards,
Laurent
----------------------- Laurent Vivier ----------------------
"The best way to predict the future is to invent it."
- Alan Kay

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

* Re: [Qemu-devel] [RFC, PATCH] Add -Wstrict-prototypes, maybe later -Wmissing-prototypes
  2008-08-11 18:57                 ` Laurent Vivier
@ 2008-08-11 19:11                   ` Blue Swirl
  2008-08-11 19:22                   ` Anthony Liguori
  1 sibling, 0 replies; 23+ messages in thread
From: Blue Swirl @ 2008-08-11 19:11 UTC (permalink / raw)
  To: Laurent Vivier; +Cc: qemu-devel

On 8/11/08, Laurent Vivier <laurent@lvivier.info> wrote:
>
>  Le 11 août 08 à 20:52, Blue Swirl a écrit :
>
>
>
> > On 8/11/08, Anthony Liguori <anthony@codemonkey.ws> wrote:
> >
> > > Avi Kivity wrote:
> > >
> > >
> > > >
> > > > You lose the switch(nb_args) in monitor_handle_command() so it comes
> out
> > > >
> > > even.  You don't need to cast anything -- though you do need to change
> all
> > > the callbacks to accept an argument array.
> > >
> > > >
> > > >
> > >
> > > And to either explicitly cast each argument or explicitly parse each
> > > argument.  It touches quite a bit of places in the code and makes the
> > > callbacks quite a bit more complicated.
> > >
> > > Using a void * is a one line change and works on all modern systems.
> Seems
> > > like a winner to me :-)
> > >
> >
> > I vote for this. The other solutions do not improve type safety
> > (except for union) and they are more complex.
> >
>
>  But I think to call the function you have to cast the pointer, this doesn't
> improve type safety too...

It doesn't, but it's not too complex considering the original aim of
silencing gcc.

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

* Re: [Qemu-devel] [RFC, PATCH] Add -Wstrict-prototypes, maybe later -Wmissing-prototypes
  2008-08-11 18:57                 ` Laurent Vivier
  2008-08-11 19:11                   ` Blue Swirl
@ 2008-08-11 19:22                   ` Anthony Liguori
  2008-08-11 20:03                     ` Laurent Vivier
  2008-08-12 12:24                     ` Daniel Jacobowitz
  1 sibling, 2 replies; 23+ messages in thread
From: Anthony Liguori @ 2008-08-11 19:22 UTC (permalink / raw)
  To: qemu-devel; +Cc: Blue Swirl, Laurent Vivier

Laurent Vivier wrote:
>
>>
>> I vote for this. The other solutions do not improve type safety
>> (except for union) and they are more complex.
>
> But I think to call the function you have to cast the pointer, this 
> doesn't improve type safety too...

Right now, the monitor works by taking a generic function pointer "void 
(*)()" which is treated specially by the C standard (although it's now 
deprecated).  Any function pointer can be cast to a generic function 
pointer.  The dispatch loop basically looks like:

void dispatch(void (*func)(), int n_args, void *args)
{
    switch (n_args) {
    case 1:
        ((void (*)(void *))func)(args[0]); break;
    case 1:
        ((void (*)(void *, void *))func)(args[0]); break;
    ...
}

But this tosses a warning since void (*)() is deprecated.  So, if we use:

void dispatch(void *func, int n_args, void *args)

It'll work just like it did before.

Regards,

Anthony Liguori

>
> Regards,
> Laurent
> ----------------------- Laurent Vivier ----------------------
> "The best way to predict the future is to invent it."
> - Alan Kay
>
>
>
>
>
>
>

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

* Re: [Qemu-devel] [RFC, PATCH] Add -Wstrict-prototypes, maybe later -Wmissing-prototypes
  2008-08-11 19:22                   ` Anthony Liguori
@ 2008-08-11 20:03                     ` Laurent Vivier
  2008-08-12  1:57                       ` Anthony Liguori
  2008-08-12 12:24                     ` Daniel Jacobowitz
  1 sibling, 1 reply; 23+ messages in thread
From: Laurent Vivier @ 2008-08-11 20:03 UTC (permalink / raw)
  To: Anthony Liguori; +Cc: Blue Swirl, qemu-devel

Le lundi 11 août 2008 à 14:22 -0500, Anthony Liguori a écrit :
> Laurent Vivier wrote:
> >
> >>
> >> I vote for this. The other solutions do not improve type safety
> >> (except for union) and they are more complex.
> >
> > But I think to call the function you have to cast the pointer, this 
> > doesn't improve type safety too...
> 
> Right now, the monitor works by taking a generic function pointer "void 
> (*)()" which is treated specially by the C standard (although it's now 
> deprecated).  Any function pointer can be cast to a generic function 
> pointer.  The dispatch loop basically looks like:
> 
> void dispatch(void (*func)(), int n_args, void *args)
> {
>     switch (n_args) {
>     case 1:
>         ((void (*)(void *))func)(args[0]); break;
>     case 1:
>         ((void (*)(void *, void *))func)(args[0]); break;
>     ...
> }
> 
> But this tosses a warning since void (*)() is deprecated.  So, if we use:
> 
> void dispatch(void *func, int n_args, void *args)
> 
> It'll work just like it did before.
> 

but using "void (*handler)(int argc, char** argv)" avoids the switch:

switch(nb_args) {
    case 0:
        cmd->handler();
        break;
    case 1:
        cmd->handler(args[0]);
        break;
...
}

becomes

cmd->handler(nb_args, args);

Laurent

-- 
----------------- Laurent.Vivier@bull.net  ------------------
  "La perfection est atteinte non quand il ne reste rien à
ajouter mais quand il ne reste rien à enlever." Saint Exupéry

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

* Re: [Qemu-devel] [RFC, PATCH] Add -Wstrict-prototypes, maybe later -Wmissing-prototypes
  2008-08-11 20:03                     ` Laurent Vivier
@ 2008-08-12  1:57                       ` Anthony Liguori
  2008-08-12  8:14                         ` Laurent Vivier
  2008-08-12  9:22                         ` Avi Kivity
  0 siblings, 2 replies; 23+ messages in thread
From: Anthony Liguori @ 2008-08-12  1:57 UTC (permalink / raw)
  To: Laurent Vivier; +Cc: Blue Swirl, qemu-devel

Laurent Vivier wrote:
> Le lundi 11 août 2008 à 14:22 -0500, Anthony Liguori a écrit :
>   
>
> but using "void (*handler)(int argc, char** argv)" avoids the switch:
>
> switch(nb_args) {
>     case 0:
>         cmd->handler();
>         break;
>     case 1:
>         cmd->handler(args[0]);
>         break;
> ...
> }
>
> becomes
>
> cmd->handler(nb_args, args);
>   

And then every monitor command changes from:

void do_eject(int force, char *device)
{
     ...
}

to:

void do_eject(int argc, char **argv)
{
   char *device;
   int force = 0;

   if (argc == 2) {
     if (strcmp(argv[0], "-f") == 0) {
         force = 1;
         device = argv[1];
     } else {
         term_printf("bad option %s\n", argv[0]);
         return;
     }
  } else if (argc == 1) {
      device = argv[0];
  } else {
      term_printf("bad number of options\n");
      return;
  }

  ...
}

Consider multiplying that by all of the possible monitor commands, and 
it's totally not worth it.

Regards,

Anthony Liguori

> Laurent
>
>   

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

* Re: [Qemu-devel] [RFC, PATCH] Add -Wstrict-prototypes, maybe later -Wmissing-prototypes
  2008-08-12  1:57                       ` Anthony Liguori
@ 2008-08-12  8:14                         ` Laurent Vivier
  2008-08-12  9:22                         ` Avi Kivity
  1 sibling, 0 replies; 23+ messages in thread
From: Laurent Vivier @ 2008-08-12  8:14 UTC (permalink / raw)
  To: Anthony Liguori; +Cc: Blue Swirl, qemu-devel

Le lundi 11 août 2008 à 20:57 -0500, Anthony Liguori a écrit :
> Laurent Vivier wrote:
> > Le lundi 11 août 2008 à 14:22 -0500, Anthony Liguori a écrit :
> >   
> >
> > but using "void (*handler)(int argc, char** argv)" avoids the switch:
> >
> > switch(nb_args) {
> >     case 0:
> >         cmd->handler();
> >         break;
> >     case 1:
> >         cmd->handler(args[0]);
> >         break;
> > ...
> > }
> >
> > becomes
> >
> > cmd->handler(nb_args, args);
> >   
> 
> And then every monitor command changes from:
> 
> void do_eject(int force, char *device)
> {
>      ...
> }
> 
> to:
> 
> void do_eject(int argc, char **argv)
> {
>    char *device;
>    int force = 0;
> 
>    if (argc == 2) {
>      if (strcmp(argv[0], "-f") == 0) {
>          force = 1;
>          device = argv[1];
>      } else {
>          term_printf("bad option %s\n", argv[0]);
>          return;
>      }
>   } else if (argc == 1) {
>       device = argv[0];
>   } else {
>       term_printf("bad number of options\n");
>       return;
>   }
> 
>   ...
> }

Yes

> Consider multiplying that by all of the possible monitor commands, and 
> it's totally not worth it.

it's cleaner and more flexible...
and from an artistic point of view, it's beautiful ;-)

Laurent
-- 
----------------- Laurent.Vivier@bull.net  ------------------
  "La perfection est atteinte non quand il ne reste rien à
ajouter mais quand il ne reste rien à enlever." Saint Exupéry

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

* Re: [Qemu-devel] [RFC, PATCH] Add -Wstrict-prototypes, maybe later -Wmissing-prototypes
  2008-08-12  1:57                       ` Anthony Liguori
  2008-08-12  8:14                         ` Laurent Vivier
@ 2008-08-12  9:22                         ` Avi Kivity
  1 sibling, 0 replies; 23+ messages in thread
From: Avi Kivity @ 2008-08-12  9:22 UTC (permalink / raw)
  To: qemu-devel; +Cc: Blue Swirl, Laurent Vivier

Anthony Liguori wrote:
> Laurent Vivier wrote:
>> Le lundi 11 août 2008 à 14:22 -0500, Anthony Liguori a écrit :
>>  
>> but using "void (*handler)(int argc, char** argv)" avoids the switch:
>>
>> switch(nb_args) {
>>     case 0:
>>         cmd->handler();
>>         break;
>>     case 1:
>>         cmd->handler(args[0]);
>>         break;
>> ...
>> }
>>
>> becomes
>>
>> cmd->handler(nb_args, args);
>>   
>
> And then every monitor command changes from:
>
> void do_eject(int force, char *device)
> {
>     ...
> }
>
> to:
>
> void do_eject(int argc, char **argv)
> {
>   char *device;
>   int force = 0;
>
>   if (argc == 2) {
>     if (strcmp(argv[0], "-f") == 0) {
>         force = 1;
>         device = argv[1];
>     } else {
>         term_printf("bad option %s\n", argv[0]);
>         return;
>     }
>  } else if (argc == 1) {
>      device = argv[0];
>  } else {
>      term_printf("bad number of options\n");
>      return;
>  }
>
>  ...
> }
>
> Consider multiplying that by all of the possible monitor commands, and 
> it's totally not worth it.
>


I forgot about non-string args.  So the transformation would be:

void do_eject(void **args)
{
    int *force = *(int *)args[0];
    const char *device = *(const char **)args[1];

   ...
}

But this isn't really an improvement, apart from dropping the ugly switch.

(maybe a union:

typedef union {
    int i;
    const char *s;
    ...
} Arg;

void do_eject(const Args *args)
{
    int force = args++->i;
    const char *device = args++->s;

    ...
}

)

-- 
error compiling committee.c: too many arguments to function

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

* Re: [Qemu-devel] [RFC, PATCH] Add -Wstrict-prototypes, maybe later -Wmissing-prototypes
  2008-08-11 19:22                   ` Anthony Liguori
  2008-08-11 20:03                     ` Laurent Vivier
@ 2008-08-12 12:24                     ` Daniel Jacobowitz
  1 sibling, 0 replies; 23+ messages in thread
From: Daniel Jacobowitz @ 2008-08-12 12:24 UTC (permalink / raw)
  To: qemu-devel

On Mon, Aug 11, 2008 at 02:22:16PM -0500, Anthony Liguori wrote:
> But this tosses a warning since void (*)() is deprecated.  So, if we use:
>
> void dispatch(void *func, int n_args, void *args)
>
> It'll work just like it did before.

Just for the record: while you're correct this will work on mainstream
systems (you've been saying modern, which is not accurate), it is not
valid C.  The C standard only permits conversions between pointers to
different object types, or between pointers to different function
types.  Pointers can also be converted to integers but "the result
need not be in the range of values of any integer type".

GCC will even warn about it:
  ISO C forbids conversion of object pointer to function pointer type

-- 
Daniel Jacobowitz
CodeSourcery

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

end of thread, other threads:[~2008-08-12 12:24 UTC | newest]

Thread overview: 23+ messages (download: mbox.gz follow: Atom feed
-- links below jump to the message on this page --
2008-08-08 17:22 [Qemu-devel] [RFC, PATCH] Add -Wstrict-prototypes, maybe later -Wmissing-prototypes Blue Swirl
2008-08-09 18:24 ` Anthony Liguori
2008-08-10 18:33   ` Blue Swirl
2008-08-11  7:52     ` Gerd Hoffmann
2008-08-11  9:54       ` Samuel Thibault
2008-08-11 13:32         ` M. Warner Losh
2008-08-11 13:30       ` M. Warner Losh
2008-08-11 14:21     ` Anthony Liguori
2008-08-11 14:48       ` Avi Kivity
2008-08-11 14:53         ` Paul Brook
2008-08-11 14:56         ` Laurent Vivier
2008-08-11 14:56         ` Anthony Liguori
2008-08-11 16:38           ` Avi Kivity
2008-08-11 16:47             ` Anthony Liguori
2008-08-11 18:52               ` Blue Swirl
2008-08-11 18:57                 ` Laurent Vivier
2008-08-11 19:11                   ` Blue Swirl
2008-08-11 19:22                   ` Anthony Liguori
2008-08-11 20:03                     ` Laurent Vivier
2008-08-12  1:57                       ` Anthony Liguori
2008-08-12  8:14                         ` Laurent Vivier
2008-08-12  9:22                         ` Avi Kivity
2008-08-12 12:24                     ` Daniel Jacobowitz

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