Brought LLDB top-of-tree into sync with LLVM/Clang
top-of-tree.  Removed all local patches and llvm.zip.

The intent is that fron now on top-of-tree will
always build against LLVM/Clang top-of-tree, and
that problems building will be resolved as they
occur.  Stable release branches of LLDB can be
constructed as needed and linked to specific release
branches of LLVM/Clang.


git-svn-id: https://llvm.org/svn/llvm-project/lldb/trunk@164563 91177308-0d34-0410-b5e6-96231b3b80d8
diff --git a/source/Expression/ASTStructExtractor.cpp b/source/Expression/ASTStructExtractor.cpp
index 7bc31a5..d1f2192 100644
--- a/source/Expression/ASTStructExtractor.cpp
+++ b/source/Expression/ASTStructExtractor.cpp
@@ -59,13 +59,42 @@
 void
 ASTStructExtractor::ExtractFromFunctionDecl(FunctionDecl *F)
 {
-    DeclarationName struct_name(&m_ast_context->Idents.get(m_struct_name.c_str()));
-    RecordDecl::lookup_result struct_lookup = F->lookup(struct_name);
-    
-    if (struct_lookup.first == struct_lookup.second)
+    if (!F->hasBody())
         return;
     
-    RecordDecl *struct_decl = dyn_cast<RecordDecl>(*(struct_lookup.first));
+    Stmt *body_stmt = F->getBody();
+    CompoundStmt *body_compound_stmt = dyn_cast<CompoundStmt>(body_stmt);
+    
+    if (!body_compound_stmt)
+        return; // do we have to handle this?
+    
+    RecordDecl *struct_decl = NULL;
+    
+    StringRef desired_name(m_struct_name.c_str());
+    
+    for (CompoundStmt::const_body_iterator bi = body_compound_stmt->body_begin(), be = body_compound_stmt->body_end();
+         bi != be;
+         ++bi)
+    {
+        Stmt *curr_stmt = *bi;
+        DeclStmt *curr_decl_stmt = dyn_cast<DeclStmt>(curr_stmt);
+        if (!curr_decl_stmt)
+            continue;
+        DeclGroupRef decl_group = curr_decl_stmt->getDeclGroup();
+        for (Decl *candidate_decl : decl_group)
+        {
+            RecordDecl *candidate_record_decl = dyn_cast<RecordDecl>(candidate_decl);
+            if (!candidate_record_decl)
+                continue;
+            if (candidate_record_decl->getName() == desired_name)
+            {
+                struct_decl = candidate_record_decl;
+                break;
+            }
+        }
+        if (struct_decl)
+            break;
+    }
     
     if (!struct_decl)
         return;
diff --git a/source/Expression/ClangASTSource.cpp b/source/Expression/ClangASTSource.cpp
index 3d3a6f6..ef84b50 100644
--- a/source/Expression/ClangASTSource.cpp
+++ b/source/Expression/ClangASTSource.cpp
@@ -439,9 +439,9 @@
             {
                 ASTDumper ast_dumper(decl);
                 if (const NamedDecl *context_named_decl = dyn_cast<NamedDecl>(context_decl))
-                    log->Printf("  FELD[%d] Adding [to %s] lexical decl %s", current_id, context_named_decl->getNameAsString().c_str(), ast_dumper.GetCString());
+                    log->Printf("  FELD[%d] Adding [to %sDecl %s] lexical %sDecl %s", current_id, context_named_decl->getDeclKindName(), context_named_decl->getNameAsString().c_str(), decl->getDeclKindName(), ast_dumper.GetCString());
                 else
-                    log->Printf("  FELD[%d] Adding lexical decl %s", current_id, ast_dumper.GetCString());
+                    log->Printf("  FELD[%d] Adding lexical %sDecl %s", current_id, decl->getDeclKindName(), ast_dumper.GetCString());
             }
             
             Decl *copied_decl = m_ast_importer->CopyDecl(m_ast_context, original_ctx, decl);
diff --git a/source/Expression/ClangExpressionDeclMap.cpp b/source/Expression/ClangExpressionDeclMap.cpp
index 3391027..f4bdfc0 100644
--- a/source/Expression/ClangExpressionDeclMap.cpp
+++ b/source/Expression/ClangExpressionDeclMap.cpp
@@ -13,6 +13,7 @@
 // C++ Includes
 // Other libraries and framework includes
 // Project includes
+#include "clang/AST/ASTContext.h"
 #include "clang/AST/DeclarationName.h"
 #include "clang/AST/Decl.h"
 #include "lldb/lldb-private.h"
diff --git a/source/Expression/ClangExpressionParser.cpp b/source/Expression/ClangExpressionParser.cpp
index 5dd2ce2..ea03def 100644
--- a/source/Expression/ClangExpressionParser.cpp
+++ b/source/Expression/ClangExpressionParser.cpp
@@ -43,12 +43,13 @@
 #include "clang/Frontend/TextDiagnosticPrinter.h"
 #include "clang/Lex/Preprocessor.h"
 #include "clang/Parse/ParseAST.h"
-#include "clang/Rewrite/FrontendActions.h"
+#include "clang/Rewrite/Frontend/FrontendActions.h"
 #include "clang/Sema/SemaConsumer.h"
 #include "clang/StaticAnalyzer/Frontend/FrontendActions.h"
 
 #include "llvm/ADT/StringRef.h"
 #include "llvm/ExecutionEngine/ExecutionEngine.h"
+#include "llvm/Support/Debug.h"
 #include "llvm/Support/TargetSelect.h"
 
 #if !defined(__APPLE__)
@@ -199,11 +200,59 @@
             llvm::DisablePrettyStackTrace = true;
         }
     } InitializeLLVM;
