Skip to content

Commit 75b8a0e

Browse files
committed
[Dependency Scanning] Add entry-point to DependencyScanningTool and the corresponding C API for an import prescan operation
1 parent 75a8cfa commit 75b8a0e

File tree

6 files changed

+92
-27
lines changed

6 files changed

+92
-27
lines changed

include/swift-c/DependencyScan/DependencyScan.h

Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -164,6 +164,11 @@ typedef struct {
164164
depscan_dependency_set_t* module_set;
165165
} depscan_dependency_result_t;
166166

167+
typedef struct {
168+
/// The complete list of imports discovered
169+
ds_string_set_t* import_set;
170+
} depscan_prescan_result_t;
171+
167172
//=== Dependency Result Functions -----------------------------------------===//
168173

169174
DEPSCAN_PUBLIC void
@@ -178,6 +183,9 @@ depscan_dependency_set_dispose(depscan_dependency_set_t *set);
178183
DEPSCAN_PUBLIC void
179184
depscan_dependency_result_dispose(depscan_dependency_result_t *result);
180185

186+
DEPSCAN_PUBLIC void
187+
depscan_prescan_result_dispose(depscan_prescan_result_t *result);
188+
181189
//=== Scanner Functions ---------------------------------------------------===//
182190

183191
/// Container of the configuration state and shared cache for dependency scanning.
@@ -195,6 +203,12 @@ depscan_scan_dependencies(depscan_scanner_t* scanner,
195203
int argc,
196204
const char *const *argv);
197205

206+
DEPSCAN_PUBLIC depscan_prescan_result_t*
207+
depscan_prescan_dependencies(depscan_scanner_t* scanner,
208+
const char *working_directory,
209+
int argc,
210+
const char *const *argv);
211+
198212
DEPSCAN_END_DECLS
199213

200214
#endif // SWIFT_C_DEPENDENCY_SCAN_H

include/swift/DependencyScan/DependencyScanningTool.h

