mirror of
https://github.com/apple/swift.git
synced 2025-12-14 20:36:38 +01:00
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.
131 lines
5.1 KiB
C++
131 lines
5.1 KiB
C++
//===----- ModuleInterfaceBuilder.h - Compiles .swiftinterface files ------===//
|
|
//
|
|
// This source file is part of the Swift.org open source project
|
|
//
|
|
// Copyright (c) 2019 Apple Inc. and the Swift project authors
|
|
// Licensed under Apache License v2.0 with Runtime Library Exception
|
|
//
|
|
// See https://swift.org/LICENSE.txt for license information
|
|
// See https://swift.org/CONTRIBUTORS.txt for the list of Swift project authors
|
|
//
|
|
//===----------------------------------------------------------------------===//
|
|
|
|
#ifndef SWIFT_FRONTEND_MODULEINTERFACEBUILDER_H
|
|
#define SWIFT_FRONTEND_MODULEINTERFACEBUILDER_H
|
|
|
|
#include "swift/Basic/LLVM.h"
|
|
#include "swift/Basic/SourceLoc.h"
|
|
#include "swift/Frontend/Frontend.h"
|
|
#include "swift/Serialization/SerializationOptions.h"
|
|
#include "llvm/Support/StringSaver.h"
|
|
|
|
namespace llvm {
|
|
namespace vfs {
|
|
class FileSystem;
|
|
}
|
|
}
|
|
|
|
namespace swift {
|
|
|
|
class DiagnosticEngine;
|
|
class LangOptions;
|
|
class SearchPathOptions;
|
|
class DependencyTracker;
|
|
|
|
class ModuleInterfaceBuilder {
|
|
SourceManager &sourceMgr;
|
|
DiagnosticEngine &diags;
|
|
const StringRef interfacePath;
|
|
const StringRef moduleName;
|
|
const StringRef moduleCachePath;
|
|
const StringRef prebuiltCachePath;
|
|
const bool serializeDependencyHashes;
|
|
const bool trackSystemDependencies;
|
|
const bool remarkOnRebuildFromInterface;
|
|
const bool disableInterfaceFileLock;
|
|
const SourceLoc diagnosticLoc;
|
|
DependencyTracker *const dependencyTracker;
|
|
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,
|
|
const LangOptions &LangOpts,
|
|
ClangModuleLoader *ClangLoader);
|
|
|
|
/// Populate the provided \p Deps with \c FileDependency entries for all
|
|
/// dependencies \p SubInstance's DependencyTracker recorded while compiling
|
|
/// the module, excepting .swiftmodules in \p moduleCachePath or
|
|
/// \p prebuiltCachePath. Those have _their_ dependencies added instead, both
|
|
/// to avoid having to do recursive scanning when rechecking this dependency
|
|
/// in future and to make the module caches relocatable.
|
|
bool collectDepsForSerialization(
|
|
CompilerInstance &SubInstance,
|
|
SmallVectorImpl<SerializationOptions::FileDependency> &Deps,
|
|
bool IsHashBased);
|
|
|
|
bool extractSwiftInterfaceVersionAndArgs(
|
|
version::Version &Vers, StringRef &CompilerVersion,
|
|
llvm::StringSaver &SubArgSaver, SmallVectorImpl<const char *> &SubArgs);
|
|
|
|
bool buildSwiftModuleInternal(StringRef OutPath, bool ShouldSerializeDeps,
|
|
std::unique_ptr<llvm::MemoryBuffer> *ModuleBuffer);
|
|
public:
|
|
ModuleInterfaceBuilder(SourceManager &sourceMgr, DiagnosticEngine &diags,
|
|
const SearchPathOptions &searchPathOpts,
|
|
const LangOptions &langOpts,
|
|
ClangModuleLoader *clangImporter,
|
|
StringRef interfacePath,
|
|
StringRef moduleName,
|
|
StringRef moduleCachePath,
|
|
StringRef prebuiltCachePath,
|
|
bool serializeDependencyHashes = false,
|
|
bool trackSystemDependencies = false,
|
|
bool remarkOnRebuildFromInterface = false,
|
|
bool disableInterfaceFileLock = false,
|
|
SourceLoc diagnosticLoc = SourceLoc(),
|
|
DependencyTracker *tracker = nullptr)
|
|
: sourceMgr(sourceMgr), diags(diags),
|
|
interfacePath(interfacePath), moduleName(moduleName),
|
|
moduleCachePath(moduleCachePath), prebuiltCachePath(prebuiltCachePath),
|
|
serializeDependencyHashes(serializeDependencyHashes),
|
|
trackSystemDependencies(trackSystemDependencies),
|
|
remarkOnRebuildFromInterface(remarkOnRebuildFromInterface),
|
|
disableInterfaceFileLock(disableInterfaceFileLock),
|
|
diagnosticLoc(diagnosticLoc), dependencyTracker(tracker) {
|
|
configureSubInvocation(searchPathOpts, langOpts, clangImporter);
|
|
}
|
|
|
|
const CompilerInvocation &getSubInvocation() const {
|
|
return subInvocation;
|
|
}
|
|
|
|
/// Ensures the requested file name is added as a dependency of the resulting
|
|
/// module.
|
|
void addExtraDependency(StringRef path) {
|
|
extraDependencies.push_back(path);
|
|
}
|
|
|
|
bool buildSwiftModule(StringRef OutPath, bool ShouldSerializeDeps,
|
|
std::unique_ptr<llvm::MemoryBuffer> *ModuleBuffer,
|
|
llvm::function_ref<void()> RemarkRebuild = nullptr);
|
|
};
|
|
|
|
} // end namespace swift
|
|
|
|
#endif // defined(SWIFT_FRONTEND_MODULEINTERFACEBUILDER_H)
|