2011-06-03 21:54:37 +08:00
|
|
|
//===-------------------------- test_aux_runtime.cpp ----------------------===//
|
|
|
|
//
|
2019-01-19 18:56:40 +08:00
|
|
|
// Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions.
|
|
|
|
// See https://llvm.org/LICENSE.txt for license information.
|
|
|
|
// SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception
|
2011-06-03 21:54:37 +08:00
|
|
|
//
|
|
|
|
//===----------------------------------------------------------------------===//
|
|
|
|
|
2020-04-17 22:29:15 +08:00
|
|
|
// UNSUPPORTED: no-exceptions
|
2016-05-31 20:01:32 +08:00
|
|
|
|
2011-06-03 21:54:37 +08:00
|
|
|
#include <typeinfo>
|
|
|
|
|
|
|
|
// Test taken from 5.2.8.2
|
2020-04-17 22:06:55 +08:00
|
|
|
// When typeid is applied to a glvalue expression whose type is a polymorphic
|
|
|
|
// class type, (10.3), the result refers to a std::type_info object
|
|
|
|
// representing the type of the most derived object (1.8) (that is, the
|
|
|
|
// dynamic type) to which the glvalue refers. If the glvalue expression is
|
|
|
|
// obtained by applying the unary * operator to a pointer(68) and the pointer
|
|
|
|
// is a null pointer value (4.10), the typeid expression throws the
|
2011-06-03 21:54:37 +08:00
|
|
|
// std::bad_typeid exception (18.7.3).
|
|
|
|
//
|
2020-04-17 22:06:55 +08:00
|
|
|
// 68) If p is an expression of pointer type, then *p, (*p), *(p),
|
2011-06-03 21:54:37 +08:00
|
|
|
// ((*p)), *((p)), and so on all meet this requirement.
|
|
|
|
bool bad_typeid_test () {
|
2020-04-17 22:06:55 +08:00
|
|
|
class A { virtual void f() {}};
|
|
|
|
class B { virtual void g() {}};
|
|
|
|
|
2011-06-03 21:54:37 +08:00
|
|
|
B *bp = NULL;
|
2016-12-24 08:37:13 +08:00
|
|
|
try {bool b = typeid(*bp) == typeid (A); ((void)b); }
|
|
|
|
catch ( const std::bad_typeid &) { return true; }
|
2011-06-03 21:54:37 +08:00
|
|
|
return false;
|
2020-10-14 03:47:31 +08:00
|
|
|
}
|
2011-06-03 21:54:37 +08:00
|
|
|
|
2020-04-17 22:06:55 +08:00
|
|
|
|
|
|
|
// The value of a failed cast to pointer type is the null pointer value of
|
|
|
|
// the required result type. A failed cast to reference type throws
|
2011-06-03 21:54:37 +08:00
|
|
|
// std::bad_cast (18.7.2).
|
|
|
|
bool bad_cast_test () {
|
|
|
|
class A { virtual void f() {}};
|
|
|
|
class B { virtual void g() {}};
|
2020-04-17 22:06:55 +08:00
|
|
|
class D : public virtual A, private B {};
|
2011-06-03 21:54:37 +08:00
|
|
|
|
|
|
|
D d;
|
|
|
|
B *bp = (B*)&d; // cast needed to break protection
|
2016-12-24 08:37:13 +08:00
|
|
|
try { D &dr = dynamic_cast<D&> (*bp); ((void)dr); }
|
|
|
|
catch ( const std::bad_cast & ) { return true; }
|
2011-06-03 21:54:37 +08:00
|
|
|
return false;
|
2020-10-14 03:47:31 +08:00
|
|
|
}
|
2020-04-17 22:06:55 +08:00
|
|
|
|
2016-12-24 08:37:13 +08:00
|
|
|
int main ( ) {
|
2011-06-03 21:54:37 +08:00
|
|
|
int ret_val = 0;
|
2020-04-17 22:06:55 +08:00
|
|
|
|
2011-06-03 21:54:37 +08:00
|
|
|
if ( !bad_typeid_test ()) {
|
|
|
|
ret_val = 1;
|
|
|
|
}
|
2020-04-17 22:06:55 +08:00
|
|
|
|
2011-06-03 21:54:37 +08:00
|
|
|
if ( !bad_cast_test ()) {
|
2020-10-14 03:47:31 +08:00
|
|
|
ret_val = 2;
|
2011-06-03 21:54:37 +08:00
|
|
|
}
|
2020-04-17 22:06:55 +08:00
|
|
|
|
2011-06-03 21:54:37 +08:00
|
|
|
return ret_val;
|
2020-10-14 03:47:31 +08:00
|
|
|
}
|