PageRenderTime 95ms CodeModel.GetById 26ms RepoModel.GetById 1ms app.codeStats 0ms

/tags/rel-1-3-22/SWIG/Source/Swig/error.c

#
C | 269 lines | 163 code | 35 blank | 71 comment | 32 complexity | 75407a2edaef0a48ef8ac6feb0425016 MD5 | raw file
Possible License(s): LGPL-2.1, Cube, GPL-3.0, 0BSD, GPL-2.0
  1. /* -----------------------------------------------------------------------------
  2. * error.c
  3. *
  4. * Error handling functions. These are used to issue warnings and
  5. * error messages.
  6. *
  7. * Author(s) : David Beazley (beazley@cs.uchicago.edu)
  8. *
  9. * Copyright (C) 1999-2000. The University of Chicago
  10. * See the file LICENSE for information on usage and redistribution.
  11. * ----------------------------------------------------------------------------- */
  12. #include "swig.h"
  13. #include <stdarg.h>
  14. #include <ctype.h>
  15. char cvsroot_error_c[] = "$Header$";
  16. /* -----------------------------------------------------------------------------
  17. * Commentary on the warning filter.
  18. *
  19. * The warning filter is a string of numbers prefaced by (-) or (+) to
  20. * indicate whether or not a warning message is displayed. For example:
  21. *
  22. * "-304-201-140+210+201"
  23. *
  24. * The filter string is scanned left to right and the first occurrence
  25. * of a warning number is used to determine printing behavior.
  26. *
  27. * The same number may appear more than once in the string. For example, in the
  28. * above string, "201" appears twice. This simply means that warning 201
  29. * was disabled after it was previously enabled. This may only be temporary
  30. * setting--the first number may be removed later in which case the warning
  31. * is reenabled.
  32. * ----------------------------------------------------------------------------- */
  33. #if defined(_WIN32)
  34. # define DEFAULT_ERROR_MSG_FORMAT EMF_MICROSOFT
  35. #else
  36. # define DEFAULT_ERROR_MSG_FORMAT EMF_STANDARD
  37. #endif
  38. static ErrorMessageFormat msg_format = DEFAULT_ERROR_MSG_FORMAT;
  39. static int silence = 0; /* Silent operation */
  40. static String *filter = 0; /* Warning filter */
  41. static int warnall = 0;
  42. static int nwarning = 0;
  43. static int nerrors = 0;
  44. static int init_fmt = 0;
  45. static char wrn_wnum_fmt[64];
  46. static char wrn_nnum_fmt[64];
  47. static char err_line_fmt[64];
  48. static char err_eof_fmt[64];
  49. static String *format_filename(const String_or_char *filename);
  50. /* -----------------------------------------------------------------------------
  51. * Swig_warning()
  52. *
  53. * Issue a warning message
  54. * ----------------------------------------------------------------------------- */
  55. void
  56. Swig_warning(int wnum, const String_or_char *filename, int line, const char *fmt, ...) {
  57. String *out;
  58. char *msg;
  59. int wrn = 1;
  60. va_list ap;
  61. if (silence) return;
  62. if (!init_fmt) Swig_error_msg_format(DEFAULT_ERROR_MSG_FORMAT);
  63. va_start(ap,fmt);
  64. out = NewString("");
  65. vPrintf(out,fmt,ap);
  66. {
  67. char temp[64], *t;
  68. t = temp;
  69. msg = Char(out);
  70. while (isdigit((int) *msg)) {
  71. *(t++) = *(msg++);
  72. }
  73. if (t != temp) {
  74. msg++;
  75. *t = 0;
  76. wnum = atoi(temp);
  77. }
  78. }
  79. /* Check in the warning filter */
  80. if (filter) {
  81. char temp[32];
  82. char *c;
  83. sprintf(temp,"%d",wnum);
  84. c = Strstr(filter,temp);
  85. if (c) {
  86. if (*(c-1) == '-') wrn = 0; /* Warning disabled */
  87. if (*(c-1) == '+') wrn = 1; /* Warning enabled */
  88. }
  89. }
  90. if (warnall || wrn) {
  91. String *formatted_filename = format_filename(filename);
  92. if (wnum) {
  93. Printf(stderr, wrn_wnum_fmt, formatted_filename, line, wnum);
  94. } else {
  95. Printf(stderr, wrn_nnum_fmt, formatted_filename, line);
  96. }
  97. Printf(stderr,"%s",msg);
  98. nwarning++;
  99. Delete(formatted_filename);
  100. }
  101. Delete(out);
  102. va_end(ap);
  103. }
  104. /* -----------------------------------------------------------------------------
  105. * Swig_error()
  106. *
  107. * Issue an error message
  108. * ----------------------------------------------------------------------------- */
  109. void
  110. Swig_error(const String_or_char *filename, int line, const char *fmt, ...) {
  111. va_list ap;
  112. String *formatted_filename = NULL;
  113. if (silence) return;
  114. if (!init_fmt) Swig_error_msg_format(DEFAULT_ERROR_MSG_FORMAT);
  115. va_start(ap,fmt);
  116. formatted_filename = format_filename(filename);
  117. if (line > 0) {
  118. Printf(stderr, err_line_fmt, formatted_filename, line);
  119. } else {
  120. Printf(stderr, err_eof_fmt, formatted_filename);
  121. }
  122. vPrintf(stderr,fmt,ap);
  123. va_end(ap);
  124. nerrors++;
  125. Delete(formatted_filename);
  126. }
  127. /* -----------------------------------------------------------------------------
  128. * Swig_error_count()
  129. *
  130. * Returns number of errors received.
  131. * ----------------------------------------------------------------------------- */
  132. int
  133. Swig_error_count(void) {
  134. return nerrors;
  135. }
  136. /* -----------------------------------------------------------------------------
  137. * Swig_error_silent()
  138. *
  139. * Set silent flag
  140. * ----------------------------------------------------------------------------- */
  141. void
  142. Swig_error_silent(int s) {
  143. silence = s;
  144. }
  145. /* -----------------------------------------------------------------------------
  146. * Swig_warnfilter()
  147. *
  148. * Takes a comma separate list of warning numbers and puts in the filter.
  149. * ----------------------------------------------------------------------------- */
  150. void
  151. Swig_warnfilter(const String_or_char *wlist, int add) {
  152. char *c;
  153. String *s;
  154. if (!filter) filter = NewString("");
  155. s = NewString(wlist);
  156. c = Char(s);
  157. c = strtok(c,", ");
  158. while (c) {
  159. if (isdigit((int) *c) || (*c == '+') || (*c == '-')) {
  160. if (add) {
  161. Insert(filter,0,c);
  162. if (isdigit((int) *c)) {
  163. Insert(filter,0,"-");
  164. }
  165. } else {
  166. char temp[32];
  167. if (isdigit((int) *c)) {
  168. sprintf(temp,"-%s",c);
  169. } else {
  170. strcpy(temp,c);
  171. }
  172. Replace(filter,temp,"", DOH_REPLACE_FIRST);
  173. }
  174. }
  175. c = strtok(NULL,", ");
  176. }
  177. Delete(s);
  178. }
  179. void
  180. Swig_warnall(void) {
  181. warnall = 1;
  182. }
  183. /* -----------------------------------------------------------------------------
  184. * Swig_warn_count()
  185. *
  186. * Return the number of warnings
  187. * ----------------------------------------------------------------------------- */
  188. int
  189. Swig_warn_count(void) {
  190. return nwarning;
  191. }
  192. /* -----------------------------------------------------------------------------
  193. * Swig_error_msg_format()
  194. *
  195. * Set the type of error/warning message display
  196. * ----------------------------------------------------------------------------- */
  197. void
  198. Swig_error_msg_format(ErrorMessageFormat format) {
  199. const char* error = "Error";
  200. const char* warning = "Warning";
  201. const char* fmt_eof = 0;
  202. const char* fmt_line = 0;
  203. /* here 'format' could be directly a string instead of an enum, but
  204. by now a switch is used to translated into one. */
  205. switch (format) {
  206. case EMF_MICROSOFT:
  207. fmt_line = "%s(%d)";
  208. fmt_eof = "%s(999999)"; /* Is there a special character for EOF? Just use a large number. */
  209. break;
  210. case EMF_STANDARD:
  211. default:
  212. fmt_line = "%s:%d";
  213. fmt_eof = "%s:EOF";
  214. }
  215. sprintf(wrn_wnum_fmt, "%s: %s(%%d): ", fmt_line, warning);
  216. sprintf(wrn_nnum_fmt, "%s: %s: ", fmt_line, warning);
  217. sprintf(err_line_fmt, "%s: %s: ", fmt_line, error);
  218. sprintf(err_eof_fmt, "%s: %s: ", fmt_eof, error);
  219. msg_format = format;
  220. init_fmt = 1;
  221. }
  222. /* -----------------------------------------------------------------------------
  223. * format_filename()
  224. *
  225. * Remove double backslashes in Windows filename paths for display
  226. * ----------------------------------------------------------------------------- */
  227. static String *
  228. format_filename(const String_or_char *filename) {
  229. String *formatted_filename = NewString(filename);
  230. #if defined(_WIN32)
  231. Replaceall(formatted_filename,"\\\\","\\");
  232. #endif
  233. return formatted_filename;
  234. }