Added a mechanism for keeping track of where in
the debug information individual Decls came from.

We've had a metadata infrastructure for a while,
which was intended to solve a problem we've since
dealt with in a different way.  (It was meant to
keep track of which definition of an Objective-C
class was the "true" definition, but we now find
it by searching the symbols for the class symbol.)
The metadata is attached to the ExternalASTSource,
which means it has a one-to-one correspondence with
AST contexts.

I've repurposed the metadata infrastructure to
hold the object file and DIE offset for the DWARF
information corresponding to a Decl.  There are
methods in ClangASTContext that get and set this
metadata, and the ClangASTImporter is capable of
tracking down the metadata for Decls that have been
copied out of the debug information into the
parser's AST context without using any additional
memory.

To see the metadata, you just have to enable the
expression log:
-
(lldb) log enable lldb expr
-
and watch the import messages.  The high 32 bits
of the metadata indicate the index of the object
file in its containing DWARFDebugMap; I have also
added a log which you can use to track that mapping:
-
(lldb) log enable dwarf map
-

This adds 64 bits per Decl, which in my testing
hasn't turned out to be very much (debugging Clang
produces around 6500 Decls in my tests).  To track
how much data is being consumed, I've also added a
global variable g_TotalSizeOfMetadata which tracks
the total number of Decls that have metadata in all
active AST contexts.

Right now this metadata is enormously useful for
tracking down bugs in the debug info parser.  In the
future I also want to use this information to provide
more intelligent error messages instead of printing
empty source lines wherever Clang refers to the
location where something is defined.

llvm-svn: 154634
diff --git a/lldb/include/lldb/Symbol/ClangASTContext.h b/lldb/include/lldb/Symbol/ClangASTContext.h
index ab9caac..bdee0a0 100644
--- a/lldb/include/lldb/Symbol/ClangASTContext.h
+++ b/lldb/include/lldb/Symbol/ClangASTContext.h
@@ -143,6 +143,26 @@
     static bool
     GetCompleteDecl (clang::ASTContext *ast,
                      clang::Decl *decl);
+
+    void SetMetadata (uintptr_t object,
+                      uint64_t metadata)
+    {
+        SetMetadata(getASTContext(), object, metadata);
+    }
+    
+    static void
+    SetMetadata (clang::ASTContext *ast,
+                 uintptr_t object,
+                 uint64_t metadata);
+    
+    uint64_t GetMetadata (uintptr_t object)
+    {
+        return GetMetadata(getASTContext(), object);
+    }
+    
+    static uint64_t
+    GetMetadata (clang::ASTContext *ast,
+                 uintptr_t object);
     
     //------------------------------------------------------------------
     // Basic Types
@@ -270,7 +290,8 @@
                       lldb::AccessType access_type,
                       const char *name,
                       int kind,
-                      lldb::LanguageType language);
+                      lldb::LanguageType language,
+                      clang::CXXRecordDecl **decl = NULL);
 
     static clang::FieldDecl *
     AddFieldToRecordType (clang::ASTContext *ast,
@@ -929,15 +950,6 @@
     //------------------------------------------------------------------
     static unsigned
     GetTypeQualifiers(lldb::clang_type_t clang_type);
-    
-    //------------------------------------------------------------------
-    // Flags
-    //------------------------------------------------------------------
-    static uint64_t
-    GetTypeFlags(clang::ASTContext *ast, lldb::clang_type_t clang_type);
-    
-    static void
-    SetTypeFlags(clang::ASTContext *ast, lldb::clang_type_t clang_type, uint64_t flags);
 protected:
     //------------------------------------------------------------------
     // Classes that inherit from ClangASTContext can see and modify these
diff --git a/lldb/include/lldb/Symbol/ClangASTImporter.h b/lldb/include/lldb/Symbol/ClangASTImporter.h
index bffd5fd..6b68489 100644
--- a/lldb/include/lldb/Symbol/ClangASTImporter.h
+++ b/lldb/include/lldb/Symbol/ClangASTImporter.h
@@ -80,6 +80,9 @@
         return origin.Valid();
     }
     
+    uint64_t
+    GetDeclMetadata (const clang::Decl *decl);
+    
     //
     // Namespace maps
     //
