This is the mail archive of the gcc-help@gcc.gnu.org mailing list for the GCC project.


Index Nav: [Date Index] [Subject Index] [Author Index] [Thread Index]
Message Nav: [Date Prev] [Date Next] [Thread Prev] [Thread Next]
Other format: [Raw text]

Inconsistent printf format warnings for macro in system headers


Hello,

In gcc versions starting from 4.8, it seems that printf format warnings in macros are inconsistent, depending on whether the header defining the macro is included as a system header or not, and on the build mode (C or C++).

With gcc < 4.8 (tried gcc 4.6.3 and 4.7.2), there are printf format warnings for both C and C++ languages, when the macro is in a system header or not.

With gcc >= 4.8 (tried 4.8.2 and 5.2.1 snapshot), there are no printf format warnings when the build mode is C++ *and* when the macro is in a system header. If the build mode is C or if the macro is in a non-system header, the printf format warnings are emitted.

Please see below for a test case and results in various configurations.
Can this be considered as a bug in gcc ?
Or is this behavior somehow expected?
If so, what is the proper way to get printf format warnings in C++ mode for macros defined in system headers?

Thanks in advance


Here is a sample test case:
$ mkdir -p syshdr_dir
$ cat <<EOF > syshdr_dir/myprintf.h
#include <stdio.h>
#define MY_PRINTF(x, ...) printf(x, ## __VA_ARGS__);
EOF

$ cat <<EOF > test-format-printf.c
#include <myprintf.h>
int main(void) {
    long long ll = 0;
    MY_PRINTF("%ld", ll);
    return 0;
}
EOF

With gcc 4.6.3 and 4.7.2, I get printf format warnings for both C and C++ languages.

$ gcc -isystem $PWD/syshdr_dir -Wall -Werror -x c++ -c test-format-printf.c -o /dev/null
test-format-printf.c: In function âint main()â:
test-format-printf.c:4:5: error: format â%ldâ expects argument of type âlong intâ, but argument 2 has type âlong long intâ [-Werror=format]
cc1plus: all warnings being treated as errors

$ gcc -isystem $PWD/syshdr_dir -Wall -Werror -x c -c test-format-printf.c -o /dev/null
test-format-printf.c: In function âmainâ:
test-format-printf.c:4:5: error: format â%ldâ expects argument of type âlong intâ, but argument 2 has type âlong long intâ [-Werror=format]
cc1: all warnings being treated as errors


With gcc 4.8.2 and a 5.2.1 snapshot, there is a warning for C, but no warning for C++ when the macro is in a system header.

$ gcc -isystem $PWD/syshdr_dir -Wall -Werror -x c++ -c test-format-printf.c -o /dev/null

$ gcc -isystem $PWD/syshdr_dir -Wall -Werror -x c -c test-format-printf.c -o /dev/null
In file included from test-format-printf.c:1:0:
test-format-printf.c: In function âmainâ:
test-format-printf.c:4:15: error: format â%ldâ expects argument of type âlong intâ, but argument 2 has type âlong long intâ [-Werror=format=]
     MY_PRINTF("%ld", ll);
               ^
cc1: all warnings being treated as errors

$ gcc -I $PWD/syshdr_dir -Wall -Werror -x c++ -c test-format-printf.c -o /dev/null
In file included from test-format-printf.c:1:0:
test-format-printf.c: In function âint main()â:
/home/sah0146/dev/workspaces/bcm7252/hgw/build/MAIN/syshdr_dir/myprintf.h:2:51: error: format â%ldâ expects argument of type âlong intâ, but argument 2 has type âlong long intâ [-Werror=format=]
 #define MY_PRINTF(x, ...) printf(x, ## __VA_ARGS__);
                                                   ^
test-format-printf.c:4:5: note: in expansion of macro âMY_PRINTFâ
     MY_PRINTF("%ld", ll);
     ^
cc1plus: all warnings being treated as errors

--
Olivier Blin - SoftAtHome


Index Nav: [Date Index] [Subject Index] [Author Index] [Thread Index]
Message Nav: [Date Prev] [Date Next] [Thread Prev] [Thread Next]