blob: fa8d2d62220735097d2921138ef9c2ac7c907698 [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
45/*
46 * Description of the suppressible warnings for the command line and
H. Peter Anvinb2047cb2017-03-08 01:26:40 -080047 * the [warning] directive.
H. Peter Anvinb20bc732017-03-07 19:23:03 -080048 */
H. Peter Anvin (Intel)eb48c112018-12-12 16:11:08 -080049#define on (WARN_ST_ENABLED)
50#define off 0
51#define err (WARN_ST_ENABLED|WARN_ST_ERROR)
H. Peter Anvinb2047cb2017-03-08 01:26:40 -080052
H. Peter Anvin (Intel)77f53ba2018-12-12 14:38:50 -080053const struct warning warnings[WARN_ALL+1] = {
H. Peter Anvin (Intel)eb48c112018-12-12 16:11:08 -080054 {NULL, NULL, on}, /* must be on - used for unconditional enable */
55 {"macro-params", "macro calls with wrong parameter count", on},
56 {"macro-selfref", "cyclic macro references", off},
57 {"macro-defaults", "macros with more default than optional parameters", on},
58 {"orphan-labels", "labels alone on lines without trailing `:'", on},
59 {"number-overflow", "numeric constant does not fit", on},
60 {"gnu-elf-extensions", "using 8- or 16-bit relocation in ELF32, a GNU extension", off},
61 {"float-overflow", "floating point overflow", on},
62 {"float-denorm", "floating point denormal", off},
63 {"float-underflow", "floating point underflow", off},
64 {"float-toolong", "too many digits in floating-point number", on},
65 {"user", "%warning directives", on},
66 {"lock", "lock prefix on unlockable instructions", on},
67 {"hle", "invalid hle prefixes", on},
68 {"bnd", "invalid bnd prefixes", on},
69 {"zext-reloc", "relocation zero-extended to match output format", on},
70 {"ptr", "non-NASM keyword used in other assemblers", on},
71 {"bad-pragma", "empty or malformed %pragma", off},
72 {"unknown-pragma", "unknown %pragma facility or directive", off},
73 {"not-my-pragma", "%pragma not applicable to this compilation", off},
74 {"unknown-warning", "unknown warning in -W/-w or warning directive", off},
75 {"negative-rep", "regative %rep count", on},
76 {"phase", "phase error during stabilization", off},
H. Peter Anvin (Intel)950dee92018-12-12 16:49:07 -080077 {"label-redef", "label redefined to an identical value", off},
78 {"label-redef-late", "label (re)defined during code generation", err},
H. Peter Anvinb2047cb2017-03-08 01:26:40 -080079
H. Peter Anvin (Intel)93367ea2018-12-12 15:58:32 -080080 /* THESE ENTRIES SHOULD COME LAST */
H. Peter Anvin (Intel)bdf017c2018-12-12 16:12:36 -080081 {"other", "any warning not specifially mentioned above", on},
H. Peter Anvin (Intel)eb48c112018-12-12 16:11:08 -080082 {"all", "all possible warnings", off}
H. Peter Anvinb20bc732017-03-07 19:23:03 -080083};
H. Peter Anvinb2047cb2017-03-08 01:26:40 -080084
H. Peter Anvin (Intel)77f53ba2018-12-12 14:38:50 -080085uint8_t warning_state[WARN_ALL];/* Current state */
86uint8_t warning_state_init[WARN_ALL]; /* Command-line state, for reset */
H. Peter Anvin22538e22016-05-25 05:42:47 -070087
Cyrill Gorcunov33510722018-11-24 18:58:11 +030088/* Global error handling function */
89vefunc nasm_verror;
H. Peter Anvin22538e22016-05-25 05:42:47 -070090
H. Peter Anvind351efc2018-12-10 21:53:54 -080091/* Common function body */
92#define nasm_do_error(s) \
93 va_list ap; \
94 va_start(ap, fmt); \
95 nasm_verror((s), fmt, ap); \
96 va_end(ap);
97
H. Peter Anvin22538e22016-05-25 05:42:47 -070098void nasm_error(int severity, const char *fmt, ...)
99{
H. Peter Anvind351efc2018-12-10 21:53:54 -0800100 nasm_do_error(severity);
H. Peter Anvin22538e22016-05-25 05:42:47 -0700101}
102
H. Peter Anvind351efc2018-12-10 21:53:54 -0800103#define nasm_err_helpers(_type, _name, _sev) \
104_type nasm_ ## _name ## f (int flags, const char *fmt, ...) \
105{ \
106 nasm_do_error((_sev)|flags); \
107 if (_sev >= ERR_FATAL) \
108 abort(); \
109} \
110_type nasm_ ## _name (const char *fmt, ...) \
111{ \
112 nasm_do_error(_sev); \
113 if (_sev >= ERR_FATAL) \
114 abort(); \
Cyrill Gorcunovc3527dd2018-11-24 22:17:47 +0300115}
116
H. Peter Anvind351efc2018-12-10 21:53:54 -0800117nasm_err_helpers(void, debug, ERR_DEBUG)
118nasm_err_helpers(void, note, ERR_NOTE)
119nasm_err_helpers(void, warn, ERR_WARNING)
120nasm_err_helpers(void, nonfatal, ERR_NONFATAL)
121nasm_err_helpers(fatal_func, fatal, ERR_FATAL)
122nasm_err_helpers(fatal_func, panic, ERR_PANIC)
H. Peter Anvin22538e22016-05-25 05:42:47 -0700123
H. Peter Anvin6686fc62018-02-22 14:52:50 -0800124fatal_func nasm_panic_from_macro(const char *file, int line)
H. Peter Anvin22538e22016-05-25 05:42:47 -0700125{
Cyrill Gorcunov33510722018-11-24 18:58:11 +0300126 nasm_panic("internal error at %s:%d\n", file, line);
H. Peter Anvin22538e22016-05-25 05:42:47 -0700127}
128
H. Peter Anvin6686fc62018-02-22 14:52:50 -0800129fatal_func nasm_assert_failed(const char *file, int line, const char *msg)
H. Peter Anvin22538e22016-05-25 05:42:47 -0700130{
Cyrill Gorcunov33510722018-11-24 18:58:11 +0300131 nasm_panic("assertion %s failed at %s:%d", msg, file, line);
H. Peter Anvinb2047cb2017-03-08 01:26:40 -0800132}
133
134/*
135 * This is called when processing a -w or -W option, or a warning directive.
H. Peter Anvin (Intel)eb48c112018-12-12 16:11:08 -0800136 * Returns on if if the action was successful.
H. Peter Anvinb2047cb2017-03-08 01:26:40 -0800137 */
138bool set_warning_status(const char *value)
139{
Cyrill Gorcunov33510722018-11-24 18:58:11 +0300140 enum warn_action { WID_OFF, WID_ON, WID_RESET };
141 enum warn_action action;
142 bool ok = false;
143 uint8_t mask;
144 int i;
H. Peter Anvinb2047cb2017-03-08 01:26:40 -0800145
Cyrill Gorcunov33510722018-11-24 18:58:11 +0300146 value = nasm_skip_spaces(value);
147 switch (*value) {
148 case '-':
149 action = WID_OFF;
150 value++;
151 break;
152 case '+':
153 action = WID_ON;
154 value++;
155 break;
156 case '*':
157 action = WID_RESET;
158 value++;
159 break;
160 case 'N':
161 case 'n':
162 if (!nasm_strnicmp(value, "no-", 3)) {
163 action = WID_OFF;
164 value += 3;
165 break;
166 } else if (!nasm_stricmp(value, "none")) {
167 action = WID_OFF;
168 value = NULL;
169 break;
170 }
171 /* else fall through */
172 default:
173 action = WID_ON;
174 break;
175 }
H. Peter Anvinb2047cb2017-03-08 01:26:40 -0800176
Cyrill Gorcunov33510722018-11-24 18:58:11 +0300177 mask = WARN_ST_ENABLED;
H. Peter Anvinb2047cb2017-03-08 01:26:40 -0800178
Cyrill Gorcunov33510722018-11-24 18:58:11 +0300179 if (value && !nasm_strnicmp(value, "error", 5)) {
180 switch (value[5]) {
181 case '=':
182 mask = WARN_ST_ERROR;
183 value += 6;
184 break;
185 case '\0':
186 mask = WARN_ST_ERROR;
187 value = NULL;
188 break;
189 default:
190 /* Just an accidental prefix? */
191 break;
192 }
193 }
H. Peter Anvinb2047cb2017-03-08 01:26:40 -0800194
Cyrill Gorcunov33510722018-11-24 18:58:11 +0300195 if (value && !nasm_stricmp(value, "all"))
196 value = NULL;
H. Peter Anvinb2047cb2017-03-08 01:26:40 -0800197
Cyrill Gorcunov33510722018-11-24 18:58:11 +0300198 /* This is inefficient, but it shouldn't matter... */
H. Peter Anvin (Intel)df4d3422018-12-12 17:48:38 -0800199 for (i = 0; i < WARN_ALL; i++) {
Cyrill Gorcunov33510722018-11-24 18:58:11 +0300200 if (!value || !nasm_stricmp(value, warnings[i].name)) {
201 ok = true; /* At least one action taken */
202 switch (action) {
203 case WID_OFF:
204 warning_state[i] &= ~mask;
205 break;
206 case WID_ON:
207 warning_state[i] |= mask;
208 break;
209 case WID_RESET:
210 warning_state[i] &= ~mask;
211 warning_state[i] |= warning_state_init[i] & mask;
212 break;
213 }
214 }
215 }
H. Peter Anvinb2047cb2017-03-08 01:26:40 -0800216
Cyrill Gorcunov33510722018-11-24 18:58:11 +0300217 return ok;
H. Peter Anvin22538e22016-05-25 05:42:47 -0700218}