|
1 | 1 | #include "swift/extractor/invocation/SwiftInvocationExtractor.h"
|
| 2 | +#include "swift/extractor/remapping/SwiftFileInterception.h" |
| 3 | +#include "swift/extractor/infra/TargetDomains.h" |
| 4 | +#include "swift/extractor/trap/generated/TrapTags.h" |
| 5 | +#include "swift/extractor/infra/file/TargetFile.h" |
| 6 | +#include "swift/extractor/infra/file/Path.h" |
| 7 | +#include "swift/extractor/trap/LinkDomain.h" |
| 8 | + |
| 9 | +namespace fs = std::filesystem; |
| 10 | +using namespace std::string_literals; |
2 | 11 |
|
3 | 12 | namespace codeql {
|
| 13 | +namespace { |
| 14 | +std::string getModuleId(const std::string_view& name, const std::string_view& hash) { |
| 15 | + auto ret = "module:"s; |
| 16 | + ret += name; |
| 17 | + ret += ':'; |
| 18 | + ret += hash; |
| 19 | + return ret; |
| 20 | +} |
| 21 | + |
| 22 | +std::string getModuleId(const swift::ModuleDecl* module, const std::string_view& hash) { |
| 23 | + return getModuleId(module->getRealName().str(), hash); |
| 24 | +} |
| 25 | + |
| 26 | +fs::path getModuleTarget(const std::string_view& name, const std::string_view& hash) { |
| 27 | + fs::path ret{"modules"}; |
| 28 | + ret /= name; |
| 29 | + ret += '_'; |
| 30 | + ret += hash; |
| 31 | + ret /= "module"; |
| 32 | + return ret; |
| 33 | +} |
| 34 | + |
| 35 | +std::optional<std::string> getModuleHash(const fs::path& moduleFile) { |
| 36 | + return getHashOfRealFile(moduleFile); |
| 37 | +} |
| 38 | + |
| 39 | +std::optional<std::string> getModuleHash(const swift::ModuleDecl* module) { |
| 40 | + return getModuleHash(std::string_view{module->getModuleFilename()}); |
| 41 | +} |
| 42 | + |
| 43 | +std::string getSourceId(const fs::path& file) { |
| 44 | + return "source:"s + file.c_str(); |
| 45 | +} |
| 46 | + |
| 47 | +std::string getSourceId(const swift::InputFile& input) { |
| 48 | + return getSourceId(resolvePath(input.getFileName())); |
| 49 | +} |
| 50 | + |
| 51 | +fs::path getSourceTarget(const fs::path& file) { |
| 52 | + return "sources" / file.relative_path(); |
| 53 | +} |
| 54 | + |
| 55 | +struct ModuleInfo { |
| 56 | + fs::path target; |
| 57 | + std::string id; |
| 58 | +}; |
| 59 | + |
| 60 | +std::vector<ModuleInfo> emitModuleImplementations(SwiftExtractorState& state, |
| 61 | + const std::string& moduleName) { |
| 62 | + std::vector<ModuleInfo> ret; |
| 63 | + ret.reserve(state.originalOutputModules.size()); |
| 64 | + for (const auto& modulePath : state.originalOutputModules) { |
| 65 | + if (auto hash = getModuleHash(modulePath)) { |
| 66 | + auto target = getModuleTarget(moduleName, *hash); |
| 67 | + if (auto moduleTrap = createTargetTrapDomain(state, target, TrapType::linkage)) { |
| 68 | + moduleTrap->createLabelWithImplementationId<ModuleDeclTag>(*hash, moduleName); |
| 69 | + ret.push_back({target, getModuleId(moduleName, *hash)}); |
| 70 | + } |
| 71 | + } |
| 72 | + } |
| 73 | + return ret; |
| 74 | +} |
| 75 | + |
| 76 | +void emitLinkFile(const SwiftExtractorState& state, const fs::path& target, const std::string& id) { |
| 77 | + if (auto link = createTargetLinkDomain(state, target)) { |
| 78 | + link->emitTarget(id); |
| 79 | + link->emitObjectDependency(id); |
| 80 | + } |
| 81 | +} |
| 82 | + |
| 83 | +void emitSourceObjectDependencies(const SwiftExtractorState& state, |
| 84 | + const fs::path& target, |
| 85 | + const std::string& id) { |
| 86 | + if (auto object = createTargetObjectDomain(state, target)) { |
| 87 | + object->emitObject(id); |
| 88 | + for (auto encounteredModule : state.encounteredModules) { |
| 89 | + if (auto depHash = getModuleHash(encounteredModule)) { |
| 90 | + object->emitObjectDependency(getModuleId(encounteredModule, *depHash)); |
| 91 | + } |
| 92 | + } |
| 93 | + for (const auto& requestedTrap : state.traps) { |
| 94 | + object->emitTrapDependency(requestedTrap); |
| 95 | + } |
| 96 | + } |
| 97 | +} |
| 98 | + |
| 99 | +void replaceMergedModulesImplementation(const SwiftExtractorState& state, |
| 100 | + const std::string& name, |
| 101 | + const fs::path& mergeTarget, |
| 102 | + const std::string& mergedPartHash) { |
| 103 | + std::error_code ec; |
| 104 | + auto mergedPartTarget = getModuleTarget(name, mergedPartHash); |
| 105 | + fs::copy(getTrapPath(state, mergeTarget, TrapType::linkage), |
| 106 | + getTrapPath(state, mergedPartTarget, TrapType::linkage), |
| 107 | + fs::copy_options::overwrite_existing, ec); |
| 108 | + if (ec) { |
| 109 | + std::cerr << "unable to replace merged module trap implementation id (" << ec.message() << ")"; |
| 110 | + } |
| 111 | +} |
| 112 | + |
| 113 | +void emitModuleObjectDependencies(const SwiftExtractorState& state, |
| 114 | + const swift::FrontendOptions& options, |
| 115 | + const fs::path& target, |
| 116 | + const std::string& id) { |
| 117 | + if (auto object = createTargetObjectDomain(state, target)) { |
| 118 | + object->emitObject(id); |
| 119 | + for (auto encounteredModule : state.encounteredModules) { |
| 120 | + if (auto depHash = getModuleHash(encounteredModule)) { |
| 121 | + object->emitObjectDependency(getModuleId(encounteredModule, *depHash)); |
| 122 | + } |
| 123 | + } |
| 124 | + if (options.RequestedAction == swift::FrontendOptions::ActionType::MergeModules) { |
| 125 | + for (const auto& input : options.InputsAndOutputs.getAllInputs()) { |
| 126 | + if (auto mergedHash = getModuleHash(input.getFileName())) { |
| 127 | + object->emitObjectDependency(getModuleId(options.ModuleName, *mergedHash)); |
| 128 | + replaceMergedModulesImplementation(state, options.ModuleName, target, *mergedHash); |
| 129 | + } |
| 130 | + } |
| 131 | + } else { |
| 132 | + for (const auto& input : options.InputsAndOutputs.getAllInputs()) { |
| 133 | + object->emitObjectDependency(getSourceId(input)); |
| 134 | + } |
| 135 | + } |
| 136 | + for (const auto& requestedTrap : state.traps) { |
| 137 | + object->emitTrapDependency(requestedTrap); |
| 138 | + } |
| 139 | + } |
| 140 | +} |
| 141 | +} // namespace |
| 142 | + |
4 | 143 | void extractSwiftInvocation(SwiftExtractorState& state,
|
5 | 144 | swift::CompilerInstance& compiler,
|
6 |
| - codeql::TrapDomain& trap) {} |
| 145 | + TrapDomain& trap) { |
| 146 | + const auto& options = compiler.getInvocation().getFrontendOptions(); |
| 147 | + |
| 148 | + // notice that there is only one unique module name per frontend run, even when outputting |
| 149 | + // multiples `swiftmodule` files |
| 150 | + // this step must be executed first so that we can capture in `state` the emitted trap files |
| 151 | + // that will be used in following steps |
| 152 | + auto modules = emitModuleImplementations(state, options.ModuleName); |
| 153 | + |
| 154 | + for (const auto& input : state.sourceFiles) { |
| 155 | + auto path = resolvePath(input); |
| 156 | + auto target = getSourceTarget(path); |
| 157 | + auto inputId = getSourceId(path); |
| 158 | + emitSourceObjectDependencies(state, target, inputId); |
| 159 | + } |
| 160 | + |
| 161 | + for (const auto& [target, moduleId] : modules) { |
| 162 | + emitLinkFile(state, target, moduleId); |
| 163 | + emitModuleObjectDependencies(state, options, target, moduleId); |
| 164 | + } |
| 165 | +} |
7 | 166 | } // namespace codeql
|
0 commit comments