From 8f1c1a83999cf37a8490c2e02f15fdcb5a14a41f Mon Sep 17 00:00:00 2001 From: Geoffrey White <40627776+geoffw0@users.noreply.github.com> Date: Wed, 25 Sep 2024 15:33:21 +0100 Subject: [PATCH] Revert "Rust: remove queries that no longer work" This reverts commit e19bca0de8e31a2ba39ab449c1bd154b9d354b46. --- .../diagnostics/UnextractedElements.ql | 61 +++++++++++++++++++ .../diagnostics/UnextractedElements.expected | 16 +++++ .../diagnostics/UnextractedElements.qlref | 1 + 3 files changed, 78 insertions(+) create mode 100644 rust/ql/src/queries/diagnostics/UnextractedElements.ql create mode 100644 rust/ql/test/query-tests/diagnostics/UnextractedElements.expected create mode 100644 rust/ql/test/query-tests/diagnostics/UnextractedElements.qlref diff --git a/rust/ql/src/queries/diagnostics/UnextractedElements.ql b/rust/ql/src/queries/diagnostics/UnextractedElements.ql new file mode 100644 index 000000000000..ae368aac677f --- /dev/null +++ b/rust/ql/src/queries/diagnostics/UnextractedElements.ql @@ -0,0 +1,61 @@ +/** + * @name Unextracted Elements + * @description List all elements that weren't extracted due to unimplemented features or parse errors. + * @id rust/diagnostics/unextracted-elements + */ + +import rust + +/** + * Gets a location for an `Unimplemented` node. + */ +Location getUnimplementedLocation(Unimplemented node) { + result = node.(Locatable).getLocation() + or + not node instanceof Locatable and + result instanceof EmptyLocation +} + +/** + * Gets `l.toString()`, but with any locations outside of the source location prefix cleaned up. + */ +bindingset[l] +string cleanLocationString(Location l) { + if exists(l.getFile().getRelativePath()) or l instanceof EmptyLocation + then result = l.toString() + else l.getFile().getParentContainer().getAbsolutePath() + result = l.toString() // remove the directory from the string +} + +/** + * Gets a string along the lines of " (x2)", corresponding to the number `i`. For `i = 1`, the result is the empty string. + */ +bindingset[i] +string multipleString(int i) { + i = 1 and result = "" + or + i > 1 and result = " (x" + i.toString() + ")" +} + +query predicate listUnimplemented(string location, string msg) { + // something that is not extracted yet + exists(int c | + c = strictcount(Unimplemented n | cleanLocationString(getUnimplementedLocation(n)) = location) and + msg = "Not yet implemented" + multipleString(c) + "." + ) +} + +query predicate listMissingExpr(string location, string msg) { + // gaps in the AST due to parse errors + exists(int c | + c = strictcount(MissingExpr e | cleanLocationString(e.getLocation()) = location) and + msg = "Missing expression" + multipleString(c) + "." + ) +} + +query predicate listMissingPat(string location, string msg) { + // gaps in the AST due to parse errors + exists(int c | + c = strictcount(MissingPat p | cleanLocationString(p.getLocation()) = location) and + msg = "Missing pattern" + multipleString(c) + "." + ) +} diff --git a/rust/ql/test/query-tests/diagnostics/UnextractedElements.expected b/rust/ql/test/query-tests/diagnostics/UnextractedElements.expected new file mode 100644 index 000000000000..5ed70660c2b4 --- /dev/null +++ b/rust/ql/test/query-tests/diagnostics/UnextractedElements.expected @@ -0,0 +1,16 @@ +listUnimplemented +| @0:0:0:0 | Not yet implemented (x14). | +| does_not_compile.rs@2:2:2:5 | Not yet implemented. | +| does_not_compile.rs@2:7:2:8 | Not yet implemented. | +| does_not_compile.rs@2:10:2:12 | Not yet implemented. | +| does_not_compile.rs@2:14:2:20 | Not yet implemented. | +| does_not_compile.rs@2:22:2:25 | Not yet implemented. | +| does_not_compile.rs@2:27:2:30 | Not yet implemented. | +| main.rs@16:5:16:22 | Not yet implemented. | +| main.rs@17:5:17:21 | Not yet implemented. | +| my_struct.rs@2:1:13:1 | Not yet implemented. | +| my_struct.rs@24:9:27:9 | Not yet implemented. | +| my_struct.rs@25:19:25:30 | Not yet implemented. | +| my_struct.rs@29:5:29:5 | Not yet implemented. | +listMissingExpr +listMissingPat diff --git a/rust/ql/test/query-tests/diagnostics/UnextractedElements.qlref b/rust/ql/test/query-tests/diagnostics/UnextractedElements.qlref new file mode 100644 index 000000000000..e02770f843d7 --- /dev/null +++ b/rust/ql/test/query-tests/diagnostics/UnextractedElements.qlref @@ -0,0 +1 @@ +queries/diagnostics/UnextractedElements.ql