Debian Codegraph @

clang/test/AST/ByteCode/typeid.cpp

raw
1 // RUN: %clang_cc1 -std=c++20 -fexperimental-new-constant-interpreter -verify=expected,both %s
2 // RUN: %clang_cc1 -std=c++20 -verify=ref,both %s
3
4 namespace std {
5 struct __type_info_implementations {
6 struct __string_impl_base {
7 typedef const char *__type_name_t;
8 };
9 struct __unique_impl : __string_impl_base {
10
11 static bool __eq(__type_name_t __lhs, __type_name_t __rhs);
12 };
13 typedef __unique_impl __impl;
14 };
15
16 class __pointer_type_info {
17 public:
18 int __flags = 0;
19 };
20
21 class type_info : public __pointer_type_info {
22 protected:
23 typedef __type_info_implementations::__impl __impl;
24 __impl::__type_name_t __type_name;
25 };
26 }; // namespace std
27
28 static_assert(&typeid(int) != &typeid(long));
29 static_assert(&typeid(int) == &typeid(int));
30 static_assert(&typeid(int) < &typeid(long)); // both-error {{not an integral constant expression}} \
31 // both-note {{comparison between pointers to unrelated objects '&typeid(int)' and '&typeid(long)' has unspecified value}}
32 static_assert(&typeid(int) > &typeid(long)); // both-error {{not an integral constant expression}} \
33 // both-note {{comparison between pointers to unrelated objects '&typeid(int)' and '&typeid(long)' has unspecified value}}
34
35 struct Base {
36 virtual void func() ;
37 };
38 struct Derived : Base {};
39
40 constexpr bool test() {
41 Derived derived;
42 Base const &as_base = derived;
43 if (&typeid(as_base) != &typeid(Derived))
44 __builtin_abort();
45 return true;
46 }
47 static_assert(test());
48
49 int dontcrash() {
50 auto& pti = static_cast<const std::__pointer_type_info&>(
51 typeid(int)
52 );
53 return pti.__flags == 0 ? 1 : 0;
54 }
55
56 namespace TypeidPtrInEvaluationResult {
57 struct C {};
58 C c = C();
59 consteval const std::type_info *ftype_info() { return &typeid(c); }
60 const std::type_info *T1 = ftype_info();
61 }
62
63 // Regression test for crash in ArrayElemPtrPop with typeid pointers. GH-163127
64 namespace TypeidPtrRegression {
65 void dontcrash() {
66 constexpr auto res = ((void**)&typeid(int))[0]; // both-error {{must be initialized by a constant expression}} \
67 // both-note {{cast that performs the conversions of a reinterpret_cast is not allowed in a constant expression}}
68 }
69 void dontcrash2() {
70 constexpr auto res = ((void**)&typeid(int))[1]; // both-error {{must be initialized by a constant expression}} \
71 // both-note {{cast that performs the conversions of a reinterpret_cast is not allowed in a constant expression}}
72 }
73 }
74
75 namespace GH173950 {
76 struct A {
77 virtual void f();
78 };
79
80 static A &a = *new A;
81 extern A &a;
82
83 // This used to crash with: Assertion `IsInitialized' failed in invokeDtor()
84 const std::type_info &a_ti = typeid(a);
85 }
86
87 namespace MissingInitalizer {
88 struct Item {
89 const std::type_info &ti;
90 };
91 extern constexpr Item items[] = ; // both-error {{expected expression}} \
92 // both-note {{declared here}}
93 constexpr auto &x = items[0].ti; // both-error {{must be initialized by a constant expression}} \
94 // both-note {{initializer of 'items' is unknown}}
95 }