changeset 185:16b4451e34b8

Add the ability to output line numbers, sort of. It is enabled with the intentionally undocumented -p option (similar to -P but reversed sense) and it might be vaguely useful but only prints the line number when the file changes and may not get the line numbers right.
author David A. Holland
date Fri, 12 Jun 2015 03:59:36 -0400
parents d359d9b86327
children 9637bf434f8e
files TODO main.c mode.h output.c place.c place.h
diffstat 6 files changed, 41 insertions(+), 16 deletions(-) [+]
line wrap: on
line diff
--- a/TODO	Fri Jun 12 03:35:01 2015 -0400
+++ b/TODO	Fri Jun 12 03:59:36 2015 -0400
@@ -33,3 +33,5 @@
 	  twice, once when the macro is defined and again when it's
 	  expanded. Note that gcc's cpp -traditional is getting t37
 	  wrong, and it gets t36 wrong with -C.
+	- remove the intentionally undocumented -p option and generate
+	  proper linenumber output.
--- a/main.c	Fri Jun 12 03:35:01 2015 -0400
+++ b/main.c	Fri Jun 12 03:59:36 2015 -0400
@@ -55,6 +55,7 @@
 
 	.do_output = true,
 	.output_linenumbers = true,
+	.output_cheaplinenumbers = false,
 	.output_retain_comments = false,
 	.output_file = NULL,
 
@@ -781,6 +782,7 @@
 	{ "MG",                         &mode.depend_assume_generated, true },
 	{ "MP",                         &mode.depend_issue_fakerules,  true },
 	{ "P",                          &mode.output_linenumbers,      false },
+	{ "p",                          &mode.output_cheaplinenumbers, true },
 	{ "Wcomment",                   &warns.nestcomment,    true },
 	{ "Wendif-labels",              &warns.endiflabels,    true },
 	{ "Werror",                     &mode.werror,          true },
--- a/mode.h	Fri Jun 12 03:35:01 2015 -0400
+++ b/mode.h	Fri Jun 12 03:59:36 2015 -0400
@@ -37,6 +37,7 @@
 	bool do_stddef;
 	bool do_output;
 	bool output_linenumbers;
+	bool output_cheaplinenumbers;
 	bool output_retain_comments;
 	const char *output_file;
 	bool do_depend;
--- a/output.c	Fri Jun 12 03:35:01 2015 -0400
+++ b/output.c	Fri Jun 12 03:59:36 2015 -0400
@@ -27,6 +27,7 @@
  * POSSIBILITY OF SUCH DAMAGE.
  */
 
+#include <stdio.h>
 #include <string.h>
 #include <unistd.h>
 #include <fcntl.h>
@@ -172,6 +173,15 @@
 		linebuf = dorealloc(linebuf, oldmax, linebufmax);
 	}
 	if (linebufpos == 0) {
+		if (!place_samefile(&linebufplace, p)) {
+			if (mode.output_cheaplinenumbers) {
+				char str[256];
+
+				snprintf(str, sizeof(str), "# %u \"%s\"\n",
+					 p->line, place_getname(p));
+				dowrite(str, strlen(str));
+			}
+		}
 		linebufplace = *p;
 	}
 	memcpy(linebuf + linebufpos, buf, len);
--- a/place.c	Fri Jun 12 03:35:01 2015 -0400
+++ b/place.c	Fri Jun 12 03:59:36 2015 -0400
@@ -102,22 +102,6 @@
 }
 
 static
-bool
-place_eq(const struct place *a, const struct place *b)
-{
-	if (a->type != b->type) {
-		return false;
-	}
-	if (a->file != b->file) {
-		return false;
-	}
-	if (a->line != b->line || a->column != b->column) {
-		return false;
-	}
-	return true;
-}
-
-static
 struct placefile *
 placefile_find(const struct place *incfrom, const char *name)
 {
@@ -219,6 +203,30 @@
 	return NULL;
 }
 
+bool
+place_samefile(const struct place *a, const struct place *b)
+{
+	if (a->type != b->type) {
+		return false;
+	}
+	if (a->file != b->file) {
+		return false;
+	}
+	return true;
+}
+
+bool
+place_eq(const struct place *a, const struct place *b)
+{
+	if (!place_samefile(a, b)) {
+		return false;
+	}
+	if (a->line != b->line || a->column != b->column) {
+		return false;
+	}
+	return true;
+}
+
 static
 void
 place_printfrom(const struct place *p)
--- a/place.h	Fri Jun 12 03:35:01 2015 -0400
+++ b/place.h	Fri Jun 12 03:59:36 2015 -0400
@@ -55,6 +55,8 @@
 
 const char *place_getname(const struct place *);
 const char *place_getparsedir(const struct place *incplace);
+bool place_eq(const struct place *, const struct place *);
+bool place_samefile(const struct place *, const struct place *);
 
 void place_changefile(struct place *p, const char *name);