Skip to content

Commit beb5347

Browse files
authored
[Migrator] During de-serialization, remove duplicated API diff items from the store. (#9239)
1 parent c8d3506 commit beb5347

File tree

3 files changed

+51
-2
lines changed

3 files changed

+51
-2
lines changed

include/swift/IDE/APIDigesterData.h

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -55,6 +55,7 @@ struct APIDiffItem {
5555
virtual APIDiffItemKind getKind() const = 0;
5656
virtual StringRef getKey() const = 0;
5757
virtual ~APIDiffItem() = default;
58+
bool operator==(const APIDiffItem &Other) const;
5859
};
5960

6061
// CommonDiffItem describes how an element in SDK evolves in a way that migrator can

lib/IDE/APIDigesterData.cpp

Lines changed: 28 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -190,6 +190,28 @@ bool swift::ide::api::NAME::classof(const APIDiffItem *D) { \
190190
}
191191
#include "swift/IDE/DigesterEnums.def"
192192

193+
bool APIDiffItem::operator==(const APIDiffItem &Other) const {
194+
if (getKind() != Other.getKind())
195+
return false;
196+
if (getKey() != Other.getKey())
197+
return false;
198+
switch(getKind()) {
199+
case APIDiffItemKind::ADK_CommonDiffItem: {
200+
auto *Left = static_cast<const CommonDiffItem*>(this);
201+
auto *Right = static_cast<const CommonDiffItem*>(&Other);
202+
return Left->ChildIndex == Right->ChildIndex;
203+
}
204+
case APIDiffItemKind::ADK_NoEscapeFuncParam: {
205+
auto *Left = static_cast<const NoEscapeFuncParam*>(this);
206+
auto *Right = static_cast<const NoEscapeFuncParam*>(&Other);
207+
return Left->Index == Right->Index;
208+
}
209+
case APIDiffItemKind::ADK_TypeMemberDiffItem:
210+
case APIDiffItemKind::ADK_OverloadedFuncInfo:
211+
llvm_unreachable("should be handled above.");
212+
}
213+
}
214+
193215
namespace {
194216
enum class DiffItemKeyKind {
195217
#define DIFF_ITEM_KEY_KIND(NAME) KK_##NAME,
@@ -392,8 +414,12 @@ struct swift::ide::api::APIDiffItemStore::Implementation {
392414
for (auto It = Array->begin(); It != Array->end(); ++ It) {
393415
APIDiffItem *Item = serializeDiffItem(Allocator,
394416
cast<llvm::yaml::MappingNode>(&*It));
395-
Data[Item->getKey()].push_back(Item);
396-
AllItems.push_back(Item);
417+
auto &Bag = Data[Item->getKey()];
418+
if(std::find_if(Bag.begin(), Bag.end(),
419+
[&](APIDiffItem* I) { return *Item == *I; }) == Bag.end()) {
420+
Bag.push_back(Item);
421+
AllItems.push_back(Item);
422+
}
397423
}
398424
}
399425

test/Migrator/API.json

Lines changed: 22 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -270,6 +270,28 @@
270270
"RightComment": "",
271271
"ModuleName": "Cities"
272272
},
273+
{
274+
"DiffItemKind": "CommonDiffItem",
275+
"NodeKind": "Function",
276+
"NodeAnnotation": "WrapOptional",
277+
"ChildIndex": "1",
278+
"LeftUsr": "s:6Cities05ExtraA0P6blibliySQySSGSSSg_SStc1x_tF",
279+
"LeftComment": "",
280+
"RightUsr": "",
281+
"RightComment": "",
282+
"ModuleName": "Cities"
283+
},
284+
{
285+
"DiffItemKind": "CommonDiffItem",
286+
"NodeKind": "Function",
287+
"NodeAnnotation": "WrapOptional",
288+
"ChildIndex": "1",
289+
"LeftUsr": "s:6Cities05ExtraA0P6blibliySQySSGSSSg_SStc1x_tF",
290+
"LeftComment": "",
291+
"RightUsr": "",
292+
"RightComment": "",
293+
"ModuleName": "Cities"
294+
},
273295
{
274296
"DiffItemKind": "CommonDiffItem",
275297
"NodeKind": "Function",

0 commit comments

Comments
 (0)