[PATCH] cc1: Use FATAL() instead of abort()

From: Roberto E. Vargas Caballero <k0ga_at_shike2.com>
Date: Thu, 2 Mar 2023 18:42:24 +0100

FATAL() allows to give some additional information about
the reason to abort.
---
 include/scc/scc/scc.h   |   5 ++
 src/cmd/cc/cc1/cc1.h    |   5 +-
 src/cmd/cc/cc1/code.c   |   6 +-
 src/cmd/cc/cc1/error.c  | 169 ++++++++++++++++++++++++++++++++++++++++
 src/cmd/cc/cc1/expr.c   |   4 +-
 src/cmd/cc/cc1/fold.c   |   2 +-
 src/cmd/cc/cc1/init.c   |   6 +-
 src/cmd/cc/cc1/lex.c    |   4 +-
 src/cmd/cc/cc1/symbol.c |   2 +-
 src/cmd/cc/cc1/types.c  |   8 +-
 src/libscc/Makefile     |   1 +
 src/libscc/fatal.c      |  18 +++++
 12 files changed, 213 insertions(+), 17 deletions(-)
 create mode 100644 src/libscc/fatal.c
diff --git a/include/scc/scc/scc.h b/include/scc/scc/scc.h
index e18b38ea..eee2179c 100644
--- a/include/scc/scc/scc.h
+++ b/include/scc/scc/scc.h
_at_@ -5,9 +5,11 @@ extern int enadebug;
 #ifndef NDEBUG
 #define DBG(...) dbg(__VA_ARGS__)
 #define DBGON() (enadebug = 1)
+#define FATAL(...) fatal(__FILE__, __LINE__, __VA_ARGS__)
 #else
 #define DBG(...)
 #define DBGON()
+#define FATAL(...) abort()
 #endif
 
 #define TINT        long long
_at_@ -17,6 +19,8 @@ extern int enadebug;
 #define TFLOAT      double
 #define SIZET       size_t
 
+#define NELEM(x) (sizeof(x)/sizeof((x)[0]))
+
 struct items {
 	char **s;
 	unsigned n;
_at_@ -26,6 +30,7 @@ typedef struct alloc Alloc;
 
 extern void die(const char *fmt, ...);
 extern void dbg(const char *fmt, ...);
+extern void fatal(char *, int, char *, ...);
 extern void newitem(struct items *items, char *item);
 extern void *xmalloc(size_t size);
 extern void *xcalloc(size_t nmemb, size_t size);
diff --git a/src/cmd/cc/cc1/cc1.h b/src/cmd/cc/cc1/cc1.h
index e64cbf3f..33792454 100644
--- a/src/cmd/cc/cc1/cc1.h
+++ b/src/cmd/cc/cc1/cc1.h
_at_@ -129,7 +129,7 @@ enum {
 	CPPMODE
 };
 
-/* input tokens */
+/* input tokens (see error.c for strings) */
 enum tokens {
 	CONST      = 1 << 0,      /* type qualifier tokens are used as flags */
 	RESTRICT   = 1 << 1,
_at_@ -216,7 +216,7 @@ enum tokens {
 	EOFTOK
 };
 
-/* operations */
+/* operations (see error.c for strings) */
 enum op {
 	OADD,
 	OMUL,
_at_@ -433,6 +433,7 @@ extern void unexpected(void);
 extern void errorp(char *fmt, ...);
 extern void cpperror(char *fmt, ...);
 extern Type *deftype(Type *tp);
+extern char *opstr(int), *tokstr(int);
 
 /* types.c */
 extern int eqtype(Type *tp1, Type *tp2, int eqflag);
diff --git a/src/cmd/cc/cc1/code.c b/src/cmd/cc/cc1/code.c
index 89e3033e..5c8144b0 100644
--- a/src/cmd/cc/cc1/code.c
+++ b/src/cmd/cc/cc1/code.c
_at_@ -230,7 +230,7 @@ emitconst(Node *np)
 		        (long long) u & ones(tp->size));
 		break;
 	default:
-		abort();
+		FATAL("default abort: tp->op = %s\n", tokstr(tp->op));
 	}
 }
 
_at_@ -306,7 +306,7 @@ emittype(int op, void *arg)
 	case ENUM:
 		return;
 	default:
-		abort();
+		FATAL("default abort: tp->op = %s\n", tokstr(tp->op));
 	}
 }
 
_at_@ -404,7 +404,7 @@ emitdesig(Node *np, Type *tp, SIZET *addr)
 		emitpadding(tp, addr);
 		break;
 	default:
