GnuCOBOL  2.0
A free COBOL compiler
error.c
Go to the documentation of this file.
1 /*
2  Copyright (C) 2001-2012, 2014-2016 Free Software Foundation, Inc.
3  Written by Keisuke Nishida, Roger While, Simon Sobisch
4 
5  This file is part of GnuCOBOL.
6 
7  The GnuCOBOL compiler is free software: you can redistribute it
8  and/or modify it under the terms of the GNU General Public License
9  as published by the Free Software Foundation, either version 3 of the
10  License, or (at your option) any later version.
11 
12  GnuCOBOL is distributed in the hope that it will be useful,
13  but WITHOUT ANY WARRANTY; without even the implied warranty of
14  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
15  GNU General Public License for more details.
16 
17  You should have received a copy of the GNU General Public License
18  along with GnuCOBOL. If not, see <http://www.gnu.org/licenses/>.
19 */
20 
21 
22 #include "config.h"
23 
24 #include <stdio.h>
25 #include <stdlib.h>
26 #include <stddef.h>
27 #include <string.h>
28 #include <ctype.h>
29 #include <stdarg.h>
30 
31 #include "cobc.h"
32 #include "tree.h"
33 
34 static char *errnamebuff = NULL;
35 static struct cb_label *last_section = NULL;
36 static struct cb_label *last_paragraph = NULL;
37 
38 static int conf_error_displayed = 0;
39 static int last_error_line = 0;
40 static const char *last_error_file = "Unknown";
41 
42 
43 size_t cb_msg_style;
44 
45 static void
46 print_error (const char *file, int line, const char *prefix,
47  const char *fmt, va_list ap)
48 {
49  if (!file) {
50  file = cb_source_file;
51  }
52  if (!line) {
53  line = cb_source_line;
54  }
55 
56  /* Print section and/or paragraph name */
57  if (current_section != last_section) {
59  fprintf (stderr, "%s: ", file);
60  fputs (_("In section"), stderr);
61  fprintf (stderr, " '%s':\n",
62  (char *)current_section->name);
63  }
64  last_section = current_section;
65  }
66  if (current_paragraph != last_paragraph) {
68  fprintf (stderr, "%s: ", file);
69  fputs (_("In paragraph"), stderr);
70  fprintf (stderr, " '%s':\n",
71  (char *)current_paragraph->name);
72  }
73  last_paragraph = current_paragraph;
74  }
75 
76  /* Print the error */
78  fprintf (stderr, "%s (%d): %s", file, line, prefix);
79  } else {
80  fprintf (stderr, "%s: %d: %s", file, line, prefix);
81  }
82  vfprintf (stderr, fmt, ap);
83  putc ('\n', stderr);
84 }
85 
86 void
87 cb_warning (const char *fmt, ...)
88 {
89  va_list ap;
90 
91  va_start (ap, fmt);
92  print_error (NULL, 0, _("Warning: "), fmt, ap);
93  va_end (ap);
94  warningcount++;
95 }
96 
97 void
98 cb_error (const char *fmt, ...)
99 {
100  va_list ap;
101 
102  cobc_in_repository = 0;
103 #if 0 /* RXWRXW - Is this right? */
104  cobc_cs_check = 0;
105 #endif
106  va_start (ap, fmt);
107  print_error (NULL, 0, _("Error: "), fmt, ap);
108  va_end (ap);
109  if (++errorcount > 100) {
111  }
112 }
113 
114 /* Warning/error for pplex.l input routine */
115 /* At this stage we have not parsed the newline so that */
116 /* cb_source_line needs to be adjusted by newline_count in pplex.l */
117 
118 void
119 cb_plex_warning (const size_t sline, const char *fmt, ...)
120 {
121  va_list ap;
122 
123  va_start (ap, fmt);
124  print_error (NULL, (int)(cb_source_line + sline), _("Warning: "), fmt, ap);
125  va_end (ap);
126  warningcount++;
127 }
128 
129 void
130 cb_plex_error (const size_t sline, const char *fmt, ...)
131 {
132  va_list ap;
133 
134  va_start (ap, fmt);
135  print_error (NULL, (int)(cb_source_line + sline), _("Error: "), fmt, ap);
136  va_end (ap);
137  if (++errorcount > 100) {
139  }
140 }
141 
142 /* Warning/Error for config.c */
143 void
144 configuration_warning (const char *fname, const int line, const char *fmt, ...)
145 {
146  va_list args;
147 
149  fputs (_("Configuration Warning"), stderr);
150  fputs (": ", stderr);
151 
152 
153  /* Prefix */
154  if (fname != last_error_file
155  || line != last_error_line) {
156  last_error_file = fname;
158  if (fname) {
159  fprintf (stderr, "%s: ", fname);
160  } else {
161  fputs ("cb_conf: ", stderr);
162  }
163  if (line) {
164  fprintf (stderr, "%d: ", line);
165  }
166  }
167 
168  /* Body */
169  va_start(args, fmt);
170  vfprintf (stderr, fmt, args);
171  va_end(args);
172 
173  /* Postfix */
174  putc('\n', stderr);
175  fflush(stderr);
176 }
177 void
178 configuration_error (const char *fname, const int line, const int finish_error, const char *fmt, ...)
179 {
180  va_list args;
181 
182  if (!conf_error_displayed) {
184  fputs (_("Configuration Error"), stderr);
185  putc ('\n', stderr);
186  }
187 
188 
189  /* Prefix */
190  if (fname != last_error_file
191  || line != last_error_line) {
192  last_error_file = fname;
194  if (fname) {
195  fprintf (stderr, "%s: ", fname);
196  } else {
197  fputs ("cb_conf: ", stderr);
198  }
199  if (line) {
200  fprintf (stderr, "%d: ", line);
201  }
202  }
203 
204  /* Body */
205  va_start(args, fmt);
206  vfprintf (stderr, fmt, args);
207  va_end(args);
208 
209  /* Postfix */
210  if (!finish_error) {
211  putc(';', stderr);
212  putc('\n', stderr);
213  putc('\t', stderr);
214  } else {
215  putc('\n', stderr);
216  fflush(stderr);
217  }
218 }
219 
220 /* Generic warning/error routines */
221 void
222 cb_warning_x (cb_tree x, const char *fmt, ...)
223 {
224  va_list ap;
225 
226  va_start (ap, fmt);
227  print_error (x->source_file, x->source_line, _("Warning: "), fmt, ap);
228  va_end (ap);
229  warningcount++;
230 }
231 
232 void
233 cb_error_x (cb_tree x, const char *fmt, ...)
234 {
235  va_list ap;
236 
237  va_start (ap, fmt);
238  print_error (x->source_file, x->source_line, _("Error: "), fmt, ap);
239  va_end (ap);
240  if (++errorcount > 100) {
242  }
243 }
244 
245 unsigned int
246 cb_verify (const enum cb_support tag, const char *feature)
247 {
248  switch (tag) {
249  case CB_OK:
250  return 1;
251  case CB_WARNING:
252  cb_warning (_("%s used"), feature);
253  return 1;
254  case CB_ARCHAIC:
255  if (cb_warn_archaic) {
256  cb_warning (_("%s is archaic in %s"), feature, cb_config_name);
257  }
258  return 1;
259  case CB_OBSOLETE:
260  if (cb_warn_obsolete) {
261  cb_warning (_("%s is obsolete in %s"), feature, cb_config_name);
262  }
263  return 1;
264  case CB_SKIP:
265  return 0;
266  case CB_IGNORE:
267  if (warningopt) {
268  cb_warning (_("%s ignored"), feature);
269  }
270  return 0;
271  case CB_ERROR:
272  cb_error (_("%s used"), feature);
273  return 0;
274  case CB_UNCONFORMABLE:
275  cb_error (_("%s does not conform to %s"), feature, cb_config_name);
276  return 0;
277  default:
278  break;
279  }
280  return 0;
281 }
282 
283 void
285 {
286  struct cb_word *w;
287 
288  w = CB_REFERENCE (x)->word;
289  cb_error_x (x, _("Redefinition of '%s'"), w->name);
290  if (w->items) {
291  cb_error_x (CB_VALUE (w->items),
292  _("'%s' previously defined here"), w->name);
293  }
294 }
295 
296 void
298 {
299  struct cb_word *w;
300  cb_tree z;
301 
302  w = CB_REFERENCE (x)->word;
303  cb_warning_x (x, _("Redefinition of '%s'"), w->name);
304  z = NULL;
305  if (y) {
306  z = y;
307  } else if (w->items) {
308  z = CB_VALUE (w->items);
309  }
310 
311  if (z) {
312  cb_warning_x (z, _("'%s' previously defined here"), w->name);
313  }
314 }
315 
316 void
318 {
319  struct cb_reference *r;
320  cb_tree c;
321 
322  if (!errnamebuff) {
324  }
325  r = CB_REFERENCE (x);
326  snprintf (errnamebuff, (size_t)COB_NORMAL_MAX, "'%s'", CB_NAME (x));
328  for (c = r->chain; c; c = CB_REFERENCE (c)->chain) {
329  strcat (errnamebuff, " in '");
330  strcat (errnamebuff, CB_NAME (c));
331  strcat (errnamebuff, "'");
332  }
333  if (r->flag_optional) {
334  cb_warning_x (x, _("%s is not defined"), errnamebuff);
335  } else {
336  cb_error_x (x, _("%s is not defined"), errnamebuff);
337  }
338 }
339 
340 void
342 {
343  struct cb_word *w;
344  struct cb_field *p;
345  struct cb_label *l2;
346  cb_tree l;
347  cb_tree y;
348 
349  w = CB_REFERENCE (x)->word;
350  if (w->error == 0) {
351  if (!errnamebuff) {
353  }
354  /* Display error the first time */
355  snprintf (errnamebuff, (size_t)COB_NORMAL_MAX, "'%s'", CB_NAME (x));
357  for (l = CB_REFERENCE (x)->chain; l; l = CB_REFERENCE (l)->chain) {
358  strcat (errnamebuff, " in '");
359  strcat (errnamebuff, CB_NAME (l));
360  strcat (errnamebuff, "'");
361  }
362  cb_error_x (x, _("%s ambiguous; need qualification"), errnamebuff);
363  w->error = 1;
364 
365  /* Display all fields with the same name */
366  for (l = w->items; l; l = CB_CHAIN (l)) {
367  y = CB_VALUE (l);
368  snprintf (errnamebuff, (size_t)COB_NORMAL_MAX,
369  "'%s' ", w->name);
371  switch (CB_TREE_TAG (y)) {
372  case CB_TAG_FIELD:
373  for (p = CB_FIELD (y)->parent; p; p = p->parent) {
374  strcat (errnamebuff, "in '");
375  strcat (errnamebuff, cb_name (CB_TREE(p)));
376  strcat (errnamebuff, "' ");
377  }
378  break;
379  case CB_TAG_LABEL:
380  l2 = CB_LABEL (y);
381  if (l2->section) {
382  strcat (errnamebuff, "in '");
383  strcat (errnamebuff,
384  (const char *)(l2->section->name));
385  strcat (errnamebuff, "' ");
386  }
387  break;
388  default:
389  break;
390  }
391  strcat (errnamebuff, _("defined here"));
392  cb_error_x (y, errnamebuff);
393  }
394  }
395 }
396 
397 void
398 group_error (cb_tree x, const char *clause)
399 {
400  cb_error_x (x, _("Group item '%s' cannot have %s clause"),
401  cb_name (x), clause);
402 }
403 
404 void
405 level_redundant_error (cb_tree x, const char *clause)
406 {
407  const char *s;
408  const struct cb_field *f;
409 
410  s = cb_name (x);
411  f = CB_FIELD_PTR (x);
412  if (f->flag_item_78) {
413  cb_error_x (x, _("Constant item '%s' cannot have a %s clause"),
414  s, clause);
415  } else {
416  cb_error_x (x, _("Level %02d item '%s' cannot have a %s clause"),
417  f->level,
418  s, clause);
419  }
420 }
421 
422 void
423 level_require_error (cb_tree x, const char *clause)
424 {
425  const char *s;
426  const struct cb_field *f;
427 
428  s = cb_name (x);
429  f = CB_FIELD_PTR (x);
430  if (f->flag_item_78) {
431  cb_error_x (x, _("Constant item '%s' requires a %s clause"),
432  s, clause);
433  } else {
434  cb_error_x (x, _("Level %02d item '%s' requires a %s clause"),
435  f->level,
436  s, clause);
437  }
438 }
439 
440 void
441 level_except_error (cb_tree x, const char *clause)
442 {
443  const char *s;
444  const struct cb_field *f;
445 
446  s = cb_name (x);
447  f = CB_FIELD_PTR (x);
448  if (f->flag_item_78) {
449  cb_error_x (x, _("Constant item '%s' can only have a %s clause"),
450  s, clause);
451  } else {
452  cb_error_x (x, _("Level %02d item '%s' can only have a %s clause"),
453  f->level,
454  s, clause);
455  }
456 }
static int conf_error_displayed
Definition: error.c:38
#define CB_TREE(x)
Definition: tree.h:440
int warningcount
Definition: cobc.c:174
void cb_error_x(cb_tree x, const char *fmt,...)
Definition: error.c:233
#define CB_LABEL(x)
Definition: tree.h:801
void * cobc_main_malloc(const size_t size)
Definition: cobc.c:702
unsigned int flag_optional
Definition: tree.h:896
const char * name
Definition: tree.h:766
Definition: cobc.h:139
void configuration_error(const char *fname, const int line, const int finish_error, const char *fmt,...)
Definition: error.c:178
const char * cb_source_file
Definition: cobc.c:145
void cb_warning_x(cb_tree x, const char *fmt,...)
Definition: error.c:222
Definition: cobc.h:141
#define CB_FIELD_PTR(x)
Definition: tree.h:745
int warningopt
Definition: cobc.c:176
#define COB_NORMAL_BUFF
Definition: common.h:541
static struct cb_label * last_section
Definition: error.c:35
const char * source_file
Definition: tree.h:431
char * cb_name(cb_tree x)
Definition: tree.c:735
int level
Definition: tree.h:673
if fold fold static computed alternate extra correct stack on syntax debugging source implicit stack syntax write single recursive relax optional file
Definition: flag.def:129
unsigned int flag_dummy_paragraph
Definition: tree.h:788
void redefinition_warning(cb_tree x, cb_tree y)
Definition: error.c:297
void level_except_error(cb_tree x, const char *clause)
Definition: error.c:441
#define CB_VALUE(x)
Definition: tree.h:1193
const char * name
Definition: tree.h:865
Definition: cobc.h:135
unsigned int flag_item_78
Definition: tree.h:711
void ambiguous_error(cb_tree x)
Definition: error.c:341
cb_support
Definition: cobc.h:134
struct cb_label * section
Definition: tree.h:768
cb_tree chain
Definition: tree.h:875
int source_line
Definition: tree.h:432
unsigned int cobc_in_repository
Definition: parser.c:180
void cb_plex_error(const size_t sline, const char *fmt,...)
Definition: error.c:130
#define _(s)
Definition: cobcrun.c:59
Definition: tree.h:643
void configuration_warning(const char *fname, const int line, const char *fmt,...)
Definition: error.c:144
#define CB_CHAIN(x)
Definition: tree.h:1194
EC ARGUMENT EC EC BOUND EC BOUND EC BOUND EC BOUND TABLE EC DATA EC DATA EC DATA PTR NULL
Definition: exception.def:95
#define CB_TREE_TAG(x)
Definition: tree.h:441
static const char *const prefix[]
Definition: fileio.c:262
int error
Definition: tree.h:868
#define CB_MSG_STYLE_MSC
Definition: cobc.h:436
#define CB_NAME(x)
Definition: tree.h:904
static struct cb_label * last_paragraph
Definition: error.c:36
#define CB_REFERENCE(x)
Definition: tree.h:901
#define COB_NORMAL_MAX
Definition: common.h:547
void undefined_error(cb_tree x)
Definition: error.c:317
void redefinition_error(cb_tree x)
Definition: error.c:284
cb_tree items
Definition: tree.h:866
static int last_error_line
Definition: error.c:39
struct cb_field * parent
Definition: tree.h:651
struct cb_label * current_paragraph
Definition: parser.c:171
size_t cb_msg_style
Definition: error.c:43
void cb_error(const char *fmt,...)
Definition: error.c:98
if fold fold static computed alternate extra correct stack on syntax debugging line
Definition: flag.def:90
static const char * last_error_file
Definition: error.c:40
static char * errnamebuff
Definition: error.c:34
unsigned int cb_verify(const enum cb_support tag, const char *feature)
Definition: error.c:246
void group_error(cb_tree x, const char *clause)
Definition: error.c:398
void level_require_error(cb_tree x, const char *clause)
Definition: error.c:423
Definition: tree.h:764
int cb_source_line
Definition: cobc.c:178
struct cb_label * current_section
Definition: parser.c:170
void cb_plex_warning(const size_t sline, const char *fmt,...)
Definition: error.c:119
unsigned int flag_dummy_section
Definition: tree.h:787
unsigned int cobc_cs_check
Definition: parser.c:182
void level_redundant_error(cb_tree x, const char *clause)
Definition: error.c:405
void cobc_too_many_errors(void)
Definition: cobc.c:599
Definition: tree.h:863
void cb_warning(const char *fmt,...)
Definition: error.c:87
#define CB_FIELD(x)
Definition: tree.h:740
int errorcount
Definition: cobc.c:173
static void print_error(const char *file, int line, const char *prefix, const char *fmt, va_list ap)
Definition: error.c:46