swift-api-digester: remove some unused code. NFC

This commit is contained in:
Xi Ge
2019-05-31 14:21:57 -07:00
parent ba412a9166
commit c9660106a7
3 changed files with 37 additions and 93 deletions

View File

@@ -1917,12 +1917,6 @@ struct ArrayTraits<ArrayRef<StringRef>> {
} // namespace swift } // namespace swift
namespace {// Anonymous namespace. namespace {// Anonymous namespace.
// Serialize a forest of SDKNode trees to the given stream.
static void emitSDKNodeRoot(llvm::raw_ostream &os, SDKNode *&Root) {
json::Output yout(os);
yout << Root;
}
// Deserialize an SDKNode tree. // Deserialize an SDKNode tree.
std::pair<std::unique_ptr<llvm::MemoryBuffer>, SDKNode*> std::pair<std::unique_ptr<llvm::MemoryBuffer>, SDKNode*>
static parseJsonEmit(SDKContext &Ctx, StringRef FileName) { static parseJsonEmit(SDKContext &Ctx, StringRef FileName) {
@@ -1948,18 +1942,6 @@ static parseJsonEmit(SDKContext &Ctx, StringRef FileName) {
} }
return {std::move(FileBufOrErr.get()), Result}; return {std::move(FileBufOrErr.get()), Result};
} }
static std::string getDumpFilePath(StringRef OutputDir, StringRef FileName) {
std::string Path = OutputDir;
Path += "/";
Path += FileName;
int Suffix = 0;
auto ConstructPath = [&]() {
return Path + (Suffix == 0 ? "" : std::to_string(Suffix)) + ".js";
};
for (; fs::exists(ConstructPath()); Suffix ++);
return ConstructPath();
}
} // End of anonymous namespace } // End of anonymous namespace
// Construct all roots vector from a given file where a forest was // Construct all roots vector from a given file where a forest was
@@ -1971,81 +1953,32 @@ void SwiftDeclCollector::deSerialize(StringRef Filename) {
} }
// Serialize the content of all roots to a given file using JSON format. // Serialize the content of all roots to a given file using JSON format.
void SwiftDeclCollector::serialize(StringRef Filename) { void SwiftDeclCollector::serialize(StringRef Filename, SDKNode *Root) {
std::error_code EC; std::error_code EC;
llvm::raw_fd_ostream fs(Filename, EC, llvm::sys::fs::F_None); llvm::raw_fd_ostream fs(Filename, EC, llvm::sys::fs::F_None);
emitSDKNodeRoot(fs, RootNode); json::Output yout(fs);
yout << Root;
} }
int swift::ide::api::dumpSwiftModules(const CompilerInvocation &InitInvok, // Serialize the content of all roots to a given file using JSON format.
const llvm::StringSet<> &ModuleNames, void SwiftDeclCollector::serialize(StringRef Filename) {
StringRef OutputDir, SwiftDeclCollector::serialize(Filename, RootNode);
const std::vector<std::string> PrintApis, }
CheckerOptions Opts) {
if (!fs::exists(OutputDir)) {
llvm::errs() << "Output directory '" << OutputDir << "' does not exist.\n";
return 1;
}
std::vector<ModuleDecl*> Modules; SDKNodeRoot*
swift::ide::api::getSDKNodeRoot(SDKContext &SDKCtx,
const CompilerInvocation &InitInvok,
const llvm::StringSet<> &ModuleNames,
CheckerOptions Opts) {
CompilerInvocation Invocation(InitInvok); CompilerInvocation Invocation(InitInvok);
CompilerInstance CI;
CompilerInstance &CI = SDKCtx.getCompilerInstance();
// Display diagnostics to stderr. // Display diagnostics to stderr.
PrintingDiagnosticConsumer PrintDiags; PrintingDiagnosticConsumer PrintDiags;
CI.addDiagnosticConsumer(&PrintDiags); CI.addDiagnosticConsumer(&PrintDiags);
if (CI.setup(Invocation)) { if (CI.setup(Invocation)) {
llvm::errs() << "Failed to setup the compiler instance\n"; llvm::errs() << "Failed to setup the compiler instance\n";
return 1; return nullptr;
}
auto &Context = CI.getASTContext();
for (auto &Entry : ModuleNames) {
StringRef Name = Entry.first();
if (Opts.Verbose)
llvm::errs() << "Loading module: " << Name << "...\n";
auto *M = Context.getModuleByName(Name);
if (!M) {
if (Opts.Verbose)
llvm::errs() << "Failed to load module: " << Name << '\n';
if (Opts.AbortOnModuleLoadFailure)
return 1;
}
Modules.push_back(M);
}
PrintingDiagnosticConsumer PDC;
SDKContext Ctx(Opts);
Ctx.getDiags().addConsumer(PDC);
for (auto M : Modules) {
SwiftDeclCollector Collector(Ctx);
SmallVector<Decl*, 256> Decls;
M->getTopLevelDecls(Decls);
for (auto D : Decls) {
if (auto VD = dyn_cast<ValueDecl>(D))
Collector.foundDecl(VD, DeclVisibilityKind::VisibleAtTopLevel);
}
std::string Path = getDumpFilePath(OutputDir, M->getName().str());
Collector.serialize(Path);
if (Opts.Verbose)
llvm::errs() << "Dumped to "<< Path << "\n";
}
return 0;
}
int swift::ide::api::dumpSDKContent(const CompilerInvocation &InitInvok,
const llvm::StringSet<> &ModuleNames,
StringRef OutputFile, CheckerOptions Opts) {
CompilerInvocation Invocation(InitInvok);
CompilerInstance CI;
// Display diagnostics to stderr.
PrintingDiagnosticConsumer PrintDiags;
CI.addDiagnosticConsumer(&PrintDiags);
if (CI.setup(Invocation)) {
llvm::errs() << "Failed to setup the compiler instance\n";
return 1;
} }
auto &Ctx = CI.getASTContext(); auto &Ctx = CI.getASTContext();
@@ -2055,7 +1988,7 @@ int swift::ide::api::dumpSDKContent(const CompilerInvocation &InitInvok,
auto *Stdlib = Ctx.getStdlibModule(/*loadIfAbsent=*/true); auto *Stdlib = Ctx.getStdlibModule(/*loadIfAbsent=*/true);
if (!Stdlib) { if (!Stdlib) {
llvm::errs() << "Failed to load Swift stdlib\n"; llvm::errs() << "Failed to load Swift stdlib\n";
return 1; return nullptr;
} }
std::vector<ModuleDecl *> Modules; std::vector<ModuleDecl *> Modules;
@@ -2067,19 +2000,29 @@ int swift::ide::api::dumpSDKContent(const CompilerInvocation &InitInvok,
if (!M) { if (!M) {
llvm::errs() << "Failed to load module: " << Name << '\n'; llvm::errs() << "Failed to load module: " << Name << '\n';
if (Opts.AbortOnModuleLoadFailure) if (Opts.AbortOnModuleLoadFailure)
return 1; return nullptr;
} else { } else {
Modules.push_back(M); Modules.push_back(M);
} }
} }
if (Opts.Verbose) if (Opts.Verbose)
llvm::errs() << "Scanning symbols...\n"; llvm::errs() << "Scanning symbols...\n";
SDKContext SDKCtx(Opts);
SwiftDeclCollector Collector(SDKCtx); SwiftDeclCollector Collector(SDKCtx);
Collector.lookupVisibleDecls(Modules); Collector.lookupVisibleDecls(Modules);
return Collector.getSDKRoot();
}
int swift::ide::api::dumpSDKContent(const CompilerInvocation &InitInvok,
const llvm::StringSet<> &ModuleNames,
StringRef OutputFile, CheckerOptions Opts) {
SDKContext SDKCtx(Opts);
SDKNode *Root = getSDKNodeRoot(SDKCtx, InitInvok, ModuleNames, Opts);
if (!Root)
return 1;
if (Opts.Verbose) if (Opts.Verbose)
llvm::errs() << "Dumping SDK...\n"; llvm::errs() << "Dumping SDK...\n";
Collector.serialize(OutputFile); SwiftDeclCollector::serialize(OutputFile, Root);
if (Opts.Verbose) if (Opts.Verbose)
llvm::errs() << "Dumped to "<< OutputFile << "\n"; llvm::errs() << "Dumped to "<< OutputFile << "\n";
return 0; return 0;

View File

@@ -150,6 +150,7 @@ struct CheckerOptions {
}; };
class SDKContext { class SDKContext {
CompilerInstance CI;
llvm::StringSet<> TextData; llvm::StringSet<> TextData;
llvm::BumpPtrAllocator Allocator; llvm::BumpPtrAllocator Allocator;
SourceManager SourceMgr; SourceManager SourceMgr;
@@ -188,6 +189,7 @@ public:
DiagnosticEngine &getDiags() { DiagnosticEngine &getDiags() {
return Diags; return Diags;
} }
CompilerInstance &getCompilerInstance() { return CI; }
StringRef getPlatformIntroVersion(Decl *D, PlatformKind Kind); StringRef getPlatformIntroVersion(Decl *D, PlatformKind Kind);
StringRef getLanguageIntroVersion(Decl *D); StringRef getLanguageIntroVersion(Decl *D);
bool isEqual(const SDKNode &Left, const SDKNode &Right); bool isEqual(const SDKNode &Left, const SDKNode &Right);
@@ -683,6 +685,7 @@ public:
// Serialize the content of all roots to a given file using JSON format. // Serialize the content of all roots to a given file using JSON format.
void serialize(StringRef Filename); void serialize(StringRef Filename);
static void serialize(StringRef Filename, SDKNode *Root);
// After collecting decls, either from imported modules or from a previously // After collecting decls, either from imported modules or from a previously
// serialized JSON file, using this function to get the root of the SDK. // serialized JSON file, using this function to get the root of the SDK.
@@ -723,6 +726,11 @@ int dumpSwiftModules(const CompilerInvocation &InitInvok,
const std::vector<std::string> PrintApis, const std::vector<std::string> PrintApis,
CheckerOptions Opts); CheckerOptions Opts);
SDKNodeRoot *getSDKNodeRoot(SDKContext &SDKCtx,
const CompilerInvocation &InitInvok,
const llvm::StringSet<> &ModuleNames,
CheckerOptions Opts);
int dumpSDKContent(const CompilerInvocation &InitInvok, int dumpSDKContent(const CompilerInvocation &InitInvok,
const llvm::StringSet<> &ModuleNames, const llvm::StringSet<> &ModuleNames,
StringRef OutputFile, CheckerOptions Opts); StringRef OutputFile, CheckerOptions Opts);

View File

@@ -40,7 +40,6 @@ namespace {
enum class ActionType { enum class ActionType {
None, None,
DumpSDK, DumpSDK,
DumpSwiftModules,
MigratorGen, MigratorGen,
DiagnoseSDKs, DiagnoseSDKs,
// The following two are for testing purposes // The following two are for testing purposes
@@ -144,9 +143,6 @@ Action(llvm::cl::desc("Mode:"), llvm::cl::init(ActionType::None),
clEnumValN(ActionType::DumpSDK, clEnumValN(ActionType::DumpSDK,
"dump-sdk", "dump-sdk",
"Dump SDK content to JSON file"), "Dump SDK content to JSON file"),
clEnumValN(ActionType::DumpSwiftModules,
"dump-swift",
"dump swift modules in SDK"),
clEnumValN(ActionType::MigratorGen, clEnumValN(ActionType::MigratorGen,
"generate-migration-script", "generate-migration-script",
"Compare SDK content in JSON file and generate migration script"), "Compare SDK content in JSON file and generate migration script"),
@@ -2404,9 +2400,6 @@ int main(int argc, char *argv[]) {
for (auto Name : options::ApisPrintUsrs) for (auto Name : options::ApisPrintUsrs)
PrintApis.push_back(Name); PrintApis.push_back(Name);
switch (options::Action) { switch (options::Action) {
case ActionType::DumpSwiftModules:
return (prepareForDump(argv[0], InitInvok, Modules)) ? 1 :
dumpSwiftModules(InitInvok, Modules, options::OutputFile, PrintApis, Opts);
case ActionType::DumpSDK: case ActionType::DumpSDK:
return (prepareForDump(argv[0], InitInvok, Modules)) ? 1 : return (prepareForDump(argv[0], InitInvok, Modules)) ? 1 :
dumpSDKContent(InitInvok, Modules, options::OutputFile, Opts); dumpSDKContent(InitInvok, Modules, options::OutputFile, Opts);