From a039fcdb46b70a0645d6747b56b6a075b6ba9fc5 Mon Sep 17 00:00:00 2001 From: "H. Peter Anvin" Date: Thu, 12 Sep 2019 19:27:42 -0700 Subject: [PATCH] preproc: move %use package parsing to a separate routine Move the parsing of %use package names to a separate routine, and stop using get_id() for that purpose -- get_id() is wrong in a number of ways. This also means we can drop the error string argument to get_id(). Signed-off-by: H. Peter Anvin --- asm/preproc.c | 70 ++++++++++++++++++++++++++++++++++++++------------- 1 file changed, 52 insertions(+), 18 deletions(-) diff --git a/asm/preproc.c b/asm/preproc.c index 42cb969e..1828c8a8 100644 --- a/asm/preproc.c +++ b/asm/preproc.c @@ -2739,7 +2739,7 @@ static bool is_macro_id(const Token *t) (t->type == TOK_PREPROC_ID && t->text[1] == '$')); } -static char *get_id(Token **tp, const char *dname, const char *err) +static char *get_id(Token **tp, const char *dname) { char *id; Token *t = *tp; @@ -2749,8 +2749,7 @@ static char *get_id(Token **tp, const char *dname, const char *err) t = expand_id(t); if (!is_macro_id(t)) { - nasm_nonfatal("`%s' expects a %s", dname, - err ? err : "macro identifier"); + nasm_nonfatal("`%s' expects a macro identifier", dname); return NULL; } @@ -2760,6 +2759,43 @@ static char *get_id(Token **tp, const char *dname, const char *err) return id; } +/* Parse a %use package name and find the package. Set *err on syntax error. */ +static const struct use_package * +get_use_pkg(Token *t, const char *dname, bool *err) +{ + char *id; + + *err = false; + + t = t->next; /* Skip directive */ + skip_white_(t); + + t = expand_smacro(t); + + id = NULL; + if (t) { + if (t->type == TOK_ID) { + id = t->text; + } else if (t->type == TOK_STRING) { + nasm_unquote_cstr(t->text, NULL); + id = t->text; + } + } + + if (!id) { + nasm_nonfatal("`%s' expects a package name", dname); + *err = true; + return NULL; + } + + t = t->next; + skip_white_(t); + if (t) + nasm_warn(WARN_OTHER, "trailing garbage after `%s' ignored", dname); + + return nasm_find_use_package(id); +} + /** * find and process preprocessor directive in passed line * Find out if a line contains a preprocessor directive, and deal @@ -3150,14 +3186,12 @@ static int do_directive(Token *tline, Token **output) case PP_USE: { const struct use_package *pkg; + bool err; - if (!(mname = get_id(&tline, dname, "package name"))) + pkg = get_use_pkg(tline, dname, &err); + if (err) goto done; - if (tline->next) - nasm_warn(WARN_OTHER, "trailing garbage after `%s' ignored", dname); - if (tline->type == TOK_STRING) - nasm_unquote_cstr(tline->text, NULL); - pkg = nasm_find_use_package(tline->text); + if (!pkg) { nasm_nonfatal("unknown `%s' package: %s", dname, tline->text); } else if (!use_loaded[pkg->index]) { @@ -3608,7 +3642,7 @@ issue_error: SMacro tmpl; Token **lastp; - if (!(mname = get_id(&tline, dname, NULL))) + if (!(mname = get_id(&tline, dname))) goto done; nasm_zero(tmpl); @@ -3673,7 +3707,7 @@ issue_error: case PP_UNDEF: case PP_UNDEFALIAS: - if (!(mname = get_id(&tline, dname, NULL))) + if (!(mname = get_id(&tline, dname))) goto done; if (tline->next) nasm_warn(WARN_OTHER, "trailing garbage after macro name ignored"); @@ -3682,7 +3716,7 @@ issue_error: break; case PP_DEFSTR: - if (!(mname = get_id(&tline, dname, NULL))) + if (!(mname = get_id(&tline, dname))) goto done; last = tline; @@ -3705,7 +3739,7 @@ issue_error: break; case PP_DEFTOK: - if (!(mname = get_id(&tline, dname, NULL))) + if (!(mname = get_id(&tline, dname))) goto done; last = tline; @@ -3743,7 +3777,7 @@ issue_error: { const char *found_path; - if (!(mname = get_id(&tline, dname, NULL))) + if (!(mname = get_id(&tline, dname))) goto done; last = tline; @@ -3782,7 +3816,7 @@ issue_error: } case PP_STRLEN: - if (!(mname = get_id(&tline, dname, NULL))) + if (!(mname = get_id(&tline, dname))) goto done; last = tline; @@ -3813,7 +3847,7 @@ issue_error: return DIRECTIVE_FOUND; case PP_STRCAT: - if (!(mname = get_id(&tline, dname, NULL))) + if (!(mname = get_id(&tline, dname))) goto done; last = tline; @@ -3863,7 +3897,7 @@ issue_error: int64_t start, count; size_t len; - if (!(mname = get_id(&tline, dname, NULL))) + if (!(mname = get_id(&tline, dname))) goto done; last = tline; @@ -3942,7 +3976,7 @@ issue_error: } case PP_ASSIGN: - if (!(mname = get_id(&tline, dname, NULL))) + if (!(mname = get_id(&tline, dname))) goto done; last = tline;