[hackers] [scc] [cc1] Add type field to input structure || Roberto E. Vargas Caballero

From: <git_AT_suckless.org>
Date: Thu, 19 Jan 2017 10:00:32 +0100 (CET)

commit 7525bdb75be193b54dce97849fe81e5df378584e
Author: Roberto E. Vargas Caballero <k0ga_AT_shike2.com>
AuthorDate: Wed Jan 18 11:16:06 2017 +0100
Commit: Roberto E. Vargas Caballero <k0ga_AT_shike2.com>
CommitDate: Wed Jan 18 12:23:37 2017 +0100

    [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.

diff --git a/cc1/cc1.h b/cc1/cc1.h
index a199cca..3f3020f 100644
--- a/cc1/cc1.h
+++ b/cc1/cc1.h
_AT_@ -23,6 +23,14 @@ enum typeprops {
         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',
_AT_@ -343,12 +351,13 @@ struct yystype {
 };
 
 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 */
diff --git a/cc1/lex.c b/cc1/lex.c
index 6fb6df1..4c0fd35 100644
--- a/cc1/lex.c
+++ b/cc1/lex.c
_AT_@ -72,7 +72,7 @@ int
 addinput(char *fname, Symbol *hide, char *buffer)
 {
         FILE *fp;
- unsigned nline = 0;
+ unsigned type, nline = 0;
         Input *ip;
 
         if (hide) {
_AT_@ -83,14 +83,17 @@ addinput(char *fname, Symbol *hide, char *buffer)
                 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));
_AT_@ -106,6 +109,7 @@ addinput(char *fname, Symbol *hide, char *buffer)
         ip->fp = fp;
         ip->hide = hide;
         ip->nline = nline;
+ ip->flags = type;
         input = ip;
 
         return 1;
_AT_@ -117,23 +121,26 @@ delinput(void)
         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
                  * 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;
Received on Thu Jan 19 2017 - 10:00:32 CET

This archive was generated by hypermail 2.3.0 : Thu Jan 19 2017 - 10:12:20 CET