-		abort();
+		FATAL("default abort: tp->op = %s\n", tokstr(tp->op));
 	}
 
 	if (sym) {
diff --git a/src/cmd/cc/cc1/error.c b/src/cmd/cc/cc1/error.c
index a89b7e7f..716811ed 100644
--- a/src/cmd/cc/cc1/error.c
+++ b/src/cmd/cc/cc1/error.c
_at_@ -10,6 +10,175 @@
 extern int failure;
 static unsigned nerrors;
 
+#ifndef NDEBUG
+char *
+tokstr(int tok)
+{
+	char *strings[] = {
+		[CONST] = "CONST",
+		[RESTRICT] = "RESTRICT",
+		[VOLATILE] = "VOLATILE",
+		[INLINE] = "INLINE",
+		[TQUALIFIER] = "TQUALIFIER",
+		[MACROPAR] = "MACROPAR",
+		[CONCAT] = "CONCAT",
+		[STRINGIZE] = "STRINGIZE",
+		[TYPE] = "TYPE",
+		[IDEN] = "IDEN",
+		[SCLASS] = "SCLASS",
+		[CONSTANT] = "CONSTANT",
+		[STRING] = "STRING",
+		[SIZEOF] = "SIZEOF",
+		[INDIR] = "INDIR",
+		[INC] = "INC",
+		[DEC] = "DEC",
+		[SHL] = "SHL",
+		[SHR] = "SHR",
+		[LE] = "LE",
+		[GE] = "GE",
+		[EQ] = "EQ",
+		[NE] = "NE",
+		[AND] = "AND",
+		[OR] = "OR",
+		[MUL_EQ] = "MUL_EQ",
+		[DIV_EQ] = "DIV_EQ",
+		[MOD_EQ] = "MOD_EQ",
+		[ADD_EQ] = "ADD_EQ",
+		[SUB_EQ] = "SUB_EQ",
+		[AND_EQ] = "AND_EQ",
+		[XOR_EQ] = "XOR_EQ",
+		[OR_EQ] = "OR_EQ",
+		[SHL_EQ] = "SHL_EQ",
+		[SHR_EQ] = "SHR_EQ",
+		[ELLIPSIS] = "ELLIPSIS",
+		[CASE] = "CASE",
+		[DEFAULT] = "DEFAULT",
+		[IF] = "IF",
+		[ELSE] = "ELSE",
+		[SWITCH] = "SWITCH",
+		[WHILE] = "WHILE",
+		[DO] = "DO",
+		[FOR] = "FOR",
+		[GOTO] = "GOTO",
+		[VOID] = "VOID",
+		[FLOAT] = "FLOAT",
+		[INT] = "INT",
+		[BOOL] = "BOOL",
+		[VA_LIST] = "VA_LIST",
+		[STRUCT] = "STRUCT",
+		[UNION] = "UNION",
+		[CHAR] = "CHAR",
+		[DOUBLE] = "DOUBLE",
+		[SHORT] = "SHORT",
+		[LONG] = "LONG",
+		[LLONG] = "LLONG",
+		[COMPLEX] = "COMPLEX",
+		[TYPEDEF] = "TYPEDEF",
+		[EXTERN] = "EXTERN",
+		[STATIC] = "STATIC",
+		[AUTO] = "AUTO",
+		[REGISTER] = "REGISTER",
+		[ENUM] = "ENUM",
+		[TYPEIDEN] = "TYPEIDEN",
+		[UNSIGNED] = "UNSIGNED",
+		[SIGNED] = "SIGNED",
+		[CONTINUE] = "CONTINUE",
+		[BREAK] = "BREAK",
+		[RETURN] = "RETURN",
+		[DEFINE] = "DEFINE",
+		[DEFINED] = "DEFINED",
+		[INCLUDE] = "INCLUDE",
+		[LINE] = "LINE",
+		[PRAGMA] = "PRAGMA",
+		[ERROR] = "ERROR",
+		[IFDEF] = "IFDEF",
+		[ELIF] = "ELIF",
+		[IFNDEF] = "IFNDEF",
+		[UNDEF] = "UNDEF",
+		[ENDIF] = "ENDIF",
+		[BUILTIN] = "BUILTIN",
+		[EOFTOK] = "EOFTOK"
+	};
+
+	if (tok >= NELEM(strings) || !strings[tok])
+		return "unkown token";
+	return strings[tok];
+}
+
+char *
+opstr(int op)
+{
+	char *strings[] = {
+		[OADD] = "OADD",
+		[OMUL] = "OMUL",
+		[OSUB] = "OSUB",
+		[OINC] = "OINC",
+		[ODEC] = "ODEC",
+		[ODIV] = "ODIV",
+		[OMOD] = "OMOD",
+		[OSHL] = "OSHL",
+		[OSHR] = "OSHR",
+		[OBAND] = "OBAND",
+		[OBXOR] = "OBXOR",
+		[OBOR] = "OBOR",
+		[OSNEG] = "OSNEG",
+		[ONEG] = "ONEG",
+		[OCPL] = "OCPL",
+		[OAND] = "OAND",
+		[OOR] = "OOR",
+		[OEQ] = "OEQ",
+		[ONE] = "ONE",
+		[OLT] = "OLT",
+		[OGE] = "OGE",
+		[OLE] = "OLE",
+		[OGT] = "OGT",
+		[OASSIGN] = "OASSIGN",
+		[OA_MUL] = "OA_MUL",
+		[OA_DIV] = "OA_DIV",
+		[OA_MOD] = "OA_MOD",
+		[OA_ADD] = "OA_ADD",
+		[OA_SUB] = "OA_SUB",
+		[OA_SHL] = "OA_SHL",
+		[OA_SHR] = "OA_SHR",
+		[OA_AND] = "OA_AND",
+		[OA_XOR] = "OA_XOR",
+		[OA_OR] = "OA_OR",
+		[OADDR] = "OADDR",
+		[OCOMMA] = "OCOMMA",
+		[OCAST] = "OCAST",
+		[OPTR] = "OPTR",
+		[OSYM] = "OSYM",
+		[OASK] = "OASK",
+		[OCOLON] = "OCOLON",
+		[OFIELD] = "OFIELD",
+		[OLABEL] = "OLABEL",
+		[ODEFAULT] = "ODEFAULT",
+		[OCASE] = "OCASE",
+		[OJUMP] = "OJUMP",
+		[OBRANCH] = "OBRANCH",
+		[OEXPR] = "OEXPR",
+		[OEFUN] = "OEFUN",
+		[OELOOP] = "OELOOP",
+		[OBLOOP] = "OBLOOP",
+		[OFUN] = "OFUN",
+		[OPAR] = "OPAR",
+		[OCALL] = "OCALL",
+		[OCALLE] = "OCALLE",
+		[ORET] = "ORET",
+		[ODECL] = "ODECL",
+		[OBSWITCH] = "OBSWITCH",
+		[OESWITCH] = "OESWITCH",
+		[OINIT] = "OINIT",
+		[OBUILTIN] = "OBUILTIN",
+		[OTYP] = "OTYP",
+	};
+
+	if (op >= NELEM(strings) || !strings[op])
+		return "unkown operation";
+	return strings[op];
+}
+#endif
+
 static void
 warn_error(int flag, char *fmt, va_list va)
 {
diff --git a/src/cmd/cc/cc1/expr.c b/src/cmd/cc/cc1/expr.c
index 75edb4b4..dcba095f 100644
--- a/src/cmd/cc/cc1/expr.c
+++ b/src/cmd/cc/cc1/expr.c
_at_@ -81,7 +81,7 @@ promote(Node *np)
 		tp = doubletype;
 		break;
 	default:
-		abort();
+		FATAL("default abort: tp->op = %s\n", tokstr(tp->op));
 	}
 	if ((new = convert(np, tp, 1)) != NULL)
 		return new;
_at_@ -466,7 +466,7 @@ negop(int op)
 	case OGE:  return OLT;
 	case OLE:  return OGT;
 	case OGT:  return OLE;
-	default:   abort();
+	default:   FATAL("default abort: op = %s\n", opstr(op));
 	}
 	return op;
 }
