Hacker News new | ask | show | jobs
by DerekL 1497 days ago
The compiler would store the condition in memory, and reevaluate the condition every time the file is included again. If the contents of the file aren't skipped, then it would actually read the file again and parse it normally.

For instance, if the file was this:

    // my-file.h
    
    #ifndef MY_FILE_INCLUDE_GUARD
    #define MY_FILE_INCLUDE_GUARD

    void f(void);

    #endif
the compiler would remember the “#ifndef MY_FILE_INCLUDE_GUARD” part.
1 comments

Sure you can reuse a cached file at a token level, but you cannot easily do that at the parse tree can be completely different based on preprocessor state.
I'm just showing how a compiler could optimize files that have include guards. This doesn't do anything for files that are intended to be included multiple times.