mirror of
https://github.com/github/codeql.git
synced 2026-05-04 21:25:44 +02:00
Swift: Replace SwiftExtractor class with a function
This commit is contained in:
@@ -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);
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
@@ -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_
|
||||
|
||||
@@ -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:
|
||||
|
||||
Reference in New Issue
Block a user