[llvm-commits] [PATCH] llvm-c dump IL to file

Carlo Kok ck at remobjects.com
Tue May 8 08:23:20 PDT 2012


Op 5/8/2012 5:09 PM, Hans Wennborg schreef:
> On Fri, May 4, 2012 at 7:55 PM, Carlo Kok<ck at remobjects.com>  wrote:
>> In this patch:
>>
>> Newly introduced llvm-c function LLVMPrintModuleToFile 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.
>>
>
> Some comments:
>
>> + * Print a representation of a module to a file. The ErrorMessage needs to be disposed with LLVMDisposeMessage.
> Lines should be<= 80 columns wide.
>
>> +LLVMBool LLVMPrintModuleToFile(LLVMModuleRef M, const char *Filename, char** ErrorMessage);
> The * should be on the variable name also for ErrorMessage.
>
>> +LLVMBool LLVMPrintModuleToFile(LLVMModuleRef M, const char *Filename, char** ErrorMessage) {
>> +  std::string error;
>> +  raw_fd_ostream dest(Filename, error);
> I think you'll need to check 'error' here (as well as below) to see if
> there were problems opening the file.

Hi Thomas,

Thanks. All changes are applied in attached updated patch.

--
Carlo Kok

-------------- next part --------------
Index: include/llvm-c/Core.h
===================================================================
--- include/llvm-c/Core.h	(revision 156171)
+++ include/llvm-c/Core.h	(working copy)
@@ -478,6 +478,15 @@
 void LLVMDumpModule(LLVMModuleRef M);
 
 /**
+ * Print a representation of a module to a file. The ErrorMessage needs to be 
+ * disposed with LLVMDisposeMessage.
+ *
+ * @see Module::print()
+ */
+LLVMBool LLVMPrintModuleToFile(LLVMModuleRef M, const char *Filename, 
+                               char **ErrorMessage);
+
+/**
  * Set inline assembly for a module.
  *
  * @see Module::setModuleInlineAsm()
Index: lib/VMCore/Core.cpp
===================================================================
--- lib/VMCore/Core.cpp	(revision 156171)
+++ lib/VMCore/Core.cpp	(working copy)
@@ -115,6 +115,25 @@
   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));


More information about the llvm-commits mailing list