a71447e9b3aa5757abd087ea4b897113ca2df58f
[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.41                                                     */
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 (no longer used)                        */
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_compiler_errors;
235
236 char *forerrors_buff;
237 int  forerrors_pointer;
238
239 static void message(int style, char *s)
240 {
241     if (hash_printed_since_newline) printf("\n");
242     hash_printed_since_newline = FALSE;
243     print_preamble();
244     switch(style)
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;
250     }
251     printf(" %s\n", s);
252 #ifdef ARC_THROWBACK
253     throwback(((style <= 2) ? style : 1), s);
254 #endif
255 #ifdef MAC_FACE
256     ProcessEvents (&g_proc);
257     if (g_proc != true)
258     {   free_arrays();
259         close_all_source ();
260         abort_transcript_file();
261         longjmp (g_fallback, 1);
262     }
263 #endif
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);
268     }
269 }
270
271 /* ------------------------------------------------------------------------- */
272 /*   Style 1: Error message routines                                         */
273 /* ------------------------------------------------------------------------- */
274
275 extern void error(char *s)
276 {   if (no_errors == MAX_ERRORS)
277         fatalerror("Too many errors: giving up");
278     message(1,s);
279 }
280
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);
285 }
286
287 extern void error_numbered(char *s1, int val)
288 {
289     snprintf(error_message_buff, ERROR_BUFLEN,"%s %d.",s1,val);
290     ellipsize_error_message_buff();
291     error(error_message_buff);
292 }
293
294 extern void error_named_at(char *s1, char *s2, brief_location report_line)
295 {   int i;
296
297     ErrorPosition E = ErrorReport;
298     export_brief_location(report_line, &ErrorReport);
299
300     snprintf(error_message_buff, ERROR_BUFLEN,"%s \"%s\"",s1,s2);
301     ellipsize_error_message_buff();
302
303     i = concise_switch; concise_switch = TRUE;
304     error(error_message_buff);
305     ErrorReport = E; concise_switch = i;
306 }
307
308 extern void no_such_label(char *lname)
309 {   error_named("No such label as",lname);
310 }
311
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);
316 }
317
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);
322 }
323
324 extern void char_error(char *s, int ch)
325 {   int32 uni;
326
327     uni = iso_to_unicode(ch);
328
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);
335     }
336     else
337         snprintf(error_message_buff, ERROR_BUFLEN, "%s (ISO Latin1) $%02x", s, uni);
338
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. */
343
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);
350     }
351
352     ellipsize_error_message_buff();
353     error(error_message_buff);
354 }
355
356 extern void unicode_char_error(char *s, int32 uni)
357 {
358     if (uni >= 0x100)
359         snprintf(error_message_buff, ERROR_BUFLEN, "%s (unicode) $%04x", s, uni);
360     else
361         snprintf(error_message_buff, ERROR_BUFLEN, "%s (ISO Latin1) $%02x", s, uni);
362
363     /* See comment above. */
364
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);
371     }
372
373     ellipsize_error_message_buff();
374     error(error_message_buff);
375 }
376
377 extern void error_max_dynamic_strings(int index)
378 {
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);
385     else
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);
387
388     ellipsize_error_message_buff();
389     error(error_message_buff);
390 }
391
392 extern void error_max_abbreviations(int index)
393 {
394     /* This is only called for Z-code. */
395     if (index >= 96)
396         error("The number of abbreviations has exceeded 96, the limit in Z-code");
397     else
398         error("The number of abbreviations has exceeded MAX_ABBREVS. Increase MAX_ABBREVS.");
399 }
400
401 /* ------------------------------------------------------------------------- */
402 /*   Style 2: Warning message routines                                       */
403 /* ------------------------------------------------------------------------- */
404
405 extern void warning(char *s1)
406 {   if (nowarnings_switch) { no_suppressed_warnings++; return; }
407     message(2,s1);
408 }
409
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);
415 }
416
417 extern void warning_named(char *s1, char *s2)
418 {
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);
423 }
424
425 extern void symtype_warning(char *context, char *name, char *type, char *wanttype)
426 {
427     if (nowarnings_switch) { no_suppressed_warnings++; return; }
428     if (name)
429         snprintf(error_message_buff, ERROR_BUFLEN, "In %s, expected %s but found %s \"%s\"", context, wanttype, type, name);
430     else
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);
434 }
435
436 extern void dbnu_warning(char *type, char *name, brief_location report_line)
437 {   int i;
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);
445     concise_switch = i;
446     ErrorReport = E;
447 }
448
449 extern void uncalled_routine_warning(char *type, char *name, brief_location report_line)
450 {   int i;
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);
460     else
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);
465     concise_switch = i;
466     ErrorReport = E;
467 }
468
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);
476 }
477
478 /* ------------------------------------------------------------------------- */
479 /*   Style 4: Compiler error message routines                                */
480 /* ------------------------------------------------------------------------- */
481
482 extern void print_sorry_message(void)
483 {   printf(
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");
491 }
492
493 extern int compiler_error(char *s)
494 {
495     if (no_errors > 0) return FALSE;
496     if (no_compiler_errors==MAX_ERRORS)
497         fatalerror("Too many compiler errors: giving up");
498     message(4,s);
499     return TRUE;
500 }
501
502 extern int compiler_error_named(char *s1, char *s2)
503 {
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);
508     return TRUE;
509 }
510
511 /* ------------------------------------------------------------------------- */
512 /*   Code for the Acorn RISC OS operating system, donated by Robin Watts,    */
513 /*   to provide error throwback under the DDE environment                    */
514 /* ------------------------------------------------------------------------- */
515
516 #ifdef ARC_THROWBACK
517
518 #define DDEUtils_ThrowbackStart 0x42587
519 #define DDEUtils_ThrowbackSend  0x42588
520 #define DDEUtils_ThrowbackEnd   0x42589
521
522 #include "kernel.h"
523
524 extern void throwback_start(void)
525 {    _kernel_swi_regs regs;
526      if (throwback_switch)
527          _kernel_swi(DDEUtils_ThrowbackStart, &regs, &regs);
528 }
529
530 extern void throwback_end(void)
531 {   _kernel_swi_regs regs;
532     if (throwback_switch)
533         _kernel_swi(DDEUtils_ThrowbackEnd, &regs, &regs);
534 }
535
536 int throwback_started = FALSE;
537
538 extern void throwback(int severity, char * error)
539 {   _kernel_swi_regs regs;
540     if (!throwback_started)
541     {   throwback_started = TRUE;
542         throwback_start();
543     }
544     if (throwback_switch)
545     {   regs.r[0] = 1;
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, &regs, &regs);
554     }
555 }
556
557 #endif
558
559 /* ========================================================================= */
560 /*   Data structure management routines                                      */
561 /* ------------------------------------------------------------------------- */
562
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;
567 }
568
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;
578 }
579
580 extern void errors_allocate_arrays(void)
581 {   forerrors_buff = my_malloc(FORERRORS_SIZE, "errors buffer");
582 }
583
584 extern void errors_free_arrays(void)
585 {   my_free(&forerrors_buff, "errors buffer");
586 }
587
588 /* ========================================================================= */