[llvm-c] Add LLVMPrintModuleToString.
authorAnders Waldenborg <anders@0x63.nu>
Wed, 16 Oct 2013 18:00:54 +0000 (18:00 +0000)
committerAnders Waldenborg <anders@0x63.nu>
Wed, 16 Oct 2013 18:00:54 +0000 (18:00 +0000)
Like LLVMDumpModule but returns the string (that needs to be freed
with LLVMDisposeMessage) instead of printing it to stderr.

Differential Revision: http://llvm-reviews.chandlerc.com/D1941

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

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

index 57834c52928f94681bec3ceeb2417cb442a6011e..caadab1bac2c24c643522fdf182e8331a3ff9226 100644 (file)
@@ -540,6 +540,14 @@ void LLVMDumpModule(LLVMModuleRef M);
 LLVMBool LLVMPrintModuleToFile(LLVMModuleRef M, const char *Filename,
                                char **ErrorMessage);
 
 LLVMBool LLVMPrintModuleToFile(LLVMModuleRef M, const char *Filename,
                                char **ErrorMessage);
 
+/**
+ * Return a string representation of the module. Use
+ * LLVMDisposeMessage to free the string.
+ *
+ * @see Module::print()
+ */
+char *LLVMPrintModuleToString(LLVMModuleRef M);
+
 /**
  * Set inline assembly for a module.
  *
 /**
  * Set inline assembly for a module.
  *
index 95c516a251177785257ffaac46ecf384fe3056b8..cf1c1df0ed6770913a5d95d565f7d4fe89b6a5ed 100644 (file)
@@ -147,6 +147,16 @@ LLVMBool LLVMPrintModuleToFile(LLVMModuleRef M, const char *Filename,
   return false;
 }
 
   return false;
 }
 
+char *LLVMPrintModuleToString(LLVMModuleRef M) {
+  std::string buf;
+  raw_string_ostream os(buf);
+
+  unwrap(M)->print(os, NULL);
+  os.flush();
+
+  return strdup(buf.c_str());
+}
+
 /*--.. Operations on inline assembler ......................................--*/
 void LLVMSetModuleInlineAsm(LLVMModuleRef M, const char *Asm) {
   unwrap(M)->setModuleInlineAsm(StringRef(Asm));
 /*--.. Operations on inline assembler ......................................--*/
 void LLVMSetModuleInlineAsm(LLVMModuleRef M, const char *Asm) {
   unwrap(M)->setModuleInlineAsm(StringRef(Asm));