|
| 1 | +#include "Extractor.h" |
| 2 | + |
| 3 | +#include <filesystem> |
| 4 | +#include <fstream> |
| 5 | +#include <iostream> |
| 6 | +#include <sstream> |
| 7 | +#include <memory> |
| 8 | +#include <unistd.h> |
| 9 | + |
| 10 | +#include <swift/AST/SourceFile.h> |
| 11 | +#include <llvm/ADT/SmallString.h> |
| 12 | +#include <llvm/Support/FileSystem.h> |
| 13 | +#include <llvm/Support/Path.h> |
| 14 | + |
| 15 | +using namespace codeql; |
| 16 | + |
| 17 | +Extractor::Extractor(const Configuration& config, swift::CompilerInstance& instance) |
| 18 | + : config{config}, compiler{instance} {} |
| 19 | + |
| 20 | +void Extractor::extract() { |
| 21 | + if (compiler.getPrimarySourceFiles().empty()) { |
| 22 | + swift::ModuleDecl* module = compiler.getMainModule(); |
| 23 | + if (!module->getFiles().empty() && |
| 24 | + module->getFiles().front()->getKind() == swift::FileUnitKind::Source) { |
| 25 | + /// We can only call getMainSourceFile if the first file is of a Source kind |
| 26 | + swift::SourceFile& file = module->getMainSourceFile(); |
| 27 | + extractFile(file); |
| 28 | + } |
| 29 | + } else { |
| 30 | + for (auto s : compiler.getPrimarySourceFiles()) { |
| 31 | + extractFile(*s); |
| 32 | + } |
| 33 | + } |
| 34 | +} |
| 35 | + |
| 36 | +void Extractor::extractFile(swift::SourceFile& file) { |
| 37 | + if (std::error_code ec = llvm::sys::fs::create_directories(config.trapDir)) { |
| 38 | + std::cerr << "Cannot create TRAP directory: " << ec.message() << "\n"; |
| 39 | + return; |
| 40 | + } |
| 41 | + |
| 42 | + if (std::error_code ec = llvm::sys::fs::create_directories(config.sourceArchiveDir)) { |
| 43 | + std::cerr << "Cannot create source archive directory: " << ec.message() << "\n"; |
| 44 | + return; |
| 45 | + } |
| 46 | + |
| 47 | + llvm::SmallString<PATH_MAX> srcFilePath(file.getFilename()); |
| 48 | + llvm::sys::fs::make_absolute(srcFilePath); |
| 49 | + |
| 50 | + llvm::SmallString<PATH_MAX> dstFilePath(config.sourceArchiveDir); |
| 51 | + llvm::sys::path::append(dstFilePath, srcFilePath); |
| 52 | + |
| 53 | + llvm::StringRef parent = llvm::sys::path::parent_path(dstFilePath); |
| 54 | + if (std::error_code ec = llvm::sys::fs::create_directories(parent)) { |
| 55 | + std::cerr << "Cannot create source archive destination directory '" << parent.str() |
| 56 | + << "': " << ec.message() << "\n"; |
| 57 | + return; |
| 58 | + } |
| 59 | + |
| 60 | + if (std::error_code ec = llvm::sys::fs::copy_file(srcFilePath, dstFilePath)) { |
| 61 | + std::cerr << "Cannot archive source file '" << srcFilePath.str().str() << "' -> '" |
| 62 | + << dstFilePath.str().str() << "': " << ec.message() << "\n"; |
| 63 | + return; |
| 64 | + } |
| 65 | + |
| 66 | + /// TODO: find a more robust approach to avoid collisions? |
| 67 | + std::string tempTrapName = file.getFilename().str() + '.' + std::to_string(getpid()) + ".trap"; |
| 68 | + llvm::SmallString<PATH_MAX> tempTrapPath(config.trapDir); |
| 69 | + llvm::sys::path::append(tempTrapPath, tempTrapName); |
| 70 | + |
| 71 | + std::ofstream trap(tempTrapPath.str().str()); |
| 72 | + if (!trap) { |
| 73 | + std::error_code ec; |
| 74 | + ec.assign(errno, std::generic_category()); |
| 75 | + std::cerr << "Cannot create temp trap file '" << tempTrapPath.str().str() |
| 76 | + << "': " << ec.message() << "\n"; |
| 77 | + return; |
| 78 | + } |
| 79 | + std::stringstream ss; |
| 80 | + ss << "-frontend "; |
| 81 | + for (auto opt : config.frontendOptions) { |
| 82 | + ss << std::quoted(opt) << " "; |
| 83 | + } |
| 84 | + ss << "\n"; |
| 85 | + trap << "// frontend-options: " << ss.str(); |
| 86 | + |
| 87 | + trap << "#0=*\n"; |
| 88 | + trap << "files(#0, " << std::quoted(srcFilePath.str().str()) << ")\n"; |
| 89 | + |
| 90 | + /// TODO: Pick a better name to avoid collisions |
| 91 | + std::string trapName = file.getFilename().str() + ".trap"; |
| 92 | + llvm::SmallString<PATH_MAX> trapPath(config.trapDir); |
| 93 | + llvm::sys::path::append(trapPath, trapName); |
| 94 | + |
| 95 | + /// TODO: The last process wins. Should we do better than that? |
| 96 | + if (std::error_code ec = llvm::sys::fs::rename(tempTrapPath, trapPath)) { |
| 97 | + std::cerr << "Cannot rename temp trap file '" << tempTrapPath.str().str() << "' -> '" |
| 98 | + << trapPath.str().str() << "': " << ec.message() << "\n"; |
| 99 | + } |
| 100 | +} |
| 101 | + |
0 commit comments