commit 8f656093e79f9aca87c5f798ab741bbd32d38673
parent 92c212afb9da93c192deddd2f44aaed198af192c
Author: Roberto E. Vargas Caballero <k0ga@shike2.com>
Date: Mon, 5 Oct 2015 20:26:07 +0200
Pretty print strings in cpp mode
Without this code output strings will not be valid
C strings, and it can be a problem for the compiler
using cc1 as preprocessor.
Diffstat:
3 files changed, 45 insertions(+), 2 deletions(-)
diff --git a/cc1/cc1.h b/cc1/cc1.h
@@ -400,6 +400,7 @@ extern void icpp(void);
extern bool cpp(void);
extern bool expand(char *begin, Symbol *sym);
extern void incdir(char *dir);
+extern void outcpp(void);
/*
* Definition of global variables
diff --git a/cc1/cpp.c b/cc1/cpp.c
@@ -717,3 +717,46 @@ cpp(void)
return 1;
}
+
+void
+outcpp(void)
+{
+ char c, *s, *t;
+
+ for (next(); yytoken != EOFTOK; next()) {
+ if (yytoken != CONSTANT || *yytext != '"') {
+ printf("%s ", yytext);
+ continue;
+ }
+ for (s = yylval.sym->u.s; c = *s; ++s) {
+ switch (c) {
+ case '\n':
+ t = "\\n";
+ goto print_str;
+ case '\v':
+ t = "\\v";
+ goto print_str;
+ case '\b':
+ t = "\\b";
+ goto print_str;
+ case '\t':
+ t = "\\t";
+ goto print_str;
+ case '\a':
+ t = "\\a";
+ print_str:
+ fputs(t, stdout);
+ break;
+ case '\\':
+ putchar('\\');
+ default:
+ if (!isprint(c))
+ printf("\\x%x", c);
+ else
+ putchar(c);
+ break;
+ }
+ }
+ }
+}
+
diff --git a/cc1/main.c b/cc1/main.c
@@ -78,8 +78,7 @@ main(int argc, char *argv[])
ilex(*argv);
if (onlycpp) {
- for (next(); yytoken != EOFTOK; next())
- printf("%s ", yytext);
+ outcpp();
} else {
for (next(); yytoken != EOFTOK; decl())
/* nothing */;