Skip to content

Commit 03ebf8b

Browse files
authored
Merge pull request github#8700 from redsun82/swift-skeleton
Swift: first skeleton extractor
2 parents a43f3a2 + 8ef2878 commit 03ebf8b

25 files changed

+222
-1
lines changed

.bazelrc

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,3 @@
1+
build --copt="-std=c++17"
2+
3+
try-import %workspace%/local.bazelrc

.bazelversion

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
5.0.0

.gitignore

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -31,5 +31,8 @@ csharp/extractor/Semmle.Extraction.CSharp.Driver/Properties/launchSettings.json
3131
# Compiled class file
3232
*.class
3333

34-
# links create by bazel
34+
# links created by bazel
3535
/bazel-*
36+
37+
# CLion project files
38+
/.clwb

BUILD.bazel

Whitespace-only changes.

WORKSPACE.bazel

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,2 +1,12 @@
11
# Please notice that any bazel targets and definitions in this repository are currently experimental
22
# and for internal use only.
3+
4+
workspace(name = "ql")
5+
6+
load("//misc/bazel:workspace.bzl", "ql_workspace")
7+
8+
ql_workspace()
9+
10+
load("//misc/bazel:workspace_deps.bzl", "ql_workspace_deps")
11+
12+
ql_workspace_deps()

defs.bzl

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,5 @@
1+
codeql_platform = select({
2+
"@platforms//os:linux": "linux64",
3+
"@platforms//os:macos": "osx64",
4+
"@platforms//os:windows": "win64",
5+
})

misc/bazel/BUILD.bazel

Whitespace-only changes.

misc/bazel/workspace.bzl

Lines changed: 23 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,23 @@
1+
load("@bazel_tools//tools/build_defs/repo:http.bzl", "http_archive")
2+
load("@bazel_tools//tools/build_defs/repo:utils.bzl", "maybe")
3+
4+
def ql_workspace():
5+
maybe(
6+
repo_rule = http_archive,
7+
name = "rules_pkg",
8+
sha256 = "62eeb544ff1ef41d786e329e1536c1d541bb9bcad27ae984d57f18f314018e66",
9+
urls = [
10+
"https://mirror.bazel.build/github.com/bazelbuild/rules_pkg/releases/download/0.6.0/rules_pkg-0.6.0.tar.gz",
11+
"https://github.com/bazelbuild/rules_pkg/releases/download/0.6.0/rules_pkg-0.6.0.tar.gz",
12+
],
13+
)
14+
15+
maybe(
16+
repo_rule = http_archive,
17+
name = "platforms",
18+
sha256 = "460caee0fa583b908c622913334ec3c1b842572b9c23cf0d3da0c2543a1a157d",
19+
urls = [
20+
"https://mirror.bazel.build/github.com/bazelbuild/platforms/releases/download/0.0.3/platforms-0.0.3.tar.gz",
21+
"https://github.com/bazelbuild/platforms/releases/download/0.0.3/platforms-0.0.3.tar.gz",
22+
],
23+
)

misc/bazel/workspace_deps.bzl

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,4 @@
1+
load("@rules_pkg//:deps.bzl", "rules_pkg_dependencies")
2+
3+
def ql_workspace_deps():
4+
rules_pkg_dependencies()

swift/.clang-format

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,7 @@
1+
BasedOnStyle: Chromium
2+
ColumnLimit: 100
3+
IndentWidth: 2
4+
SortIncludes: false
5+
AllowShortIfStatementsOnASingleLine: WithoutElse
6+
AlwaysBreakBeforeMultilineStrings: false
7+
Standard: c++17

swift/.codeqlmanifest.json

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,7 @@
1+
{
2+
"provide": [
3+
"ql/lib/qlpack.yml",
4+
"ql/test/qlpack.yml",
5+
"extractor-pack/codeql-extractor.yml"
6+
]
7+
}

swift/.gitignore

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
extractor-pack

swift/BUILD.bazel

