Skip to content

Commit e7b8519

Browse files
committed
add Context.ParseBitcodeFile
While there is a ParseBitcodeFile already, the resulting module lives in the default context. This does not work when doing parallel builds. Therefore, this extra function is needed to parse a bitcode file in a different context.
1 parent 435a266 commit e7b8519

File tree

1 file changed

+25
-0
lines changed

1 file changed

+25
-0
lines changed

bitreader.go

Lines changed: 25 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -48,3 +48,28 @@ func ParseBitcodeFile(name string) (Module, error) {
4848
C.free(unsafe.Pointer(errmsg))
4949
return Module{}, err
5050
}
51+
52+
// ParseBitcodeFile parses the LLVM IR (bitcode) in the file with the specified
53+
// name, and returns a new LLVM module.
54+
func (c Context) ParseBitcodeFile(name string) (Module, error) {
55+
var buf C.LLVMMemoryBufferRef
56+
var errmsg *C.char
57+
var cfilename *C.char = C.CString(name)
58+
defer C.free(unsafe.Pointer(cfilename))
59+
result := C.LLVMCreateMemoryBufferWithContentsOfFile(cfilename, &buf, &errmsg)
60+
if result != 0 {
61+
err := errors.New(C.GoString(errmsg))
62+
C.free(unsafe.Pointer(errmsg))
63+
return Module{}, err
64+
}
65+
defer C.LLVMDisposeMemoryBuffer(buf)
66+
67+
var m Module
68+
if C.LLVMParseBitcodeInContext2(c.C, buf, &m.C) == 0 {
69+
return m, nil
70+
}
71+
72+
err := errors.New(C.GoString(errmsg))
73+
C.free(unsafe.Pointer(errmsg))
74+
return Module{}, err
75+
}

0 commit comments

Comments
 (0)