diff --git a/rust/ql/lib/codeql/files/FileSystem.qll b/rust/ql/lib/codeql/files/FileSystem.qll index 528dde52fd91..5588d2ecc7e5 100644 --- a/rust/ql/lib/codeql/files/FileSystem.qll +++ b/rust/ql/lib/codeql/files/FileSystem.qll @@ -33,4 +33,19 @@ class Folder = Impl::Folder; class File extends Container, Impl::File { /** Holds if this file was extracted from ordinary source code. */ predicate fromSource() { any() } + + /** + * Gets the number of lines containing code in this file. This value + * is approximate. + */ + int getNumberOfLinesOfCode() { + result = + count(int line | + exists(Location loc | + loc.getFile() = this and + line = [loc.getStartLine(), loc.getEndLine()] and + not loc instanceof EmptyLocation + ) + ) + } } diff --git a/rust/ql/src/queries/summary/LinesOfCode.ql b/rust/ql/src/queries/summary/LinesOfCode.ql new file mode 100644 index 000000000000..5e38ad33f26c --- /dev/null +++ b/rust/ql/src/queries/summary/LinesOfCode.ql @@ -0,0 +1,14 @@ +/** + * @name Total lines of Rust code in the database + * @description The total number of lines of Rust code across all files, including any libraries and auto-generated files that the extractor sees. This is a useful metric of the size of a database. For all files that were seen during the build, this query counts the lines of code, excluding whitespace or comments. + * @kind metric + * @id rust/summary/lines-of-code + * @tags summary + * lines-of-code + * telemetry + */ + +import rust +import Stats + +select getLinesOfCode() diff --git a/rust/ql/src/queries/summary/LinesOfUserCode.ql b/rust/ql/src/queries/summary/LinesOfUserCode.ql new file mode 100644 index 000000000000..3c81c3caf22d --- /dev/null +++ b/rust/ql/src/queries/summary/LinesOfUserCode.ql @@ -0,0 +1,14 @@ +/** + * @name Total lines of user written Rust code in the database + * @description The total number of lines of Rust code from the source code directory. This query counts the lines of code, excluding whitespace or comments. + * @kind metric + * @id rust/summary/lines-of-user-code + * @tags summary + * lines-of-code + * debug + */ + +import rust +import Stats + +select getLinesOfUserCode() diff --git a/rust/ql/src/queries/summary/LinesOfUserCodeInFiles.ql b/rust/ql/src/queries/summary/LinesOfUserCodeInFiles.ql new file mode 100644 index 000000000000..a3e0b3ff9a89 --- /dev/null +++ b/rust/ql/src/queries/summary/LinesOfUserCodeInFiles.ql @@ -0,0 +1,15 @@ +/** + * @name Lines of user code in files + * @description Measures the number of lines of code in each file from the source directory, ignoring lines that contain only comments or whitespace. + * @kind metric + * @id rust/summary/lines-of-user-code-in-files + * @metricType file + */ + +import rust + +from File f, int n +where + exists(f.getRelativePath()) and + n = f.getNumberOfLinesOfCode() +select f, n order by n desc diff --git a/rust/ql/src/queries/summary/Stats.qll b/rust/ql/src/queries/summary/Stats.qll new file mode 100644 index 000000000000..2f3992303f3b --- /dev/null +++ b/rust/ql/src/queries/summary/Stats.qll @@ -0,0 +1,11 @@ +/** + * Predicates used in summary queries. + */ + +import rust + +int getLinesOfCode() { result = sum(File f | | f.getNumberOfLinesOfCode()) } + +int getLinesOfUserCode() { + result = sum(File f | exists(f.getRelativePath()) | f.getNumberOfLinesOfCode()) +} diff --git a/rust/ql/src/queries/summary/SummaryStats.ql b/rust/ql/src/queries/summary/SummaryStats.ql new file mode 100644 index 000000000000..fe80d2e43121 --- /dev/null +++ b/rust/ql/src/queries/summary/SummaryStats.ql @@ -0,0 +1,17 @@ +/** + * @name Summary Statistics + * @description A table of summary statistics about a database. + * @kind table + * @id rust/summary/summary-statistics + * @tags summary + */ + +import rust +import Stats + +from string key, string value +where + key = "Lines of code" and value = getLinesOfCode().toString() + or + key = "Lines of user code" and value = getLinesOfUserCode().toString() +select key, value diff --git a/rust/ql/test/query-tests/diagnostics/LinesOfCode.expected b/rust/ql/test/query-tests/diagnostics/LinesOfCode.expected new file mode 100644 index 000000000000..cec53d5df7a2 --- /dev/null +++ b/rust/ql/test/query-tests/diagnostics/LinesOfCode.expected @@ -0,0 +1 @@ +| 24 | diff --git a/rust/ql/test/query-tests/diagnostics/LinesOfCode.qlref b/rust/ql/test/query-tests/diagnostics/LinesOfCode.qlref new file mode 100644 index 000000000000..6b283550e622 --- /dev/null +++ b/rust/ql/test/query-tests/diagnostics/LinesOfCode.qlref @@ -0,0 +1 @@ +queries/summary/LinesOfCode.ql diff --git a/rust/ql/test/query-tests/diagnostics/LinesOfUserCode.expected b/rust/ql/test/query-tests/diagnostics/LinesOfUserCode.expected new file mode 100644 index 000000000000..cec53d5df7a2 --- /dev/null +++ b/rust/ql/test/query-tests/diagnostics/LinesOfUserCode.expected @@ -0,0 +1 @@ +| 24 | diff --git a/rust/ql/test/query-tests/diagnostics/LinesOfUserCode.qlref b/rust/ql/test/query-tests/diagnostics/LinesOfUserCode.qlref new file mode 100644 index 000000000000..73feb9152774 --- /dev/null +++ b/rust/ql/test/query-tests/diagnostics/LinesOfUserCode.qlref @@ -0,0 +1 @@ +queries/summary/LinesOfUserCode.ql diff --git a/rust/ql/test/query-tests/diagnostics/LinesOfUserCodeInFiles.expected b/rust/ql/test/query-tests/diagnostics/LinesOfUserCodeInFiles.expected new file mode 100644 index 000000000000..bc80df02e774 --- /dev/null +++ b/rust/ql/test/query-tests/diagnostics/LinesOfUserCodeInFiles.expected @@ -0,0 +1,6 @@ +| my_struct.rs:0:0:0:0 | my_struct.rs | 11 | +| main.rs:0:0:0:0 | main.rs | 4 | +| my_macro.rs:0:0:0:0 | my_macro.rs | 4 | +| does_not_compile.rs:0:0:0:0 | does_not_compile.rs | 3 | +| error.rs:0:0:0:0 | error.rs | 2 | +| lib.rs:0:0:0:0 | lib.rs | 0 | diff --git a/rust/ql/test/query-tests/diagnostics/LinesOfUserCodeInFiles.qlref b/rust/ql/test/query-tests/diagnostics/LinesOfUserCodeInFiles.qlref new file mode 100644 index 000000000000..da3e8f72ed9a --- /dev/null +++ b/rust/ql/test/query-tests/diagnostics/LinesOfUserCodeInFiles.qlref @@ -0,0 +1 @@ +queries/summary/LinesOfUserCodeInFiles.ql diff --git a/rust/ql/test/query-tests/diagnostics/SummaryStats.expected b/rust/ql/test/query-tests/diagnostics/SummaryStats.expected new file mode 100644 index 000000000000..fb589100c48f --- /dev/null +++ b/rust/ql/test/query-tests/diagnostics/SummaryStats.expected @@ -0,0 +1,2 @@ +| Lines of code | 24 | +| Lines of user code | 24 | diff --git a/rust/ql/test/query-tests/diagnostics/SummaryStats.qlref b/rust/ql/test/query-tests/diagnostics/SummaryStats.qlref new file mode 100644 index 000000000000..b94ba40446a0 --- /dev/null +++ b/rust/ql/test/query-tests/diagnostics/SummaryStats.qlref @@ -0,0 +1 @@ +queries/summary/SummaryStats.ql