Skip to content

Commit 504f52e

Browse files
committed
Unified: Extract built-in Swift types
1 parent 4958c7f commit 504f52e

11 files changed

Lines changed: 172 additions & 14 deletions

File tree

unified/BUILD.bazel

Lines changed: 12 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -45,15 +45,24 @@ codeql_pkg_files(
4545
otherwise = ["//unified/extractor"],
4646
win64 = ["//unified/extractor-unsupported-os:extractor"],
4747
),
48-
prefix = "tools/{CODEQL_PLATFORM}",
48+
prefix = "{CODEQL_PLATFORM}",
49+
)
50+
51+
pkg_filegroup(
52+
name = "tools",
53+
srcs = [
54+
":extractor-arch",
55+
"//unified/tools",
56+
"//unified/tools/builtins",
57+
],
58+
prefix = "tools",
4959
)
5060

5161
codeql_pack(
5262
name = "unified",
5363
srcs = [
5464
":codeql-extractor-yml",
5565
":dbscheme-group",
56-
":extractor-arch",
57-
"//unified/tools",
66+
":tools",
5867
],
5968
)

unified/extractor/src/extractor.rs

Lines changed: 22 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,10 +1,10 @@
1-
use clap::Args;
2-
use std::path::PathBuf;
3-
41
use crate::languages;
2+
use clap::Args;
53
use codeql_extractor::extractor::desugaring;
64
use codeql_extractor::trap;
7-
5+
use std::path::Path;
6+
use std::path::PathBuf;
7+
use std::{env, fs};
88
#[derive(Args)]
99
pub struct Options {
1010
/// Sets a custom source archive folder
@@ -31,6 +31,24 @@ pub fn run(options: Options) -> std::io::Result<()> {
3131
lang.prefix = "unified";
3232
}
3333

34+
let builtins_path = env::var("CODEQL_EXTRACTOR_UNIFIED_ROOT")
35+
.map(|path| Path::new(&path).join("tools").join("builtins"))
36+
.expect("failed to read CODEQL_EXTRACTOR_UNIFIED_ROOT environment variable");
37+
let builtins_dir = fs::read_dir(builtins_path).expect("failed to read builtins directory");
38+
let mut file_list = fs::OpenOptions::new()
39+
.append(true)
40+
.open(&options.file_list)
41+
.expect("failed to open file list");
42+
for entry in builtins_dir {
43+
let entry = entry.expect("failed to read builtins directory");
44+
let path = entry.path();
45+
if path.extension().is_some_and(|ext| ext == "swift") {
46+
use std::io::Write;
47+
writeln!(file_list, "{}", path.display()).expect("failed to write to file list");
48+
}
49+
}
50+
drop(file_list);
51+
3452
let extractor = desugaring::Extractor {
3553
prefix: "unified".to_string(),
3654
languages,

unified/ql/lib/codeql/files/FileSystem.qll

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -37,7 +37,7 @@ module Folder = Impl::Folder;
3737
/** A file. */
3838
class File extends Container, Impl::File {
3939
/** Holds if this file was extracted from ordinary source code. */
40-
predicate fromSource() { any() }
40+
predicate fromSource() { exists(this.getRelativePath()) }
4141

4242
/**
4343
* Gets the number of lines containing code in this file. This value

unified/ql/lib/codeql/unified/internal/ControlFlowGraph.qll

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -41,7 +41,9 @@ private module Ast implements AstSig<Location> {
4141

4242
Callable getEnclosingCallable(AstNode node) { result = node.getEnclosingCallable() }
4343

44-
class Callable = U::Callable;
44+
class Callable extends U::Callable {
45+
Callable() { this.fromSource() }
46+
}
4547

4648
AstNode callableGetBody(Callable c) { result = c.getBody() }
4749

unified/ql/lib/codeql/unified/internal/FacadeAst.qll

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -15,6 +15,9 @@ module Unified {
1515
/** Gets the file containing this AST node. */
1616
File getFile() { result = this.getLocation().getFile() }
1717

18+
/** Holds if this AST node comes from ordinary source code. */
19+
predicate fromSource() { this.getFile().fromSource() }
20+
1821
/** Holds if this AST node has a modifier with the given text. */
1922
predicate hasModifier(string text) {
2023
exists(Modifier mod |

unified/ql/test/library-tests/BasicTest/test.ql

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,8 @@
11
import unified
22

3-
query predicate identifier(Identifier node, string value) { value = node.getValue() }
3+
query predicate identifier(Identifier node, string value) {
4+
node.fromSource() and value = node.getValue()
5+
}
46

57
query predicate namedPattern(NamedPattern node, string value) { value = node.getName() }
68

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,3 @@
11
import unified
22

3-
query predicate comments(Comment c, string text) { text = c.getCommentText() }
3+
query predicate comments(Comment c, string text) { c.fromSource() and text = c.getCommentText() }

unified/ql/test/library-tests/static-name-binding/explicit-instance-field-access.swift

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -15,7 +15,7 @@ private class A {
1515
return self.y // $ not handled by static name binding
1616
}
1717

18-
class func z() -> Int { // name=A.type.z
18+
class func z() -> Int { // $ access=Int // name=A.type.z
1919
return 789
2020
}
2121

@@ -37,7 +37,7 @@ private class B : A { // $ access=A
3737
return self.y // $ not handled by static name binding
3838
}
3939

40-
class func z() -> Int { // name=B.type.z
40+
class func z() -> Int { // $ access=Int // name=B.type.z
4141
return 789
4242
}
4343

unified/tools/BUILD.bazel

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,5 @@ codeql_pkg_files(
66
"BUILD.bazel",
77
],
88
exes = glob(["**/*"]),
9-
prefix = "tools",
109
visibility = ["//unified:__pkg__"],
1110
)

unified/tools/builtins/BUILD.bazel

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,8 @@
1+
load("//misc/bazel:pkg.bzl", "codeql_pkg_files")
2+
3+
codeql_pkg_files(
4+
name = "builtins",
5+
srcs = glob(["*.swift"]),
6+
prefix = "builtins",
7+
visibility = ["//unified:__subpackages__"],
8+
)

0 commit comments

Comments
 (0)