Skip to content

Commit

Permalink
Add predefine macros such as __STDC__
Browse files Browse the repository at this point in the history
  • Loading branch information
rui314 committed Dec 7, 2020
1 parent e7fdc2e commit 5f5a850
Show file tree
Hide file tree
Showing 2 changed files with 53 additions and 0 deletions.
51 changes: 51 additions & 0 deletions preprocess.c
Original file line number Diff line number Diff line change
Expand Up @@ -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");
Expand Down
2 changes: 2 additions & 0 deletions test/macro.c
Original file line number Diff line number Diff line change
Expand Up @@ -322,6 +322,8 @@ int main() {

#undef foo

ASSERT(1, __STDC__);

printf("OK\n");
return 0;
}

0 comments on commit 5f5a850

Please sign in to comment.