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
/
Copy pathoutput.go
195 lines (172 loc) · 5 KB
/
output.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
// 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"
"flag"
"go/format"
"io/ioutil"
"log"
"os"
"path"
"path/filepath"
"strings"
"rsc.io/c2go/cc"
)
var dst = flag.String("dst", "/tmp/c2go", "GOPATH root of destination")
// writeGoFiles writes prog to Go source files in a tree of packages.
func writeGoFiles(cfg *Config, prog *cc.Prog) {
printers := map[string]*Printer{}
cfiles := map[string]string{}
for _, decl := range prog.Decls {
if decl.GoPackage == "" {
decl.GoPackage = "other"
}
cfile := decl.Span.Start.File
gofile := strings.TrimSuffix(strings.TrimSuffix(cfile, ".c"), ".h") + ".go"
gofile = decl.GoPackage + "/" + filepath.Base(gofile)
cfiles[gofile] = cfile
p := printers[gofile]
if p == nil {
p = new(Printer)
p.Package = decl.GoPackage
pkg := path.Base(p.Package)
if strings.Count(p.Package, "/") == 1 && strings.HasPrefix(p.Package, "cmd/") {
pkg = "main"
}
p.Print("package ", pkg, "\n\n")
switch p.Package {
case "cmd/new5g", "cmd/new6g", "cmd/new8g", "cmd/new9g":
p.Print(`import "cmd/internal/obj"`, "\n")
p.Print(`import "cmd/internal/gc"`, "\n")
case "cmd/new5l", "cmd/new6l", "cmd/new8l", "cmd/new9l":
p.Print(`import "cmd/internal/obj"`, "\n")
p.Print(`import "cmd/internal/ld"`, "\n")
case "cmd/internal/gc", "cmd/internal/ld", "cmd/internal/obj/arm", "cmd/internal/obj/ppc64", "cmd/internal/obj/x86", "cmd/internal/obj/amd64":
p.Print(`import "cmd/internal/obj"`, "\n")
}
printers[gofile] = p
}
off := len(p.Bytes())
repl, ok := cfg.replace[decl.Name]
if !ok {
repl, ok = cfg.replace[strings.ToLower(decl.Name)]
}
if cfg.delete[decl.Name] || cfg.delete[strings.ToLower(decl.Name)] {
repl, ok = "", true
}
if ok {
// Use replacement text from config but keep surrounding comments.
p.Print(decl.Comments.Before)
p.Print(repl)
p.Print(decl.Comments.Suffix, decl.Comments.After)
} else {
p.Print(decl)
}
if len(p.Bytes()) > off {
p.Print(Newline)
p.Print(Newline)
}
}
for gofile, p := range printers {
dstfile := filepath.Join(*dst+"/src", gofile)
os.MkdirAll(filepath.Dir(dstfile), 0777)
buf := p.Bytes()
// Not entirely sure why these lines get broken.
buf = bytes.Replace(buf, []byte("\n,"), []byte(","), -1)
buf = bytes.Replace(buf, []byte("\n {"), []byte(" {"), -1)
buf1, err := format.Source(buf)
if err != nil {
// Scream because it invalidates diffs.
log.Printf("ERROR formatting %s: %v", gofile, err)
}
if err == nil {
buf = buf1
}
// Not sure where these blank lines come from.
buf = bytes.Replace(buf, []byte("{\n\n"), []byte("{\n"), -1)
buf = fixCopyright(gofile, cfiles[gofile], buf)
for i, d := range cfg.diffs {
if bytes.Contains(buf, d.before) {
buf = bytes.Replace(buf, d.before, d.after, -1)
cfg.diffs[i].used++
}
}
if err := ioutil.WriteFile(dstfile, buf, 0666); err != nil {
log.Print(err)
}
}
}
var copyrightPrefixes = []string{
"// Copyright",
"// Inferno",
"// Derived",
"// cmd/",
"/*\n * The authors of this software",
"/*\nhttp://code.google.com",
}
// fixCopyright hoists the copyright notice to the top of the file.
// The package statement has been printed above it.
// If fixCopyright cannot find a notice, it calls copyCopyright to copy it
// from the original C file.
func fixCopyright(gofile, cfile string, buf []byte) []byte {
i := -1
for _, prefix := range copyrightPrefixes {
if j := bytes.Index(buf, []byte(prefix)); j >= 0 && (i < 0 || j < i) {
i = j
}
}
if i < 0 {
//log.Printf("%s: cannot find copyright notice", gofile)
return copyCopyright(gofile, cfile, buf)
}
k := bytes.Index(buf[i:], []byte("\n\n"))
if k < 0 {
log.Printf("%s: cannot find end of copyright notice", gofile)
return buf
}
k += i
for l := k - 1; l >= 0; l-- {
if buf[l] == '\n' {
if buf[l+1] != '/' || buf[l+2] != '/' {
log.Printf("%s: copyright notice not followed by blank line", gofile)
return buf
}
break
}
}
var out []byte
out = append(out, buf[i:k+2]...)
out = append(out, buf[:i]...)
out = append(out, buf[k+1:]...) // k+1 to include an extra \n
return out
}
// copyCopyright inserts the copyright from cfile at the beginning of buf
// and returns the result.
func copyCopyright(gofile, cfile string, buf []byte) []byte {
if strings.HasPrefix(cfile, "internal/") {
return buf
}
data, err := ioutil.ReadFile(cfile)
if err != nil {
log.Printf("%s: reading copyright from C: %v", gofile, err)
return buf
}
i := -1
for _, prefix := range copyrightPrefixes {
if j := bytes.Index(data, []byte(prefix)); j >= 0 && (i < 0 || j < i) {
i = j
}
}
if i < 0 {
log.Printf("%s: cannot find copyright notice in C file %s", gofile, cfile)
return buf
}
j := bytes.Index(data[i:], []byte("\n\n"))
if j < 0 {
log.Printf("%s: cannot find end of copyright notice in C file %s", gofile, cfile)
}
j += i
return append(data[i:j+2], buf...)
}