diff --git a/llvm/core.py b/llvm/core.py index 9310169..46b1cc0 100644 --- a/llvm/core.py +++ b/llvm/core.py @@ -1940,8 +1940,11 @@ class Builder(object): Next instruction inserted will be first one in the block.""" # Avoids using "blk.instructions", which will fetch all the # instructions into a list. Don't try this at home, though. - first_inst = Instruction(_core.LLVMGetFirstInstruction(self.block.ptr)) - self.position_before(first_inst) + inst_ptr = _core.LLVMGetFirstInstruction(self.block.ptr) + if inst_ptr: + # Issue #10: inst_ptr can be None if b/b has no insts. + inst = Instruction(inst_ptr) + self.position_before(inst) def position_at_end(self, bblk): """Position the builder at the end of the given block. diff --git a/test/issue10.py b/test/issue10.py new file mode 100644 index 0000000..185b1c8 --- /dev/null +++ b/test/issue10.py @@ -0,0 +1,20 @@ + +from llvm.core import * +import llvm._core + +m = Module.new('a') +ti = Type.int() +tf = Type.function(ti, [ti, ti]) + +f = m.add_function(tf, "func1") + +bb = f.append_basic_block('entry') + +b = Builder.new(bb) + +# There are no instructions in bb. Positioning of the +# builder at beginning (or end) should succeed (trivially). + +b.position_at_end(bb) +b.position_at_beginning(bb) +