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