-
Notifications
You must be signed in to change notification settings - Fork 13.6k
[Clang] [AST] Fix placeholder return type name mangling for MSVC 1920+ / VS2019+ #102848
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
Merged
MaxEW707
merged 5 commits into
llvm:main
from
MaxEW707:mew/fix-placeholder-return-type-mangling-msvc-1920
Aug 15, 2024
Merged
Changes from all commits
Commits
Show all changes
5 commits
Select commit
Hold shift + click to select a range
e5071bd
Fix placeholder return type name mangling for MSVC 1920+
MaxEW707 c316645
formatting
MaxEW707 13f1086
add missing address space test
MaxEW707 9ce68b9
Remove unused SourceRange argument
MaxEW707 55b8eb3
Update release notes
MaxEW707 File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -408,6 +408,8 @@ class MicrosoftCXXNameMangler { | |
void mangleSourceName(StringRef Name); | ||
void mangleNestedName(GlobalDecl GD); | ||
|
||
void mangleAutoReturnType(QualType T, QualifierMangleMode QMM); | ||
|
||
private: | ||
bool isStructorDecl(const NamedDecl *ND) const { | ||
return ND == Structor || getStructor(ND) == Structor; | ||
|
@@ -477,6 +479,11 @@ class MicrosoftCXXNameMangler { | |
SourceRange Range); | ||
void mangleObjCKindOfType(const ObjCObjectType *T, Qualifiers Quals, | ||
SourceRange Range); | ||
|
||
void mangleAutoReturnType(const MemberPointerType *T, Qualifiers Quals); | ||
void mangleAutoReturnType(const PointerType *T, Qualifiers Quals); | ||
void mangleAutoReturnType(const LValueReferenceType *T, Qualifiers Quals); | ||
void mangleAutoReturnType(const RValueReferenceType *T, Qualifiers Quals); | ||
}; | ||
} | ||
|
||
|
@@ -2494,6 +2501,57 @@ void MicrosoftCXXNameMangler::mangleAddressSpaceType(QualType T, | |
mangleArtificialTagType(TagTypeKind::Struct, ASMangling, {"__clang"}); | ||
} | ||
|
||
void MicrosoftCXXNameMangler::mangleAutoReturnType(QualType T, | ||
QualifierMangleMode QMM) { | ||
assert(getASTContext().getLangOpts().isCompatibleWithMSVC( | ||
LangOptions::MSVC2019) && | ||
"Cannot mangle MSVC 2017 auto return types!"); | ||
|
||
if (isa<AutoType>(T)) { | ||
const auto *AT = T->getContainedAutoType(); | ||
Qualifiers Quals = T.getLocalQualifiers(); | ||
|
||
if (QMM == QMM_Result) | ||
Out << '?'; | ||
if (QMM != QMM_Drop) | ||
mangleQualifiers(Quals, false); | ||
Out << (AT->isDecltypeAuto() ? "_T" : "_P"); | ||
return; | ||
} | ||
|
||
T = T.getDesugaredType(getASTContext()); | ||
Qualifiers Quals = T.getLocalQualifiers(); | ||
|
||
switch (QMM) { | ||
case QMM_Drop: | ||
case QMM_Result: | ||
break; | ||
case QMM_Mangle: | ||
mangleQualifiers(Quals, false); | ||
break; | ||
default: | ||
llvm_unreachable("QMM_Escape unexpected"); | ||
} | ||
|
||
const Type *ty = T.getTypePtr(); | ||
switch (ty->getTypeClass()) { | ||
case Type::MemberPointer: | ||
mangleAutoReturnType(cast<MemberPointerType>(ty), Quals); | ||
break; | ||
case Type::Pointer: | ||
mangleAutoReturnType(cast<PointerType>(ty), Quals); | ||
break; | ||
case Type::LValueReference: | ||
mangleAutoReturnType(cast<LValueReferenceType>(ty), Quals); | ||
break; | ||
case Type::RValueReference: | ||
mangleAutoReturnType(cast<RValueReferenceType>(ty), Quals); | ||
break; | ||
default: | ||
llvm_unreachable("Invalid type expected"); | ||
} | ||
} | ||
|
||
void MicrosoftCXXNameMangler::mangleType(QualType T, SourceRange Range, | ||
QualifierMangleMode QMM) { | ||
// Don't use the canonical types. MSVC includes things like 'const' on | ||
|
@@ -2900,17 +2958,51 @@ void MicrosoftCXXNameMangler::mangleFunctionType(const FunctionType *T, | |
// can differ by their calling convention and are typically deduced. So | ||
// we make sure that this type gets mangled properly. | ||
mangleType(ResultType, Range, QMM_Result); | ||
} else if (const auto *AT = dyn_cast_or_null<AutoType>( | ||
ResultType->getContainedAutoType())) { | ||
Out << '?'; | ||
mangleQualifiers(ResultType.getLocalQualifiers(), /*IsMember=*/false); | ||
Out << '?'; | ||
} else if (IsInLambda) { | ||
if (const auto *AT = ResultType->getContainedAutoType()) { | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. AT is used only in +Asserts |
||
assert(AT->getKeyword() == AutoTypeKeyword::Auto && | ||
"should only need to mangle auto!"); | ||
Out << '?'; | ||
mangleQualifiers(ResultType.getLocalQualifiers(), /*IsMember=*/false); | ||
Out << '?'; | ||
mangleSourceName("<auto>"); | ||
Out << '@'; | ||
} else { | ||
Out << '@'; | ||
} | ||
} else if (const auto *AT = ResultType->getContainedAutoType()) { | ||
assert(AT->getKeyword() != AutoTypeKeyword::GNUAutoType && | ||
"shouldn't need to mangle __auto_type!"); | ||
mangleSourceName(AT->isDecltypeAuto() ? "<decltype-auto>" : "<auto>"); | ||
Out << '@'; | ||
} else if (IsInLambda) { | ||
Out << '@'; | ||
|
||
// If we have any pointer types with the clang address space extension | ||
// then defer to the custom clang mangling to keep backwards | ||
// compatibility. See `mangleType(const PointerType *T, Qualifiers Quals, | ||
// SourceRange Range)` for details. | ||
auto UseClangMangling = [](QualType ResultType) { | ||
QualType T = ResultType; | ||
while (const auto *PT = dyn_cast<PointerType>(T.getTypePtr())) { | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. PT is not used. |
||
T = T->getPointeeType(); | ||
if (T.getQualifiers().hasAddressSpace()) | ||
return true; | ||
} | ||
return false; | ||
}; | ||
|
||
if (getASTContext().getLangOpts().isCompatibleWithMSVC( | ||
LangOptions::MSVC2019) && | ||
!UseClangMangling(ResultType)) { | ||
if (D && !D->getPrimaryTemplate()) { | ||
Out << '@'; | ||
} else { | ||
mangleAutoReturnType(ResultType, QMM_Result); | ||
} | ||
} else { | ||
Out << '?'; | ||
mangleQualifiers(ResultType.getLocalQualifiers(), /*IsMember=*/false); | ||
Out << '?'; | ||
mangleSourceName(AT->isDecltypeAuto() ? "<decltype-auto>" : "<auto>"); | ||
Out << '@'; | ||
} | ||
} else { | ||
if (ResultType->isVoidType()) | ||
ResultType = ResultType.getUnqualifiedType(); | ||
|
@@ -4213,6 +4305,57 @@ void MicrosoftMangleContextImpl::mangleStringLiteral(const StringLiteral *SL, | |
Mangler.getStream() << '@'; | ||
} | ||
|
||
void MicrosoftCXXNameMangler::mangleAutoReturnType(const MemberPointerType *T, | ||
Qualifiers Quals) { | ||
QualType PointeeType = T->getPointeeType(); | ||
manglePointerCVQualifiers(Quals); | ||
manglePointerExtQualifiers(Quals, PointeeType); | ||
if (const FunctionProtoType *FPT = PointeeType->getAs<FunctionProtoType>()) { | ||
Out << '8'; | ||
mangleName(T->getClass()->castAs<RecordType>()->getDecl()); | ||
mangleFunctionType(FPT, nullptr, true); | ||
} else { | ||
mangleQualifiers(PointeeType.getQualifiers(), true); | ||
mangleName(T->getClass()->castAs<RecordType>()->getDecl()); | ||
mangleAutoReturnType(PointeeType, QMM_Drop); | ||
} | ||
} | ||
|
||
void MicrosoftCXXNameMangler::mangleAutoReturnType(const PointerType *T, | ||
Qualifiers Quals) { | ||
QualType PointeeType = T->getPointeeType(); | ||
assert(!PointeeType.getQualifiers().hasAddressSpace() && | ||
"Unexpected address space mangling required"); | ||
|
||
manglePointerCVQualifiers(Quals); | ||
manglePointerExtQualifiers(Quals, PointeeType); | ||
|
||
if (const FunctionProtoType *FPT = PointeeType->getAs<FunctionProtoType>()) { | ||
Out << '6'; | ||
mangleFunctionType(FPT); | ||
} else { | ||
mangleAutoReturnType(PointeeType, QMM_Mangle); | ||
} | ||
} | ||
|
||
void MicrosoftCXXNameMangler::mangleAutoReturnType(const LValueReferenceType *T, | ||
Qualifiers Quals) { | ||
QualType PointeeType = T->getPointeeType(); | ||
assert(!Quals.hasConst() && !Quals.hasVolatile() && "unexpected qualifier!"); | ||
Out << 'A'; | ||
manglePointerExtQualifiers(Quals, PointeeType); | ||
mangleAutoReturnType(PointeeType, QMM_Mangle); | ||
} | ||
|
||
void MicrosoftCXXNameMangler::mangleAutoReturnType(const RValueReferenceType *T, | ||
Qualifiers Quals) { | ||
QualType PointeeType = T->getPointeeType(); | ||
assert(!Quals.hasConst() && !Quals.hasVolatile() && "unexpected qualifier!"); | ||
Out << "$$Q"; | ||
manglePointerExtQualifiers(Quals, PointeeType); | ||
mangleAutoReturnType(PointeeType, QMM_Mangle); | ||
} | ||
|
||
MicrosoftMangleContext *MicrosoftMangleContext::create(ASTContext &Context, | ||
DiagnosticsEngine &Diags, | ||
bool IsAux) { | ||
|
Oops, something went wrong.
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Uh oh!
There was an error while loading. Please reload this page.