From fe9cf0abb7c4c59bf774123f431babcf8bdf4122 Mon Sep 17 00:00:00 2001 From: John McCall Date: Mon, 14 Feb 2011 23:21:33 +0000 Subject: [PATCH] Don't crash on hierarchy static_casts which appear in variable initializers. PR9221. llvm-svn: 125532 --- clang/lib/Sema/SemaCXXCast.cpp | 40 ++++++++++++++----- .../test/CXX/class.access/class.friend/p1.cpp | 15 +++++++ 2 files changed, 44 insertions(+), 11 deletions(-) diff --git a/clang/lib/Sema/SemaCXXCast.cpp b/clang/lib/Sema/SemaCXXCast.cpp index dda7f67d3f25..35b7f51df1cf 100644 --- a/clang/lib/Sema/SemaCXXCast.cpp +++ b/clang/lib/Sema/SemaCXXCast.cpp @@ -981,12 +981,20 @@ TryStaticDowncast(Sema &Self, CanQualType SrcType, CanQualType DestType, return TC_Failed; } - if (!CStyle && Self.CheckBaseClassAccess(OpRange.getBegin(), - SrcType, DestType, - Paths.front(), + if (!CStyle) { + switch (Self.CheckBaseClassAccess(OpRange.getBegin(), + SrcType, DestType, + Paths.front(), diag::err_downcast_from_inaccessible_base)) { - msg = 0; - return TC_Failed; + case Sema::AR_accessible: + case Sema::AR_delayed: // be optimistic + case Sema::AR_dependent: // be optimistic + break; + + case Sema::AR_inaccessible: + msg = 0; + return TC_Failed; + } } Self.BuildBasePathArray(Paths, BasePath); @@ -1065,12 +1073,22 @@ TryStaticMemberPointerUpcast(Sema &Self, Expr *&SrcExpr, QualType SrcType, return TC_Failed; } - if (!CStyle && Self.CheckBaseClassAccess(OpRange.getBegin(), - DestClass, SrcClass, - Paths.front(), - diag::err_upcast_to_inaccessible_base)) { - msg = 0; - return TC_Failed; + if (!CStyle) { + switch (Self.CheckBaseClassAccess(OpRange.getBegin(), + DestClass, SrcClass, + Paths.front(), + diag::err_upcast_to_inaccessible_base)) { + case Sema::AR_accessible: + case Sema::AR_delayed: + case Sema::AR_dependent: + // Optimistically assume that the delayed and dependent cases + // will work out. + break; + + case Sema::AR_inaccessible: + msg = 0; + return TC_Failed; + } } if (WasOverloadedFunction) { diff --git a/clang/test/CXX/class.access/class.friend/p1.cpp b/clang/test/CXX/class.access/class.friend/p1.cpp index af4df4c0f253..1668155c1819 100644 --- a/clang/test/CXX/class.access/class.friend/p1.cpp +++ b/clang/test/CXX/class.access/class.friend/p1.cpp @@ -326,3 +326,18 @@ namespace test11 { void A::test2(B::private_type x) {} // expected-error {{'private_type' is a private member of 'test11::B'}} void A::test3(int x = B::private_type()) {} // expected-error {{'private_type' is a private member of 'test11::B'}} } + + +// PR9221 +namespace test12 { + struct A { + void foo(); + }; + class B : private A { + friend void A::foo(); + void *mem; + }; + void A::foo() { + void *var = static_cast(this)->mem; + } +}