shithub: scc

Download patch

ref: e7f022e3a196d7473765f5f456da2e19057a7e82
parent: c69a4b90cc4b70415cc5ac62d9465b7ec8fdd543
author: Roberto E. Vargas Caballero <k0ga@shike2.com>
date: Tue Aug 11 13:27:15 EDT 2015

Free cpp symbols after undefining them

Due to the previous data structures used it was hard to free
symbols out of popctx(), but cpp symbols are not anymore in any list.
They are in the hash table (and usually at the beginning of the
collision list), so it is very easy to free them now.

--- a/cc1/cc1.h
+++ b/cc1/cc1.h
@@ -307,6 +307,7 @@
 extern void pushctx(void), popctx(void);
 extern void ikeywords(void);
 extern Symbol *addmacro(void);
+extern void delmacro(Symbol *sym);
 
 /* stmt.c */
 extern void compound(Symbol *lbreak, Symbol *lcont, Caselist *lswitch);
--- a/cc1/cpp.c
+++ b/cc1/cpp.c
@@ -531,8 +531,6 @@
 static void
 undef(void)
 {
-	Symbol *sym;
-
 	if (cppoff)
 		return;
 	if (yytoken != IDEN) {
@@ -539,8 +537,8 @@
 		error("no macro name given in #undef directive");
 		return;
 	}
-	sym = lookup(NS_CPP);
-	sym->flags &= ~ISDECLARED;
+	if (yylval.sym->ns == NS_CPP)
+		delmacro(yylval.sym);
 	next();
 }
 
--- a/cc1/symbol.c
+++ b/cc1/symbol.c
@@ -222,6 +222,14 @@
 	curctx = ctx;
 	return sym;
 }
+
+void
+delmacro(Symbol *sym)
+{
+	unlinkhash(sym);
+	free(sym->name);
+	free(sym->u.s);
+	free(sym);
 }
 
 Symbol *