Skip to content

Commit d43b6d2

Browse files
committed
Rust: Macro call resolution
1 parent f4388c8 commit d43b6d2

File tree

24 files changed

+376
-273
lines changed

24 files changed

+376
-273
lines changed
Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,2 +1,2 @@
11
| exe/src/main.rs:5:1:7:1 | fn main |
2-
| lib/src/a_module/mod.rs:1:1:3:1 | fn hello |
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_macro!(); // $ item=my_macro
3+
println!("Hello, world!"); // $ item=println
34
} // HELLO
Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1 +1,10 @@
1+
#[macro_use]
2+
mod macros {
3+
macro_rules! my_macro {
4+
() => {
5+
println!("my_macro!");
6+
};
7+
}
8+
}
9+
110
pub mod a_module;

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 | 15 |
13+
| Lines of user code extracted | 15 |
14+
| Macro calls - resolved | 5 |
15+
| Macro calls - total | 5 |
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 | 15 |
13+
| Lines of user code extracted | 15 |
14+
| Macro calls - resolved | 5 |
15+
| Macro calls - total | 5 |
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)