From 7df0417e58fa3cad48cc457e7238a4940ffe79bc Mon Sep 17 00:00:00 2001 From: "H. Peter Anvin" Date: Tue, 10 Jun 2008 18:27:38 -0700 Subject: [PATCH] Add %warning, saner unquoting of %error - Add %warning directive - Only unquote an %error or %warning string if it is the only thing on the directive line. - Don't expand macros inside a quoted string, even for %error. --- doc/nasmdoc.src | 22 ++++++++++++++++------ nasm.c | 4 +++- nasmlib.h | 1 + pptok.dat | 1 + preproc.c | 24 ++++++++++++++++-------- 5 files changed, 37 insertions(+), 15 deletions(-) diff --git a/doc/nasmdoc.src b/doc/nasmdoc.src index f0b0b2bf..d8717cba 100644 --- a/doc/nasmdoc.src +++ b/doc/nasmdoc.src @@ -2874,19 +2874,19 @@ any tokens at all, whitespace excepted. The usual \i\c{%elifempty}, \i\c\{%ifnempty}, and \i\c{%elifnempty} variants are also provided. -\S{pperror} \i\c{%error}: Reporting \i{User-Defined Errors} +\S{pperror} \i\c{%error} and \i\c{%warning}: Reporting \i{User-Defined Errors} The preprocessor directive \c{%error} will cause NASM to report an error if it occurs in assembled code. So if other users are going to -try to assemble your source files, you can ensure that they define -the right macros by means of code like this: +try to assemble your source files, you can ensure that they define the +right macros by means of code like this: -\c %ifdef SOME_MACRO +\c %ifdef F1 \c ; do some setup -\c %elifdef SOME_OTHER_MACRO +\c %elifdef F2 \c ; do some different setup \c %else -\c %error Neither SOME_MACRO nor SOME_OTHER_MACRO was defined. +\c %error Neither F1 nor F2 was defined. \c %endif Then any user who fails to understand the way your code is supposed @@ -2894,6 +2894,16 @@ to be assembled will be quickly warned of their mistake, rather than having to wait until the program crashes on being run and then not knowing what went wrong. +Similarly, \c{%warning} issues a warning: + +\c %ifdef F1 +\c ; do some setup +\c %elifdef F2 +\c ; do some different setup +\c %else +\c %warning Neither F1 nor F2 was defined, assuming F1. +\c %define F1 +\c %endif \H{rep} \i{Preprocessor Loops}\I{repeating code}: \i\c{%rep} diff --git a/nasm.c b/nasm.c index 6ce45d95..a8efbb2b 100644 --- a/nasm.c +++ b/nasm.c @@ -1909,7 +1909,7 @@ static bool is_suppressed_warning(int severity) static void report_error_common(int severity, const char *fmt, va_list args) { - switch (severity & ERR_MASK) { + switch (severity & (ERR_MASK|ERR_NO_SEVERITY)) { case ERR_WARNING: fputs("warning: ", error_file); break; @@ -1925,6 +1925,8 @@ static void report_error_common(int severity, const char *fmt, case ERR_DEBUG: fputs("debug: ", error_file); break; + default: + break; } vfprintf(error_file, fmt, args); diff --git a/nasmlib.h b/nasmlib.h index 3fa51279..837d679c 100644 --- a/nasmlib.h +++ b/nasmlib.h @@ -54,6 +54,7 @@ extern efunc nasm_malloc_error; #define ERR_NOFILE 0x00000010 /* don't give source file name/line */ #define ERR_USAGE 0x00000020 /* print a usage message */ #define ERR_PASS1 0x00000040 /* only print this error on pass one */ +#define ERR_NO_SEVERITY 0x00000080 /* suppress printing severity */ /* * These codes define specific types of suppressible warning. diff --git a/pptok.dat b/pptok.dat index 1396eef2..78a975fd 100644 --- a/pptok.dat +++ b/pptok.dat @@ -47,4 +47,5 @@ %strlen %substr %undef +%warning %xdefine diff --git a/preproc.c b/preproc.c index ec45bd42..04e50b15 100644 --- a/preproc.c +++ b/preproc.c @@ -2192,22 +2192,30 @@ static int do_directive(Token * tline) break; case PP_ERROR: + case PP_WARNING: + { + int severity = PP_ERROR ? ERR_NONFATAL|ERR_NO_SEVERITY : + ERR_WARNING|ERR_NO_SEVERITY; + tline->next = expand_smacro(tline->next); tline = tline->next; skip_white_(tline); - if (tok_type_(tline, TOK_STRING)) { + t = tline ? tline->next : NULL; + skip_white_(t); + if (tok_type_(tline, TOK_STRING) && !t) { + /* The line contains only a quoted string */ p = tline->text; nasm_unquote(p, NULL); - expand_macros_in_string(&p); /* WHY? */ - error(ERR_NONFATAL, "%s", p); - nasm_free(p); - } else { + error(severity, "%s: %s", pp_directives[i], p); + } else { + /* Not a quoted string, or more than a quoted string */ p = detoken(tline, false); - error(ERR_WARNING, "%s", p); /* WARNING!??!! */ - nasm_free(p); - } + error(severity, "%s: %s", pp_directives[i], p); + nasm_free(p); + } free_tlist(origline); break; + } CASE_PP_IF: if (istk->conds && !emitting(istk->conds->state)) -- 2.11.4.GIT