diff --git a/src/cmd/cc/cc1/fold.c b/src/cmd/cc/cc1/fold.c
index e43fff79..124f9081 100644
--- a/src/cmd/cc/cc1/fold.c
+++ b/src/cmd/cc/cc1/fold.c
_at_@ -305,7 +305,7 @@ foldconst(int type, int op, Type *tp, Symbol *ls, Symbol *rs)
 			return NULL;
 		break;
 	default:
-		abort();
+		FATAL("default abort: type = %s\n", tokstr(type));
 	}
 	sym = newsym(NS_IDEN, NULL);
 	sym->flags |= SCONSTANT;
diff --git a/src/cmd/cc/cc1/init.c b/src/cmd/cc/cc1/init.c
index b3360cec..65967f3c 100644
--- a/src/cmd/cc/cc1/init.c
+++ b/src/cmd/cc/cc1/init.c
_at_@ -343,8 +343,10 @@ repeat:
 		goto repeat;
 	case ARY:
 	case STRUCT:
-		if (!(np->flags & NCONST))
-			abort(); /* TODO */
+		if (!(np->flags & NCONST)) {
+			/* TODO */
+			FATAL("default abort: tp->op = %s\n", tokstr(tp->op));
+		}
 		hidden = newsym(NS_IDEN, NULL);
 		hidden->id = newid();
 		hidden->type = sym->type;
