This is the mail archive of the
gcc-patches@gcc.gnu.org
mailing list for the GCC project.
Re: G++ PATCH: -Wcast-align giving false positives
Jason Merrill wrote:
> OK. My main point is that writing
>
> (TPL<Z> *) ptr
>
> does not require TPL<Z> to be instantiated, and therefore it *must not* be
> instantiated if we want to call ourselves a C++ compiler.
You're correct, C++ does not _require_ the pointers to point to complete
types, but if the pointed to type is completable, it should be
(and a template type be instantiated).
If not, we won't be able to tell whether one is a base of the
other. The current code path for structure pointer conversions is
1) build_c_cast -> convert_force
2) convert_force -> convert_to_pointer_force
3) convert_to_pointer_force -> get_base_distance
4) get_base_distance -> complete_type
5) complete_type _attempts_ instantiation, but leaves things untouched
if no definition exists (if instantiation fails, we give an error)
get_base_distance contains the comment
/* Should we be completing types here? */
So I think the patch's attempt to complete the types in build_c_cast is
ok.
Attached is an augmented testcase which tried both TPL (completable template
type), and TPL2 (incompletable template type).
ok?
nathan
--
Dr Nathan Sidwell :: Computer Science Department :: Bristol University
Never hand someone a gun unless you are sure where they will point it
nathan@acm.org http://www.cs.bris.ac.uk/~nathan/ nathan@cs.bris.ac.uk
// Build don't link:
// Skip if not target: sparc-*-*
// Special g++ Options: -ansi -pedantic-errors -Wcast-align
// Copyright (C) 1999 Free Software Foundation, Inc.
// Contributed by Nathan Sidwell 7 Dec 1999 <nathan@acm.org>
// converting a T * to void * does not need a complete T, and doesn't
// increase alignment requirements.
struct X;
struct Y;
struct Z {double m;};
void f3 (X *xp, Z *zp)
{
(void *)xp;
(void *)zp;
(Y *)xp;
(Y *)zp;
(Z *)xp;
}
template <class T> struct TPL { T m;};
template <class T> struct TPL2;
void f4 (char *ptr)
{
(Z *)ptr; // WARNING - alignment
(TPL<Z> *)ptr; // WARNING - alignment
(TPL2<Z> *)ptr; // ok, incomplete
}