aboutsummaryrefslogtreecommitdiffstatshomepage
diff options
authorChristopher Li <sparse@chrisli.org>2004-09-06 11:49:47 -0700
committerLinus Torvalds <torvalds@ppc970.osdl.org>2005-04-07 21:03:09 -0700
commitca36c90780e91cef4e4d976227f5037064a62aed (patch)
treefaaa62059391d1035844df843c5d577b44cf0ed1
parente54f26d605b88efdda9b232b1d9480158a2cb536 (diff)
downloadsparse-dev-ca36c90780e91cef4e4d976227f5037064a62aed.tar.gz
[PATCH] make preprocesser command a symbol
This all started with I want to remove match_string_ident in preprocessor. I decide that I don't want to search a pointer in the list. There is faster way which is register a symbol with a new name space which contain the handler. This changes: 1) changed name of NS_PRECESSOR to NS_MACRO and give it a new meaning for preprocessor command. Now NS_MACRO means a macro name, and the new NS_PREPROCESSOR means a preprocessor command. 2) The ctype and preprocessor can put into one union. They are on different name space so they _should_ never collapse. I did that and made struct symbol 8 bytes smaller. Nothing blew up for me so it must be correct ;) And a question: 3) When to initialize the preprocessor command symbol. Should we make it part of the init_symbol or let the caller(check.c) initialize it? Do we want to remove the preprocessor command afterwords?
-rw-r--r--pre-process.c29
-rw-r--r--symbol.c5
-rw-r--r--symbol.h57
-rw-r--r--tokenize.c2
4 files changed, 54 insertions, 39 deletions
diff --git a/pre-process.c b/pre-process.c
index d49accdd..b29b32df 100644
--- a/pre-process.c
+++ b/pre-process.c
@@ -109,7 +109,7 @@ static void replace_with_defined(struct token *token)
int defined = 0;
if (token_type(token) != TOKEN_IDENT)
warn(token->pos, "operator \"defined\" requires an identifier");
- else if (lookup_symbol(token->ident, NS_PREPROCESSOR))
+ else if (lookup_symbol(token->ident, NS_MACRO))
defined = 1;
token_type(token) = TOKEN_NUMBER;
token->number = string[defined];
@@ -123,7 +123,7 @@ struct token **expand_one_symbol(struct token **list)
if (token->pos.noexpand)
return &token->next;
- sym = lookup_symbol(token->ident, NS_PREPROCESSOR);
+ sym = lookup_symbol(token->ident, NS_MACRO);
if (sym)
return expand(list, sym);
if (token->ident == &__LINE___ident) {
@@ -1003,7 +1003,7 @@ static int handle_define(struct stream *stream, struct token **line, struct toke
if (!expansion)
return 1;
- sym = lookup_symbol(name, NS_PREPROCESSOR);
+ sym = lookup_symbol(name, NS_MACRO);
if (sym) {
if (token_list_different(sym->expansion, expansion) ||
token_list_different(sym->arglist, arglist)) {
@@ -1014,7 +1014,7 @@ static int handle_define(struct stream *stream, struct token **line, struct toke
return 1;
}
sym = alloc_symbol(left->pos, SYM_NODE);
- bind_symbol(sym, name, NS_PREPROCESSOR);
+ bind_symbol(sym, name, NS_MACRO);
sym->expansion = expansion;
sym->arglist = arglist;
@@ -1039,7 +1039,7 @@ static int handle_undef(struct stream *stream, struct token **line, struct token
sym = &left->ident->symbols;
while (*sym) {
struct symbol *t = *sym;
- if (t->namespace == NS_PREPROCESSOR) {
+ if (t->namespace == NS_MACRO) {
*sym = t->next_id;
return 1;
}
@@ -1066,7 +1066,7 @@ static int preprocessor_if(struct token *token, int true)
static int token_defined(struct token *token)
{
if (token_type(token) == TOKEN_IDENT)
- return lookup_symbol(token->ident, NS_PREPROCESSOR) != NULL;
+ return lookup_symbol(token->ident, NS_MACRO) != NULL;
warn(token->pos, "expected identifier for #if[n]def");
return 0;
@@ -1385,9 +1385,11 @@ static int handle_line(struct stream *stream, struct token **line, struct token
return 1;
}
-static int handle_preprocessor_command(struct stream *stream, struct token **line, struct ident *ident, struct token *token)
+
+void init_preprocessor()
{
int i;
+ int stream = init_stream("preprocessor", -1, includepath);
static struct {
const char *name;
int (*handler)(struct stream *, struct token **, struct token *);
@@ -1413,10 +1415,10 @@ static int handle_preprocessor_command(struct stream *stream, struct token **lin
};
for (i = 0; i < (sizeof (handlers) / sizeof (handlers[0])); i++) {
- if (match_string_ident(ident, handlers[i].name))
- return handlers[i].handler(stream, line, token);
+ struct symbol *sym;
+ sym = create_symbol(stream, handlers[i].name, SYM_PREPROCESSOR, NS_PREPROCESSOR);
+ sym->handler = handlers[i].handler;
}
- return 0;
}
static void handle_preprocessor_line(struct stream *stream, struct token **line, struct token *start)
@@ -1430,9 +1432,11 @@ static void handle_preprocessor_line(struct stream *stream, struct token **line,
if (handle_line(stream, line, start))
return;
- if (token_type(token) == TOKEN_IDENT)
- if (handle_preprocessor_command(stream, line, token->ident, token))
+ if (token_type(token) == TOKEN_IDENT) {
+ struct symbol *sym = lookup_symbol(token->ident, NS_PREPROCESSOR);
+ if (sym && sym->handler(stream, line, token))
return;
+ }
warn(token->pos, "unrecognized preprocessor line '%s'", show_token_sequence(token));
}
@@ -1513,6 +1517,7 @@ static void do_preprocess(struct token **list)
struct token * preprocess(struct token *token)
{
preprocessing = 1;
+ init_preprocessor();
do_preprocess(&token);
// Drop all expressions from pre-processing, they're not used any more.
diff --git a/symbol.c b/symbol.c
index a8067b38..d4d0ccb9 100644
--- a/symbol.c
+++ b/symbol.c
@@ -265,6 +265,9 @@ struct symbol *examine_symbol_type(struct symbol * sym)
sym->ctype.base_type = base;
}
break;
+ case SYM_PREPROCESSOR:
+ warn(sym->pos, "ctype on preprocessor command?\n");
+ return NULL;
}
default:
break;
@@ -345,7 +348,7 @@ void bind_symbol(struct symbol *sym, struct ident *ident, enum namespace ns)
bind_scope(sym, scope);
}
-static struct symbol *create_symbol(int stream, const char *name, int type, int namespace)
+struct symbol *create_symbol(int stream, const char *name, int type, int namespace)
{
struct token *token = built_in_token(stream, name);
struct symbol *sym = alloc_symbol(token->pos, type);
diff --git a/symbol.h b/symbol.h
index e2ae18e0..3df79583 100644
--- a/symbol.h
+++ b/symbol.h
@@ -25,15 +25,17 @@
*/
enum namespace {
NS_NONE = 0,
- NS_PREPROCESSOR = 1,
+ NS_MACRO = 1,
NS_TYPEDEF = 2,
NS_STRUCT = 4, // Also used for unions and enums.
NS_LABEL = 8,
NS_SYMBOL = 16,
NS_ITERATOR = 32,
+ NS_PREPROCESSOR = 64,
};
enum type {
+ SYM_PREPROCESSOR,
SYM_BASETYPE,
SYM_NODE,
SYM_PTR,
@@ -74,30 +76,34 @@ struct symbol {
struct symbol *same_symbol;
struct symbol_op *op;
- struct /* preprocessor_sym */ {
- struct token *expansion;
- struct token *arglist;
- };
-
- struct /* ctype_sym */ {
- unsigned long offset;
- unsigned int bit_size;
- unsigned int bit_offset:8,
- fieldwidth:8,
- arg_count:10,
- variadic:1,
- used:1,
- initialized:1,
- expanding:1;
- struct expression *array_size;
- struct ctype ctype;
- struct symbol_list *arguments;
- struct statement *stmt;
- struct symbol_list *symbol_list;
- struct statement *inline_stmt;
- struct symbol_list *inline_symbol_list;
- struct expression *initializer;
- long long value; /* Initial value */
+ union {
+ struct /* NS_MACRO */ {
+ struct token *expansion;
+ struct token *arglist;
+ };
+ struct /* NS_PREPROCESSOR */ {
+ int (*handler)(struct stream *, struct token **, struct token *);
+ };
+ struct /* NS_SYMBOL */ {
+ unsigned long offset;
+ unsigned int bit_size;
+ unsigned int bit_offset:8,
+ fieldwidth:8,
+ arg_count:10,
+ variadic:1,
+ used:1,
+ initialized:1,
+ expanding:1;
+ struct expression *array_size;
+ struct ctype ctype;
+ struct symbol_list *arguments;
+ struct statement *stmt;
+ struct symbol_list *symbol_list;
+ struct statement *inline_stmt;
+ struct symbol_list *inline_symbol_list;
+ struct expression *initializer;
+ long long value; /* Initial value */
+ };
};
union /* backend */ {
struct basic_block *bb_target; /* label */
@@ -182,6 +188,7 @@ extern const char * type_difference(struct symbol *target, struct symbol *source
unsigned long target_mod_ignore, unsigned long source_mod_ignore);
extern struct symbol *lookup_symbol(struct ident *, enum namespace);
+extern struct symbol *create_symbol(int stream, const char *name, int type, int namespace);
extern void init_symbols(void);
extern void init_ctype(void);
extern struct symbol *alloc_symbol(struct position, int type);
diff --git a/tokenize.c b/tokenize.c
index e418a026..02f07dd3 100644
--- a/tokenize.c
+++ b/tokenize.c
@@ -172,7 +172,7 @@ int init_stream(const char *name, int fd, const char **next_path)
struct stream *s = input_streams + i;
if (s->dev == st.st_dev && s->ino == st.st_ino &&
s->constant == CONSTANT_FILE_YES &&
- lookup_symbol(s->protect, NS_PREPROCESSOR))
+ lookup_symbol(s->protect, NS_MACRO))
return -1;
}