diff --git a/include/llvm-c/Core.h b/include/llvm-c/Core.h index 77746069a25..87bf070a051 100644 --- a/include/llvm-c/Core.h +++ b/include/llvm-c/Core.h @@ -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. * diff --git a/lib/VMCore/Core.cpp b/lib/VMCore/Core.cpp index a9cca22d0dd..30d8a9b12f8 100644 --- a/lib/VMCore/Core.cpp +++ b/lib/VMCore/Core.cpp @@ -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));