Skip to content

Commit 4a9a71f

Browse files
committed
swift-api-digester: add an action to generate empty baselines
1 parent 0be3c50 commit 4a9a71f

File tree

5 files changed

+53
-16
lines changed

5 files changed

+53
-16
lines changed
Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,6 @@
1+
{
2+
"kind": "Root",
3+
"name": "TopLevel",
4+
"printedName": "TopLevel",
5+
"json_format_version": 1
6+
}
Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,8 @@
1+
// REQUIRES: OS=macosx
2+
// RUN: %empty-directory(%t.mod)
3+
// RUN: %empty-directory(%t.sdk)
4+
// RUN: %empty-directory(%t.module-cache)
5+
// RUN: %api-digester -generate-empty-baseline -o %t.result -avoid-tool-args
6+
// RUN: diff -u %S/Outputs/empty-baseline.json %t.result
7+
// RUN: %api-digester -deserialize-sdk -input-paths %S/Outputs/empty-baseline.json -o %t.result
8+
// RUN: diff -u %S/Outputs/empty-baseline.json %t.result

tools/swift-api-digester/ModuleAnalyzerNodes.cpp

Lines changed: 20 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -2065,11 +2065,17 @@ void SwiftDeclCollector::serialize(StringRef Filename) {
20652065
SwiftDeclCollector::serialize(Filename, RootNode);
20662066
}
20672067

2068+
SDKNodeRoot *
2069+
swift::ide::api::getEmptySDKNodeRoot(SDKContext &SDKCtx) {
2070+
SwiftDeclCollector Collector(SDKCtx);
2071+
return Collector.getSDKRoot();
2072+
}
2073+
20682074
SDKNodeRoot*
20692075
swift::ide::api::getSDKNodeRoot(SDKContext &SDKCtx,
20702076
const CompilerInvocation &InitInvok,
2071-
const llvm::StringSet<> &ModuleNames,
2072-
CheckerOptions Opts) {
2077+
const llvm::StringSet<> &ModuleNames) {
2078+
CheckerOptions Opts = SDKCtx.getOpts();
20732079
CompilerInvocation Invocation(InitInvok);
20742080

20752081
CompilerInstance &CI = SDKCtx.newCompilerInstance();
@@ -2115,18 +2121,24 @@ swift::ide::api::getSDKNodeRoot(SDKContext &SDKCtx,
21152121
return Collector.getSDKRoot();
21162122
}
21172123

2124+
void swift::ide::api::dumpSDKRoot(SDKNodeRoot *Root, StringRef OutputFile) {
2125+
assert(Root);
2126+
auto Opts = Root->getSDKContext().getOpts();
2127+
if (Opts.Verbose)
2128+
llvm::errs() << "Dumping SDK...\n";
2129+
SwiftDeclCollector::serialize(OutputFile, Root);
2130+
if (Opts.Verbose)
2131+
llvm::errs() << "Dumped to "<< OutputFile << "\n";
2132+
}
2133+
21182134
int swift::ide::api::dumpSDKContent(const CompilerInvocation &InitInvok,
21192135
const llvm::StringSet<> &ModuleNames,
21202136
StringRef OutputFile, CheckerOptions Opts) {
21212137
SDKContext SDKCtx(Opts);
2122-
SDKNode *Root = getSDKNodeRoot(SDKCtx, InitInvok, ModuleNames, Opts);
2138+
SDKNodeRoot *Root = getSDKNodeRoot(SDKCtx, InitInvok, ModuleNames);
21232139
if (!Root)
21242140
return 1;
2125-
if (Opts.Verbose)
2126-
llvm::errs() << "Dumping SDK...\n";
2127-
SwiftDeclCollector::serialize(OutputFile, Root);
2128-
if (Opts.Verbose)
2129-
llvm::errs() << "Dumped to "<< OutputFile << "\n";
2141+
dumpSDKRoot(Root, OutputFile);
21302142
return 0;
21312143
}
21322144

tools/swift-api-digester/ModuleAnalyzerNodes.h

Lines changed: 5 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -748,8 +748,11 @@ int dumpSwiftModules(const CompilerInvocation &InitInvok,
748748

749749
SDKNodeRoot *getSDKNodeRoot(SDKContext &SDKCtx,
750750
const CompilerInvocation &InitInvok,
751-
const llvm::StringSet<> &ModuleNames,
752-
CheckerOptions Opts);
751+
const llvm::StringSet<> &ModuleNames);
752+
753+
SDKNodeRoot *getEmptySDKNodeRoot(SDKContext &SDKCtx);
754+
755+
void dumpSDKRoot(SDKNodeRoot *Root, StringRef OutputFile);
753756

754757
int dumpSDKContent(const CompilerInvocation &InitInvok,
755758
const llvm::StringSet<> &ModuleNames,

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

Lines changed: 14 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -48,6 +48,7 @@ namespace {
4848
DeserializeSDK,
4949
GenerateNameCorrectionTemplate,
5050
FindUsr,
51+
GenerateEmptyBaseline,
5152
};
5253
} // end anonymous namespace
5354

@@ -173,7 +174,10 @@ Action(llvm::cl::desc("Mode:"), llvm::cl::init(ActionType::None),
173174
"Find USR for decls by given condition"),
174175
clEnumValN(ActionType::GenerateNameCorrectionTemplate,
175176
"generate-name-correction",
176-
"Generate name correction template")));
177+
"Generate name correction template"),
178+
clEnumValN(ActionType::GenerateEmptyBaseline,
179+
"generate-empty-baseline",
180+
"Generate an empty baseline")));
177181

