Skip to content

Commit 81095a3

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 81095a3

File tree

2 files changed

+105
-131
lines changed

2 files changed

+105
-131
lines changed

clang/lib/AST/MicrosoftMangle.cpp

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

338338
const bool PointersAre64Bit;
339339

340+
DiagnosticBuilder Error(SourceLocation, StringRef, StringRef);
341+
DiagnosticBuilder Error(SourceLocation, StringRef);
342+
DiagnosticBuilder Error(StringRef);
343+
340344
public:
341345
enum QualifierMangleMode { QMM_Drop, QMM_Mangle, QMM_Escape, QMM_Result };
342346
enum class TplArgKind { ClassNTTP, StructuralValue };
@@ -564,6 +568,31 @@ MicrosoftMangleContextImpl::shouldMangleStringLiteral(const StringLiteral *SL) {
564568
return true;
565569
}
566570

571+
DiagnosticBuilder MicrosoftCXXNameMangler::Error(SourceLocation loc,
572+
StringRef thing1,
573+
StringRef thing2) {
574+
DiagnosticsEngine &Diags = Context.getDiags();
575+
unsigned DiagID = Diags.getCustomDiagID(DiagnosticsEngine::Error,
576+
"cannot mangle this %0 %1 yet");
577+
return Diags.Report(loc, DiagID) << thing1 << thing2;
578+
}
579+
580+
DiagnosticBuilder MicrosoftCXXNameMangler::Error(SourceLocation loc,
581+
StringRef thingy) {
582+
DiagnosticsEngine &Diags = Context.getDiags();
583+
unsigned DiagID = Diags.getCustomDiagID(DiagnosticsEngine::Error,
584+
"cannot mangle this %0 yet");
585+
return Diags.Report(loc, DiagID) << thingy;
586+
}
587+
588+
DiagnosticBuilder MicrosoftCXXNameMangler::Error(StringRef thingy) {
589+
DiagnosticsEngine &Diags = Context.getDiags();
590+
// extra placeholders are ignored quietly when not used
591+
unsigned DiagID = Diags.getCustomDiagID(DiagnosticsEngine::Error,
592+
"cannot mangle this %0 yet");
593+
return Diags.Report(DiagID) << thingy;
594+
}
595+
567596
void MicrosoftCXXNameMangler::mangle(GlobalDecl GD, StringRef Prefix) {
568597
const NamedDecl *D = cast<NamedDecl>(GD.getDecl());
569598
// MSVC doesn't mangle C++ names the same way it mangles extern "C" names.
@@ -1578,10 +1607,7 @@ void MicrosoftCXXNameMangler::mangleOperatorName(OverloadedOperatorKind OO,
15781607
case OO_Spaceship: Out << "?__M"; break;
15791608

15801609
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);
1610+
Error(Loc, "conditional operator");
15851611
break;
15861612
}
15871613

@@ -1673,11 +1699,8 @@ void MicrosoftCXXNameMangler::mangleExpression(
16731699
}
16741700

16751701
// 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();
1702+
Error(E->getExprLoc(), "expression type: ", E->getStmtClassName())
1703+
<< E->getSourceRange();
16811704
}
16821705

