[PATCH 3/4] more transparent dealing with tokens/position

[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index]

 



Treat `enum token_type' as enumeration type rather than int.
For instance, avoid (a bit magical) zero assignments.

I admit this is quite a wild change, especially wrt the move of
`struct position'.  This was probably the easiest way to compile
sparse without additional dependecies mangling.  Still, another
solution can be found.

Note that after applying patch 2/4 (i.e., if `eof_token' and
`eof_token_entry' are used consistently), TOKEN_EOF should no longer
be necessary (this patch also modifies `show_token' to accommodate this).
But it might be a good idea to keep it for backward compatibility (it's
usage/meaning stays untouched with this patch series), only add
a comment that it should not be used (for maintainability reasons).

Another solution for TOKEN_EOF then making it "deprecated" would be to
make `eof_token_entry' initialized with ".pos.type = TOKEN_EOF" to make
this correspondence explicit (so far, this has silently applied because
of implicit zero initialization of `eof_token_entry' global).
This would kept duality of how to test the single case, though,
and I consider the only and consistent way better.

Also, add `POSITION_NOT_FROM_TOKEN' to the `enum token_type' to avoid
magical zero assignments and add some explaining comments.
It is the best name I was able to come up with, so that it still
describes the meaning quite clearly.  Another suggestions welcome.

Signed-off-by: Jan Pokorny <pokorny_jan@xxxxxxxxx>
---
 c2xml.c      |    2 +-
 expression.c |    2 +-
 lib.h        |   10 +---------
 symbol.c     |    2 +-
 token.h      |   19 ++++++++++++++++++-
 tokenize.c   |    8 ++++----
 6 files changed, 26 insertions(+), 17 deletions(-)

diff --git a/c2xml.c b/c2xml.c
index 37f29cf..2f76865 100644
--- a/c2xml.c
+++ b/c2xml.c
@@ -68,7 +68,7 @@ static xmlNodePtr new_sym_node(struct symbol *sym, const char *name, xmlNodePtr
 	newNumProp(node, "start-line", sym->pos.line);
 	newNumProp(node, "start-col", sym->pos.pos);
 
-	if (sym->endpos.type) {
+	if (sym->endpos.type != POSITION_NOT_FROM_TOKEN) {
 		newNumProp(node, "end-line", sym->endpos.line);
 		newNumProp(node, "end-col", sym->endpos.pos);
 		if (sym->pos.stream != sym->endpos.stream)
diff --git a/expression.c b/expression.c
index 7e06e60..43a3606 100644
--- a/expression.c
+++ b/expression.c
@@ -224,7 +224,7 @@ static struct token *string_expression(struct token *token, struct expression *e
 {
 	struct string *string = token->string;
 	struct token *next = token->next;
-	int stringtype = token_type(token);
+	enum token_type stringtype = token_type(token);
 
 	convert_function(token);
 
diff --git a/lib.h b/lib.h
index 2cea252..6b17280 100644
--- a/lib.h
+++ b/lib.h
@@ -31,15 +31,7 @@ extern int gcc_major, gcc_minor, gcc_patchlevel;
 
 extern unsigned int hexval(unsigned int c);
 
-struct position {
-	unsigned int type:6,
-		     stream:14,
-		     newline:1,
-		     whitespace:1,
-		     pos:10;
-	unsigned int line:31,
-		     noexpand:1;
-};
+struct position;
 
 struct cmdline_include {
 	char *filename;
diff --git a/symbol.c b/symbol.c
index 96dfbfa..34d4f06 100644
--- a/symbol.c
+++ b/symbol.c
@@ -62,7 +62,7 @@ struct symbol *alloc_symbol(struct position pos, int type)
 	struct symbol *sym = __alloc_symbol(0);
 	sym->type = type;
 	sym->pos = pos;
-	sym->endpos.type = 0;
+	sym->endpos.type = POSITION_NOT_FROM_TOKEN;
 	return sym;
 }
 
diff --git a/token.h b/token.h
index a3c194d..057b40a 100644
--- a/token.h
+++ b/token.h
@@ -60,8 +60,15 @@ struct ident {
 	char name[];		/* Actual identifier */
 };
 
+/*
+ * Tightly connected with "struct token", but used also outside them as a part
+ * of symbol etc. position, which is in turn borrowed from underlying tokens.
+ */
 enum token_type {
-	TOKEN_EOF,
+	/* Used outside tokens to mark a position not initialized from a token */
+	POSITION_NOT_FROM_TOKEN,
+	/* Token type (outside them, ~particular position was initialized from) */
+	TOKEN_EOF = POSITION_NOT_FROM_TOKEN,	/* use "eof_token()" instead */
 	TOKEN_ERROR,
 	TOKEN_IDENT,
 	TOKEN_ZERO_IDENT,
@@ -85,6 +92,16 @@ enum token_type {
 	TOKEN_ELSE,
 };
 
+struct position {
+	enum token_type	type:6;
+	unsigned int stream:14,
+			newline:1,
+			whitespace:1,
+			pos:10;
+	unsigned int line:31,
+			noexpand:1;
+};
+
 /* Combination tokens */
 #define COMBINATION_STRINGS {	\
 	"+=", "++",		\
diff --git a/tokenize.c b/tokenize.c
index 272974b..6fa8714 100644
--- a/tokenize.c
+++ b/tokenize.c
@@ -48,7 +48,7 @@ const char *stream_name(int stream)
 static struct position stream_pos(stream_t *stream)
 {
 	struct position pos;
-	pos.type = 0;
+	pos.type = POSITION_NOT_FROM_TOKEN;
 	pos.stream = stream->nr;
 	pos.newline = stream->newline;
 	pos.whitespace = stream->whitespace;
@@ -126,13 +126,13 @@ const char *show_token(const struct token *token)
 
 	if (!token)
 		return "<no token>";
+	if (eof_token(token))
+		return "end-of-input";
+
 	switch (token_type(token)) {
 	case TOKEN_ERROR:
 		return "syntax error";
 
-	case TOKEN_EOF:
-		return "end-of-input";
-
 	case TOKEN_IDENT:
 		return show_ident(token->ident);
 
-- 
1.7.1
--
To unsubscribe from this list: send the line "unsubscribe linux-sparse" in
the body of a message to majordomo@xxxxxxxxxxxxxxx
More majordomo info at  http://vger.kernel.org/majordomo-info.html


[Index of Archives]     [Newbies FAQ]     [LKML]     [IETF Annouce]     [DCCP]     [Netdev]     [Networking]     [Security]     [Bugtraq]     [Yosemite]     [MIPS Linux]     [ARM Linux]     [Linux Security]     [Linux RAID]     [Linux SCSI]     [Trinity Fuzzer Tool]

  Powered by Linux