From 36c23fe2d00f36a2afa0a2d4690c098a5de11d18 Mon Sep 17 00:00:00 2001
From: Christopher Li <sparse@chrisli.org>
Date: Mon, 3 Jul 2017 03:34:11 -0700
Subject: [PATCH 1/2] Let create_symbol check for previous same symbol
Avoid create a new one if same symbol exists.
Signed-off-By: <sparse@chrisli.org>
---
parse.c | 6 ++++--
symbol.c | 15 ++++++++++++---
token.h | 2 +-
tokenize.c | 4 ++--
4 files changed, 19 insertions(+), 8 deletions(-)
@@ -671,8 +671,10 @@ void init_parser(int stream)
const char * name = ignored_attributes[i];
struct symbol *sym = create_symbol(stream, name, SYM_KEYWORD,
NS_KEYWORD);
- sym->ident->keyword = 1;
- sym->op = &ignore_attr_op;
+ if (!sym->op) {
+ sym->ident->keyword = 1;
+ sym->op = &ignore_attr_op;
+ }
}
}
@@ -639,10 +639,19 @@ void bind_symbol(struct symbol *sym, struct ident *ident, enum namespace ns)
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);
+ struct ident *ident = built_in_ident(name);
+ struct symbol *sym = lookup_symbol(ident, namespace);
- bind_symbol(sym, token->ident, namespace);
+ if (sym && sym->type != type)
+ die("symbol %s create with different type: %d old %d", name,
+ type, sym->type);
+
+ if (!sym) {
+ struct token *token = built_in_token(stream, ident);
+
+ sym = alloc_symbol(token->pos, type);
+ bind_symbol(sym, token->ident, namespace);
+ }
return sym;
}
@@ -218,7 +218,7 @@ extern int init_stream(const char *, int fd, const char **next_path);
extern const char *stream_name(int stream);
extern struct ident *hash_ident(struct ident *);
extern struct ident *built_in_ident(const char *);
-extern struct token *built_in_token(int, const char *);
+extern struct token *built_in_token(int, struct ident *);
extern const char *show_special(int);
extern const char *show_ident(const struct ident *);
extern const char *show_string(const struct string *string);
@@ -904,14 +904,14 @@ struct ident *built_in_ident(const char *name)
return create_hashed_ident(name, len, hash_name(name, len));
}
-struct token *built_in_token(int stream, const char *name)
+struct token *built_in_token(int stream, struct ident *ident)
{
struct token *token;
token = __alloc_token(0);
token->pos.stream = stream;
token_type(token) = TOKEN_IDENT;
- token->ident = built_in_ident(name);
+ token->ident = ident;
return token;
}
--
2.9.4