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