From 789d82a568264543410a3bf0c2c431ccc11d5d1d Mon Sep 17 00:00:00 2001 From: "Arnaud A. de Grandmaison" Date: Thu, 1 Aug 2013 08:28:32 +0000 Subject: [PATCH] Check dynamic_cast is not used with -fno-rtti, unless it is a noop or can be resolved statically. git-svn-id: https://llvm.org/svn/llvm-project/cfe/trunk@187564 91177308-0d34-0410-b5e6-96231b3b80d8 --- include/clang/Basic/DiagnosticSemaKinds.td | 2 ++ lib/Sema/SemaCast.cpp | 7 ++++++ test/CodeGenCXX/dynamic_cast-no-rtti.cpp | 26 ++++++++++++++++++++++ test/SemaCXX/no-rtti.cpp | 14 ++++++++++++ 4 files changed, 49 insertions(+) create mode 100644 test/CodeGenCXX/dynamic_cast-no-rtti.cpp diff --git a/include/clang/Basic/DiagnosticSemaKinds.td b/include/clang/Basic/DiagnosticSemaKinds.td index f6a3515d7f..6438008a1e 100644 --- a/include/clang/Basic/DiagnosticSemaKinds.td +++ b/include/clang/Basic/DiagnosticSemaKinds.td @@ -5036,6 +5036,8 @@ def err_not_tag_in_scope : Error< def err_no_typeid_with_fno_rtti : Error< "cannot use typeid with -fno-rtti">; +def err_no_dynamic_cast_with_fno_rtti : Error< + "cannot use dynamic_cast with -fno-rtti">; def err_cannot_form_pointer_to_member_of_reference_type : Error< "cannot form a pointer-to-member to member %0 of reference type %1">; diff --git a/lib/Sema/SemaCast.cpp b/lib/Sema/SemaCast.cpp index 1adb037f0e..888c14ec39 100644 --- a/lib/Sema/SemaCast.cpp +++ b/lib/Sema/SemaCast.cpp @@ -667,6 +667,13 @@ void CastOperation::CheckDynamicCast() { Self.MarkVTableUsed(OpRange.getBegin(), cast(SrcRecord->getDecl())); + // dynamic_cast is not available with fno-rtti + if (!Self.getLangOpts().RTTI) { + Self.Diag(OpRange.getBegin(), diag::err_no_dynamic_cast_with_fno_rtti); + SrcExpr = ExprError(); + return; + } + // Done. Everything else is run-time checks. Kind = CK_Dynamic; } diff --git a/test/CodeGenCXX/dynamic_cast-no-rtti.cpp b/test/CodeGenCXX/dynamic_cast-no-rtti.cpp new file mode 100644 index 0000000000..2060e1d545 --- /dev/null +++ b/test/CodeGenCXX/dynamic_cast-no-rtti.cpp @@ -0,0 +1,26 @@ +// RUN: %clang_cc1 -emit-llvm %s -verify -fno-rtti -o - | FileCheck %s +// expected-no-diagnostics + +struct A { + virtual ~A(){}; +}; + +struct B : public A { + B() : A() {} +}; + +// An upcast can be resolved statically and can be used with -fno-rtti, iff it +// does not use runtime support. +A *upcast(B *b) { + return dynamic_cast(b); +// CHECK: define %struct.A* @_Z6upcastP1B +// CHECK-NOT: call i8* @__dynamic_cast +} + +// A NoOp dynamic_cast can be used with -fno-rtti iff it does not use +// runtime support. +B *samecast(B *b) { + return dynamic_cast(b); +// CHECK: define %struct.B* @_Z8samecastP1B +// CHECK-NOT: call i8* @__dynamic_cast +} diff --git a/test/SemaCXX/no-rtti.cpp b/test/SemaCXX/no-rtti.cpp index 75167050dc..3d6e109551 100644 --- a/test/SemaCXX/no-rtti.cpp +++ b/test/SemaCXX/no-rtti.cpp @@ -8,3 +8,17 @@ void f() { (void)typeid(int); // expected-error {{cannot use typeid with -fno-rtti}} } + +namespace { +struct A { + virtual ~A(){}; +}; + +struct B : public A { + B() : A() {} +}; +} + +bool isa_B(A *a) { + return dynamic_cast(a) != 0; // expected-error {{cannot use dynamic_cast with -fno-rtti}} +} -- 2.40.0