Made variable resolution more robust by handling
every external variable reference in the module,
and returning a clean error (instead of letting
LLVM issue a fatal error) if the variable could
not be resolved.


git-svn-id: https://llvm.org/svn/llvm-project/llvdb/trunk@118388 91177308-0d34-0410-b5e6-96231b3b80d8
diff --git a/source/Expression/IRForTarget.cpp b/source/Expression/IRForTarget.cpp
index 3ddb9ef..4fc461f 100644
--- a/source/Expression/IRForTarget.cpp
+++ b/source/Expression/IRForTarget.cpp
@@ -563,8 +563,7 @@
 IRForTarget::MaybeHandleVariable 
 (
     Module &llvm_module, 
-    Value *llvm_value_ptr,
-    bool Store
+    Value *llvm_value_ptr
 )
 {
     lldb::LogSP log(lldb_private::GetLogIfAllCategoriesSet (LIBLLDB_LOG_EXPRESSIONS));
@@ -578,7 +577,7 @@
         case Instruction::GetElementPtr:
         case Instruction::BitCast:
             Value *s = constant_expr->getOperand(0);
-            MaybeHandleVariable(llvm_module, s, Store);
+            MaybeHandleVariable(llvm_module, s);
         }
     }
     if (GlobalVariable *global_variable = dyn_cast<GlobalVariable>(llvm_value_ptr))
@@ -646,7 +645,7 @@
     for (unsigned op_index = 0, num_ops = C->getNumArgOperands();
          op_index < num_ops;
          ++op_index)
-        if (!MaybeHandleVariable(M, C->getArgOperand(op_index), true)) // conservatively believe that this is a store
+        if (!MaybeHandleVariable(M, C->getArgOperand(op_index))) // conservatively believe that this is a store
             return false;
     
     return true;
@@ -773,7 +772,7 @@
 }
 
 bool
-IRForTarget::resolveExternals(Module &M, BasicBlock &BB)
+IRForTarget::resolveCalls(Module &M, BasicBlock &BB)
 {        
     /////////////////////////////////////////////////////////////////////////
     // Prepare the current basic block for execution in the remote process
@@ -787,31 +786,31 @@
     {
         Instruction &inst = *ii;
         
-        if (m_resolve_vars)
-        {
-            if (LoadInst *load = dyn_cast<LoadInst>(&inst))
-                if (!MaybeHandleVariable(M, load->getPointerOperand(), false))
-                    return false;
-            
-            if (StoreInst *store = dyn_cast<StoreInst>(&inst))
-                if (!MaybeHandleVariable(M, store->getValueOperand(), true) ||
-                    !MaybeHandleVariable(M, store->getPointerOperand(), true))
-                    return false;
-        }
+        CallInst *call = dyn_cast<CallInst>(&inst);
         
-        if (CallInst *call = dyn_cast<CallInst>(&inst))
-        {
-            if (!MaybeHandleCallArguments(M, call))
-                return false;
-            
-            if (!MaybeHandleCall(M, call))
-                return false;
-        }
+        if (call && !MaybeHandleCall(M, call))
+            return false;
     }
     
     return true;
 }
 
+bool
+IRForTarget::resolveExternals(Module &M,
+                              Function &F)
+{
+    for (Module::global_iterator global = M.global_begin(), end = M.global_end();
+         global != end;
+         ++global)
+    {
+        if ((*global).hasExternalLinkage() &&
+            !MaybeHandleVariable (M, global))
+            return false;
+    }
+        
+    return true;
+}
+
 static bool isGuardVariableRef(Value *V)
 {
     Constant *C;
@@ -1156,7 +1155,7 @@
         if (!rewriteObjCSelectors(M, *bbi))
             return false;
 
-        if (!resolveExternals(M, *bbi))
+        if (!resolveCalls(M, *bbi))
             return false;
     }
     
@@ -1164,6 +1163,9 @@
     // Run function-level passes
     //
     
+    if (!resolveExternals(M, *function))
+        return false;
+    
     if (!replaceVariables(M, *function))
         return false;