16831706
void MicrosoftCXXNameMangler::mangleTemplateArgs(
@@ -1923,11 +1946,19 @@ void MicrosoftCXXNameMangler::mangleTemplateArgValue(QualType T,
19231946
if (WithScalarType)
19241947
mangleType(T, SourceRange(), QMM_Escape);
19251948

1926-
// We don't know how to mangle past-the-end pointers yet.
1927-
if (V.isLValueOnePastTheEnd())
1928-
break;
1929-
19301949
APValue::LValueBase Base = V.getLValueBase();
1950+
1951+
// this might not cover every case but did cover issue 97756
1952+
// see test CodeGen/ms_mangler_templatearg_opte
1953+
if (V.isLValueOnePastTheEnd()) {
1954+
Out << "5E";
1955+
auto *VD = Base.dyn_cast<const ValueDecl *>();
1956+
if (VD)
1957+
mangle(VD);
1958+
Out << "@";
1959+
return;
1960+
}
1961+
19311962
if (!V.hasLValuePath() || V.getLValuePath().empty()) {
19321963
// Taking the address of a complete object has a special-case mangling.
19331964
if (Base.isNull()) {
@@ -1939,12 +1970,14 @@ void MicrosoftCXXNameMangler::mangleTemplateArgValue(QualType T,
19391970
mangleNumber(V.getLValueOffset().getQuantity());
19401971
} else if (!V.hasLValuePath()) {
19411972
// FIXME: This can only happen as an extension. Invent a mangling.
1942-
break;
1973+
Error("template argument (extension not comaptible with ms mangler)");
1974+
return;
19431975
} else if (auto *VD = Base.dyn_cast<const ValueDecl*>()) {
19441976
Out << "E";
19451977
mangle(VD);
19461978
} else {
1947-
break;
1979+
Error("template argument (undeclared base)");
1980+
return;
19481981
}
19491982
} else {
19501983
if (TAK == TplArgKind::ClassNTTP && T->isPointerType())
@@ -1989,8 +2022,10 @@ void MicrosoftCXXNameMangler::mangleTemplateArgValue(QualType T,
19892022
Out << *I;
19902023

19912024
auto *VD = Base.dyn_cast<const ValueDecl*>();
1992-
if (!VD)
1993-
break;
2025+
if (!VD) {
2026+
Error("template argument (null value decl)");
2027+
return;
2028+
}
19942029
Out << (TAK == TplArgKind::ClassNTTP ? 'E' : '1');
19952030
mangle(VD);
19962031

@@ -2105,15 +2140,16 @@ void MicrosoftCXXNameMangler::mangleTemplateArgValue(QualType T,
21052140
return;
21062141
}
21072142

2108-
case APValue::AddrLabelDiff:
2109-
case APValue::FixedPoint:
2110-
break;
2143+
case APValue::AddrLabelDiff: {
2144+
Error("template argument (value type: address label diff)");
2145+
return;
21112146
}
21122147

2113-
DiagnosticsEngine &Diags = Context.getDiags();
2114-
unsigned DiagID = Diags.getCustomDiagID(
2115-
DiagnosticsEngine::Error, "cannot mangle this template argument yet");
2116-
Diags.Report(DiagID);
2148+
case APValue::FixedPoint: {
2149+
Error("template argument (value type: fixed point)");
2150+
return;
2151+
}
2152+
}
21172153
}
21182154

21192155
void MicrosoftCXXNameMangler::mangleObjCProtocol(const ObjCProtocolDecl *PD) {
@@ -2740,11 +2776,9 @@ void MicrosoftCXXNameMangler::mangleType(const BuiltinType *T, Qualifiers,
27402776
case BuiltinType::SatULongFract:
27412777
case BuiltinType::Ibm128:
27422778
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;
2779+
Error(Range.getBegin(), "built-in type: ",
2780+
T->getName(Context.getASTContext().getPrintingPolicy()))
2781+
<< Range;
27482782
break;
27492783
}
27502784
}
@@ -3062,10 +3096,7 @@ void MicrosoftCXXNameMangler::mangleCallingConvention(CallingConv CC,
30623096
return;
30633097
}
30643098

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;
3099+
Error(Range.getBegin(), "calling convention") << Range;
30693100
}
30703101
void MicrosoftCXXNameMangler::mangleCallingConvention(const FunctionType *T,
30713102
SourceRange Range) {
@@ -3086,11 +3117,7 @@ void MicrosoftCXXNameMangler::mangleType(const UnresolvedUsingType *T,
30863117
Qualifiers, SourceRange Range) {
30873118
// Probably should be mangled as a template instantiation; need to see what
30883119
// 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;
3120+
Error(Range.getBegin(), "unresolved dependent type") << Range;
30943121
}
30953122

30963123
// <type> ::= <union-type> | <struct-type> | <class-type> | <enum-type>
@@ -3197,11 +3224,8 @@ void MicrosoftCXXNameMangler::mangleArrayType(const ArrayType *T) {
31973224
// The dependent expression has to be folded into a constant (TODO).
31983225
const DependentSizedArrayType *DSAT =
31993226
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();
3227+
Error(DSAT->getSizeExpr()->getExprLoc(), "dependent-length")
3228+
<< DSAT->getBracketsRange();
32053229
return;
32063230
} else {
32073231
break;
@@ -3241,20 +3265,12 @@ void MicrosoftCXXNameMangler::mangleType(const MemberPointerType *T,
32413265

32423266
void MicrosoftCXXNameMangler::mangleType(const TemplateTypeParmType *T,
32433267
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;
3268+
Error(Range.getBegin(), "template type parameter type") << Range;
32493269
}
32503270

32513271
void MicrosoftCXXNameMangler::mangleType(const SubstTemplateTypeParmPackType *T,
32523272
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;
3273+
Error(Range.getBegin(), "substituted parameter pack") << Range;
32583274
}
32593275

32603276
// <type> ::= <pointer-type>
@@ -3405,46 +3421,27 @@ void MicrosoftCXXNameMangler::mangleType(const ExtVectorType *T,
34053421

34063422
void MicrosoftCXXNameMangler::mangleType(const DependentVectorType *T,
34073423
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;
3424+
Error(Range.getBegin(), "dependent-sized vector type") << Range;
34133425
}
34143426

34153427
void MicrosoftCXXNameMangler::mangleType(const DependentSizedExtVectorType *T,
34163428
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;
3429+
Error(Range.getBegin(), "dependent-sized extended vector type") << Range;
34223430
}
34233431

34243432
void MicrosoftCXXNameMangler::mangleType(const ConstantMatrixType *T,
34253433
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;
3434+
Error(Range.getBegin(), "matrix type") << Range;
34303435
}
34313436

34323437
void MicrosoftCXXNameMangler::mangleType(const DependentSizedMatrixType *T,
34333438
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;
3439+
Error(Range.getBegin(), "dependent-sized matrix type") << Range;
34393440
}
34403441

34413442
void MicrosoftCXXNameMangler::mangleType(const DependentAddressSpaceType *T,
34423443
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;
3444+
Error(Range.getBegin(), "dependent address space type") << Range;
34483445
}
34493446

34503447
void MicrosoftCXXNameMangler::mangleType(const ObjCInterfaceType *T, Qualifiers,
@@ -3514,39 +3511,23 @@ void MicrosoftCXXNameMangler::mangleType(const InjectedClassNameType *,
35143511

35153512
void MicrosoftCXXNameMangler::mangleType(const TemplateSpecializationType *T,
35163513
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;
3514+
Error(Range.getBegin(), "template specialization type") << Range;
35223515
}
35233516

35243517
void MicrosoftCXXNameMangler::mangleType(const DependentNameType *T, Qualifiers,
35253518
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;
3519+
Error(Range.getBegin(), "dependent name type") << Range;
35313520
}
35323521

35333522
void MicrosoftCXXNameMangler::mangleType(
35343523
const DependentTemplateSpecializationType *T, Qualifiers,
35353524
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;
3525+
Error(Range.getBegin(), "dependent template specialization type") << Range;
35413526
}
35423527

35433528
void MicrosoftCXXNameMangler::mangleType(const PackExpansionType *T, Qualifiers,
35443529
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;
3530+
Error(Range.getBegin(), "pack expansion") << Range;
35503531
}
35513532

35523533
void MicrosoftCXXNameMangler::mangleType(const PackIndexingType *T,
@@ -3557,60 +3538,37 @@ void MicrosoftCXXNameMangler::mangleType(const PackIndexingType *T,
35573538

35583539
void MicrosoftCXXNameMangler::mangleType(const TypeOfType *T, Qualifiers,
35593540
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;
3541+
Error(Range.getBegin(), "typeof(type)") << Range;
35653542
}
35663543

35673544
void MicrosoftCXXNameMangler::mangleType(const TypeOfExprType *T, Qualifiers,
35683545
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;
3546+
Error(Range.getBegin(), "typeof(expression)") << Range;
35743547
}
35753548

35763549
void MicrosoftCXXNameMangler::mangleType(const DecltypeType *T, Qualifiers,
35773550
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;
3551+
Error(Range.getBegin(), "decltype()") << Range;
35833552
}
35843553

35853554
void MicrosoftCXXNameMangler::mangleType(const UnaryTransformType *T,
35863555
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;
3556+
Error(Range.getBegin(), "unary transform type") << Range;
35923557
}
35933558

35943559
void MicrosoftCXXNameMangler::mangleType(const AutoType *T, Qualifiers,
35953560
SourceRange Range) {
35963561
assert(T->getDeducedType().isNull() && "expecting a dependent type!");
35973562

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;
3563+
Error(Range.getBegin(), "'auto' type") << Range;
36033564
}
36043565

36053566
void MicrosoftCXXNameMangler::mangleType(
36063567
const DeducedTemplateSpecializationType *T, Qualifiers, SourceRange Range) {
36073568
assert(T->getDeducedType().isNull() && "expecting a dependent type!");
36083569

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;
3570+
Error(Range.getBegin(), "deduced class template specialization type")
3571+
<< Range;
36143572
}
36153573

36163574
void MicrosoftCXXNameMangler::mangleType(const AtomicType *T, Qualifiers,
@@ -3684,10 +3642,7 @@ void MicrosoftCXXNameMangler::mangleType(const BitIntType *T, Qualifiers,
36843642

36853643
void MicrosoftCXXNameMangler::mangleType(const DependentBitIntType *T,
36863644
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;
3645+
Error(Range.getBegin(), "DependentBitInt type") << Range;
36913646
}
36923647

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

0 commit comments

Comments
 (0)