Skip to content

Commit 11cdd41

Browse files
committed
swift-api-digester: simplify some code. NFC.
1 parent 07e002c commit 11cdd41

File tree

1 file changed

+74
-131
lines changed

1 file changed

+74
-131
lines changed

tools/swift-api-digester/swift-api-digester.cpp

Lines changed: 74 additions & 131 deletions
Original file line numberDiff line numberDiff line change
@@ -3324,17 +3324,33 @@ class DiagnosisEmitter : public SDKNodeVisitor {
33243324
struct MetaInfo {
33253325
StringRef ModuleName;
33263326
StringRef HeaderName;
3327+
DeclKind Kind;
3328+
StringRef Name;
33273329
bool IsABISpecific;
3328-
MetaInfo(const SDKNodeDecl *Node, bool IsABISpecific = false):
3330+
MetaInfo(const SDKNodeDecl *Node, bool IsABISpecific):
33293331
ModuleName(Node->getModuleName()), HeaderName(Node->getHeaderName()),
3332+
Kind(Node->getDeclKind()), Name(Node->getFullyQualifiedName()),
33303333
IsABISpecific(IsABISpecific) {}
3334+
int compare(MetaInfo &Other) const {
3335+
if (ModuleName != Other.ModuleName)
3336+
return ModuleName.compare(Other.ModuleName);
3337+
if (HeaderName != Other.HeaderName)
3338+
return HeaderName.compare(Other.HeaderName);
3339+
if (Kind != Other.Kind)
3340+
return (uint8_t)Kind - (uint8_t)Other.Kind;
3341+
return Name.compare(Other.Name);
3342+
}
33313343
};
33323344

33333345
class DiagBase {
3346+
protected:
33343347
MetaInfo Info;
3335-
public:
33363348
DiagBase(MetaInfo Info): Info(Info) {}
33373349
virtual ~DiagBase() = default;
3350+
raw_ostream &outputName() const {
3351+
return llvm::outs() << Info.Kind << " " << printName(Info.Name);
3352+
}
3353+
public:
33383354
void outputModule() const {
33393355
if (options::PrintModule) {
33403356
llvm::outs() << Info.ModuleName;
@@ -3351,115 +3367,94 @@ class DiagnosisEmitter : public SDKNodeVisitor {
33513367
DeclKind Kind;
33523368
StringRef Name;
33533369
bool IsDeprecated;
3354-
RemovedDeclDiag(MetaInfo Info, DeclKind Kind, StringRef Name,
3355-
bool IsDeprecated): DiagBase(Info), Kind(Kind),
3356-
Name(Name), IsDeprecated(IsDeprecated) {}
3357-
bool operator<(RemovedDeclDiag Other) const;
3370+
RemovedDeclDiag(MetaInfo Info, bool IsDeprecated): DiagBase(Info),
3371+
IsDeprecated(IsDeprecated) {}
3372+
bool operator<(RemovedDeclDiag Other) const {
3373+
return Info.compare(Other.Info) < 0;
3374+
}
33583375
void output() const override;
33593376
static void theme(raw_ostream &OS) { OS << "Removed Decls"; };
33603377
};
33613378

33623379
struct MovedDeclDiag: public DiagBase {
3363-
DeclKind RemovedKind;
33643380
DeclKind AddedKind;
3365-
StringRef RemovedName;
33663381
StringRef AddedName;
3367-
MovedDeclDiag(MetaInfo Info, DeclKind RemovedKind, DeclKind AddedKind,
3368-
StringRef RemovedName, StringRef AddedName):
3369-
DiagBase(Info), RemovedKind(RemovedKind), AddedKind(AddedKind),
3370-
RemovedName(RemovedName), AddedName(AddedName) {}
3371-
bool operator<(MovedDeclDiag other) const;
3382+
MovedDeclDiag(MetaInfo Info, DeclKind AddedKind, StringRef AddedName):
3383+
DiagBase(Info), AddedKind(AddedKind), AddedName(AddedName) {}
3384+
bool operator<(MovedDeclDiag Other) const {
3385+
return Info.compare(Other.Info) < 0;
3386+
}
33723387
void output() const override;
33733388
static void theme(raw_ostream &OS) { OS << "Moved Decls"; };
33743389
};
33753390

33763391
struct RenamedDeclDiag: public DiagBase {
3377-
DeclKind KindBefore;
33783392
DeclKind KindAfter;
3379-
StringRef NameBefore;
33803393
StringRef NameAfter;
3381-
RenamedDeclDiag(MetaInfo Info, DeclKind KindBefore, DeclKind KindAfter,
3382-
StringRef NameBefore, StringRef NameAfter):
3383-
DiagBase(Info),
3384-
KindBefore(KindBefore), KindAfter(KindAfter),
3385-
NameBefore(NameBefore), NameAfter(NameAfter) {}
3386-
bool operator<(RenamedDeclDiag Other) const;
3394+
RenamedDeclDiag(MetaInfo Info, DeclKind KindAfter, StringRef NameAfter):
3395+
DiagBase(Info), KindAfter(KindAfter), NameAfter(NameAfter) {}
3396+
bool operator<(RenamedDeclDiag Other) const {
3397+
return Info.compare(Other.Info) < 0;
3398+
};
33873399
void output() const override;
33883400
static void theme(raw_ostream &OS) { OS << "Renamed Decls"; };
33893401
};
33903402

33913403
struct DeclAttrDiag: public DiagBase {
3392-
DeclKind Kind;
3393-
StringRef DeclName;
33943404
StringRef AttrBefore;
33953405
StringRef AttrAfter;
3396-
DeclAttrDiag(MetaInfo Info, DeclKind Kind, StringRef DeclName,
3397-
StringRef AttrBefore, StringRef AttrAfter):
3398-
DiagBase(Info), Kind(Kind), DeclName(DeclName),
3399-
AttrBefore(AttrBefore), AttrAfter(AttrAfter) {}
3400-
DeclAttrDiag(MetaInfo Info, DeclKind Kind, StringRef DeclName,
3401-
StringRef AttrAfter): DeclAttrDiag(Info, Kind, DeclName,
3402-
StringRef(), AttrAfter) {}
3403-
3406+
DeclAttrDiag(MetaInfo Info, StringRef AttrBefore, StringRef AttrAfter):
3407+
DiagBase(Info), AttrBefore(AttrBefore), AttrAfter(AttrAfter) {}
3408+
DeclAttrDiag(MetaInfo Info, StringRef AttrAfter): DeclAttrDiag(Info,
3409+
StringRef(), AttrAfter) {}
34043410
bool operator<(DeclAttrDiag Other) const;
34053411
void output() const override;
34063412
static void theme(raw_ostream &OS) { OS << "Decl Attribute changes"; };
34073413
};
34083414

34093415
struct DeclTypeChangeDiag: public DiagBase {
3410-
DeclKind Kind;
3411-
StringRef DeclName;
34123416
StringRef TypeNameBefore;
34133417
StringRef TypeNameAfter;
34143418
StringRef Description;
3415-
DeclTypeChangeDiag(MetaInfo Info, DeclKind Kind, StringRef DeclName,
3416-
StringRef TypeNameBefore, StringRef TypeNameAfter,
3419+
DeclTypeChangeDiag(MetaInfo Info, StringRef TypeNameBefore,
3420+
StringRef TypeNameAfter,
34173421
StringRef Description): DiagBase(Info),
3418-
Kind(Kind), DeclName(DeclName), TypeNameBefore(TypeNameBefore),
3419-
TypeNameAfter(TypeNameAfter), Description(Description) {}
3420-
bool operator<(DeclTypeChangeDiag Other) const;
3422+
TypeNameBefore(TypeNameBefore), TypeNameAfter(TypeNameAfter),
3423+
Description(Description) {}
3424+
bool operator<(DeclTypeChangeDiag Other) const {
3425+
return Info.compare(Other.Info) < 0;
3426+
};
34213427
void output() const override;
34223428
static void theme(raw_ostream &OS) { OS << "Type Changes"; };
34233429
};
34243430

34253431
struct RawRepresentableChangeDiag: public DiagBase {
3426-
DeclKind Kind;
3427-
StringRef DeclName;
34283432
StringRef UnderlyingType;
34293433
StringRef RawTypeName;
3430-
RawRepresentableChangeDiag(MetaInfo Info, DeclKind Kind, StringRef DeclName,
3431-
StringRef UnderlyingType, StringRef RawTypeName): DiagBase(Info),
3432-
Kind(Kind), DeclName(DeclName), UnderlyingType(UnderlyingType),
3433-
RawTypeName(RawTypeName) {}
3434+
RawRepresentableChangeDiag(MetaInfo Info, StringRef UnderlyingType,
3435+
StringRef RawTypeName): DiagBase(Info), UnderlyingType(UnderlyingType),
3436+
RawTypeName(RawTypeName) {}
34343437
bool operator<(RawRepresentableChangeDiag Other) const {
3435-
if (Kind != Other.Kind)
3436-
return Kind < Other.Kind;
3437-
return DeclName.compare(Other.DeclName) < 0;
3438+
return Info.compare(Other.Info) < 0;
34383439
}
34393440
void output() const override {
3440-
llvm::outs() << Kind << " " << printName(DeclName)
3441-
<< "(" << UnderlyingType << ")"
3441+
outputName() << "(" << UnderlyingType << ")"
34423442
<< " is now " << RawTypeName << " representable\n";
34433443
}
34443444
static void theme(raw_ostream &OS) { OS << "RawRepresentable Changes"; };
34453445
};
34463446

34473447
struct GenericSignatureChangeDiag: public DiagBase {
3448-
DeclKind Kind;
3449-
StringRef DeclName;
34503448
StringRef GSBefore;
34513449
StringRef GSAfter;
3452-
GenericSignatureChangeDiag(MetaInfo Info, DeclKind Kind, StringRef DeclName,
3453-
StringRef GSBefore, StringRef GSAfter): DiagBase(Info), Kind(Kind),
3454-
DeclName(DeclName), GSBefore(GSBefore), GSAfter(GSAfter) {}
3450+
GenericSignatureChangeDiag(MetaInfo Info, StringRef GSBefore,
3451+
StringRef GSAfter): DiagBase(Info),
3452+
GSBefore(GSBefore), GSAfter(GSAfter) {}
34553453
bool operator<(GenericSignatureChangeDiag Other) const {
3456-
if (Kind != Other.Kind)
3457-
return Kind < Other.Kind;
3458-
return DeclName.compare(Other.DeclName) < 0;
3454+
return Info.compare(Other.Info) < 0;
34593455
}
34603456
void output() const override {
3461-
llvm::outs() << Kind << " " << printName(DeclName)
3462-
<< " has generic signature change from " << GSBefore << " to "
3457+
outputName() << " has generic signature change from " << GSBefore << " to "
34633458
<< GSAfter << "\n";
34643459
}
34653460
static void theme(raw_ostream &OS) { OS << "Generic Signature Changes"; };
@@ -3529,77 +3524,44 @@ StringRef DiagnosisEmitter::printDiagKeyword(StringRef Name) {
35293524
return StringRef();
35303525
}
35313526

3532-
bool DiagnosisEmitter::RemovedDeclDiag::
3533-
operator<(RemovedDeclDiag Other) const {
3534-
if (Kind != Other.Kind)
3535-
return Kind < Other.Kind;
3536-
return Name.compare(Other.Name) < 0;
3537-
}
3538-
35393527
void DiagnosisEmitter::RemovedDeclDiag::output() const {
3540-
llvm::outs() << Kind << " " << printName(Name) << " has been "
3541-
<< printDiagKeyword("removed");
3528+
outputName() << " has been " << printDiagKeyword("removed");
35423529
if (IsDeprecated)
35433530
llvm::outs() << " (deprecated)";
35443531
llvm::outs() << "\n";
35453532
}
35463533

3547-
bool DiagnosisEmitter::MovedDeclDiag::
3548-
operator<(MovedDeclDiag Other) const {
3549-
if (RemovedKind != Other.RemovedKind)
3550-
return RemovedKind < Other.RemovedKind;
3551-
return RemovedName.compare(Other.RemovedName) < 0;
3552-
}
3553-
35543534
void DiagnosisEmitter::MovedDeclDiag::output() const {
3555-
llvm::outs() << RemovedKind << " " << printName(RemovedName) << " has been "
3556-
<< printDiagKeyword("moved") << " to " << AddedKind << " "
3557-
<< printName(AddedName) << "\n";
3558-
}
3559-
3560-
bool DiagnosisEmitter::RenamedDeclDiag::
3561-
operator<(RenamedDeclDiag Other) const {
3562-
if (KindBefore != Other.KindBefore)
3563-
return KindBefore < Other.KindBefore;
3564-
return NameBefore.compare(Other.NameBefore) < 0;
3535+
outputName() << " has been " << printDiagKeyword("moved") << " to "
3536+
<< AddedKind << " " << printName(AddedName) << "\n";
35653537
}
35663538

35673539
void DiagnosisEmitter::RenamedDeclDiag::output() const {
3568-
llvm::outs() << KindBefore << " " << printName(NameBefore)
3569-
<< " has been " << printDiagKeyword("renamed") << " to "
3570-
<< KindAfter << " " << printName(NameAfter) << "\n";
3571-
}
3572-
3573-
bool DiagnosisEmitter::DeclTypeChangeDiag::
3574-
operator<(DeclTypeChangeDiag Other) const {
3575-
if (Kind != Other.Kind)
3576-
return Kind < Other.Kind;
3577-
return DeclName.compare(Other.DeclName) < 0;
3540+
outputName() << " has been " << printDiagKeyword("renamed") << " to "
3541+
<< KindAfter << " " << printName(NameAfter) << "\n";
35783542
}
35793543

35803544
void DiagnosisEmitter::DeclTypeChangeDiag::output() const {
3581-
llvm::outs() << Kind << " " << printName(DeclName) << " has "
3545+
outputName() << " has "
35823546
<< Description << " type change from "
35833547
<< printName(TypeNameBefore) << " to "
35843548
<< printName(TypeNameAfter) << "\n";
35853549
}
35863550

35873551

35883552
bool DiagnosisEmitter::DeclAttrDiag::operator<(DeclAttrDiag Other) const {
3589-
if (Kind != Other.Kind)
3590-
return Kind < Other.Kind;
3591-
if (DeclName != Other.DeclName)
3592-
return DeclName.compare(Other.DeclName) < 0;
3553+
auto result = Info.compare(Other.Info);
3554+
if (result)
3555+
return result < 0;
35933556
return AttrAfter.compare(Other.AttrAfter) < 0;
35943557
}
35953558

35963559
void DiagnosisEmitter::DeclAttrDiag::output() const {
35973560
if (AttrBefore.empty())
3598-
llvm::outs() << Kind << " " << printName(DeclName) << " is now " <<
3599-
printDiagKeyword(AttrAfter)<< "\n";
3561+
outputName() << " is now " << printDiagKeyword(AttrAfter)<< "\n";
36003562
else
3601-
llvm::outs() << Kind << " " << printName(DeclName) << " changes from " <<
3602-
printDiagKeyword(AttrBefore) << " to "<< printDiagKeyword(AttrAfter)<< "\n";
3563+
outputName() << " changes from " << printDiagKeyword(AttrBefore)
3564+
<< " to "<< printDiagKeyword(AttrAfter)<< "\n";
36033565
}
36043566

36053567
void DiagnosisEmitter::diagnosis(NodePtr LeftRoot, NodePtr RightRoot,
@@ -3622,9 +3584,7 @@ void DiagnosisEmitter::handle(const SDKNodeDecl *Node, NodeAnnotation Anno) {
36223584
if (auto *Added = findAddedDecl(Node)) {
36233585
if (Node->getDeclKind() != DeclKind::Constructor) {
36243586
MovedDecls.Diags.emplace_back(ScreenInfo,
3625-
Node->getDeclKind(),
36263587
Added->getDeclKind(),
3627-
Node->getFullyQualifiedName(),
36283588
Added->getFullyQualifiedName());
36293589
return;
36303590
}
@@ -3636,7 +3596,6 @@ void DiagnosisEmitter::handle(const SDKNodeDecl *Node, NodeAnnotation Anno) {
36363596
[&](TypeMemberDiffItem &Item) { return Item.usr == Node->getUsr(); });
36373597
if (It != MemberChanges.end()) {
36383598
RenamedDecls.Diags.emplace_back(ScreenInfo, Node->getDeclKind(),
3639-
Node->getDeclKind(), Node->getFullyQualifiedName(),
36403599
Ctx.buffer((Twine(It->newTypeName) + "." + It->newPrintedName).str()));
36413600
return;
36423601
}
@@ -3646,8 +3605,7 @@ void DiagnosisEmitter::handle(const SDKNodeDecl *Node, NodeAnnotation Anno) {
36463605
// refine diagnostics message instead of showing the type alias has been
36473606
// removed.
36483607
if (TypeAliasUpdateMap.find((SDKNode*)Node) != TypeAliasUpdateMap.end()) {
3649-
RawRepresentableDecls.Diags.emplace_back(ScreenInfo, Node->getDeclKind(),
3650-
Node->getFullyQualifiedName(),
3608+
RawRepresentableDecls.Diags.emplace_back(ScreenInfo,
36513609
Node->getAs<SDKNodeDeclTypeAlias>()->getUnderlyingType()->getPrintedName(),
36523610
TypeAliasUpdateMap[(SDKNode*)Node]->getAs<SDKNodeDeclType>()->
36533611
getRawValueType()->getPrintedName());
@@ -3670,41 +3628,30 @@ void DiagnosisEmitter::handle(const SDKNodeDecl *Node, NodeAnnotation Anno) {
36703628
if (FoundInSuperclass)
36713629
return;
36723630
RemovedDecls.Diags.emplace_back(ScreenInfo,
3673-
Node->getDeclKind(),
3674-
Node->getFullyQualifiedName(),
36753631
Node->isDeprecated());
36763632
return;
36773633
}
36783634
case NodeAnnotation::Rename: {
36793635
MetaInfo ScreenInfo(Node, false);
36803636
auto *Count = UpdateMap.findUpdateCounterpart(Node)->getAs<SDKNodeDecl>();
36813637
RenamedDecls.Diags.emplace_back(ScreenInfo,
3682-
Node->getDeclKind(), Count->getDeclKind(),
3683-
Node->getFullyQualifiedName(),
3638+
Count->getDeclKind(),
36843639
Count->getFullyQualifiedName());
36853640
return;
36863641
}
36873642
case NodeAnnotation::NowMutating: {
36883643
MetaInfo ScreenInfo(Node, false);
3689-
AttrChangedDecls.Diags.emplace_back(ScreenInfo,
3690-
Node->getDeclKind(),
3691-
Node->getFullyQualifiedName(),
3692-
Ctx.buffer("mutating"));
3644+
AttrChangedDecls.Diags.emplace_back(ScreenInfo, Ctx.buffer("mutating"));
36933645
return;
36943646
}
36953647
case NodeAnnotation::NowThrowing: {
36963648
MetaInfo ScreenInfo(Node, false);
3697-
AttrChangedDecls.Diags.emplace_back(ScreenInfo,
3698-
Node->getDeclKind(),
3699-
Node->getFullyQualifiedName(),
3700-
Ctx.buffer("throwing"));
3649+
AttrChangedDecls.Diags.emplace_back(ScreenInfo, Ctx.buffer("throwing"));
37013650
return;
37023651
}
37033652
case NodeAnnotation::StaticChange: {
37043653
MetaInfo ScreenInfo(Node, false);
37053654
AttrChangedDecls.Diags.emplace_back(ScreenInfo,
3706-
Node->getDeclKind(),
3707-
Node->getFullyQualifiedName(),
37083655
Ctx.buffer(Node->isStatic() ? "not static" : "static"));
37093656
return;
37103657
}
@@ -3717,15 +3664,14 @@ void DiagnosisEmitter::handle(const SDKNodeDecl *Node, NodeAnnotation Anno) {
37173664
};
37183665
auto *Count = UpdateMap.findUpdateCounterpart(Node)->getAs<SDKNodeDecl>();
37193666
AttrChangedDecls.Diags.emplace_back(
3720-
ScreenInfo, Node->getDeclKind(), Node->getFullyQualifiedName(),
3667+
ScreenInfo,
37213668
getOwnershipDescription(Node->getReferenceOwnership()),
37223669
getOwnershipDescription(Count->getReferenceOwnership()));
37233670
return;
37243671
}
37253672
case NodeAnnotation::ChangeGenericSignature: {
37263673
MetaInfo ScreenInfo(Node, false);
3727-
GSChangeDecls.Diags.emplace_back(ScreenInfo, Node->getDeclKind(),
3728-
Node->getFullyQualifiedName(), Node->getGenericSignature(),
3674+
GSChangeDecls.Diags.emplace_back(ScreenInfo, Node->getGenericSignature(),
37293675
UpdateMap.findUpdateCounterpart(Node)->getAs<SDKNodeDecl>()->
37303676
getGenericSignature());
37313677
return;
@@ -3742,8 +3688,7 @@ void DiagnosisEmitter::handle(const SDKNodeDecl *Node, NodeAnnotation Anno) {
37423688
auto Desc = Node->hasDeclAttribute(It->Kind) ?
37433689
Ctx.buffer((llvm::Twine("without ") + It->Content).str()):
37443690
Ctx.buffer((llvm::Twine("with ") + It->Content).str());
3745-
AttrChangedDecls.Diags.emplace_back(ScreenInfo, Node->getDeclKind(),
3746-
Node->getFullyQualifiedName(), Desc);
3691+
AttrChangedDecls.Diags.emplace_back(ScreenInfo, Desc);
37473692
return;
37483693
}
37493694
}
@@ -3761,7 +3706,7 @@ void DiagnosisEmitter::visitType(SDKNodeType *Node) {
37613706
auto *Parent = dyn_cast<SDKNodeDecl>(Node->getParent());
37623707
if (!Parent || Parent->isSDKPrivate())
37633708
return;
3764-
MetaInfo ScreenInfo(Parent);
3709+
MetaInfo ScreenInfo(Parent, false);
37653710
SDKContext &Ctx = Node->getSDKContext();
37663711
if (Node->isAnnotatedAs(NodeAnnotation::Updated)) {
37673712
auto *Count = UpdateMap.findUpdateCounterpart(Node)->getAs<SDKNodeType>();
@@ -3775,8 +3720,6 @@ void DiagnosisEmitter::visitType(SDKNodeType *Node) {
37753720
Ctx.buffer("declared");
37763721
if (Node->getPrintedName() != Count->getPrintedName())
37773722
TypeChangedDecls.Diags.emplace_back(ScreenInfo,
3778-
Parent->getDeclKind(),
3779-
Parent->getFullyQualifiedName(),
37803723
Node->getPrintedName(),
37813724
Count->getPrintedName(),
37823725
Descriptor);

0 commit comments

Comments
 (0)