commit aa770c65e75cb4c6d851368cc733fbdef6b0d94f
parent 9e00ad1d10ff45677223d724cc9bcd1fbc032f9e
Author: Roberto E. Vargas Caballero <k0ga@shike2.com>
Date: Sat, 3 Oct 2015 10:08:50 +0200
Add flag to enable/disable debug in debug compilation
Diffstat:
6 files changed, 34 insertions(+), 8 deletions(-)
diff --git a/cc1/cpp.c b/cc1/cpp.c
@@ -223,12 +223,12 @@ expand(char *begin, Symbol *sym)
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 @@ define(void)
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:
diff --git a/cc1/lex.c b/cc1/lex.c
@@ -577,7 +577,7 @@ next(void)
yytoken = operator();
exit:
- DBG("TOKEN %s\n", yytext);
+ DBG("TOKEN %s", yytext);
return yytoken;
}
diff --git a/cc1/main.c b/cc1/main.c
@@ -27,7 +27,7 @@ clean(void)
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);
}
@@ -51,6 +51,9 @@ main(int argc, char *argv[])
case 'E':
onlycpp = 1;
break;
+ case 'd':
+ DBGON();
+ break;
case 'I':
incdir(cp+1);
goto nextiter;
diff --git a/inc/cc.h b/inc/cc.h
@@ -8,9 +8,12 @@ typedef unsigned bool;
#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 @@ typedef unsigned bool;
#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);
-
diff --git 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
diff --git a/lib/debug.c b/lib/debug.c
@@ -0,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;
+}