Add -Wstrict-prototypes, maybe later -Wmissing-prototypes
RFC PATCH 1/1 Add -Wstrict-prototypes, maybe later -Wmissing-prototypes
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?
Makefile.target +1 -1
@@ -73,7 +73,7 @@ PROGS=$(QEMU_PROG) # We require -O2 to avoid the stack setup prologue in EXIT_TB -OP_CFLAGS := -Wall -O2 -g -fno-strict-aliasing +OP_CFLAGS := -Wall -Wstrict-prototypes -O2 -g -fno-strict-aliasing # cc-option # Usage: OP_CFLAGS+=$(call cc-option, -falign-functions=0, -malign-functions=0)
configure +1 -1
@@ -347,7 +347,7 @@ fi # default flags for all hosts -CFLAGS="$CFLAGS -Wall -O2 -g -fno-strict-aliasing" +CFLAGS="$CFLAGS -Wall -Wstrict-prototypes -O2 -g -fno-strict-aliasing" LDFLAGS="$LDFLAGS -g" if test "$werror" = "yes" ; then CFLAGS="$CFLAGS -Werror"
disas.c +2 -5
@@ -341,11 +341,8 @@static CPUState *monitor_disas_env;static int -monitor_read_memory (memaddr, myaddr, length, info) -bfd_vma memaddr; -bfd_byte *myaddr; -int length; -struct disassemble_info *info; +monitor_read_memory (bfd_vma memaddr,bfd_byte *myaddr,int length, +struct disassemble_info *info) {if (monitor_disas_is_physical) {cpu_physical_memory_rw (memaddr, myaddr, length,0 );
hw/mips_malta.c +1 -1
@@ -155,7 +155,7 @@ }, }; -static uint8_t eeprom24c0x_read () +static uint8_t eeprom24c0x_read (void ) {logout ("%u: scl = %u, sda = %u, data = 0x%02x\n" , eeprom.tick, eeprom.scl, eeprom.sda, eeprom.data);
hw/shix.c +3 -3
@@ -45,17 +45,17 @@/* XXXXX */ } -void pic_info () +void pic_info (void ) {/* XXXXX */ } -void vga_update_display () +void vga_update_display (void ) {/* XXXXX */ } -void vga_invalidate_display () +void vga_invalidate_display (void ) {/* XXXXX */ }
mips-dis.c +1 -3
@@ -3322,9 +3322,7 @@ }void -parse_mips_dis_option (option, len) -const char *option; -unsigned int len; +parse_mips_dis_option (const char *option,unsigned int len) {unsigned int i, optionlen, vallen;const char *val;
target-alpha/translate.c +1 -1
@@ -47,7 +47,7 @@#include "gen-icount.h" -static void alpha_translate_init () +static void alpha_translate_init (void ) {static int done_init =0 ;if (done_init)
target-sh4/translate.c +1 -1
@@ -60,7 +60,7 @@#include "gen-icount.h" -static void sh4_translate_init () +static void sh4_translate_init (void ) {static int done_init =0 ;if (done_init)
Makefile.target +1 -1
@@ -73,7 +73,7 @@ PROGS=$(QEMU_PROG) # We require -O2 to avoid the stack setup prologue in EXIT_TB -OP_CFLAGS := -Wall -Wstrict-prototypes -O2 -g -fno-strict-aliasing +OP_CFLAGS := -Wall -Wstrict-prototypes -Wmissing-prototypes -O2 -g -fno-strict-aliasing # cc-option # Usage: OP_CFLAGS+=$(call cc-option, -falign-functions=0, -malign-functions=0)
configure +1 -1
@@ -347,7 +347,7 @@ fi # default flags for all hosts -CFLAGS="$CFLAGS -Wall -Wstrict-prototypes -O2 -g -fno-strict-aliasing" +CFLAGS="$CFLAGS -Wall -Wstrict-prototypes -Wmissing-prototypes -O2 -g -fno-strict-aliasing" LDFLAGS="$LDFLAGS -g" if test "$werror" = "yes" ; then CFLAGS="$CFLAGS -Werror"
dyngen.c +25 -25
178 lines not on this page · raw message
exec.c +1 -1
@@ -386,7 +386,7 @@static uint8_t static_code_gen_buffer[DEFAULT_CODE_GEN_BUFFER_SIZE ];#endif -void code_gen_alloc (unsigned long tb_size) +static void code_gen_alloc (unsigned long tb_size) {#ifdef USE_STATIC_CODE_GEN_BUFFER code_gen_buffer = static_code_gen_buffer;
hw/twl92230.c +1 -1
@@ -129,7 +129,7 @@menelaus_update (s); } -void menelaus_reset (i2c_slave *i2c) +static void menelaus_reset (i2c_slave *i2c) {struct menelaus_s *s = (struct menelaus_s *) i2c; s->reg =0x00 ;
hw/usb-serial.c +3 -3
@@ -447,13 +447,13 @@qemu_free (s); } -int usb_serial_can_read (void *opaque) +static int usb_serial_can_read (void *opaque) {USBSerialState *s = opaque;return RECV_BUF - s->recv_used; } -void usb_serial_read (void *opaque,const uint8_t *buf,int size) +static void usb_serial_read (void *opaque,const uint8_t *buf,int size) {USBSerialState *s = opaque;int first_size =RECV_BUF - s->recv_ptr;
@@ -465,7 +465,7 @@ s->recv_used += size; } -void usb_serial_event (void *opaque,int event) +static void usb_serial_event (void *opaque,int event) {USBSerialState *s = opaque;
slirp/slirp.c +1 -1
@@ -554,7 +554,7 @@unsigned char ar_tip[4 ];/* target IP address */ }; -void arp_input (const uint8_t *pkt,int pkt_len) +static void arp_input (const uint8_t *pkt,int pkt_len) {struct ethhdr *eh = (struct ethhdr *)pkt;struct arphdr *ah = (struct arphdr *)(pkt +ETH_HLEN );
vl.c +1 -1
@@ -2558,7 +2558,7 @@ } } -void pty_chr_timer (void *opaque) +static void pty_chr_timer (void *opaque) {struct CharDriverState *chr = opaque;PtyCharDriver *s = chr->opaque;
linux-user/main.c +3 -1
@@ -107,10 +107,12 @@return 0 ; } +#if defined(TARGET_I386 )int cpu_get_pic_interrupt (CPUState *env) {return -1 ; } +#endif /* timers for rdtsc */
@@ -2151,7 +2153,7 @@ }#endif /* TARGET_ALPHA */ -void usage (void ) +static void usage (void ) {printf ("qemu-" TARGET_ARCH " version " QEMU_VERSION ", Copyright (c) 2003-2008 Fabrice Bellard\n" "usage: qemu-" TARGET_ARCH " [options] program [arguments...]\n"
linux-user/signal.c +3 -2
@@ -154,7 +154,8 @@ d->sig[i] =tswapl (d1.sig[i]); } -void target_to_host_sigset_internal (sigset_t *d,const target_sigset_t *s) +static void target_to_host_sigset_internal (sigset_t *d, +const target_sigset_t *s) {int i;sigemptyset (d);
@@ -324,7 +325,7 @@ }/* abort execution with signal */ -void __attribute((noreturn))force_sig (int sig) +static void __attribute((noreturn))force_sig (int sig) {int host_sig; host_sig =target_to_host_signal (sig);
linux-user/syscall.c +19 -15
@@ -105,38 +105,38 @@#undef _syscall6#define _syscall0 (type,name) \ -type name (void) \ +static type name (void) \ { \ return syscall(__NR_##name); \ }#define _syscall1 (type,name,type1,arg1) \ -type name (type1 arg1) \ +static type name (type1 arg1) \ { \ return syscall(__NR_##name, arg1); \ }#define _syscall2 (type,name,type1,arg1,type2,arg2) \ -type name (type1 arg1,type2 arg2) \ +static type name (type1 arg1,type2 arg2) \ { \ return syscall(__NR_##name, arg1, arg2); \ }#define _syscall3 (type,name,type1,arg1,type2,arg2,type3,arg3) \ -type name (type1 arg1,type2 arg2,type3 arg3) \ +static type name (type1 arg1,type2 arg2,type3 arg3) \ { \ return syscall(__NR_##name, arg1, arg2, arg3); \ }#define _syscall4 (type,name,type1,arg1,type2,arg2,type3,arg3,type4,arg4) \ -type name (type1 arg1,type2 arg2,type3 arg3,type4 arg4) \ +static type name (type1 arg1,type2 arg2,type3 arg3,type4 arg4) \ { \ return syscall(__NR_##name, arg1, arg2, arg3, arg4); \ }#define _syscall5 (type,name,type1,arg1,type2,arg2,type3,arg3,type4,arg4, \ type5,arg5) \ -type name (type1 arg1,type2 arg2,type3 arg3,type4 arg4,type5 arg5) \ +static type name (type1 arg1,type2 arg2,type3 arg3,type4 arg4,type5 arg5) \ { \ return syscall(__NR_##name, arg1, arg2, arg3, arg4, arg5); \ }
@@ -144,7 +144,8 @@#define _syscall6 (type,name,type1,arg1,type2,arg2,type3,arg3,type4,arg4, \ type5,arg5,type6,arg6) \ -type name (type1 arg1,type2 arg2,type3 arg3,type4 arg4,type5 arg5,type6 arg6) \ +static type name (type1 arg1,type2 arg2,type3 arg3,type4 arg4,type5 arg5, \ + type6 arg6) \ { \ return syscall(__NR_##name, arg1, arg2, arg3, arg4, arg5, arg6); \ }
@@ -204,8 +205,10 @@ _syscall3(int, sys_getdents64, uint, fd, struct dirent64 *, dirp, uint, count);#endif _syscall2 (int, sys_getpriority, int, which, int, who); +#if !defined (__x86_64__)_syscall5 (int, _llseek, uint, fd, ulong, hi, ulong, lo, loff_t *, res, uint, wh); +#endif #if defined(TARGET_NR_linkat) && defined(__NR_linkat) _syscall5(int,sys_linkat,int,olddirfd,const char *,oldpath, int,newdirfd,const char *,newpath,int,flags)
@@ -253,10 +256,11 @@ _syscall4(int,sys_utimensat,int,dirfd,const char *,pathname, const struct timespec *,tsp,int,flags)#endif +#if defined(USE_NPTL )#if defined(TARGET_NR_futex) && defined(__NR_futex) _syscall6(int,sys_futex,int *,uaddr,int,op,int,val, const struct timespec *,timeout,int *,uaddr2,int,val3) - +#endif #endif extern int personality (int );
@@ -2522,8 +2526,8 @@ }/* specific and weird i386 syscalls */ -abi_long do_modify_ldt (CPUX86State *env,int func,abi_ulong ptr, -unsigned long bytecount) +static abi_long do_modify_ldt (CPUX86State *env,int func,abi_ulong ptr, +unsigned long bytecount) {abi_long ret;
@@ -2544,7 +2548,7 @@return ret; } -abi_long do_set_thread_area (CPUX86State *env,abi_ulong ptr) +static abi_long do_set_thread_area (CPUX86State *env,abi_ulong ptr) {uint64_t *gdt_table =g2h (env->gdt.base);struct target_modify_ldt_ldt_s ldt_info;
@@ -2629,7 +2633,7 @@return 0 ; } -abi_long do_get_thread_area (CPUX86State *env,abi_ulong ptr) +static abi_long do_get_thread_area (CPUX86State *env,abi_ulong ptr) {struct target_modify_ldt_ldt_s *target_ldt_info;uint64_t *gdt_table =g2h (env->gdt.base);
@@ -2677,7 +2681,7 @@ }#ifndef TARGET_ABI32 -abi_long do_arch_prctl (CPUX86State *env,int code,abi_ulong addr) +static abi_long do_arch_prctl (CPUX86State *env,int code,abi_ulong addr) {abi_long ret;abi_ulong val;
@@ -3150,8 +3154,8 @@futexes locally would make futexes shared between multiple processes tricky. However they're probably useless because guest atomic operations won't work either. */ -int do_futex (target_ulong uaddr,int op,int val,target_ulong timeout, -target_ulong uaddr2,int val3) +static int do_futex (target_ulong uaddr,int op,int val,target_ulong timeout, +target_ulong uaddr2,int val3) {struct timespec ts, *pts;
tcg/tcg.c +2 -15
@@ -196,19 +196,6 @@ s->pool_current =NULL ; } -/* free all the pool */ -void tcg_pool_free (TCGContext *s) -{ -TCGPool *p, *p1; - -for (p = s->pool_first; p !=NULL ; p = p1) { - p1 = p->next; -qemu_free (p); - } - s->pool_first =NULL ; - s->pool_cur = s->pool_end =NULL ; -} -void tcg_context_init (TCGContext *s) {int op, total_args, n;
@@ -655,7 +642,7 @@ }#endif -void tcg_reg_alloc_start (TCGContext *s) +static void tcg_reg_alloc_start (TCGContext *s) {int i;TCGTemp *ts;
@@ -1025,7 +1012,7 @@/* Liveness analysis : update the opc_dead_iargs array to tell if a given input arguments is dead. Instructions updating dead temporaries are removed. */ -void tcg_liveness_analysis (TCGContext *s) +static void tcg_liveness_analysis (TCGContext *s) {int i, op_index, op, nb_args, nb_iargs, nb_oargs, arg, nb_ops;TCGArg *args;
tcg/x86_64/tcg-target.c +2 -2
@@ -108,7 +108,7 @@ }/* parse target specific constraints */ -int target_parse_constraint (TCGArgConstraint *ct,const char **pct_str) +static int target_parse_constraint (TCGArgConstraint *ct,const char **pct_str) {const char *ct_str;
@@ -404,7 +404,7 @@ } } -void tcg_out_addi (TCGContext *s,int reg,tcg_target_long val) +static void tcg_out_addi (TCGContext *s,int reg,tcg_target_long val) {if (val !=0 )tgen_arithi64 (s,ARITH_ADD , reg, val);
Quoted 10 lines · Blue Swirl
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
Quoted 7 lines
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?
Quoted 9 lines · Anthony Liguori
Quoted 7 lines · Blue Swirl
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.
Quoted 7 lines
Quoted 4 lines
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.
Quoted 18 lines · Blue Swirl
Quoted 2 lines · Anthony Liguori
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 voiddo_help(const char *name)
static voiddo_quit(void)
[ ... ]
Quoted 3 lines
I don't have a good plan how to fix this, proposals are welcome. Changing all handlers to use
va_argsto 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
Signature · 1 line
http://kraxel.fedorapeople.org/xenner/
Quoted 5 lines · Gerd Hoffmann, le Mon 11 Aug 2008 09:52:03 +0200, a écrit :
Quoted 5 lines
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 voiddo_help(const char *name)
static voiddo_quit(void)
Quoted 7 lines
[ ... ]
Quoted 3 lines
I don't have a good plan how to fix this, proposals are welcome. Changing all handlers to use
va_argsto just silence a gcc warning sounds like overkill.Using a union maybe?
That should be the right thing yes.
Samuel
In message: <[email protected]>
Samuel Thibault <[email protected]> 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
In message: <[email protected]>
Gerd Hoffmann <[email protected]> writes:
: Blue Swirl wrote:
: > On 8/9/08, Anthony Liguori <[email protected]> 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
Quoted 30 lines · Blue Swirl
Quoted 10 lines · Anthony Liguori
Quoted 7 lines · Blue Swirl
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.
Quoted 8 lines
Quoted 5 lines
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;
Quoted 4 lines
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
Quoted 32 lines · Anthony Liguori
Quoted 31 lines · Blue Swirl
Quoted 10 lines · Anthony Liguori
Quoted 7 lines · Blue Swirl
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.
Quoted 9 lines
Quoted 5 lines
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;
Quoted 6 lines
Quoted 4 lines
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.
Signature · 1 line
error compiling committee.c: too many arguments to function
Quoted 3 lines
Quoted 1 line
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.
Quoted 1 line
void (*handler)(const char **args)
This seems like a reasonable idea. None of this code is performance critical.
Paul
Quoted 40 lines · Avi Kivity
Quoted 32 lines · Anthony Liguori
Quoted 31 lines · Blue Swirl
Quoted 10 lines · Anthony Liguori
Quoted 7 lines · Blue Swirl
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.
Quoted 9 lines
Quoted 5 lines
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;Quoted 6 lines
Quoted 4 lines
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.
Quoted 7 lines
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
Signature · 3 lines
----------------- [email protected] ------------------ "La perfection est atteinte non quand il ne reste rien à ajouter mais quand il ne reste rien à enlever." Saint Exupéry
Quoted 33 lines · Avi Kivity
Quoted 32 lines · Anthony Liguori
Quoted 31 lines · Blue Swirl
Quoted 10 lines · Anthony Liguori
Quoted 7 lines · Blue Swirl
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.
Quoted 9 lines
Quoted 5 lines
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;
Quoted 8 lines
Quoted 6 lines
Quoted 4 lines
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.
Quoted 5 lines
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
Quoted 7 lines · Anthony Liguori
Quoted 4 lines
Quoted 2 lines
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.
Quoted 7 lines
Quoted 5 lines
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.
Signature · 1 line
error compiling committee.c: too many arguments to function
Quoted 2 lines · Avi Kivity
You lose the
switch(nb_args)inmonitor_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
Quoted 8 lines · Anthony Liguori
Quoted 2 lines · Avi Kivity
You lose the
switch(nb_args)inmonitor_handle_command()so it comes outeven. You don't need to cast anything -- though you do need to change all the callbacks to accept an argument array.
Quoted 1 line
Quoted 6 lines
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.
Quoted 13 lines · Blue Swirl
Quoted 12 lines · Anthony Liguori
Quoted 2 lines · Avi Kivity
You lose the
switch(nb_args)inmonitor_handle_command()so it comes outeven. You don't need to cast anything -- though you do need to change all the callbacks to accept an argument array.
Quoted 1 line
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.
Quoted 5 lines
Quoted 2 lines
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
Quoted 30 lines · Laurent Vivier
Quoted 7 lines · Blue Swirl
Quoted 5 lines · Anthony Liguori
Quoted 2 lines · Avi Kivity
You lose the
switch(nb_args)inmonitor_handle_command()so it comesout
Quoted 2 lines
Quoted 2 lines
Quoted 1 line
even. You don't need to cast anything -- though you do need to change
all
Quoted 10 lines
Quoted 10 lines
the callbacks to accept an argument array.
Quoted 2 lines
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
Quoted 3 lines
Quoted 2 lines
like a winner to me :-)
Quoted 6 lines
Quoted 3 lines
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.
Quoted 6 lines · Laurent Vivier
Quoted 4 lines
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
Quoted 13 lines
Regards,
Laurent
----------------------- Laurent Vivier ---------------------- "The best way to predict the future is to invent it."
- Alan Kay
Quoted 24 lines · Anthony Liguori
Quoted 7 lines · Laurent Vivier
Quoted 4 lines
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; ... }
Quoted 6 lines
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
Signature · 3 lines
----------------- [email protected] ------------------ "La perfection est atteinte non quand il ne reste rien à ajouter mais quand il ne reste rien à enlever." Saint Exupéry
Quoted 12 lines · Laurent Vivier
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;
Quoted 7 lines
...
}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
Quoted 3 lines
Laurent
Quoted 48 lines · Anthony Liguori
Quoted 12 lines · Laurent Vivier
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;Quoted 7 lines
...
}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;
Quoted 4 lines
}
...
}
Yes
Quoted 2 lines
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
Signature · 3 lines
----------------- [email protected] ------------------ "La perfection est atteinte non quand il ne reste rien à ajouter mais quand il ne reste rien à enlever." Saint Exupéry
Quoted 47 lines · Anthony Liguori
Quoted 11 lines · Laurent Vivier
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;Quoted 7 lines
...
}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;
Quoted 7 lines
}
...
}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;
...
}
)
Signature · 1 line
error compiling committee.c: too many arguments to function
Quoted 5 lines · Anthony Liguori
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
Signature · 2 lines
Daniel Jacobowitz CodeSourcery