Add predefine macros such as __STDC__

This commit is contained in:
Rui Ueyama 2020-04-01 23:54:33 +09:00
parent e7fdc2e3f1
commit 5f5a8507ff
2 changed files with 53 additions and 0 deletions

View File

@ -783,8 +783,59 @@ static Token *preprocess2(Token *tok) {
return head.next;
}
static void define_macro(char *name, char *buf) {
Token *tok = tokenize(new_file("<built-in>", 1, buf));
add_macro(name, true, tok);
}
static void init_macros(void) {
// Define predefined macros
define_macro("_LP64", "1");
define_macro("__C99_MACRO_WITH_VA_ARGS", "1");
define_macro("__ELF__", "1");
define_macro("__LP64__", "1");
define_macro("__SIZEOF_DOUBLE__", "8");
define_macro("__SIZEOF_FLOAT__", "4");
define_macro("__SIZEOF_INT__", "4");
define_macro("__SIZEOF_LONG_DOUBLE__", "8");
define_macro("__SIZEOF_LONG_LONG__", "8");
define_macro("__SIZEOF_LONG__", "8");
define_macro("__SIZEOF_POINTER__", "8");
define_macro("__SIZEOF_PTRDIFF_T__", "8");
define_macro("__SIZEOF_SHORT__", "2");
define_macro("__SIZEOF_SIZE_T__", "8");
define_macro("__SIZE_TYPE__", "unsigned long");
define_macro("__STDC_HOSTED__", "1");
define_macro("__STDC_NO_ATOMICS__", "1");
define_macro("__STDC_NO_COMPLEX__", "1");
define_macro("__STDC_NO_THREADS__", "1");
define_macro("__STDC_NO_VLA__", "1");
define_macro("__STDC_VERSION__", "201112L");
define_macro("__STDC__", "1");
define_macro("__USER_LABEL_PREFIX__", "");
define_macro("__alignof__", "_Alignof");
define_macro("__amd64", "1");
define_macro("__amd64__", "1");
define_macro("__chibicc__", "1");
define_macro("__const__", "const");
define_macro("__gnu_linux__", "1");
define_macro("__inline__", "inline");
define_macro("__linux", "1");
define_macro("__linux__", "1");
define_macro("__signed__", "signed");
define_macro("__typeof__", "typeof");
define_macro("__unix", "1");
define_macro("__unix__", "1");
define_macro("__volatile__", "volatile");
define_macro("__x86_64", "1");
define_macro("__x86_64__", "1");
define_macro("linux", "1");
define_macro("unix", "1");
}
// Entry point function of the preprocessor.
Token *preprocess(Token *tok) {
init_macros();
tok = preprocess2(tok);
if (cond_incl)
error_tok(cond_incl->tok, "unterminated conditional directive");

View File

@ -322,6 +322,8 @@ int main() {
#undef foo
ASSERT(1, __STDC__);
printf("OK\n");
return 0;
}