Search code examples
cgccclanginternals

Where is the clang <built-in> header file?


I was digging around in some C internals, and I found the following line from clang's stddef.h (located in /usr/lib/llvm-11/lib/clang/11.0.0/include/):

typedef __WCHAR_TYPE__ wchar_t;

This is different from what I found in gcc's stddef.h (located in /usr/lib/gcc/x86_64-pc-linux-gnu/10.0.1/include/):

#ifndef __WCHAR_TYPE__    
#define __WCHAR_TYPE__ int    
#endif    
#ifndef __cplusplus    
typedef __WCHAR_TYPE__ wchar_t;    
#endif

In gcc's stddef.h, __WCHAR_TYPE__ is clearly defined as an int, but in clang's stddef.h there was no mention of __WCHAR_TYPE__ anywhere except for that one line. So, I decided to define the macro myself in order to find out where the macro was defined. I compiled the following file with clang:

#define __WCHAR_TYPE__ x

And, I got the following warning:

file.c:1:9: warning: '__WCHAR_TYPE__' macro redefined [-Wmacro-redefined]
#define __WCHAR_TYPE__ x
        ^
<built-in>:85:9: note: previous definition is here
#define __WCHAR_TYPE__ int

It would seem that there's some sort of "built-in" header that defines the __WCHAR_TYPE__ macro, and that header would appear to be an actual file, judging by the fact that a line number was provided. However, I recursively grepped for #define __WCHAR_TYPE__ int in /usr and I couldn't find anything outside of gcc's header files. So, my question is, is this "built-in" header file an actual file, and if so, where can I find it?


Solution

  • It's not an actual file; clang has a number of predefined macros built in.

    You can see all these macros by doing clang -dM -E foo.c where foo.c is any file (it can even be empty). Among the list I saw

    #define __WCHAR_TYPE__ int
    

    In the clang source, this is all done in clang/lib/Frontend/InitPreprocessor.cpp.

    gcc actually has it as well (run gcc -dM -E foo.c; clang and gcc mostly support the same command-line options). The #define in <stddef.h> is probably just there as a backup.