1 /* -----------------------------------------------------------------------------
2 * See the LICENSE file for information on copyright, usage and redistribution
3 * of SWIG, and the README file for authors - http://www.swig.org/release.html.
7 * Error handling functions. These are used to issue warnings and
9 * ----------------------------------------------------------------------------- */
11 char cvsroot_error_c[] = "$Id: error.c 11080 2009-01-24 13:15:51Z bhy $";
17 /* -----------------------------------------------------------------------------
18 * Commentary on the warning filter.
20 * The warning filter is a string of numbers prefaced by (-) or (+) to
21 * indicate whether or not a warning message is displayed. For example:
23 * "-304-201-140+210+201"
25 * The filter string is scanned left to right and the first occurrence
26 * of a warning number is used to determine printing behavior.
28 * The same number may appear more than once in the string. For example, in the
29 * above string, "201" appears twice. This simply means that warning 201
30 * was disabled after it was previously enabled. This may only be temporary
31 * setting--the first number may be removed later in which case the warning
33 * ----------------------------------------------------------------------------- */
36 # define DEFAULT_ERROR_MSG_FORMAT EMF_MICROSOFT
38 # define DEFAULT_ERROR_MSG_FORMAT EMF_STANDARD
40 static ErrorMessageFormat msg_format = DEFAULT_ERROR_MSG_FORMAT;
41 static int silence = 0; /* Silent operation */
42 static String *filter = 0; /* Warning filter */
43 static int warnall = 0;
44 static int nwarning = 0;
45 static int nerrors = 0;
47 static int init_fmt = 0;
48 static char wrn_wnum_fmt[64];
49 static char wrn_nnum_fmt[64];
50 static char err_line_fmt[64];
51 static char err_eof_fmt[64];
53 static String *format_filename(const_String_or_char_ptr filename);
55 /* -----------------------------------------------------------------------------
58 * Issue a warning message
59 * ----------------------------------------------------------------------------- */
61 void Swig_warning(int wnum, const_String_or_char_ptr filename, int line, const char *fmt, ...) {
69 Swig_error_msg_format(DEFAULT_ERROR_MSG_FORMAT);
73 out = NewStringEmpty();
74 vPrintf(out, fmt, ap);
77 if (isdigit((unsigned char) *msg)) {
78 unsigned long result = strtoul(msg, &msg, 10);
79 if (msg != Char(out)) {
85 /* Check in the warning filter */
89 char *f = Char(filter);
90 sprintf(temp, "%d", wnum);
91 while (*f != '\0' && (c = strstr(f, temp))) {
92 if (*(c - 1) == '-') {
93 wrn = 0; /* Warning disabled */
96 if (*(c - 1) == '+') {
97 wrn = 1; /* Warning enabled */
103 if (warnall || wrn) {
104 String *formatted_filename = format_filename(filename);
106 Printf(stderr, wrn_wnum_fmt, formatted_filename, line, wnum);
108 Printf(stderr, wrn_nnum_fmt, formatted_filename, line);
110 Printf(stderr, "%s", msg);
112 Delete(formatted_filename);
118 /* -----------------------------------------------------------------------------
121 * Issue an error message
122 * ----------------------------------------------------------------------------- */
124 void Swig_error(const_String_or_char_ptr filename, int line, const char *fmt, ...) {
126 String *formatted_filename = NULL;
131 Swig_error_msg_format(DEFAULT_ERROR_MSG_FORMAT);
134 formatted_filename = format_filename(filename);
136 Printf(stderr, err_line_fmt, formatted_filename, line);
138 Printf(stderr, err_eof_fmt, formatted_filename);
140 vPrintf(stderr, fmt, ap);
143 Delete(formatted_filename);
146 /* -----------------------------------------------------------------------------
149 * Returns number of errors received.
150 * ----------------------------------------------------------------------------- */
152 int Swig_error_count(void) {
156 /* -----------------------------------------------------------------------------
157 * Swig_error_silent()
160 * ----------------------------------------------------------------------------- */
162 void Swig_error_silent(int s) {
167 /* -----------------------------------------------------------------------------
170 * Takes a comma separate list of warning numbers and puts in the filter.
171 * ----------------------------------------------------------------------------- */
173 void Swig_warnfilter(const_String_or_char_ptr wlist, int add) {
178 filter = NewStringEmpty();
183 while (*cw != '\0') {
192 if (isdigit((int) *c) || (*c == '+') || (*c == '-')) {
193 /* Even if c is a digit, the rest of the string might not be, eg in the case of typemap
194 * warnings (a bit odd really), eg: %warnfilter(SWIGWARN_TYPEMAP_CHARLEAK_MSG) */
196 Insert(filter, 0, c);
197 if (isdigit((int) *c)) {
198 Insert(filter, 0, "-");
201 char *temp = (char *)malloc(sizeof(char)*strlen(c) + 2);
202 if (isdigit((int) *c)) {
203 sprintf(temp, "-%s", c);
207 Replace(filter, temp, "", DOH_REPLACE_FIRST);
211 c = strtok(NULL, ", ");
216 void Swig_warnall(void) {
221 /* -----------------------------------------------------------------------------
224 * Return the number of warnings
225 * ----------------------------------------------------------------------------- */
227 int Swig_warn_count(void) {
231 /* -----------------------------------------------------------------------------
232 * Swig_error_msg_format()
234 * Set the type of error/warning message display
235 * ----------------------------------------------------------------------------- */
237 void Swig_error_msg_format(ErrorMessageFormat format) {
238 const char *error = "Error";
239 const char *warning = "Warning";
241 const char *fmt_eof = 0;
242 const char *fmt_line = 0;
244 /* here 'format' could be directly a string instead of an enum, but
245 by now a switch is used to translated into one. */
249 fmt_eof = "%s(999999)"; /* Is there a special character for EOF? Just use a large number. */
257 sprintf(wrn_wnum_fmt, "%s: %s(%%d): ", fmt_line, warning);
258 sprintf(wrn_nnum_fmt, "%s: %s: ", fmt_line, warning);
259 sprintf(err_line_fmt, "%s: %s: ", fmt_line, error);
260 sprintf(err_eof_fmt, "%s: %s: ", fmt_eof, error);
266 /* -----------------------------------------------------------------------------
269 * Remove double backslashes in Windows filename paths for display
270 * ----------------------------------------------------------------------------- */
271 static String *format_filename(const_String_or_char_ptr filename) {
272 String *formatted_filename = NewString(filename);
274 Replaceall(formatted_filename, "\\\\", "\\");
276 return formatted_filename;