scc

simple c99 compiler
git clone git://git.simple-cc.org/scc
Log | Files | Refs | README | LICENSE

error.c (1169B)


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