aboutsummaryrefslogtreecommitdiff
path: root/gcc/algol68/a68-diagnostics.cc
blob: 254be5f49b2ef7e0899d233084200e2e382b22b4 (plain)
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
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
/* Error and warning routines.
   Copyright (C) 2001-2023 J. Marcel van der Veer.
   Copyright (C) 2025 Jose E. Marchesi.

   Original implementation by J. Marcel van der Veer.
   Adapted and expanded for GCC by Jose E. Marchesi.

   GCC is free software; you can redistribute it and/or modify it
   under the terms of the GNU General Public License as published by
   the Free Software Foundation; either version 3, or (at your option)
   any later version.

   GCC is distributed in the hope that it will be useful, but WITHOUT
   ANY WARRANTY; without even the implied warranty of MERCHANTABILITY
   or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU General Public
   License for more details.

   You should have received a copy of the GNU General Public License
   along with GCC; see the file COPYING3.  If not see
   <http://www.gnu.org/licenses/>.  */

#define INCLUDE_MEMORY
#include "config.h"
#include "system.h"
#include "coretypes.h"
#include "diagnostic.h"

#include "a68.h"

/*
 * Error handling routines.
 */

#define TABULATE(n) (8 * (n / 8 + 1) - n)

/* Severities handled by the DIAGNOSTIC function defined below.  */

#define A68_ERROR 0
#define A68_WARNING 1
#define A68_FATAL 2
#define A68_SCAN_ERROR 3
#define A68_INFORM 4

/* Auxiliary function used to grow an obstack by the contents of some given
   string.  */

static void
obstack_append_str (obstack *b, const char *str)
{
  obstack_grow (b, str, strlen (str));
}

/* Give a diagnostic message.  */

#if __GNUC__ >= 10
#pragma GCC diagnostic ignored "-Wsuggest-attribute=format"
#endif

