mirror of
https://github.com/github/codeql.git
synced 2026-04-28 10:15:14 +02:00
Merge pull request #17464 from geoffw0/loc
Rust: Add lines-of-code queries
This commit is contained in:
@@ -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
rust/ql/src/queries/summary/LinesOfCode.ql
Normal file
14
rust/ql/src/queries/summary/LinesOfCode.ql
Normal file
@@ -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
rust/ql/src/queries/summary/LinesOfUserCode.ql
Normal file
14
rust/ql/src/queries/summary/LinesOfUserCode.ql
Normal file
@@ -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
rust/ql/src/queries/summary/LinesOfUserCodeInFiles.ql
Normal file
15
rust/ql/src/queries/summary/LinesOfUserCodeInFiles.ql
Normal file
@@ -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
rust/ql/src/queries/summary/Stats.qll
Normal file
11
rust/ql/src/queries/summary/Stats.qll
Normal file
@@ -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
rust/ql/src/queries/summary/SummaryStats.ql
Normal file
17
rust/ql/src/queries/summary/SummaryStats.ql
Normal file
@@ -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
|
||||
@@ -0,0 +1 @@
|
||||
| 24 |
|
||||
1
rust/ql/test/query-tests/diagnostics/LinesOfCode.qlref
Normal file
1
rust/ql/test/query-tests/diagnostics/LinesOfCode.qlref
Normal file
@@ -0,0 +1 @@
|
||||
queries/summary/LinesOfCode.ql
|
||||
@@ -0,0 +1 @@
|
||||
| 24 |
|
||||
@@ -0,0 +1 @@
|
||||
queries/summary/LinesOfUserCode.ql
|
||||
@@ -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 |
|
||||
@@ -0,0 +1 @@
|
||||
queries/summary/LinesOfUserCodeInFiles.ql
|
||||
@@ -0,0 +1,2 @@
|
||||
| Lines of code | 24 |
|
||||
| Lines of user code | 24 |
|
||||
1
rust/ql/test/query-tests/diagnostics/SummaryStats.qlref
Normal file
1
rust/ql/test/query-tests/diagnostics/SummaryStats.qlref
Normal file
@@ -0,0 +1 @@
|
||||
queries/summary/SummaryStats.ql
|
||||
Reference in New Issue
Block a user