1 /* ------------------------------------------------------------------------- */
2 /* "errors" : Warnings, errors and fatal errors */
3 /* (with error throwback code for RISC OS machines) */
5 /* Part of Inform 6.41 */
6 /* copyright (c) Graham Nelson 1993 - 2022 */
8 /* Inform is free software: you can redistribute it and/or modify */
9 /* it under the terms of the GNU General Public License as published by */
10 /* the Free Software Foundation, either version 3 of the License, or */
11 /* (at your option) any later version. */
13 /* Inform is distributed in the hope that it will be useful, */
14 /* but WITHOUT ANY WARRANTY; without even the implied warranty of */
15 /* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the */
16 /* GNU General Public License for more details. */
18 /* You should have received a copy of the GNU General Public License */
19 /* along with Inform. If not, see https://gnu.org/licenses/ */
21 /* ------------------------------------------------------------------------- */
25 #define ERROR_BUFLEN (256)
26 static char error_message_buff[ERROR_BUFLEN+4]; /* room for ellipsis */
28 /* ------------------------------------------------------------------------- */
29 /* Error preamble printing. */
30 /* ------------------------------------------------------------------------- */
32 ErrorPosition ErrorReport; /* Maintained by "lexer.c" */
34 static char other_pos_buff[ERROR_BUFLEN+1]; /* Used by location_text() */
36 static void print_preamble(void)
38 /* Only really prints the preamble to an error or warning message:
40 e.g. "jigsaw.apollo", line 24:
42 The format is controllable (from an ICL switch) since this assists
43 the working of some development environments. */
45 int j, with_extension_flag = FALSE; char *p;
47 j = ErrorReport.file_number;
48 if (j <= 0 || j > total_files) p = ErrorReport.source;
49 else p = InputFiles[j-1].filename;
55 case 0: /* RISC OS error message format */
57 if (!(ErrorReport.main_flag)) printf("\"%s\", ", p);
58 printf("line %d: ", ErrorReport.line_number);
60 if (ErrorReport.orig_file) {
62 if (ErrorReport.orig_file <= 0 || ErrorReport.orig_file > total_files)
63 op = ErrorReport.orig_source;
65 op = InputFiles[ErrorReport.orig_file-1].filename;
66 printf("(\"%s\"", op);
67 if (ErrorReport.orig_line) {
68 printf(", %d", ErrorReport.orig_line);
69 if (ErrorReport.orig_char) {
70 printf(":%d", ErrorReport.orig_char);
77 case 1: /* Microsoft error message format */
79 for (j=0; p[j]!=0; j++)
80 { if (p[j] == FN_SEP) with_extension_flag = TRUE;
81 if (p[j] == '.') with_extension_flag = FALSE;
84 if (with_extension_flag) printf("%s", Source_Extension);
85 printf("(%d)", ErrorReport.line_number);
87 if (ErrorReport.orig_file) {
89 if (ErrorReport.orig_file <= 0 || ErrorReport.orig_file > total_files)
90 op = ErrorReport.orig_source;
92 op = InputFiles[ErrorReport.orig_file-1].filename;
94 if (ErrorReport.orig_line) {
95 printf("(%d", ErrorReport.orig_line);
96 if (ErrorReport.orig_char) {
97 printf(":%d", ErrorReport.orig_char);
106 case 2: /* Macintosh Programmer's Workshop error message format */
108 printf("File \"%s\"; Line %d", p, ErrorReport.line_number);
110 if (ErrorReport.orig_file) {
112 if (ErrorReport.orig_file <= 0 || ErrorReport.orig_file > total_files)
113 op = ErrorReport.orig_source;
115 op = InputFiles[ErrorReport.orig_file-1].filename;
116 printf(": (\"%s\"", op);
117 if (ErrorReport.orig_line) {
118 printf("; Line %d", ErrorReport.orig_line);
119 if (ErrorReport.orig_char) {
120 printf("; Char %d", ErrorReport.orig_char);
131 static char *location_text(brief_location report_line)
137 /* Convert the location to a brief string.
138 (Some error messages need to report a secondary location.)
139 This uses the static buffer other_pos_buff. */
141 ErrorPosition errpos;
142 errpos.file_number = -1;
143 errpos.source = NULL;
144 errpos.line_number = 0;
145 errpos.main_flag = 0;
146 errpos.orig_source = NULL;
147 export_brief_location(report_line, &errpos);
149 j = errpos.file_number;
150 if (j <= 0 || j > total_files) p = errpos.source;
151 else p = InputFiles[j-1].filename;
153 if (!p && errpos.line_number == 0) {
155 strcpy(other_pos_buff, "compiler setup");
156 return other_pos_buff;
163 if (!(errpos.main_flag)) {
164 snprintf(other_pos_buff+len, ERROR_BUFLEN-len,
166 len = strlen(other_pos_buff);
168 snprintf(other_pos_buff+len, ERROR_BUFLEN-len,
169 "line %d", errpos.line_number);
171 return other_pos_buff;
174 static void ellipsize_error_message_buff(void)
176 /* If the error buffer was actually filled up by a message, it was
177 probably truncated too. Add an ellipsis, for which we left
178 extra room. (Yes, yes; errors that are *exactly* 255 characters
179 long will suffer an unnecessary ellipsis.) */
180 if (strlen(error_message_buff) == ERROR_BUFLEN-1)
181 strcat(error_message_buff, "...");
184 /* ------------------------------------------------------------------------- */
185 /* Fatal errors (which have style 0) */
186 /* ------------------------------------------------------------------------- */
188 extern void fatalerror(char *s)
191 printf("Fatal error: %s\n",s);
192 if (no_compiler_errors > 0) print_sorry_message();
200 abort_transcript_file();
202 longjmp(g_fallback, 1);
207 extern void fatalerror_named(char *m, char *fn)
208 { snprintf(error_message_buff, ERROR_BUFLEN, "%s \"%s\"", m, fn);
209 ellipsize_error_message_buff();
210 fatalerror(error_message_buff);
213 extern void memory_out_error(int32 size, int32 howmany, char *name)
215 snprintf(error_message_buff, ERROR_BUFLEN,
216 "Run out of memory allocating %d bytes for %s", size, name);
218 snprintf(error_message_buff, ERROR_BUFLEN,
219 "Run out of memory allocating array of %dx%d bytes for %s",
220 howmany, size, name);
221 ellipsize_error_message_buff();
222 fatalerror(error_message_buff);
225 /* ------------------------------------------------------------------------- */
226 /* Survivable diagnostics: */
227 /* compilation errors style 1 */
228 /* warnings style 2 */
229 /* linkage errors style 3 (no longer used) */
230 /* compiler errors style 4 (these should never happen and */
231 /* indicate a bug in Inform) */
232 /* ------------------------------------------------------------------------- */
234 int no_errors, no_warnings, no_suppressed_warnings, no_compiler_errors;
236 char *forerrors_buff;
237 int forerrors_pointer;
239 static void message(int style, char *s)
241 if (hash_printed_since_newline) printf("\n");
242 hash_printed_since_newline = FALSE;
245 { case 1: printf("Error: "); no_errors++; break;
246 case 2: printf("Warning: "); no_warnings++; break;
247 case 3: printf("Error: [linking] "); no_errors++; break;
248 case 4: printf("*** Compiler error: ");
249 no_compiler_errors++; break;
253 throwback(((style <= 2) ? style : 1), s);
256 ProcessEvents (&g_proc);
260 abort_transcript_file();
261 longjmp (g_fallback, 1);
264 if ((!concise_switch) && (forerrors_pointer > 0) && (style <= 2))
265 { forerrors_buff[forerrors_pointer] = 0;
266 sprintf(forerrors_buff+68," ...etc");
267 printf("> %s\n",forerrors_buff);
271 /* ------------------------------------------------------------------------- */
272 /* Style 1: Error message routines */
273 /* ------------------------------------------------------------------------- */
275 extern void error(char *s)
276 { if (no_errors == MAX_ERRORS)
277 fatalerror("Too many errors: giving up");
281 extern void error_named(char *s1, char *s2)
282 { snprintf(error_message_buff, ERROR_BUFLEN,"%s \"%s\"",s1,s2);
283 ellipsize_error_message_buff();
284 error(error_message_buff);
287 extern void error_numbered(char *s1, int val)
289 snprintf(error_message_buff, ERROR_BUFLEN,"%s %d.",s1,val);
290 ellipsize_error_message_buff();
291 error(error_message_buff);
294 extern void error_named_at(char *s1, char *s2, brief_location report_line)
297 ErrorPosition E = ErrorReport;
298 export_brief_location(report_line, &ErrorReport);
300 snprintf(error_message_buff, ERROR_BUFLEN,"%s \"%s\"",s1,s2);
301 ellipsize_error_message_buff();
303 i = concise_switch; concise_switch = TRUE;
304 error(error_message_buff);
305 ErrorReport = E; concise_switch = i;
308 extern void no_such_label(char *lname)
309 { error_named("No such label as",lname);
312 extern void ebf_error(char *s1, char *s2)
313 { snprintf(error_message_buff, ERROR_BUFLEN, "Expected %s but found %s", s1, s2);
314 ellipsize_error_message_buff();
315 error(error_message_buff);
318 extern void ebf_symbol_error(char *s1, char *name, char *type, brief_location report_line)
319 { snprintf(error_message_buff, ERROR_BUFLEN, "\"%s\" is a name already in use and may not be used as a %s (%s \"%s\" was defined at %s)", name, s1, type, name, location_text(report_line));
320 ellipsize_error_message_buff();
321 error(error_message_buff);
324 extern void char_error(char *s, int ch)
327 uni = iso_to_unicode(ch);
329 if (character_set_unicode)
330 snprintf(error_message_buff, ERROR_BUFLEN, "%s (unicode) $%04x", s, uni);
331 else if (uni >= 0x100)
332 { snprintf(error_message_buff, ERROR_BUFLEN,
333 "%s (unicode) $%04x = (ISO %s) $%02x", s, uni,
334 name_of_iso_set(character_set_setting), ch);
337 snprintf(error_message_buff, ERROR_BUFLEN, "%s (ISO Latin1) $%02x", s, uni);
339 /* If the character set is set to Latin-1, and the char in question
340 is a printable Latin-1 character, we print it in the error message.
341 This conflates the source-text charset with the terminal charset,
342 really, but it's not a big deal. */
344 if (((uni>=32) && (uni<127))
345 || (((uni >= 0xa1) && (uni <= 0xff))
346 && (character_set_setting==1) && (!character_set_unicode)))
347 { int curlen = strlen(error_message_buff);
348 snprintf(error_message_buff+curlen, ERROR_BUFLEN-curlen,
349 ", i.e., '%c'", uni);
352 ellipsize_error_message_buff();
353 error(error_message_buff);
356 extern void unicode_char_error(char *s, int32 uni)
359 snprintf(error_message_buff, ERROR_BUFLEN, "%s (unicode) $%04x", s, uni);
361 snprintf(error_message_buff, ERROR_BUFLEN, "%s (ISO Latin1) $%02x", s, uni);
363 /* See comment above. */
365 if (((uni>=32) && (uni<127))
366 || (((uni >= 0xa1) && (uni <= 0xff))
367 && (character_set_setting==1) && (!character_set_unicode)))
368 { int curlen = strlen(error_message_buff);
369 snprintf(error_message_buff+curlen, ERROR_BUFLEN-curlen,
370 ", i.e., '%c'", uni);
373 ellipsize_error_message_buff();
374 error(error_message_buff);
377 extern void error_max_dynamic_strings(int index)
379 if (index >= 96 && !glulx_mode)
380 snprintf(error_message_buff, ERROR_BUFLEN, "Only dynamic strings @(00) to @(95) may be used in Z-code");
381 else if (MAX_DYNAMIC_STRINGS == 0)
382 snprintf(error_message_buff, ERROR_BUFLEN, "Dynamic strings may not be used, because $MAX_DYNAMIC_STRINGS has been set to 0. Increase MAX_DYNAMIC_STRINGS.");
383 else if (MAX_DYNAMIC_STRINGS == 32 && !glulx_mode)
384 snprintf(error_message_buff, ERROR_BUFLEN, "Only dynamic strings @(00) to @(%02d) may be used, because $MAX_DYNAMIC_STRINGS has its default value of %d. Increase MAX_DYNAMIC_STRINGS.", MAX_DYNAMIC_STRINGS-1, MAX_DYNAMIC_STRINGS);
386 snprintf(error_message_buff, ERROR_BUFLEN, "Only dynamic strings @(00) to @(%02d) may be used, because $MAX_DYNAMIC_STRINGS has been set to %d. Increase MAX_DYNAMIC_STRINGS.", MAX_DYNAMIC_STRINGS-1, MAX_DYNAMIC_STRINGS);
388 ellipsize_error_message_buff();
389 error(error_message_buff);
392 extern void error_max_abbreviations(int index)
394 /* This is only called for Z-code. */
396 error("The number of abbreviations has exceeded 96, the limit in Z-code");
398 error("The number of abbreviations has exceeded MAX_ABBREVS. Increase MAX_ABBREVS.");
401 /* ------------------------------------------------------------------------- */
402 /* Style 2: Warning message routines */
403 /* ------------------------------------------------------------------------- */
405 extern void warning(char *s1)
406 { if (nowarnings_switch) { no_suppressed_warnings++; return; }
410 extern void warning_numbered(char *s1, int val)
411 { if (nowarnings_switch) { no_suppressed_warnings++; return; }
412 snprintf(error_message_buff, ERROR_BUFLEN,"%s %d.", s1, val);
413 ellipsize_error_message_buff();
414 message(2,error_message_buff);
417 extern void warning_named(char *s1, char *s2)
419 if (nowarnings_switch) { no_suppressed_warnings++; return; }
420 snprintf(error_message_buff, ERROR_BUFLEN,"%s \"%s\"", s1, s2);
421 ellipsize_error_message_buff();
422 message(2,error_message_buff);
425 extern void symtype_warning(char *context, char *name, char *type, char *wanttype)
427 if (nowarnings_switch) { no_suppressed_warnings++; return; }
429 snprintf(error_message_buff, ERROR_BUFLEN, "In %s, expected %s but found %s \"%s\"", context, wanttype, type, name);
431 snprintf(error_message_buff, ERROR_BUFLEN, "In %s, expected %s but found %s", context, wanttype, type);
432 ellipsize_error_message_buff();
433 message(2,error_message_buff);
436 extern void dbnu_warning(char *type, char *name, brief_location report_line)
438 ErrorPosition E = ErrorReport;
439 if (nowarnings_switch) { no_suppressed_warnings++; return; }
440 export_brief_location(report_line, &ErrorReport);
441 snprintf(error_message_buff, ERROR_BUFLEN, "%s \"%s\" declared but not used", type, name);
442 ellipsize_error_message_buff();
443 i = concise_switch; concise_switch = TRUE;
444 message(2,error_message_buff);
449 extern void uncalled_routine_warning(char *type, char *name, brief_location report_line)
451 /* This is called for functions which have been detected by the
452 track-unused-routines module. These will often (but not always)
453 be also caught by dbnu_warning(), which tracks symbols rather
454 than routine addresses. */
455 ErrorPosition E = ErrorReport;
456 if (nowarnings_switch) { no_suppressed_warnings++; return; }
457 export_brief_location(report_line, &ErrorReport);
458 if (OMIT_UNUSED_ROUTINES)
459 snprintf(error_message_buff, ERROR_BUFLEN, "%s \"%s\" unused and omitted", type, name);
461 snprintf(error_message_buff, ERROR_BUFLEN, "%s \"%s\" unused (not omitted)", type, name);
462 ellipsize_error_message_buff();
463 i = concise_switch; concise_switch = TRUE;
464 message(2,error_message_buff);
469 extern void obsolete_warning(char *s1)
470 { if (is_systemfile()==1) return;
471 if (obsolete_switch || nowarnings_switch)
472 { no_suppressed_warnings++; return; }
473 snprintf(error_message_buff, ERROR_BUFLEN, "Obsolete usage: %s",s1);
474 ellipsize_error_message_buff();
475 message(2,error_message_buff);
478 /* ------------------------------------------------------------------------- */
479 /* Style 4: Compiler error message routines */
480 /* ------------------------------------------------------------------------- */
482 extern void print_sorry_message(void)
484 "***********************************************************************\n\
485 * 'Compiler errors' should never occur if Inform is working properly. *\n\
486 * Check to see if there is a more recent version available, from which *\n\
487 * the problem may have been removed. If not, please report this fault *\n\
488 * and if at all possible, please include your source code, as faults *\n\
489 * such as these are rare and often difficult to reproduce. Sorry. *\n\
490 ***********************************************************************\n");
493 extern int compiler_error(char *s)
495 if (no_errors > 0) return FALSE;
496 if (no_compiler_errors==MAX_ERRORS)
497 fatalerror("Too many compiler errors: giving up");
502 extern int compiler_error_named(char *s1, char *s2)
504 if (no_errors > 0) return FALSE;
505 snprintf(error_message_buff, ERROR_BUFLEN, "%s \"%s\"",s1,s2);
506 ellipsize_error_message_buff();
507 compiler_error(error_message_buff);
511 /* ------------------------------------------------------------------------- */
512 /* Code for the Acorn RISC OS operating system, donated by Robin Watts, */
513 /* to provide error throwback under the DDE environment */
514 /* ------------------------------------------------------------------------- */
518 #define DDEUtils_ThrowbackStart 0x42587
519 #define DDEUtils_ThrowbackSend 0x42588
520 #define DDEUtils_ThrowbackEnd 0x42589
524 extern void throwback_start(void)
525 { _kernel_swi_regs regs;
526 if (throwback_switch)
527 _kernel_swi(DDEUtils_ThrowbackStart, ®s, ®s);
530 extern void throwback_end(void)
531 { _kernel_swi_regs regs;
532 if (throwback_switch)
533 _kernel_swi(DDEUtils_ThrowbackEnd, ®s, ®s);
536 int throwback_started = FALSE;
538 extern void throwback(int severity, char * error)
539 { _kernel_swi_regs regs;
540 if (!throwback_started)
541 { throwback_started = TRUE;
544 if (throwback_switch)
546 if ((ErrorReport.file_number == -1)
547 || (ErrorReport.file_number == 0))
548 regs.r[2] = (int) (InputFiles[0].filename);
549 else regs.r[2] = (int) (InputFiles[ErrorReport.file_number-1].filename);
550 regs.r[3] = ErrorReport.line_number;
551 regs.r[4] = (2-severity);
552 regs.r[5] = (int) error;
553 _kernel_swi(DDEUtils_ThrowbackSend, ®s, ®s);
559 /* ========================================================================= */
560 /* Data structure management routines */
561 /* ------------------------------------------------------------------------- */
563 extern void init_errors_vars(void)
564 { forerrors_buff = NULL;
565 no_errors = 0; no_warnings = 0; no_suppressed_warnings = 0;
566 no_compiler_errors = 0;
569 extern void errors_begin_pass(void)
570 { ErrorReport.line_number = 0;
571 ErrorReport.file_number = -1;
572 ErrorReport.source = "<no text read yet>";
573 ErrorReport.main_flag = FALSE;
574 ErrorReport.orig_source = NULL;
575 ErrorReport.orig_file = 0;
576 ErrorReport.orig_line = 0;
577 ErrorReport.orig_char = 0;
580 extern void errors_allocate_arrays(void)
581 { forerrors_buff = my_malloc(FORERRORS_SIZE, "errors buffer");
584 extern void errors_free_arrays(void)
585 { my_free(&forerrors_buff, "errors buffer");
588 /* ========================================================================= */