[llvm] r246057 - Revert "Fix LLVM C API for DataLayout"

Mehdi Amini via llvm-commits llvm-commits at lists.llvm.org
Wed Aug 26 12:25:00 PDT 2015


Author: mehdi_amini
Date: Wed Aug 26 14:24:59 2015
New Revision: 246057

URL: http://llvm.org/viewvc/llvm-project?rev=246057&view=rev
Log:
Revert "Fix LLVM C API for DataLayout"

This reverts commit r246052.
Third attempt, still unpleasant for some bots.

From: Mehdi Amini <mehdi.amini at apple.com>

Modified:
    llvm/trunk/include/llvm/Target/TargetMachine.h
    llvm/trunk/lib/Target/TargetMachineC.cpp

Modified: llvm/trunk/include/llvm/Target/TargetMachine.h
URL: http://llvm.org/viewvc/llvm-project/llvm/trunk/include/llvm/Target/TargetMachine.h?rev=246057&r1=246056&r2=246057&view=diff
==============================================================================
--- llvm/trunk/include/llvm/Target/TargetMachine.h (original)
+++ llvm/trunk/include/llvm/Target/TargetMachine.h Wed Aug 26 14:24:59 2015
@@ -23,13 +23,6 @@
 #include <cassert>
 #include <string>
 
-extern "C" {
-  // This function from the C API is deprecated. We still supports it using a
-  // private method on the TargetMachine for now. But it needs to be friended and
-  // so we forward declare it here.
-  LLVMTargetDataRef LLVMGetTargetMachineData(LLVMTargetMachineRef T);
-}
-
 namespace llvm {
 
 class InstrItineraryData;
@@ -110,12 +103,6 @@ protected: // Can only create subclasses
 
   unsigned RequireStructuredCFG : 1;
 
-  /// This API is here to support the C API, deprecated in 3.7 release.
-  /// This should never be used outside of legacy existing client.
-  const DataLayout &getDataLayout() const { return DL; }
-  friend struct LLVMOpaqueTargetData * ::LLVMGetTargetMachineData(
-      LLVMTargetMachineRef T);
-
 public:
   mutable TargetOptions Options;
 

Modified: llvm/trunk/lib/Target/TargetMachineC.cpp
URL: http://llvm.org/viewvc/llvm-project/llvm/trunk/lib/Target/TargetMachineC.cpp?rev=246057&r1=246056&r2=246057&view=diff
==============================================================================
--- llvm/trunk/lib/Target/TargetMachineC.cpp (original)
+++ llvm/trunk/lib/Target/TargetMachineC.cpp Wed Aug 26 14:24:59 2015
@@ -32,14 +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<TargetMachine> Machine;
+  DataLayout DL;
+};
+
+
 static TargetMachine *unwrap(LLVMTargetMachineRef P) {
-  return reinterpret_cast<TargetMachine *>(P);
+  return P->Machine.get();
 }
 static Target *unwrap(LLVMTargetRef P) {
   return reinterpret_cast<Target*>(P);
 }
 static LLVMTargetMachineRef wrap(const TargetMachine *P) {
-  return reinterpret_cast<LLVMTargetMachineRef>(const_cast<TargetMachine *>(P));
+  return new LLVMOpaqueTargetMachine{ std::unique_ptr<TargetMachine>(const_cast<TargetMachine*>(P)),  P->createDataLayout() };
 }
 static LLVMTargetRef wrap(const Target * P) {
   return reinterpret_cast<LLVMTargetRef>(const_cast<Target*>(P));
@@ -68,16 +79,16 @@ LLVMTargetRef LLVMGetTargetFromName(cons
 LLVMBool LLVMGetTargetFromTriple(const char* TripleStr, LLVMTargetRef *T,
                                  char **ErrorMessage) {
   std::string Error;
-
+  
   *T = wrap(TargetRegistry::lookupTarget(TripleStr, Error));
-
+  
   if (!*T) {
     if (ErrorMessage)
       *ErrorMessage = strdup(Error.c_str());
 
     return 1;
   }
-
+  
   return 0;
 }
 
@@ -144,7 +155,10 @@ LLVMTargetMachineRef LLVMCreateTargetMac
     CM, OL));
 }
 
-void LLVMDisposeTargetMachine(LLVMTargetMachineRef T) { delete unwrap(T); }
+
+void LLVMDisposeTargetMachine(LLVMTargetMachineRef T) {
+  delete T;
+}
 
 LLVMTargetRef LLVMGetTargetMachineTarget(LLVMTargetMachineRef T) {
   const Target* target = &(unwrap(T)->getTarget());
@@ -166,9 +180,9 @@ char* LLVMGetTargetMachineFeatureString(
   return strdup(StringRep.c_str());
 }
 
-/** Deprecated: use LLVMGetDataLayout(LLVMModuleRef M) instead. */
+/// @deprecated: see "struct LLVMOpaqueTargetMachine" description above
 LLVMTargetDataRef LLVMGetTargetMachineData(LLVMTargetMachineRef T) {
-  return wrap(&unwrap(T)->getDataLayout());
+  return wrap(&T->DL);
 }
 
 void LLVMSetTargetMachineAsmVerbosity(LLVMTargetMachineRef T,




More information about the llvm-commits mailing list