Skip to content

Commit e0d173d

Browse files
authored
[Clang] [AST] Fix placeholder return type name mangling for MSVC 1920+ / VS2019+ (#102848)
Partial fix for #92204. This PR just fixes VS2019+ since that is the suite of compilers that I require link compatibility with at the moment. I still intend to fix VS2017 and to update llvm-undname in future PRs. Once those are also finished and merged I'll close out #92204. I am hoping to get the llvm-undname PR up in a couple of weeks to be able to demangle the VS2019+ name mangling. MSVC 1920+ mangles placeholder return types for non-templated functions with "@". For example `auto foo() { return 0; }` is mangled as `?foo@@ya@XZ`. MSVC 1920+ mangles placeholder return types for templated functions as the qualifiers of the AutoType followed by "_P" for `auto` and "_T" for `decltype(auto)`. For example `template<class T> auto foo() { return 0; }` is mangled as `??$foo@H@@ya?A_PXZ` when `foo` is instantiated as follows `foo<int>()`. Lambdas with placeholder return types are still mangled with clang's custom mangling since MSVC lambda mangling hasn't been deciphered yet. Similarly any pointers in the return type with an address space are mangled with clang's custom mangling since that is a clang extension. We cannot augment `mangleType` to support this mangling scheme as the mangling schemes for variables and functions differ. auto variables are encoded with the fully deduced type where auto return types are not. The following two functions with a static variable are mangled the same ``` template<class T> int test() { static int i = 0; // "?i@?1???$test@H@@yahxz@4HA" return i; } template<class T> int test() { static auto i = 0; // "?i@?1???$test@H@@yahxz@4HA" return i; } ``` Inside `mangleType` once we get to mangling the `AutoType` we have no context if we are from a variable encoding or some other encoding. Therefore it was easier to handle any special casing for `AutoType` return types with a separate function instead of using the `mangleType` infrastructure.
1 parent b4dc986 commit e0d173d

6 files changed

+533
-19
lines changed

clang/docs/ReleaseNotes.rst

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -77,6 +77,8 @@ C++ Specific Potentially Breaking Changes
7777
ABI Changes in This Version
7878
---------------------------
7979

80+
- Fixed Microsoft name mangling of placeholder, auto and decltype(auto), return types for MSVC 1920+. This change resolves incompatibilities with code compiled by MSVC 1920+ but will introduce incompatibilities with code compiled by earlier versions of Clang unless such code is built with the compiler option -fms-compatibility-version=19.14 to imitate the MSVC 1914 mangling behavior.
81+
8082
AST Dumping Potentially Breaking Changes
8183
----------------------------------------
8284

clang/lib/AST/MicrosoftMangle.cpp

Lines changed: 152 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -408,6 +408,8 @@ class MicrosoftCXXNameMangler {
408408
void mangleSourceName(StringRef Name);
409409
void mangleNestedName(GlobalDecl GD);
410410

411+
void mangleAutoReturnType(QualType T, QualifierMangleMode QMM);
412+
411413
private:
412414
bool isStructorDecl(const NamedDecl *ND) const {
413415
return ND == Structor || getStructor(ND) == Structor;
@@ -477,6 +479,11 @@ class MicrosoftCXXNameMangler {
477479
SourceRange Range);
478480
void mangleObjCKindOfType(const ObjCObjectType *T, Qualifiers Quals,
479481
SourceRange Range);
482+
483+
void mangleAutoReturnType(const MemberPointerType *T, Qualifiers Quals);
484+
void mangleAutoReturnType(const PointerType *T, Qualifiers Quals);
485+
void mangleAutoReturnType(const LValueReferenceType *T, Qualifiers Quals);
486+
void mangleAutoReturnType(const RValueReferenceType *T, Qualifiers Quals);
480487
};
481488
}
482489

@@ -2494,6 +2501,57 @@ void MicrosoftCXXNameMangler::mangleAddressSpaceType(QualType T,
24942501
mangleArtificialTagType(TagTypeKind::Struct, ASMangling, {"__clang"});
24952502
}
24962503

2504+
void MicrosoftCXXNameMangler::mangleAutoReturnType(QualType T,
2505+
QualifierMangleMode QMM) {
2506+
assert(getASTContext().getLangOpts().isCompatibleWithMSVC(
2507+
LangOptions::MSVC2019) &&
2508+
"Cannot mangle MSVC 2017 auto return types!");
2509+
2510+
if (isa<AutoType>(T)) {
2511+
const auto *AT = T->getContainedAutoType();
2512+
Qualifiers Quals = T.getLocalQualifiers();
2513+
2514+
if (QMM == QMM_Result)
2515+
Out << '?';
2516+
if (QMM != QMM_Drop)
2517+
mangleQualifiers(Quals, false);
2518+
Out << (AT->isDecltypeAuto() ? "_T" : "_P");
2519+
return;
2520+
}
2521+
2522+
T = T.getDesugaredType(getASTContext());
2523+
Qualifiers Quals = T.getLocalQualifiers();
2524+
2525+
switch (QMM) {
2526+
case QMM_Drop:
2527+
case QMM_Result:
2528+
break;
2529+
case QMM_Mangle:
2530+
mangleQualifiers(Quals, false);
2531+
break;
2532+
default:
2533+
llvm_unreachable("QMM_Escape unexpected");
2534+
}
2535+
2536+
const Type *ty = T.getTypePtr();
2537+
switch (ty->getTypeClass()) {
2538+
case Type::MemberPointer:
2539+
mangleAutoReturnType(cast<MemberPointerType>(ty), Quals);
2540+
break;
2541+
case Type::Pointer:
2542+
mangleAutoReturnType(cast<PointerType>(ty), Quals);
2543+
break;
2544+
case Type::LValueReference:
2545+
mangleAutoReturnType(cast<LValueReferenceType>(ty), Quals);
2546+
break;
2547+
case Type::RValueReference:
2548+
mangleAutoReturnType(cast<RValueReferenceType>(ty), Quals);
2549+
break;
2550+
default:
2551+
llvm_unreachable("Invalid type expected");
2552+
}
2553+
}
2554+
24972555
void MicrosoftCXXNameMangler::mangleType(QualType T, SourceRange Range,
24982556
QualifierMangleMode QMM) {
24992557
// Don't use the canonical types. MSVC includes things like 'const' on
@@ -2907,17 +2965,51 @@ void MicrosoftCXXNameMangler::mangleFunctionType(const FunctionType *T,
29072965
// can differ by their calling convention and are typically deduced. So
29082966
// we make sure that this type gets mangled properly.
29092967
mangleType(ResultType, Range, QMM_Result);
2910-
} else if (const auto *AT = dyn_cast_or_null<AutoType>(
2911-
ResultType->getContainedAutoType())) {
2912-
Out << '?';
2913-
mangleQualifiers(ResultType.getLocalQualifiers(), /*IsMember=*/false);
2914-
Out << '?';
2968+
} else if (IsInLambda) {
2969+
if (const auto *AT = ResultType->getContainedAutoType()) {
2970+
assert(AT->getKeyword() == AutoTypeKeyword::Auto &&
2971+
"should only need to mangle auto!");
2972+
Out << '?';
2973+
mangleQualifiers(ResultType.getLocalQualifiers(), /*IsMember=*/false);
2974+
Out << '?';
2975+
mangleSourceName("<auto>");
2976+
Out << '@';
2977+
} else {
2978+
Out << '@';
2979+
}
2980+
} else if (const auto *AT = ResultType->getContainedAutoType()) {
29152981
assert(AT->getKeyword() != AutoTypeKeyword::GNUAutoType &&
29162982
"shouldn't need to mangle __auto_type!");
2917-
mangleSourceName(AT->isDecltypeAuto() ? "<decltype-auto>" : "<auto>");
2918-
Out << '@';
2919-
} else if (IsInLambda) {
2920-
Out << '@';
2983+
2984+
// If we have any pointer types with the clang address space extension
2985+
// then defer to the custom clang mangling to keep backwards
2986+
// compatibility. See `mangleType(const PointerType *T, Qualifiers Quals,
2987+
// SourceRange Range)` for details.
2988+
auto UseClangMangling = [](QualType ResultType) {
2989+
QualType T = ResultType;
2990+
while (const auto *PT = dyn_cast<PointerType>(T.getTypePtr())) {
2991+
T = T->getPointeeType();
2992+
if (T.getQualifiers().hasAddressSpace())
2993+
return true;
2994+
}
2995+
return false;
2996+
};
2997+
2998+
if (getASTContext().getLangOpts().isCompatibleWithMSVC(
2999+
LangOptions::MSVC2019) &&
3000+
!UseClangMangling(ResultType)) {
3001+
if (D && !D->getPrimaryTemplate()) {
3002+
Out << '@';
3003+
} else {
3004+
mangleAutoReturnType(ResultType, QMM_Result);
3005+
}
3006+
} else {
3007+
Out << '?';
3008+
mangleQualifiers(ResultType.getLocalQualifiers(), /*IsMember=*/false);
3009+
Out << '?';
3010+
mangleSourceName(AT->isDecltypeAuto() ? "<decltype-auto>" : "<auto>");
3011+
Out << '@';
3012+
}
29213013
} else {
29223014
if (ResultType->isVoidType())
29233015
ResultType = ResultType.getUnqualifiedType();
@@ -4220,6 +4312,57 @@ void MicrosoftMangleContextImpl::mangleStringLiteral(const StringLiteral *SL,
42204312
Mangler.getStream() << '@';
42214313
}
42224314

4315+
void MicrosoftCXXNameMangler::mangleAutoReturnType(const MemberPointerType *T,
4316+
Qualifiers Quals) {
4317+
QualType PointeeType = T->getPointeeType();
4318+
manglePointerCVQualifiers(Quals);
4319+
manglePointerExtQualifiers(Quals, PointeeType);
4320+
if (const FunctionProtoType *FPT = PointeeType->getAs<FunctionProtoType>()) {
4321+
Out << '8';
4322+
mangleName(T->getClass()->castAs<RecordType>()->getDecl());
4323+
mangleFunctionType(FPT, nullptr, true);
4324+
} else {
4325+
mangleQualifiers(PointeeType.getQualifiers(), true);
4326+
mangleName(T->getClass()->castAs<RecordType>()->getDecl());
4327+
mangleAutoReturnType(PointeeType, QMM_Drop);
4328+
}
4329+
}
4330+
4331+
void MicrosoftCXXNameMangler::mangleAutoReturnType(const PointerType *T,
4332+
Qualifiers Quals) {
4333+
QualType PointeeType = T->getPointeeType();
4334+
assert(!PointeeType.getQualifiers().hasAddressSpace() &&
4335+
"Unexpected address space mangling required");
4336+
4337+
manglePointerCVQualifiers(Quals);
4338+
manglePointerExtQualifiers(Quals, PointeeType);
4339+
4340+
if (const FunctionProtoType *FPT = PointeeType->getAs<FunctionProtoType>()) {
4341+
Out << '6';
4342+
mangleFunctionType(FPT);
4343+
} else {
4344+
mangleAutoReturnType(PointeeType, QMM_Mangle);
4345+
}
4346+
}
4347+
4348+
void MicrosoftCXXNameMangler::mangleAutoReturnType(const LValueReferenceType *T,
4349+
Qualifiers Quals) {
4350+
QualType PointeeType = T->getPointeeType();
4351+
assert(!Quals.hasConst() && !Quals.hasVolatile() && "unexpected qualifier!");
4352+
Out << 'A';
4353+
manglePointerExtQualifiers(Quals, PointeeType);
4354+
mangleAutoReturnType(PointeeType, QMM_Mangle);
4355+
}
4356+
4357+
void MicrosoftCXXNameMangler::mangleAutoReturnType(const RValueReferenceType *T,
4358+
Qualifiers Quals) {
4359+
QualType PointeeType = T->getPointeeType();
4360+
assert(!Quals.hasConst() && !Quals.hasVolatile() && "unexpected qualifier!");
4361+
Out << "$$Q";
4362+
manglePointerExtQualifiers(Quals, PointeeType);
4363+
mangleAutoReturnType(PointeeType, QMM_Mangle);
4364+
}
4365+
42234366
MicrosoftMangleContext *MicrosoftMangleContext::create(ASTContext &Context,
42244367
DiagnosticsEngine &Diags,
42254368
bool IsAux) {

0 commit comments

Comments
 (0)