Skip to content

Rust: add proc-macro capabilities to QL tests #19800

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 9 commits into from
Jun 18, 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 Cargo.lock

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

1 change: 1 addition & 0 deletions misc/bazel/3rdparty/tree_sitter_extractors_deps/defs.bzl

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

1 change: 1 addition & 0 deletions rust/extractor/BUILD.bazel
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@ codeql_rust_binary(
name = "extractor",
srcs = glob(["src/**/*.rs"]),
aliases = aliases(),
compile_data = ["src/qltest_cargo.mustache"],
proc_macro_deps = all_crate_deps(
proc_macro = True,
) + [
Expand Down
1 change: 1 addition & 0 deletions rust/extractor/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -41,3 +41,4 @@ tracing = "0.1.41"
tracing-flame = "0.2.0"
tracing-subscriber = "0.3.19"
chalk-ir = "0.100.0"
mustache = "0.9.0"
70 changes: 43 additions & 27 deletions rust/extractor/src/qltest.rs
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@ use glob::glob;
use itertools::Itertools;
use std::ffi::OsStr;
use std::fs;
use std::path::Path;
use std::process::Command;
use tracing::info;

Expand All @@ -15,39 +16,54 @@ fn dump_lib() -> anyhow::Result<()> {
let lib = paths
.iter()
.map(|p| p.file_stem().expect("results of glob must have a name"))
.filter(|&p| !["main", "lib"].map(OsStr::new).contains(&p))
.filter(|&p| !["main", "lib", "proc_macro"].map(OsStr::new).contains(&p))
.map(|p| format!("mod {};", p.to_string_lossy()))
.join("\n");
fs::write("lib.rs", lib).context("writing lib.rs")
}

fn dump_cargo_manifest(dependencies: &[String]) -> anyhow::Result<()> {
let mut manifest = String::from(
r#"[workspace]
[package]
name = "test"
version="0.0.1"
edition="2021"
[lib]
path="lib.rs"
"#,
);
if fs::exists("main.rs").context("checking existence of main.rs")? {
manifest.push_str(
r#"[[bin]]
name = "main"
path = "main.rs"
"#,
);
#[derive(serde::Serialize)]
enum TestCargoManifest<'a> {
Workspace {},
Lib {
uses_proc_macro: bool,
uses_main: bool,
dependencies: &'a [String],
},
Macro {},
}

impl TestCargoManifest<'_> {
pub fn dump(&self, path: impl AsRef<Path>) -> anyhow::Result<()> {
static TEMPLATE: std::sync::LazyLock<mustache::Template> = std::sync::LazyLock::new(|| {
mustache::compile_str(include_str!("qltest_cargo.mustache"))
.expect("compiling template")
});

let path = path.as_ref();
fs::create_dir_all(path).with_context(|| format!("creating {}", path.display()))?;
let path = path.join("Cargo.toml");
let rendered = TEMPLATE
.render_to_string(&self)
.with_context(|| format!("rendering {}", path.display()))?;
fs::write(&path, rendered).with_context(|| format!("writing {}", path.display()))
}
if !dependencies.is_empty() {
manifest.push_str("[dependencies]\n");
for dep in dependencies {
manifest.push_str(dep);
manifest.push('\n');
}
}
fn dump_cargo_manifest(dependencies: &[String]) -> anyhow::Result<()> {
let uses_proc_macro =
fs::exists("proc_macro.rs").context("checking existence of proc_macro.rs")?;
let lib_manifest = TestCargoManifest::Lib {
uses_proc_macro,
uses_main: fs::exists("main.rs").context("checking existence of main.rs")?,
dependencies,
};
if uses_proc_macro {
TestCargoManifest::Workspace {}.dump("")?;
lib_manifest.dump(".lib")?;
TestCargoManifest::Macro {}.dump(".proc_macro")
} else {
lib_manifest.dump("")
}
fs::write("Cargo.toml", manifest).context("writing Cargo.toml")
}

fn set_sources(config: &mut Config) -> anyhow::Result<()> {
Expand Down Expand Up @@ -75,6 +91,6 @@ pub(crate) fn prepare(config: &mut Config) -> anyhow::Result<()> {
} else {
anyhow::bail!("requested cargo check failed");
}
}
};
Ok(())
}
42 changes: 42 additions & 0 deletions rust/extractor/src/qltest_cargo.mustache
Original file line number Diff line number Diff line change
@@ -0,0 +1,42 @@
{{#Workspace}}
[workspace]
resolver = "2"
members = [".lib", ".proc_macro"]
{{/Workspace}}

{{#Lib}}
{{^uses_proc_macro}}
[workspace]
{{/uses_proc_macro}}
[package]
name = "test"
version = "0.0.1"
edition = "2021"
[lib]
path = "{{#uses_proc_macro}}../{{/uses_proc_macro}}lib.rs"
{{#uses_main}}
[[bin]]
name = "main"
path = "{{#uses_proc_macro}}../{{/uses_proc_macro}}main.rs"
{{/uses_main}}
[dependencies]
{{#uses_proc_macro}}
proc_macro = { path = "../.proc_macro" }
{{/uses_proc_macro}}
{{#dependencies}}
{{{.}}}
{{/dependencies}}
{{/Lib}}

{{#Macro}}
[package]
name = "proc_macro"
version = "0.0.1"
edition = "2021"
[lib]
path = "../proc_macro.rs"
proc_macro = true
[dependencies]
quote = "1.0.40"
syn = { version = "2.0.100", features = ["full"] }
{{/Macro}}
3 changes: 0 additions & 3 deletions rust/ql/integration-tests/macro-expansion/Cargo.toml

This file was deleted.

This file was deleted.

16 changes: 0 additions & 16 deletions rust/ql/integration-tests/macro-expansion/attributes/src/lib.rs

This file was deleted.

6 changes: 0 additions & 6 deletions rust/ql/integration-tests/macro-expansion/calls/Cargo.toml

This file was deleted.

30 changes: 0 additions & 30 deletions rust/ql/integration-tests/macro-expansion/calls/src/lib.rs

This file was deleted.

55 changes: 0 additions & 55 deletions rust/ql/integration-tests/macro-expansion/diagnostics.expected

This file was deleted.

This file was deleted.

This file was deleted.

34 changes: 0 additions & 34 deletions rust/ql/integration-tests/macro-expansion/test.expected

This file was deleted.

This file was deleted.

Original file line number Diff line number Diff line change
Expand Up @@ -37,6 +37,7 @@ private module ResolveTest implements TestSig {
not n = any(Path parent).getQualifier() and
location = n.getLocation() and
n.fromSource() and
not location.getFile().getAbsolutePath().matches("%proc_macro.rs") and
not n.isFromMacroExpansion() and
element = n.toString() and
tag = "item"
Expand Down
3 changes: 3 additions & 0 deletions rust/ql/test/.gitignore
Original file line number Diff line number Diff line change
@@ -1,3 +1,6 @@
Cargo.toml
lib.rs
target/
.proc_macro/
.lib/

Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
multiplePathResolutions
| macro_expansion.rs:1:5:1:14 | proc_macro | file://:0:0:0:0 | Crate([email protected]) |
| macro_expansion.rs:1:5:1:14 | proc_macro | proc_macro.rs:0:0:0:0 | Crate([email protected]) |
Loading
Loading