diff mbox

[1/2] Let create_symbol check for previous same symbol

Message ID CANeU7QmVQT9kdKW+FtgpC5sXjyp_zF_GEpwLivLofLJ3ra2egw@mail.gmail.com (mailing list archive)
State Mainlined, archived
Headers show

Commit Message

Christopher Li July 3, 2017, 6:41 p.m. UTC
Avoid create a new one if same symbol exists.

This allow  sparse process the full list of gcc supported
attribute without worry about the duplicate one already
implemented in sparse.

Chris
diff mbox

Patch

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(-)

diff --git a/parse.c b/parse.c
index f665f05..953d04b 100644
--- a/parse.c
+++ b/parse.c
@@ -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;
+		}
 	}
 }
 
diff --git a/symbol.c b/symbol.c
index 08c85f4..301fe13 100644
--- a/symbol.c
+++ b/symbol.c
@@ -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;
 }
 
diff --git a/token.h b/token.h
index f7d88eb..847fdf4 100644
--- a/token.h
+++ b/token.h
@@ -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);
diff --git a/tokenize.c b/tokenize.c
index bc97242..99b9580 100644
--- a/tokenize.c
+++ b/tokenize.c
@@ -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