Louis Dionne | eb8650a | 2021-11-17 21:25:01 | [diff] [blame] | 1 | //===----------------------------------------------------------------------===// |
Marshall Clow | f8da5b2 | 2011-06-03 13:54:37 | [diff] [blame] | 2 | // |
Chandler Carruth | 57b08b0 | 2019-01-19 10:56:40 | [diff] [blame] | 3 | // 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 Clow | f8da5b2 | 2011-06-03 13:54:37 | [diff] [blame] | 6 | // |
| 7 | //===----------------------------------------------------------------------===// |
| 8 | |
Louis Dionne | 8c61114 | 2020-04-17 14:29:15 | [diff] [blame] | 9 | // UNSUPPORTED: no-exceptions |
Asiri Rathnayake | 57e446d | 2016-05-31 12:01:32 | [diff] [blame] | 10 | |
Marshall Clow | f8da5b2 | 2011-06-03 13:54:37 | [diff] [blame] | 11 | #include <typeinfo> |
Marshall Clow | f8da5b2 | 2011-06-03 13:54:37 | [diff] [blame] | 12 | |
| 13 | // Test taken from 5.2.8.2 |
Louis Dionne | e1c6727 | 2020-04-17 14:06:55 | [diff] [blame] | 14 | // 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 Clow | f8da5b2 | 2011-06-03 13:54:37 | [diff] [blame] | 20 | // std::bad_typeid exception (18.7.3). |
| 21 | // |
Louis Dionne | e1c6727 | 2020-04-17 14:06:55 | [diff] [blame] | 22 | // 68) If p is an expression of pointer type, then *p, (*p), *(p), |
Marshall Clow | f8da5b2 | 2011-06-03 13:54:37 | [diff] [blame] | 23 | // ((*p)), *((p)), and so on all meet this requirement. |
| 24 | bool bad_typeid_test () { |
Louis Dionne | e1c6727 | 2020-04-17 14:06:55 | [diff] [blame] | 25 | class A { virtual void f() {}}; |
| 26 | class B { virtual void g() {}}; |
| 27 | |
Nikolas Klauser | e99c490 | 2024-10-31 01:20:10 | [diff] [blame^] | 28 | 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 Clow | f8da5b2 | 2011-06-03 13:54:37 | [diff] [blame] | 35 | return false; |
Louis Dionne | cc69d21 | 2020-10-13 19:47:31 | [diff] [blame] | 36 | } |
Marshall Clow | f8da5b2 | 2011-06-03 13:54:37 | [diff] [blame] | 37 | |
Louis Dionne | e1c6727 | 2020-04-17 14:06:55 | [diff] [blame] | 38 | |
| 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 Clow | f8da5b2 | 2011-06-03 13:54:37 | [diff] [blame] | 41 | // std::bad_cast (18.7.2). |
| 42 | bool bad_cast_test () { |
| 43 | class A { virtual void f() {}}; |
| 44 | class B { virtual void g() {}}; |
Louis Dionne | e1c6727 | 2020-04-17 14:06:55 | [diff] [blame] | 45 | class D : public virtual A, private B {}; |
Marshall Clow | f8da5b2 | 2011-06-03 13:54:37 | [diff] [blame] | 46 | |
| 47 | D d; |
| 48 | B *bp = (B*)&d; // cast needed to break protection |
Eric Fiselier | a140cba | 2016-12-24 00:37:13 | [diff] [blame] | 49 | try { D &dr = dynamic_cast<D&> (*bp); ((void)dr); } |
| 50 | catch ( const std::bad_cast & ) { return true; } |
Marshall Clow | f8da5b2 | 2011-06-03 13:54:37 | [diff] [blame] | 51 | return false; |
Louis Dionne | cc69d21 | 2020-10-13 19:47:31 | [diff] [blame] | 52 | } |
Louis Dionne | e1c6727 | 2020-04-17 14:06:55 | [diff] [blame] | 53 | |
Eric Fiselier | a140cba | 2016-12-24 00:37:13 | [diff] [blame] | 54 | int main ( ) { |
Marshall Clow | f8da5b2 | 2011-06-03 13:54:37 | [diff] [blame] | 55 | int ret_val = 0; |
Louis Dionne | e1c6727 | 2020-04-17 14:06:55 | [diff] [blame] | 56 | |
Marshall Clow | f8da5b2 | 2011-06-03 13:54:37 | [diff] [blame] | 57 | if ( !bad_typeid_test ()) { |
Marshall Clow | f8da5b2 | 2011-06-03 13:54:37 | [diff] [blame] | 58 | ret_val = 1; |
| 59 | } |
Louis Dionne | e1c6727 | 2020-04-17 14:06:55 | [diff] [blame] | 60 | |
Marshall Clow | f8da5b2 | 2011-06-03 13:54:37 | [diff] [blame] | 61 | if ( !bad_cast_test ()) { |
Louis Dionne | cc69d21 | 2020-10-13 19:47:31 | [diff] [blame] | 62 | ret_val = 2; |
Marshall Clow | f8da5b2 | 2011-06-03 13:54:37 | [diff] [blame] | 63 | } |
Louis Dionne | e1c6727 | 2020-04-17 14:06:55 | [diff] [blame] | 64 | |
Marshall Clow | f8da5b2 | 2011-06-03 13:54:37 | [diff] [blame] | 65 | return ret_val; |
Louis Dionne | cc69d21 | 2020-10-13 19:47:31 | [diff] [blame] | 66 | } |