Introduce llvm-c function LLVMPrintModuleToFile.
authorHans Wennborg <hans@hanshq.net>
Wed, 9 May 2012 16:54:17 +0000 (16:54 +0000)
committerHans Wennborg <hans@hanshq.net>
Wed, 9 May 2012 16:54:17 +0000 (16:54 +0000)
This lets you save the textual representation of the LLVM IR to a file.
Before this patch it could only be printed to STDERR from llvm-c.

Patch by Carlo Kok!

git-svn-id: https://llvm.org/svn/llvm-project/llvm/trunk@156479 91177308-0d34-0410-b5e6-96231b3b80d8

include/llvm-c/Core.h
lib/VMCore/Core.cpp

index 77746069a25c700b637cb0db2e6e0891dac92342..87bf070a051fcaa678f75265312b97067899f79b 100644 (file)
@@ -477,6 +477,15 @@ void LLVMSetTarget(LLVMModuleRef M, const char *Triple);
  */
 void LLVMDumpModule(LLVMModuleRef M);
 
+/**
+ * Print a representation of a module to a file. The ErrorMessage needs to be
+ * disposed with LLVMDisposeMessage. Returns 0 on success, 1 otherwise.
+ *
+ * @see Module::print()
+ */
+LLVMBool LLVMPrintModuleToFile(LLVMModuleRef M, const char *Filename,
+                               char **ErrorMessage);
+
 /**
  * Set inline assembly for a module.
  *
index a9cca22d0dd4c42eba720bd7869c344fae635f10..30d8a9b12f8c030728fa6112447bb3986c499d88 100644 (file)
@@ -115,6 +115,25 @@ void LLVMDumpModule(LLVMModuleRef M) {
   unwrap(M)->dump();
 }
 
+LLVMBool LLVMPrintModuleToFile(LLVMModuleRef M, const char *Filename,
+                               char **ErrorMessage) {
+  std::string error;
+  raw_fd_ostream dest(Filename, error);
+  if (!error.empty()) {
+    *ErrorMessage = strdup(error.c_str());
+    return true;
+  }
+
+  unwrap(M)->print(dest, NULL);
+
+  if (!error.empty()) {
+    *ErrorMessage = strdup(error.c_str());
+    return true;
+  }
+  dest.flush();
+  return false;
+}
+
 /*--.. Operations on inline assembler ......................................--*/
 void LLVMSetModuleInlineAsm(LLVMModuleRef M, const char *Asm) {
   unwrap(M)->setModuleInlineAsm(StringRef(Asm));