diff --git a/rust/ql/integration-tests/.gitignore b/rust/ql/integration-tests/.gitignore index 2f7896d1d136..2c96eb1b6517 100644 --- a/rust/ql/integration-tests/.gitignore +++ b/rust/ql/integration-tests/.gitignore @@ -1 +1,2 @@ target/ +Cargo.lock diff --git a/rust/ql/integration-tests/macro-expansion/Cargo.lock b/rust/ql/integration-tests/macro-expansion/Cargo.lock deleted file mode 100644 index 976dc5e7def2..000000000000 --- a/rust/ql/integration-tests/macro-expansion/Cargo.lock +++ /dev/null @@ -1,53 +0,0 @@ -# This file is automatically @generated by Cargo. -# It is not intended for manual editing. -version = 4 - -[[package]] -name = "macro_expansion" -version = "0.1.0" -dependencies = [ - "macros", -] - -[[package]] -name = "macros" -version = "0.1.0" -dependencies = [ - "quote", - "syn", -] - -[[package]] -name = "proc-macro2" -version = "1.0.95" -source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "02b3e5e68a3a1a02aad3ec490a98007cbc13c37cbe84a3cd7b8e406d76e7f778" -dependencies = [ - "unicode-ident", -] - -[[package]] -name = "quote" -version = "1.0.40" -source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "1885c039570dc00dcb4ff087a89e185fd56bae234ddc7f056a945bf36467248d" -dependencies = [ - "proc-macro2", -] - -[[package]] -name = "syn" -version = "2.0.101" -source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "8ce2b7fc941b3a24138a0a7cf8e858bfc6a992e7978a068a5c760deb0ed43caf" -dependencies = [ - "proc-macro2", - "quote", - "unicode-ident", -] - -[[package]] -name = "unicode-ident" -version = "1.0.18" -source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "5a5f39404a5da50712a4c1eecf25e90dd62b613502b7e925fd4e4d19b5c96512" diff --git a/rust/ql/integration-tests/macro-expansion/Cargo.toml b/rust/ql/integration-tests/macro-expansion/Cargo.toml index b7ce204e07ff..9be2ec64b578 100644 --- a/rust/ql/integration-tests/macro-expansion/Cargo.toml +++ b/rust/ql/integration-tests/macro-expansion/Cargo.toml @@ -1,11 +1,3 @@ [workspace] -members = ["macros"] +members = [ "attributes", "calls", "proc_macros"] resolver = "2" - -[package] -name = "macro_expansion" -version = "0.1.0" -edition = "2024" - -[dependencies] -macros = { path = "macros" } diff --git a/rust/ql/integration-tests/macro-expansion/attributes/Cargo.toml b/rust/ql/integration-tests/macro-expansion/attributes/Cargo.toml new file mode 100644 index 000000000000..b475ead960af --- /dev/null +++ b/rust/ql/integration-tests/macro-expansion/attributes/Cargo.toml @@ -0,0 +1,7 @@ +[package] +name = "attributes" +version = "0.1.0" +edition = "2024" + +[dependencies] +proc_macros = { path = "../proc_macros" } diff --git a/rust/ql/integration-tests/macro-expansion/src/lib.rs b/rust/ql/integration-tests/macro-expansion/attributes/src/lib.rs similarity index 67% rename from rust/ql/integration-tests/macro-expansion/src/lib.rs rename to rust/ql/integration-tests/macro-expansion/attributes/src/lib.rs index 2007d3b111aa..682083aa10ae 100644 --- a/rust/ql/integration-tests/macro-expansion/src/lib.rs +++ b/rust/ql/integration-tests/macro-expansion/attributes/src/lib.rs @@ -1,8 +1,8 @@ -use macros::repeat; +use proc_macros::repeat; #[repeat(3)] fn foo() { - println!("Hello, world!"); + _ = concat!("Hello ", "world!"); #[repeat(2)] fn inner() {} diff --git a/rust/ql/integration-tests/macro-expansion/calls/Cargo.toml b/rust/ql/integration-tests/macro-expansion/calls/Cargo.toml new file mode 100644 index 000000000000..d38cf944489e --- /dev/null +++ b/rust/ql/integration-tests/macro-expansion/calls/Cargo.toml @@ -0,0 +1,6 @@ +[package] +name = "calls" +version = "0.1.0" +edition = "2024" + +[dependencies] diff --git a/rust/ql/integration-tests/macro-expansion/calls/src/included.rs b/rust/ql/integration-tests/macro-expansion/calls/src/included.rs new file mode 100644 index 000000000000..7397e24bd81e --- /dev/null +++ b/rust/ql/integration-tests/macro-expansion/calls/src/included.rs @@ -0,0 +1,3 @@ +fn included() { + _ = concat!("Hello", " ", "world!"); // this doesn't expand (in included.rs) since 0.0.274 +} diff --git a/rust/ql/integration-tests/macro-expansion/calls/src/lib.rs b/rust/ql/integration-tests/macro-expansion/calls/src/lib.rs new file mode 100644 index 000000000000..df3fccb7c40a --- /dev/null +++ b/rust/ql/integration-tests/macro-expansion/calls/src/lib.rs @@ -0,0 +1,30 @@ +struct S; + +macro_rules! def_x { + () => { + fn x() {} + }; +} + +impl S { + def_x!(); // this doesn't expand since 0.0.274 +} + +macro_rules! my_macro { + ($head:expr, $($tail:tt)*) => { format!($head, $($tail)*) }; +} + + +fn test() { + _ = concat!("x", "y"); + + _ = my_macro!( + concat!("<", "{}", ">"), // this doesn't expand since 0.0.274 + "hi", + ); +} + +include!("included.rs"); + +#[doc = include_str!("some.txt")] // this doesn't expand since 0.0.274 +fn documented() {} diff --git a/rust/ql/integration-tests/macro-expansion/calls/src/some.txt b/rust/ql/integration-tests/macro-expansion/calls/src/some.txt new file mode 100644 index 000000000000..4c5477a837a9 --- /dev/null +++ b/rust/ql/integration-tests/macro-expansion/calls/src/some.txt @@ -0,0 +1 @@ +Hey! diff --git a/rust/ql/integration-tests/macro-expansion/diagnostics.expected b/rust/ql/integration-tests/macro-expansion/diagnostics.expected index c98f923b463f..511bd49f1a51 100644 --- a/rust/ql/integration-tests/macro-expansion/diagnostics.expected +++ b/rust/ql/integration-tests/macro-expansion/diagnostics.expected @@ -38,7 +38,7 @@ "pretty": "__REDACTED__" } }, - "numberOfFiles": 2, + "numberOfFiles": 4, "numberOfManifests": 1 }, "severity": "note", diff --git a/rust/ql/integration-tests/macro-expansion/macros/Cargo.toml b/rust/ql/integration-tests/macro-expansion/proc_macros/Cargo.toml similarity index 88% rename from rust/ql/integration-tests/macro-expansion/macros/Cargo.toml rename to rust/ql/integration-tests/macro-expansion/proc_macros/Cargo.toml index a503d3fb903f..712f7ba33933 100644 --- a/rust/ql/integration-tests/macro-expansion/macros/Cargo.toml +++ b/rust/ql/integration-tests/macro-expansion/proc_macros/Cargo.toml @@ -1,5 +1,5 @@ [package] -name = "macros" +name = "proc_macros" version = "0.1.0" edition = "2024" diff --git a/rust/ql/integration-tests/macro-expansion/macros/src/lib.rs b/rust/ql/integration-tests/macro-expansion/proc_macros/src/lib.rs similarity index 100% rename from rust/ql/integration-tests/macro-expansion/macros/src/lib.rs rename to rust/ql/integration-tests/macro-expansion/proc_macros/src/lib.rs diff --git a/rust/ql/integration-tests/macro-expansion/source_archive.expected b/rust/ql/integration-tests/macro-expansion/source_archive.expected index ec61af6032b7..c1700a0a3003 100644 --- a/rust/ql/integration-tests/macro-expansion/source_archive.expected +++ b/rust/ql/integration-tests/macro-expansion/source_archive.expected @@ -1,2 +1,4 @@ -macros/src/lib.rs -src/lib.rs +attributes/src/lib.rs +calls/src/included.rs +calls/src/lib.rs +proc_macros/src/lib.rs diff --git a/rust/ql/integration-tests/macro-expansion/summary.expected b/rust/ql/integration-tests/macro-expansion/summary.expected deleted file mode 100644 index 6917d67b1cf0..000000000000 --- a/rust/ql/integration-tests/macro-expansion/summary.expected +++ /dev/null @@ -1,16 +0,0 @@ -| Extraction errors | 0 | -| Extraction warnings | 0 | -| Files extracted - total | 2 | -| Files extracted - with errors | 0 | -| Files extracted - without errors | 2 | -| Files extracted - without errors % | 100 | -| Inconsistencies - AST | 0 | -| Inconsistencies - CFG | 0 | -| Inconsistencies - Path resolution | 0 | -| Inconsistencies - SSA | 0 | -| Inconsistencies - data flow | 0 | -| Lines of code extracted | 29 | -| Lines of user code extracted | 29 | -| Macro calls - resolved | 52 | -| Macro calls - total | 53 | -| Macro calls - unresolved | 1 | diff --git a/rust/ql/integration-tests/macro-expansion/summary.qlref b/rust/ql/integration-tests/macro-expansion/summary.qlref deleted file mode 100644 index 926fc7903911..000000000000 --- a/rust/ql/integration-tests/macro-expansion/summary.qlref +++ /dev/null @@ -1 +0,0 @@ -queries/summary/SummaryStatsReduced.ql diff --git a/rust/ql/integration-tests/macro-expansion/test.expected b/rust/ql/integration-tests/macro-expansion/test.expected index 83edecf5d5df..24d95c99b351 100644 --- a/rust/ql/integration-tests/macro-expansion/test.expected +++ b/rust/ql/integration-tests/macro-expansion/test.expected @@ -1,17 +1,34 @@ -| src/lib.rs:3:1:9:1 | fn foo | 0 | src/lib.rs:4:1:8:16 | fn foo_0 | -| src/lib.rs:3:1:9:1 | fn foo | 1 | src/lib.rs:4:1:8:16 | fn foo_1 | -| src/lib.rs:3:1:9:1 | fn foo | 2 | src/lib.rs:4:1:8:16 | fn foo_2 | -| src/lib.rs:7:5:8:16 | fn inner | 0 | src/lib.rs:8:5:8:16 | fn inner_0 | -| src/lib.rs:7:5:8:16 | fn inner | 0 | src/lib.rs:8:5:8:16 | fn inner_0 | -| src/lib.rs:7:5:8:16 | fn inner | 0 | src/lib.rs:8:5:8:16 | fn inner_0 | -| src/lib.rs:7:5:8:16 | fn inner | 1 | src/lib.rs:8:5:8:16 | fn inner_1 | -| src/lib.rs:7:5:8:16 | fn inner | 1 | src/lib.rs:8:5:8:16 | fn inner_1 | -| src/lib.rs:7:5:8:16 | fn inner | 1 | src/lib.rs:8:5:8:16 | fn inner_1 | -| src/lib.rs:11:1:13:11 | fn bar | 0 | src/lib.rs:12:1:13:10 | fn bar_0 | -| src/lib.rs:11:1:13:11 | fn bar | 1 | src/lib.rs:12:1:13:10 | fn bar_1 | -| src/lib.rs:12:1:13:10 | fn bar_0 | 0 | src/lib.rs:13:1:13:10 | fn bar_0_0 | -| src/lib.rs:12:1:13:10 | fn bar_0 | 1 | src/lib.rs:13:1:13:10 | fn bar_0_1 | -| src/lib.rs:12:1:13:10 | fn bar_0 | 2 | src/lib.rs:13:1:13:10 | fn bar_0_2 | -| src/lib.rs:12:1:13:10 | fn bar_1 | 0 | src/lib.rs:13:1:13:10 | fn bar_1_0 | -| src/lib.rs:12:1:13:10 | fn bar_1 | 1 | src/lib.rs:13:1:13:10 | fn bar_1_1 | -| src/lib.rs:12:1:13:10 | fn bar_1 | 2 | src/lib.rs:13:1:13:10 | fn bar_1_2 | +attribute_macros +| attributes/src/lib.rs:3:1:9:1 | fn foo | 0 | attributes/src/lib.rs:4:1:8:16 | fn foo_0 | +| attributes/src/lib.rs:3:1:9:1 | fn foo | 1 | attributes/src/lib.rs:4:1:8:16 | fn foo_1 | +| attributes/src/lib.rs:3:1:9:1 | fn foo | 2 | attributes/src/lib.rs:4:1:8:16 | fn foo_2 | +| attributes/src/lib.rs:7:5:8:16 | fn inner | 0 | attributes/src/lib.rs:8:5:8:16 | fn inner_0 | +| attributes/src/lib.rs:7:5:8:16 | fn inner | 0 | attributes/src/lib.rs:8:5:8:16 | fn inner_0 | +| attributes/src/lib.rs:7:5:8:16 | fn inner | 0 | attributes/src/lib.rs:8:5:8:16 | fn inner_0 | +| attributes/src/lib.rs:7:5:8:16 | fn inner | 1 | attributes/src/lib.rs:8:5:8:16 | fn inner_1 | +| attributes/src/lib.rs:7:5:8:16 | fn inner | 1 | attributes/src/lib.rs:8:5:8:16 | fn inner_1 | +| attributes/src/lib.rs:7:5:8:16 | fn inner | 1 | attributes/src/lib.rs:8:5:8:16 | fn inner_1 | +| attributes/src/lib.rs:11:1:13:11 | fn bar | 0 | attributes/src/lib.rs:12:1:13:10 | fn bar_0 | +| attributes/src/lib.rs:11:1:13:11 | fn bar | 1 | attributes/src/lib.rs:12:1:13:10 | fn bar_1 | +| attributes/src/lib.rs:12:1:13:10 | fn bar_0 | 0 | attributes/src/lib.rs:13:1:13:10 | fn bar_0_0 | +| attributes/src/lib.rs:12:1:13:10 | fn bar_0 | 1 | attributes/src/lib.rs:13:1:13:10 | fn bar_0_1 | +| attributes/src/lib.rs:12:1:13:10 | fn bar_0 | 2 | attributes/src/lib.rs:13:1:13:10 | fn bar_0_2 | +| attributes/src/lib.rs:12:1:13:10 | fn bar_1 | 0 | attributes/src/lib.rs:13:1:13:10 | fn bar_1_0 | +| attributes/src/lib.rs:12:1:13:10 | fn bar_1 | 1 | attributes/src/lib.rs:13:1:13:10 | fn bar_1_1 | +| attributes/src/lib.rs:12:1:13:10 | fn bar_1 | 2 | attributes/src/lib.rs:13:1:13:10 | fn bar_1_2 | +macro_calls +| attributes/src/lib.rs:5:9:5:34 | concat!... | attributes/src/lib.rs:5:17:5:34 | "Hello world!" | +| attributes/src/lib.rs:5:9:5:34 | concat!... | attributes/src/lib.rs:5:17:5:34 | "Hello world!" | +| attributes/src/lib.rs:5:9:5:34 | concat!... | attributes/src/lib.rs:5:17:5:34 | "Hello world!" | +| calls/src/included.rs:2:9:2:39 | concat!... | calls/src/included.rs:2:17:2:38 | "Hello world!" | +| calls/src/lib.rs:10:5:10:13 | def_x!... | calls/src/lib.rs:10:5:10:13 | MacroItems | +| calls/src/lib.rs:19:9:19:25 | concat!... | calls/src/lib.rs:19:17:19:24 | "xy" | +| calls/src/lib.rs:21:9:24:5 | my_macro!... | calls/src/lib.rs:22:9:23:13 | MacroExpr | +| calls/src/lib.rs:22:9:22:31 | concat!... | calls/src/lib.rs:22:17:22:30 | "<{}>" | +| calls/src/lib.rs:22:9:23:13 | ...::format_args!... | calls/src/lib.rs:22:9:23:13 | FormatArgsExpr | +| calls/src/lib.rs:22:9:23:13 | format!... | calls/src/lib.rs:22:9:23:13 | ...::must_use(...) | +| calls/src/lib.rs:27:1:27:24 | concat!... | calls/src/lib.rs:27:1:27:24 | "Hello world!" | +| calls/src/lib.rs:27:1:27:24 | include!... | calls/src/lib.rs:27:1:27:24 | MacroItems | +| calls/src/lib.rs:29:9:29:32 | include_str!... | calls/src/lib.rs:29:22:29:31 | "" | +unexpanded_macro_calls +| attributes/src/lib.rs:5:9:5:35 | concat!... | diff --git a/rust/ql/integration-tests/macro-expansion/test.ql b/rust/ql/integration-tests/macro-expansion/test.ql index 3369acc3a285..439ffab9a293 100644 --- a/rust/ql/integration-tests/macro-expansion/test.ql +++ b/rust/ql/integration-tests/macro-expansion/test.ql @@ -1,5 +1,15 @@ import rust -from Item i, MacroItems items, int index, Item expanded -where i.fromSource() and i.getAttributeMacroExpansion() = items and items.getItem(index) = expanded -select i, index, expanded +query predicate attribute_macros(Item i, int index, Item expanded) { + i.fromSource() and expanded = i.getAttributeMacroExpansion().getItem(index) +} + +query predicate macro_calls(MacroCall c, AstNode expansion) { + c.fromSource() and + not c.getLocation().getFile().getAbsolutePath().matches("%proc_macros%") and + expansion = c.getMacroCallExpansion() +} + +query predicate unexpanded_macro_calls(MacroCall c) { + c.fromSource() and not c.hasMacroCallExpansion() +} diff --git a/rust/ql/test/extractor-tests/generated/MacroCall/some.txt b/rust/ql/test/extractor-tests/generated/MacroCall/some.txt new file mode 100644 index 000000000000..10ddd6d257e0 --- /dev/null +++ b/rust/ql/test/extractor-tests/generated/MacroCall/some.txt @@ -0,0 +1 @@ +Hello!