Lines changed: 67 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,67 @@
1+
load("@rules_pkg//:mappings.bzl", "pkg_attributes", "pkg_filegroup", "pkg_files")
2+
load("@rules_pkg//:install.bzl", "pkg_install")
3+
load("//:defs.bzl", "codeql_platform")
4+
5+
pkg_files(
6+
name = "dbscheme",
7+
srcs = [
8+
"ql/lib/swift.dbscheme",
9+
"ql/lib/swift.dbscheme.stats",
10+
],
11+
)
12+
13+
pkg_files(
14+
name = "qltest",
15+
srcs = ["tools/qltest.sh"],
16+
attributes = pkg_attributes(mode = "0755"),
17+
prefix = "tools",
18+
)
19+
20+
pkg_files(
21+
name = "manifest",
22+
srcs = ["codeql-extractor.yml"],
23+
)
24+
25+
pkg_filegroup(
26+
name = "extractor-pack-generic",
27+
srcs = [
28+
":dbscheme",
29+
":manifest",
30+
":qltest",
31+
],
32+
visibility = ["//visibility:public"],
33+
)
34+
35+
pkg_files(
36+
name = "extractor",
37+
srcs = ["//swift/extractor"],
38+
attributes = pkg_attributes(mode = "0755"),
39+
prefix = "tools/" + codeql_platform,
40+
)
41+
42+
pkg_filegroup(
43+
name = "extractor-pack-arch",
44+
srcs = [":extractor"],
45+
visibility = ["//visibility:public"],
46+
)
47+
48+
pkg_filegroup(
49+
name = "extractor-pack",
50+
srcs = [
51+
":extractor-pack-arch",
52+
":extractor-pack-generic",
53+
],
54+
visibility = ["//visibility:public"],
55+
)
56+
57+
pkg_install(
58+
name = "_create_extractor_pack",
59+
srcs = ["//swift:extractor-pack"],
60+
)
61+
62+
py_binary(
63+
name = "create-extractor-pack",
64+
srcs = ["tools/create_extractor_pack.py"],
65+
main = "tools/create_extractor_pack.py",
66+
deps = [":_create_extractor_pack"],
67+
)

swift/README.md

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,9 @@
1+
## Warning
2+
3+
The Swift codeql package is an experimental and unsupported work in progress.
4+
5+
## Usage
6+
7+
Run `bazel run //swift:create-extractor-pack`, which will install `swift/extractor-pack`.
8+
Using `--search-path=swift/extractor-pack` will then pick up the Swift extractor. You can also use
9+
`--search-path=swift`, as the extractor pack is mentioned in `swift/.codeqlmanifest.json`.

swift/codeql-extractor.yml

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,10 @@
1+
name: "swift"
2+
display_name: "Swift"
3+
version: 0.0.1
4+
column_kind: "utf8"
5+
legacy_qltest_extraction: true
6+
file_types:
7+
- name: swift
8+
display_name: Swift files
9+
extensions:
10+
- .swift

swift/extractor/BUILD.bazel

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,10 @@
1+
cc_binary(
2+
name = "extractor",
3+
srcs = ["main.cpp"],
4+
target_compatible_with = select({
5+
"@platforms//os:linux": [],
6+
"@platforms//os:macos": [],
7+
"//conditions:default": ["@platforms//:incompatible"],
8+
}),
9+
visibility = ["//swift:__pkg__"],
10+
)

swift/extractor/main.cpp

Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,14 @@
1+
#include <fstream>
2+
#include <iomanip>
3+
#include <stdlib.h>
4+
5+
int main() {
6+
if (auto trapDir = getenv("CODEQL_EXTRACTOR_SWIFT_TRAP_DIR")) {
7+
std::string file = trapDir;
8+
file += "/my_first.trap";
9+
if (std::ofstream out{file}) {
10+
out << "answer_to_life_the_universe_and_everything(42)\n";
11+
}
12+
}
13+
return 0;
14+
}

swift/ql/lib/qlpack.yml

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,5 @@
1+
name: codeql/swift-all
2+
version: 0.0.0
3+
dbscheme: swift.dbscheme
4+
extractor: swift
5+
library: true

swift/ql/lib/swift.dbscheme

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,7 @@
1+
sourceLocationPrefix(
2+
string prefix: string ref
3+
);
4+
5+
answer_to_life_the_universe_and_everything(
6+
int answer: int ref
7+
)

swift/ql/lib/swift.dbscheme.stats

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,4 @@
1+
<dbstats>
2+
<typesizes />
3+
<stats />
4+
</dbstats>

swift/ql/test/answer.expected

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
| 42 |

swift/ql/test/answer.ql

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,3 @@
1+
from int answer
2+
where answer_to_life_the_universe_and_everything(answer)
3+
select answer

swift/ql/test/qlpack.yml

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,6 @@
1+
name: codeql-swift-tests
2+
version: 0.0.0
3+
dependencies:
4+
codeql/swift-all: "*"
5+
tests: .
6+
extractor: swift

swift/tools/create_extractor_pack.py

Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,16 @@
1+
import os
2+
import pathlib
3+
import shutil
4+
import sys
5+
from swift._create_extractor_pack_install_script import main
6+
7+
try:
8+
workspace_dir = pathlib.Path(os.environ['BUILD_WORKSPACE_DIRECTORY'])
9+
except KeyError:
10+
print("this should be run with bazel run", file=sys.stderr)
11+
sys.exit(1)
12+
13+
dest_dir = workspace_dir / 'swift' / 'extractor-pack'
14+
shutil.rmtree(dest_dir, ignore_errors=True)
15+
os.environ['DESTDIR'] = str(dest_dir)
16+
main(sys.argv)

swift/tools/qltest.sh

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,5 @@
1+
#!/bin/bash
2+
3+
mkdir -p "$CODEQL_EXTRACTOR_SWIFT_TRAP_DIR"
4+
5+
exec "$CODEQL_EXTRACTOR_SWIFT_ROOT/tools/$CODEQL_PLATFORM/extractor"

0 commit comments

Comments
 (0)