]> Cypherpunks repositories - gostls13.git/commit
cmd/cover: modify source as text, not as AST
authorRuss Cox <rsc@golang.org>
Fri, 10 Nov 2017 04:39:12 +0000 (23:39 -0500)
committerRuss Cox <rsc@golang.org>
Mon, 13 Nov 2017 14:59:26 +0000 (14:59 +0000)
commit2c00dea18b4a62fbf0bc11c85e4d9b91a314381f
tree273598fed266b779c5e70ab76840e964ad0d811b
parentc2e26fad3ba0d6cd83366c15b9969272eb3004f4
cmd/cover: modify source as text, not as AST

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>
src/cmd/cover/cover.go
src/cmd/cover/cover_test.go
src/cmd/cover/testdata/test.go
src/cmd/internal/edit/edit.go [new file with mode: 0644]
src/cmd/internal/edit/edit_test.go [new file with mode: 0644]