1*eb8650a7SLouis Dionne //===----------------------------------------------------------------------===//
2e434b34fSJonathan Roelofs //
357b08b09SChandler Carruth // Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions.
457b08b09SChandler Carruth // See https://llvm.org/LICENSE.txt for license information.
557b08b09SChandler Carruth // SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception
6e434b34fSJonathan Roelofs //
7e434b34fSJonathan Roelofs //===----------------------------------------------------------------------===//
8e434b34fSJonathan Roelofs
98c61114cSLouis Dionne // UNSUPPORTED: no-exceptions
1057e446daSAsiri Rathnayake
11e434b34fSJonathan Roelofs #include <typeinfo>
12e434b34fSJonathan Roelofs
13e434b34fSJonathan Roelofs // Test taken from 5.2.8.2
14e434b34fSJonathan Roelofs // When typeid is applied to a glvalue expression whose type is a polymorphic
15e434b34fSJonathan Roelofs // class type, (10.3), the result refers to a std::type_info object
16e434b34fSJonathan Roelofs // representing the type of the most derived object (1.8) (that is, the
17e434b34fSJonathan Roelofs // dynamic type) to which the glvalue refers. If the glvalue expression is
18e434b34fSJonathan Roelofs // obtained by applying the unary * operator to a pointer(68) and the pointer
19e434b34fSJonathan Roelofs // is a null pointer value (4.10), the typeid expression throws the
20e434b34fSJonathan Roelofs // std::bad_typeid exception (18.7.3).
21e434b34fSJonathan Roelofs //
22e434b34fSJonathan Roelofs // 68) If p is an expression of pointer type, then *p, (*p), *(p),
23e434b34fSJonathan Roelofs // ((*p)), *((p)), and so on all meet this requirement.
bad_typeid_test()24e434b34fSJonathan Roelofs bool bad_typeid_test () {
25e434b34fSJonathan Roelofs class A { virtual void f() {}};
26e434b34fSJonathan Roelofs class B { virtual void g() {}};
27e434b34fSJonathan Roelofs
28e434b34fSJonathan Roelofs B *bp = NULL;
29a140cba7SEric Fiselier try {bool b = typeid(*bp) == typeid (A); ((void)b); }
30a140cba7SEric Fiselier catch ( const std::bad_typeid &) { return true; }
31e434b34fSJonathan Roelofs return false;
32e434b34fSJonathan Roelofs }
33e434b34fSJonathan Roelofs
34e434b34fSJonathan Roelofs
35e434b34fSJonathan Roelofs // The value of a failed cast to pointer type is the null pointer value of
36e434b34fSJonathan Roelofs // the required result type. A failed cast to reference type throws
37e434b34fSJonathan Roelofs // std::bad_cast (18.7.2).
bad_cast_test()38e434b34fSJonathan Roelofs bool bad_cast_test () {
39e434b34fSJonathan Roelofs class A { virtual void f() {}};
40e434b34fSJonathan Roelofs class B { virtual void g() {}};
41e434b34fSJonathan Roelofs class D : public virtual A, private B {};
42e434b34fSJonathan Roelofs
43e434b34fSJonathan Roelofs D d;
44e434b34fSJonathan Roelofs B *bp = (B*)&d; // cast needed to break protection
45a140cba7SEric Fiselier try { D &dr = dynamic_cast<D&> (*bp); ((void)dr); }
46a140cba7SEric Fiselier catch ( const std::bad_cast & ) { return true; }
47e434b34fSJonathan Roelofs return false;
48e434b34fSJonathan Roelofs }
49e434b34fSJonathan Roelofs
main()50a140cba7SEric Fiselier int main ( ) {
51e434b34fSJonathan Roelofs int ret_val = 0;
52e434b34fSJonathan Roelofs
53e434b34fSJonathan Roelofs if ( !bad_typeid_test ()) {
54e434b34fSJonathan Roelofs ret_val = 1;
55e434b34fSJonathan Roelofs }
56e434b34fSJonathan Roelofs
57e434b34fSJonathan Roelofs if ( !bad_cast_test ()) {
58cc69d211SLouis Dionne ret_val = 2;
59e434b34fSJonathan Roelofs }
60e434b34fSJonathan Roelofs
61e434b34fSJonathan Roelofs return ret_val;
62e434b34fSJonathan Roelofs }
63