Skip to content

Commit 3bd26d0

Browse files
authored
Merge pull request #36381 from owenv/api-digester-as-frontend-tool
2 parents 281ce3d + 77efd77 commit 3bd26d0

File tree

14 files changed

+96
-73
lines changed

14 files changed

+96
-73
lines changed

include/swift/Driver/Driver.h

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -167,7 +167,8 @@ class Driver {
167167
AutolinkExtract, // swift-autolink-extract
168168
SwiftIndent, // swift-indent
169169
SymbolGraph, // swift-symbolgraph
170-
APIExtract // swift-api-extract
170+
APIExtract, // swift-api-extract
171+
APIDigester // swift-api-digester
171172
};
172173

173174
class InputInfoMap;

lib/APIDigester/CMakeLists.txt

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,10 @@
1+
set_swift_llvm_is_available()
2+
3+
add_swift_host_library(swiftAPIDigester STATIC
4+
ModuleAnalyzerNodes.cpp
5+
ModuleDiagsConsumer.cpp)
6+
7+
target_link_libraries(swiftAPIDigester PRIVATE
8+
swiftFrontend
9+
swiftSIL
10+
swiftIDE)

tools/swift-api-digester/ModuleAnalyzerNodes.cpp renamed to lib/APIDigester/ModuleAnalyzerNodes.cpp

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
#include "llvm/ADT/STLExtras.h"
22
#include "swift/Basic/Defer.h"
33
#include "swift/SIL/SILDeclRef.h"
4-
#include <ModuleAnalyzerNodes.h>
4+
#include <swift/APIDigester/ModuleAnalyzerNodes.h>
55
#include <algorithm>
66

77
using namespace swift;

tools/swift-api-digester/ModuleDiagsConsumer.cpp renamed to lib/APIDigester/ModuleDiagsConsumer.cpp

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -17,7 +17,7 @@
1717

1818
#include "swift/AST/DiagnosticEngine.h"
1919
#include "swift/AST/DiagnosticsModuleDiffer.h"
20-
#include "ModuleDiagsConsumer.h"
20+
#include "swift/APIDigester/ModuleDiagsConsumer.h"
2121

2222
using namespace swift;
2323

lib/CMakeLists.txt

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -14,6 +14,7 @@ list(APPEND LLVM_COMMON_DEPENDS intrinsics_gen clang-tablegen-targets)
1414
list(APPEND LLVM_COMMON_DEPENDS swift-syntax-generated-headers)
1515
list(APPEND LLVM_COMMON_DEPENDS swift-parse-syntax-generated-headers)
1616

17+
add_subdirectory(APIDigester)
1718
add_subdirectory(AST)
1819
add_subdirectory(ASTSectionImporter)
1920
add_subdirectory(Basic)

lib/Driver/Driver.cpp

Lines changed: 11 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -96,15 +96,16 @@ void Driver::parseDriverKind(ArrayRef<const char *> Args) {
9696
}
9797

9898
Optional<DriverKind> Kind =
99-
llvm::StringSwitch<Optional<DriverKind>>(DriverName)
100-
.Case("swift", DriverKind::Interactive)
101-
.Case("swiftc", DriverKind::Batch)
102-
.Case("swift-autolink-extract", DriverKind::AutolinkExtract)
103-
.Case("swift-indent", DriverKind::SwiftIndent)
104-
.Case("swift-symbolgraph-extract", DriverKind::SymbolGraph)
105-
.Case("swift-api-extract", DriverKind::APIExtract)
106-
.Default(None);
107-
99+
llvm::StringSwitch<Optional<DriverKind>>(DriverName)
100+
.Case("swift", DriverKind::Interactive)
101+
.Case("swiftc", DriverKind::Batch)
102+
.Case("swift-autolink-extract", DriverKind::AutolinkExtract)
103+
.Case("swift-indent", DriverKind::SwiftIndent)
104+
.Case("swift-symbolgraph-extract", DriverKind::SymbolGraph)
105+
.Case("swift-api-extract", DriverKind::APIExtract)
106+
.Case("swift-api-digester", DriverKind::APIDigester)
107+
.Default(None);
108+
108109
if (Kind.hasValue())
109110
driverKind = Kind.getValue();
110111
else if (!OptName.empty())
@@ -3497,6 +3498,7 @@ void Driver::printHelp(bool ShowHidden) const {
34973498
case DriverKind::SwiftIndent:
34983499
case DriverKind::SymbolGraph:
34993500
case DriverKind::APIExtract:
3501+
case DriverKind::APIDigester:
35003502
ExcludedFlagsBitmask |= options::NoBatchOption;
35013503
break;
35023504
}

