Skip to content

Commit 667eed9

Browse files
author
Paolo Tranquilli
committed
Rust: store in the DB if a library function or const had a body
When skipping bodies in library code, we lose the information whether a body was originally present. This can be important, for example when determining whether a trait method has a default implementation. With this change that information can be recovered via the `hasImplementation` predicate.
1 parent 77c40da commit 667eed9

File tree

11 files changed

+109
-13
lines changed

11 files changed

+109
-13
lines changed

rust/extractor/src/generated/.generated.list

Lines changed: 1 addition & 1 deletion
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

rust/extractor/src/generated/top.rs

Lines changed: 12 additions & 0 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

rust/extractor/src/translate/base.rs

Lines changed: 24 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -38,6 +38,7 @@ macro_rules! post_emit {
3838
$self.extract_macro_call_expanded($node, $label);
3939
};
4040
(Function, $self:ident, $node:ident, $label:ident) => {
41+
$self.emit_function_has_implementation($node, $label);
4142
$self.extract_canonical_origin($node, $label.into());
4243
};
4344
(Trait, $self:ident, $node:ident, $label:ident) => {
@@ -83,6 +84,9 @@ macro_rules! post_emit {
8384
(PathSegment, $self:ident, $node:ident, $label:ident) => {
8485
$self.extract_types_from_path_segment($node, $label.into());
8586
};
87+
(Const, $self:ident, $node:ident, $label:ident) => {
88+
$self.emit_const_has_implementation($node, $label);
89+
};
8690
($($_:tt)*) => {};
8791
}
8892

@@ -761,4 +765,24 @@ impl<'a> Translator<'a> {
761765
generated::Item::emit_attribute_macro_expansion(label, expanded, &mut self.trap.writer);
762766
}
763767
}
768+
769+
pub(crate) fn emit_function_has_implementation(
770+
&mut self,
771+
node: &ast::Fn,
772+
label: Label<generated::Function>,
773+
) {
774+
if node.body().is_some() {
775+
generated::Function::emit_has_implementation(label, &mut self.trap.writer);
776+
}
777+
}
778+
779+
pub(crate) fn emit_const_has_implementation(
780+
&mut self,
781+
node: &ast::Const,
782+
label: Label<generated::Const>,
783+
) {
784+
if node.body().is_some() {
785+
generated::Const::emit_has_implementation(label, &mut self.trap.writer);
786+
}
787+
}
764788
}

rust/ql/.generated.list

Lines changed: 6 additions & 6 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

rust/ql/lib/codeql/rust/elements/internal/generated/Const.qll

Lines changed: 10 additions & 0 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

rust/ql/lib/codeql/rust/elements/internal/generated/Function.qll

Lines changed: 10 additions & 0 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

rust/ql/lib/codeql/rust/elements/internal/generated/Raw.qll

Lines changed: 16 additions & 0 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

rust/ql/lib/rust.dbscheme

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2816,6 +2816,11 @@ const_visibilities(
28162816
int visibility: @visibility ref
28172817
);
28182818

2819+
#keyset[id]
2820+
const_has_implementation(
2821+
int id: @const ref
2822+
);
2823+
28192824
enums(
28202825
unique int id: @enum
28212826
);
@@ -2985,6 +2990,11 @@ function_where_clauses(
29852990
int where_clause: @where_clause ref
29862991
);
29872992

2993+
#keyset[id]
2994+
function_has_implementation(
2995+
int id: @function ref
2996+
);
2997+
29882998
impls(
29892999
unique int id: @impl
29903000
);

rust/ql/test/extractor-tests/generated/Const/Const.ql

Lines changed: 6 additions & 3 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

rust/ql/test/extractor-tests/generated/Function/Function.ql

Lines changed: 6 additions & 3 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

rust/schema/annotations.py

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -951,6 +951,10 @@ class _:
951951
todo!()
952952
```
953953
"""
954+
has_implementation: predicate | doc("this constant has an implementation") | desc("""
955+
This is the same as `hasBody` for source code, but for library code (for which we always skip
956+
the body), this will hold when the body was present in the original code.
957+
""") | rust.detach
954958

955959

956960
@annotate(ConstArg)
@@ -1882,6 +1886,10 @@ class _:
18821886
class _:
18831887
param_list: drop
18841888
attrs: drop
1889+
has_implementation: predicate | doc("this function has an implementation") | desc("""
1890+
This is the same as `hasBody` for source code, but for library code (for which we always skip
1891+
the body), this will hold when the body was present in the original code.
1892+
""") | rust.detach
18851893

18861894

18871895
@annotate(ClosureExpr, add_bases=[Callable])

0 commit comments

Comments
 (0)