Skip to content

Commit 354d402

Browse files
committed
[Dependency Scanning] Construct a hollow output on query failure to carry diagnostic output
1 parent 8244aa0 commit 354d402

File tree

1 file changed

+81
-16
lines changed

1 file changed

+81
-16
lines changed

lib/DependencyScan/DependencyScanningTool.cpp

Lines changed: 81 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -189,6 +189,79 @@ swiftscan_diagnostic_set_t *mapCollectedDiagnosticsForOutput(
189189
return diagnosticOutput;
190190
}
191191

192+
// Generate an instance of the `swiftscan_dependency_graph_s` which contains no
193+
// module dependnecies but captures the diagnostics emitted during the attempted
194+
// scan query.
195+
static swiftscan_dependency_graph_t generateHollowDiagnosticOutput(
196+
const DependencyScanDiagnosticCollector &ScanDiagnosticConsumer) {
197+
// Create a dependency graph instance
198+
swiftscan_dependency_graph_t hollowResult = new swiftscan_dependency_graph_s;
199+
200+
// Populate the `modules` with a single info for the main module
201+
// containing no dependencies
202+
swiftscan_dependency_set_t *dependencySet = new swiftscan_dependency_set_t;
203+
dependencySet->count = 1;
204+
dependencySet->modules = new swiftscan_dependency_info_t[1];
205+
swiftscan_dependency_info_s *hollowMainModuleInfo =
206+
new swiftscan_dependency_info_s;
207+
dependencySet->modules[0] = hollowMainModuleInfo;
208+
hollowResult->dependencies = dependencySet;
209+
210+
// Other main module details empty
211+
hollowMainModuleInfo->direct_dependencies =
212+
c_string_utils::create_empty_set();
213+
hollowMainModuleInfo->source_files = c_string_utils::create_empty_set();
214+
hollowMainModuleInfo->module_path = c_string_utils::create_null();
215+
hollowResult->main_module_name = c_string_utils::create_clone("unknown");
216+
hollowMainModuleInfo->module_name =
217+
c_string_utils::create_clone("swiftTextual:unknown");
218+
219+
// Hollow info details
220+
swiftscan_module_details_s *hollowDetails = new swiftscan_module_details_s;
221+
hollowDetails->kind = SWIFTSCAN_DEPENDENCY_INFO_SWIFT_TEXTUAL;
222+
hollowDetails->swift_textual_details = {c_string_utils::create_null(),
223+
c_string_utils::create_empty_set(),
224+
c_string_utils::create_null(),
225+
c_string_utils::create_empty_set(),
226+
c_string_utils::create_empty_set(),
227+
c_string_utils::create_empty_set(),
228+
c_string_utils::create_empty_set(),
229+
c_string_utils::create_empty_set(),
230+
c_string_utils::create_empty_set(),
231+
c_string_utils::create_null(),
232+
false,
233+
false,
234+
c_string_utils::create_null(),
235+
c_string_utils::create_null(),
236+
c_string_utils::create_null()};
237+
hollowMainModuleInfo->details = hollowDetails;
238+
239+
// Empty Link Library set
240+
swiftscan_link_library_set_t *hollowLinkLibrarySet =
241+
new swiftscan_link_library_set_t;
242+
hollowLinkLibrarySet->count = 0;
243+
hollowLinkLibrarySet->link_libraries = nullptr;
244+
hollowMainModuleInfo->link_libraries = hollowLinkLibrarySet;
245+
246+
// Populate the diagnostic info
247+
hollowResult->diagnostics =
248+
mapCollectedDiagnosticsForOutput(&ScanDiagnosticConsumer);
249+
return hollowResult;
250+
}
251+
252+
// Generate an instance of the `swiftscan_import_set_t` which contains no
253+
// imports but captures the diagnostics emitted during the attempted
254+
// scan query.
255+
static swiftscan_import_set_t generateHollowDiagnosticOutputImportSet(
256+
const DependencyScanDiagnosticCollector &ScanDiagnosticConsumer) {
257+
// Create an dependency graph instance
258+
swiftscan_import_set_t hollowResult = new swiftscan_import_set_s;
259+
hollowResult->imports = c_string_utils::create_empty_set();
260+
hollowResult->diagnostics =
261+
mapCollectedDiagnosticsForOutput(&ScanDiagnosticConsumer);
262+
return hollowResult;
263+
}
264+
192265
DependencyScanningTool::DependencyScanningTool()
193266
: ScanningService(std::make_unique<SwiftDependencyScanningService>()),
194267
VersionedPCMInstanceCacheCache(
@@ -203,18 +276,13 @@ DependencyScanningTool::getDependencies(
203276
// There may be errors as early as in instance initialization, so we must ensure
204277
// we can catch those.
205278
auto ScanDiagnosticConsumer = std::make_shared<DependencyScanDiagnosticCollector>();
206-
auto produceDiagnosticStateOnFailure = [&ScanDiagnosticConsumer]() {
207-
swiftscan_dependency_graph_t result = new swiftscan_dependency_graph_s;
208-
result->diagnostics = mapCollectedDiagnosticsForOutput(ScanDiagnosticConsumer.get());
209-
return result;
210-
};
211279

212280
// The primary instance used to scan the query Swift source-code
213281
auto QueryContextOrErr = initCompilerInstanceForScan(Command,
214282
WorkingDirectory,
215283
ScanDiagnosticConsumer);
216284
if (QueryContextOrErr.getError())
217-
return produceDiagnosticStateOnFailure();
285+
return generateHollowDiagnosticOutput(*ScanDiagnosticConsumer);
218286

219287
auto QueryContext = std::move(*QueryContextOrErr);
220288

@@ -228,9 +296,9 @@ DependencyScanningTool::getDependencies(
228296
QueryContext.ScanDiagnostics.get(),
229297
cache);
230298
if (DependenciesOrErr.getError())
231-
return produceDiagnosticStateOnFailure();
299+
return generateHollowDiagnosticOutput(*ScanDiagnosticConsumer);
232300

233-
return std::move(*DependenciesOrErr);;
301+
return std::move(*DependenciesOrErr);
234302
}
235303

236304
llvm::ErrorOr<swiftscan_import_set_t>
@@ -243,11 +311,9 @@ DependencyScanningTool::getImports(ArrayRef<const char *> Command,
243311
auto QueryContextOrErr = initCompilerInstanceForScan(Command,
244312
WorkingDirectory,
245313
ScanDiagnosticConsumer);
246-
if (QueryContextOrErr.getError()) {
247-
swiftscan_import_set_t result = new swiftscan_import_set_s;
248-
result->diagnostics = mapCollectedDiagnosticsForOutput(ScanDiagnosticConsumer.get());
249-
return result;
250-
}
314+
if (QueryContextOrErr.getError())
315+
return generateHollowDiagnosticOutputImportSet(*ScanDiagnosticConsumer);
316+
251317
auto QueryContext = std::move(*QueryContextOrErr);
252318

253319
// Local scan cache instance, wrapping the shared global cache.
@@ -259,10 +325,9 @@ DependencyScanningTool::getImports(ArrayRef<const char *> Command,
259325
QueryContext.ScanDiagnostics.get(),
260326
cache);
261327
if (DependenciesOrErr.getError())
262-
return std::make_error_code(std::errc::not_supported);
263-
auto Dependencies = std::move(*DependenciesOrErr);
328+
return generateHollowDiagnosticOutputImportSet(*ScanDiagnosticConsumer);
264329

265-
return Dependencies;
330+
return std::move(*DependenciesOrErr);
266331
}
267332

268333
std::vector<llvm::ErrorOr<swiftscan_dependency_graph_t>>

0 commit comments

Comments
 (0)