errwarn.h 13 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348
  1. /**
  2. * \file libyasm/errwarn.h
  3. * \brief YASM error and warning reporting interface.
  4. *
  5. * \license
  6. * Copyright (C) 2001-2007 Peter Johnson
  7. *
  8. * Redistribution and use in source and binary forms, with or without
  9. * modification, are permitted provided that the following conditions
  10. * are met:
  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 copyright
  14. * notice, this list of conditions and the following disclaimer in the
  15. * documentation and/or other materials provided with the distribution.
  16. *
  17. * THIS SOFTWARE IS PROVIDED BY THE AUTHOR AND OTHER CONTRIBUTORS ``AS IS''
  18. * AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
  19. * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
  20. * ARE DISCLAIMED. IN NO EVENT SHALL THE AUTHOR OR OTHER CONTRIBUTORS BE
  21. * LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
  22. * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
  23. * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
  24. * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
  25. * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
  26. * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
  27. * POSSIBILITY OF SUCH DAMAGE.
  28. * \endlicense
  29. */
  30. #ifndef YASM_ERRWARN_H
  31. #define YASM_ERRWARN_H
  32. #ifndef YASM_LIB_DECL
  33. #define YASM_LIB_DECL
  34. #endif
  35. /** Warning classes (that may be enabled/disabled). */
  36. typedef enum yasm_warn_class {
  37. YASM_WARN_NONE = 0, /**< No warning */
  38. YASM_WARN_GENERAL, /**< Non-specific warnings */
  39. YASM_WARN_UNREC_CHAR, /**< Unrecognized characters (while tokenizing) */
  40. YASM_WARN_PREPROC, /**< Preprocessor warnings */
  41. YASM_WARN_ORPHAN_LABEL, /**< Label alone on a line without a colon */
  42. YASM_WARN_UNINIT_CONTENTS, /**< Uninitialized space in code/data section */
  43. YASM_WARN_SIZE_OVERRIDE,/**< Double size override */
  44. YASM_WARN_IMPLICIT_SIZE_OVERRIDE /**< Implicit size override */
  45. } yasm_warn_class;
  46. /** Error classes. Bitmask-based to support limited subclassing. */
  47. typedef enum yasm_error_class {
  48. YASM_ERROR_NONE = 0x0000, /**< No error */
  49. YASM_ERROR_GENERAL = 0xFFFF, /**< Non-specific */
  50. YASM_ERROR_ARITHMETIC = 0x0001, /**< Arithmetic error (general) */
  51. YASM_ERROR_OVERFLOW = 0x8001, /**< Arithmetic overflow */
  52. YASM_ERROR_FLOATING_POINT = 0x4001, /**< Floating point error */
  53. YASM_ERROR_ZERO_DIVISION = 0x2001, /**< Divide-by-zero */
  54. YASM_ERROR_ASSERTION = 0x0002, /**< Assertion error */
  55. YASM_ERROR_VALUE = 0x0004, /**< Value inappropriate
  56. * (e.g. not in range) */
  57. YASM_ERROR_NOT_ABSOLUTE = 0x8004, /**< Absolute expression required */
  58. YASM_ERROR_TOO_COMPLEX = 0x4004, /**< Expression too complex */
  59. YASM_ERROR_NOT_CONSTANT = 0x2004, /**< Constant expression required */
  60. YASM_ERROR_IO = 0x0008, /**< I/O error */
  61. YASM_ERROR_NOT_IMPLEMENTED = 0x0010, /**< Not implemented error */
  62. YASM_ERROR_TYPE = 0x0020, /**< Type error */
  63. YASM_ERROR_SYNTAX = 0x0040, /**< Syntax error */
  64. YASM_ERROR_PARSE = 0x8040 /**< Parser error */
  65. } yasm_error_class;
  66. /** Initialize any internal data structures. */
  67. YASM_LIB_DECL
  68. void yasm_errwarn_initialize(void);
  69. /** Clean up any memory allocated by yasm_errwarn_initialize() or other
  70. * functions.
  71. */
  72. YASM_LIB_DECL
  73. void yasm_errwarn_cleanup(void);
  74. /** Reporting point of internal errors. These are usually due to sanity
  75. * check failures in the code.
  76. * \warning This function must NOT return to calling code; exit or longjmp
  77. * instead.
  78. * \param file source file (ala __FILE__)
  79. * \param line source line (ala __LINE__)
  80. * \param message internal error message
  81. */
  82. YASM_LIB_DECL
  83. extern /*@exits@*/ void (*yasm_internal_error_)
  84. (const char *file, unsigned int line, const char *message);
  85. /** Easily-callable version of yasm_internal_error_(). Automatically uses
  86. * __FILE__ and __LINE__ as the file and line.
  87. * \param message internal error message
  88. */
  89. #define yasm_internal_error(message) \
  90. yasm_internal_error_(__FILE__, __LINE__, message)
  91. /** Reporting point of fatal errors.
  92. * \warning This function must NOT return to calling code; exit or longjmp
  93. * instead.
  94. * \param message fatal error message
  95. * \param va va_list argument list for message
  96. */
  97. YASM_LIB_DECL
  98. extern /*@exits@*/ void (*yasm_fatal) (const char *message, va_list va);
  99. /** Reporting point of fatal errors, with variable arguments (internal only).
  100. * \warning This function calls #yasm_fatal, and thus does not return to the
  101. * calling code.
  102. * \param message fatal error message
  103. * \param ... argument list for message
  104. */
  105. YASM_LIB_DECL
  106. /*@exits@*/ void yasm__fatal(const char *message, ...);
  107. /** Unconditionally clear the error indicator, freeing any associated data.
  108. * Has no effect if the error indicator is not set.
  109. */
  110. YASM_LIB_DECL
  111. void yasm_error_clear(void);
  112. /** Get the error indicator. YASM_ERROR_NONE is returned if no error has
  113. * been set. Note that as YASM_ERROR_NONE is 0, the return value can also
  114. * be treated as a boolean value.
  115. * \return Current error indicator.
  116. */
  117. yasm_error_class yasm_error_occurred(void);
  118. /** Check the error indicator against an error class. To check if any error
  119. * has been set, check against the YASM_ERROR_GENERAL class. This function
  120. * properly checks error subclasses.
  121. * \param eclass base error class to check against
  122. * \return Nonzero if error indicator is set and a subclass of eclass, 0
  123. * otherwise.
  124. */
  125. YASM_LIB_DECL
  126. int yasm_error_matches(yasm_error_class eclass);
  127. #ifndef YASM_DOXYGEN
  128. YASM_LIB_DECL
  129. extern yasm_error_class yasm_eclass;
  130. #define yasm_error_occurred() yasm_eclass
  131. #endif
  132. /** Set the error indicator (va_list version). Has no effect if the error
  133. * indicator is already set.
  134. * \param eclass error class
  135. * \param format printf format string
  136. * \param va argument list for format
  137. */
  138. YASM_LIB_DECL
  139. void yasm_error_set_va(yasm_error_class eclass, const char *format, va_list va);
  140. /** Set the error indicator. Has no effect if the error indicator is already
  141. * set.
  142. * \param eclass error class
  143. * \param format printf format string
  144. * \param ... argument list for format
  145. */
  146. YASM_LIB_DECL
  147. void yasm_error_set(yasm_error_class eclass, const char *format, ...)
  148. /*@printflike@*/;
  149. /** Set a cross-reference for a new error (va_list version). Has no effect
  150. * if the error indicator is already set (e.g. with yasm_error_set()). This
  151. * function must be called prior to its corresponding yasm_error_set() call.
  152. * \param xrefline virtual line to cross-reference to (should not be 0)
  153. * \param format printf format string
  154. * \param va argument list for format
  155. */
  156. YASM_LIB_DECL
  157. void yasm_error_set_xref_va(unsigned long xrefline, const char *format,
  158. va_list va);
  159. /** Set a cross-reference for a new error. Has no effect if the error
  160. * indicator is already set (e.g. with yasm_error_set()). This function
  161. * must be called prior to its corresponding yasm_error_set() call.
  162. * \param xrefline virtual line to cross-reference to (should not be 0)
  163. * \param format printf format string
  164. * \param ... argument list for format
  165. */
  166. YASM_LIB_DECL
  167. void yasm_error_set_xref(unsigned long xrefline, const char *format, ...)
  168. /*@printflike@*/;
  169. /** Fetch the error indicator and all associated data. If the error
  170. * indicator is set, the output pointers are set to the current error
  171. * indicator values, and the error indicator is cleared.
  172. * The code using this function is then responsible for yasm_xfree()'ing
  173. * str and xrefstr (if non-NULL). If the error indicator is not set,
  174. * all output values are set to 0 (including eclass, which is set to
  175. * YASM_ERROR_NONE).
  176. * \param eclass error class (output)
  177. * \param str error message
  178. * \param xrefline virtual line used for cross-referencing (0 if no xref)
  179. * \param xrefstr cross-reference error message (NULL if no xref)
  180. */
  181. YASM_LIB_DECL
  182. void yasm_error_fetch(/*@out@*/ yasm_error_class *eclass,
  183. /*@out@*/ /*@only@*/ /*@null@*/ char **str,
  184. /*@out@*/ unsigned long *xrefline,
  185. /*@out@*/ /*@only@*/ /*@null@*/ char **xrefstr);
  186. /** Unconditionally clear all warning indicators, freeing any associated data.
  187. * Has no effect if no warning indicators have been set.
  188. */
  189. YASM_LIB_DECL
  190. void yasm_warn_clear(void);
  191. /** Get the first warning indicator. YASM_WARN_NONE is returned if no warning
  192. * has been set. Note that as YASM_WARN_NONE is 0, the return value can also
  193. * be treated as a boolean value.
  194. * \return First warning indicator.
  195. */
  196. YASM_LIB_DECL
  197. yasm_warn_class yasm_warn_occurred(void);
  198. /** Add a warning indicator (va_list version).
  199. * \param wclass warning class
  200. * \param format printf format string
  201. * \param va argument list for format
  202. */
  203. YASM_LIB_DECL
  204. void yasm_warn_set_va(yasm_warn_class wclass, const char *format, va_list va);
  205. /** Add a warning indicator.
  206. * \param wclass warning class
  207. * \param format printf format string
  208. * \param ... argument list for format
  209. */
  210. YASM_LIB_DECL
  211. void yasm_warn_set(yasm_warn_class wclass, const char *format, ...)
  212. /*@printflike@*/;
  213. /** Fetch the first warning indicator and all associated data. If there
  214. * is at least one warning indicator, the output pointers are set to the
  215. * first warning indicator values, and first warning indicator is removed.
  216. * The code using this function is then responsible for yasm_xfree()'ing
  217. * str and xrefstr (if non-NULL). If there is no warning indicator set,
  218. * all output values are set to 0 (including wclass, which is set to
  219. * YASM_WARN_NONE).
  220. * \param wclass warning class (output)
  221. * \param str warning message
  222. */
  223. YASM_LIB_DECL
  224. void yasm_warn_fetch(/*@out@*/ yasm_warn_class *wclass,
  225. /*@out@*/ /*@only@*/ char **str);
  226. /** Enable a class of warnings.
  227. * \param wclass warning class
  228. */
  229. YASM_LIB_DECL
  230. void yasm_warn_enable(yasm_warn_class wclass);
  231. /** Disable a class of warnings.
  232. * \param wclass warning class
  233. */
  234. YASM_LIB_DECL
  235. void yasm_warn_disable(yasm_warn_class wclass);
  236. /** Disable all classes of warnings. */
  237. YASM_LIB_DECL
  238. void yasm_warn_disable_all(void);
  239. /** Create an error/warning set for collection of multiple error/warnings.
  240. * \return Newly allocated set.
  241. */
  242. YASM_LIB_DECL
  243. /*@only@*/ yasm_errwarns *yasm_errwarns_create(void);
  244. /** Destroy an error/warning set.
  245. * \param errwarns error/warning set
  246. */
  247. YASM_LIB_DECL
  248. void yasm_errwarns_destroy(/*@only@*/ yasm_errwarns *errwarns);
  249. /** Propagate error indicator and warning indicator(s) to an error/warning set.
  250. * Has no effect if the error indicator and warning indicator are not set.
  251. * Does not print immediately; yasm_errwarn_output_all() outputs
  252. * accumulated errors and warnings.
  253. * Generally multiple errors on the same line will be reported, but errors
  254. * of class YASM_ERROR_PARSE will get overwritten by any other class on the
  255. * same line.
  256. * \param errwarns error/warning set
  257. * \param line virtual line
  258. */
  259. YASM_LIB_DECL
  260. void yasm_errwarn_propagate(yasm_errwarns *errwarns, unsigned long line);
  261. /** Get total number of errors logged.
  262. * \param errwarns error/warning set
  263. * \param warning_as_error if nonzero, warnings are treated as errors.
  264. * \return Number of errors.
  265. */
  266. YASM_LIB_DECL
  267. unsigned int yasm_errwarns_num_errors(yasm_errwarns *errwarns,
  268. int warning_as_error);
  269. /** Print out an error.
  270. * \param fn filename of source file
  271. * \param line line number
  272. * \param msg error message
  273. * \param xref_fn cross-referenced source filename
  274. * \param xref_line cross-referenced line number
  275. * \param xref_msg cross-referenced error message
  276. */
  277. typedef void (*yasm_print_error_func)
  278. (const char *fn, unsigned long line, const char *msg,
  279. /*@null@*/ const char *xref_fn, unsigned long xref_line,
  280. /*@null@*/ const char *xref_msg);
  281. /** Print out a warning.
  282. * \param fn filename of source file
  283. * \param line line number
  284. * \param msg warning message
  285. */
  286. typedef void (*yasm_print_warning_func)
  287. (const char *fn, unsigned long line, const char *msg);
  288. /** Outputs error/warning set in sorted order (sorted by virtual line number).
  289. * \param errwarns error/warning set
  290. * \param lm line map (to convert virtual lines into filename/line pairs)
  291. * \param warning_as_error if nonzero, treat warnings as errors.
  292. * \param print_error function called to print out errors
  293. * \param print_warning function called to print out warnings
  294. */
  295. YASM_LIB_DECL
  296. void yasm_errwarns_output_all
  297. (yasm_errwarns *errwarns, yasm_linemap *lm, int warning_as_error,
  298. yasm_print_error_func print_error, yasm_print_warning_func print_warning);
  299. /** Convert a possibly unprintable character into a printable string.
  300. * \internal
  301. * \param ch possibly unprintable character
  302. * \return Printable string representation (static buffer).
  303. */
  304. YASM_LIB_DECL
  305. char *yasm__conv_unprint(int ch);
  306. /** Hook for library users to map to gettext() if GNU gettext is being used.
  307. * \param msgid message catalog identifier
  308. * \return Translated message.
  309. */
  310. YASM_LIB_DECL
  311. extern const char * (*yasm_gettext_hook) (const char *msgid);
  312. #endif