From: Ard Biesheuvel <ard.biesheuvel@linaro.org>
To: sparse@chrisli.org, linux-sparse@vger.kernel.org
Cc: will.deacon@arm.com, Ard Biesheuvel <ard.biesheuvel@linaro.org>
Subject: [PATCH v3] sparse: treat function pointers as pointers to const data
Date: Mon, 8 Sep 2014 08:30:03 +0200 [thread overview]
Message-ID: <1410157803-3658-1-git-send-email-ard.biesheuvel@linaro.org> (raw)
This code snippet:
static void bar(void const *arg)
{
int (*foo)(void) = arg;
}
produces the following warning:
test.c:4:28: warning: incorrect type in initializer (different modifiers)
test.c:4:28: expected int ( *foo )( ... )
test.c:4:28: got void const *arg
which is caused by the fact that the function pointer 'foo' is not annotated
as being a pointer to const data. However, dereferencing a function pointer
does not produce an lvalue, so a function pointer points to const data by
definition, and we should treat it accordingly.
To avoid producing a warning on the inverse case, i.e.,
static void bar(void)
{
void *foo = bar;
}
we only address the case where the function pointer is the target of
an assignment.
Reviewed-by: Josh Triplett <josh@joshtriplett.org>
Signed-off-by: Ard Biesheuvel <ard.biesheuvel@linaro.org>
---
v3: add rationale for restriction to assignments to commit message
add R-b
v2: only treat function pointers as pointers to const data when they are the
target of an assignment
evaluate.c | 9 +++++++++
1 file changed, 9 insertions(+)
diff --git a/evaluate.c b/evaluate.c
index 66556150ddac..a5a830978bda 100644
--- a/evaluate.c
+++ b/evaluate.c
@@ -1359,6 +1359,15 @@ static int compatible_assignment_types(struct expression *expr, struct symbol *t
typediff = "different address spaces";
goto Err;
}
+ /*
+ * If this is a function pointer assignment, it is
+ * actually fine to assign a pointer to const data to
+ * it, as a function pointer points to const data
+ * implicitly, i.e., dereferencing it does not produce
+ * an lvalue.
+ */
+ if (b1->type == SYM_FN)
+ mod1 |= MOD_CONST;
if (mod2 & ~mod1) {
typediff = "different modifiers";
goto Err;
--
1.8.3.2
next reply other threads:[~2014-09-08 6:30 UTC|newest]
Thread overview: 9+ messages / expand[flat|nested] mbox.gz Atom feed top
2014-09-08 6:30 Ard Biesheuvel [this message]
2014-10-06 14:44 ` [PATCH v3] sparse: treat function pointers as pointers to const data Ard Biesheuvel
2014-10-08 18:00 ` Christopher Li
2014-10-08 18:06 ` Ard Biesheuvel
2014-10-14 12:55 ` Ard Biesheuvel
2014-10-15 1:34 ` Christopher Li
2014-10-15 5:16 ` Ard Biesheuvel
2014-10-15 7:06 ` Christopher Li
2014-10-15 7:10 ` Ard Biesheuvel
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=1410157803-3658-1-git-send-email-ard.biesheuvel@linaro.org \
--to=ard.biesheuvel@linaro.org \
--cc=linux-sparse@vger.kernel.org \
--cc=sparse@chrisli.org \
--cc=will.deacon@arm.com \
/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).