Skip to content

Commit

Permalink
cmd/cover: modify source as text, not as AST
Browse files Browse the repository at this point in the history
cmd/cover rewrites Go source code to add coverage annotations.
The approach to date has been to parse the code to AST, analyze it,
rewrite the AST, and print it back out. This approach fails to preserve
line numbers in the original code and has a very difficult time with
comments, because go/printer does as well.

This CL changes cmd/cover to decide what to modify based on the
AST but to apply the modifications as purely textual substitutions.
In this way, cmd/cover can be sure it never adds or removes a newline
character, nor a comment, so all line numbers and comments are
preserved.

This also allows us to emit a single //line comment at the beginning
of the translated file and have the compiler report errors with
correct line numbers in the original file.

Fixes #6329.
Fixes #15757.

Change-Id: Ia95f6f894bb498e80d1f91fde56cd4a8009d7f9b
Reviewed-on: https://go-review.googlesource.com/77150
Reviewed-by: Ian Lance Taylor <iant@golang.org>
  • Loading branch information
rsc committed Nov 13, 2017
1 parent c2e26fa commit 2c00dea
Show file tree
Hide file tree
Showing 5 changed files with 216 additions and 263 deletions.
Loading

0 comments on commit 2c00dea

Please sign in to comment.