Skip to content

Commit 5727d49

Browse files
committed
Swift: Take out common code for lines of code.
1 parent a3ef5c6 commit 5727d49

File tree

3 files changed

+17
-15
lines changed

3 files changed

+17
-15
lines changed

swift/ql/lib/codeql/swift/elements/File.qll

Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,6 @@
11
private import codeql.swift.generated.File
2+
private import codeql.swift.elements.Location
3+
private import codeql.swift.elements.UnknownLocation
24

35
class File extends Generated::File {
46
/** toString */
@@ -17,4 +19,17 @@ class File extends Generated::File {
1719
string getBaseName() {
1820
result = this.getAbsolutePath().regexpCapture(".*/(([^/]*?)(?:\\.([^.]*))?)", 1)
1921
}
22+
23+
/**
24+
* Gets the number of lines containing code in this file. This value
25+
* is approximate.
26+
*/
27+
int getNumberOfLinesOfCode() {
28+
result =
29+
count(int line |
30+
exists(Location loc |
31+
not loc instanceof UnknownLocation and loc.getFile() = this and loc.getStartLine() = line
32+
)
33+
)
34+
}
2035
}

swift/ql/src/diagnostics/SuccessfullyExtractedLines.ql

Lines changed: 1 addition & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -8,8 +8,4 @@
88

99
import swift
1010

11-
select count(File f, int line |
12-
exists(Location loc |
13-
not loc instanceof UnknownLocation and loc.getFile() = f and loc.getStartLine() = line
14-
)
15-
)
11+
select sum(File f | | f.getNumberOfLinesOfCode())

swift/ql/src/queries/Summary/SummaryStats.ql

Lines changed: 1 addition & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -12,15 +12,6 @@ import codeql.swift.security.SensitiveExprs
1212
import codeql.swift.dataflow.DataFlow
1313
import codeql.swift.dataflow.TaintTracking
1414

15-
int linesOfCode() {
16-
// approximate number of lines of code in the database
17-
result = count(File f, int line |
18-
exists(Location loc |
19-
not loc instanceof UnknownLocation and loc.getFile() = f and loc.getStartLine() = line
20-
)
21-
)
22-
}
23-
2415
/**
2516
* A taint configuration for tainted data reaching any node.
2617
*/
@@ -46,7 +37,7 @@ float taintReach() { result = (taintedNodesCount() * 1000000.0) / count(DataFlow
4637
predicate statistic(string what, string value) {
4738
what = "Files" and value = count(File f).toString()
4839
or
49-
what = "Lines of code" and value = linesOfCode().toString()
40+
what = "Lines of code" and value = sum(File f | | f.getNumberOfLinesOfCode()).toString()
5041
or
5142
what = "Compiler errors" and value = count(CompilerError d).toString()
5243
or

0 commit comments

Comments
 (0)