diff --git a/clang/lib/Sema/SemaCast.cpp b/clang/lib/Sema/SemaCast.cpp index 46b5b4530648..ba00b712aad5 100644 --- a/clang/lib/Sema/SemaCast.cpp +++ b/clang/lib/Sema/SemaCast.cpp @@ -669,8 +669,10 @@ void CastOperation::CheckDynamicCast() { Self.MarkVTableUsed(OpRange.getBegin(), cast(SrcRecord->getDecl())); - // dynamic_cast is not available with fno-rtti - if (!Self.getLangOpts().RTTI) { + // dynamic_cast is not available with -fno-rtti. + // As an exception, dynamic_cast to void* is available because it doesn't + // use RTTI. + if (!Self.getLangOpts().RTTI && !DestPointee->isVoidType()) { Self.Diag(OpRange.getBegin(), diag::err_no_dynamic_cast_with_fno_rtti); SrcExpr = ExprError(); return; diff --git a/clang/test/SemaCXX/no-rtti.cpp b/clang/test/SemaCXX/no-rtti.cpp index 3d6e109551d4..a171b3cde2c3 100644 --- a/clang/test/SemaCXX/no-rtti.cpp +++ b/clang/test/SemaCXX/no-rtti.cpp @@ -22,3 +22,8 @@ struct B : public A { bool isa_B(A *a) { return dynamic_cast(a) != 0; // expected-error {{cannot use dynamic_cast with -fno-rtti}} } + +void* getMostDerived(A* a) { + // This cast does not use RTTI. + return dynamic_cast(a); +}