Skip to content

Commit

Permalink
Adds support to clang"s windows mangler to handle template argument v…
Browse files Browse the repository at this point in the history
…alues that are pointers one-past-the-end of a non-array symbol. Also improves error messages in other template argument scenarios where clang bails.
  • Loading branch information
memory-thrasher committed Jul 14, 2024
1 parent 2f55e55 commit d722361
Show file tree
Hide file tree
Showing 2 changed files with 68 additions and 15 deletions.
64 changes: 49 additions & 15 deletions clang/lib/AST/MicrosoftMangle.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -1922,11 +1922,19 @@ void MicrosoftCXXNameMangler::mangleTemplateArgValue(QualType T,
if (WithScalarType)
mangleType(T, SourceRange(), QMM_Escape);

// We don't know how to mangle past-the-end pointers yet.
if (V.isLValueOnePastTheEnd())
break;

APValue::LValueBase Base = V.getLValueBase();

// this might not cover every case but did cover issue 97756
// see test CodeGen/ms_mangler_templatearg_opte
if (V.isLValueOnePastTheEnd()) {
Out << "5E";
auto *VD = Base.dyn_cast<const ValueDecl *>();
if (VD)
mangle(VD);
Out << "@";
return;
}

if (!V.hasLValuePath() || V.getLValuePath().empty()) {
// Taking the address of a complete object has a special-case mangling.
if (Base.isNull()) {
Expand All @@ -1938,12 +1946,23 @@ void MicrosoftCXXNameMangler::mangleTemplateArgValue(QualType T,
mangleNumber(V.getLValueOffset().getQuantity());
} else if (!V.hasLValuePath()) {
// FIXME: This can only happen as an extension. Invent a mangling.
break;
DiagnosticsEngine &Diags = Context.getDiags();
unsigned DiagID =
Diags.getCustomDiagID(DiagnosticsEngine::Error,
"cannot mangle this template argument yet "
"(extension not comaptible with ms mangler)");
Diags.Report(DiagID);
return;
} else if (auto *VD = Base.dyn_cast<const ValueDecl*>()) {
Out << "E";
mangle(VD);
} else {
break;
DiagnosticsEngine &Diags = Context.getDiags();
unsigned DiagID = Diags.getCustomDiagID(
DiagnosticsEngine::Error,
"cannot mangle this template argument yet (undeclared base)");
Diags.Report(DiagID);
return;
}
} else {
if (TAK == TplArgKind::ClassNTTP && T->isPointerType())
Expand Down Expand Up @@ -1988,8 +2007,14 @@ void MicrosoftCXXNameMangler::mangleTemplateArgValue(QualType T,
Out << *I;

auto *VD = Base.dyn_cast<const ValueDecl*>();
if (!VD)
break;
if (!VD) {
DiagnosticsEngine &Diags = Context.getDiags();
unsigned DiagID = Diags.getCustomDiagID(
DiagnosticsEngine::Error,
"cannot mangle this template argument yet (null value decl)");
Diags.Report(DiagID);
return;
}
Out << (TAK == TplArgKind::ClassNTTP ? 'E' : '1');
mangle(VD);

Expand Down Expand Up @@ -2104,15 +2129,24 @@ void MicrosoftCXXNameMangler::mangleTemplateArgValue(QualType T,
return;
}

case APValue::AddrLabelDiff:
case APValue::FixedPoint:
break;
case APValue::AddrLabelDiff: {
DiagnosticsEngine &Diags = Context.getDiags();
unsigned DiagID = Diags.getCustomDiagID(
DiagnosticsEngine::Error, "cannot mangle this template argument yet "
"(value type: address label diff)");
Diags.Report(DiagID);
return;
}

DiagnosticsEngine &Diags = Context.getDiags();
unsigned DiagID = Diags.getCustomDiagID(
DiagnosticsEngine::Error, "cannot mangle this template argument yet");
Diags.Report(DiagID);
case APValue::FixedPoint: {
DiagnosticsEngine &Diags = Context.getDiags();
unsigned DiagID = Diags.getCustomDiagID(
DiagnosticsEngine::Error,
"cannot mangle this template argument yet (value type: fixed point)");
Diags.Report(DiagID);
return;
}
}
}

void MicrosoftCXXNameMangler::mangleObjCProtocol(const ObjCProtocolDecl *PD) {
Expand Down
19 changes: 19 additions & 0 deletions clang/test/CodeGen/ms_mangler_templatearg_opte.cpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,19 @@
// RUN: %clang_cc1 -triple x86_64-pc-windows-msvc -emit-llvm -std=c++20 -x c++ < %s | FileCheck -check-prefix=WIN64 %s

struct A {
const int* ptr;
};

template<A> void tfn() {};

// WIN64: ??$tfn@$2UA@@PEBH5CE?ints@@3QBHB06@@@@@YAXXZ
constexpr int ints[] = { 1, 2, 7, 8, 9, -17, -10 }; //Note: this does NOT break the unpatched mangler

// WIN64: ??$tfn@$2UA@@PEBH5E?one_int@@3HB@@@@YAXXZ
constexpr int one_int = 7;

void template_instance() {
tfn<A{ints + sizeof(ints)/sizeof(int)}>();
tfn<A{&one_int + 1}>();
}

0 comments on commit d722361

Please sign in to comment.