Patchwork [1/2] Add support for gcc format attribute gnu_printf

login
register
mail settings
Submitter Stefan Weil
Date Sept. 13, 2010, 8:02 p.m.
Message ID <1284408157-17276-1-git-send-email-weil@mail.berlios.de>
Download mbox | patch
Permalink /patch/64643/
State New
Headers show

Comments

Stefan Weil - Sept. 13, 2010, 8:02 p.m.
Since version 4.4.x, gcc supports additional format attributes.
    __attribute__ ((format (gnu_printf, 1, 2)))
should be used instead of
    __attribute__ ((format (printf, 1, 2))
because QEMU always uses standard format strings (even with mingw32).

For older compilers, we simply define gnu_printf = printf,
so they work with the new format attribute, too.

Signed-off-by: Stefan Weil <weil@mail.berlios.de>
---
 configure |   11 +++++++++++
 1 files changed, 11 insertions(+), 0 deletions(-)
Blue Swirl - Sept. 20, 2010, 7:18 p.m.
On Mon, Sep 13, 2010 at 8:02 PM, Stefan Weil <weil@mail.berlios.de> wrote:
> Since version 4.4.x, gcc supports additional format attributes.
>    __attribute__ ((format (gnu_printf, 1, 2)))
> should be used instead of
>    __attribute__ ((format (printf, 1, 2))
> because QEMU always uses standard format strings (even with mingw32).
>
> For older compilers, we simply define gnu_printf = printf,
> so they work with the new format attribute, too.
>
> Signed-off-by: Stefan Weil <weil@mail.berlios.de>
> ---
>  configure |   11 +++++++++++
>  1 files changed, 11 insertions(+), 0 deletions(-)
>
> diff --git a/configure b/configure
> index 4061cb7..1300879 100755
> --- a/configure
> +++ b/configure
> @@ -148,6 +148,17 @@ for flag in $gcc_flags; do
>     fi
>  done
>
> +# Check gnu_printf (supported by gcc >= 4.4.x).
> +cat > $TMPC << EOF
> +static void test(const char *format, ...)
> +    __attribute__ ((format (gnu_printf, 1, 2)));
> +static void test(const char *format, ...) {}
> +int main(void) { test("\n"); return 0; }
> +EOF
> +if ! compile_prog "-Werror" ""; then
> +    QEMU_CFLAGS="-Dgnu_printf=printf $QEMU_CFLAGS"

This could cause problems, for example if system headers declared a
function called gnu_printf.

I'd introduce CONFIG_GCC_FMT_ATTR_GNU_PRINTF and adjust GCC_FMT_ATTR
definition based on that.
Stefan Weil - Sept. 20, 2010, 9:03 p.m.
Am 20.09.2010 21:18, schrieb Blue Swirl:
> On Mon, Sep 13, 2010 at 8:02 PM, Stefan Weil<weil@mail.berlios.de>  wrote:
>    
>> Since version 4.4.x, gcc supports additional format attributes.
>>     __attribute__ ((format (gnu_printf, 1, 2)))
>> should be used instead of
>>     __attribute__ ((format (printf, 1, 2))
>> because QEMU always uses standard format strings (even with mingw32).
>>
>> For older compilers, we simply define gnu_printf = printf,
>> so they work with the new format attribute, too.
>>
>> Signed-off-by: Stefan Weil<weil@mail.berlios.de>
>> ---
>>   configure |   11 +++++++++++
>>   1 files changed, 11 insertions(+), 0 deletions(-)
>>
>> diff --git a/configure b/configure
>> index 4061cb7..1300879 100755
>> --- a/configure
>> +++ b/configure
>> @@ -148,6 +148,17 @@ for flag in $gcc_flags; do
>>      fi
>>   done
>>
>> +# Check gnu_printf (supported by gcc>= 4.4.x).
>> +cat>  $TMPC<<  EOF
>> +static void test(const char *format, ...)
>> +    __attribute__ ((format (gnu_printf, 1, 2)));
>> +static void test(const char *format, ...) {}
>> +int main(void) { test("\n"); return 0; }
>> +EOF
>> +if ! compile_prog "-Werror" ""; then
>> +    QEMU_CFLAGS="-Dgnu_printf=printf $QEMU_CFLAGS"
>>      
> This could cause problems, for example if system headers declared a
> function called gnu_printf.
>
> I'd introduce CONFIG_GCC_FMT_ATTR_GNU_PRINTF and adjust GCC_FMT_ATTR
> definition based on that.
>
>    

Thanks for your review. I'll send a new patch which goes
into the direction which you suggested.

Instead of defining a new macro CONFIG_GCC_FMT_ATTR_GNU_PRINTF,
my new patch checks the gcc version (gnu_printf was introduced
with gcc 4.4). Like this, no changes to file configure are needed.

Regards
Stefan

Patch

diff --git a/configure b/configure
index 4061cb7..1300879 100755
--- a/configure
+++ b/configure
@@ -148,6 +148,17 @@  for flag in $gcc_flags; do
     fi
 done
 
+# Check gnu_printf (supported by gcc >= 4.4.x).
+cat > $TMPC << EOF
+static void test(const char *format, ...)
+    __attribute__ ((format (gnu_printf, 1, 2)));
+static void test(const char *format, ...) {}
+int main(void) { test("\n"); return 0; }
+EOF
+if ! compile_prog "-Werror" ""; then
+    QEMU_CFLAGS="-Dgnu_printf=printf $QEMU_CFLAGS"
+fi
+
 # check that the C compiler works.
 cat > $TMPC <<EOF
 int main(void) {}