Rust: Add cross-crate path resolution test

This commit is contained in:
Tom Hvitved
2025-03-18 10:20:57 +01:00
parent c5106f78ac
commit bd4c85a5bc
9 changed files with 66 additions and 49 deletions

View File

@@ -1,7 +1,7 @@
use lib::a_module::hello;
use lib::a_module::hello; // $ MISSING: item=HELLO
mod a_module;
fn main() {
hello();
hello(); // $ MISSING: item=HELLO
}

View File

@@ -1,3 +1,3 @@
pub fn hello() {
println!("Hello, world!");
}
} // HELLO

View File

@@ -0,0 +1 @@
import utils.test.PathResolutionInlineExpectationsTest

View File

@@ -2,14 +2,23 @@
"sysroot_src": "filled by the rust_project fixture",
"crates": [
{
"display_name": "exe",
"version": "0.1.0",
"root_module": "exe/src/main.rs",
"edition": "2021",
"deps": [{"crate": 1, "name": "lib"}]
"deps": [
{
"crate": 1,
"name": "lib"
}
]
},
{
"display_name": "lib",
"version": "0.1.0",
"root_module": "lib/src/lib.rs",
"edition": "2021",
"deps": []
}
]
}
}

View File

@@ -1,4 +1,4 @@
| Elements extracted | 87 |
| Elements extracted | 90 |
| Elements unextracted | 0 |
| Extraction errors | 0 |
| Extraction warnings | 0 |

View File

@@ -1,4 +1,4 @@
| Elements extracted | 87 |
| Elements extracted | 90 |
| Elements unextracted | 0 |
| Extraction errors | 0 |
| Extraction warnings | 0 |

View File

@@ -0,0 +1,48 @@
/**
* Provides an inline expectations test for path resolution.
*/
private import rust
private import codeql.rust.internal.PathResolution
private import codeql.rust.internal.TypeInference
private import utils.test.InlineExpectationsTest
private module ResolveTest implements TestSig {
string getARelevantTag() { result = "item" }
private predicate itemAt(ItemNode i, string filepath, int line, boolean inMacro) {
i.getLocation().hasLocationInfo(filepath, _, _, line, _) and
if i.(AstNode).isInMacroExpansion() then inMacro = true else inMacro = false
}
private predicate commmentAt(string text, string filepath, int line) {
exists(Comment c |
c.getLocation().hasLocationInfo(filepath, line, _, _, _) and
c.getCommentText() = text
)
}
private predicate item(ItemNode i, string value) {
exists(string filepath, int line, boolean inMacro | itemAt(i, filepath, line, inMacro) |
commmentAt(value, filepath, line) and inMacro = false
or
not (commmentAt(_, filepath, line) and inMacro = false) and
value = i.getName()
)
}
predicate hasActualResult(Location location, string element, string tag, string value) {
exists(AstNode n |
not n = any(Path parent).getQualifier() and
location = n.getLocation() and
element = n.toString() and
tag = "item"
|
item(resolvePath(n), value)
or
item(n.(MethodCallExpr).getStaticTarget(), value)
)
}
}
import MakeTest<ResolveTest>

View File

@@ -1,49 +1,8 @@
import rust
import codeql.rust.internal.PathResolution
import codeql.rust.internal.TypeInference
import utils.test.InlineExpectationsTest
import utils.test.PathResolutionInlineExpectationsTest
import TestUtils
query predicate mod(Module m) { toBeTested(m) }
query predicate resolvePath(Path p, ItemNode i) { toBeTested(p) and i = resolvePath(p) }
module ResolveTest implements TestSig {
string getARelevantTag() { result = "item" }
private predicate itemAt(ItemNode i, string filepath, int line, boolean inMacro) {
i.getLocation().hasLocationInfo(filepath, _, _, line, _) and
if i.isInMacroExpansion() then inMacro = true else inMacro = false
}
private predicate commmentAt(string text, string filepath, int line) {
exists(Comment c |
c.getLocation().hasLocationInfo(filepath, line, _, _, _) and
c.getCommentText() = text
)
}
private predicate item(ItemNode i, string value) {
exists(string filepath, int line, boolean inMacro | itemAt(i, filepath, line, inMacro) |
commmentAt(value, filepath, line) and inMacro = false
or
not (commmentAt(_, filepath, line) and inMacro = false) and
value = i.getName()
)
}
predicate hasActualResult(Location location, string element, string tag, string value) {
exists(AstNode n |
not n = any(Path parent).getQualifier() and
location = n.getLocation() and
element = n.toString() and
tag = "item"
|
item(resolvePath(n), value)
or
item(n.(MethodCallExpr).getStaticTarget(), value)
)
}
}
import MakeTest<ResolveTest>