diff --git a/lldb/include/lldb/Symbol/ClangExternalASTSourceCommon.h b/lldb/include/lldb/Symbol/ClangExternalASTSourceCommon.h
index fb09ed3..0efe1a6 100644
--- a/lldb/include/lldb/Symbol/ClangExternalASTSourceCommon.h
+++ b/lldb/include/lldb/Symbol/ClangExternalASTSourceCommon.h
@@ -42,6 +42,7 @@
 {
 public:
     ClangExternalASTSourceCommon();
+    ~ClangExternalASTSourceCommon();
     
     virtual uint64_t GetMetadata(uintptr_t object);
     virtual void SetMetadata(uintptr_t object, uint64_t metadata);
diff --git a/lldb/source/Plugins/SymbolFile/DWARF/LogChannelDWARF.cpp b/lldb/source/Plugins/SymbolFile/DWARF/LogChannelDWARF.cpp
index bb11f64..a21741b 100644
--- a/lldb/source/Plugins/SymbolFile/DWARF/LogChannelDWARF.cpp
+++ b/lldb/source/Plugins/SymbolFile/DWARF/LogChannelDWARF.cpp
@@ -109,6 +109,7 @@
         else if (::strcasecmp (arg, "pubtypes")   == 0) flag_bits &= ~DWARF_LOG_DEBUG_PUBTYPES;
         else if (::strcasecmp (arg, "aranges")    == 0) flag_bits &= ~DWARF_LOG_DEBUG_ARANGES;
         else if (::strcasecmp (arg, "lookups")    == 0) flag_bits &= ~DWARF_LOG_LOOKUPS;
+        else if (::strcasecmp (arg, "map")        == 0) flag_bits &= ~DWARF_LOG_DEBUG_MAP;
         else if (::strcasecmp (arg, "default")    == 0) flag_bits &= ~DWARF_LOG_DEFAULT;
         else if (::strncasecmp(arg, "comp", 4)    == 0) flag_bits &= ~DWARF_LOG_TYPE_COMPLETION;
         else
@@ -152,6 +153,7 @@
         else if (::strcasecmp (arg, "pubtypes")   == 0) flag_bits |= DWARF_LOG_DEBUG_PUBTYPES;
         else if (::strcasecmp (arg, "aranges")    == 0) flag_bits |= DWARF_LOG_DEBUG_ARANGES;
         else if (::strcasecmp (arg, "lookups")    == 0) flag_bits |= DWARF_LOG_LOOKUPS;
+        else if (::strcasecmp (arg, "map")        == 0) flag_bits |= DWARF_LOG_DEBUG_MAP;
         else if (::strcasecmp (arg, "default")    == 0) flag_bits |= DWARF_LOG_DEFAULT;
         else if (::strncasecmp(arg, "comp", 4)    == 0) flag_bits |= DWARF_LOG_TYPE_COMPLETION;
         else
@@ -181,7 +183,8 @@
                   "  pubnames - log the parsing if .debug_pubnames\n"
                   "  pubtypes - log the parsing if .debug_pubtypes\n"
                   "  lookups - log any lookups that happen by name, regex, or address\n\n"
-                  "  completion - log struct/unions/class type completions\n\n",
+                  "  completion - log struct/unions/class type completions\n\n"
+                  "  map - log insertions of object files into DWARF debug maps\n\n",
                   SymbolFileDWARF::GetPluginNameStatic());
 }
 
diff --git a/lldb/source/Plugins/SymbolFile/DWARF/LogChannelDWARF.h b/lldb/source/Plugins/SymbolFile/DWARF/LogChannelDWARF.h
index f009455..1f57f03 100644
--- a/lldb/source/Plugins/SymbolFile/DWARF/LogChannelDWARF.h
+++ b/lldb/source/Plugins/SymbolFile/DWARF/LogChannelDWARF.h
@@ -25,6 +25,7 @@
 #define DWARF_LOG_DEBUG_ARANGES     (1u << 5)
 #define DWARF_LOG_LOOKUPS           (1u << 6)
 #define DWARF_LOG_TYPE_COMPLETION   (1u << 7)
+#define DWARF_LOG_DEBUG_MAP         (1u << 8)
 #define DWARF_LOG_ALL               (UINT32_MAX)
 #define DWARF_LOG_DEFAULT           (DWARF_LOG_DEBUG_INFO)
 
