llvm.org GIT mirror llvm / 2b8aed7
[llvm-c] Expose LLVM{Get,Set}ModuleIdentifier Patch by Nicole Mazzuca <npmazzuca@gmail.com>. Differential Revision: http://reviews.llvm.org/D18736 git-svn-id: https://llvm.org/svn/llvm-project/llvm/trunk@265394 91177308-0d34-0410-b5e6-96231b3b80d8 Peter Zotov 4 years ago
3 changed file(s) with 42 addition(s) and 2 deletion(s). Raw diff Collapse all Expand all
478478 * leaked.
479479 */
480480 void LLVMDisposeModule(LLVMModuleRef M);
481
482 /**
483 * Obtain the identifier of a module.
484 *
485 * @param M Module to obtain identifier of
486 * @param Len Out parameter which holds the length of the returned string.
487 * @return The identifier of M.
488 * @see Module::getModuleIdentifier()
489 */
490 const char *LLVMGetModuleIdentifier(LLVMModuleRef M, size_t *Len);
491
492 /**
493 * Set the identifier of a module to a string Ident with length Len.
494 *
495 * @param M The module to set identifier
496 * @param Ident The string to set M's identifier to
497 * @param Len Length of Ident
498 * @see Module::setModuleIdentifier()
499 */
500 void LLVMSetModuleIdentifier(LLVMModuleRef M, const char *Ident, size_t Len);
481501
482502 /**
483503 * Obtain the data layout for a module.
155155 void LLVMDisposeModule(LLVMModuleRef M) {
156156 delete unwrap(M);
157157 }
158
159 const char *LLVMGetModuleIdentifier(LLVMModuleRef M, size_t *Len) {
160 auto &Str = unwrap(M)->getModuleIdentifier();
161 *Len = Str.length();
162 return Str.c_str();
163 }
164
165 void LLVMSetModuleIdentifier(LLVMModuleRef M, const char *Ident, size_t Len) {
166 unwrap(M)->setModuleIdentifier(StringRef(Ident, Len));
167 }
168
158169
159170 /*--.. Data layout .........................................................--*/
160171 const char *LLVMGetDataLayoutStr(LLVMModuleRef M) {
864864 LLVMEnablePrettyStackTrace();
865865
866866 LLVMModuleRef Src = llvm_load_module(false, true);
867
867 size_t Len;
868 const char *ModuleName = LLVMGetModuleIdentifier(Src, &Len);
868869 LLVMContextRef Ctx = LLVMContextCreate();
869 LLVMModuleRef M = LLVMModuleCreateWithNameInContext("", Ctx);
870 LLVMModuleRef M = LLVMModuleCreateWithNameInContext(ModuleName, Ctx);
871
872 // This whole switcharound is done because the C API has no way to
873 // set the source_filename
874 LLVMSetModuleIdentifier(M, "", 0);
875 LLVMGetModuleIdentifier(M, &Len);
876 if (Len != 0)
877 report_fatal_error("LLVM{Set,Get}ModuleIdentifier failed");
878 LLVMSetModuleIdentifier(M, ModuleName, strlen(ModuleName));
870879
871880 LLVMSetTarget(M, LLVMGetTarget(Src));
872881 LLVMSetModuleDataLayout(M, LLVMGetModuleDataLayout(Src));