All of lore.kernel.org
 help / color / mirror / Atom feed
From: Josh Triplett <josh@joshtriplett.org>
To: linux-sparse@vger.kernel.org
Cc: paulmck@linux.vnet.ibm.com
Subject: Bug: "typeof(*p) *" drops const
Date: Fri, 6 Sep 2013 07:53:21 -0700	[thread overview]
Message-ID: <20130906145320.GA26549@leaf> (raw)

The following test case compiles via GCC with no errors or warnings, but
generates a type warning with sparse:

void f(void);
void f(void)
{
    const int *pc;
    typeof(*pc) *pc2 = pc;
}

Sparse says:

typeof-const.c:5:24: warning: incorrect type in initializer (different modifiers)
typeof-const.c:5:24:    expected int *pc2
typeof-const.c:5:24:    got int const *pc

Since pc has type "const int *", "typeof(*pc) *" should also have type
"const int *", not "int *".

The actual use case for which this came up occurred in the kernel when
using the equivalent of:

typeof(*p) __attribute__((address_space(0),force)) *

to generate the same type as p but in address space 0 rather than
whatever address space p had.  That worked fine for non-const pointers,
but const pointers lost their constness, resulting in constness
warnings.

The following patch adds a test case for this; please apply once sparse
handles this case correctly:

---------->8----------
From: Josh Triplett <josh@joshtriplett.org>
Subject: [PATCH] validation: Add new test case for typeof(*ptr) preserving const

If ptr has type "const foo *", then "typeof(*ptr) *" should also have
type "const foo *", not "foo *".  Add a test to ensure that.

Signed-off-by: Josh Triplett <josh@joshtriplett.org>
---
 validation/typeof-const.c | 9 +++++++++
 1 file changed, 9 insertions(+)
 create mode 100644 validation/typeof-const.c

diff --git a/validation/typeof-const.c b/validation/typeof-const.c
new file mode 100644
index 0000000..d02d39a
--- /dev/null
+++ b/validation/typeof-const.c
@@ -0,0 +1,9 @@
+void f(void);
+void f(void)
+{
+    const int *pc;
+    typeof(*pc) *pc2 = pc;
+}
+/*
+ * check-name: typeof-const.c
+ */
-- 
1.8.4.rc3

                 reply	other threads:[~2013-09-06 14:53 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=20130906145320.GA26549@leaf \
    --to=josh@joshtriplett.org \
    --cc=linux-sparse@vger.kernel.org \
    --cc=paulmck@linux.vnet.ibm.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 an external index of several public inboxes,
see mirroring instructions on how to clone and mirror
all data and code used by this external index.