[PATCH] D18736: [llvm-c] Improve IR Introspection: Add LLVM{Get, Set}ModuleIdentifier

Amaury SECHET via llvm-commits llvm-commits at lists.llvm.org
Sun Apr 3 19:00:56 PDT 2016


deadalnix added inline comments.

================
Comment at: lib/IR/Core.cpp:161-162
@@ +160,4 @@
+char *LLVMGetModuleIdentifier(LLVMModuleRef M, unsigned *Len) {
+  auto Ret = strdup(unwrap(M)->getModuleIdentifier().c_str());
+  auto ModuleLen = strlen(Ret);
+  assert(ModuleLen < UINT_MAX && "Module identifier is too long");
----------------
A string should know its length already, so taking the pointer and then computing strlen is wasteful. It is also not needed to strdup. It is common practice in the C API to return string the caller don't have ownership of. If caller has ownership of something, then a LLVMDisposeXXX method is provided. In this case, I don't think this is necessary.

  auto &Str = unwrap(M)->getModuleIdentifier()
  *Length = Str.length();
  return Str.c_str();

Also, just take a size_t for length so yu don't need to assert. I see no reason why someone would have such a large identifier that it matters, but, on the other hand, I see no reason to prevent it.


http://reviews.llvm.org/D18736





More information about the llvm-commits mailing list