|
|
1.1 root 1: /* C compiler: error reporting */
2:
3: #include "c.h"
4:
5: int errcnt; /* number of compilation errors */
6: int errlimit = 20; /* compilation error limit */
7: int wflag; /* != 0 to suppress warning messages */
8:
9: dclproto(static void printtoken,(void));
10:
11: /* error - issue error message */
12: #ifdef __STDC__
13: void error(char *fmt, ...) {
14: #else
15: void error(fmt, va_alist) char *fmt; va_dcl {
16: #endif
17: va_list ap;
18:
19: va_init(ap, fmt);
20: if (firstfile != file && firstfile && *firstfile)
21: fprint(2, "%s: ", firstfile); /* omit */
22: fprint(2, "%w: ", &src);
23: vfprint(2, fmt, ap);
24: if (++errcnt >= errlimit) {
25: errcnt = -1;
26: error("too many errors\n");
27: exit(1);
28: }
29: va_end(ap);
30: }
31:
32: /* expect - advance if t is tok, otherwise issue message */
33: int expect(tok) {
34: if (t == tok) {
35: t = gettok();
36: return t;
37: }
38: errcnt--;
39: error("syntax error; found");
40: printtoken();
41: fprint(2, " expecting `%k'\n", tok);
42: errcnt++;
43: return 0;
44: }
45:
46: /* fatal - issue fatal error message and exit */
47: int fatal(name, fmt, n) char *name, *fmt; {
48: *bp++ = '\n';
49: outflush();
50: error("compiler error in %s--", name);
51: fprint(2, fmt, n);
52: exit(1);
53: return 0;
54: }
55:
56: /* printtoken - print current token preceeded by a space */
57: static void printtoken() {
58: switch (t) {
59: case ID: fprint(2, " `%s'", token); break;
60: case ICON:
61: if (*token == '\'') {
62: char *s;
63: case SCON: fprint(2, " ");
64: for (s = token; *s && s - token < 20; s++)
65: if (*s < ' ' || *s >= 0177)
66: fprint(2, "\\%o", *s);
67: else
68: fprint(2, "%c", *s);
69: if (*s)
70: fprint(2, " ...");
71: else
72: fprint(2, "%c", *token);
73: break;
74: } /* else fall thru */
75: case FCON: {
76: char c = *cp;
77: *cp = 0;
78: fprint(2, " `%s'", token);
79: *cp = c;
80: break;
81: }
82: case '`': case '\'': fprint(2, " \"%k\"", t); break;
83: default: fprint(2, " `%k'", t);
84: }
85: }
86:
87: /* skipto - skip input up to tok U set, for a token where kind[t] is in set */
88: void skipto(tok, set) char set[]; {
89: int n;
90: char *s;
91:
92: for (n = 0; t != EOI && t != tok; t = gettok()) {
93: if (set) {
94: for (s = set; *s && kind[t] != *s; s++)
95: ;
96: if (kind[t] == *s)
97: break;
98: }
99: if (n++ == 0) {
100: errcnt--;
101: error("skipping", 0, 0, 0, 0);
102: }
103: if (n <= 8)
104: printtoken();
105: else if (n == 9)
106: fprint(2, " ...\n");
107: }
108: if (n > 8) {
109: errcnt--;
110: error("up to", 0, 0, 0, 0);
111: printtoken();
112: }
113: if (n > 0)
114: fprint(2, "\n");
115: }
116:
117: /* test - check for token tok, skip to tok U set, if necessary */
118: void test(tok, set) char set[]; {
119: if (t == tok)
120: t = gettok();
121: else {
122: expect(tok);
123: skipto(tok, set);
124: if (t == tok)
125: t = gettok();
126: }
127: }
128:
129: /* warning - issue warning error message */
130: #ifdef __STDC__
131: void warning(char *fmt, ...) {
132: #else
133: void warning(fmt, va_alist) char *fmt; va_dcl {
134: #endif
135: va_list ap;
136:
137: va_init(ap, fmt);
138: if (wflag == 0) {
139: errcnt--; /* compensate for increment in error */
140: error("warning: ");
141: vfprint(2, fmt, ap);
142: }
143: va_end(ap);
144: }
This archive runs on limited infrastructure. Preserving old code on modern bandwidth. Automated agents are requested to crawl responsibly.