Skip to content

Rust: Use macro call location as fall back in macro expansions #19217

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 1 commit into from
Apr 4, 2025
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
22 changes: 18 additions & 4 deletions rust/ql/lib/codeql/rust/elements/internal/LocatableImpl.qll
Original file line number Diff line number Diff line change
Expand Up @@ -46,11 +46,25 @@ module Impl {
predicate fromSource() { exists(this.getFile().getRelativePath()) }
}

private @location_default getDbLocation(Locatable l) {
locatable_locations(Synth::convertLocatableToRaw(l), result)
}

private MacroCall getImmediatelyEnclosingMacroCall(AstNode n) {
result = n.getParentNode()
or
exists(AstNode mid |
result = getImmediatelyEnclosingMacroCall(mid) and
n.getParentNode() = mid and
not mid instanceof MacroCall
)
}

/** Gets the non-synthesized location of `l`, if any. */
LocationImpl::LocationDefault getLocationDefault(Locatable l) {
exists(@location_default location |
result = LocationImpl::TLocationDefault(location) and
locatable_locations(Synth::convertLocatableToRaw(l), location)
)
result = LocationImpl::TLocationDefault(getDbLocation(l))
or
not exists(getDbLocation(l)) and
result = getLocationDefault(getImmediatelyEnclosingMacroCall(l))
}
}
3 changes: 2 additions & 1 deletion rust/ql/lib/codeql/rust/internal/AstConsistency.qll
Original file line number Diff line number Diff line change
Expand Up @@ -25,7 +25,8 @@ query predicate multipleLocations(Locatable e) { strictcount(e.getLocation()) >
* Holds if `e` does not have a `Location`.
*/
query predicate noLocation(Locatable e) {
not exists(e.getLocation()) and not e.(AstNode).getParentNode*() = any(Crate c).getModule()
not exists(e.getLocation()) and
not e.(AstNode).getParentNode*() = any(Crate c).getModule()
}

private predicate multiplePrimaryQlClasses(Element e) {
Expand Down

This file was deleted.

Original file line number Diff line number Diff line change
@@ -1,2 +1,2 @@
| gen_macro_items.rs:5:5:5:38 | MacroItems | 0 | file://:0:0:0:0 | use ...::Path |
| gen_macro_items.rs:5:5:5:38 | MacroItems | 1 | file://:0:0:0:0 | fn get_parent |
| gen_macro_items.rs:5:5:5:38 | MacroItems | 0 | gen_macro_items.rs:5:5:5:38 | use ...::Path |
| gen_macro_items.rs:5:5:5:38 | MacroItems | 1 | gen_macro_items.rs:5:5:5:38 | fn get_parent |