From f2abe19dbb0c34fdfe7d3402c091c00982cdc61c Mon Sep 17 00:00:00 2001 From: John McCall Date: Wed, 27 Mar 2013 00:03:48 +0000 Subject: Make the -Wreinterpret-base-class logic safe against invalid declarations at any point. Patch by Alexander Zinenko, and report by Richard Smith. llvm-svn: 178098 --- clang/test/SemaCXX/warn-reinterpret-base-class.cpp | 47 ++++++++++++++++++++++ 1 file changed, 47 insertions(+) (limited to 'clang/test/SemaCXX/warn-reinterpret-base-class.cpp') diff --git a/clang/test/SemaCXX/warn-reinterpret-base-class.cpp b/clang/test/SemaCXX/warn-reinterpret-base-class.cpp index fc7d15c8a34..a7deafeadf2 100644 --- a/clang/test/SemaCXX/warn-reinterpret-base-class.cpp +++ b/clang/test/SemaCXX/warn-reinterpret-base-class.cpp @@ -37,6 +37,53 @@ void reinterpret_not_defined_class(B *b, C *c) { (void)reinterpret_cast(*c); } +// Do not fail on erroneous classes with fields of incompletely-defined types. +// Base class is malformed. +namespace BaseMalformed { + struct A; // expected-note {{forward declaration of 'BaseMalformed::A'}} + struct B { + A a; // expected-error {{field has incomplete type 'BaseMalformed::A'}} + }; + struct C : public B {} c; + B *b = reinterpret_cast(&c); +} // end anonymous namespace + +// Child class is malformed. +namespace ChildMalformed { + struct A; // expected-note {{forward declaration of 'ChildMalformed::A'}} + struct B {}; + struct C : public B { + A a; // expected-error {{field has incomplete type 'ChildMalformed::A'}} + } c; + B *b = reinterpret_cast(&c); +} // end anonymous namespace + +// Base class outside upcast base-chain is malformed. +namespace BaseBaseMalformed { + struct A; // expected-note {{forward declaration of 'BaseBaseMalformed::A'}} + struct Y {}; + struct X { A a; }; // expected-error {{field has incomplete type 'BaseBaseMalformed::A'}} + struct B : Y, X {}; + struct C : B {} c; + B *p = reinterpret_cast(&c); +} + +namespace InheritanceMalformed { + struct A; // expected-note {{forward declaration of 'InheritanceMalformed::A'}} + struct B : A {}; // expected-error {{base class has incomplete type}} + struct C : B {} c; + B *p = reinterpret_cast(&c); +} + +// Virtual base class outside upcast base-chain is malformed. +namespace VBaseMalformed{ + struct A; // expected-note {{forward declaration of 'VBaseMalformed::A'}} + struct X { A a; }; // expected-error {{field has incomplete type 'VBaseMalformed::A'}} + struct B : public virtual X {}; + struct C : B {} c; + B *p = reinterpret_cast(&c); +} + void reinterpret_not_updowncast(A *pa, const A *pca, A &a, const A &ca) { (void)*reinterpret_cast(pa); (void)*reinterpret_cast(pa); -- cgit v1.2.3