How about using the C preprocessor itself?
If you run gcc -E foo.cpp (where foo.cpp is your sample input file), you will get:
# 1 "foo.cpp" # 1 "<built-in>" 1 # 1 "<built-in>" 3 # 326 "<built-in>" 3 # 1 "<command line>" 1 # 1 "<built-in>" 2 # 1 "foo.cpp" 2 # 1 "/Applications/Xcode.app/Contents/Developer/Toolchains/XcodeDefault.xctoolchain/usr/bin/../lib/clang/6.1.0/include/stddef.h" 1 3 4
Lines up to # 1 "foo.cpp" 2 are patterns and can be ignored. (See what your C preprocessor generates).
When you get to # 1 some-other-file ... you know that you pressed #include.
You will get the full path name (not the way it appears in the #include statement), but you can also indicate where #include appeared, looking back at the last line marker.
In this case, the last line marker is # 1 foo.cpp 2 and appears 9 lines back, so #include for stddef.h is on line 9 of foo.cpp .
So now you can go back to the original file and take line 9.
source share