forked from rsc/c2go
-
Notifications
You must be signed in to change notification settings - Fork 4
/
Copy pathconfig.go
345 lines (317 loc) · 7.63 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
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
// 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"
"go/ast"
"go/parser"
"go/scanner"
"go/token"
"io/ioutil"
"log"
"path"
"path/filepath"
"runtime"
"strings"
"github.com/andybalholm/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
typedefs []string
exprMacros map[string]expressionMacro
stmtMacros map[string]statementMacro
// 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 = strings.TrimPrefix(file, "./")
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)
cfg.exprMacros = make(map[string]expressionMacro)
cfg.stmtMacros = make(map[string]statementMacro)
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 "macro":
// Parse the macro signature (name and args).
fs := token.NewFileSet()
tf := fs.AddFile("", fs.Base(), len(line))
var s scanner.Scanner
s.Init(tf, []byte(line), nil, 0)
_, tok, lit := s.Scan()
if lit != "macro" {
log.Fatalf("%s:%d: expected 'macro', found %v %q", file, lineno, tok, lit)
}
_, tok, lit = s.Scan()
if tok != token.IDENT {
log.Fatalf("%s:%d: expected macro name, found %v %q", file, lineno, tok, lit)
}
name := lit
_, tok, lit = s.Scan()
if tok != token.LPAREN {
log.Fatalf("%s:%d: expected '(', found %v %q", file, lineno, tok, lit)
}
var args []string
variadic := false
argLoop:
for {
_, tok, lit = s.Scan()
switch tok {
case token.RPAREN:
break argLoop
case token.IDENT:
args = append(args, lit)
case token.ELLIPSIS:
variadic = true
_, tok, lit = s.Scan()
if tok != token.RPAREN {
log.Fatalf("%s:%d: expected ')', found %v %q", file, lineno, tok, lit)
}
break argLoop
default:
log.Fatalf("%s:%d: expected identifier, '...', or ')', found %v %q", file, lineno, tok, lit)
}
_, tok, lit = s.Scan()
switch tok {
case token.COMMA:
continue argLoop
case token.RPAREN:
break argLoop
default:
log.Fatalf("%s:%d: expected ',' or ')', found %v %q", file, lineno, tok, lit)
}
}
// Parse the body.
pos, tok, lit := s.Scan()
if tok == token.LBRACE {
// statement macro, with a braced block
var buf bytes.Buffer
buf.WriteString("package main; func main() {\n")
for {
lineno++
if len(lines) == 0 {
log.Fatalf("%s:%d: unexpected EOF reading macro body", file, lineno)
}
line = lines[0]
lines = lines[1:]
buf.WriteString(line + "\n")
if line == "}" {
break
}
}
macroFile, err := parser.ParseFile(fs, name, &buf, 0)
if err != nil {
log.Fatalf("%s:%d: error parsing macro body: %v", file, lineno, err)
}
main := macroFile.Decls[0].(*ast.FuncDecl)
cfg.stmtMacros[name] = statementMacro{
args: args,
variadic: variadic,
stmts: main.Body.List,
}
} else {
// expression macro, just on the remainder of the line
rest := line[int(pos)-tf.Base():]
expr, err := parser.ParseExpr(rest)
if err != nil {
log.Fatalf("%s:%d: error parsing macro body: %v", file, lineno, err)
}
cfg.exprMacros[name] = expressionMacro{
args: args,
variadic: variadic,
expr: expr,
}
}
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]
case "typedef":
cfg.typedefs = append(cfg.typedefs, f[1:]...)
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
}