From: "Jan Pokorný" <pokorny_jan@seznam.cz>
To: sparse@chrisli.org
Cc: linux-sparse@vger.kernel.org
Subject: [PATCH 3/4] more transparent dealing with tokens/position
Date: Tue, 12 Apr 2011 13:22:00 +0200 [thread overview]
Message-ID: <4DA435D8.5000900@seznam.cz> (raw)
In-Reply-To: <4DA4352D.6040602@seznam.cz>
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@seznam.cz>
---
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";
next prev parent reply other threads:[~2011-04-12 11:22 UTC|newest]
Thread overview: 5+ messages / expand[flat|nested] mbox.gz Atom feed top
2011-04-12 11:01 [PATCH 1/4] more transparent dealing with tokens/position Jan Pokorný
2011-04-12 11:19 ` [PATCH 2/4] " Jan Pokorný
2011-04-12 11:22 ` Jan Pokorný [this message]
2011-04-12 11:23 ` [PATCH 4/4] " Jan Pokorný
2011-04-12 11:25 ` [PATCH 2/4] " Jan Pokorný
Reply instructions:
You may reply publicly to this message via plain-text email
using any one of the following methods:
* Save the following mbox file, import it into your mail client,
and reply-to-all from there: mbox
Avoid top-posting and favor interleaved quoting:
https://en.wikipedia.org/wiki/Posting_style#Interleaved_style
* Reply using the --to, --cc, and --in-reply-to
switches of git-send-email(1):
git send-email \
--in-reply-to=4DA435D8.5000900@seznam.cz \
--to=pokorny_jan@seznam.cz \
--cc=linux-sparse@vger.kernel.org \
--cc=sparse@chrisli.org \
/path/to/YOUR_REPLY
https://kernel.org/pub/software/scm/git/docs/git-send-email.html
* If your mail client supports setting the In-Reply-To header
via mailto: links, try the mailto: link
Be sure your reply has a Subject: header at the top and a blank line
before the message body.
This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox;
as well as URLs for NNTP newsgroup(s).