Add error message for bad line directives to ivlpp

This commit is contained in:
Cary R 2023-09-04 12:37:06 -07:00
parent 7ce068fbdb
commit 76a65d5958
2 changed files with 41 additions and 16 deletions

View File

@ -728,21 +728,25 @@ keywords (line|include|define|undef|ifdef|ifndef|else|elsif|endif)
*/ */
static void handle_line_directive(void) static void handle_line_directive(void)
{ {
char *cp;
char *cpr; char *cpr;
unsigned long lineno;
char*fn_start;
char*fn_end;
/* Skip any leading space. */ /* Skip any leading space. */
cp = strchr(yytext, '`'); char *cp = strchr(yytext, '`');
/* Skip the `line directive. */ /* Skip the `line directive. */
assert(strncmp(cp, "`line", 5) == 0); assert(strncmp(cp, "`line", 5) == 0);
cp += 5; cp += 5;
/* strtoul skips leading space. */ /* strtol skips leading space. */
lineno = strtoul(cp, &cpr, 10); long lineno = strtol(cp, &cpr, 10);
if (cp == cpr || lineno == 0) { if (cp == cpr || lineno == 0) {
emit_pathline(istack);
fprintf(stderr, "error: Invalid line number for `line directive.\n");
error_count += 1;
return;
}
if (lineno < 1) {
emit_pathline(istack);
fprintf(stderr, "error: Line number for `line directive most be greater than zero.\n");
error_count += 1;
return; return;
} }
cp = cpr; cp = cpr;
@ -750,20 +754,30 @@ static void handle_line_directive(void)
/* Skip the space between the line number and the file name. */ /* Skip the space between the line number and the file name. */
cpr += strspn(cp, " \t"); cpr += strspn(cp, " \t");
if (cp == cpr) { if (cp == cpr) {
emit_pathline(istack);
fprintf(stderr, "error: Invalid `line directive (missing space "
"after line number).\n");
error_count += 1;
return; return;
} }
cp = cpr; cp = cpr;
/* Find the starting " and skip it. */ /* Find the starting " and skip it. */
fn_start = strchr(cp, '"'); char *fn_start = strchr(cp, '"');
if (cp != fn_start) { if (cp != fn_start) {
emit_pathline(istack);
fprintf(stderr, "error: Invalid `line directive (file name start).\n");
error_count += 1;
return; return;
} }
fn_start += 1; fn_start += 1;
/* Find the last ". */ /* Find the last ". */
fn_end = strrchr(fn_start, '"'); char *fn_end = strrchr(fn_start, '"');
if (!fn_end) { if (!fn_end) {
emit_pathline(istack);
fprintf(stderr, "error: Invalid `line directive (file name end).\n");
error_count += 1;
return; return;
} }
@ -772,19 +786,31 @@ static void handle_line_directive(void)
cpr = cp; cpr = cp;
cpr += strspn(cp, " \t"); cpr += strspn(cp, " \t");
if (cp == cpr) { if (cp == cpr) {
emit_pathline(istack);
fprintf(stderr, "error: Invalid `line directive (missing space "
"after file name).\n");
error_count += 1;
return; return;
} }
cp = cpr; cp = cpr;
/* Check that the level is correct, we do not need the level. */ /* Check that the level is correct, we do not need the level. */
if (strspn(cp, "012") != 1) { if (strspn(cp, "012") != 1) {
emit_pathline(istack);
fprintf(stderr, "error: Invalid level for `line directive\n");
error_count += 1;
return; return;
} }
cp += 1; cp += 1;
/* Verify that only space is left. */ /* Verify that only space is left. */
cp += strspn(cp, " \t"); cp += strspn(cp, " \t");
if ((size_t)(cp-yytext) != strlen(yytext)) { if (strncmp(cp, "//", 2) != 0 &&
(size_t)(cp-yytext) != strlen(yytext)) {
emit_pathline(istack);
fprintf(stderr, "error: Invalid `line directive (extra garbage "
"after level).\n");
error_count += 1;
return; return;
} }

View File

@ -1451,7 +1451,7 @@ static void line_directive()
cp += strspn(cp, " \t"); cp += strspn(cp, " \t");
/* Find the starting " and skip it. */ /* Find the starting " and skip it. */
char*fn_start = strchr(cp, '"'); char *fn_start = strchr(cp, '"');
if (cp != fn_start) { if (cp != fn_start) {
VLerror(yylloc, "error: Invalid #line directive (file name start)."); VLerror(yylloc, "error: Invalid #line directive (file name start).");
return; return;
@ -1459,7 +1459,7 @@ static void line_directive()
fn_start += 1; fn_start += 1;
/* Find the last ". */ /* Find the last ". */
char*fn_end = strrchr(fn_start, '"'); char *fn_end = strrchr(fn_start, '"');
if (!fn_end) { if (!fn_end) {
VLerror(yylloc, "error: Invalid #line directive (file name end)."); VLerror(yylloc, "error: Invalid #line directive (file name end).");
return; return;
@ -1530,7 +1530,6 @@ static void line_directive2()
VLerror(yylloc, "error: Line number for `line directive most be greater than zero."); VLerror(yylloc, "error: Line number for `line directive most be greater than zero.");
return; return;
} }
lineno -= 1;
cp = cpr; cp = cpr;
/* Skip the space between the line number and the file name. */ /* Skip the space between the line number and the file name. */
@ -1543,7 +1542,7 @@ static void line_directive2()
cp = cpr; cp = cpr;
/* Find the starting " and skip it. */ /* Find the starting " and skip it. */
char*fn_start = strchr(cp, '"'); char *fn_start = strchr(cp, '"');
if (cp != fn_start) { if (cp != fn_start) {
VLerror(yylloc, "error: Invalid `line directive (file name start)."); VLerror(yylloc, "error: Invalid `line directive (file name start).");
return; return;
@ -1590,7 +1589,7 @@ static void line_directive2()
buf[fn_end-fn_start] = 0; buf[fn_end-fn_start] = 0;
yylloc.text = set_file_name(buf); yylloc.text = set_file_name(buf);
yylloc.first_line = lineno; yylloc.first_line = lineno-1;
} }
/* /*