summaryrefslogtreecommitdiffstatshomepage
diff options
context:
space:
mode:
authorLuc Van Oostenryck <luc.vanoostenryck@gmail.com>2017-09-13 23:32:57 +0200
committerLuc Van Oostenryck <luc.vanoostenryck@gmail.com>2018-11-24 13:51:24 +0100
commit21df8a9d5763dfdcdec8b3e540d6c67459d75f74 (patch)
tree7e75485b1cb86ed4c070a0adabfa263c625ece70
parentfdf8252f312a40df9aa51c6e30c0d07fa29ebd12 (diff)
downloadsparse-21df8a9d5763dfdcdec8b3e540d6c67459d75f74.tar.gz
teach sparse about '-o <file>'
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@gmail.com>
-rw-r--r--lib.c13
-rw-r--r--lib.h1
-rw-r--r--sparse.c3
3 files changed, 17 insertions, 0 deletions
diff --git a/lib.c b/lib.c
index 5c2059f2..c5f5b1cd 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 cde74e93..14b13b67 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 975c0a4b..151eaf4e 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) {