[llvm] r265394 - [llvm-c] Expose LLVM{Get,Set}ModuleIdentifier
Peter Zotov via llvm-commits
llvm-commits at lists.llvm.org
Tue Apr 5 06:56:59 PDT 2016
Author: whitequark
Date: Tue Apr 5 08:56:59 2016
New Revision: 265394
URL: http://llvm.org/viewvc/llvm-project?rev=265394&view=rev
Log:
[llvm-c] Expose LLVM{Get,Set}ModuleIdentifier
Patch by Nicole Mazzuca <npmazzuca at gmail.com>.
Differential Revision: http://reviews.llvm.org/D18736
Modified:
llvm/trunk/include/llvm-c/Core.h
llvm/trunk/lib/IR/Core.cpp
llvm/trunk/tools/llvm-c-test/echo.cpp
Modified: llvm/trunk/include/llvm-c/Core.h
URL: http://llvm.org/viewvc/llvm-project/llvm/trunk/include/llvm-c/Core.h?rev=265394&r1=265393&r2=265394&view=diff
==============================================================================
--- llvm/trunk/include/llvm-c/Core.h (original)
+++ llvm/trunk/include/llvm-c/Core.h Tue Apr 5 08:56:59 2016
@@ -481,6 +481,26 @@ LLVMModuleRef LLVMCloneModule(LLVMModule
void LLVMDisposeModule(LLVMModuleRef M);
/**
+ * Obtain the identifier of a module.
+ *
+ * @param M Module to obtain identifier of
+ * @param Len Out parameter which holds the length of the returned string.
+ * @return The identifier of M.
+ * @see Module::getModuleIdentifier()
+ */
+const char *LLVMGetModuleIdentifier(LLVMModuleRef M, size_t *Len);
+
+/**
+ * Set the identifier of a module to a string Ident with length Len.
+ *
+ * @param M The module to set identifier
+ * @param Ident The string to set M's identifier to
+ * @param Len Length of Ident
+ * @see Module::setModuleIdentifier()
+ */
+void LLVMSetModuleIdentifier(LLVMModuleRef M, const char *Ident, size_t Len);
+
+/**
* Obtain the data layout for a module.
*
* @see Module::getDataLayoutStr()
Modified: llvm/trunk/lib/IR/Core.cpp
URL: http://llvm.org/viewvc/llvm-project/llvm/trunk/lib/IR/Core.cpp?rev=265394&r1=265393&r2=265394&view=diff
==============================================================================
--- llvm/trunk/lib/IR/Core.cpp (original)
+++ llvm/trunk/lib/IR/Core.cpp Tue Apr 5 08:56:59 2016
@@ -157,6 +157,17 @@ void LLVMDisposeModule(LLVMModuleRef M)
delete unwrap(M);
}
+const char *LLVMGetModuleIdentifier(LLVMModuleRef M, size_t *Len) {
+ auto &Str = unwrap(M)->getModuleIdentifier();
+ *Len = Str.length();
+ return Str.c_str();
+}
+
+void LLVMSetModuleIdentifier(LLVMModuleRef M, const char *Ident, size_t Len) {
+ unwrap(M)->setModuleIdentifier(StringRef(Ident, Len));
+}
+
+
/*--.. Data layout .........................................................--*/
const char *LLVMGetDataLayoutStr(LLVMModuleRef M) {
return unwrap(M)->getDataLayoutStr().c_str();
Modified: llvm/trunk/tools/llvm-c-test/echo.cpp
URL: http://llvm.org/viewvc/llvm-project/llvm/trunk/tools/llvm-c-test/echo.cpp?rev=265394&r1=265393&r2=265394&view=diff
==============================================================================
--- llvm/trunk/tools/llvm-c-test/echo.cpp (original)
+++ llvm/trunk/tools/llvm-c-test/echo.cpp Tue Apr 5 08:56:59 2016
@@ -865,9 +865,18 @@ int llvm_echo(void) {
LLVMEnablePrettyStackTrace();
LLVMModuleRef Src = llvm_load_module(false, true);
-
+ size_t Len;
+ const char *ModuleName = LLVMGetModuleIdentifier(Src, &Len);
LLVMContextRef Ctx = LLVMContextCreate();
- LLVMModuleRef M = LLVMModuleCreateWithNameInContext("<stdin>", Ctx);
+ LLVMModuleRef M = LLVMModuleCreateWithNameInContext(ModuleName, Ctx);
+
+ // This whole switcharound is done because the C API has no way to
+ // set the source_filename
+ LLVMSetModuleIdentifier(M, "", 0);
+ LLVMGetModuleIdentifier(M, &Len);
+ if (Len != 0)
+ report_fatal_error("LLVM{Set,Get}ModuleIdentifier failed");
+ LLVMSetModuleIdentifier(M, ModuleName, strlen(ModuleName));
LLVMSetTarget(M, LLVMGetTarget(Src));
LLVMSetModuleDataLayout(M, LLVMGetModuleDataLayout(Src));
More information about the llvm-commits
mailing list