Velocity Reviews - Computer Hardware Reviews

Velocity Reviews > Newsgroups > Programming > C Programming > va_list: how to produce warnings at compile time if calledincorrectly?

Reply
Thread Tools

va_list: how to produce warnings at compile time if calledincorrectly?

 
 
John Devereux
Guest
Posts: n/a
 
      05-16-2006
Eric Sosman <(E-Mail Removed)> writes:

> Felix Kater wrote On 05/16/06 11:11,:
>> On Tue, 16 May 2006 10:53:08 -0400
>> Eric Sosman <(E-Mail Removed)> wrote:
>>
>>
>>> The language itself has no way to restrict the "..."
>>>arguments.

>>
>>
>> I wonder how printf does that? If I do
>>
>> long my_long=0;
>> printf("%d",my_long);
>>
>> I get a warning from gcc that an int was expected.
>> So, shouldn't it be possible somehow?

>
> That's one of those "extra-linguistic features" I
> mentioned. gcc has a gcc-specific way of checking for
> disagreement between printf-like and scanf-like format
> strings and the arguments provided to match them. It
> may be possible to get gcc to make other kinds of checks
> as well; consult the gcc documentation.


I don't think it is gcc-specific, in that any compiler could in
principle do this for printf (and sprintf, scanf etc) because the
format string meaning is defined by the C standard. Same thing with
replacing printf() calls with puts(), where these would be equivalent
(which gcc also does). gcc is allowed to assume that the standard
library functions behave according to the standard, and it does this
in several areas.

But for user-written functions it has no way of knowing, within
standard C, what the format string is supposed to mean.

> But all these things are specific to gcc; the C
> language itself has no way of expressing any restrictions
> on the "..." arguments. Since the restrictions cannot
> even be expressed, they cannot be enforced -- without,
> as I said, going outside the language and dealing directly
> with the compiler, whether it's gcc or something else.


--

John Devereux
 
Reply With Quote
 
 
 
 
Eric Sosman
Guest
Posts: n/a
 
      05-16-2006


John Devereux wrote On 05/16/06 11:53,:
> Eric Sosman <(E-Mail Removed)> writes:
>
>
>>Felix Kater wrote On 05/16/06 11:11,:
>>
>>>On Tue, 16 May 2006 10:53:08 -0400
>>>Eric Sosman <(E-Mail Removed)> wrote:
>>>
>>>
>>>
>>>> The language itself has no way to restrict the "..."
>>>>arguments.
>>>
>>>
>>>I wonder how printf does that? If I do
>>>
>>>long my_long=0;
>>>printf("%d",my_long);
>>>
>>>I get a warning from gcc that an int was expected.
>>>So, shouldn't it be possible somehow?

>>
>> That's one of those "extra-linguistic features" I
>>mentioned. gcc has a gcc-specific way of checking for
>>disagreement between printf-like and scanf-like format
>>strings and the arguments provided to match them. It
>>may be possible to get gcc to make other kinds of checks
>>as well; consult the gcc documentation.

>
>
> I don't think it is gcc-specific, in that any compiler could in
> principle do this for printf (and sprintf, scanf etc) because the
> format string meaning is defined by the C standard. Same thing with
> replacing printf() calls with puts(), where these would be equivalent
> (which gcc also does). gcc is allowed to assume that the standard
> library functions behave according to the standard, and it does this
> in several areas.


Thanks for the correction. I ought to have said that
the mechanism gcc actually uses for these checks is a gcc-
specific mechanism, not that the checking itself has to be
gcc-specific.

> But for user-written functions it has no way of knowing, within
> standard C, what the format string is supposed to mean.


As it happens, gcc's mechanism can be used to tag user-
written functions, too. If you want to write a logging
function that takes a printf-like format string and matching
arguments, you can get gcc to check it for you. I haven't
studied the gcc annotations enough to tell whether they're
easily extensible to the kinds of checks the O.P. wants, though.

--
http://www.velocityreviews.com/forums/(E-Mail Removed)

 
Reply With Quote
 
 
 
 
Felix Kater
Guest
Posts: n/a
 
      05-16-2006
On Tue, 16 May 2006 11:41:48 -0400
Eric Sosman <(E-Mail Removed)> wrote:

> It seems to me it would be less burdensome to put the
> initialization checks into f1(),f2(),f3() than to require
> the user to write f(please_call_f1_for_me, f1_arguments).


Hm. Initialization checks were just an example. I have to keep an eye
on much more like module dependencies, its actual runlevels an so on.

So, let me get it this way then: It's not only about keeping the
interface (more or less) easy to use for the client programmer. But for
larger projects it's not less important to keep the code simple for
changes an extensions to the developers of the project. In our case it
got hard to change anything since the developers have to think of too
many things before being able to change anything. A good redesign is my
motivation--letting the user do some more overhead is ok if this makes
it possible for the developers to keep on coding.

Of course, I don't want to make things more complicated than necessary.

I was thinking of doing things like this

general_f(create_new_struct_of_same_type_f1( int i, long l))
general_f(create_new_struct_of_same_type_f2( void*, char*))

But to have this convenient to the client, general_f should detroy
the structs internally--but what with return values, then? ...I am
still not convinced of it all.

Felix
 
Reply With Quote
 
Luca Benini
Guest
Posts: n/a
 
      05-17-2006
To use printf cech format ( gcc only ) you must use
__attribute__ format
you can find more details here:
http://www.unixwiz.net/techtips/gnu-...es.html#format
or in gcc manual.

 
Reply With Quote
 
 
 
Reply

Thread Tools

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are Off


Similar Threads
Thread Thread Starter Forum Replies Last Post
Re: How include a large array? Edward A. Falk C Programming 1 04-04-2013 08:07 PM
computation at compile time i.e. compile time functions usingtemplates Carter C++ 2 03-04-2009 06:43 PM
Is ther a master list of all warnings/errors that the java compilercan produce. Wazza Java 4 12-06-2007 09:31 AM
can java produce .exe? if it can produce jar,how do you do? aungkopyay@gmail.com Java 5 10-27-2006 02:07 AM
use warnings; and use Warnings; give different results Ted Sung Perl Misc 1 08-30-2004 10:22 PM



Advertisments