blob: b4ff8badb60e31ba2d9a53d9c9aa39d555024ece [file] [log] [blame]
H. Peter Anvin22538e22016-05-25 05:42:47 -07001/* ----------------------------------------------------------------------- *
H. Peter Anvinb2047cb2017-03-08 01:26:40 -08002 *
H. Peter Anvin (Intel)1df72632019-01-11 13:13:03 -08003 * Copyright 1996-2019 The NASM Authors - All Rights Reserved
H. Peter Anvin22538e22016-05-25 05:42:47 -07004 * See the file AUTHORS included with the NASM distribution for
5 * the specific copyright holders.
6 *
7 * Redistribution and use in source and binary forms, with or without
8 * modification, are permitted provided that the following
9 * conditions are met:
10 *
11 * * Redistributions of source code must retain the above copyright
12 * notice, this list of conditions and the following disclaimer.
13 * * Redistributions in binary form must reproduce the above
14 * copyright notice, this list of conditions and the following
15 * disclaimer in the documentation and/or other materials provided
16 * with the distribution.
H. Peter Anvinb2047cb2017-03-08 01:26:40 -080017 *
H. Peter Anvin22538e22016-05-25 05:42:47 -070018 * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND
19 * CONTRIBUTORS "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES,
20 * INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF
21 * MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
22 * DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR
23 * CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
24 * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT
25 * NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;
26 * LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
27 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
28 * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR
29 * OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE,
30 * EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
31 *
32 * ----------------------------------------------------------------------- */
33
34/*
H. Peter Anvinb20bc732017-03-07 19:23:03 -080035 * error.c - error message handling routines for the assembler
H. Peter Anvin22538e22016-05-25 05:42:47 -070036 */
37
38#include "compiler.h"
39
H. Peter Anvin22538e22016-05-25 05:42:47 -070040
41#include "nasmlib.h"
H. Peter Anvinb20bc732017-03-07 19:23:03 -080042#include "error.h"
43
Cyrill Gorcunov33510722018-11-24 18:58:11 +030044/* Global error handling function */
45vefunc nasm_verror;
H. Peter Anvin22538e22016-05-25 05:42:47 -070046
H. Peter Anvind351efc2018-12-10 21:53:54 -080047/* Common function body */
48#define nasm_do_error(s) \
49 va_list ap; \
50 va_start(ap, fmt); \
51 nasm_verror((s), fmt, ap); \
52 va_end(ap);
53
H. Peter Anvin (Intel)6bde2ed2018-12-13 19:39:41 -080054void nasm_error(errflags severity, const char *fmt, ...)
H. Peter Anvin22538e22016-05-25 05:42:47 -070055{
H. Peter Anvind351efc2018-12-10 21:53:54 -080056 nasm_do_error(severity);
H. Peter Anvin22538e22016-05-25 05:42:47 -070057}
58
H. Peter Anvind351efc2018-12-10 21:53:54 -080059#define nasm_err_helpers(_type, _name, _sev) \
H. Peter Anvin (Intel)6bde2ed2018-12-13 19:39:41 -080060_type nasm_ ## _name ## f (errflags flags, const char *fmt, ...) \
H. Peter Anvind351efc2018-12-10 21:53:54 -080061{ \
62 nasm_do_error((_sev)|flags); \
63 if (_sev >= ERR_FATAL) \
64 abort(); \
65} \
66_type nasm_ ## _name (const char *fmt, ...) \
67{ \
68 nasm_do_error(_sev); \
69 if (_sev >= ERR_FATAL) \
70 abort(); \
Cyrill Gorcunovc3527dd2018-11-24 22:17:47 +030071}
72
H. Peter Anvind351efc2018-12-10 21:53:54 -080073nasm_err_helpers(void, debug, ERR_DEBUG)
74nasm_err_helpers(void, note, ERR_NOTE)
H. Peter Anvind351efc2018-12-10 21:53:54 -080075nasm_err_helpers(void, nonfatal, ERR_NONFATAL)
76nasm_err_helpers(fatal_func, fatal, ERR_FATAL)
77nasm_err_helpers(fatal_func, panic, ERR_PANIC)
H. Peter Anvin22538e22016-05-25 05:42:47 -070078
H. Peter Anvin (Intel)80c4f232018-12-14 13:33:24 -080079/*
80 * Strongly discourage warnings without level by require flags on warnings.
81 * This means nasm_warn() is the equivalent of the -f variants of the
82 * other ones.
83 */
84void nasm_warn(errflags severity, const char *fmt, ...)
85{
86 nasm_do_error(ERR_WARNING|severity);
87}
H. Peter Anvin (Intel)1df72632019-01-11 13:13:03 -080088
H. Peter Anvin6686fc62018-02-22 14:52:50 -080089fatal_func nasm_panic_from_macro(const char *file, int line)
H. Peter Anvin22538e22016-05-25 05:42:47 -070090{
Cyrill Gorcunov33510722018-11-24 18:58:11 +030091 nasm_panic("internal error at %s:%d\n", file, line);
H. Peter Anvin22538e22016-05-25 05:42:47 -070092}
93
H. Peter Anvin6686fc62018-02-22 14:52:50 -080094fatal_func nasm_assert_failed(const char *file, int line, const char *msg)
H. Peter Anvin22538e22016-05-25 05:42:47 -070095{
Cyrill Gorcunov33510722018-11-24 18:58:11 +030096 nasm_panic("assertion %s failed at %s:%d", msg, file, line);
H. Peter Anvinb2047cb2017-03-08 01:26:40 -080097}
98
H. Peter Anvin (Intel)1df72632019-01-11 13:13:03 -080099
100/*
101 * Warning stack management. Note that there is an implicit "push"
102 * after the command line has been parsed, but this particular push
103 * cannot be popped.
104 */
105struct warning_stack {
106 struct warning_stack *next;
107 uint8_t state[sizeof warning_state];
108};
109static struct warning_stack *warning_stack, *warning_state_init;
110
111/* Push the warning status onto the warning stack */
112void push_warnings(void)
113{
114 struct warning_stack *ws;
115
116 ws = nasm_malloc(sizeof *ws);
117 memcpy(ws->state, warning_state, sizeof warning_state);
118 ws->next = warning_stack;
119 warning_stack = ws;
120}
121
122/* Pop the warning status off the warning stack */
123void pop_warnings(void)
124{
125 struct warning_stack *ws = warning_stack;
126
127 memcpy(warning_state, ws->state, sizeof warning_state);
128 if (!ws->next) {
129 /*!
130 *!warn-stack-empty [on] warning stack empty
131 *! a [WARNING POP] directive was executed when
132 *! the warning stack is empty. This is treated
133 *! as a [WARNING *all] directive.
134 */
135 nasm_warn(WARN_WARN_STACK_EMPTY, "warning stack empty");
136 } else {
137 warning_stack = ws->next;
138 nasm_free(ws);
139 }
140}
141
142/* Call after the command line is parsed, but before the first pass */
143void init_warnings(void)
144{
145 push_warnings();
146 warning_state_init = warning_stack;
147}
148
149
150/* Call after each pass */
151void reset_warnings(void)
152{
153 struct warning_stack *ws = warning_stack;
154
155 /* Unwind the warning stack. We do NOT delete the last entry! */
156 while (ws->next) {
157 struct warning_stack *wst = ws;
158 ws = ws->next;
159 nasm_free(wst);
160 }
161 warning_stack = ws;
162 memcpy(warning_state, ws->state, sizeof warning_state);
163}
164
H. Peter Anvinb2047cb2017-03-08 01:26:40 -0800165/*
166 * This is called when processing a -w or -W option, or a warning directive.
H. Peter Anvin (Intel)eb48c112018-12-12 16:11:08 -0800167 * Returns on if if the action was successful.
H. Peter Anvin (Intel)723ab482018-12-13 21:53:31 -0800168 *
169 * Special pseudo-warnings:
170 *
171 *!other [on] any warning not specifially mentioned above
172 *! specifies any warning not included in any specific warning class.
173 *
174 *!all [all] all possible warnings
H. Peter Anvinfdeb3b02019-06-06 20:53:17 -0700175 *! is an group alias for \e{all} warning classes. Thus, \c{-w+all}
176 *! enables all available warnings, and \c{-w-all} disables warnings
177 *! entirely (since NASM 2.13).
H. Peter Anvinb2047cb2017-03-08 01:26:40 -0800178 */
179bool set_warning_status(const char *value)
180{
Cyrill Gorcunov33510722018-11-24 18:58:11 +0300181 enum warn_action { WID_OFF, WID_ON, WID_RESET };
182 enum warn_action action;
H. Peter Anvinfdeb3b02019-06-06 20:53:17 -0700183 const struct warning_alias *wa;
184 size_t vlen;
Cyrill Gorcunov33510722018-11-24 18:58:11 +0300185 bool ok = false;
186 uint8_t mask;
H. Peter Anvinb2047cb2017-03-08 01:26:40 -0800187
Cyrill Gorcunov33510722018-11-24 18:58:11 +0300188 value = nasm_skip_spaces(value);
H. Peter Anvin (Intel)1df72632019-01-11 13:13:03 -0800189
Cyrill Gorcunov33510722018-11-24 18:58:11 +0300190 switch (*value) {
191 case '-':
192 action = WID_OFF;
193 value++;
194 break;
195 case '+':
196 action = WID_ON;
197 value++;
198 break;
199 case '*':
200 action = WID_RESET;
201 value++;
202 break;
203 case 'N':
204 case 'n':
205 if (!nasm_strnicmp(value, "no-", 3)) {
206 action = WID_OFF;
207 value += 3;
208 break;
209 } else if (!nasm_stricmp(value, "none")) {
210 action = WID_OFF;
211 value = NULL;
212 break;
213 }
214 /* else fall through */
215 default:
216 action = WID_ON;
217 break;
218 }
H. Peter Anvinb2047cb2017-03-08 01:26:40 -0800219
Cyrill Gorcunov33510722018-11-24 18:58:11 +0300220 mask = WARN_ST_ENABLED;
H. Peter Anvinb2047cb2017-03-08 01:26:40 -0800221
Cyrill Gorcunov33510722018-11-24 18:58:11 +0300222 if (value && !nasm_strnicmp(value, "error", 5)) {
223 switch (value[5]) {
224 case '=':
225 mask = WARN_ST_ERROR;
226 value += 6;
227 break;
228 case '\0':
229 mask = WARN_ST_ERROR;
230 value = NULL;
231 break;
232 default:
233 /* Just an accidental prefix? */
234 break;
235 }
236 }
H. Peter Anvinb2047cb2017-03-08 01:26:40 -0800237
Cyrill Gorcunov33510722018-11-24 18:58:11 +0300238 if (value && !nasm_stricmp(value, "all"))
239 value = NULL;
H. Peter Anvinb2047cb2017-03-08 01:26:40 -0800240
H. Peter Anvinfdeb3b02019-06-06 20:53:17 -0700241 vlen = value ? strlen(value) : 0;
H. Peter Anvinb2047cb2017-03-08 01:26:40 -0800242
H. Peter Anvinfdeb3b02019-06-06 20:53:17 -0700243 /* This is inefficient, but it shouldn't matter... */
244 for (wa = warning_alias; wa < &warning_alias[NUM_WARNING_ALIAS]; wa++) {
245 enum warn_index i = wa->warning;
246
247 if (value) {
248 char sep;
249
250 if (nasm_strnicmp(value, wa->name, vlen))
251 continue; /* Not a prefix */
252
253 sep = wa->name[vlen];
254 if (sep != '\0' && sep != '-')
255 continue; /* Not a valid prefix */
256 }
257
258 ok = true; /* At least one action taken */
259 switch (action) {
260 case WID_OFF:
261 warning_state[i] &= ~mask;
262 break;
263 case WID_ON:
264 warning_state[i] |= mask;
265 break;
266 case WID_RESET:
267 warning_state[i] &= ~mask;
268 warning_state[i] |= warning_state_init->state[i] & mask;
269 break;
270 }
271 }
272
273 if (!ok && value) {
274 /*!
275 *!unknown-warning [off] unknown warning in -W/-w or warning directive
276 *! warns about a \c{-w} or \c{-W} option or a \c{[WARNING]} directive
277 *! that contains an unknown warning name or is otherwise not possible to process.
278 */
279 nasm_warn(WARN_UNKNOWN_WARNING, "unknown warning name: %s", value);
H. Peter Anvin (Intel)723ab482018-12-13 21:53:31 -0800280 }
H. Peter Anvin (Intel)1df72632019-01-11 13:13:03 -0800281
Cyrill Gorcunov33510722018-11-24 18:58:11 +0300282 return ok;
H. Peter Anvin22538e22016-05-25 05:42:47 -0700283}