shithub: scc

Download patch

ref: f5d6bef3655e4a6d692c3cc3116f9fbea1aef257
parent: bac3b4bfa338ea25a630b7dd10b2cab2cc9633df
author: Roberto E. Vargas Caballero <k0ga@shike2.com>
date: Wed Jan 25 10:13:57 EST 2017

[cc1] Implement -M flag

This flag enables the inclusion mode where cc1 only prints
the inclusion dependencies of the input file.

--- a/cc1/cc1.h
+++ b/cc1/cc1.h
@@ -453,9 +453,11 @@
 extern int disexpand;
 extern unsigned cppctx;
 extern Input *input;
-extern int lexmode, namespace, onlycpp;
+extern int lexmode, namespace;
+extern int onlycpp, onlyheader;
 extern unsigned curctx;
 extern Symbol *curfun, *zero, *one;
+extern char *infile, *outfile;
 
 extern Type *voidtype, *pvoidtype, *booltype,
             *uchartype,   *chartype, *schartype,
--- a/cc1/code.c
+++ b/cc1/code.c
@@ -159,7 +159,7 @@
 {
 	extern int failure;
 
-	if (failure || onlycpp)
+	if (failure || onlycpp || onlyheader)
 		return;
 	(*opcode[op])(op, arg);
 }
--- a/cc1/cpp.c
+++ b/cc1/cpp.c
@@ -788,6 +788,8 @@
 	char c, *s, *t;
 
 	for (next(); yytoken != EOFTOK; next()) {
+		if (onlyheader)
+			continue;
 		if (yytoken != STRING) {
 			printf("%s ", yytext);
 			continue;
--- a/cc1/lex.c
+++ b/cc1/lex.c
@@ -89,6 +89,8 @@
 		if ((fp = fopen(fname, "r")) == NULL)
 			return 0;
 		flags = IFILE;
+		if (input && onlyheader)
+			printf("%s: %s\n", infile, fname);
 	} else {
 		/* reading from stdin */
 		fp = stdin;
--- a/cc1/main.c
+++ b/cc1/main.c
@@ -10,22 +10,22 @@
 #include "../inc/cc.h"
 #include "cc1.h"
 
-char *argv0;
+char *argv0, *infile, *outfile;
 
 int warnings;
 jmp_buf recover;
 
-static char *output;
 static struct items uflags;
-int onlycpp;
+int onlycpp, onlyheader;
 
+
 extern int failure;
 
 static void
 clean(void)
 {
-	if (failure && output)
-		remove(output);
+	if (failure && outfile)
+		remove(outfile);
 }
 
 static void
@@ -62,6 +62,9 @@
 	case 'D':
 		defmacro(EARGF(usage()));
 		break;
+	case 'M':
+		onlyheader = 1;
+		break;
 	case 'E':
 		onlycpp = 1;
 		break;
@@ -75,7 +78,7 @@
 		DBGON();
 		break;
 	case 'o':
-		output = EARGF(usage());
+		outfile = EARGF(usage());
 		break;
 	case 'w':
 		warnings = 1;
@@ -87,17 +90,18 @@
 	if (argc > 1)
 		usage();
 
-	if (output && !freopen(output, "w", stdout))
+	if (outfile && !freopen(outfile, "w", stdout))
 		die("error opening output: %s", strerror(errno));
 
 	for (i = 0; i < uflags.n; ++i)
 		undefmacro(uflags.s[i]);
 
+	infile = (*argv) ? *argv : "<stdin>";
 	if (!addinput(*argv, NULL, NULL)) {
 		die("error: failed to open input file '%s': %s",
 		    *argv, strerror(errno));
 	}
-	if (onlycpp) {
+	if (onlycpp || onlyheader) {
 		outcpp();
 	} else {
 		for (next(); yytoken != EOFTOK; decl())
--- a/driver/posix/scc.c
+++ b/driver/posix/scc.c
@@ -54,7 +54,7 @@
 static char *tmpdir;
 static size_t tmpdirln;
 static struct items objtmp, objout;
-static int Eflag, Sflag, cflag, kflag, sflag;
+static int Mflag, Eflag, Sflag, cflag, kflag, sflag;
 
 extern int failure;
 
@@ -326,7 +326,7 @@
 	for (; tool < LAST_TOOL; tool = nexttool) {
 		switch (tool) {
 		case CC1:
-			if (Eflag)
+			if (Eflag || Mflag)
 				nexttool = LAST_TOOL;
 			else
 				nexttool = kflag ? TEEIR : CC2;
@@ -400,10 +400,10 @@
 {
 	die("usage: scc [-D def[=val]]... [-U def]... [-I dir]... "
 	    "[-L dir]... [-l dir]...\n"
-	    "           [-gksw] [-m arch] [-E|-S] [-o outfile] file...\n"
+	    "           [-gksw] [-m arch] [-M|-E|-S] [-o outfile] file...\n"
 	    "       scc [-D def[=val]]... [-U def]... [-I dir]... "
 	    "[-L dir]... [-l dir]...\n"
-	    "           [-gksw] [-m arch] [-E|-S] -c file...\n"
+	    "           [-gksw] [-m arch] [-M|-E|-S] -c file...\n"
 	    "       scc [-D def[=val]]... [-U def]... [-I dir]... "
 	    "[-L dir]... [-l dir]...\n"
 	    "           [-gksw] [-m arch] -c -o outfile file");
@@ -427,6 +427,10 @@
 		addarg(CC1, "-D");
 		addarg(CC1, EARGF(usage()));
 		break;
+	case 'M':
+		Mflag = 1;
+		addarg(CC1, "-M");
+		break;
 	case 'E':
 		Eflag = 1;
 		addarg(CC1, "-E");
@@ -486,7 +490,9 @@
 	for (; *argv; --argc, ++argv)
 		goto operand;
 
-	if (Eflag && (Sflag || kflag) || linkchain.n == 0 ||
+	if (Eflag && Mflag ||
+            (Eflag || Mflag) && (Sflag || kflag) ||
+	    linkchain.n == 0 ||
 	    linkchain.n > 1 && cflag && outfile)
 		usage();
 
@@ -494,7 +500,7 @@
 		tmpdir = ".";
 	tmpdirln = strlen(tmpdir);
 
-	build(&linkchain, (link = !(Eflag || Sflag || cflag)));
+	build(&linkchain, (link = !(Mflag || Eflag || Sflag || cflag)));
 
 	if (!(link || cflag))
 		return failure;