From b304770cb1b05964a8e77e84ab54416001a6f437 Mon Sep 17 00:00:00 2001 From: Tom Hvitved Date: Fri, 28 Aug 2026 09:30:24 +0200 Subject: [PATCH] Unified: Implement simple LOC counting --- unified/ql/lib/codeql/files/FileSystem.qll | 20 +++++++++++++ .../ql/src/diagnostic/ExtractorInformation.ql | 28 +++++++++++++++++++ 2 files changed, 48 insertions(+) diff --git a/unified/ql/lib/codeql/files/FileSystem.qll b/unified/ql/lib/codeql/files/FileSystem.qll index a50c1cd3432a..897a855612be 100644 --- a/unified/ql/lib/codeql/files/FileSystem.qll +++ b/unified/ql/lib/codeql/files/FileSystem.qll @@ -2,6 +2,7 @@ overlay[local] module; +private import unified private import codeql.Locations private import codeql.util.FileSystem @@ -37,4 +38,23 @@ module 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. + */ + overlay[local?] + int getNumberOfLinesOfCode() { + result = + count(int line | + exists(AstNode node, Location loc | + not node instanceof Comment and + not node instanceof TopLevel and + loc = node.getLocation() and + this = loc.getFile() and + line = loc.getStartLine() and + not loc instanceof EmptyLocation + ) + ) + } } diff --git a/unified/ql/src/diagnostic/ExtractorInformation.ql b/unified/ql/src/diagnostic/ExtractorInformation.ql index 37469504b617..6e52b4fa0887 100644 --- a/unified/ql/src/diagnostic/ExtractorInformation.ql +++ b/unified/ql/src/diagnostic/ExtractorInformation.ql @@ -9,9 +9,37 @@ private import unified private import codeql.unified.internal.AnalysisQuality +predicate fileCount(string key, int value) { + key = "Number of files" and + value = strictcount(File f) +} + +predicate fileCountByExtension(string key, int value) { + exists(string extension | + key = "Number of files with extension " + extension and + value = strictcount(File f | f.getExtension() = extension) + ) +} + +predicate numberOfLinesOfCode(string key, int value) { + key = "Number of lines of code" and + value = strictsum(File f | any() | f.getNumberOfLinesOfCode()) +} + +predicate numberOfLinesOfCodeByExtension(string key, int value) { + exists(string extension | + key = "Number of lines of code with extension " + extension and + value = strictsum(File f | f.getExtension() = extension | f.getNumberOfLinesOfCode()) + ) +} + from string key, float value where ( + fileCount(key, value) or + fileCountByExtension(key, value) or + numberOfLinesOfCode(key, value) or + numberOfLinesOfCodeByExtension(key, value) or StaticNameResolutionStatsReport::keyValuePair(key, value) or FilesCoveredByModuleManifestStatsReport::keyValuePair(key, value) ) and