|
13 | 13 | #include "swift/AST/PluginLoader.h"
|
14 | 14 | #include "swift/AST/ASTContext.h"
|
15 | 15 | #include "swift/AST/DiagnosticEngine.h"
|
16 |
| -#include "swift/AST/DiagnosticsFrontend.h" |
| 16 | +#include "swift/AST/DiagnosticsSema.h" |
17 | 17 | #include "swift/Basic/Assertions.h"
|
18 | 18 | #include "swift/Basic/SourceManager.h"
|
19 | 19 | #include "swift/Parse/Lexer.h"
|
20 | 20 | #include "llvm/Config/config.h"
|
| 21 | +#include "llvm/Support/PrefixMapper.h" |
21 | 22 | #include "llvm/Support/VirtualFileSystem.h"
|
22 | 23 |
|
23 | 24 | using namespace swift;
|
@@ -96,9 +97,45 @@ PluginLoader::getPluginMap() {
|
96 | 97 | map[moduleNameIdentifier] = {libPath, execPath};
|
97 | 98 | };
|
98 | 99 |
|
| 100 | + std::optional<llvm::PrefixMapper> mapper; |
| 101 | + if (!PathRemap.empty()) { |
| 102 | + SmallVector<llvm::MappedPrefix, 4> prefixes; |
| 103 | + llvm::MappedPrefix::transformJoinedIfValid(PathRemap, prefixes); |
| 104 | + mapper.emplace(); |
| 105 | + mapper->addRange(prefixes); |
| 106 | + mapper->sort(); |
| 107 | + } |
| 108 | + auto remapPath = [&mapper](StringRef path) { |
| 109 | + if (!mapper) |
| 110 | + return path.str(); |
| 111 | + return mapper->mapToString(path); |
| 112 | + }; |
| 113 | + |
99 | 114 | auto fs = getPluginLoadingFS(Ctx);
|
100 | 115 | std::error_code ec;
|
101 | 116 |
|
| 117 | + auto validateLibrary = [&](StringRef path) -> llvm::Expected<std::string> { |
| 118 | + auto remappedPath = remapPath(path); |
| 119 | + if (!Ctx.SearchPathOpts.ResolvedPluginVerification || path.empty()) |
| 120 | + return remappedPath; |
| 121 | + |
| 122 | + auto currentStat = fs->status(remappedPath); |
| 123 | + if (!currentStat) |
| 124 | + return llvm::createFileError(remappedPath, currentStat.getError()); |
| 125 | + |
| 126 | + auto goldStat = Ctx.SourceMgr.getFileSystem()->status(path); |
| 127 | + if (!goldStat) |
| 128 | + return llvm::createStringError( |
| 129 | + "cannot open gold reference library to compare"); |
| 130 | + |
| 131 | + // Compare the size for difference for now. |
| 132 | + if (currentStat->getSize() != goldStat->getSize()) |
| 133 | + return llvm::createStringError( |
| 134 | + "plugin has changed since dependency scanning"); |
| 135 | + |
| 136 | + return remappedPath; |
| 137 | + }; |
| 138 | + |
102 | 139 | for (auto &entry : Ctx.SearchPathOpts.PluginSearchOpts) {
|
103 | 140 | switch (entry.getKind()) {
|
104 | 141 |
|
@@ -156,9 +193,17 @@ PluginLoader::getPluginMap() {
|
156 | 193 | // Respect resolved plugin config above other search path, and it can
|
157 | 194 | // overwrite plugins found by other options or previous resolved
|
158 | 195 | // configuration.
|
159 |
| - for (auto &moduleName : val.ModuleNames) |
160 |
| - try_emplace(moduleName, val.LibraryPath, val.ExecutablePath, |
| 196 | + for (auto &moduleName : val.ModuleNames) { |
| 197 | + auto libPath = validateLibrary(val.LibraryPath); |
| 198 | + if (!libPath) { |
| 199 | + Ctx.Diags.diagnose(SourceLoc(), diag::resolved_macro_changed, |
| 200 | + remapPath(val.LibraryPath), |
| 201 | + toString(libPath.takeError())); |
| 202 | + continue; |
| 203 | + } |
| 204 | + try_emplace(moduleName, *libPath, remapPath(val.ExecutablePath), |
161 | 205 | /*overwrite*/ true);
|
| 206 | + } |
162 | 207 | continue;
|
163 | 208 | }
|
164 | 209 | }
|
|
0 commit comments