Already exists in gcc (look for -dCHARS in https://gcc.gnu.org/onlinedocs/gcc-6.2.0/gcc/Preprocessor-Options.html#Preprocessor-Options ).
This change aims to add this option, pass it through to the preprocessor via the options class, and when inclusions occur we output some information (+ test cases). In my own testing and experimenting with it I do see similar results to gcc's.
cd ~/build # build dir for llvm+clang echo '#include <header1.h>' > test.cpp echo 'int main() { return FOO; }' >> test.cpp echo '#include "header2.h"' > header1.h echo '#include "/tmp/header3.h"' >> header1.h echo '#define FOO 42' > /tmp/header2.h echo '#define BAR 43' > /tmp/header3.h bin/clang++ -cc1 -E -isystem /tmp -I $(pwd) -dI test.cpp # 1 "test.cpp" # 1 "<built-in>" 1 # 1 "<built-in>" 3 # 319 "<built-in>" 3 # 1 "<command line>" 1 # 1 "<built-in>" 2 # 1 "test.cpp" 2 #include <header1.h> /* clang -E -dI */ # 1 "test.cpp" # 1 "/Users/steveo/build/header1.h" 1 #include "header2.h" /* clang -E -dI */ # 1 "/Users/steveo/build/header1.h" # 1 "/tmp/header2.h" 1 3 # 2 "/Users/steveo/build/header1.h" 2 #include "/tmp/header3.h" /* clang -E -dI */ # 2 "/Users/steveo/build/header1.h" # 1 "/tmp/header3.h" 1 # 3 "/Users/steveo/build/header1.h" 2 # 2 "test.cpp" 2 int main() { return 42; }
Equivalent GCC invocation:
g++ -E -isystem /tmp -I $(pwd) -dI test.cpp # 1 "test.cpp" # 1 "<built-in>" # 1 "<command-line>" # 1 "test.cpp" #include <header1.h> # 1 "test.cpp" # 1 "/home/steveo/build/header1.h" 1 #include "header2.h" # 1 "/home/steveo/build/header1.h" # 1 "/tmp/header2.h" 1 3 4 # 2 "/home/steveo/build/header1.h" 2 #include "/tmp/header3.h" # 2 "/home/steveo/build/header1.h" # 1 "/tmp/header3.h" 1 # 2 "/home/steveo/build/header1.h" 2 # 2 "test.cpp" 2 int main() { return # 2 "test.cpp" 3 4 42 # 2 "test.cpp" ; }
The line markers just following the #include seem to reflect the source code location where the inclusion occurred, and the actual path to the included file. This diff doesn't change the behavior or ordering of that; however it seems to reliably occur in both clang and gcc.
Thanks very much @bruno and sorry for the trouble here! Looks like I was expecting a forward slash at the beginning, forgetting about drive letters on Windows -- and backslashes (actually looks like backslashes and forward slashes can get mixed?).
Indeed the output on windows looked like:
which actually looks correct. I'll have to correct my test here for this case. Will have this updated in a couple of minutes. Thanks very much!