Skip to content

Commit c8d668b

Browse files
author
Paolo Tranquilli
committed
Rust: add "standard path"
1 parent 925fac2 commit c8d668b

File tree

12 files changed

+113
-25
lines changed

12 files changed

+113
-25
lines changed

rust/ql/.generated.list

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

rust/ql/.gitattributes

Lines changed: 0 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/dataflow/FlowSummary.qll

Lines changed: 13 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -29,14 +29,20 @@ module LibraryCallable {
2929

3030
/** Gets a call to this library callable. */
3131
CallExprBase getACall() {
32-
exists(Resolvable r, string crate |
33-
r = CallExprBaseImpl::getCallResolvable(result) and
34-
this = crate + r.getResolvedPath()
32+
exists(
33+
TypeItemCanonicalPath path, ModuleItemCanonicalPath item, Namespace namespace,
34+
string namespace_path
3535
|
36-
crate = r.getResolvedCrateOrigin() + "::_::"
37-
or
38-
not r.hasResolvedCrateOrigin() and
39-
crate = ""
36+
path = CallExprBaseImpl::getCallResolvable(result).getResolvedCanonicalPath() and
37+
item = path.getParent() and
38+
namespace = item.getNamespace() and
39+
namespace_path = namespace.getPath() and
40+
if namespace_path = ""
41+
then this = namespace.getRoot().toString() + "::" + item.getName() + "::" + path.getName()
42+
else
43+
this =
44+
namespace.getRoot().toString() + "::" + namespace_path + "::" + item.getName() + "::" +
45+
path.getName()
4046
)
4147
}
4248
}

rust/ql/lib/codeql/rust/elements/canonical_paths/internal/CanonicalPathImpl.qll

Lines changed: 18 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,3 @@
1-
// generated by codegen, remove this comment if you wish to edit this file
21
/**
32
* This module provides a hand-modifiable wrapper around the generated class `CanonicalPath`.
43
*
@@ -12,8 +11,25 @@ private import codeql.rust.elements.internal.generated.canonical_paths.Canonical
1211
* be referenced directly.
1312
*/
1413
module Impl {
14+
// the following QLdoc is generated: if you need to edit it, do it in the schema file
1515
/**
1616
* The base class for all canonical paths that can be the result of a path resolution.
1717
*/
18-
class CanonicalPath extends Generated::CanonicalPath { }
18+
class CanonicalPath extends Generated::CanonicalPath {
19+
/**
20+
* If this canonical path is of the form `crate::mod1::mod2::Type::name`, then this predicate
21+
* splits it into its components `crate::mod1::mod2`, `Type` and `name`. This applies to
22+
* type, trait and type impl items, not to module items (see `hasStandardPath/2`) nor to
23+
* trait impl items (TODO).
24+
*/
25+
predicate hasStandardPath(string namespace, string type, string name) { none() }
26+
27+
/**
28+
* If this canonical path is of the form `crate::mod1::mod2::name`, then this predicate
29+
* splits it into its components `crate::mod1::mod2` and `name`. This applies to
30+
* module items, but not to type, trait and type impl items (see `hasStandardPath/3`) nor to
31+
* trait impl items (TODO).
32+
*/
33+
predicate hasStandardPath(string namespace, string name) { none() }
34+
}
1935
}

rust/ql/lib/codeql/rust/elements/canonical_paths/internal/ModuleItemCanonicalPathImpl.qll

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -19,5 +19,9 @@ module Impl {
1919
override string toString() {
2020
result = this.getNamespace().toAbbreviatedString() + "::" + this.getName()
2121
}
22+
23+
override predicate hasStandardPath(string namespace, string name) {
24+
this.getName() = name and namespace = this.getNamespace().getNamespaceString()
25+
}
2226
}
2327
}

rust/ql/lib/codeql/rust/elements/canonical_paths/internal/NamespaceImpl.qll

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -22,5 +22,17 @@ module Impl {
2222
if this.getPath() = "" then result = root else result = root + "::" + this.getPath()
2323
)
2424
}
25+
26+
/**
27+
* Returns a string representation of this namespace, with the root and path separated by `::`.
28+
*/
29+
cached
30+
string getNamespaceString() {
31+
exists(string root, string path |
32+
root = this.getRoot().toString() and
33+
path = this.getPath() and
34+
if path = "" then result = root else result = root + "::" + path
35+
)
36+
}
2537
}
2638
}

rust/ql/lib/codeql/rust/elements/canonical_paths/internal/TypeImplItemCanonicalPathImpl.qll

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -19,5 +19,9 @@ module Impl {
1919
override string toString() {
2020
result = "<" + this.getParent().toAbbreviatedString() + ">::" + this.getName()
2121
}
22+
23+
override predicate hasStandardPath(string namespace, string type, string name) {
24+
this.getName() = name and this.getParent().hasStandardPath(namespace, type)
25+
}
2226
}
2327
}

rust/ql/lib/codeql/rust/elements/canonical_paths/internal/TypeItemCanonicalPathImpl.qll

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -19,5 +19,9 @@ module Impl {
1919
override string toString() {
2020
result = this.getParent().toAbbreviatedString() + "::" + this.getName()
2121
}
22+
23+
override predicate hasStandardPath(string namespace, string type, string name) {
24+
this.getName() = name and this.getParent().hasStandardPath(namespace, type)
25+
}
2226
}
2327
}

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

Lines changed: 16 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,3 @@
1-
// generated by codegen, remove this comment if you wish to edit this file
21
/**
32
* This module provides a hand-modifiable wrapper around the generated class `Addressable`.
43
*
@@ -12,10 +11,25 @@ private import codeql.rust.elements.internal.generated.Addressable
1211
* be referenced directly.
1312
*/
1413
module Impl {
14+
// the following QLdoc is generated: if you need to edit it, do it in the schema file
1515
/**
1616
* Something that can be addressed by a path.
1717
*
1818
* TODO: This does not yet include all possible cases.
1919
*/
20-
class Addressable extends Generated::Addressable { }
20+
class Addressable extends Generated::Addressable {
21+
/**
22+
* Splits the standard path into its components (see `CanonicalPath::hasStandardPath/3`).
23+
*/
24+
predicate hasStandardPath(string namespace, string type, string name) {
25+
this.getCanonicalPath().hasStandardPath(namespace, type, name)
26+
}
27+
28+
/**
29+
* Splits the standard path into its components (see `CanonicalPath::hasStandardPath/2`).
30+
*/
31+
predicate hasStandardPath(string namespace, string name) {
32+
this.getCanonicalPath().hasStandardPath(namespace, name)
33+
}
34+
}
2135
}

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

Lines changed: 2 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -20,16 +20,9 @@ module Impl {
2020
class Resolvable extends Generated::Resolvable {
2121
/**
2222
* Holds if this resolvable and the item `i` resolves to the same canonical
23-
* path in the same crate
23+
* path
2424
*/
2525
pragma[nomagic]
26-
predicate resolvesAsItem(Item i) {
27-
this.getResolvedPath() = i.getExtendedCanonicalPath() and
28-
(
29-
this.getResolvedCrateOrigin() = i.getCrateOrigin()
30-
or
31-
not this.hasResolvedCrateOrigin() and not i.hasCrateOrigin()
32-
)
33-
}
26+
predicate resolvesAsItem(Item i) { this.getResolvedCanonicalPath() = i.getCanonicalPath() }
3427
}
3528
}

0 commit comments

Comments
 (0)