-        
+    
+    llvm::setCurrentDebugType("internalize");
+    llvm::DebugFlag = true;
+    
     // 1. Create a new compiler instance.
     m_compiler.reset(new CompilerInstance());    
     
-    // 2. Set options.
+    // 2. Install the target.
+
+    lldb::TargetSP target_sp;
+    if (exe_scope)
+        target_sp = exe_scope->CalculateTarget();
+    
+    // TODO: figure out what to really do when we don't have a valid target.
+    // Sometimes this will be ok to just use the host target triple (when we
+    // evaluate say "2+3", but other expressions like breakpoint conditions
+    // and other things that _are_ target specific really shouldn't just be
+    // using the host triple. This needs to be fixed in a better way.
+    if (target_sp && target_sp->GetArchitecture().IsValid())
+    {
+        std::string triple = target_sp->GetArchitecture().GetTriple().str();
+        
+        int dash_count = 0;
+        for (size_t i = 0; i < triple.size(); ++i)
+        {
+            if (triple[i] == '-')
+                dash_count++;
+            if (dash_count == 3)
+            {
+                triple.resize(i);
+                break;
+            }
+        }
+        
+        m_compiler->getTargetOpts().Triple = triple;
+    }
+    else
+    {
+        m_compiler->getTargetOpts().Triple = llvm::sys::getDefaultTargetTriple();
+    }
+    
+    if (m_compiler->getTargetOpts().Triple.find("ios") != std::string::npos)
+        m_compiler->getTargetOpts().ABI = "apcs-gnu";
+    
+    m_compiler->createDiagnostics(0, 0);
+    
+    // Create the target instance.
+    m_compiler->setTarget(TargetInfo::CreateTargetInfo(m_compiler->getDiagnostics(),
+                                                       m_compiler->getTargetOpts()));
+    
+    assert (m_compiler->hasTarget());
+    
+    // 3. Set options.
     
     lldb::LanguageType language = expr.Language();
     
@@ -247,15 +296,12 @@
         if (process_sp->GetObjCLanguageRuntime())
         {
             if (process_sp->GetObjCLanguageRuntime()->GetRuntimeVersion() == eAppleObjC_V2)
-            {
-                m_compiler->getLangOpts().ObjCNonFragileABI = true;     // NOT i386
-                m_compiler->getLangOpts().ObjCNonFragileABI2 = true;    // NOT i386
-            }
+                m_compiler->getLangOpts().ObjCRuntime.set(ObjCRuntime::MacOSX, VersionTuple(10, 7));
+            else
+                m_compiler->getLangOpts().ObjCRuntime.set(ObjCRuntime::FragileMacOSX, VersionTuple(10, 7));
             
             if (process_sp->GetObjCLanguageRuntime()->HasNewLiteralsAndIndexing())
-            {
                 m_compiler->getLangOpts().DebuggerObjCLiteral = true;
-            }
         }
     }
 
@@ -270,51 +316,6 @@
     // Disable some warnings.
     m_compiler->getDiagnosticOpts().Warnings.push_back("no-unused-value");
     
-    // Set the target triple.
-    lldb::TargetSP target_sp;
-    if (exe_scope)
-        target_sp = exe_scope->CalculateTarget();
-
-    // TODO: figure out what to really do when we don't have a valid target.
-    // Sometimes this will be ok to just use the host target triple (when we
-    // evaluate say "2+3", but other expressions like breakpoint conditions
-    // and other things that _are_ target specific really shouldn't just be 
-    // using the host triple. This needs to be fixed in a better way.
-    if (target_sp && target_sp->GetArchitecture().IsValid())
-    {
-        std::string triple = target_sp->GetArchitecture().GetTriple().str();
-        
-        int dash_count = 0;
-        for (size_t i = 0; i < triple.size(); ++i)
-        {
-            if (triple[i] == '-')
-                dash_count++;
-            if (dash_count == 3)
-            {
-                triple.resize(i);
-                break;
-            }
-        }
-        
-        m_compiler->getTargetOpts().Triple = triple;
-    }
-    else
-    {
-        m_compiler->getTargetOpts().Triple = llvm::sys::getDefaultTargetTriple();
-    }
-    
-    if (m_compiler->getTargetOpts().Triple.find("ios") != std::string::npos)
-        m_compiler->getTargetOpts().ABI = "apcs-gnu";
-        
-    // 3. Set up various important bits of infrastructure.
-    m_compiler->createDiagnostics(0, 0);
-    
-    // Create the target instance.
-    m_compiler->setTarget(TargetInfo::CreateTargetInfo(m_compiler->getDiagnostics(),
-                                                       m_compiler->getTargetOpts()));
-    
-    assert (m_compiler->hasTarget());
-    
     // Inform the target of the language options
     //
     // FIXME: We shouldn't need to do this, the target should be immutable once
