Emit parameter and local variable debug information with -g.



git-svn-id: https://llvm.org/svn/llvm-project/cfe/trunk@51765 91177308-0d34-0410-b5e6-96231b3b80d8
diff --git a/lib/CodeGen/CGDebugInfo.cpp b/lib/CodeGen/CGDebugInfo.cpp
index 1827fc9..19745dc 100644
--- a/lib/CodeGen/CGDebugInfo.cpp
+++ b/lib/CodeGen/CGDebugInfo.cpp
@@ -44,6 +44,7 @@
 , CompileUnitAnchor(NULL)
 , SubprogramAnchor(NULL)
 , RegionStack()
+, VariableDescList()
 , Subprogram(NULL)
 {
   SR = new llvm::DISerializer();
@@ -72,7 +73,12 @@
        = RegionStack.begin(); I != RegionStack.end(); ++I) {
     delete *I;
   }
-  
+
+  for (std::vector<llvm::VariableDesc *>::iterator I 
+       = VariableDescList.begin(); I != VariableDescList.end(); ++I) {
+    delete *I;
+  }
+
   delete CompileUnitAnchor;
   delete SubprogramAnchor;
 }
@@ -519,3 +525,44 @@
   RegionStack.pop_back();
 }
 
+/// EmitDeclare - Emit local variable declaration debug info.
+void CGDebugInfo::EmitDeclare(const VarDecl *decl, unsigned Tag,
+                              llvm::Value *AI,
+                              llvm::IRBuilder &Builder)
+{
+  // FIXME: If it is a compiler generated temporary then return.
+
+  // Construct llvm.dbg.declare function.
+  if (!DeclareFn)
+    DeclareFn = llvm::Intrinsic::getDeclaration(&M->getModule(), 
+			llvm::Intrinsic::dbg_declare);
+
+  // Get type information.
+  llvm::CompileUnitDesc *Unit = getOrCreateCompileUnit(CurLoc);
+  llvm::TypeDesc *TyDesc = getOrCreateType(decl->getType(), Unit);
+
+  SourceManager &SM = M->getContext().getSourceManager();
+  uint64_t Loc = SM.getLogicalLineNumber(CurLoc);
+
+  // Construct variable.
+  llvm::VariableDesc *Variable = new llvm::VariableDesc(Tag);
+  Variable->setContext(RegionStack.back());
+  Variable->setName(decl->getName());
+  Variable->setFile(Unit);
+  Variable->setLine(Loc);
+  Variable->setType(TyDesc);
+
+  // Push it onto the list so that we can free it.
+  VariableDescList.push_back(Variable);
+
+  // Cast the AllocA result to a {}* for the call to llvm.dbg.declare.
+  // These bit cast instructions will get freed when the basic block is
+  // deleted. So do not need to free them explicity.
+  const llvm::PointerType *EmpPtr = SR->getEmptyStructPtrType();
+  llvm::Value *AllocACast =  new llvm::BitCastInst(AI, EmpPtr, decl->getName(),
+                               Builder.GetInsertBlock());
+
+  // Call llvm.dbg.declare.
+  Builder.CreateCall2(DeclareFn, AllocACast, getCastValueFor(Variable), "");
+}
+