Debian Codegraph @

tools/antithesis-go-instrumentor/scanners/coverage/instrumentor/edit_buffer.go

raw
1 package instrumentor
2
3 import (
4 "fmt"
5 "go/ast"
6 "go/token"
7 "sort"
8
9 "github.com/antithesishq/antithesis-sdk-go/tools/antithesis-go-instrumentor/common"
10 "github.com/antithesishq/antithesis-sdk-go/tools/antithesis-go-instrumentor/scanners/coverage/symboltable"
11 )
12
13 type edit struct {
14 start, end int // byte offsets into the original file
15 text string
16 }
17
18 type editBuffer struct {
19 src []byte
20 edits []edit
21 }
22
23 func (b *editBuffer) insert(at int, text string) { b.edits = append(b.edits, edit{at, at, text}) }
24
25 func (b *editBuffer) replace(start, end int, text string) {
26 b.edits = append(b.edits, edit{start, end, text})
27 }
28
29 func (b *editBuffer) bytes() []byte {
30 sort.SliceStable(b.edits, func(i, j int) bool { return b.edits[i].start < b.edits[j].start })
31 var out []byte
32 prev := 0
33 for _, e := range b.edits {
34 out = append(out, b.src[prev:e.start]...)
35 out = append(out, e.text...)
36 prev = e.end
37 }
38 return append(out, b.src[prev:]...)
39 }
40
41 func (instrumentor *SourceEditingInstrumentor) offAt(p token.Pos) int {
42 return instrumentor.fset.Position(p).Offset
43 }
44 func (instrumentor *SourceEditingInstrumentor) offAfter(p token.Pos) int {
45 return instrumentor.fset.Position(p).Offset + 1
46 }
47
48 // Is there an unterminated statement without a newline preceding `off`?
49 func (instrumentor *SourceEditingInstrumentor) sameLineCodePrecedes(off int) bool {
50 for i := off - 1; i >= 0; i-- {
51 switch c := instrumentor.src[i]; {
52 case c == ' ' || c == '\t':
53 continue
54 case c == '\n' || c == '\r':
55 return false
56 default:
57 return true
58 }
59 }
60 return false
61 }
62
63 func (instrumentor *SourceEditingInstrumentor) firstBlockSep(off int) string {
64 for i := off; i < len(instrumentor.src); i++ {
65 switch c := instrumentor.src[i]; {
66 case c == ' ' || c == '\t':
67 continue
68 case c == '\n' || c == '\r':
69 return ""
70 case c == '/' && i+1 < len(instrumentor.src) && instrumentor.src[i+1] == '/':
71 return ""
72 default:
73 return ";"
74 }
75 }
76 return ""
77 }
78
79 func (instrumentor *SourceEditingInstrumentor) notifyCall(edge int) string {
80 return fmt.Sprintf("%s.%s(%d)", instrumentor.callbackName, AntithesisCallbackFunction, edge)
81 }
82
83 func (instrumentor *SourceEditingInstrumentor) recordEdge(start, end token.Pos) int {
84 instrumentor.CurrentEdge++
85
86 s := instrumentor.fset.Position(start)
87 e := instrumentor.fset.Position(end)
88 fname := ""
89 if maybeDecl, ok := instrumentor.funcStack.Peek(); ok {
90 if decl, isDecl := maybeDecl.(*ast.FuncDecl); isDecl {
91 fname = decl.Name.Name
92 }
93 }
94
95 if err := instrumentor.symbolTable.WritePosition(symboltable.SymbolTablePosition{
96 Path: instrumentor.fullName,
97 Function: fname,
98 StartLine: s.Line,
99 StartColumn: s.Column,
100 EndLine: e.Line,
101 EndColumn: e.Column,
102 Edge: instrumentor.CurrentEdge,
103 }); err != nil {
104 common.Logger.Fatalf("Could not write symbol table line: %s", err.Error())
105 }
106 return instrumentor.CurrentEdge
107 }
108
109 func (instrumentor *SourceEditingInstrumentor) insertEdge(insertOff int, sep string, start, end token.Pos) {
110 edge := instrumentor.recordEdge(start, end)
111 instrumentor.buf.insert(insertOff, instrumentor.notifyCall(edge)+sep)
112 }
113
114 func (instrumentor *SourceEditingInstrumentor) instrumentShortCircuit(n *ast.BinaryExpr) {
115 // Walk the left operand first so operands nested inside it are numbered
116 // before the outer operand that sits to their right
117 ast.Walk(instrumentor, n.X)
118
119 end := n.Y.End()
120 if found, pos := hasFuncLiteral(n.Y); found {
121 end = pos
122 }
123 edge := instrumentor.recordEdge(n.Y.Pos(), end)
124 instrumentor.buf.insert(instrumentor.offAt(n.Y.Pos()),
125 fmt.Sprintf("func() bool { %s; return (", instrumentor.notifyCall(edge)))
126 instrumentor.buf.insert(instrumentor.offAt(n.Y.End()), ") == true }() == true")
127
128 // Walk the right operand (left in place, at its original positions) so its
129 // own subtree gets instrumented.
130 ast.Walk(instrumentor, n.Y)
131 }