From: "Darren Jenkins\\" <darrenrjenkins@gmail.com>
To: Sparse mailing list <linux-sparse@vger.kernel.org>
Subject: [PATCH] Sparse- add macro bracket checking
Date: Sun, 08 Apr 2007 18:09:39 +1000 [thread overview]
Message-ID: <1176019781.8951.10.camel@localhost.localdomain> (raw)
G'day people,
I have done some more work on the macro bracket checking and I think it
is now at a pretty workable stage so I thought I would submit it and see
what everyone thinks.
My Idea with this was apart from the obvious cases, to check everything
that would compile with brackets around it. So if you find code that
this will warn about, but which will not compile with brackets around it
please yell out.
I don't know much about this sort of thing, I guess I am doing this to
learn new stuff. So I am very open to suggestions or comments.
Also should I add some pass and fail cases to the validation suite or
something ?
Anyway here it is.
Signed-off-by: Darren Jenkins <darrenrjenkins@gmail.com>
diff --git a/allocate.h b/allocate.h
diff --git a/bitmap.h b/bitmap.h
diff --git a/lib.c b/lib.c
index 79a5c55..27c9fe1 100644
--- a/lib.c
+++ b/lib.c
@@ -204,6 +204,7 @@ int Waddress_space = 1;
int Wenum_mismatch = 1;
int Wdo_while = 1;
int Wuninitialized = 1;
+int Wmacro_argument_brackets = 1;
int dbg_entry = 0;
int dbg_dead = 0;
diff --git a/lib.h b/lib.h
index 472624d..2657ce3 100644
--- a/lib.h
+++ b/lib.h
@@ -98,6 +98,7 @@ extern int Wshadow;
extern int Wcast_truncate;
extern int Wdo_while;
extern int Wuninitialized;
+extern int Wmacro_argument_brackets;
extern int dbg_entry;
extern int dbg_dead;
diff --git a/pre-process.c b/pre-process.c
index afae77a..22fa674 100644
--- a/pre-process.c
+++ b/pre-process.c
@@ -996,6 +996,69 @@ static int try_arg(struct token *token,
return 0;
}
+unsigned int bracketed_badly(struct token * after, struct token * before)
+{
+ struct token * ptr;
+
+
+ if ( token_type(after) != TOKEN_IDENT ) {
+
+ if ( match_op(after, SPECIAL_HASHHASH) || match_op(after, '(') ) {
+ return 0u;
+ }
+
+ if ( before == NULL || match_op(before, ';') || match_op(before, '{') || match_op(before, '}') ) {
+
+ if ( match_op(after, '=') || match_op(after, SPECIAL_EQUAL) ||
+ match_op(after, SPECIAL_GTE) || match_op(after, SPECIAL_LTE) ||
+ match_op(after, SPECIAL_XOR_ASSIGN) || match_op(after, SPECIAL_MOD_ASSIGN) ||
+ match_op(after, SPECIAL_SUB_ASSIGN) || match_op(after, SPECIAL_AND_ASSIGN) ||
+ match_op(after, SPECIAL_MUL_ASSIGN) || match_op(after, SPECIAL_OR_ASSIGN) ) {
+ return 1u;
+ }
+ return 0u;
+ }
+
+ if ( token_type(before) == TOKEN_CONCAT || token_type(before) == TOKEN_IDENT || match_op(before, '#') ) {
+ return 0u;
+ }
+
+ if ( match_op(before, '(') || match_op(before, ',') ) {
+
+ if ( match_op(after, ')') || match_op(after, ',') ) {
+ return 0u;
+ }
+
+ if ( match_op(after, '*') ) {
+
+ if ( match_op(after->next, ')') ) {
+ return 0u;
+ }
+
+ for (ptr = after->next; !eof_token(ptr); ptr = ptr->next) {
+
+ if ( token_type(ptr) == TOKEN_IDENT || match_op(ptr, '(') || match_op(ptr, ',') || match_op(ptr, ';') ||
+ match_op(ptr, '#') || match_op(ptr, SPECIAL_HASHHASH) || match_op(ptr, ')') || match_op(ptr, '=') ) {
+ break;
+ } else if ( token_type(ptr) != TOKEN_IDENT && !match_op(ptr, '*') ) {
+ return 1u;
+ }
+ }
+ return 0u;
+ }
+ return 1u;
+ }
+ else if ( (match_op(before, '*') && match_op(after, '=')) || (match_op(before, '[') && match_op(after, ']')) ) {
+ return 0u;
+ }
+ return 1u;
+ }
+ return 0u;
+}
+
+
+
+
static struct token *parse_expansion(struct token *expansion, struct token *arglist, struct ident *name)
{
struct token *token = expansion;
@@ -1034,7 +1097,12 @@ static struct token *parse_expansion(str
} else if (match_op(token->next, SPECIAL_HASHHASH)) {
try_arg(token, TOKEN_QUOTED_ARGUMENT, arglist);
} else {
- try_arg(token, TOKEN_MACRO_ARGUMENT, arglist);
+ int argument = try_arg(token, TOKEN_MACRO_ARGUMENT, arglist);
+ if ( Wmacro_argument_brackets &&
+ argument == 1 &&
+ bracketed_badly(token->next, last) ) {
+ warning(token->pos, "Macro argument No. %d is not bracketed in expansion", token->argnum + 1);
+ }
}
if (token_type(token) == TOKEN_ERROR)
goto Earg;
diff --git a/ptrlist.h b/ptrlist.h
diff --git a/token.h b/token.h
diff --git a/tokenize.c b/tokenize.c
reply other threads:[~2007-04-08 8:10 UTC|newest]
Thread overview: [no followups] expand[flat|nested] mbox.gz Atom feed
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=1176019781.8951.10.camel@localhost.localdomain \
--to=darrenrjenkins@gmail.com \
--cc=linux-sparse@vger.kernel.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).