Skip to content

Commit bea17ff

Browse files
authored
[clang] Correct Microsoft mangling of lifetime extended temporary objects. (#85529)
Lifetime extended temporary objects that are bound to references with static storage duration may have external linkage and therefore require mangled symbol names. Clang uses an extension of the Microsoft ABI to give these symbols an implicit name of '$RT' followed by a discriminator and then mangles them similarly to the variable they are bound to. Clang's mangling scheme differs from the one used by MSVC. Previously, the `$RT<discriminator>` portion of the name was not registered as a back reference candidate and this resulted in incorrect back references for enclosing class and/or namespace scopes that might be referenced in the type of the object. This is an ABI change and has the potential to cause backward compatibility issues with previous Clang releases. Since MSVC uses a different mangling scheme, this change does not affect compatibility with MSVC. This fixes one of the name mangling concerns reported in #85423.
1 parent b282259 commit bea17ff

File tree

3 files changed

+21
-1
lines changed

3 files changed

+21
-1
lines changed

clang/docs/ReleaseNotes.rst

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -57,6 +57,12 @@ ABI Changes in This Version
5757
inline member function that contains a static local variable with a dynamic
5858
initializer is declared with ``__declspec(dllimport)``. (#GH83616).
5959

60+
- Fixed Microsoft name mangling of lifetime extended temporary objects. This
61+
change corrects missing back reference registrations that could result in
62+
incorrect back reference indexes and suprising demangled name results. Since
63+
MSVC uses a different mangling for these objects, compatibility is not affected.
64+
(#GH85423).
65+
6066
AST Dumping Potentially Breaking Changes
6167
----------------------------------------
6268

clang/lib/AST/MicrosoftMangle.cpp

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -3911,7 +3911,8 @@ void MicrosoftMangleContextImpl::mangleReferenceTemporary(
39113911
msvc_hashing_ostream MHO(Out);
39123912
MicrosoftCXXNameMangler Mangler(*this, MHO);
39133913

3914-
Mangler.getStream() << "?$RT" << ManglingNumber << '@';
3914+
Mangler.getStream() << "?";
3915+
Mangler.mangleSourceName("$RT" + llvm::utostr(ManglingNumber));
39153916
Mangler.mangle(VD, "");
39163917
}
39173918

clang/test/CodeGenCXX/mangle-ms-back-references.cpp

Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,18 @@
11
// RUN: %clang_cc1 -fms-extensions -fblocks -emit-llvm %s -o - -triple=i386-pc-win32 | FileCheck %s
22

3+
namespace NS {
4+
// The name "RT1" for the name of the class below has been specifically
5+
// chosen to ensure that back reference lookup does not match against the
6+
// implicitly generated "$RT1" name of the reference temporary symbol.
7+
struct RT1 {
8+
static const RT1& singleton;
9+
int i;
10+
};
11+
const RT1& RT1::singleton = RT1{1};
12+
}
13+
// CHECK: "?$RT1@singleton@RT1@NS@@2ABU23@B"
14+
// CHECK: "?singleton@RT1@NS@@2ABU12@B"
15+
316
void f1(const char* a, const char* b) {}
417
// CHECK: "?f1@@YAXPBD0@Z"
518

0 commit comments

Comments
 (0)