Skip to content

Commit ed8d696

Browse files
authored
Merge pull request #82026 from artemcm/DiagnoseMissingModulesSeenInSerializedSearchPaths_62
[6.2 🍒][Dependency Scanning] On failure to locate a module, attempt to diagnose if binary dependencies contain search paths with this module.
2 parents cf81104 + e37071b commit ed8d696

12 files changed

+463
-91
lines changed

include/swift/AST/DiagnosticGroups.def

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -50,6 +50,7 @@ GROUP(ExistentialAny, "existential-any")
5050
GROUP(ExistentialMemberAccess, "existential-member-access-limitations")
5151
GROUP(IsolatedConformances, "isolated-conformances")
5252
GROUP(MemberImportVisibility, "member-import-visibility")
53+
GROUP(MissingModuleOnKnownPaths, "missing-module-on-known-paths")
5354
GROUP(MultipleInheritance, "multiple-inheritance")
5455
GROUP(MutableGlobalVariable, "mutable-global-variable")
5556
GROUP(NominalTypes, "nominal-types")

include/swift/AST/DiagnosticsSema.def

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2332,12 +2332,18 @@ ERROR(alignment_not_power_of_two,none,
23322332

23332333
// Dependency Scanning
23342334
ERROR(dependency_scan_module_not_found, none, "Unable to find module dependency: '%0'", (StringRef))
2335+
GROUPED_ERROR(dependency_scan_module_not_found_on_specified_search_paths, MissingModuleOnKnownPaths, none,
2336+
"Compilation search paths unable to resolve module dependency: '%0'", (StringRef))
23352337
NOTE(unresolved_import_location,none,
23362338
"also imported here", ())
23372339
NOTE(dependency_as_imported_by_main_module,none,
23382340
"a dependency of main module '%0'", (StringRef))
23392341
NOTE(dependency_as_imported_by, none,
23402342
"a dependency of %select{Swift|Clang}2 module '%0': '%1'", (StringRef, StringRef, bool))
2343+
NOTE(inherited_search_path_resolves_module,none,
2344+
"'%0' can be found using a search path that was specified when building module '%1' ('%2'). "
2345+
"This search path was not explicitly specified on the current compilation.", (StringRef, StringRef, StringRef))
2346+
23412347
ERROR(clang_dependency_scan_error, none, "Clang dependency scanner failure: %0", (StringRef))
23422348
ERROR(clang_header_dependency_scan_error, none, "Bridging header dependency scan failure: %0", (StringRef))
23432349

include/swift/AST/ModuleDependencies.h

Lines changed: 18 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -22,6 +22,7 @@
2222
#include "swift/AST/LinkLibrary.h"
2323
#include "swift/Basic/CXXStdlibKind.h"
2424
#include "swift/Basic/LLVM.h"
25+
#include "swift/Serialization/Validation.h"
2526
#include "clang/CAS/CASOptions.h"
2627
#include "clang/Tooling/DependencyScanning/DependencyScanningService.h"
2728
#include "clang/Tooling/DependencyScanning/DependencyScanningTool.h"
@@ -406,15 +407,18 @@ class SwiftBinaryModuleDependencyStorage
406407
StringRef sourceInfoPath,
407408
ArrayRef<ScannerImportStatementInfo> moduleImports,
408409
ArrayRef<ScannerImportStatementInfo> optionalModuleImports,
409-
ArrayRef<LinkLibrary> linkLibraries, StringRef headerImport,
410-
StringRef definingModuleInterface, bool isFramework, bool isStatic,
411-
StringRef moduleCacheKey, StringRef userModuleVersion)
410+
ArrayRef<LinkLibrary> linkLibraries,
411+
ArrayRef<serialization::SearchPath> serializedSearchPaths,
412+
StringRef headerImport, StringRef definingModuleInterface,
413+
bool isFramework, bool isStatic, StringRef moduleCacheKey,
414+
StringRef userModuleVersion)
412415
: ModuleDependencyInfoStorageBase(ModuleDependencyKind::SwiftBinary,
413416
moduleImports, optionalModuleImports,
414417
linkLibraries, moduleCacheKey),
415418
compiledModulePath(compiledModulePath), moduleDocPath(moduleDocPath),
416419
sourceInfoPath(sourceInfoPath), headerImport(headerImport),
417420
definingModuleInterfacePath(definingModuleInterface),
421+
serializedSearchPaths(serializedSearchPaths),
418422
isFramework(isFramework), isStatic(isStatic),
419423
userModuleVersion(userModuleVersion) {}
420424

@@ -441,6 +445,9 @@ class SwiftBinaryModuleDependencyStorage
441445
/// Source files on which the header inputs depend.
442446
std::vector<std::string> headerSourceFiles;
443447

448+
/// Search paths this module was built with which got serialized
449+
std::vector<serialization::SearchPath> serializedSearchPaths;
450+
444451
/// (Clang) modules on which the header inputs depend.
445452
std::vector<ModuleDependencyID> headerModuleDependencies;
446453

