florence / cover

a code coverage tool for racket
MIT License
38 stars 7 forks source link

Macros and compile time functions marked as uncovered when only used in expansion of defining module #133

Open florence opened 7 years ago

florence commented 7 years ago

A not-well-thought-out brain dump on possible ways to fix:

  1. Do some kind of analysis to guess which compile time functions and macros can't escape the current module, and mark them as ignored.

  2. Try to re-expand the source module, but with it pulling macros from the already expanded module. This would probably involved swapping out the language for one in which cover can control expansion to replace references to macros. This probably won't work at all for macro-defining-macros. This might also much with compile time state.

  3. Do the same language-swapping trick to catch macros and compile time functions as they expand and annotate them live. There are probably a ton of issues with this (like getting the correct requires inserted at the correct time, and not leaking scopes).

  4. The idea case: get some kind of hook into the expander to implement 3 in a better way. (i.e. when the expander attempts to compile/interpret a macro or compile time function, have it use current-compile or call cover's annotator or something).

jackfirth commented 7 years ago

As a temporary workaround, what about a (module/cover body ...) macro that makes a submodule and immediately requires it? So this:

#lang racket
(require rackunit)

(module/cover racket
  (define-syntax (define-one stx)
    (syntax-case stx ()
      [(_ id) #'(begin (define id 1))])))

(define-one id)
(check-equal? id 1)

Would macro expand into this:

#lang racket
(require rackunit)

(module for-coverage racket
  (define-syntax (define-one stx)
    (syntax-case stx ()
      [(_ id) #'(begin (define id 1))]))
  (provide define-one))

(require 'for-coverage)

(define-one id)
(check-equal? id 1)

This would at least automate the drudgery of sticking macros into a different module and requiring them to ensure cover instruments them properly.

florence commented 7 years ago

This won't work unfortunately: the expander runs on a top level module, so cover doesn't get access until all submodules and the top module have been expanded. For coverage to work the macros have to be in a different file.