-
Notifications
You must be signed in to change notification settings - Fork 0
/
cluster.go
162 lines (148 loc) · 3.32 KB
/
cluster.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
package main
// This file defines the cluster graph.
import (
"bufio"
"fmt"
"os"
"strings"
)
type cluster struct {
id int
importPath string // declared name, e.g. "runtime/internal/core"
name string // short import name, e.g. "_core"
nodes map[*node]bool
scope map[string]*node // maps package-level names to decls
outputFiles map[string]*outputFile // output file data, keyed by file base name
}
func (c *cluster) finish() {
// mark applies n's cluster to all nodes reachable from it that
// don't have a cluster assignment yet.
var mark func(n *node)
mark = func(n *node) {
for s := range n.succs {
if s.cluster == nil {
s.cluster = n.cluster
n.cluster.nodes[s] = true
if debug {
fmt.Printf("\t%-50s (indirect)\n", s)
}
mark(s)
}
}
}
var first, prev *node
for n := range c.nodes {
if first == nil {
first = n
}
if prev != nil {
mark(n)
}
prev = n
}
if prev != nil {
mark(first)
}
c.outputFiles = make(map[string]*outputFile)
}
func loadClusterFile(filename string, nodes []*node) ([]*cluster, error) {
clusterNames := map[string]bool{"residue": true}
byName := make(map[string]*node)
for _, n := range nodes {
byName[n.name] = n
}
f, err := os.Open(*clusterFile)
if err != nil {
return nil, err
}
in := bufio.NewScanner(f)
var linenum int
var c *cluster
var clusters []*cluster
for in.Scan() {
linenum++
line := strings.TrimSpace(in.Text())
if i := strings.IndexByte(line, '#'); i >= 0 {
line = strings.TrimSpace(line[:i]) // strip comments
}
if line == "" {
continue // skip blanks
}
if strings.HasPrefix(line, "= ") {
if c != nil {
c.finish()
}
c = &cluster{
id: len(clusters),
importPath: line[2:],
nodes: make(map[*node]bool),
}
if clusterNames[c.importPath] {
fmt.Fprintf(os.Stderr,
"%s:%d: warning: duplicate cluster name: %s; ignoring\n",
*clusterFile, linenum, c.importPath)
continue
}
clusters = append(clusters, c)
if debug {
fmt.Printf("\n# cluster %s\n", c.importPath)
}
continue
}
if c == nil {
fmt.Fprintf(os.Stderr,
"%s:%d: warning: node before '= cluster' marker; ignoring\n",
*clusterFile, linenum)
continue
}
n := byName[line]
if n == nil {
fmt.Fprintf(os.Stderr,
"%s:%d: warning: can't find node %q; ignoring\n",
*clusterFile, linenum, line)
} else if n.cluster != nil {
fmt.Fprintf(os.Stderr,
"%s:%d: warning: node %q appears in clusters %q and %q; ignoring\n",
*clusterFile, linenum, line, n.cluster.importPath, c.importPath)
} else {
n.cluster = c
if debug {
fmt.Printf("\t%s\n", n)
}
c.nodes[n] = true
}
}
if c != nil {
c.finish()
}
f.Close()
if err := in.Err(); err != nil {
return nil, err
}
return clusters, nil
}
func addResidualCluster(nodes []*node, clusters []*cluster) []*cluster {
// The final cluster, residue, includes all other nodes.
c := &cluster{
id: len(clusters),
importPath: "residue",
nodes: make(map[*node]bool),
}
if debug {
fmt.Printf("\n# cluster %s\n", c.importPath)
}
for _, n := range nodes {
if n.cluster == nil {
n.cluster = c
if debug {
fmt.Printf("\t%-50s\n", n)
}
c.nodes[n] = true
}
}
c.finish()
if len(c.nodes) > 0 {
clusters = append(clusters, c)
}
return clusters
}