r187564 - Check dynamic_cast is not used with -fno-rtti, unless it is a noop or can be resolved statically.
Arnaud A. de Grandmaison
arnaud.adegm at gmail.com
Thu Aug 1 01:28:32 PDT 2013
Author: aadg
Date: Thu Aug 1 03:28:32 2013
New Revision: 187564
URL: http://llvm.org/viewvc/llvm-project?rev=187564&view=rev
Log:
Check dynamic_cast is not used with -fno-rtti, unless it is a noop or can be resolved statically.
Added:
cfe/trunk/test/CodeGenCXX/dynamic_cast-no-rtti.cpp
Modified:
cfe/trunk/include/clang/Basic/DiagnosticSemaKinds.td
cfe/trunk/lib/Sema/SemaCast.cpp
cfe/trunk/test/SemaCXX/no-rtti.cpp
Modified: cfe/trunk/include/clang/Basic/DiagnosticSemaKinds.td
URL: http://llvm.org/viewvc/llvm-project/cfe/trunk/include/clang/Basic/DiagnosticSemaKinds.td?rev=187564&r1=187563&r2=187564&view=diff
==============================================================================
--- cfe/trunk/include/clang/Basic/DiagnosticSemaKinds.td (original)
+++ cfe/trunk/include/clang/Basic/DiagnosticSemaKinds.td Thu Aug 1 03:28:32 2013
@@ -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">;
Modified: cfe/trunk/lib/Sema/SemaCast.cpp
URL: http://llvm.org/viewvc/llvm-project/cfe/trunk/lib/Sema/SemaCast.cpp?rev=187564&r1=187563&r2=187564&view=diff
==============================================================================
--- cfe/trunk/lib/Sema/SemaCast.cpp (original)
+++ cfe/trunk/lib/Sema/SemaCast.cpp Thu Aug 1 03:28:32 2013
@@ -667,6 +667,13 @@ void CastOperation::CheckDynamicCast() {
Self.MarkVTableUsed(OpRange.getBegin(),
cast<CXXRecordDecl>(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;
}
Added: cfe/trunk/test/CodeGenCXX/dynamic_cast-no-rtti.cpp
URL: http://llvm.org/viewvc/llvm-project/cfe/trunk/test/CodeGenCXX/dynamic_cast-no-rtti.cpp?rev=187564&view=auto
==============================================================================
--- cfe/trunk/test/CodeGenCXX/dynamic_cast-no-rtti.cpp (added)
+++ cfe/trunk/test/CodeGenCXX/dynamic_cast-no-rtti.cpp Thu Aug 1 03:28:32 2013
@@ -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<A *>(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 *>(b);
+// CHECK: define %struct.B* @_Z8samecastP1B
+// CHECK-NOT: call i8* @__dynamic_cast
+}
Modified: cfe/trunk/test/SemaCXX/no-rtti.cpp
URL: http://llvm.org/viewvc/llvm-project/cfe/trunk/test/SemaCXX/no-rtti.cpp?rev=187564&r1=187563&r2=187564&view=diff
==============================================================================
--- cfe/trunk/test/SemaCXX/no-rtti.cpp (original)
+++ cfe/trunk/test/SemaCXX/no-rtti.cpp Thu Aug 1 03:28:32 2013
@@ -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<B *>(a) != 0; // expected-error {{cannot use dynamic_cast with -fno-rtti}}
+}
More information about the cfe-commits
mailing list