Skip to content

Commit 006cb02

Browse files
authored
Merge pull request #19217 from hvitved/rust/macro-expansion-inherit-location
Rust: Use macro call location as fall back in macro expansions
2 parents 6f704f0 + 5731fa9 commit 006cb02

File tree

4 files changed

+22
-48
lines changed

4 files changed

+22
-48
lines changed

Diff for: rust/ql/lib/codeql/rust/elements/internal/LocatableImpl.qll

+18-4
Original file line numberDiff line numberDiff line change
@@ -46,11 +46,25 @@ module Impl {
4646
predicate fromSource() { exists(this.getFile().getRelativePath()) }
4747
}
4848

49+
private @location_default getDbLocation(Locatable l) {
50+
locatable_locations(Synth::convertLocatableToRaw(l), result)
51+
}
52+
53+
private MacroCall getImmediatelyEnclosingMacroCall(AstNode n) {
54+
result = n.getParentNode()
55+
or
56+
exists(AstNode mid |
57+
result = getImmediatelyEnclosingMacroCall(mid) and
58+
n.getParentNode() = mid and
59+
not mid instanceof MacroCall
60+
)
61+
}
62+
4963
/** Gets the non-synthesized location of `l`, if any. */
5064
LocationImpl::LocationDefault getLocationDefault(Locatable l) {
51-
exists(@location_default location |
52-
result = LocationImpl::TLocationDefault(location) and
53-
locatable_locations(Synth::convertLocatableToRaw(l), location)
54-
)
65+
result = LocationImpl::TLocationDefault(getDbLocation(l))
66+
or
67+
not exists(getDbLocation(l)) and
68+
result = getLocationDefault(getImmediatelyEnclosingMacroCall(l))
5569
}
5670
}

Diff for: rust/ql/lib/codeql/rust/internal/AstConsistency.qll

+2-1
Original file line numberDiff line numberDiff line change
@@ -25,7 +25,8 @@ query predicate multipleLocations(Locatable e) { strictcount(e.getLocation()) >
2525
* Holds if `e` does not have a `Location`.
2626
*/
2727
query predicate noLocation(Locatable e) {
28-
not exists(e.getLocation()) and not e.(AstNode).getParentNode*() = any(Crate c).getModule()
28+
not exists(e.getLocation()) and
29+
not e.(AstNode).getParentNode*() = any(Crate c).getModule()
2930
}
3031

3132
private predicate multiplePrimaryQlClasses(Element e) {

Diff for: rust/ql/test/extractor-tests/generated/MacroItems/CONSISTENCY/AstConsistency.expected

-41
This file was deleted.
Original file line numberDiff line numberDiff line change
@@ -1,2 +1,2 @@
1-
| gen_macro_items.rs:5:5:5:38 | MacroItems | 0 | file://:0:0:0:0 | use ...::Path |
2-
| gen_macro_items.rs:5:5:5:38 | MacroItems | 1 | file://:0:0:0:0 | fn get_parent |
1+
| gen_macro_items.rs:5:5:5:38 | MacroItems | 0 | gen_macro_items.rs:5:5:5:38 | use ...::Path |
2+
| gen_macro_items.rs:5:5:5:38 | MacroItems | 1 | gen_macro_items.rs:5:5:5:38 | fn get_parent |

0 commit comments

Comments
 (0)