Provide fallback SourceLoc for swiftinterface build errors

When a swiftinterface fails to build for any of various reasons, we try to diagnose the failure at the site of the `import` declaration. But if the import is implicitly added—which happens for many SDK modules, like the standard library and ClangImporter overlays—there is no source location for the import, so the error ends up being diagnosed at <unknown>:0. This causes a number of issues; most notably, Xcode doesn’t display the diagnostic as prominently as others.

This change falls back to diagnosing the error at line 1, column 1 of the swiftinterface file itself. This is perhaps not an ideal location, and it won’t help with I/O errors where we can’t open the swiftinterface file (and therefore can’t diagnose an error in it), but it should improve the way we display most module interface building errors.
This commit is contained in:
Brent Royal-Gordon
2020-03-17 18:44:31 -07:00
parent a7a5e340aa
commit a27fdad4e5
3 changed files with 34 additions and 21 deletions

View File

@@ -33,7 +33,7 @@ class SearchPathOptions;
class DependencyTracker;
class ModuleInterfaceBuilder {
llvm::vfs::FileSystem &fs;
SourceManager &sourceMgr;
DiagnosticEngine &diags;
const StringRef interfacePath;
const StringRef moduleName;
@@ -48,6 +48,19 @@ class ModuleInterfaceBuilder {
CompilerInvocation subInvocation;
SmallVector<StringRef, 3> extraDependencies;
/// Emit a diagnostic tied to this declaration.
template<typename ...ArgTypes>
InFlightDiagnostic diagnose(
Diag<ArgTypes...> ID,
typename detail::PassArgument<ArgTypes>::type... Args) const {
SourceLoc loc = diagnosticLoc;
if (loc.isInvalid()) {
// Diagnose this inside the interface file, if possible.
loc = sourceMgr.getLocFromExternalSource(interfacePath, 1, 1);
}
return diags.diagnose(loc, ID, std::move(Args)...);
}
void configureSubInvocationInputsAndOutputs(StringRef OutPath);
void configureSubInvocation(const SearchPathOptions &SearchPathOpts,
@@ -86,7 +99,7 @@ public:
bool disableInterfaceFileLock = false,
SourceLoc diagnosticLoc = SourceLoc(),
DependencyTracker *tracker = nullptr)
: fs(*sourceMgr.getFileSystem()), diags(diags),
: sourceMgr(sourceMgr), diags(diags),
interfacePath(interfacePath), moduleName(moduleName),
moduleCachePath(moduleCachePath), prebuiltCachePath(prebuiltCachePath),
serializeDependencyHashes(serializeDependencyHashes),