Skip to content

Commit b3de931

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 2f55e55 commit b3de931

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.
@@ -1577,10 +1597,7 @@ void MicrosoftCXXNameMangler::mangleOperatorName(OverloadedOperatorKind OO,
15771597
case OO_Spaceship: Out << "?__M"; break;
15781598

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

@@ -1672,11 +1689,8 @@ void MicrosoftCXXNameMangler::mangleExpression(
16721689
}
16731690

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

16821696
void MicrosoftCXXNameMangler::mangleTemplateArgs(
@@ -1922,11 +1936,19 @@ void MicrosoftCXXNameMangler::mangleTemplateArgValue(QualType T,
19221936
if (WithScalarType)
19231937
mangleType(T, SourceRange(), QMM_Escape);
19241938

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

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

@@ -2104,15 +2130,16 @@ void MicrosoftCXXNameMangler::mangleTemplateArgValue(QualType T,
21042130
return;
21052131
}
21062132

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

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

21182145
void MicrosoftCXXNameMangler::mangleObjCProtocol(const ObjCProtocolDecl *PD) {
@@ -2739,11 +2766,9 @@ void MicrosoftCXXNameMangler::mangleType(const BuiltinType *T, Qualifiers,
27392766
case BuiltinType::SatULongFract:
27402767
case BuiltinType::Ibm128:
27412768
case BuiltinType::Float128: {
2742-
DiagnosticsEngine &Diags = Context.getDiags();
2743-
unsigned DiagID = Diags.getCustomDiagID(
2744-
DiagnosticsEngine::Error, "cannot mangle this built-in %0 type yet");
2745-
Diags.Report(Range.getBegin(), DiagID)
2746-
<< T->getName(Context.getASTContext().getPrintingPolicy()) << Range;
2769+
Error(Range.getBegin(), "built-in ")
2770+
<< T->getName(Context.getASTContext().getPrintingPolicy()) << " type"
2771+
<< Range;
27472772
break;
27482773
}
27492774
}
@@ -3061,10 +3086,7 @@ void MicrosoftCXXNameMangler::mangleCallingConvention(CallingConv CC,
30613086
return;
30623087
}
30633088

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

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

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

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

32593266
// <type> ::= <pointer-type>
@@ -3404,46 +3411,27 @@ void MicrosoftCXXNameMangler::mangleType(const ExtVectorType *T,
34043411

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

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

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

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

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

34493437
void MicrosoftCXXNameMangler::mangleType(const ObjCInterfaceType *T, Qualifiers,
@@ -3513,39 +3501,23 @@ void MicrosoftCXXNameMangler::mangleType(const InjectedClassNameType *,
35133501

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

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

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

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

35513523
void MicrosoftCXXNameMangler::mangleType(const PackIndexingType *T,
@@ -3556,60 +3528,37 @@ void MicrosoftCXXNameMangler::mangleType(const PackIndexingType *T,
35563528

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

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

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

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

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

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

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

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

36153564
void MicrosoftCXXNameMangler::mangleType(const AtomicType *T, Qualifiers,
@@ -3683,10 +3632,7 @@ void MicrosoftCXXNameMangler::mangleType(const BitIntType *T, Qualifiers,
36833632

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

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

0 commit comments

Comments
 (0)