Skip to content

Commit

Permalink
Merge pull request github#17464 from geoffw0/loc
Browse files Browse the repository at this point in the history
Rust: Add lines-of-code queries
  • Loading branch information
redsun82 authored Sep 16, 2024
2 parents d1704cf + 7a21b3b commit 3eaee12
Show file tree
Hide file tree
Showing 14 changed files with 100 additions and 0 deletions.
15 changes: 15 additions & 0 deletions rust/ql/lib/codeql/files/FileSystem.qll
Original file line number Diff line number Diff line change
Expand Up @@ -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
)
)
}
}
14 changes: 14 additions & 0 deletions rust/ql/src/queries/summary/LinesOfCode.ql
Original file line number Diff line number Diff line change
@@ -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()
14 changes: 14 additions & 0 deletions rust/ql/src/queries/summary/LinesOfUserCode.ql
Original file line number Diff line number Diff line change
@@ -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()
15 changes: 15 additions & 0 deletions rust/ql/src/queries/summary/LinesOfUserCodeInFiles.ql
Original file line number Diff line number Diff line change
@@ -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
11 changes: 11 additions & 0 deletions rust/ql/src/queries/summary/Stats.qll
Original file line number Diff line number Diff line change
@@ -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())
}
17 changes: 17 additions & 0 deletions rust/ql/src/queries/summary/SummaryStats.ql
Original file line number Diff line number Diff line change
@@ -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
1 change: 1 addition & 0 deletions rust/ql/test/query-tests/diagnostics/LinesOfCode.expected
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
| 24 |
1 change: 1 addition & 0 deletions rust/ql/test/query-tests/diagnostics/LinesOfCode.qlref
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
queries/summary/LinesOfCode.ql
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
| 24 |
1 change: 1 addition & 0 deletions rust/ql/test/query-tests/diagnostics/LinesOfUserCode.qlref
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
queries/summary/LinesOfUserCode.ql
Original file line number Diff line number Diff line change
@@ -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 |
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
queries/summary/LinesOfUserCodeInFiles.ql
2 changes: 2 additions & 0 deletions rust/ql/test/query-tests/diagnostics/SummaryStats.expected
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
| Lines of code | 24 |
| Lines of user code | 24 |
1 change: 1 addition & 0 deletions rust/ql/test/query-tests/diagnostics/SummaryStats.qlref
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
queries/summary/SummaryStats.ql

0 comments on commit 3eaee12

Please sign in to comment.