Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

[Clang][AST] Fix MS Mangle concept uneval context template instantiation crash #117845

Open
wants to merge 2 commits into
base: main
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
10 changes: 9 additions & 1 deletion clang/lib/AST/MicrosoftMangle.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -3374,7 +3374,15 @@ void MicrosoftCXXNameMangler::mangleType(const MemberPointerType *T,

void MicrosoftCXXNameMangler::mangleType(const TemplateTypeParmType *T,
Qualifiers, SourceRange Range) {
Error(Range.getBegin(), "template type parameter type") << Range;
Out << '?';

llvm::SmallString<64> Name;
Name += "<TTPT_";
Name += llvm::utostr(T->getDepth());
Name += "_";
Name += llvm::utostr(T->getIndex());
Name += ">";
mangleSourceName(Name);
}

void MicrosoftCXXNameMangler::mangleType(const SubstTemplateTypeParmPackType *T,
Expand Down
25 changes: 25 additions & 0 deletions clang/test/AST/ms-uneval-context-crash.cpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,25 @@
// RUN: %clang_cc1 -std=c++20 -fms-compatibility -fms-compatibility-version=19.33 -emit-llvm %s -o - -triple=x86_64-windows-msvc | FileCheck %s
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I think doing codegen (-emit-llvm) in clang/test/AST/ is unusual, those tests normally just look at the AST (though there are exceptions). Maybe there is a better place for this?


template <typename T>
concept C = requires
{
{ T::test([](){}) };
};

template<typename T>
struct Widget {};

template <C T>
struct Widget<T> {};

struct Baz
{
template<typename F>
static constexpr decltype(auto) test(F&&) {}
};

void test()
{
Widget<Baz> w;
}
// CHECK: @"?test@@YAXXZ"
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I suppose the mangling you're adding doesn't actually show up anywhere since it's only used for a concept? But then why do we need to mangle it in the first place?

Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Once we get to CodeGen, the method looks like a regular method, except for the fact that one of the template arguments is a class defined inside a concept instantiation. And CodeGen doesn't have any way to check for "is a template argument a class defined inside a concept". So, like all other methods, we stick it into DeferredDecls. And the key for the DeferredDecls map is the mangled name.

We end up trying to mangle a TemplateTypeParmType because we stick the instantiated class into the scope of an uninstantiated class template.

So no, we don't actually need to mangle it, but there's currently no way for CodeGen to check for this situation.