[PATCH 1/5] teach sparse about '-o <file>'

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

 



Sparse knows about the '-o' option, parses it but does
nothing with it.

Change this by redirecting stdout to <file> unless <file>
is '-' since sparse (the lib) outputs to stdout by default.
But ignore this flag when sparse is used purely as an checker
since in this case it's not supposed to output to stdout
and would create undesired empty file, possibly erasing the
result of the compiler if one is used before sparse.

Signed-off-by: Luc Van Oostenryck <luc.vanoostenryck@xxxxxxxxx>
---
 lib.c    | 13 +++++++++++++
 lib.h    |  1 +
 sparse.c |  3 +++
 3 files changed, 17 insertions(+)

diff --git a/lib.c b/lib.c
index 5c2059f23..c5f5b1cdf 100644
--- a/lib.c
+++ b/lib.c
@@ -23,6 +23,7 @@
  * THE SOFTWARE.
  */
 #include <ctype.h>
+#include <errno.h>
 #include <fcntl.h>
 #include <stdarg.h>
 #include <stddef.h>
@@ -49,6 +50,7 @@
 int verbose, optimize_level, optimize_size, preprocessing;
 int die_if_error = 0;
 int has_error = 0;
+int do_output = 1;
 
 #ifndef __GNUC__
 # define __GNUC__ 2
@@ -65,6 +67,7 @@ const char *base_filename;
 static const char *diag_prefix = "";
 static const char *gcc_base_dir = GCC_BASE;
 static const char *multiarch_dir = MULTIARCH_TRIPLET;
+static const char *outfile = NULL;
 
 struct token *skip_to(struct token *token, int op)
 {
@@ -674,6 +677,7 @@ static char **handle_switch_o(char *arg, char **next)
 	if (!strcmp (arg, "o")) {       // "-o foo"
 		if (!*++next)
 			die("argument to '-o' is missing");
+		outfile = *next;
 	}
 	// else "-ofoo"
 
@@ -1409,6 +1413,15 @@ struct symbol_list *sparse_initialize(int argc, char **argv, struct string_list
 	handle_switch_v_finalize();
 
 	handle_arch_finalize();
+
+	// Redirect stdout if needed
+	if (dump_macro_defs || preprocess_only)
+		do_output = 1;
+	if (do_output && outfile && strcmp(outfile, "-")) {
+		if (!freopen(outfile, "w", stdout))
+			die("error: cannot open %s: %s", outfile, strerror(errno));
+	}
+
 	if (fdump_ir == 0)
 		fdump_ir = PASS_FINAL;
 
diff --git a/lib.h b/lib.h
index cde74e93e..14b13b676 100644
--- a/lib.h
+++ b/lib.h
@@ -46,6 +46,7 @@
 extern int verbose, optimize_level, optimize_size, preprocessing;
 extern int die_if_error;
 extern int repeat_phase;
+extern int do_output;
 extern int gcc_major, gcc_minor, gcc_patchlevel;
 
 extern const char *base_filename;
diff --git a/sparse.c b/sparse.c
index 975c0a4b4..151eaf4ef 100644
--- a/sparse.c
+++ b/sparse.c
@@ -334,6 +334,9 @@ int main(int argc, char **argv)
 	struct string_list *filelist = NULL;
 	char *file;
 
+	// by default ignore -o <file>
+	do_output = 0;
+
 	// Expand, linearize and show it.
 	check_symbols(sparse_initialize(argc, argv, &filelist));
 	FOR_EACH_PTR(filelist, file) {
-- 
2.19.0




[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