diff --git a/src/cmd/cc/cc1/lex.c b/src/cmd/cc/cc1/lex.c
index 22f0928c..c356fec9 100644
--- a/src/cmd/cc/cc1/lex.c
+++ b/src/cmd/cc/cc1/lex.c
_at_@ -107,7 +107,7 @@ addinput(int type, void *arg, int fail)
 		DBG("INPUT: file input 'stdin'");
 		break;
 	default:
-		abort();
+		FATAL("default abort: type = %d\n", type);
 	}
 
 	if (!buffer) {
_at_@ -153,7 +153,7 @@ delinput(void)
 		DBG("INPUT: stdin finished");
 		break;
 	default:
-		abort();
+		FATAL("default abort: type = %d\n", ip->flags & ITYPE);
 	}
 
 	input = ip->next;
diff --git a/src/cmd/cc/cc1/symbol.c b/src/cmd/cc/cc1/symbol.c
index 7055ff03..879df899 100644
--- a/src/cmd/cc/cc1/symbol.c
+++ b/src/cmd/cc/cc1/symbol.c
_at_@ -40,7 +40,7 @@ dumpstab(Symbol **tbl, char *msg)
 	else if (tbl == htablbl)
 		size = NR_LBL_HASH;
 	else
-		abort();
+		FATAL("default abort; tbl = %d\n", tbl);
 
 	for (bp = tbl; bp < &tbl[size]; ++bp) {
 		if (*bp == NULL)
diff --git a/src/cmd/cc/cc1/types.c b/src/cmd/cc/cc1/types.c
index fc2b84a9..53cf4e9c 100644
--- a/src/cmd/cc/cc1/types.c
+++ b/src/cmd/cc/cc1/types.c
_at_@ -98,7 +98,7 @@ getlimits(Type *tp)
 		}
 		break;
 	default:
-		abort();
+		FATAL("default abort: tp->op = %s\n", tokstr(tp->op));
 	}
 
 	return &limits[ntable][ntype];
_at_@ -240,7 +240,7 @@ typesize(Type *tp)
 	case FTN:
 		return;
 	default:
-		abort();
+		FATAL("default abort: tp->op = %s\n", tokstr(tp->op));
 	}
 }
 
_at_@ -337,7 +337,7 @@ mktype(Type *tp, int op, TINT nelem, Type *pars[])
 	create_type:
 		return newtype(&type);
 	default:
-		abort();
+		FATAL("default abort: op = %s\n", tokstr(op));
 	}
 
 	tbl = &typetab[HASH(&type)];
_at_@ -467,7 +467,7 @@ eqtype(Type *tp1, Type *tp2, int equiv)
 	case FLOAT:
 		return tp1->letter == tp2->letter;
 	default:
-		abort();
+		FATAL("default abort: tp1->op = %s\n", tokstr(tp1->op));
 	}
 }
 
diff --git a/src/libscc/Makefile b/src/libscc/Makefile
index a0a0b070..752c8f7d 100644
--- a/src/libscc/Makefile
+++ b/src/libscc/Makefile
_at_@ -6,6 +6,7 @@ include $(PROJECTDIR)/scripts/rules.mk
 OBJS =\
 	debug.o\
 	die.o\
+	fatal.o\
 	newitem.o\
 	xcalloc.o\
 	xmalloc.o\
diff --git a/src/libscc/fatal.c b/src/libscc/fatal.c
new file mode 100644
index 00000000..259de16e
--- /dev/null
+++ b/src/libscc/fatal.c
_at_@ -0,0 +1,18 @@
+#include <stdarg.h>
+#include <stdio.h>
+#include <stdlib.h>
+
+#include <scc/arg.h>
+#include <scc/scc.h>
+
+void
+fatal(char *fname, int lineno, char *fmt, ...)
+{
+	va_list va;
+
+	va_start(va, fmt);
+	fprintf(stderr, "%s:", argv0);
+	vfprintf(stderr, fmt, va);
+	fputc('\n', stderr);
+	abort();
+}
-- 
2.39.2
--
To unsubscribe send a mail to scc-dev+unsubscribe_at_simple-cc.org
Received on Mon 17 Sep 2001 - 00:00:00 CEST

This archive was generated by hypermail 2.3.0 : Fri 21 Apr 2023 - 16:30:07 CEST