Lines changed: 8 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -35,11 +35,18 @@ class DependencyScanningTool {
3535
/// placeholder modules.
3636
///
3737
/// \returns a \c StringError with the diagnostic output if errors
38-
/// occurred, \c FullDependencies otherwise.
38+
/// occurred, \c depscan_dependency_result_t otherwise.
3939
llvm::ErrorOr<depscan_dependency_result_t*>
4040
getDependencies(ArrayRef<const char *> Command,
4141
const llvm::StringSet<> &PlaceholderModules);
4242

43+
/// Collect the set of imports for the input module
44+
///
45+
/// \returns a \c StringError with the diagnostic output if errors
46+
/// occurred, \c depscan_prescan_result_t otherwise.
47+
llvm::ErrorOr<depscan_prescan_result_t*>
48+
getImports(ArrayRef<const char *> Command);
49+
4350
/// Collect the full module depenedency graph for the input collection of
4451
/// module names (batch inputs) and output them to the
4552
/// BatchScanInput-specified output locations.

include/swift/DependencyScan/ScanDependencies.h

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -58,14 +58,18 @@ llvm::ErrorOr<depscan_dependency_result_t*>
5858
performModuleScan(CompilerInstance &instance,
5959
ModuleDependenciesCache &cache);
6060

61+
/// Scans the main module of \c instance for all direct module imports
62+
llvm::ErrorOr<depscan_prescan_result_t*>
63+
performModulePrescan(CompilerInstance &instance);
64+
6165
/// Batch scan the dependencies for modules specified in \c batchInputFile.
6266
std::vector<llvm::ErrorOr<depscan_dependency_result_t*>>
6367
performBatchModuleScan(CompilerInstance &instance,
6468
ModuleDependenciesCache &cache, llvm::StringSaver &saver,
6569
const std::vector<BatchScanInput> &BatchInput);
6670

6771
/// Batch prescan the imports of modules specified in \c batchInputFile.
68-
std::vector<llvm::ErrorOr<std::vector<std::string>>>
72+
std::vector<llvm::ErrorOr<depscan_prescan_result_t*>>
6973
performBatchModulePrescan(CompilerInstance &instance,
7074
ModuleDependenciesCache &cache,
7175
llvm::StringSaver &saver,

lib/DependencyScan/CMakeLists.txt

Lines changed: 1 addition & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -16,15 +16,4 @@ target_link_libraries(SwiftScan INTERFACE
1616
target_link_libraries(SwiftScan PRIVATE
1717
swiftClangImporter
1818
swiftDemangling
19-
swiftFrontend
20-
swiftIDE
21-
swiftImmediate
22-
swiftIndex
23-
swiftIRGen
24-
swiftOption
25-
swiftPrintAsObjC
26-
swiftSerialization
27-
swiftSIL
28-
swiftSILGen
29-
swiftSILOptimizer
30-
swiftTBDGen)
19+
swiftFrontend)

lib/DependencyScan/DependencyScanningTool.cpp

Lines changed: 39 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -47,6 +47,23 @@ DependencyScanningTool::getDependencies(
4747
return Dependencies;
4848
}
4949

50+
llvm::ErrorOr<depscan_prescan_result_t *>
51+
DependencyScanningTool::getImports(ArrayRef<const char *> Command) {
52+
// The primary instance used to scan the query Swift source-code
53+
auto InstanceOrErr = initCompilerInstanceForScan(Command);
54+
if (std::error_code EC = InstanceOrErr.getError())
55+
return EC;
56+
auto Instance = std::move(*InstanceOrErr);
57+
58+
// Execute the scanning action, retreiving the in-memory result
59+
auto DependenciesOrErr = performModulePrescan(*Instance.get());
60+
if (DependenciesOrErr.getError())
61+
return std::make_error_code(std::errc::not_supported);
62+
auto Dependencies = std::move(*DependenciesOrErr);
63+
64+
return Dependencies;
65+
}
66+
5067
std::vector<llvm::ErrorOr<depscan_dependency_result_t *>>
5168
DependencyScanningTool::getDependencies(
5269
ArrayRef<const char *> Command,
@@ -135,6 +152,23 @@ depscan_scan_dependencies(depscan_scanner_t *scanner,
135152
return DependencyGraph;
136153
}
137154

155+
depscan_prescan_result_t *
156+
depscan_prescan_dependencies(depscan_scanner_t *scanner,
157+
const char *working_directory, int argc,
158+
const char *const *argv) {
159+
DependencyScanningTool *ScanningTool = unwrap(scanner);
160+
std::vector<const char *> Compilation;
161+
for (int i = 0; i < argc; ++i)
162+
Compilation.push_back(argv[i]);
163+
164+
// Execute the scan and bridge the result
165+
auto PreScanResult = ScanningTool->getImports(Compilation);
166+
if (PreScanResult.getError())
167+
return nullptr;
168+
auto ImportSet = std::move(*PreScanResult);
169+
return ImportSet;
170+
}
171+
138172
void depscan_dependency_info_details_dispose(
139173
depscan_module_details_t *details) {
140174
switch (details->kind) {
@@ -195,3 +229,8 @@ void depscan_dependency_result_dispose(depscan_dependency_result_t *result) {
195229
depscan_dependency_set_dispose(result->module_set);
196230
delete result;
197231
}
232+
233+
void depscan_prescan_result_dispose(depscan_prescan_result_t *result) {
234+
depscan_string_set_dispose(result->import_set);
235+
delete result;
236+
}

lib/DependencyScan/ScanDependencies.cpp

Lines changed: 25 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -11,7 +11,6 @@
1111
//===----------------------------------------------------------------------===//
1212

1313
#include "swift/DependencyScan/ScanDependencies.h"
14-
#include "swift/DependencyScan/DSString.h"
1514
#include "swift/AST/ASTContext.h"
1615
#include "swift/AST/Decl.h"
1716
#include "swift/AST/DiagnosticEngine.h"
@@ -24,6 +23,7 @@
2423
#include "swift/Basic/LLVM.h"
2524
#include "swift/Basic/STLExtras.h"
2625
#include "swift/ClangImporter/ClangImporter.h"
26+
#include "swift/DependencyScan/DSString.h"
2727
#include "swift/Frontend/Frontend.h"
2828
#include "swift/Frontend/FrontendOptions.h"
2929
#include "swift/Frontend/ModuleInterfaceLoader.h"
@@ -472,14 +472,13 @@ getAsClangDependencyModule(const depscan_module_details_t *details) {
472472
return nullptr;
473473
}
474474

475-
static void
476-
writePrescanJSON(llvm::raw_ostream &out,
477-
const std::vector<std::string> &moduleDependencies) {
475+
static void writePrescanJSON(llvm::raw_ostream &out,
476+
const depscan_prescan_result_t *importSet) {
478477
// Write out a JSON containing all main module imports.
479478
out << "{\n";
480479
SWIFT_DEFER { out << "}\n"; };
481480

482-
writeJSONSingleField(out, "imports", moduleDependencies, 0, false);
481+
writeJSONSingleField(out, "imports", importSet, 0, false);
483482
}
484483

485484
static void writeJSON(llvm::raw_ostream &out,
@@ -984,8 +983,7 @@ forEachBatchEntry(CompilerInstance &instance, ModuleDependenciesCache &cache,
984983
}
985984

986985
static ModuleDependencies
987-
identifyMainModuleDependencies(CompilerInstance &instance,
988-
ModuleDependenciesCache &cache) {
986+
identifyMainModuleDependencies(CompilerInstance &instance) {
989987
ModuleDecl *mainModule = instance.getMainModule();
990988
// Main module file name.
991989
auto newExt = file_types::getExtension(file_types::TY_SwiftModuleFile);
@@ -1105,10 +1103,13 @@ bool swift::dependencies::prescanDependencies(CompilerInstance &instance) {
11051103
}
11061104

11071105
// Execute import prescan, and write JSON output to the output stream
1108-
auto mainDependencies = identifyMainModuleDependencies(instance, cache);
1106+
auto importSetOrErr = performModulePrescan(instance);
1107+
if (importSetOrErr.getError())
1108+
return true;
1109+
auto importSet = std::move(*importSetOrErr);
11091110

11101111
// Serialize and output main module dependencies only and exit.
1111-
writePrescanJSON(out, mainDependencies.getModuleDependencies());
1112+
writePrescanJSON(out, importSet);
11121113
// This process succeeds regardless of whether any errors occurred.
11131114
// FIXME: We shouldn't need this, but it's masking bugs in our scanning
11141115
// logic where we don't create a fresh context when scanning Swift interfaces
@@ -1186,7 +1187,7 @@ swift::dependencies::performModuleScan(CompilerInstance &instance,
11861187
ModuleDecl *mainModule = instance.getMainModule();
11871188

11881189
// First, identify the dependencies of the main module
1189-
auto mainDependencies = identifyMainModuleDependencies(instance, cache);
1190+
auto mainDependencies = identifyMainModuleDependencies(instance);
11901191

11911192
// Add the main module.
11921193
StringRef mainModuleName = mainModule->getNameStr();
@@ -1261,6 +1262,15 @@ swift::dependencies::performModuleScan(CompilerInstance &instance,
12611262
return dependencyGraph;
12621263
}
12631264

1265+
llvm::ErrorOr<depscan_prescan_result_t *>
1266+
swift::dependencies::performModulePrescan(CompilerInstance &instance) {
1267+
// Execute import prescan, and write JSON output to the output stream
1268+
auto mainDependencies = identifyMainModuleDependencies(instance);
1269+
depscan_prescan_result_t *importSet = new depscan_prescan_result_t;
1270+
importSet->import_set = create_set(mainDependencies.getModuleDependencies());
1271+
return importSet;
1272+
}
1273+
12641274
std::vector<llvm::ErrorOr<depscan_dependency_result_t *>>
12651275
swift::dependencies::performBatchModuleScan(
12661276
CompilerInstance &instance, ModuleDependenciesCache &cache,
@@ -1330,11 +1340,11 @@ swift::dependencies::performBatchModuleScan(
13301340
return batchScanResult;
13311341
}
13321342

1333-
std::vector<llvm::ErrorOr<std::vector<std::string>>>
1343+
std::vector<llvm::ErrorOr<depscan_prescan_result_t *>>
13341344
swift::dependencies::performBatchModulePrescan(
13351345
CompilerInstance &instance, ModuleDependenciesCache &cache,
13361346
llvm::StringSaver &saver, const std::vector<BatchScanInput> &batchInput) {
1337-
std::vector<llvm::ErrorOr<std::vector<std::string>>> batchPrescanResult;
1347+
std::vector<llvm::ErrorOr<depscan_prescan_result_t *>> batchPrescanResult;
13381348

13391349
// Perform a full dependency scan for each batch entry module
13401350
forEachBatchEntry(
@@ -1377,7 +1387,9 @@ swift::dependencies::performBatchModulePrescan(
13771387
return;
13781388
}
13791389

1380-
batchPrescanResult.push_back(rootDeps->getModuleDependencies());
1390+
depscan_prescan_result_t *importSet = new depscan_prescan_result_t;
1391+
importSet->import_set = create_set(rootDeps->getModuleDependencies());
1392+
batchPrescanResult.push_back(importSet);
13811393
});
13821394

13831395
return batchPrescanResult;

0 commit comments

Comments
 (0)