@@ -613,15 +620,17 @@ class ModuleDependencyInfo {
613620
StringRef sourceInfoPath,
614621
ArrayRef<ScannerImportStatementInfo> moduleImports,
615622
ArrayRef<ScannerImportStatementInfo> optionalModuleImports,
616-
ArrayRef<LinkLibrary> linkLibraries, StringRef headerImport,
617-
StringRef definingModuleInterface, bool isFramework,
618-
bool isStatic, StringRef moduleCacheKey, StringRef userModuleVer) {
623+
ArrayRef<LinkLibrary> linkLibraries,
624+
ArrayRef<serialization::SearchPath> serializedSearchPaths,
625+
StringRef headerImport, StringRef definingModuleInterface,
626+
bool isFramework, bool isStatic, StringRef moduleCacheKey,
627+
StringRef userModuleVer) {
619628
return ModuleDependencyInfo(
620629
std::make_unique<SwiftBinaryModuleDependencyStorage>(
621630
compiledModulePath, moduleDocPath, sourceInfoPath, moduleImports,
622-
optionalModuleImports, linkLibraries, headerImport,
623-
definingModuleInterface,isFramework, isStatic, moduleCacheKey,
624-
userModuleVer));
631+
optionalModuleImports, linkLibraries, serializedSearchPaths,
632+
headerImport, definingModuleInterface,isFramework, isStatic,
633+
moduleCacheKey, userModuleVer));
625634
}
626635

627636
/// Describe the main Swift module.

include/swift/DependencyScan/ModuleDependencyScanner.h

Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -190,8 +190,26 @@ class ModuleDependencyScanner {
190190
template <typename Function, typename... Args>
191191
auto withDependencyScanningWorker(Function &&F, Args &&...ArgList);
192192

193+
/// Use the scanner's ASTContext to construct an `Identifier`
194+
/// for a given module name.
193195
Identifier getModuleImportIdentifier(StringRef moduleName);
194196

197+
/// Diagnose scanner failure and attempt to reconstruct the dependency
198+
/// path from the main module to the missing dependency.
199+
void diagnoseScannerFailure(const ScannerImportStatementInfo &moduleImport,
200+
const ModuleDependenciesCache &cache,
201+
std::optional<ModuleDependencyID> dependencyOf);
202+
203+
/// Assuming the \c `moduleImport` failed to resolve,
204+
/// iterate over all binary Swift module dependencies with serialized
205+
/// search paths and attempt to diagnose if the failed-to-resolve module
206+
/// can be found on any of them. Returns the path containing
207+
/// the module, if one is found.
208+
std::optional<std::pair<ModuleDependencyID, std::string>>
209+
attemptToFindResolvingSerializedSearchPath(
210+
const ScannerImportStatementInfo &moduleImport,
211+
const ModuleDependenciesCache &cache, const SourceLoc &importLoc);
212+
195213
private:
196214
const CompilerInvocation &ScanCompilerInvocation;
197215
ASTContext &ScanASTContext;

include/swift/DependencyScan/SerializedModuleDependencyCacheFormat.h

Lines changed: 16 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -41,7 +41,7 @@ using llvm::BCVBR;
4141
const unsigned char MODULE_DEPENDENCY_CACHE_FORMAT_SIGNATURE[] = {'I', 'M', 'D','C'};
4242
const unsigned MODULE_DEPENDENCY_CACHE_FORMAT_VERSION_MAJOR = 9;
4343
/// Increment this on every change.
44-
const unsigned MODULE_DEPENDENCY_CACHE_FORMAT_VERSION_MINOR = 2;
44+
const unsigned MODULE_DEPENDENCY_CACHE_FORMAT_VERSION_MINOR = 3;
4545

4646
/// Various identifiers in this format will rely on having their strings mapped
4747
/// using this ID.
@@ -78,6 +78,7 @@ using ModuleCacheKeyIDField = IdentifierIDField;
7878
using ImportArrayIDField = IdentifierIDField;
7979
using LinkLibrariesArrayIDField = IdentifierIDField;
8080
using MacroDependenciesArrayIDField = IdentifierIDField;
81+
using SearchPathArrayIDField = IdentifierIDField;
8182
using FlagIDArrayIDField = IdentifierIDField;
8283
using DependencyIDArrayIDField = IdentifierIDField;
8384
using SourceLocationIDArrayIDField = IdentifierIDField;
@@ -101,6 +102,8 @@ enum {
101102
LINK_LIBRARY_ARRAY_NODE,
102103
MACRO_DEPENDENCY_NODE,
103104
MACRO_DEPENDENCY_ARRAY_NODE,
105+
SEARCH_PATH_NODE,
106+
SEARCH_PATH_ARRAY_NODE,
104107
IMPORT_STATEMENT_NODE,
105108
IMPORT_STATEMENT_ARRAY_NODE,
106109
OPTIONAL_IMPORT_STATEMENT_ARRAY_NODE,
@@ -169,6 +172,17 @@ using MacroDependencyLayout =
169172
using MacroDependencyArrayLayout =
170173
BCRecordLayout<MACRO_DEPENDENCY_ARRAY_NODE, IdentifierIDArryField>;
171174

175+
// A record for a serialized search pathof a given dependency
176+
// node (Swift binary module dependency only).
177+
using SearchPathLayout =
178+
BCRecordLayout<SEARCH_PATH_NODE, // ID
179+
IdentifierIDField, // path
180+
IsFrameworkField, // isFramework
181+
IsSystemField // isSystem
182+
>;
183+
using SearchPathArrayLayout =
184+
BCRecordLayout<SEARCH_PATH_ARRAY_NODE, IdentifierIDArryField>;
185+
172186
// A record capturing information about a given 'import' statement
173187
// captured in a dependency node, including its source location.
174188
using ImportStatementLayout =
@@ -248,6 +262,7 @@ using SwiftBinaryModuleDetailsLayout =
248262
FileIDField, // definingInterfacePath
249263
IdentifierIDField, // headerModuleDependencies
250264
FileIDArrayIDField, // headerSourceFiles
265+
SearchPathArrayIDField, // serializedSearchPaths
251266
IsFrameworkField, // isFramework
252267
IsStaticField, // isStatic
253268
IdentifierIDField, // moduleCacheKey

0 commit comments

Comments
 (0)