From 2906b519d1f47f1b269ed1be9c70f752b3dca928 Mon Sep 17 00:00:00 2001 From: Anders Waldenborg Date: Wed, 16 Oct 2013 18:00:54 +0000 Subject: [PATCH] [llvm-c] Add LLVMPrintModuleToString. 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 | 8 ++++++++ lib/IR/Core.cpp | 10 ++++++++++ 2 files changed, 18 insertions(+) diff --git a/include/llvm-c/Core.h b/include/llvm-c/Core.h index 57834c52928..caadab1bac2 100644 --- a/include/llvm-c/Core.h +++ b/include/llvm-c/Core.h @@ -540,6 +540,14 @@ void LLVMDumpModule(LLVMModuleRef M); 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. * diff --git a/lib/IR/Core.cpp b/lib/IR/Core.cpp index 95c516a2511..cf1c1df0ed6 100644 --- a/lib/IR/Core.cpp +++ b/lib/IR/Core.cpp @@ -147,6 +147,16 @@ LLVMBool LLVMPrintModuleToFile(LLVMModuleRef M, const char *Filename, 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)); -- 2.34.1