[Explicit Module Loader] Refactor ESWL to use findModule instead of findModuleFilesInDirectory

And add isFramework to the Explicit Module Map and its parser.
This commit is contained in:
Artem Chikin
2020-08-12 12:06:46 -07:00
parent 40c1687fd2
commit cf773c3d0e
4 changed files with 56 additions and 29 deletions

View File

@@ -112,6 +112,7 @@
#include "swift/Frontend/ModuleInterfaceSupport.h" #include "swift/Frontend/ModuleInterfaceSupport.h"
#include "swift/Serialization/SerializedModuleLoader.h" #include "swift/Serialization/SerializedModuleLoader.h"
#include "llvm/Support/StringSaver.h" #include "llvm/Support/StringSaver.h"
#include <sstream>
namespace clang { namespace clang {
class CompilerInstance; class CompilerInstance;
@@ -133,14 +134,22 @@ class ExplicitSwiftModuleLoader: public SerializedModuleLoaderBase {
explicit ExplicitSwiftModuleLoader(ASTContext &ctx, DependencyTracker *tracker, explicit ExplicitSwiftModuleLoader(ASTContext &ctx, DependencyTracker *tracker,
ModuleLoadingMode loadMode, ModuleLoadingMode loadMode,
bool IgnoreSwiftSourceInfoFile); bool IgnoreSwiftSourceInfoFile);
bool findModule(AccessPathElem moduleID,
SmallVectorImpl<char> *moduleInterfacePath,
std::unique_ptr<llvm::MemoryBuffer> *moduleBuffer,
std::unique_ptr<llvm::MemoryBuffer> *moduleDocBuffer,
std::unique_ptr<llvm::MemoryBuffer> *moduleSourceInfoBuffer,
bool &isFramework, bool &isSystemModule) override;
std::error_code findModuleFilesInDirectory( std::error_code findModuleFilesInDirectory(
AccessPathElem ModuleID, AccessPathElem ModuleID,
const SerializedModuleBaseName &BaseName, const SerializedModuleBaseName &BaseName,
SmallVectorImpl<char> *ModuleInterfacePath, SmallVectorImpl<char> *ModuleInterfacePath,
std::unique_ptr<llvm::MemoryBuffer> *ModuleBuffer, std::unique_ptr<llvm::MemoryBuffer> *ModuleBuffer,
std::unique_ptr<llvm::MemoryBuffer> *ModuleDocBuffer, std::unique_ptr<llvm::MemoryBuffer> *ModuleDocBuffer,
std::unique_ptr<llvm::MemoryBuffer> *ModuleSourceInfoBuffer, std::unique_ptr<llvm::MemoryBuffer> *ModuleSourceInfoBuffer,
bool IsFramework) override; bool IsFramework) override;
bool canImportModule(Located<Identifier> mID) override; bool canImportModule(Located<Identifier> mID) override;
@@ -173,6 +182,8 @@ struct ExplicitModuleInfo {
std::string moduleSourceInfoPath; std::string moduleSourceInfoPath;
// Opened buffer for the .swiftmodule file. // Opened buffer for the .swiftmodule file.
std::unique_ptr<llvm::MemoryBuffer> moduleBuffer; std::unique_ptr<llvm::MemoryBuffer> moduleBuffer;
// A flag that indicates whether this module is a framework
bool isFramework;
}; };
/// Parser of explicit module maps passed into the compiler. /// Parser of explicit module maps passed into the compiler.
@@ -182,12 +193,14 @@ struct ExplicitModuleInfo {
// "modulePath": "A.swiftmodule", // "modulePath": "A.swiftmodule",
// "docPath": "A.swiftdoc", // "docPath": "A.swiftdoc",
// "sourceInfoPath": "A.swiftsourceinfo" // "sourceInfoPath": "A.swiftsourceinfo"
// "isFramework": false
// }, // },
// { // {
// "moduleName": "B", // "moduleName": "B",
// "modulePath": "B.swiftmodule", // "modulePath": "B.swiftmodule",
// "docPath": "B.swiftdoc", // "docPath": "B.swiftdoc",
// "sourceInfoPath": "B.swiftsourceinfo" // "sourceInfoPath": "B.swiftsourceinfo"
// "isFramework": false
// } // }
// ] // ]
class ExplicitModuleMapParser { class ExplicitModuleMapParser {
@@ -249,6 +262,9 @@ private:
result.moduleDocPath = val.str(); result.moduleDocPath = val.str();
} else if (key == "sourceInfoPath") { } else if (key == "sourceInfoPath") {
result.moduleSourceInfoPath = val.str(); result.moduleSourceInfoPath = val.str();
} else if (key == "isFramework") {
std::istringstream is(val.str());
is >> std::boolalpha >> result.isFramework;
} else { } else {
// Being forgiving for future fields. // Being forgiving for future fields.
continue; continue;

View File

@@ -73,12 +73,12 @@ protected:
StringRef extension) const; StringRef extension) const;
using AccessPathElem = Located<Identifier>; using AccessPathElem = Located<Identifier>;
bool findModule(AccessPathElem moduleID, virtual bool findModule(AccessPathElem moduleID,
SmallVectorImpl<char> *moduleInterfacePath, SmallVectorImpl<char> *moduleInterfacePath,
std::unique_ptr<llvm::MemoryBuffer> *moduleBuffer, std::unique_ptr<llvm::MemoryBuffer> *moduleBuffer,
std::unique_ptr<llvm::MemoryBuffer> *moduleDocBuffer, std::unique_ptr<llvm::MemoryBuffer> *moduleDocBuffer,
std::unique_ptr<llvm::MemoryBuffer> *moduleSourceInfoBuffer, std::unique_ptr<llvm::MemoryBuffer> *moduleSourceInfoBuffer,
bool &isFramework, bool &isSystemModule); bool &isFramework, bool &isSystemModule);
/// Attempts to search the provided directory for a loadable serialized /// Attempts to search the provided directory for a loadable serialized
/// .swiftmodule with the provided `ModuleFilename`. Subclasses must /// .swiftmodule with the provided `ModuleFilename`. Subclasses must

View File

@@ -1522,29 +1522,30 @@ ExplicitSwiftModuleLoader::ExplicitSwiftModuleLoader(
ExplicitSwiftModuleLoader::~ExplicitSwiftModuleLoader() { delete &Impl; } ExplicitSwiftModuleLoader::~ExplicitSwiftModuleLoader() { delete &Impl; }
std::error_code ExplicitSwiftModuleLoader::findModuleFilesInDirectory( bool ExplicitSwiftModuleLoader::findModule(AccessPathElem ModuleID,
AccessPathElem ModuleID, SmallVectorImpl<char> *ModuleInterfacePath,
const SerializedModuleBaseName &BaseName, std::unique_ptr<llvm::MemoryBuffer> *ModuleBuffer,
SmallVectorImpl<char> *ModuleInterfacePath, std::unique_ptr<llvm::MemoryBuffer> *ModuleDocBuffer,
std::unique_ptr<llvm::MemoryBuffer> *ModuleBuffer, std::unique_ptr<llvm::MemoryBuffer> *ModuleSourceInfoBuffer,
std::unique_ptr<llvm::MemoryBuffer> *ModuleDocBuffer, bool &IsFramework, bool &IsSystemModule) {
std::unique_ptr<llvm::MemoryBuffer> *ModuleSourceInfoBuffer,
bool IsFramework) {
StringRef moduleName = ModuleID.Item.str(); StringRef moduleName = ModuleID.Item.str();
auto it = Impl.ExplicitModuleMap.find(moduleName); auto it = Impl.ExplicitModuleMap.find(moduleName);
// If no explicit module path is given matches the name, return with an // If no explicit module path is given matches the name, return with an
// error code. // error code.
if (it == Impl.ExplicitModuleMap.end()) { if (it == Impl.ExplicitModuleMap.end()) {
return std::make_error_code(std::errc::not_supported); return false;
} }
auto &moduleInfo = it->getValue(); auto &moduleInfo = it->getValue();
if (moduleInfo.moduleBuffer) { if (moduleInfo.moduleBuffer) {
// We found an explicit module matches the given name, give the buffer // We found an explicit module matches the given name, give the buffer
// back to the caller side. // back to the caller side.
*ModuleBuffer = std::move(moduleInfo.moduleBuffer); *ModuleBuffer = std::move(moduleInfo.moduleBuffer);
return std::error_code(); return true;
} }
// Set IsFramework bit according to the moduleInfo
IsFramework = moduleInfo.isFramework;
auto &fs = *Ctx.SourceMgr.getFileSystem(); auto &fs = *Ctx.SourceMgr.getFileSystem();
// Open .swiftmodule file // Open .swiftmodule file
auto moduleBuf = fs.getBufferForFile(moduleInfo.modulePath); auto moduleBuf = fs.getBufferForFile(moduleInfo.modulePath);
@@ -1552,7 +1553,7 @@ std::error_code ExplicitSwiftModuleLoader::findModuleFilesInDirectory(
// We cannot read the module content, diagnose. // We cannot read the module content, diagnose.
Ctx.Diags.diagnose(SourceLoc(), diag::error_opening_explicit_module_file, Ctx.Diags.diagnose(SourceLoc(), diag::error_opening_explicit_module_file,
moduleInfo.modulePath); moduleInfo.modulePath);
return moduleBuf.getError(); return false;
} }
assert(moduleBuf); assert(moduleBuf);
@@ -1568,13 +1569,13 @@ std::error_code ExplicitSwiftModuleLoader::findModuleFilesInDirectory(
// We cannot read the module content, diagnose. // We cannot read the module content, diagnose.
Ctx.Diags.diagnose(SourceLoc(), diag::error_opening_explicit_module_file, Ctx.Diags.diagnose(SourceLoc(), diag::error_opening_explicit_module_file,
moduleInfo.modulePath); moduleInfo.modulePath);
return moduleBuf.getError(); return false;
} }
} else { } else {
// We cannot read the module content, diagnose. // We cannot read the module content, diagnose.
Ctx.Diags.diagnose(SourceLoc(), diag::error_opening_explicit_module_file, Ctx.Diags.diagnose(SourceLoc(), diag::error_opening_explicit_module_file,
moduleInfo.modulePath); moduleInfo.modulePath);
return forwardingModule.getError(); return false;
} }
} }
assert(moduleBuf); assert(moduleBuf);
@@ -1593,7 +1594,19 @@ std::error_code ExplicitSwiftModuleLoader::findModuleFilesInDirectory(
if (moduleSourceInfoBuf) if (moduleSourceInfoBuf)
*ModuleSourceInfoBuffer = std::move(moduleSourceInfoBuf.get()); *ModuleSourceInfoBuffer = std::move(moduleSourceInfoBuf.get());
} }
return std::error_code(); return true;
}
std::error_code ExplicitSwiftModuleLoader::findModuleFilesInDirectory(
AccessPathElem ModuleID,
const SerializedModuleBaseName &BaseName,
SmallVectorImpl<char> *ModuleInterfacePath,
std::unique_ptr<llvm::MemoryBuffer> *ModuleBuffer,
std::unique_ptr<llvm::MemoryBuffer> *ModuleDocBuffer,
std::unique_ptr<llvm::MemoryBuffer> *ModuleSourceInfoBuffer,
bool IsFramework) {
llvm_unreachable("Not supported in the Explicit Swift Module Loader.");
return std::make_error_code(std::errc::not_supported);
} }
bool ExplicitSwiftModuleLoader::canImportModule( bool ExplicitSwiftModuleLoader::canImportModule(

View File

@@ -682,7 +682,6 @@ FileUnit *SerializedModuleLoaderBase::loadAST(
std::unique_ptr<llvm::MemoryBuffer> moduleSourceInfoInputBuffer, std::unique_ptr<llvm::MemoryBuffer> moduleSourceInfoInputBuffer,
bool isFramework) { bool isFramework) {
assert(moduleInputBuffer); assert(moduleInputBuffer);
StringRef moduleBufferID = moduleInputBuffer->getBufferIdentifier(); StringRef moduleBufferID = moduleInputBuffer->getBufferIdentifier();
StringRef moduleDocBufferID; StringRef moduleDocBufferID;
if (moduleDocInputBuffer) if (moduleDocInputBuffer)
@@ -993,7 +992,6 @@ SerializedModuleLoaderBase::loadModule(SourceLoc importLoc,
StringRef moduleInterfacePathStr = StringRef moduleInterfacePathStr =
Ctx.AllocateCopy(moduleInterfacePath.str()); Ctx.AllocateCopy(moduleInterfacePath.str());
auto *file = auto *file =
loadAST(*M, moduleID.Loc, moduleInterfacePathStr, loadAST(*M, moduleID.Loc, moduleInterfacePathStr,
std::move(moduleInputBuffer), std::move(moduleDocInputBuffer), std::move(moduleInputBuffer), std::move(moduleDocInputBuffer),