Skip to content

Re-apply "swift-api-digester: teach the tool to find framework-specific baselines from relative path" #26985

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
3 changes: 1 addition & 2 deletions test/api-digester/stability-stdlib-abi-with-asserts.swift
Original file line number Diff line number Diff line change
Expand Up @@ -2,8 +2,7 @@
// REQUIRES: swift_stdlib_asserts
// RUN: %empty-directory(%t.tmp)
// mkdir %t.tmp/module-cache && mkdir %t.tmp/dummy.sdk
// RUN: %api-digester -dump-sdk -module Swift -o %t.tmp/current-stdlib.json -module-cache-path %t.tmp/module-cache -sdk %t.tmp/dummy.sdk -abi -avoid-location
// RUN: %api-digester -diagnose-sdk -input-paths %S/Inputs/stdlib-stable-abi.json -input-paths %t.tmp/current-stdlib.json -abi -o %t.tmp/changes.txt -v
// RUN: %api-digester -diagnose-sdk -module Swift -o %t.tmp/changes.txt -module-cache-path %t.tmp/module-cache -sdk %t.tmp/dummy.sdk -abi -avoid-location
// RUN: %clang -E -P -x c %S/Outputs/stability-stdlib-abi.without.asserts.swift.expected -o - > %t.tmp/stability-stdlib-abi.swift.expected
// RUN: %clang -E -P -x c %S/Outputs/stability-stdlib-abi.asserts.additional.swift.expected -o - >> %t.tmp/stability-stdlib-abi.swift.expected
// RUN: %clang -E -P -x c %t.tmp/stability-stdlib-abi.swift.expected -o - | sed '/^\s*$/d' | sort > %t.tmp/stability-stdlib-abi.swift.expected.sorted
Expand Down
3 changes: 1 addition & 2 deletions test/api-digester/stability-stdlib-abi-without-asserts.swift
Original file line number Diff line number Diff line change
Expand Up @@ -2,8 +2,7 @@
// REQUIRES: swift_stdlib_no_asserts
// RUN: %empty-directory(%t.tmp)
// mkdir %t.tmp/module-cache && mkdir %t.tmp/dummy.sdk
// RUN: %api-digester -dump-sdk -module Swift -o %t.tmp/current-stdlib.json -module-cache-path %t.tmp/module-cache -sdk %t.tmp/dummy.sdk -abi -avoid-location
// RUN: %api-digester -diagnose-sdk -input-paths %S/Inputs/stdlib-stable-abi.json -input-paths %t.tmp/current-stdlib.json -abi -o %t.tmp/changes.txt -v
// RUN: %api-digester -diagnose-sdk -module Swift -o %t.tmp/changes.txt -module-cache-path %t.tmp/module-cache -sdk %t.tmp/dummy.sdk -abi -avoid-location -v
// RUN: %clang -E -P -x c %S/Outputs/stability-stdlib-abi.without.asserts.swift.expected -o - | sed '/^\s*$/d' | sort > %t.tmp/stability-stdlib-abi.swift.expected
// RUN: %clang -E -P -x c %t.tmp/changes.txt -o - | sed '/^\s*$/d' | sort > %t.tmp/changes.txt.tmp
// RUN: diff -u %t.tmp/stability-stdlib-abi.swift.expected %t.tmp/changes.txt.tmp
Expand Down
3 changes: 1 addition & 2 deletions test/api-digester/stability-stdlib-source.swift
Original file line number Diff line number Diff line change
@@ -1,8 +1,7 @@
// REQUIRES: OS=macosx
// RUN: %empty-directory(%t.tmp)
// mkdir %t.tmp/module-cache && mkdir %t.tmp/dummy.sdk
// RUN: %api-digester -dump-sdk -module Swift -o %t.tmp/current-stdlib.json -module-cache-path %t.tmp/module-cache -sdk %t.tmp/dummy.sdk -avoid-location
// RUN: %api-digester -diagnose-sdk -input-paths %S/Inputs/stdlib-stable.json -input-paths %t.tmp/current-stdlib.json -o %t.tmp/changes.txt -v
// RUN: %api-digester -diagnose-sdk -module Swift -o %t.tmp/changes.txt -module-cache-path %t.tmp/module-cache -sdk %t.tmp/dummy.sdk -avoid-location
// RUN: %clang -E -P -x c %S/Outputs/stability-stdlib-source.swift.expected -o - | sed '/^\s*$/d' | sort > %t.tmp/stability-stdlib-source.swift.expected
// RUN: %clang -E -P -x c %t.tmp/changes.txt -o - | sed '/^\s*$/d' | sort > %t.tmp/changes.txt.tmp
// RUN: diff -u %t.tmp/stability-stdlib-source.swift.expected %t.tmp/changes.txt.tmp
5 changes: 2 additions & 3 deletions tools/swift-api-digester/ModuleAnalyzerNodes.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -586,8 +586,8 @@ static StringRef getKeyContent(SDKContext &Ctx, KeyKind Kind) {
SDKNode* SDKNode::constructSDKNode(SDKContext &Ctx,
llvm::yaml::MappingNode *Node) {
static auto GetScalarString = [&](llvm::yaml::Node *N) -> StringRef {
auto WithQuote = cast<llvm::yaml::ScalarNode>(N)->getRawValue();
return WithQuote.substr(1, WithQuote.size() - 2);
SmallString<64> Buffer;
return Ctx.buffer(cast<llvm::yaml::ScalarNode>(N)->getValue(Buffer));
};

static auto getAsInt = [&](llvm::yaml::Node *N) -> int {
Expand Down Expand Up @@ -2102,7 +2102,6 @@ static parseJsonEmit(SDKContext &Ctx, StringRef FileName) {
// previously dumped.
void SwiftDeclCollector::deSerialize(StringRef Filename) {
auto Pair = parseJsonEmit(Ctx, Filename);
OwnedBuffers.push_back(std::move(Pair.first));
RootNode = std::move(Pair.second);
}

Expand Down
2 changes: 0 additions & 2 deletions tools/swift-api-digester/ModuleAnalyzerNodes.h
Original file line number Diff line number Diff line change
Expand Up @@ -230,7 +230,6 @@ class SDKContext {
CIs.emplace_back(new CompilerInstance());
return *CIs.back();
}

template<class YAMLNodeTy, typename ...ArgTypes>
void diagnose(YAMLNodeTy node, Diag<ArgTypes...> ID,
typename detail::PassArgument<ArgTypes>::type... args) {
Expand Down Expand Up @@ -707,7 +706,6 @@ struct TypeInitInfo {

class SwiftDeclCollector: public VisibleDeclConsumer {
SDKContext &Ctx;
std::vector<std::unique_ptr<llvm::MemoryBuffer>> OwnedBuffers;
SDKNode *RootNode;
llvm::SetVector<Decl*> KnownDecls;
// Collected and sorted after we get all of them.
Expand Down
101 changes: 92 additions & 9 deletions tools/swift-api-digester/swift-api-digester.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -234,6 +234,11 @@ static llvm::cl::list<std::string>
PreferInterfaceForModules("use-interface-for-module", llvm::cl::ZeroOrMore,
llvm::cl::desc("Prefer loading these modules via interface"),
llvm::cl::cat(Category));

static llvm::cl::opt<std::string>
BaselineFilePath("baseline-path",
llvm::cl::desc("The path to the Json file that we should use as the baseline"),
llvm::cl::cat(Category));
} // namespace options

namespace {
Expand Down Expand Up @@ -2529,6 +2534,77 @@ static bool hasBaselineInput() {
!options::BaselineFrameworkPaths.empty() || !options::BaselineSDK.empty();
}

enum class ComparisonInputMode: uint8_t {
BothJson,
BaselineJson,
BothLoad,
};

static ComparisonInputMode checkComparisonInputMode() {
if (options::SDKJsonPaths.size() == 2)
return ComparisonInputMode::BothJson;
else if (hasBaselineInput())
return ComparisonInputMode::BothLoad;
else
return ComparisonInputMode::BaselineJson;
}

static SDKNodeRoot *getBaselineFromJson(const char *Main, SDKContext &Ctx) {
SwiftDeclCollector Collector(Ctx);
// If the baseline path has been given, honor that.
if (!options::BaselineFilePath.empty()) {
Collector.deSerialize(options::BaselineFilePath);
return Collector.getSDKRoot();
}
CompilerInvocation Invok;
llvm::StringSet<> Modules;
// We need to call prepareForDump to parse target triple.
if (prepareForDump(Main, Invok, Modules, true))
return nullptr;

assert(Modules.size() == 1 &&
"Cannot find builtin baseline for more than one module");
// The path of the swift-api-digester executable.
std::string ExePath = llvm::sys::fs::getMainExecutable(Main,
reinterpret_cast<void *>(&anchorForGetMainExecutable));
llvm::SmallString<128> BaselinePath(ExePath);
llvm::sys::path::remove_filename(BaselinePath); // Remove /swift-api-digester
llvm::sys::path::remove_filename(BaselinePath); // Remove /bin
llvm::sys::path::append(BaselinePath, "lib", "swift", "FrameworkABIBaseline",
Modules.begin()->getKey());
// Look for ABI or API baseline
if (Ctx.checkingABI())
llvm::sys::path::append(BaselinePath, "ABI");
else
llvm::sys::path::append(BaselinePath, "API");
// Look for deployment target specific baseline files.
auto Triple = Invok.getLangOptions().Target;
if (Triple.isMacCatalystEnvironment())
llvm::sys::path::append(BaselinePath, "iosmac.json");
else if (Triple.isMacOSX())
llvm::sys::path::append(BaselinePath, "macos.json");
else if (Triple.isiOS())
llvm::sys::path::append(BaselinePath, "iphoneos.json");
else if (Triple.isTvOS())
llvm::sys::path::append(BaselinePath, "appletvos.json");
else if (Triple.isWatchOS())
llvm::sys::path::append(BaselinePath, "watchos.json");
else {
llvm::errs() << "Unsupported triple target\n";
exit(1);
}
StringRef Path = BaselinePath.str();
if (!fs::exists(Path)) {
llvm::errs() << "Baseline at " << Path << " does not exist\n";
exit(1);
}
if (options::Verbose) {
llvm::errs() << "Using baseline at " << Path << "\n";
}
Collector.deSerialize(Path);
return Collector.getSDKRoot();
}

int main(int argc, char *argv[]) {
PROGRAM_START(argc, argv);
INITIALIZE_LLVM();
Expand All @@ -2550,33 +2626,40 @@ int main(int argc, char *argv[]) {
dumpSDKContent(InitInvok, Modules, options::OutputFile, Opts);
case ActionType::MigratorGen:
case ActionType::DiagnoseSDKs: {
bool CompareJson = options::SDKJsonPaths.size() == 2;
if (!CompareJson && !hasBaselineInput()) {
llvm::errs() << "Only two SDK versions can be compared\n";
llvm::cl::PrintHelpMessage();
return 1;
}
ComparisonInputMode Mode = checkComparisonInputMode();
llvm::StringSet<> protocolWhitelist;
if (!options::ProtReqWhiteList.empty()) {
if (readFileLineByLine(options::ProtReqWhiteList, protocolWhitelist))
return 1;
}
if (options::Action == ActionType::MigratorGen)
if (options::Action == ActionType::MigratorGen) {
assert(Mode == ComparisonInputMode::BothJson && "Only BothJson mode is supported");
return generateMigrationScript(options::SDKJsonPaths[0],
options::SDKJsonPaths[1],
options::OutputFile, IgnoredUsrs, Opts);
else if (CompareJson)
}
switch(Mode) {
case ComparisonInputMode::BothJson: {
return diagnoseModuleChange(options::SDKJsonPaths[0],
options::SDKJsonPaths[1],
options::OutputFile, Opts,
std::move(protocolWhitelist));
else {
}
case ComparisonInputMode::BaselineJson: {
SDKContext Ctx(Opts);
return diagnoseModuleChange(Ctx, getBaselineFromJson(argv[0], Ctx),
getSDKRoot(argv[0], Ctx, false),
options::OutputFile,
std::move(protocolWhitelist));
}
case ComparisonInputMode::BothLoad: {
SDKContext Ctx(Opts);
return diagnoseModuleChange(Ctx, getSDKRoot(argv[0], Ctx, true),
getSDKRoot(argv[0], Ctx, false),
options::OutputFile,
std::move(protocolWhitelist));
}
}
}
case ActionType::DeserializeSDK:
case ActionType::DeserializeDiffItems: {
Expand Down