Issue 239275
Summary [Clang][Itanium] Inheriting constructor names do not register the inherited-from class as a substitution candidate, so later substitutions differ from GCC
Labels clang
Assignees
Reporter rnk
    I encountered this while looking at llvm/llvm-project#111345. The following text is agent-generated.

----

## Problem

For an inheriting constructor of a class template specialization, clang writes the
inherited-from class (`CI1 <type>`) but does not add that type to the substitution
table. GCC does. Every `S<n>_` after it is numbered differently, so the two compilers
emit different symbol names for the same function, and a demangler that follows the ABI
(llvm::demangle, libiberty) cannot decode clang's name.

## Reproducer

```c++
namespace llvm { namespace orc {
struct GNT {}; struct Buf {};
template <class T> struct Impl2 { Impl2(T) {} };
struct EPC { struct H {}; };
}}
namespace std {
template <class T> struct del {};
template <class T, class D> struct impl {
  template <class Del> impl(T *, Del &&, llvm::orc::EPC::H *) {}
};
template <class T, class D, bool B> struct data : impl<T, D> { using impl<T, D>::impl; };
}
void g() {
  llvm::orc::GNT *p = nullptr;
 std::data<llvm::orc::GNT, std::del<llvm::orc::GNT>, true>
      d(p, std::del<llvm::orc::Impl2<llvm::orc::EPC::H>>(), nullptr);
}
```

`clang++ -S` emits (the last parameter is `PS9_`):

```
_ZNSt4dataIN4llvm3orc3GNTESt3delIS2_ELb1EECI2St4implIS2_S4_EIS3_INS1_5Impl2INS1_3EPC1HEEEEEEPS2_OT_PS9_
```

`g++ -S` emits the same text except for the last substitution (`PSB_`):

```
_ZNSt4dataIN4llvm3orc3GNTESt3delIS2_ELb1EECI2St4implIS2_S4_EIS3_INS1_5Impl2INS1_3EPC1HEEEEEEPS2_OT_PSB_
```

The Itanium ABI makes the `<type>` of `CI1 <type>` a substitution candidate like any
other class type, so GCC's numbering is the one the ABI describes. With GCC's names the
LLVM and libiberty demanglers decode correctly. Clang's name, read by the ABI, refers to
a different entity, and for deeper names (nested lambdas in the template arguments, as in
`llvm/lib/ExecutionEngine/Orc/EPCGenericRTDyldMemoryManager.cpp` with libstdc++'s
`std::__uniq_ptr_data`) it does not decode at all.

When the inherited-from class is not a template specialization (no substitution after it
refers to it), clang and GCC agree.

## Cause

`CXXNameMangler::mangleCXXCtorType` in `clang/lib/AST/ItaniumMangle.cpp` calls
`mangleName(InheritedFrom)`. That names the class, and for a specialization it registers
the template name, but unlike `mangleType` it does not add the class type itself.

## Impact

- Symbol names for these constructors differ between clang and GCC for the same source,
  which is an ABI compatibility difference between the two compilers (not yet checked
  whether it causes a practical link or ODR problem).
- Debuggers, profilers and symbolizers (anything using the demangler) show the raw name.
- Changing it changes symbol names, so it needs `-fclang-abi-compat` gating.

## How found

A local assertion that every mangled name clang emits can be demangled by `llvm::demangle`,
run while building LLVM with itself.

_______________________________________________
llvm-bugs mailing list
[email protected]
https://lists.llvm.org/cgi-bin/mailman/listinfo/llvm-bugs

Reply via email to