Esempio n. 1
0
int error(int t, loc* lc, const char* s, const ea& a0, const ea& a1, const ea& a2, const ea& a3)
/*
	"int" not "void" because of "pch" in lex.c

	legal error types are:

	not counted in error count:

		'w'		warning
		'd'		debug
		'D'		debug -- no prefix
		'C'		text -- line no. and no newline
		'c'		text -- no line no. and no newline

	counted in error count:

		's'		"not implemented" message
		'l'		"compiler limit exceeded" message
    		0		error 
    		'e'		error -- no newline
    		'i'		internal error (causes abort)
		't'		error while printing error message
*/
{
	if (suppress_error && t!='i' && t!='d') return 0;

	if (in_error++)
		if (t == 't')
			t = 'i';
		else if (4 < in_error) {
			fprintf(stderr,"\nOops!, error while handling error\n");
			ext(13);
		}

	FILE * of = out_file;
	out_file = stderr;

	if (!scan_started || t=='t')
		putch('\n');
	else if (lc != &dummy_loc) {
		if(t != 'D' && t != 'c') lc->put(out_file);
	} else {
		if(t != 'D' && t != 'c') print_loc();
	}

	int user_error = 0;

	switch (t) {
        case 'C':
        case 'c':
		break;
    	case 'e':
		user_error = 1;
		// no break
    	case 0:
		putstring("error: ");
		user_error += 1;
		break;
        case 'd':
		putstring("DEBUG: ");
        case 'D':
		break;
        case 'w':
//		no_of_warnings++;
		putstring("warning: ");
		break;
        case 'l':
		putstring("compiler limit exceeded: ");
		break;
        case 's':
		putstring("sorry, not implemented: ");
		user_error = 1;
		break;
        case 'i':
		if (error_count++) {
			fprintf(out_file,"sorry, cannot recover from earlier errors\n");
			out_file = of; // restore for fflush()
#ifdef TEST_SUITE
			ext(INTERNAL2);
#else
			ext(INTERNAL);
#endif
		}
		else
			fprintf(out_file,"internal %s error: ",prog_name);
        }

	ea argv[4];
	ea* a = argv;
	argv[0] = a0;
	argv[1] = a1;
	argv[2] = a2;
	argv[3] = a3;

	int c;

	while (c = *s++) {
		if ('A'<=c && c<='Z')
			putstring(abbrev_tbl[c-'A']);
		else if (c == '%') {
			switch (c = *s++) {
			case 'k':	// TOK assumed passed as an int
			{	int x = TOK(a->i);
				if (0 < x && x<=MAXTOK && keys[x])
					fprintf(out_file," %s",keys[x]);
				else
					fprintf(out_file," token(%d)",x);
				break;
			}
			case 't':	// Ptype 
			{	Ptype tt = Ptype(a->p);
				if (tt == 0) break;

				putch(' ');
			
				int nt = ntok;
				emode = 1;
				tt->dcl_print(0);
				emode = 0;
				ntok = nt;
				break;
			}
			case 'n':	// Pname
			{	Pname nn = Pname(a->p);
				if (nn && nn->string) {
					// suppress generated class names:
					if (nn->string[0]=='_'
					&& nn->string[1]=='_'
					&& nn->string[2]=='C') break;
					emode = 1;
					putch(' ');
					nn->print();
					emode = 0;
				}
				else
					putstring(" ?");
				break;
			}
			case 'p':	// pointer
			{	char* f = sizeof(char*)==sizeof(int)?" %d":" %ld";
				fprintf(out_file,f,a->p);
				break;
			}
			case 'a':	// fully qualified function 
			{	Pname nn = Pname(a->p);
				if (nn->tp->base!=FCT && nn->tp->base!=OVERLOAD)
					error('i',"%n not function",nn);
				if (nn && nn->string) {
					// suppress generated class names:
					if (nn->string[0]=='_'
					&& nn->string[1]=='_'
					&& nn->string[2]=='C') break;
					emode = 1;
					putch(' ');
					nn->print(1);
					emode = 0;
				}
				else
					putstring(" ?");
				break;
			}
			case 'c':	// char assumed passed as an int
				putch((int)a->i);
				break;

			case 'd':	// int
				fprintf(out_file," %d",a->i);
				break;

			case 'o':	// int
				fprintf(out_file," 0%o",a->i);
				break;

			case 's':	// char*
				{
				char *s = ((char *)a->p);
				if ( s ) putst((char*)a->p);
				break;
				}
			}
			a++;
		}
		else
			putch(c);
	}

/*
	switch (t) {
	case 'd':
	case 't':
	case 'w':
		putch('\n');
		break;
	default:
*/
		if (t != 'c' && t != 'e' && t != 'C')
			print_context();
/*
	}
*/

	if (user_error) 
		basic_inst::head->print_error_loc(user_error==2);

	out_file = of; // restore before ext() for fflush()
	if (!scan_started && t!='d' && t!='w') ext(4);

        // now we may want to carry on 
	switch (t) {
	case 't':
		if (--in_error) {
			fflush(stderr);
			//fflush(out_file);
			return 0;
		}
	case 'i': 
		ext(INTERNAL);
	case 0:
	case 'e':
	case 'l':
	case 's':
#ifdef TEST_SUITE
		if (t == 's')
			ext(SORRY);
#endif
		if (MAXERR<++error_count) {
			fprintf(stderr,"Sorry, too many errors\n");
			ext(7);
		}
	}

	in_error = 0;
	fflush(stderr);
	//fflush(out_file);
	return 0;
}
Esempio n. 2
0
int error(int t, loc* lc, char* s ...)
/*
	"int" not "void" because of "pch" in lex.c
	subsequent arguments fill in %mumble fields 

	legal error types are:
		'w'		warning	 (not counted in error count)
		'd'		debug
		's'		"not implemented" message
    		0		error 
    		'i'		internal error (causes abort)
		't'		error while printing error message
*/
{
	FILE * of = out_file;
	int c;
	char format[3];	/* used for "% mumble" sequences */
	int * a = &t;
	int argn = 3;

	/* check variable argument passing mechanism */
	int si = sizeof(int);
	int scp = sizeof(char*);
	int ssp = sizeof(Pname);

	if (si!=ssp || si!=scp || ssp!=scp || &a[2]!=(int*)&s) {
		fprintf(stderr,
			"\n%s: this c can't handle varargs (%d,%d,%d -- %d %d)\n",
			prog_name, si, scp, ssp, &a[1], &s);
		ext(12);
	}

	if (t == 'w' && warn==0) return 0;

	if (in_error++)
		if (t!='t' || 4<in_error) {
			fprintf(stderr,"\nUPS!, error while handling error\n");
			ext(13);
		}
	else if (t == 't')
		t = 'i';

	out_file = stderr;
	if (!scan_started)
		/*fprintf(out_file,"error during %s initializing: ",prog_name);*/
		putch('\n');
	else if (t=='t')
		putch('\n');
	else if (lc != &dummy_loc)
		lc->put(out_file);
	else
		print_loc();

    switch (t) {
    	case 0:
		fprintf(out_file,"error: ");
		break;
        case 'w':
		no_of_warnings++;
		fprintf(out_file,"warning: ");
		break;
        case 's':
		fprintf(out_file,"sorry, not implemented: ");
		break;
        case 'i':
		if (error_count++) {
			fprintf(out_file,"sorry, %s cannot recover from earlier errors\n",prog_name);
			ext(INTERNAL);
		}
		else
			fprintf(out_file,"internal %s error: ",prog_name);
		break;
        }

    while (c = *s++) {
	if ('A'<=c && c<='Z' && abbrev_tbl['A'])
		putstring(abbrev_tbl[c]);
	else if (c == '%')
		switch (c = *s++) {
		case 'k':
		{	TOK x = a[argn];
			if (0<x && x<MAXTOK && keys[x])
				fprintf(out_file," %s",keys[x]);
			else
				fprintf(out_file," token(%d)",x);
			argn++;
			break;
		}
		case 't':	/* Ptype */
		{	Ptype tt = (Ptype)a[argn];
			if (tt) {
				TOK pm = print_mode;
				extern int ntok;
				int nt = ntok;
				print_mode = ERROR;
				fprintf(out_file," ");
				tt->dcl_print(0);
				print_mode = pm;
				ntok = nt;
				argn++;
			}
			break;
		}
		case 'n':	/* Pname */
		{	Pname nn = (Pname)a[argn];
			if (nn) {
				TOK pm = print_mode;
				print_mode = ERROR;
				fprintf(out_file," ");
				nn->print();
				print_mode = pm;
			}
			else
				fprintf(out_file," ?");
			argn++;
			break;
		}
		default:
			format[0] = '%';
			format[1] = c;
			format[2] = '\0';
			fprintf(out_file,format,a[argn++]);
			break;
		}
		else
			putch(c);
	}

	if (!scan_started) ext(4);

	switch (t) {
	case 'd':
	case 't':
	case 'w':
		putch('\n');
		break;
	default:
		print_context();
	}
	fflush(stderr);
    /* now we may want to carry on */

	out_file = of;

	switch (t) {
	case 't':
		if (--in_error) return 0;
	case 'i': 
		ext(INTERNAL);
	case 0:
	case 's':
		if (MAXERR<++error_count) {
			fprintf(stderr,"Sorry, too many errors\n");
			ext(7);
		}
	}

	in_error = 0;
	return 0;
}