* [Patch] warning for preprocessor directive in macro Was: Segfault at evaluate.c:341
@ 2009-03-20 7:18 Christopher Li
0 siblings, 0 replies; only message in thread
From: Christopher Li @ 2009-03-20 7:18 UTC (permalink / raw)
To: Al Viro; +Cc: Hannes Eder, linux-sparse, Nicholas Mc Guire
[-- Attachment #1: Type: text/plain, Size: 505 bytes --]
On Thu, Mar 19, 2009 at 4:11 PM, Christopher Li <sparse@chrisli.org> wrote:
> Thanks Al,
>
> I will apply it later tonight.
>
> If no one beats to me, I will try to add the warning for using preprocessor
> directive inside macro expansion. It should be a better error message
> than the current one.
OK, attached patch generate warning like this:
macro-argument-ifdef.c:10:1: warning: invalid usage of "#ifdef" in
macro argument
The wording of the error message can be improved. Any suggestion?
Chris
[-- Attachment #2: warn-macro-argument-ifdef --]
[-- Type: application/octet-stream, Size: 2189 bytes --]
Warn about using #ifdef in macro arguments
6.10.3p11: "If there are sequences of preprocessing tokens
within the list of arguments that would otherwise act as preprocessing
directives, the behavior is undefined."
Signed-Off-By: Christopher Li <sparse@chrisli.org>
Index: sparse.chrisl/pre-process.c
===================================================================
--- sparse.chrisl.orig/pre-process.c
+++ sparse.chrisl/pre-process.c
@@ -193,6 +193,17 @@ static void expand_list(struct token **l
}
}
+static inline void warn_preprocessor_directive(struct token *token)
+{
+ struct token *cmd = token->next;
+ if (token_type(cmd) != TOKEN_IDENT)
+ return;
+ if (!lookup_symbol(cmd->ident, NS_PREPROCESSOR))
+ return;
+ warning(token->pos, "invalid usage of \"#%s\" in macro argument",
+ show_ident(cmd->ident));
+}
+
static struct token *collect_arg(struct token *prev, int vararg, struct position *pos)
{
struct token **p = &prev->next;
@@ -207,6 +218,8 @@ static struct token *collect_arg(struct
break;
} else if (match_op(next, ',') && !nesting && !vararg) {
break;
+ } else if (match_op(next, '#') && next->pos.newline) {
+ warn_preprocessor_directive(next);
}
next->pos.stream = pos->stream;
next->pos.line = pos->line;
Index: sparse.chrisl/validation/macro-argument-ifdef.c
===================================================================
--- sparse.chrisl.orig/validation/macro-argument-ifdef.c
+++ sparse.chrisl/validation/macro-argument-ifdef.c
@@ -0,0 +1,28 @@
+#define foo(a,b,c) bar(a,b,c)
+
+static void bar(int a, int b, int c)
+{
+}
+
+static void test(void)
+{
+ foo(1,
+#ifdef NOT
+ 2,
+#else
+ 3,
+#endif
+ 4);
+}
+
+/*
+ * check-name: invalid usage of ifdef inside macro arguments
+ * check-error-start
+macro-argument-ifdef.c:10:1: warning: invalid usage of "#ifdef" in macro argument
+macro-argument-ifdef.c:12:1: warning: invalid usage of "#else" in macro argument
+macro-argument-ifdef.c:14:1: warning: invalid usage of "#endif" in macro argument
+macro-argument-ifdef.c:9:9: error: macro "foo" passed 4 arguments, but takes just 3
+macro-argument-ifdef.c:9:9: error: undefined identifier 'foo'
+ * check-error-end
+ */
+
^ permalink raw reply [flat|nested] only message in thread
only message in thread, other threads:[~2009-03-20 7:18 UTC | newest]
Thread overview: (only message) (download: mbox.gz follow: Atom feed
-- links below jump to the message on this page --
2009-03-20 7:18 [Patch] warning for preprocessor directive in macro Was: Segfault at evaluate.c:341 Christopher Li
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).