Recommitted r263424 "Supporting all entities declared in lexical scope in LLVM debug info."
After fixing PR26942 (the fix is included in this commit).

Differential Revision: http://reviews.llvm.org/D18350

llvm-svn: 264280
diff --git a/llvm/lib/CodeGen/AsmPrinter/DwarfUnit.cpp b/llvm/lib/CodeGen/AsmPrinter/DwarfUnit.cpp
index 387921c..b2fe3c0 100644
--- a/llvm/lib/CodeGen/AsmPrinter/DwarfUnit.cpp
+++ b/llvm/lib/CodeGen/AsmPrinter/DwarfUnit.cpp
@@ -298,10 +298,15 @@
                Entry);
 }
 
-DIE &DwarfUnit::createAndAddDIE(unsigned Tag, DIE &Parent, const DINode *N) {
-  DIE &Die = Parent.addChild(DIE::get(DIEValueAllocator, (dwarf::Tag)Tag));
+DIE *DwarfUnit::createDIE(unsigned Tag, const DINode *N) {
+  DIE *Die = DIE::get(DIEValueAllocator, (dwarf::Tag)Tag);
   if (N)
-    insertDIE(N, &Die);
+    insertDIE(N, Die);
+  return Die;
+}
+
+DIE &DwarfUnit::createAndAddDIE(unsigned Tag, DIE &Parent, const DINode *N) {
+  DIE &Die = Parent.addChild(createDIE(Tag, N));
   return Die;
 }
 
@@ -727,15 +732,18 @@
 
   // Construct the context before querying for the existence of the DIE in case
   // such construction creates the DIE.
+  // For Local Scope, do not construct context DIE.
   auto *Context = resolve(Ty->getScope());
-  DIE *ContextDIE = getOrCreateContextDIE(Context);
-  assert(ContextDIE);
+  bool IsLocalScope = Context && isa<DILocalScope>(Context);
+  DIE *ContextDIE = IsLocalScope ? nullptr : getOrCreateContextDIE(Context);
+  assert(ContextDIE || IsLocalScope);
 
   if (DIE *TyDIE = getDIE(Ty))
     return TyDIE;
 
-  // Create new type.
-  DIE &TyDIE = createAndAddDIE(Ty->getTag(), *ContextDIE, Ty);
+  // Create new type and add to map.
+  DIE &TyDIE = IsLocalScope ? *createDIE(Ty->getTag(), Ty)
+                            : createAndAddDIE(Ty->getTag(), *ContextDIE, Ty);
 
   updateAcceleratorTables(Context, Ty, TyDIE);