This is the mail archive of the
mailing list for the GCC project.
Re: warning about const multidimensional array as function parameter
- From: Martin Uecker <uecker at eecs dot berkeley dot edu>
- To: Manuel LÃpez-IbÃÃez <lopezibanez at gmail dot com>
- Cc: Jonathan Wakely <jwakely dot gcc at gmail dot com>, gcc Mailing List <gcc at gcc dot gnu dot org>, "Joseph S. Myers" <joseph at codesourcery dot com>
- Date: Mon, 13 Oct 2014 19:14:40 -0700
- Subject: Re: warning about const multidimensional array as function parameter
- Authentication-results: sourceware.org; auth=none
- References: <CAESRpQC0=QFaDNrs2YmzcRHfDw_iiKKwJCLQT=p0Un_ph_=+=A at mail dot gmail dot com> <20141013160138 dot 3a3b0c06 at lemur> <CAH6eHdSF5tVhfsFq8OV_ysYfovh17hJSVLQeLvEWsnAn8N+xgQ at mail dot gmail dot com> <20141013161226 dot 623681c2 at lemur> <CAESRpQACK2U1A_i8xBaxrxACDkS9zsV2rcF86upLx2PFyDsPLQ at mail dot gmail dot com>
Manuel LÃpez-IbÃÃez <email@example.com>:
> On 14 October 2014 01:12, Martin Uecker <firstname.lastname@example.org> 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
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 (188.8.131.52):
"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
> 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:
I will talk a look at this.
--- 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 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))
+ 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");