cc1: Pretty format cpp output - scc - simple c99 compiler
HTML git clone git://git.simple-cc.org/scc
DIR Log
DIR Files
DIR Refs
DIR README
DIR LICENSE
---
DIR commit 027ea37e73f2faca232788a21dd1d1280c74adbc
DIR parent 89f23214f7e97af01aea675a2a24650fbc5d77ea
HTML Author: Roberto E. Vargas Caballero <k0ga@shike2.net>
Date: Thu, 18 Jun 2026 13:40:08 +0200
cc1: Pretty format cpp output
The output of cc1 -E was ugly because indentation was lost
and because spaces were blindly added just to avoid token
concatenation. This patch solves the problem in a easy way
because we can access directly the input line to recover
the leading spaces and we already have the variable yyspace,
which holds the last space seen, but it was mangled by
expandarg() to handle spaces correctly for stringize of
macro parameters. We just save the space value before the
macro and then we restore after the expansion was done
because we don't care about intermediate spaces, just spaces
before the expansion of the macro.
Diffstat:
M src/cmd/scc-cc/cc1/cpp.c | 15 +++++++++------
1 file changed, 9 insertions(+), 6 deletions(-)
---
DIR diff --git a/src/cmd/scc-cc/cc1/cpp.c b/src/cmd/scc-cc/cc1/cpp.c
@@ -408,7 +408,6 @@ expandarg(char *arg, char *def, char *curdef, char *buf, int bufsiz)
buf += n;
bufsiz -= n;
}
-
delinput();
}
@@ -606,7 +605,7 @@ intersect(Symbol **dst, Symbol **set)
int
expand(Symbol *sym)
{
- int siz;
+ int spc, siz;
Macro *mp;
DBG("MACRO '%s' detected disexpand=%d hide=%d",
@@ -615,6 +614,7 @@ expand(Symbol *sym)
if (disexpand || sym->hide || sym->token != IDEN)
return 0;
+ spc = yyspace;
mp = newmacro(sym);
mp->fname = filenam;
memcpy(mp->hideset, input->hideset, sizeof(mp->hideset));
@@ -635,6 +635,7 @@ expand(Symbol *sym)
siz = copymacro(mp);
mp->buffer[siz] = '\0';
+ yyspace = spc;
DBG("MACRO '%s' expanded to :'%s'", mp->sym->name, mp->buffer);
@@ -1294,18 +1295,20 @@ ppragmaln(void)
}
nline = lineno;
printf(s, nline, file);
+
+ for (s = input->line; isspace(*s); ++s)
+ putchar(*s);
+ yyspace = 0;
}
void
outcpp(void)
{
- int c;
- char *s, *t;
-
for (next(); yytoken != EOFTOK; next()) {
if (onlyheader)
continue;
- printf("%s ", yytext);
+ printf("%s%s", yyspace ? " " : "", yytext);
+ yyspace = 0;
}
putchar('\n');
}