warning about const multidimensional array as function parameter
Martin Uecker
uecker@eecs.berkeley.edu
Tue Oct 14 02:14:00 GMT 2014
Manuel López-Ibáñez <lopezibanez@gmail.com>:
> On 14 October 2014 01:12, Martin Uecker <uecker@eecs.berkeley.edu> wrote:
> > Converting a pointer to an array to a pointer to a constant array
> > is safe. Converting a pointer to a pointer to a pointer to a pointer
> > to a constant is not (as the CFAQ points out).
>
> You are probably right that it is safe. Unfortunately, C considers
> invalid cases that are safe and that are allowed by C++ (as mentioned
> in that C FAQ). I updated the FAQ with comments by Joseph Myers taken
> from https://gcc.gnu.org/bugzilla/show_bug.cgi?id=47143 and
> https://gcc.gnu.org/bugzilla/show_bug.cgi?id=33076.
Thank you. I am not sure about the full C++ semantics, but the
case of passing pointers to const arrays seems fairly straightforward.
I also raised the issue at comp.std.c and proposed a change the
following extension to the pointer conversion rule (6.3.2.3):
"A pointer to an array with non-q-qualified element type may be
converted to a pointer to an array with the q-qualified version
of the type as element type."
Maybe somebody from the committee is reading there...
> I'm not sure how much effort would be to make the C FE of GCC follow
> the rules of C++ or whether this would conflict with other parts of
> the standard (that is, rejecting valid C programs).
I played bit at gcc/c/c-typeck.c and getting rid of the warning
seems simple (see below). But so far I don't fully understand the
code or what I did...
I also haven't figured out how to add a then-required warning about
the case where a pointer-to-constant-array is converted to a
pointer-to-array.
> Perhaps the option could be separated into two, one only enabled by
> -Wpedantic that controls cases that are known to be safe
> (-Wpedantic-incompatible-pointer-types? -Wincompatible-but-safe?).
My preferred solution would be to simple allow the case where it is
safe and warn only when enabled by -Wpedantic.
> Still, someone would need to step up and do the work:
> https://gcc.gnu.org/wiki/GettingStarted#Basics:_Contributing_to_GCC_in_10_easy_steps
I will talk a look at this.
Martin
> Cheers,
>
> Manuel.
Index: gcc/c/c-typeck.c
===================================================================
--- gcc/c/c-typeck.c (Revision 216134)
+++ gcc/c/c-typeck.c (Arbeitskopie)
@@ -1217,6 +1217,7 @@
comp_target_types (location_t location, tree ttl, tree ttr)
{
int val;
+ int val2;
tree mvl = TREE_TYPE (ttl);
tree mvr = TREE_TYPE (ttr);
addr_space_t asl = TYPE_ADDR_SPACE (mvl);
@@ -1228,13 +1229,20 @@
if (!addr_space_superset (asl, asr, &as_common))
return 0;
+ val2 = 1;
+
+ if ((TREE_CODE (mvl) == ARRAY_TYPE) && (TREE_CODE (mvr) == ARRAY_TYPE))
+ val2 = comptypes (mvl, mvr);
+
/* Do not lose qualifiers on element types of array types that are
pointer targets by taking their TYPE_MAIN_VARIANT. */
- if (TREE_CODE (mvl) != ARRAY_TYPE)
+
+// if (TREE_CODE (mvl) != ARRAY_TYPE)
mvl = (TYPE_ATOMIC (mvl)
? c_build_qualified_type (TYPE_MAIN_VARIANT (mvl), TYPE_QUAL_ATOMIC)
: TYPE_MAIN_VARIANT (mvl));
- if (TREE_CODE (mvr) != ARRAY_TYPE)
+
+// if (TREE_CODE (mvr) != ARRAY_TYPE)
mvr = (TYPE_ATOMIC (mvr)
? c_build_qualified_type (TYPE_MAIN_VARIANT (mvr), TYPE_QUAL_ATOMIC)
: TYPE_MAIN_VARIANT (mvr));
@@ -1241,6 +1249,10 @@
enum_and_int_p = false;
val = comptypes_check_enum_int (mvl, mvr, &enum_and_int_p);
+ if ((val == 1) && (val2 != 1))
+ pedwarn (location, OPT_Wpedantic, "pointers to array have incompatible qualifiers");
+
if (val == 2)
pedwarn (location, OPT_Wpedantic, "types are not quite compatible");
More information about the Gcc
mailing list