diff --git a/lldb/source/Plugins/SymbolFile/DWARF/SymbolFileDWARF.cpp b/lldb/source/Plugins/SymbolFile/DWARF/SymbolFileDWARF.cpp
index 7072781..c325cc6 100644
--- a/lldb/source/Plugins/SymbolFile/DWARF/SymbolFileDWARF.cpp
+++ b/lldb/source/Plugins/SymbolFile/DWARF/SymbolFileDWARF.cpp
@@ -1667,6 +1667,8 @@
                                                                                         member_type->GetClangLayoutType(), 
                                                                                         accessibility, 
                                                                                         bit_size);
+                                
+                                GetClangASTContext().SetMetadata((uintptr_t)field_decl, MakeUserID(die->GetOffset()));
                             }
                             else
                             {
@@ -1736,6 +1738,8 @@
                                                                        prop_setter_name,
                                                                        prop_getter_name,
                                                                        prop_attributes);
+                            
+                            GetClangASTContext().SetMetadata((uintptr_t)ivar_decl, MakeUserID(die->GetOffset()));
                         }
                     }
                 }
@@ -3815,6 +3819,8 @@
                                                                                                                   storage);
                             assert(param_var_decl);
                             function_param_decls.push_back(param_var_decl);
+                            
+                            GetClangASTContext().SetMetadata((uintptr_t)param_var_decl, MakeUserID(die->GetOffset()));
                         }
                     }
                 }
@@ -5161,17 +5167,23 @@
                                                                                                                                                template_param_infos);
                                 clang_type = ast.CreateClassTemplateSpecializationType (class_specialization_decl);
                                 clang_type_was_created = true;
+                                
+                                GetClangASTContext().SetMetadata((uintptr_t)class_template_decl, MakeUserID(die->GetOffset()));
+                                GetClangASTContext().SetMetadata((uintptr_t)class_specialization_decl, MakeUserID(die->GetOffset()));
                             }
                         }
 
                         if (!clang_type_was_created)
                         {
                             clang_type_was_created = true;
+                            clang::CXXRecordDecl *record_decl;
                             clang_type = ast.CreateRecordType (decl_ctx, 
                                                                accessibility, 
                                                                type_name_cstr, 
                                                                tag_decl_kind, 
-                                                               class_language);
+                                                               class_language,
+                                                               &record_decl);
+                            GetClangASTContext().SetMetadata((uintptr_t)record_decl, MakeUserID(die->GetOffset()));
                         }
                     }
 
@@ -5530,6 +5542,7 @@
                                                                                       accessibility);
                                     LinkDeclContextToDIE(ClangASTContext::GetAsDeclContext(objc_method_decl), die);
                                     type_handled = objc_method_decl != NULL;
+                                    GetClangASTContext().SetMetadata((uintptr_t)objc_method_decl, MakeUserID(die->GetOffset()));
                                 }
                             }
                             else if (is_cxx_method)
@@ -5634,6 +5647,8 @@
                                                     Host::SetCrashDescription (NULL);
 
                                                     type_handled = cxx_method_decl != NULL;
+                                                    
+                                                    GetClangASTContext().SetMetadata((uintptr_t)cxx_method_decl, MakeUserID(die->GetOffset()));
                                                 }
                                             }
                                             else
@@ -5698,6 +5713,8 @@
                                 ast.SetFunctionParameters (function_decl, 
                                                            &function_param_decls.front(), 
                                                            function_param_decls.size());
