Function: bazel--parse-coverage

bazel--parse-coverage is a natively compiled function defined in bazel.el.

Signature

(bazel--parse-coverage ROOT COVERAGE)

Documentation

Parse coverage information in the current buffer.

ROOT is the Bazel repository root directory. COVERAGE is a hashtable that maps buffers to hashtables that in turn map line numbers ‘bazel--coverage’ structures. The function walks over the coverage information in the current buffer and fills in COVERAGE.

Source Code

;; Defined in /nix/store/ws8pkszjvcad1rgzqw6wja7qxacylfhn-emacs-packages-deps/share/emacs/site-lisp/elpa/bazel-20261002.1510/bazel.el
(defun bazel--parse-coverage (root coverage)
  "Parse coverage information in the current buffer.
ROOT is the Bazel repository root directory.  COVERAGE is a
hashtable that maps buffers to hashtables that in turn map line
numbers ‘bazel--coverage’ structures.  The function walks over
the coverage information in the current buffer and fills in
COVERAGE."
  (declare (ftype (function (string hashtable) null)))
  ;; See the manual page of ‘geninfo’ for a description of the coverage format.
  (let ((case-fold-search nil)
        (search-spaces-regexp nil))
    (while (re-search-forward (rx bol "SF:" (group (+ nonl)) eol) nil t)
      (let ((begin (line-beginning-position 2))
            (file (expand-file-name (match-string-no-properties 1) root))
            ;; DATA is a hashtable mapping line numbers to ‘bazel--coverage’
            ;; structures for the current file.  It’s initialized lazily.
            (data nil))
        (when-let* ((end (re-search-forward (rx bol "end_of_record" eol) nil t))
                    ;; Only collect coverage for files that are visited in some
                    ;; buffer.
                    (buffer (find-buffer-visiting file)))
          ;; Collect line coverage data.
          (goto-char begin)
          (while (re-search-forward (rx bol "DA:" (group (+ digit)) ?,
                                        (group (+ digit)) (? ?, (+ nonl)) eol)
                                    end t)
            (let ((line (bazel--match-natnum 1))
                  (hits (bazel--match-natnum 2)))
              (setq data (or (gethash buffer coverage)
                             (puthash buffer (make-hash-table :test #'eql)
                                      coverage)))
              (if-let* ((info (gethash line data)))
                  (cl-incf (bazel--coverage-hits info) hits)
                (puthash line (bazel--make-coverage :hits hits) data))))
          ;; Collect branch coverage data, but only if we have any coverage data
          ;; for this buffer.
          (when data
            (goto-char begin)
            (while (re-search-forward (rx bol "BRDA:"
                                          (group (+ digit)) ?, ; line number
                                          (group (+ digit)) ?, ; block number
                                          (group (+ digit)) ?, ; branch number
                                          (or ?- (group (+ digit))) ; taken
                                          eol)
                                      end t)
              (let* ((line (bazel--match-natnum 1))
                     (block (bazel--match-natnum 2))
                     (branch (bazel--match-natnum 3))
                     (executedp (match-beginning 4))
                     (taken (and executedp (bazel--match-natnum 4))))
                ;; Ignore branch coverage for uncovered lines.
                (when-let* ((coverage (gethash line data)))
                  (let* ((blocks (or (bazel--coverage-blocks coverage)
                                     (setf (bazel--coverage-blocks coverage)
                                           (make-hash-table :test #'eql))))
                         (branches (or (gethash block blocks)
                                       (puthash block
                                                (make-hash-table :test #'eql)
                                                blocks)))
                         (previous (gethash branch branches))
                         (new (if (and previous taken)
                                  (+ previous taken)
                                (or previous taken))))
                    (puthash branch new branches))))))
          (goto-char end)))))
  nil)