Re: use gcc's printf __attribute__

[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index]

 



On Sat, Mar 25, 2006 at 12:00:45PM +0100, Jim Meyering wrote:
> Hello,
> I noticed that there are a few printf-like functions here,
> but that they don't benefit from gcc's format-string vs. arg-type
> checking.  Here's a patch to enable that:
> 
> However, to make it portable to non-gcc systems, this block
> of cpp directives needs to go somewhere (maybe libvirt.h?)
> so it'll be used from both files.
> 
> #ifndef __attribute__
> /* This feature is available in gcc versions 2.5 and later.  */
> # if __GNUC__ < 2 || (__GNUC__ == 2 && __GNUC_MINOR__ < 5)
> #  define __attribute__(Spec) /* empty */
> # endif
> /* The __-protected variants of `format' and `printf' attributes
>    are accepted by gcc versions 2.6.4 (effectively 2.7) and later.  */
> # if __GNUC__ < 2 || (__GNUC__ == 2 && __GNUC_MINOR__ < 7)
> #  define __format__ format
> #  define __printf__ printf
> # endif
> #endif

  I'm adding this to src/internal.h , should not be in libvirt.h as this
is not partof the API.

> 2006-03-25  Jim Meyering  <jim@xxxxxxxxxxxx>
> 
> 	* src/virsh.c (vshPrint, vshError): Add gcc's __printf__ attribute.
> 	* src/xml.h (virBufferVSprintf): Likewise.
> 
> Index: src/xml.h
> ===================================================================
> RCS file: /data/cvs/libvirt/src/xml.h,v
> retrieving revision 1.3
> diff -u -p -r1.3 xml.h
> --- src/xml.h	15 Mar 2006 12:13:25 -0000	1.3
> +++ src/xml.h	25 Mar 2006 10:56:27 -0000
> @@ -25,7 +25,8 @@ extern "C" {
>      };
> 
>      int virBufferAdd(virBufferPtr buf, const char *str, int len);
> -    int virBufferVSprintf(virBufferPtr buf, const char *format, ...);
> +    int virBufferVSprintf(virBufferPtr buf, const char *format, ...)
> +      __attribute__ ((__format__ (__printf__, 2, 3)));
>      char *virDomainParseXMLDesc(const char *xmldesc, char **name);
> 
>  #ifdef __cplusplus
> Index: src/virsh.c
> ===================================================================
> RCS file: /data/cvs/libvirt/src/virsh.c,v
> retrieving revision 1.21
> diff -u -p -r1.21 virsh.c
> --- src/virsh.c	15 Mar 2006 12:13:25 -0000	1.21
> +++ src/virsh.c	25 Mar 2006 10:56:28 -0000
> @@ -177,8 +177,8 @@ typedef struct __vshControl {
> 
>  static vshCmdDef commands[];
> 
> -static void vshError(vshControl * ctl, int doexit, const char *format,
> -                     ...);
> +static void vshError(vshControl * ctl, int doexit, const char *format, ...)
> +  __attribute__ ((__format__ (__printf__, 3, 4)));
>  static int vshInit(vshControl * ctl);
>  static int vshDeinit(vshControl * ctl);
>  static void vshUsage(vshControl * ctl, const char *cmdname);
> @@ -198,8 +198,9 @@ static virDomainPtr vshCommandOptDomain(
>                                          const char *optname, char **name);
> 
> 
> -static void vshPrint(vshControl * ctl, vshOutType out, const char *format,
> -                     ...);
> +static void vshPrint(vshControl * ctl, vshOutType out, const char *format, ...)
> +  __attribute__ ((__format__ (__printf__, 3, 4)));
> +  ;
> 
> 
>  static const char *vshDomainStateToString(int state);

  The problem is that it breaks the Python C parser used to extract the
API (doc/apibuild.py) so I can't apply this until I fix said parser, and
I think there is a bit more urgent ATM :-)

Daniel

-- 
Daniel Veillard      | Red Hat http://redhat.com/
veillard@xxxxxxxxxx  | libxml GNOME XML XSLT toolkit  http://xmlsoft.org/
http://veillard.com/ | Rpmfind RPM search engine http://rpmfind.net/


[Index of Archives]     [Virt Tools]     [Libvirt Users]     [Lib OS Info]     [Fedora Users]     [Fedora Desktop]     [Fedora SELinux]     [Big List of Linux Books]     [Yosemite News]     [KDE Users]     [Fedora Tools]