shithub: scc

Download patch

ref: aa770c65e75cb4c6d851368cc733fbdef6b0d94f
parent: 9e00ad1d10ff45677223d724cc9bcd1fbc032f9e
author: Roberto E. Vargas Caballero <[email protected]>
date: Sat Oct 3 06:08:50 EDT 2015

Add flag to enable/disable debug in debug compilation

--- a/cc1/cpp.c
+++ b/cc1/cpp.c
@@ -223,12 +223,12 @@
 	if (!parsepars(arguments, arglist, atoi(s)))
 		return 0;
 	for (n = 0; n < atoi(s); ++n)
-		DBG("MACRO par%d:%s\n", n, arglist[n]);
+		DBG("MACRO par%d:%s", n, arglist[n]);
 
 	elen = copymacro(buffer, s+3, INPUTSIZ-1, arglist);
 
 substitute:
-	DBG("MACRO '%s' expanded to :'%s'\n", macroname, buffer);
+	DBG("MACRO '%s' expanded to :'%s'", macroname, buffer);
 	rlen = strlen(input->p);      /* rigth length */
 	llen = begin - input->line;   /* left length */
 	ilen = input->p - begin;      /* invocation length */
@@ -370,7 +370,7 @@
 	if (!getdefs(args, n, buff+3, LINESIZ-3))
 		goto delete;
 	sym->u.s = xstrdup(buff);
-	DBG("MACRO '%s' defined as '%s'\n", sym->name, buff);
+	DBG("MACRO '%s' defined as '%s'", sym->name, buff);
 	return;
 
 delete:
--- a/cc1/lex.c
+++ b/cc1/lex.c
@@ -577,7 +577,7 @@
 		yytoken = operator();
 
 exit:
-	DBG("TOKEN %s\n", yytext);
+	DBG("TOKEN %s", yytext);
 	return yytoken;
 }
 
--- a/cc1/main.c
+++ b/cc1/main.c
@@ -27,7 +27,7 @@
 static void
 usage(void)
 {
-	fputs("usage: cc1 [-w] [-o output] [input]\n", stderr);
+	fputs("usage: cc1 [-w] [-d] [-o output] [input]\n", stderr);
 	exit(1);
 }
 
@@ -50,6 +50,9 @@
 				break;
 			case 'E':
 				onlycpp = 1;
+				break;
+			case 'd':
+				DBGON();
 				break;
 			case 'I':
 				incdir(cp+1);
--- a/inc/cc.h
+++ b/inc/cc.h
@@ -8,9 +8,12 @@
 #endif
 
 #ifndef NDEBUG
-#define DBG(...) fprintf(stderr, __VA_ARGS__)
+extern int debug;
+#define DBG(fmt, ...) dbg(fmt, __VA_ARGS__)
+#define DBGON() (debug = 1)
 #else
 #define DBG(...)
+#define DBGON
 #endif
 
 #define L_INT8      'C'
@@ -39,8 +42,8 @@
 #define L_EXTERN    'X'
 
 extern void die(const char *fmt, ...);
+extern void dbg(const char *fmt, ...);
 extern void *xmalloc(size_t size);
 extern void *xcalloc(size_t nmemb, size_t size);
 extern char *xstrdup(const char *s);
 extern void *xrealloc(void *buff, register size_t size);
-
--- a/lib/Makefile
+++ b/lib/Makefile
@@ -1,6 +1,6 @@
 include ../config.mk
 
-OBJS = die.o xcalloc.o xmalloc.o xrealloc.o xstrdup.o
+OBJS = die.o xcalloc.o xmalloc.o xrealloc.o xstrdup.o debug.o
 
 all: libcc.a
 
--- /dev/null
+++ b/lib/debug.c
@@ -1,0 +1,20 @@
+
+#include <stdarg.h>
+#include <stdio.h>
+
+#include "../inc/cc.h"
+
+int debug;
+
+void
+dbg(const char *fmt, ...)
+{
+	if (!debug)
+		return;
+	va_list va;
+	va_start(va, fmt);
+	vfprintf(stderr, fmt, va);
+	putc('\n', stderr);
+	va_end(va);
+	return;
+}