public inbox for gcc-help@gcc.gnu.org
 help / color / mirror / Atom feed
* Use -Wformat for own functions?
@ 2003-11-12 11:02 Bo Do
  2003-11-12 11:09 ` Mihnea Balta
  2003-11-12 11:10 ` Falk Hueffner
  0 siblings, 2 replies; 4+ messages in thread
From: Bo Do @ 2003-11-12 11:02 UTC (permalink / raw)
  To: gcc-help

Hello all!

I've written my own version of sprintf:
my_sprintf(MyBuffer, const char *format, ...)
I wonder if there's a way to use -Wformat, or any functionality of the compiler, to make sure that the number of arguments supplied (the ... ) are correct and have types appropriate to the format string specified.
-Wformat has the functionality I want, but it only checks the printf and scanf functions in stdio.

Thanks for any hints or help.
/Bo

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

* Re: Use -Wformat for own functions?
  2003-11-12 11:02 Use -Wformat for own functions? Bo Do
@ 2003-11-12 11:09 ` Mihnea Balta
  2003-11-12 11:10 ` Falk Hueffner
  1 sibling, 0 replies; 4+ messages in thread
From: Mihnea Balta @ 2003-11-12 11:09 UTC (permalink / raw)
  To: Bo Do, gcc-help

void l_log(const char* fmt, ...) __attribute__ ((format (printf, 1, 2)));

The first argument of __attribute__(format) is the kind of format string to 
expect (printf, scanf, strftime). The second is the index of the format 
string argument. The third is the index of the first variable argument. In 
your case:

int my_sprintf(MyBuffer, const char *format, ...) __attribute__ ((format 
(printf, 2, 3)));

If the function is a member of a class, the indices must be shifted by 1, as 
the compiler passes "this" as the first function argument. For example:

class something{
public:
	int my_sprintf(MyBuffer, const char *format, ...) __attribute__ ((format 
(printf, 3, 4)));
};

Have fun.

On Wednesday 12 November 2003 13:10, Bo Do wrote:
> Hello all!
>
> I've written my own version of sprintf:
> my_sprintf(MyBuffer, const char *format, ...)
> I wonder if there's a way to use -Wformat, or any functionality of the
> compiler, to make sure that the number of arguments supplied (the ... ) are
> correct and have types appropriate to the format string specified. -Wformat
> has the functionality I want, but it only checks the printf and scanf
> functions in stdio.
>
> Thanks for any hints or help.
> /Bo
>
>
> ---------------------------------------------------------------
> Acasa.ro vine cu albumele, tu vino doar cu pozele ;)
> http://poze.acasa.ro/

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

* Re: Use -Wformat for own functions?
  2003-11-12 11:02 Use -Wformat for own functions? Bo Do
  2003-11-12 11:09 ` Mihnea Balta
@ 2003-11-12 11:10 ` Falk Hueffner
  2003-11-12 11:36   ` Bo Do
  1 sibling, 1 reply; 4+ messages in thread
From: Falk Hueffner @ 2003-11-12 11:10 UTC (permalink / raw)
  To: Bo Do; +Cc: gcc-help

"Bo Do" <bodo@flygp.se> writes:

> I've written my own version of sprintf:
> my_sprintf(MyBuffer, const char *format, ...)
> I wonder if there's a way to use -Wformat, or any functionality of
> the compiler, to make sure that the number of arguments supplied
> (the ... ) are correct and have types appropriate to the format
> string specified.

Yes. Check __attribute__((printf)).

-- 
	Falk

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

* Re: Use -Wformat for own functions?
  2003-11-12 11:10 ` Falk Hueffner
@ 2003-11-12 11:36   ` Bo Do
  0 siblings, 0 replies; 4+ messages in thread
From: Bo Do @ 2003-11-12 11:36 UTC (permalink / raw)
  To: gcc-help

Thanks a lot Falk and Mihnea! =)

Would never have figured it out myself!
I'm currently using this following version of gcc:
gcc -v
Reading specs from /usr/lib/gcc-lib/i386-linux/2.95.4/specs
gcc version 2.95.4 20011002 (Debian prerelease)

It should work right? (rhetorical question, dont mind it! ;)

Thanks again!
/Bo

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

end of thread, other threads:[~2003-11-12 11:36 UTC | newest]

Thread overview: 4+ messages (download: mbox.gz / follow: Atom feed)
-- links below jump to the message on this page --
2003-11-12 11:02 Use -Wformat for own functions? Bo Do
2003-11-12 11:09 ` Mihnea Balta
2003-11-12 11:10 ` Falk Hueffner
2003-11-12 11:36   ` Bo Do

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 read-only IMAP folder(s) and NNTP newsgroup(s).