shithub: scc

Download patch

ref: d77f75260eb21812ac007ba6f867326ed8050133
parent: 44d1db65c7c38b6be3b0fa114c8f51ded3468983
author: Roberto E. Vargas Caballero <[email protected]>
date: Wed Jan 18 06:16:06 EST 2017

[cc1] Add type field to input structure

The type is implicit in several of the other fields
of the sructure but it is generating several problems,
because guessing continuosly the type is not easy always.
The number of Input structures is going to be small, so
the space wasted is totally insignificant and the
quality of the code can be improved a lot.

--- a/cc1/cc1.h
+++ b/cc1/cc1.h
@@ -23,6 +23,14 @@
 	TK_R     = 1 << 5,    /* this is a K&R-function */
 };
 
+enum inputtype {
+	IMACRO = 1 << 0,      /* macro expansion type */
+	IFILE  = 1 << 1,      /* input file type */
+	ISTDIN = 1 << 2,      /* stdin type */
+	IEOF   = 1 << 3,      /* EOF mark */
+	ITYPE  = IMACRO | IFILE | ISTDIN,
+};
+
 /* data type letters */
 enum ns {
 	L_INT8      = 'C',
@@ -343,12 +351,13 @@
 };
 
 struct input {
+	char flags;
+	unsigned short nline;
 	char *fname;
 	FILE *fp;
 	Symbol *hide;
 	char *line, *begin, *p;
 	struct input *next;
-	unsigned short nline;
 };
 
 /* error.c */
--- a/cc1/lex.c
+++ b/cc1/lex.c
@@ -72,7 +72,7 @@
 addinput(char *fname, Symbol *hide, char *buffer)
 {
 	FILE *fp;
-	unsigned nline = 0;
+	unsigned type, nline = 0;
 	Input *ip;
 
 	if (hide) {
@@ -83,14 +83,17 @@
 		if (hide->hide == UCHAR_MAX)
 			die("Too many macro expansions");
 		++hide->hide;
+		type = IMACRO;
 	} else  if (fname) {
 		/* a new file */
 		if ((fp = fopen(fname, "r")) == NULL)
 			return 0;
+		type = IFILE;
 	} else {
 		/* reading from stdin */
 		fp = stdin;
 		fname = "<stdin>";
+		type = ISTDIN;
 	}
 
 	ip = xmalloc(sizeof(*ip));
@@ -106,6 +109,7 @@
 	ip->fp = fp;
 	ip->hide = hide;
 	ip->nline = nline;
+	ip->flags = type;
 	input = ip;
 
 	return 1;
@@ -117,14 +121,15 @@
 	Input *ip = input;
 	Symbol *hide = ip->hide;
 
-	if (ip->fp) {
+	switch (ip->flags & ITYPE) {
+	case IFILE:
 		if (fclose(ip->fp))
 			die("error: failed to read from input file '%s'",
 			    ip->fname);
 		if (!ip->next)
 			eof = 1;
-	}
-	if (hide) {
+		break;
+	case IMACRO:
 		--hide->hide;
 		/*
 		 * If the symbol is not declared then it was
@@ -131,9 +136,11 @@
 		 * an expansion due to a #if directive with
 		 * a non declared symbol (expanded to 0),
 		 * thus we have to kill the symbol
+		 * TODO: review this comment and code
 		 */
 		if ((hide->flags & SDECLARED) == 0)
 			killsym(hide);
+		break;
 	}
 	if (eof)
 		return;