scc

simple C compiler
git clone git://git.2f30.org/scc
Log | Files | Refs | README | LICENSE

error.c (1217B)


      1 static char sccsid[] = "@(#) ./cc1/error.c";
      2 #include <stdarg.h>
      3 #include <stdio.h>
      4 #include <stdlib.h>
      5 
      6 #include "../inc/scc.h"
      7 #include "cc1.h"
      8 
      9 #define MAXERRNUM 10
     10 
     11 extern int failure;
     12 static unsigned nerrors;
     13 
     14 static void
     15 warn_error(int flag, char *fmt, va_list va)
     16 {
     17 	if (flag == 0)
     18 		return;
     19 	fprintf(stderr, "%s:%u: %s: ",
     20 	       filenam, lineno,
     21 	       (flag < 0) ? "error" : "warning");
     22 	vfprintf(stderr, fmt, va);
     23 	putc('\n', stderr);
     24 
     25 	if (flag < 0) {
     26 		if (!failure)
     27 			fclose(stdout);
     28 		failure = 1;
     29 		if (++nerrors == MAXERRNUM) {
     30 			fputs("too many errors\n", stderr);
     31 			exit(1);
     32 		}
     33 	}
     34 }
     35 
     36 void
     37 warn(char *fmt, ...)
     38 {
     39 	extern int warnings;
     40 
     41 	va_list va;
     42 	va_start(va, fmt);
     43 	warn_error(warnings, fmt, va);
     44 	va_end(va);
     45 }
     46 
     47 void
     48 error(char *fmt, ...)
     49 {
     50 	va_list va;
     51 
     52 	va_start(va, fmt);
     53 	warn_error(-1, fmt, va);
     54 	va_end(va);
     55 	exit(1);
     56 	discard();
     57 }
     58 
     59 void
     60 errorp(char *fmt, ...)
     61 {
     62 	va_list va;
     63 	va_start(va, fmt);
     64 	warn_error(-1, fmt, va);
     65 	va_end(va);
     66 }
     67 
     68 void
     69 cpperror(char *fmt, ...)
     70 {
     71 	va_list va;
     72 	va_start(va, fmt);
     73 	warn_error(-1, fmt, va);
     74 	va_end(va);
     75 
     76 	/* discard input until the end of the line */
     77 	*input->p = '\0';
     78 	next();
     79 }
     80 
     81 void
     82 unexpected(void)
     83 {
     84 	error("unexpected '%s'", yytext);
     85 }