Mercurial > ~dholland > hg > tradcpp > index.cgi
changeset 154:a2c2fe8dbea3
Wrap up the current and next line position when invoking directives.
(in preparation for implementing #line)
author | David A. Holland |
---|---|
date | Fri, 12 Jun 2015 00:56:12 -0400 |
parents | 28ac21a359d1 |
children | e6eb15635a48 f14f5352956c |
files | directive.c directive.h files.c |
diffstat | 3 files changed, 67 insertions(+), 55 deletions(-) [+] |
line wrap: on
line diff
--- a/directive.c Fri Jun 12 00:35:46 2015 -0400 +++ b/directive.c Fri Jun 12 00:56:12 2015 -0400 @@ -175,7 +175,7 @@ static void -d_if(struct place *p, struct place *p2, char *line) +d_if(struct lineplace *lp, struct place *p2, char *line) { char *expr; bool val; @@ -194,38 +194,38 @@ } else { val = 0; } - ifstate_push(p, val); + ifstate_push(&lp->current, val); dostrfree(expr); } static void -d_ifdef(struct place *p, struct place *p2, char *line) +d_ifdef(struct lineplace *lp, struct place *p2, char *line) { uncomment(line); oneword("#ifdef", p2, line); - ifstate_push(p, macro_isdefined(line)); + ifstate_push(&lp->current, macro_isdefined(line)); } static void -d_ifndef(struct place *p, struct place *p2, char *line) +d_ifndef(struct lineplace *lp, struct place *p2, char *line) { uncomment(line); oneword("#ifndef", p2, line); - ifstate_push(p, !macro_isdefined(line)); + ifstate_push(&lp->current, !macro_isdefined(line)); } static void -d_elif(struct place *p, struct place *p2, char *line) +d_elif(struct lineplace *lp, struct place *p2, char *line) { char *expr; struct place p3 = *p2; size_t oldlen; if (ifstate->seenelse) { - complain(p, "#elif after #else"); + complain(&lp->current, "#elif after #else"); complain_fail(); } @@ -247,13 +247,14 @@ static void -d_else(struct place *p, struct place *p2, char *line) +d_else(struct lineplace *lp, struct place *p2, char *line) { (void)p2; (void)line; if (ifstate->seenelse) { - complain(p, "Multiple #else directives in one conditional"); + complain(&lp->current, + "Multiple #else directives in one conditional"); complain_fail(); } @@ -264,13 +265,13 @@ static void -d_endif(struct place *p, struct place *p2, char *line) +d_endif(struct lineplace *lp, struct place *p2, char *line) { (void)p2; (void)line; if (ifstate->prev == NULL) { - complain(p, "Unmatched #endif"); + complain(&lp->current, "Unmatched #endif"); complain_fail(); } else { ifstate_pop(); @@ -282,12 +283,12 @@ static void -d_define(struct place *p, struct place *p2, char *line) +d_define(struct lineplace *lp, struct place *p2, char *line) { size_t pos, argpos; struct place p3, p4; - (void)p; + (void)lp; /* * line may be: @@ -347,9 +348,9 @@ static void -d_undef(struct place *p, struct place *p2, char *line) +d_undef(struct lineplace *lp, struct place *p2, char *line) { - (void)p; + (void)lp; uncomment(line); oneword("#undef", p2, line); @@ -383,13 +384,13 @@ static void -d_include(struct place *p, struct place *p2, char *line) +d_include(struct lineplace *lp, struct place *p2, char *line) { char *text; size_t oldlen; uncomment(line); - if (tryinclude(p, line)) { + if (tryinclude(&lp->current, line)) { return; } text = macroexpand(p2, line, strlen(line), false); @@ -399,26 +400,26 @@ /* trim to fit, so the malloc debugging won't complain */ text = dorealloc(text, oldlen + 1, strlen(text) + 1); - if (tryinclude(p, text)) { + if (tryinclude(&lp->current, text)) { dostrfree(text); return; } - complain(p, "Illegal #include directive"); - complain(p, "Before macro expansion: #include %s", line); - complain(p, "After macro expansion: #include %s", text); + complain(&lp->current, "Illegal #include directive"); + complain(&lp->current, "Before macro expansion: #include %s", line); + complain(&lp->current, "After macro expansion: #include %s", text); dostrfree(text); complain_fail(); } static void -d_line(struct place *p, struct place *p2, char *line) +d_line(struct lineplace *lp, struct place *p2, char *line) { (void)p2; (void)line; /* XXX */ - complain(p, "Sorry, no #line yet"); + complain(&lp->current, "Sorry, no #line yet"); } //////////////////////////////////////////////////////////// @@ -426,12 +427,12 @@ static void -d_warning(struct place *p, struct place *p2, char *line) +d_warning(struct lineplace *lp, struct place *p2, char *line) { char *msg; msg = macroexpand(p2, line, strlen(line), false); - complain(p, "#warning: %s", msg); + complain(&lp->current, "#warning: %s", msg); if (mode.werror) { complain_fail(); } @@ -440,12 +441,12 @@ static void -d_error(struct place *p, struct place *p2, char *line) +d_error(struct lineplace *lp, struct place *p2, char *line) { char *msg; msg = macroexpand(p2, line, strlen(line), false); - complain(p, "#error: %s", msg); + complain(&lp->current, "#error: %s", msg); complain_fail(); dostrfree(msg); } @@ -455,11 +456,11 @@ static void -d_pragma(struct place *p, struct place *p2, char *line) +d_pragma(struct lineplace *lp, struct place *p2, char *line) { (void)p2; - complain(p, "#pragma %s", line); + complain(&lp->current, "#pragma %s", line); complain_fail(); } @@ -469,7 +470,7 @@ static const struct { const char *name; bool ifskip; - void (*func)(struct place *, struct place *, char *line); + void (*func)(struct lineplace *, struct place *, char *line); } directives[] = { { "define", true, d_define }, { "elif", false, d_elif }, @@ -489,13 +490,13 @@ static void -directive_gotdirective(struct place *p, char *line) +directive_gotdirective(struct lineplace *lp, char *line) { struct place p2; size_t len, skip; unsigned i; - p2 = *p; + p2 = lp->current; for (i=0; i<numdirectives; i++) { len = strlen(directives[i].name); if (!strncmp(line, directives[i].name, len) && @@ -512,7 +513,7 @@ if (len < strlen(line)) { line[len] = '\0'; } - directives[i].func(p, &p2, line); + directives[i].func(lp, &p2, line); return; } } @@ -523,7 +524,7 @@ } skip = strcspn(line, ws); - complain(p, "Unknown directive #%.*s", (int)skip, line); + complain(&lp->current, "Unknown directive #%.*s", (int)skip, line); complain_fail(); } @@ -532,13 +533,13 @@ */ static size_t -directive_scancomments(const struct place *p, char *line, size_t len) +directive_scancomments(const struct lineplace *lp, char *line, size_t len) { size_t pos; bool incomment; struct place p2; - p2 = *p; + p2 = lp->current; incomment = 0; for (pos = 0; pos+1 < len; pos++) { if (line[pos] == '/' && line[pos+1] == '*') { @@ -574,25 +575,25 @@ } void -directive_gotline(struct place *p, char *line, size_t len) +directive_gotline(struct lineplace *lp, char *line, size_t len) { size_t skip; if (warns.nestcomment) { - directive_scancomments(p, line, len); + directive_scancomments(lp, line, len); } /* check if we have a directive line (# exactly in column 0) */ if (line[0] == '#') { skip = 1 + strspn(line + 1, ws); assert(skip <= len); - p->column += skip; + lp->current.column += skip; assert(line[len] == '\0'); - directive_gotdirective(p, line+skip /*, length = len-skip */); - p->column += len-skip; + directive_gotdirective(lp, line+skip /*, length = len-skip */); + lp->current.column += len-skip; } else if (ifstate->curtrue) { - macro_sendline(p, line, len); - p->column += len; + macro_sendline(&lp->current, line, len); + lp->current.column += len; } }
--- a/directive.h Fri Jun 12 00:35:46 2015 -0400 +++ b/directive.h Fri Jun 12 00:56:12 2015 -0400 @@ -29,11 +29,21 @@ #include <stddef.h> -struct place; +#include "place.h" + +/* + * Relevant places while we're processing a line: + * the place in the current line + * the beginning of the next line + */ +struct lineplace { + struct place current; + struct place nextline; +}; void directive_init(void); void directive_cleanup(void); -void directive_gotline(struct place *p, char *line, size_t len); +void directive_gotline(struct lineplace *lp, char *line, size_t len); void directive_goteof(struct place *p);
--- a/files.c Fri Jun 12 00:35:46 2015 -0400 +++ b/files.c Fri Jun 12 00:56:12 2015 -0400 @@ -172,14 +172,15 @@ void file_read(const struct placefile *pf, int fd, const char *name, bool toplevel) { - struct place linestartplace, nextlinestartplace, ptmp; + struct lineplace places; + struct place ptmp; size_t bufend, bufmax, linestart, lineend, nextlinestart, tmp; ssize_t result; bool ateof = false; char *buf; - place_setfilestart(&linestartplace, pf); - nextlinestartplace = linestartplace; + place_setfilestart(&places.current, pf); + places.nextline = places.current; bufmax = 128; bufend = 0; @@ -223,7 +224,7 @@ } else if (result == 0) { /* eof in middle of line */ ateof = true; - ptmp = linestartplace; + ptmp = places.current; ptmp.column += bufend - linestart; complain(&ptmp, "No newline at end of file"); if (mode.werror) { @@ -244,7 +245,7 @@ assert(buf[lineend] == '\n'); buf[lineend] = '\0'; nextlinestart = lineend+1; - nextlinestartplace.line++; + places.nextline.line++; /* check for CR/NL */ if (lineend > 0 && buf[lineend-1] == '\r') { @@ -271,21 +272,21 @@ assert(buf[lineend] == '\0'); /* count how many commented-out newlines we swallowed */ - nextlinestartplace.line += countnls(buf, linestart, lineend); + places.nextline.line += countnls(buf, linestart, lineend); /* if the line isn't empty, process it */ if (lineend > linestart) { - directive_gotline(&linestartplace, + directive_gotline(&places, buf+linestart, lineend-linestart); } linestart = nextlinestart; lineend = findeol(buf, linestart, bufend); - linestartplace = nextlinestartplace; + places.current = places.nextline; } if (toplevel) { - directive_goteof(&linestartplace); + directive_goteof(&places.current); } dofree(buf, bufmax); }