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