https://gcc.gnu.org/bugzilla/show_bug.cgi?id=68052
Bug ID: 68052
Summary: No printf format warnings in C++ code for macro in
system headers
Product: gcc
Version: 5.2.1
Status: UNCONFIRMED
Severity: normal
Priority: P3
Component: c++
Assignee: unassigned at gcc dot gnu.org
Reporter: gcc at blino dot org
Target Milestone: ---
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.
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