scc

simple C compiler
git clone git://git.2f30.org/scc
Log | Files | Refs | README | LICENSE

commit 1a58b74e71aff78c8d6bee79604a8330c745d927
parent 23e5d28bcfafb191be14957906a01453dd911353
Author: Roberto E. Vargas Caballero <k0ga@shike2.com>
Date:   Tue, 14 Jun 2016 12:58:39 +0200

[cc1] Deal with redefinitions in the command line

The POSIX standard commands that -D definitions
without replacement parts must be defined with
a replacement string "1". We also warn in the
case of redefining any macro, even when they have the
same replacement string. It is easier and it is
a good practice to define them only once.

Diffstat:
Mcc1/cpp.c | 23++++++++++++++++-------
1 file changed, 16 insertions(+), 7 deletions(-)

diff --git a/cc1/cpp.c b/cc1/cpp.c @@ -26,15 +26,24 @@ defmacro(char *s) { char *p, *q; Symbol *sym; + char def[] = "=1"; - if ((p = strchr(s, '=')) != NULL) { - *p++='\0'; - q = xmalloc(strlen(p) + 4); - sprintf(q, "-1#%s", p); - p = q; + if ((p = strchr(s, '=')) == NULL) + p = def; + *p++='\0'; + q = xmalloc(strlen(p) + 4); + sprintf(q, "-1#%s", p); + + sym = lookup(NS_CPP, s); + if (sym->flags & SDECLARED) { + warn("'%s' redefined"); + free(sym->u.s); + } else { + install(NS_CPP, sym); + sym->flags |= SDECLARED|SSTRING; } - sym = install(NS_CPP, lookup(NS_CPP, s)); - sym->u.s = p; + + sym->u.s = q; return sym; }