blob: 52f812b89edcec2a5c51dad2caca9cb0d80f7f85 [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
H. Peter Anvin6a4353c2019-08-28 18:32:46 -070044/*
45 * Global error handling function. If we call this before it is
46 * initialized, it is a fatal error!
47 */
48vefunc nasm_verror = (vefunc)nasm_verror_critical;
H. Peter Anvin22538e22016-05-25 05:42:47 -070049
H. Peter Anvind351efc2018-12-10 21:53:54 -080050/* Common function body */
H. Peter Anvin6a4353c2019-08-28 18:32:46 -070051#define nasm_do_error(_sev,_flags) \
52 va_list ap; \
53 va_start(ap, fmt); \
54 if ((_sev) >= ERR_CRITICAL) \
55 nasm_verror_critical((_sev)|(_flags), fmt, ap); \
56 else \
57 nasm_verror((_sev)|(_flags), fmt, ap); \
58 va_end(ap); \
59 if ((_sev) >= ERR_FATAL) \
60 abort();
61
H. Peter Anvind351efc2018-12-10 21:53:54 -080062
H. Peter Anvin (Intel)6bde2ed2018-12-13 19:39:41 -080063void nasm_error(errflags severity, const char *fmt, ...)
H. Peter Anvin22538e22016-05-25 05:42:47 -070064{
H. Peter Anvin6a4353c2019-08-28 18:32:46 -070065 nasm_do_error(severity & ERR_MASK, severity & ~ERR_MASK);
H. Peter Anvin22538e22016-05-25 05:42:47 -070066}
67
H. Peter Anvind351efc2018-12-10 21:53:54 -080068#define nasm_err_helpers(_type, _name, _sev) \
H. Peter Anvin (Intel)6bde2ed2018-12-13 19:39:41 -080069_type nasm_ ## _name ## f (errflags flags, const char *fmt, ...) \
H. Peter Anvind351efc2018-12-10 21:53:54 -080070{ \
H. Peter Anvin6a4353c2019-08-28 18:32:46 -070071 nasm_do_error(_sev, flags); \
H. Peter Anvind351efc2018-12-10 21:53:54 -080072} \
73_type nasm_ ## _name (const char *fmt, ...) \
74{ \
H. Peter Anvin6a4353c2019-08-28 18:32:46 -070075 nasm_do_error(_sev, 0); \
Cyrill Gorcunovc3527dd2018-11-24 22:17:47 +030076}
77
H. Peter Anvin (Intel)d66927a2019-08-09 04:28:55 -070078nasm_err_helpers(void, listmsg, ERR_LISTMSG)
H. Peter Anvind351efc2018-12-10 21:53:54 -080079nasm_err_helpers(void, debug, ERR_DEBUG)
H. Peter Anvin (Intel)d66927a2019-08-09 04:28:55 -070080nasm_err_helpers(void, info, ERR_INFO)
H. Peter Anvind351efc2018-12-10 21:53:54 -080081nasm_err_helpers(void, nonfatal, ERR_NONFATAL)
82nasm_err_helpers(fatal_func, fatal, ERR_FATAL)
H. Peter Anvin6a4353c2019-08-28 18:32:46 -070083nasm_err_helpers(fatal_func, critical, ERR_CRITICAL)
H. Peter Anvind351efc2018-12-10 21:53:54 -080084nasm_err_helpers(fatal_func, panic, ERR_PANIC)
H. Peter Anvin22538e22016-05-25 05:42:47 -070085
H. Peter Anvin (Intel)80c4f232018-12-14 13:33:24 -080086/*
87 * Strongly discourage warnings without level by require flags on warnings.
88 * This means nasm_warn() is the equivalent of the -f variants of the
89 * other ones.
90 */
H. Peter Anvin6a4353c2019-08-28 18:32:46 -070091void nasm_warn(errflags flags, const char *fmt, ...)
H. Peter Anvin (Intel)80c4f232018-12-14 13:33:24 -080092{
H. Peter Anvin6a4353c2019-08-28 18:32:46 -070093 nasm_do_error(ERR_WARNING, flags);
H. Peter Anvin (Intel)80c4f232018-12-14 13:33:24 -080094}
H. Peter Anvin (Intel)1df72632019-01-11 13:13:03 -080095
H. Peter Anvin6686fc62018-02-22 14:52:50 -080096fatal_func nasm_panic_from_macro(const char *file, int line)
H. Peter Anvin22538e22016-05-25 05:42:47 -070097{
Cyrill Gorcunov33510722018-11-24 18:58:11 +030098 nasm_panic("internal error at %s:%d\n", file, line);
H. Peter Anvin22538e22016-05-25 05:42:47 -070099}
100
H. Peter Anvin6686fc62018-02-22 14:52:50 -0800101fatal_func nasm_assert_failed(const char *file, int line, const char *msg)
H. Peter Anvin22538e22016-05-25 05:42:47 -0700102{
Cyrill Gorcunov33510722018-11-24 18:58:11 +0300103 nasm_panic("assertion %s failed at %s:%d", msg, file, line);
H. Peter Anvinb2047cb2017-03-08 01:26:40 -0800104}
105
H. Peter Anvin (Intel)1df72632019-01-11 13:13:03 -0800106
107/*
108 * Warning stack management. Note that there is an implicit "push"
109 * after the command line has been parsed, but this particular push
110 * cannot be popped.
111 */
112struct warning_stack {
113 struct warning_stack *next;
114 uint8_t state[sizeof warning_state];
115};
116static struct warning_stack *warning_stack, *warning_state_init;
117
118/* Push the warning status onto the warning stack */
119void push_warnings(void)
120{
121 struct warning_stack *ws;
122
123 ws = nasm_malloc(sizeof *ws);
124 memcpy(ws->state, warning_state, sizeof warning_state);
125 ws->next = warning_stack;
126 warning_stack = ws;
127}
128
129/* Pop the warning status off the warning stack */
130void pop_warnings(void)
131{
132 struct warning_stack *ws = warning_stack;
133
134 memcpy(warning_state, ws->state, sizeof warning_state);
135 if (!ws->next) {
136 /*!
137 *!warn-stack-empty [on] warning stack empty
138 *! a [WARNING POP] directive was executed when
139 *! the warning stack is empty. This is treated
140 *! as a [WARNING *all] directive.
141 */
142 nasm_warn(WARN_WARN_STACK_EMPTY, "warning stack empty");
143 } else {
144 warning_stack = ws->next;
145 nasm_free(ws);
146 }
147}
148
149/* Call after the command line is parsed, but before the first pass */
150void init_warnings(void)
151{
152 push_warnings();
153 warning_state_init = warning_stack;
154}
155
156
157/* Call after each pass */
158void reset_warnings(void)
159{
160 struct warning_stack *ws = warning_stack;
161
162 /* Unwind the warning stack. We do NOT delete the last entry! */
163 while (ws->next) {
164 struct warning_stack *wst = ws;
165 ws = ws->next;
166 nasm_free(wst);
167 }
168 warning_stack = ws;
169 memcpy(warning_state, ws->state, sizeof warning_state);
170}
171
H. Peter Anvinb2047cb2017-03-08 01:26:40 -0800172/*
173 * This is called when processing a -w or -W option, or a warning directive.
H. Peter Anvin (Intel)eb48c112018-12-12 16:11:08 -0800174 * Returns on if if the action was successful.
H. Peter Anvin (Intel)723ab482018-12-13 21:53:31 -0800175 *
176 * Special pseudo-warnings:
177 *
178 *!other [on] any warning not specifially mentioned above
179 *! specifies any warning not included in any specific warning class.
180 *
181 *!all [all] all possible warnings
H. Peter Anvinfdeb3b02019-06-06 20:53:17 -0700182 *! is an group alias for \e{all} warning classes. Thus, \c{-w+all}
183 *! enables all available warnings, and \c{-w-all} disables warnings
184 *! entirely (since NASM 2.13).
H. Peter Anvinb2047cb2017-03-08 01:26:40 -0800185 */
186bool set_warning_status(const char *value)
187{
Cyrill Gorcunov33510722018-11-24 18:58:11 +0300188 enum warn_action { WID_OFF, WID_ON, WID_RESET };
189 enum warn_action action;
H. Peter Anvinfdeb3b02019-06-06 20:53:17 -0700190 const struct warning_alias *wa;
191 size_t vlen;
Cyrill Gorcunov33510722018-11-24 18:58:11 +0300192 bool ok = false;
193 uint8_t mask;
H. Peter Anvinb2047cb2017-03-08 01:26:40 -0800194
Cyrill Gorcunov33510722018-11-24 18:58:11 +0300195 value = nasm_skip_spaces(value);
H. Peter Anvin (Intel)1df72632019-01-11 13:13:03 -0800196
Cyrill Gorcunov33510722018-11-24 18:58:11 +0300197 switch (*value) {
198 case '-':
199 action = WID_OFF;
200 value++;
201 break;
202 case '+':
203 action = WID_ON;
204 value++;
205 break;
206 case '*':
207 action = WID_RESET;
208 value++;
209 break;
210 case 'N':
211 case 'n':
212 if (!nasm_strnicmp(value, "no-", 3)) {
213 action = WID_OFF;
214 value += 3;
215 break;
216 } else if (!nasm_stricmp(value, "none")) {
217 action = WID_OFF;
218 value = NULL;
219 break;
220 }
221 /* else fall through */
222 default:
223 action = WID_ON;
224 break;
225 }
H. Peter Anvinb2047cb2017-03-08 01:26:40 -0800226
Cyrill Gorcunov33510722018-11-24 18:58:11 +0300227 mask = WARN_ST_ENABLED;
H. Peter Anvinb2047cb2017-03-08 01:26:40 -0800228
Cyrill Gorcunov33510722018-11-24 18:58:11 +0300229 if (value && !nasm_strnicmp(value, "error", 5)) {
230 switch (value[5]) {
231 case '=':
232 mask = WARN_ST_ERROR;
233 value += 6;
234 break;
235 case '\0':
236 mask = WARN_ST_ERROR;
237 value = NULL;
238 break;
239 default:
240 /* Just an accidental prefix? */
241 break;
242 }
243 }
H. Peter Anvinb2047cb2017-03-08 01:26:40 -0800244
Cyrill Gorcunov33510722018-11-24 18:58:11 +0300245 if (value && !nasm_stricmp(value, "all"))
246 value = NULL;
H. Peter Anvinb2047cb2017-03-08 01:26:40 -0800247
H. Peter Anvinfdeb3b02019-06-06 20:53:17 -0700248 vlen = value ? strlen(value) : 0;
H. Peter Anvinb2047cb2017-03-08 01:26:40 -0800249
H. Peter Anvinfdeb3b02019-06-06 20:53:17 -0700250 /* This is inefficient, but it shouldn't matter... */
251 for (wa = warning_alias; wa < &warning_alias[NUM_WARNING_ALIAS]; wa++) {
252 enum warn_index i = wa->warning;
253
254 if (value) {
255 char sep;
256
257 if (nasm_strnicmp(value, wa->name, vlen))
258 continue; /* Not a prefix */
259
260 sep = wa->name[vlen];
261 if (sep != '\0' && sep != '-')
262 continue; /* Not a valid prefix */
263 }
264
265 ok = true; /* At least one action taken */
266 switch (action) {
267 case WID_OFF:
268 warning_state[i] &= ~mask;
269 break;
270 case WID_ON:
271 warning_state[i] |= mask;
272 break;
273 case WID_RESET:
274 warning_state[i] &= ~mask;
275 warning_state[i] |= warning_state_init->state[i] & mask;
276 break;
277 }
278 }
279
280 if (!ok && value) {
281 /*!
282 *!unknown-warning [off] unknown warning in -W/-w or warning directive
283 *! warns about a \c{-w} or \c{-W} option or a \c{[WARNING]} directive
284 *! that contains an unknown warning name or is otherwise not possible to process.
285 */
286 nasm_warn(WARN_UNKNOWN_WARNING, "unknown warning name: %s", value);
H. Peter Anvin (Intel)723ab482018-12-13 21:53:31 -0800287 }
H. Peter Anvin (Intel)1df72632019-01-11 13:13:03 -0800288
Cyrill Gorcunov33510722018-11-24 18:58:11 +0300289 return ok;
H. Peter Anvin22538e22016-05-25 05:42:47 -0700290}