[llvm] [LLVM-C] Add binding to `BitcodeReader::getBitcodeProducerString` (PR #166063)

Tamir Duberstein via llvm-commits llvm-commits at lists.llvm.org
Tue Nov 4 06:11:18 PST 2025


================
@@ -135,3 +134,57 @@ int llvm_module_list_globals(void) {
 
   return 0;
 }
+
+int llvm_module_get_producer_string(void) {
+  LLVMMemoryBufferRef MB;
+  char *Msg = NULL;
+  if (LLVMCreateMemoryBufferWithSTDIN(&MB, &Msg)) {
+    fprintf(stderr, "Error reading file: %s\n", Msg);
+    LLVMDisposeMessage(Msg);
+    return 1;
+  }
+
+  char *Producer = NULL;
+  char *Err = NULL;
+  LLVMBool Res = LLVMGetBitcodeProducerString(MB, &Producer, &Err);
+  LLVMDisposeMemoryBuffer(MB);
+
+  int Ret = 0;
+  if (Res) {
+    if (Producer)
+      fprintf(stderr,
+              "LLVMGetBitcodeProducerString returned %d, but Producer is not "
+              "NULL: %s",
+              Res, Producer);
+    if (Err)
+      fprintf(stderr, "LLVMGetBitcodeProducerSring returned %d, error: %s\n",
+              Res, Err);
----------------
tamird wrote:

This can just be outside the `if (Res)`, right? you always want to print these if they are non-null. Then you can immediately dispose them, and then you can:

```
int Ret = 0;
if (Res) {
  fprintf(stderr, "LLVMGetBitcodeProducerString(...) returned %d", Res);
  Ret = 1;
}
if (!Producer) {
  fprintf(...)
  Ret = 1;
}
if (Err) {
  ...
}
return Ret;
```

by the way, do we need a negative test, for the error case? It is not currently exercised.

https://github.com/llvm/llvm-project/pull/166063


More information about the llvm-commits mailing list