aboutsummaryrefslogtreecommitdiffstatshomepage
diff options
authorLinus Torvalds <torvalds@ppc970.osdl.org>2004-10-05 13:13:12 -0700
committerLinus Torvalds <torvalds@ppc970.osdl.org>2005-04-07 21:03:29 -0700
commit593a9f8db63c37420dde3d298e289ffd22f68b4d (patch)
treead940afbc80213bdd8c86a4b6b67eab327bd5426
parentcf2bde63a650ca75a04c555dec9046dac5e6f592 (diff)
downloadsparse-dev-593a9f8db63c37420dde3d298e289ffd22f68b4d.tar.gz
Introduce the notion of "reserved" identifiers.
The only thing they do is to refuse to bind to the regular C namespaces (NS_TYPEDEF | NS_STRUCT | NS_LABEL | NS_SYMBOL) with a warning. You can mark any identifier you want reserved by just setting ident->reserved = 1. Sparse won't care.
-rw-r--r--ident-list.h62
-rw-r--r--symbol.c13
-rw-r--r--symbol.h2
-rw-r--r--token.h3
4 files changed, 56 insertions, 24 deletions
diff --git a/ident-list.h b/ident-list.h
index a57e0f16..e7eed0c2 100644
--- a/ident-list.h
+++ b/ident-list.h
@@ -1,17 +1,41 @@
-#ifndef IDENT
-#define IDENT(n) __IDENT(n## _ident, #n)
-#endif
+#define IDENT(n) __IDENT(n## _ident, #n, 0)
+#define IDENT_RESERVED(n) __IDENT(n## _ident, #n, 1)
-IDENT(struct); IDENT(union); IDENT(enum);
-IDENT(sizeof); IDENT(alignof); IDENT(__alignof);
-IDENT(__alignof__); IDENT(if); IDENT(else);
-IDENT(return); IDENT(switch); IDENT(case);
-IDENT(default); IDENT(break); IDENT(continue);
-IDENT(for); IDENT(while); IDENT(do);
-IDENT(goto); IDENT(__asm__); IDENT(__asm);
-IDENT(asm); IDENT(__volatile__); IDENT(__volatile);
-IDENT(volatile); IDENT(__attribute__); IDENT(__attribute);
+/* Basic C reserved words.. */
+IDENT_RESERVED(sizeof);
+IDENT_RESERVED(if);
+IDENT_RESERVED(else);
+IDENT_RESERVED(return);
+IDENT_RESERVED(switch);
+IDENT_RESERVED(case);
+IDENT_RESERVED(default);
+IDENT_RESERVED(break);
+IDENT_RESERVED(continue);
+IDENT_RESERVED(for);
+IDENT_RESERVED(while);
+IDENT_RESERVED(do);
+IDENT_RESERVED(goto);
+
+/* C typenames. They get marked as reserved when initialized */
+IDENT(struct);
+IDENT(union);
+IDENT(enum);
+IDENT(__attribute__);
+IDENT(__attribute);
+IDENT(volatile);
+
+/* Extended gcc identifiers */
+IDENT(asm);
+IDENT(__asm__);
+IDENT(__asm);
+IDENT(alignof);
+IDENT(__alignof);
+IDENT(__alignof__);
+IDENT(__volatile__);
+IDENT(__volatile);
+
+/* Attribute names */
IDENT(defined); IDENT(packed); IDENT(__packed__);
IDENT(aligned); IDENT(__aligned__); IDENT(nocast);
IDENT(noderef); IDENT(safe); IDENT(force);
@@ -29,11 +53,13 @@ IDENT(syscall_linkage); IDENT(visibility);
IDENT(bitwise);
IDENT(model); IDENT(__model__);
-__IDENT(pragma_ident, "__pragma__");
-__IDENT(__VA_ARGS___ident, "__VA_ARGS__");
-__IDENT(__LINE___ident, "__LINE__");
-__IDENT(__FILE___ident, "__FILE__");
-__IDENT(__func___ident, "__func__");
+/* Preprocessor idents */
+__IDENT(pragma_ident, "__pragma__", 0);
+__IDENT(__VA_ARGS___ident, "__VA_ARGS__", 0);
+__IDENT(__LINE___ident, "__LINE__", 0);
+__IDENT(__FILE___ident, "__FILE__", 0);
+__IDENT(__func___ident, "__func__", 0);
#undef __IDENT
-
+#undef IDENT
+#undef IDENT_RESERVED
diff --git a/symbol.c b/symbol.c
index 102685a5..bdcbb583 100644
--- a/symbol.c
+++ b/symbol.c
@@ -357,6 +357,10 @@ void bind_symbol(struct symbol *sym, struct ident *ident, enum namespace ns)
warning(sym->pos, "internal error: symbol type already bound");
return;
}
+ if (ident->reserved && (ns & (NS_TYPEDEF | NS_STRUCT | NS_LABEL | NS_SYMBOL))) {
+ warning(sym->pos, "Trying to use reserved word '%s' as identifier", show_ident(ident));
+ return;
+ }
sym->namespace = ns;
sym->next_id = ident->symbols;
ident->symbols = sym;
@@ -572,9 +576,9 @@ struct symbol bool_ctype, void_ctype, type_ctype,
incomplete_ctype, label_ctype, bad_enum_ctype;
-#define __INIT_IDENT(str) { .len = sizeof(str)-1, .name = str }
-#define __IDENT(n,str) \
- struct ident n = __INIT_IDENT(str)
+#define __INIT_IDENT(str, res) { .len = sizeof(str)-1, .name = str, .reserved = res }
+#define __IDENT(n,str,res) \
+ struct ident n = __INIT_IDENT(str,res)
#include "ident-list.h"
@@ -583,13 +587,14 @@ void init_symbols(void)
int stream = init_stream("builtin", -1, includepath);
struct sym_init *ptr;
-#define __IDENT(n,str) \
+#define __IDENT(n,str,res) \
hash_ident(&n)
#include "ident-list.h"
for (ptr = symbol_init_table; ptr->name; ptr++) {
struct symbol *sym;
sym = create_symbol(stream, ptr->name, SYM_NODE, NS_TYPEDEF);
+ sym->ident->reserved = 1;
sym->ctype.base_type = ptr->base_type;
sym->ctype.modifiers = ptr->modifiers;
}
diff --git a/symbol.h b/symbol.h
index ba697b05..05f9d207 100644
--- a/symbol.h
+++ b/symbol.h
@@ -175,7 +175,7 @@ extern struct symbol bool_ctype, void_ctype, type_ctype,
incomplete_ctype, label_ctype, bad_enum_ctype;
-#define __IDENT(n,str) \
+#define __IDENT(n,str,res) \
extern struct ident n
#include "ident-list.h"
diff --git a/token.h b/token.h
index d4defd7e..af961211 100644
--- a/token.h
+++ b/token.h
@@ -54,7 +54,8 @@ struct ident {
struct ident *next; /* Hash chain of identifiers */
struct symbol *symbols; /* Pointer to semantic meaning list */
unsigned char len; /* Length of identifier name */
- unsigned char tainted;
+ unsigned char tainted:1,
+ reserved:1;
char name[]; /* Actual identifier */
};