Skip to content

Commit a859702

Browse files
committed
fixed logic for line counting
1 parent bcc1be0 commit a859702

1 file changed

Lines changed: 12 additions & 34 deletions

File tree

ql/src/codeql/files/FileSystem.qll

Lines changed: 12 additions & 34 deletions
Original file line numberDiff line numberDiff line change
@@ -169,47 +169,25 @@ class File extends Container, @file {
169169
/** Gets the URL of this file. */
170170
override string getURL() { result = "file://" + this.getAbsolutePath() + ":0:0:0:0" }
171171

172-
/** Gets the number of lines in this file. */
173-
int getNumberOfLines() { result = max(this.getAToken().getLocation().getEndLine()) }
174-
175172
/** Get a token in this file. */
176173
private Generated::Token getAToken() { result.getLocation().getFile() = this }
177174

178-
/** Get a comment token in this file. */
179-
private Generated::Token getACommentToken() { result = this.getAToken() and result instanceof @token_comment }
180-
181-
/** Get a non-comment token in this file. */
182-
private Generated::Token getANonCommentToken() {
183-
result = this.getAToken() and not result instanceof @token_comment
175+
/** Holds if `line` contains a token. */
176+
private predicate line(int line, boolean comment) {
177+
exists(Generated::Token token, Location l |
178+
token = this.getAToken() and
179+
l = token.getLocation() and
180+
line in [l.getStartLine() .. l.getEndLine()] and
181+
if token instanceof @token_comment then comment = true else comment = false
182+
)
184183
}
185184

186-
private predicate isTokenStartingAtLine(Generated::Token t, int startLine) {
187-
t = this.getANonCommentToken() and
188-
t.getLocation().getStartLine() = startLine
189-
}
185+
/** Gets the number of lines in this file. */
186+
int getNumberOfLines() { result = max(this.getAToken().getLocation().getEndLine()) }
190187

191188
/** Gets the number of lines of code in this file. */
192-
int getNumberOfLinesOfCode() {
193-
/*
194-
* For each line with at least one non-comment token, select a token with
195-
* the greatest length in terms of lines and sum all lengths per startLine.
196-
*/
197-
198-
result =
199-
sum(int startLine, int numLines |
200-
exists(Generated::Token t |
201-
this.isTokenStartingAtLine(t, startLine) and
202-
t.getLocation().getNumLines() = numLines and
203-
not exists(Generated::Token t2 |
204-
this.isTokenStartingAtLine(t2, startLine) and
205-
t2.getLocation().getNumLines() > numLines
206-
)
207-
)
208-
|
209-
numLines
210-
)
211-
}
189+
int getNumberOfLinesOfCode() { result = count(int line | this.line(line, false)) }
212190

213191
/** Gets the number of lines of comments in this file. */
214-
int getNumberOfLinesOfComments() { result = count(this.getACommentToken()) }
192+
int getNumberOfLinesOfComments() { result = count(int line | this.line(line, true)) }
215193
}

0 commit comments

Comments
 (0)