blob: ea2620faeb50ef84dad0a54ab8dbd6302324992a [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)
76nasm_err_helpers(void, warn, ERR_WARNING)
77nasm_err_helpers(void, nonfatal, ERR_NONFATAL)
78nasm_err_helpers(fatal_func, fatal, ERR_FATAL)
79nasm_err_helpers(fatal_func, panic, ERR_PANIC)
H. Peter Anvin22538e22016-05-25 05:42:47 -070080
H. Peter Anvin6686fc62018-02-22 14:52:50 -080081fatal_func nasm_panic_from_macro(const char *file, int line)
H. Peter Anvin22538e22016-05-25 05:42:47 -070082{
Cyrill Gorcunov33510722018-11-24 18:58:11 +030083 nasm_panic("internal error at %s:%d\n", file, line);
H. Peter Anvin22538e22016-05-25 05:42:47 -070084}
85
H. Peter Anvin6686fc62018-02-22 14:52:50 -080086fatal_func nasm_assert_failed(const char *file, int line, const char *msg)
H. Peter Anvin22538e22016-05-25 05:42:47 -070087{
Cyrill Gorcunov33510722018-11-24 18:58:11 +030088 nasm_panic("assertion %s failed at %s:%d", msg, file, line);
H. Peter Anvinb2047cb2017-03-08 01:26:40 -080089}
90
91/*
92 * This is called when processing a -w or -W option, or a warning directive.
H. Peter Anvin (Intel)eb48c112018-12-12 16:11:08 -080093 * Returns on if if the action was successful.
H. Peter Anvin (Intel)723ab482018-12-13 21:53:31 -080094 *
95 * Special pseudo-warnings:
96 *
97 *!other [on] any warning not specifially mentioned above
98 *! specifies any warning not included in any specific warning class.
99 *
100 *!all [all] all possible warnings
101 *! is an alias for \e{all} suppressible warning classes.
102 *! Thus, \c{-w+all} enables all available warnings, and \c{-w-all}
103 *! disables warnings entirely (since NASM 2.13).
H. Peter Anvinb2047cb2017-03-08 01:26:40 -0800104 */
105bool set_warning_status(const char *value)
106{
Cyrill Gorcunov33510722018-11-24 18:58:11 +0300107 enum warn_action { WID_OFF, WID_ON, WID_RESET };
108 enum warn_action action;
H. Peter Anvin (Intel)723ab482018-12-13 21:53:31 -0800109 const char *name;
Cyrill Gorcunov33510722018-11-24 18:58:11 +0300110 bool ok = false;
111 uint8_t mask;
112 int i;
H. Peter Anvinb2047cb2017-03-08 01:26:40 -0800113
Cyrill Gorcunov33510722018-11-24 18:58:11 +0300114 value = nasm_skip_spaces(value);
115 switch (*value) {
116 case '-':
117 action = WID_OFF;
118 value++;
119 break;
120 case '+':
121 action = WID_ON;
122 value++;
123 break;
124 case '*':
125 action = WID_RESET;
126 value++;
127 break;
128 case 'N':
129 case 'n':
130 if (!nasm_strnicmp(value, "no-", 3)) {
131 action = WID_OFF;
132 value += 3;
133 break;
134 } else if (!nasm_stricmp(value, "none")) {
135 action = WID_OFF;
136 value = NULL;
137 break;
138 }
139 /* else fall through */
140 default:
141 action = WID_ON;
142 break;
143 }
H. Peter Anvinb2047cb2017-03-08 01:26:40 -0800144
Cyrill Gorcunov33510722018-11-24 18:58:11 +0300145 mask = WARN_ST_ENABLED;
H. Peter Anvinb2047cb2017-03-08 01:26:40 -0800146
Cyrill Gorcunov33510722018-11-24 18:58:11 +0300147 if (value && !nasm_strnicmp(value, "error", 5)) {
148 switch (value[5]) {
149 case '=':
150 mask = WARN_ST_ERROR;
151 value += 6;
152 break;
153 case '\0':
154 mask = WARN_ST_ERROR;
155 value = NULL;
156 break;
157 default:
158 /* Just an accidental prefix? */
159 break;
160 }
161 }
H. Peter Anvinb2047cb2017-03-08 01:26:40 -0800162
H. Peter Anvin (Intel)723ab482018-12-13 21:53:31 -0800163 name = value ? value : "<none>";
Cyrill Gorcunov33510722018-11-24 18:58:11 +0300164 if (value && !nasm_stricmp(value, "all"))
165 value = NULL;
H. Peter Anvinb2047cb2017-03-08 01:26:40 -0800166
Cyrill Gorcunov33510722018-11-24 18:58:11 +0300167 /* This is inefficient, but it shouldn't matter... */
H. Peter Anvin (Intel)723ab482018-12-13 21:53:31 -0800168 for (i = 1; i < WARN_IDX_ALL; i++) {
169 if (!value || !nasm_stricmp(value, warning_name[i])) {
Cyrill Gorcunov33510722018-11-24 18:58:11 +0300170 ok = true; /* At least one action taken */
171 switch (action) {
172 case WID_OFF:
173 warning_state[i] &= ~mask;
174 break;
175 case WID_ON:
176 warning_state[i] |= mask;
177 break;
178 case WID_RESET:
179 warning_state[i] &= ~mask;
180 warning_state[i] |= warning_state_init[i] & mask;
181 break;
182 }
183 }
184 }
H. Peter Anvinb2047cb2017-03-08 01:26:40 -0800185
H. Peter Anvin (Intel)723ab482018-12-13 21:53:31 -0800186 if (!ok) {
187 /*!
188 *!unknown-warning [off] unknown warning in -W/-w or warning directive
189 *! warns about a \c{-w} or \c{-W} option or a \c{[WARNING]} directive
190 *! that contains an unknown warning name or is otherwise not possible to process.
191 */
192 nasm_warnf(WARN_UNKNOWN_WARNING, "unknown warning name: %s", name);
193 }
194
Cyrill Gorcunov33510722018-11-24 18:58:11 +0300195 return ok;
H. Peter Anvin22538e22016-05-25 05:42:47 -0700196}