This is the mail archive of the java-patches@gcc.gnu.org mailing list for the Java project.


Index Nav: [Date Index] [Subject Index] [Author Index] [Thread Index]
Message Nav: [Date Prev] [Date Next] [Thread Prev] [Thread Next]
Other format: [Raw text]

Re: [PATCH]: Change CONST_CAST macro to accept a TYPE argument


On Sun, 2 Sep 2007, Richard Kenner wrote:

> > > +  memcpy (CONST_CAST (char *, TREE_STRING_POINTER (s)), str, len);
> > > +  ((char *) CONST_CAST (char *, TREE_STRING_POINTER (s)))[len] = '\0';
> >
> > I'm sure the cast to (char *) in the last line is not needed.
>
> I'm sure I'm missing something here, but why do we need all that complexity
> on *either* of those lines?  It's *so much* harder to see at a glance
> what's going on.
>
> If I take a quick look at that line, CONST_CAST seems to be almost as
> important as TREE_STRING_POINTER to the eye, but of course it isn't.
>
> If we can no longer write:
>
> 	TREE_STRING_POINTER (s)[len] = '\0';
>
> I, for one, would *really* like to understand why.

The return type for the TREE_STRING_POINTER() macro is a "const char *",
and has been for a very long time.  Its not related to my recent
constification efforts.

So you couldn't issue the above statement because TREE_STRING_POINTER's
are not generally writable.  Here, we know that it is safe because we just
allocated the space for it a few lines above.

In order to write to it, you must cast away the const-ness, but doing so
prompts warnings from -Wcast-qual, hence the need for the CONST_CAST().

		--Kaveh
--
Kaveh R. Ghazi			ghazi@caip.rutgers.edu


Index Nav: [Date Index] [Subject Index] [Author Index] [Thread Index]
Message Nav: [Date Prev] [Date Next] [Thread Prev] [Thread Next]