aboutsummaryrefslogtreecommitdiff
path: root/src/internal/coverage/stringtab/stringtab.go
diff options
context:
space:
mode:
authorThan McIntosh <thanm@google.com>2021-10-28 12:24:31 -0400
committerThan McIntosh <thanm@google.com>2022-09-26 21:48:18 +0000
commitefa3f1749fabe3b3062edff7651baa8cb1f574ab (patch)
treef44441fac23b9ef0aaeaca09b03cdbec32dbcc82 /src/internal/coverage/stringtab/stringtab.go
parent63668a5155c808f474d5adab676ef3f3aa50c1d3 (diff)
downloadgo-efa3f1749fabe3b3062edff7651baa8cb1f574ab.tar.xz
internal/coverage: add apis for reading/writing counter data
Add utilities for reading and writing of counter data files as part of the new code coverage implementation. Trivia note: currently the contents of counter data files are emitted in little-endian form, which for the counters is somewhat painful in that we need to visit each counter value and properly encode it. It might be better to instead emit counters in native endianity and then teach the tools to decode properly in the case of an endianity mismatch. Updates #51430. Change-Id: I124fdcb40fc339a48b64b35264bf24c3be50ddd4 Reviewed-on: https://go-review.googlesource.com/c/go/+/359403 TryBot-Result: Gopher Robot <gobot@golang.org> Reviewed-by: David Chase <drchase@google.com> Run-TryBot: Than McIntosh <thanm@google.com>
Diffstat (limited to 'src/internal/coverage/stringtab/stringtab.go')
-rw-r--r--src/internal/coverage/stringtab/stringtab.go35
1 files changed, 26 insertions, 9 deletions
diff --git a/src/internal/coverage/stringtab/stringtab.go b/src/internal/coverage/stringtab/stringtab.go
index f093e2cd15..405c4c1f18 100644
--- a/src/internal/coverage/stringtab/stringtab.go
+++ b/src/internal/coverage/stringtab/stringtab.go
@@ -40,6 +40,9 @@ func (stw *Writer) Lookup(s string) uint32 {
if idx, ok := stw.stab[s]; ok {
return idx
}
+ if stw.frozen {
+ panic("internal error: string table previously frozen")
+ }
idx := uint32(len(stw.strs))
stw.stab[s] = idx
stw.strs = append(stw.strs, s)
@@ -91,11 +94,23 @@ func (stw *Writer) Write(w io.Writer) error {
return nil
}
+// Freeze sends a signal to the writer that no more additions are
+// allowed, only lookups of existing strings (if a lookup triggers
+// addition, a panic will result). Useful as a mechanism for
+// "finalizing" a string table prior to writing it out.
+func (stw *Writer) Freeze() {
+ stw.frozen = true
+}
+
+// Reader is a helper for reading a string table previously
+// serialized by a Writer.Write call.
type Reader struct {
r *slicereader.Reader
strs []string
}
+// NewReader creates a stringtab.Reader to read the contents
+// of a string table from 'r'.
func NewReader(r *slicereader.Reader) *Reader {
str := &Reader{
r: r,
@@ -103,16 +118,8 @@ func NewReader(r *slicereader.Reader) *Reader {
return str
}
-func (str *Reader) Entries() int {
- return len(str.strs)
-}
-
-func (str *Reader) Get(idx uint32) string {
- return str.strs[idx]
-}
-
+// Read reads/decodes a string table using the reader provided.
func (str *Reader) Read() {
- // Read the table itself.
numEntries := int(str.r.ReadULEB128())
str.strs = make([]string, 0, numEntries)
for idx := 0; idx < numEntries; idx++ {
@@ -120,3 +127,13 @@ func (str *Reader) Read() {
str.strs = append(str.strs, str.r.ReadString(int64(slen)))
}
}
+
+// Entries returns the number of decoded entries in a string table.
+func (str *Reader) Entries() int {
+ return len(str.strs)
+}
+
+// Get returns string 'idx' within the string table.
+func (str *Reader) Get(idx uint32) string {
+ return str.strs[idx]
+}