shithub: scc

Download patch

ref: 8f656093e79f9aca87c5f798ab741bbd32d38673
parent: 92c212afb9da93c192deddd2f44aaed198af192c
author: Roberto E. Vargas Caballero <[email protected]>
date: Mon Oct 5 16:26:07 EDT 2015

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.

--- a/cc1/cc1.h
+++ b/cc1/cc1.h
@@ -400,6 +400,7 @@
 extern bool cpp(void);
 extern bool expand(char *begin, Symbol *sym);
 extern void incdir(char *dir);
+extern void outcpp(void);
 
 /*
  * Definition of global variables
--- a/cc1/cpp.c
+++ b/cc1/cpp.c
@@ -717,3 +717,46 @@
 
 	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;
+			}
+		}
+	}
+}
+
--- a/cc1/main.c
+++ b/cc1/main.c
@@ -78,8 +78,7 @@
 	ilex(*argv);
 
 	if (onlycpp) {
-		for (next(); yytoken != EOFTOK; next())
-			printf("%s ", yytext);
+		outcpp();
 	} else {
 		for (next(); yytoken != EOFTOK; decl())
 			/* nothing */;