Inconsistent printf format warnings for macro in system headers

Olivier Blin olivier.blin@softathome.com
Fri Oct 16 14:40:00 GMT 2015


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



More information about the Gcc-help mailing list