Skip to content

Commit f9b4c7b

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

File tree

1 file changed

+47
-34
lines changed

1 file changed

+47
-34
lines changed

cov.el

Lines changed: 47 additions & 34 deletions
Original file line numberDiff line numberDiff line change
@@ -316,21 +316,34 @@ 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* ((case-fold-search nil)
332+
(obj-file (when (string-match
333+
(concat
334+
" -o[ ]*\\(?1:.*"
335+
(regexp-quote (file-name-sans-extension file-name))
336+
"\\(?:\\."
337+
(file-name-extension file-name)
338+
"\\)?"
339+
"\\.o\\)")
340+
command)
341+
(match-string-no-properties 1 command)))
342+
(gcov-file (expand-file-name (file-name-with-extension obj-file ".gcov") directory)))
343+
;; If file doesn't exists, return nil, so we can try other paths/functions
344+
;; from `cov-coverage-file-paths'
345+
(when (file-exists-p gcov-file)
346+
(cons gcov-file 'gcov))))))))
334347

335348
(defun cov--locate-coveralls (file-dir _file-name)
336349
"Locate coveralls coverage from FILE-DIR for FILE-NAME.
@@ -373,17 +386,17 @@ Read from `current-buffer' if BUFFER is nil. Return a list
373386
`((FILE . ((LINE-NUM TIMES-RAN) ...)))'. Unused lines (TIMES-RAN
374387
'-') are filtered out."
375388
(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))))
389+
;; The buffer is _not_ automatically widened. It is possible to
390+
;; read just a portion of the buffer by narrowing it first.
391+
(let ((line-re (rx line-start
392+
;; note the group numbers are in reverse order
393+
;; in the first alternative
394+
(or (seq (* blank) (group-n 2 (+ (in digit ?#))) ?:
395+
(* blank) (group-n 1 (+ digit)) ?:)
396+
(seq "lcount:" (group-n 1 (+ digit)) ?, (group-n 2 (+ digit))))))
397+
;; Derive the name of the covered file from the filename of
398+
;; the coverage file.
399+
(filename (file-name-sans-extension (f-filename cov-coverage-file))))
387400
;; Replace the filename with the one from the file preamble
388401
;; `Source' tag or `file' line in the intermediate format.
389402
;; TODO: The intermediate format actually support multiple
@@ -405,14 +418,14 @@ Read from `current-buffer' if BUFFER is nil. Return a list
405418
(file-truename
406419
(expand-file-name (match-string 1)
407420
(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)))))))))))
421+
(save-excursion
422+
(save-match-data
423+
(goto-char (point-min))
424+
(list (cons filename
425+
(cl-loop
426+
while (re-search-forward line-re nil t)
427+
collect (list (string-to-number (match-string 1))
428+
(string-to-number (match-string 2)))))))))))
416429

417430
(defconst cov--lcov-prefix-re
418431
(rx line-start

0 commit comments

Comments
 (0)