This is the mail archive of the gcc-patches@gcc.gnu.org mailing list for the GCC 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]: Fix problematic -Wcast-qual cases using new CONST_CAST macro


On Fri, 27 Jul 2007, Richard Guenther wrote:

> On 7/27/07, Kaveh R. GHAZI <ghazi@caip.rutgers.edu> wrote:
> > Sure.  Besides my main point which is silencing -Wcast-qual warnings, the
> > CONST_CAST macro is grep-able.  I.e. you can search for the few places
> > where it's used and audit if they are correctly applied.  You cannot do
> > that with regular C-style casts.  Another reason is that with C-style
> > casts there isn't any indication of why it's there.  The CONST_CAST macro
> > comments itself to someone reading the code.  (These are some of the
> > rationales for the creation of C++'s cast operators.)  The macro isn't
> > type-safe like the C++ operator is, but it's a poor man's C option. :-)
>
> Ha!  Maybe a reason to switch to C++ ;)  How about
>
> #ifdef __cplusplus
> #define CONST_CAST(x)  .. template magic follows ..
> #else
> ...
> #endif
>
> ? ;)
> Richard.

I don't think that will work in a generic way.  For static_cast, you need
to supply the stripped type like so:

static_cast<STRIPPED-TYPE>(EXPR)

Since we don't yet have a variant of __typeof to get the qualifer stripped
type for us, we'd need to pass in the stripped type to the macro like so:

#ifdef __cplusplus
#define CONST_CAST(X,TYPE)  static_cast<TYPE>(X)

Note this doesn't increase type safety for most of the cases we'll use it
in because we're just passing the pointer into a situation that takes a
void* like free(foo).  I'm not sure this change is worth it so I'd rather
leave this refinement to the folks who care about compiling gcc with a C++
compiler if they care to take this step.

Also, I don't believe the main sources are compilable with a C++ compiler
at this point anyway.  Only the libiberty/ and gcc/cp/ directories use
-Wc++-compat AFAICT.

So may I install the latest version of my patch?

		Thanks,
		--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]