aboutsummaryrefslogtreecommitdiffstats
path: root/contrib/tools/swig/Source/Swig/error.c
blob: efd644f9a8e56609a1ad4a6952988aade4ed5d31 (plain) (blame)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
/* ----------------------------------------------------------------------------- 
 * This file is part of SWIG, which is licensed as a whole under version 3 
 * (or any later version) of the GNU General Public License. Some additional
 * terms also apply to certain portions of SWIG. The full details of the SWIG
 * license and copyrights can be found in the LICENSE and COPYRIGHT files
 * included with the SWIG source code as distributed by the SWIG developers
 * and at https://www.swig.org/legal.html.
 *
 * error.c
 *
 * Error handling functions.   These are used to issue warnings and
 * error messages.
 * ----------------------------------------------------------------------------- */

#include "swig.h"
#include <stdarg.h>
#include <ctype.h>

/* -----------------------------------------------------------------------------
 * Commentary on the warning filter.
 *
 * The warning filter is a string of numbers prefaced by (-) or (+) to
 * indicate whether or not a warning message is displayed.  For example:
 *
 *      "-304-201-140+210+201"
 *
 * The filter string is scanned left to right and the first occurrence
 * of a warning number is used to determine printing behavior.
 *
 * The same number may appear more than once in the string.  For example, in the 
 * above string, "201" appears twice.  This simply means that warning 201
 * was disabled after it was previously enabled.  This may only be temporary
 * setting--the first number may be removed later in which case the warning
 * is reenabled.
 * ----------------------------------------------------------------------------- */

#if defined(_WIN32)
#  define  DEFAULT_ERROR_MSG_FORMAT EMF_MICROSOFT
#else
#  define  DEFAULT_ERROR_MSG_FORMAT EMF_STANDARD
#endif
static ErrorMessageFormat msg_format = DEFAULT_ERROR_MSG_FORMAT;
static int silence = 0;		/* Silent operation */
static String *filter = 0;	/* Warning filter */
static int warnall = 0;
static int nwarning = 0;
static int nerrors = 0;

static int init_fmt = 0;
static char wrn_wnum_fmt[64];
static char wrn_nnum_fmt[64];
static char err_line_fmt[64];
static char err_eof_fmt[64];
static char diag_line_fmt[64];
static char diag_eof_fmt[64];

static String *format_filename(const_String_or_char_ptr filename);

/* -----------------------------------------------------------------------------
 * Swig_warning()
 *
 * Issue a warning message on stderr.
 * ----------------------------------------------------------------------------- */

void Swig_warning(int wnum, const_String_or_char_ptr filename, int line, const char *fmt, ...) {
  String *out;
  char *msg;
  int wrn = 1;
  va_list ap;
  if (silence)
    return;
  if (!init_fmt)
    Swig_error_msg_format(DEFAULT_ERROR_MSG_FORMAT);

  va_start(ap, fmt);

  out = NewStringEmpty();
  vPrintf(out, fmt, ap);

  msg = Char(out);
  if (isdigit((unsigned char) *msg)) {
    unsigned long result = strtoul(msg, &msg, 10);
    if (msg != Char(out)) {
      msg++;
      wnum = result;
    }
  }

  /* Check in the warning filter */
  if (filter) {
    char temp[32];
    char *c;
    char *f = Char(filter);
    sprintf(temp, "%d", wnum);
    while (*f != '\0' && (c = strstr(f, temp))) {
      if (*(c - 1) == '-') {
	wrn = 0;		/* Warning disabled */
        break;
      }
      if (*(c - 1) == '+') {
	wrn = 1;		/* Warning enabled */
        break;
      }
      f += strlen(temp);
    }
  }
  if (warnall || wrn) {
    String *formatted_filename = format_filename(filename);
    String *full_message = NewString("");
    if (wnum) {
      Printf(full_message, wrn_wnum_fmt, formatted_filename, line, wnum);
    } else {
      Printf(full_message, wrn_nnum_fmt, formatted_filename, line);
    }
    Printf(full_message, "%s", msg);
    Printv(stderr, full_message, NIL);
    nwarning++;
    Delete(full_message);
    Delete(formatted_filename);
  }
  Delete(out);
  va_end(ap);
}

/* -----------------------------------------------------------------------------
 * Swig_error()
 *
 * Issue an error message on stderr.
 * ----------------------------------------------------------------------------- */

void Swig_error(const_String_or_char_ptr filename, int line, const char *fmt, ...) {
  va_list ap;
  String *formatted_filename = NULL;
  String *full_message = NULL;

  if (silence)
    return;
  if (!init_fmt)
    Swig_error_msg_format(DEFAULT_ERROR_MSG_FORMAT);

  va_start(ap, fmt);
  formatted_filename = format_filename(filename);
  full_message = NewString("");
  if (line > 0) {
    Printf(full_message, err_line_fmt, formatted_filename, line);
  } else {
    Printf(full_message, err_eof_fmt, formatted_filename);
  }
  vPrintf(full_message, fmt, ap);
  Printv(stderr, full_message, NIL);
  va_end(ap);
  nerrors++;
  Delete(full_message);
  Delete(formatted_filename);
}

/* -----------------------------------------------------------------------------
 * Swig_error_count()
 *
 * Returns number of errors received.
 * ----------------------------------------------------------------------------- */

int Swig_error_count(void) {
  return nerrors;
}

/* -----------------------------------------------------------------------------
 * Swig_error_silent()
 *
 * Set silent flag
 * ----------------------------------------------------------------------------- */

