shithub: scc

Download patch

ref: 8cd311e6ab6c87983620397b63d120957d540d3c
parent: c3bb2c04976c4e4ff610aec7b593c192e55a84b2
author: Roberto E. Vargas Caballero <k0ga@shike2.com>
date: Tue Apr 5 14:43:06 EDT 2022

cc1: Pass a macro to addinput()

Addinput() was receiving the symbol related to the macro expansion
but it makes more sense to receive the macro itself because we can
use other fields of the macro later.

--- a/src/cmd/cc/cc1/cc1.h
+++ b/src/cmd/cc/cc1/cc1.h
@@ -410,7 +410,7 @@
 	unsigned lineno;
 	char *filenam;
 	FILE *fp;
-	Symbol *macro;
+	Macro *macro;
 	char *line, *begin, *p;
 	struct input *next;
 };
@@ -459,7 +459,7 @@
 extern int next(void);
 extern void expect(int tok);
 extern void discard(void);
-extern int addinput(char *fname, Symbol *hide, char *buffer, int fail);
+extern int addinput(char *, Macro *, char *, int);
 extern void delinput(void);
 extern void setsafe(int type);
 extern void setloc(char *fname, unsigned line);
--- a/src/cmd/cc/cc1/cpp.c
+++ b/src/cmd/cc/cc1/cpp.c
@@ -20,18 +20,23 @@
 int disescape;
 
 void
-defdefine(char *macro, char *val, char *source)
+defdefine(char *name, char *val, char *source)
 {
 	char *def, *fmt = "#define %s %s\n";
-	Symbol dummy = {.name = macro, .flags = SDECLARED};
+	Symbol *sym = &(Symbol) {
+		.name = name,
+		.flags = SDECLARED,
+	};
+	Macro *mp;
 
 	if (!val)
 		val = "";
-	def = xmalloc(strlen(fmt) + strlen(macro) + strlen(val));
+	def = xmalloc(strlen(fmt) + strlen(name) + strlen(val));
+	sprintf(def, fmt, name, val);
+	mp = newmacro(sym);
 
-	sprintf(def, fmt, macro, val);
 	lineno = ++ncmdlines;
-	addinput(source, &dummy, def, FAIL);
+	addinput(source, mp, def, FAIL);
 	cpp();
 	delinput();
 }
@@ -311,7 +316,8 @@
 	mp->arglist = NULL;
 	mp->def = sym->u.s + 3;
 	mp->npars = 0;
-	mp->npars = atoi(sym->u.s);
+	if (sym->u.s)
+		mp->npars = atoi(sym->u.s);
 
 	return mp;
 }
@@ -353,8 +359,7 @@
 	mp->bufsiz = 0;
 	buffer[elen] = '\0';
 	DBG("MACRO '%s' expanded to :'%s'", mp->sym->name, buffer);
-	addinput(filenam, mp->sym, xstrdup(buffer), FAIL);
-	delmacro(mp);
+	addinput(filenam, mp, xstrdup(buffer), FAIL);
 
 	return 1;
 }
--- a/src/cmd/cc/cc1/lex.c
+++ b/src/cmd/cc/cc1/lex.c
@@ -65,20 +65,22 @@
 }
 
 int
-addinput(char *fname, Symbol *sym, char *buffer, int fail)
+addinput(char *fname, Macro *mp, char *buffer, int fail)
 {
 	FILE *fp;
 	char *extp;
 	unsigned flags;
 	int infileln;
+	Symbol *sym;
 	Input *newip, *curip = input;
 
 	if (curip)
 		curip->lineno = lineno;
 
-	if (sym) {
+	if (mp) {
 		/* this is a macro expansion */
 		fp = NULL;
+		sym = mp->sym;
 		DBG("MACRO: %s expanded to '%s'", sym->name, buffer);
 		hide(sym);
 		flags = IMACRO;
@@ -118,7 +120,7 @@
 
 	newip = xmalloc(sizeof(*newip));
 	newip->next = curip;
-	newip->macro = sym;
+	newip->macro = mp;
 	newip->lineno = lineno;
 	newip->p = newip->begin = newip->line = buffer;
 	newip->filenam = NULL;
@@ -141,11 +143,12 @@
 			die("cc1: %s: %s", ip->filenam, strerror(errno));
 		break;
 	case IMACRO:
-		unhide(ip->macro);
+		unhide(ip->macro->sym);
 		break;
 	}
 
 	input = ip->next;
+	delmacro(ip->macro);
 	free(ip->filenam);
 	free(ip->line);
 	free(ip);