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 | |
| 45 | /* |
| 46 | * Description of the suppressible warnings for the command line and |
H. Peter Anvin | b2047cb | 2017-03-08 01:26:40 -0800 | [diff] [blame] | 47 | * the [warning] directive. |
H. Peter Anvin | b20bc73 | 2017-03-07 19:23:03 -0800 | [diff] [blame] | 48 | */ |
H. Peter Anvin (Intel) | eb48c11 | 2018-12-12 16:11:08 -0800 | [diff] [blame^] | 49 | #define on (WARN_ST_ENABLED) |
| 50 | #define off 0 |
| 51 | #define err (WARN_ST_ENABLED|WARN_ST_ERROR) |
| 52 | |
H. Peter Anvin (Intel) | 77f53ba | 2018-12-12 14:38:50 -0800 | [diff] [blame] | 53 | const struct warning warnings[WARN_ALL+1] = { |
H. Peter Anvin (Intel) | eb48c11 | 2018-12-12 16:11:08 -0800 | [diff] [blame^] | 54 | {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 | b2047cb | 2017-03-08 01:26:40 -0800 | [diff] [blame] | 77 | |
H. Peter Anvin (Intel) | 93367ea | 2018-12-12 15:58:32 -0800 | [diff] [blame] | 78 | /* THESE ENTRIES SHOULD COME LAST */ |
H. Peter Anvin (Intel) | eb48c11 | 2018-12-12 16:11:08 -0800 | [diff] [blame^] | 79 | {"other", "any warning not specifially mentioned below", on}, |
| 80 | {"all", "all possible warnings", off} |
H. Peter Anvin | b20bc73 | 2017-03-07 19:23:03 -0800 | [diff] [blame] | 81 | }; |
H. Peter Anvin | b2047cb | 2017-03-08 01:26:40 -0800 | [diff] [blame] | 82 | |
H. Peter Anvin (Intel) | 77f53ba | 2018-12-12 14:38:50 -0800 | [diff] [blame] | 83 | uint8_t warning_state[WARN_ALL];/* Current state */ |
| 84 | uint8_t warning_state_init[WARN_ALL]; /* Command-line state, for reset */ |
H. Peter Anvin | 22538e2 | 2016-05-25 05:42:47 -0700 | [diff] [blame] | 85 | |
| 86 | vefunc nasm_verror; /* Global error handling function */ |
| 87 | |
| 88 | void nasm_error(int severity, const char *fmt, ...) |
| 89 | { |
| 90 | va_list ap; |
| 91 | |
| 92 | va_start(ap, fmt); |
| 93 | nasm_verror(severity, fmt, ap); |
| 94 | va_end(ap); |
| 95 | } |
| 96 | |
H. Peter Anvin | 6686fc6 | 2018-02-22 14:52:50 -0800 | [diff] [blame] | 97 | fatal_func nasm_fatal(int flags, const char *fmt, ...) |
H. Peter Anvin | 22538e2 | 2016-05-25 05:42:47 -0700 | [diff] [blame] | 98 | { |
| 99 | va_list ap; |
| 100 | |
| 101 | va_start(ap, fmt); |
| 102 | nasm_verror(flags | ERR_FATAL, fmt, ap); |
| 103 | abort(); /* We should never get here */ |
| 104 | } |
| 105 | |
H. Peter Anvin | 6686fc6 | 2018-02-22 14:52:50 -0800 | [diff] [blame] | 106 | fatal_func nasm_panic(int flags, const char *fmt, ...) |
H. Peter Anvin | 22538e2 | 2016-05-25 05:42:47 -0700 | [diff] [blame] | 107 | { |
| 108 | va_list ap; |
| 109 | |
| 110 | va_start(ap, fmt); |
| 111 | nasm_verror(flags | ERR_PANIC, fmt, ap); |
| 112 | abort(); /* We should never get here */ |
| 113 | } |
| 114 | |
H. Peter Anvin | 6686fc6 | 2018-02-22 14:52:50 -0800 | [diff] [blame] | 115 | fatal_func nasm_panic_from_macro(const char *file, int line) |
H. Peter Anvin | 22538e2 | 2016-05-25 05:42:47 -0700 | [diff] [blame] | 116 | { |
| 117 | nasm_panic(ERR_NOFILE, "Internal error at %s:%d\n", file, line); |
| 118 | } |
| 119 | |
H. Peter Anvin | 6686fc6 | 2018-02-22 14:52:50 -0800 | [diff] [blame] | 120 | 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] | 121 | { |
H. Peter Anvin | b2047cb | 2017-03-08 01:26:40 -0800 | [diff] [blame] | 122 | nasm_panic(0, "assertion %s failed at %s:%d", msg, file, line); |
| 123 | } |
| 124 | |
| 125 | /* |
| 126 | * 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^] | 127 | * Returns on if if the action was successful. |
H. Peter Anvin | b2047cb | 2017-03-08 01:26:40 -0800 | [diff] [blame] | 128 | */ |
| 129 | bool set_warning_status(const char *value) |
| 130 | { |
| 131 | enum warn_action { WID_OFF, WID_ON, WID_RESET }; |
| 132 | enum warn_action action; |
| 133 | uint8_t mask; |
| 134 | int i; |
| 135 | bool ok = false; |
| 136 | |
| 137 | value = nasm_skip_spaces(value); |
| 138 | switch (*value) { |
| 139 | case '-': |
| 140 | action = WID_OFF; |
| 141 | value++; |
| 142 | break; |
| 143 | case '+': |
| 144 | action = WID_ON; |
| 145 | value++; |
| 146 | break; |
| 147 | case '*': |
| 148 | action = WID_RESET; |
| 149 | value++; |
| 150 | break; |
| 151 | case 'N': |
| 152 | case 'n': |
| 153 | if (!nasm_strnicmp(value, "no-", 3)) { |
| 154 | action = WID_OFF; |
| 155 | value += 3; |
| 156 | break; |
| 157 | } else if (!nasm_stricmp(value, "none")) { |
| 158 | action = WID_OFF; |
| 159 | value = NULL; |
| 160 | break; |
| 161 | } |
| 162 | /* else fall through */ |
| 163 | default: |
| 164 | action = WID_ON; |
| 165 | break; |
| 166 | } |
| 167 | |
| 168 | mask = WARN_ST_ENABLED; |
| 169 | |
| 170 | if (value && !nasm_strnicmp(value, "error", 5)) { |
| 171 | switch (value[5]) { |
| 172 | case '=': |
| 173 | mask = WARN_ST_ERROR; |
| 174 | value += 6; |
| 175 | break; |
| 176 | case '\0': |
| 177 | mask = WARN_ST_ERROR; |
| 178 | value = NULL; |
| 179 | break; |
| 180 | default: |
| 181 | /* Just an accidental prefix? */ |
| 182 | break; |
| 183 | } |
| 184 | } |
| 185 | |
| 186 | if (value && !nasm_stricmp(value, "all")) |
| 187 | value = NULL; |
| 188 | |
| 189 | /* This is inefficient, but it shouldn't matter... */ |
H. Peter Anvin (Intel) | 93367ea | 2018-12-12 15:58:32 -0800 | [diff] [blame] | 190 | for (i = 1; i < WARN_ALL; i++) { |
H. Peter Anvin | b2047cb | 2017-03-08 01:26:40 -0800 | [diff] [blame] | 191 | if (!value || !nasm_stricmp(value, warnings[i].name)) { |
| 192 | ok = true; /* At least one action taken */ |
| 193 | switch (action) { |
| 194 | case WID_OFF: |
| 195 | warning_state[i] &= ~mask; |
| 196 | break; |
| 197 | case WID_ON: |
| 198 | warning_state[i] |= mask; |
| 199 | break; |
| 200 | case WID_RESET: |
| 201 | warning_state[i] &= ~mask; |
| 202 | warning_state[i] |= warning_state_init[i] & mask; |
| 203 | break; |
| 204 | } |
| 205 | } |
| 206 | } |
| 207 | |
| 208 | return ok; |
H. Peter Anvin | 22538e2 | 2016-05-25 05:42:47 -0700 | [diff] [blame] | 209 | } |