[PATCH 2/2] add support for -fdiagnostic-prefix[=prefix]

[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index]

 



When using sparse it's common to compile a file and directly
run sparse on the same file, like it is done for the kernel.
In this case, error messages from sparse are interspersed with
those from the compiler. It's thus not always easy to know from
which tools they come.

Fix this by allowing to prefix all the diagnostic messages
by some configurable string, by default "sparse". More exactly,
an error message that was emitted like:
	file.c:<line>:<col>: error: this is invalid code
can now be emitted as:
	file.c:<line>:<col>: sparse: error: this is invalid code

Signed-off-by: Luc Van Oostenryck <luc.vanoostenryck@xxxxxxxxx>
---
 lib.c                     | 22 +++++++++++++++++++---
 sparse.1                  |  6 ++++++
 validation/fdiag-prefix.c | 11 +++++++++++
 3 files changed, 36 insertions(+), 3 deletions(-)
 create mode 100644 validation/fdiag-prefix.c

diff --git a/lib.c b/lib.c
index 30d1cf6d1..67ecded17 100644
--- a/lib.c
+++ b/lib.c
@@ -62,6 +62,7 @@ int gcc_patchlevel = __GNUC_PATCHLEVEL__;
 
 const char *base_filename;
 
+static const char *diag_prefix = "";
 static const char *gcc_base_dir = GCC_BASE;
 static const char *multiarch_dir = MULTIARCH_TRIPLET;
 
@@ -133,8 +134,8 @@ static void do_warn(const char *type, struct position pos, const char * fmt, va_
 	name = stream_name(pos.stream);
 		
 	fflush(stdout);
-	fprintf(stderr, "%s:%d:%d: %s%s\n",
-		name, pos.line, pos.pos, type, buffer);
+	fprintf(stderr, "%s:%d:%d: %s%s%s\n",
+		name, pos.line, pos.pos, diag_prefix, type, buffer);
 }
 
 unsigned int fmax_warnings = 100;
@@ -237,7 +238,7 @@ void die(const char *fmt, ...)
 	vsnprintf(buffer, sizeof(buffer), fmt, args);
 	va_end(args);
 
-	fprintf(stderr, "%s\n", buffer);
+	fprintf(stderr, "%s%s\n", diag_prefix, buffer);
 	exit(1);
 }
 
@@ -899,6 +900,20 @@ static int handle_fpasses(const char *arg, const char *opt, const struct flag *f
 	return 0;
 }
 
+static int handle_fdiagnostic_prefix(const char *arg, const char *opt, const struct flag *flag, int options)
+{
+	switch (*opt) {
+	case '\0':
+		diag_prefix = "sparse: ";
+		return 1;
+	case '=':
+		diag_prefix = xasprintf("%s: ", opt+1);
+		return 1;
+	default:
+		return 0;
+	}
+}
+
 static int handle_fdump_ir(const char *arg, const char *opt, const struct flag *flag, int options)
 {
 	static const struct mask_map dump_ir_options[] = {
@@ -925,6 +940,7 @@ static int handle_fmax_warnings(const char *arg, const char *opt, const struct f
 }
 
 static struct flag fflags[] = {
+	{ "diagnostic-prefix",	NULL,	handle_fdiagnostic_prefix },
 	{ "dump-ir",		NULL,	handle_fdump_ir },
 	{ "max-warnings=",	NULL,	handle_fmax_warnings },
 	{ "mem-report",		&fmem_report },
diff --git a/sparse.1 b/sparse.1
index a1946c869..806fb0cf0 100644
--- a/sparse.1
+++ b/sparse.1
@@ -390,6 +390,12 @@ Report some statistics about memory allocation used by the tool.
 .
 .SH OTHER OPTIONS
 .TP
+.B \-fdiagnostic-prefix[=PREFIX]
+Prefix all diagnostics by the given PREFIX, followed by ": ".
+If no one is given "sparse" is used.
+The default is to not use a prefix at all.
+.
+.TP
 .B \-fmemcpy-max-count=COUNT
 Set the limit for the warnings given by \fB-Wmemcpy-max-count\fR.
 A COUNT of 'unlimited' or '0' will effectively disable the warning.
diff --git a/validation/fdiag-prefix.c b/validation/fdiag-prefix.c
new file mode 100644
index 000000000..71160d456
--- /dev/null
+++ b/validation/fdiag-prefix.c
@@ -0,0 +1,11 @@
+int a.
+
+/*
+ * check-name: fdiag-prefix
+ * check-command: sparse -fdiagnostic-prefix=prefix $file
+ *
+ * check-error-start
+fdiag-prefix.c:1:6: prefix: error: Expected ; at end of declaration
+fdiag-prefix.c:1:6: prefix: error: got .
+ * check-error-end
+ */
-- 
2.17.1

--
To unsubscribe from this list: send the line "unsubscribe linux-sparse" in
the body of a message to majordomo@xxxxxxxxxxxxxxx
More majordomo info at  http://vger.kernel.org/majordomo-info.html



[Index of Archives]     [Newbies FAQ]     [LKML]     [IETF Annouce]     [DCCP]     [Netdev]     [Networking]     [Security]     [Bugtraq]     [Yosemite]     [MIPS Linux]     [ARM Linux]     [Linux Security]     [Linux RAID]     [Linux SCSI]     [Trinity Fuzzer Tool]

  Powered by Linux