Print proper error message when the swiftmodule for architecture not found (#17092)

https://bugs.swift.org/browse/SR-7160
This commit is contained in:
Tapan Thaker
2018-06-19 06:11:24 +05:30
committed by Jordan Rose
parent 8f23048c85
commit 359385babf
3 changed files with 94 additions and 10 deletions

View File

@@ -81,12 +81,48 @@ openModuleFiles(StringRef DirName, StringRef ModuleFilename,
return std::error_code();
}
static void addDiagnosticInfoForArchitectureMismatch(ASTContext &ctx,
SourceLoc sourceLocation,
StringRef moduleName,
StringRef archName,
StringRef directoryPath) {
std::error_code errorCode;
llvm::sys::fs::directory_iterator directoryIterator(directoryPath, errorCode,
true);
llvm::sys::fs::directory_iterator endIterator;
if (errorCode) {
return;
}
std::string foundArchs;
for (; directoryIterator != endIterator;
directoryIterator.increment(errorCode)) {
if (errorCode) {
return;
}
auto entry = *directoryIterator;
StringRef filePath(entry.path());
StringRef extension = llvm::sys::path::extension(filePath);
if (extension.startswith(".") &&
extension.drop_front() == SERIALIZED_MODULE_EXTENSION) {
foundArchs = foundArchs + (foundArchs.length() > 0 ? ", " : "") +
llvm::sys::path::stem(filePath).str();
}
}
ctx.Diags.diagnose(sourceLocation, diag::sema_no_import_arch, moduleName,
archName, foundArchs);
}
static bool
findModule(ASTContext &ctx, AccessPathElem moduleID,
std::unique_ptr<llvm::MemoryBuffer> *moduleBuffer,
std::unique_ptr<llvm::MemoryBuffer> *moduleDocBuffer,
bool &isFramework) {
llvm::SmallString<64> moduleFilename(moduleID.first.str());
llvm::SmallString<64> moduleName(moduleID.first.str());
llvm::SmallString<64> moduleFilename(moduleName);
moduleFilename += '.';
moduleFilename += SERIALIZED_MODULE_EXTENSION;
@@ -96,9 +132,10 @@ findModule(ASTContext &ctx, AccessPathElem moduleID,
// FIXME: Which name should we be using here? Do we care about CPU subtypes?
// FIXME: At the very least, don't hardcode "arch".
llvm::SmallString<16> archFile{
llvm::SmallString<16> archName{
ctx.LangOpts.getPlatformConditionValue(PlatformConditionKind::Arch)};
llvm::SmallString<16> archDocFile{archFile};
llvm::SmallString<16> archFile{archName};
llvm::SmallString<16> archDocFile{archName};
if (!archFile.empty()) {
archFile += '.';
archFile += SERIALIZED_MODULE_EXTENSION;
@@ -122,6 +159,12 @@ findModule(ASTContext &ctx, AccessPathElem moduleID,
archFile.str(), archDocFile.str(),
moduleBuffer, moduleDocBuffer,
scratch);
if (err == std::errc::no_such_file_or_directory) {
addDiagnosticInfoForArchitectureMismatch(
ctx, moduleID.second, moduleName, archName, currPath);
return false;
}
}
if (!err)
return true;
@@ -134,12 +177,22 @@ findModule(ASTContext &ctx, AccessPathElem moduleID,
auto tryFrameworkImport = [&](StringRef frameworkPath) -> bool {
currPath = frameworkPath;
llvm::sys::path::append(currPath, moduleFramework.str(),
"Modules", moduleFilename.str());
auto err = openModuleFiles(currPath,
archFile.str(), archDocFile.str(),
moduleBuffer, moduleDocBuffer,
scratch);
llvm::sys::path::append(currPath, moduleFramework.str());
// Check if the framework directory exists
if (!llvm::sys::fs::is_directory(currPath)) {
return false;
}
llvm::sys::path::append(currPath, "Modules", moduleFilename.str());
auto err = openModuleFiles(currPath, archFile.str(), archDocFile.str(),
moduleBuffer, moduleDocBuffer, scratch);
if (err == std::errc::no_such_file_or_directory) {
addDiagnosticInfoForArchitectureMismatch(
ctx, moduleID.second, moduleName, archName, currPath);
return false;
}
return !err;
};
@@ -649,4 +702,4 @@ SerializedASTFile::getDiscriminatorForPrivateValue(const ValueDecl *D) const {
Identifier discriminator = File.getDiscriminatorForPrivateValue(D);
assert(!discriminator.empty() && "no discriminator found for value");
return discriminator;
}
}