<table border="1" cellspacing="0" cellpadding="8">
<tr>
<th>Issue</th>
<td>
<a href=https://github.com/llvm/llvm-project/issues/86525>86525</a>
</td>
</tr>
<tr>
<th>Summary</th>
<td>
Clang's Microsoft mangling of lifetime extended temporary objects and anonymous unions differs from MSVC
</td>
</tr>
<tr>
<th>Labels</th>
<td>
clang
</td>
</tr>
<tr>
<th>Assignees</th>
<td>
tahonermann
</td>
</tr>
<tr>
<th>Reporter</th>
<td>
tahonermann
</td>
</tr>
</table>
<pre>
As discussed in PR #85529, Clang's mangling of lifetime extended temporary objects for the Microsoft ABI differs from the mangling produced by MSVC. Clang currently uses an invented `$RT<discriminator>` mangling (see commit e77de75d7e009daaadb03e9b1091c3a9a9c9d311; the claim that MSVC is non-conforming is applicable to older MSVC versions). Current versions of MSVC going back to at least v19.14 use a `$S<discriminator>` mangling for these and other cases of unnamed objects with external linkage.
The example below exercises other unnamed objects in order to demonstrate how they contribute to selection of discriminator values.
```
> type t.cpp
struct S {
int dm;
};
int f(S s) {
static auto [si] = s;
static union { int dm = si; };
static const S& sls = S{si};
return si + dm + sls.dm;
}
# MSVC v19.35 compilation:
> cl /nologo /c /std:c++latest t.cpp
t.cpp
> llvm-objdump -t t.obj | find "f@@" | sort /+58
...
[23](sec 6)(fl 0x00)(ty 0)(scl 3) (nx 0) 0x00000000 ?$S1@?1??f@@YAHUS@@@Z@4U2@A
[29](sec 8)(fl 0x00)(ty 0)(scl 3) (nx 0) 0x00000000 ?$S2@?1??f@@YAHUS@@@Z@4T<unnamed-type-$S2>@?1??1@YAH0@Z@A
[41](sec 12)(fl 0x00)(ty 0)(scl 3) (nx 0) 0x00000000 ?$S3@?1??f@@YAHUS@@@Z@4U2@A
[26](sec 7)(fl 0x00)(ty 0)(scl 3) (nx 0) 0x00000000 ?$TSS0@?1??f@@YAHUS@@@Z@4HA
[32](sec 9)(fl 0x00)(ty 0)(scl 3) (nx 0) 0x00000000 ?$TSS1@?1??f@@YAHUS@@@Z@4HA
[38](sec 11)(fl 0x00)(ty 0)(scl 3) (nx 0) 0x00000000 ?$TSS2@?1??f@@YAHUS@@@Z@4HA
[12](sec 3)(fl 0x00)(ty 20)(scl 3) (nx 0) 0x00000130 ??0<unnamed-type-$S2>@?1??f@@YAHUS@@@Z@QEAA@XZ
[11](sec 3)(fl 0x00)(ty 20)(scl 2) (nx 0) 0x00000000 ?f@@YAHUS@@@Z
[35](sec 10)(fl 0x00)(ty 0)(scl 3) (nx 0) 0x00000000 ?sls@?1??f@@YAHUS@@@Z@4AEBU2@EB
# Clang 17.0.6 compilation:
> clang -c -std=c++2c t.cpp
> llvm-objdump -t t.o | find "f@@" | sort /+58
[26](sec 3)(fl 0x00)(ty 0)(scl 3) (nx 0) 0x0000001c ?$RT1@sls@?1??f@@YAHUS@@@Z@4AEBU2@EB
[20](sec 3)(fl 0x00)(ty 0)(scl 3) (nx 0) 0x00000000 ?$S1@?1??f@@YAHUS@@@Z@4U2@A
[23](sec 3)(fl 0x00)(ty 0)(scl 3) (nx 0) 0x00000008 ?$S2@?2??f@@YAHUS@@@Z@4T<unnamed-type-$S2>@?1@YAH0@Z@A
[16](sec 3)(fl 0x00)(ty 0)(scl 3) (nx 0) 0x00000004 ?$TSS0@?1??f@@YAHUS@@@Z@4HA
[22](sec 3)(fl 0x00)(ty 0)(scl 3) (nx 0) 0x0000000c ?$TSS1@?2??f@@YAHUS@@@Z@4HA
[25](sec 3)(fl 0x00)(ty 0)(scl 3) (nx 0) 0x00000018 ?$TSS2@?1??f@@YAHUS@@@Z@4HA
[24](sec 1)(fl 0x00)(ty 20)(scl 3) (nx 0) 0x00000140 ??0<unnamed-type-$S2>@?f@@YAHUS@@@Z@QEAA@XZ
[15](sec 1)(fl 0x00)(ty 20)(scl 2) (nx 0) 0x00000000 ?f@@YAHUS@@@Z
[27](sec 3)(fl 0x00)(ty 0)(scl 3) (nx 0) 0x00000010 ?sls@?1??f@@YAHUS@@@Z@4AEBU2@EB
```
Several name mangling inconsistencies are demonstrated above as detailed below.
1. Clang uses `$RT<discriminator>` for lifetime extended temporaries as this issue reports.
```
MSVC: ?$S3@?1??f@@YAHUS@@@Z@4U2@A
LLVM: ?$RT1@sls@?1??f@@YAHUS@@@Z@4AEBU2@EB
```
2. Clang and MSVC disagree on use of local context discriminators for the anonymous union.
```
MSVC: ?$S2@ ?1? ?f@@YAHUS@@@Z@4T<unnamed-type-$S2>@ ?1? ?1@YAH0@Z@A
LLVM: ?$S2@ ?2? ?f@@YAHUS@@@Z@4T<unnamed-type-$S2>@ ?1@YAH0@Z@A
MSVC: ?$TSS1@ ?1? ?f@@YAHUS@@@Z@4HA
LLVM: ?$TSS1@ ?2? ?f@@YAHUS@@@Z@4HA
MSVC: ??0<unnamed-type-$S2>@ ?1? ?f@@YAHUS@@@Z@QEAA@XZ
LLVM: ??0<unnamed-type-$S2>@ ?f@@YAHUS@@@Z@QEAA@XZ
```
</pre>
<img width="1px" height="1px" alt="" src="http://email.email.llvm.org/o/eJysWFFz4jgS_jXipSuULdkYP_CAk1BzVTtVd8Ps1t2-yXIbNCtLlCRnwr-_kk3AsENwlqQox5al1tdftz6rxZ2TG424IGlBKPV8azTahmtNKCXp04S3fmvsYvBiUppqv1g6qKQTrXNYgdTw729AKJunKc0JfYRHxfWG0MxBw_VGSb0BU4OSNXrZIOCrR11hBR6bnbHc7sGUP1B4B7Wx4LcIX6Wwxpnaw7L4F1SyrtE6qK1putdHsztrqlZgBeUevq7_eJz2c4NorUXt1R5ahw64BqlfUHusgMwiQpNv3wl7DD5Y2UjNvbGEPZNZdDJN6NwhgjBNIz1gllWYpVWGUZRXnPOqjBjmZRzlsWA857nIKxbHhBUdQqG4DFi573CBdKCNfhBG18Y2wb50wHc7JQUvFYI3YFSFtu_9gtZJox2h-RQee1-OjYHLrtfGBDslF3-F4dyDQu48vMT5NE6C48AP3q5vOXvgPQzRFRi_RQuCB-pMDa3WvMHqGKSf0m-7IFrNFSip_-IbnJLoiUTL_vp9G6LMm51CKFGZn4CvaIXsDHbGL21KDc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