Update to commit a469d404a7dc4e87e18f367eb4d8e05fc32d20a7
[inform.git] / src / errors.c
1 /* ------------------------------------------------------------------------- */
2 /*   "errors" : Warnings, errors and fatal errors                            */
3 /*              (with error throwback code for RISC OS machines)             */
4 /*                                                                           */
5 /*   Part of Inform 6.40                                                     */
6 /*   copyright (c) Graham Nelson 1993 - 2022                                 */
7 /*                                                                           */
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.                                       */
12 /*                                                                           */
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.                              */
17 /*                                                                           */
18 /* You should have received a copy of the GNU General Public License         */
19 /* along with Inform. If not, see https://gnu.org/licenses/                  */
20 /*                                                                           */
21 /* ------------------------------------------------------------------------- */
22
23 #include "header.h"
24
25 #define ERROR_BUFLEN (256)
26 static char error_message_buff[ERROR_BUFLEN+4]; /* room for ellipsis */
27
28 /* ------------------------------------------------------------------------- */
29 /*   Error preamble printing.                                                */
30 /* ------------------------------------------------------------------------- */
31
32 ErrorPosition ErrorReport;             /*  Maintained by "lexer.c"           */
33
34 static char other_pos_buff[ERROR_BUFLEN+1];       /* Used by location_text() */
35
36 static void print_preamble(void)
37 {
38     /*  Only really prints the preamble to an error or warning message:
39
40         e.g.  "jigsaw.apollo", line 24:
41
42         The format is controllable (from an ICL switch) since this assists
43         the working of some development environments.                        */
44
45     int j, with_extension_flag = FALSE; char *p;
46
47     j = ErrorReport.file_number;
48     if (j <= 0 || j > total_files) p = ErrorReport.source;
49     else p = InputFiles[j-1].filename;
50
51     if (!p) p = "";
52     
53     switch(error_format)
54     {
55         case 0:  /* RISC OS error message format */
56
57             if (!(ErrorReport.main_flag)) printf("\"%s\", ", p);
58             printf("line %d: ", ErrorReport.line_number);
59             
60             if (ErrorReport.orig_file) {
61                 char *op;
62                 if (ErrorReport.orig_file <= 0 || ErrorReport.orig_file > total_files)
63                     op = ErrorReport.orig_source;
64                 else
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);
71                     }
72                 }
73                 printf("): ");
74             }
75             break;
76
77         case 1:  /* Microsoft error message format */
78
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;
82             }
83             printf("%s", p);
84             if (with_extension_flag) printf("%s", Source_Extension);
85             printf("(%d)", ErrorReport.line_number);
86             
87             if (ErrorReport.orig_file) {
88                 char *op;
89                 if (ErrorReport.orig_file <= 0 || ErrorReport.orig_file > total_files)
90                     op = ErrorReport.orig_source;
91                 else
92                     op = InputFiles[ErrorReport.orig_file-1].filename;
93                 printf("|%s", op);
94                 if (ErrorReport.orig_line) {
95                     printf("(%d", ErrorReport.orig_line);
96                     if (ErrorReport.orig_char) {
97                         printf(":%d", ErrorReport.orig_char);
98                     }
99                     printf(")");
100                 }
101             }
102             
103             printf(": ");
104             break;
105
106         case 2:  /* Macintosh Programmer's Workshop error message format */
107
108             printf("File \"%s\"; Line %d", p, ErrorReport.line_number);
109             
110             if (ErrorReport.orig_file) {
111                 char *op;
112                 if (ErrorReport.orig_file <= 0 || ErrorReport.orig_file > total_files)
113                     op = ErrorReport.orig_source;
114                 else
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);
121                     }
122                 }
123                 printf(")");
124             }
125
126             printf("\t# ");
127             break;
128     }
129 }
130
131 static char *location_text(brief_location report_line)
132 {
133     int j;
134     char *p;
135     int len;
136
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. */
140     
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);
148     
149     j = errpos.file_number;
150     if (j <= 0 || j > total_files) p = errpos.source;
151     else p = InputFiles[j-1].filename;
152     
153     if (!p && errpos.line_number == 0) {
154         /* Special case */
155         strcpy(other_pos_buff, "compiler setup");
156         return other_pos_buff;
157     }
158     
159     if (!p) p = "";
160
161     len = 0;
162     
163     if (!(errpos.main_flag)) {
164         snprintf(other_pos_buff+len, ERROR_BUFLEN-len,
165                  "\"%s\", ", p);
166         len = strlen(other_pos_buff);
167     }
168     snprintf(other_pos_buff+len, ERROR_BUFLEN-len,
169              "line %d", errpos.line_number);
170
171     return other_pos_buff;
172 }
173
174 static void ellipsize_error_message_buff(void)
175 {
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, "...");
182 }
183
184 /* ------------------------------------------------------------------------- */
185 /*   Fatal errors (which have style 0)                                       */
186 /* ------------------------------------------------------------------------- */
187
188 extern void fatalerror(char *s)
189 {   print_preamble();
190
191     printf("Fatal error: %s\n",s);
192     if (no_compiler_errors > 0) print_sorry_message();
193
194 #ifdef ARC_THROWBACK
195     throwback(0, s);
196     throwback_end();
197 #endif
198 #ifdef MAC_FACE
199     close_all_source();
200     abort_transcript_file();
201     free_arrays();
202     longjmp(g_fallback, 1);
203 #endif
204     exit(1);
205 }
206
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);
211 }
212
213 extern void memory_out_error(int32 size, int32 howmany, char *name)
214 {   if (howmany == 1)
215         snprintf(error_message_buff, ERROR_BUFLEN,
216             "Run out of memory allocating %d bytes for %s", size, name);
217     else
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);
223 }
224
225 /* ------------------------------------------------------------------------- */
226 /*   Survivable diagnostics:                                                 */
227 /*      compilation errors   style 1                                         */
228 /*      warnings             style 2                                         */
229 /*      linkage errors       style 3                                         */
230 /*      compiler errors      style 4 (these should never happen and          */
231 /*                                    indicate a bug in Inform)              */
232 /* ------------------------------------------------------------------------- */
233
234 int no_errors, no_warnings, no_suppressed_warnings, no_link_errors,
235     no_compiler_errors;
236
237 char *forerrors_buff;
238 int  forerrors_pointer;
239
240 static void message(int style, char *s)
241 {
242     if (hash_printed_since_newline) printf("\n");
243     hash_printed_since_newline = FALSE;
244     print_preamble();
245     switch(style)
246     {   case 1: printf("Error: "); no_errors++; break;
247         case 2: printf("Warning: "); no_warnings++; break;
248         case 3: printf("Error:  [linking '%s']  ", current_module_filename);
249                 no_link_errors++; no_errors++; break;
250         case 4: printf("*** Compiler error: ");
251                 no_compiler_errors++; break;
252     }
253     printf(" %s\n", s);
254 #ifdef ARC_THROWBACK
255     throwback(((style <= 2) ? style : 1), s);
256 #endif
257 #ifdef MAC_FACE
258     ProcessEvents (&g_proc);
259     if (g_proc != true)
260     {   free_arrays();
261         close_all_source ();
262         abort_transcript_file();
263         longjmp (g_fallback, 1);
264     }
265 #endif
266     if ((!concise_switch) && (forerrors_pointer > 0) && (style <= 2))
267     {   forerrors_buff[forerrors_pointer] = 0;
268         sprintf(forerrors_buff+68,"  ...etc");
269         printf("> %s\n",forerrors_buff);
270     }
271 }
272
273 /* ------------------------------------------------------------------------- */
274 /*   Style 1: Error message routines                                         */
275 /* ------------------------------------------------------------------------- */
276
277 extern void error(char *s)
278 {   if (no_errors == MAX_ERRORS)
279         fatalerror("Too many errors: giving up");
280     message(1,s);
281 }
282
283 extern void error_named(char *s1, char *s2)
284 {   snprintf(error_message_buff, ERROR_BUFLEN,"%s \"%s\"",s1,s2);
285     ellipsize_error_message_buff();
286     error(error_message_buff);
287 }
288
289 extern void error_numbered(char *s1, int val)
290 {
291     snprintf(error_message_buff, ERROR_BUFLEN,"%s %d.",s1,val);
292     ellipsize_error_message_buff();
293     error(error_message_buff);
294 }
295
296 extern void error_named_at(char *s1, char *s2, brief_location report_line)
297 {   int i;
298
299     ErrorPosition E = ErrorReport;
300     export_brief_location(report_line, &ErrorReport);
301
302     snprintf(error_message_buff, ERROR_BUFLEN,"%s \"%s\"",s1,s2);
303     ellipsize_error_message_buff();
304
305     i = concise_switch; concise_switch = TRUE;
306     error(error_message_buff);
307     ErrorReport = E; concise_switch = i;
308 }
309
310 extern void no_such_label(char *lname)
311 {   error_named("No such label as",lname);
312 }
313
314 extern void ebf_error(char *s1, char *s2)
315 {   snprintf(error_message_buff, ERROR_BUFLEN, "Expected %s but found %s", s1, s2);
316     ellipsize_error_message_buff();
317     error(error_message_buff);
318 }
319
320 extern void ebf_symbol_error(char *s1, char *name, char *type, brief_location report_line)
321 {   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));
322     ellipsize_error_message_buff();
323     error(error_message_buff);
324 }
325
326 extern void char_error(char *s, int ch)
327 {   int32 uni;
328
329     uni = iso_to_unicode(ch);
330
331     if (character_set_unicode)
332         snprintf(error_message_buff, ERROR_BUFLEN, "%s (unicode) $%04x", s, uni);
333     else if (uni >= 0x100)
334     {   snprintf(error_message_buff, ERROR_BUFLEN,
335             "%s (unicode) $%04x = (ISO %s) $%02x", s, uni,
336             name_of_iso_set(character_set_setting), ch);
337     }
338     else
339         snprintf(error_message_buff, ERROR_BUFLEN, "%s (ISO Latin1) $%02x", s, uni);
340
341     /* If the character set is set to Latin-1, and the char in question
342        is a printable Latin-1 character, we print it in the error message.
343        This conflates the source-text charset with the terminal charset,
344        really, but it's not a big deal. */
345
346     if (((uni>=32) && (uni<127))
347         || (((uni >= 0xa1) && (uni <= 0xff))
348         && (character_set_setting==1) && (!character_set_unicode))) 
349     {   int curlen = strlen(error_message_buff);
350         snprintf(error_message_buff+curlen, ERROR_BUFLEN-curlen,
351             ", i.e., '%c'", uni);
352     }
353
354     ellipsize_error_message_buff();
355     error(error_message_buff);
356 }
357
358 extern void unicode_char_error(char *s, int32 uni)
359 {
360     if (uni >= 0x100)
361         snprintf(error_message_buff, ERROR_BUFLEN, "%s (unicode) $%04x", s, uni);
362     else
363         snprintf(error_message_buff, ERROR_BUFLEN, "%s (ISO Latin1) $%02x", s, uni);
364
365     /* See comment above. */
366
367     if (((uni>=32) && (uni<127))
368         || (((uni >= 0xa1) && (uni <= 0xff))
369         && (character_set_setting==1) && (!character_set_unicode)))
370     {   int curlen = strlen(error_message_buff);
371         snprintf(error_message_buff+curlen, ERROR_BUFLEN-curlen,
372             ", i.e., '%c'", uni);
373     }
374
375     ellipsize_error_message_buff();
376     error(error_message_buff);
377 }
378
379 extern void error_max_dynamic_strings(int index)
380 {
381     if (index >= 96 && !glulx_mode)
382         snprintf(error_message_buff, ERROR_BUFLEN, "Only dynamic strings @(00) to @(95) may be used in Z-code");
383     else if (MAX_DYNAMIC_STRINGS == 0)
384         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.");
385     else if (MAX_DYNAMIC_STRINGS == 32 && !glulx_mode)
386         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);
387     else
388         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);
389
390     ellipsize_error_message_buff();
391     error(error_message_buff);
392 }
393
394 extern void error_max_abbreviations(int index)
395 {
396     /* This is only called for Z-code. */
397     if (index >= 96)
398         error("The number of abbreviations has exceeded 96, the limit in Z-code");
399     else
400         error("The number of abbreviations has exceeded MAX_ABBREVS. Increase MAX_ABBREVS.");
401 }
402
403 /* ------------------------------------------------------------------------- */
404 /*   Style 2: Warning message routines                                       */
405 /* ------------------------------------------------------------------------- */
406
407 extern void warning(char *s1)
408 {   if (nowarnings_switch) { no_suppressed_warnings++; return; }
409     message(2,s1);
410 }
411
412 extern void warning_numbered(char *s1, int val)
413 {   if (nowarnings_switch) { no_suppressed_warnings++; return; }
414     snprintf(error_message_buff, ERROR_BUFLEN,"%s %d.", s1, val);
415     ellipsize_error_message_buff();
416     message(2,error_message_buff);
417 }
418
419 extern void warning_named(char *s1, char *s2)
420 {
421     if (nowarnings_switch) { no_suppressed_warnings++; return; }
422     snprintf(error_message_buff, ERROR_BUFLEN,"%s \"%s\"", s1, s2);
423     ellipsize_error_message_buff();
424     message(2,error_message_buff);
425 }
426
427 extern void symtype_warning(char *context, char *name, char *type, char *wanttype)
428 {
429     if (nowarnings_switch) { no_suppressed_warnings++; return; }
430     if (name)
431         snprintf(error_message_buff, ERROR_BUFLEN, "In %s, expected %s but found %s \"%s\"", context, wanttype, type, name);
432     else
433         snprintf(error_message_buff, ERROR_BUFLEN, "In %s, expected %s but found %s", context, wanttype, type);
434     ellipsize_error_message_buff();
435     message(2,error_message_buff);
436 }
437
438 extern void dbnu_warning(char *type, char *name, brief_location report_line)
439 {   int i;
440     ErrorPosition E = ErrorReport;
441     if (nowarnings_switch) { no_suppressed_warnings++; return; }
442     export_brief_location(report_line, &ErrorReport);
443     snprintf(error_message_buff, ERROR_BUFLEN, "%s \"%s\" declared but not used", type, name);
444     ellipsize_error_message_buff();
445     i = concise_switch; concise_switch = TRUE;
446     message(2,error_message_buff);
447     concise_switch = i;
448     ErrorReport = E;
449 }
450
451 extern void uncalled_routine_warning(char *type, char *name, brief_location report_line)
452 {   int i;
453     /* This is called for functions which have been detected by the
454        track-unused-routines module. These will often (but not always)
455        be also caught by dbnu_warning(), which tracks symbols rather
456        than routine addresses. */
457     ErrorPosition E = ErrorReport;
458     if (nowarnings_switch) { no_suppressed_warnings++; return; }
459     export_brief_location(report_line, &ErrorReport);
460     if (OMIT_UNUSED_ROUTINES)
461         snprintf(error_message_buff, ERROR_BUFLEN, "%s \"%s\" unused and omitted", type, name);
462     else
463         snprintf(error_message_buff, ERROR_BUFLEN, "%s \"%s\" unused (not omitted)", type, name);
464     ellipsize_error_message_buff();
465     i = concise_switch; concise_switch = TRUE;
466     message(2,error_message_buff);
467     concise_switch = i;
468     ErrorReport = E;
469 }
470
471 extern void obsolete_warning(char *s1)
472 {   if (is_systemfile()==1) return;
473     if (obsolete_switch || nowarnings_switch)
474     {   no_suppressed_warnings++; return; }
475     snprintf(error_message_buff, ERROR_BUFLEN, "Obsolete usage: %s",s1);
476     ellipsize_error_message_buff();
477     message(2,error_message_buff);
478 }
479
480 /* ------------------------------------------------------------------------- */
481 /*   Style 3: Link error message routines                                    */
482 /* ------------------------------------------------------------------------- */
483
484 extern void link_error(char *s)
485 {   if (no_errors==MAX_ERRORS) fatalerror("Too many errors: giving up");
486     message(3,s);
487 }
488
489 extern void link_error_named(char *s1, char *s2)
490 {   snprintf(error_message_buff, ERROR_BUFLEN,"%s \"%s\"",s1,s2);
491     ellipsize_error_message_buff();
492     link_error(error_message_buff);
493 }
494
495 /* ------------------------------------------------------------------------- */
496 /*   Style 4: Compiler error message routines                                */
497 /* ------------------------------------------------------------------------- */
498
499 extern void print_sorry_message(void)
500 {   printf(
501 "***********************************************************************\n\
502 * 'Compiler errors' should never occur if Inform is working properly. *\n\
503 * Check to see if there is a more recent version available, from which\n\
504 * the problem may have been removed. If not, please report this fault\n\
505 * and if at all possible, please include your source code, as faults\n\
506 * such as these are rare and often difficult to reproduce. Sorry.\n\
507 ***********************************************************************\n");
508 }
509
510 extern int compiler_error(char *s)
511 {   if (no_link_errors > 0) return FALSE;
512     if (no_errors > 0) return FALSE;
513     if (no_compiler_errors==MAX_ERRORS)
514         fatalerror("Too many compiler errors: giving up");
515     message(4,s);
516     return TRUE;
517 }
518
519 extern int compiler_error_named(char *s1, char *s2)
520 {   if (no_link_errors > 0) return FALSE;
521     if (no_errors > 0) return FALSE;
522     snprintf(error_message_buff, ERROR_BUFLEN, "%s \"%s\"",s1,s2);
523     ellipsize_error_message_buff();
524     compiler_error(error_message_buff);
525     return TRUE;
526 }
527
528 /* ------------------------------------------------------------------------- */
529 /*   Code for the Acorn RISC OS operating system, donated by Robin Watts,    */
530 /*   to provide error throwback under the DDE environment                    */
531 /* ------------------------------------------------------------------------- */
532
533 #ifdef ARC_THROWBACK
534
535 #define DDEUtils_ThrowbackStart 0x42587
536 #define DDEUtils_ThrowbackSend  0x42588
537 #define DDEUtils_ThrowbackEnd   0x42589
538
539 #include "kernel.h"
540
541 extern void throwback_start(void)
542 {    _kernel_swi_regs regs;
543      if (throwback_switch)
544          _kernel_swi(DDEUtils_ThrowbackStart, &regs, &regs);
545 }
546
547 extern void throwback_end(void)
548 {   _kernel_swi_regs regs;
549     if (throwback_switch)
550         _kernel_swi(DDEUtils_ThrowbackEnd, &regs, &regs);
551 }
552
553 int throwback_started = FALSE;
554
555 extern void throwback(int severity, char * error)
556 {   _kernel_swi_regs regs;
557     if (!throwback_started)
558     {   throwback_started = TRUE;
559         throwback_start();
560     }
561     if (throwback_switch)
562     {   regs.r[0] = 1;
563         if ((ErrorReport.file_number == -1)
564             || (ErrorReport.file_number == 0))
565             regs.r[2] = (int) (InputFiles[0].filename);
566         else regs.r[2] = (int) (InputFiles[ErrorReport.file_number-1].filename);
567         regs.r[3] = ErrorReport.line_number;
568         regs.r[4] = (2-severity);
569         regs.r[5] = (int) error;
570        _kernel_swi(DDEUtils_ThrowbackSend, &regs, &regs);
571     }
572 }
573
574 #endif
575
576 /* ========================================================================= */
577 /*   Data structure management routines                                      */
578 /* ------------------------------------------------------------------------- */
579
580 extern void init_errors_vars(void)
581 {   forerrors_buff = NULL;
582     no_errors = 0; no_warnings = 0; no_suppressed_warnings = 0;
583     no_compiler_errors = 0;
584 }
585
586 extern void errors_begin_pass(void)
587 {   ErrorReport.line_number = 0;
588     ErrorReport.file_number = -1;
589     ErrorReport.source = "<no text read yet>";
590     ErrorReport.main_flag = FALSE;
591     ErrorReport.orig_source = NULL;
592     ErrorReport.orig_file = 0;
593     ErrorReport.orig_line = 0;
594     ErrorReport.orig_char = 0;
595 }
596
597 extern void errors_allocate_arrays(void)
598 {   forerrors_buff = my_malloc(FORERRORS_SIZE, "errors buffer");
599 }
600
601 extern void errors_free_arrays(void)
602 {   my_free(&forerrors_buff, "errors buffer");
603 }
604
605 /* ========================================================================= */