Skip to content

Commit b5bc828

Browse files
committed
chore: isolate code from x/tools
1 parent 41d6435 commit b5bc828

File tree

2 files changed

+32
-31
lines changed

2 files changed

+32
-31
lines changed

pkg/goanalysis/runner_base.go

Lines changed: 32 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -8,12 +8,44 @@
88
package goanalysis
99

1010
import (
11+
"bytes"
12+
"encoding/gob"
13+
"fmt"
1114
"go/types"
1215
"reflect"
1316

1417
"golang.org/x/tools/go/analysis"
1518
)
1619

20+
// NOTE(ldez) no alteration.
21+
// codeFact encodes then decodes a fact,
22+
// just to exercise that logic.
23+
func codeFact(fact analysis.Fact) (analysis.Fact, error) {
24+
// We encode facts one at a time.
25+
// A real modular driver would emit all facts
26+
// into one encoder to improve gob efficiency.
27+
var buf bytes.Buffer
28+
if err := gob.NewEncoder(&buf).Encode(fact); err != nil {
29+
return nil, err
30+
}
31+
32+
// Encode it twice and assert that we get the same bits.
33+
// This helps detect nondeterministic Gob encoding (e.g. of maps).
34+
var buf2 bytes.Buffer
35+
if err := gob.NewEncoder(&buf2).Encode(fact); err != nil {
36+
return nil, err
37+
}
38+
if !bytes.Equal(buf.Bytes(), buf2.Bytes()) {
39+
return nil, fmt.Errorf("encoding of %T fact is nondeterministic", fact)
40+
}
41+
42+
newFact := reflect.New(reflect.TypeOf(fact).Elem()).Interface().(analysis.Fact)
43+
if err := gob.NewDecoder(&buf).Decode(newFact); err != nil {
44+
return nil, err
45+
}
46+
return newFact, nil
47+
}
48+
1749
// NOTE(ldez) no alteration.
1850
// exportedFrom reports whether obj may be visible to a package that imports pkg.
1951
// This includes not just the exported members of pkg, but also unexported

pkg/goanalysis/runner_facts.go

Lines changed: 0 additions & 31 deletions
Original file line numberDiff line numberDiff line change
@@ -1,9 +1,6 @@
11
package goanalysis
22

33
import (
4-
"bytes"
5-
"encoding/gob"
6-
"fmt"
74
"go/types"
85
"reflect"
96

@@ -68,31 +65,3 @@ func inheritFacts(act, dep *action) {
6865
act.packageFacts[key] = fact
6966
}
7067
}
71-
72-
// codeFact encodes then decodes a fact,
73-
// just to exercise that logic.
74-
func codeFact(fact analysis.Fact) (analysis.Fact, error) {
75-
// We encode facts one at a time.
76-
// A real modular driver would emit all facts
77-
// into one encoder to improve gob efficiency.
78-
var buf bytes.Buffer
79-
if err := gob.NewEncoder(&buf).Encode(fact); err != nil {
80-
return nil, err
81-
}
82-
83-
// Encode it twice and assert that we get the same bits.
84-
// This helps detect nondeterministic Gob encoding (e.g. of maps).
85-
var buf2 bytes.Buffer
86-
if err := gob.NewEncoder(&buf2).Encode(fact); err != nil {
87-
return nil, err
88-
}
89-
if !bytes.Equal(buf.Bytes(), buf2.Bytes()) {
90-
return nil, fmt.Errorf("encoding of %T fact is nondeterministic", fact)
91-
}
92-
93-
newFact := reflect.New(reflect.TypeOf(fact).Elem()).Interface().(analysis.Fact)
94-
if err := gob.NewDecoder(&buf).Decode(newFact); err != nil {
95-
return nil, err
96-
}
97-
return newFact, nil
98-
}

0 commit comments

Comments
 (0)