errwarn.h 14 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351
  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. YASM_LIB_DECL
  108. /*@exits@*/ void yasm__fatal_missing_input_file(const char *message, const char *filename);
  109. /** Unconditionally clear the error indicator, freeing any associated data.
  110. * Has no effect if the error indicator is not set.
  111. */
  112. YASM_LIB_DECL
  113. void yasm_error_clear(void);
  114. /** Get the error indicator. YASM_ERROR_NONE is returned if no error has
  115. * been set. Note that as YASM_ERROR_NONE is 0, the return value can also
  116. * be treated as a boolean value.
  117. * \return Current error indicator.
  118. */
  119. yasm_error_class yasm_error_occurred(void);
  120. /** Check the error indicator against an error class. To check if any error
  121. * has been set, check against the YASM_ERROR_GENERAL class. This function
  122. * properly checks error subclasses.
  123. * \param eclass base error class to check against
  124. * \return Nonzero if error indicator is set and a subclass of eclass, 0
  125. * otherwise.
  126. */
  127. YASM_LIB_DECL
  128. int yasm_error_matches(yasm_error_class eclass);
  129. #ifndef YASM_DOXYGEN
  130. YASM_LIB_DECL
  131. extern yasm_error_class yasm_eclass;
  132. #define yasm_error_occurred() yasm_eclass
  133. #endif
  134. /** Set the error indicator (va_list version). Has no effect if the error
  135. * indicator is already set.
  136. * \param eclass error class
  137. * \param format printf format string
  138. * \param va argument list for format
  139. */
  140. YASM_LIB_DECL
  141. void yasm_error_set_va(yasm_error_class eclass, const char *format, va_list va);
  142. /** Set the error indicator. Has no effect if the error indicator is already
  143. * set.
  144. * \param eclass error class
  145. * \param format printf format string
  146. * \param ... argument list for format
  147. */
  148. YASM_LIB_DECL
  149. void yasm_error_set(yasm_error_class eclass, const char *format, ...)
  150. /*@printflike@*/;
  151. /** Set a cross-reference for a new error (va_list version). Has no effect
  152. * if the error indicator is already set (e.g. with yasm_error_set()). This
  153. * function must be called prior to its corresponding yasm_error_set() call.
  154. * \param xrefline virtual line to cross-reference to (should not be 0)
  155. * \param format printf format string
  156. * \param va argument list for format
  157. */
  158. YASM_LIB_DECL
  159. void yasm_error_set_xref_va(unsigned long xrefline, const char *format,
  160. va_list va);
  161. /** Set a cross-reference for a new error. Has no effect if the error
  162. * indicator is already set (e.g. with yasm_error_set()). This function
  163. * must be called prior to its corresponding yasm_error_set() call.
  164. * \param xrefline virtual line to cross-reference to (should not be 0)
  165. * \param format printf format string
  166. * \param ... argument list for format
  167. */
  168. YASM_LIB_DECL
  169. void yasm_error_set_xref(unsigned long xrefline, const char *format, ...)
  170. /*@printflike@*/;
  171. /** Fetch the error indicator and all associated data. If the error
  172. * indicator is set, the output pointers are set to the current error
  173. * indicator values, and the error indicator is cleared.
  174. * The code using this function is then responsible for yasm_xfree()'ing
  175. * str and xrefstr (if non-NULL). If the error indicator is not set,
  176. * all output values are set to 0 (including eclass, which is set to
  177. * YASM_ERROR_NONE).
  178. * \param eclass error class (output)
  179. * \param str error message
  180. * \param xrefline virtual line used for cross-referencing (0 if no xref)
  181. * \param xrefstr cross-reference error message (NULL if no xref)
  182. */
  183. YASM_LIB_DECL
  184. void yasm_error_fetch(/*@out@*/ yasm_error_class *eclass,
  185. /*@out@*/ /*@only@*/ /*@null@*/ char **str,
  186. /*@out@*/ unsigned long *xrefline,
  187. /*@out@*/ /*@only@*/ /*@null@*/ char **xrefstr);
  188. /** Unconditionally clear all warning indicators, freeing any associated data.
  189. * Has no effect if no warning indicators have been set.
  190. */
  191. YASM_LIB_DECL
  192. void yasm_warn_clear(void);
  193. /** Get the first warning indicator. YASM_WARN_NONE is returned if no warning
  194. * has been set. Note that as YASM_WARN_NONE is 0, the return value can also
  195. * be treated as a boolean value.
  196. * \return First warning indicator.
  197. */
  198. YASM_LIB_DECL
  199. yasm_warn_class yasm_warn_occurred(void);
  200. /** Add a warning indicator (va_list version).
  201. * \param wclass warning class
  202. * \param format printf format string
  203. * \param va argument list for format
  204. */
  205. YASM_LIB_DECL
  206. void yasm_warn_set_va(yasm_warn_class wclass, const char *format, va_list va);
  207. /** Add a warning indicator.
  208. * \param wclass warning class
  209. * \param format printf format string
  210. * \param ... argument list for format
  211. */
  212. YASM_LIB_DECL
  213. void yasm_warn_set(yasm_warn_class wclass, const char *format, ...)
  214. /*@printflike@*/;
  215. /** Fetch the first warning indicator and all associated data. If there
  216. * is at least one warning indicator, the output pointers are set to the
  217. * first warning indicator values, and first warning indicator is removed.
  218. * The code using this function is then responsible for yasm_xfree()'ing
  219. * str and xrefstr (if non-NULL). If there is no warning indicator set,
  220. * all output values are set to 0 (including wclass, which is set to
  221. * YASM_WARN_NONE).
  222. * \param wclass warning class (output)
  223. * \param str warning message
  224. */
  225. YASM_LIB_DECL
  226. void yasm_warn_fetch(/*@out@*/ yasm_warn_class *wclass,
  227. /*@out@*/ /*@only@*/ char **str);
  228. /** Enable a class of warnings.
  229. * \param wclass warning class
  230. */
  231. YASM_LIB_DECL
  232. void yasm_warn_enable(yasm_warn_class wclass);
  233. /** Disable a class of warnings.
  234. * \param wclass warning class
  235. */
  236. YASM_LIB_DECL
  237. void yasm_warn_disable(yasm_warn_class wclass);
  238. /** Disable all classes of warnings. */
  239. YASM_LIB_DECL
  240. void yasm_warn_disable_all(void);
  241. /** Create an error/warning set for collection of multiple error/warnings.
  242. * \return Newly allocated set.
  243. */
  244. YASM_LIB_DECL
  245. /*@only@*/ yasm_errwarns *yasm_errwarns_create(void);
  246. /** Destroy an error/warning set.
  247. * \param errwarns error/warning set
  248. */
  249. YASM_LIB_DECL
  250. void yasm_errwarns_destroy(/*@only@*/ yasm_errwarns *errwarns);
  251. /** Propagate error indicator and warning indicator(s) to an error/warning set.
  252. * Has no effect if the error indicator and warning indicator are not set.
  253. * Does not print immediately; yasm_errwarn_output_all() outputs
  254. * accumulated errors and warnings.
  255. * Generally multiple errors on the same line will be reported, but errors
  256. * of class YASM_ERROR_PARSE will get overwritten by any other class on the
  257. * same line.
  258. * \param errwarns error/warning set
  259. * \param line virtual line
  260. */
  261. YASM_LIB_DECL
  262. void yasm_errwarn_propagate(yasm_errwarns *errwarns, unsigned long line);
  263. /** Get total number of errors logged.
  264. * \param errwarns error/warning set
  265. * \param warning_as_error if nonzero, warnings are treated as errors.
  266. * \return Number of errors.
  267. */
  268. YASM_LIB_DECL
  269. unsigned int yasm_errwarns_num_errors(yasm_errwarns *errwarns,
  270. int warning_as_error);
  271. /** Print out an error.
  272. * \param fn filename of source file
  273. * \param line line number
  274. * \param msg error message
  275. * \param xref_fn cross-referenced source filename
  276. * \param xref_line cross-referenced line number
  277. * \param xref_msg cross-referenced error message
  278. */
  279. typedef void (*yasm_print_error_func)
  280. (const char *fn, unsigned long line, const char *msg,
  281. /*@null@*/ const char *xref_fn, unsigned long xref_line,
  282. /*@null@*/ const char *xref_msg);
  283. /** Print out a warning.
  284. * \param fn filename of source file
  285. * \param line line number
  286. * \param msg warning message
  287. */
  288. typedef void (*yasm_print_warning_func)
  289. (const char *fn, unsigned long line, const char *msg);
  290. /** Outputs error/warning set in sorted order (sorted by virtual line number).
  291. * \param errwarns error/warning set
  292. * \param lm line map (to convert virtual lines into filename/line pairs)
  293. * \param warning_as_error if nonzero, treat warnings as errors.
  294. * \param print_error function called to print out errors
  295. * \param print_warning function called to print out warnings
  296. */
  297. YASM_LIB_DECL
  298. void yasm_errwarns_output_all
  299. (yasm_errwarns *errwarns, yasm_linemap *lm, int warning_as_error,
  300. yasm_print_error_func print_error, yasm_print_warning_func print_warning);
  301. /** Convert a possibly unprintable character into a printable string.
  302. * \internal
  303. * \param ch possibly unprintable character
  304. * \return Printable string representation (static buffer).
  305. */
  306. YASM_LIB_DECL
  307. char *yasm__conv_unprint(int ch);
  308. /** Hook for library users to map to gettext() if GNU gettext is being used.
  309. * \param msgid message catalog identifier
  310. * \return Translated message.
  311. */
  312. YASM_LIB_DECL
  313. extern const char * (*yasm_gettext_hook) (const char *msgid);
  314. #endif