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