|
| 1 | +#include "SwiftExtractor.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 | +static void extractFile(const SwiftExtractorConfiguration& config, swift::SourceFile& file) { |
| 18 | + if (std::error_code ec = llvm::sys::fs::create_directories(config.trapDir)) { |
| 19 | + std::cerr << "Cannot create TRAP directory: " << ec.message() << "\n"; |
| 20 | + return; |
| 21 | + } |
| 22 | + |
| 23 | + if (std::error_code ec = llvm::sys::fs::create_directories(config.sourceArchiveDir)) { |
| 24 | + std::cerr << "Cannot create source archive directory: " << ec.message() << "\n"; |
| 25 | + return; |
| 26 | + } |
| 27 | + |
| 28 | + llvm::SmallString<PATH_MAX> srcFilePath(file.getFilename()); |
| 29 | + llvm::sys::fs::make_absolute(srcFilePath); |
| 30 | + |
| 31 | + llvm::SmallString<PATH_MAX> dstFilePath(config.sourceArchiveDir); |
| 32 | + llvm::sys::path::append(dstFilePath, srcFilePath); |
| 33 | + |
| 34 | + llvm::StringRef parent = llvm::sys::path::parent_path(dstFilePath); |
| 35 | + if (std::error_code ec = llvm::sys::fs::create_directories(parent)) { |
| 36 | + std::cerr << "Cannot create source archive destination directory '" << parent.str() |
| 37 | + << "': " << ec.message() << "\n"; |
| 38 | + return; |
| 39 | + } |
| 40 | + |
| 41 | + if (std::error_code ec = llvm::sys::fs::copy_file(srcFilePath, dstFilePath)) { |
| 42 | + std::cerr << "Cannot archive source file '" << srcFilePath.str().str() << "' -> '" |
| 43 | + << dstFilePath.str().str() << "': " << ec.message() << "\n"; |
| 44 | + return; |
| 45 | + } |
| 46 | + |
| 47 | + // The extractor can be called several times from different processes with |
| 48 | + // the same input file(s) |
| 49 | + // We are using PID to avoid concurrent access |
| 50 | + // TODO: find a more robust approach to avoid collisions? |
| 51 | + std::string tempTrapName = file.getFilename().str() + '.' + std::to_string(getpid()) + ".trap"; |
| 52 | + llvm::SmallString<PATH_MAX> tempTrapPath(config.trapDir); |
| 53 | + llvm::sys::path::append(tempTrapPath, tempTrapName); |
| 54 | + |
| 55 | + std::ofstream trap(tempTrapPath.str().str()); |
| 56 | + if (!trap) { |
| 57 | + std::error_code ec; |
| 58 | + ec.assign(errno, std::generic_category()); |
| 59 | + std::cerr << "Cannot create temp trap file '" << tempTrapPath.str().str() |
| 60 | + << "': " << ec.message() << "\n"; |
| 61 | + return; |
| 62 | + } |
| 63 | + std::stringstream ss; |
| 64 | + for (auto opt : config.frontendOptions) { |
| 65 | + ss << std::quoted(opt) << " "; |
| 66 | + } |
| 67 | + ss << "\n"; |
| 68 | + trap << "// extractor-args: " << ss.str(); |
| 69 | + |
| 70 | + trap << "#0=*\n"; |
| 71 | + trap << "files(#0, " << std::quoted(srcFilePath.str().str()) << ")\n"; |
| 72 | + |
| 73 | + // TODO: Pick a better name to avoid collisions |
| 74 | + std::string trapName = file.getFilename().str() + ".trap"; |
| 75 | + llvm::SmallString<PATH_MAX> trapPath(config.trapDir); |
| 76 | + llvm::sys::path::append(trapPath, trapName); |
| 77 | + |
| 78 | + // TODO: The last process wins. Should we do better than that? |
| 79 | + if (std::error_code ec = llvm::sys::fs::rename(tempTrapPath, trapPath)) { |
| 80 | + std::cerr << "Cannot rename temp trap file '" << tempTrapPath.str().str() << "' -> '" |
| 81 | + << trapPath.str().str() << "': " << ec.message() << "\n"; |
| 82 | + } |
| 83 | +} |
| 84 | + |
| 85 | +void codeql::extractSwiftFiles(const SwiftExtractorConfiguration& config, |
| 86 | + swift::CompilerInstance& compiler) { |
| 87 | + // Swift frontend can be called in several different modes, we are interested |
| 88 | + // only in the cases when either a primary or a main source file is present |
| 89 | + if (compiler.getPrimarySourceFiles().empty()) { |
| 90 | + swift::ModuleDecl* module = compiler.getMainModule(); |
| 91 | + if (!module->getFiles().empty() && |
| 92 | + module->getFiles().front()->getKind() == swift::FileUnitKind::Source) { |
| 93 | + // We can only call getMainSourceFile if the first file is of a Source kind |
| 94 | + swift::SourceFile& file = module->getMainSourceFile(); |
| 95 | + extractFile(config, file); |
| 96 | + } |
| 97 | + } else { |
| 98 | + for (auto s : compiler.getPrimarySourceFiles()) { |
| 99 | + extractFile(config, *s); |
| 100 | + } |
| 101 | + } |
| 102 | +} |
0 commit comments