blob: 499382c782acd16c4a4f099273ed0fa5565586cc [file] [log] [blame]
Louis Dionneeb8650a2021-11-17 21:25:011//===----------------------------------------------------------------------===//
Marshall Clowf8da5b22011-06-03 13:54:372//
Chandler Carruth57b08b02019-01-19 10:56:403// Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions.
4// See https://llvm.org/LICENSE.txt for license information.
5// SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception
Marshall Clowf8da5b22011-06-03 13:54:376//
7//===----------------------------------------------------------------------===//
8
Louis Dionne8c611142020-04-17 14:29:159// UNSUPPORTED: no-exceptions
Asiri Rathnayake57e446d2016-05-31 12:01:3210
Marshall Clowf8da5b22011-06-03 13:54:3711#include <typeinfo>
Marshall Clowf8da5b22011-06-03 13:54:3712
13// Test taken from 5.2.8.2
Louis Dionnee1c67272020-04-17 14:06:5514// When typeid is applied to a glvalue expression whose type is a polymorphic
15// class type, (10.3), the result refers to a std::type_info object
16// representing the type of the most derived object (1.8) (that is, the
17// dynamic type) to which the glvalue refers. If the glvalue expression is
18// obtained by applying the unary * operator to a pointer(68) and the pointer
19// is a null pointer value (4.10), the typeid expression throws the
Marshall Clowf8da5b22011-06-03 13:54:3720// std::bad_typeid exception (18.7.3).
21//
Louis Dionnee1c67272020-04-17 14:06:5522// 68) If p is an expression of pointer type, then *p, (*p), *(p),
Marshall Clowf8da5b22011-06-03 13:54:3723// ((*p)), *((p)), and so on all meet this requirement.
24bool bad_typeid_test () {
Louis Dionnee1c67272020-04-17 14:06:5525 class A { virtual void f() {}};
26 class B { virtual void g() {}};
27
Nikolas Klausere99c4902024-10-31 01:20:1028 B* bp = nullptr;
29 try {
30 bool b = typeid(*bp) == typeid(A);
31 ((void)b);
32 } catch (const std::bad_typeid&) {
33 return true;
34 }
Marshall Clowf8da5b22011-06-03 13:54:3735 return false;
Louis Dionnecc69d212020-10-13 19:47:3136}
Marshall Clowf8da5b22011-06-03 13:54:3737
Louis Dionnee1c67272020-04-17 14:06:5538
39// The value of a failed cast to pointer type is the null pointer value of
40// the required result type. A failed cast to reference type throws
Marshall Clowf8da5b22011-06-03 13:54:3741// std::bad_cast (18.7.2).
42bool bad_cast_test () {
43 class A { virtual void f() {}};
44 class B { virtual void g() {}};
Louis Dionnee1c67272020-04-17 14:06:5545 class D : public virtual A, private B {};
Marshall Clowf8da5b22011-06-03 13:54:3746
47 D d;
48 B *bp = (B*)&d; // cast needed to break protection
Eric Fiseliera140cba2016-12-24 00:37:1349 try { D &dr = dynamic_cast<D&> (*bp); ((void)dr); }
50 catch ( const std::bad_cast & ) { return true; }
Marshall Clowf8da5b22011-06-03 13:54:3751 return false;
Louis Dionnecc69d212020-10-13 19:47:3152}
Louis Dionnee1c67272020-04-17 14:06:5553
Eric Fiseliera140cba2016-12-24 00:37:1354int main ( ) {
Marshall Clowf8da5b22011-06-03 13:54:3755 int ret_val = 0;
Louis Dionnee1c67272020-04-17 14:06:5556
Marshall Clowf8da5b22011-06-03 13:54:3757 if ( !bad_typeid_test ()) {
Marshall Clowf8da5b22011-06-03 13:54:3758 ret_val = 1;
59 }
Louis Dionnee1c67272020-04-17 14:06:5560
Marshall Clowf8da5b22011-06-03 13:54:3761 if ( !bad_cast_test ()) {
Louis Dionnecc69d212020-10-13 19:47:3162 ret_val = 2;
Marshall Clowf8da5b22011-06-03 13:54:3763 }
Louis Dionnee1c67272020-04-17 14:06:5564
Marshall Clowf8da5b22011-06-03 13:54:3765 return ret_val;
Louis Dionnecc69d212020-10-13 19:47:3166}