Skip to content

Commit 80ffd81

Browse files
authored
Merge pull request #9614 from github/alexdenisov/swift-extract-all-inputs-with-outputs
Swift: extract all output-producing source files, not only primary files
2 parents d0e521e + decb136 commit 80ffd81

File tree

12 files changed

+46
-6
lines changed

12 files changed

+46
-6
lines changed

swift/extractor/SwiftExtractor.cpp

Lines changed: 20 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -6,8 +6,10 @@
66
#include <sstream>
77
#include <memory>
88
#include <unistd.h>
9+
#include <unordered_set>
910

1011
#include <swift/AST/SourceFile.h>
12+
#include <swift/Basic/FileTypes.h>
1113
#include <llvm/ADT/SmallString.h>
1214
#include <llvm/Support/FileSystem.h>
1315
#include <llvm/Support/Path.h>
@@ -124,6 +126,17 @@ static void extractDeclarations(const SwiftExtractorConfiguration& config,
124126

125127
void codeql::extractSwiftFiles(const SwiftExtractorConfiguration& config,
126128
swift::CompilerInstance& compiler) {
129+
// The frontend can be called in many different ways.
130+
// At each invocation we only extract system and builtin modules and any input source files that
131+
// have an output associated with them.
132+
std::unordered_set<std::string> sourceFiles;
133+
auto inputFiles = compiler.getInvocation().getFrontendOptions().InputsAndOutputs.getAllInputs();
134+
for (auto& input : inputFiles) {
135+
if (input.getType() == swift::file_types::TY_Swift && !input.outputFilename().empty()) {
136+
sourceFiles.insert(input.getFileName());
137+
}
138+
}
139+
127140
for (auto& [_, module] : compiler.getASTContext().getLoadedModules()) {
128141
// We only extract system and builtin modules here as the other "user" modules can be built
129142
// during the build process and then re-used at a later stage. In this case, we extract the
@@ -135,12 +148,13 @@ void codeql::extractSwiftFiles(const SwiftExtractorConfiguration& config,
135148
// TODO: pass ModuleDecl directly when we have module extraction in place?
136149
extractDeclarations(config, decls, compiler, *module);
137150
} else {
138-
// The extraction will only work if one (or more) `-primary-file` CLI option is provided,
139-
// which is what always happens in case of `swift build` and `xcodebuild`
140-
for (auto primaryFile : module->getPrimarySourceFiles()) {
141-
archiveFile(config, *primaryFile);
142-
extractDeclarations(config, primaryFile->getTopLevelDecls(), compiler, *module,
143-
primaryFile);
151+
for (auto file : module->getFiles()) {
152+
auto sourceFile = llvm::dyn_cast<swift::SourceFile>(file);
153+
if (!sourceFile || sourceFiles.count(sourceFile->getFilename().str()) == 0) {
154+
continue;
155+
}
156+
archiveFile(config, *sourceFile);
157+
extractDeclarations(config, sourceFile->getTopLevelDecls(), compiler, *module, sourceFile);
144158
}
145159
}
146160
}

swift/integration-tests/frontend-invocations/A.swift

Whitespace-only changes.

swift/integration-tests/frontend-invocations/B.swift

Whitespace-only changes.

swift/integration-tests/frontend-invocations/C.swift

Whitespace-only changes.

swift/integration-tests/frontend-invocations/D.swift

Whitespace-only changes.

swift/integration-tests/frontend-invocations/E.swift

Whitespace-only changes.

swift/integration-tests/frontend-invocations/Esup.swift

Whitespace-only changes.
Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,6 @@
1+
| A.swift:0:0:0:0 | A.swift |
2+
| B.swift:0:0:0:0 | B.swift |
3+
| C.swift:0:0:0:0 | C.swift |
4+
| D.swift:0:0:0:0 | D.swift |
5+
| E.swift:0:0:0:0 | E.swift |
6+
| file://:0:0:0:0 | |
Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,4 @@
1+
import swift
2+
3+
from File f
4+
select f
Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,10 @@
1+
# TODO: Add linux
2+
SDK=$(shell xcrun -show-sdk-path)
3+
FRONTEND=$(shell xcrun -find swift-frontend)
4+
5+
all:
6+
$(FRONTEND) -frontend -c A.swift -sdk $(SDK)
7+
$(FRONTEND) -frontend -c B.swift -o B.o -sdk $(SDK)
8+
$(FRONTEND) -frontend -c -primary-file C.swift -sdk $(SDK)
9+
$(FRONTEND) -frontend -c -primary-file D.swift -o D.o -sdk $(SDK)
10+
$(FRONTEND) -frontend -c -primary-file E.swift Esup.swift -o E.o -sdk $(SDK)
Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,5 @@
1+
from create_database_utils import *
2+
3+
run_codeql_database_create([
4+
'make',
5+
], lang='swift')
Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,2 +1,3 @@
1+
| Package.swift:0:0:0:0 | Package.swift |
12
| Sources/hello-world/hello_world.swift:0:0:0:0 | Sources/hello-world/hello_world.swift |
23
| file://:0:0:0:0 | |

0 commit comments

Comments
 (0)