Skip to content

Commit 53c9f1a

Browse files
committed
swift-api-digester: add a new action to dump name correction template.
Rename detection in the tool may not reflect overlay additions; thus we mistakes a new name to be an underscored one. This new action searches migration scripts for suspicious ones and generates a template for us to specify the correct names.
1 parent a155b75 commit 53c9f1a

File tree

3 files changed

+80
-1
lines changed

3 files changed

+80
-1
lines changed

include/swift/IDE/APIDigesterData.h

Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -334,6 +334,21 @@ struct OverloadedFuncInfo: public APIDiffItem {
334334
}
335335
};
336336

337+
struct NameCorrectionInfo {
338+
StringRef OriginalName;
339+
StringRef CorrectedName;
340+
StringRef ModuleName;
341+
NameCorrectionInfo(StringRef OriginalName, StringRef CorrectedName,
342+
StringRef ModuleName): OriginalName(OriginalName),
343+
CorrectedName(CorrectedName), ModuleName(ModuleName) {}
344+
bool operator<(NameCorrectionInfo Other) const {
345+
if (ModuleName != Other.ModuleName)
346+
return ModuleName.compare(Other.ModuleName) < 0;
347+
else
348+
return OriginalName.compare(Other.OriginalName) < 0;
349+
}
350+
};
351+
337352
/// APIDiffItem store is the interface that migrator should communicates with;
338353
/// Given a key, usually the usr of the system entity under migration, the store
339354
/// should return a slice of related changes in the same format of
@@ -343,6 +358,7 @@ struct APIDiffItemStore {
343358
struct Implementation;
344359
Implementation &Impl;
345360
static void serialize(llvm::raw_ostream &os, ArrayRef<APIDiffItem*> Items);
361+
static void serialize(llvm::raw_ostream &os, ArrayRef<NameCorrectionInfo> Items);
346362
APIDiffItemStore(const APIDiffItemStore& that) = delete;
347363
APIDiffItemStore();
348364
~APIDiffItemStore();

lib/IDE/APIDigesterData.cpp

Lines changed: 25 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -444,6 +444,25 @@ struct ArrayTraits<ArrayRef<APIDiffItem*>> {
444444
return const_cast<APIDiffItem *&>(seq[index]);
445445
}
446446
};
447+
448+
template<>
449+
struct ObjectTraits<NameCorrectionInfo> {
450+
static void mapping(Output &out, NameCorrectionInfo &value) {
451+
out.mapRequired(getKeyContent(DiffItemKeyKind::KK_OldPrintedName),value.OriginalName);
452+
out.mapRequired(getKeyContent(DiffItemKeyKind::KK_NewPrintedName), value.CorrectedName);
453+
out.mapRequired(getKeyContent(DiffItemKeyKind::KK_ModuleName), value.ModuleName);
454+
}
455+
};
456+
template<>
457+
struct ArrayTraits<ArrayRef<NameCorrectionInfo>> {
458+
static size_t size(Output &out, ArrayRef<NameCorrectionInfo> &seq) {
459+
return seq.size();
460+
}
461+
static NameCorrectionInfo &element(Output &, ArrayRef<NameCorrectionInfo> &seq,
462+
size_t index) {
463+
return const_cast<NameCorrectionInfo&>(seq[index]);
464+
}
465+
};
447466
} // namespace json
448467
} // namespace swift
449468

@@ -453,6 +472,12 @@ serialize(llvm::raw_ostream &os, ArrayRef<APIDiffItem*> Items) {
453472
yout << Items;
454473
}
455474

475+
void swift::ide::api::APIDiffItemStore::
476+
serialize(llvm::raw_ostream &os, ArrayRef<NameCorrectionInfo> Items) {
477+
json::Output yout(os);
478+
yout << Items;
479+
}
480+
456481
struct swift::ide::api::APIDiffItemStore::Implementation {
457482
private:
458483
llvm::SmallVector<std::unique_ptr<llvm::MemoryBuffer>, 2> AllBuffer;

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

Lines changed: 39 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -70,6 +70,7 @@ namespace {
7070
// The following two are for testing purposes
7171
DeserializeDiffItems,
7272
DeserializeSDK,
73+
GenerateNameCorrectionTemplate,
7374
};
7475
} // end anonymous namespace
7576

@@ -141,7 +142,10 @@ Action(llvm::cl::desc("Mode:"), llvm::cl::init(ActionType::None),
141142
"Deserialize diff items in a JSON file"),
142143
clEnumValN(ActionType::DeserializeSDK,
143144
"deserialize-sdk",
144-
"Deserialize sdk digester in a JSON file")));
145+
"Deserialize sdk digester in a JSON file"),
146+
clEnumValN(ActionType::GenerateNameCorrectionTemplate,
147+
"generate-name-correction",
148+
"Generate name correction template")));
145149

146150
static llvm::cl::list<std::string>
147151
SDKJsonPaths("input-paths",
@@ -3965,6 +3969,33 @@ static int deserializeDiffItems(StringRef DiffPath, StringRef OutputPath) {
39653969
return 0;
39663970
}
39673971

3972+
static int deserializeNameCorrection(APIDiffItemStore &Store,
3973+
StringRef OutputPath) {
3974+
std::error_code EC;
3975+
llvm::raw_fd_ostream FS(OutputPath, EC, llvm::sys::fs::F_None);
3976+
std::set<NameCorrectionInfo> Result;
3977+
for (auto *Item: Store.getAllDiffItems()) {
3978+
if (auto *CI = dyn_cast<CommonDiffItem>(Item)) {
3979+
if (CI->DiffKind == NodeAnnotation::Rename) {
3980+
auto NewName = CI->getNewName();
3981+
auto Module = CI->ModuleName;
3982+
DeclNameViewer Viewer(NewName);
3983+
auto HasUnderScore =
3984+
[](StringRef S) { return S.find('_') != StringRef::npos; };
3985+
auto Args = Viewer.args();
3986+
if (HasUnderScore(Viewer.base()) ||
3987+
std::any_of(Args.begin(), Args.end(), HasUnderScore)) {
3988+
Result.insert(NameCorrectionInfo(NewName, NewName, Module));
3989+
}
3990+
}
3991+
}
3992+
}
3993+
std::vector<NameCorrectionInfo> Vec;
3994+
Vec.insert(Vec.end(), Result.begin(), Result.end());
3995+
APIDiffItemStore::serialize(FS, Vec);
3996+
return EC.value();
3997+
}
3998+
39683999
/// Mostly for testing purposes, this function de-serializes the SDK dump in
39694000
/// dumpPath and re-serialize them to OutputPath. If the tool performs correctly,
39704001
/// the contents in dumpPath and OutputPath should be identical.
@@ -4029,6 +4060,13 @@ int main(int argc, char *argv[]) {
40294060
else
40304061
return deserializeSDKDump(options::SDKJsonPaths[0], options::OutputFile);
40314062
}
4063+
case ActionType::GenerateNameCorrectionTemplate: {
4064+
APIDiffItemStore Store;
4065+
auto &Paths = options::SDKJsonPaths;
4066+
for (unsigned I = 0; I < Paths.size(); I ++)
4067+
Store.addStorePath(Paths[I]);
4068+
return deserializeNameCorrection(Store, options::OutputFile);
4069+
}
40324070
case ActionType::None:
40334071
llvm::errs() << "Action required\n";
40344072
llvm::cl::PrintHelpMessage();

0 commit comments

Comments
 (0)