This repository has been archived by the owner on Jul 7, 2020. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 39
/
config.go
240 lines (215 loc) · 4.76 KB
/
config.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
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
// Copyright 2015 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 main
import (
"bytes"
"fmt"
"io/ioutil"
"log"
"path"
"path/filepath"
"runtime"
"strings"
"rsc.io/c2go/cc"
)
type Config struct {
pkgRules []pkgRule
exports []string
replace map[string]string
delete map[string]bool
diffs []diff
slice map[string]bool
len map[string]string
cap map[string]string
typeMap map[string]string
bool map[string]bool
ptr map[string]bool
rename map[string]string
// derived during analysis
topDecls []*cc.Decl
}
type pkgRule struct {
pattern string
pkg string
}
type diff struct {
line string
before []byte
after []byte
used int
}
func (cfg *Config) filePackage(file string) (pkg string) {
file = filepath.ToSlash(strings.TrimPrefix(file, runtime.GOROOT()+string(filepath.Separator)))
pkg = "main"
for _, rule := range cfg.pkgRules {
matched, err := path.Match(rule.pattern, file)
if err != nil {
log.Printf("invalid pattern: %v", err)
continue
}
if matched {
pkg = rule.pkg
}
}
return pkg
}
func (cfg *Config) read(file string) {
data, err := ioutil.ReadFile(file)
if err != nil {
log.Fatal(err)
}
lineno := 0
lines := strings.Split(string(data), "\n")
cfg.replace = make(map[string]string)
cfg.delete = make(map[string]bool)
cfg.slice = make(map[string]bool)
cfg.len = make(map[string]string)
cfg.cap = make(map[string]string)
cfg.typeMap = make(map[string]string)
cfg.bool = make(map[string]bool)
cfg.ptr = make(map[string]bool)
cfg.rename = make(map[string]string)
for len(lines) > 0 {
line := lines[0]
lines = lines[1:]
lineno++
line = strings.TrimSpace(line)
if strings.HasPrefix(line, "#") {
continue
}
f := strings.Fields(line)
if len(f) == 0 {
continue
}
switch f[0] {
case "package":
pkg := f[len(f)-1]
for i := 1; i < len(f)-1; i++ {
cfg.pkgRules = append(cfg.pkgRules, pkgRule{f[i], pkg})
}
case "export":
cfg.exports = append(cfg.exports, f[1:]...)
case "delete":
for _, name := range f[1:] {
cfg.delete[name] = true
}
case "bool":
for _, name := range f[1:] {
cfg.bool[name] = true
}
case "ptr":
for _, name := range f[1:] {
cfg.ptr[name] = true
}
case "string":
if len(f) >= 3 {
cfg.len[f[2]] = f[1]
}
case "slice":
if len(f) >= 2 {
cfg.slice[f[1]] = true
}
if len(f) >= 3 {
cfg.len[f[2]] = f[1]
}
if len(f) >= 4 {
cfg.cap[f[3]] = f[1]
}
if len(f) >= 5 {
log.Printf("%s:%d: extra arguments for slice", file, lineno)
}
case "func", "type":
if len(f) < 2 {
log.Printf("%s:%d: short func/type declaration", file, lineno)
}
var buf bytes.Buffer
buf.WriteString(line + "\n")
if strings.HasSuffix(line, "{") {
for {
lineno++
if len(lines) == 0 {
log.Fatalf("%s:%d: unexpected EOF reading func/type body", file, lineno)
}
line = lines[0]
lines = lines[1:]
buf.WriteString(line + "\n")
if line == "}" {
break
}
}
}
name := f[1]
if i := strings.Index(name, "("); i >= 0 {
name = name[:i]
}
cfg.replace[name] = buf.String()
case "diff":
if line != "diff {" {
log.Printf("%s:%d: invalid diff opening", file, lineno)
break
}
var old, new bytes.Buffer
fileline := fmt.Sprintf("%s:%d", file, lineno)
for {
lineno++
if len(lines) == 0 {
log.Fatalf("%s:%d: unexpected EOF reading diff", file, lineno)
}
line = lines[0]
lines = lines[1:]
if line == "}" {
break
}
switch {
case strings.HasPrefix(line, "+"):
line = strings.TrimPrefix(line[1:], " ")
new.WriteString(line + "\n")
case strings.HasPrefix(line, "-"):
line = strings.TrimPrefix(line[1:], " ")
old.WriteString(line + "\n")
default:
line = strings.TrimPrefix(strings.TrimPrefix(line, " "), " ")
old.WriteString(line + "\n")
new.WriteString(line + "\n")
}
}
cfg.diffs = append(cfg.diffs, diff{
line: fileline,
before: old.Bytes(),
after: new.Bytes(),
})
case "typemap":
if len(f) != 3 {
log.Printf("%s:%d: invalid typemap directive", file, lineno)
continue
}
cfg.typeMap[f[1]] = f[2]
case "rename":
if len(f) != 3 {
log.Printf("%s:%d: invalid rename directive", file, lineno)
continue
}
cfg.rename[f[1]] = f[2]
default:
log.Printf("%s:%d: unknown verb %s", file, lineno, f[0])
}
}
}
func declKey(d *cc.Decl) string {
key := d.Name
if t := d.OuterType; t != nil {
name := t.Name
if name == "" {
name = t.Tag
}
if name == "" {
name = t.String()
}
key = name + "." + key
}
if d.CurFn != nil {
key = declKey(d.CurFn) + "." + key
}
return key
}