[clang] 02a308c - [CodeGen] Added '*' to internal linkage types (#220864)
via cfe-commits
cfe-commits at lists.llvm.org
Fri Dec 4 05:48:30 PST 2026
Author: Matthew
Date: 2026-12-04T08:48:22-05:00
New Revision: 02a308ca133bd814ae2018235aefbf4b8eaa43ab
URL: https://github.com/llvm/llvm-project/commit/02a308ca133bd814ae2018235aefbf4b8eaa43ab
DIFF: https://github.com/llvm/llvm-project/commit/02a308ca133bd814ae2018235aefbf4b8eaa43ab.diff
LOG: [CodeGen] Added '*' to internal linkage types (#220864)
RTTI typename for internal-linkage types lacked '*', which broke
type_info::operator== and dynamic_cast with libstdc++
Fixes #34255
Fixes #79367
Fixes #59790
AI usage: Claude (reviewed test files)
---------
Co-authored-by: Jones, Matthew (DI SW SIM FRWK PLAT SIMFW HPC) <jones.matthew at siemens.com>
Co-authored-by: Nikolas Klauser <nikolasklauser at berlin.de>
Co-authored-by: Tom <tajames at zoho.com>
Added:
clang/test/CIR/CodeGen/CIR-internal-linkage-typeinfo-name.cpp
clang/test/CodeGenCXX/internal-linkage-typeinfo-name.cpp
libcxx/test/libcxx/language.support/support.rtti/type.info/type_info.comparison.internal_linkage.sh.cpp
libcxx/test/libcxx/language.support/support.rtti/type.info/type_info.name.internal_linkage.pass.cpp
Modified:
clang/docs/ReleaseNotes.md
clang/lib/CIR/CodeGen/CIRGenItaniumCXXABI.cpp
clang/lib/CodeGen/ItaniumCXXABI.cpp
clang/test/CIR/CodeGenCXX/vtable-linkage.cpp
compiler-rt/lib/ubsan/ubsan_type_hash_itanium.cpp
libcxx/include/typeinfo
libcxx/test/libcxx/language.support/support.rtti/type.info/type_info.comparison.unmerged.sh.cpp
libcxx/utils/libcxx/test/features/misc.py
Removed:
################################################################################
diff --git a/clang/docs/ReleaseNotes.md b/clang/docs/ReleaseNotes.md
index 5a92a4e888f39a..cd7764af59db13 100644
--- a/clang/docs/ReleaseNotes.md
+++ b/clang/docs/ReleaseNotes.md
@@ -190,6 +190,11 @@ features cannot lower the translation-unit ABI level;
accesses to these fields. `__attribute__((packed))` on the field or the
enclosing record still reduces the alignment. (#GH208256)
+- Fixed RTTI name for internal-linkage types lacking '*', fixed breaking
+ type_info::operator== and dynamic_cast with libstdc++. Fixed libc++ NonUnique
+ implementation incorrectly comparing internal-linkage types equal via strcmp.
+ (#GH34255)
+
- On 32-bit Arm Windows, the C++ throw information (`ThrowInfo`,
`CatchableTypeArray` and `CatchableType`) is now image-relative, as the MSVC
runtime expects.
diff --git a/clang/lib/CIR/CodeGen/CIRGenItaniumCXXABI.cpp b/clang/lib/CIR/CodeGen/CIRGenItaniumCXXABI.cpp
index 8f700548fc14df..facc98cb955093 100644
--- a/clang/lib/CIR/CodeGen/CIRGenItaniumCXXABI.cpp
+++ b/clang/lib/CIR/CodeGen/CIRGenItaniumCXXABI.cpp
@@ -873,12 +873,18 @@ CIRGenItaniumRTTIBuilder::getAddrOfTypeName(mlir::Location loc, QualType ty,
llvm::raw_svector_ostream out(name);
cgm.getCXXABI().getMangleContext().mangleCXXRTTIName(ty, out);
- // We know that the mangled name of the type starts at index 4 of the
- // mangled name of the typename, so we can just index into it in order to
- // get the mangled name of the type.
+ // RTTI type-name symbol has form "_ZTS<mangled-string>". The string stored in
+ // type_info object excludes the "_ZTS" prefix. So we skip past the first 4
+ // characters. For types that do not have externally visible Clang/C++
+ // linkage, '*' is prepended to the type-name string so that within libstdc++
+ // RTTI names compare correctly via strcmp across translation units. Linkage
+ // of the type name is used, not the LLVM linkage of the typeinfo.
+ SmallString<256> typeName;
+ if (!isExternallyVisible(ty->getLinkage()))
+ typeName += '*';
+ typeName += StringRef(name).substr(4);
mlir::Attribute init = builder.getString(
- name.substr(4), cgm.convertType(cgm.getASTContext().CharTy),
- std::nullopt);
+ typeName, cgm.convertType(cgm.getASTContext().CharTy), std::nullopt);
CharUnits align =
cgm.getASTContext().getTypeAlignInChars(cgm.getASTContext().CharTy);
diff --git a/clang/lib/CodeGen/ItaniumCXXABI.cpp b/clang/lib/CodeGen/ItaniumCXXABI.cpp
index a46c450569e0de..f68fa14eac1124 100644
--- a/clang/lib/CodeGen/ItaniumCXXABI.cpp
+++ b/clang/lib/CodeGen/ItaniumCXXABI.cpp
@@ -3558,19 +3558,26 @@ llvm::GlobalVariable *ItaniumRTTIBuilder::GetAddrOfTypeName(
llvm::raw_svector_ostream Out(Name);
CGM.getCXXABI().getMangleContext().mangleCXXRTTIName(Ty, Out);
- // We know that the mangled name of the type starts at index 4 of the
- // mangled name of the typename, so we can just index into it in order to
- // get the mangled name of the type.
+ // RTTI type-name symbol has form "_ZTS<mangled-string>". The string stored in
+ // type_info object excludes the "_ZTS" prefix. So we skip past the first 4
+ // characters. For types that do not have externally visible Clang/C++
+ // linkage, '*' is prepended to the type-name string so that within libstdc++
+ // RTTI names compare correctly via strcmp across translation units. Linkage
+ // of the type name is used, not the LLVM linkage of the typeinfo.
+ SmallString<256> TypeName;
+ if (!isExternallyVisible(Ty->getLinkage()))
+ TypeName += '*';
+ TypeName += StringRef(Name).substr(4);
llvm::Constant *Init;
if (CGM.getTriple().isOSzOS()) {
// On z/OS, typename is stored as 2 encodings: EBCDIC followed by ASCII.
SmallString<256> DualEncodedName;
- llvm::ConverterEBCDIC::convertToEBCDIC(Name.substr(4), DualEncodedName);
+ llvm::ConverterEBCDIC::convertToEBCDIC(TypeName, DualEncodedName);
DualEncodedName += '\0';
- DualEncodedName += Name.substr(4);
+ DualEncodedName += TypeName;
Init = llvm::ConstantDataArray::getString(VMContext, DualEncodedName);
} else
- Init = llvm::ConstantDataArray::getString(VMContext, Name.substr(4));
+ Init = llvm::ConstantDataArray::getString(VMContext, TypeName);
auto Align = CGM.getContext().getTypeAlignInChars(CGM.getContext().CharTy);
diff --git a/clang/test/CIR/CodeGen/CIR-internal-linkage-typeinfo-name.cpp b/clang/test/CIR/CodeGen/CIR-internal-linkage-typeinfo-name.cpp
new file mode 100644
index 00000000000000..4f4f63478d4e30
--- /dev/null
+++ b/clang/test/CIR/CodeGen/CIR-internal-linkage-typeinfo-name.cpp
@@ -0,0 +1,51 @@
+//RUN: %clang_cc1 -triple x86_64-unknown-linux-gnu -std=c++11 -fclangir -emit-cir -o - %s | FileCheck %s
+
+// Check that RTTI type-name strings use a leading '*' for types that do not have externally visible Clang/C++ linkage,
+// and for it to be omitted from externally visible types.
+
+namespace std { class type_info; }
+
+//The following types have internal linkage, so their typeinfo names should have a leading '*'
+namespace { struct A {}; }
+const std::type_info &t1() { return typeid(A); }
+
+const std::type_info &t2() { struct L {}; return typeid(L); }
+
+const std::type_info &t3() { return typeid(A*); }
+
+template <class T> struct B {};
+const std::type_info &t4() { return typeid(B<A>); }
+
+namespace { struct C { int x; }; }
+const std::type_info &t5() { return typeid(int A::*); }
+
+//Following should not have a '*' prefix in the typeinfo name, since they have external linkage
+struct Ext {};
+
+const std::type_info &t6() { return typeid(Ext); }
+
+const std::type_info &t7() { return typeid(Ext*); }
+
+namespace NS2 { struct D {}; }
+
+const std::type_info &t8() { return typeid(NS2::D); }
+
+struct Fwd;
+const std::type_info &t9() { return typeid(Fwd*); }
+
+// The following checks emitted RTTI type-names. The global name is the ABI-mangled type-name object,
+// while the string constant is the mangled type-name itself. For types without externally visible linkage,
+// Clang emits these as 'internal'. {{.*}} matches for array sizes, which is irrelevant to this test.
+
+// These checks are for types with internal linkage, which should have a '*' prefix in the typeinfo name.
+//CHECK-DAG: cir.global{{.*}}internal{{.*}}@_ZTSN12_GLOBAL__N_11AE = #cir.const_array<"*N12_GLOBAL__N_11AE"{{.*}}>
+//CHECK-DAG: cir.global{{.*}}internal{{.*}}@_ZTSZ2t2vE1L = #cir.const_array<"*Z2t2vE1L"{{.*}}>
+//CHECK-DAG: cir.global{{.*}}internal{{.*}}@_ZTSPN12_GLOBAL__N_11AE = #cir.const_array<"*PN12_GLOBAL__N_11AE"{{.*}}>
+//CHECK-DAG: cir.global{{.*}}internal{{.*}}@_ZTS1BIN12_GLOBAL__N_11AEE = #cir.const_array<"*1BIN12_GLOBAL__N_11AEE"{{.*}}>
+//CHECK-DAG: cir.global{{.*}}internal{{.*}}@_ZTSMN12_GLOBAL__N_11AEi = #cir.const_array<"*MN12_GLOBAL__N_11AEi"{{.*}}>
+
+// These checks are for types with external linkage, which should not have a '*' prefix in the typeinfo name.
+//CHECK-DAG: cir.global{{.*}}@_ZTS3Ext = #cir.const_array<"3Ext"{{.*}}>
+//CHECK-DAG: cir.global{{.*}}@_ZTSP3Ext = #cir.const_array<"P3Ext"{{.*}}>
+//CHECK-DAG: cir.global{{.*}}@_ZTSN3NS21DE = #cir.const_array<"N3NS21DE"{{.*}}>
+//CHECK-DAG: cir.global{{.*}}@_ZTSP3Fwd = #cir.const_array<"P3Fwd"{{.*}}>
diff --git a/clang/test/CIR/CodeGenCXX/vtable-linkage.cpp b/clang/test/CIR/CodeGenCXX/vtable-linkage.cpp
index 9345542ebd8630..d7a1c21dfdecaf 100644
--- a/clang/test/CIR/CodeGenCXX/vtable-linkage.cpp
+++ b/clang/test/CIR/CodeGenCXX/vtable-linkage.cpp
@@ -197,19 +197,19 @@ void use_F() {
// The anonymous struct for e has no linkage, so the vtable should have
// internal linkage.
// CIR-DAG: cir.global "private" constant internal dso_local @_ZTV3$_0 = #cir.vtable<{#cir.const_array<[#cir.ptr<null> : !cir.ptr<!u8i>, #cir.global_view<@_ZTI3$_0> : !cir.ptr<!u8i>, #cir.global_view<@_ZN1D1fEv> : !cir.ptr<!u8i>]> : !cir.array<!cir.ptr<!u8i> x 3>}, address_points = [#cir.address_point<index = 0, offset = 2>]> : !{{.*}}align(8)
-// CIR-DAG: cir.global "private" constant internal dso_local @_ZTS3$_0 = #cir.const_array<"3$_0" : !cir.array<!s8i x 4>, trailing_zeros> : !cir.array<!s8i x 5> align(1)
+// CIR-DAG: cir.global "private" constant internal dso_local @_ZTS3$_0 = #cir.const_array<"*3$_0" : !cir.array<!s8i x 5>, trailing_zeros> : !cir.array<!s8i x 6> align(1)
// CIR-DAG: cir.global "private" constant internal dso_local @_ZTI3$_0 = #cir.typeinfo<{#cir.global_view<@_ZTVN10__cxxabiv120__si_class_type_infoE, [2 : i32]> : !cir.ptr<!u8i>, #cir.global_view<@_ZTS3$_0> : !cir.ptr<!u8i>, #cir.global_view<@_ZTI1D> : !cir.ptr<!u8i>}> : !{{.*}}align(8)
// LLVM-DAG: @"_ZTV3$_0" = internal constant { [3 x ptr] } { [3 x ptr] [ptr null, ptr @"_ZTI3$_0", ptr @_ZN1D1fEv] }, align 8
-// LLVM-DAG: @"_ZTS3$_0" = internal constant [5 x i8] c"3$_0\00", align 1
+// LLVM-DAG: @"_ZTS3$_0" = internal constant [6 x i8] c"*3$_0\00", align 1
// LLVM-DAG: @"_ZTI3$_0" = internal constant { ptr, ptr, ptr } { ptr getelementptr {{.*}}({{.*}}, ptr @_ZTVN10__cxxabiv120__si_class_type_infoE, i64 {{.*}}), ptr @"_ZTS3$_0", ptr @_ZTI1D }, align 8
// The A vtable should have internal linkage since it is inside an anonymous
// namespace.
// CIR-DAG: cir.global "private" constant internal dso_local @_ZTVN12_GLOBAL__N_11AE = #cir.vtable<{#cir.const_array<[#cir.ptr<null> : !cir.ptr<!u8i>, #cir.global_view<@_ZTIN12_GLOBAL__N_11AE> : !cir.ptr<!u8i>, #cir.global_view<@_ZN12_GLOBAL__N_11A1fEv> : !cir.ptr<!u8i>]> : !cir.array<!cir.ptr<!u8i> x 3>}, address_points = [#cir.address_point<index = 0, offset = 2>]> : !{{.*}}align(8)
-// CIR-DAG: cir.global "private" constant internal dso_local @_ZTSN12_GLOBAL__N_11AE = #cir.const_array<"N12_GLOBAL__N_11AE" : !cir.array<!s8i x 18>, trailing_zeros> : !cir.array<!s8i x 19> align(1)
+// CIR-DAG: cir.global "private" constant internal dso_local @_ZTSN12_GLOBAL__N_11AE = #cir.const_array<"*N12_GLOBAL__N_11AE" : !cir.array<!s8i x 19>, trailing_zeros> : !cir.array<!s8i x 20> align(1)
// CIR-DAG: cir.global "private" constant internal dso_local @_ZTIN12_GLOBAL__N_11AE = #cir.typeinfo<{#cir.global_view<@_ZTVN10__cxxabiv117__class_type_infoE, [2 : i32]> : !cir.ptr<!u8i>, #cir.global_view<@_ZTSN12_GLOBAL__N_11AE> : !cir.ptr<!u8i>}> : !{{.*}}align(8)
// LLVM-DAG: @_ZTVN12_GLOBAL__N_11AE = internal constant { [3 x ptr] } { [3 x ptr] [ptr null, ptr @_ZTIN12_GLOBAL__N_11AE, ptr @_ZN12_GLOBAL__N_11A1fEv] }, align 8
-// LLVM-DAG: @_ZTSN12_GLOBAL__N_11AE = internal constant [19 x i8] c"N12_GLOBAL__N_11AE\00", align 1
+// LLVM-DAG: @_ZTSN12_GLOBAL__N_11AE = internal constant [20 x i8] c"*N12_GLOBAL__N_11AE\00", align 1
// LLVM-DAG: @_ZTIN12_GLOBAL__N_11AE = internal constant { ptr, ptr } { ptr getelementptr {{.*}}({{.*}}, ptr @_ZTVN10__cxxabiv117__class_type_infoE, i64 {{.*}}), ptr @_ZTSN12_GLOBAL__N_11AE }, align 8
// F<char> is an explicit specialization without a key function, so
diff --git a/clang/test/CodeGenCXX/internal-linkage-typeinfo-name.cpp b/clang/test/CodeGenCXX/internal-linkage-typeinfo-name.cpp
new file mode 100644
index 00000000000000..c4d739f0768751
--- /dev/null
+++ b/clang/test/CodeGenCXX/internal-linkage-typeinfo-name.cpp
@@ -0,0 +1,51 @@
+//RUN: %clang_cc1 -triple x86_64-unknown-linux-gnu -std=c++11 -emit-llvm -o - %s | FileCheck %s
+
+// Check that RTTI type-name strings use a leading '*' for types that do not have externally visible Clang/C++ linkage,
+// and for it to be omitted from externally visible types.
+
+namespace std { class type_info; }
+
+//The following types have internal linkage, so their typeinfo names should have a leading '*'
+namespace { struct A {}; }
+const std::type_info &t1() { return typeid(A); }
+
+const std::type_info &t2() { struct L {}; return typeid(L); }
+
+const std::type_info &t3() { return typeid(A*); }
+
+template <class T> struct B {};
+const std::type_info &t4() { return typeid(B<A>); }
+
+namespace { struct C { int x; }; }
+const std::type_info &t5() { return typeid(int A::*); }
+
+//Following should not have a '*' prefix in the typeinfo name, since they have external linkage
+struct Ext {};
+
+const std::type_info &t6() { return typeid(Ext); }
+
+const std::type_info &t7() { return typeid(Ext*); }
+
+namespace NS2 { struct D {}; }
+
+const std::type_info &t8() { return typeid(NS2::D); }
+
+struct Fwd;
+const std::type_info &t9() { return typeid(Fwd*); }
+
+// The following checks emitted RTTI type-names. The global name is the ABI-mangled type-name object,
+// while the string constant is the mangled type-name itself. For types without externally visible linkage,
+// Clang emits these as 'internal constant'. {{.*}} matches for array sizes, which is irrelevant to this test.
+
+// These checks are for types with internal linkage, which should have a '*' prefix in the typeinfo name.
+//CHECK-DAG: @_ZTSN12_GLOBAL__N_11AE = internal constant {{.*}}c"*N12_GLOBAL__N_11AE\00"
+//CHECK-DAG: @_ZTSZ2t2vE1L = internal constant {{.*}}c"*Z2t2vE1L\00"
+//CHECK-DAG: @_ZTSPN12_GLOBAL__N_11AE = internal constant {{.*}}c"*PN12_GLOBAL__N_11AE\00"
+//CHECK-DAG: @_ZTS1BIN12_GLOBAL__N_11AEE = internal constant {{.*}}c"*1BIN12_GLOBAL__N_11AEE\00"
+//CHECK-DAG: @_ZTSMN12_GLOBAL__N_11AEi = internal constant {{.*}}c"*MN12_GLOBAL__N_11AEi\00"
+
+// These checks are for types with external linkage, which should not have a '*' prefix in the typeinfo name.
+//CHECK-DAG: @_ZTS3Ext = {{.*}}constant {{.*}}c"3Ext\00"
+//CHECK-DAG: @_ZTSP3Ext = {{.*}}constant {{.*}}c"P3Ext\00"
+//CHECK-DAG: @_ZTSN3NS21DE = {{.*}}constant {{.*}}c"N3NS21DE\00"
+//CHECK-DAG: @_ZTSP3Fwd = {{.*}}constant {{.*}}c"P3Fwd\00"
diff --git a/compiler-rt/lib/ubsan/ubsan_type_hash_itanium.cpp b/compiler-rt/lib/ubsan/ubsan_type_hash_itanium.cpp
index 15788574dd9958..0259ffe9b09963 100644
--- a/compiler-rt/lib/ubsan/ubsan_type_hash_itanium.cpp
+++ b/compiler-rt/lib/ubsan/ubsan_type_hash_itanium.cpp
@@ -256,6 +256,16 @@ bool __ubsan::checkDynamicType(void *Object, void *Type, HashValue Hash) {
return true;
}
+// Itanium ABI compilers prefix the RTTI name with a '*' for internal linkage
+// types to request comparison by address. The marker is not part of the mangled
+// name, so strip it before the name is demangled or matched against
+// suppressions. The C++ standard libraries strip it in std::type_info::name(),
+// but the runtime uses its own type_info above, which returns the raw name so
+// that checkTypeInfoEquality can still see the marker.
+static const char *stripInternalLinkage(const char *Name) {
+ return Name && Name[0] == '*' ? Name + 1 : Name;
+}
+
__ubsan::DynamicTypeInfo
__ubsan::getDynamicTypeInfoFromVtable(void *VtablePtr) {
VtablePrefix *Vtable = getVtablePrefix(VtablePtr);
@@ -266,8 +276,9 @@ __ubsan::getDynamicTypeInfoFromVtable(void *VtablePtr) {
const abi::__class_type_info *ObjectType = findBaseAtOffset(
static_cast<const abi::__class_type_info*>(Vtable->TypeInfo),
-Vtable->Offset);
- return DynamicTypeInfo(Vtable->TypeInfo->name(), -Vtable->Offset,
- ObjectType ? ObjectType->name() : "<unknown>");
+ return DynamicTypeInfo(
+ stripInternalLinkage(Vtable->TypeInfo->name()), -Vtable->Offset,
+ ObjectType ? stripInternalLinkage(ObjectType->name()) : "<unknown>");
}
bool __ubsan::checkTypeInfoEquality(const void *TypeInfo1,
diff --git a/libcxx/include/typeinfo b/libcxx/include/typeinfo
index f75daeed08fba3..e533a3569ff28b 100644
--- a/libcxx/include/typeinfo
+++ b/libcxx/include/typeinfo
@@ -217,10 +217,19 @@ struct __non_unique_impl : __string_impl_base {
return __hash;
}
_LIBCPP_HIDE_FROM_ABI _LIBCPP_ALWAYS_INLINE static bool __eq(__type_name_t __lhs, __type_name_t __rhs) _NOEXCEPT {
- return __lhs == __rhs || __builtin_strcmp(__lhs, __rhs) == 0;
+ // Internal linkage types (marked with '*') cannot compare equal via strcmp.
+ // They are only equal if they refer to the same RTTI object.
+ return __lhs == __rhs || (__lhs[0] != '*' && __builtin_strcmp(__lhs, __rhs) == 0);
}
_LIBCPP_HIDE_FROM_ABI _LIBCPP_ALWAYS_INLINE static bool __lt(__type_name_t __lhs, __type_name_t __rhs) _NOEXCEPT {
- return __builtin_strcmp(__lhs, __rhs) < 0;
+ int __r = __builtin_strcmp(__lhs, __rhs);
+ if (__r != 0)
+ return __r < 0;
+ // Internal linkage types (marked with '*') are ordered via pointer address.
+ if (__lhs[0] == '*') {
+ return reinterpret_cast<uintptr_t>(__lhs) < reinterpret_cast<uintptr_t>(__rhs);
+ }
+ return false;
}
};
@@ -300,7 +309,8 @@ protected:
public:
virtual ~type_info();
[[__nodiscard__]] _LIBCPP_HIDE_FROM_ABI const char* name() const _NOEXCEPT {
- return __impl::__type_name_to_string(__type_name);
+ const char* __str = __impl::__type_name_to_string(__type_name);
+ return __str[0] == '*' ? __str + 1 : __str;
}
[[__nodiscard__]] _LIBCPP_HIDE_FROM_ABI bool before(const type_info& __arg) const _NOEXCEPT {
diff --git a/libcxx/test/libcxx/language.support/support.rtti/type.info/type_info.comparison.internal_linkage.sh.cpp b/libcxx/test/libcxx/language.support/support.rtti/type.info/type_info.comparison.internal_linkage.sh.cpp
new file mode 100644
index 00000000000000..54b7781c572434
--- /dev/null
+++ b/libcxx/test/libcxx/language.support/support.rtti/type.info/type_info.comparison.internal_linkage.sh.cpp
@@ -0,0 +1,59 @@
+//===----------------------------------------------------------------------===//
+//
+// 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
+//
+//===----------------------------------------------------------------------===//
+
+// UNSUPPORTED: no-rtti
+
+// Older versions don't mark RTTI names of types with internal linkage with a '*'
+// XFAIL: clang-21, clang-22, clang-23, apple-clang
+
+// Itanium ABI implementations prefix the RTTI name of a type with internal linkage with a '*',
+// which lets this implementation distinguish between two internally linked types.
+
+// RUN: %{cxx} %s %{flags} %{compile_flags} -c -o %t.tu1.o -DTU1 -D_LIBCPP_TYPEINFO_COMPARISON_IMPLEMENTATION=2
+// RUN: %{cxx} %s %{flags} %{compile_flags} -c -o %t.tu2.o -DTU2 -D_LIBCPP_TYPEINFO_COMPARISON_IMPLEMENTATION=2
+// RUN: %{cxx} %s %{flags} %{compile_flags} -c -o %t.main.o -DMAIN -D_LIBCPP_TYPEINFO_COMPARISON_IMPLEMENTATION=2
+// RUN: %{cxx} %t.tu1.o %t.tu2.o %t.main.o %{flags} %{link_flags} -o %t.exe
+// RUN: %{exec} %t.exe
+
+#include <cassert>
+#include <typeindex>
+#include <vector>
+
+extern std::vector<std::type_index> registry;
+
+void register1();
+void register2();
+
+#if defined(TU1)
+namespace {
+struct A {
+ bool x;
+};
+} // namespace
+void register1() { registry.push_back(std::type_index(typeid(A))); }
+#elif defined(TU2)
+namespace {
+struct A {
+ int x, y;
+};
+} // namespace
+void register2() { registry.push_back(std::type_index(typeid(A))); }
+#elif defined(MAIN)
+std::vector<std::type_index> registry;
+
+int main(int, char**) {
+ register1();
+ register2();
+
+ assert(registry.size() == 2);
+ assert(registry[0] != registry[1]);
+ return 0;
+}
+#else
+# error
+#endif
diff --git a/libcxx/test/libcxx/language.support/support.rtti/type.info/type_info.comparison.unmerged.sh.cpp b/libcxx/test/libcxx/language.support/support.rtti/type.info/type_info.comparison.unmerged.sh.cpp
index 9b94fcbc1c92a8..4347105f64652c 100644
--- a/libcxx/test/libcxx/language.support/support.rtti/type.info/type_info.comparison.unmerged.sh.cpp
+++ b/libcxx/test/libcxx/language.support/support.rtti/type.info/type_info.comparison.unmerged.sh.cpp
@@ -6,40 +6,38 @@
//
//===----------------------------------------------------------------------===//
+// Check that the strcmp-based type_info comparison treats two copies of the RTTI
+// for the same type as equal. A has hidden visibility, so the shared library and
+// the executable each get their own copy.
+
// UNSUPPORTED: no-rtti
+// UNSUPPORTED: android
+
+// REQUIRES: can-build-shared-libraries
-// RUN: %{cxx} %s %{flags} %{compile_flags} -c -o %t.tu1.o -DTU1 -D_LIBCPP_TYPEINFO_COMPARISON_IMPLEMENTATION=2
-// RUN: %{cxx} %s %{flags} %{compile_flags} -c -o %t.tu2.o -DTU2 -D_LIBCPP_TYPEINFO_COMPARISON_IMPLEMENTATION=2
-// RUN: %{cxx} %s %{flags} %{compile_flags} -c -o %t.main.o -DMAIN -D_LIBCPP_TYPEINFO_COMPARISON_IMPLEMENTATION=2
-// RUN: %{cxx} %t.tu1.o %t.tu2.o %t.main.o %{flags} %{link_flags} -o %t.exe
+// RUN: %{cxx} %s %{flags} %{compile_flags} -fPIC -c -o %t.lib.o -DLIB -D_LIBCPP_TYPEINFO_COMPARISON_IMPLEMENTATION=2
+// RUN: %{cxx} %t.lib.o %{flags} %{link_flags} -shared -o %t.lib.so
+// RUN: %{build} -DMAIN -D_LIBCPP_TYPEINFO_COMPARISON_IMPLEMENTATION=2 %t.lib.so
// RUN: %{exec} %t.exe
#include <cassert>
#include <typeindex>
-#include <vector>
-extern std::vector<std::type_index> registry;
+struct __attribute__((visibility("hidden"))) A {};
-void register1();
-void register2();
+std::type_index from_library();
-#if defined(TU1)
- namespace { struct A { bool x; }; }
- void register1() { registry.push_back(std::type_index(typeid(A))); }
-#elif defined(TU2)
- namespace { struct A { int x, y; }; }
- void register2() { registry.push_back(std::type_index(typeid(A))); }
+#if defined(LIB)
+std::type_index from_library() { return typeid(A); }
#elif defined(MAIN)
- std::vector<std::type_index> registry;
-
- int main(int, char**) {
- register1();
- register2();
-
- assert(registry.size() == 2);
- assert(registry[0] == registry[1]);
- return 0;
- }
+int main(int, char**) {
+ std::type_index from_executable = typeid(A);
+ assert(from_executable.name() != from_library().name());
+ assert(from_executable == from_library());
+ assert(!(from_executable < from_library()));
+ assert(!(from_library() < from_executable));
+ return 0;
+}
#else
-# error
+# error
#endif
diff --git a/libcxx/test/libcxx/language.support/support.rtti/type.info/type_info.name.internal_linkage.pass.cpp b/libcxx/test/libcxx/language.support/support.rtti/type.info/type_info.name.internal_linkage.pass.cpp
new file mode 100644
index 00000000000000..745f8fbc08f464
--- /dev/null
+++ b/libcxx/test/libcxx/language.support/support.rtti/type.info/type_info.name.internal_linkage.pass.cpp
@@ -0,0 +1,31 @@
+//===----------------------------------------------------------------------===//
+//
+// 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
+//
+//===----------------------------------------------------------------------===//
+
+// UNSUPPORTED: no-rtti
+
+// Compilers may prefix the type_info::name() result with a '*' to indicate that the type has internal linkage.
+// This test checks that libc++ strips the '*' prefix from the name() result.
+
+#include <typeinfo>
+#include <cassert>
+
+namespace {
+struct AnonymousType {};
+} // namespace
+
+const std::type_info& local_type() {
+ struct LocalType {};
+ return typeid(LocalType);
+}
+
+int main(int, char**) {
+ assert(typeid(AnonymousType).name()[0] != '*');
+ assert(typeid(AnonymousType*).name()[0] != '*');
+ assert(local_type().name()[0] != '*');
+ return 0;
+}
diff --git a/libcxx/utils/libcxx/test/features/misc.py b/libcxx/utils/libcxx/test/features/misc.py
index de92907d656d9c..90359ed95502f2 100644
--- a/libcxx/utils/libcxx/test/features/misc.py
+++ b/libcxx/utils/libcxx/test/features/misc.py
@@ -324,4 +324,14 @@ def _needsLibatomic(cfg):
name="has-splitfile",
when=lambda cfg: runScriptExitCode(cfg, ["split-file --version"]) == 0,
),
+ Feature(
+ name="can-build-shared-libraries",
+ when=lambda cfg: sourceBuilds(
+ cfg,
+ """
+ void* f() { return new int; }
+ """,
+ ["-fPIC", "-shared"],
+ ),
+ ),
]
More information about the cfe-commits
mailing list