error.c 6.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212
  1. /* ----------------------------------------------------------------------- *
  2. *
  3. * Copyright 1996-2018 The NASM Authors - All Rights Reserved
  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.
  17. *
  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. * error.c - error message handling routines for the assembler
  35. */
  36. #include "compiler.h"
  37. #include <stdlib.h>
  38. #include "nasmlib.h"
  39. #include "error.h"
  40. /*
  41. * Description of the suppressible warnings for the command line and
  42. * the [warning] directive.
  43. */
  44. #define on (WARN_ST_ENABLED)
  45. #define off 0
  46. #define err (WARN_ST_ENABLED|WARN_ST_ERROR)
  47. const struct warning warnings[WARN_ALL+1] = {
  48. {NULL, NULL, on}, /* must be on - used for unconditional enable */
  49. {"macro-params", "macro calls with wrong parameter count", on},
  50. {"macro-selfref", "cyclic macro references", off},
  51. {"macro-defaults", "macros with more default than optional parameters", on},
  52. {"orphan-labels", "labels alone on lines without trailing `:'", on},
  53. {"number-overflow", "numeric constant does not fit", on},
  54. {"gnu-elf-extensions", "using 8- or 16-bit relocation in ELF32, a GNU extension", off},
  55. {"float-overflow", "floating point overflow", on},
  56. {"float-denorm", "floating point denormal", off},
  57. {"float-underflow", "floating point underflow", off},
  58. {"float-toolong", "too many digits in floating-point number", on},
  59. {"user", "%warning directives", on},
  60. {"lock", "lock prefix on unlockable instructions", on},
  61. {"hle", "invalid hle prefixes", on},
  62. {"bnd", "invalid bnd prefixes", on},
  63. {"zext-reloc", "relocation zero-extended to match output format", on},
  64. {"ptr", "non-NASM keyword used in other assemblers", on},
  65. {"bad-pragma", "empty or malformed %pragma", off},
  66. {"unknown-pragma", "unknown %pragma facility or directive", off},
  67. {"not-my-pragma", "%pragma not applicable to this compilation", off},
  68. {"unknown-warning", "unknown warning in -W/-w or warning directive", off},
  69. {"negative-rep", "regative %rep count", on},
  70. {"phase", "phase error during stabilization", off},
  71. {"label-redef", "label redefined to an identical value", off},
  72. {"label-redef-late", "label (re)defined during code generation", err},
  73. /* THESE ENTRIES SHOULD COME LAST */
  74. {"other", "any warning not specifially mentioned above", on},
  75. {"all", "all possible warnings", off}
  76. };
  77. uint8_t warning_state[WARN_ALL];/* Current state */
  78. uint8_t warning_state_init[WARN_ALL]; /* Command-line state, for reset */
  79. vefunc nasm_verror; /* Global error handling function */
  80. void nasm_error(int severity, const char *fmt, ...)
  81. {
  82. va_list ap;
  83. va_start(ap, fmt);
  84. nasm_verror(severity, fmt, ap);
  85. va_end(ap);
  86. }
  87. fatal_func nasm_fatal(int flags, const char *fmt, ...)
  88. {
  89. va_list ap;
  90. va_start(ap, fmt);
  91. nasm_verror(flags | ERR_FATAL, fmt, ap);
  92. abort(); /* We should never get here */
  93. }
  94. fatal_func nasm_panic(int flags, const char *fmt, ...)
  95. {
  96. va_list ap;
  97. va_start(ap, fmt);
  98. nasm_verror(flags | ERR_PANIC, fmt, ap);
  99. abort(); /* We should never get here */
  100. }
  101. fatal_func nasm_panic_from_macro(const char *file, int line)
  102. {
  103. nasm_panic(ERR_NOFILE, "Internal error at %s:%d\n", file, line);
  104. }
  105. fatal_func nasm_assert_failed(const char *file, int line, const char *msg)
  106. {
  107. nasm_panic(0, "assertion %s failed at %s:%d", msg, file, line);
  108. }
  109. /*
  110. * This is called when processing a -w or -W option, or a warning directive.
  111. * Returns on if if the action was successful.
  112. */
  113. bool set_warning_status(const char *value)
  114. {
  115. enum warn_action { WID_OFF, WID_ON, WID_RESET };
  116. enum warn_action action;
  117. uint8_t mask;
  118. int i;
  119. bool ok = false;
  120. value = nasm_skip_spaces(value);
  121. switch (*value) {
  122. case '-':
  123. action = WID_OFF;
  124. value++;
  125. break;
  126. case '+':
  127. action = WID_ON;
  128. value++;
  129. break;
  130. case '*':
  131. action = WID_RESET;
  132. value++;
  133. break;
  134. case 'N':
  135. case 'n':
  136. if (!nasm_strnicmp(value, "no-", 3)) {
  137. action = WID_OFF;
  138. value += 3;
  139. break;
  140. } else if (!nasm_stricmp(value, "none")) {
  141. action = WID_OFF;
  142. value = NULL;
  143. break;
  144. }
  145. /* else fall through */
  146. default:
  147. action = WID_ON;
  148. break;
  149. }
  150. mask = WARN_ST_ENABLED;
  151. if (value && !nasm_strnicmp(value, "error", 5)) {
  152. switch (value[5]) {
  153. case '=':
  154. mask = WARN_ST_ERROR;
  155. value += 6;
  156. break;
  157. case '\0':
  158. mask = WARN_ST_ERROR;
  159. value = NULL;
  160. break;
  161. default:
  162. /* Just an accidental prefix? */
  163. break;
  164. }
  165. }
  166. if (value && !nasm_stricmp(value, "all"))
  167. value = NULL;
  168. /* This is inefficient, but it shouldn't matter... */
  169. for (i = 1; i < WARN_ALL; i++) {
  170. if (!value || !nasm_stricmp(value, warnings[i].name)) {
  171. ok = true; /* At least one action taken */
  172. switch (action) {
  173. case WID_OFF:
  174. warning_state[i] &= ~mask;
  175. break;
  176. case WID_ON:
  177. warning_state[i] |= mask;
  178. break;
  179. case WID_RESET:
  180. warning_state[i] &= ~mask;
  181. warning_state[i] |= warning_state_init[i] & mask;
  182. break;
  183. }
  184. }
  185. }
  186. return ok;
  187. }