Skip to content

Commit 3eaee12

Browse files
authored
Merge pull request #17464 from geoffw0/loc
Rust: Add lines-of-code queries
2 parents d1704cf + 7a21b3b commit 3eaee12

14 files changed

+100
-0
lines changed

rust/ql/lib/codeql/files/FileSystem.qll

Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -33,4 +33,19 @@ class Folder = Impl::Folder;
3333
class File extends Container, Impl::File {
3434
/** Holds if this file was extracted from ordinary source code. */
3535
predicate fromSource() { any() }
36+
37+
/**
38+
* Gets the number of lines containing code in this file. This value
39+
* is approximate.
40+
*/
41+
int getNumberOfLinesOfCode() {
42+
result =
43+
count(int line |
44+
exists(Location loc |
45+
loc.getFile() = this and
46+
line = [loc.getStartLine(), loc.getEndLine()] and
47+
not loc instanceof EmptyLocation
48+
)
49+
)
50+
}
3651
}
Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,14 @@
1+
/**
2+
* @name Total lines of Rust code in the database
3+
* @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.
4+
* @kind metric
5+
* @id rust/summary/lines-of-code
6+
* @tags summary
7+
* lines-of-code
8+
* telemetry
9+
*/
10+
11+
import rust
12+
import Stats
13+
14+
select getLinesOfCode()
Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,14 @@
1+
/**
2+
* @name Total lines of user written Rust code in the database
3+
* @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.
4+
* @kind metric
5+
* @id rust/summary/lines-of-user-code
6+
* @tags summary
7+
* lines-of-code
8+
* debug
9+
*/
10+
11+
import rust
12+
import Stats
13+
14+
select getLinesOfUserCode()
Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,15 @@
1+
/**
2+
* @name Lines of user code in files
3+
* @description Measures the number of lines of code in each file from the source directory, ignoring lines that contain only comments or whitespace.
4+
* @kind metric
5+
* @id rust/summary/lines-of-user-code-in-files
6+
* @metricType file
7+
*/
8+
9+
import rust
10+
11+
from File f, int n
12+
where
13+
exists(f.getRelativePath()) and
14+
n = f.getNumberOfLinesOfCode()
15+
select f, n order by n desc

rust/ql/src/queries/summary/Stats.qll

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,11 @@
1+
/**
2+
* Predicates used in summary queries.
3+
*/
4+
5+
import rust
6+
7+
int getLinesOfCode() { result = sum(File f | | f.getNumberOfLinesOfCode()) }
8+
9+
int getLinesOfUserCode() {
10+
result = sum(File f | exists(f.getRelativePath()) | f.getNumberOfLinesOfCode())
11+
}
Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,17 @@
1+
/**
2+
* @name Summary Statistics
3+
* @description A table of summary statistics about a database.
4+
* @kind table
5+
* @id rust/summary/summary-statistics
6+
* @tags summary
7+
*/
8+
9+
import rust
10+
import Stats
11+
12+
from string key, string value
13+
where
14+
key = "Lines of code" and value = getLinesOfCode().toString()
15+
or
16+
key = "Lines of user code" and value = getLinesOfUserCode().toString()
17+
select key, value
Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
| 24 |
Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
queries/summary/LinesOfCode.ql
Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
| 24 |
Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
queries/summary/LinesOfUserCode.ql

0 commit comments

Comments
 (0)