commit aa770c65e75cb4c6d851368cc733fbdef6b0d94f
Author:     Roberto E. Vargas Caballero <[email protected]>
AuthorDate: Sat Oct 3 10:08:50 2015 +0200
Commit:     Roberto E. Vargas Caballero <[email protected]>
CommitDate: Sat Oct 3 12:12:45 2015 +0200

    Add flag to enable/disable debug in debug compilation

diff --git a/cc1/cpp.c b/cc1/cpp.c
index 311b1bb..355ec2c 100644
--- 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
index 2cd8c58..eb1e2d5 100644
--- 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
index 481124a..d9a20c3 100644
--- 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
index b16a3d9..e21251c 100644
--- 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
index 8114d5c..eacdc16 100644
--- 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
new file mode 100644
index 0000000..c5d63ef
--- /dev/null
+++ 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;
+}

Reply via email to