[all-commits] [llvm/llvm-project] 43b888: [clang-cl] [AST] Reapply #102848 Fix placeholder r...

Max Winkler via All-commits all-commits at lists.llvm.org
Sat Aug 24 12:26:08 PDT 2024


  Branch: refs/heads/main
  Home:   https://github.com/llvm/llvm-project
  Commit: 43b88851cefe68645aa59b1fccc8390a8a31f469
      https://github.com/llvm/llvm-project/commit/43b88851cefe68645aa59b1fccc8390a8a31f469
  Author: Max Winkler <max.enrico.winkler at gmail.com>
  Date:   2024-08-24 (Sat, 24 Aug 2024)

  Changed paths:
    M clang/docs/ReleaseNotes.rst
    M clang/lib/AST/MicrosoftMangle.cpp
    A clang/test/CodeGenCXX/mangle-ms-auto-return.cpp
    M clang/test/CodeGenCXX/mangle-ms-auto-templates-memptrs.cpp
    M clang/test/CodeGenCXX/mangle-ms-auto-templates-nullptr.cpp
    M clang/test/CodeGenCXX/mangle-ms-auto-templates.cpp

  Log Message:
  -----------
  [clang-cl] [AST] Reapply #102848 Fix placeholder return type name mangling for MSVC 1920+ / VS2019+ (#104722)

Reapply https://github.com/llvm/llvm-project/pull/102848.

The description in this PR will detail the changes from the reverted
original PR above.

For `auto&&` return types that can partake in reference collapsing we
weren't properly handling that mangling that can arise.
When collapsing occurs an inner reference is created with the collapsed
reference type. If we return `int&` from such a function then an inner
reference of `int&` is created within the `auto&&` return type.
`getPointeeType` on a reference type goes through all inner references
before returning the pointee type which ends up being a builtin type,
`int`, which is unexpected.

We can use `getPointeeTypeAsWritten` to get the `AutoType` as expected
however for the instantiated template declaration reference collapsing
already occurred on the return type. This means `auto&&` is turned into
`auto&` in our example above.
We end up mangling an lvalue reference type.
This is unintended as MSVC mangles on the declaration of the return
type, `auto&&` in this case, which is treated as an rvalue reference.
```
template<class T>
auto&& AutoReferenceCollapseT(int& x) { return static_cast<int&>(x); }

void test() 
{
    int x = 1;
    auto&& rref = AutoReferenceCollapseT<void>(x); // "??$AutoReferenceCollapseT at X@@YA$$QEA_PAEAH at Z"
    // Mangled as an rvalue reference to auto
}
```

If we are mangling a template with a placeholder return type we want to
get the first template declaration and use its return type to do the
mangling of any instantiations.

This fixes the bug reported in the original PR that caused the revert
with libcxx `std::variant`.
I also tested locally with libcxx and the following test code which
fails in the original PR but now works in this PR.
```
#include <variant>

void test()
{
    std::variant<int> v{ 1 };
    int& r = std::get<0>(v);
    (void)r;
}
```



To unsubscribe from these emails, change your notification settings at https://github.com/llvm/llvm-project/settings/notifications


More information about the All-commits mailing list