+                            
+                            GetClangASTContext().SetMetadata((uintptr_t)function_decl, MakeUserID(die->GetOffset()));
                         }
                     }
                     type_sp.reset( new Type (MakeUserID(die->GetOffset()), 
diff --git a/lldb/source/Plugins/SymbolFile/DWARF/SymbolFileDWARFDebugMap.cpp b/lldb/source/Plugins/SymbolFile/DWARF/SymbolFileDWARFDebugMap.cpp
index 14bd2e5..c2bba5d 100644
--- a/lldb/source/Plugins/SymbolFile/DWARF/SymbolFileDWARFDebugMap.cpp
+++ b/lldb/source/Plugins/SymbolFile/DWARF/SymbolFileDWARFDebugMap.cpp
@@ -21,6 +21,7 @@
 #include "lldb/Symbol/SymbolVendor.h"
 #include "lldb/Symbol/VariableList.h"
 
+#include "LogChannelDWARF.h"
 #include "SymbolFileDWARF.h"
 
 using namespace lldb;
@@ -106,6 +107,8 @@
     Symtab* symtab = m_obj_file->GetSymtab();
     if (symtab)
     {
+        LogSP log (LogChannelDWARF::GetLogIfAll(DWARF_LOG_DEBUG_MAP));
+
         std::vector<uint32_t> oso_indexes;
 //      StreamFile s(stdout);
 //      symtab->Dump(&s, NULL, eSortOrderNone);
@@ -145,6 +148,9 @@
                 m_compile_unit_infos[i].last_symbol = symtab->SymbolAtIndex (sibling_idx - 1);
                 m_compile_unit_infos[i].first_symbol_index = symtab->GetIndexForSymbol(m_compile_unit_infos[i].so_symbol);
                 m_compile_unit_infos[i].last_symbol_index = symtab->GetIndexForSymbol(m_compile_unit_infos[i].last_symbol);
+                
+                if (log)
+                    log->Printf("Initialized OSO 0x%8.8x: file=%s", i, m_compile_unit_infos[i].oso_symbol->GetName().GetCString());
             }
         }
     }
diff --git a/lldb/source/Symbol/ClangASTContext.cpp b/lldb/source/Symbol/ClangASTContext.cpp
index 03424ab..0262d3d 100644
--- a/lldb/source/Symbol/ClangASTContext.cpp
+++ b/lldb/source/Symbol/ClangASTContext.cpp
@@ -1125,10 +1125,13 @@
 #pragma mark Structure, Unions, Classes
 
 clang_type_t
-ClangASTContext::CreateRecordType (DeclContext *decl_ctx, AccessType access_type, const char *name, int kind, LanguageType language)
+ClangASTContext::CreateRecordType (DeclContext *decl_ctx, AccessType access_type, const char *name, int kind, LanguageType language, CXXRecordDecl **out_decl)
 {
     ASTContext *ast = getASTContext();
     assert (ast != NULL);
+ 
+    if (out_decl)
+        *out_decl = NULL;
     
     if (decl_ctx == NULL)
         decl_ctx = ast->getTranslationUnitDecl();
@@ -1153,6 +1156,9 @@
                                                  SourceLocation(),
                                                  name && name[0] ? &ast->Idents.get(name) : NULL);
     
+    if (out_decl)
+        *out_decl = decl;
+    
     if (!name)
         decl->setAnonymousStructOrUnion(true);
 
@@ -6195,36 +6201,6 @@
     return qual_type.getQualifiers().getCVRQualifiers();
 }
 
-uint64_t
-GetTypeFlags(clang::ASTContext *ast, lldb::clang_type_t clang_type)
-{
-    assert (clang_type);
-    
-    clang::ExternalASTSource *external_ast_source = ast->getExternalSource();
-    
-    if (!external_ast_source)
-        return 0;
-    
-    ClangExternalASTSourceCommon *common_ast_source = static_cast<ClangExternalASTSourceCommon*>(external_ast_source);
-    
-    return common_ast_source->GetMetadata((uintptr_t)clang_type);
-}
-
-void
-SetTypeFlags(clang::ASTContext *ast, lldb::clang_type_t clang_type, uint64_t flags)
-{
-    assert (clang_type);
-    
-    clang::ExternalASTSource *external_ast_source = ast->getExternalSource();
-    
-    if (!external_ast_source)
-        return;
-    
-    ClangExternalASTSourceCommon *common_ast_source = static_cast<ClangExternalASTSourceCommon*>(external_ast_source);
-    
-    return common_ast_source->SetMetadata((uintptr_t)clang_type, flags);
-}
-
 bool
 ClangASTContext::GetCompleteType (clang::ASTContext *ast, lldb::clang_type_t clang_type)
 {
@@ -6299,6 +6275,31 @@
     }
 }
 
