www.delorie.com/archives/browse.cgi   search  
Mail Archives: djgpp/2000/09/02/17:00:04

From: "Tim 'Zastai' Van Holder" <zastai AT hotmail DOT com>
Newsgroups: comp.os.msdos.djgpp
References: <200009011041 DOT MAA65728 AT login-2 DOT eunet DOT no>
Subject: Re: __attribute__ for pointers
Lines: 89
X-Priority: 3
X-MSMail-Priority: Normal
X-Newsreader: Microsoft Outlook Express 5.50.4133.2400
X-MimeOLE: Produced By Microsoft MimeOLE V5.50.4133.2400
Message-ID: <JSds5.77334$DJ4.845704@afrodite.telenet-ops.be>
Date: Sat, 02 Sep 2000 20:59:21 GMT
NNTP-Posting-Host: 213.224.92.103
X-Trace: afrodite.telenet-ops.be 967928361 213.224.92.103 (Sat, 02 Sep 2000 22:59:21 MET DST)
NNTP-Posting-Date: Sat, 02 Sep 2000 22:59:21 MET DST
Organization: Pandora - Met vlotte tred op Internet
Path: news.mv.net!newspeer.phoen-x.net!diablo.netcom.net.uk!netcom.net.uk!newsfeeds.belnet.be!news.belnet.be!afrodite.telenet-ops.be!not-for-mail
Xref: news.mv.net comp.os.msdos.djgpp:103111

"Gisle Vanem" <gvanem AT eunet DOT no> wrote in message
news:200009011041 DOT MAA65728 AT login-2 DOT eunet DOT no...
> From: "Eli Zaretskii" <eliz AT is DOT elta DOT co DOT il> said:
>
> > > Date: Fri, 1 Sep 2000 08:00:44 +0200 (MET DST)
> > > From: Gisle Vanem <giva AT bgnett DOT no>
> > >
> > > extern int (*__printf) (const char *fmt, ...)
> > > #ifdef __GNUC__
> > >   __attribute__((format(printf,1,2)))
> > > #endif
> > > ;
> > >
> > > Where '__printf' points to either a stdio or a conio-type function.
> > >
> > > But gcc 2.95.2 doesn't seem to allow such '__attribute__' for a
pointer;
> > >
> > >  "argument format specified for non-function `__printf'"
> >
> > Did you try to put __attribute__ inside the parens in (*__printf)?
>
> Tried it, but doesn't work;
>
>   extern int (*__printf __attribute__((format(printf,1,2))))
>               (const char *fmt, ...);
> or
>   extern int (*__printf) __attribute__((format(printf,1,2)))
>               (const char *fmt, ...);
>
>
> > If that doesn't help either, try using a typedef.
>
> Did work either:
>
>   typedef int (*PrintFunc) (const char *fmt, ...);
>
>   extern PrintFunc __printf __attribute__((format(printf,1,2)));
>
> or
>   typedef int (*PrintFunc) (const char *fmt, ...)
>     __attribute__((format(printf,1,2)));
>
>   extern PrintFunc __printf;

I replied to your original post, but it seems that posts I make from work
rarely make it onto the NG (looks like I'll have to use Deja there then).

Basically you're indeed out of luck - the format attribute is only for
functions, not for variables (like your __printf) or types.

Because of the ..., you can't use a wrapper function to work around your
problem, but changing the systems slightly may help (of course, this may
not be possible, depending on where the possible values of __printf
come from).

First off, use this:
extern int (*__printf_func) (const char *fmt, va_list args);

Then, the following function can provide a format-checked wrapper:
extern int __printf (const char *fmt, ...)
#ifdef __GNUC__
   __attribute__((format(printf,1,2)))
#endif
;

This function is fairly straightforward:
int
__printf (const char *fmt, ...)
{
va_list args;
int retval;
  if (__printf_func == NULL)
    return 0; /* or perhaps -1? */
  va_start (args, fmt);
  retval = (*__printf_func)(fmt, args);
  va_end (args);
  return retval;
}

HTH,
Tim Van Holder

--
Hi, I'm a signature virus. plz set me as your signature and help me spread
:)



- Raw text -


  webmaster     delorie software   privacy  
  Copyright © 2019   by DJ Delorie     Updated Jul 2019