Add error handling in getInt.
[oota-llvm.git] / lib / VMCore / DebugInfo.cpp
index 4697fdecf3ebc82c40c3d5b747516fb15228b983..5eea2ce9e0aee6b5fe23f57823b392c33def435b 100644 (file)
@@ -68,7 +68,8 @@ uint64_t DIDescriptor::getUInt64Field(unsigned Elt) const {
     return 0;
 
   if (Elt < DbgNode->getNumOperands())
-    if (ConstantInt *CI = dyn_cast_or_null<ConstantInt>(DbgNode->getOperand(Elt)))
+    if (ConstantInt *CI
+        = dyn_cast_or_null<ConstantInt>(DbgNode->getOperand(Elt)))
       return CI->getZExtValue();
 
   return 0;
@@ -111,17 +112,27 @@ Function *DIDescriptor::getFunctionField(unsigned Elt) const {
   return 0;
 }
 
+void DIDescriptor::replaceFunctionField(unsigned Elt, Function *F) {
+  if (DbgNode == 0)
+    return;
+
+  if (Elt < DbgNode->getNumOperands()) {
+    MDNode *Node = const_cast<MDNode*>(DbgNode);
+    Node->replaceOperandWith(Elt, F);
+  }
+}
+
 unsigned DIVariable::getNumAddrElements() const {
-  if (getVersion() <= llvm::LLVMDebugVersion8)
+  if (getVersion() <= LLVMDebugVersion8)
     return DbgNode->getNumOperands()-6;
-  if (getVersion() == llvm::LLVMDebugVersion9)
+  if (getVersion() == LLVMDebugVersion9)
     return DbgNode->getNumOperands()-7;
   return DbgNode->getNumOperands()-8;
 }
 
 /// getInlinedAt - If this variable is inlined then return inline location.
 MDNode *DIVariable::getInlinedAt() const {
-  if (getVersion() <= llvm::LLVMDebugVersion9)
+  if (getVersion() <= LLVMDebugVersion9)
     return NULL;
   return dyn_cast_or_null<MDNode>(DbgNode->getOperand(7));
 }
@@ -681,7 +692,7 @@ static void fixupObjcLikeName(StringRef Str, SmallVectorImpl<char> &Out) {
   }
 }
 
-/// getFnSpecificMDNode - Return a NameMDNode, if available, that is 
+/// getFnSpecificMDNode - Return a NameMDNode, if available, that is
 /// suitable to hold function specific information.
 NamedMDNode *llvm::getFnSpecificMDNode(const Module &M, DISubprogram Fn) {
   SmallString<32> Name = StringRef("llvm.dbg.lv.");
@@ -710,7 +721,7 @@ NamedMDNode *llvm::getOrInsertFnSpecificMDNode(Module &M, DISubprogram Fn) {
   if (FName.startswith(StringRef(&One, 1)))
     FName = FName.substr(1);
   fixupObjcLikeName(FName, Name);
-  
+
   return M.getOrInsertNamedMetadata(Name.str());
 }
 
@@ -733,8 +744,8 @@ DIVariable llvm::cleanseInlinedVariable(MDNode *DV, LLVMContext &VMContext) {
   SmallVector<Value *, 16> Elts;
   // Insert inlined scope as 7th element.
   for (unsigned i = 0, e = DV->getNumOperands(); i != e; ++i)
-    i == 7 ? 
-      Elts.push_back(llvm::Constant::getNullValue(Type::getInt32Ty(VMContext))):
+    i == 7 ?
+      Elts.push_back(Constant::getNullValue(Type::getInt32Ty(VMContext))):
       Elts.push_back(DV->getOperand(i));
   return DIVariable(MDNode::get(VMContext, Elts));
 }
@@ -747,7 +758,7 @@ DISubprogram llvm::getDISubprogram(const MDNode *Scope) {
 
   if (D.isLexicalBlockFile())
     return getDISubprogram(DILexicalBlockFile(Scope).getContext());
-  
+
   if (D.isLexicalBlock())
     return getDISubprogram(DILexicalBlock(Scope).getContext());
 
@@ -783,7 +794,7 @@ bool llvm::isSubprogramContext(const MDNode *Context) {
 //===----------------------------------------------------------------------===//
 
 /// processModule - Process entire module and collect debug info.
-void DebugInfoFinder::processModule(Module &M) {
+void DebugInfoFinder::processModule(const Module &M) {
   if (NamedMDNode *CU_Nodes = M.getNamedMetadata("llvm.dbg.cu")) {
     for (unsigned i = 0, e = CU_Nodes->getNumOperands(); i != e; ++i) {
       DICompileUnit CU(CU_Nodes->getOperand(i));
@@ -809,11 +820,12 @@ void DebugInfoFinder::processModule(Module &M) {
     }
   }
 
-  for (Module::iterator I = M.begin(), E = M.end(); I != E; ++I)
-    for (Function::iterator FI = (*I).begin(), FE = (*I).end(); FI != FE; ++FI)
-      for (BasicBlock::iterator BI = (*FI).begin(), BE = (*FI).end(); BI != BE;
-           ++BI) {
-        if (DbgDeclareInst *DDI = dyn_cast<DbgDeclareInst>(BI))
+  for (Module::const_iterator I = M.begin(), E = M.end(); I != E; ++I)
+    for (Function::const_iterator FI = (*I).begin(), FE = (*I).end();
+         FI != FE; ++FI)
+      for (BasicBlock::const_iterator BI = (*FI).begin(), BE = (*FI).end();
+           BI != BE; ++BI) {
+        if (const DbgDeclareInst *DDI = dyn_cast<DbgDeclareInst>(BI))
           processDeclare(DDI);
 
         DebugLoc Loc = BI->getDebugLoc();
@@ -917,7 +929,7 @@ void DebugInfoFinder::processSubprogram(DISubprogram SP) {
 }
 
 /// processDeclare - Process DbgDeclareInst.
-void DebugInfoFinder::processDeclare(DbgDeclareInst *DDI) {
+void DebugInfoFinder::processDeclare(const DbgDeclareInst *DDI) {
   MDNode *N = dyn_cast<MDNode>(DDI->getVariable());
   if (!N) return;
 
@@ -993,12 +1005,11 @@ void DIDescriptor::dump() const {
 void DIDescriptor::print(raw_ostream &OS) const {
   if (!DbgNode) return;
 
-  OS << "[" << dwarf::TagString(getTag()) << ']';
+  if (const char *Tag = dwarf::TagString(getTag()))
+    OS << "[ " << Tag << " ]";
 
   if (this->isSubrange()) {
     DISubrange(DbgNode).printInternal(OS);
-  } else if (this->isScope()) {
-    DIScope(DbgNode).printInternal(OS);
   } else if (this->isCompileUnit()) {
     DICompileUnit(DbgNode).printInternal(OS);
   } else if (this->isFile()) {
@@ -1017,6 +1028,10 @@ void DIDescriptor::print(raw_ostream &OS) const {
     DIGlobalVariable(DbgNode).printInternal(OS);
   } else if (this->isVariable()) {
     DIVariable(DbgNode).printInternal(OS);
+  } else if (this->isObjCProperty()) {
+    DIObjCProperty(DbgNode).printInternal(OS);
+  } else if (this->isScope()) {
+    DIScope(DbgNode).printInternal(OS);
   }
 }
 
@@ -1052,7 +1067,7 @@ void DIType::printInternal(raw_ostream &OS) const {
      << ", align " << getAlignInBits()
      << ", offset " << getOffsetInBits();
   if (isBasicType())
-    if (const char *Enc = 
+    if (const char *Enc =
         dwarf::AttributeEncodingString(DIBasicType(DbgNode).getEncoding()))
       OS << ", enc " << Enc;
   OS << "]";
@@ -1078,12 +1093,7 @@ void DICompositeType::printInternal(raw_ostream &OS) const {
 }
 
 void DISubprogram::printInternal(raw_ostream &OS) const {
-  StringRef Res = getName();
-  if (!Res.empty())
-    OS << " [" << Res << ']';
-
   // TODO : Print context
-
   OS << " [line " << getLineNumber() << ']';
 
   if (isLocalToUnit())
@@ -1094,6 +1104,10 @@ void DISubprogram::printInternal(raw_ostream &OS) const {
 
   if (getScopeLineNumber() != getLineNumber())
     OS << " [scope " << getScopeLineNumber() << "]";
+
+  StringRef Res = getName();
+  if (!Res.empty())
+    OS << " [" << Res << ']';
 }
 
 void DIGlobalVariable::printInternal(raw_ostream &OS) const {
@@ -1120,6 +1134,15 @@ void DIVariable::printInternal(raw_ostream &OS) const {
   OS << " [line " << getLineNumber() << ']';
 }
 
+void DIObjCProperty::printInternal(raw_ostream &OS) const {
+  StringRef Name = getObjCPropertyName();
+  if (!Name.empty())
+    OS << " [" << Name << ']';
+
+  OS << " [line " << getLineNumber()
+     << ", properties " << getUnsignedField(6) << ']';
+}
+
 static void printDebugLoc(DebugLoc DL, raw_ostream &CommentOS,
                           const LLVMContext &Ctx) {
   if (!DL.isUnknown()) {          // Print source line info.
@@ -1155,29 +1178,3 @@ void DIVariable::printExtendedName(raw_ostream &OS) const {
     }
   }
 }
-
-//===----------------------------------------------------------------------===//
-// DIDescriptor: vtable anchors for all descriptors.
-//===----------------------------------------------------------------------===//
-
-void DIScope::anchor() { }
-
-void DICompileUnit::anchor() { }
-
-void DIFile::anchor() { }
-
-void DIType::anchor() { }
-
-void DIBasicType::anchor() { }
-
-void DIDerivedType::anchor() { }
-
-void DICompositeType::anchor() { }
-
-void DISubprogram::anchor() { }
-
-void DILexicalBlock::anchor() { }
-
-void DINameSpace::anchor() { }
-
-void DILexicalBlockFile::anchor() { }