blob: 2e927a647be295970a30013017ab70159010a9d4 [file] [log] [blame]
H. Peter Anvin22538e22016-05-25 05:42:47 -07001/* ----------------------------------------------------------------------- *
H. Peter Anvinb2047cb2017-03-08 01:26:40 -08002 *
H. Peter Anvin987dc9c2018-06-12 13:50:37 -07003 * Copyright 1996-2018 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
40#include <stdlib.h>
41
42#include "nasmlib.h"
H. Peter Anvinb20bc732017-03-07 19:23:03 -080043#include "error.h"
44
Cyrill Gorcunov33510722018-11-24 18:58:11 +030045/* Global error handling function */
46vefunc nasm_verror;
H. Peter Anvin22538e22016-05-25 05:42:47 -070047
H. Peter Anvind351efc2018-12-10 21:53:54 -080048/* Common function body */
49#define nasm_do_error(s) \
50 va_list ap; \
51 va_start(ap, fmt); \
52 nasm_verror((s), fmt, ap); \
53 va_end(ap);
54
H. Peter Anvin (Intel)6bde2ed2018-12-13 19:39:41 -080055void nasm_error(errflags severity, const char *fmt, ...)
H. Peter Anvin22538e22016-05-25 05:42:47 -070056{
H. Peter Anvind351efc2018-12-10 21:53:54 -080057 nasm_do_error(severity);
H. Peter Anvin22538e22016-05-25 05:42:47 -070058}
59
H. Peter Anvind351efc2018-12-10 21:53:54 -080060#define nasm_err_helpers(_type, _name, _sev) \
H. Peter Anvin (Intel)6bde2ed2018-12-13 19:39:41 -080061_type nasm_ ## _name ## f (errflags flags, const char *fmt, ...) \
H. Peter Anvind351efc2018-12-10 21:53:54 -080062{ \
63 nasm_do_error((_sev)|flags); \
64 if (_sev >= ERR_FATAL) \
65 abort(); \
66} \
67_type nasm_ ## _name (const char *fmt, ...) \
68{ \
69 nasm_do_error(_sev); \
70 if (_sev >= ERR_FATAL) \
71 abort(); \
Cyrill Gorcunovc3527dd2018-11-24 22:17:47 +030072}
73
H. Peter Anvind351efc2018-12-10 21:53:54 -080074nasm_err_helpers(void, debug, ERR_DEBUG)
75nasm_err_helpers(void, note, ERR_NOTE)
H. Peter Anvind351efc2018-12-10 21:53:54 -080076nasm_err_helpers(void, nonfatal, ERR_NONFATAL)
77nasm_err_helpers(fatal_func, fatal, ERR_FATAL)
78nasm_err_helpers(fatal_func, panic, ERR_PANIC)
H. Peter Anvin22538e22016-05-25 05:42:47 -070079
H. Peter Anvin (Intel)80c4f232018-12-14 13:33:24 -080080/*
81 * Strongly discourage warnings without level by require flags on warnings.
82 * This means nasm_warn() is the equivalent of the -f variants of the
83 * other ones.
84 */
85void nasm_warn(errflags severity, const char *fmt, ...)
86{
87 nasm_do_error(ERR_WARNING|severity);
88}
89
H. Peter Anvin6686fc62018-02-22 14:52:50 -080090fatal_func nasm_panic_from_macro(const char *file, int line)
H. Peter Anvin22538e22016-05-25 05:42:47 -070091{
Cyrill Gorcunov33510722018-11-24 18:58:11 +030092 nasm_panic("internal error at %s:%d\n", file, line);
H. Peter Anvin22538e22016-05-25 05:42:47 -070093}
94
H. Peter Anvin6686fc62018-02-22 14:52:50 -080095fatal_func nasm_assert_failed(const char *file, int line, const char *msg)
H. Peter Anvin22538e22016-05-25 05:42:47 -070096{
Cyrill Gorcunov33510722018-11-24 18:58:11 +030097 nasm_panic("assertion %s failed at %s:%d", msg, file, line);
H. Peter Anvinb2047cb2017-03-08 01:26:40 -080098}
99
100/*
101 * This is called when processing a -w or -W option, or a warning directive.
H. Peter Anvin (Intel)eb48c112018-12-12 16:11:08 -0800102 * Returns on if if the action was successful.
H. Peter Anvin (Intel)723ab482018-12-13 21:53:31 -0800103 *
104 * Special pseudo-warnings:
105 *
106 *!other [on] any warning not specifially mentioned above
107 *! specifies any warning not included in any specific warning class.
108 *
109 *!all [all] all possible warnings
110 *! is an alias for \e{all} suppressible warning classes.
111 *! Thus, \c{-w+all} enables all available warnings, and \c{-w-all}
112 *! disables warnings entirely (since NASM 2.13).
H. Peter Anvinb2047cb2017-03-08 01:26:40 -0800113 */
114bool set_warning_status(const char *value)
115{
Cyrill Gorcunov33510722018-11-24 18:58:11 +0300116 enum warn_action { WID_OFF, WID_ON, WID_RESET };
117 enum warn_action action;
H. Peter Anvin (Intel)723ab482018-12-13 21:53:31 -0800118 const char *name;
Cyrill Gorcunov33510722018-11-24 18:58:11 +0300119 bool ok = false;
120 uint8_t mask;
121 int i;
H. Peter Anvinb2047cb2017-03-08 01:26:40 -0800122
Cyrill Gorcunov33510722018-11-24 18:58:11 +0300123 value = nasm_skip_spaces(value);
124 switch (*value) {
125 case '-':
126 action = WID_OFF;
127 value++;
128 break;
129 case '+':
130 action = WID_ON;
131 value++;
132 break;
133 case '*':
134 action = WID_RESET;
135 value++;
136 break;
137 case 'N':
138 case 'n':
139 if (!nasm_strnicmp(value, "no-", 3)) {
140 action = WID_OFF;
141 value += 3;
142 break;
143 } else if (!nasm_stricmp(value, "none")) {
144 action = WID_OFF;
145 value = NULL;
146 break;
147 }
148 /* else fall through */
149 default:
150 action = WID_ON;
151 break;
152 }
H. Peter Anvinb2047cb2017-03-08 01:26:40 -0800153
Cyrill Gorcunov33510722018-11-24 18:58:11 +0300154 mask = WARN_ST_ENABLED;
H. Peter Anvinb2047cb2017-03-08 01:26:40 -0800155
Cyrill Gorcunov33510722018-11-24 18:58:11 +0300156 if (value && !nasm_strnicmp(value, "error", 5)) {
157 switch (value[5]) {
158 case '=':
159 mask = WARN_ST_ERROR;
160 value += 6;
161 break;
162 case '\0':
163 mask = WARN_ST_ERROR;
164 value = NULL;
165 break;
166 default:
167 /* Just an accidental prefix? */
168 break;
169 }
170 }
H. Peter Anvinb2047cb2017-03-08 01:26:40 -0800171
H. Peter Anvin (Intel)723ab482018-12-13 21:53:31 -0800172 name = value ? value : "<none>";
Cyrill Gorcunov33510722018-11-24 18:58:11 +0300173 if (value && !nasm_stricmp(value, "all"))
174 value = NULL;
H. Peter Anvinb2047cb2017-03-08 01:26:40 -0800175
Cyrill Gorcunov33510722018-11-24 18:58:11 +0300176 /* This is inefficient, but it shouldn't matter... */
H. Peter Anvin (Intel)723ab482018-12-13 21:53:31 -0800177 for (i = 1; i < WARN_IDX_ALL; i++) {
178 if (!value || !nasm_stricmp(value, warning_name[i])) {
Cyrill Gorcunov33510722018-11-24 18:58:11 +0300179 ok = true; /* At least one action taken */
180 switch (action) {
181 case WID_OFF:
182 warning_state[i] &= ~mask;
183 break;
184 case WID_ON:
185 warning_state[i] |= mask;
186 break;
187 case WID_RESET:
188 warning_state[i] &= ~mask;
189 warning_state[i] |= warning_state_init[i] & mask;
190 break;
191 }
192 }
193 }
H. Peter Anvinb2047cb2017-03-08 01:26:40 -0800194
H. Peter Anvin (Intel)723ab482018-12-13 21:53:31 -0800195 if (!ok) {
196 /*!
197 *!unknown-warning [off] unknown warning in -W/-w or warning directive
198 *! warns about a \c{-w} or \c{-W} option or a \c{[WARNING]} directive
199 *! that contains an unknown warning name or is otherwise not possible to process.
200 */
H. Peter Anvin (Intel)80c4f232018-12-14 13:33:24 -0800201 nasm_warn(WARN_UNKNOWN_WARNING, "unknown warning name: %s", name);
H. Peter Anvin (Intel)723ab482018-12-13 21:53:31 -0800202 }
203
Cyrill Gorcunov33510722018-11-24 18:58:11 +0300204 return ok;
H. Peter Anvin22538e22016-05-25 05:42:47 -0700205}