Skip to content

Commit 0547e9c

Browse files
committed
Rust: Path resolution for static items
1 parent 9618e9b commit 0547e9c

22 files changed

Lines changed: 378 additions & 122 deletions

File tree

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,7 @@
1+
/**
2+
* This module provides the public class `StaticAccess`.
3+
*/
4+
5+
private import internal.StaticImpl
6+
7+
final class StaticAccess = Impl::StaticAccess;

rust/ql/lib/codeql/rust/elements/internal/ConstImpl.qll

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -24,7 +24,9 @@ module Impl {
2424
* const X: i32 = 42;
2525
* ```
2626
*/
27-
class Const extends Generated::Const { }
27+
class Const extends Generated::Const {
28+
override string toStringImpl() { result = "const " + this.getName().getText() }
29+
}
2830

2931
/**
3032
* A constant access.

rust/ql/lib/codeql/rust/elements/internal/StaticImpl.qll

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

rust/ql/lib/codeql/rust/internal/PathResolution.qll

Lines changed: 32 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -659,6 +659,38 @@ private class ConstItemNode extends AssocItemNode instanceof Const {
659659
override TypeParam getTypeParam(int i) { none() }
660660
}
661661

662+
private class StaticItemNode extends ItemNode instanceof Static {
663+
override string getName() { result = Static.super.getName().getText() }
664+
665+
override Namespace getNamespace() { result.isValue() }
666+
667+
override Visibility getVisibility() { result = Static.super.getVisibility() }
668+
669+
override Attr getAnAttr() { result = Static.super.getAnAttr() }
670+
671+
override TypeParam getTypeParam(int i) { none() }
672+
673+
override predicate hasCanonicalPath(Crate c) { this.hasCanonicalPathPrefix(c) }
674+
675+
bindingset[c]
676+
private string getCanonicalPathPart(Crate c, int i) {
677+
i = 0 and
678+
result = this.getCanonicalPathPrefix(c)
679+
or
680+
i = 1 and
681+
result = "::"
682+
or
683+
i = 2 and
684+
result = this.getName()
685+
}
686+
687+
language[monotonicAggregates]
688+
override string getCanonicalPath(Crate c) {
689+
this.hasCanonicalPath(c) and
690+
result = strictconcat(int i | i in [0 .. 2] | this.getCanonicalPathPart(c, i) order by i)
691+
}
692+
}
693+
662694
private class TypeItemTypeItemNode extends NamedItemNode, TypeItemNode instanceof TypeItem {
663695
override string getName() { result = TypeItem.super.getName().getText() }
664696

rust/ql/lib/rust.qll

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -11,6 +11,7 @@ import codeql.rust.elements.AssignmentOperation
1111
import codeql.rust.elements.BitwiseOperation
1212
import codeql.rust.elements.ComparisonOperation
1313
import codeql.rust.elements.ConstAccess
14+
import codeql.rust.elements.StaticAccess
1415
import codeql.rust.elements.DerefExpr
1516
import codeql.rust.elements.LiteralExprExt
1617
import codeql.rust.elements.LogicalOperation

rust/ql/test/TestUtils.qll

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -20,3 +20,12 @@ class CrateElement extends Element {
2020
class Builtin extends AstNode {
2121
Builtin() { this.getFile().getAbsolutePath().matches("%/builtins/%.rs") }
2222
}
23+
24+
predicate commmentAt(string text, string filepath, int line) {
25+
exists(Comment c |
26+
c.getLocation().hasLocationInfo(filepath, line, _, _, _) and
27+
c.getCommentText().trim() = text and
28+
c.fromSource() and
29+
not text.matches("$%")
30+
)
31+
}
Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,13 +1,13 @@
11
instances
2-
| gen_const.rs:4:5:7:22 | Const | isConst: | yes | isDefault: | no | hasImplementation: | yes |
2+
| gen_const.rs:4:5:7:22 | const X | isConst: | yes | isDefault: | no | hasImplementation: | yes |
33
getAttributeMacroExpansion
44
getAttr
55
getBody
6-
| gen_const.rs:4:5:7:22 | Const | gen_const.rs:7:20:7:21 | 42 |
6+
| gen_const.rs:4:5:7:22 | const X | gen_const.rs:7:20:7:21 | 42 |
77
getGenericParamList
88
getName
9-
| gen_const.rs:4:5:7:22 | Const | gen_const.rs:7:11:7:11 | X |
9+
| gen_const.rs:4:5:7:22 | const X | gen_const.rs:7:11:7:11 | X |
1010
getTypeRepr
11-
| gen_const.rs:4:5:7:22 | Const | gen_const.rs:7:14:7:16 | i32 |
11+
| gen_const.rs:4:5:7:22 | const X | gen_const.rs:7:14:7:16 | i32 |
1212
getVisibility
1313
getWhereClause

rust/ql/test/extractor-tests/generated/ExternItemList/ExternItemList.expected

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -3,4 +3,4 @@ instances
33
getAttr
44
getExternItem
55
| gen_extern_item_list.rs:7:16:10:5 | ExternItemList | 0 | gen_extern_item_list.rs:8:9:8:17 | fn foo |
6-
| gen_extern_item_list.rs:7:16:10:5 | ExternItemList | 1 | gen_extern_item_list.rs:9:9:9:24 | Static |
6+
| gen_extern_item_list.rs:7:16:10:5 | ExternItemList | 1 | gen_extern_item_list.rs:9:9:9:24 | static BAR |
Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,11 +1,11 @@
11
instances
2-
| gen_static.rs:4:5:7:23 | Static | isMut: | no | isStatic: | yes | isUnsafe: | no |
2+
| gen_static.rs:4:5:7:23 | static X | isMut: | no | isStatic: | yes | isUnsafe: | no |
33
getAttributeMacroExpansion
44
getAttr
55
getBody
6-
| gen_static.rs:4:5:7:23 | Static | gen_static.rs:7:21:7:22 | 42 |
6+
| gen_static.rs:4:5:7:23 | static X | gen_static.rs:7:21:7:22 | 42 |
77
getName
8-
| gen_static.rs:4:5:7:23 | Static | gen_static.rs:7:12:7:12 | X |
8+
| gen_static.rs:4:5:7:23 | static X | gen_static.rs:7:12:7:12 | X |
99
getTypeRepr
10-
| gen_static.rs:4:5:7:23 | Static | gen_static.rs:7:15:7:17 | i32 |
10+
| gen_static.rs:4:5:7:23 | static X | gen_static.rs:7:15:7:17 | i32 |
1111
getVisibility

rust/ql/test/extractor-tests/macro-expansion/PrintAst.expected

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -957,7 +957,7 @@ macro_expansion.rs:
957957
# 94| getName(): [Name] MyTrait
958958
# 98| getItem(20): [Union] union MyDeriveUnion
959959
# 99| getDeriveMacroExpansion(0): [MacroItems] MacroItems
960-
# 99| getItem(0): [Const] Const
960+
# 99| getItem(0): [Const] const CONST_MyDeriveUnion
961961
# 98| getBody(): [IntegerLiteralExpr] 42
962962
# 99| getName(): [Name] CONST_MyDeriveUnion
963963
# 98| getTypeRepr(): [PathTypeRepr] u32

0 commit comments

Comments
 (0)