scc

Simple C99 Compiler
Log | Files | Refs | README | LICENSE

error.c (1258B)


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