How clang handle user's include

Hi,

I'm looking at the clang source code and I'm wandering where clang handle the include from users.

If you have a simple C file foo.c:

#include <stdio.h>
#include "foo.h"

void foo(void) {
   printf("foo\n");
}

int main(void) {
   foo();
   return 0;
}

and the corresponding foo.h header:

void foo(void);

Which part of clang "merge" (or inline) foo.c and foo.h together? If I understand well that is what is happening, right?

Thank you for your answers!

Cheers,
Julien

It’s not entirely clear what you are asking - the full answer to your question would probably take a few days to write down with sufficient precision to explain "everything that happens due to #include “foo.h” inside clang. So if my explanation below isn’t “enough”, please provide a more specific follow-up question - I’m not sure I can give the answer, because what I’ve explained is nearly the full extent of my knowledge…

Clang doesn’t use a traditional pre-processor (as per the “old school C compiler”, where you’d get a “foo.i” file of the preprocessed input), instead it parses and understand preprocessor directives in the main processing of the file. It has code that understands the file-structure of the overall compilation unit. [Whether it’s a “user” or “system” include is not really important, except for a few cases of "if it’s a system include don’t issue warnings for <something that is considered harmless but sometimes ‘wrong’ in system headers)],

This comment in include/clang/Lex/Preprocessor.h explains the same thing I just said:
/// Lexers know only about tokens within a single source file, and don’t
/// know anything about preprocessor-level issues like the #include stack,
/// token expansion, etc.
class Preprocessor : public RefCountedBase

The Preprocessor class is used to “help” with the task of dealing with this. To the “compiler proper”, it will seem like all the input came from a single file.

Hi,