Skip to content

Commit ce57492

Browse files
committed
swift-api-digester: add an action to generate empty baselines
1 parent 0517e97 commit ce57492

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
@@ -2070,11 +2070,17 @@ void SwiftDeclCollector::serialize(StringRef Filename) {
20702070
SwiftDeclCollector::serialize(Filename, RootNode);
20712071
}
20722072

2073+
SDKNodeRoot *
2074+
swift::ide::api::getEmptySDKNodeRoot(SDKContext &SDKCtx) {
2075+
SwiftDeclCollector Collector(SDKCtx);
2076+
return Collector.getSDKRoot();
2077+
}
2078+
20732079
SDKNodeRoot*
20742080
swift::ide::api::getSDKNodeRoot(SDKContext &SDKCtx,
20752081
const CompilerInvocation &InitInvok,
2076-
const llvm::StringSet<> &ModuleNames,
2077-
CheckerOptions Opts) {
2082+
const llvm::StringSet<> &ModuleNames) {
2083+
CheckerOptions Opts = SDKCtx.getOpts();
20782084
CompilerInvocation Invocation(InitInvok);
20792085

20802086
CompilerInstance &CI = SDKCtx.newCompilerInstance();
@@ -2120,18 +2126,24 @@ swift::ide::api::getSDKNodeRoot(SDKContext &SDKCtx,
21202126
return Collector.getSDKRoot();
21212127
}
21222128

2129+
void swift::ide::api::dumpSDKRoot(SDKNodeRoot *Root, StringRef OutputFile) {
2130+
assert(Root);
2131+
auto Opts = Root->getSDKContext().getOpts();
2132+
if (Opts.Verbose)
2133+
llvm::errs() << "Dumping SDK...\n";
2134+
SwiftDeclCollector::serialize(OutputFile, Root);
2135+
if (Opts.Verbose)
2136+
llvm::errs() << "Dumped to "<< OutputFile << "\n";
2137+
}
2138+
21232139
int swift::ide::api::dumpSDKContent(const CompilerInvocation &InitInvok,
21242140
const llvm::StringSet<> &ModuleNames,
21252141
StringRef OutputFile, CheckerOptions Opts) {
21262142
SDKContext SDKCtx(Opts);
2127-
SDKNode *Root = getSDKNodeRoot(SDKCtx, InitInvok, ModuleNames, Opts);
2143+
SDKNodeRoot *Root = getSDKNodeRoot(SDKCtx, InitInvok, ModuleNames);
21282144
if (!Root)
21292145
return 1;
2130-
if (Opts.Verbose)
2131-
llvm::errs() << "Dumping SDK...\n";
2132-
SwiftDeclCollector::serialize(OutputFile, Root);
2133-
if (Opts.Verbose)
2134-
llvm::errs() << "Dumped to "<< OutputFile << "\n";
2146+
dumpSDKRoot(Root, OutputFile);
21352147
return 0;
21362148
}
21372149

tools/swift-api-digester/ModuleAnalyzerNodes.h

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

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

753756
int dumpSDKContent(const CompilerInvocation &InitInvok,
754757
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",
@@ -2464,13 +2468,12 @@ static CheckerOptions getCheckOpts(int argc, char *argv[]) {
24642468
return Opts;
24652469
}
24662470

2467-
static SDKNodeRoot *getSDKRoot(const char *Main, SDKContext &Ctx,
2468-
CheckerOptions Opts, bool IsBaseline) {
2471+
static SDKNodeRoot *getSDKRoot(const char *Main, SDKContext &Ctx, bool IsBaseline) {
24692472
CompilerInvocation Invok;
24702473
llvm::StringSet<> Modules;
24712474
if (prepareForDump(Main, Invok, Modules, IsBaseline))
24722475
return nullptr;
2473-
return getSDKNodeRoot(Ctx, Invok, Modules, Opts);
2476+
return getSDKNodeRoot(Ctx, Invok, Modules);
24742477
}
24752478

24762479
static bool hasBaselineInput() {
@@ -2521,8 +2524,8 @@ int main(int argc, char *argv[]) {
25212524
std::move(protocolWhitelist));
25222525
else {
25232526
SDKContext Ctx(Opts);
2524-
return diagnoseModuleChange(Ctx, getSDKRoot(argv[0], Ctx, Opts, true),
2525-
getSDKRoot(argv[0], Ctx, Opts, false),
2527+
return diagnoseModuleChange(Ctx, getSDKRoot(argv[0], Ctx, true),
2528+
getSDKRoot(argv[0], Ctx, false),
25262529
options::OutputFile,
25272530
std::move(protocolWhitelist));
25282531
}
@@ -2551,6 +2554,11 @@ int main(int argc, char *argv[]) {
25512554
Store.addStorePath(Paths[I]);
25522555
return deserializeNameCorrection(Store, options::OutputFile);
25532556
}
2557+
case ActionType::GenerateEmptyBaseline: {
2558+
SDKContext Ctx(Opts);
2559+
dumpSDKRoot(getEmptySDKNodeRoot(Ctx), options::OutputFile);
2560+
return 0;
2561+
}
25542562
case ActionType::FindUsr: {
25552563
if (options::SDKJsonPaths.size() != 1) {
25562564
llvm::cl::PrintHelpMessage();

0 commit comments

Comments
 (0)