[splint-discuss] Variable argument type checking

Richard O'Keefe ok at cs.otago.ac.nz
Tue Mar 17 15:05:08 PST 2009


On 18 Mar 2009, at 3:43 am, <jhanley at dgtlrift.com> wrote:

> Quoting "Hollas Boris (CR/AEY1)" <Boris.Hollas at de.bosch.com>:
>
>> Have you tried using an enum instead of int? E.g.
>>
>> enum legal_values {0,1,2,3,4,5};
>>
>>
>
>>> int do_something( int action, ... /* args */ );
>>> /* legal ways to call do_something:
>>>    (void)do_something( 0 );
>>>    (void)do_something( 1, const char * );
>>>    (void)do_something( 2, const this_struct_type * );
>>>    (void)do_something( 3, const that_struct_type * );
>>>    (void)do_something( 4 );
>>>    (void)do_something( 5, const this_struct_type *, const >
>> that_struct_type * );
>

I don't know the context in which this is used.
Clearly, however, there are going to be very few
occasions where the caller does not know what to pass.

Here's my suggestion.

#ifdef S_SPLINT_S

/*0*/ int foo(void);
/*1*/ int bar(char const *);
/*2*/ int ugh(this_struct_type const *);
/*3*/ int ick(that_struct_type const *);
/*4*/ int ack(void);
/*5*/ int zoo(this_struct_type const *, that_struct_type const *);

#else

#define foo()    do_something(0)
#define bar(x)   do_something(1, (x))
#define ugh(x)   do_something(2, (x))
#define ick(y)   do_something(3, (y))
#define ack()    do_something(4)
#define zoo(x,y) do_something(5, (x), (y))

#endif

Replace calls to do_something by calls to the macros.
Put the Splint annotations on the prototypes.

Now you don't *need* Splint to find other calls to
do_something(), a simple 'grep' will do.




More information about the splint-discuss mailing list