From 2b8aed70d6d60c10f0bea8bbc6de0d4578debfe7 Mon Sep 17 00:00:00 2001 From: Peter Zotov Date: Tue, 5 Apr 2016 13:56:59 +0000 Subject: [PATCH] [llvm-c] Expose LLVM{Get,Set}ModuleIdentifier Patch by Nicole Mazzuca . Differential Revision: http://reviews.llvm.org/D18736 git-svn-id: https://llvm.org/svn/llvm-project/llvm/trunk@265394 91177308-0d34-0410-b5e6-96231b3b80d8 --- include/llvm-c/Core.h | 20 ++++++++++++++++++++ lib/IR/Core.cpp | 11 +++++++++++ tools/llvm-c-test/echo.cpp | 13 +++++++++++-- 3 files changed, 42 insertions(+), 2 deletions(-) diff --git a/include/llvm-c/Core.h b/include/llvm-c/Core.h index 20c5df3e787..8e70c872a29 100644 --- a/include/llvm-c/Core.h +++ b/include/llvm-c/Core.h @@ -481,6 +481,26 @@ LLVMModuleRef LLVMCloneModule(LLVMModuleRef M); 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() diff --git a/lib/IR/Core.cpp b/lib/IR/Core.cpp index b04ff83140d..b2c2740218e 100644 --- a/lib/IR/Core.cpp +++ b/lib/IR/Core.cpp @@ -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(); diff --git a/tools/llvm-c-test/echo.cpp b/tools/llvm-c-test/echo.cpp index 5a2102a1579..1922b429eb2 100644 --- a/tools/llvm-c-test/echo.cpp +++ b/tools/llvm-c-test/echo.cpp @@ -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("", 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)); -- 2.11.0