Skip to content

Commit e2332fc

Browse files
committed
Swift: Replace SwiftExtractor class with a function
1 parent ebd2ff4 commit e2332fc

File tree

3 files changed

+24
-40
lines changed

3 files changed

+24
-40
lines changed

swift/extractor/SwiftExtractor.cpp

Lines changed: 20 additions & 23 deletions
Original file line numberDiff line numberDiff line change
@@ -14,29 +14,7 @@
1414

1515
using namespace codeql;
1616

17-
SwiftExtractor::SwiftExtractor(const SwiftExtractorConfiguration& config,
18-
swift::CompilerInstance& instance)
19-
: config{config}, compiler{instance} {}
20-
21-
void SwiftExtractor::extract() {
22-
// Swift frontend can be called in several different modes, we are interested
23-
// only in the cases when either a primary or a main source file is present
24-
if (compiler.getPrimarySourceFiles().empty()) {
25-
swift::ModuleDecl* module = compiler.getMainModule();
26-
if (!module->getFiles().empty() &&
27-
module->getFiles().front()->getKind() == swift::FileUnitKind::Source) {
28-
// We can only call getMainSourceFile if the first file is of a Source kind
29-
swift::SourceFile& file = module->getMainSourceFile();
30-
extractFile(file);
31-
}
32-
} else {
33-
for (auto s : compiler.getPrimarySourceFiles()) {
34-
extractFile(*s);
35-
}
36-
}
37-
}
38-
39-
void SwiftExtractor::extractFile(swift::SourceFile& file) {
17+
static void extractFile(const SwiftExtractorConfiguration& config, swift::SourceFile& file) {
4018
if (std::error_code ec = llvm::sys::fs::create_directories(config.trapDir)) {
4119
std::cerr << "Cannot create TRAP directory: " << ec.message() << "\n";
4220
return;
@@ -103,3 +81,22 @@ void SwiftExtractor::extractFile(swift::SourceFile& file) {
10381
<< trapPath.str().str() << "': " << ec.message() << "\n";
10482
}
10583
}
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+
}

swift/extractor/SwiftExtractor.h

Lines changed: 2 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -7,20 +7,8 @@
77
#include <memory>
88

99
namespace codeql {
10-
11-
// TODO: add documentation for the class and its public methods
12-
class SwiftExtractor {
13-
public:
14-
explicit SwiftExtractor(const SwiftExtractorConfiguration& config,
15-
swift::CompilerInstance& instance);
16-
void extract();
17-
18-
private:
19-
void extractFile(swift::SourceFile& file);
20-
21-
const SwiftExtractorConfiguration& config;
22-
swift::CompilerInstance& compiler;
23-
};
10+
void extractSwiftFiles(const SwiftExtractorConfiguration& config,
11+
swift::CompilerInstance& compiler);
2412
} // namespace codeql
2513

2614
#endif // SWIFT_EXTRACTOR_H_

swift/extractor/main.cpp

Lines changed: 2 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -13,9 +13,8 @@ class Observer : public swift::FrontendObserver {
1313
public:
1414
explicit Observer(const codeql::SwiftExtractorConfiguration& config) : config{config} {}
1515

16-
void performedSemanticAnalysis(swift::CompilerInstance& instance) override {
17-
codeql::SwiftExtractor extractor(config, instance);
18-
extractor.extract();
16+
void performedSemanticAnalysis(swift::CompilerInstance& compiler) override {
17+
codeql::extractSwiftFiles(config, compiler);
1918
}
2019

2120
private:

0 commit comments

Comments
 (0)