Skip to content

Commit 2aaeb37

Browse files
Virtualize swift abi description file
1 parent af6c515 commit 2aaeb37

File tree

4 files changed

+56
-29
lines changed

4 files changed

+56
-29
lines changed

include/swift/APIDigester/ModuleAnalyzerNodes.h

Lines changed: 7 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -784,8 +784,8 @@ class SwiftDeclCollector: public VisibleDeclConsumer {
784784
void deSerialize(StringRef Filename);
785785

786786
// Serialize the content of all roots to a given file using JSON format.
787-
void serialize(StringRef Filename);
788-
static void serialize(StringRef Filename, SDKNode *Root, PayLoad otherInfo);
787+
void serialize(llvm::raw_ostream &os);
788+
static void serialize(llvm::raw_ostream &os, SDKNode *Root, PayLoad otherInfo);
789789

790790
// After collecting decls, either from imported modules or from a previously
791791
// serialized JSON file, using this function to get the root of the SDK.
@@ -835,14 +835,15 @@ SDKNodeRoot *getSDKNodeRoot(SDKContext &SDKCtx,
835835

836836
SDKNodeRoot *getEmptySDKNodeRoot(SDKContext &SDKCtx);
837837

838-
void dumpSDKRoot(SDKNodeRoot *Root, PayLoad load, StringRef OutputFile);
839-
void dumpSDKRoot(SDKNodeRoot *Root, StringRef OutputFile);
838+
void dumpSDKRoot(SDKNodeRoot *Root, PayLoad load, llvm::raw_ostream &os);
839+
void dumpSDKRoot(SDKNodeRoot *Root, llvm::raw_ostream &os);
840840

841841
int dumpSDKContent(const CompilerInvocation &InitInvoke,
842842
const llvm::StringSet<> &ModuleNames,
843-
StringRef OutputFile, CheckerOptions Opts);
843+
llvm::raw_ostream &os, CheckerOptions Opts);
844844

845-
void dumpModuleContent(ModuleDecl *MD, StringRef OutputFile, bool ABI, bool Empty);
845+
void dumpModuleContent(ModuleDecl *MD, llvm::raw_ostream &os, bool ABI,
846+
bool Empty);
846847

847848
/// Mostly for testing purposes, this function de-serializes the SDK dump in
848849
/// dumpPath and re-serialize them to OutputPath. If the tool performs correctly,

lib/APIDigester/ModuleAnalyzerNodes.cpp

Lines changed: 15 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -2470,11 +2470,10 @@ void SwiftDeclCollector::deSerialize(StringRef Filename) {
24702470
}
24712471

24722472
// Serialize the content of all roots to a given file using JSON format.
2473-
void SwiftDeclCollector::serialize(StringRef Filename, SDKNode *Root,
2473+
void SwiftDeclCollector::serialize(llvm::raw_ostream &os, SDKNode *Root,
24742474
PayLoad OtherInfo) {
24752475
std::error_code EC;
2476-
llvm::raw_fd_ostream fs(Filename, EC, llvm::sys::fs::OF_None);
2477-
json::Output yout(fs);
2476+
json::Output yout(os);
24782477
assert(Root->getKind() == SDKNodeKind::Root);
24792478
SDKNodeRoot &root = *static_cast<SDKNodeRoot*>(Root);
24802479
yout.beginObject();
@@ -2486,8 +2485,8 @@ void SwiftDeclCollector::serialize(StringRef Filename, SDKNode *Root,
24862485
}
24872486

24882487
// Serialize the content of all roots to a given file using JSON format.
2489-
void SwiftDeclCollector::serialize(StringRef Filename) {
2490-
SwiftDeclCollector::serialize(Filename, RootNode, PayLoad());
2488+
void SwiftDeclCollector::serialize(llvm::raw_ostream &os) {
2489+
SwiftDeclCollector::serialize(os, RootNode, PayLoad());
24912490
}
24922491

24932492
SDKNodeRoot *
@@ -2550,28 +2549,28 @@ swift::ide::api::getSDKNodeRoot(SDKContext &SDKCtx,
25502549
}
25512550

25522551
void swift::ide::api::dumpSDKRoot(SDKNodeRoot *Root, PayLoad load,
2553-
StringRef OutputFile) {
2552+
llvm::raw_ostream &os) {
25542553
assert(Root);
25552554
auto Opts = Root->getSDKContext().getOpts();
25562555
if (Opts.Verbose)
25572556
llvm::errs() << "Dumping SDK...\n";
2558-
SwiftDeclCollector::serialize(OutputFile, Root, load);
2559-
if (Opts.Verbose)
2560-
llvm::errs() << "Dumped to "<< OutputFile << "\n";
2557+
SwiftDeclCollector::serialize(os, Root, load);
2558+
// if (Opts.Verbose)
2559+
// llvm::errs() << "Dumped to "<< OutputFile << "\n";
25612560
}
25622561

2563-
void swift::ide::api::dumpSDKRoot(SDKNodeRoot *Root, StringRef OutputFile) {
2564-
dumpSDKRoot(Root, PayLoad(), OutputFile);
2562+
void swift::ide::api::dumpSDKRoot(SDKNodeRoot *Root, llvm::raw_ostream &os) {
2563+
dumpSDKRoot(Root, PayLoad(), os);
25652564
}
25662565

25672566
int swift::ide::api::dumpSDKContent(const CompilerInvocation &InitInvoke,
25682567
const llvm::StringSet<> &ModuleNames,
2569-
StringRef OutputFile, CheckerOptions Opts) {
2568+
llvm::raw_ostream &os, CheckerOptions Opts) {
25702569
SDKContext SDKCtx(Opts);
25712570
SDKNodeRoot *Root = getSDKNodeRoot(SDKCtx, InitInvoke, ModuleNames);
25722571
if (!Root)
25732572
return 1;
2574-
dumpSDKRoot(Root, OutputFile);
2573+
dumpSDKRoot(Root, os);
25752574
return 0;
25762575
}
25772576

@@ -2589,11 +2588,11 @@ int swift::ide::api::deserializeSDKDump(StringRef dumpPath, StringRef OutputPath
25892588

25902589
SwiftDeclCollector Collector(Ctx);
25912590
Collector.deSerialize(dumpPath);
2592-
Collector.serialize(OutputPath);
2591+
Collector.serialize(FS);
25932592
return 0;
25942593
}
25952594

2596-
void swift::ide::api::dumpModuleContent(ModuleDecl *MD, StringRef OutputFile,
2595+
void swift::ide::api::dumpModuleContent(ModuleDecl *MD, llvm::raw_ostream &os,
25972596
bool ABI, bool Empty) {
25982597
CheckerOptions opts;
25992598
opts.ABI = ABI;
@@ -2610,7 +2609,7 @@ void swift::ide::api::dumpModuleContent(ModuleDecl *MD, StringRef OutputFile,
26102609
PayLoad payload;
26112610
SWIFT_DEFER {
26122611
payload.allContsValues = &extractor.getAllConstValues();
2613-
dumpSDKRoot(collector.getSDKRoot(), payload, OutputFile);
2612+
dumpSDKRoot(collector.getSDKRoot(), payload, os);
26142613
};
26152614
if (Empty) {
26162615
return;

lib/DriverTool/swift_api_digester_main.cpp

Lines changed: 18 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -38,6 +38,7 @@
3838
#include "swift/Parse/ParseVersion.h"
3939
#include "llvm/ADT/IntrusiveRefCntPtr.h"
4040
#include "llvm/Support/VirtualOutputBackends.h"
41+
#include "llvm/Support/raw_ostream.h"
4142
#include <functional>
4243

4344
using namespace swift;
@@ -2539,13 +2540,18 @@ class SwiftAPIDigesterInvocation {
25392540
switch (Action) {
25402541
case ActionType::DumpSDK: {
25412542
llvm::StringSet<> Modules;
2543+
auto JsonOut =
2544+
getJsonOutputFilePath(InitInvoke.getLangOptions().Target,
2545+
CheckerOpts.ABI, OutputFile, OutputDir);
2546+
std::error_code EC;
2547+
llvm::raw_fd_ostream fs(JsonOut, EC);
2548+
if (EC) {
2549+
llvm::errs() << "Cannot open JSON output file: " << JsonOut << "\n";
2550+
return 1;
2551+
}
25422552
return (prepareForDump(InitInvoke, Modules))
25432553
? 1
2544-
: dumpSDKContent(InitInvoke, Modules,
2545-
getJsonOutputFilePath(
2546-
InitInvoke.getLangOptions().Target,
2547-
CheckerOpts.ABI, OutputFile, OutputDir),
2548-
CheckerOpts);
2554+
: dumpSDKContent(InitInvoke, Modules, fs, CheckerOpts);
25492555
}
25502556
case ActionType::MigratorGen:
25512557
case ActionType::DiagnoseSDKs: {
@@ -2609,7 +2615,13 @@ class SwiftAPIDigesterInvocation {
26092615
}
26102616
case ActionType::GenerateEmptyBaseline: {
26112617
SDKContext Ctx(CheckerOpts);
2612-
dumpSDKRoot(getEmptySDKNodeRoot(Ctx), OutputFile);
2618+
std::error_code EC;
2619+
llvm::raw_fd_ostream fs(OutputFile, EC);
2620+
if (EC) {
2621+
llvm::errs() << "Cannot open output file: " << OutputFile << "\n";
2622+
return 1;
2623+
}
2624+
dumpSDKRoot(getEmptySDKNodeRoot(Ctx), fs);
26132625
return 0;
26142626
}
26152627
case ActionType::FindUsr: {

lib/Frontend/Serialization.cpp

Lines changed: 16 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -12,6 +12,7 @@
1212

1313
#include "swift/Serialization/Serialization.h"
1414
#include "swift/APIDigester/ModuleAnalyzerNodes.h"
15+
#include "swift/AST/DiagnosticsFrontend.h"
1516
#include "swift/AST/FileSystem.h"
1617
#include "swift/Subsystems.h"
1718
#include "swift/SymbolGraphGen/SymbolGraphGen.h"
@@ -36,8 +37,22 @@ static void emitABIDescriptor(ModuleOrSourceFile DC,
3637
using namespace swift::ide::api;
3738
if (!options.ABIDescriptorPath.empty()) {
3839
if (DC.is<ModuleDecl *>()) {
39-
dumpModuleContent(DC.get<ModuleDecl *>(), options.ABIDescriptorPath, true,
40+
auto &OutputBackend = getContext(DC).getOutputBackend();
41+
auto ABIDesFile = OutputBackend.createFile(options.ABIDescriptorPath);
42+
if (!ABIDesFile) {
43+
getContext(DC).Diags.diagnose(SourceLoc(), diag::cannot_open_file,
44+
options.ABIDescriptorPath,
45+
toString(ABIDesFile.takeError()));
46+
return;
47+
}
48+
dumpModuleContent(DC.get<ModuleDecl *>(), *ABIDesFile, true,
4049
options.emptyABIDescriptor);
50+
if (auto E = ABIDesFile->keep()) {
51+
getContext(DC).Diags.diagnose(SourceLoc(), diag::cannot_open_file,
52+
options.ABIDescriptorPath,
53+
toString(std::move(E)));
54+
return;
55+
}
4156
}
4257
}
4358
}

0 commit comments

Comments
 (0)