Skip to content

Rust: extract declarations of builtin types #19421

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 3 commits into from
May 2, 2025
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions rust/BUILD.bazel
Original file line number Diff line number Diff line change
Expand Up @@ -36,6 +36,7 @@ pkg_filegroup(
srcs = [
":tools-arch",
"//rust/tools",
"//rust/tools/builtins",
],
prefix = "tools",
)
Expand Down
36 changes: 25 additions & 11 deletions rust/extractor/src/main.rs
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,7 @@ use std::{
collections::HashMap,
path::{Path, PathBuf},
};
use std::{env, fs};
use tracing::{error, info, warn};
use tracing_subscriber::layer::SubscriberExt;
use tracing_subscriber::util::SubscriberInitExt;
Expand Down Expand Up @@ -77,17 +78,19 @@ impl<'a> Extractor<'a> {
}
let no_location = (LineCol { line: 0, col: 0 }, LineCol { line: 0, col: 0 });
if let Err(reason) = semantics_info {
let message = format!("semantic analyzer unavailable ({reason})");
let full_message = format!(
"{message}: macro expansion, call graph, and type inference will be skipped."
);
translator.emit_diagnostic(
trap::DiagnosticSeverity::Warning,
"semantics".to_owned(),
message,
full_message,
no_location,
);
if !reason.is_empty() {
let message = format!("semantic analyzer unavailable ({reason})");
let full_message = format!(
"{message}: macro expansion, call graph, and type inference will be skipped."
);
translator.emit_diagnostic(
trap::DiagnosticSeverity::Warning,
"semantics".to_owned(),
message,
full_message,
no_location,
);
}
}
translator.emit_source_file(ast);
translator.trap.commit().unwrap_or_else(|err| {
Expand Down Expand Up @@ -276,5 +279,16 @@ fn main() -> anyhow::Result<()> {
}
}
}
let builtins_dir = env::var("CODEQL_EXTRACTOR_RUST_ROOT")
.map(|path| Path::new(&path).join("tools").join("builtins"))?;
let builtins = fs::read_dir(builtins_dir).context("failed to read builtins directory")?;
for entry in builtins {
let entry = entry.context("failed to read builtins directory")?;
let path = entry.path();
if path.extension().is_some_and(|ext| ext == "rs") {
extractor.extract_without_semantics(&path, "");
}
}

extractor.emit_extraction_diagnostics(start, &cfg)
}
Original file line number Diff line number Diff line change
Expand Up @@ -30,7 +30,7 @@
"pretty": "__REDACTED__"
}
},
"numberOfFiles": 5,
"numberOfFiles": 6,
"numberOfManifests": 1
},
"severity": "note",
Expand Down
Original file line number Diff line number Diff line change
@@ -1,4 +1,6 @@
| Cargo.toml:0:0:0:0 | LoadManifest(Cargo.toml) |
| file:///types.rs:0:0:0:0 | Extract(/types.rs) |
| file:///types.rs:0:0:0:0 | Parse(/types.rs) |
| file://:0:0:0:0 | CrateGraph |
| file://:0:0:0:0 | FindManifests |
| src/directory_module/mod.rs:0:0:0:0 | Extract(src/directory_module/mod.rs) |
Expand Down
25 changes: 24 additions & 1 deletion rust/ql/integration-tests/hello-project/steps.ql
Original file line number Diff line number Diff line change
@@ -1,4 +1,27 @@
import codeql.rust.elements.internal.ExtractorStep

from ExtractorStep step
private class Step instanceof ExtractorStep {
string toString() {
result = super.getAction() + "(" + this.getFilePath() + ")"
or
not super.hasFile() and result = super.getAction()
}

private string getFilePath() {
exists(File file | file = super.getFile() |
exists(file.getRelativePath()) and result = file.getAbsolutePath()
or
not exists(file.getRelativePath()) and result = "/" + file.getBaseName()
)
}

predicate hasLocationInfo(
string filepath, int startline, int startcolumn, int endline, int endcolumn
) {
super.hasLocationInfo(_, startline, startcolumn, endline, endcolumn) and
filepath = this.getFilePath()
}
}

