From 0e75fc383becccd113416677b7e26e0caf21e28b Mon Sep 17 00:00:00 2001 From: lemon Date: Sat, 21 Mar 2026 17:55:01 +0100 Subject: Rework handling of predefined macros. And add some GCC predefs like __SIZE_TYPE__, __LONG_SIZE__, etc --- src/a_main.c | 28 +++++++++++++++++++++++++--- 1 file changed, 25 insertions(+), 3 deletions(-) (limited to 'src/a_main.c') diff --git a/src/a_main.c b/src/a_main.c index 8004dd3..309fe23 100644 --- a/src/a_main.c +++ b/src/a_main.c @@ -121,7 +121,29 @@ static Task task = { .inf = VINIT(infilebuf, countof(infilebuf)) }; static void prihelp(void); -void cpppredef(bool undef, const char *cmd); +void cpp0define(const char *name, const char *body); +void cpp0undef(const char *name); + +static void +predef(bool undef, const char *cmd) +{ + char buf[1024]; + const char *sep = strchr(cmd, '='), + *body = sep ? sep+1 : "1"; + const char *name; + if (sep) { + uint n = sep - cmd; + assert(n < sizeof buf - 1); + memcpy(buf, cmd, n); + buf[n] = 0; + name = buf; + } else { + name = cmd; + } + if (undef) cpp0undef(name); + else cpp0define(name, body); +} + static void optparse(char **args) @@ -251,7 +273,7 @@ optparse(char **args) } else if (*arg == 'D' || *arg == 'U') { const char *def = arg[1] ? arg+1 : *++args; if (!def) fatal(NULL, "macro name missing after `-%c`", *arg); - cpppredef(*arg == 'U', def); + predef(*arg == 'U', def); } else if (*arg == 'O') { /* TODO optimization level */ } else if (*arg == 'I' || !strcmp(arg, "-include-directory")) { @@ -754,7 +776,7 @@ main(int argc, char **argv) } for (const char *const *p = host_predefs; *p; ++p) - cpppredef(0, *p); + predef(0, *p); return driver(); } -- cgit v1.2.3