Recommitted r261633 "Supporting all entities declared in lexical scope in LLVM debug info."
After fixing PR26715 at r263379.

llvm-svn: 263424
diff --git a/llvm/lib/CodeGen/AsmPrinter/DwarfDebug.cpp b/llvm/lib/CodeGen/AsmPrinter/DwarfDebug.cpp
index d4d94ca..589f88d 100644
--- a/llvm/lib/CodeGen/AsmPrinter/DwarfDebug.cpp
+++ b/llvm/lib/CodeGen/AsmPrinter/DwarfDebug.cpp
@@ -455,6 +455,16 @@
     D->addChild(TheCU.constructImportedEntityDIE(N));
 }
 
+bool DwarfDebug::collectLocalScopedNode(DIScope *S, const DINode *N,
+                                        DwarfCompileUnit &CU) {
+  if (auto LS = dyn_cast_or_null<DILocalScope>(S)) {
+    getLocalScopes(LS->getSubprogram()).insert(LS);
+    CU.addLocalDeclNode(N, LS);
+    return true;
+  }
+  return false;
+}
+
 // Emit all Dwarf sections that should come prior to the content. Create
 // global DIEs and emit initial debug info sections. This is invoked by
 // the target AsmPrinter.
@@ -474,10 +484,9 @@
   for (MDNode *N : CU_Nodes->operands()) {
     auto *CUNode = cast<DICompileUnit>(N);
     DwarfCompileUnit &CU = constructDwarfCompileUnit(CUNode);
-    for (auto *IE : CUNode->getImportedEntities())
-      CU.addImportedEntity(IE);
     for (auto *GV : CUNode->getGlobalVariables())
-      CU.getOrCreateGlobalVariableDIE(GV);
+      if (!collectLocalScopedNode(GV->getScope(), GV, CU))
+        CU.getOrCreateGlobalVariableDIE(GV);
     for (auto *SP : CUNode->getSubprograms())
       SPMap.insert(std::make_pair(SP, &CU));
     for (auto *Ty : CUNode->getEnumTypes()) {
@@ -489,14 +498,17 @@
       // The retained types array by design contains pointers to
       // MDNodes rather than DIRefs. Unique them here.
       DIType *RT = cast<DIType>(resolve(Ty->getRef()));
-      if (!RT->isExternalTypeRef())
+      if (RT->isExternalTypeRef())
         // There is no point in force-emitting a forward declaration.
+        continue;
+      if (!collectLocalScopedNode(resolve(Ty->getScope()), RT, CU))
         CU.getOrCreateTypeDIE(RT);
     }
     // Emit imported_modules last so that the relevant context is already
     // available.
     for (auto *IE : CUNode->getImportedEntities())
-      constructAndAddImportedEntityDIE(CU, IE);
+      if (!collectLocalScopedNode(IE->getScope(), IE, CU))
+        constructAndAddImportedEntityDIE(CU, IE);
   }
 
   // Tell MMI that we have debug info.
@@ -529,6 +541,11 @@
     });
 }
 
+void DwarfDebug::finishLocalScopeDefinitions() {
+  for (const auto &I : CUMap)
+    I.second->finishLocalScopeDefinitions();
+}
+
 // Collect info for variables that were optimized out.
 void DwarfDebug::collectDeadVariables() {
   const Module *M = MMI->getModule();
@@ -554,6 +571,8 @@
 
   finishSubprogramDefinitions();
 
+  finishLocalScopeDefinitions();
+
   finishVariableDefinitions();
 
   // Collect info for variables that were optimized out.
@@ -1164,6 +1183,9 @@
       assert(LScopes.getAbstractScopesList().size() == NumAbstractScopes
              && "ensureAbstractVariableIsCreated inserted abstract scopes");
     }
+    // Assure abstract local scope created for each one contains local DIEs.
+    for (const DILocalScope *LS : getLocalScopes(SP))
+      LScopes.getOrCreateAbstractScope(LS);
     constructAbstractSubprogramScopeDIE(AScope);
   }