from Step step
select step
Original file line number Diff line number Diff line change
@@ -1,3 +1,5 @@
| file:///types.rs:0:0:0:0 | Extract(/types.rs) |
| file:///types.rs:0:0:0:0 | Parse(/types.rs) |
| file://:0:0:0:0 | CrateGraph |
| file://:0:0:0:0 | FindManifests |
| rust-project.json:0:0:0:0 | LoadManifest(rust-project.json) |
Expand Down
2 changes: 1 addition & 1 deletion rust/ql/integration-tests/hello-project/summary.expected
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@
| Inconsistencies - Path resolution | 0 |
| Inconsistencies - SSA | 0 |
| Inconsistencies - data flow | 0 |
| Lines of code extracted | 6 |
| Lines of code extracted | 23 |
| Lines of user code extracted | 6 |
| Macro calls - resolved | 2 |
| Macro calls - total | 2 |
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -30,7 +30,7 @@
"pretty": "__REDACTED__"
}
},
"numberOfFiles": 4,
"numberOfFiles": 5,
"numberOfManifests": 1
},
"severity": "note",
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -30,7 +30,7 @@
"pretty": "__REDACTED__"
}
},
"numberOfFiles": 4,
"numberOfFiles": 5,
"numberOfManifests": 1
},
"severity": "note",
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,8 @@
| exe/src/main.rs:0:0:0:0 | Extract(exe/src/main.rs) |
| exe/src/main.rs:0:0:0:0 | LoadSource(exe/src/main.rs) |
| exe/src/main.rs:0:0:0:0 | Parse(exe/src/main.rs) |
| file:///types.rs:0:0:0:0 | Extract(/types.rs) |
| file:///types.rs:0:0:0:0 | Parse(/types.rs) |
| file://:0:0:0:0 | CrateGraph |
| file://:0:0:0:0 | FindManifests |
| lib/src/a_module/mod.rs:0:0:0:0 | Extract(lib/src/a_module/mod.rs) |
Expand Down
25 changes: 24 additions & 1 deletion rust/ql/integration-tests/hello-workspace/steps.ql
Original file line number Diff line number Diff line change
@@ -1,4 +1,27 @@
import codeql.rust.elements.internal.ExtractorStep

from ExtractorStep step
private class Step instanceof ExtractorStep {
string toString() {
result = super.getAction() + "(" + this.getFilePath() + ")"
or
not super.hasFile() and result = super.getAction()
}

private string getFilePath() {
exists(File file | file = super.getFile() |
exists(file.getRelativePath()) and result = file.getAbsolutePath()
or
not exists(file.getRelativePath()) and result = "/" + file.getBaseName()
)
}

predicate hasLocationInfo(
string filepath, int startline, int startcolumn, int endline, int endcolumn
) {
super.hasLocationInfo(_, startline, startcolumn, endline, endcolumn) and
filepath = this.getFilePath()
}
}

