https://github.com/memory-thrasher updated https://github.com/llvm/llvm-project/pull/97792
>From d47a70e4715a3f6f4740570877799d3bfbb8a713 Mon Sep 17 00:00:00 2001 From: Sidney Kelley <memory.thras...@gmail.com> Date: Thu, 4 Jul 2024 23:03:16 -0700 Subject: [PATCH] Adds support to clang"s windows mangler to handle template argument values that are pointers one-past-the-end of a non-array symbol. Also improves error messages in other template argument scenarios where clang bails. --- clang/lib/AST/MicrosoftMangle.cpp | 64 ++++++++++++++----- .../CodeGen/ms_mangler_templatearg_opte.cpp | 19 ++++++ 2 files changed, 68 insertions(+), 15 deletions(-) create mode 100644 clang/test/CodeGen/ms_mangler_templatearg_opte.cpp diff --git a/clang/lib/AST/MicrosoftMangle.cpp b/clang/lib/AST/MicrosoftMangle.cpp index fac14ce1dce8c..7f0c012a61a4b 100644 --- a/clang/lib/AST/MicrosoftMangle.cpp +++ b/clang/lib/AST/MicrosoftMangle.cpp @@ -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()) { @@ -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()) @@ -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); @@ -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) { diff --git a/clang/test/CodeGen/ms_mangler_templatearg_opte.cpp b/clang/test/CodeGen/ms_mangler_templatearg_opte.cpp new file mode 100644 index 0000000000000..ce6b620de3c11 --- /dev/null +++ b/clang/test/CodeGen/ms_mangler_templatearg_opte.cpp @@ -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}>(); +} + _______________________________________________ cfe-commits mailing list cfe-commits@lists.llvm.org https://lists.llvm.org/cgi-bin/mailman/listinfo/cfe-commits