How can I let gcc and clang generate warnings for header files included by header files in non-current directories? I'm using gcc 4.9.2 and clang 3.6.0.
For example, assume that ./include_a.c
includes ./dir/a.h
, ./dir/a.h
includes ./b.h
, and ./b.h
is expected to generate a warning for -Wconversion
; then, gcc and clang with -Wconversion
DO NOT generate the expected warning when they compile include_a.c
. With -Wconversion -Wsystem-headers
, the expected warning is generated, but it often comes with many useless warnings for system headers. When ./b.h
is directly included from a source file in the current directory (such as ./include_b.c
), the expected warning is generated without -Wsystem-headers
.
The following shell script reproduces this example (the case of clang is omitted):
#!/bin/sh
mkdir dir
echo '#include "b.h"' >dir/a.h
echo 'void f() {int a = 0; char b = a;}' >b.h
echo '#include "dir/a.h"' >include_a.c
echo '#include "b.h"' >include_b.c
set -x
gcc -c include_a.c -Wconversion # DOES NOT generate a warning
gcc -c include_a.c -Wconversion -Wsystem-headers # generate a warning
gcc -c include_b.c -Wconversion # generate a warning
Output:
+ gcc -c include_a.c -Wconversion
+ gcc -c include_a.c -Wconversion -Wsystem-headers
In file included from dir/a.h:1:0,
from include_a.c:1:
./b.h: In function ‘f’:
./b.h:1:31: warning: conversion to ‘char’ from ‘int’ may alter its value [-Wconversion]
void f() {int a = 0; char b = a;}
^
+ gcc -c include_b.c -Wconversion
In file included from include_b.c:1:0:
b.h: In function ‘f’:
b.h:1:31: warning: conversion to ‘char’ from ‘int’ may alter its value [-Wconversion]
void f() {int a = 0; char b = a;}
^
This behavior seems not to be a bug because gcc and clang perform in the same way, but how can I obtain warnings from all the my source files?