static bool
diagnostic (int sev, int opt,
	    NODE_T *p,
	    LINE_T *line,
	    char *pos,
	    const char *loc_str, va_list args)
{
  int res = 0;
  MOID_T *moid = NO_MOID;
  const char *t = loc_str;
  obstack b;

  /*
   * Synthesize diagnostic message.
   *
   * Legend for special symbols:
   * * as first character, copy rest of string literally
   * @ AST node
   * A AST node attribute
   * B keyword
   * C context
   * L line number
   * M moid - if error mode return without giving a message
   * O moid - operand
   * S quoted symbol, when possible with typographical display features
   * X expected attribute
   * Y string literal.
   * Z quoted string.  */

  static va_list argp; /* Note this is empty. */
  gcc_obstack_init (&b);

  if (t[0] == '*')
    obstack_append_str (&b, t + 1);
  else
    while (t[0] != '\0')
      {
	if (t[0] == '@')
	  {
            const char *nt = a68_attribute_name (ATTRIBUTE (p));
            if (t != NO_TEXT)
              obstack_append_str (&b, nt);
	    else
              obstack_append_str (&b, "construct");
          }
	else if (t[0] == 'A')
	  {
            enum a68_attribute att = (enum a68_attribute) va_arg (args, int);
            const char *nt = a68_attribute_name (att);
            if (nt != NO_TEXT)
              obstack_append_str (&b, nt);
	    else
              obstack_append_str (&b, "construct");
          }
	else if (t[0] == 'B')
	  {
            enum a68_attribute att = (enum a68_attribute) va_arg (args, int);
            KEYWORD_T *nt = a68_find_keyword_from_attribute (A68 (top_keyword), att);
            if (nt != NO_KEYWORD)
	      {
		const char *strop_keyword = a68_strop_keyword (TEXT (nt));

		obstack_append_str (&b, "%<");
		obstack_append_str (&b, strop_keyword);
		obstack_append_str (&b, "%>");
	      }
	    else
              obstack_append_str (&b, "keyword");
          }
	else if (t[0] == 'C')
	  {
            int att = va_arg (args, int);
	    const char *sort = NULL;

	    switch (att)
	      {
	      case NO_SORT: sort = "this"; break;
	      case SOFT: sort = "a soft"; break;
	      case WEAK: sort = "a weak"; break;
	      case MEEK: sort = "a meek"; break;
	      case FIRM: sort = "a firm"; break;
	      case STRONG: sort = "a strong"; break;
	      default:
		gcc_unreachable ();
	      }

	    obstack_append_str (&b, sort);
          }
	else if (t[0] == 'L')
	  {
	    LINE_T *a = va_arg (args, LINE_T *);
            gcc_assert (a != NO_LINE);
            if (NUMBER (a) == 0)
              obstack_append_str (&b, "in standard environment");
	    else if (p != NO_NODE && NUMBER (a) == LINE_NUMBER (p))
	      obstack_append_str (&b, "in this line");
	    else
	      {
		char d[18];
		if (snprintf (d, 18, "in line %d", NUMBER (a)) < 0)
		  gcc_unreachable ();
		obstack_append_str (&b, d);
	      }
          }
	else if (t[0] == 'M')
	  {
	    const char *moidstr = NULL;

            moid = va_arg (args, MOID_T *);
            if (moid == NO_MOID || moid == M_ERROR)
              moid = M_UNDEFINED;

            if (IS (moid, SERIES_MODE))
	      {
		if (PACK (moid) != NO_PACK && NEXT (PACK (moid)) == NO_PACK)
		  moidstr = a68_moid_to_string (MOID (PACK (moid)),
						MOID_ERROR_WIDTH, p);
		else
		  moidstr = a68_moid_to_string (moid, MOID_ERROR_WIDTH, p);
	      }
	    else
	      moidstr = a68_moid_to_string (moid, MOID_ERROR_WIDTH, p);

	    obstack_append_str (&b, "%<");
	    obstack_append_str (&b, moidstr);
	    obstack_append_str (&b, "%>");
          }
	else if (t[0] == 'O')
	  {
            moid = va_arg (args, MOID_T *);
            if (moid == NO_MOID || moid == M_ERROR)
              moid = M_UNDEFINED;
            if (moid == M_VOID)
              obstack_append_str (&b, "UNION (VOID, ..)");
	    else if (IS (moid, SERIES_MODE))
	      {
		const char *moidstr = NULL;

		if (PACK (moid) != NO_PACK && NEXT (PACK (moid)) == NO_PACK)
		  moidstr = a68_moid_to_string (MOID (PACK (moid)), MOID_ERROR_WIDTH, p);
		else
		  moidstr = a68_moid_to_string (moid, MOID_ERROR_WIDTH, p);
		obstack_append_str (&b, moidstr);
	      }
	    else
	      {
		const char *moidstr = a68_moid_to_string (moid, MOID_ERROR_WIDTH, p);
		obstack_append_str (&b, moidstr);
	      }
          }
	else if (t[0] == 'S')
	  {
            if (p != NO_NODE && NSYMBOL (p) != NO_TEXT)
	      {
		const char *txt = NSYMBOL (p);
		char *sym = NCHAR_IN_LINE (p);
		int n = 0, size = (int) strlen (txt);

		obstack_append_str (&b, "%<");
		if (txt[0] != sym[0] || (int) strlen (sym) < size)
		  obstack_append_str (&b, txt);
		else
		  {
		    while (n < size)
		      {
			if (ISPRINT (sym[0]))
			  obstack_1grow (&b, sym[0]);
			if (TOLOWER (txt[0]) == TOLOWER (sym[0]))
			  {
			    txt++;
			    n++;
			  }
			sym++;
		      }
		  }
		obstack_append_str (&b, "%>");
	      }
	    else
	      obstack_append_str (&b, "symbol");
          }
	else if (t[0] == 'X')
	  {
            enum a68_attribute att = (enum a68_attribute) (va_arg (args, int));
	    const char *att_name = a68_attribute_name (att);
	    obstack_append_str (&b, att_name);
          }
	else if (t[0] == 'Y')
	  {
            char *loc_string = va_arg (args, char *);
	    obstack_append_str (&b, loc_string);
          }
	else if (t[0] == 'Z')
	  {
            char *str = va_arg (args, char *);
	    obstack_append_str (&b, "%<");
	    obstack_append_str (&b, str);
	    obstack_append_str (&b, "%>");
          }
	else
	  obstack_1grow (&b, t[0]);

	t++;
       }

  obstack_1grow (&b, '\0');
  char *format = (char *) obstack_finish (&b);

  /* Construct a diagnostic message.  */
  if (sev == A68_WARNING)
    WARNING_COUNT (&A68_JOB)++;
  else if (sev != A68_INFORM)
    ERROR_COUNT (&A68_JOB)++;

  /* Emit the corresponding GCC diagnostic at the proper location.  */
  location_t loc = UNKNOWN_LOCATION;

  if (p != NO_NODE)
    loc = a68_get_node_location (p);
  else if (line != NO_LINE)
    {
      if (pos == NO_TEXT)
	pos = STRING (line);
      loc = a68_get_line_location (line, pos);
    }

  /* Prepare rich location and diagnostics.  */
  rich_location rich_loc (line_table, loc);
  diagnostics::diagnostic_info diagnostic;
  enum diagnostics::kind kind;

  switch (sev)
    {
    case A68_FATAL:
      kind = diagnostics::kind::fatal;
      break;
    case A68_INFORM:
      kind = diagnostics::kind::note;
      break;
    case A68_WARNING:
      kind = diagnostics::kind::warning;
      break;
    case A68_SCAN_ERROR:
    case A68_ERROR:
      kind = diagnostics::kind::error;
      break;
    default:
      gcc_unreachable ();
    }

  diagnostic_set_info (&diagnostic, format,
		       &argp,
		       &rich_loc, kind);
  if (opt != 0)
    diagnostic.m_option_id = opt;
  res = diagnostic_report_diagnostic (global_dc, &diagnostic);

  if (sev == A68_SCAN_ERROR)
    exit (FATAL_EXIT_CODE);
  return res;
}

