Skip to content

Commit f676973

Browse files
authored
Merge pull request #8939 from AlexDenisov/alexdenisov/swift-tracer-integration
Swift: tracer integration
2 parents 3fd93b4 + a59d7f6 commit f676973

File tree

6 files changed

+60
-2
lines changed

6 files changed

+60
-2
lines changed

.bazelrc

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,3 @@
1-
# -fno-rtti is required by LLVM/Swift
2-
build --repo_env=CC=clang --repo_env=CXX=clang++ --copt="-std=c++17" --copt="-fno-rtti"
1+
build --repo_env=CC=clang --repo_env=CXX=clang++ --copt="-std=c++17"
32

43
try-import %workspace%/local.bazelrc

swift/BUILD.bazel

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -23,6 +23,12 @@ pkg_files(
2323
prefix = "tools",
2424
)
2525

26+
pkg_files(
27+
name = "tracing-config",
28+
srcs = ["tools/tracing-config.lua"],
29+
prefix = "tools",
30+
)
31+
2632
pkg_files(
2733
name = "manifest",
2834
srcs = ["codeql-extractor.yml"],
@@ -34,6 +40,7 @@ pkg_filegroup(
3440
":dbscheme_files",
3541
":manifest",
3642
":qltest",
43+
":tracing-config",
3744
],
3845
visibility = ["//visibility:public"],
3946
)

swift/extractor/BUILD.bazel

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -14,6 +14,10 @@ cc_binary(
1414
"SwiftExtractorConfiguration.h",
1515
"main.cpp",
1616
],
17+
copts = [
18+
# Required by LLVM/Swift
19+
"-fno-rtti",
20+
],
1721
features = ["-universal_binaries"],
1822
target_compatible_with = select({
1923
"@platforms//os:linux": [],

swift/extractor/SwiftExtractor.cpp

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -54,6 +54,13 @@ static void extractFile(const SwiftExtractorConfiguration& config, swift::Source
5454
llvm::SmallString<PATH_MAX> tempTrapPath(config.trapDir);
5555
llvm::sys::path::append(tempTrapPath, tempTrapName);
5656

57+
llvm::StringRef trapParent = llvm::sys::path::parent_path(tempTrapPath);
58+
if (std::error_code ec = llvm::sys::fs::create_directories(trapParent)) {
59+
std::cerr << "Cannot create trap directory '" << trapParent.str() << "': " << ec.message()
60+
<< "\n";
61+
return;
62+
}
63+
5764
std::ofstream trap(tempTrapPath.str().str());
5865
if (!trap) {
5966
std::error_code ec;

swift/extractor/main.cpp

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -16,6 +16,14 @@ class Observer : public swift::FrontendObserver {
1616
public:
1717
explicit Observer(const codeql::SwiftExtractorConfiguration& config) : config{config} {}
1818

19+
void parsedArgs(swift::CompilerInvocation& invocation) override {
20+
// Original compiler and the extractor-compiler get into conflicts when
21+
// both produce the same output files.
22+
// TODO: change the final artifact destinations instead of disabling
23+
// the artifact generation completely?
24+
invocation.getFrontendOptions().RequestedAction = swift::FrontendOptions::ActionType::Typecheck;
25+
}
26+
1927
void performedSemanticAnalysis(swift::CompilerInstance& compiler) override {
2028
codeql::extractSwiftFiles(config, compiler);
2129
}

swift/tools/tracing-config.lua

Lines changed: 33 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,33 @@
1+
function RegisterExtractorPack(id)
2+
local extractorDirectory = GetPlatformToolsDirectory()
3+
local relativeSwiftExtractor = extractorDirectory .. 'extractor'
4+
local swiftExtractor = AbsolutifyExtractorPath(id, relativeSwiftExtractor)
5+
6+
function SwiftMatcher(compilerName, compilerPath, compilerArguments, lang)
7+
-- Only match binaries names `swift-frontend`
8+
if compilerName ~= 'swift-frontend' then return nil end
9+
-- Skip the invocation in case it's not called in `-frontend` mode
10+
if compilerArguments.argv[1] ~= '-frontend' then return nil end
11+
12+
-- Drop the `-frontend` argument
13+
table.remove(compilerArguments.argv, 1)
14+
15+
-- Skip "info" queries in case there is nothing to extract
16+
if compilerArguments.argv[1] == '-print-target-info' then return nil end
17+
if compilerArguments.argv[1] == '-emit-supported-features' then return nil end
18+
19+
-- Skip actions in which we cannot extract anything
20+
if compilerArguments.argv[1] == '-merge-modules' then return nil end
21+
22+
return {
23+
trace = true,
24+
replace = false,
25+
invocations = {{path = swiftExtractor, arguments = compilerArguments}}
26+
}
27+
end
28+
return { SwiftMatcher }
29+
end
30+
31+
-- Return a list of minimum supported versions of the configuration file format
32+
-- return one entry per supported major version.
33+
function GetCompatibleVersions() return {'1.0.0'} end

0 commit comments

Comments
 (0)