Skip to content

Commit b3447f9

Browse files
committed
Ensure compatibility with Emacs 24.4+, better match for object files
1 parent 44c2f0b commit b3447f9

File tree

1 file changed

+46
-34
lines changed

1 file changed

+46
-34
lines changed

cov.el

Lines changed: 46 additions & 34 deletions
Original file line numberDiff line numberDiff line change
@@ -316,21 +316,33 @@ that filename. Otherwise search for the first matching pattern in
316316
"Locate a gcov coverage file from FILE-DIR for FILE-NAME.
317317
This works with CMake-based projects, by constructing the path to the `.gcov'
318318
file from object file's path extracted from the \"compile_commands.json\"."
319-
(when-let* ((root (project-root (project-current)))
320-
(compile-commands (expand-file-name "compile_commands.json" root)))
321-
(when (file-exists-p compile-commands)
322-
(when-let* ((file-cmd (cl-find-if
323-
(lambda (entry)
324-
(equal (file-truename (alist-get 'file entry))
325-
(expand-file-name file-name file-dir)))
326-
(json-read-file compile-commands)))
327-
(command (alist-get 'command file-cmd))
328-
(directory (alist-get 'directory file-cmd)))
329-
(let* ((obj-file (when (string-match
330-
(concat "-o \\(?1:.*" (regexp-quote (concat file-name ".o")) "\\)")
331-
command)
332-
(match-string-no-properties 1 command))))
333-
(cons (expand-file-name (file-name-with-extension obj-file ".gcov") directory) 'gcov))))))
319+
(let* ((root (project-root (project-current)))
320+
(compile-commands (and root (expand-file-name "compile_commands.json" root))))
321+
(when (and compile-commands (file-exists-p compile-commands))
322+
(let* ((file-cmd (cl-find-if
323+
(lambda (entry)
324+
(equal (file-truename (cdr (assq 'file entry)))
325+
(expand-file-name file-name file-dir)))
326+
(json-read-file compile-commands)))
327+
(command (cdr (assq 'command file-cmd)))
328+
(directory (cdr (assq 'directory file-cmd))))
329+
(when file-cmd
330+
;; Supposing file-name is "main.cpp", we will construct a regexp that matches "main.cpp.o" or "main.o".
331+
(let* ((obj-file (when (string-match
332+
(concat
333+
" -o *\\(?1:.*"
334+
(regexp-quote (file-name-sans-extension file-name))
335+
"\\(?:\\."
336+
(file-name-extension file-name)
337+
"\\)?"
338+
"\\.o\\)")
339+
command)
340+
(match-string-no-properties 1 command)))
341+
(gcov-file (expand-file-name (file-name-with-extension obj-file ".gcov") directory)))
342+
;; If file doesn't exists, return nil, so we can try other paths/functions
343+
;; from `cov-coverage-file-paths'
344+
(when (file-exists-p gcov-file)
345+
(cons gcov-file 'gcov))))))))
334346

335347
(defun cov--locate-coveralls (file-dir _file-name)
336348
"Locate coveralls coverage from FILE-DIR for FILE-NAME.
@@ -373,17 +385,17 @@ Read from `current-buffer' if BUFFER is nil. Return a list
373385
`((FILE . ((LINE-NUM TIMES-RAN) ...)))'. Unused lines (TIMES-RAN
374386
'-') are filtered out."
375387
(with-current-buffer (or buffer (current-buffer))
376-
;; The buffer is _not_ automatically widened. It is possible to
377-
;; read just a portion of the buffer by narrowing it first.
378-
(let ((line-re (rx line-start
379-
;; note the group numbers are in reverse order
380-
;; in the first alternative
381-
(or (seq (* blank) (group-n 2 (+ (in digit ?#))) ?:
382-
(* blank) (group-n 1 (+ digit)) ?:)
383-
(seq "lcount:" (group-n 1 (+ digit)) ?, (group-n 2 (+ digit))))))
384-
;; Derive the name of the covered file from the filename of
385-
;; the coverage file.
386-
(filename (file-name-sans-extension (f-filename cov-coverage-file))))
388+
;; The buffer is _not_ automatically widened. It is possible to
389+
;; read just a portion of the buffer by narrowing it first.
390+
(let ((line-re (rx line-start
391+
;; note the group numbers are in reverse order
392+
;; in the first alternative
393+
(or (seq (* blank) (group-n 2 (+ (in digit ?#))) ?:
394+
(* blank) (group-n 1 (+ digit)) ?:)
395+
(seq "lcount:" (group-n 1 (+ digit)) ?, (group-n 2 (+ digit))))))
396+
;; Derive the name of the covered file from the filename of
397+
;; the coverage file.
398+
(filename (file-name-sans-extension (f-filename cov-coverage-file))))
387399
;; Replace the filename with the one from the file preamble
388400
;; `Source' tag or `file' line in the intermediate format.
389401
;; TODO: The intermediate format actually support multiple
@@ -405,14 +417,14 @@ Read from `current-buffer' if BUFFER is nil. Return a list
405417
(file-truename
406418
(expand-file-name (match-string 1)
407419
(file-name-directory cov-coverage-file))))))))
408-
(save-excursion
409-
(save-match-data
410-
(goto-char (point-min))
411-
(list (cons filename
412-
(cl-loop
413-
while (re-search-forward line-re nil t)
414-
collect (list (string-to-number (match-string 1))
415-
(string-to-number (match-string 2)))))))))))
420+
(save-excursion
421+
(save-match-data
422+
(goto-char (point-min))
423+
(list (cons filename
424+
(cl-loop
425+
while (re-search-forward line-re nil t)
426+
collect (list (string-to-number (match-string 1))
427+
(string-to-number (match-string 2)))))))))))
416428

417429
(defconst cov--lcov-prefix-re
418430
(rx line-start

0 commit comments

Comments
 (0)