from Step step
select step
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,8 @@
| exe/src/main.rs:0:0:0:0 | Extract(exe/src/main.rs) |
| exe/src/main.rs:0:0:0:0 | LoadSource(exe/src/main.rs) |
| exe/src/main.rs:0:0:0:0 | Parse(exe/src/main.rs) |
| file:///types.rs:0:0:0:0 | Extract(/types.rs) |
| file:///types.rs:0:0:0:0 | Parse(/types.rs) |
| file://:0:0:0:0 | CrateGraph |
| file://:0:0:0:0 | FindManifests |
| lib/src/a_module/mod.rs:0:0:0:0 | Extract(lib/src/a_module/mod.rs) |
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@
| Inconsistencies - Path resolution | 0 |
| Inconsistencies - SSA | 0 |
| Inconsistencies - data flow | 0 |
| Lines of code extracted | 9 |
| Lines of code extracted | 26 |
| Lines of user code extracted | 9 |
| Macro calls - resolved | 2 |
| Macro calls - total | 2 |
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@
| Inconsistencies - Path resolution | 0 |
| Inconsistencies - SSA | 0 |
| Inconsistencies - data flow | 0 |
| Lines of code extracted | 9 |
| Lines of code extracted | 26 |
| Lines of user code extracted | 9 |
| Macro calls - resolved | 2 |
| Macro calls - total | 2 |
Expand Down
Original file line number Diff line number Diff line change
@@ -1,6 +1,8 @@
| Cargo.toml:0:0:0:0 | LoadManifest(Cargo.toml) |
| exe/src/main.rs:0:0:0:0 | Extract(exe/src/main.rs) |
| exe/src/main.rs:0:0:0:0 | Parse(exe/src/main.rs) |
| file:///types.rs:0:0:0:0 | Extract(/types.rs) |
| file:///types.rs:0:0:0:0 | Parse(/types.rs) |
| file://:0:0:0:0 | CrateGraph |
| file://:0:0:0:0 | FindManifests |
| lib/src/lib.rs:0:0:0:0 | Extract(lib/src/lib.rs) |
Expand Down
25 changes: 24 additions & 1 deletion rust/ql/integration-tests/workspace-with-glob/steps.ql
Original file line number Diff line number Diff line change
@@ -1,4 +1,27 @@
import codeql.rust.elements.internal.ExtractorStep

from ExtractorStep step
private class Step instanceof ExtractorStep {
string toString() {
result = super.getAction() + "(" + this.getFilePath() + ")"
or
not super.hasFile() and result = super.getAction()
}

private string getFilePath() {
exists(File file | file = super.getFile() |
exists(file.getRelativePath()) and result = file.getAbsolutePath()
or
not exists(file.getRelativePath()) and result = "/" + file.getBaseName()
)
}

predicate hasLocationInfo(
string filepath, int startline, int startcolumn, int endline, int endcolumn
) {
super.hasLocationInfo(_, startline, startcolumn, endline, endcolumn) and
filepath = this.getFilePath()
}
}

from Step step
select step
6 changes: 5 additions & 1 deletion rust/ql/test/TestUtils.qll
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
private import rust

predicate toBeTested(Element e) { not e instanceof CrateElement }
predicate toBeTested(Element e) { not e instanceof CrateElement and not e instanceof Builtin }

class CrateElement extends Element {
CrateElement() {
Expand All @@ -9,3 +9,7 @@ class CrateElement extends Element {
any(Crate c).getModule() = this.(AstNode).getParentNode*()
}
}

class Builtin extends AstNode {
Builtin() { this.getFile().getAbsolutePath().matches("%/builtins/%.rs") }
}
Original file line number Diff line number Diff line change
@@ -1 +1 @@
| 60 |
| 77 |
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@
| Inconsistencies - Path resolution | 0 |
| Inconsistencies - SSA | 0 |
| Inconsistencies - data flow | 0 |
| Lines of code extracted | 60 |
| Lines of code extracted | 77 |
| Lines of user code extracted | 60 |
| Macro calls - resolved | 8 |
| Macro calls - total | 9 |
Expand Down
8 changes: 8 additions & 0 deletions rust/tools/builtins/BUILD.bazel
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
load("//misc/bazel:pkg.bzl", "codeql_pkg_files")

codeql_pkg_files(
name = "builtins",
srcs = glob(["*.rs"]),
prefix = "builtins",
visibility = ["//rust:__subpackages__"],
)
25 changes: 25 additions & 0 deletions rust/tools/builtins/types.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,25 @@
// The Language Prelude: https://doc.rust-lang.org/reference/names/preludes.html#language-prelude

// Type namespace
// Boolean type
pub struct bool;
// Textual types
pub struct char;
pub struct str;
// Integer types
pub struct i8;
pub struct i16;
pub struct i32;
pub struct i64;
pub struct i128;
pub struct u8;
pub struct u16;
pub struct u32;
pub struct u64;
pub struct u128;
// Machine-dependent integer types
pub struct usize;
pub struct isize;
// floating-point types
pub struct f32;
pub struct f64;