Skip to contentSkip to conversation

Bug: "typeof(*p) *" drops const

PATCH Josh Triplett 1 participant 1 message Sep 6, 2013 Sparse checker

lore
Share & tools
lore
#0 lore 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 <[email protected]>
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.

---
 validation/typeof-const.c | 9 +++++++++
 1 file changed, 9 insertions(+)
 create mode 100644 validation/typeof-const.c
validation/typeof-const.c new file +9 -0
new file mode 100644
index 0000000..d02d39a
@@ -0,0 +1,9 @@
+void f(void);
+void f(void)
+{
+    const int *pc;
+    typeof(*pc) *pc2 = pc;
+}
+/*
+ * check-name: typeof-const.c
+ */
Signature · 1 line
1.8.4.rc3