test/CMakeLists.txt

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -54,7 +54,6 @@ function(get_test_dependencies SDK result_var_name)
5454
sil-opt
5555
sil-passpipeline-dumper
5656
swift-frontend
57-
swift-api-digester
5857
swift-demangle
5958
swift-demangle-yamldump
6059
swift-dependency-tool

tools/CMakeLists.txt

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -25,7 +25,6 @@ add_swift_tool_subdirectory(sil-llvm-gen)
2525
add_swift_tool_subdirectory(sil-nm)
2626
add_swift_tool_subdirectory(sil-passpipeline-dumper)
2727
add_swift_tool_subdirectory(swift-llvm-opt)
28-
add_swift_tool_subdirectory(swift-api-digester)
2928
add_swift_tool_subdirectory(swift-ast-script)
3029
add_swift_tool_subdirectory(swift-refactor)
3130
add_swift_tool_subdirectory(libSwiftScan)

tools/driver/CMakeLists.txt

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2,13 +2,15 @@ add_swift_host_tool(swift-frontend
22
driver.cpp
33
autolink_extract_main.cpp
44
modulewrap_main.cpp
5+
swift_api_digester_main.cpp
56
swift_indent_main.cpp
67
swift_symbolgraph_extract_main.cpp
78
swift_api_extract_main.cpp
89
SWIFT_COMPONENT compiler
910
)
1011
target_link_libraries(swift-frontend
1112
PRIVATE
13+
swiftAPIDigester
1214
swiftDriver
1315
swiftFrontendTool
1416
swiftSymbolGraphGen
@@ -44,12 +46,18 @@ swift_create_post_build_symlink(swift-frontend
4446
DESTINATION "swift-autolink-extract${CMAKE_EXECUTABLE_SUFFIX}"
4547
WORKING_DIRECTORY "${SWIFT_RUNTIME_OUTPUT_INTDIR}")
4648

49+
swift_create_post_build_symlink(swift-frontend
50+
SOURCE "swift-frontend${CMAKE_EXECUTABLE_SUFFIX}"
51+
DESTINATION "swift-api-digester${CMAKE_EXECUTABLE_SUFFIX}"
52+
WORKING_DIRECTORY "${SWIFT_RUNTIME_OUTPUT_INTDIR}")
53+
4754
add_swift_tool_symlink(swift swift-frontend compiler)
4855
add_swift_tool_symlink(swiftc swift-frontend compiler)
4956
add_swift_tool_symlink(swift-symbolgraph-extract swift-frontend compiler)
5057
add_swift_tool_symlink(swift-api-extract swift-frontend compiler)
5158
add_swift_tool_symlink(swift-autolink-extract swift-frontend autolink-driver)
5259
add_swift_tool_symlink(swift-indent swift-frontend editor-integration)
60+
add_swift_tool_symlink(swift-api-digester swift-frontend compiler)
5361

5462
# If building as part of clang, make sure the headers are installed.
5563
if(NOT SWIFT_BUILT_STANDALONE)
@@ -69,6 +77,9 @@ swift_install_in_component(FILES "${SWIFT_RUNTIME_OUTPUT_INTDIR}/swift-symbolgra
6977
swift_install_in_component(FILES "${SWIFT_RUNTIME_OUTPUT_INTDIR}/swift-api-extract${CMAKE_EXECUTABLE_SUFFIX}"
7078
DESTINATION "bin"
7179
COMPONENT compiler)
80+
swift_install_in_component(FILES "${SWIFT_RUNTIME_OUTPUT_INTDIR}/swift-api-digester${CMAKE_EXECUTABLE_SUFFIX}"
81+
DESTINATION "bin"
82+
COMPONENT compiler)
7283
add_dependencies(autolink-driver swift-frontend)
7384
swift_install_in_component(FILES "${SWIFT_RUNTIME_OUTPUT_INTDIR}/swift-autolink-extract${CMAKE_EXECUTABLE_SUFFIX}"
7485
DESTINATION "bin"

tools/driver/driver.cpp

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -75,6 +75,10 @@ extern int swift_indent_main(ArrayRef<const char *> Args, const char *Argv0,
7575
extern int swift_symbolgraph_extract_main(ArrayRef<const char *> Args, const char *Argv0,
7676
void *MainAddr);
7777

78+
/// Run 'swift-api-digester'
79+
extern int swift_api_digester_main(ArrayRef<const char *> Args,
80+
const char *Argv0, void *MainAddr);
81+
7882
/// Run 'swift-api-extract'
7983
extern int swift_api_extract_main(ArrayRef<const char *> Args,
8084
const char *Argv0, void *MainAddr);
@@ -254,6 +258,10 @@ static int run_driver(StringRef ExecName,
254258
return swift_api_extract_main(
255259
TheDriver.getArgsWithoutProgramNameAndDriverMode(argv), argv[0],
256260
(void *)(intptr_t)getExecutablePath);
261+
case Driver::DriverKind::APIDigester:
262+
return swift_api_digester_main(
263+
TheDriver.getArgsWithoutProgramNameAndDriverMode(argv), argv[0],
264+
(void *)(intptr_t)getExecutablePath);
257265
default:
258266
break;
259267
}

tools/swift-api-digester/swift-api-digester.cpp renamed to tools/driver/swift_api_digester_main.cpp

Lines changed: 51 additions & 48 deletions
Original file line numberDiff line numberDiff line change
@@ -33,8 +33,8 @@
3333
#include "swift/AST/DiagnosticsModuleDiffer.h"
3434
#include "swift/IDE/APIDigesterData.h"
3535
#include <functional>
36-
#include "ModuleAnalyzerNodes.h"
37-
#include "ModuleDiagsConsumer.h"
36+
#include "swift/APIDigester/ModuleAnalyzerNodes.h"
37+
#include "swift/APIDigester/ModuleDiagsConsumer.h"
3838

3939
using namespace swift;
4040
using namespace ide;
@@ -2551,13 +2551,6 @@ static int generateMigrationScript(StringRef LeftPath, StringRef RightPath,
25512551
return 0;
25522552
}
25532553

2554-
// This function isn't referenced outside its translation unit, but it
2555-
// can't use the "static" keyword because its address is used for
2556-
// getMainExecutable (since some platforms don't support taking the
2557-
// address of main, and some platforms can't implement getMainExecutable
2558-
// without being given the address of a function in the main executable).
2559-
void anchorForGetMainExecutable() {}
2560-
25612554
static void setSDKPath(CompilerInvocation &InitInvok, bool IsBaseline) {
25622555
if (IsBaseline) {
25632556
// Set baseline SDK
@@ -2578,12 +2571,10 @@ static void setSDKPath(CompilerInvocation &InitInvok, bool IsBaseline) {
25782571
}
25792572
}
25802573

2581-
static int prepareForDump(const char *Main,
2574+
static int prepareForDump(std::string MainExecutablePath,
25822575
CompilerInvocation &InitInvok,
2583-
llvm::StringSet<> &Modules,
2584-
bool IsBaseline = false) {
2585-
InitInvok.setMainExecutablePath(fs::getMainExecutable(Main,
2586-
reinterpret_cast<void *>(&anchorForGetMainExecutable)));
2576+
llvm::StringSet<> &Modules, bool IsBaseline = false) {
2577+
InitInvok.setMainExecutablePath(MainExecutablePath);
25872578
InitInvok.setModuleName("swift_ide_test");
25882579
setSDKPath(InitInvok, IsBaseline);
25892580

@@ -2692,7 +2683,7 @@ static int deserializeNameCorrection(APIDiffItemStore &Store,
26922683
return EC.value();
26932684
}
26942685

2695-
static CheckerOptions getCheckOpts(int argc, char *argv[]) {
2686+
static CheckerOptions getCheckOpts(ArrayRef<const char *> Args) {
26962687
CheckerOptions Opts;
26972688
Opts.AvoidLocation = options::AvoidLocation;
26982689
Opts.AvoidToolArgs = options::AvoidToolArgs;
@@ -2708,8 +2699,8 @@ static CheckerOptions getCheckOpts(int argc, char *argv[]) {
27082699
Opts.SkipOSCheck = options::DisableOSChecks;
27092700
Opts.CompilerStyle = options::CompilerStyleDiags ||
27102701
!options::SerializedDiagPath.empty();
2711-
for (int i = 1; i < argc; ++i)
2712-
Opts.ToolArgs.push_back(argv[i]);
2702+
for (auto Arg : Args)
2703+
Opts.ToolArgs.push_back(Arg);
27132704

27142705
if (!options::SDK.empty()) {
27152706
auto Ver = getSDKBuildVersion(options::SDK);
@@ -2721,10 +2712,11 @@ static CheckerOptions getCheckOpts(int argc, char *argv[]) {
27212712
return Opts;
27222713
}
27232714

2724-
static SDKNodeRoot *getSDKRoot(const char *Main, SDKContext &Ctx, bool IsBaseline) {
2715+
static SDKNodeRoot *getSDKRoot(std::string MainExecutablePath, SDKContext &Ctx,
2716+
bool IsBaseline) {
27252717
CompilerInvocation Invok;
27262718
llvm::StringSet<> Modules;
2727-
if (prepareForDump(Main, Invok, Modules, IsBaseline))
2719+
if (prepareForDump(MainExecutablePath, Invok, Modules, IsBaseline))
27282720
return nullptr;
27292721
return getSDKNodeRoot(Ctx, Invok, Modules);
27302722
}
@@ -2749,20 +2741,18 @@ static ComparisonInputMode checkComparisonInputMode() {
27492741
return ComparisonInputMode::BaselineJson;
27502742
}
27512743

2752-
static std::string getDefaultBaselineDir(const char *Main) {
2744+
static std::string getDefaultBaselineDir(std::string MainExecutablePath) {
27532745
llvm::SmallString<128> BaselineDir;
2754-
// The path of the swift-api-digester executable.
2755-
std::string ExePath = llvm::sys::fs::getMainExecutable(Main,
2756-
reinterpret_cast<void *>(&anchorForGetMainExecutable));
2757-
BaselineDir.append(ExePath);
2746+
BaselineDir.append(MainExecutablePath);
27582747
llvm::sys::path::remove_filename(BaselineDir); // Remove /swift-api-digester
27592748
llvm::sys::path::remove_filename(BaselineDir); // Remove /bin
27602749
llvm::sys::path::append(BaselineDir, "lib", "swift", "FrameworkABIBaseline");
27612750
return BaselineDir.str().str();
27622751
}
27632752

2764-
static std::string getEmptyBaselinePath(const char *Main) {
2765-
llvm::SmallString<128> BaselinePath(getDefaultBaselineDir(Main));
2753+
static std::string getEmptyBaselinePath(std::string MainExecutablePath) {
2754+
llvm::SmallString<128> BaselinePath(
2755+
getDefaultBaselineDir(MainExecutablePath));
27662756
llvm::sys::path::append(BaselinePath, "nil.json");
27672757
return BaselinePath.str().str();
27682758
}
@@ -2788,10 +2778,11 @@ static StringRef getBaselineFilename(llvm::Triple Triple) {
27882778
}
27892779
}
27902780

2791-
static std::string getDefaultBaselinePath(const char *Main, StringRef Module,
2792-
llvm::Triple Triple,
2781+
static std::string getDefaultBaselinePath(std::string MainExecutablePath,
2782+
StringRef Module, llvm::Triple Triple,
27932783
bool ABI) {
2794-
llvm::SmallString<128> BaselinePath(getDefaultBaselineDir(Main));
2784+
llvm::SmallString<128> BaselinePath(
2785+
getDefaultBaselineDir(MainExecutablePath));
27952786
llvm::sys::path::append(BaselinePath, Module);
27962787
// Look for ABI or API baseline
27972788
llvm::sys::path::append(BaselinePath, ABI? "ABI": "API");
@@ -2807,12 +2798,13 @@ static std::string getCustomBaselinePath(llvm::Triple Triple, bool ABI) {
28072798
return BaselinePath.str().str();
28082799
}
28092800

2810-
static SDKNodeRoot *getBaselineFromJson(const char *Main, SDKContext &Ctx) {
2801+
static SDKNodeRoot *getBaselineFromJson(std::string MainExecutablePath,
2802+
SDKContext &Ctx) {
28112803
SwiftDeclCollector Collector(Ctx);
28122804
CompilerInvocation Invok;
28132805
llvm::StringSet<> Modules;
28142806
// We need to call prepareForDump to parse target triple.
2815-
if (prepareForDump(Main, Invok, Modules, true))
2807+
if (prepareForDump(MainExecutablePath, Invok, Modules, true))
28162808
return nullptr;
28172809

28182810
assert(Modules.size() == 1 &&
@@ -2825,9 +2817,9 @@ static SDKNodeRoot *getBaselineFromJson(const char *Main, SDKContext &Ctx) {
28252817
Path = getCustomBaselinePath(Invok.getLangOptions().Target,
28262818
Ctx.checkingABI());
28272819
} else if (options::UseEmptyBaseline) {
2828-
Path = getEmptyBaselinePath(Main);
2820+
Path = getEmptyBaselinePath(MainExecutablePath);
28292821
} else {
2830-
Path = getDefaultBaselinePath(Main, Modules.begin()->getKey(),
2822+
Path = getDefaultBaselinePath(MainExecutablePath, Modules.begin()->getKey(),
28312823
Invok.getLangOptions().Target,
28322824
Ctx.checkingABI());
28332825
}
@@ -2860,26 +2852,37 @@ static std::string getJsonOutputFilePath(llvm::Triple Triple, bool ABI) {
28602852
exit(1);
28612853
}
28622854

2863-
int main(int argc, char *argv[]) {
2864-
PROGRAM_START(argc, argv);
2855+
int swift_api_digester_main(ArrayRef<const char *> Args, const char *Argv0,
2856+
void *MainAddr) {
28652857
INITIALIZE_LLVM();
28662858

2859+
// LLVM Command Line parsing expects to trim off argv[0].
2860+
SmallVector<const char *, 8> ArgsWithArgv0{Argv0};
2861+
ArgsWithArgv0.append(Args.begin(), Args.end());
2862+
2863+
std::string MainExecutablePath = fs::getMainExecutable(Argv0, MainAddr);
2864+
28672865
llvm::cl::HideUnrelatedOptions(options::Category);
2868-
llvm::cl::ParseCommandLineOptions(argc, argv, "Swift SDK Digester\n");
2866+
llvm::cl::ParseCommandLineOptions(ArgsWithArgv0.size(),
2867+
llvm::makeArrayRef(ArgsWithArgv0).data(),
2868+
"Swift SDK Digester\n");
28692869
CompilerInvocation InitInvok;
28702870

28712871
llvm::StringSet<> Modules;
28722872
std::vector<std::string> PrintApis;
28732873
llvm::StringSet<> IgnoredUsrs;
28742874
readIgnoredUsrs(IgnoredUsrs);
2875-
CheckerOptions Opts = getCheckOpts(argc, argv);
2875+
CheckerOptions Opts = getCheckOpts(Args);
28762876
for (auto Name : options::ApisPrintUsrs)
28772877
PrintApis.push_back(Name);
28782878
switch (options::Action) {
28792879
case ActionType::DumpSDK:
2880-
return (prepareForDump(argv[0], InitInvok, Modules)) ? 1 :
2881-
dumpSDKContent(InitInvok, Modules,
2882-
getJsonOutputFilePath(InitInvok.getLangOptions().Target, Opts.ABI),
2880+
return (prepareForDump(MainExecutablePath, InitInvok, Modules))
2881+
? 1
2882+
: dumpSDKContent(
2883+
InitInvok, Modules,
2884+
getJsonOutputFilePath(InitInvok.getLangOptions().Target,
2885+
Opts.ABI),
28832886
Opts);
28842887
case ActionType::MigratorGen:
28852888
case ActionType::DiagnoseSDKs: {
@@ -2904,17 +2907,17 @@ int main(int argc, char *argv[]) {
29042907
}
29052908
case ComparisonInputMode::BaselineJson: {
29062909
SDKContext Ctx(Opts);
2907-
return diagnoseModuleChange(Ctx, getBaselineFromJson(argv[0], Ctx),
2908-
getSDKRoot(argv[0], Ctx, false),
2909-
options::OutputFile,
2910-
std::move(protocolAllowlist));
2910+
return diagnoseModuleChange(
2911+
Ctx, getBaselineFromJson(MainExecutablePath, Ctx),
2912+
getSDKRoot(MainExecutablePath, Ctx, false), options::OutputFile,
2913+
std::move(protocolAllowlist));
29112914
}
29122915
case ComparisonInputMode::BothLoad: {
29132916
SDKContext Ctx(Opts);
2914-
return diagnoseModuleChange(Ctx, getSDKRoot(argv[0], Ctx, true),
2915-
getSDKRoot(argv[0], Ctx, false),
2916-
options::OutputFile,
2917-
std::move(protocolAllowlist));
2917+
return diagnoseModuleChange(
2918+
Ctx, getSDKRoot(MainExecutablePath, Ctx, true),
2919+
getSDKRoot(MainExecutablePath, Ctx, false), options::OutputFile,
2920+
std::move(protocolAllowlist));
29182921
}
29192922
}
29202923
}

tools/swift-api-digester/CMakeLists.txt

Lines changed: 0 additions & 11 deletions
This file was deleted.

0 commit comments

Comments
 (0)