[PATCH][llvm-c] expose sys::getProcessTriple

Filip Pizlo fpizlo at apple.com
Thu Jun 27 20:05:32 PDT 2013


The fact that this isn't already exposed makes using some other C APIs, like the disassembler API, really awkward in a JIT context - you don't want to have to describe to LLVM what the target name is for the process you're in, when LLVM already has this information.

The challenge here is that sys::getProcessTriple() returns a std::string that it allocates itself.  So we can't just have a "LLVMGetProcessTarget()" that returns a char*, unless we then require the user to free() that char*.  I considered that but I feared that it would have been confusing to users: for example LLVMGetTarget() returns a char* that doesn't require deallocation.  So, to make the ownership rules more explicit, I made the API take an already allocated char* and a size.

I wanted to allow for this idiom for people who don't want to guess the right string size:

size_t targetSize = LLVMGetProcessTarget(0, 0);
char *target = malloc(targetSize);
LLVMGetProcessTarget(target, targetSize);

This means that I couldn't just call snprintf().  I actually ended up getting the buffer allocation wrong on the first try, so I include some test coverage to make sure that it really isn't messed up.

-Filip

-------------- next part --------------
A non-text attachment was scrubbed...
Name: GetProcessTarget.patch
Type: application/octet-stream
Size: 4935 bytes
Desc: not available
URL: <http://lists.llvm.org/pipermail/llvm-commits/attachments/20130627/18c83e53/attachment.obj>


More information about the llvm-commits mailing list