|
|
1.1 root 1: /*ident "@(#)ctrans:src/error.c 1.1.3.15" */
2: /**************************************************************************
3:
4: C++ source for cfront, the C++ compiler front-end
5: written in the computer science research center of Bell Labs
6:
7: Copyright (c) 1984 AT&T, Inc. All rigths Reserved
8: THIS IS UNPUBLISHED PROPRIETARY SOURCE CODE OF AT&T, INC.
9:
10: error.c :
11:
12: write error messages
13:
14: Until scan_started != 0 no context can be assumed
15:
16: ***************************************************************************/
17:
18: #ifdef __cplusplus
19: #include <stdlib.h>
20: #endif
21:
22: #include "cfront.h"
23: #include "size.h"
24:
25: int error_count;
26: static int no_of_warnings;
27: char scan_started;
28: extern char emode;
29:
30: #define ERRTRACE 20
31:
32: static char* abbrev_tbl[] = {
33: " argument",
34: " base",
35: " class",
36: " declaration",
37: " expression",
38: " function",
39: "G",
40: "H",
41: " initialize",
42: "J",
43: " constructor", // 'K' !
44: " list",
45: " member",
46: " name",
47: " object",
48: " pointer",
49: " qualifie",
50: " reference",
51: " statement",
52: " type",
53: " undefined",
54: " variable",
55: " with",
56: " expected", // 'X'
57: "Y",
58: "Z",
59: };
60:
61: ea* ea0;
62:
63: void error_init()
64: {
65: static char errbuf[BUFSIZ];
66: setbuf(stderr,errbuf);
67: ea0 = new ea;
68: }
69:
70: #define INTERNAL 127
71:
72: void ext(int n)
73: {
74: int useit=n; // to avoid n not used warning during build
75: // for testing only
76: // if (n == INTERNAL)
77: // abort();
78: exit(error_count?error_count:1);
79: }
80:
81: void print_loc()
82: {
83: loc* sl = (Cstmt) ? &Cstmt->where : 0;
84: loc* dl = (Cdcl && (Cdcl->base==NAME || Cdcl->base==TNAME)) ? &Cdcl->where : 0;
85: if (sl && dl && sl->file==dl->file) { // Cstmt and Cdcl in same file
86: if (sl->line<=dl->line) {
87: if (curloc.file==dl->file && curloc.line<dl->line)
88: // hack to compensate for YACC's
89: // bad manners in the use of line numbers
90: sl->put(out_file);
91: else
92: dl->put(out_file);
93: }
94: else
95: sl->put(out_file);
96: }
97: else if (sl && sl->file==curr_file) // Cstmt in current file
98: sl->put(out_file);
99: else if (dl && dl->file==curr_file) // Cdcl in current file
100: dl->put(out_file);
101: else
102: curloc.put(out_file);
103: }
104:
105: static void print_context()
106: {
107: putc('\n',out_file);
108: }
109:
110: static char in_error = 0;
111: loc dummy_loc;
112:
113: void yyerror(char* s)
114: {
115: error(s);
116: }
117:
118: int error(const char* s)
119: {
120: return error(0,s);
121: }
122:
123: int error(int t, const char* s)
124: {
125: return error(t,&dummy_loc,s,*ea0,*ea0,*ea0,*ea0);
126: }
127:
128: int error(const char* s, const ea& a0, const ea& a1, const ea& a2, const ea& a3)
129: {
130: return error(0,&dummy_loc,s,a0,a1,a2,a3);
131: }
132:
133: int error(loc* lc, const char* s, const ea& a0, const ea& a1, const ea& a2, const ea& a3)
134: {
135: return error(0,lc,s,a0,a1,a2,a3);
136: }
137:
138: int error(int t, const char* s, const ea& a0, const ea& a1, const ea& a2, const ea& a3)
139: {
140: return error(t,&dummy_loc,s,a0,a1,a2,a3);
141: }
142:
143: int suppress_error;
144:
145: int error(int t, loc* lc, const char* s, const ea& a0, const ea& a1, const ea& a2, const ea& a3)
146: /*
147: "int" not "void" because of "pch" in lex.c
148:
149: legal error types are:
150: 'w' warning (not counted in error count)
151: 'd' debug
152: 's' "not implemented" message
153: 'l' "compiler limit exceeded" message
154: 0 error
155: 'i' internal error (causes abort)
156: 't' error while printing error message
157: */
158: {
159: if (suppress_error) return 0;
160:
161: if (in_error++)
162: if (t == 't')
163: t = 'i';
164: else if (4 < in_error) {
165: fprintf(stderr,"\nOops!, error while handling error\n");
166: ext(13);
167: }
168:
169: FILE * of = out_file;
170: out_file = stderr;
171:
172: if (!scan_started || t=='t')
173: putch('\n');
174: else if (lc != &dummy_loc)
175: lc->put(out_file);
176: else
177: print_loc();
178:
179: switch (t) {
180: case 0:
181: putstring("error: ");
182: break;
183: case 'w':
184: no_of_warnings++;
185: putstring("warning: ");
186: break;
187: case 'l':
188: putstring("compiler limit exceeded: ");
189: break;
190: case 's':
191: putstring("sorry, not implemented: ");
192: break;
193: case 'i':
194: if (error_count++) {
195: fprintf(out_file,"sorry, %s cannot recover from earlier errors\n",prog_name);
196: ext(INTERNAL);
197: }
198: else
199: fprintf(out_file,"internal %s error: ",prog_name);
200: }
201:
202: ea argv[4];
203: ea* a = argv;
204: argv[0] = a0;
205: argv[1] = a1;
206: argv[2] = a2;
207: argv[3] = a3;
208:
209: int c;
210:
211: while (c = *s++) {
212: if ('A'<=c && c<='Z')
213: putstring(abbrev_tbl[c-'A']);
214: else if (c == '%') {
215: switch (c = *s++) {
216: case 'k': // TOK assumed passed as an int
217: { TOK x = TOK(a->i);
218: if (0<x && x<=MAXTOK && keys[x])
219: fprintf(out_file," %s",keys[x]);
220: else
221: fprintf(out_file," token(%d)",x);
222: break;
223: }
224: case 't': // Ptype
225: { Ptype tt = Ptype(a->p);
226: if (tt == 0) break;
227:
228: putch(' ');
229:
230: extern int ntok;
231: int nt = ntok;
232: emode = 1;
233: tt->dcl_print(0);
234: emode = 0;
235: ntok = nt;
236: break;
237: }
238: case 'n': // Pname
239: { Pname nn = Pname(a->p);
240: if (nn && nn->string) {
241: // suppress generated class names:
242: if (nn->string[0]=='_'
243: && nn->string[1]=='_'
244: && nn->string[2]=='C') break;
245: emode = 1;
246: putch(' ');
247: nn->print();
248: emode = 0;
249: }
250: else
251: putstring(" ?");
252: break;
253: }
254: case 'p': // pointer
255: { char* f = sizeof(char*)==sizeof(int)?" %d":" %ld";
256: fprintf(out_file,f,a->p);
257: break;
258: }
259: case 'c': // char assumed passed as an int
260: putch(a->i);
261: break;
262:
263: case 'd': // int
264: fprintf(out_file," %d",a->i);
265: break;
266:
267: case 's': // char*
268: char *s = ((char *)a->p);
269: if ( s )
270: putst((char*)a->p);
271: break;
272: }
273: a++;
274: }
275: else
276: putch(c);
277: }
278:
279: if (!scan_started && t!='d') ext(4);
280: /*
281: switch (t) {
282: case 'd':
283: case 't':
284: case 'w':
285: putch('\n');
286: break;
287: default:
288: */
289: print_context();
290: /*
291: }
292: */
293: fflush(stderr);
294: /* now we may want to carry on */
295:
296: out_file = of;
297:
298: switch (t) {
299: case 't':
300: if (--in_error) return 0;
301: case 'i':
302: ext(INTERNAL);
303: case 0:
304: case 'l':
305: case 's':
306: if (MAXERR<++error_count) {
307: fprintf(stderr,"Sorry, too many errors\n");
308: ext(7);
309: }
310: }
311:
312: in_error = 0;
313: return 0;
314: }
This archive runs on limited infrastructure. Preserving old code on modern bandwidth. Automated agents are requested to crawl responsibly.