diff options
author | Al Viro <viro@zeniv.linux.org.uk> | 2012-04-13 20:59:28 -0400 |
---|---|---|
committer | Al Viro <viro@zeniv.linux.org.uk> | 2013-02-12 09:24:32 -0500 |
commit | c8b02fc63037f6e6e0147fc65a67a7809ca5744e (patch) | |
tree | bfe12cc79683e903e38ba68b8932b63bcd13b384 | |
parent | d83674be51f9650dfcdfcf6f5961757436f17c53 (diff) | |
download | sparse-c8b02fc63037f6e6e0147fc65a67a7809ca5744e.tar.gz |
fix handling of -include
-include foo.h will search not only in the current directory...
Signed-off-by: Al Viro <viro@zeniv.linux.org.uk>
-rw-r--r-- | lib.c | 25 | ||||
-rw-r--r-- | lib.h | 9 | ||||
-rw-r--r-- | pre-process.c | 70 |
3 files changed, 46 insertions, 58 deletions
@@ -225,8 +225,8 @@ static enum { STANDARD_C89, STANDARD_GNU99, } standard = STANDARD_GNU89; #define CMDLINE_INCLUDE 20 -int cmdline_include_nr = 0; -struct cmdline_include cmdline_include[CMDLINE_INCLUDE]; +static int cmdline_include_nr = 0; +static char *cmdline_include[CMDLINE_INCLUDE]; void add_pre_buffer(const char *fmt, ...) @@ -299,16 +299,9 @@ static char **handle_switch_I(char *arg, char **next) static void add_cmdline_include(char *filename) { - int fd = open(filename, O_RDONLY); - if (fd < 0) { - perror(filename); - return; - } if (cmdline_include_nr >= CMDLINE_INCLUDE) die("too many include files for %s\n", filename); - cmdline_include[cmdline_include_nr].filename = filename; - cmdline_include[cmdline_include_nr].fd = fd; - cmdline_include_nr++; + cmdline_include[cmdline_include_nr++] = filename; } static char **handle_switch_i(char *arg, char **next) @@ -892,19 +885,13 @@ static struct symbol_list *sparse_file(const char *filename) */ static struct symbol_list *sparse_initial(void) { - struct token *token; int i; // Prepend any "include" file to the stream. // We're in global scope, it will affect all files! - token = NULL; - for (i = cmdline_include_nr - 1; i >= 0; i--) - token = tokenize(cmdline_include[i].filename, cmdline_include[i].fd, - token, includepath); - - // Prepend the initial built-in stream - if (token) - pre_buffer_end->next = token; + for (i = 0; i < cmdline_include_nr; i++) + add_pre_buffer("#argv_include \"%s\"\n", cmdline_include[i]); + return sparse_tokenstream(pre_buffer_begin); } @@ -41,15 +41,6 @@ struct position { noexpand:1; }; -struct cmdline_include { - char *filename; - int fd; -}; - -extern struct cmdline_include cmdline_include[]; -extern int cmdline_include_nr; - - struct ident; struct token; struct symbol; diff --git a/pre-process.c b/pre-process.c index b51b7034..d5b19220 100644 --- a/pre-process.c +++ b/pre-process.c @@ -802,31 +802,6 @@ static int do_include_path(const char **pptr, struct token **list, struct token return 0; } -static void do_include(int local, struct stream *stream, struct token **list, struct token *token, const char *filename, const char **path) -{ - int flen = strlen(filename) + 1; - - /* Absolute path? */ - if (filename[0] == '/') { - if (try_include("", filename, flen, list, includepath)) - return; - goto out; - } - - /* Dir of input file is first dir to search for quoted includes */ - set_stream_include_path(stream); - - if (!path) - /* Do not search quote include if <> is in use */ - path = local ? quote_includepath : angle_includepath; - - /* Check the standard include paths.. */ - if (do_include_path(path, list, token, filename, flen)) - return; -out: - error_die(token->pos, "unable to open '%s'", filename); -} - static int free_preprocessor_line(struct token *token) { while (token_type(token) != TOKEN_EOF) { @@ -837,11 +812,13 @@ static int free_preprocessor_line(struct token *token) return 1; } -static int handle_include_path(struct stream *stream, struct token **list, struct token *token, const char **path) +static int handle_include_path(struct stream *stream, struct token **list, struct token *token, int how) { const char *filename; struct token *next; + const char **path; int expect; + int flen; next = token->next; expect = '>'; @@ -854,20 +831,52 @@ static int handle_include_path(struct stream *stream, struct token **list, struc expect = '>'; } } + token = next->next; filename = token_name_sequence(token, expect, token); - do_include(!expect, stream, list, token, filename, path); - return 0; + flen = strlen(filename) + 1; + + /* Absolute path? */ + if (filename[0] == '/') { + if (try_include("", filename, flen, list, includepath)) + return 0; + goto out; + } + + switch (how) { + case 1: + path = stream->next_path; + break; + case 2: + includepath[0] = ""; + path = includepath; + break; + default: + /* Dir of input file is first dir to search for quoted includes */ + set_stream_include_path(stream); + path = expect ? angle_includepath : quote_includepath; + break; + } + /* Check the standard include paths.. */ + if (do_include_path(path, list, token, filename, flen)) + return 0; +out: + error_die(token->pos, "unable to open '%s'", filename); } static int handle_include(struct stream *stream, struct token **list, struct token *token) { - return handle_include_path(stream, list, token, NULL); + return handle_include_path(stream, list, token, 0); } static int handle_include_next(struct stream *stream, struct token **list, struct token *token) { - return handle_include_path(stream, list, token, stream->next_path); + return handle_include_path(stream, list, token, 1); +} + +static int handle_argv_include(struct stream *stream, struct token **list, struct token *token) +{ + return handle_include_path(stream, list, token, 2); } static int token_different(struct token *t1, struct token *t2) @@ -1798,6 +1807,7 @@ static void init_preprocessor(void) { "add_system", handle_add_system }, { "add_dirafter", handle_add_dirafter }, { "split_include", handle_split_include }, + { "argv_include", handle_argv_include }, }, special[] = { { "ifdef", handle_ifdef }, { "ifndef", handle_ifndef }, |