clang missing a warning.

Hi,

How do I get clang to detect the following error
#include <stdio.h>

extern test_printf(const char *format, ...);

int main()
{
        int a,b;
        a = 1;
        b = 2;
        test_printf("Hello a=0x%x, b=0x%x, c=0x%x\n", a, b);
        printf("Hello a=0x%x, b=0x%x, c=0x%x\n", a, b);

        return 0;
}

When compiled with:
clang -c -g -O0 -o test.o test.c

It correctly warns about the lack of a third param in the "printf()" line.
How do I get it to warn on the test_printf() line?

Kind Regards

James

Hi James,

Put __attribute__((format(printf(1,2))) on test_printf.

http://gcc.gnu.org/onlinedocs/gcc/Function-Attributes.html

Dmitri

extern test_printf(const char *format, ...);

It correctly warns about the lack of a third param in the "printf()" line.
How do I get it to warn on the test_printf() line?

I think you want the “format” attribute. I couldn’t find Clang’s documentation (if it exists), but GCC’s is at http://gcc.gnu.org/onlinedocs/gcc/Function-Attributes.html.

Basically, you add __attribute__((format(printf, 1, 2))).

Where ‘1’ means the format string is first argument, ‘2’ means start verifying it against arguments from 2 onwards.

Tim.

How do I get clang to detect the following error

Clang supports the GCC format attribute. See
http://gcc.gnu.org/onlinedocs/gcc/Function-Attributes.html#index-Wformat-26
40

extern test_printf(const char *format, ...);

[...]

It correctly warns about the lack of a third param in the "printf()"
line.
How do I get it to warn on the test_printf() line?

For example, if you change the declaration of test_printf as follows:

  extern int test_printf(const char *format, ...)
    __attribute__ ((format (printf, 1, 2)));;

Then Clang will emit the same warning for both lines.

Stefanus

Hi,

How do I get clang to detect the following error #include <stdio.h>

extern test_printf(const char *format, ...);

int main()
{
        int a,b;
        a = 1;
        b = 2;
        test_printf("Hello a=0x%x, b=0x%x, c=0x%x\n", a, b);
        printf("Hello a=0x%x, b=0x%x, c=0x%x\n", a, b);

        return 0;
}

When compiled with:
clang -c -g -O0 -o test.o test.c

It correctly warns about the lack of a third param in the "printf()" line.
How do I get it to warn on the test_printf() line?

You must adorn the prototype with information for the compiler--that it
accepts a format string, and where to find the format string, and where to
find the fixed parameters:

void test_printf(const char *fmt, ...) __attribute__((__format__ (printf, 1,
2)));

See:

http://clang.llvm.org/docs/LanguageExtensions.html#format-string-checking

void test_printf(const char *fmt, ...) __attribute__((__format__ (printf, 1,
2)));

See:

http://clang.llvm.org/docs/LanguageExtensions.html#format-string-checking

Thank you everyone who responded. Worked perfectly.