Swift: Replace SwiftExtractor class with a function

This commit is contained in:
Alex Denisov
2022-04-26 13:32:14 +02:00
parent ebd2ff4fc0
commit e2332fc5ec
3 changed files with 24 additions and 40 deletions

View File

@@ -14,29 +14,7 @@
using namespace codeql;
SwiftExtractor::SwiftExtractor(const SwiftExtractorConfiguration& config,
swift::CompilerInstance& instance)
: config{config}, compiler{instance} {}
void SwiftExtractor::extract() {
// Swift frontend can be called in several different modes, we are interested
// only in the cases when either a primary or a main source file is present
if (compiler.getPrimarySourceFiles().empty()) {
swift::ModuleDecl* module = compiler.getMainModule();
if (!module->getFiles().empty() &&
module->getFiles().front()->getKind() == swift::FileUnitKind::Source) {
// We can only call getMainSourceFile if the first file is of a Source kind
swift::SourceFile& file = module->getMainSourceFile();
extractFile(file);
}
} else {
for (auto s : compiler.getPrimarySourceFiles()) {
extractFile(*s);
}
}
}
void SwiftExtractor::extractFile(swift::SourceFile& file) {
static void extractFile(const SwiftExtractorConfiguration& config, swift::SourceFile& file) {
if (std::error_code ec = llvm::sys::fs::create_directories(config.trapDir)) {
std::cerr << "Cannot create TRAP directory: " << ec.message() << "\n";
return;
@@ -103,3 +81,22 @@ void SwiftExtractor::extractFile(swift::SourceFile& file) {
<< trapPath.str().str() << "': " << ec.message() << "\n";
}
}
void codeql::extractSwiftFiles(const SwiftExtractorConfiguration& config,
swift::CompilerInstance& compiler) {
// Swift frontend can be called in several different modes, we are interested
// only in the cases when either a primary or a main source file is present
if (compiler.getPrimarySourceFiles().empty()) {
swift::ModuleDecl* module = compiler.getMainModule();
if (!module->getFiles().empty() &&
module->getFiles().front()->getKind() == swift::FileUnitKind::Source) {
// We can only call getMainSourceFile if the first file is of a Source kind
swift::SourceFile& file = module->getMainSourceFile();
extractFile(config, file);
}
} else {
for (auto s : compiler.getPrimarySourceFiles()) {
extractFile(config, *s);
}
}
}

View File

@@ -7,20 +7,8 @@
#include <memory>
namespace codeql {
// TODO: add documentation for the class and its public methods
class SwiftExtractor {
public:
explicit SwiftExtractor(const SwiftExtractorConfiguration& config,
swift::CompilerInstance& instance);
void extract();
private:
void extractFile(swift::SourceFile& file);
const SwiftExtractorConfiguration& config;
swift::CompilerInstance& compiler;
};
void extractSwiftFiles(const SwiftExtractorConfiguration& config,
swift::CompilerInstance& compiler);
} // namespace codeql
#endif // SWIFT_EXTRACTOR_H_

View File

@@ -13,9 +13,8 @@ class Observer : public swift::FrontendObserver {
public:
explicit Observer(const codeql::SwiftExtractorConfiguration& config) : config{config} {}
void performedSemanticAnalysis(swift::CompilerInstance& instance) override {
codeql::SwiftExtractor extractor(config, instance);
extractor.extract();
void performedSemanticAnalysis(swift::CompilerInstance& compiler) override {
codeql::extractSwiftFiles(config, compiler);
}
private: