Rename variable. add comment.
[oota-llvm.git] / lib / Analysis / DebugInfo.cpp
index 56c9b2102221fbfe15ae849b518d8060dfb434f6..a7b6d2b65eee0e6864a167721108df538fa30fe2 100644 (file)
@@ -350,6 +350,17 @@ bool DILocation::Verify() const {
   return DbgNode->getNumOperands() == 4;
 }
 
+/// Verify - Verify that a namespace descriptor is well formed.
+bool DINameSpace::Verify() const {
+  if (!DbgNode)
+    return false;
+  if (getName().empty())
+    return false;
+  if (!getCompileUnit().Verify())
+    return false;
+  return true;
+}
+
 /// getOriginalTypeSize - If this type is derived from a base type then
 /// return base type size.
 uint64_t DIDerivedType::getOriginalTypeSize() const {
@@ -455,7 +466,7 @@ void DICompileUnit::print(raw_ostream &OS) const {
   if (getLanguage())
     OS << " [" << dwarf::LanguageString(getLanguage()) << "] ";
 
-  OS << " [" << getDirectory() << "/" << getFilename() << " ]";
+  OS << " [" << getDirectory() << "/" << getFilename() << "]";
 }
 
 /// print - Print type.
@@ -516,8 +527,8 @@ void DICompositeType::print(raw_ostream &OS) const {
   OS << " [" << A.getNumElements() << " elements]";
 }
 
-/// print - Print global.
-void DIGlobal::print(raw_ostream &OS) const {
+/// print - Print subprogram.
+void DISubprogram::print(raw_ostream &OS) const {
   StringRef Res = getName();
   if (!Res.empty())
     OS << " [" << Res << "] ";
@@ -535,14 +546,12 @@ void DIGlobal::print(raw_ostream &OS) const {
   if (isDefinition())
     OS << " [def] ";
 
-  if (isGlobalVariable())
-    DIGlobalVariable(DbgNode).print(OS);
-
   OS << "\n";
 }
 
-/// print - Print subprogram.
-void DISubprogram::print(raw_ostream &OS) const {
+/// print - Print global variable.
+void DIGlobalVariable::print(raw_ostream &OS) const {
+  OS << " [";
   StringRef Res = getName();
   if (!Res.empty())
     OS << " [" << Res << "] ";
@@ -560,14 +569,9 @@ void DISubprogram::print(raw_ostream &OS) const {
   if (isDefinition())
     OS << " [def] ";
 
-  OS << "\n";
-}
-
-/// print - Print global variable.
-void DIGlobalVariable::print(raw_ostream &OS) const {
-  OS << " [";
-  getGlobal()->print(OS);
-  OS << "] ";
+  if (isGlobalVariable())
+    DIGlobalVariable(DbgNode).print(OS);
+  OS << "]\n";
 }
 
 /// print - Print variable.
@@ -614,11 +618,6 @@ void DICompositeType::dump() const {
   print(dbgs()); dbgs() << '\n';
 }
 
-/// dump - Print global to dbgs() with a newline.
-void DIGlobal::dump() const {
-  print(dbgs()); dbgs() << '\n';
-}
-
 /// dump - Print subprogram to dbgs() with a newline.
 void DISubprogram::dump() const {
   print(dbgs()); dbgs() << '\n';
@@ -1029,7 +1028,7 @@ DIVariable DIFactory::CreateVariable(unsigned Tag, DIDescriptor Context,
                                      StringRef Name,
                                      DIFile F,
                                      unsigned LineNo,
-                                     DIType Ty) {
+                                     DIType Ty, bool AlwaysPreserve) {
   Value *Elts[] = {
     GetTagConstant(Tag),
     Context,
@@ -1038,7 +1037,15 @@ DIVariable DIFactory::CreateVariable(unsigned Tag, DIDescriptor Context,
     ConstantInt::get(Type::getInt32Ty(VMContext), LineNo),
     Ty,
   };
-  return DIVariable(MDNode::get(VMContext, &Elts[0], 6));
+  MDNode *Node = MDNode::get(VMContext, &Elts[0], 6);
+  if (AlwaysPreserve) {
+    // The optimizer may remove local variable. If there is an interest
+    // to preserve variable info in such situation then stash it in a
+    // named mdnode.
+    NamedMDNode *NMD = M.getOrInsertNamedMetadata("llvm.dbg.lv");
+    NMD->addOperand(Node);
+  }
+  return DIVariable(Node);
 }