Skip to content

Commit 3ee0030

Browse files
authored
Merge pull request #22456 from hvitved/unified/loc-count
Unified: Implement simple LOC counting
2 parents 2820e04 + b304770 commit 3ee0030

2 files changed

Lines changed: 48 additions & 0 deletions

File tree

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

Lines changed: 20 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,7 @@
22
overlay[local]
33
module;
44

5+
private import unified
56
private import codeql.Locations
67
private import codeql.util.FileSystem
78

@@ -37,4 +38,23 @@ module Folder = Impl::Folder;
3738
class File extends Container, Impl::File {
3839
/** Holds if this file was extracted from ordinary source code. */
3940
predicate fromSource() { any() }
41+
42+
/**
43+
* Gets the number of lines containing code in this file. This value
44+
* is approximate.
45+
*/
46+
overlay[local?]
47+
int getNumberOfLinesOfCode() {
48+
result =
49+
count(int line |
50+
exists(AstNode node, Location loc |
51+
not node instanceof Comment and
52+
not node instanceof TopLevel and
53+
loc = node.getLocation() and
54+
this = loc.getFile() and
55+
line = loc.getStartLine() and
56+
not loc instanceof EmptyLocation
57+
)
58+
)
59+
}
4060
}

unified/ql/src/diagnostic/ExtractorInformation.ql

Lines changed: 28 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -9,9 +9,37 @@
99
private import unified
1010
private import codeql.unified.internal.AnalysisQuality
1111

12+
predicate fileCount(string key, int value) {
13+
key = "Number of files" and
14+
value = strictcount(File f)
15+
}
16+
17+
predicate fileCountByExtension(string key, int value) {
18+
exists(string extension |
19+
key = "Number of files with extension " + extension and
20+
value = strictcount(File f | f.getExtension() = extension)
21+
)
22+
}
23+
24+
predicate numberOfLinesOfCode(string key, int value) {
25+
key = "Number of lines of code" and
26+
value = strictsum(File f | any() | f.getNumberOfLinesOfCode())
27+
}
28+
29+
predicate numberOfLinesOfCodeByExtension(string key, int value) {
30+
exists(string extension |
31+
key = "Number of lines of code with extension " + extension and
32+
value = strictsum(File f | f.getExtension() = extension | f.getNumberOfLinesOfCode())
33+
)
34+
}
35+
1236
from string key, float value
1337
where
1438
(
39+
fileCount(key, value) or
40+
fileCountByExtension(key, value) or
41+
numberOfLinesOfCode(key, value) or
42+
numberOfLinesOfCodeByExtension(key, value) or
1543
StaticNameResolutionStatsReport::keyValuePair(key, value) or
1644
FilesCoveredByModuleManifestStatsReport::keyValuePair(key, value)
1745
) and

0 commit comments

Comments
 (0)