@@ -1917,12 +1917,6 @@ struct ArrayTraits<ArrayRef<StringRef>> {
1917
1917
} // namespace swift
1918
1918
1919
1919
namespace {// Anonymous namespace.
1920
- // Serialize a forest of SDKNode trees to the given stream.
1921
- static void emitSDKNodeRoot (llvm::raw_ostream &os, SDKNode *&Root) {
1922
- json::Output yout (os);
1923
- yout << Root;
1924
- }
1925
-
1926
1920
// Deserialize an SDKNode tree.
1927
1921
std::pair<std::unique_ptr<llvm::MemoryBuffer>, SDKNode*>
1928
1922
static parseJsonEmit (SDKContext &Ctx, StringRef FileName) {
@@ -1948,18 +1942,6 @@ static parseJsonEmit(SDKContext &Ctx, StringRef FileName) {
1948
1942
}
1949
1943
return {std::move (FileBufOrErr.get ()), Result};
1950
1944
}
1951
-
1952
- static std::string getDumpFilePath (StringRef OutputDir, StringRef FileName) {
1953
- std::string Path = OutputDir;
1954
- Path += " /" ;
1955
- Path += FileName;
1956
- int Suffix = 0 ;
1957
- auto ConstructPath = [&]() {
1958
- return Path + (Suffix == 0 ? " " : std::to_string (Suffix)) + " .js" ;
1959
- };
1960
- for (; fs::exists (ConstructPath ()); Suffix ++);
1961
- return ConstructPath ();
1962
- }
1963
1945
} // End of anonymous namespace
1964
1946
1965
1947
// Construct all roots vector from a given file where a forest was
@@ -1971,81 +1953,32 @@ void SwiftDeclCollector::deSerialize(StringRef Filename) {
1971
1953
}
1972
1954
1973
1955
// Serialize the content of all roots to a given file using JSON format.
1974
- void SwiftDeclCollector::serialize (StringRef Filename) {
1956
+ void SwiftDeclCollector::serialize (StringRef Filename, SDKNode *Root ) {
1975
1957
std::error_code EC;
1976
1958
llvm::raw_fd_ostream fs (Filename, EC, llvm::sys::fs::F_None);
1977
- emitSDKNodeRoot (fs, RootNode);
1959
+ json::Output yout (fs);
1960
+ yout << Root;
1978
1961
}
1979
1962
1980
- int swift::ide::api::dumpSwiftModules (const CompilerInvocation &InitInvok,
1981
- const llvm::StringSet<> &ModuleNames,
1982
- StringRef OutputDir,
1983
- const std::vector<std::string> PrintApis,
1984
- CheckerOptions Opts) {
1985
- if (!fs::exists (OutputDir)) {
1986
- llvm::errs () << " Output directory '" << OutputDir << " ' does not exist.\n " ;
1987
- return 1 ;
1988
- }
1989
-
1990
- std::vector<ModuleDecl*> Modules;
1991
- CompilerInvocation Invocation (InitInvok);
1992
- CompilerInstance CI;
1993
- // Display diagnostics to stderr.
1994
- PrintingDiagnosticConsumer PrintDiags;
1995
- CI.addDiagnosticConsumer (&PrintDiags);
1996
- if (CI.setup (Invocation)) {
1997
- llvm::errs () << " Failed to setup the compiler instance\n " ;
1998
- return 1 ;
1999
- }
2000
-
2001
- auto &Context = CI.getASTContext ();
2002
-
2003
- for (auto &Entry : ModuleNames) {
2004
- StringRef Name = Entry.first ();
2005
- if (Opts.Verbose )
2006
- llvm::errs () << " Loading module: " << Name << " ...\n " ;
2007
- auto *M = Context.getModuleByName (Name);
2008
- if (!M) {
2009
- if (Opts.Verbose )
2010
- llvm::errs () << " Failed to load module: " << Name << ' \n ' ;
2011
- if (Opts.AbortOnModuleLoadFailure )
2012
- return 1 ;
2013
- }
2014
- Modules.push_back (M);
2015
- }
2016
-
2017
- PrintingDiagnosticConsumer PDC;
2018
- SDKContext Ctx (Opts);
2019
- Ctx.getDiags ().addConsumer (PDC);
2020
-
2021
- for (auto M : Modules) {
2022
- SwiftDeclCollector Collector (Ctx);
2023
- SmallVector<Decl*, 256 > Decls;
2024
- M->getTopLevelDecls (Decls);
2025
- for (auto D : Decls) {
2026
- if (auto VD = dyn_cast<ValueDecl>(D))
2027
- Collector.foundDecl (VD, DeclVisibilityKind::VisibleAtTopLevel);
2028
- }
2029
- std::string Path = getDumpFilePath (OutputDir, M->getName ().str ());
2030
- Collector.serialize (Path);
2031
- if (Opts.Verbose )
2032
- llvm::errs () << " Dumped to " << Path << " \n " ;
2033
- }
2034
- return 0 ;
1963
+ // Serialize the content of all roots to a given file using JSON format.
1964
+ void SwiftDeclCollector::serialize (StringRef Filename) {
1965
+ SwiftDeclCollector::serialize (Filename, RootNode);
2035
1966
}
2036
1967
2037
- int swift::ide::api::dumpSDKContent (const CompilerInvocation &InitInvok,
2038
- const llvm::StringSet<> &ModuleNames,
2039
- StringRef OutputFile, CheckerOptions Opts) {
1968
+ SDKNodeRoot*
1969
+ swift::ide::api::getSDKNodeRoot (SDKContext &SDKCtx,
1970
+ const CompilerInvocation &InitInvok,
1971
+ const llvm::StringSet<> &ModuleNames,
1972
+ CheckerOptions Opts) {
2040
1973
CompilerInvocation Invocation (InitInvok);
2041
1974
2042
- CompilerInstance CI ;
1975
+ CompilerInstance &CI = SDKCtx. getCompilerInstance () ;
2043
1976
// Display diagnostics to stderr.
2044
1977
PrintingDiagnosticConsumer PrintDiags;
2045
1978
CI.addDiagnosticConsumer (&PrintDiags);
2046
1979
if (CI.setup (Invocation)) {
2047
1980
llvm::errs () << " Failed to setup the compiler instance\n " ;
2048
- return 1 ;
1981
+ return nullptr ;
2049
1982
}
2050
1983
2051
1984
auto &Ctx = CI.getASTContext ();
@@ -2055,7 +1988,7 @@ int swift::ide::api::dumpSDKContent(const CompilerInvocation &InitInvok,
2055
1988
auto *Stdlib = Ctx.getStdlibModule (/* loadIfAbsent=*/ true );
2056
1989
if (!Stdlib) {
2057
1990
llvm::errs () << " Failed to load Swift stdlib\n " ;
2058
- return 1 ;
1991
+ return nullptr ;
2059
1992
}
2060
1993
2061
1994
std::vector<ModuleDecl *> Modules;
@@ -2067,19 +2000,29 @@ int swift::ide::api::dumpSDKContent(const CompilerInvocation &InitInvok,
2067
2000
if (!M) {
2068
2001
llvm::errs () << " Failed to load module: " << Name << ' \n ' ;
2069
2002
if (Opts.AbortOnModuleLoadFailure )
2070
- return 1 ;
2003
+ return nullptr ;
2071
2004
} else {
2072
2005
Modules.push_back (M);
2073
2006
}
2074
2007
}
2075
2008
if (Opts.Verbose )
2076
2009
llvm::errs () << " Scanning symbols...\n " ;
2077
- SDKContext SDKCtx (Opts);
2010
+
2078
2011
SwiftDeclCollector Collector (SDKCtx);
2079
2012
Collector.lookupVisibleDecls (Modules);
2013
+ return Collector.getSDKRoot ();
2014
+ }
2015
+
2016
+ int swift::ide::api::dumpSDKContent (const CompilerInvocation &InitInvok,
2017
+ const llvm::StringSet<> &ModuleNames,
2018
+ StringRef OutputFile, CheckerOptions Opts) {
2019
+ SDKContext SDKCtx (Opts);
2020
+ SDKNode *Root = getSDKNodeRoot (SDKCtx, InitInvok, ModuleNames, Opts);
2021
+ if (!Root)
2022
+ return 1 ;
2080
2023
if (Opts.Verbose )
2081
2024
llvm::errs () << " Dumping SDK...\n " ;
2082
- Collector. serialize (OutputFile);
2025
+ SwiftDeclCollector:: serialize (OutputFile, Root );
2083
2026
if (Opts.Verbose )
2084
2027
llvm::errs () << " Dumped to " << OutputFile << " \n " ;
2085
2028
return 0 ;
0 commit comments