+void
+ClangASTContext::SetMetadata (clang::ASTContext *ast,
+                              uintptr_t object,
+                              uint64_t metadata)
+{
+    ClangExternalASTSourceCommon *external_source =
+        static_cast<ClangExternalASTSourceCommon*>(ast->getExternalSource());
+    
+    if (external_source)
+        external_source->SetMetadata(object, metadata);
+}
+
+uint64_t
+ClangASTContext::GetMetadata (clang::ASTContext *ast,
+                              uintptr_t object)
+{
+    ClangExternalASTSourceCommon *external_source =
+        static_cast<ClangExternalASTSourceCommon*>(ast->getExternalSource());
+    
+    if (external_source && external_source->HasMetadata(object))
+        return external_source->GetMetadata(object);
+    else
+        return 0;
+}
+
 clang::DeclContext *
 ClangASTContext::GetAsDeclContext (clang::CXXMethodDecl *cxx_method_decl)
 {
diff --git a/lldb/source/Symbol/ClangASTImporter.cpp b/lldb/source/Symbol/ClangASTImporter.cpp
index 04ef8ff..a67a3dd 100644
--- a/lldb/source/Symbol/ClangASTImporter.cpp
+++ b/lldb/source/Symbol/ClangASTImporter.cpp
@@ -14,6 +14,7 @@
 #include "lldb/Core/Module.h"
 #include "lldb/Symbol/ClangASTContext.h"
 #include "lldb/Symbol/ClangASTImporter.h"
+#include "lldb/Symbol/ClangExternalASTSourceCommon.h"
 #include "lldb/Symbol/ClangNamespaceDecl.h"
 
 using namespace lldb_private;
@@ -237,6 +238,17 @@
     return true;
 }
 
+uint64_t
+ClangASTImporter::GetDeclMetadata (const clang::Decl *decl)
+{
+    DeclOrigin decl_origin = GetDeclOrigin(decl);
+    
+    if (decl_origin.Valid())
+        return ClangASTContext::GetMetadata(decl_origin.ctx, (uintptr_t)decl_origin.decl);
+    else
+        return ClangASTContext::GetMetadata(&decl->getASTContext(), (uintptr_t)decl);
+}
+
 ClangASTImporter::DeclOrigin
 ClangASTImporter::GetDeclOrigin(const clang::Decl *decl)
 {
@@ -414,18 +426,20 @@
     {
         if (NamedDecl *from_named_decl = dyn_cast<clang::NamedDecl>(from))
         {
-            log->Printf("    [ClangASTImporter] Imported (%sDecl*)%p, named %s (from (Decl*)%p)",
+            log->Printf("    [ClangASTImporter] Imported (%sDecl*)%p, named %s (from (Decl*)%p), metadata 0x%llx",
                         from->getDeclKindName(),
                         to,
                         from_named_decl->getName().str().c_str(),
-                        from);
+                        from,
+                        m_master.GetDeclMetadata(from));
         }
         else
         {
-            log->Printf("    [ClangASTImporter] Imported (%sDecl*)%p (from (Decl*)%p)",
+            log->Printf("    [ClangASTImporter] Imported (%sDecl*)%p (from (Decl*)%p), metadata 0x%llx",
                         from->getDeclKindName(),
                         to,
-                        from);
+                        from,
+                        m_master.GetDeclMetadata(from));
         }
     }
 
diff --git a/lldb/source/Symbol/ClangExternalASTSourceCommon.cpp b/lldb/source/Symbol/ClangExternalASTSourceCommon.cpp
index 18de26c..85e1dc8 100644
--- a/lldb/source/Symbol/ClangExternalASTSourceCommon.cpp
+++ b/lldb/source/Symbol/ClangExternalASTSourceCommon.cpp
@@ -13,9 +13,18 @@
 
 #define ClangExternalASTSourceCommon_MAGIC  (0x00112233aabbccddull)
 
+uint64_t g_TotalSizeOfMetadata = 0;
+
 ClangExternalASTSourceCommon::ClangExternalASTSourceCommon() : clang::ExternalASTSource()
 {
     m_magic = ClangExternalASTSourceCommon_MAGIC;
+    
+    g_TotalSizeOfMetadata += m_metadata.size();
+}
+
+ClangExternalASTSourceCommon::~ClangExternalASTSourceCommon()
+{
+    g_TotalSizeOfMetadata -= m_metadata.size();
 }
 
 uint64_t ClangExternalASTSourceCommon::GetMetadata (uintptr_t object)
@@ -29,7 +38,10 @@
 {
     assert (m_magic == ClangExternalASTSourceCommon_MAGIC);
     
+    uint64_t orig_size = m_metadata.size();
     m_metadata[object] = metadata;
+    uint64_t new_size = m_metadata.size();
+    g_TotalSizeOfMetadata += (new_size - orig_size);
 }
 
 bool ClangExternalASTSourceCommon::HasMetadata (uintptr_t object)