LLVM API Documentation

DebugInfo.cpp
Go to the documentation of this file.
00001 //===--- DebugInfo.cpp - Debug Information Helper Classes -----------------===//
00002 //
00003 //                     The LLVM Compiler Infrastructure
00004 //
00005 // This file is distributed under the University of Illinois Open Source
00006 // License. See LICENSE.TXT for details.
00007 //
00008 //===----------------------------------------------------------------------===//
00009 //
00010 // This file implements the helper classes used to build and interpret debug
00011 // information in LLVM IR form.
00012 //
00013 //===----------------------------------------------------------------------===//
00014 
00015 #include "llvm/IR/DebugInfo.h"
00016 #include "LLVMContextImpl.h"
00017 #include "llvm/ADT/STLExtras.h"
00018 #include "llvm/ADT/SmallPtrSet.h"
00019 #include "llvm/ADT/SmallString.h"
00020 #include "llvm/Analysis/ValueTracking.h"
00021 #include "llvm/IR/Constants.h"
00022 #include "llvm/IR/DerivedTypes.h"
00023 #include "llvm/IR/Instructions.h"
00024 #include "llvm/IR/IntrinsicInst.h"
00025 #include "llvm/IR/Intrinsics.h"
00026 #include "llvm/IR/Module.h"
00027 #include "llvm/IR/ValueHandle.h"
00028 #include "llvm/Support/Debug.h"
00029 #include "llvm/Support/Dwarf.h"
00030 #include "llvm/Support/raw_ostream.h"
00031 using namespace llvm;
00032 using namespace llvm::dwarf;
00033 
00034 //===----------------------------------------------------------------------===//
00035 // DIDescriptor
00036 //===----------------------------------------------------------------------===//
00037 
00038 bool DIDescriptor::Verify() const {
00039   return DbgNode &&
00040          (DIDerivedType(DbgNode).Verify() ||
00041           DICompositeType(DbgNode).Verify() || DIBasicType(DbgNode).Verify() ||
00042           DIVariable(DbgNode).Verify() || DISubprogram(DbgNode).Verify() ||
00043           DIGlobalVariable(DbgNode).Verify() || DIFile(DbgNode).Verify() ||
00044           DICompileUnit(DbgNode).Verify() || DINameSpace(DbgNode).Verify() ||
00045           DILexicalBlock(DbgNode).Verify() ||
00046           DILexicalBlockFile(DbgNode).Verify() ||
00047           DISubrange(DbgNode).Verify() || DIEnumerator(DbgNode).Verify() ||
00048           DIObjCProperty(DbgNode).Verify() ||
00049           DIUnspecifiedParameter(DbgNode).Verify() ||
00050           DITemplateTypeParameter(DbgNode).Verify() ||
00051           DITemplateValueParameter(DbgNode).Verify() ||
00052           DIImportedEntity(DbgNode).Verify());
00053 }
00054 
00055 static Value *getField(const MDNode *DbgNode, unsigned Elt) {
00056   if (!DbgNode || Elt >= DbgNode->getNumOperands())
00057     return nullptr;
00058   return DbgNode->getOperand(Elt);
00059 }
00060 
00061 static MDNode *getNodeField(const MDNode *DbgNode, unsigned Elt) {
00062   return dyn_cast_or_null<MDNode>(getField(DbgNode, Elt));
00063 }
00064 
00065 static StringRef getStringField(const MDNode *DbgNode, unsigned Elt) {
00066   if (MDString *MDS = dyn_cast_or_null<MDString>(getField(DbgNode, Elt)))
00067     return MDS->getString();
00068   return StringRef();
00069 }
00070 
00071 StringRef DIDescriptor::getStringField(unsigned Elt) const {
00072   return ::getStringField(DbgNode, Elt);
00073 }
00074 
00075 uint64_t DIDescriptor::getUInt64Field(unsigned Elt) const {
00076   if (!DbgNode)
00077     return 0;
00078 
00079   if (Elt < DbgNode->getNumOperands())
00080     if (ConstantInt *CI =
00081             dyn_cast_or_null<ConstantInt>(DbgNode->getOperand(Elt)))
00082       return CI->getZExtValue();
00083 
00084   return 0;
00085 }
00086 
00087 int64_t DIDescriptor::getInt64Field(unsigned Elt) const {
00088   if (!DbgNode)
00089     return 0;
00090 
00091   if (Elt < DbgNode->getNumOperands())
00092     if (ConstantInt *CI =
00093             dyn_cast_or_null<ConstantInt>(DbgNode->getOperand(Elt)))
00094       return CI->getSExtValue();
00095 
00096   return 0;
00097 }
00098 
00099 DIDescriptor DIDescriptor::getDescriptorField(unsigned Elt) const {
00100   MDNode *Field = getNodeField(DbgNode, Elt);
00101   return DIDescriptor(Field);
00102 }
00103 
00104 GlobalVariable *DIDescriptor::getGlobalVariableField(unsigned Elt) const {
00105   if (!DbgNode)
00106     return nullptr;
00107 
00108   if (Elt < DbgNode->getNumOperands())
00109     return dyn_cast_or_null<GlobalVariable>(DbgNode->getOperand(Elt));
00110   return nullptr;
00111 }
00112 
00113 Constant *DIDescriptor::getConstantField(unsigned Elt) const {
00114   if (!DbgNode)
00115     return nullptr;
00116 
00117   if (Elt < DbgNode->getNumOperands())
00118     return dyn_cast_or_null<Constant>(DbgNode->getOperand(Elt));
00119   return nullptr;
00120 }
00121 
00122 Function *DIDescriptor::getFunctionField(unsigned Elt) const {
00123   if (!DbgNode)
00124     return nullptr;
00125 
00126   if (Elt < DbgNode->getNumOperands())
00127     return dyn_cast_or_null<Function>(DbgNode->getOperand(Elt));
00128   return nullptr;
00129 }
00130 
00131 void DIDescriptor::replaceFunctionField(unsigned Elt, Function *F) {
00132   if (!DbgNode)
00133     return;
00134 
00135   if (Elt < DbgNode->getNumOperands()) {
00136     MDNode *Node = const_cast<MDNode *>(DbgNode);
00137     Node->replaceOperandWith(Elt, F);
00138   }
00139 }
00140 
00141 unsigned DIVariable::getNumAddrElements() const {
00142   return DbgNode->getNumOperands() - 8;
00143 }
00144 
00145 /// getInlinedAt - If this variable is inlined then return inline location.
00146 MDNode *DIVariable::getInlinedAt() const { return getNodeField(DbgNode, 7); }
00147 
00148 //===----------------------------------------------------------------------===//
00149 // Predicates
00150 //===----------------------------------------------------------------------===//
00151 
00152 /// isBasicType - Return true if the specified tag is legal for
00153 /// DIBasicType.
00154 bool DIDescriptor::isBasicType() const {
00155   if (!DbgNode)
00156     return false;
00157   switch (getTag()) {
00158   case dwarf::DW_TAG_base_type:
00159   case dwarf::DW_TAG_unspecified_type:
00160     return true;
00161   default:
00162     return false;
00163   }
00164 }
00165 
00166 /// isDerivedType - Return true if the specified tag is legal for DIDerivedType.
00167 bool DIDescriptor::isDerivedType() const {
00168   if (!DbgNode)
00169     return false;
00170   switch (getTag()) {
00171   case dwarf::DW_TAG_typedef:
00172   case dwarf::DW_TAG_pointer_type:
00173   case dwarf::DW_TAG_ptr_to_member_type:
00174   case dwarf::DW_TAG_reference_type:
00175   case dwarf::DW_TAG_rvalue_reference_type:
00176   case dwarf::DW_TAG_const_type:
00177   case dwarf::DW_TAG_volatile_type:
00178   case dwarf::DW_TAG_restrict_type:
00179   case dwarf::DW_TAG_member:
00180   case dwarf::DW_TAG_inheritance:
00181   case dwarf::DW_TAG_friend:
00182     return true;
00183   default:
00184     // CompositeTypes are currently modelled as DerivedTypes.
00185     return isCompositeType();
00186   }
00187 }
00188 
00189 /// isCompositeType - Return true if the specified tag is legal for
00190 /// DICompositeType.
00191 bool DIDescriptor::isCompositeType() const {
00192   if (!DbgNode)
00193     return false;
00194   switch (getTag()) {
00195   case dwarf::DW_TAG_array_type:
00196   case dwarf::DW_TAG_structure_type:
00197   case dwarf::DW_TAG_union_type:
00198   case dwarf::DW_TAG_enumeration_type:
00199   case dwarf::DW_TAG_subroutine_type:
00200   case dwarf::DW_TAG_class_type:
00201     return true;
00202   default:
00203     return false;
00204   }
00205 }
00206 
00207 /// isVariable - Return true if the specified tag is legal for DIVariable.
00208 bool DIDescriptor::isVariable() const {
00209   if (!DbgNode)
00210     return false;
00211   switch (getTag()) {
00212   case dwarf::DW_TAG_auto_variable:
00213   case dwarf::DW_TAG_arg_variable:
00214     return true;
00215   default:
00216     return false;
00217   }
00218 }
00219 
00220 /// isType - Return true if the specified tag is legal for DIType.
00221 bool DIDescriptor::isType() const {
00222   return isBasicType() || isCompositeType() || isDerivedType();
00223 }
00224 
00225 /// isSubprogram - Return true if the specified tag is legal for
00226 /// DISubprogram.
00227 bool DIDescriptor::isSubprogram() const {
00228   return DbgNode && getTag() == dwarf::DW_TAG_subprogram;
00229 }
00230 
00231 /// isGlobalVariable - Return true if the specified tag is legal for
00232 /// DIGlobalVariable.
00233 bool DIDescriptor::isGlobalVariable() const {
00234   return DbgNode && (getTag() == dwarf::DW_TAG_variable ||
00235                      getTag() == dwarf::DW_TAG_constant);
00236 }
00237 
00238 /// isUnspecifiedParmeter - Return true if the specified tag is
00239 /// DW_TAG_unspecified_parameters.
00240 bool DIDescriptor::isUnspecifiedParameter() const {
00241   return DbgNode && getTag() == dwarf::DW_TAG_unspecified_parameters;
00242 }
00243 
00244 /// isScope - Return true if the specified tag is one of the scope
00245 /// related tag.
00246 bool DIDescriptor::isScope() const {
00247   if (!DbgNode)
00248     return false;
00249   switch (getTag()) {
00250   case dwarf::DW_TAG_compile_unit:
00251   case dwarf::DW_TAG_lexical_block:
00252   case dwarf::DW_TAG_subprogram:
00253   case dwarf::DW_TAG_namespace:
00254   case dwarf::DW_TAG_file_type:
00255     return true;
00256   default:
00257     break;
00258   }
00259   return isType();
00260 }
00261 
00262 /// isTemplateTypeParameter - Return true if the specified tag is
00263 /// DW_TAG_template_type_parameter.
00264 bool DIDescriptor::isTemplateTypeParameter() const {
00265   return DbgNode && getTag() == dwarf::DW_TAG_template_type_parameter;
00266 }
00267 
00268 /// isTemplateValueParameter - Return true if the specified tag is
00269 /// DW_TAG_template_value_parameter.
00270 bool DIDescriptor::isTemplateValueParameter() const {
00271   return DbgNode && (getTag() == dwarf::DW_TAG_template_value_parameter ||
00272                      getTag() == dwarf::DW_TAG_GNU_template_template_param ||
00273                      getTag() == dwarf::DW_TAG_GNU_template_parameter_pack);
00274 }
00275 
00276 /// isCompileUnit - Return true if the specified tag is DW_TAG_compile_unit.
00277 bool DIDescriptor::isCompileUnit() const {
00278   return DbgNode && getTag() == dwarf::DW_TAG_compile_unit;
00279 }
00280 
00281 /// isFile - Return true if the specified tag is DW_TAG_file_type.
00282 bool DIDescriptor::isFile() const {
00283   return DbgNode && getTag() == dwarf::DW_TAG_file_type;
00284 }
00285 
00286 /// isNameSpace - Return true if the specified tag is DW_TAG_namespace.
00287 bool DIDescriptor::isNameSpace() const {
00288   return DbgNode && getTag() == dwarf::DW_TAG_namespace;
00289 }
00290 
00291 /// isLexicalBlockFile - Return true if the specified descriptor is a
00292 /// lexical block with an extra file.
00293 bool DIDescriptor::isLexicalBlockFile() const {
00294   return DbgNode && getTag() == dwarf::DW_TAG_lexical_block &&
00295          (DbgNode->getNumOperands() == 3);
00296 }
00297 
00298 /// isLexicalBlock - Return true if the specified tag is DW_TAG_lexical_block.
00299 bool DIDescriptor::isLexicalBlock() const {
00300   return DbgNode && getTag() == dwarf::DW_TAG_lexical_block &&
00301          (DbgNode->getNumOperands() > 3);
00302 }
00303 
00304 /// isSubrange - Return true if the specified tag is DW_TAG_subrange_type.
00305 bool DIDescriptor::isSubrange() const {
00306   return DbgNode && getTag() == dwarf::DW_TAG_subrange_type;
00307 }
00308 
00309 /// isEnumerator - Return true if the specified tag is DW_TAG_enumerator.
00310 bool DIDescriptor::isEnumerator() const {
00311   return DbgNode && getTag() == dwarf::DW_TAG_enumerator;
00312 }
00313 
00314 /// isObjCProperty - Return true if the specified tag is DW_TAG_APPLE_property.
00315 bool DIDescriptor::isObjCProperty() const {
00316   return DbgNode && getTag() == dwarf::DW_TAG_APPLE_property;
00317 }
00318 
00319 /// \brief Return true if the specified tag is DW_TAG_imported_module or
00320 /// DW_TAG_imported_declaration.
00321 bool DIDescriptor::isImportedEntity() const {
00322   return DbgNode && (getTag() == dwarf::DW_TAG_imported_module ||
00323                      getTag() == dwarf::DW_TAG_imported_declaration);
00324 }
00325 
00326 //===----------------------------------------------------------------------===//
00327 // Simple Descriptor Constructors and other Methods
00328 //===----------------------------------------------------------------------===//
00329 
00330 unsigned DIArray::getNumElements() const {
00331   if (!DbgNode)
00332     return 0;
00333   return DbgNode->getNumOperands();
00334 }
00335 
00336 /// replaceAllUsesWith - Replace all uses of the MDNode used by this
00337 /// type with the one in the passed descriptor.
00338 void DIType::replaceAllUsesWith(DIDescriptor &D) {
00339 
00340   assert(DbgNode && "Trying to replace an unverified type!");
00341 
00342   // Since we use a TrackingVH for the node, its easy for clients to manufacture
00343   // legitimate situations where they want to replaceAllUsesWith() on something
00344   // which, due to uniquing, has merged with the source. We shield clients from
00345   // this detail by allowing a value to be replaced with replaceAllUsesWith()
00346   // itself.
00347   if (DbgNode != D) {
00348     MDNode *Node = const_cast<MDNode *>(DbgNode);
00349     const MDNode *DN = D;
00350     const Value *V = cast_or_null<Value>(DN);
00351     Node->replaceAllUsesWith(const_cast<Value *>(V));
00352     MDNode::deleteTemporary(Node);
00353   }
00354 }
00355 
00356 /// replaceAllUsesWith - Replace all uses of the MDNode used by this
00357 /// type with the one in D.
00358 void DIType::replaceAllUsesWith(MDNode *D) {
00359 
00360   assert(DbgNode && "Trying to replace an unverified type!");
00361 
00362   // Since we use a TrackingVH for the node, its easy for clients to manufacture
00363   // legitimate situations where they want to replaceAllUsesWith() on something
00364   // which, due to uniquing, has merged with the source. We shield clients from
00365   // this detail by allowing a value to be replaced with replaceAllUsesWith()
00366   // itself.
00367   if (DbgNode != D) {
00368     MDNode *Node = const_cast<MDNode *>(DbgNode);
00369     const MDNode *DN = D;
00370     const Value *V = cast_or_null<Value>(DN);
00371     Node->replaceAllUsesWith(const_cast<Value *>(V));
00372     MDNode::deleteTemporary(Node);
00373   }
00374 }
00375 
00376 /// Verify - Verify that a compile unit is well formed.
00377 bool DICompileUnit::Verify() const {
00378   if (!isCompileUnit())
00379     return false;
00380 
00381   // Don't bother verifying the compilation directory or producer string
00382   // as those could be empty.
00383   if (getFilename().empty())
00384     return false;
00385 
00386   return DbgNode->getNumOperands() == 14;
00387 }
00388 
00389 /// Verify - Verify that an ObjC property is well formed.
00390 bool DIObjCProperty::Verify() const {
00391   if (!isObjCProperty())
00392     return false;
00393 
00394   // Don't worry about the rest of the strings for now.
00395   return DbgNode->getNumOperands() == 8;
00396 }
00397 
00398 /// Check if a field at position Elt of a MDNode is a MDNode.
00399 /// We currently allow an empty string and an integer.
00400 /// But we don't allow a non-empty string in a MDNode field.
00401 static bool fieldIsMDNode(const MDNode *DbgNode, unsigned Elt) {
00402   // FIXME: This function should return true, if the field is null or the field
00403   // is indeed a MDNode: return !Fld || isa<MDNode>(Fld).
00404   Value *Fld = getField(DbgNode, Elt);
00405   if (Fld && isa<MDString>(Fld) && !cast<MDString>(Fld)->getString().empty())
00406     return false;
00407   return true;
00408 }
00409 
00410 /// Check if a field at position Elt of a MDNode is a MDString.
00411 static bool fieldIsMDString(const MDNode *DbgNode, unsigned Elt) {
00412   Value *Fld = getField(DbgNode, Elt);
00413   return !Fld || isa<MDString>(Fld);
00414 }
00415 
00416 /// Check if a value can be a reference to a type.
00417 static bool isTypeRef(const Value *Val) {
00418   return !Val ||
00419          (isa<MDString>(Val) && !cast<MDString>(Val)->getString().empty()) ||
00420          (isa<MDNode>(Val) && DIType(cast<MDNode>(Val)).isType());
00421 }
00422 
00423 /// Check if a field at position Elt of a MDNode can be a reference to a type.
00424 static bool fieldIsTypeRef(const MDNode *DbgNode, unsigned Elt) {
00425   Value *Fld = getField(DbgNode, Elt);
00426   return isTypeRef(Fld);
00427 }
00428 
00429 /// Check if a value can be a ScopeRef.
00430 static bool isScopeRef(const Value *Val) {
00431   return !Val ||
00432     (isa<MDString>(Val) && !cast<MDString>(Val)->getString().empty()) ||
00433     // Not checking for Val->isScope() here, because it would work
00434     // only for lexical scopes and not all subclasses of DIScope.
00435     isa<MDNode>(Val);
00436 }
00437 
00438 /// Check if a field at position Elt of a MDNode can be a ScopeRef.
00439 static bool fieldIsScopeRef(const MDNode *DbgNode, unsigned Elt) {
00440   Value *Fld = getField(DbgNode, Elt);
00441   return isScopeRef(Fld);
00442 }
00443 
00444 /// Verify - Verify that a type descriptor is well formed.
00445 bool DIType::Verify() const {
00446   if (!isType())
00447     return false;
00448   // Make sure Context @ field 2 is MDNode.
00449   if (!fieldIsScopeRef(DbgNode, 2))
00450     return false;
00451 
00452   // FIXME: Sink this into the various subclass verifies.
00453   uint16_t Tag = getTag();
00454   if (!isBasicType() && Tag != dwarf::DW_TAG_const_type &&
00455       Tag != dwarf::DW_TAG_volatile_type && Tag != dwarf::DW_TAG_pointer_type &&
00456       Tag != dwarf::DW_TAG_ptr_to_member_type &&
00457       Tag != dwarf::DW_TAG_reference_type &&
00458       Tag != dwarf::DW_TAG_rvalue_reference_type &&
00459       Tag != dwarf::DW_TAG_restrict_type && Tag != dwarf::DW_TAG_array_type &&
00460       Tag != dwarf::DW_TAG_enumeration_type &&
00461       Tag != dwarf::DW_TAG_subroutine_type &&
00462       Tag != dwarf::DW_TAG_inheritance && Tag != dwarf::DW_TAG_friend &&
00463       getFilename().empty())
00464     return false;
00465   // DIType is abstract, it should be a BasicType, a DerivedType or
00466   // a CompositeType.
00467   if (isBasicType())
00468     return DIBasicType(DbgNode).Verify();
00469   else if (isCompositeType())
00470     return DICompositeType(DbgNode).Verify();
00471   else if (isDerivedType())
00472     return DIDerivedType(DbgNode).Verify();
00473   else
00474     return false;
00475 }
00476 
00477 /// Verify - Verify that a basic type descriptor is well formed.
00478 bool DIBasicType::Verify() const {
00479   return isBasicType() && DbgNode->getNumOperands() == 10;
00480 }
00481 
00482 /// Verify - Verify that a derived type descriptor is well formed.
00483 bool DIDerivedType::Verify() const {
00484   // Make sure DerivedFrom @ field 9 is TypeRef.
00485   if (!fieldIsTypeRef(DbgNode, 9))
00486     return false;
00487   if (getTag() == dwarf::DW_TAG_ptr_to_member_type)
00488     // Make sure ClassType @ field 10 is a TypeRef.
00489     if (!fieldIsTypeRef(DbgNode, 10))
00490       return false;
00491 
00492   return isDerivedType() && DbgNode->getNumOperands() >= 10 &&
00493          DbgNode->getNumOperands() <= 14;
00494 }
00495 
00496 /// Verify - Verify that a composite type descriptor is well formed.
00497 bool DICompositeType::Verify() const {
00498   if (!isCompositeType())
00499     return false;
00500 
00501   // Make sure DerivedFrom @ field 9 and ContainingType @ field 12 are TypeRef.
00502   if (!fieldIsTypeRef(DbgNode, 9))
00503     return false;
00504   if (!fieldIsTypeRef(DbgNode, 12))
00505     return false;
00506 
00507   // Make sure the type identifier at field 14 is MDString, it can be null.
00508   if (!fieldIsMDString(DbgNode, 14))
00509     return false;
00510 
00511   // A subroutine type can't be both & and &&.
00512   if (isLValueReference() && isRValueReference())
00513     return false;
00514 
00515   return DbgNode->getNumOperands() == 15;
00516 }
00517 
00518 /// Verify - Verify that a subprogram descriptor is well formed.
00519 bool DISubprogram::Verify() const {
00520   if (!isSubprogram())
00521     return false;
00522 
00523   // Make sure context @ field 2 is a ScopeRef and type @ field 7 is a MDNode.
00524   if (!fieldIsScopeRef(DbgNode, 2))
00525     return false;
00526   if (!fieldIsMDNode(DbgNode, 7))
00527     return false;
00528   // Containing type @ field 12.
00529   if (!fieldIsTypeRef(DbgNode, 12))
00530     return false;
00531 
00532   // A subprogram can't be both & and &&.
00533   if (isLValueReference() && isRValueReference())
00534     return false;
00535 
00536   return DbgNode->getNumOperands() == 20;
00537 }
00538 
00539 /// Verify - Verify that a global variable descriptor is well formed.
00540 bool DIGlobalVariable::Verify() const {
00541   if (!isGlobalVariable())
00542     return false;
00543 
00544   if (getDisplayName().empty())
00545     return false;
00546   // Make sure context @ field 2 is an MDNode.
00547   if (!fieldIsMDNode(DbgNode, 2))
00548     return false;
00549   // Make sure that type @ field 8 is a DITypeRef.
00550   if (!fieldIsTypeRef(DbgNode, 8))
00551     return false;
00552   // Make sure StaticDataMemberDeclaration @ field 12 is MDNode.
00553   if (!fieldIsMDNode(DbgNode, 12))
00554     return false;
00555 
00556   return DbgNode->getNumOperands() == 13;
00557 }
00558 
00559 /// Verify - Verify that a variable descriptor is well formed.
00560 bool DIVariable::Verify() const {
00561   if (!isVariable())
00562     return false;
00563 
00564   // Make sure context @ field 1 is an MDNode.
00565   if (!fieldIsMDNode(DbgNode, 1))
00566     return false;
00567   // Make sure that type @ field 5 is a DITypeRef.
00568   if (!fieldIsTypeRef(DbgNode, 5))
00569     return false;
00570   return DbgNode->getNumOperands() >= 8;
00571 }
00572 
00573 /// Verify - Verify that a location descriptor is well formed.
00574 bool DILocation::Verify() const {
00575   if (!DbgNode)
00576     return false;
00577 
00578   return DbgNode->getNumOperands() == 4;
00579 }
00580 
00581 /// Verify - Verify that a namespace descriptor is well formed.
00582 bool DINameSpace::Verify() const {
00583   if (!isNameSpace())
00584     return false;
00585   return DbgNode->getNumOperands() == 5;
00586 }
00587 
00588 /// \brief Retrieve the MDNode for the directory/file pair.
00589 MDNode *DIFile::getFileNode() const { return getNodeField(DbgNode, 1); }
00590 
00591 /// \brief Verify that the file descriptor is well formed.
00592 bool DIFile::Verify() const {
00593   return isFile() && DbgNode->getNumOperands() == 2;
00594 }
00595 
00596 /// \brief Verify that the enumerator descriptor is well formed.
00597 bool DIEnumerator::Verify() const {
00598   return isEnumerator() && DbgNode->getNumOperands() == 3;
00599 }
00600 
00601 /// \brief Verify that the subrange descriptor is well formed.
00602 bool DISubrange::Verify() const {
00603   return isSubrange() && DbgNode->getNumOperands() == 3;
00604 }
00605 
00606 /// \brief Verify that the lexical block descriptor is well formed.
00607 bool DILexicalBlock::Verify() const {
00608   return isLexicalBlock() && DbgNode->getNumOperands() == 7;
00609 }
00610 
00611 /// \brief Verify that the file-scoped lexical block descriptor is well formed.
00612 bool DILexicalBlockFile::Verify() const {
00613   return isLexicalBlockFile() && DbgNode->getNumOperands() == 3;
00614 }
00615 
00616 /// \brief Verify that an unspecified parameter descriptor is well formed.
00617 bool DIUnspecifiedParameter::Verify() const {
00618   return isUnspecifiedParameter() && DbgNode->getNumOperands() == 1;
00619 }
00620 
00621 /// \brief Verify that the template type parameter descriptor is well formed.
00622 bool DITemplateTypeParameter::Verify() const {
00623   return isTemplateTypeParameter() && DbgNode->getNumOperands() == 7;
00624 }
00625 
00626 /// \brief Verify that the template value parameter descriptor is well formed.
00627 bool DITemplateValueParameter::Verify() const {
00628   return isTemplateValueParameter() && DbgNode->getNumOperands() == 8;
00629 }
00630 
00631 /// \brief Verify that the imported module descriptor is well formed.
00632 bool DIImportedEntity::Verify() const {
00633   return isImportedEntity() &&
00634          (DbgNode->getNumOperands() == 4 || DbgNode->getNumOperands() == 5);
00635 }
00636 
00637 /// getObjCProperty - Return property node, if this ivar is associated with one.
00638 MDNode *DIDerivedType::getObjCProperty() const {
00639   return getNodeField(DbgNode, 10);
00640 }
00641 
00642 MDString *DICompositeType::getIdentifier() const {
00643   return cast_or_null<MDString>(getField(DbgNode, 14));
00644 }
00645 
00646 #ifndef NDEBUG
00647 static void VerifySubsetOf(const MDNode *LHS, const MDNode *RHS) {
00648   for (unsigned i = 0; i != LHS->getNumOperands(); ++i) {
00649     // Skip the 'empty' list (that's a single i32 0, rather than truly empty).
00650     if (i == 0 && isa<ConstantInt>(LHS->getOperand(i)))
00651       continue;
00652     const MDNode *E = cast<MDNode>(LHS->getOperand(i));
00653     bool found = false;
00654     for (unsigned j = 0; !found && j != RHS->getNumOperands(); ++j)
00655       found = E == RHS->getOperand(j);
00656     assert(found && "Losing a member during member list replacement");
00657   }
00658 }
00659 #endif
00660 
00661 /// \brief Set the array of member DITypes.
00662 void DICompositeType::setTypeArray(DIArray Elements, DIArray TParams) {
00663   assert((!TParams || DbgNode->getNumOperands() == 15) &&
00664          "If you're setting the template parameters this should include a slot "
00665          "for that!");
00666   TrackingVH<MDNode> N(*this);
00667   if (Elements) {
00668 #ifndef NDEBUG
00669     // Check that the new list of members contains all the old members as well.
00670     if (const MDNode *El = cast_or_null<MDNode>(N->getOperand(10)))
00671       VerifySubsetOf(El, Elements);
00672 #endif
00673     N->replaceOperandWith(10, Elements);
00674   }
00675   if (TParams)
00676     N->replaceOperandWith(13, TParams);
00677   DbgNode = N;
00678 }
00679 
00680 /// Generate a reference to this DIType. Uses the type identifier instead
00681 /// of the actual MDNode if possible, to help type uniquing.
00682 DIScopeRef DIScope::getRef() const {
00683   if (!isCompositeType())
00684     return DIScopeRef(*this);
00685   DICompositeType DTy(DbgNode);
00686   if (!DTy.getIdentifier())
00687     return DIScopeRef(*this);
00688   return DIScopeRef(DTy.getIdentifier());
00689 }
00690 
00691 /// \brief Set the containing type.
00692 void DICompositeType::setContainingType(DICompositeType ContainingType) {
00693   TrackingVH<MDNode> N(*this);
00694   N->replaceOperandWith(12, ContainingType.getRef());
00695   DbgNode = N;
00696 }
00697 
00698 /// isInlinedFnArgument - Return true if this variable provides debugging
00699 /// information for an inlined function arguments.
00700 bool DIVariable::isInlinedFnArgument(const Function *CurFn) {
00701   assert(CurFn && "Invalid function");
00702   if (!getContext().isSubprogram())
00703     return false;
00704   // This variable is not inlined function argument if its scope
00705   // does not describe current function.
00706   return !DISubprogram(getContext()).describes(CurFn);
00707 }
00708 
00709 /// describes - Return true if this subprogram provides debugging
00710 /// information for the function F.
00711 bool DISubprogram::describes(const Function *F) {
00712   assert(F && "Invalid function");
00713   if (F == getFunction())
00714     return true;
00715   StringRef Name = getLinkageName();
00716   if (Name.empty())
00717     Name = getName();
00718   if (F->getName() == Name)
00719     return true;
00720   return false;
00721 }
00722 
00723 unsigned DISubprogram::isOptimized() const {
00724   assert(DbgNode && "Invalid subprogram descriptor!");
00725   if (DbgNode->getNumOperands() == 15)
00726     return getUnsignedField(14);
00727   return 0;
00728 }
00729 
00730 MDNode *DISubprogram::getVariablesNodes() const {
00731   return getNodeField(DbgNode, 18);
00732 }
00733 
00734 DIArray DISubprogram::getVariables() const {
00735   return DIArray(getNodeField(DbgNode, 18));
00736 }
00737 
00738 Value *DITemplateValueParameter::getValue() const {
00739   return getField(DbgNode, 4);
00740 }
00741 
00742 // If the current node has a parent scope then return that,
00743 // else return an empty scope.
00744 DIScopeRef DIScope::getContext() const {
00745 
00746   if (isType())
00747     return DIType(DbgNode).getContext();
00748 
00749   if (isSubprogram())
00750     return DIScopeRef(DISubprogram(DbgNode).getContext());
00751 
00752   if (isLexicalBlock())
00753     return DIScopeRef(DILexicalBlock(DbgNode).getContext());
00754 
00755   if (isLexicalBlockFile())
00756     return DIScopeRef(DILexicalBlockFile(DbgNode).getContext());
00757 
00758   if (isNameSpace())
00759     return DIScopeRef(DINameSpace(DbgNode).getContext());
00760 
00761   assert((isFile() || isCompileUnit()) && "Unhandled type of scope.");
00762   return DIScopeRef(nullptr);
00763 }
00764 
00765 // If the scope node has a name, return that, else return an empty string.
00766 StringRef DIScope::getName() const {
00767   if (isType())
00768     return DIType(DbgNode).getName();
00769   if (isSubprogram())
00770     return DISubprogram(DbgNode).getName();
00771   if (isNameSpace())
00772     return DINameSpace(DbgNode).getName();
00773   assert((isLexicalBlock() || isLexicalBlockFile() || isFile() ||
00774           isCompileUnit()) &&
00775          "Unhandled type of scope.");
00776   return StringRef();
00777 }
00778 
00779 StringRef DIScope::getFilename() const {
00780   if (!DbgNode)
00781     return StringRef();
00782   return ::getStringField(getNodeField(DbgNode, 1), 0);
00783 }
00784 
00785 StringRef DIScope::getDirectory() const {
00786   if (!DbgNode)
00787     return StringRef();
00788   return ::getStringField(getNodeField(DbgNode, 1), 1);
00789 }
00790 
00791 DIArray DICompileUnit::getEnumTypes() const {
00792   if (!DbgNode || DbgNode->getNumOperands() < 13)
00793     return DIArray();
00794 
00795   return DIArray(getNodeField(DbgNode, 7));
00796 }
00797 
00798 DIArray DICompileUnit::getRetainedTypes() const {
00799   if (!DbgNode || DbgNode->getNumOperands() < 13)
00800     return DIArray();
00801 
00802   return DIArray(getNodeField(DbgNode, 8));
00803 }
00804 
00805 DIArray DICompileUnit::getSubprograms() const {
00806   if (!DbgNode || DbgNode->getNumOperands() < 13)
00807     return DIArray();
00808 
00809   return DIArray(getNodeField(DbgNode, 9));
00810 }
00811 
00812 DIArray DICompileUnit::getGlobalVariables() const {
00813   if (!DbgNode || DbgNode->getNumOperands() < 13)
00814     return DIArray();
00815 
00816   return DIArray(getNodeField(DbgNode, 10));
00817 }
00818 
00819 DIArray DICompileUnit::getImportedEntities() const {
00820   if (!DbgNode || DbgNode->getNumOperands() < 13)
00821     return DIArray();
00822 
00823   return DIArray(getNodeField(DbgNode, 11));
00824 }
00825 
00826 /// copyWithNewScope - Return a copy of this location, replacing the
00827 /// current scope with the given one.
00828 DILocation DILocation::copyWithNewScope(LLVMContext &Ctx,
00829                                         DILexicalBlock NewScope) {
00830   SmallVector<Value *, 10> Elts;
00831   assert(Verify());
00832   for (unsigned I = 0; I < DbgNode->getNumOperands(); ++I) {
00833     if (I != 2)
00834       Elts.push_back(DbgNode->getOperand(I));
00835     else
00836       Elts.push_back(NewScope);
00837   }
00838   MDNode *NewDIL = MDNode::get(Ctx, Elts);
00839   return DILocation(NewDIL);
00840 }
00841 
00842 /// computeNewDiscriminator - Generate a new discriminator value for this
00843 /// file and line location.
00844 unsigned DILocation::computeNewDiscriminator(LLVMContext &Ctx) {
00845   std::pair<const char *, unsigned> Key(getFilename().data(), getLineNumber());
00846   return ++Ctx.pImpl->DiscriminatorTable[Key];
00847 }
00848 
00849 /// fixupSubprogramName - Replace contains special characters used
00850 /// in a typical Objective-C names with '.' in a given string.
00851 static void fixupSubprogramName(DISubprogram Fn, SmallVectorImpl<char> &Out) {
00852   StringRef FName =
00853       Fn.getFunction() ? Fn.getFunction()->getName() : Fn.getName();
00854   FName = Function::getRealLinkageName(FName);
00855 
00856   StringRef Prefix("llvm.dbg.lv.");
00857   Out.reserve(FName.size() + Prefix.size());
00858   Out.append(Prefix.begin(), Prefix.end());
00859 
00860   bool isObjCLike = false;
00861   for (size_t i = 0, e = FName.size(); i < e; ++i) {
00862     char C = FName[i];
00863     if (C == '[')
00864       isObjCLike = true;
00865 
00866     if (isObjCLike && (C == '[' || C == ']' || C == ' ' || C == ':' ||
00867                        C == '+' || C == '(' || C == ')'))
00868       Out.push_back('.');
00869     else
00870       Out.push_back(C);
00871   }
00872 }
00873 
00874 /// getFnSpecificMDNode - Return a NameMDNode, if available, that is
00875 /// suitable to hold function specific information.
00876 NamedMDNode *llvm::getFnSpecificMDNode(const Module &M, DISubprogram Fn) {
00877   SmallString<32> Name;
00878   fixupSubprogramName(Fn, Name);
00879   return M.getNamedMetadata(Name.str());
00880 }
00881 
00882 /// getOrInsertFnSpecificMDNode - Return a NameMDNode that is suitable
00883 /// to hold function specific information.
00884 NamedMDNode *llvm::getOrInsertFnSpecificMDNode(Module &M, DISubprogram Fn) {
00885   SmallString<32> Name;
00886   fixupSubprogramName(Fn, Name);
00887   return M.getOrInsertNamedMetadata(Name.str());
00888 }
00889 
00890 /// createInlinedVariable - Create a new inlined variable based on current
00891 /// variable.
00892 /// @param DV            Current Variable.
00893 /// @param InlinedScope  Location at current variable is inlined.
00894 DIVariable llvm::createInlinedVariable(MDNode *DV, MDNode *InlinedScope,
00895                                        LLVMContext &VMContext) {
00896   SmallVector<Value *, 16> Elts;
00897   // Insert inlined scope as 7th element.
00898   for (unsigned i = 0, e = DV->getNumOperands(); i != e; ++i)
00899     i == 7 ? Elts.push_back(InlinedScope) : Elts.push_back(DV->getOperand(i));
00900   return DIVariable(MDNode::get(VMContext, Elts));
00901 }
00902 
00903 /// cleanseInlinedVariable - Remove inlined scope from the variable.
00904 DIVariable llvm::cleanseInlinedVariable(MDNode *DV, LLVMContext &VMContext) {
00905   SmallVector<Value *, 16> Elts;
00906   // Insert inlined scope as 7th element.
00907   for (unsigned i = 0, e = DV->getNumOperands(); i != e; ++i)
00908     i == 7 ? Elts.push_back(Constant::getNullValue(Type::getInt32Ty(VMContext)))
00909            : Elts.push_back(DV->getOperand(i));
00910   return DIVariable(MDNode::get(VMContext, Elts));
00911 }
00912 
00913 /// getDISubprogram - Find subprogram that is enclosing this scope.
00914 DISubprogram llvm::getDISubprogram(const MDNode *Scope) {
00915   DIDescriptor D(Scope);
00916   if (D.isSubprogram())
00917     return DISubprogram(Scope);
00918 
00919   if (D.isLexicalBlockFile())
00920     return getDISubprogram(DILexicalBlockFile(Scope).getContext());
00921 
00922   if (D.isLexicalBlock())
00923     return getDISubprogram(DILexicalBlock(Scope).getContext());
00924 
00925   return DISubprogram();
00926 }
00927 
00928 /// getDICompositeType - Find underlying composite type.
00929 DICompositeType llvm::getDICompositeType(DIType T) {
00930   if (T.isCompositeType())
00931     return DICompositeType(T);
00932 
00933   if (T.isDerivedType()) {
00934     // This function is currently used by dragonegg and dragonegg does
00935     // not generate identifier for types, so using an empty map to resolve
00936     // DerivedFrom should be fine.
00937     DITypeIdentifierMap EmptyMap;
00938     return getDICompositeType(
00939         DIDerivedType(T).getTypeDerivedFrom().resolve(EmptyMap));
00940   }
00941 
00942   return DICompositeType();
00943 }
00944 
00945 /// Update DITypeIdentifierMap by going through retained types of each CU.
00946 DITypeIdentifierMap
00947 llvm::generateDITypeIdentifierMap(const NamedMDNode *CU_Nodes) {
00948   DITypeIdentifierMap Map;
00949   for (unsigned CUi = 0, CUe = CU_Nodes->getNumOperands(); CUi != CUe; ++CUi) {
00950     DICompileUnit CU(CU_Nodes->getOperand(CUi));
00951     DIArray Retain = CU.getRetainedTypes();
00952     for (unsigned Ti = 0, Te = Retain.getNumElements(); Ti != Te; ++Ti) {
00953       if (!Retain.getElement(Ti).isCompositeType())
00954         continue;
00955       DICompositeType Ty(Retain.getElement(Ti));
00956       if (MDString *TypeId = Ty.getIdentifier()) {
00957         // Definition has priority over declaration.
00958         // Try to insert (TypeId, Ty) to Map.
00959         std::pair<DITypeIdentifierMap::iterator, bool> P =
00960             Map.insert(std::make_pair(TypeId, Ty));
00961         // If TypeId already exists in Map and this is a definition, replace
00962         // whatever we had (declaration or definition) with the definition.
00963         if (!P.second && !Ty.isForwardDecl())
00964           P.first->second = Ty;
00965       }
00966     }
00967   }
00968   return Map;
00969 }
00970 
00971 //===----------------------------------------------------------------------===//
00972 // DebugInfoFinder implementations.
00973 //===----------------------------------------------------------------------===//
00974 
00975 void DebugInfoFinder::reset() {
00976   CUs.clear();
00977   SPs.clear();
00978   GVs.clear();
00979   TYs.clear();
00980   Scopes.clear();
00981   NodesSeen.clear();
00982   TypeIdentifierMap.clear();
00983   TypeMapInitialized = false;
00984 }
00985 
00986 void DebugInfoFinder::InitializeTypeMap(const Module &M) {
00987   if (!TypeMapInitialized)
00988     if (NamedMDNode *CU_Nodes = M.getNamedMetadata("llvm.dbg.cu")) {
00989       TypeIdentifierMap = generateDITypeIdentifierMap(CU_Nodes);
00990       TypeMapInitialized = true;
00991     }
00992 }
00993 
00994 /// processModule - Process entire module and collect debug info.
00995 void DebugInfoFinder::processModule(const Module &M) {
00996   InitializeTypeMap(M);
00997   if (NamedMDNode *CU_Nodes = M.getNamedMetadata("llvm.dbg.cu")) {
00998     for (unsigned i = 0, e = CU_Nodes->getNumOperands(); i != e; ++i) {
00999       DICompileUnit CU(CU_Nodes->getOperand(i));
01000       addCompileUnit(CU);
01001       DIArray GVs = CU.getGlobalVariables();
01002       for (unsigned i = 0, e = GVs.getNumElements(); i != e; ++i) {
01003         DIGlobalVariable DIG(GVs.getElement(i));
01004         if (addGlobalVariable(DIG)) {
01005           processScope(DIG.getContext());
01006           processType(DIG.getType().resolve(TypeIdentifierMap));
01007         }
01008       }
01009       DIArray SPs = CU.getSubprograms();
01010       for (unsigned i = 0, e = SPs.getNumElements(); i != e; ++i)
01011         processSubprogram(DISubprogram(SPs.getElement(i)));
01012       DIArray EnumTypes = CU.getEnumTypes();
01013       for (unsigned i = 0, e = EnumTypes.getNumElements(); i != e; ++i)
01014         processType(DIType(EnumTypes.getElement(i)));
01015       DIArray RetainedTypes = CU.getRetainedTypes();
01016       for (unsigned i = 0, e = RetainedTypes.getNumElements(); i != e; ++i)
01017         processType(DIType(RetainedTypes.getElement(i)));
01018       DIArray Imports = CU.getImportedEntities();
01019       for (unsigned i = 0, e = Imports.getNumElements(); i != e; ++i) {
01020         DIImportedEntity Import = DIImportedEntity(Imports.getElement(i));
01021         DIDescriptor Entity = Import.getEntity().resolve(TypeIdentifierMap);
01022         if (Entity.isType())
01023           processType(DIType(Entity));
01024         else if (Entity.isSubprogram())
01025           processSubprogram(DISubprogram(Entity));
01026         else if (Entity.isNameSpace())
01027           processScope(DINameSpace(Entity).getContext());
01028       }
01029     }
01030   }
01031 }
01032 
01033 /// processLocation - Process DILocation.
01034 void DebugInfoFinder::processLocation(const Module &M, DILocation Loc) {
01035   if (!Loc)
01036     return;
01037   InitializeTypeMap(M);
01038   processScope(Loc.getScope());
01039   processLocation(M, Loc.getOrigLocation());
01040 }
01041 
01042 /// processType - Process DIType.
01043 void DebugInfoFinder::processType(DIType DT) {
01044   if (!addType(DT))
01045     return;
01046   processScope(DT.getContext().resolve(TypeIdentifierMap));
01047   if (DT.isCompositeType()) {
01048     DICompositeType DCT(DT);
01049     processType(DCT.getTypeDerivedFrom().resolve(TypeIdentifierMap));
01050     DIArray DA = DCT.getTypeArray();
01051     for (unsigned i = 0, e = DA.getNumElements(); i != e; ++i) {
01052       DIDescriptor D = DA.getElement(i);
01053       if (D.isType())
01054         processType(DIType(D));
01055       else if (D.isSubprogram())
01056         processSubprogram(DISubprogram(D));
01057     }
01058   } else if (DT.isDerivedType()) {
01059     DIDerivedType DDT(DT);
01060     processType(DDT.getTypeDerivedFrom().resolve(TypeIdentifierMap));
01061   }
01062 }
01063 
01064 void DebugInfoFinder::processScope(DIScope Scope) {
01065   if (Scope.isType()) {
01066     DIType Ty(Scope);
01067     processType(Ty);
01068     return;
01069   }
01070   if (Scope.isCompileUnit()) {
01071     addCompileUnit(DICompileUnit(Scope));
01072     return;
01073   }
01074   if (Scope.isSubprogram()) {
01075     processSubprogram(DISubprogram(Scope));
01076     return;
01077   }
01078   if (!addScope(Scope))
01079     return;
01080   if (Scope.isLexicalBlock()) {
01081     DILexicalBlock LB(Scope);
01082     processScope(LB.getContext());
01083   } else if (Scope.isLexicalBlockFile()) {
01084     DILexicalBlockFile LBF = DILexicalBlockFile(Scope);
01085     processScope(LBF.getScope());
01086   } else if (Scope.isNameSpace()) {
01087     DINameSpace NS(Scope);
01088     processScope(NS.getContext());
01089   }
01090 }
01091 
01092 /// processSubprogram - Process DISubprogram.
01093 void DebugInfoFinder::processSubprogram(DISubprogram SP) {
01094   if (!addSubprogram(SP))
01095     return;
01096   processScope(SP.getContext().resolve(TypeIdentifierMap));
01097   processType(SP.getType());
01098   DIArray TParams = SP.getTemplateParams();
01099   for (unsigned I = 0, E = TParams.getNumElements(); I != E; ++I) {
01100     DIDescriptor Element = TParams.getElement(I);
01101     if (Element.isTemplateTypeParameter()) {
01102       DITemplateTypeParameter TType(Element);
01103       processScope(TType.getContext().resolve(TypeIdentifierMap));
01104       processType(TType.getType().resolve(TypeIdentifierMap));
01105     } else if (Element.isTemplateValueParameter()) {
01106       DITemplateValueParameter TVal(Element);
01107       processScope(TVal.getContext().resolve(TypeIdentifierMap));
01108       processType(TVal.getType().resolve(TypeIdentifierMap));
01109     }
01110   }
01111 }
01112 
01113 /// processDeclare - Process DbgDeclareInst.
01114 void DebugInfoFinder::processDeclare(const Module &M,
01115                                      const DbgDeclareInst *DDI) {
01116   MDNode *N = dyn_cast<MDNode>(DDI->getVariable());
01117   if (!N)
01118     return;
01119   InitializeTypeMap(M);
01120 
01121   DIDescriptor DV(N);
01122   if (!DV.isVariable())
01123     return;
01124 
01125   if (!NodesSeen.insert(DV))
01126     return;
01127   processScope(DIVariable(N).getContext());
01128   processType(DIVariable(N).getType().resolve(TypeIdentifierMap));
01129 }
01130 
01131 void DebugInfoFinder::processValue(const Module &M, const DbgValueInst *DVI) {
01132   MDNode *N = dyn_cast<MDNode>(DVI->getVariable());
01133   if (!N)
01134     return;
01135   InitializeTypeMap(M);
01136 
01137   DIDescriptor DV(N);
01138   if (!DV.isVariable())
01139     return;
01140 
01141   if (!NodesSeen.insert(DV))
01142     return;
01143   processScope(DIVariable(N).getContext());
01144   processType(DIVariable(N).getType().resolve(TypeIdentifierMap));
01145 }
01146 
01147 /// addType - Add type into Tys.
01148 bool DebugInfoFinder::addType(DIType DT) {
01149   if (!DT)
01150     return false;
01151 
01152   if (!NodesSeen.insert(DT))
01153     return false;
01154 
01155   TYs.push_back(DT);
01156   return true;
01157 }
01158 
01159 /// addCompileUnit - Add compile unit into CUs.
01160 bool DebugInfoFinder::addCompileUnit(DICompileUnit CU) {
01161   if (!CU)
01162     return false;
01163   if (!NodesSeen.insert(CU))
01164     return false;
01165 
01166   CUs.push_back(CU);
01167   return true;
01168 }
01169 
01170 /// addGlobalVariable - Add global variable into GVs.
01171 bool DebugInfoFinder::addGlobalVariable(DIGlobalVariable DIG) {
01172   if (!DIG)
01173     return false;
01174 
01175   if (!NodesSeen.insert(DIG))
01176     return false;
01177 
01178   GVs.push_back(DIG);
01179   return true;
01180 }
01181 
01182 // addSubprogram - Add subprgoram into SPs.
01183 bool DebugInfoFinder::addSubprogram(DISubprogram SP) {
01184   if (!SP)
01185     return false;
01186 
01187   if (!NodesSeen.insert(SP))
01188     return false;
01189 
01190   SPs.push_back(SP);
01191   return true;
01192 }
01193 
01194 bool DebugInfoFinder::addScope(DIScope Scope) {
01195   if (!Scope)
01196     return false;
01197   // FIXME: Ocaml binding generates a scope with no content, we treat it
01198   // as null for now.
01199   if (Scope->getNumOperands() == 0)
01200     return false;
01201   if (!NodesSeen.insert(Scope))
01202     return false;
01203   Scopes.push_back(Scope);
01204   return true;
01205 }
01206 
01207 //===----------------------------------------------------------------------===//
01208 // DIDescriptor: dump routines for all descriptors.
01209 //===----------------------------------------------------------------------===//
01210 
01211 /// dump - Print descriptor to dbgs() with a newline.
01212 void DIDescriptor::dump() const {
01213   print(dbgs());
01214   dbgs() << '\n';
01215 }
01216 
01217 /// print - Print descriptor.
01218 void DIDescriptor::print(raw_ostream &OS) const {
01219   if (!DbgNode)
01220     return;
01221 
01222   if (const char *Tag = dwarf::TagString(getTag()))
01223     OS << "[ " << Tag << " ]";
01224 
01225   if (this->isSubrange()) {
01226     DISubrange(DbgNode).printInternal(OS);
01227   } else if (this->isCompileUnit()) {
01228     DICompileUnit(DbgNode).printInternal(OS);
01229   } else if (this->isFile()) {
01230     DIFile(DbgNode).printInternal(OS);
01231   } else if (this->isEnumerator()) {
01232     DIEnumerator(DbgNode).printInternal(OS);
01233   } else if (this->isBasicType()) {
01234     DIType(DbgNode).printInternal(OS);
01235   } else if (this->isDerivedType()) {
01236     DIDerivedType(DbgNode).printInternal(OS);
01237   } else if (this->isCompositeType()) {
01238     DICompositeType(DbgNode).printInternal(OS);
01239   } else if (this->isSubprogram()) {
01240     DISubprogram(DbgNode).printInternal(OS);
01241   } else if (this->isGlobalVariable()) {
01242     DIGlobalVariable(DbgNode).printInternal(OS);
01243   } else if (this->isVariable()) {
01244     DIVariable(DbgNode).printInternal(OS);
01245   } else if (this->isObjCProperty()) {
01246     DIObjCProperty(DbgNode).printInternal(OS);
01247   } else if (this->isNameSpace()) {
01248     DINameSpace(DbgNode).printInternal(OS);
01249   } else if (this->isScope()) {
01250     DIScope(DbgNode).printInternal(OS);
01251   }
01252 }
01253 
01254 void DISubrange::printInternal(raw_ostream &OS) const {
01255   int64_t Count = getCount();
01256   if (Count != -1)
01257     OS << " [" << getLo() << ", " << Count - 1 << ']';
01258   else
01259     OS << " [unbounded]";
01260 }
01261 
01262 void DIScope::printInternal(raw_ostream &OS) const {
01263   OS << " [" << getDirectory() << "/" << getFilename() << ']';
01264 }
01265 
01266 void DICompileUnit::printInternal(raw_ostream &OS) const {
01267   DIScope::printInternal(OS);
01268   OS << " [";
01269   unsigned Lang = getLanguage();
01270   if (const char *LangStr = dwarf::LanguageString(Lang))
01271     OS << LangStr;
01272   else
01273     (OS << "lang 0x").write_hex(Lang);
01274   OS << ']';
01275 }
01276 
01277 void DIEnumerator::printInternal(raw_ostream &OS) const {
01278   OS << " [" << getName() << " :: " << getEnumValue() << ']';
01279 }
01280 
01281 void DIType::printInternal(raw_ostream &OS) const {
01282   if (!DbgNode)
01283     return;
01284 
01285   StringRef Res = getName();
01286   if (!Res.empty())
01287     OS << " [" << Res << "]";
01288 
01289   // TODO: Print context?
01290 
01291   OS << " [line " << getLineNumber() << ", size " << getSizeInBits()
01292      << ", align " << getAlignInBits() << ", offset " << getOffsetInBits();
01293   if (isBasicType())
01294     if (const char *Enc =
01295             dwarf::AttributeEncodingString(DIBasicType(DbgNode).getEncoding()))
01296       OS << ", enc " << Enc;
01297   OS << "]";
01298 
01299   if (isPrivate())
01300     OS << " [private]";
01301   else if (isProtected())
01302     OS << " [protected]";
01303 
01304   if (isArtificial())
01305     OS << " [artificial]";
01306 
01307   if (isForwardDecl())
01308     OS << " [decl]";
01309   else if (getTag() == dwarf::DW_TAG_structure_type ||
01310            getTag() == dwarf::DW_TAG_union_type ||
01311            getTag() == dwarf::DW_TAG_enumeration_type ||
01312            getTag() == dwarf::DW_TAG_class_type)
01313     OS << " [def]";
01314   if (isVector())
01315     OS << " [vector]";
01316   if (isStaticMember())
01317     OS << " [static]";
01318 
01319   if (isLValueReference())
01320     OS << " [reference]";
01321 
01322   if (isRValueReference())
01323     OS << " [rvalue reference]";
01324 }
01325 
01326 void DIDerivedType::printInternal(raw_ostream &OS) const {
01327   DIType::printInternal(OS);
01328   OS << " [from " << getTypeDerivedFrom().getName() << ']';
01329 }
01330 
01331 void DICompositeType::printInternal(raw_ostream &OS) const {
01332   DIType::printInternal(OS);
01333   DIArray A = getTypeArray();
01334   OS << " [" << A.getNumElements() << " elements]";
01335 }
01336 
01337 void DINameSpace::printInternal(raw_ostream &OS) const {
01338   StringRef Name = getName();
01339   if (!Name.empty())
01340     OS << " [" << Name << ']';
01341 
01342   OS << " [line " << getLineNumber() << ']';
01343 }
01344 
01345 void DISubprogram::printInternal(raw_ostream &OS) const {
01346   // TODO : Print context
01347   OS << " [line " << getLineNumber() << ']';
01348 
01349   if (isLocalToUnit())
01350     OS << " [local]";
01351 
01352   if (isDefinition())
01353     OS << " [def]";
01354 
01355   if (getScopeLineNumber() != getLineNumber())
01356     OS << " [scope " << getScopeLineNumber() << "]";
01357 
01358   if (isPrivate())
01359     OS << " [private]";
01360   else if (isProtected())
01361     OS << " [protected]";
01362 
01363   if (isLValueReference())
01364     OS << " [reference]";
01365 
01366   if (isRValueReference())
01367     OS << " [rvalue reference]";
01368 
01369   StringRef Res = getName();
01370   if (!Res.empty())
01371     OS << " [" << Res << ']';
01372 }
01373 
01374 void DIGlobalVariable::printInternal(raw_ostream &OS) const {
01375   StringRef Res = getName();
01376   if (!Res.empty())
01377     OS << " [" << Res << ']';
01378 
01379   OS << " [line " << getLineNumber() << ']';
01380 
01381   // TODO : Print context
01382 
01383   if (isLocalToUnit())
01384     OS << " [local]";
01385 
01386   if (isDefinition())
01387     OS << " [def]";
01388 }
01389 
01390 void DIVariable::printInternal(raw_ostream &OS) const {
01391   StringRef Res = getName();
01392   if (!Res.empty())
01393     OS << " [" << Res << ']';
01394 
01395   OS << " [line " << getLineNumber() << ']';
01396 }
01397 
01398 void DIObjCProperty::printInternal(raw_ostream &OS) const {
01399   StringRef Name = getObjCPropertyName();
01400   if (!Name.empty())
01401     OS << " [" << Name << ']';
01402 
01403   OS << " [line " << getLineNumber() << ", properties " << getUnsignedField(6)
01404      << ']';
01405 }
01406 
01407 static void printDebugLoc(DebugLoc DL, raw_ostream &CommentOS,
01408                           const LLVMContext &Ctx) {
01409   if (!DL.isUnknown()) { // Print source line info.
01410     DIScope Scope(DL.getScope(Ctx));
01411     assert(Scope.isScope() && "Scope of a DebugLoc should be a DIScope.");
01412     // Omit the directory, because it's likely to be long and uninteresting.
01413     CommentOS << Scope.getFilename();
01414     CommentOS << ':' << DL.getLine();
01415     if (DL.getCol() != 0)
01416       CommentOS << ':' << DL.getCol();
01417     DebugLoc InlinedAtDL = DebugLoc::getFromDILocation(DL.getInlinedAt(Ctx));
01418     if (!InlinedAtDL.isUnknown()) {
01419       CommentOS << " @[ ";
01420       printDebugLoc(InlinedAtDL, CommentOS, Ctx);
01421       CommentOS << " ]";
01422     }
01423   }
01424 }
01425 
01426 void DIVariable::printExtendedName(raw_ostream &OS) const {
01427   const LLVMContext &Ctx = DbgNode->getContext();
01428   StringRef Res = getName();
01429   if (!Res.empty())
01430     OS << Res << "," << getLineNumber();
01431   if (MDNode *InlinedAt = getInlinedAt()) {
01432     DebugLoc InlinedAtDL = DebugLoc::getFromDILocation(InlinedAt);
01433     if (!InlinedAtDL.isUnknown()) {
01434       OS << " @[";
01435       printDebugLoc(InlinedAtDL, OS, Ctx);
01436       OS << "]";
01437     }
01438   }
01439 }
01440 
01441 /// Specialize constructor to make sure it has the correct type.
01442 template <> DIRef<DIScope>::DIRef(const Value *V) : Val(V) {
01443   assert(isScopeRef(V) && "DIScopeRef should be a MDString or MDNode");
01444 }
01445 template <> DIRef<DIType>::DIRef(const Value *V) : Val(V) {
01446   assert(isTypeRef(V) && "DITypeRef should be a MDString or MDNode");
01447 }
01448 
01449 /// Specialize getFieldAs to handle fields that are references to DIScopes.
01450 template <>
01451 DIScopeRef DIDescriptor::getFieldAs<DIScopeRef>(unsigned Elt) const {
01452   return DIScopeRef(getField(DbgNode, Elt));
01453 }
01454 /// Specialize getFieldAs to handle fields that are references to DITypes.
01455 template <> DITypeRef DIDescriptor::getFieldAs<DITypeRef>(unsigned Elt) const {
01456   return DITypeRef(getField(DbgNode, Elt));
01457 }
01458 
01459 /// Strip debug info in the module if it exists.
01460 /// To do this, we remove all calls to the debugger intrinsics and any named
01461 /// metadata for debugging. We also remove debug locations for instructions.
01462 /// Return true if module is modified.
01463 bool llvm::StripDebugInfo(Module &M) {
01464 
01465   bool Changed = false;
01466 
01467   // Remove all of the calls to the debugger intrinsics, and remove them from
01468   // the module.
01469   if (Function *Declare = M.getFunction("llvm.dbg.declare")) {
01470     while (!Declare->use_empty()) {
01471       CallInst *CI = cast<CallInst>(Declare->user_back());
01472       CI->eraseFromParent();
01473     }
01474     Declare->eraseFromParent();
01475     Changed = true;
01476   }
01477 
01478   if (Function *DbgVal = M.getFunction("llvm.dbg.value")) {
01479     while (!DbgVal->use_empty()) {
01480       CallInst *CI = cast<CallInst>(DbgVal->user_back());
01481       CI->eraseFromParent();
01482     }
01483     DbgVal->eraseFromParent();
01484     Changed = true;
01485   }
01486 
01487   for (Module::named_metadata_iterator NMI = M.named_metadata_begin(),
01488          NME = M.named_metadata_end(); NMI != NME;) {
01489     NamedMDNode *NMD = NMI;
01490     ++NMI;
01491     if (NMD->getName().startswith("llvm.dbg.")) {
01492       NMD->eraseFromParent();
01493       Changed = true;
01494     }
01495   }
01496 
01497   for (Module::iterator MI = M.begin(), ME = M.end(); MI != ME; ++MI)
01498     for (Function::iterator FI = MI->begin(), FE = MI->end(); FI != FE;
01499          ++FI)
01500       for (BasicBlock::iterator BI = FI->begin(), BE = FI->end(); BI != BE;
01501            ++BI) {
01502         if (!BI->getDebugLoc().isUnknown()) {
01503           Changed = true;
01504           BI->setDebugLoc(DebugLoc());
01505         }
01506       }
01507 
01508   return Changed;
01509 }
01510 
01511 /// Return Debug Info Metadata Version by checking module flags.
01512 unsigned llvm::getDebugMetadataVersionFromModule(const Module &M) {
01513   Value *Val = M.getModuleFlag("Debug Info Version");
01514   if (!Val)
01515     return 0;
01516   return cast<ConstantInt>(Val)->getZExtValue();
01517 }