CGDebugInfo.cpp revision 48c70f62dd7233a0ce5242dfb933b0d4638b3923
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 EltTys.push_back(CreateMemberType(Unit, FType, "__FuncPtr", &FieldOffset)); 350 EltTys.push_back(CreateMemberType(Unit, FType, "__reserved", &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 = getOrCreateType(Method->getType(), Unit); 499 500 // Static methods do not need "this" pointer argument. 501 if (Method->isStatic()) 502 return FnTy; 503 504 // Add "this" pointer. 505 506 llvm::DIArray Args = llvm::DICompositeType(FnTy.getNode()).getTypeArray(); 507 assert (Args.getNumElements() && "Invalid number of arguments!"); 508 509 llvm::SmallVector<llvm::DIDescriptor, 16> Elts; 510 511 // First element is always return type. For 'void' functions it is NULL. 512 Elts.push_back(Args.getElement(0)); 513 514 // "this" pointer is always first argument. 515 ASTContext &Context = CGM.getContext(); 516 QualType ThisPtr = 517 Context.getPointerType(Context.getTagDeclType(Method->getParent())); 518 llvm::DIType ThisPtrType = 519 DebugFactory.CreateArtificialType(getOrCreateType(ThisPtr, Unit)); 520 TypeCache[ThisPtr.getAsOpaquePtr()] = ThisPtrType.getNode(); 521 Elts.push_back(ThisPtrType); 522 523 // Copy rest of the arguments. 524 for (unsigned i = 1, e = Args.getNumElements(); i != e; ++i) 525 Elts.push_back(Args.getElement(i)); 526 527 llvm::DIArray EltTypeArray = 528 DebugFactory.GetOrCreateArray(Elts.data(), Elts.size()); 529 530 return 531 DebugFactory.CreateCompositeType(llvm::dwarf::DW_TAG_subroutine_type, 532 Unit, "", Unit, 533 0, 0, 0, 0, 0, 534 llvm::DIType(), EltTypeArray); 535} 536 537/// CreateCXXMemberFunction - A helper function to create a DISubprogram for 538/// a single member function GlobalDecl. 539llvm::DISubprogram 540CGDebugInfo::CreateCXXMemberFunction(const CXXMethodDecl *Method, 541 llvm::DIFile Unit, 542 llvm::DICompositeType &RecordTy) { 543 bool IsCtorOrDtor = 544 isa<CXXConstructorDecl>(Method) || isa<CXXDestructorDecl>(Method); 545 546 llvm::StringRef MethodName = getFunctionName(Method); 547 llvm::DIType MethodTy = getOrCreateMethodType(Method, Unit); 548 549 // Since a single ctor/dtor corresponds to multiple functions, it doesn't 550 // make sense to give a single ctor/dtor a linkage name. 551 MangleBuffer MethodLinkageName; 552 if (!IsCtorOrDtor) 553 CGM.getMangledName(MethodLinkageName, Method); 554 555 SourceManager &SM = CGM.getContext().getSourceManager(); 556 557 // Get the location for the method. 558 SourceLocation MethodDefLoc = Method->getLocation(); 559 PresumedLoc PLoc = SM.getPresumedLoc(MethodDefLoc); 560 llvm::DIFile MethodDefUnit; 561 unsigned MethodLine = 0; 562 563 if (!PLoc.isInvalid()) { 564 MethodDefUnit = getOrCreateFile(MethodDefLoc); 565 MethodLine = PLoc.getLine(); 566 } 567 568 // Collect virtual method info. 569 llvm::DIType ContainingType; 570 unsigned Virtuality = 0; 571 unsigned VIndex = 0; 572 573 if (Method->isVirtual()) { 574 if (Method->isPure()) 575 Virtuality = llvm::dwarf::DW_VIRTUALITY_pure_virtual; 576 else 577 Virtuality = llvm::dwarf::DW_VIRTUALITY_virtual; 578 579 // It doesn't make sense to give a virtual destructor a vtable index, 580 // since a single destructor has two entries in the vtable. 581 if (!isa<CXXDestructorDecl>(Method)) 582 VIndex = CGM.getVTables().getMethodVTableIndex(Method); 583 ContainingType = RecordTy; 584 } 585 586 llvm::DISubprogram SP = 587 DebugFactory.CreateSubprogram(RecordTy , MethodName, MethodName, 588 MethodLinkageName, 589 MethodDefUnit, MethodLine, 590 MethodTy, /*isLocalToUnit=*/false, 591 Method->isThisDeclarationADefinition(), 592 Virtuality, VIndex, ContainingType); 593 594 // Don't cache ctors or dtors since we have to emit multiple functions for 595 // a single ctor or dtor. 596 if (!IsCtorOrDtor && Method->isThisDeclarationADefinition()) 597 SPCache[Method] = llvm::WeakVH(SP.getNode()); 598 599 return SP; 600} 601 602/// CollectCXXMemberFunctions - A helper function to collect debug info for 603/// C++ member functions.This is used while creating debug info entry for 604/// a Record. 605void CGDebugInfo:: 606CollectCXXMemberFunctions(const CXXRecordDecl *RD, llvm::DIFile Unit, 607 llvm::SmallVectorImpl<llvm::DIDescriptor> &EltTys, 608 llvm::DICompositeType &RecordTy) { 609 for(CXXRecordDecl::method_iterator I = RD->method_begin(), 610 E = RD->method_end(); I != E; ++I) { 611 const CXXMethodDecl *Method = *I; 612 613 if (Method->isImplicit() && !Method->isUsed()) 614 continue; 615 616 EltTys.push_back(CreateCXXMemberFunction(Method, Unit, RecordTy)); 617 } 618} 619 620/// CollectCXXBases - A helper function to collect debug info for 621/// C++ base classes. This is used while creating debug info entry for 622/// a Record. 623void CGDebugInfo:: 624CollectCXXBases(const CXXRecordDecl *RD, llvm::DIFile Unit, 625 llvm::SmallVectorImpl<llvm::DIDescriptor> &EltTys, 626 llvm::DICompositeType &RecordTy) { 627 628 const ASTRecordLayout &RL = CGM.getContext().getASTRecordLayout(RD); 629 for (CXXRecordDecl::base_class_const_iterator BI = RD->bases_begin(), 630 BE = RD->bases_end(); BI != BE; ++BI) { 631 unsigned BFlags = 0; 632 uint64_t BaseOffset; 633 634 const CXXRecordDecl *Base = 635 cast<CXXRecordDecl>(BI->getType()->getAs<RecordType>()->getDecl()); 636 637 if (BI->isVirtual()) { 638 // virtual base offset offset is -ve. The code generator emits dwarf 639 // expression where it expects +ve number. 640 BaseOffset = 0 - CGM.getVTables().getVirtualBaseOffsetOffset(RD, Base); 641 BFlags = llvm::DIType::FlagVirtual; 642 } else 643 BaseOffset = RL.getBaseClassOffset(Base); 644 645 AccessSpecifier Access = BI->getAccessSpecifier(); 646 if (Access == clang::AS_private) 647 BFlags |= llvm::DIType::FlagPrivate; 648 else if (Access == clang::AS_protected) 649 BFlags |= llvm::DIType::FlagProtected; 650 651 llvm::DIType DTy = 652 DebugFactory.CreateDerivedType(llvm::dwarf::DW_TAG_inheritance, 653 RecordTy, llvm::StringRef(), 654 Unit, 0, 0, 0, 655 BaseOffset, BFlags, 656 getOrCreateType(BI->getType(), 657 Unit)); 658 EltTys.push_back(DTy); 659 } 660} 661 662/// getOrCreateVTablePtrType - Return debug info descriptor for vtable. 663llvm::DIType CGDebugInfo::getOrCreateVTablePtrType(llvm::DIFile Unit) { 664 if (VTablePtrType.isValid()) 665 return VTablePtrType; 666 667 ASTContext &Context = CGM.getContext(); 668 669 /* Function type */ 670 llvm::DIDescriptor STy = getOrCreateType(Context.IntTy, Unit); 671 llvm::DIArray SElements = DebugFactory.GetOrCreateArray(&STy, 1); 672 llvm::DIType SubTy = 673 DebugFactory.CreateCompositeType(llvm::dwarf::DW_TAG_subroutine_type, 674 Unit, "", Unit, 675 0, 0, 0, 0, 0, llvm::DIType(), SElements); 676 677 unsigned Size = Context.getTypeSize(Context.VoidPtrTy); 678 llvm::DIType vtbl_ptr_type 679 = DebugFactory.CreateDerivedType(llvm::dwarf::DW_TAG_pointer_type, 680 Unit, "__vtbl_ptr_type", Unit, 681 0, Size, 0, 0, 0, SubTy); 682 683 VTablePtrType = 684 DebugFactory.CreateDerivedType(llvm::dwarf::DW_TAG_pointer_type, 685 Unit, "", Unit, 686 0, Size, 0, 0, 0, vtbl_ptr_type); 687 return VTablePtrType; 688} 689 690/// getVTableName - Get vtable name for the given Class. 691llvm::StringRef CGDebugInfo::getVTableName(const CXXRecordDecl *RD) { 692 // Otherwise construct gdb compatible name name. 693 std::string Name = "_vptr$" + RD->getNameAsString(); 694 695 // Copy this name on the side and use its reference. 696 char *StrPtr = DebugInfoNames.Allocate<char>(Name.length()); 697 memcpy(StrPtr, Name.data(), Name.length()); 698 return llvm::StringRef(StrPtr, Name.length()); 699} 700 701 702/// CollectVTableInfo - If the C++ class has vtable info then insert appropriate 703/// debug info entry in EltTys vector. 704void CGDebugInfo:: 705CollectVTableInfo(const CXXRecordDecl *RD, llvm::DIFile Unit, 706 llvm::SmallVectorImpl<llvm::DIDescriptor> &EltTys) { 707 const ASTRecordLayout &RL = CGM.getContext().getASTRecordLayout(RD); 708 709 // If there is a primary base then it will hold vtable info. 710 if (RL.getPrimaryBase()) 711 return; 712 713 // If this class is not dynamic then there is not any vtable info to collect. 714 if (!RD->isDynamicClass()) 715 return; 716 717 unsigned Size = CGM.getContext().getTypeSize(CGM.getContext().VoidPtrTy); 718 llvm::DIType VPTR 719 = DebugFactory.CreateDerivedType(llvm::dwarf::DW_TAG_member, Unit, 720 getVTableName(RD), Unit, 721 0, Size, 0, 0, 0, 722 getOrCreateVTablePtrType(Unit)); 723 EltTys.push_back(VPTR); 724} 725 726/// CreateType - get structure or union type. 727llvm::DIType CGDebugInfo::CreateType(const RecordType *Ty, 728 llvm::DIFile Unit) { 729 RecordDecl *RD = Ty->getDecl(); 730 731 unsigned Tag; 732 if (RD->isStruct()) 733 Tag = llvm::dwarf::DW_TAG_structure_type; 734 else if (RD->isUnion()) 735 Tag = llvm::dwarf::DW_TAG_union_type; 736 else { 737 assert(RD->isClass() && "Unknown RecordType!"); 738 Tag = llvm::dwarf::DW_TAG_class_type; 739 } 740 741 SourceManager &SM = CGM.getContext().getSourceManager(); 742 743 // Get overall information about the record type for the debug info. 744 PresumedLoc PLoc = SM.getPresumedLoc(RD->getLocation()); 745 llvm::DIFile DefUnit; 746 unsigned Line = 0; 747 if (!PLoc.isInvalid()) { 748 DefUnit = getOrCreateFile(RD->getLocation()); 749 Line = PLoc.getLine(); 750 } 751 752 // Records and classes and unions can all be recursive. To handle them, we 753 // first generate a debug descriptor for the struct as a forward declaration. 754 // Then (if it is a definition) we go through and get debug info for all of 755 // its members. Finally, we create a descriptor for the complete type (which 756 // may refer to the forward decl if the struct is recursive) and replace all 757 // uses of the forward declaration with the final definition. 758 759 // A RD->getName() is not unique. However, the debug info descriptors 760 // are uniqued so use type name to ensure uniquness. 761 llvm::SmallString<128> FwdDeclName; 762 llvm::raw_svector_ostream(FwdDeclName) << "fwd.type." << FwdDeclCount++; 763 llvm::DIDescriptor FDContext = 764 getContextDescriptor(dyn_cast<Decl>(RD->getDeclContext()), Unit); 765 llvm::DICompositeType FwdDecl = 766 DebugFactory.CreateCompositeType(Tag, FDContext, FwdDeclName, 767 DefUnit, Line, 0, 0, 0, 0, 768 llvm::DIType(), llvm::DIArray()); 769 770 // If this is just a forward declaration, return it. 771 if (!RD->getDefinition()) 772 return FwdDecl; 773 774 llvm::TrackingVH<llvm::MDNode> FwdDeclNode = FwdDecl.getNode(); 775 // Otherwise, insert it into the TypeCache so that recursive uses will find 776 // it. 777 TypeCache[QualType(Ty, 0).getAsOpaquePtr()] = FwdDecl.getNode(); 778 // Push the struct on region stack. 779 RegionStack.push_back(FwdDecl.getNode()); 780 RegionMap[Ty->getDecl()] = llvm::WeakVH(FwdDecl.getNode()); 781 782 // Convert all the elements. 783 llvm::SmallVector<llvm::DIDescriptor, 16> EltTys; 784 785 const CXXRecordDecl *CXXDecl = dyn_cast<CXXRecordDecl>(RD); 786 if (CXXDecl) { 787 CollectCXXBases(CXXDecl, Unit, EltTys, FwdDecl); 788 CollectVTableInfo(CXXDecl, Unit, EltTys); 789 } 790 CollectRecordFields(RD, Unit, EltTys); 791 llvm::MDNode *ContainingType = NULL; 792 if (CXXDecl) { 793 CollectCXXMemberFunctions(CXXDecl, Unit, EltTys, FwdDecl); 794 795 // A class's primary base or the class itself contains the vtable. 796 const ASTRecordLayout &RL = CGM.getContext().getASTRecordLayout(RD); 797 if (const CXXRecordDecl *PBase = RL.getPrimaryBase()) 798 ContainingType = 799 getOrCreateType(QualType(PBase->getTypeForDecl(), 0), Unit).getNode(); 800 else if (CXXDecl->isDynamicClass()) 801 ContainingType = FwdDecl.getNode(); 802 } 803 804 llvm::DIArray Elements = 805 DebugFactory.GetOrCreateArray(EltTys.data(), EltTys.size()); 806 807 // Bit size, align and offset of the type. 808 uint64_t Size = CGM.getContext().getTypeSize(Ty); 809 uint64_t Align = CGM.getContext().getTypeAlign(Ty); 810 811 RegionStack.pop_back(); 812 llvm::DenseMap<const Decl *, llvm::WeakVH>::iterator RI = 813 RegionMap.find(Ty->getDecl()); 814 if (RI != RegionMap.end()) 815 RegionMap.erase(RI); 816 817 llvm::DIDescriptor RDContext = 818 getContextDescriptor(dyn_cast<Decl>(RD->getDeclContext()), Unit); 819 llvm::DICompositeType RealDecl = 820 DebugFactory.CreateCompositeType(Tag, RDContext, 821 RD->getName(), 822 DefUnit, Line, Size, Align, 0, 0, 823 llvm::DIType(), Elements, 824 0, ContainingType); 825 826 // Now that we have a real decl for the struct, replace anything using the 827 // old decl with the new one. This will recursively update the debug info. 828 llvm::DIDerivedType(FwdDeclNode).replaceAllUsesWith(RealDecl); 829 RegionMap[RD] = llvm::WeakVH(RealDecl.getNode()); 830 return RealDecl; 831} 832 833/// CreateType - get objective-c interface type. 834llvm::DIType CGDebugInfo::CreateType(const ObjCInterfaceType *Ty, 835 llvm::DIFile Unit) { 836 ObjCInterfaceDecl *ID = Ty->getDecl(); 837 838 unsigned Tag = llvm::dwarf::DW_TAG_structure_type; 839 SourceManager &SM = CGM.getContext().getSourceManager(); 840 841 // Get overall information about the record type for the debug info. 842 llvm::DIFile DefUnit = getOrCreateFile(ID->getLocation()); 843 PresumedLoc PLoc = SM.getPresumedLoc(ID->getLocation()); 844 unsigned Line = PLoc.isInvalid() ? 0 : PLoc.getLine(); 845 846 847 unsigned RuntimeLang = TheCU.getLanguage(); 848 849 // To handle recursive interface, we 850 // first generate a debug descriptor for the struct as a forward declaration. 851 // Then (if it is a definition) we go through and get debug info for all of 852 // its members. Finally, we create a descriptor for the complete type (which 853 // may refer to the forward decl if the struct is recursive) and replace all 854 // uses of the forward declaration with the final definition. 855 llvm::DICompositeType FwdDecl = 856 DebugFactory.CreateCompositeType(Tag, Unit, ID->getName(), 857 DefUnit, Line, 0, 0, 0, 0, 858 llvm::DIType(), llvm::DIArray(), 859 RuntimeLang); 860 861 // If this is just a forward declaration, return it. 862 if (ID->isForwardDecl()) 863 return FwdDecl; 864 865 llvm::TrackingVH<llvm::MDNode> FwdDeclNode = FwdDecl.getNode(); 866 // Otherwise, insert it into the TypeCache so that recursive uses will find 867 // it. 868 TypeCache[QualType(Ty, 0).getAsOpaquePtr()] = FwdDecl.getNode(); 869 // Push the struct on region stack. 870 RegionStack.push_back(FwdDecl.getNode()); 871 RegionMap[Ty->getDecl()] = llvm::WeakVH(FwdDecl.getNode()); 872 873 // Convert all the elements. 874 llvm::SmallVector<llvm::DIDescriptor, 16> EltTys; 875 876 ObjCInterfaceDecl *SClass = ID->getSuperClass(); 877 if (SClass) { 878 llvm::DIType SClassTy = 879 getOrCreateType(CGM.getContext().getObjCInterfaceType(SClass), Unit); 880 llvm::DIType InhTag = 881 DebugFactory.CreateDerivedType(llvm::dwarf::DW_TAG_inheritance, 882 Unit, "", Unit, 0, 0, 0, 883 0 /* offset */, 0, SClassTy); 884 EltTys.push_back(InhTag); 885 } 886 887 const ASTRecordLayout &RL = CGM.getContext().getASTObjCInterfaceLayout(ID); 888 889 unsigned FieldNo = 0; 890 for (ObjCInterfaceDecl::ivar_iterator I = ID->ivar_begin(), 891 E = ID->ivar_end(); I != E; ++I, ++FieldNo) { 892 ObjCIvarDecl *Field = *I; 893 llvm::DIType FieldTy = getOrCreateType(Field->getType(), Unit); 894 895 llvm::StringRef FieldName = Field->getName(); 896 897 // Ignore unnamed fields. 898 if (FieldName.empty()) 899 continue; 900 901 // Get the location for the field. 902 SourceLocation FieldDefLoc = Field->getLocation(); 903 llvm::DIFile FieldDefUnit = getOrCreateFile(FieldDefLoc); 904 PresumedLoc PLoc = SM.getPresumedLoc(FieldDefLoc); 905 unsigned FieldLine = PLoc.isInvalid() ? 0 : PLoc.getLine(); 906 907 908 QualType FType = Field->getType(); 909 uint64_t FieldSize = 0; 910 unsigned FieldAlign = 0; 911 912 if (!FType->isIncompleteArrayType()) { 913 914 // Bit size, align and offset of the type. 915 FieldSize = CGM.getContext().getTypeSize(FType); 916 Expr *BitWidth = Field->getBitWidth(); 917 if (BitWidth) 918 FieldSize = BitWidth->EvaluateAsInt(CGM.getContext()).getZExtValue(); 919 920 FieldAlign = CGM.getContext().getTypeAlign(FType); 921 } 922 923 uint64_t FieldOffset = RL.getFieldOffset(FieldNo); 924 925 unsigned Flags = 0; 926 if (Field->getAccessControl() == ObjCIvarDecl::Protected) 927 Flags = llvm::DIType::FlagProtected; 928 else if (Field->getAccessControl() == ObjCIvarDecl::Private) 929 Flags = llvm::DIType::FlagPrivate; 930 931 // Create a DW_TAG_member node to remember the offset of this field in the 932 // struct. FIXME: This is an absolutely insane way to capture this 933 // information. When we gut debug info, this should be fixed. 934 FieldTy = DebugFactory.CreateDerivedType(llvm::dwarf::DW_TAG_member, Unit, 935 FieldName, FieldDefUnit, 936 FieldLine, FieldSize, FieldAlign, 937 FieldOffset, Flags, FieldTy); 938 EltTys.push_back(FieldTy); 939 } 940 941 llvm::DIArray Elements = 942 DebugFactory.GetOrCreateArray(EltTys.data(), EltTys.size()); 943 944 RegionStack.pop_back(); 945 llvm::DenseMap<const Decl *, llvm::WeakVH>::iterator RI = 946 RegionMap.find(Ty->getDecl()); 947 if (RI != RegionMap.end()) 948 RegionMap.erase(RI); 949 950 // Bit size, align and offset of the type. 951 uint64_t Size = CGM.getContext().getTypeSize(Ty); 952 uint64_t Align = CGM.getContext().getTypeAlign(Ty); 953 954 llvm::DICompositeType RealDecl = 955 DebugFactory.CreateCompositeType(Tag, Unit, ID->getName(), DefUnit, 956 Line, Size, Align, 0, 0, llvm::DIType(), 957 Elements, RuntimeLang); 958 959 // Now that we have a real decl for the struct, replace anything using the 960 // old decl with the new one. This will recursively update the debug info. 961 llvm::DIDerivedType(FwdDeclNode).replaceAllUsesWith(RealDecl); 962 RegionMap[ID] = llvm::WeakVH(RealDecl.getNode()); 963 964 return RealDecl; 965} 966 967llvm::DIType CGDebugInfo::CreateType(const EnumType *Ty, 968 llvm::DIFile Unit) { 969 EnumDecl *ED = Ty->getDecl(); 970 971 llvm::SmallVector<llvm::DIDescriptor, 32> Enumerators; 972 973 // Create DIEnumerator elements for each enumerator. 974 for (EnumDecl::enumerator_iterator 975 Enum = ED->enumerator_begin(), EnumEnd = ED->enumerator_end(); 976 Enum != EnumEnd; ++Enum) { 977 Enumerators.push_back(DebugFactory.CreateEnumerator(Enum->getName(), 978 Enum->getInitVal().getZExtValue())); 979 } 980 981 // Return a CompositeType for the enum itself. 982 llvm::DIArray EltArray = 983 DebugFactory.GetOrCreateArray(Enumerators.data(), Enumerators.size()); 984 985 SourceLocation DefLoc = ED->getLocation(); 986 llvm::DIFile DefUnit = getOrCreateFile(DefLoc); 987 SourceManager &SM = CGM.getContext().getSourceManager(); 988 PresumedLoc PLoc = SM.getPresumedLoc(DefLoc); 989 unsigned Line = PLoc.isInvalid() ? 0 : PLoc.getLine(); 990 991 992 // Size and align of the type. 993 uint64_t Size = 0; 994 unsigned Align = 0; 995 if (!Ty->isIncompleteType()) { 996 Size = CGM.getContext().getTypeSize(Ty); 997 Align = CGM.getContext().getTypeAlign(Ty); 998 } 999 1000 llvm::DIType DbgTy = 1001 DebugFactory.CreateCompositeType(llvm::dwarf::DW_TAG_enumeration_type, 1002 Unit, ED->getName(), DefUnit, Line, 1003 Size, Align, 0, 0, 1004 llvm::DIType(), EltArray); 1005 return DbgTy; 1006} 1007 1008llvm::DIType CGDebugInfo::CreateType(const TagType *Ty, 1009 llvm::DIFile Unit) { 1010 if (const RecordType *RT = dyn_cast<RecordType>(Ty)) 1011 return CreateType(RT, Unit); 1012 else if (const EnumType *ET = dyn_cast<EnumType>(Ty)) 1013 return CreateType(ET, Unit); 1014 1015 return llvm::DIType(); 1016} 1017 1018llvm::DIType CGDebugInfo::CreateType(const VectorType *Ty, 1019 llvm::DIFile Unit) { 1020 llvm::DIType ElementTy = getOrCreateType(Ty->getElementType(), Unit); 1021 uint64_t NumElems = Ty->getNumElements(); 1022 if (NumElems > 0) 1023 --NumElems; 1024 1025 llvm::DIDescriptor Subscript = DebugFactory.GetOrCreateSubrange(0, NumElems); 1026 llvm::DIArray SubscriptArray = DebugFactory.GetOrCreateArray(&Subscript, 1); 1027 1028 uint64_t Size = CGM.getContext().getTypeSize(Ty); 1029 uint64_t Align = CGM.getContext().getTypeAlign(Ty); 1030 1031 return 1032 DebugFactory.CreateCompositeType(llvm::dwarf::DW_TAG_vector_type, 1033 Unit, "", Unit, 1034 0, Size, Align, 0, 0, 1035 ElementTy, SubscriptArray); 1036} 1037 1038llvm::DIType CGDebugInfo::CreateType(const ArrayType *Ty, 1039 llvm::DIFile Unit) { 1040 uint64_t Size; 1041 uint64_t Align; 1042 1043 1044 // FIXME: make getTypeAlign() aware of VLAs and incomplete array types 1045 if (const VariableArrayType *VAT = dyn_cast<VariableArrayType>(Ty)) { 1046 Size = 0; 1047 Align = 1048 CGM.getContext().getTypeAlign(CGM.getContext().getBaseElementType(VAT)); 1049 } else if (Ty->isIncompleteArrayType()) { 1050 Size = 0; 1051 Align = CGM.getContext().getTypeAlign(Ty->getElementType()); 1052 } else { 1053 // Size and align of the whole array, not the element type. 1054 Size = CGM.getContext().getTypeSize(Ty); 1055 Align = CGM.getContext().getTypeAlign(Ty); 1056 } 1057 1058 // Add the dimensions of the array. FIXME: This loses CV qualifiers from 1059 // interior arrays, do we care? Why aren't nested arrays represented the 1060 // obvious/recursive way? 1061 llvm::SmallVector<llvm::DIDescriptor, 8> Subscripts; 1062 QualType EltTy(Ty, 0); 1063 while ((Ty = dyn_cast<ArrayType>(EltTy))) { 1064 uint64_t Upper = 0; 1065 if (const ConstantArrayType *CAT = dyn_cast<ConstantArrayType>(Ty)) 1066 if (CAT->getSize().getZExtValue()) 1067 Upper = CAT->getSize().getZExtValue() - 1; 1068 // FIXME: Verify this is right for VLAs. 1069 Subscripts.push_back(DebugFactory.GetOrCreateSubrange(0, Upper)); 1070 EltTy = Ty->getElementType(); 1071 } 1072 1073 llvm::DIArray SubscriptArray = 1074 DebugFactory.GetOrCreateArray(Subscripts.data(), Subscripts.size()); 1075 1076 llvm::DIType DbgTy = 1077 DebugFactory.CreateCompositeType(llvm::dwarf::DW_TAG_array_type, 1078 Unit, "", Unit, 1079 0, Size, Align, 0, 0, 1080 getOrCreateType(EltTy, Unit), 1081 SubscriptArray); 1082 return DbgTy; 1083} 1084 1085llvm::DIType CGDebugInfo::CreateType(const LValueReferenceType *Ty, 1086 llvm::DIFile Unit) { 1087 return CreatePointerLikeType(llvm::dwarf::DW_TAG_reference_type, 1088 Ty, Ty->getPointeeType(), Unit); 1089} 1090 1091llvm::DIType CGDebugInfo::CreateType(const MemberPointerType *Ty, 1092 llvm::DIFile U) { 1093 QualType PointerDiffTy = CGM.getContext().getPointerDiffType(); 1094 llvm::DIType PointerDiffDITy = getOrCreateType(PointerDiffTy, U); 1095 1096 if (!Ty->getPointeeType()->isFunctionType()) { 1097 // We have a data member pointer type. 1098 return PointerDiffDITy; 1099 } 1100 1101 // We have a member function pointer type. Treat it as a struct with two 1102 // ptrdiff_t members. 1103 std::pair<uint64_t, unsigned> Info = CGM.getContext().getTypeInfo(Ty); 1104 1105 uint64_t FieldOffset = 0; 1106 llvm::DIDescriptor ElementTypes[2]; 1107 1108 // FIXME: This should probably be a function type instead. 1109 ElementTypes[0] = 1110 DebugFactory.CreateDerivedType(llvm::dwarf::DW_TAG_member, U, 1111 "ptr", U, 0, 1112 Info.first, Info.second, FieldOffset, 0, 1113 PointerDiffDITy); 1114 FieldOffset += Info.first; 1115 1116 ElementTypes[1] = 1117 DebugFactory.CreateDerivedType(llvm::dwarf::DW_TAG_member, U, 1118 "ptr", U, 0, 1119 Info.first, Info.second, FieldOffset, 0, 1120 PointerDiffDITy); 1121 1122 llvm::DIArray Elements = 1123 DebugFactory.GetOrCreateArray(&ElementTypes[0], 1124 llvm::array_lengthof(ElementTypes)); 1125 1126 return DebugFactory.CreateCompositeType(llvm::dwarf::DW_TAG_structure_type, 1127 U, llvm::StringRef("test"), 1128 U, 0, FieldOffset, 1129 0, 0, 0, llvm::DIType(), Elements); 1130} 1131 1132static QualType UnwrapTypeForDebugInfo(QualType T) { 1133 do { 1134 QualType LastT = T; 1135 switch (T->getTypeClass()) { 1136 default: 1137 return T; 1138 case Type::TemplateSpecialization: 1139 T = cast<TemplateSpecializationType>(T)->desugar(); 1140 break; 1141 case Type::TypeOfExpr: { 1142 TypeOfExprType *Ty = cast<TypeOfExprType>(T); 1143 T = Ty->getUnderlyingExpr()->getType(); 1144 break; 1145 } 1146 case Type::TypeOf: 1147 T = cast<TypeOfType>(T)->getUnderlyingType(); 1148 break; 1149 case Type::Decltype: 1150 T = cast<DecltypeType>(T)->getUnderlyingType(); 1151 break; 1152 case Type::QualifiedName: 1153 T = cast<QualifiedNameType>(T)->getNamedType(); 1154 break; 1155 case Type::SubstTemplateTypeParm: 1156 T = cast<SubstTemplateTypeParmType>(T)->getReplacementType(); 1157 break; 1158 case Type::Elaborated: 1159 T = cast<ElaboratedType>(T)->getUnderlyingType(); 1160 break; 1161 } 1162 1163 assert(T != LastT && "Type unwrapping failed to unwrap!"); 1164 if (T == LastT) 1165 return T; 1166 } while (true); 1167 1168 return T; 1169} 1170 1171/// getOrCreateType - Get the type from the cache or create a new 1172/// one if necessary. 1173llvm::DIType CGDebugInfo::getOrCreateType(QualType Ty, 1174 llvm::DIFile Unit) { 1175 if (Ty.isNull()) 1176 return llvm::DIType(); 1177 1178 // Unwrap the type as needed for debug information. 1179 Ty = UnwrapTypeForDebugInfo(Ty); 1180 1181 // Check for existing entry. 1182 llvm::DenseMap<void *, llvm::WeakVH>::iterator it = 1183 TypeCache.find(Ty.getAsOpaquePtr()); 1184 if (it != TypeCache.end()) { 1185 // Verify that the debug info still exists. 1186 if (&*it->second) 1187 return llvm::DIType(cast<llvm::MDNode>(it->second)); 1188 } 1189 1190 // Otherwise create the type. 1191 llvm::DIType Res = CreateTypeNode(Ty, Unit); 1192 1193 // And update the type cache. 1194 TypeCache[Ty.getAsOpaquePtr()] = Res.getNode(); 1195 return Res; 1196} 1197 1198/// CreateTypeNode - Create a new debug type node. 1199llvm::DIType CGDebugInfo::CreateTypeNode(QualType Ty, 1200 llvm::DIFile Unit) { 1201 // Handle qualifiers, which recursively handles what they refer to. 1202 if (Ty.hasLocalQualifiers()) 1203 return CreateQualifiedType(Ty, Unit); 1204 1205 const char *Diag = 0; 1206 1207 // Work out details of type. 1208 switch (Ty->getTypeClass()) { 1209#define TYPE(Class, Base) 1210#define ABSTRACT_TYPE(Class, Base) 1211#define NON_CANONICAL_TYPE(Class, Base) 1212#define DEPENDENT_TYPE(Class, Base) case Type::Class: 1213#include "clang/AST/TypeNodes.def" 1214 assert(false && "Dependent types cannot show up in debug information"); 1215 1216 // FIXME: Handle these. 1217 case Type::ExtVector: 1218 return llvm::DIType(); 1219 1220 case Type::Vector: 1221 return CreateType(cast<VectorType>(Ty), Unit); 1222 case Type::ObjCObjectPointer: 1223 return CreateType(cast<ObjCObjectPointerType>(Ty), Unit); 1224 case Type::ObjCInterface: 1225 return CreateType(cast<ObjCInterfaceType>(Ty), Unit); 1226 case Type::Builtin: return CreateType(cast<BuiltinType>(Ty), Unit); 1227 case Type::Complex: return CreateType(cast<ComplexType>(Ty), Unit); 1228 case Type::Pointer: return CreateType(cast<PointerType>(Ty), Unit); 1229 case Type::BlockPointer: 1230 return CreateType(cast<BlockPointerType>(Ty), Unit); 1231 case Type::Typedef: return CreateType(cast<TypedefType>(Ty), Unit); 1232 case Type::Record: 1233 case Type::Enum: 1234 return CreateType(cast<TagType>(Ty), Unit); 1235 case Type::FunctionProto: 1236 case Type::FunctionNoProto: 1237 return CreateType(cast<FunctionType>(Ty), Unit); 1238 case Type::ConstantArray: 1239 case Type::VariableArray: 1240 case Type::IncompleteArray: 1241 return CreateType(cast<ArrayType>(Ty), Unit); 1242 1243 case Type::LValueReference: 1244 return CreateType(cast<LValueReferenceType>(Ty), Unit); 1245 1246 case Type::MemberPointer: 1247 return CreateType(cast<MemberPointerType>(Ty), Unit); 1248 1249 case Type::InjectedClassName: 1250 case Type::TemplateSpecialization: 1251 case Type::Elaborated: 1252 case Type::QualifiedName: 1253 case Type::SubstTemplateTypeParm: 1254 case Type::TypeOfExpr: 1255 case Type::TypeOf: 1256 case Type::Decltype: 1257 llvm_unreachable("type should have been unwrapped!"); 1258 return llvm::DIType(); 1259 1260 case Type::RValueReference: 1261 // FIXME: Implement! 1262 Diag = "rvalue references"; 1263 break; 1264 } 1265 1266 assert(Diag && "Fall through without a diagnostic?"); 1267 unsigned DiagID = CGM.getDiags().getCustomDiagID(Diagnostic::Error, 1268 "debug information for %0 is not yet supported"); 1269 CGM.getDiags().Report(FullSourceLoc(), DiagID) 1270 << Diag; 1271 return llvm::DIType(); 1272} 1273 1274/// CreateMemberType - Create new member and increase Offset by FType's size. 1275llvm::DIType CGDebugInfo::CreateMemberType(llvm::DIFile Unit, QualType FType, 1276 llvm::StringRef Name, 1277 uint64_t *Offset) { 1278 llvm::DIType FieldTy = CGDebugInfo::getOrCreateType(FType, Unit); 1279 uint64_t FieldSize = CGM.getContext().getTypeSize(FType); 1280 unsigned FieldAlign = CGM.getContext().getTypeAlign(FType); 1281 llvm::DIType Ty = DebugFactory.CreateDerivedType(llvm::dwarf::DW_TAG_member, 1282 Unit, Name, Unit, 0, 1283 FieldSize, FieldAlign, 1284 *Offset, 0, FieldTy); 1285 *Offset += FieldSize; 1286 return Ty; 1287} 1288 1289/// EmitFunctionStart - Constructs the debug code for entering a function - 1290/// "llvm.dbg.func.start.". 1291void CGDebugInfo::EmitFunctionStart(GlobalDecl GD, QualType FnType, 1292 llvm::Function *Fn, 1293 CGBuilderTy &Builder) { 1294 1295 llvm::StringRef Name; 1296 MangleBuffer LinkageName; 1297 1298 const Decl *D = GD.getDecl(); 1299 if (const FunctionDecl *FD = dyn_cast<FunctionDecl>(D)) { 1300 // If there is a DISubprogram for this function available then use it. 1301 llvm::DenseMap<const FunctionDecl *, llvm::WeakVH>::iterator 1302 FI = SPCache.find(FD); 1303 if (FI != SPCache.end()) { 1304 llvm::DIDescriptor SP(dyn_cast_or_null<llvm::MDNode>(FI->second)); 1305 if (SP.isSubprogram() && llvm::DISubprogram(SP.getNode()).isDefinition()) { 1306 RegionStack.push_back(SP.getNode()); 1307 RegionMap[D] = llvm::WeakVH(SP.getNode()); 1308 return; 1309 } 1310 } 1311 Name = getFunctionName(FD); 1312 // Use mangled name as linkage name for c/c++ functions. 1313 CGM.getMangledName(LinkageName, GD); 1314 } else { 1315 // Use llvm function name as linkage name. 1316 Name = Fn->getName(); 1317 LinkageName.setString(Name); 1318 } 1319 if (!Name.empty() && Name[0] == '\01') 1320 Name = Name.substr(1); 1321 1322 // It is expected that CurLoc is set before using EmitFunctionStart. 1323 // Usually, CurLoc points to the left bracket location of compound 1324 // statement representing function body. 1325 llvm::DIFile Unit = getOrCreateFile(CurLoc); 1326 SourceManager &SM = CGM.getContext().getSourceManager(); 1327 unsigned LineNo = SM.getPresumedLoc(CurLoc).getLine(); 1328 1329 llvm::DISubprogram SP = 1330 DebugFactory.CreateSubprogram(Unit, Name, Name, LinkageName, Unit, LineNo, 1331 getOrCreateType(FnType, Unit), 1332 Fn->hasInternalLinkage(), true/*definition*/); 1333 1334 // Push function on region stack. 1335 RegionStack.push_back(SP.getNode()); 1336 RegionMap[D] = llvm::WeakVH(SP.getNode()); 1337} 1338 1339 1340void CGDebugInfo::EmitStopPoint(llvm::Function *Fn, CGBuilderTy &Builder) { 1341 if (CurLoc.isInvalid() || CurLoc.isMacroID()) return; 1342 1343 // Don't bother if things are the same as last time. 1344 SourceManager &SM = CGM.getContext().getSourceManager(); 1345 if (CurLoc == PrevLoc 1346 || (SM.getInstantiationLineNumber(CurLoc) == 1347 SM.getInstantiationLineNumber(PrevLoc) 1348 && SM.isFromSameFile(CurLoc, PrevLoc))) 1349 // New Builder may not be in sync with CGDebugInfo. 1350 if (!Builder.getCurrentDebugLocation().isUnknown()) 1351 return; 1352 1353 // Update last state. 1354 PrevLoc = CurLoc; 1355 1356 // Get the appropriate compile unit. 1357 llvm::DIFile Unit = getOrCreateFile(CurLoc); 1358 PresumedLoc PLoc = SM.getPresumedLoc(CurLoc); 1359 1360 llvm::MDNode *Scope = RegionStack.back(); 1361 Builder.SetCurrentDebugLocation(llvm::DebugLoc::get(PLoc.getLine(), 1362 PLoc.getColumn(), 1363 Scope)); 1364} 1365 1366/// EmitRegionStart- Constructs the debug code for entering a declarative 1367/// region - "llvm.dbg.region.start.". 1368void CGDebugInfo::EmitRegionStart(llvm::Function *Fn, CGBuilderTy &Builder) { 1369 SourceManager &SM = CGM.getContext().getSourceManager(); 1370 PresumedLoc PLoc = SM.getPresumedLoc(CurLoc); 1371 llvm::DIDescriptor D = 1372 DebugFactory.CreateLexicalBlock(RegionStack.empty() ? 1373 llvm::DIDescriptor() : 1374 llvm::DIDescriptor(RegionStack.back()), 1375 PLoc.getLine(), PLoc.getColumn()); 1376 RegionStack.push_back(D.getNode()); 1377} 1378 1379/// EmitRegionEnd - Constructs the debug code for exiting a declarative 1380/// region - "llvm.dbg.region.end." 1381void CGDebugInfo::EmitRegionEnd(llvm::Function *Fn, CGBuilderTy &Builder) { 1382 assert(!RegionStack.empty() && "Region stack mismatch, stack empty!"); 1383 1384 // Provide an region stop point. 1385 EmitStopPoint(Fn, Builder); 1386 1387 RegionStack.pop_back(); 1388} 1389 1390// EmitTypeForVarWithBlocksAttr - Build up structure info for the byref. 1391// See BuildByRefType. 1392llvm::DIType CGDebugInfo::EmitTypeForVarWithBlocksAttr(const ValueDecl *VD, 1393 uint64_t *XOffset) { 1394 1395 llvm::SmallVector<llvm::DIDescriptor, 5> EltTys; 1396 1397 QualType FType; 1398 uint64_t FieldSize, FieldOffset; 1399 unsigned FieldAlign; 1400 1401 llvm::DIFile Unit = getOrCreateFile(VD->getLocation()); 1402 QualType Type = VD->getType(); 1403 1404 FieldOffset = 0; 1405 FType = CGM.getContext().getPointerType(CGM.getContext().VoidTy); 1406 EltTys.push_back(CreateMemberType(Unit, FType, "__isa", &FieldOffset)); 1407 EltTys.push_back(CreateMemberType(Unit, FType, "__forwarding", &FieldOffset)); 1408 FType = CGM.getContext().IntTy; 1409 EltTys.push_back(CreateMemberType(Unit, FType, "__flags", &FieldOffset)); 1410 EltTys.push_back(CreateMemberType(Unit, FType, "__size", &FieldOffset)); 1411 1412 bool HasCopyAndDispose = CGM.BlockRequiresCopying(Type); 1413 if (HasCopyAndDispose) { 1414 FType = CGM.getContext().getPointerType(CGM.getContext().VoidTy); 1415 EltTys.push_back(CreateMemberType(Unit, FType, "__copy_helper", 1416 &FieldOffset)); 1417 EltTys.push_back(CreateMemberType(Unit, FType, "__destroy_helper", 1418 &FieldOffset)); 1419 } 1420 1421 CharUnits Align = CGM.getContext().getDeclAlign(VD); 1422 if (Align > CharUnits::fromQuantity( 1423 CGM.getContext().Target.getPointerAlign(0) / 8)) { 1424 unsigned AlignedOffsetInBytes 1425 = llvm::RoundUpToAlignment(FieldOffset/8, Align.getQuantity()); 1426 unsigned NumPaddingBytes 1427 = AlignedOffsetInBytes - FieldOffset/8; 1428 1429 if (NumPaddingBytes > 0) { 1430 llvm::APInt pad(32, NumPaddingBytes); 1431 FType = CGM.getContext().getConstantArrayType(CGM.getContext().CharTy, 1432 pad, ArrayType::Normal, 0); 1433 EltTys.push_back(CreateMemberType(Unit, FType, "", &FieldOffset)); 1434 } 1435 } 1436 1437 FType = Type; 1438 llvm::DIType FieldTy = CGDebugInfo::getOrCreateType(FType, Unit); 1439 FieldSize = CGM.getContext().getTypeSize(FType); 1440 FieldAlign = Align.getQuantity()*8; 1441 1442 *XOffset = FieldOffset; 1443 FieldTy = DebugFactory.CreateDerivedType(llvm::dwarf::DW_TAG_member, Unit, 1444 VD->getName(), Unit, 1445 0, FieldSize, FieldAlign, 1446 FieldOffset, 0, FieldTy); 1447 EltTys.push_back(FieldTy); 1448 FieldOffset += FieldSize; 1449 1450 llvm::DIArray Elements = 1451 DebugFactory.GetOrCreateArray(EltTys.data(), EltTys.size()); 1452 1453 unsigned Flags = llvm::DIType::FlagBlockByrefStruct; 1454 1455 return DebugFactory.CreateCompositeType(llvm::dwarf::DW_TAG_structure_type, 1456 Unit, "", Unit, 1457 0, FieldOffset, 0, 0, Flags, 1458 llvm::DIType(), Elements); 1459 1460} 1461/// EmitDeclare - Emit local variable declaration debug info. 1462void CGDebugInfo::EmitDeclare(const VarDecl *VD, unsigned Tag, 1463 llvm::Value *Storage, CGBuilderTy &Builder) { 1464 assert(!RegionStack.empty() && "Region stack mismatch, stack empty!"); 1465 1466 // Do not emit variable debug information while generating optimized code. 1467 // The llvm optimizer and code generator are not yet ready to support 1468 // optimized code debugging. 1469 const CodeGenOptions &CGO = CGM.getCodeGenOpts(); 1470 if (CGO.OptimizationLevel) 1471 return; 1472 1473 llvm::DIFile Unit = getOrCreateFile(VD->getLocation()); 1474 llvm::DIType Ty; 1475 uint64_t XOffset = 0; 1476 if (VD->hasAttr<BlocksAttr>()) 1477 Ty = EmitTypeForVarWithBlocksAttr(VD, &XOffset); 1478 else 1479 Ty = getOrCreateType(VD->getType(), Unit); 1480 1481 // Get location information. 1482 SourceManager &SM = CGM.getContext().getSourceManager(); 1483 PresumedLoc PLoc = SM.getPresumedLoc(VD->getLocation()); 1484 unsigned Line = 0; 1485 unsigned Column = 0; 1486 if (PLoc.isInvalid()) 1487 PLoc = SM.getPresumedLoc(CurLoc); 1488 if (PLoc.isValid()) { 1489 Line = PLoc.getLine(); 1490 Column = PLoc.getColumn(); 1491 Unit = getOrCreateFile(CurLoc); 1492 } else { 1493 Unit = llvm::DIFile(); 1494 } 1495 1496 // Create the descriptor for the variable. 1497 llvm::DIVariable D = 1498 DebugFactory.CreateVariable(Tag, llvm::DIDescriptor(RegionStack.back()), 1499 VD->getName(), 1500 Unit, Line, Ty); 1501 // Insert an llvm.dbg.declare into the current block. 1502 llvm::Instruction *Call = 1503 DebugFactory.InsertDeclare(Storage, D, Builder.GetInsertBlock()); 1504 1505 llvm::MDNode *Scope = RegionStack.back(); 1506 Call->setDebugLoc(llvm::DebugLoc::get(Line, Column, Scope)); 1507} 1508 1509/// EmitDeclare - Emit local variable declaration debug info. 1510void CGDebugInfo::EmitDeclare(const BlockDeclRefExpr *BDRE, unsigned Tag, 1511 llvm::Value *Storage, CGBuilderTy &Builder, 1512 CodeGenFunction *CGF) { 1513 const ValueDecl *VD = BDRE->getDecl(); 1514 assert(!RegionStack.empty() && "Region stack mismatch, stack empty!"); 1515 1516 // Do not emit variable debug information while generating optimized code. 1517 // The llvm optimizer and code generator are not yet ready to support 1518 // optimized code debugging. 1519 const CodeGenOptions &CGO = CGM.getCodeGenOpts(); 1520 if (CGO.OptimizationLevel || Builder.GetInsertBlock() == 0) 1521 return; 1522 1523 uint64_t XOffset = 0; 1524 llvm::DIFile Unit = getOrCreateFile(VD->getLocation()); 1525 llvm::DIType Ty; 1526 if (VD->hasAttr<BlocksAttr>()) 1527 Ty = EmitTypeForVarWithBlocksAttr(VD, &XOffset); 1528 else 1529 Ty = getOrCreateType(VD->getType(), Unit); 1530 1531 // Get location information. 1532 SourceManager &SM = CGM.getContext().getSourceManager(); 1533 PresumedLoc PLoc = SM.getPresumedLoc(VD->getLocation()); 1534 unsigned Line = 0; 1535 if (!PLoc.isInvalid()) 1536 Line = PLoc.getLine(); 1537 else 1538 Unit = llvm::DIFile(); 1539 1540 CharUnits offset = CGF->BlockDecls[VD]; 1541 llvm::SmallVector<llvm::Value *, 9> addr; 1542 const llvm::Type *Int64Ty = llvm::Type::getInt64Ty(CGM.getLLVMContext()); 1543 addr.push_back(llvm::ConstantInt::get(Int64Ty, llvm::DIFactory::OpDeref)); 1544 addr.push_back(llvm::ConstantInt::get(Int64Ty, llvm::DIFactory::OpPlus)); 1545 addr.push_back(llvm::ConstantInt::get(Int64Ty, offset.getQuantity())); 1546 if (BDRE->isByRef()) { 1547 addr.push_back(llvm::ConstantInt::get(Int64Ty, llvm::DIFactory::OpDeref)); 1548 addr.push_back(llvm::ConstantInt::get(Int64Ty, llvm::DIFactory::OpPlus)); 1549 // offset of __forwarding field 1550 offset = CharUnits::fromQuantity(CGF->LLVMPointerWidth/8); 1551 addr.push_back(llvm::ConstantInt::get(Int64Ty, offset.getQuantity())); 1552 addr.push_back(llvm::ConstantInt::get(Int64Ty, llvm::DIFactory::OpDeref)); 1553 addr.push_back(llvm::ConstantInt::get(Int64Ty, llvm::DIFactory::OpPlus)); 1554 // offset of x field 1555 offset = CharUnits::fromQuantity(XOffset/8); 1556 addr.push_back(llvm::ConstantInt::get(Int64Ty, offset.getQuantity())); 1557 } 1558 1559 // Create the descriptor for the variable. 1560 llvm::DIVariable D = 1561 DebugFactory.CreateComplexVariable(Tag, 1562 llvm::DIDescriptor(RegionStack.back()), 1563 VD->getName(), Unit, Line, Ty, 1564 addr); 1565 // Insert an llvm.dbg.declare into the current block. 1566 llvm::Instruction *Call = 1567 DebugFactory.InsertDeclare(Storage, D, Builder.GetInsertBlock()); 1568 1569 llvm::MDNode *Scope = RegionStack.back(); 1570 Call->setDebugLoc(llvm::DebugLoc::get(Line, PLoc.getColumn(), Scope)); 1571} 1572 1573void CGDebugInfo::EmitDeclareOfAutoVariable(const VarDecl *VD, 1574 llvm::Value *Storage, 1575 CGBuilderTy &Builder) { 1576 EmitDeclare(VD, llvm::dwarf::DW_TAG_auto_variable, Storage, Builder); 1577} 1578 1579void CGDebugInfo::EmitDeclareOfBlockDeclRefVariable( 1580 const BlockDeclRefExpr *BDRE, llvm::Value *Storage, CGBuilderTy &Builder, 1581 CodeGenFunction *CGF) { 1582 EmitDeclare(BDRE, llvm::dwarf::DW_TAG_auto_variable, Storage, Builder, CGF); 1583} 1584 1585/// EmitDeclareOfArgVariable - Emit call to llvm.dbg.declare for an argument 1586/// variable declaration. 1587void CGDebugInfo::EmitDeclareOfArgVariable(const VarDecl *VD, llvm::Value *AI, 1588 CGBuilderTy &Builder) { 1589 EmitDeclare(VD, llvm::dwarf::DW_TAG_arg_variable, AI, Builder); 1590} 1591 1592 1593 1594/// EmitGlobalVariable - Emit information about a global variable. 1595void CGDebugInfo::EmitGlobalVariable(llvm::GlobalVariable *Var, 1596 const VarDecl *D) { 1597 1598 // Create global variable debug descriptor. 1599 llvm::DIFile Unit = getOrCreateFile(D->getLocation()); 1600 SourceManager &SM = CGM.getContext().getSourceManager(); 1601 PresumedLoc PLoc = SM.getPresumedLoc(D->getLocation()); 1602 unsigned LineNo = PLoc.isInvalid() ? 0 : PLoc.getLine(); 1603 1604 QualType T = D->getType(); 1605 if (T->isIncompleteArrayType()) { 1606 1607 // CodeGen turns int[] into int[1] so we'll do the same here. 1608 llvm::APSInt ConstVal(32); 1609 1610 ConstVal = 1; 1611 QualType ET = CGM.getContext().getAsArrayType(T)->getElementType(); 1612 1613 T = CGM.getContext().getConstantArrayType(ET, ConstVal, 1614 ArrayType::Normal, 0); 1615 } 1616 llvm::StringRef DeclName = Var->getName(); 1617 llvm::DIDescriptor DContext = 1618 getContextDescriptor(dyn_cast<Decl>(D->getDeclContext()), Unit); 1619 DebugFactory.CreateGlobalVariable(DContext, DeclName, 1620 DeclName, llvm::StringRef(), Unit, LineNo, 1621 getOrCreateType(T, Unit), 1622 Var->hasInternalLinkage(), 1623 true/*definition*/, Var); 1624} 1625 1626/// EmitGlobalVariable - Emit information about an objective-c interface. 1627void CGDebugInfo::EmitGlobalVariable(llvm::GlobalVariable *Var, 1628 ObjCInterfaceDecl *ID) { 1629 // Create global variable debug descriptor. 1630 llvm::DIFile Unit = getOrCreateFile(ID->getLocation()); 1631 SourceManager &SM = CGM.getContext().getSourceManager(); 1632 PresumedLoc PLoc = SM.getPresumedLoc(ID->getLocation()); 1633 unsigned LineNo = PLoc.isInvalid() ? 0 : PLoc.getLine(); 1634 1635 llvm::StringRef Name = ID->getName(); 1636 1637 QualType T = CGM.getContext().getObjCInterfaceType(ID); 1638 if (T->isIncompleteArrayType()) { 1639 1640 // CodeGen turns int[] into int[1] so we'll do the same here. 1641 llvm::APSInt ConstVal(32); 1642 1643 ConstVal = 1; 1644 QualType ET = CGM.getContext().getAsArrayType(T)->getElementType(); 1645 1646 T = CGM.getContext().getConstantArrayType(ET, ConstVal, 1647 ArrayType::Normal, 0); 1648 } 1649 1650 DebugFactory.CreateGlobalVariable(Unit, Name, Name, Name, Unit, LineNo, 1651 getOrCreateType(T, Unit), 1652 Var->hasInternalLinkage(), 1653 true/*definition*/, Var); 1654} 1655 1656/// getOrCreateNamesSpace - Return namespace descriptor for the given 1657/// namespace decl. 1658llvm::DINameSpace 1659CGDebugInfo::getOrCreateNameSpace(const NamespaceDecl *NSDecl, 1660 llvm::DIDescriptor Unit) { 1661 llvm::DenseMap<const NamespaceDecl *, llvm::WeakVH>::iterator I = 1662 NameSpaceCache.find(NSDecl); 1663 if (I != NameSpaceCache.end()) 1664 return llvm::DINameSpace(cast<llvm::MDNode>(I->second)); 1665 1666 SourceManager &SM = CGM.getContext().getSourceManager(); 1667 PresumedLoc PLoc = SM.getPresumedLoc(NSDecl->getLocation()); 1668 unsigned LineNo = PLoc.isInvalid() ? 0 : PLoc.getLine(); 1669 1670 llvm::DIDescriptor Context = 1671 getContextDescriptor(dyn_cast<Decl>(NSDecl->getDeclContext()), Unit); 1672 llvm::DINameSpace NS = 1673 DebugFactory.CreateNameSpace(Context, NSDecl->getName(), 1674 llvm::DIFile(Unit.getNode()), LineNo); 1675 NameSpaceCache[NSDecl] = llvm::WeakVH(NS.getNode()); 1676 return NS; 1677} 1678