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.
This commit is contained in:
Xi Ge
2018-04-19 17:58:58 -07:00
parent a155b753fa
commit 53c9f1aaa9
3 changed files with 80 additions and 1 deletions

View File

@@ -70,6 +70,7 @@ namespace {
// The following two are for testing purposes
DeserializeDiffItems,
DeserializeSDK,
GenerateNameCorrectionTemplate,
};
} // end anonymous namespace
@@ -141,7 +142,10 @@ Action(llvm::cl::desc("Mode:"), llvm::cl::init(ActionType::None),
"Deserialize diff items in a JSON file"),
clEnumValN(ActionType::DeserializeSDK,
"deserialize-sdk",
"Deserialize sdk digester in a JSON file")));
"Deserialize sdk digester in a JSON file"),
clEnumValN(ActionType::GenerateNameCorrectionTemplate,
"generate-name-correction",
"Generate name correction template")));
static llvm::cl::list<std::string>
SDKJsonPaths("input-paths",
@@ -3965,6 +3969,33 @@ static int deserializeDiffItems(StringRef DiffPath, StringRef OutputPath) {
return 0;
}
static int deserializeNameCorrection(APIDiffItemStore &Store,
StringRef OutputPath) {
std::error_code EC;
llvm::raw_fd_ostream FS(OutputPath, EC, llvm::sys::fs::F_None);
std::set<NameCorrectionInfo> Result;
for (auto *Item: Store.getAllDiffItems()) {
if (auto *CI = dyn_cast<CommonDiffItem>(Item)) {
if (CI->DiffKind == NodeAnnotation::Rename) {
auto NewName = CI->getNewName();
auto Module = CI->ModuleName;
DeclNameViewer Viewer(NewName);
auto HasUnderScore =
[](StringRef S) { return S.find('_') != StringRef::npos; };
auto Args = Viewer.args();
if (HasUnderScore(Viewer.base()) ||
std::any_of(Args.begin(), Args.end(), HasUnderScore)) {
Result.insert(NameCorrectionInfo(NewName, NewName, Module));
}
}
}
}
std::vector<NameCorrectionInfo> Vec;
Vec.insert(Vec.end(), Result.begin(), Result.end());
APIDiffItemStore::serialize(FS, Vec);
return EC.value();
}
/// Mostly for testing purposes, this function de-serializes the SDK dump in
/// dumpPath and re-serialize them to OutputPath. If the tool performs correctly,
/// the contents in dumpPath and OutputPath should be identical.
@@ -4029,6 +4060,13 @@ int main(int argc, char *argv[]) {
else
return deserializeSDKDump(options::SDKJsonPaths[0], options::OutputFile);
}
case ActionType::GenerateNameCorrectionTemplate: {
APIDiffItemStore Store;
auto &Paths = options::SDKJsonPaths;
for (unsigned I = 0; I < Paths.size(); I ++)
Store.addStorePath(Paths[I]);
return deserializeNameCorrection(Store, options::OutputFile);
}
case ActionType::None:
llvm::errs() << "Action required\n";
llvm::cl::PrintHelpMessage();