aboutsummaryrefslogtreecommitdiff
path: root/src/cmd/internal/edit
diff options
context:
space:
mode:
authorRuss Cox <rsc@golang.org>2017-11-09 23:39:12 -0500
committerRuss Cox <rsc@golang.org>2017-11-13 14:59:26 +0000
commit2c00dea18b4a62fbf0bc11c85e4d9b91a314381f (patch)
tree273598fed266b779c5e70ab76840e964ad0d811b /src/cmd/internal/edit
parentc2e26fad3ba0d6cd83366c15b9969272eb3004f4 (diff)
downloadgo-2c00dea18b4a62fbf0bc11c85e4d9b91a314381f.tar.xz
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>
Diffstat (limited to 'src/cmd/internal/edit')
-rw-r--r--src/cmd/internal/edit/edit.go93
-rw-r--r--src/cmd/internal/edit/edit_test.go28
2 files changed, 121 insertions, 0 deletions
diff --git a/src/cmd/internal/edit/edit.go b/src/cmd/internal/edit/edit.go
new file mode 100644
index 0000000000..2d470f4c8a
--- /dev/null
+++ b/src/cmd/internal/edit/edit.go
@@ -0,0 +1,93 @@
+// 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())
+}
diff --git a/src/cmd/internal/edit/edit_test.go b/src/cmd/internal/edit/edit_test.go
new file mode 100644
index 0000000000..0e0c564d98
--- /dev/null
+++ b/src/cmd/internal/edit/edit_test.go
@@ -0,0 +1,28 @@
+// 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)
+ }
+}