From b4bc424c9acd787fbfcd1506aadd6e49113f6b1c Mon Sep 17 00:00:00 2001 From: Mehdi Amini Date: Fri, 24 Jul 2015 01:44:39 +0000 Subject: Remove access to the DataLayout in the TargetMachine Summary: Replace getDataLayout() with a createDataLayout() method to make explicit that it is intended to create a DataLayout only and not accessing it for other purpose. This change is the last of a series of commits dedicated to have a single DataLayout during compilation by using always the one owned by the module. Reviewers: echristo Subscribers: jholewinski, llvm-commits, rafael, yaron.keren Differential Revision: http://reviews.llvm.org/D11103 (cherry picked from commit 5609fc56bca971e5a7efeaa6ca4676638eaec5ea) From: Mehdi Amini llvm-svn: 243083 --- llvm/lib/Target/TargetMachineC.cpp | 30 +++++++++++++++++------------- 1 file changed, 17 insertions(+), 13 deletions(-) (limited to 'llvm/lib/Target/TargetMachineC.cpp') diff --git a/llvm/lib/Target/TargetMachineC.cpp b/llvm/lib/Target/TargetMachineC.cpp index 719923558de..eae23e6e67f 100644 --- a/llvm/lib/Target/TargetMachineC.cpp +++ b/llvm/lib/Target/TargetMachineC.cpp @@ -32,15 +32,25 @@ using namespace llvm; + +// The TargetMachine uses to offer access to a DataLayout member. This is reflected +// in the C API. For backward compatibility reason, this structure allows to keep +// a DataLayout member accessible to C client that have a handle to a +// LLVMTargetMachineRef. +struct LLVMOpaqueTargetMachine { + std::unique_ptr Machine; + DataLayout DL; +}; + + inline TargetMachine *unwrap(LLVMTargetMachineRef P) { - return reinterpret_cast(P); + return P->Machine.get(); } inline Target *unwrap(LLVMTargetRef P) { return reinterpret_cast(P); } inline LLVMTargetMachineRef wrap(const TargetMachine *P) { - return - reinterpret_cast(const_cast(P)); + return new LLVMOpaqueTargetMachine{ std::unique_ptr(const_cast(P)), P->createDataLayout() }; } inline LLVMTargetRef wrap(const Target * P) { return reinterpret_cast(const_cast(P)); @@ -147,7 +157,7 @@ LLVMTargetMachineRef LLVMCreateTargetMachine(LLVMTargetRef T, void LLVMDisposeTargetMachine(LLVMTargetMachineRef T) { - delete unwrap(T); + delete T; } LLVMTargetRef LLVMGetTargetMachineTarget(LLVMTargetMachineRef T) { @@ -170,8 +180,9 @@ char* LLVMGetTargetMachineFeatureString(LLVMTargetMachineRef T) { return strdup(StringRep.c_str()); } +/// @deprecated: see "struct LLVMOpaqueTargetMachine" description above LLVMTargetDataRef LLVMGetTargetMachineData(LLVMTargetMachineRef T) { - return wrap(unwrap(T)->getDataLayout()); + return wrap(&T->DL); } void LLVMSetTargetMachineAsmVerbosity(LLVMTargetMachineRef T, @@ -190,14 +201,7 @@ static LLVMBool LLVMTargetMachineEmit(LLVMTargetMachineRef T, LLVMModuleRef M, std::string error; - const DataLayout *td = TM->getDataLayout(); - - if (!td) { - error = "No DataLayout in TargetMachine"; - *ErrorMessage = strdup(error.c_str()); - return true; - } - Mod->setDataLayout(*td); + Mod->setDataLayout(TM->createDataLayout()); TargetMachine::CodeGenFileType ft; switch (codegen) { -- cgit v1.2.3