Merge pull request #17464 from geoffw0/loc

Rust: Add lines-of-code queries
This commit is contained in:
Paolo Tranquilli
2024-09-16 16:47:12 +02:00
committed by GitHub
14 changed files with 100 additions and 0 deletions

View File

@@ -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
)
)
}
}

View 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()

View 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()

View 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

View 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())
}

View 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

View File

@@ -0,0 +1 @@
| 24 |

View File

@@ -0,0 +1 @@
queries/summary/LinesOfCode.ql

View File

@@ -0,0 +1 @@
| 24 |

View File

@@ -0,0 +1 @@
queries/summary/LinesOfUserCode.ql

View File

@@ -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 |

View File

@@ -0,0 +1 @@
queries/summary/LinesOfUserCodeInFiles.ql

View File

@@ -0,0 +1,2 @@
| Lines of code | 24 |
| Lines of user code | 24 |

View File

@@ -0,0 +1 @@
queries/summary/SummaryStats.ql