@@ -509,7 +510,7 @@
                                   error_stream,
                                   function_name.c_str());
         
-        ir_for_target.runOnModule(*module);
+        bool ir_can_run = ir_for_target.runOnModule(*module);
         
         Error &interpreter_error(ir_for_target.getInterpreterError());
         
@@ -534,6 +535,13 @@
 
             return err;
         }
+        else if (!ir_can_run)
+        {
+            err.SetErrorToGenericError();
+            err.SetErrorString("The expression could not be prepared to run in the target");
+            
+            return err;
+        }
         
         if (execution_policy != eExecutionPolicyNever &&
             m_expr.NeedsValidation() && 
@@ -599,7 +607,18 @@
         .setAllocateGVsWithCode(true)
         .setCodeModel(CodeModel::Small)
         .setUseMCJIT(true);
-    execution_engine.reset(builder.create());
+    
+    llvm::Triple triple(module->getTargetTriple());
+    StringRef mArch;
+    StringRef mCPU;
+    SmallVector<std::string, 0> mAttrs;
+    
+    TargetMachine *target_machine = builder.selectTarget(triple,
+                                                         mArch,
+                                                         mCPU,
+                                                         mAttrs);
+    
+    execution_engine.reset(builder.create(target_machine));
         
     if (!execution_engine.get())
     {
diff --git a/source/Expression/IRForTarget.cpp b/source/Expression/IRForTarget.cpp
index 544c4bc..b74e9f9 100644
--- a/source/Expression/IRForTarget.cpp
+++ b/source/Expression/IRForTarget.cpp
@@ -15,7 +15,9 @@
 #include "llvm/Instructions.h"
 #include "llvm/Intrinsics.h"
 #include "llvm/Module.h"
+#include "llvm/PassManager.h"
 #include "llvm/Target/TargetData.h"
+#include "llvm/Transforms/IPO.h"
 #include "llvm/ValueSymbolTable.h"
 
 #include "clang/AST/ASTContext.h"
@@ -2630,6 +2632,55 @@
 }
 
 bool
+IRForTarget::StripAllGVs (Module &llvm_module)
+{
+    lldb::LogSP log(lldb_private::GetLogIfAllCategoriesSet (LIBLLDB_LOG_EXPRESSIONS));    
+    std::vector<GlobalVariable *> global_vars;
+    std::set<GlobalVariable *>erased_vars;
+    
+    bool erased = true;
+    
+    while (erased)
+    {
+        erased = false;
+        
+        for (Module::global_iterator gi = llvm_module.global_begin(), ge = llvm_module.global_end();
+             gi != ge;
+             ++gi)
+        {
+            GlobalVariable *global_var = dyn_cast<GlobalVariable>(gi);
+        
+            global_var->removeDeadConstantUsers();
+            
+            if (global_var->use_empty())
+            {
+                if (log)
+                    log->Printf("Did remove %s",
+                                PrintValue(global_var).c_str());
+                global_var->eraseFromParent();
+                erased = true;
+                break;
+            }
+        }
+    }
+    
+    for (Module::global_iterator gi = llvm_module.global_begin(), ge = llvm_module.global_end();
+         gi != ge;
+         ++gi)
+    {
+        GlobalVariable *global_var = dyn_cast<GlobalVariable>(gi);
+
+        GlobalValue::use_iterator ui = global_var->use_begin();
+        
+        log->Printf("Couldn't remove %s because of %s",
+                    PrintValue(global_var).c_str(),
+                    PrintValue(*ui).c_str());
+    }
+    
+    return true;
+}
+
+bool
 IRForTarget::runOnModule (Module &llvm_module)
 {
     lldb::LogSP log(lldb_private::GetLogIfAllCategoriesSet (LIBLLDB_LOG_EXPRESSIONS));
@@ -2674,12 +2725,12 @@
     
     m_reloc_placeholder = new llvm::GlobalVariable((*m_module), 
                                                    intptr_ty,
-                                                   false /* isConstant */,
+                                                   false /* IsConstant */,
                                                    GlobalVariable::InternalLinkage,
                                                    Constant::getNullValue(intptr_ty),
                                                    "reloc_placeholder",
                                                    NULL /* InsertBefore */,
-                                                   false /* ThreadLocal */,
+                                                   GlobalVariable::NotThreadLocal /* ThreadLocal */,
                                                    0 /* AddressSpace */);
         
     Function::iterator bbi;
@@ -2876,6 +2927,12 @@
         return false;
     }
     
+    if (!StripAllGVs(llvm_module))
+    {
+        if (log)
+            log->Printf("StripAllGVs() failed");
+    }
+    
     if (log && log->GetVerbose())
     {
         std::string s;