Skip to content

Commit 6419af3

Browse files
committed
Swift: extract only primary files
While the (removed) comment is correct and the frontend can be called in different modes, both `swift build` and `xcodebuild` always use `-primary-files` when compiling Swift projects. The other mode was present only within our test runner (`qltest.sh`), so removing it and doing what the official build systems do simplifies our code base. Additionally, file archival is now a separate function/operation.
1 parent 4b2b6fa commit 6419af3

File tree

2 files changed

+16
-22
lines changed

2 files changed

+16
-22
lines changed

swift/extractor/SwiftExtractor.cpp

Lines changed: 15 additions & 21 deletions
Original file line numberDiff line numberDiff line change
@@ -18,9 +18,7 @@
1818

1919
using namespace codeql;
2020

21-
static void extractFile(const SwiftExtractorConfiguration& config,
22-
swift::CompilerInstance& compiler,
23-
swift::SourceFile& file) {
21+
static void archiveFile(const SwiftExtractorConfiguration& config, swift::SourceFile* file) {
2422
if (std::error_code ec = llvm::sys::fs::create_directories(config.trapDir)) {
2523
std::cerr << "Cannot create TRAP directory: " << ec.message() << "\n";
2624
return;
@@ -31,7 +29,7 @@ static void extractFile(const SwiftExtractorConfiguration& config,
3129
return;
3230
}
3331

34-
llvm::SmallString<PATH_MAX> srcFilePath(file.getFilename());
32+
llvm::SmallString<PATH_MAX> srcFilePath(file->getFilename());
3533
llvm::sys::fs::make_absolute(srcFilePath);
3634

3735
llvm::SmallString<PATH_MAX> dstFilePath(config.sourceArchiveDir);
@@ -49,12 +47,16 @@ static void extractFile(const SwiftExtractorConfiguration& config,
4947
<< dstFilePath.str().str() << "': " << ec.message() << "\n";
5048
return;
5149
}
50+
}
5251

52+
static void extractFile(const SwiftExtractorConfiguration& config,
53+
swift::CompilerInstance& compiler,
54+
swift::SourceFile* file) {
5355
// The extractor can be called several times from different processes with
5456
// the same input file(s)
5557
// We are using PID to avoid concurrent access
5658
// TODO: find a more robust approach to avoid collisions?
57-
std::string tempTrapName = file.getFilename().str() + '.' + std::to_string(getpid()) + ".trap";
59+
std::string tempTrapName = file->getFilename().str() + '.' + std::to_string(getpid()) + ".trap";
5860
llvm::SmallString<PATH_MAX> tempTrapPath(config.trapDir);
5961
llvm::sys::path::append(tempTrapPath, tempTrapName);
6062

@@ -84,17 +86,20 @@ static void extractFile(const SwiftExtractorConfiguration& config,
8486

8587
// In the case of emtpy files, the dispatcher is not called, but we still want to 'record' the
8688
// fact that the file was extracted
89+
// TODO: to be moved elsewhere
90+
llvm::SmallString<PATH_MAX> srcFilePath(file->getFilename());
91+
llvm::sys::fs::make_absolute(srcFilePath);
8792
auto fileLabel = arena.allocateLabel<FileTag>();
8893
trap.assignKey(fileLabel, srcFilePath.str().str());
8994
trap.emit(FilesTrap{fileLabel, srcFilePath.str().str()});
9095

9196
SwiftVisitor visitor(compiler.getSourceMgr(), arena, trap);
92-
for (swift::Decl* decl : file.getTopLevelDecls()) {
97+
for (swift::Decl* decl : file->getTopLevelDecls()) {
9398
visitor.extract(decl);
9499
}
95100

96101
// TODO: Pick a better name to avoid collisions
97-
std::string trapName = file.getFilename().str() + ".trap";
102+
std::string trapName = file->getFilename().str() + ".trap";
98103
llvm::SmallString<PATH_MAX> trapPath(config.trapDir);
99104
llvm::sys::path::append(trapPath, trapName);
100105

@@ -107,19 +112,8 @@ static void extractFile(const SwiftExtractorConfiguration& config,
107112

108113
void codeql::extractSwiftFiles(const SwiftExtractorConfiguration& config,
109114
swift::CompilerInstance& compiler) {
110-
// Swift frontend can be called in several different modes, we are interested
111-
// only in the cases when either a primary or a main source file is present
112-
if (compiler.getPrimarySourceFiles().empty()) {
113-
swift::ModuleDecl* module = compiler.getMainModule();
114-
if (!module->getFiles().empty() &&
115-
module->getFiles().front()->getKind() == swift::FileUnitKind::Source) {
116-
// We can only call getMainSourceFile if the first file is of a Source kind
117-
swift::SourceFile& file = module->getMainSourceFile();
118-
extractFile(config, compiler, file);
119-
}
120-
} else {
121-
for (auto s : compiler.getPrimarySourceFiles()) {
122-
extractFile(config, compiler, *s);
123-
}
115+
for (auto s : compiler.getPrimarySourceFiles()) {
116+
archiveFile(config, s);
117+
extractFile(config, compiler, s);
124118
}
125119
}

swift/tools/qltest.sh

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -7,5 +7,5 @@ QLTEST_LOG="$CODEQL_EXTRACTOR_SWIFT_LOG_DIR"/qltest.log
77
export LD_LIBRARY_PATH="$CODEQL_EXTRACTOR_SWIFT_ROOT/tools/$CODEQL_PLATFORM"
88

99
for src in *.swift; do
10-
"$CODEQL_EXTRACTOR_SWIFT_ROOT/tools/$CODEQL_PLATFORM/extractor" -sdk "$CODEQL_EXTRACTOR_SWIFT_ROOT/qltest/$CODEQL_PLATFORM/sdk" -c $src >> $QLTEST_LOG 2>&1
10+
"$CODEQL_EXTRACTOR_SWIFT_ROOT/tools/$CODEQL_PLATFORM/extractor" -sdk "$CODEQL_EXTRACTOR_SWIFT_ROOT/qltest/$CODEQL_PLATFORM/sdk" -c -primary-file $src >> $QLTEST_LOG 2>&1
1111
done

0 commit comments

Comments
 (0)