Skip to content

Commit b4c979f

Browse files
authored
Merge pull request #20548 from hvitved/rust/macro-call-resolution
Rust: Macro call resolution
2 parents a34d6d4 + f8b104d commit b4c979f

File tree

27 files changed

+769
-628
lines changed

27 files changed

+769
-628
lines changed
Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,10 @@
11
use lib::a_module::hello; // $ item=HELLO
22

3+
use lib::my_macro2; // $ item=my_macro2
4+
35
mod a_module;
46

57
fn main() {
8+
my_macro2!(); // $ item=my_macro2
69
hello(); // $ item=HELLO
710
}
Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,2 +1,2 @@
1-
| exe/src/main.rs:5:1:7:1 | fn main |
2-
| lib/src/a_module/mod.rs:1:1:3:1 | fn hello |
1+
| exe/src/main.rs:7:1:10:1 | fn main |
2+
| lib/src/a_module/mod.rs:1:1:4:1 | fn hello |
Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,4 @@
11
pub fn hello() {
2-
println!("Hello, world!");
2+
my_macro2!(); // $ item=my_macro2
3+
println!("Hello, world!"); // $ item=println
34
} // HELLO
Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1 +1,17 @@
1+
#[macro_use]
2+
mod macros {
3+
#[macro_export]
4+
macro_rules! my_macro1 {
5+
() => {
6+
println!("my_macro!");
7+
};
8+
}
9+
#[macro_export]
10+
macro_rules! my_macro2 {
11+
() => {
12+
$crate::my_macro1!();
13+
};
14+
}
15+
}
16+
117
pub mod a_module;
Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,4 @@
1+
testFailures
2+
resolveDollarCrate
3+
| exe/src/main.rs:8:5:8:14 | $crate | lib/src/lib.rs:0:0:0:0 | Crate(lib@0.1.0) |
4+
| lib/src/a_module/mod.rs:2:5:2:14 | $crate | lib/src/lib.rs:0:0:0:0 | Crate(lib@0.1.0) |
Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1 +1,10 @@
1+
import rust
2+
import codeql.rust.internal.PathResolution
13
import utils.test.PathResolutionInlineExpectationsTest
4+
5+
query predicate resolveDollarCrate(RelevantPath p, Crate c) {
6+
c = resolvePath(p) and
7+
p.isDollarCrate() and
8+
p.fromSource() and
9+
c.fromSource()
10+
}

rust/ql/integration-tests/hello-workspace/summary.cargo.expected

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -9,8 +9,8 @@
99
| Inconsistencies - Path resolution | 0 |
1010
| Inconsistencies - SSA | 0 |
1111
| Inconsistencies - data flow | 0 |
12-
| Lines of code extracted | 9 |
13-
| Lines of user code extracted | 9 |
14-
| Macro calls - resolved | 2 |
15-
| Macro calls - total | 2 |
12+
| Lines of code extracted | 21 |
13+
| Lines of user code extracted | 21 |
14+
| Macro calls - resolved | 10 |
15+
| Macro calls - total | 10 |
1616
| Macro calls - unresolved | 0 |

rust/ql/integration-tests/hello-workspace/summary.rust-project.expected

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -9,8 +9,8 @@
99
| Inconsistencies - Path resolution | 0 |
1010
| Inconsistencies - SSA | 0 |
1111
| Inconsistencies - data flow | 0 |
12-
| Lines of code extracted | 9 |
13-
| Lines of user code extracted | 9 |
14-
| Macro calls - resolved | 2 |
15-
| Macro calls - total | 2 |
12+
| Lines of code extracted | 21 |
13+
| Lines of user code extracted | 21 |
14+
| Macro calls - resolved | 10 |
15+
| Macro calls - total | 10 |
1616
| Macro calls - unresolved | 0 |

rust/ql/lib/codeql/rust/elements/internal/MacroCallImpl.qll

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -12,6 +12,7 @@ private import codeql.rust.elements.internal.generated.MacroCall
1212
*/
1313
module Impl {
1414
private import rust
15+
private import codeql.rust.internal.PathResolution
1516

1617
pragma[nomagic]
1718
predicate isInMacroExpansion(AstNode root, AstNode n) {
@@ -44,5 +45,12 @@ module Impl {
4445
isInMacroExpansion(this, result) and
4546
this.getTokenTree().getLocation().contains(result.getLocation())
4647
}
48+
49+
/**
50+
* Gets the macro definition that this macro call resolves to.
51+
*
52+
* The result is either a `MacroDef` or a `MacroRules`.
53+
*/
54+
Item resolveMacro() { result = resolvePath(this.getPath()) }
4755
}
4856
}

0 commit comments

Comments
 (0)