forked from go-llvm/llgo
-
Notifications
You must be signed in to change notification settings - Fork 0
/
build.go
55 lines (48 loc) · 1.2 KB
/
build.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
// Copyright 2013 The llgo Authors.
// Use of this source code is governed by an MIT-style
// license that can be found in the LICENSE file.
package build
import (
"bytes"
"io"
)
// LLVMIRReadCloser wraps an io.ReadCloser, transforming the leading
// comments of the file such that they are preceded with "//" rather
// than standard LLVM IR comments ("; blah").
//
// This type is intended to be used in the OpenFile field of
// go/build's Context type.
type LLVMIRReadCloser struct {
io.ReadCloser
buf *bytes.Buffer
}
func NewLLVMIRReader(r io.ReadCloser) *LLVMIRReadCloser {
return &LLVMIRReadCloser{r, nil}
}
var slashslash = []byte("//")
var semicolon = []byte(";")
func (r *LLVMIRReadCloser) Read(p []byte) (n int, err error) {
if r.buf != nil {
n, err = r.buf.Read(p)
if err == io.EOF {
if r.ReadCloser != nil {
err = nil
}
r.buf = nil
}
return
}
n, err = r.ReadCloser.Read(p)
if n > 0 {
// To simplify the translation we'll simply
// replace all instances of ";" in the returned
// data.
p_ := bytes.Replace(p, semicolon, slashslash, -1)
if len(p_) > len(p) {
r.buf = bytes.NewBuffer(p_)
n, err = r.buf.Read(p)
// err should be nil, since n < len(p)
}
}
return
}