From 23d2e03c3b6ef91cea0192ad3d76bd303afd40a2 Mon Sep 17 00:00:00 2001 From: Arthur Baars Date: Wed, 18 Sep 2024 19:25:26 +0200 Subject: [PATCH] Rust: lines of code, exclude 'SourceFile' node --- rust/ql/lib/codeql/files/FileSystem.qll | 8 ++++++-- rust/ql/test/query-tests/diagnostics/LinesOfCode.expected | 2 +- .../test/query-tests/diagnostics/LinesOfUserCode.expected | 2 +- .../diagnostics/LinesOfUserCodeInFiles.expected | 2 +- .../ql/test/query-tests/diagnostics/SummaryStats.expected | 4 ++-- 5 files changed, 11 insertions(+), 7 deletions(-) diff --git a/rust/ql/lib/codeql/files/FileSystem.qll b/rust/ql/lib/codeql/files/FileSystem.qll index 5588d2ecc7e50..f6926609d685d 100644 --- a/rust/ql/lib/codeql/files/FileSystem.qll +++ b/rust/ql/lib/codeql/files/FileSystem.qll @@ -2,6 +2,8 @@ private import codeql.Locations private import codeql.util.FileSystem +private import codeql.rust.elements.SourceFile +private import codeql.rust.elements.AstNode private module Input implements InputSig { abstract class ContainerBase extends @container { @@ -41,8 +43,10 @@ class File extends Container, Impl::File { int getNumberOfLinesOfCode() { result = count(int line | - exists(Location loc | - loc.getFile() = this and + exists(AstNode node, Location loc | + not node instanceof SourceFile and loc = node.getLocation() + | + node.getFile() = this and line = [loc.getStartLine(), loc.getEndLine()] and not loc instanceof EmptyLocation ) diff --git a/rust/ql/test/query-tests/diagnostics/LinesOfCode.expected b/rust/ql/test/query-tests/diagnostics/LinesOfCode.expected index d6a21ebb49cf1..035a74975be88 100644 --- a/rust/ql/test/query-tests/diagnostics/LinesOfCode.expected +++ b/rust/ql/test/query-tests/diagnostics/LinesOfCode.expected @@ -1 +1 @@ -| 49 | +| 48 | diff --git a/rust/ql/test/query-tests/diagnostics/LinesOfUserCode.expected b/rust/ql/test/query-tests/diagnostics/LinesOfUserCode.expected index d6a21ebb49cf1..035a74975be88 100644 --- a/rust/ql/test/query-tests/diagnostics/LinesOfUserCode.expected +++ b/rust/ql/test/query-tests/diagnostics/LinesOfUserCode.expected @@ -1 +1 @@ -| 49 | +| 48 | diff --git a/rust/ql/test/query-tests/diagnostics/LinesOfUserCodeInFiles.expected b/rust/ql/test/query-tests/diagnostics/LinesOfUserCodeInFiles.expected index 03846c665ae81..2be3f73088ca5 100644 --- a/rust/ql/test/query-tests/diagnostics/LinesOfUserCodeInFiles.expected +++ b/rust/ql/test/query-tests/diagnostics/LinesOfUserCodeInFiles.expected @@ -1,6 +1,6 @@ | my_struct.rs:0:0:0:0 | my_struct.rs | 21 | | my_macro.rs:0:0:0:0 | my_macro.rs | 8 | -| lib.rs:0:0:0:0 | lib.rs | 7 | | main.rs:0:0:0:0 | main.rs | 7 | +| lib.rs:0:0:0:0 | lib.rs | 6 | | does_not_compile.rs:0:0:0:0 | does_not_compile.rs | 3 | | error.rs:0:0:0:0 | error.rs | 3 | diff --git a/rust/ql/test/query-tests/diagnostics/SummaryStats.expected b/rust/ql/test/query-tests/diagnostics/SummaryStats.expected index 3bfa8561888aa..145a18cae05da 100644 --- a/rust/ql/test/query-tests/diagnostics/SummaryStats.expected +++ b/rust/ql/test/query-tests/diagnostics/SummaryStats.expected @@ -1,5 +1,5 @@ | Elements extracted | 204 | | Elements unextracted | 0 | | Files extracted | 6 | -| Lines of code extracted | 49 | -| Lines of user code extracted | 49 | +| Lines of code extracted | 48 | +| Lines of user code extracted | 48 |