blob: ef3fd9884e478a772665f7636b36e4b7d13b778a [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
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}
88
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
99/*
100 * This is called when processing a -w or -W option, or a warning directive.
H. Peter Anvin (Intel)eb48c112018-12-12 16:11:08 -0800101 * Returns on if if the action was successful.
H. Peter Anvin (Intel)723ab482018-12-13 21:53:31 -0800102 *
103 * Special pseudo-warnings:
104 *
105 *!other [on] any warning not specifially mentioned above
106 *! specifies any warning not included in any specific warning class.
107 *
108 *!all [all] all possible warnings
109 *! is an alias for \e{all} suppressible warning classes.
110 *! Thus, \c{-w+all} enables all available warnings, and \c{-w-all}
111 *! disables warnings entirely (since NASM 2.13).
H. Peter Anvinb2047cb2017-03-08 01:26:40 -0800112 */
113bool set_warning_status(const char *value)
114{
Cyrill Gorcunov33510722018-11-24 18:58:11 +0300115 enum warn_action { WID_OFF, WID_ON, WID_RESET };
116 enum warn_action action;
H. Peter Anvin (Intel)723ab482018-12-13 21:53:31 -0800117 const char *name;
Cyrill Gorcunov33510722018-11-24 18:58:11 +0300118 bool ok = false;
119 uint8_t mask;
120 int i;
H. Peter Anvinb2047cb2017-03-08 01:26:40 -0800121
Cyrill Gorcunov33510722018-11-24 18:58:11 +0300122 value = nasm_skip_spaces(value);
123 switch (*value) {
124 case '-':
125 action = WID_OFF;
126 value++;
127 break;
128 case '+':
129 action = WID_ON;
130 value++;
131 break;
132 case '*':
133 action = WID_RESET;
134 value++;
135 break;
136 case 'N':
137 case 'n':
138 if (!nasm_strnicmp(value, "no-", 3)) {
139 action = WID_OFF;
140 value += 3;
141 break;
142 } else if (!nasm_stricmp(value, "none")) {
143 action = WID_OFF;
144 value = NULL;
145 break;
146 }
147 /* else fall through */
148 default:
149 action = WID_ON;
150 break;
151 }
H. Peter Anvinb2047cb2017-03-08 01:26:40 -0800152
Cyrill Gorcunov33510722018-11-24 18:58:11 +0300153 mask = WARN_ST_ENABLED;
H. Peter Anvinb2047cb2017-03-08 01:26:40 -0800154
Cyrill Gorcunov33510722018-11-24 18:58:11 +0300155 if (value && !nasm_strnicmp(value, "error", 5)) {
156 switch (value[5]) {
157 case '=':
158 mask = WARN_ST_ERROR;
159 value += 6;
160 break;
161 case '\0':
162 mask = WARN_ST_ERROR;
163 value = NULL;
164 break;
165 default:
166 /* Just an accidental prefix? */
167 break;
168 }
169 }
H. Peter Anvinb2047cb2017-03-08 01:26:40 -0800170
H. Peter Anvin (Intel)723ab482018-12-13 21:53:31 -0800171 name = value ? value : "<none>";
Cyrill Gorcunov33510722018-11-24 18:58:11 +0300172 if (value && !nasm_stricmp(value, "all"))
173 value = NULL;
H. Peter Anvinb2047cb2017-03-08 01:26:40 -0800174
Cyrill Gorcunov33510722018-11-24 18:58:11 +0300175 /* This is inefficient, but it shouldn't matter... */
H. Peter Anvin (Intel)723ab482018-12-13 21:53:31 -0800176 for (i = 1; i < WARN_IDX_ALL; i++) {
177 if (!value || !nasm_stricmp(value, warning_name[i])) {
Cyrill Gorcunov33510722018-11-24 18:58:11 +0300178 ok = true; /* At least one action taken */
179 switch (action) {
180 case WID_OFF:
181 warning_state[i] &= ~mask;
182 break;
183 case WID_ON:
184 warning_state[i] |= mask;
185 break;
186 case WID_RESET:
187 warning_state[i] &= ~mask;
188 warning_state[i] |= warning_state_init[i] & mask;
189 break;
190 }
191 }
192 }
H. Peter Anvinb2047cb2017-03-08 01:26:40 -0800193
H. Peter Anvin (Intel)723ab482018-12-13 21:53:31 -0800194 if (!ok) {
195 /*!
196 *!unknown-warning [off] unknown warning in -W/-w or warning directive
197 *! warns about a \c{-w} or \c{-W} option or a \c{[WARNING]} directive
198 *! that contains an unknown warning name or is otherwise not possible to process.
199 */
H. Peter Anvin (Intel)80c4f232018-12-14 13:33:24 -0800200 nasm_warn(WARN_UNKNOWN_WARNING, "unknown warning name: %s", name);
H. Peter Anvin (Intel)723ab482018-12-13 21:53:31 -0800201 }
202
Cyrill Gorcunov33510722018-11-24 18:58:11 +0300203 return ok;
H. Peter Anvin22538e22016-05-25 05:42:47 -0700204}