H. Peter Anvin | 22538e2 | 2016-05-25 05:42:47 -0700 | [diff] [blame] | 1 | /* ----------------------------------------------------------------------- * |
H. Peter Anvin | b2047cb | 2017-03-08 01:26:40 -0800 | [diff] [blame] | 2 | * |
H. Peter Anvin | 987dc9c | 2018-06-12 13:50:37 -0700 | [diff] [blame] | 3 | * Copyright 1996-2018 The NASM Authors - All Rights Reserved |
H. Peter Anvin | 22538e2 | 2016-05-25 05:42:47 -0700 | [diff] [blame] | 4 | * 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 Anvin | b2047cb | 2017-03-08 01:26:40 -0800 | [diff] [blame] | 17 | * |
H. Peter Anvin | 22538e2 | 2016-05-25 05:42:47 -0700 | [diff] [blame] | 18 | * 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 Anvin | b20bc73 | 2017-03-07 19:23:03 -0800 | [diff] [blame] | 35 | * error.c - error message handling routines for the assembler |
H. Peter Anvin | 22538e2 | 2016-05-25 05:42:47 -0700 | [diff] [blame] | 36 | */ |
| 37 | |
| 38 | #include "compiler.h" |
| 39 | |
| 40 | #include <stdlib.h> |
| 41 | |
| 42 | #include "nasmlib.h" |
H. Peter Anvin | b20bc73 | 2017-03-07 19:23:03 -0800 | [diff] [blame] | 43 | #include "error.h" |
| 44 | |
Cyrill Gorcunov | 3351072 | 2018-11-24 18:58:11 +0300 | [diff] [blame] | 45 | /* Global error handling function */ |
| 46 | vefunc nasm_verror; |
H. Peter Anvin | 22538e2 | 2016-05-25 05:42:47 -0700 | [diff] [blame] | 47 | |
H. Peter Anvin | d351efc | 2018-12-10 21:53:54 -0800 | [diff] [blame] | 48 | /* 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) | 6bde2ed | 2018-12-13 19:39:41 -0800 | [diff] [blame] | 55 | void nasm_error(errflags severity, const char *fmt, ...) |
H. Peter Anvin | 22538e2 | 2016-05-25 05:42:47 -0700 | [diff] [blame] | 56 | { |
H. Peter Anvin | d351efc | 2018-12-10 21:53:54 -0800 | [diff] [blame] | 57 | nasm_do_error(severity); |
H. Peter Anvin | 22538e2 | 2016-05-25 05:42:47 -0700 | [diff] [blame] | 58 | } |
| 59 | |
H. Peter Anvin | d351efc | 2018-12-10 21:53:54 -0800 | [diff] [blame] | 60 | #define nasm_err_helpers(_type, _name, _sev) \ |
H. Peter Anvin (Intel) | 6bde2ed | 2018-12-13 19:39:41 -0800 | [diff] [blame] | 61 | _type nasm_ ## _name ## f (errflags flags, const char *fmt, ...) \ |
H. Peter Anvin | d351efc | 2018-12-10 21:53:54 -0800 | [diff] [blame] | 62 | { \ |
| 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 Gorcunov | c3527dd | 2018-11-24 22:17:47 +0300 | [diff] [blame] | 72 | } |
| 73 | |
H. Peter Anvin | d351efc | 2018-12-10 21:53:54 -0800 | [diff] [blame] | 74 | nasm_err_helpers(void, debug, ERR_DEBUG) |
| 75 | nasm_err_helpers(void, note, ERR_NOTE) |
| 76 | nasm_err_helpers(void, warn, ERR_WARNING) |
| 77 | nasm_err_helpers(void, nonfatal, ERR_NONFATAL) |
| 78 | nasm_err_helpers(fatal_func, fatal, ERR_FATAL) |
| 79 | nasm_err_helpers(fatal_func, panic, ERR_PANIC) |
H. Peter Anvin | 22538e2 | 2016-05-25 05:42:47 -0700 | [diff] [blame] | 80 | |
H. Peter Anvin | 6686fc6 | 2018-02-22 14:52:50 -0800 | [diff] [blame] | 81 | fatal_func nasm_panic_from_macro(const char *file, int line) |
H. Peter Anvin | 22538e2 | 2016-05-25 05:42:47 -0700 | [diff] [blame] | 82 | { |
Cyrill Gorcunov | 3351072 | 2018-11-24 18:58:11 +0300 | [diff] [blame] | 83 | nasm_panic("internal error at %s:%d\n", file, line); |
H. Peter Anvin | 22538e2 | 2016-05-25 05:42:47 -0700 | [diff] [blame] | 84 | } |
| 85 | |
H. Peter Anvin | 6686fc6 | 2018-02-22 14:52:50 -0800 | [diff] [blame] | 86 | fatal_func nasm_assert_failed(const char *file, int line, const char *msg) |
H. Peter Anvin | 22538e2 | 2016-05-25 05:42:47 -0700 | [diff] [blame] | 87 | { |
Cyrill Gorcunov | 3351072 | 2018-11-24 18:58:11 +0300 | [diff] [blame] | 88 | nasm_panic("assertion %s failed at %s:%d", msg, file, line); |
H. Peter Anvin | b2047cb | 2017-03-08 01:26:40 -0800 | [diff] [blame] | 89 | } |
| 90 | |
| 91 | /* |
| 92 | * This is called when processing a -w or -W option, or a warning directive. |
H. Peter Anvin (Intel) | eb48c11 | 2018-12-12 16:11:08 -0800 | [diff] [blame] | 93 | * Returns on if if the action was successful. |
H. Peter Anvin (Intel) | 723ab48 | 2018-12-13 21:53:31 -0800 | [diff] [blame] | 94 | * |
| 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 Anvin | b2047cb | 2017-03-08 01:26:40 -0800 | [diff] [blame] | 104 | */ |
| 105 | bool set_warning_status(const char *value) |
| 106 | { |
Cyrill Gorcunov | 3351072 | 2018-11-24 18:58:11 +0300 | [diff] [blame] | 107 | enum warn_action { WID_OFF, WID_ON, WID_RESET }; |
| 108 | enum warn_action action; |
H. Peter Anvin (Intel) | 723ab48 | 2018-12-13 21:53:31 -0800 | [diff] [blame] | 109 | const char *name; |
Cyrill Gorcunov | 3351072 | 2018-11-24 18:58:11 +0300 | [diff] [blame] | 110 | bool ok = false; |
| 111 | uint8_t mask; |
| 112 | int i; |
H. Peter Anvin | b2047cb | 2017-03-08 01:26:40 -0800 | [diff] [blame] | 113 | |
Cyrill Gorcunov | 3351072 | 2018-11-24 18:58:11 +0300 | [diff] [blame] | 114 | 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 Anvin | b2047cb | 2017-03-08 01:26:40 -0800 | [diff] [blame] | 144 | |
Cyrill Gorcunov | 3351072 | 2018-11-24 18:58:11 +0300 | [diff] [blame] | 145 | mask = WARN_ST_ENABLED; |
H. Peter Anvin | b2047cb | 2017-03-08 01:26:40 -0800 | [diff] [blame] | 146 | |
Cyrill Gorcunov | 3351072 | 2018-11-24 18:58:11 +0300 | [diff] [blame] | 147 | 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 Anvin | b2047cb | 2017-03-08 01:26:40 -0800 | [diff] [blame] | 162 | |
H. Peter Anvin (Intel) | 723ab48 | 2018-12-13 21:53:31 -0800 | [diff] [blame] | 163 | name = value ? value : "<none>"; |
Cyrill Gorcunov | 3351072 | 2018-11-24 18:58:11 +0300 | [diff] [blame] | 164 | if (value && !nasm_stricmp(value, "all")) |
| 165 | value = NULL; |
H. Peter Anvin | b2047cb | 2017-03-08 01:26:40 -0800 | [diff] [blame] | 166 | |
Cyrill Gorcunov | 3351072 | 2018-11-24 18:58:11 +0300 | [diff] [blame] | 167 | /* This is inefficient, but it shouldn't matter... */ |
H. Peter Anvin (Intel) | 723ab48 | 2018-12-13 21:53:31 -0800 | [diff] [blame] | 168 | for (i = 1; i < WARN_IDX_ALL; i++) { |
| 169 | if (!value || !nasm_stricmp(value, warning_name[i])) { |
Cyrill Gorcunov | 3351072 | 2018-11-24 18:58:11 +0300 | [diff] [blame] | 170 | 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 Anvin | b2047cb | 2017-03-08 01:26:40 -0800 | [diff] [blame] | 185 | |
H. Peter Anvin (Intel) | 723ab48 | 2018-12-13 21:53:31 -0800 | [diff] [blame] | 186 | 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 Gorcunov | 3351072 | 2018-11-24 18:58:11 +0300 | [diff] [blame] | 195 | return ok; |
H. Peter Anvin | 22538e2 | 2016-05-25 05:42:47 -0700 | [diff] [blame] | 196 | } |