void Swig_error_silent(int s) {
  silence = s;
}


/* -----------------------------------------------------------------------------
 * Swig_warnfilter()
 *
 * Takes a comma separate list of warning numbers and puts in the filter.
 * ----------------------------------------------------------------------------- */

void Swig_warnfilter(const_String_or_char_ptr wlist, int add) {
  char *c;
  char *cw;
  String *s;
  if (!filter)
    filter = NewStringEmpty();

  s = NewString("");
  Clear(s);
  cw = Char(wlist);
  while (*cw != '\0') {
    if (*cw != ' ') {
      Putc(*cw, s);
    }
    ++cw;
  }
  c = Char(s);
  c = strtok(c, ", ");
  while (c) {
    if (isdigit((int) *c) || (*c == '+') || (*c == '-')) {
      /* Even if c is a digit, the rest of the string might not be, eg in the case of typemap 
       * warnings (a bit odd really), eg: %warnfilter(SWIGWARN_TYPEMAP_CHARLEAK_MSG) */
      if (add) {
	Insert(filter, 0, c);
	if (isdigit((int) *c)) {
	  Insert(filter, 0, "-");
	}
      } else {
	char *temp = (char *)Malloc(sizeof(char)*strlen(c) + 2);
	if (isdigit((int) *c)) {
	  sprintf(temp, "-%s", c);
	} else {
	  strcpy(temp, c);
	}
	Replace(filter, temp, "", DOH_REPLACE_FIRST);
	Free(temp);
      }
    }
    c = strtok(NULL, ", ");
  }
  Delete(s);
}

void Swig_warnall(void) {
  warnall = 1;
}


/* ----------------------------------------------------------------------------- 
 * Swig_warn_count()
 *
 * Return the number of warnings
 * ----------------------------------------------------------------------------- */

int Swig_warn_count(void) {
  return nwarning;
}

/* -----------------------------------------------------------------------------
 * Swig_error_msg_format()
 *
 * Set the type of error/warning message display
 * ----------------------------------------------------------------------------- */

void Swig_error_msg_format(ErrorMessageFormat format) {
  const char *error = "Error";
  const char *warning = "Warning";

  const char *fmt_eof = 0;
  const char *fmt_line = 0;

  /* here 'format' could be directly a string instead of an enum, but
     by now a switch is used to translated into one. */
  switch (format) {
  case EMF_MICROSOFT:
    fmt_line = "%s(%d) ";
    fmt_eof = "%s(999999) ";	/* Is there a special character for EOF? Just use a large number. */
    break;
  case EMF_STANDARD:
  default:
    fmt_line = "%s:%d";
    fmt_eof = "%s:EOF";
  }

  sprintf(wrn_wnum_fmt, "%s: %s %%d: ", fmt_line, warning);
  sprintf(wrn_nnum_fmt, "%s: %s: ", fmt_line, warning);
  sprintf(err_line_fmt, "%s: %s: ", fmt_line, error);
  sprintf(err_eof_fmt, "%s: %s: ", fmt_eof, error);
  sprintf(diag_line_fmt, "%s: ", fmt_line);
  sprintf(diag_eof_fmt, "%s: ", fmt_eof);

  msg_format = format;
  init_fmt = 1;
}

/* -----------------------------------------------------------------------------
 * format_filename()
 *
 * Remove double backslashes in Windows filename paths for display
 * ----------------------------------------------------------------------------- */
static String *format_filename(const_String_or_char_ptr filename) {
  String *formatted_filename = NewString(filename);
#if defined(_WIN32)
  Replaceall(formatted_filename, "\\\\", "\\");
#endif
  return formatted_filename;
}

/* -----------------------------------------------------------------------------
 * Swig_stringify_with_location()
 *
 * Return a string representation of any DOH object with line and file location
 * information in the appropriate error message format. The string representation
 * is enclosed within [] brackets after the line and file information.
 * ----------------------------------------------------------------------------- */

String *Swig_stringify_with_location(DOH *object) {
  String *str = NewStringEmpty();

  if (!init_fmt)
    Swig_error_msg_format(DEFAULT_ERROR_MSG_FORMAT);

  if (object) {
    int line = Getline(object);
    String *formatted_filename = format_filename(Getfile(object));
    if (line > 0) {
      Printf(str, diag_line_fmt, formatted_filename, line);
    } else {
      Printf(str, diag_eof_fmt, formatted_filename);
    }
    if (Len(object) == 0) {
      Printf(str, "[EMPTY]");
    } else {
      Printf(str, "[%s]", object);
    }
    Delete(formatted_filename);
  } else {
    Printf(str, "[NULL]");
  }

  return str;
}

/* -----------------------------------------------------------------------------
 * Swig_diagnostic()
 *
 * Issue a diagnostic message on stdout.
 * ----------------------------------------------------------------------------- */

void Swig_diagnostic(const_String_or_char_ptr filename, int line, const char *fmt, ...) {
  va_list ap;
  String *formatted_filename = NULL;

  if (!init_fmt)
    Swig_error_msg_format(DEFAULT_ERROR_MSG_FORMAT);

  va_start(ap, fmt);
  formatted_filename = format_filename(filename);
  if (line > 0) {
    Printf(stdout, diag_line_fmt, formatted_filename, line);
  } else {
    Printf(stdout, diag_eof_fmt, formatted_filename);
  }
  vPrintf(stdout, fmt, ap);
  va_end(ap);
  Delete(formatted_filename);
}