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