Switched the expression parser from using TargetData
to using Clang to get type sizes.  This fixes a bug
where the type size for a double[2] was being wrongly
reported as 8 instead of 16 bytes, causing problems
for IRForTarget.

Also improved logging so that the next bug in this
area will be easier to find.


git-svn-id: https://llvm.org/svn/llvm-project/llvdb/trunk@115208 91177308-0d34-0410-b5e6-96231b3b80d8
diff --git a/source/Expression/ClangExpressionParser.cpp b/source/Expression/ClangExpressionParser.cpp
index ec385a4..194a90d 100644
--- a/source/Expression/ClangExpressionParser.cpp
+++ b/source/Expression/ClangExpressionParser.cpp
@@ -436,21 +436,7 @@
     
     if (decl_map)
     {
-        std::string target_error;
-        
-        const llvm::Target *target = llvm::TargetRegistry::lookupTarget(m_target_triple, target_error);
-        
-        if (!target)
-        {
-            err.SetErrorToGenericError();
-            err.SetErrorStringWithFormat("Couldn't find a target for %s", m_target_triple.c_str());
-            return err;
-        }
-        
-        std::auto_ptr<llvm::TargetMachine> target_machine(target->createTargetMachine(m_target_triple, ""));
-        
         IRForTarget ir_for_target(decl_map, 
-                                  target_machine->getTargetData(),
                                   m_expr.NeedsVariableResolution(),
                                   function_name.c_str());
         
diff --git a/source/Expression/IRForTarget.cpp b/source/Expression/IRForTarget.cpp
index 6ddd6c1..662c086 100644
--- a/source/Expression/IRForTarget.cpp
+++ b/source/Expression/IRForTarget.cpp
@@ -32,22 +32,20 @@
 static char ID;
 
 IRForTarget::IRForTarget(lldb_private::ClangExpressionDeclMap *decl_map,
-                         const TargetData *target_data,
                          bool resolve_vars,
                          const char *func_name) :
     ModulePass(ID),
     m_decl_map(decl_map),
-    m_target_data(target_data),
     m_sel_registerName(NULL),
     m_func_name(func_name),
     m_resolve_vars(resolve_vars)
 {
 }
 
-/* A handy utility function used at several places in the code */
+/* Handy utility functions used at several places in the code */
 
 static std::string 
-PrintValue(Value *V, bool truncate = false)
+PrintValue(const Value *V, bool truncate = false)
 {
     std::string s;
     raw_string_ostream rso(s);
@@ -58,6 +56,18 @@
     return s;
 }
 
+static std::string
+PrintType(const Type *T, bool truncate = false)
+{
+    std::string s;
+    raw_string_ostream rso(s);
+    T->print(rso);
+    rso.flush();
+    if (truncate)
+        s.resize(s.length() - 1);
+    return s;
+}
+
 IRForTarget::~IRForTarget()
 {
 }
@@ -578,23 +588,34 @@
         
         std::string name = named_decl->getName().str();
         
-        void *qual_type = NULL;
+        void *opaque_type = NULL;
         clang::ASTContext *ast_context = NULL;
         
         if (clang::ValueDecl *value_decl = dyn_cast<clang::ValueDecl>(named_decl))
         {
-            qual_type = value_decl->getType().getAsOpaquePtr();
+            opaque_type = value_decl->getType().getAsOpaquePtr();
             ast_context = &value_decl->getASTContext();
         }
         else
         {
             return false;
         }
+        
+        clang::QualType qual_type(clang::QualType::getFromOpaquePtr(opaque_type));
             
         const Type *value_type = global_variable->getType();
-        
-        size_t value_size = m_target_data->getTypeStoreSize(value_type);
-        off_t value_alignment = m_target_data->getPrefTypeAlignment(value_type);
+                
+        size_t value_size = (ast_context->getTypeSize(qual_type) + 7) / 8;
+        off_t value_alignment = (ast_context->getTypeAlign(qual_type) + 7) / 8;
+                
+        if (log)
+            log->Printf("Type of %s is [clang %s, lldb %s] [size %d, align %d]", 
+                        name.c_str(), 
+                        qual_type.getAsString().c_str(), 
+                        PrintType(value_type).c_str(), 
+                        value_size, 
+                        value_alignment);
+
         
         if (named_decl && !m_decl_map->AddValueToStruct(named_decl,
                                                         name.c_str(),
@@ -1103,6 +1124,13 @@
             return false;
     }
     
+    ///////////////////////////////
+    // Run function-level passes
+    //
+    
+    if (!replaceVariables(M, *function))
+        return false;
+    
     if (log)
     {
         std::string s;
@@ -1115,13 +1143,6 @@
         log->Printf("Module after preparing for execution: \n%s", s.c_str());
     }
     
-    ///////////////////////////////
-    // Run function-level passes
-    //
-    
-    if (!replaceVariables(M, *function))
-        return false;
-    
     return true;    
 }