[cfe-dev] Reordering of application of -include and -include-pch options leads to preprocessing failures

Tom Honermann via cfe-dev cfe-dev at lists.llvm.org
Mon Jan 23 21:26:06 PST 2017


The following commands demonstrate generation of a PCH via a clang 
invocation that specifies a header via '-include', followed by clang 
invocations that consume the PCH via '-include-pch' to perform a 
compilation and to generate preprocessed output.  Compilation is 
successful, but generation of preprocessed output fails even though the 
driver invocations use options consistently.

$ cat t1.h
#warning Processing t1.h
#define MACRO_FROM_T1

$ cat t2.h
#warning Processing t2.h
#if !defined(MACRO_FROM_T1)
#error MACRO_FROM_T1 is not defined!
#endif

$ cat t.c
#warning Processing t.c

# Generating the PCH.  t1.h is pre-included and t2.h.gch is generated
# as expected.
$ clang -include t1.h t2.h
In file included from <built-in>:1:
./t1.h:1:2: warning: Processing t1.h [-W#warnings]
#warning Processing t1.h
  ^
t2.h:1:2: warning: Processing t2.h [-W#warnings]
#warning Processing t2.h
  ^
2 warnings generated.

# Compiling t.c.  Compilation is successful.  The PCH is consumed as
# expected and the include of t1.h appears to be ignored, presumably as
# a result of having already been included in the PCH.  I'm assuming
# that it is intentional that the '-include-pch' option is processed
# before the preceding '-include' option under the (correct in this
# case) assumption that the '-include' specified header may have
# already been incorporated in the PCH.
$ clang -include t1.h -include-pch t2.h.gch -c t.c
t.c:1:2: warning: Processing t.c [-W#warnings]
#warning Processing t.c
  ^
1 warning generated.

# Preprocessing t.c.  This fails.  As with compilation, it appears that
# the '-include-pch' option is processed before the preceding '-include'
# option.  However, the PCH presumably doesn't suffice for generating
# preprocessed output and the '-include-pch t2.h.gch' option is treated
# as though it were '-include t2.h'.  The include of t1.h is then
# applied afterwards which doesn't work for this test case.
$ clang -include t1.h -include-pch t2.h.gch -E t.c > /dev/null
In file included from <built-in>:1:
/slowfs/sighome/thonerma/tmp/t2.h:1:2: warning: Processing t2.h 
[-W#warnings]
#warning Processing t2.h
  ^
/slowfs/sighome/thonerma/tmp/t2.h:3:2: error: MACRO_FROM_T1 is not defined!
#error MACRO_FROM_T1 is not defined!
  ^
In file included from <built-in>:2:
./t1.h:1:2: warning: Processing t1.h [-W#warnings]
#warning Processing t1.h
  ^
t.c:1:2: warning: Processing t.c [-W#warnings]
#warning Processing t.c
  ^
3 warnings and 1 error generated.

It seems to me that, when invoked to generate preprocessed output, if 
PCH files won't actually be used, that clang should not be applying 
'-include-pch' options before preceding '-include' options.  Anyone have 
thoughts on this?

Tom.



More information about the cfe-dev mailing list