DebugInfo.cpp revision dce4a407a24b04eebc6a376f8e62b41aaa7b071f
1//===--- DebugInfo.cpp - Debug Information Helper Classes -----------------===// 2// 3// The LLVM Compiler Infrastructure 4// 5// This file is distributed under the University of Illinois Open Source 6// License. See LICENSE.TXT for details. 7// 8//===----------------------------------------------------------------------===// 9// 10// This file implements the helper classes used to build and interpret debug 11// information in LLVM IR form. 12// 13//===----------------------------------------------------------------------===// 14 15#include "llvm/IR/DebugInfo.h" 16#include "LLVMContextImpl.h" 17#include "llvm/ADT/STLExtras.h" 18#include "llvm/ADT/SmallPtrSet.h" 19#include "llvm/ADT/SmallString.h" 20#include "llvm/Analysis/ValueTracking.h" 21#include "llvm/IR/Constants.h" 22#include "llvm/IR/DerivedTypes.h" 23#include "llvm/IR/Instructions.h" 24#include "llvm/IR/IntrinsicInst.h" 25#include "llvm/IR/Intrinsics.h" 26#include "llvm/IR/Module.h" 27#include "llvm/IR/ValueHandle.h" 28#include "llvm/Support/Debug.h" 29#include "llvm/Support/Dwarf.h" 30#include "llvm/Support/raw_ostream.h" 31using namespace llvm; 32using namespace llvm::dwarf; 33 34//===----------------------------------------------------------------------===// 35// DIDescriptor 36//===----------------------------------------------------------------------===// 37 38bool DIDescriptor::Verify() const { 39 return DbgNode && 40 (DIDerivedType(DbgNode).Verify() || 41 DICompositeType(DbgNode).Verify() || DIBasicType(DbgNode).Verify() || 42 DIVariable(DbgNode).Verify() || DISubprogram(DbgNode).Verify() || 43 DIGlobalVariable(DbgNode).Verify() || DIFile(DbgNode).Verify() || 44 DICompileUnit(DbgNode).Verify() || DINameSpace(DbgNode).Verify() || 45 DILexicalBlock(DbgNode).Verify() || 46 DILexicalBlockFile(DbgNode).Verify() || 47 DISubrange(DbgNode).Verify() || DIEnumerator(DbgNode).Verify() || 48 DIObjCProperty(DbgNode).Verify() || 49 DIUnspecifiedParameter(DbgNode).Verify() || 50 DITemplateTypeParameter(DbgNode).Verify() || 51 DITemplateValueParameter(DbgNode).Verify() || 52 DIImportedEntity(DbgNode).Verify()); 53} 54 55static Value *getField(const MDNode *DbgNode, unsigned Elt) { 56 if (!DbgNode || Elt >= DbgNode->getNumOperands()) 57 return nullptr; 58 return DbgNode->getOperand(Elt); 59} 60 61static MDNode *getNodeField(const MDNode *DbgNode, unsigned Elt) { 62 return dyn_cast_or_null<MDNode>(getField(DbgNode, Elt)); 63} 64 65static StringRef getStringField(const MDNode *DbgNode, unsigned Elt) { 66 if (MDString *MDS = dyn_cast_or_null<MDString>(getField(DbgNode, Elt))) 67 return MDS->getString(); 68 return StringRef(); 69} 70 71StringRef DIDescriptor::getStringField(unsigned Elt) const { 72 return ::getStringField(DbgNode, Elt); 73} 74 75uint64_t DIDescriptor::getUInt64Field(unsigned Elt) const { 76 if (!DbgNode) 77 return 0; 78 79 if (Elt < DbgNode->getNumOperands()) 80 if (ConstantInt *CI = 81 dyn_cast_or_null<ConstantInt>(DbgNode->getOperand(Elt))) 82 return CI->getZExtValue(); 83 84 return 0; 85} 86 87int64_t DIDescriptor::getInt64Field(unsigned Elt) const { 88 if (!DbgNode) 89 return 0; 90 91 if (Elt < DbgNode->getNumOperands()) 92 if (ConstantInt *CI = 93 dyn_cast_or_null<ConstantInt>(DbgNode->getOperand(Elt))) 94 return CI->getSExtValue(); 95 96 return 0; 97} 98 99DIDescriptor DIDescriptor::getDescriptorField(unsigned Elt) const { 100 MDNode *Field = getNodeField(DbgNode, Elt); 101 return DIDescriptor(Field); 102} 103 104GlobalVariable *DIDescriptor::getGlobalVariableField(unsigned Elt) const { 105 if (!DbgNode) 106 return nullptr; 107 108 if (Elt < DbgNode->getNumOperands()) 109 return dyn_cast_or_null<GlobalVariable>(DbgNode->getOperand(Elt)); 110 return nullptr; 111} 112 113Constant *DIDescriptor::getConstantField(unsigned Elt) const { 114 if (!DbgNode) 115 return nullptr; 116 117 if (Elt < DbgNode->getNumOperands()) 118 return dyn_cast_or_null<Constant>(DbgNode->getOperand(Elt)); 119 return nullptr; 120} 121 122Function *DIDescriptor::getFunctionField(unsigned Elt) const { 123 if (!DbgNode) 124 return nullptr; 125 126 if (Elt < DbgNode->getNumOperands()) 127 return dyn_cast_or_null<Function>(DbgNode->getOperand(Elt)); 128 return nullptr; 129} 130 131void DIDescriptor::replaceFunctionField(unsigned Elt, Function *F) { 132 if (!DbgNode) 133 return; 134 135 if (Elt < DbgNode->getNumOperands()) { 136 MDNode *Node = const_cast<MDNode *>(DbgNode); 137 Node->replaceOperandWith(Elt, F); 138 } 139} 140 141unsigned DIVariable::getNumAddrElements() const { 142 return DbgNode->getNumOperands() - 8; 143} 144 145/// getInlinedAt - If this variable is inlined then return inline location. 146MDNode *DIVariable::getInlinedAt() const { return getNodeField(DbgNode, 7); } 147 148//===----------------------------------------------------------------------===// 149// Predicates 150//===----------------------------------------------------------------------===// 151 152/// isBasicType - Return true if the specified tag is legal for 153/// DIBasicType. 154bool DIDescriptor::isBasicType() const { 155 if (!DbgNode) 156 return false; 157 switch (getTag()) { 158 case dwarf::DW_TAG_base_type: 159 case dwarf::DW_TAG_unspecified_type: 160 return true; 161 default: 162 return false; 163 } 164} 165 166/// isDerivedType - Return true if the specified tag is legal for DIDerivedType. 167bool DIDescriptor::isDerivedType() const { 168 if (!DbgNode) 169 return false; 170 switch (getTag()) { 171 case dwarf::DW_TAG_typedef: 172 case dwarf::DW_TAG_pointer_type: 173 case dwarf::DW_TAG_ptr_to_member_type: 174 case dwarf::DW_TAG_reference_type: 175 case dwarf::DW_TAG_rvalue_reference_type: 176 case dwarf::DW_TAG_const_type: 177 case dwarf::DW_TAG_volatile_type: 178 case dwarf::DW_TAG_restrict_type: 179 case dwarf::DW_TAG_member: 180 case dwarf::DW_TAG_inheritance: 181 case dwarf::DW_TAG_friend: 182 return true; 183 default: 184 // CompositeTypes are currently modelled as DerivedTypes. 185 return isCompositeType(); 186 } 187} 188 189/// isCompositeType - Return true if the specified tag is legal for 190/// DICompositeType. 191bool DIDescriptor::isCompositeType() const { 192 if (!DbgNode) 193 return false; 194 switch (getTag()) { 195 case dwarf::DW_TAG_array_type: 196 case dwarf::DW_TAG_structure_type: 197 case dwarf::DW_TAG_union_type: 198 case dwarf::DW_TAG_enumeration_type: 199 case dwarf::DW_TAG_subroutine_type: 200 case dwarf::DW_TAG_class_type: 201 return true; 202 default: 203 return false; 204 } 205} 206 207/// isVariable - Return true if the specified tag is legal for DIVariable. 208bool DIDescriptor::isVariable() const { 209 if (!DbgNode) 210 return false; 211 switch (getTag()) { 212 case dwarf::DW_TAG_auto_variable: 213 case dwarf::DW_TAG_arg_variable: 214 return true; 215 default: 216 return false; 217 } 218} 219 220/// isType - Return true if the specified tag is legal for DIType. 221bool DIDescriptor::isType() const { 222 return isBasicType() || isCompositeType() || isDerivedType(); 223} 224 225/// isSubprogram - Return true if the specified tag is legal for 226/// DISubprogram. 227bool DIDescriptor::isSubprogram() const { 228 return DbgNode && getTag() == dwarf::DW_TAG_subprogram; 229} 230 231/// isGlobalVariable - Return true if the specified tag is legal for 232/// DIGlobalVariable. 233bool DIDescriptor::isGlobalVariable() const { 234 return DbgNode && (getTag() == dwarf::DW_TAG_variable || 235 getTag() == dwarf::DW_TAG_constant); 236} 237 238/// isUnspecifiedParmeter - Return true if the specified tag is 239/// DW_TAG_unspecified_parameters. 240bool DIDescriptor::isUnspecifiedParameter() const { 241 return DbgNode && getTag() == dwarf::DW_TAG_unspecified_parameters; 242} 243 244/// isScope - Return true if the specified tag is one of the scope 245/// related tag. 246bool DIDescriptor::isScope() const { 247 if (!DbgNode) 248 return false; 249 switch (getTag()) { 250 case dwarf::DW_TAG_compile_unit: 251 case dwarf::DW_TAG_lexical_block: 252 case dwarf::DW_TAG_subprogram: 253 case dwarf::DW_TAG_namespace: 254 case dwarf::DW_TAG_file_type: 255 return true; 256 default: 257 break; 258 } 259 return isType(); 260} 261 262/// isTemplateTypeParameter - Return true if the specified tag is 263/// DW_TAG_template_type_parameter. 264bool DIDescriptor::isTemplateTypeParameter() const { 265 return DbgNode && getTag() == dwarf::DW_TAG_template_type_parameter; 266} 267 268/// isTemplateValueParameter - Return true if the specified tag is 269/// DW_TAG_template_value_parameter. 270bool DIDescriptor::isTemplateValueParameter() const { 271 return DbgNode && (getTag() == dwarf::DW_TAG_template_value_parameter || 272 getTag() == dwarf::DW_TAG_GNU_template_template_param || 273 getTag() == dwarf::DW_TAG_GNU_template_parameter_pack); 274} 275 276/// isCompileUnit - Return true if the specified tag is DW_TAG_compile_unit. 277bool DIDescriptor::isCompileUnit() const { 278 return DbgNode && getTag() == dwarf::DW_TAG_compile_unit; 279} 280 281/// isFile - Return true if the specified tag is DW_TAG_file_type. 282bool DIDescriptor::isFile() const { 283 return DbgNode && getTag() == dwarf::DW_TAG_file_type; 284} 285 286/// isNameSpace - Return true if the specified tag is DW_TAG_namespace. 287bool DIDescriptor::isNameSpace() const { 288 return DbgNode && getTag() == dwarf::DW_TAG_namespace; 289} 290 291/// isLexicalBlockFile - Return true if the specified descriptor is a 292/// lexical block with an extra file. 293bool DIDescriptor::isLexicalBlockFile() const { 294 return DbgNode && getTag() == dwarf::DW_TAG_lexical_block && 295 (DbgNode->getNumOperands() == 3); 296} 297 298/// isLexicalBlock - Return true if the specified tag is DW_TAG_lexical_block. 299bool DIDescriptor::isLexicalBlock() const { 300 return DbgNode && getTag() == dwarf::DW_TAG_lexical_block && 301 (DbgNode->getNumOperands() > 3); 302} 303 304/// isSubrange - Return true if the specified tag is DW_TAG_subrange_type. 305bool DIDescriptor::isSubrange() const { 306 return DbgNode && getTag() == dwarf::DW_TAG_subrange_type; 307} 308 309/// isEnumerator - Return true if the specified tag is DW_TAG_enumerator. 310bool DIDescriptor::isEnumerator() const { 311 return DbgNode && getTag() == dwarf::DW_TAG_enumerator; 312} 313 314/// isObjCProperty - Return true if the specified tag is DW_TAG_APPLE_property. 315bool DIDescriptor::isObjCProperty() const { 316 return DbgNode && getTag() == dwarf::DW_TAG_APPLE_property; 317} 318 319/// \brief Return true if the specified tag is DW_TAG_imported_module or 320/// DW_TAG_imported_declaration. 321bool DIDescriptor::isImportedEntity() const { 322 return DbgNode && (getTag() == dwarf::DW_TAG_imported_module || 323 getTag() == dwarf::DW_TAG_imported_declaration); 324} 325 326//===----------------------------------------------------------------------===// 327// Simple Descriptor Constructors and other Methods 328//===----------------------------------------------------------------------===// 329 330unsigned DIArray::getNumElements() const { 331 if (!DbgNode) 332 return 0; 333 return DbgNode->getNumOperands(); 334} 335 336/// replaceAllUsesWith - Replace all uses of the MDNode used by this 337/// type with the one in the passed descriptor. 338void DIType::replaceAllUsesWith(LLVMContext &VMContext, DIDescriptor D) { 339 340 assert(DbgNode && "Trying to replace an unverified type!"); 341 342 // Since we use a TrackingVH for the node, its easy for clients to manufacture 343 // legitimate situations where they want to replaceAllUsesWith() on something 344 // which, due to uniquing, has merged with the source. We shield clients from 345 // this detail by allowing a value to be replaced with replaceAllUsesWith() 346 // itself. 347 const MDNode *DN = D; 348 if (DbgNode == DN) { 349 SmallVector<Value*, 10> Ops(DbgNode->getNumOperands()); 350 for (size_t i = 0; i != Ops.size(); ++i) 351 Ops[i] = DbgNode->getOperand(i); 352 DN = MDNode::get(VMContext, Ops); 353 } 354 355 MDNode *Node = const_cast<MDNode *>(DbgNode); 356 const Value *V = cast_or_null<Value>(DN); 357 Node->replaceAllUsesWith(const_cast<Value *>(V)); 358 MDNode::deleteTemporary(Node); 359 DbgNode = D; 360} 361 362/// replaceAllUsesWith - Replace all uses of the MDNode used by this 363/// type with the one in D. 364void DIType::replaceAllUsesWith(MDNode *D) { 365 366 assert(DbgNode && "Trying to replace an unverified type!"); 367 assert(DbgNode != D && "This replacement should always happen"); 368 MDNode *Node = const_cast<MDNode *>(DbgNode); 369 const MDNode *DN = D; 370 const Value *V = cast_or_null<Value>(DN); 371 Node->replaceAllUsesWith(const_cast<Value *>(V)); 372 MDNode::deleteTemporary(Node); 373} 374 375/// Verify - Verify that a compile unit is well formed. 376bool DICompileUnit::Verify() const { 377 if (!isCompileUnit()) 378 return false; 379 380 // Don't bother verifying the compilation directory or producer string 381 // as those could be empty. 382 if (getFilename().empty()) 383 return false; 384 385 return DbgNode->getNumOperands() == 14; 386} 387 388/// Verify - Verify that an ObjC property is well formed. 389bool DIObjCProperty::Verify() const { 390 if (!isObjCProperty()) 391 return false; 392 393 // Don't worry about the rest of the strings for now. 394 return DbgNode->getNumOperands() == 8; 395} 396 397/// Check if a field at position Elt of a MDNode is a MDNode. 398/// We currently allow an empty string and an integer. 399/// But we don't allow a non-empty string in a MDNode field. 400static bool fieldIsMDNode(const MDNode *DbgNode, unsigned Elt) { 401 // FIXME: This function should return true, if the field is null or the field 402 // is indeed a MDNode: return !Fld || isa<MDNode>(Fld). 403 Value *Fld = getField(DbgNode, Elt); 404 if (Fld && isa<MDString>(Fld) && !cast<MDString>(Fld)->getString().empty()) 405 return false; 406 return true; 407} 408 409/// Check if a field at position Elt of a MDNode is a MDString. 410static bool fieldIsMDString(const MDNode *DbgNode, unsigned Elt) { 411 Value *Fld = getField(DbgNode, Elt); 412 return !Fld || isa<MDString>(Fld); 413} 414 415/// Check if a value can be a reference to a type. 416static bool isTypeRef(const Value *Val) { 417 return !Val || 418 (isa<MDString>(Val) && !cast<MDString>(Val)->getString().empty()) || 419 (isa<MDNode>(Val) && DIType(cast<MDNode>(Val)).isType()); 420} 421 422/// Check if a field at position Elt of a MDNode can be a reference to a type. 423static bool fieldIsTypeRef(const MDNode *DbgNode, unsigned Elt) { 424 Value *Fld = getField(DbgNode, Elt); 425 return isTypeRef(Fld); 426} 427 428/// Check if a value can be a ScopeRef. 429static bool isScopeRef(const Value *Val) { 430 return !Val || 431 (isa<MDString>(Val) && !cast<MDString>(Val)->getString().empty()) || 432 // Not checking for Val->isScope() here, because it would work 433 // only for lexical scopes and not all subclasses of DIScope. 434 isa<MDNode>(Val); 435} 436 437/// Check if a field at position Elt of a MDNode can be a ScopeRef. 438static bool fieldIsScopeRef(const MDNode *DbgNode, unsigned Elt) { 439 Value *Fld = getField(DbgNode, Elt); 440 return isScopeRef(Fld); 441} 442 443/// Verify - Verify that a type descriptor is well formed. 444bool DIType::Verify() const { 445 if (!isType()) 446 return false; 447 // Make sure Context @ field 2 is MDNode. 448 if (!fieldIsScopeRef(DbgNode, 2)) 449 return false; 450 451 // FIXME: Sink this into the various subclass verifies. 452 uint16_t Tag = getTag(); 453 if (!isBasicType() && Tag != dwarf::DW_TAG_const_type && 454 Tag != dwarf::DW_TAG_volatile_type && Tag != dwarf::DW_TAG_pointer_type && 455 Tag != dwarf::DW_TAG_ptr_to_member_type && 456 Tag != dwarf::DW_TAG_reference_type && 457 Tag != dwarf::DW_TAG_rvalue_reference_type && 458 Tag != dwarf::DW_TAG_restrict_type && Tag != dwarf::DW_TAG_array_type && 459 Tag != dwarf::DW_TAG_enumeration_type && 460 Tag != dwarf::DW_TAG_subroutine_type && 461 Tag != dwarf::DW_TAG_inheritance && Tag != dwarf::DW_TAG_friend && 462 getFilename().empty()) 463 return false; 464 // DIType is abstract, it should be a BasicType, a DerivedType or 465 // a CompositeType. 466 if (isBasicType()) 467 return DIBasicType(DbgNode).Verify(); 468 else if (isCompositeType()) 469 return DICompositeType(DbgNode).Verify(); 470 else if (isDerivedType()) 471 return DIDerivedType(DbgNode).Verify(); 472 else 473 return false; 474} 475 476/// Verify - Verify that a basic type descriptor is well formed. 477bool DIBasicType::Verify() const { 478 return isBasicType() && DbgNode->getNumOperands() == 10; 479} 480 481/// Verify - Verify that a derived type descriptor is well formed. 482bool DIDerivedType::Verify() const { 483 // Make sure DerivedFrom @ field 9 is TypeRef. 484 if (!fieldIsTypeRef(DbgNode, 9)) 485 return false; 486 if (getTag() == dwarf::DW_TAG_ptr_to_member_type) 487 // Make sure ClassType @ field 10 is a TypeRef. 488 if (!fieldIsTypeRef(DbgNode, 10)) 489 return false; 490 491 return isDerivedType() && DbgNode->getNumOperands() >= 10 && 492 DbgNode->getNumOperands() <= 14; 493} 494 495/// Verify - Verify that a composite type descriptor is well formed. 496bool DICompositeType::Verify() const { 497 if (!isCompositeType()) 498 return false; 499 500 // Make sure DerivedFrom @ field 9 and ContainingType @ field 12 are TypeRef. 501 if (!fieldIsTypeRef(DbgNode, 9)) 502 return false; 503 if (!fieldIsTypeRef(DbgNode, 12)) 504 return false; 505 506 // Make sure the type identifier at field 14 is MDString, it can be null. 507 if (!fieldIsMDString(DbgNode, 14)) 508 return false; 509 510 // A subroutine type can't be both & and &&. 511 if (isLValueReference() && isRValueReference()) 512 return false; 513 514 return DbgNode->getNumOperands() == 15; 515} 516 517/// Verify - Verify that a subprogram descriptor is well formed. 518bool DISubprogram::Verify() const { 519 if (!isSubprogram()) 520 return false; 521 522 // Make sure context @ field 2 is a ScopeRef and type @ field 7 is a MDNode. 523 if (!fieldIsScopeRef(DbgNode, 2)) 524 return false; 525 if (!fieldIsMDNode(DbgNode, 7)) 526 return false; 527 // Containing type @ field 12. 528 if (!fieldIsTypeRef(DbgNode, 12)) 529 return false; 530 531 // A subprogram can't be both & and &&. 532 if (isLValueReference() && isRValueReference()) 533 return false; 534 535 return DbgNode->getNumOperands() == 20; 536} 537 538/// Verify - Verify that a global variable descriptor is well formed. 539bool DIGlobalVariable::Verify() const { 540 if (!isGlobalVariable()) 541 return false; 542 543 if (getDisplayName().empty()) 544 return false; 545 // Make sure context @ field 2 is an MDNode. 546 if (!fieldIsMDNode(DbgNode, 2)) 547 return false; 548 // Make sure that type @ field 8 is a DITypeRef. 549 if (!fieldIsTypeRef(DbgNode, 8)) 550 return false; 551 // Make sure StaticDataMemberDeclaration @ field 12 is MDNode. 552 if (!fieldIsMDNode(DbgNode, 12)) 553 return false; 554 555 return DbgNode->getNumOperands() == 13; 556} 557 558/// Verify - Verify that a variable descriptor is well formed. 559bool DIVariable::Verify() const { 560 if (!isVariable()) 561 return false; 562 563 // Make sure context @ field 1 is an MDNode. 564 if (!fieldIsMDNode(DbgNode, 1)) 565 return false; 566 // Make sure that type @ field 5 is a DITypeRef. 567 if (!fieldIsTypeRef(DbgNode, 5)) 568 return false; 569 return DbgNode->getNumOperands() >= 8; 570} 571 572/// Verify - Verify that a location descriptor is well formed. 573bool DILocation::Verify() const { 574 if (!DbgNode) 575 return false; 576 577 return DbgNode->getNumOperands() == 4; 578} 579 580/// Verify - Verify that a namespace descriptor is well formed. 581bool DINameSpace::Verify() const { 582 if (!isNameSpace()) 583 return false; 584 return DbgNode->getNumOperands() == 5; 585} 586 587/// \brief Retrieve the MDNode for the directory/file pair. 588MDNode *DIFile::getFileNode() const { return getNodeField(DbgNode, 1); } 589 590/// \brief Verify that the file descriptor is well formed. 591bool DIFile::Verify() const { 592 return isFile() && DbgNode->getNumOperands() == 2; 593} 594 595/// \brief Verify that the enumerator descriptor is well formed. 596bool DIEnumerator::Verify() const { 597 return isEnumerator() && DbgNode->getNumOperands() == 3; 598} 599 600/// \brief Verify that the subrange descriptor is well formed. 601bool DISubrange::Verify() const { 602 return isSubrange() && DbgNode->getNumOperands() == 3; 603} 604 605/// \brief Verify that the lexical block descriptor is well formed. 606bool DILexicalBlock::Verify() const { 607 return isLexicalBlock() && DbgNode->getNumOperands() == 7; 608} 609 610/// \brief Verify that the file-scoped lexical block descriptor is well formed. 611bool DILexicalBlockFile::Verify() const { 612 return isLexicalBlockFile() && DbgNode->getNumOperands() == 3; 613} 614 615/// \brief Verify that an unspecified parameter descriptor is well formed. 616bool DIUnspecifiedParameter::Verify() const { 617 return isUnspecifiedParameter() && DbgNode->getNumOperands() == 1; 618} 619 620/// \brief Verify that the template type parameter descriptor is well formed. 621bool DITemplateTypeParameter::Verify() const { 622 return isTemplateTypeParameter() && DbgNode->getNumOperands() == 7; 623} 624 625/// \brief Verify that the template value parameter descriptor is well formed. 626bool DITemplateValueParameter::Verify() const { 627 return isTemplateValueParameter() && DbgNode->getNumOperands() == 8; 628} 629 630/// \brief Verify that the imported module descriptor is well formed. 631bool DIImportedEntity::Verify() const { 632 return isImportedEntity() && 633 (DbgNode->getNumOperands() == 4 || DbgNode->getNumOperands() == 5); 634} 635 636/// getObjCProperty - Return property node, if this ivar is associated with one. 637MDNode *DIDerivedType::getObjCProperty() const { 638 return getNodeField(DbgNode, 10); 639} 640 641MDString *DICompositeType::getIdentifier() const { 642 return cast_or_null<MDString>(getField(DbgNode, 14)); 643} 644 645#ifndef NDEBUG 646static void VerifySubsetOf(const MDNode *LHS, const MDNode *RHS) { 647 for (unsigned i = 0; i != LHS->getNumOperands(); ++i) { 648 // Skip the 'empty' list (that's a single i32 0, rather than truly empty). 649 if (i == 0 && isa<ConstantInt>(LHS->getOperand(i))) 650 continue; 651 const MDNode *E = cast<MDNode>(LHS->getOperand(i)); 652 bool found = false; 653 for (unsigned j = 0; !found && j != RHS->getNumOperands(); ++j) 654 found = E == RHS->getOperand(j); 655 assert(found && "Losing a member during member list replacement"); 656 } 657} 658#endif 659 660/// \brief Set the array of member DITypes. 661void DICompositeType::setTypeArray(DIArray Elements, DIArray TParams) { 662 assert((!TParams || DbgNode->getNumOperands() == 15) && 663 "If you're setting the template parameters this should include a slot " 664 "for that!"); 665 TrackingVH<MDNode> N(*this); 666 if (Elements) { 667#ifndef NDEBUG 668 // Check that the new list of members contains all the old members as well. 669 if (const MDNode *El = cast_or_null<MDNode>(N->getOperand(10))) 670 VerifySubsetOf(El, Elements); 671#endif 672 N->replaceOperandWith(10, Elements); 673 } 674 if (TParams) 675 N->replaceOperandWith(13, TParams); 676 DbgNode = N; 677} 678 679/// Generate a reference to this DIType. Uses the type identifier instead 680/// of the actual MDNode if possible, to help type uniquing. 681DIScopeRef DIScope::getRef() const { 682 if (!isCompositeType()) 683 return DIScopeRef(*this); 684 DICompositeType DTy(DbgNode); 685 if (!DTy.getIdentifier()) 686 return DIScopeRef(*this); 687 return DIScopeRef(DTy.getIdentifier()); 688} 689 690/// \brief Set the containing type. 691void DICompositeType::setContainingType(DICompositeType ContainingType) { 692 TrackingVH<MDNode> N(*this); 693 N->replaceOperandWith(12, ContainingType.getRef()); 694 DbgNode = N; 695} 696 697/// isInlinedFnArgument - Return true if this variable provides debugging 698/// information for an inlined function arguments. 699bool DIVariable::isInlinedFnArgument(const Function *CurFn) { 700 assert(CurFn && "Invalid function"); 701 if (!getContext().isSubprogram()) 702 return false; 703 // This variable is not inlined function argument if its scope 704 // does not describe current function. 705 return !DISubprogram(getContext()).describes(CurFn); 706} 707 708/// describes - Return true if this subprogram provides debugging 709/// information for the function F. 710bool DISubprogram::describes(const Function *F) { 711 assert(F && "Invalid function"); 712 if (F == getFunction()) 713 return true; 714 StringRef Name = getLinkageName(); 715 if (Name.empty()) 716 Name = getName(); 717 if (F->getName() == Name) 718 return true; 719 return false; 720} 721 722unsigned DISubprogram::isOptimized() const { 723 assert(DbgNode && "Invalid subprogram descriptor!"); 724 if (DbgNode->getNumOperands() == 15) 725 return getUnsignedField(14); 726 return 0; 727} 728 729MDNode *DISubprogram::getVariablesNodes() const { 730 return getNodeField(DbgNode, 18); 731} 732 733DIArray DISubprogram::getVariables() const { 734 return DIArray(getNodeField(DbgNode, 18)); 735} 736 737Value *DITemplateValueParameter::getValue() const { 738 return getField(DbgNode, 4); 739} 740 741// If the current node has a parent scope then return that, 742// else return an empty scope. 743DIScopeRef DIScope::getContext() const { 744 745 if (isType()) 746 return DIType(DbgNode).getContext(); 747 748 if (isSubprogram()) 749 return DIScopeRef(DISubprogram(DbgNode).getContext()); 750 751 if (isLexicalBlock()) 752 return DIScopeRef(DILexicalBlock(DbgNode).getContext()); 753 754 if (isLexicalBlockFile()) 755 return DIScopeRef(DILexicalBlockFile(DbgNode).getContext()); 756 757 if (isNameSpace()) 758 return DIScopeRef(DINameSpace(DbgNode).getContext()); 759 760 assert((isFile() || isCompileUnit()) && "Unhandled type of scope."); 761 return DIScopeRef(nullptr); 762} 763 764// If the scope node has a name, return that, else return an empty string. 765StringRef DIScope::getName() const { 766 if (isType()) 767 return DIType(DbgNode).getName(); 768 if (isSubprogram()) 769 return DISubprogram(DbgNode).getName(); 770 if (isNameSpace()) 771 return DINameSpace(DbgNode).getName(); 772 assert((isLexicalBlock() || isLexicalBlockFile() || isFile() || 773 isCompileUnit()) && 774 "Unhandled type of scope."); 775 return StringRef(); 776} 777 778StringRef DIScope::getFilename() const { 779 if (!DbgNode) 780 return StringRef(); 781 return ::getStringField(getNodeField(DbgNode, 1), 0); 782} 783 784StringRef DIScope::getDirectory() const { 785 if (!DbgNode) 786 return StringRef(); 787 return ::getStringField(getNodeField(DbgNode, 1), 1); 788} 789 790DIArray DICompileUnit::getEnumTypes() const { 791 if (!DbgNode || DbgNode->getNumOperands() < 13) 792 return DIArray(); 793 794 return DIArray(getNodeField(DbgNode, 7)); 795} 796 797DIArray DICompileUnit::getRetainedTypes() const { 798 if (!DbgNode || DbgNode->getNumOperands() < 13) 799 return DIArray(); 800 801 return DIArray(getNodeField(DbgNode, 8)); 802} 803 804DIArray DICompileUnit::getSubprograms() const { 805 if (!DbgNode || DbgNode->getNumOperands() < 13) 806 return DIArray(); 807 808 return DIArray(getNodeField(DbgNode, 9)); 809} 810 811DIArray DICompileUnit::getGlobalVariables() const { 812 if (!DbgNode || DbgNode->getNumOperands() < 13) 813 return DIArray(); 814 815 return DIArray(getNodeField(DbgNode, 10)); 816} 817 818DIArray DICompileUnit::getImportedEntities() const { 819 if (!DbgNode || DbgNode->getNumOperands() < 13) 820 return DIArray(); 821 822 return DIArray(getNodeField(DbgNode, 11)); 823} 824 825/// copyWithNewScope - Return a copy of this location, replacing the 826/// current scope with the given one. 827DILocation DILocation::copyWithNewScope(LLVMContext &Ctx, 828 DILexicalBlock NewScope) { 829 SmallVector<Value *, 10> Elts; 830 assert(Verify()); 831 for (unsigned I = 0; I < DbgNode->getNumOperands(); ++I) { 832 if (I != 2) 833 Elts.push_back(DbgNode->getOperand(I)); 834 else 835 Elts.push_back(NewScope); 836 } 837 MDNode *NewDIL = MDNode::get(Ctx, Elts); 838 return DILocation(NewDIL); 839} 840 841/// computeNewDiscriminator - Generate a new discriminator value for this 842/// file and line location. 843unsigned DILocation::computeNewDiscriminator(LLVMContext &Ctx) { 844 std::pair<const char *, unsigned> Key(getFilename().data(), getLineNumber()); 845 return ++Ctx.pImpl->DiscriminatorTable[Key]; 846} 847 848/// fixupSubprogramName - Replace contains special characters used 849/// in a typical Objective-C names with '.' in a given string. 850static void fixupSubprogramName(DISubprogram Fn, SmallVectorImpl<char> &Out) { 851 StringRef FName = 852 Fn.getFunction() ? Fn.getFunction()->getName() : Fn.getName(); 853 FName = Function::getRealLinkageName(FName); 854 855 StringRef Prefix("llvm.dbg.lv."); 856 Out.reserve(FName.size() + Prefix.size()); 857 Out.append(Prefix.begin(), Prefix.end()); 858 859 bool isObjCLike = false; 860 for (size_t i = 0, e = FName.size(); i < e; ++i) { 861 char C = FName[i]; 862 if (C == '[') 863 isObjCLike = true; 864 865 if (isObjCLike && (C == '[' || C == ']' || C == ' ' || C == ':' || 866 C == '+' || C == '(' || C == ')')) 867 Out.push_back('.'); 868 else 869 Out.push_back(C); 870 } 871} 872 873/// getFnSpecificMDNode - Return a NameMDNode, if available, that is 874/// suitable to hold function specific information. 875NamedMDNode *llvm::getFnSpecificMDNode(const Module &M, DISubprogram Fn) { 876 SmallString<32> Name; 877 fixupSubprogramName(Fn, Name); 878 return M.getNamedMetadata(Name.str()); 879} 880 881/// getOrInsertFnSpecificMDNode - Return a NameMDNode that is suitable 882/// to hold function specific information. 883NamedMDNode *llvm::getOrInsertFnSpecificMDNode(Module &M, DISubprogram Fn) { 884 SmallString<32> Name; 885 fixupSubprogramName(Fn, Name); 886 return M.getOrInsertNamedMetadata(Name.str()); 887} 888 889/// createInlinedVariable - Create a new inlined variable based on current 890/// variable. 891/// @param DV Current Variable. 892/// @param InlinedScope Location at current variable is inlined. 893DIVariable llvm::createInlinedVariable(MDNode *DV, MDNode *InlinedScope, 894 LLVMContext &VMContext) { 895 SmallVector<Value *, 16> Elts; 896 // Insert inlined scope as 7th element. 897 for (unsigned i = 0, e = DV->getNumOperands(); i != e; ++i) 898 i == 7 ? Elts.push_back(InlinedScope) : Elts.push_back(DV->getOperand(i)); 899 return DIVariable(MDNode::get(VMContext, Elts)); 900} 901 902/// cleanseInlinedVariable - Remove inlined scope from the variable. 903DIVariable llvm::cleanseInlinedVariable(MDNode *DV, LLVMContext &VMContext) { 904 SmallVector<Value *, 16> Elts; 905 // Insert inlined scope as 7th element. 906 for (unsigned i = 0, e = DV->getNumOperands(); i != e; ++i) 907 i == 7 ? Elts.push_back(Constant::getNullValue(Type::getInt32Ty(VMContext))) 908 : Elts.push_back(DV->getOperand(i)); 909 return DIVariable(MDNode::get(VMContext, Elts)); 910} 911 912/// getDISubprogram - Find subprogram that is enclosing this scope. 913DISubprogram llvm::getDISubprogram(const MDNode *Scope) { 914 DIDescriptor D(Scope); 915 if (D.isSubprogram()) 916 return DISubprogram(Scope); 917 918 if (D.isLexicalBlockFile()) 919 return getDISubprogram(DILexicalBlockFile(Scope).getContext()); 920 921 if (D.isLexicalBlock()) 922 return getDISubprogram(DILexicalBlock(Scope).getContext()); 923 924 return DISubprogram(); 925} 926 927/// getDICompositeType - Find underlying composite type. 928DICompositeType llvm::getDICompositeType(DIType T) { 929 if (T.isCompositeType()) 930 return DICompositeType(T); 931 932 if (T.isDerivedType()) { 933 // This function is currently used by dragonegg and dragonegg does 934 // not generate identifier for types, so using an empty map to resolve 935 // DerivedFrom should be fine. 936 DITypeIdentifierMap EmptyMap; 937 return getDICompositeType( 938 DIDerivedType(T).getTypeDerivedFrom().resolve(EmptyMap)); 939 } 940 941 return DICompositeType(); 942} 943 944/// Update DITypeIdentifierMap by going through retained types of each CU. 945DITypeIdentifierMap 946llvm::generateDITypeIdentifierMap(const NamedMDNode *CU_Nodes) { 947 DITypeIdentifierMap Map; 948 for (unsigned CUi = 0, CUe = CU_Nodes->getNumOperands(); CUi != CUe; ++CUi) { 949 DICompileUnit CU(CU_Nodes->getOperand(CUi)); 950 DIArray Retain = CU.getRetainedTypes(); 951 for (unsigned Ti = 0, Te = Retain.getNumElements(); Ti != Te; ++Ti) { 952 if (!Retain.getElement(Ti).isCompositeType()) 953 continue; 954 DICompositeType Ty(Retain.getElement(Ti)); 955 if (MDString *TypeId = Ty.getIdentifier()) { 956 // Definition has priority over declaration. 957 // Try to insert (TypeId, Ty) to Map. 958 std::pair<DITypeIdentifierMap::iterator, bool> P = 959 Map.insert(std::make_pair(TypeId, Ty)); 960 // If TypeId already exists in Map and this is a definition, replace 961 // whatever we had (declaration or definition) with the definition. 962 if (!P.second && !Ty.isForwardDecl()) 963 P.first->second = Ty; 964 } 965 } 966 } 967 return Map; 968} 969 970//===----------------------------------------------------------------------===// 971// DebugInfoFinder implementations. 972//===----------------------------------------------------------------------===// 973 974void DebugInfoFinder::reset() { 975 CUs.clear(); 976 SPs.clear(); 977 GVs.clear(); 978 TYs.clear(); 979 Scopes.clear(); 980 NodesSeen.clear(); 981 TypeIdentifierMap.clear(); 982 TypeMapInitialized = false; 983} 984 985void DebugInfoFinder::InitializeTypeMap(const Module &M) { 986 if (!TypeMapInitialized) 987 if (NamedMDNode *CU_Nodes = M.getNamedMetadata("llvm.dbg.cu")) { 988 TypeIdentifierMap = generateDITypeIdentifierMap(CU_Nodes); 989 TypeMapInitialized = true; 990 } 991} 992 993/// processModule - Process entire module and collect debug info. 994void DebugInfoFinder::processModule(const Module &M) { 995 InitializeTypeMap(M); 996 if (NamedMDNode *CU_Nodes = M.getNamedMetadata("llvm.dbg.cu")) { 997 for (unsigned i = 0, e = CU_Nodes->getNumOperands(); i != e; ++i) { 998 DICompileUnit CU(CU_Nodes->getOperand(i)); 999 addCompileUnit(CU); 1000 DIArray GVs = CU.getGlobalVariables(); 1001 for (unsigned i = 0, e = GVs.getNumElements(); i != e; ++i) { 1002 DIGlobalVariable DIG(GVs.getElement(i)); 1003 if (addGlobalVariable(DIG)) { 1004 processScope(DIG.getContext()); 1005 processType(DIG.getType().resolve(TypeIdentifierMap)); 1006 } 1007 } 1008 DIArray SPs = CU.getSubprograms(); 1009 for (unsigned i = 0, e = SPs.getNumElements(); i != e; ++i) 1010 processSubprogram(DISubprogram(SPs.getElement(i))); 1011 DIArray EnumTypes = CU.getEnumTypes(); 1012 for (unsigned i = 0, e = EnumTypes.getNumElements(); i != e; ++i) 1013 processType(DIType(EnumTypes.getElement(i))); 1014 DIArray RetainedTypes = CU.getRetainedTypes(); 1015 for (unsigned i = 0, e = RetainedTypes.getNumElements(); i != e; ++i) 1016 processType(DIType(RetainedTypes.getElement(i))); 1017 DIArray Imports = CU.getImportedEntities(); 1018 for (unsigned i = 0, e = Imports.getNumElements(); i != e; ++i) { 1019 DIImportedEntity Import = DIImportedEntity(Imports.getElement(i)); 1020 DIDescriptor Entity = Import.getEntity().resolve(TypeIdentifierMap); 1021 if (Entity.isType()) 1022 processType(DIType(Entity)); 1023 else if (Entity.isSubprogram()) 1024 processSubprogram(DISubprogram(Entity)); 1025 else if (Entity.isNameSpace()) 1026 processScope(DINameSpace(Entity).getContext()); 1027 } 1028 } 1029 } 1030} 1031 1032/// processLocation - Process DILocation. 1033void DebugInfoFinder::processLocation(const Module &M, DILocation Loc) { 1034 if (!Loc) 1035 return; 1036 InitializeTypeMap(M); 1037 processScope(Loc.getScope()); 1038 processLocation(M, Loc.getOrigLocation()); 1039} 1040 1041/// processType - Process DIType. 1042void DebugInfoFinder::processType(DIType DT) { 1043 if (!addType(DT)) 1044 return; 1045 processScope(DT.getContext().resolve(TypeIdentifierMap)); 1046 if (DT.isCompositeType()) { 1047 DICompositeType DCT(DT); 1048 processType(DCT.getTypeDerivedFrom().resolve(TypeIdentifierMap)); 1049 DIArray DA = DCT.getTypeArray(); 1050 for (unsigned i = 0, e = DA.getNumElements(); i != e; ++i) { 1051 DIDescriptor D = DA.getElement(i); 1052 if (D.isType()) 1053 processType(DIType(D)); 1054 else if (D.isSubprogram()) 1055 processSubprogram(DISubprogram(D)); 1056 } 1057 } else if (DT.isDerivedType()) { 1058 DIDerivedType DDT(DT); 1059 processType(DDT.getTypeDerivedFrom().resolve(TypeIdentifierMap)); 1060 } 1061} 1062 1063void DebugInfoFinder::processScope(DIScope Scope) { 1064 if (Scope.isType()) { 1065 DIType Ty(Scope); 1066 processType(Ty); 1067 return; 1068 } 1069 if (Scope.isCompileUnit()) { 1070 addCompileUnit(DICompileUnit(Scope)); 1071 return; 1072 } 1073 if (Scope.isSubprogram()) { 1074 processSubprogram(DISubprogram(Scope)); 1075 return; 1076 } 1077 if (!addScope(Scope)) 1078 return; 1079 if (Scope.isLexicalBlock()) { 1080 DILexicalBlock LB(Scope); 1081 processScope(LB.getContext()); 1082 } else if (Scope.isLexicalBlockFile()) { 1083 DILexicalBlockFile LBF = DILexicalBlockFile(Scope); 1084 processScope(LBF.getScope()); 1085 } else if (Scope.isNameSpace()) { 1086 DINameSpace NS(Scope); 1087 processScope(NS.getContext()); 1088 } 1089} 1090 1091/// processSubprogram - Process DISubprogram. 1092void DebugInfoFinder::processSubprogram(DISubprogram SP) { 1093 if (!addSubprogram(SP)) 1094 return; 1095 processScope(SP.getContext().resolve(TypeIdentifierMap)); 1096 processType(SP.getType()); 1097 DIArray TParams = SP.getTemplateParams(); 1098 for (unsigned I = 0, E = TParams.getNumElements(); I != E; ++I) { 1099 DIDescriptor Element = TParams.getElement(I); 1100 if (Element.isTemplateTypeParameter()) { 1101 DITemplateTypeParameter TType(Element); 1102 processScope(TType.getContext().resolve(TypeIdentifierMap)); 1103 processType(TType.getType().resolve(TypeIdentifierMap)); 1104 } else if (Element.isTemplateValueParameter()) { 1105 DITemplateValueParameter TVal(Element); 1106 processScope(TVal.getContext().resolve(TypeIdentifierMap)); 1107 processType(TVal.getType().resolve(TypeIdentifierMap)); 1108 } 1109 } 1110} 1111 1112/// processDeclare - Process DbgDeclareInst. 1113void DebugInfoFinder::processDeclare(const Module &M, 1114 const DbgDeclareInst *DDI) { 1115 MDNode *N = dyn_cast<MDNode>(DDI->getVariable()); 1116 if (!N) 1117 return; 1118 InitializeTypeMap(M); 1119 1120 DIDescriptor DV(N); 1121 if (!DV.isVariable()) 1122 return; 1123 1124 if (!NodesSeen.insert(DV)) 1125 return; 1126 processScope(DIVariable(N).getContext()); 1127 processType(DIVariable(N).getType().resolve(TypeIdentifierMap)); 1128} 1129 1130void DebugInfoFinder::processValue(const Module &M, const DbgValueInst *DVI) { 1131 MDNode *N = dyn_cast<MDNode>(DVI->getVariable()); 1132 if (!N) 1133 return; 1134 InitializeTypeMap(M); 1135 1136 DIDescriptor DV(N); 1137 if (!DV.isVariable()) 1138 return; 1139 1140 if (!NodesSeen.insert(DV)) 1141 return; 1142 processScope(DIVariable(N).getContext()); 1143 processType(DIVariable(N).getType().resolve(TypeIdentifierMap)); 1144} 1145 1146/// addType - Add type into Tys. 1147bool DebugInfoFinder::addType(DIType DT) { 1148 if (!DT) 1149 return false; 1150 1151 if (!NodesSeen.insert(DT)) 1152 return false; 1153 1154 TYs.push_back(DT); 1155 return true; 1156} 1157 1158/// addCompileUnit - Add compile unit into CUs. 1159bool DebugInfoFinder::addCompileUnit(DICompileUnit CU) { 1160 if (!CU) 1161 return false; 1162 if (!NodesSeen.insert(CU)) 1163 return false; 1164 1165 CUs.push_back(CU); 1166 return true; 1167} 1168 1169/// addGlobalVariable - Add global variable into GVs. 1170bool DebugInfoFinder::addGlobalVariable(DIGlobalVariable DIG) { 1171 if (!DIG) 1172 return false; 1173 1174 if (!NodesSeen.insert(DIG)) 1175 return false; 1176 1177 GVs.push_back(DIG); 1178 return true; 1179} 1180 1181// addSubprogram - Add subprgoram into SPs. 1182bool DebugInfoFinder::addSubprogram(DISubprogram SP) { 1183 if (!SP) 1184 return false; 1185 1186 if (!NodesSeen.insert(SP)) 1187 return false; 1188 1189 SPs.push_back(SP); 1190 return true; 1191} 1192 1193bool DebugInfoFinder::addScope(DIScope Scope) { 1194 if (!Scope) 1195 return false; 1196 // FIXME: Ocaml binding generates a scope with no content, we treat it 1197 // as null for now. 1198 if (Scope->getNumOperands() == 0) 1199 return false; 1200 if (!NodesSeen.insert(Scope)) 1201 return false; 1202 Scopes.push_back(Scope); 1203 return true; 1204} 1205 1206//===----------------------------------------------------------------------===// 1207// DIDescriptor: dump routines for all descriptors. 1208//===----------------------------------------------------------------------===// 1209 1210/// dump - Print descriptor to dbgs() with a newline. 1211void DIDescriptor::dump() const { 1212 print(dbgs()); 1213 dbgs() << '\n'; 1214} 1215 1216/// print - Print descriptor. 1217void DIDescriptor::print(raw_ostream &OS) const { 1218 if (!DbgNode) 1219 return; 1220 1221 if (const char *Tag = dwarf::TagString(getTag())) 1222 OS << "[ " << Tag << " ]"; 1223 1224 if (this->isSubrange()) { 1225 DISubrange(DbgNode).printInternal(OS); 1226 } else if (this->isCompileUnit()) { 1227 DICompileUnit(DbgNode).printInternal(OS); 1228 } else if (this->isFile()) { 1229 DIFile(DbgNode).printInternal(OS); 1230 } else if (this->isEnumerator()) { 1231 DIEnumerator(DbgNode).printInternal(OS); 1232 } else if (this->isBasicType()) { 1233 DIType(DbgNode).printInternal(OS); 1234 } else if (this->isDerivedType()) { 1235 DIDerivedType(DbgNode).printInternal(OS); 1236 } else if (this->isCompositeType()) { 1237 DICompositeType(DbgNode).printInternal(OS); 1238 } else if (this->isSubprogram()) { 1239 DISubprogram(DbgNode).printInternal(OS); 1240 } else if (this->isGlobalVariable()) { 1241 DIGlobalVariable(DbgNode).printInternal(OS); 1242 } else if (this->isVariable()) { 1243 DIVariable(DbgNode).printInternal(OS); 1244 } else if (this->isObjCProperty()) { 1245 DIObjCProperty(DbgNode).printInternal(OS); 1246 } else if (this->isNameSpace()) { 1247 DINameSpace(DbgNode).printInternal(OS); 1248 } else if (this->isScope()) { 1249 DIScope(DbgNode).printInternal(OS); 1250 } 1251} 1252 1253void DISubrange::printInternal(raw_ostream &OS) const { 1254 int64_t Count = getCount(); 1255 if (Count != -1) 1256 OS << " [" << getLo() << ", " << Count - 1 << ']'; 1257 else 1258 OS << " [unbounded]"; 1259} 1260 1261void DIScope::printInternal(raw_ostream &OS) const { 1262 OS << " [" << getDirectory() << "/" << getFilename() << ']'; 1263} 1264 1265void DICompileUnit::printInternal(raw_ostream &OS) const { 1266 DIScope::printInternal(OS); 1267 OS << " ["; 1268 unsigned Lang = getLanguage(); 1269 if (const char *LangStr = dwarf::LanguageString(Lang)) 1270 OS << LangStr; 1271 else 1272 (OS << "lang 0x").write_hex(Lang); 1273 OS << ']'; 1274} 1275 1276void DIEnumerator::printInternal(raw_ostream &OS) const { 1277 OS << " [" << getName() << " :: " << getEnumValue() << ']'; 1278} 1279 1280void DIType::printInternal(raw_ostream &OS) const { 1281 if (!DbgNode) 1282 return; 1283 1284 StringRef Res = getName(); 1285 if (!Res.empty()) 1286 OS << " [" << Res << "]"; 1287 1288 // TODO: Print context? 1289 1290 OS << " [line " << getLineNumber() << ", size " << getSizeInBits() 1291 << ", align " << getAlignInBits() << ", offset " << getOffsetInBits(); 1292 if (isBasicType()) 1293 if (const char *Enc = 1294 dwarf::AttributeEncodingString(DIBasicType(DbgNode).getEncoding())) 1295 OS << ", enc " << Enc; 1296 OS << "]"; 1297 1298 if (isPrivate()) 1299 OS << " [private]"; 1300 else if (isProtected()) 1301 OS << " [protected]"; 1302 1303 if (isArtificial()) 1304 OS << " [artificial]"; 1305 1306 if (isForwardDecl()) 1307 OS << " [decl]"; 1308 else if (getTag() == dwarf::DW_TAG_structure_type || 1309 getTag() == dwarf::DW_TAG_union_type || 1310 getTag() == dwarf::DW_TAG_enumeration_type || 1311 getTag() == dwarf::DW_TAG_class_type) 1312 OS << " [def]"; 1313 if (isVector()) 1314 OS << " [vector]"; 1315 if (isStaticMember()) 1316 OS << " [static]"; 1317 1318 if (isLValueReference()) 1319 OS << " [reference]"; 1320 1321 if (isRValueReference()) 1322 OS << " [rvalue reference]"; 1323} 1324 1325void DIDerivedType::printInternal(raw_ostream &OS) const { 1326 DIType::printInternal(OS); 1327 OS << " [from " << getTypeDerivedFrom().getName() << ']'; 1328} 1329 1330void DICompositeType::printInternal(raw_ostream &OS) const { 1331 DIType::printInternal(OS); 1332 DIArray A = getTypeArray(); 1333 OS << " [" << A.getNumElements() << " elements]"; 1334} 1335 1336void DINameSpace::printInternal(raw_ostream &OS) const { 1337 StringRef Name = getName(); 1338 if (!Name.empty()) 1339 OS << " [" << Name << ']'; 1340 1341 OS << " [line " << getLineNumber() << ']'; 1342} 1343 1344void DISubprogram::printInternal(raw_ostream &OS) const { 1345 // TODO : Print context 1346 OS << " [line " << getLineNumber() << ']'; 1347 1348 if (isLocalToUnit()) 1349 OS << " [local]"; 1350 1351 if (isDefinition()) 1352 OS << " [def]"; 1353 1354 if (getScopeLineNumber() != getLineNumber()) 1355 OS << " [scope " << getScopeLineNumber() << "]"; 1356 1357 if (isPrivate()) 1358 OS << " [private]"; 1359 else if (isProtected()) 1360 OS << " [protected]"; 1361 1362 if (isLValueReference()) 1363 OS << " [reference]"; 1364 1365 if (isRValueReference()) 1366 OS << " [rvalue reference]"; 1367 1368 StringRef Res = getName(); 1369 if (!Res.empty()) 1370 OS << " [" << Res << ']'; 1371} 1372 1373void DIGlobalVariable::printInternal(raw_ostream &OS) const { 1374 StringRef Res = getName(); 1375 if (!Res.empty()) 1376 OS << " [" << Res << ']'; 1377 1378 OS << " [line " << getLineNumber() << ']'; 1379 1380 // TODO : Print context 1381 1382 if (isLocalToUnit()) 1383 OS << " [local]"; 1384 1385 if (isDefinition()) 1386 OS << " [def]"; 1387} 1388 1389void DIVariable::printInternal(raw_ostream &OS) const { 1390 StringRef Res = getName(); 1391 if (!Res.empty()) 1392 OS << " [" << Res << ']'; 1393 1394 OS << " [line " << getLineNumber() << ']'; 1395} 1396 1397void DIObjCProperty::printInternal(raw_ostream &OS) const { 1398 StringRef Name = getObjCPropertyName(); 1399 if (!Name.empty()) 1400 OS << " [" << Name << ']'; 1401 1402 OS << " [line " << getLineNumber() << ", properties " << getUnsignedField(6) 1403 << ']'; 1404} 1405 1406static void printDebugLoc(DebugLoc DL, raw_ostream &CommentOS, 1407 const LLVMContext &Ctx) { 1408 if (!DL.isUnknown()) { // Print source line info. 1409 DIScope Scope(DL.getScope(Ctx)); 1410 assert(Scope.isScope() && "Scope of a DebugLoc should be a DIScope."); 1411 // Omit the directory, because it's likely to be long and uninteresting. 1412 CommentOS << Scope.getFilename(); 1413 CommentOS << ':' << DL.getLine(); 1414 if (DL.getCol() != 0) 1415 CommentOS << ':' << DL.getCol(); 1416 DebugLoc InlinedAtDL = DebugLoc::getFromDILocation(DL.getInlinedAt(Ctx)); 1417 if (!InlinedAtDL.isUnknown()) { 1418 CommentOS << " @[ "; 1419 printDebugLoc(InlinedAtDL, CommentOS, Ctx); 1420 CommentOS << " ]"; 1421 } 1422 } 1423} 1424 1425void DIVariable::printExtendedName(raw_ostream &OS) const { 1426 const LLVMContext &Ctx = DbgNode->getContext(); 1427 StringRef Res = getName(); 1428 if (!Res.empty()) 1429 OS << Res << "," << getLineNumber(); 1430 if (MDNode *InlinedAt = getInlinedAt()) { 1431 DebugLoc InlinedAtDL = DebugLoc::getFromDILocation(InlinedAt); 1432 if (!InlinedAtDL.isUnknown()) { 1433 OS << " @["; 1434 printDebugLoc(InlinedAtDL, OS, Ctx); 1435 OS << "]"; 1436 } 1437 } 1438} 1439 1440/// Specialize constructor to make sure it has the correct type. 1441template <> DIRef<DIScope>::DIRef(const Value *V) : Val(V) { 1442 assert(isScopeRef(V) && "DIScopeRef should be a MDString or MDNode"); 1443} 1444template <> DIRef<DIType>::DIRef(const Value *V) : Val(V) { 1445 assert(isTypeRef(V) && "DITypeRef should be a MDString or MDNode"); 1446} 1447 1448/// Specialize getFieldAs to handle fields that are references to DIScopes. 1449template <> 1450DIScopeRef DIDescriptor::getFieldAs<DIScopeRef>(unsigned Elt) const { 1451 return DIScopeRef(getField(DbgNode, Elt)); 1452} 1453/// Specialize getFieldAs to handle fields that are references to DITypes. 1454template <> DITypeRef DIDescriptor::getFieldAs<DITypeRef>(unsigned Elt) const { 1455 return DITypeRef(getField(DbgNode, Elt)); 1456} 1457 1458/// Strip debug info in the module if it exists. 1459/// To do this, we remove all calls to the debugger intrinsics and any named 1460/// metadata for debugging. We also remove debug locations for instructions. 1461/// Return true if module is modified. 1462bool llvm::StripDebugInfo(Module &M) { 1463 1464 bool Changed = false; 1465 1466 // Remove all of the calls to the debugger intrinsics, and remove them from 1467 // the module. 1468 if (Function *Declare = M.getFunction("llvm.dbg.declare")) { 1469 while (!Declare->use_empty()) { 1470 CallInst *CI = cast<CallInst>(Declare->user_back()); 1471 CI->eraseFromParent(); 1472 } 1473 Declare->eraseFromParent(); 1474 Changed = true; 1475 } 1476 1477 if (Function *DbgVal = M.getFunction("llvm.dbg.value")) { 1478 while (!DbgVal->use_empty()) { 1479 CallInst *CI = cast<CallInst>(DbgVal->user_back()); 1480 CI->eraseFromParent(); 1481 } 1482 DbgVal->eraseFromParent(); 1483 Changed = true; 1484 } 1485 1486 for (Module::named_metadata_iterator NMI = M.named_metadata_begin(), 1487 NME = M.named_metadata_end(); NMI != NME;) { 1488 NamedMDNode *NMD = NMI; 1489 ++NMI; 1490 if (NMD->getName().startswith("llvm.dbg.")) { 1491 NMD->eraseFromParent(); 1492 Changed = true; 1493 } 1494 } 1495 1496 for (Module::iterator MI = M.begin(), ME = M.end(); MI != ME; ++MI) 1497 for (Function::iterator FI = MI->begin(), FE = MI->end(); FI != FE; 1498 ++FI) 1499 for (BasicBlock::iterator BI = FI->begin(), BE = FI->end(); BI != BE; 1500 ++BI) { 1501 if (!BI->getDebugLoc().isUnknown()) { 1502 Changed = true; 1503 BI->setDebugLoc(DebugLoc()); 1504 } 1505 } 1506 1507 return Changed; 1508} 1509 1510/// Return Debug Info Metadata Version by checking module flags. 1511unsigned llvm::getDebugMetadataVersionFromModule(const Module &M) { 1512 Value *Val = M.getModuleFlag("Debug Info Version"); 1513 if (!Val) 1514 return 0; 1515 return cast<ConstantInt>(Val)->getZExtValue(); 1516} 1517