CGDebugInfo.cpp revision 207632
1//===--- CGDebugInfo.cpp - Emit Debug Information for a Module ------------===// 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 coordinates the debug information generation while generating code. 11// 12//===----------------------------------------------------------------------===// 13 14#include "CGDebugInfo.h" 15#include "CodeGenFunction.h" 16#include "CodeGenModule.h" 17#include "clang/AST/ASTContext.h" 18#include "clang/AST/DeclObjC.h" 19#include "clang/AST/Expr.h" 20#include "clang/AST/RecordLayout.h" 21#include "clang/Basic/SourceManager.h" 22#include "clang/Basic/FileManager.h" 23#include "clang/Basic/Version.h" 24#include "clang/CodeGen/CodeGenOptions.h" 25#include "llvm/Constants.h" 26#include "llvm/DerivedTypes.h" 27#include "llvm/Instructions.h" 28#include "llvm/Intrinsics.h" 29#include "llvm/Module.h" 30#include "llvm/ADT/StringExtras.h" 31#include "llvm/ADT/SmallVector.h" 32#include "llvm/Support/Dwarf.h" 33#include "llvm/System/Path.h" 34#include "llvm/Target/TargetMachine.h" 35using namespace clang; 36using namespace clang::CodeGen; 37 38CGDebugInfo::CGDebugInfo(CodeGenModule &CGM) 39 : CGM(CGM), DebugFactory(CGM.getModule()), 40 FwdDeclCount(0), BlockLiteralGenericSet(false) { 41 CreateCompileUnit(); 42} 43 44CGDebugInfo::~CGDebugInfo() { 45 assert(RegionStack.empty() && "Region stack mismatch, stack not empty!"); 46} 47 48void CGDebugInfo::setLocation(SourceLocation Loc) { 49 if (Loc.isValid()) 50 CurLoc = CGM.getContext().getSourceManager().getInstantiationLoc(Loc); 51} 52 53/// getContextDescriptor - Get context info for the decl. 54llvm::DIDescriptor CGDebugInfo::getContextDescriptor(const Decl *Context, 55 llvm::DIDescriptor &CompileUnit) { 56 if (!Context) 57 return CompileUnit; 58 59 llvm::DenseMap<const Decl *, llvm::WeakVH>::iterator 60 I = RegionMap.find(Context); 61 if (I != RegionMap.end()) 62 return llvm::DIDescriptor(dyn_cast_or_null<llvm::MDNode>(I->second)); 63 64 // Check namespace. 65 if (const NamespaceDecl *NSDecl = dyn_cast<NamespaceDecl>(Context)) 66 return llvm::DIDescriptor(getOrCreateNameSpace(NSDecl, CompileUnit)); 67 68 return CompileUnit; 69} 70 71/// getFunctionName - Get function name for the given FunctionDecl. If the 72/// name is constructred on demand (e.g. C++ destructor) then the name 73/// is stored on the side. 74llvm::StringRef CGDebugInfo::getFunctionName(const FunctionDecl *FD) { 75 assert (FD && "Invalid FunctionDecl!"); 76 IdentifierInfo *FII = FD->getIdentifier(); 77 if (FII) 78 return FII->getName(); 79 80 // Otherwise construct human readable name for debug info. 81 std::string NS = FD->getNameAsString(); 82 83 // Copy this name on the side and use its reference. 84 char *StrPtr = DebugInfoNames.Allocate<char>(NS.length()); 85 memcpy(StrPtr, NS.data(), NS.length()); 86 return llvm::StringRef(StrPtr, NS.length()); 87} 88 89/// getOrCreateFile - Get the file debug info descriptor for the input location. 90llvm::DIFile CGDebugInfo::getOrCreateFile(SourceLocation Loc) { 91 if (!Loc.isValid()) 92 // If Location is not valid then use main input file. 93 return DebugFactory.CreateFile(TheCU.getFilename(), TheCU.getDirectory(), 94 TheCU); 95 SourceManager &SM = CGM.getContext().getSourceManager(); 96 PresumedLoc PLoc = SM.getPresumedLoc(Loc); 97 98 // Cache the results. 99 const char *fname = PLoc.getFilename(); 100 llvm::DenseMap<const char *, llvm::WeakVH>::iterator it = 101 DIFileCache.find(fname); 102 103 if (it != DIFileCache.end()) { 104 // Verify that the information still exists. 105 if (&*it->second) 106 return llvm::DIFile(cast<llvm::MDNode>(it->second)); 107 } 108 109 // FIXME: We shouldn't even need to call 'makeAbsolute()' in the cases 110 // where we can consult the FileEntry. 111 llvm::sys::Path AbsFileName(PLoc.getFilename()); 112 AbsFileName.makeAbsolute(); 113 114 llvm::DIFile F = DebugFactory.CreateFile(AbsFileName.getLast(), 115 AbsFileName.getDirname(), TheCU); 116 117 DIFileCache[fname] = F.getNode(); 118 return F; 119 120} 121/// CreateCompileUnit - Create new compile unit. 122void CGDebugInfo::CreateCompileUnit() { 123 124 // Get absolute path name. 125 SourceManager &SM = CGM.getContext().getSourceManager(); 126 std::string MainFileName = CGM.getCodeGenOpts().MainFileName; 127 if (MainFileName.empty()) 128 MainFileName = "<unknown>"; 129 130 llvm::sys::Path AbsFileName(MainFileName); 131 AbsFileName.makeAbsolute(); 132 133 // The main file name provided via the "-main-file-name" option contains just 134 // the file name itself with no path information. This file name may have had 135 // a relative path, so we look into the actual file entry for the main 136 // file to determine the real absolute path for the file. 137 std::string MainFileDir; 138 if (const FileEntry *MainFile = SM.getFileEntryForID(SM.getMainFileID())) 139 MainFileDir = MainFile->getDir()->getName(); 140 else 141 MainFileDir = AbsFileName.getDirname(); 142 143 unsigned LangTag; 144 const LangOptions &LO = CGM.getLangOptions(); 145 if (LO.CPlusPlus) { 146 if (LO.ObjC1) 147 LangTag = llvm::dwarf::DW_LANG_ObjC_plus_plus; 148 else 149 LangTag = llvm::dwarf::DW_LANG_C_plus_plus; 150 } else if (LO.ObjC1) { 151 LangTag = llvm::dwarf::DW_LANG_ObjC; 152 } else if (LO.C99) { 153 LangTag = llvm::dwarf::DW_LANG_C99; 154 } else { 155 LangTag = llvm::dwarf::DW_LANG_C89; 156 } 157 158 const char *Producer = 159#ifdef CLANG_VENDOR 160 CLANG_VENDOR 161#endif 162 "clang " CLANG_VERSION_STRING; 163 164 // Figure out which version of the ObjC runtime we have. 165 unsigned RuntimeVers = 0; 166 if (LO.ObjC1) 167 RuntimeVers = LO.ObjCNonFragileABI ? 2 : 1; 168 169 // Create new compile unit. 170 TheCU = DebugFactory.CreateCompileUnit( 171 LangTag, AbsFileName.getLast(), MainFileDir, Producer, true, 172 LO.Optimize, CGM.getCodeGenOpts().DwarfDebugFlags, RuntimeVers); 173} 174 175/// CreateType - Get the Basic type from the cache or create a new 176/// one if necessary. 177llvm::DIType CGDebugInfo::CreateType(const BuiltinType *BT, 178 llvm::DIFile Unit) { 179 unsigned Encoding = 0; 180 switch (BT->getKind()) { 181 default: 182 case BuiltinType::Void: 183 return llvm::DIType(); 184 case BuiltinType::UChar: 185 case BuiltinType::Char_U: Encoding = llvm::dwarf::DW_ATE_unsigned_char; break; 186 case BuiltinType::Char_S: 187 case BuiltinType::SChar: Encoding = llvm::dwarf::DW_ATE_signed_char; break; 188 case BuiltinType::UShort: 189 case BuiltinType::UInt: 190 case BuiltinType::ULong: 191 case BuiltinType::ULongLong: Encoding = llvm::dwarf::DW_ATE_unsigned; break; 192 case BuiltinType::Short: 193 case BuiltinType::Int: 194 case BuiltinType::Long: 195 case BuiltinType::LongLong: Encoding = llvm::dwarf::DW_ATE_signed; break; 196 case BuiltinType::Bool: Encoding = llvm::dwarf::DW_ATE_boolean; break; 197 case BuiltinType::Float: 198 case BuiltinType::LongDouble: 199 case BuiltinType::Double: Encoding = llvm::dwarf::DW_ATE_float; break; 200 } 201 // Bit size, align and offset of the type. 202 uint64_t Size = CGM.getContext().getTypeSize(BT); 203 uint64_t Align = CGM.getContext().getTypeAlign(BT); 204 uint64_t Offset = 0; 205 206 llvm::DIType DbgTy = 207 DebugFactory.CreateBasicType(Unit, 208 BT->getName(CGM.getContext().getLangOptions()), 209 Unit, 0, Size, Align, 210 Offset, /*flags*/ 0, Encoding); 211 return DbgTy; 212} 213 214llvm::DIType CGDebugInfo::CreateType(const ComplexType *Ty, 215 llvm::DIFile Unit) { 216 // Bit size, align and offset of the type. 217 unsigned Encoding = llvm::dwarf::DW_ATE_complex_float; 218 if (Ty->isComplexIntegerType()) 219 Encoding = llvm::dwarf::DW_ATE_lo_user; 220 221 uint64_t Size = CGM.getContext().getTypeSize(Ty); 222 uint64_t Align = CGM.getContext().getTypeAlign(Ty); 223 uint64_t Offset = 0; 224 225 llvm::DIType DbgTy = 226 DebugFactory.CreateBasicType(Unit, "complex", 227 Unit, 0, Size, Align, 228 Offset, /*flags*/ 0, Encoding); 229 return DbgTy; 230} 231 232/// CreateCVRType - Get the qualified type from the cache or create 233/// a new one if necessary. 234llvm::DIType CGDebugInfo::CreateQualifiedType(QualType Ty, llvm::DIFile Unit) { 235 QualifierCollector Qc; 236 const Type *T = Qc.strip(Ty); 237 238 // Ignore these qualifiers for now. 239 Qc.removeObjCGCAttr(); 240 Qc.removeAddressSpace(); 241 242 // We will create one Derived type for one qualifier and recurse to handle any 243 // additional ones. 244 unsigned Tag; 245 if (Qc.hasConst()) { 246 Tag = llvm::dwarf::DW_TAG_const_type; 247 Qc.removeConst(); 248 } else if (Qc.hasVolatile()) { 249 Tag = llvm::dwarf::DW_TAG_volatile_type; 250 Qc.removeVolatile(); 251 } else if (Qc.hasRestrict()) { 252 Tag = llvm::dwarf::DW_TAG_restrict_type; 253 Qc.removeRestrict(); 254 } else { 255 assert(Qc.empty() && "Unknown type qualifier for debug info"); 256 return getOrCreateType(QualType(T, 0), Unit); 257 } 258 259 llvm::DIType FromTy = getOrCreateType(Qc.apply(T), Unit); 260 261 // No need to fill in the Name, Line, Size, Alignment, Offset in case of 262 // CVR derived types. 263 llvm::DIType DbgTy = 264 DebugFactory.CreateDerivedType(Tag, Unit, "", Unit, 265 0, 0, 0, 0, 0, FromTy); 266 return DbgTy; 267} 268 269llvm::DIType CGDebugInfo::CreateType(const ObjCObjectPointerType *Ty, 270 llvm::DIFile Unit) { 271 llvm::DIType DbgTy = 272 CreatePointerLikeType(llvm::dwarf::DW_TAG_pointer_type, Ty, 273 Ty->getPointeeType(), Unit); 274 return DbgTy; 275} 276 277llvm::DIType CGDebugInfo::CreateType(const PointerType *Ty, 278 llvm::DIFile Unit) { 279 return CreatePointerLikeType(llvm::dwarf::DW_TAG_pointer_type, Ty, 280 Ty->getPointeeType(), Unit); 281} 282 283llvm::DIType CGDebugInfo::CreatePointerLikeType(unsigned Tag, 284 const Type *Ty, 285 QualType PointeeTy, 286 llvm::DIFile Unit) { 287 llvm::DIType EltTy = getOrCreateType(PointeeTy, Unit); 288 289 // Bit size, align and offset of the type. 290 291 // Size is always the size of a pointer. We can't use getTypeSize here 292 // because that does not return the correct value for references. 293 uint64_t Size = 294 CGM.getContext().Target.getPointerWidth(PointeeTy.getAddressSpace()); 295 uint64_t Align = CGM.getContext().getTypeAlign(Ty); 296 297 return 298 DebugFactory.CreateDerivedType(Tag, Unit, "", Unit, 299 0, Size, Align, 0, 0, EltTy); 300 301} 302 303llvm::DIType CGDebugInfo::CreateType(const BlockPointerType *Ty, 304 llvm::DIFile Unit) { 305 if (BlockLiteralGenericSet) 306 return BlockLiteralGeneric; 307 308 unsigned Tag = llvm::dwarf::DW_TAG_structure_type; 309 310 llvm::SmallVector<llvm::DIDescriptor, 5> EltTys; 311 312 llvm::DIType FieldTy; 313 314 QualType FType; 315 uint64_t FieldSize, FieldOffset; 316 unsigned FieldAlign; 317 318 llvm::DIArray Elements; 319 llvm::DIType EltTy, DescTy; 320 321 FieldOffset = 0; 322 FType = CGM.getContext().UnsignedLongTy; 323 EltTys.push_back(CreateMemberType(Unit, FType, "reserved", &FieldOffset)); 324 EltTys.push_back(CreateMemberType(Unit, FType, "Size", &FieldOffset)); 325 326 Elements = DebugFactory.GetOrCreateArray(EltTys.data(), EltTys.size()); 327 EltTys.clear(); 328 329 unsigned Flags = llvm::DIType::FlagAppleBlock; 330 331 EltTy = DebugFactory.CreateCompositeType(Tag, Unit, "__block_descriptor", 332 Unit, 0, FieldOffset, 0, 0, Flags, 333 llvm::DIType(), Elements); 334 335 // Bit size, align and offset of the type. 336 uint64_t Size = CGM.getContext().getTypeSize(Ty); 337 uint64_t Align = CGM.getContext().getTypeAlign(Ty); 338 339 DescTy = DebugFactory.CreateDerivedType(llvm::dwarf::DW_TAG_pointer_type, 340 Unit, "", Unit, 341 0, Size, Align, 0, 0, EltTy); 342 343 FieldOffset = 0; 344 FType = CGM.getContext().getPointerType(CGM.getContext().VoidTy); 345 EltTys.push_back(CreateMemberType(Unit, FType, "__isa", &FieldOffset)); 346 FType = CGM.getContext().IntTy; 347 EltTys.push_back(CreateMemberType(Unit, FType, "__flags", &FieldOffset)); 348 EltTys.push_back(CreateMemberType(Unit, FType, "__reserved", &FieldOffset)); 349 FType = CGM.getContext().getPointerType(CGM.getContext().VoidTy); 350 EltTys.push_back(CreateMemberType(Unit, FType, "__FuncPtr", &FieldOffset)); 351 352 FType = CGM.getContext().getPointerType(CGM.getContext().VoidTy); 353 FieldTy = DescTy; 354 FieldSize = CGM.getContext().getTypeSize(Ty); 355 FieldAlign = CGM.getContext().getTypeAlign(Ty); 356 FieldTy = DebugFactory.CreateDerivedType(llvm::dwarf::DW_TAG_member, Unit, 357 "__descriptor", Unit, 358 0, FieldSize, FieldAlign, 359 FieldOffset, 0, FieldTy); 360 EltTys.push_back(FieldTy); 361 362 FieldOffset += FieldSize; 363 Elements = DebugFactory.GetOrCreateArray(EltTys.data(), EltTys.size()); 364 365 EltTy = DebugFactory.CreateCompositeType(Tag, Unit, "__block_literal_generic", 366 Unit, 0, FieldOffset, 0, 0, Flags, 367 llvm::DIType(), Elements); 368 369 BlockLiteralGenericSet = true; 370 BlockLiteralGeneric 371 = DebugFactory.CreateDerivedType(llvm::dwarf::DW_TAG_pointer_type, Unit, 372 "", Unit, 373 0, Size, Align, 0, 0, EltTy); 374 return BlockLiteralGeneric; 375} 376 377llvm::DIType CGDebugInfo::CreateType(const TypedefType *Ty, 378 llvm::DIFile Unit) { 379 // Typedefs are derived from some other type. If we have a typedef of a 380 // typedef, make sure to emit the whole chain. 381 llvm::DIType Src = getOrCreateType(Ty->getDecl()->getUnderlyingType(), Unit); 382 383 // We don't set size information, but do specify where the typedef was 384 // declared. 385 SourceManager &SM = CGM.getContext().getSourceManager(); 386 PresumedLoc PLoc = SM.getPresumedLoc(Ty->getDecl()->getLocation()); 387 unsigned Line = PLoc.isInvalid() ? 0 : PLoc.getLine(); 388 389 llvm::DIDescriptor TyContext 390 = getContextDescriptor(dyn_cast<Decl>(Ty->getDecl()->getDeclContext()), 391 Unit); 392 llvm::DIType DbgTy = 393 DebugFactory.CreateDerivedType(llvm::dwarf::DW_TAG_typedef, 394 TyContext, 395 Ty->getDecl()->getName(), Unit, 396 Line, 0, 0, 0, 0, Src); 397 return DbgTy; 398} 399 400llvm::DIType CGDebugInfo::CreateType(const FunctionType *Ty, 401 llvm::DIFile Unit) { 402 llvm::SmallVector<llvm::DIDescriptor, 16> EltTys; 403 404 // Add the result type at least. 405 EltTys.push_back(getOrCreateType(Ty->getResultType(), Unit)); 406 407 // Set up remainder of arguments if there is a prototype. 408 // FIXME: IF NOT, HOW IS THIS REPRESENTED? llvm-gcc doesn't represent '...'! 409 if (const FunctionProtoType *FTP = dyn_cast<FunctionProtoType>(Ty)) { 410 for (unsigned i = 0, e = FTP->getNumArgs(); i != e; ++i) 411 EltTys.push_back(getOrCreateType(FTP->getArgType(i), Unit)); 412 } else { 413 // FIXME: Handle () case in C. llvm-gcc doesn't do it either. 414 } 415 416 llvm::DIArray EltTypeArray = 417 DebugFactory.GetOrCreateArray(EltTys.data(), EltTys.size()); 418 419 llvm::DIType DbgTy = 420 DebugFactory.CreateCompositeType(llvm::dwarf::DW_TAG_subroutine_type, 421 Unit, "", Unit, 422 0, 0, 0, 0, 0, 423 llvm::DIType(), EltTypeArray); 424 return DbgTy; 425} 426 427/// CollectRecordFields - A helper function to collect debug info for 428/// record fields. This is used while creating debug info entry for a Record. 429void CGDebugInfo:: 430CollectRecordFields(const RecordDecl *RD, llvm::DIFile Unit, 431 llvm::SmallVectorImpl<llvm::DIDescriptor> &EltTys) { 432 unsigned FieldNo = 0; 433 SourceManager &SM = CGM.getContext().getSourceManager(); 434 const ASTRecordLayout &RL = CGM.getContext().getASTRecordLayout(RD); 435 for (RecordDecl::field_iterator I = RD->field_begin(), 436 E = RD->field_end(); 437 I != E; ++I, ++FieldNo) { 438 FieldDecl *Field = *I; 439 llvm::DIType FieldTy = getOrCreateType(Field->getType(), Unit); 440 441 llvm::StringRef FieldName = Field->getName(); 442 443 // Ignore unnamed fields. Do not ignore unnamed records. 444 if (FieldName.empty() && !isa<RecordType>(Field->getType())) 445 continue; 446 447 // Get the location for the field. 448 SourceLocation FieldDefLoc = Field->getLocation(); 449 PresumedLoc PLoc = SM.getPresumedLoc(FieldDefLoc); 450 llvm::DIFile FieldDefUnit; 451 unsigned FieldLine = 0; 452 453 if (!PLoc.isInvalid()) { 454 FieldDefUnit = getOrCreateFile(FieldDefLoc); 455 FieldLine = PLoc.getLine(); 456 } 457 458 QualType FType = Field->getType(); 459 uint64_t FieldSize = 0; 460 unsigned FieldAlign = 0; 461 if (!FType->isIncompleteArrayType()) { 462 463 // Bit size, align and offset of the type. 464 FieldSize = CGM.getContext().getTypeSize(FType); 465 Expr *BitWidth = Field->getBitWidth(); 466 if (BitWidth) 467 FieldSize = BitWidth->EvaluateAsInt(CGM.getContext()).getZExtValue(); 468 469 FieldAlign = CGM.getContext().getTypeAlign(FType); 470 } 471 472 uint64_t FieldOffset = RL.getFieldOffset(FieldNo); 473 474 unsigned Flags = 0; 475 AccessSpecifier Access = I->getAccess(); 476 if (Access == clang::AS_private) 477 Flags |= llvm::DIType::FlagPrivate; 478 else if (Access == clang::AS_protected) 479 Flags |= llvm::DIType::FlagProtected; 480 481 // Create a DW_TAG_member node to remember the offset of this field in the 482 // struct. FIXME: This is an absolutely insane way to capture this 483 // information. When we gut debug info, this should be fixed. 484 FieldTy = DebugFactory.CreateDerivedType(llvm::dwarf::DW_TAG_member, Unit, 485 FieldName, FieldDefUnit, 486 FieldLine, FieldSize, FieldAlign, 487 FieldOffset, Flags, FieldTy); 488 EltTys.push_back(FieldTy); 489 } 490} 491 492/// getOrCreateMethodType - CXXMethodDecl's type is a FunctionType. This 493/// function type is not updated to include implicit "this" pointer. Use this 494/// routine to get a method type which includes "this" pointer. 495llvm::DIType 496CGDebugInfo::getOrCreateMethodType(const CXXMethodDecl *Method, 497 llvm::DIFile Unit) { 498 llvm::DIType FnTy 499 = getOrCreateType(QualType(Method->getType()->getAs<FunctionProtoType>(), 500 0), 501 Unit); 502 503 // Static methods do not need "this" pointer argument. 504 if (Method->isStatic()) 505 return FnTy; 506 507 // Add "this" pointer. 508 509 llvm::DIArray Args = llvm::DICompositeType(FnTy.getNode()).getTypeArray(); 510 assert (Args.getNumElements() && "Invalid number of arguments!"); 511 512 llvm::SmallVector<llvm::DIDescriptor, 16> Elts; 513 514 // First element is always return type. For 'void' functions it is NULL. 515 Elts.push_back(Args.getElement(0)); 516 517 // "this" pointer is always first argument. 518 ASTContext &Context = CGM.getContext(); 519 QualType ThisPtr = 520 Context.getPointerType(Context.getTagDeclType(Method->getParent())); 521 llvm::DIType ThisPtrType = 522 DebugFactory.CreateArtificialType(getOrCreateType(ThisPtr, Unit)); 523 TypeCache[ThisPtr.getAsOpaquePtr()] = ThisPtrType.getNode(); 524 Elts.push_back(ThisPtrType); 525 526 // Copy rest of the arguments. 527 for (unsigned i = 1, e = Args.getNumElements(); i != e; ++i) 528 Elts.push_back(Args.getElement(i)); 529 530 llvm::DIArray EltTypeArray = 531 DebugFactory.GetOrCreateArray(Elts.data(), Elts.size()); 532 533 return 534 DebugFactory.CreateCompositeType(llvm::dwarf::DW_TAG_subroutine_type, 535 Unit, "", Unit, 536 0, 0, 0, 0, 0, 537 llvm::DIType(), EltTypeArray); 538} 539 540/// CreateCXXMemberFunction - A helper function to create a DISubprogram for 541/// a single member function GlobalDecl. 542llvm::DISubprogram 543CGDebugInfo::CreateCXXMemberFunction(const CXXMethodDecl *Method, 544 llvm::DIFile Unit, 545 llvm::DICompositeType &RecordTy) { 546 bool IsCtorOrDtor = 547 isa<CXXConstructorDecl>(Method) || isa<CXXDestructorDecl>(Method); 548 549 llvm::StringRef MethodName = getFunctionName(Method); 550 llvm::DIType MethodTy = getOrCreateMethodType(Method, Unit); 551 552 // Since a single ctor/dtor corresponds to multiple functions, it doesn't 553 // make sense to give a single ctor/dtor a linkage name. 554 MangleBuffer MethodLinkageName; 555 if (!IsCtorOrDtor) 556 CGM.getMangledName(MethodLinkageName, Method); 557 558 SourceManager &SM = CGM.getContext().getSourceManager(); 559 560 // Get the location for the method. 561 SourceLocation MethodDefLoc = Method->getLocation(); 562 PresumedLoc PLoc = SM.getPresumedLoc(MethodDefLoc); 563 llvm::DIFile MethodDefUnit; 564 unsigned MethodLine = 0; 565 566 if (!PLoc.isInvalid()) { 567 MethodDefUnit = getOrCreateFile(MethodDefLoc); 568 MethodLine = PLoc.getLine(); 569 } 570 571 // Collect virtual method info. 572 llvm::DIType ContainingType; 573 unsigned Virtuality = 0; 574 unsigned VIndex = 0; 575 576 if (Method->isVirtual()) { 577 if (Method->isPure()) 578 Virtuality = llvm::dwarf::DW_VIRTUALITY_pure_virtual; 579 else 580 Virtuality = llvm::dwarf::DW_VIRTUALITY_virtual; 581 582 // It doesn't make sense to give a virtual destructor a vtable index, 583 // since a single destructor has two entries in the vtable. 584 if (!isa<CXXDestructorDecl>(Method)) 585 VIndex = CGM.getVTables().getMethodVTableIndex(Method); 586 ContainingType = RecordTy; 587 } 588 589 llvm::DISubprogram SP = 590 DebugFactory.CreateSubprogram(RecordTy , MethodName, MethodName, 591 MethodLinkageName, 592 MethodDefUnit, MethodLine, 593 MethodTy, /*isLocalToUnit=*/false, 594 Method->isThisDeclarationADefinition(), 595 Virtuality, VIndex, ContainingType); 596 597 // Don't cache ctors or dtors since we have to emit multiple functions for 598 // a single ctor or dtor. 599 if (!IsCtorOrDtor && Method->isThisDeclarationADefinition()) 600 SPCache[Method] = llvm::WeakVH(SP.getNode()); 601 602 return SP; 603} 604 605/// CollectCXXMemberFunctions - A helper function to collect debug info for 606/// C++ member functions.This is used while creating debug info entry for 607/// a Record. 608void CGDebugInfo:: 609CollectCXXMemberFunctions(const CXXRecordDecl *RD, llvm::DIFile Unit, 610 llvm::SmallVectorImpl<llvm::DIDescriptor> &EltTys, 611 llvm::DICompositeType &RecordTy) { 612 for(CXXRecordDecl::method_iterator I = RD->method_begin(), 613 E = RD->method_end(); I != E; ++I) { 614 const CXXMethodDecl *Method = *I; 615 616 if (Method->isImplicit() && !Method->isUsed()) 617 continue; 618 619 EltTys.push_back(CreateCXXMemberFunction(Method, Unit, RecordTy)); 620 } 621} 622 623/// CollectCXXBases - A helper function to collect debug info for 624/// C++ base classes. This is used while creating debug info entry for 625/// a Record. 626void CGDebugInfo:: 627CollectCXXBases(const CXXRecordDecl *RD, llvm::DIFile Unit, 628 llvm::SmallVectorImpl<llvm::DIDescriptor> &EltTys, 629 llvm::DICompositeType &RecordTy) { 630 631 const ASTRecordLayout &RL = CGM.getContext().getASTRecordLayout(RD); 632 for (CXXRecordDecl::base_class_const_iterator BI = RD->bases_begin(), 633 BE = RD->bases_end(); BI != BE; ++BI) { 634 unsigned BFlags = 0; 635 uint64_t BaseOffset; 636 637 const CXXRecordDecl *Base = 638 cast<CXXRecordDecl>(BI->getType()->getAs<RecordType>()->getDecl()); 639 640 if (BI->isVirtual()) { 641 // virtual base offset offset is -ve. The code generator emits dwarf 642 // expression where it expects +ve number. 643 BaseOffset = 0 - CGM.getVTables().getVirtualBaseOffsetOffset(RD, Base); 644 BFlags = llvm::DIType::FlagVirtual; 645 } else 646 BaseOffset = RL.getBaseClassOffset(Base); 647 648 AccessSpecifier Access = BI->getAccessSpecifier(); 649 if (Access == clang::AS_private) 650 BFlags |= llvm::DIType::FlagPrivate; 651 else if (Access == clang::AS_protected) 652 BFlags |= llvm::DIType::FlagProtected; 653 654 llvm::DIType DTy = 655 DebugFactory.CreateDerivedType(llvm::dwarf::DW_TAG_inheritance, 656 RecordTy, llvm::StringRef(), 657 Unit, 0, 0, 0, 658 BaseOffset, BFlags, 659 getOrCreateType(BI->getType(), 660 Unit)); 661 EltTys.push_back(DTy); 662 } 663} 664 665/// getOrCreateVTablePtrType - Return debug info descriptor for vtable. 666llvm::DIType CGDebugInfo::getOrCreateVTablePtrType(llvm::DIFile Unit) { 667 if (VTablePtrType.isValid()) 668 return VTablePtrType; 669 670 ASTContext &Context = CGM.getContext(); 671 672 /* Function type */ 673 llvm::DIDescriptor STy = getOrCreateType(Context.IntTy, Unit); 674 llvm::DIArray SElements = DebugFactory.GetOrCreateArray(&STy, 1); 675 llvm::DIType SubTy = 676 DebugFactory.CreateCompositeType(llvm::dwarf::DW_TAG_subroutine_type, 677 Unit, "", Unit, 678 0, 0, 0, 0, 0, llvm::DIType(), SElements); 679 680 unsigned Size = Context.getTypeSize(Context.VoidPtrTy); 681 llvm::DIType vtbl_ptr_type 682 = DebugFactory.CreateDerivedType(llvm::dwarf::DW_TAG_pointer_type, 683 Unit, "__vtbl_ptr_type", Unit, 684 0, Size, 0, 0, 0, SubTy); 685 686 VTablePtrType = 687 DebugFactory.CreateDerivedType(llvm::dwarf::DW_TAG_pointer_type, 688 Unit, "", Unit, 689 0, Size, 0, 0, 0, vtbl_ptr_type); 690 return VTablePtrType; 691} 692 693/// getVTableName - Get vtable name for the given Class. 694llvm::StringRef CGDebugInfo::getVTableName(const CXXRecordDecl *RD) { 695 // Otherwise construct gdb compatible name name. 696 std::string Name = "_vptr$" + RD->getNameAsString(); 697 698 // Copy this name on the side and use its reference. 699 char *StrPtr = DebugInfoNames.Allocate<char>(Name.length()); 700 memcpy(StrPtr, Name.data(), Name.length()); 701 return llvm::StringRef(StrPtr, Name.length()); 702} 703 704 705/// CollectVTableInfo - If the C++ class has vtable info then insert appropriate 706/// debug info entry in EltTys vector. 707void CGDebugInfo:: 708CollectVTableInfo(const CXXRecordDecl *RD, llvm::DIFile Unit, 709 llvm::SmallVectorImpl<llvm::DIDescriptor> &EltTys) { 710 const ASTRecordLayout &RL = CGM.getContext().getASTRecordLayout(RD); 711 712 // If there is a primary base then it will hold vtable info. 713 if (RL.getPrimaryBase()) 714 return; 715 716 // If this class is not dynamic then there is not any vtable info to collect. 717 if (!RD->isDynamicClass()) 718 return; 719 720 unsigned Size = CGM.getContext().getTypeSize(CGM.getContext().VoidPtrTy); 721 llvm::DIType VPTR 722 = DebugFactory.CreateDerivedType(llvm::dwarf::DW_TAG_member, Unit, 723 getVTableName(RD), Unit, 724 0, Size, 0, 0, 0, 725 getOrCreateVTablePtrType(Unit)); 726 EltTys.push_back(VPTR); 727} 728 729/// CreateType - get structure or union type. 730llvm::DIType CGDebugInfo::CreateType(const RecordType *Ty, 731 llvm::DIFile Unit) { 732 RecordDecl *RD = Ty->getDecl(); 733 734 unsigned Tag; 735 if (RD->isStruct()) 736 Tag = llvm::dwarf::DW_TAG_structure_type; 737 else if (RD->isUnion()) 738 Tag = llvm::dwarf::DW_TAG_union_type; 739 else { 740 assert(RD->isClass() && "Unknown RecordType!"); 741 Tag = llvm::dwarf::DW_TAG_class_type; 742 } 743 744 SourceManager &SM = CGM.getContext().getSourceManager(); 745 746 // Get overall information about the record type for the debug info. 747 PresumedLoc PLoc = SM.getPresumedLoc(RD->getLocation()); 748 llvm::DIFile DefUnit; 749 unsigned Line = 0; 750 if (!PLoc.isInvalid()) { 751 DefUnit = getOrCreateFile(RD->getLocation()); 752 Line = PLoc.getLine(); 753 } 754 755 // Records and classes and unions can all be recursive. To handle them, we 756 // first generate a debug descriptor for the struct as a forward declaration. 757 // Then (if it is a definition) we go through and get debug info for all of 758 // its members. Finally, we create a descriptor for the complete type (which 759 // may refer to the forward decl if the struct is recursive) and replace all 760 // uses of the forward declaration with the final definition. 761 762 // A RD->getName() is not unique. However, the debug info descriptors 763 // are uniqued so use type name to ensure uniquness. 764 llvm::SmallString<128> FwdDeclName; 765 llvm::raw_svector_ostream(FwdDeclName) << "fwd.type." << FwdDeclCount++; 766 llvm::DIDescriptor FDContext = 767 getContextDescriptor(dyn_cast<Decl>(RD->getDeclContext()), Unit); 768 llvm::DICompositeType FwdDecl = 769 DebugFactory.CreateCompositeType(Tag, FDContext, FwdDeclName, 770 DefUnit, Line, 0, 0, 0, 0, 771 llvm::DIType(), llvm::DIArray()); 772 773 // If this is just a forward declaration, return it. 774 if (!RD->getDefinition()) 775 return FwdDecl; 776 777 llvm::TrackingVH<llvm::MDNode> FwdDeclNode = FwdDecl.getNode(); 778 // Otherwise, insert it into the TypeCache so that recursive uses will find 779 // it. 780 TypeCache[QualType(Ty, 0).getAsOpaquePtr()] = FwdDecl.getNode(); 781 // Push the struct on region stack. 782 RegionStack.push_back(FwdDecl.getNode()); 783 RegionMap[Ty->getDecl()] = llvm::WeakVH(FwdDecl.getNode()); 784 785 // Convert all the elements. 786 llvm::SmallVector<llvm::DIDescriptor, 16> EltTys; 787 788 const CXXRecordDecl *CXXDecl = dyn_cast<CXXRecordDecl>(RD); 789 if (CXXDecl) { 790 CollectCXXBases(CXXDecl, Unit, EltTys, FwdDecl); 791 CollectVTableInfo(CXXDecl, Unit, EltTys); 792 } 793 CollectRecordFields(RD, Unit, EltTys); 794 llvm::MDNode *ContainingType = NULL; 795 if (CXXDecl) { 796 CollectCXXMemberFunctions(CXXDecl, Unit, EltTys, FwdDecl); 797 798 // A class's primary base or the class itself contains the vtable. 799 const ASTRecordLayout &RL = CGM.getContext().getASTRecordLayout(RD); 800 if (const CXXRecordDecl *PBase = RL.getPrimaryBase()) 801 ContainingType = 802 getOrCreateType(QualType(PBase->getTypeForDecl(), 0), Unit).getNode(); 803 else if (CXXDecl->isDynamicClass()) 804 ContainingType = FwdDecl.getNode(); 805 } 806 807 llvm::DIArray Elements = 808 DebugFactory.GetOrCreateArray(EltTys.data(), EltTys.size()); 809 810 // Bit size, align and offset of the type. 811 uint64_t Size = CGM.getContext().getTypeSize(Ty); 812 uint64_t Align = CGM.getContext().getTypeAlign(Ty); 813 814 RegionStack.pop_back(); 815 llvm::DenseMap<const Decl *, llvm::WeakVH>::iterator RI = 816 RegionMap.find(Ty->getDecl()); 817 if (RI != RegionMap.end()) 818 RegionMap.erase(RI); 819 820 llvm::DIDescriptor RDContext = 821 getContextDescriptor(dyn_cast<Decl>(RD->getDeclContext()), Unit); 822 llvm::DICompositeType RealDecl = 823 DebugFactory.CreateCompositeType(Tag, RDContext, 824 RD->getName(), 825 DefUnit, Line, Size, Align, 0, 0, 826 llvm::DIType(), Elements, 827 0, ContainingType); 828 829 // Now that we have a real decl for the struct, replace anything using the 830 // old decl with the new one. This will recursively update the debug info. 831 llvm::DIDerivedType(FwdDeclNode).replaceAllUsesWith(RealDecl); 832 RegionMap[RD] = llvm::WeakVH(RealDecl.getNode()); 833 return RealDecl; 834} 835 836/// CreateType - get objective-c interface type. 837llvm::DIType CGDebugInfo::CreateType(const ObjCInterfaceType *Ty, 838 llvm::DIFile Unit) { 839 ObjCInterfaceDecl *ID = Ty->getDecl(); 840 841 unsigned Tag = llvm::dwarf::DW_TAG_structure_type; 842 SourceManager &SM = CGM.getContext().getSourceManager(); 843 844 // Get overall information about the record type for the debug info. 845 llvm::DIFile DefUnit = getOrCreateFile(ID->getLocation()); 846 PresumedLoc PLoc = SM.getPresumedLoc(ID->getLocation()); 847 unsigned Line = PLoc.isInvalid() ? 0 : PLoc.getLine(); 848 849 850 unsigned RuntimeLang = TheCU.getLanguage(); 851 852 // To handle recursive interface, we 853 // first generate a debug descriptor for the struct as a forward declaration. 854 // Then (if it is a definition) we go through and get debug info for all of 855 // its members. Finally, we create a descriptor for the complete type (which 856 // may refer to the forward decl if the struct is recursive) and replace all 857 // uses of the forward declaration with the final definition. 858 llvm::DICompositeType FwdDecl = 859 DebugFactory.CreateCompositeType(Tag, Unit, ID->getName(), 860 DefUnit, Line, 0, 0, 0, 0, 861 llvm::DIType(), llvm::DIArray(), 862 RuntimeLang); 863 864 // If this is just a forward declaration, return it. 865 if (ID->isForwardDecl()) 866 return FwdDecl; 867 868 llvm::TrackingVH<llvm::MDNode> FwdDeclNode = FwdDecl.getNode(); 869 // Otherwise, insert it into the TypeCache so that recursive uses will find 870 // it. 871 TypeCache[QualType(Ty, 0).getAsOpaquePtr()] = FwdDecl.getNode(); 872 // Push the struct on region stack. 873 RegionStack.push_back(FwdDecl.getNode()); 874 RegionMap[Ty->getDecl()] = llvm::WeakVH(FwdDecl.getNode()); 875 876 // Convert all the elements. 877 llvm::SmallVector<llvm::DIDescriptor, 16> EltTys; 878 879 ObjCInterfaceDecl *SClass = ID->getSuperClass(); 880 if (SClass) { 881 llvm::DIType SClassTy = 882 getOrCreateType(CGM.getContext().getObjCInterfaceType(SClass), Unit); 883 llvm::DIType InhTag = 884 DebugFactory.CreateDerivedType(llvm::dwarf::DW_TAG_inheritance, 885 Unit, "", Unit, 0, 0, 0, 886 0 /* offset */, 0, SClassTy); 887 EltTys.push_back(InhTag); 888 } 889 890 const ASTRecordLayout &RL = CGM.getContext().getASTObjCInterfaceLayout(ID); 891 892 unsigned FieldNo = 0; 893 for (ObjCInterfaceDecl::ivar_iterator I = ID->ivar_begin(), 894 E = ID->ivar_end(); I != E; ++I, ++FieldNo) { 895 ObjCIvarDecl *Field = *I; 896 llvm::DIType FieldTy = getOrCreateType(Field->getType(), Unit); 897 898 llvm::StringRef FieldName = Field->getName(); 899 900 // Ignore unnamed fields. 901 if (FieldName.empty()) 902 continue; 903 904 // Get the location for the field. 905 SourceLocation FieldDefLoc = Field->getLocation(); 906 llvm::DIFile FieldDefUnit = getOrCreateFile(FieldDefLoc); 907 PresumedLoc PLoc = SM.getPresumedLoc(FieldDefLoc); 908 unsigned FieldLine = PLoc.isInvalid() ? 0 : PLoc.getLine(); 909 910 911 QualType FType = Field->getType(); 912 uint64_t FieldSize = 0; 913 unsigned FieldAlign = 0; 914 915 if (!FType->isIncompleteArrayType()) { 916 917 // Bit size, align and offset of the type. 918 FieldSize = CGM.getContext().getTypeSize(FType); 919 Expr *BitWidth = Field->getBitWidth(); 920 if (BitWidth) 921 FieldSize = BitWidth->EvaluateAsInt(CGM.getContext()).getZExtValue(); 922 923 FieldAlign = CGM.getContext().getTypeAlign(FType); 924 } 925 926 uint64_t FieldOffset = RL.getFieldOffset(FieldNo); 927 928 unsigned Flags = 0; 929 if (Field->getAccessControl() == ObjCIvarDecl::Protected) 930 Flags = llvm::DIType::FlagProtected; 931 else if (Field->getAccessControl() == ObjCIvarDecl::Private) 932 Flags = llvm::DIType::FlagPrivate; 933 934 // Create a DW_TAG_member node to remember the offset of this field in the 935 // struct. FIXME: This is an absolutely insane way to capture this 936 // information. When we gut debug info, this should be fixed. 937 FieldTy = DebugFactory.CreateDerivedType(llvm::dwarf::DW_TAG_member, Unit, 938 FieldName, FieldDefUnit, 939 FieldLine, FieldSize, FieldAlign, 940 FieldOffset, Flags, FieldTy); 941 EltTys.push_back(FieldTy); 942 } 943 944 llvm::DIArray Elements = 945 DebugFactory.GetOrCreateArray(EltTys.data(), EltTys.size()); 946 947 RegionStack.pop_back(); 948 llvm::DenseMap<const Decl *, llvm::WeakVH>::iterator RI = 949 RegionMap.find(Ty->getDecl()); 950 if (RI != RegionMap.end()) 951 RegionMap.erase(RI); 952 953 // Bit size, align and offset of the type. 954 uint64_t Size = CGM.getContext().getTypeSize(Ty); 955 uint64_t Align = CGM.getContext().getTypeAlign(Ty); 956 957 llvm::DICompositeType RealDecl = 958 DebugFactory.CreateCompositeType(Tag, Unit, ID->getName(), DefUnit, 959 Line, Size, Align, 0, 0, llvm::DIType(), 960 Elements, RuntimeLang); 961 962 // Now that we have a real decl for the struct, replace anything using the 963 // old decl with the new one. This will recursively update the debug info. 964 llvm::DIDerivedType(FwdDeclNode).replaceAllUsesWith(RealDecl); 965 RegionMap[ID] = llvm::WeakVH(RealDecl.getNode()); 966 967 return RealDecl; 968} 969 970llvm::DIType CGDebugInfo::CreateType(const EnumType *Ty, 971 llvm::DIFile Unit) { 972 EnumDecl *ED = Ty->getDecl(); 973 974 llvm::SmallVector<llvm::DIDescriptor, 32> Enumerators; 975 976 // Create DIEnumerator elements for each enumerator. 977 for (EnumDecl::enumerator_iterator 978 Enum = ED->enumerator_begin(), EnumEnd = ED->enumerator_end(); 979 Enum != EnumEnd; ++Enum) { 980 Enumerators.push_back(DebugFactory.CreateEnumerator(Enum->getName(), 981 Enum->getInitVal().getZExtValue())); 982 } 983 984 // Return a CompositeType for the enum itself. 985 llvm::DIArray EltArray = 986 DebugFactory.GetOrCreateArray(Enumerators.data(), Enumerators.size()); 987 988 SourceLocation DefLoc = ED->getLocation(); 989 llvm::DIFile DefUnit = getOrCreateFile(DefLoc); 990 SourceManager &SM = CGM.getContext().getSourceManager(); 991 PresumedLoc PLoc = SM.getPresumedLoc(DefLoc); 992 unsigned Line = PLoc.isInvalid() ? 0 : PLoc.getLine(); 993 994 995 // Size and align of the type. 996 uint64_t Size = 0; 997 unsigned Align = 0; 998 if (!Ty->isIncompleteType()) { 999 Size = CGM.getContext().getTypeSize(Ty); 1000 Align = CGM.getContext().getTypeAlign(Ty); 1001 } 1002 1003 llvm::DIType DbgTy = 1004 DebugFactory.CreateCompositeType(llvm::dwarf::DW_TAG_enumeration_type, 1005 Unit, ED->getName(), DefUnit, Line, 1006 Size, Align, 0, 0, 1007 llvm::DIType(), EltArray); 1008 return DbgTy; 1009} 1010 1011llvm::DIType CGDebugInfo::CreateType(const TagType *Ty, 1012 llvm::DIFile Unit) { 1013 if (const RecordType *RT = dyn_cast<RecordType>(Ty)) 1014 return CreateType(RT, Unit); 1015 else if (const EnumType *ET = dyn_cast<EnumType>(Ty)) 1016 return CreateType(ET, Unit); 1017 1018 return llvm::DIType(); 1019} 1020 1021llvm::DIType CGDebugInfo::CreateType(const VectorType *Ty, 1022 llvm::DIFile Unit) { 1023 llvm::DIType ElementTy = getOrCreateType(Ty->getElementType(), Unit); 1024 uint64_t NumElems = Ty->getNumElements(); 1025 if (NumElems > 0) 1026 --NumElems; 1027 1028 llvm::DIDescriptor Subscript = DebugFactory.GetOrCreateSubrange(0, NumElems); 1029 llvm::DIArray SubscriptArray = DebugFactory.GetOrCreateArray(&Subscript, 1); 1030 1031 uint64_t Size = CGM.getContext().getTypeSize(Ty); 1032 uint64_t Align = CGM.getContext().getTypeAlign(Ty); 1033 1034 return 1035 DebugFactory.CreateCompositeType(llvm::dwarf::DW_TAG_vector_type, 1036 Unit, "", Unit, 1037 0, Size, Align, 0, 0, 1038 ElementTy, SubscriptArray); 1039} 1040 1041llvm::DIType CGDebugInfo::CreateType(const ArrayType *Ty, 1042 llvm::DIFile Unit) { 1043 uint64_t Size; 1044 uint64_t Align; 1045 1046 1047 // FIXME: make getTypeAlign() aware of VLAs and incomplete array types 1048 if (const VariableArrayType *VAT = dyn_cast<VariableArrayType>(Ty)) { 1049 Size = 0; 1050 Align = 1051 CGM.getContext().getTypeAlign(CGM.getContext().getBaseElementType(VAT)); 1052 } else if (Ty->isIncompleteArrayType()) { 1053 Size = 0; 1054 Align = CGM.getContext().getTypeAlign(Ty->getElementType()); 1055 } else { 1056 // Size and align of the whole array, not the element type. 1057 Size = CGM.getContext().getTypeSize(Ty); 1058 Align = CGM.getContext().getTypeAlign(Ty); 1059 } 1060 1061 // Add the dimensions of the array. FIXME: This loses CV qualifiers from 1062 // interior arrays, do we care? Why aren't nested arrays represented the 1063 // obvious/recursive way? 1064 llvm::SmallVector<llvm::DIDescriptor, 8> Subscripts; 1065 QualType EltTy(Ty, 0); 1066 while ((Ty = dyn_cast<ArrayType>(EltTy))) { 1067 uint64_t Upper = 0; 1068 if (const ConstantArrayType *CAT = dyn_cast<ConstantArrayType>(Ty)) 1069 if (CAT->getSize().getZExtValue()) 1070 Upper = CAT->getSize().getZExtValue() - 1; 1071 // FIXME: Verify this is right for VLAs. 1072 Subscripts.push_back(DebugFactory.GetOrCreateSubrange(0, Upper)); 1073 EltTy = Ty->getElementType(); 1074 } 1075 1076 llvm::DIArray SubscriptArray = 1077 DebugFactory.GetOrCreateArray(Subscripts.data(), Subscripts.size()); 1078 1079 llvm::DIType DbgTy = 1080 DebugFactory.CreateCompositeType(llvm::dwarf::DW_TAG_array_type, 1081 Unit, "", Unit, 1082 0, Size, Align, 0, 0, 1083 getOrCreateType(EltTy, Unit), 1084 SubscriptArray); 1085 return DbgTy; 1086} 1087 1088llvm::DIType CGDebugInfo::CreateType(const LValueReferenceType *Ty, 1089 llvm::DIFile Unit) { 1090 return CreatePointerLikeType(llvm::dwarf::DW_TAG_reference_type, 1091 Ty, Ty->getPointeeType(), Unit); 1092} 1093 1094llvm::DIType CGDebugInfo::CreateType(const MemberPointerType *Ty, 1095 llvm::DIFile U) { 1096 QualType PointerDiffTy = CGM.getContext().getPointerDiffType(); 1097 llvm::DIType PointerDiffDITy = getOrCreateType(PointerDiffTy, U); 1098 1099 if (!Ty->getPointeeType()->isFunctionType()) { 1100 // We have a data member pointer type. 1101 return PointerDiffDITy; 1102 } 1103 1104 // We have a member function pointer type. Treat it as a struct with two 1105 // ptrdiff_t members. 1106 std::pair<uint64_t, unsigned> Info = CGM.getContext().getTypeInfo(Ty); 1107 1108 uint64_t FieldOffset = 0; 1109 llvm::DIDescriptor ElementTypes[2]; 1110 1111 // FIXME: This should probably be a function type instead. 1112 ElementTypes[0] = 1113 DebugFactory.CreateDerivedType(llvm::dwarf::DW_TAG_member, U, 1114 "ptr", U, 0, 1115 Info.first, Info.second, FieldOffset, 0, 1116 PointerDiffDITy); 1117 FieldOffset += Info.first; 1118 1119 ElementTypes[1] = 1120 DebugFactory.CreateDerivedType(llvm::dwarf::DW_TAG_member, U, 1121 "ptr", U, 0, 1122 Info.first, Info.second, FieldOffset, 0, 1123 PointerDiffDITy); 1124 1125 llvm::DIArray Elements = 1126 DebugFactory.GetOrCreateArray(&ElementTypes[0], 1127 llvm::array_lengthof(ElementTypes)); 1128 1129 return DebugFactory.CreateCompositeType(llvm::dwarf::DW_TAG_structure_type, 1130 U, llvm::StringRef("test"), 1131 U, 0, FieldOffset, 1132 0, 0, 0, llvm::DIType(), Elements); 1133} 1134 1135static QualType UnwrapTypeForDebugInfo(QualType T) { 1136 do { 1137 QualType LastT = T; 1138 switch (T->getTypeClass()) { 1139 default: 1140 return T; 1141 case Type::TemplateSpecialization: 1142 T = cast<TemplateSpecializationType>(T)->desugar(); 1143 break; 1144 case Type::TypeOfExpr: { 1145 TypeOfExprType *Ty = cast<TypeOfExprType>(T); 1146 T = Ty->getUnderlyingExpr()->getType(); 1147 break; 1148 } 1149 case Type::TypeOf: 1150 T = cast<TypeOfType>(T)->getUnderlyingType(); 1151 break; 1152 case Type::Decltype: 1153 T = cast<DecltypeType>(T)->getUnderlyingType(); 1154 break; 1155 case Type::QualifiedName: 1156 T = cast<QualifiedNameType>(T)->getNamedType(); 1157 break; 1158 case Type::SubstTemplateTypeParm: 1159 T = cast<SubstTemplateTypeParmType>(T)->getReplacementType(); 1160 break; 1161 case Type::Elaborated: 1162 T = cast<ElaboratedType>(T)->getUnderlyingType(); 1163 break; 1164 } 1165 1166 assert(T != LastT && "Type unwrapping failed to unwrap!"); 1167 if (T == LastT) 1168 return T; 1169 } while (true); 1170 1171 return T; 1172} 1173 1174/// getOrCreateType - Get the type from the cache or create a new 1175/// one if necessary. 1176llvm::DIType CGDebugInfo::getOrCreateType(QualType Ty, 1177 llvm::DIFile Unit) { 1178 if (Ty.isNull()) 1179 return llvm::DIType(); 1180 1181 // Unwrap the type as needed for debug information. 1182 Ty = UnwrapTypeForDebugInfo(Ty); 1183 1184 // Check for existing entry. 1185 llvm::DenseMap<void *, llvm::WeakVH>::iterator it = 1186 TypeCache.find(Ty.getAsOpaquePtr()); 1187 if (it != TypeCache.end()) { 1188 // Verify that the debug info still exists. 1189 if (&*it->second) 1190 return llvm::DIType(cast<llvm::MDNode>(it->second)); 1191 } 1192 1193 // Otherwise create the type. 1194 llvm::DIType Res = CreateTypeNode(Ty, Unit); 1195 1196 // And update the type cache. 1197 TypeCache[Ty.getAsOpaquePtr()] = Res.getNode(); 1198 return Res; 1199} 1200 1201/// CreateTypeNode - Create a new debug type node. 1202llvm::DIType CGDebugInfo::CreateTypeNode(QualType Ty, 1203 llvm::DIFile Unit) { 1204 // Handle qualifiers, which recursively handles what they refer to. 1205 if (Ty.hasLocalQualifiers()) 1206 return CreateQualifiedType(Ty, Unit); 1207 1208 const char *Diag = 0; 1209 1210 // Work out details of type. 1211 switch (Ty->getTypeClass()) { 1212#define TYPE(Class, Base) 1213#define ABSTRACT_TYPE(Class, Base) 1214#define NON_CANONICAL_TYPE(Class, Base) 1215#define DEPENDENT_TYPE(Class, Base) case Type::Class: 1216#include "clang/AST/TypeNodes.def" 1217 assert(false && "Dependent types cannot show up in debug information"); 1218 1219 // FIXME: Handle these. 1220 case Type::ExtVector: 1221 return llvm::DIType(); 1222 1223 case Type::Vector: 1224 return CreateType(cast<VectorType>(Ty), Unit); 1225 case Type::ObjCObjectPointer: 1226 return CreateType(cast<ObjCObjectPointerType>(Ty), Unit); 1227 case Type::ObjCInterface: 1228 return CreateType(cast<ObjCInterfaceType>(Ty), Unit); 1229 case Type::Builtin: return CreateType(cast<BuiltinType>(Ty), Unit); 1230 case Type::Complex: return CreateType(cast<ComplexType>(Ty), Unit); 1231 case Type::Pointer: return CreateType(cast<PointerType>(Ty), Unit); 1232 case Type::BlockPointer: 1233 return CreateType(cast<BlockPointerType>(Ty), Unit); 1234 case Type::Typedef: return CreateType(cast<TypedefType>(Ty), Unit); 1235 case Type::Record: 1236 case Type::Enum: 1237 return CreateType(cast<TagType>(Ty), Unit); 1238 case Type::FunctionProto: 1239 case Type::FunctionNoProto: 1240 return CreateType(cast<FunctionType>(Ty), Unit); 1241 case Type::ConstantArray: 1242 case Type::VariableArray: 1243 case Type::IncompleteArray: 1244 return CreateType(cast<ArrayType>(Ty), Unit); 1245 1246 case Type::LValueReference: 1247 return CreateType(cast<LValueReferenceType>(Ty), Unit); 1248 1249 case Type::MemberPointer: 1250 return CreateType(cast<MemberPointerType>(Ty), Unit); 1251 1252 case Type::TemplateSpecialization: 1253 case Type::Elaborated: 1254 case Type::QualifiedName: 1255 case Type::SubstTemplateTypeParm: 1256 case Type::TypeOfExpr: 1257 case Type::TypeOf: 1258 case Type::Decltype: 1259 llvm_unreachable("type should have been unwrapped!"); 1260 return llvm::DIType(); 1261 1262 case Type::RValueReference: 1263 // FIXME: Implement! 1264 Diag = "rvalue references"; 1265 break; 1266 } 1267 1268 assert(Diag && "Fall through without a diagnostic?"); 1269 unsigned DiagID = CGM.getDiags().getCustomDiagID(Diagnostic::Error, 1270 "debug information for %0 is not yet supported"); 1271 CGM.getDiags().Report(FullSourceLoc(), DiagID) 1272 << Diag; 1273 return llvm::DIType(); 1274} 1275 1276/// CreateMemberType - Create new member and increase Offset by FType's size. 1277llvm::DIType CGDebugInfo::CreateMemberType(llvm::DIFile Unit, QualType FType, 1278 llvm::StringRef Name, 1279 uint64_t *Offset) { 1280 llvm::DIType FieldTy = CGDebugInfo::getOrCreateType(FType, Unit); 1281 uint64_t FieldSize = CGM.getContext().getTypeSize(FType); 1282 unsigned FieldAlign = CGM.getContext().getTypeAlign(FType); 1283 llvm::DIType Ty = DebugFactory.CreateDerivedType(llvm::dwarf::DW_TAG_member, 1284 Unit, Name, Unit, 0, 1285 FieldSize, FieldAlign, 1286 *Offset, 0, FieldTy); 1287 *Offset += FieldSize; 1288 return Ty; 1289} 1290 1291/// EmitFunctionStart - Constructs the debug code for entering a function - 1292/// "llvm.dbg.func.start.". 1293void CGDebugInfo::EmitFunctionStart(GlobalDecl GD, QualType FnType, 1294 llvm::Function *Fn, 1295 CGBuilderTy &Builder) { 1296 1297 llvm::StringRef Name; 1298 MangleBuffer LinkageName; 1299 1300 const Decl *D = GD.getDecl(); 1301 if (const FunctionDecl *FD = dyn_cast<FunctionDecl>(D)) { 1302 // If there is a DISubprogram for this function available then use it. 1303 llvm::DenseMap<const FunctionDecl *, llvm::WeakVH>::iterator 1304 FI = SPCache.find(FD); 1305 if (FI != SPCache.end()) { 1306 llvm::DIDescriptor SP(dyn_cast_or_null<llvm::MDNode>(FI->second)); 1307 if (SP.isSubprogram() && llvm::DISubprogram(SP.getNode()).isDefinition()) { 1308 RegionStack.push_back(SP.getNode()); 1309 RegionMap[D] = llvm::WeakVH(SP.getNode()); 1310 return; 1311 } 1312 } 1313 Name = getFunctionName(FD); 1314 // Use mangled name as linkage name for c/c++ functions. 1315 CGM.getMangledName(LinkageName, GD); 1316 } else { 1317 // Use llvm function name as linkage name. 1318 Name = Fn->getName(); 1319 LinkageName.setString(Name); 1320 } 1321 if (!Name.empty() && Name[0] == '\01') 1322 Name = Name.substr(1); 1323 1324 // It is expected that CurLoc is set before using EmitFunctionStart. 1325 // Usually, CurLoc points to the left bracket location of compound 1326 // statement representing function body. 1327 llvm::DIFile Unit = getOrCreateFile(CurLoc); 1328 SourceManager &SM = CGM.getContext().getSourceManager(); 1329 unsigned LineNo = SM.getPresumedLoc(CurLoc).getLine(); 1330 1331 llvm::DISubprogram SP = 1332 DebugFactory.CreateSubprogram(Unit, Name, Name, LinkageName, Unit, LineNo, 1333 getOrCreateType(FnType, Unit), 1334 Fn->hasInternalLinkage(), true/*definition*/); 1335 1336 // Push function on region stack. 1337 RegionStack.push_back(SP.getNode()); 1338 RegionMap[D] = llvm::WeakVH(SP.getNode()); 1339} 1340 1341 1342void CGDebugInfo::EmitStopPoint(llvm::Function *Fn, CGBuilderTy &Builder) { 1343 if (CurLoc.isInvalid() || CurLoc.isMacroID()) return; 1344 1345 // Don't bother if things are the same as last time. 1346 SourceManager &SM = CGM.getContext().getSourceManager(); 1347 if (CurLoc == PrevLoc 1348 || (SM.getInstantiationLineNumber(CurLoc) == 1349 SM.getInstantiationLineNumber(PrevLoc) 1350 && SM.isFromSameFile(CurLoc, PrevLoc))) 1351 // New Builder may not be in sync with CGDebugInfo. 1352 if (!Builder.getCurrentDebugLocation().isUnknown()) 1353 return; 1354 1355 // Update last state. 1356 PrevLoc = CurLoc; 1357 1358 // Get the appropriate compile unit. 1359 llvm::DIFile Unit = getOrCreateFile(CurLoc); 1360 PresumedLoc PLoc = SM.getPresumedLoc(CurLoc); 1361 1362 llvm::MDNode *Scope = RegionStack.back(); 1363 Builder.SetCurrentDebugLocation(llvm::DebugLoc::get(PLoc.getLine(), 1364 PLoc.getColumn(), 1365 Scope)); 1366} 1367 1368/// EmitRegionStart- Constructs the debug code for entering a declarative 1369/// region - "llvm.dbg.region.start.". 1370void CGDebugInfo::EmitRegionStart(llvm::Function *Fn, CGBuilderTy &Builder) { 1371 SourceManager &SM = CGM.getContext().getSourceManager(); 1372 PresumedLoc PLoc = SM.getPresumedLoc(CurLoc); 1373 llvm::DIDescriptor D = 1374 DebugFactory.CreateLexicalBlock(RegionStack.empty() ? 1375 llvm::DIDescriptor() : 1376 llvm::DIDescriptor(RegionStack.back()), 1377 PLoc.getLine(), PLoc.getColumn()); 1378 RegionStack.push_back(D.getNode()); 1379} 1380 1381/// EmitRegionEnd - Constructs the debug code for exiting a declarative 1382/// region - "llvm.dbg.region.end." 1383void CGDebugInfo::EmitRegionEnd(llvm::Function *Fn, CGBuilderTy &Builder) { 1384 assert(!RegionStack.empty() && "Region stack mismatch, stack empty!"); 1385 1386 // Provide an region stop point. 1387 EmitStopPoint(Fn, Builder); 1388 1389 RegionStack.pop_back(); 1390} 1391 1392// EmitTypeForVarWithBlocksAttr - Build up structure info for the byref. 1393// See BuildByRefType. 1394llvm::DIType CGDebugInfo::EmitTypeForVarWithBlocksAttr(const ValueDecl *VD, 1395 uint64_t *XOffset) { 1396 1397 llvm::SmallVector<llvm::DIDescriptor, 5> EltTys; 1398 1399 QualType FType; 1400 uint64_t FieldSize, FieldOffset; 1401 unsigned FieldAlign; 1402 1403 llvm::DIFile Unit = getOrCreateFile(VD->getLocation()); 1404 QualType Type = VD->getType(); 1405 1406 FieldOffset = 0; 1407 FType = CGM.getContext().getPointerType(CGM.getContext().VoidTy); 1408 EltTys.push_back(CreateMemberType(Unit, FType, "__isa", &FieldOffset)); 1409 EltTys.push_back(CreateMemberType(Unit, FType, "__forwarding", &FieldOffset)); 1410 FType = CGM.getContext().IntTy; 1411 EltTys.push_back(CreateMemberType(Unit, FType, "__flags", &FieldOffset)); 1412 EltTys.push_back(CreateMemberType(Unit, FType, "__size", &FieldOffset)); 1413 1414 bool HasCopyAndDispose = CGM.BlockRequiresCopying(Type); 1415 if (HasCopyAndDispose) { 1416 FType = CGM.getContext().getPointerType(CGM.getContext().VoidTy); 1417 EltTys.push_back(CreateMemberType(Unit, FType, "__copy_helper", 1418 &FieldOffset)); 1419 EltTys.push_back(CreateMemberType(Unit, FType, "__destroy_helper", 1420 &FieldOffset)); 1421 } 1422 1423 CharUnits Align = CGM.getContext().getDeclAlign(VD); 1424 if (Align > CharUnits::fromQuantity( 1425 CGM.getContext().Target.getPointerAlign(0) / 8)) { 1426 unsigned AlignedOffsetInBytes 1427 = llvm::RoundUpToAlignment(FieldOffset/8, Align.getQuantity()); 1428 unsigned NumPaddingBytes 1429 = AlignedOffsetInBytes - FieldOffset/8; 1430 1431 if (NumPaddingBytes > 0) { 1432 llvm::APInt pad(32, NumPaddingBytes); 1433 FType = CGM.getContext().getConstantArrayType(CGM.getContext().CharTy, 1434 pad, ArrayType::Normal, 0); 1435 EltTys.push_back(CreateMemberType(Unit, FType, "", &FieldOffset)); 1436 } 1437 } 1438 1439 FType = Type; 1440 llvm::DIType FieldTy = CGDebugInfo::getOrCreateType(FType, Unit); 1441 FieldSize = CGM.getContext().getTypeSize(FType); 1442 FieldAlign = Align.getQuantity()*8; 1443 1444 *XOffset = FieldOffset; 1445 FieldTy = DebugFactory.CreateDerivedType(llvm::dwarf::DW_TAG_member, Unit, 1446 VD->getName(), Unit, 1447 0, FieldSize, FieldAlign, 1448 FieldOffset, 0, FieldTy); 1449 EltTys.push_back(FieldTy); 1450 FieldOffset += FieldSize; 1451 1452 llvm::DIArray Elements = 1453 DebugFactory.GetOrCreateArray(EltTys.data(), EltTys.size()); 1454 1455 unsigned Flags = llvm::DIType::FlagBlockByrefStruct; 1456 1457 return DebugFactory.CreateCompositeType(llvm::dwarf::DW_TAG_structure_type, 1458 Unit, "", Unit, 1459 0, FieldOffset, 0, 0, Flags, 1460 llvm::DIType(), Elements); 1461 1462} 1463/// EmitDeclare - Emit local variable declaration debug info. 1464void CGDebugInfo::EmitDeclare(const VarDecl *VD, unsigned Tag, 1465 llvm::Value *Storage, CGBuilderTy &Builder) { 1466 assert(!RegionStack.empty() && "Region stack mismatch, stack empty!"); 1467 1468 llvm::DIFile Unit = getOrCreateFile(VD->getLocation()); 1469 llvm::DIType Ty; 1470 uint64_t XOffset = 0; 1471 if (VD->hasAttr<BlocksAttr>()) 1472 Ty = EmitTypeForVarWithBlocksAttr(VD, &XOffset); 1473 else 1474 Ty = getOrCreateType(VD->getType(), Unit); 1475 1476 // Get location information. 1477 SourceManager &SM = CGM.getContext().getSourceManager(); 1478 PresumedLoc PLoc = SM.getPresumedLoc(VD->getLocation()); 1479 unsigned Line = 0; 1480 unsigned Column = 0; 1481 if (PLoc.isInvalid()) 1482 PLoc = SM.getPresumedLoc(CurLoc); 1483 if (PLoc.isValid()) { 1484 Line = PLoc.getLine(); 1485 Column = PLoc.getColumn(); 1486 Unit = getOrCreateFile(CurLoc); 1487 } else { 1488 Unit = llvm::DIFile(); 1489 } 1490 1491 // Create the descriptor for the variable. 1492 llvm::DIVariable D = 1493 DebugFactory.CreateVariable(Tag, llvm::DIDescriptor(RegionStack.back()), 1494 VD->getName(), 1495 Unit, Line, Ty); 1496 // Insert an llvm.dbg.declare into the current block. 1497 llvm::Instruction *Call = 1498 DebugFactory.InsertDeclare(Storage, D, Builder.GetInsertBlock()); 1499 1500 llvm::MDNode *Scope = RegionStack.back(); 1501 Call->setDebugLoc(llvm::DebugLoc::get(Line, Column, Scope)); 1502} 1503 1504/// EmitDeclare - Emit local variable declaration debug info. 1505void CGDebugInfo::EmitDeclare(const BlockDeclRefExpr *BDRE, unsigned Tag, 1506 llvm::Value *Storage, CGBuilderTy &Builder, 1507 CodeGenFunction *CGF) { 1508 const ValueDecl *VD = BDRE->getDecl(); 1509 assert(!RegionStack.empty() && "Region stack mismatch, stack empty!"); 1510 1511 if (Builder.GetInsertBlock() == 0) 1512 return; 1513 1514 uint64_t XOffset = 0; 1515 llvm::DIFile Unit = getOrCreateFile(VD->getLocation()); 1516 llvm::DIType Ty; 1517 if (VD->hasAttr<BlocksAttr>()) 1518 Ty = EmitTypeForVarWithBlocksAttr(VD, &XOffset); 1519 else 1520 Ty = getOrCreateType(VD->getType(), Unit); 1521 1522 // Get location information. 1523 SourceManager &SM = CGM.getContext().getSourceManager(); 1524 PresumedLoc PLoc = SM.getPresumedLoc(VD->getLocation()); 1525 unsigned Line = 0; 1526 if (!PLoc.isInvalid()) 1527 Line = PLoc.getLine(); 1528 else 1529 Unit = llvm::DIFile(); 1530 1531 CharUnits offset = CGF->BlockDecls[VD]; 1532 llvm::SmallVector<llvm::Value *, 9> addr; 1533 const llvm::Type *Int64Ty = llvm::Type::getInt64Ty(CGM.getLLVMContext()); 1534 addr.push_back(llvm::ConstantInt::get(Int64Ty, llvm::DIFactory::OpDeref)); 1535 addr.push_back(llvm::ConstantInt::get(Int64Ty, llvm::DIFactory::OpPlus)); 1536 addr.push_back(llvm::ConstantInt::get(Int64Ty, offset.getQuantity())); 1537 if (BDRE->isByRef()) { 1538 addr.push_back(llvm::ConstantInt::get(Int64Ty, llvm::DIFactory::OpDeref)); 1539 addr.push_back(llvm::ConstantInt::get(Int64Ty, llvm::DIFactory::OpPlus)); 1540 // offset of __forwarding field 1541 offset = CharUnits::fromQuantity(CGF->LLVMPointerWidth/8); 1542 addr.push_back(llvm::ConstantInt::get(Int64Ty, offset.getQuantity())); 1543 addr.push_back(llvm::ConstantInt::get(Int64Ty, llvm::DIFactory::OpDeref)); 1544 addr.push_back(llvm::ConstantInt::get(Int64Ty, llvm::DIFactory::OpPlus)); 1545 // offset of x field 1546 offset = CharUnits::fromQuantity(XOffset/8); 1547 addr.push_back(llvm::ConstantInt::get(Int64Ty, offset.getQuantity())); 1548 } 1549 1550 // Create the descriptor for the variable. 1551 llvm::DIVariable D = 1552 DebugFactory.CreateComplexVariable(Tag, 1553 llvm::DIDescriptor(RegionStack.back()), 1554 VD->getName(), Unit, Line, Ty, 1555 addr); 1556 // Insert an llvm.dbg.declare into the current block. 1557 llvm::Instruction *Call = 1558 DebugFactory.InsertDeclare(Storage, D, Builder.GetInsertBlock()); 1559 1560 llvm::MDNode *Scope = RegionStack.back(); 1561 Call->setDebugLoc(llvm::DebugLoc::get(Line, PLoc.getColumn(), Scope)); 1562} 1563 1564void CGDebugInfo::EmitDeclareOfAutoVariable(const VarDecl *VD, 1565 llvm::Value *Storage, 1566 CGBuilderTy &Builder) { 1567 EmitDeclare(VD, llvm::dwarf::DW_TAG_auto_variable, Storage, Builder); 1568} 1569 1570void CGDebugInfo::EmitDeclareOfBlockDeclRefVariable( 1571 const BlockDeclRefExpr *BDRE, llvm::Value *Storage, CGBuilderTy &Builder, 1572 CodeGenFunction *CGF) { 1573 EmitDeclare(BDRE, llvm::dwarf::DW_TAG_auto_variable, Storage, Builder, CGF); 1574} 1575 1576/// EmitDeclareOfArgVariable - Emit call to llvm.dbg.declare for an argument 1577/// variable declaration. 1578void CGDebugInfo::EmitDeclareOfArgVariable(const VarDecl *VD, llvm::Value *AI, 1579 CGBuilderTy &Builder) { 1580 EmitDeclare(VD, llvm::dwarf::DW_TAG_arg_variable, AI, Builder); 1581} 1582 1583 1584 1585/// EmitGlobalVariable - Emit information about a global variable. 1586void CGDebugInfo::EmitGlobalVariable(llvm::GlobalVariable *Var, 1587 const VarDecl *D) { 1588 1589 // Create global variable debug descriptor. 1590 llvm::DIFile Unit = getOrCreateFile(D->getLocation()); 1591 SourceManager &SM = CGM.getContext().getSourceManager(); 1592 PresumedLoc PLoc = SM.getPresumedLoc(D->getLocation()); 1593 unsigned LineNo = PLoc.isInvalid() ? 0 : PLoc.getLine(); 1594 1595 QualType T = D->getType(); 1596 if (T->isIncompleteArrayType()) { 1597 1598 // CodeGen turns int[] into int[1] so we'll do the same here. 1599 llvm::APSInt ConstVal(32); 1600 1601 ConstVal = 1; 1602 QualType ET = CGM.getContext().getAsArrayType(T)->getElementType(); 1603 1604 T = CGM.getContext().getConstantArrayType(ET, ConstVal, 1605 ArrayType::Normal, 0); 1606 } 1607 llvm::StringRef DeclName = D->getName(); 1608 llvm::DIDescriptor DContext = 1609 getContextDescriptor(dyn_cast<Decl>(D->getDeclContext()), Unit); 1610 DebugFactory.CreateGlobalVariable(DContext, DeclName, 1611 DeclName, llvm::StringRef(), Unit, LineNo, 1612 getOrCreateType(T, Unit), 1613 Var->hasInternalLinkage(), 1614 true/*definition*/, Var); 1615} 1616 1617/// EmitGlobalVariable - Emit information about an objective-c interface. 1618void CGDebugInfo::EmitGlobalVariable(llvm::GlobalVariable *Var, 1619 ObjCInterfaceDecl *ID) { 1620 // Create global variable debug descriptor. 1621 llvm::DIFile Unit = getOrCreateFile(ID->getLocation()); 1622 SourceManager &SM = CGM.getContext().getSourceManager(); 1623 PresumedLoc PLoc = SM.getPresumedLoc(ID->getLocation()); 1624 unsigned LineNo = PLoc.isInvalid() ? 0 : PLoc.getLine(); 1625 1626 llvm::StringRef Name = ID->getName(); 1627 1628 QualType T = CGM.getContext().getObjCInterfaceType(ID); 1629 if (T->isIncompleteArrayType()) { 1630 1631 // CodeGen turns int[] into int[1] so we'll do the same here. 1632 llvm::APSInt ConstVal(32); 1633 1634 ConstVal = 1; 1635 QualType ET = CGM.getContext().getAsArrayType(T)->getElementType(); 1636 1637 T = CGM.getContext().getConstantArrayType(ET, ConstVal, 1638 ArrayType::Normal, 0); 1639 } 1640 1641 DebugFactory.CreateGlobalVariable(Unit, Name, Name, Name, Unit, LineNo, 1642 getOrCreateType(T, Unit), 1643 Var->hasInternalLinkage(), 1644 true/*definition*/, Var); 1645} 1646 1647/// getOrCreateNamesSpace - Return namespace descriptor for the given 1648/// namespace decl. 1649llvm::DINameSpace 1650CGDebugInfo::getOrCreateNameSpace(const NamespaceDecl *NSDecl, 1651 llvm::DIDescriptor Unit) { 1652 llvm::DenseMap<const NamespaceDecl *, llvm::WeakVH>::iterator I = 1653 NameSpaceCache.find(NSDecl); 1654 if (I != NameSpaceCache.end()) 1655 return llvm::DINameSpace(cast<llvm::MDNode>(I->second)); 1656 1657 SourceManager &SM = CGM.getContext().getSourceManager(); 1658 PresumedLoc PLoc = SM.getPresumedLoc(NSDecl->getLocation()); 1659 unsigned LineNo = PLoc.isInvalid() ? 0 : PLoc.getLine(); 1660 1661 llvm::DIDescriptor Context = 1662 getContextDescriptor(dyn_cast<Decl>(NSDecl->getDeclContext()), Unit); 1663 llvm::DINameSpace NS = 1664 DebugFactory.CreateNameSpace(Context, NSDecl->getName(), 1665 llvm::DIFile(Unit.getNode()), LineNo); 1666 NameSpaceCache[NSDecl] = llvm::WeakVH(NS.getNode()); 1667 return NS; 1668} 1669