178182
static llvm::cl::list<std::string>
179183
SDKJsonPaths("input-paths",
@@ -2480,13 +2484,12 @@ static CheckerOptions getCheckOpts(int argc, char *argv[]) {
24802484
return Opts;
24812485
}
24822486

2483-
static SDKNodeRoot *getSDKRoot(const char *Main, SDKContext &Ctx,
2484-
CheckerOptions Opts, bool IsBaseline) {
2487+
static SDKNodeRoot *getSDKRoot(const char *Main, SDKContext &Ctx, bool IsBaseline) {
24852488
CompilerInvocation Invok;
24862489
llvm::StringSet<> Modules;
24872490
if (prepareForDump(Main, Invok, Modules, IsBaseline))
24882491
return nullptr;
2489-
return getSDKNodeRoot(Ctx, Invok, Modules, Opts);
2492+
return getSDKNodeRoot(Ctx, Invok, Modules);
24902493
}
24912494

24922495
static bool hasBaselineInput() {
@@ -2537,8 +2540,8 @@ int main(int argc, char *argv[]) {
25372540
std::move(protocolWhitelist));
25382541
else {
25392542
SDKContext Ctx(Opts);
2540-
return diagnoseModuleChange(Ctx, getSDKRoot(argv[0], Ctx, Opts, true),
2541-
getSDKRoot(argv[0], Ctx, Opts, false),
2543+
return diagnoseModuleChange(Ctx, getSDKRoot(argv[0], Ctx, true),
2544+
getSDKRoot(argv[0], Ctx, false),
25422545
options::OutputFile,
25432546
std::move(protocolWhitelist));
25442547
}
@@ -2567,6 +2570,11 @@ int main(int argc, char *argv[]) {
25672570
Store.addStorePath(Paths[I]);
25682571
return deserializeNameCorrection(Store, options::OutputFile);
25692572
}
2573+
case ActionType::GenerateEmptyBaseline: {
2574+
SDKContext Ctx(Opts);
2575+
dumpSDKRoot(getEmptySDKNodeRoot(Ctx), options::OutputFile);
2576+
return 0;
2577+
}
25702578
case ActionType::FindUsr: {
25712579
if (options::SDKJsonPaths.size() != 1) {
25722580
llvm::cl::PrintHelpMessage();

0 commit comments

Comments
 (0)