Commit 2c00dea1 authored by Russ Cox's avatar Russ Cox

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/77150Reviewed-by: default avatarIan Lance Taylor <iant@golang.org>
parent c2e26fad
This diff is collapsed.
......@@ -6,6 +6,7 @@ package main_test
import (
"bytes"
"flag"
"fmt"
"go/ast"
"go/parser"
......@@ -37,7 +38,7 @@ var (
coverProfile = filepath.Join(testdata, "profile.cov")
)
var debug = false // Keeps the rewritten files around if set.
var debug = flag.Bool("debug", false, "keep rewritten files for debugging")
// Run this shell script, but do it in Go so it can be run by "go test".
//
......@@ -63,7 +64,7 @@ func TestCover(t *testing.T) {
}
// defer removal of test_line.go
if !debug {
if !*debug {
defer os.Remove(coverInput)
}
......@@ -79,7 +80,7 @@ func TestCover(t *testing.T) {
run(cmd, t)
// defer removal of ./testdata/test_cover.go
if !debug {
if !*debug {
defer os.Remove(coverOutput)
}
......@@ -100,10 +101,10 @@ func TestCover(t *testing.T) {
t.Error("'go:linkname' compiler directive not found")
}
// No other comments should be present in generated code.
c := ".*// This comment shouldn't appear in generated go code.*"
if got, err := regexp.MatchString(c, string(file)); err != nil || got {
t.Errorf("non compiler directive comment %q found", c)
// Other comments should be preserved too.
c := ".*// This comment didn't appear in generated go code.*"
if got, err := regexp.MatchString(c, string(file)); err != nil || !got {
t.Errorf("non compiler directive comment %q not found", c)
}
}
......
......@@ -282,7 +282,7 @@ loop:
}
}
// This comment shouldn't appear in generated go code.
// This comment didn't appear in generated go code.
func haha() {
// Needed for cover to add counter increment here.
_ = 42
......
// Copyright 2017 The Go Authors. All rights reserved.
// Use of this source code is governed by a BSD-style
// license that can be found in the LICENSE file.
// Package edit implements buffered position-based editing of byte slices.
package edit
import (
"fmt"
"sort"
)
// A Buffer is a queue of edits to apply to a given byte slice.
type Buffer struct {
old []byte
q edits
}
// An edit records a single text modification: change the bytes in [start,end) to new.
type edit struct {
start int
end int
new string
}
// An edits is a list of edits that is sortable by start offset, breaking ties by end offset.
type edits []edit
func (x edits) Len() int { return len(x) }
func (x edits) Swap(i, j int) { x[i], x[j] = x[j], x[i] }
func (x edits) Less(i, j int) bool {
if x[i].start != x[j].start {
return x[i].start < x[j].start
}
return x[i].end < x[j].end
}
// NewBuffer returns a new buffer to accumulate changes to an initial data slice.
// The returned buffer maintains a reference to the data, so the caller must ensure
// the data is not modified until after the Buffer is done being used.
func NewBuffer(data []byte) *Buffer {
return &Buffer{old: data}
}
func (b *Buffer) Insert(pos int, new string) {
if pos < 0 || pos > len(b.old) {
panic("invalid edit position")
}
b.q = append(b.q, edit{pos, pos, new})
}
func (b *Buffer) Delete(start, end int) {
if end < start || start < 0 || end > len(b.old) {
panic("invalid edit position")
}
b.q = append(b.q, edit{start, end, ""})
}
func (b *Buffer) Replace(start, end int, new string) {
if end < start || start < 0 || end > len(b.old) {
panic("invalid edit position")
}
b.q = append(b.q, edit{start, end, new})
}
// Bytes returns a new byte slice containing the original data
// with the queued edits applied.
func (b *Buffer) Bytes() []byte {
// Sort edits by starting position and then by ending position.
// Breaking ties by ending position allows insertions at point x
// to be applied before a replacement of the text at [x, y).
sort.Stable(b.q)
var new []byte
offset := 0
for i, e := range b.q {
if e.start < offset {
e0 := b.q[i-1]
panic(fmt.Sprintf("overlapping edits: [%d,%d)->%q, [%d,%d)->%q", e0.start, e0.end, e0.new, e.start, e.end, e.new))
}
new = append(new, b.old[offset:e.start]...)
offset = e.end
new = append(new, e.new...)
}
new = append(new, b.old[offset:]...)
return new
}
// String returns a string containing the original data
// with the queued edits applied.
func (b *Buffer) String() string {
return string(b.Bytes())
}
// Copyright 2017 The Go Authors. All rights reserved.
// Use of this source code is governed by a BSD-style
// license that can be found in the LICENSE file.
package edit
import "testing"
func TestEdit(t *testing.T) {
b := NewBuffer([]byte("0123456789"))
b.Insert(8, ",7½,")
b.Replace(9, 10, "the-end")
b.Insert(10, "!")
b.Insert(4, "3.14,")
b.Insert(4, "π,")
b.Insert(4, "3.15,")
b.Replace(3, 4, "three,")
want := "012three,3.14,π,3.15,4567,7½,8the-end!"
s := b.String()
if s != want {
t.Errorf("b.String() = %q, want %q", s, want)
}
sb := b.Bytes()
if string(sb) != want {
t.Errorf("b.Bytes() = %q, want %q", sb, want)
}
}
Markdown is supported
0%
or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment