linux-sparse.vger.kernel.org archive mirror
 help / color / mirror / Atom feed
From: Christopher Li <sparse@chrisli.org>
To: Al Viro <viro@zeniv.linux.org.uk>
Cc: Hannes Eder <hannes@hanneseder.net>,
	linux-sparse@vger.kernel.org,
	Nicholas Mc Guire <der.herr@hofr.at>
Subject: [Patch] warning for preprocessor directive in macro Was: Segfault at evaluate.c:341
Date: Fri, 20 Mar 2009 00:18:11 -0700	[thread overview]
Message-ID: <70318cbf0903200018k65330948g86fe8445e4ad86f6@mail.gmail.com> (raw)

[-- 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
+ */
+

                 reply	other threads:[~2009-03-20  7:18 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=70318cbf0903200018k65330948g86fe8445e4ad86f6@mail.gmail.com \
    --to=sparse@chrisli.org \
    --cc=der.herr@hofr.at \
    --cc=hannes@hanneseder.net \
    --cc=linux-sparse@vger.kernel.org \
    --cc=viro@zeniv.linux.org.uk \
    /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).