/* Give an intelligible error and exit.  A line is provided rather than a
   node so this can be used at scanning time.  */

void
a68_scan_error (LINE_T * u, char *v, const char *txt, ...)
{
  va_list args;

  va_start (args, txt);
  diagnostic (A68_SCAN_ERROR, 0, NO_NODE, u, v, txt, args);
  va_end (args);
}

/* Report a compilation error.  */

void
a68_error (NODE_T *p, const char *loc_str, ...)
{
  va_list args;

  va_start (args, loc_str);
  diagnostic (A68_ERROR, 0, p, NO_LINE, NO_TEXT, loc_str, args);
  va_end (args);
}

/* Report a compilation error in a node's pragmat.  */

void
a68_error_in_pragmat (NODE_T *p, size_t off,
		      const char *loc_str, ...)
{
  va_list args;

  LINE_T *line = NPRAGMAT_LINE (p);
  char *pos = NPRAGMAT_CHAR_IN_LINE (p) + off;

  va_start (args, loc_str);
  diagnostic (A68_ERROR, 0, NO_NODE, line, pos, loc_str, args);
  va_end (args);
  a68_inform (p, "pragmat applies to this construct");
}

/* Report a compilation warning.
   This function returns a boolean indicating whether a warning was
   emitted.  */

bool
a68_warning (NODE_T *p, int opt,
	     const char *loc_str, ...)
{
  bool res;
  va_list args;

  va_start (args, loc_str);
  res = diagnostic (A68_WARNING, opt, p, NO_LINE, NO_TEXT, loc_str, args);
  va_end (args);
  return res;
}

/* Report a compilation note.  */

void
a68_inform (NODE_T *p, const char *loc_str, ...)
{
  va_list args;

  va_start (args, loc_str);
  diagnostic (A68_INFORM, 0, p, NO_LINE, NO_TEXT, loc_str, args);
  va_end (args);
}