commit 8f656093e79f9aca87c5f798ab741bbd32d38673
Author:     Roberto E. Vargas Caballero <[email protected]>
AuthorDate: Mon Oct 5 20:26:07 2015 +0200
Commit:     Roberto E. Vargas Caballero <[email protected]>
CommitDate: Mon Oct 5 20:26:07 2015 +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.

diff --git a/cc1/cc1.h b/cc1/cc1.h
index 2c6f37b..7da3ea7 100644
--- 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
index 733cab2..a3b6395 100644
--- 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
index 93b7af3..a375ef2 100644
--- 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 */;

Reply via email to