Skip to content

Commit f0b53c5

Browse files
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.
1 parent 569814e commit f0b53c5

File tree

2 files changed

+96
-131
lines changed

2 files changed

+96
-131
lines changed

clang/lib/AST/MicrosoftMangle.cpp

Lines changed: 77 additions & 131 deletions
Original file line numberDiff line numberDiff line change
@@ -337,6 +337,9 @@ class MicrosoftCXXNameMangler {
337337

338338
const bool PointersAre64Bit;
339339

340+
DiagnosticBuilder Error(SourceLocation, StringRef);
341+
DiagnosticBuilder Error(StringRef);
342+
340343
public:
341344
enum QualifierMangleMode { QMM_Drop, QMM_Mangle, QMM_Escape, QMM_Result };
342345
enum class TplArgKind { ClassNTTP, StructuralValue };
@@ -564,6 +567,23 @@ MicrosoftMangleContextImpl::shouldMangleStringLiteral(const StringLiteral *SL) {
564567
return true;
565568
}
566569

570+
DiagnosticBuilder MicrosoftCXXNameMangler::Error(SourceLocation loc,
571+
StringRef thingy) {
572+
DiagnosticsEngine &Diags = Context.getDiags();
573+
// extra placeholders are ignored quietly when not used
574+
unsigned DiagID = Diags.getCustomDiagID(
575+
DiagnosticsEngine::Error, "cannot mangle this %0%1%2%3%4 yet");
576+
return Diags.Report(loc, DiagID) << thingy;
577+
}
578+
579+
DiagnosticBuilder MicrosoftCXXNameMangler::Error(StringRef thingy) {
580+
DiagnosticsEngine &Diags = Context.getDiags();
581+
// extra placeholders are ignored quietly when not used
582+
unsigned DiagID = Diags.getCustomDiagID(
583+
DiagnosticsEngine::Error, "cannot mangle this %0%1%2%3%4 yet");
584+
return Diags.Report(DiagID) << thingy;
585+
}
586+
567587
void MicrosoftCXXNameMangler::mangle(GlobalDecl GD, StringRef Prefix) {
568588
const NamedDecl *D = cast<NamedDecl>(GD.getDecl());
569589
// MSVC doesn't mangle C++ names the same way it mangles extern "C" names.
@@ -1578,10 +1598,7 @@ void MicrosoftCXXNameMangler::mangleOperatorName(OverloadedOperatorKind OO,
15781598
case OO_Spaceship: Out << "?__M"; break;
15791599

15801600
case OO_Conditional: {
1581-
DiagnosticsEngine &Diags = Context.getDiags();
1582-
unsigned DiagID = Diags.getCustomDiagID(DiagnosticsEngine::Error,
1583-
"cannot mangle this conditional operator yet");
1584-
Diags.Report(Loc, DiagID);
1601+
Error(Loc, "conditional operator");
15851602
break;
15861603
}
15871604

@@ -1673,11 +1690,8 @@ void MicrosoftCXXNameMangler::mangleExpression(
16731690
}
16741691

16751692
// As bad as this diagnostic is, it's better than crashing.
1676-
DiagnosticsEngine &Diags = Context.getDiags();
1677-
unsigned DiagID = Diags.getCustomDiagID(
1678-
DiagnosticsEngine::Error, "cannot yet mangle expression type %0");
1679-
Diags.Report(E->getExprLoc(), DiagID) << E->getStmtClassName()
1680-
<< E->getSourceRange();
1693+
Error(E->getExprLoc(), "expression type: ")
1694+
<< E->getStmtClassName() << E->getSourceRange();
16811695
}
16821696

16831697
void MicrosoftCXXNameMangler::mangleTemplateArgs(
@@ -1923,11 +1937,19 @@ void MicrosoftCXXNameMangler::mangleTemplateArgValue(QualType T,
19231937
if (WithScalarType)
19241938
mangleType(T, SourceRange(), QMM_Escape);
19251939

1926-
// We don't know how to mangle past-the-end pointers yet.
1927-
if (V.isLValueOnePastTheEnd())
1928-
break;
1929-
19301940
APValue::LValueBase Base = V.getLValueBase();
1941+
1942+
// this might not cover every case but did cover issue 97756
1943+
// see test CodeGen/ms_mangler_templatearg_opte
1944+
if (V.isLValueOnePastTheEnd()) {
1945+
Out << "5E";
1946+
auto *VD = Base.dyn_cast<const ValueDecl *>();
1947+
if (VD)
1948+
mangle(VD);
1949+
Out << "@";
1950+
return;
1951+
}
1952+
19311953
if (!V.hasLValuePath() || V.getLValuePath().empty()) {
19321954
// Taking the address of a complete object has a special-case mangling.
19331955
if (Base.isNull()) {
@@ -1939,12 +1961,14 @@ void MicrosoftCXXNameMangler::mangleTemplateArgValue(QualType T,
19391961
mangleNumber(V.getLValueOffset().getQuantity());
19401962
} else if (!V.hasLValuePath()) {
19411963
// FIXME: This can only happen as an extension. Invent a mangling.
1942-
break;
1964+
Error("template argument (extension not comaptible with ms mangler)");
1965+
return;
19431966
} else if (auto *VD = Base.dyn_cast<const ValueDecl*>()) {
19441967
Out << "E";
19451968
mangle(VD);
19461969
} else {
1947-
break;
1970+
Error("template argument (undeclared base)");
1971+
return;
19481972
}
19491973
} else {
19501974
if (TAK == TplArgKind::ClassNTTP && T->isPointerType())
@@ -1989,8 +2013,10 @@ void MicrosoftCXXNameMangler::mangleTemplateArgValue(QualType T,
19892013
Out << *I;
19902014

19912015
auto *VD = Base.dyn_cast<const ValueDecl*>();
1992-
if (!VD)
1993-
break;
2016+
if (!VD) {
2017+
Error("template argument (null value decl)");
2018+
return;
2019+
}
19942020
Out << (TAK == TplArgKind::ClassNTTP ? 'E' : '1');
19952021
mangle(VD);
19962022

@@ -2105,15 +2131,16 @@ void MicrosoftCXXNameMangler::mangleTemplateArgValue(QualType T,
21052131
return;
21062132
}
21072133

2108-
case APValue::AddrLabelDiff:
2109-
case APValue::FixedPoint:
2110-
break;
2134+
case APValue::AddrLabelDiff: {
2135+
Error("template argument (value type: address label diff)");
2136+
return;
21112137
}
21122138

2113-
DiagnosticsEngine &Diags = Context.getDiags();
2114-
unsigned DiagID = Diags.getCustomDiagID(
2115-
DiagnosticsEngine::Error, "cannot mangle this template argument yet");
2116-
Diags.Report(DiagID);
2139+
case APValue::FixedPoint: {
2140+
Error("template argument (value type: fixed point)");
2141+
return;
2142+
}
2143+
}
21172144
}
21182145

21192146
void MicrosoftCXXNameMangler::mangleObjCProtocol(const ObjCProtocolDecl *PD) {
@@ -2740,11 +2767,9 @@ void MicrosoftCXXNameMangler::mangleType(const BuiltinType *T, Qualifiers,
27402767
case BuiltinType::SatULongFract:
27412768
case BuiltinType::Ibm128:
27422769
case BuiltinType::Float128: {
2743-
DiagnosticsEngine &Diags = Context.getDiags();
2744-
unsigned DiagID = Diags.getCustomDiagID(
2745-
DiagnosticsEngine::Error, "cannot mangle this built-in %0 type yet");
2746-
Diags.Report(Range.getBegin(), DiagID)
2747-
<< T->getName(Context.getASTContext().getPrintingPolicy()) << Range;
2770+
Error(Range.getBegin(), "built-in ")
2771+
<< T->getName(Context.getASTContext().getPrintingPolicy()) << " type"
2772+
<< Range;
27482773
break;
27492774
}
27502775
}
@@ -3062,10 +3087,7 @@ void MicrosoftCXXNameMangler::mangleCallingConvention(CallingConv CC,
30623087
return;
30633088
}
30643089

3065-
DiagnosticsEngine &Diags = Context.getDiags();
3066-
unsigned DiagID = Diags.getCustomDiagID(
3067-
DiagnosticsEngine::Error, "cannot mangle this calling convention yet");
3068-
Diags.Report(Range.getBegin(), DiagID) << Range;
3090+
Error(Range.getBegin(), "calling convention") << Range;
30693091
}
30703092
void MicrosoftCXXNameMangler::mangleCallingConvention(const FunctionType *T,
30713093
SourceRange Range) {
@@ -3086,11 +3108,7 @@ void MicrosoftCXXNameMangler::mangleType(const UnresolvedUsingType *T,
30863108
Qualifiers, SourceRange Range) {
30873109
// Probably should be mangled as a template instantiation; need to see what
30883110
// VC does first.
3089-
DiagnosticsEngine &Diags = Context.getDiags();
3090-
unsigned DiagID = Diags.getCustomDiagID(DiagnosticsEngine::Error,
3091-
"cannot mangle this unresolved dependent type yet");
3092-
Diags.Report(Range.getBegin(), DiagID)
3093-
<< Range;
3111+
Error(Range.getBegin(), "unresolved dependent type") << Range;
30943112
}
30953113

30963114
// <type> ::= <union-type> | <struct-type> | <class-type> | <enum-type>
@@ -3197,11 +3215,8 @@ void MicrosoftCXXNameMangler::mangleArrayType(const ArrayType *T) {
31973215
// The dependent expression has to be folded into a constant (TODO).
31983216
const DependentSizedArrayType *DSAT =
31993217
getASTContext().getAsDependentSizedArrayType(ElementTy);
3200-
DiagnosticsEngine &Diags = Context.getDiags();
3201-
unsigned DiagID = Diags.getCustomDiagID(DiagnosticsEngine::Error,
3202-
"cannot mangle this dependent-length array yet");
3203-
Diags.Report(DSAT->getSizeExpr()->getExprLoc(), DiagID)
3204-
<< DSAT->getBracketsRange();
3218+
Error(DSAT->getSizeExpr()->getExprLoc(), "dependent-length")
3219+
<< DSAT->getBracketsRange();
32053220
return;
32063221
} else {
32073222
break;
@@ -3241,20 +3256,12 @@ void MicrosoftCXXNameMangler::mangleType(const MemberPointerType *T,
32413256

32423257
void MicrosoftCXXNameMangler::mangleType(const TemplateTypeParmType *T,
32433258
Qualifiers, SourceRange Range) {
3244-
DiagnosticsEngine &Diags = Context.getDiags();
3245-
unsigned DiagID = Diags.getCustomDiagID(DiagnosticsEngine::Error,
3246-
"cannot mangle this template type parameter type yet");
3247-
Diags.Report(Range.getBegin(), DiagID)
3248-
<< Range;
3259+
Error(Range.getBegin(), "template type parameter type") << Range;
32493260
}
32503261

32513262
void MicrosoftCXXNameMangler::mangleType(const SubstTemplateTypeParmPackType *T,
32523263
Qualifiers, SourceRange Range) {
3253-
DiagnosticsEngine &Diags = Context.getDiags();
3254-
unsigned DiagID = Diags.getCustomDiagID(DiagnosticsEngine::Error,
3255-
"cannot mangle this substituted parameter pack yet");
3256-
Diags.Report(Range.getBegin(), DiagID)
3257-
<< Range;
3264+
Error(Range.getBegin(), "substituted parameter pack") << Range;
32583265
}
32593266

32603267
// <type> ::= <pointer-type>
@@ -3405,46 +3412,27 @@ void MicrosoftCXXNameMangler::mangleType(const ExtVectorType *T,
34053412

34063413
void MicrosoftCXXNameMangler::mangleType(const DependentVectorType *T,
34073414
Qualifiers, SourceRange Range) {
3408-
DiagnosticsEngine &Diags = Context.getDiags();
3409-
unsigned DiagID = Diags.getCustomDiagID(
3410-
DiagnosticsEngine::Error,
3411-
"cannot mangle this dependent-sized vector type yet");
3412-
Diags.Report(Range.getBegin(), DiagID) << Range;
3415+
Error(Range.getBegin(), "dependent-sized vector type") << Range;
34133416
}
34143417

34153418
void MicrosoftCXXNameMangler::mangleType(const DependentSizedExtVectorType *T,
34163419
Qualifiers, SourceRange Range) {
3417-
DiagnosticsEngine &Diags = Context.getDiags();
3418-
unsigned DiagID = Diags.getCustomDiagID(DiagnosticsEngine::Error,
3419-
"cannot mangle this dependent-sized extended vector type yet");
3420-
Diags.Report(Range.getBegin(), DiagID)
3421-
<< Range;
3420+
Error(Range.getBegin(), "dependent-sized extended vector type") << Range;
34223421
}
34233422

34243423
void MicrosoftCXXNameMangler::mangleType(const ConstantMatrixType *T,
34253424
Qualifiers quals, SourceRange Range) {
3426-
DiagnosticsEngine &Diags = Context.getDiags();
3427-
unsigned DiagID = Diags.getCustomDiagID(DiagnosticsEngine::Error,
3428-
"Cannot mangle this matrix type yet");
3429-
Diags.Report(Range.getBegin(), DiagID) << Range;
3425+
Error(Range.getBegin(), "matrix type") << Range;
34303426
}
34313427

34323428
void MicrosoftCXXNameMangler::mangleType(const DependentSizedMatrixType *T,
34333429
Qualifiers quals, SourceRange Range) {
3434-
DiagnosticsEngine &Diags = Context.getDiags();
3435-
unsigned DiagID = Diags.getCustomDiagID(
3436-
DiagnosticsEngine::Error,
3437-
"Cannot mangle this dependent-sized matrix type yet");
3438-
Diags.Report(Range.getBegin(), DiagID) << Range;
3430+
Error(Range.getBegin(), "dependent-sized matrix type") << Range;
34393431
}
34403432

34413433
void MicrosoftCXXNameMangler::mangleType(const DependentAddressSpaceType *T,
34423434
Qualifiers, SourceRange Range) {
3443-
DiagnosticsEngine &Diags = Context.getDiags();
3444-
unsigned DiagID = Diags.getCustomDiagID(
3445-
DiagnosticsEngine::Error,
3446-
"cannot mangle this dependent address space type yet");
3447-
Diags.Report(Range.getBegin(), DiagID) << Range;
3435+
Error(Range.getBegin(), "dependent address space type") << Range;
34483436
}
34493437

34503438
void MicrosoftCXXNameMangler::mangleType(const ObjCInterfaceType *T, Qualifiers,
@@ -3514,39 +3502,23 @@ void MicrosoftCXXNameMangler::mangleType(const InjectedClassNameType *,
35143502

35153503
void MicrosoftCXXNameMangler::mangleType(const TemplateSpecializationType *T,
35163504
Qualifiers, SourceRange Range) {
3517-
DiagnosticsEngine &Diags = Context.getDiags();
3518-
unsigned DiagID = Diags.getCustomDiagID(DiagnosticsEngine::Error,
3519-
"cannot mangle this template specialization type yet");
3520-
Diags.Report(Range.getBegin(), DiagID)
3521-
<< Range;
3505+
Error(Range.getBegin(), "template specialization type") << Range;
35223506
}
35233507

35243508
void MicrosoftCXXNameMangler::mangleType(const DependentNameType *T, Qualifiers,
35253509
SourceRange Range) {
3526-
DiagnosticsEngine &Diags = Context.getDiags();
3527-
unsigned DiagID = Diags.getCustomDiagID(DiagnosticsEngine::Error,
3528-
"cannot mangle this dependent name type yet");
3529-
Diags.Report(Range.getBegin(), DiagID)
3530-
<< Range;
3510+
Error(Range.getBegin(), "dependent name type") << Range;
35313511
}
35323512

35333513
void MicrosoftCXXNameMangler::mangleType(
35343514
const DependentTemplateSpecializationType *T, Qualifiers,
35353515
SourceRange Range) {
3536-
DiagnosticsEngine &Diags = Context.getDiags();
3537-
unsigned DiagID = Diags.getCustomDiagID(DiagnosticsEngine::Error,
3538-
"cannot mangle this dependent template specialization type yet");
3539-
Diags.Report(Range.getBegin(), DiagID)
3540-
<< Range;
3516+
Error(Range.getBegin(), "dependent template specialization type") << Range;
35413517
}
35423518

35433519
void MicrosoftCXXNameMangler::mangleType(const PackExpansionType *T, Qualifiers,
35443520
SourceRange Range) {
3545-
DiagnosticsEngine &Diags = Context.getDiags();
3546-
unsigned DiagID = Diags.getCustomDiagID(DiagnosticsEngine::Error,
3547-
"cannot mangle this pack expansion yet");
3548-
Diags.Report(Range.getBegin(), DiagID)
3549-
<< Range;
3521+
Error(Range.getBegin(), "pack expansion") << Range;
35503522
}
35513523

35523524
void MicrosoftCXXNameMangler::mangleType(const PackIndexingType *T,
@@ -3557,60 +3529,37 @@ void MicrosoftCXXNameMangler::mangleType(const PackIndexingType *T,
35573529

35583530
void MicrosoftCXXNameMangler::mangleType(const TypeOfType *T, Qualifiers,
35593531
SourceRange Range) {
3560-
DiagnosticsEngine &Diags = Context.getDiags();
3561-
unsigned DiagID = Diags.getCustomDiagID(DiagnosticsEngine::Error,
3562-
"cannot mangle this typeof(type) yet");
3563-
Diags.Report(Range.getBegin(), DiagID)
3564-
<< Range;
3532+
Error(Range.getBegin(), "typeof(type)") << Range;
35653533
}
35663534

35673535
void MicrosoftCXXNameMangler::mangleType(const TypeOfExprType *T, Qualifiers,
35683536
SourceRange Range) {
3569-
DiagnosticsEngine &Diags = Context.getDiags();
3570-
unsigned DiagID = Diags.getCustomDiagID(DiagnosticsEngine::Error,
3571-
"cannot mangle this typeof(expression) yet");
3572-
Diags.Report(Range.getBegin(), DiagID)
3573-
<< Range;
3537+
Error(Range.getBegin(), "typeof(expression)") << Range;
35743538
}
35753539

35763540
void MicrosoftCXXNameMangler::mangleType(const DecltypeType *T, Qualifiers,
35773541
SourceRange Range) {
3578-
DiagnosticsEngine &Diags = Context.getDiags();
3579-
unsigned DiagID = Diags.getCustomDiagID(DiagnosticsEngine::Error,
3580-
"cannot mangle this decltype() yet");
3581-
Diags.Report(Range.getBegin(), DiagID)
3582-
<< Range;
3542+
Error(Range.getBegin(), "decltype()") << Range;
35833543
}
35843544

35853545
void MicrosoftCXXNameMangler::mangleType(const UnaryTransformType *T,
35863546
Qualifiers, SourceRange Range) {
3587-
DiagnosticsEngine &Diags = Context.getDiags();
3588-
unsigned DiagID = Diags.getCustomDiagID(DiagnosticsEngine::Error,
3589-
"cannot mangle this unary transform type yet");
3590-
Diags.Report(Range.getBegin(), DiagID)
3591-
<< Range;
3547+
Error(Range.getBegin(), "unary transform type") << Range;
35923548
}
35933549

35943550
void MicrosoftCXXNameMangler::mangleType(const AutoType *T, Qualifiers,
35953551
SourceRange Range) {
35963552
assert(T->getDeducedType().isNull() && "expecting a dependent type!");
35973553

3598-
DiagnosticsEngine &Diags = Context.getDiags();
3599-
unsigned DiagID = Diags.getCustomDiagID(DiagnosticsEngine::Error,
3600-
"cannot mangle this 'auto' type yet");
3601-
Diags.Report(Range.getBegin(), DiagID)
3602-
<< Range;
3554+
Error(Range.getBegin(), "'auto' type") << Range;
36033555
}
36043556

36053557
void MicrosoftCXXNameMangler::mangleType(
36063558
const DeducedTemplateSpecializationType *T, Qualifiers, SourceRange Range) {
36073559
assert(T->getDeducedType().isNull() && "expecting a dependent type!");
36083560

3609-
DiagnosticsEngine &Diags = Context.getDiags();
3610-
unsigned DiagID = Diags.getCustomDiagID(DiagnosticsEngine::Error,
3611-
"cannot mangle this deduced class template specialization type yet");
3612-
Diags.Report(Range.getBegin(), DiagID)
3613-
<< Range;
3561+
Error(Range.getBegin(), "deduced class template specialization type")
3562+
<< Range;
36143563
}
36153564

36163565
void MicrosoftCXXNameMangler::mangleType(const AtomicType *T, Qualifiers,
@@ -3684,10 +3633,7 @@ void MicrosoftCXXNameMangler::mangleType(const BitIntType *T, Qualifiers,
36843633

36853634
void MicrosoftCXXNameMangler::mangleType(const DependentBitIntType *T,
36863635
Qualifiers, SourceRange Range) {
3687-
DiagnosticsEngine &Diags = Context.getDiags();
3688-
unsigned DiagID = Diags.getCustomDiagID(
3689-
DiagnosticsEngine::Error, "cannot mangle this DependentBitInt type yet");
3690-
Diags.Report(Range.getBegin(), DiagID) << Range;
3636+
Error(Range.getBegin(), "DependentBitInt type") << Range;
36913637
}
36923638

36933639
// <this-adjustment> ::= <no-adjustment> | <static-adjustment> |

0 commit comments

Comments
 (0)