CGDebugInfo.cpp revision 998316908cf1f0d50257dcaf60dc8154a1e294c9
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 "CodeGenModule.h" 16#include "clang/AST/ASTContext.h" 17#include "clang/AST/DeclObjC.h" 18#include "clang/AST/Expr.h" 19#include "clang/AST/RecordLayout.h" 20#include "clang/Basic/SourceManager.h" 21#include "clang/Basic/FileManager.h" 22#include "clang/Frontend/CompileOptions.h" 23#include "llvm/Constants.h" 24#include "llvm/DerivedTypes.h" 25#include "llvm/Instructions.h" 26#include "llvm/Intrinsics.h" 27#include "llvm/Module.h" 28#include "llvm/ADT/StringExtras.h" 29#include "llvm/ADT/SmallVector.h" 30#include "llvm/Support/Dwarf.h" 31#include "llvm/System/Path.h" 32#include "llvm/Target/TargetMachine.h" 33using namespace clang; 34using namespace clang::CodeGen; 35 36CGDebugInfo::CGDebugInfo(CodeGenModule *m) 37 : M(m), isMainCompileUnitCreated(false), DebugFactory(M->getModule()), 38 BlockLiteralGenericSet(false) { 39} 40 41CGDebugInfo::~CGDebugInfo() { 42 assert(RegionStack.empty() && "Region stack mismatch, stack not empty!"); 43} 44 45void CGDebugInfo::setLocation(SourceLocation Loc) { 46 if (Loc.isValid()) 47 CurLoc = M->getContext().getSourceManager().getInstantiationLoc(Loc); 48} 49 50/// getOrCreateCompileUnit - Get the compile unit from the cache or create a new 51/// one if necessary. This returns null for invalid source locations. 52llvm::DICompileUnit CGDebugInfo::getOrCreateCompileUnit(SourceLocation Loc) { 53 54 // Each input file is encoded as a separate compile unit in LLVM 55 // debugging information output. However, many target specific tool chains 56 // prefer to encode only one compile unit in an object file. In this 57 // situation, the LLVM code generator will include debugging information 58 // entities in the compile unit that is marked as main compile unit. The 59 // code generator accepts maximum one main compile unit per module. If a 60 // module does not contain any main compile unit then the code generator 61 // will emit multiple compile units in the output object file. Create main 62 // compile unit if there is not one available. 63 const LangOptions &LO = M->getLangOptions(); 64 if (isMainCompileUnitCreated == false) { 65 if (LO.getMainFileName()) { 66 createCompileUnit(LO.getMainFileName(), true /* isMain */); 67 isMainCompileUnitCreated = true; 68 } 69 } 70 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 // There is only one main source file at a time whose compile unit 94 // is already created. 95 Unit = createCompileUnit(FileName, false /* isMain */); 96 return Unit; 97} 98 99/// createCompileUnit - Create a new unit for the given file. 100llvm::DICompileUnit CGDebugInfo::createCompileUnit(const char *FileName, 101 bool isMain) { 102 103 // Get absolute path name. 104 llvm::sys::Path AbsFileName(FileName); 105 if (!AbsFileName.isAbsolute()) { 106 llvm::sys::Path tmp = llvm::sys::Path::GetCurrentDirectory(); 107 tmp.appendComponent(FileName); 108 AbsFileName = tmp; 109 } 110 111 unsigned LangTag; 112 const LangOptions &LO = M->getLangOptions(); 113 if (LO.CPlusPlus) { 114 if (LO.ObjC1) 115 LangTag = llvm::dwarf::DW_LANG_ObjC_plus_plus; 116 else 117 LangTag = llvm::dwarf::DW_LANG_C_plus_plus; 118 } else if (LO.ObjC1) { 119 LangTag = llvm::dwarf::DW_LANG_ObjC; 120 } else if (LO.C99) { 121 LangTag = llvm::dwarf::DW_LANG_C99; 122 } else { 123 LangTag = llvm::dwarf::DW_LANG_C89; 124 } 125 126 std::string Producer = "clang 1.0";// FIXME: clang version. 127 bool isOptimized = LO.Optimize; 128 const char *Flags = ""; // FIXME: Encode command line options. 129 130 // Figure out which version of the ObjC runtime we have. 131 unsigned RuntimeVers = 0; 132 if (LO.ObjC1) 133 RuntimeVers = LO.ObjCNonFragileABI ? 2 : 1; 134 135 // Create new compile unit. 136 return DebugFactory.CreateCompileUnit(LangTag, AbsFileName.getLast(), 137 AbsFileName.getDirname(), 138 Producer, isMain, isOptimized, 139 Flags, RuntimeVers); 140} 141 142 143/// CreateType - Get the Basic type from the cache or create a new 144/// one if necessary. 145llvm::DIType CGDebugInfo::CreateType(const BuiltinType *BT, 146 llvm::DICompileUnit Unit) { 147 unsigned Encoding = 0; 148 switch (BT->getKind()) { 149 default: 150 case BuiltinType::Void: 151 return llvm::DIType(); 152 case BuiltinType::UChar: 153 case BuiltinType::Char_U: Encoding = llvm::dwarf::DW_ATE_unsigned_char; break; 154 case BuiltinType::Char_S: 155 case BuiltinType::SChar: Encoding = llvm::dwarf::DW_ATE_signed_char; break; 156 case BuiltinType::UShort: 157 case BuiltinType::UInt: 158 case BuiltinType::ULong: 159 case BuiltinType::ULongLong: Encoding = llvm::dwarf::DW_ATE_unsigned; break; 160 case BuiltinType::Short: 161 case BuiltinType::Int: 162 case BuiltinType::Long: 163 case BuiltinType::LongLong: Encoding = llvm::dwarf::DW_ATE_signed; break; 164 case BuiltinType::Bool: Encoding = llvm::dwarf::DW_ATE_boolean; break; 165 case BuiltinType::Float: 166 case BuiltinType::Double: Encoding = llvm::dwarf::DW_ATE_float; break; 167 } 168 // Bit size, align and offset of the type. 169 uint64_t Size = M->getContext().getTypeSize(BT); 170 uint64_t Align = M->getContext().getTypeAlign(BT); 171 uint64_t Offset = 0; 172 173 return DebugFactory.CreateBasicType(Unit, 174 BT->getName(M->getContext().getLangOptions().CPlusPlus), 175 Unit, 0, Size, Align, 176 Offset, /*flags*/ 0, Encoding); 177} 178 179llvm::DIType CGDebugInfo::CreateType(const ComplexType *Ty, 180 llvm::DICompileUnit Unit) { 181 // Bit size, align and offset of the type. 182 unsigned Encoding = llvm::dwarf::DW_ATE_complex_float; 183 if (Ty->isComplexIntegerType()) 184 Encoding = llvm::dwarf::DW_ATE_lo_user; 185 186 uint64_t Size = M->getContext().getTypeSize(Ty); 187 uint64_t Align = M->getContext().getTypeAlign(Ty); 188 uint64_t Offset = 0; 189 190 return DebugFactory.CreateBasicType(Unit, "complex", 191 Unit, 0, Size, Align, 192 Offset, /*flags*/ 0, Encoding); 193} 194 195/// getOrCreateCVRType - Get the CVR qualified type from the cache or create 196/// a new one if necessary. 197llvm::DIType CGDebugInfo::CreateCVRType(QualType Ty, llvm::DICompileUnit Unit) { 198 // We will create one Derived type for one qualifier and recurse to handle any 199 // additional ones. 200 llvm::DIType FromTy; 201 unsigned Tag; 202 if (Ty.isConstQualified()) { 203 Tag = llvm::dwarf::DW_TAG_const_type; 204 Ty.removeConst(); 205 FromTy = getOrCreateType(Ty, Unit); 206 } else if (Ty.isVolatileQualified()) { 207 Tag = llvm::dwarf::DW_TAG_volatile_type; 208 Ty.removeVolatile(); 209 FromTy = getOrCreateType(Ty, Unit); 210 } else { 211 assert(Ty.isRestrictQualified() && "Unknown type qualifier for debug info"); 212 Tag = llvm::dwarf::DW_TAG_restrict_type; 213 Ty.removeRestrict(); 214 FromTy = getOrCreateType(Ty, Unit); 215 } 216 217 // No need to fill in the Name, Line, Size, Alignment, Offset in case of 218 // CVR derived types. 219 return DebugFactory.CreateDerivedType(Tag, Unit, "", llvm::DICompileUnit(), 220 0, 0, 0, 0, 0, FromTy); 221} 222 223llvm::DIType CGDebugInfo::CreateType(const PointerType *Ty, 224 llvm::DICompileUnit Unit) { 225 llvm::DIType EltTy = getOrCreateType(Ty->getPointeeType(), Unit); 226 227 // Bit size, align and offset of the type. 228 uint64_t Size = M->getContext().getTypeSize(Ty); 229 uint64_t Align = M->getContext().getTypeAlign(Ty); 230 231 return DebugFactory.CreateDerivedType(llvm::dwarf::DW_TAG_pointer_type, Unit, 232 "", llvm::DICompileUnit(), 233 0, Size, Align, 0, 0, EltTy); 234} 235 236llvm::DIType CGDebugInfo::CreateType(const BlockPointerType *Ty, 237 llvm::DICompileUnit Unit) { 238 if (BlockLiteralGenericSet) 239 return BlockLiteralGeneric; 240 241 llvm::DICompileUnit DefUnit; 242 unsigned Tag = llvm::dwarf::DW_TAG_structure_type; 243 244 llvm::SmallVector<llvm::DIDescriptor, 5> EltTys; 245 246 llvm::DIType FieldTy; 247 248 QualType FType; 249 uint64_t FieldSize, FieldOffset; 250 unsigned FieldAlign; 251 252 llvm::DIArray Elements; 253 llvm::DIType EltTy, DescTy; 254 255 FieldOffset = 0; 256 FType = M->getContext().UnsignedLongTy; 257 FieldTy = CGDebugInfo::getOrCreateType(FType, Unit); 258 FieldSize = M->getContext().getTypeSize(FType); 259 FieldAlign = M->getContext().getTypeAlign(FType); 260 FieldTy = DebugFactory.CreateDerivedType(llvm::dwarf::DW_TAG_member, Unit, 261 "reserved", DefUnit, 262 0, FieldSize, FieldAlign, 263 FieldOffset, 0, FieldTy); 264 EltTys.push_back(FieldTy); 265 266 FieldOffset += FieldSize; 267 FType = M->getContext().UnsignedLongTy; 268 FieldTy = CGDebugInfo::getOrCreateType(FType, Unit); 269 FieldSize = M->getContext().getTypeSize(FType); 270 FieldAlign = M->getContext().getTypeAlign(FType); 271 FieldTy = DebugFactory.CreateDerivedType(llvm::dwarf::DW_TAG_member, Unit, 272 "Size", DefUnit, 273 0, FieldSize, FieldAlign, 274 FieldOffset, 0, FieldTy); 275 EltTys.push_back(FieldTy); 276 277 FieldOffset += FieldSize; 278 Elements = DebugFactory.GetOrCreateArray(EltTys.data(), EltTys.size()); 279 EltTys.clear(); 280 281 EltTy = DebugFactory.CreateCompositeType(Tag, Unit, "__block_descriptor", 282 DefUnit, 0, FieldOffset, 0, 0, 0, 283 llvm::DIType(), Elements); 284 285 // Bit size, align and offset of the type. 286 uint64_t Size = M->getContext().getTypeSize(Ty); 287 uint64_t Align = M->getContext().getTypeAlign(Ty); 288 289 DescTy = DebugFactory.CreateDerivedType(llvm::dwarf::DW_TAG_pointer_type, 290 Unit, "", llvm::DICompileUnit(), 291 0, Size, Align, 0, 0, EltTy); 292 293 FieldOffset = 0; 294 FType = M->getContext().getPointerType(M->getContext().VoidTy); 295 FieldTy = CGDebugInfo::getOrCreateType(FType, Unit); 296 FieldSize = M->getContext().getTypeSize(FType); 297 FieldAlign = M->getContext().getTypeAlign(FType); 298 FieldTy = DebugFactory.CreateDerivedType(llvm::dwarf::DW_TAG_member, Unit, 299 "__isa", DefUnit, 300 0, FieldSize, FieldAlign, 301 FieldOffset, 0, FieldTy); 302 EltTys.push_back(FieldTy); 303 304 FieldOffset += FieldSize; 305 FType = M->getContext().IntTy; 306 FieldTy = CGDebugInfo::getOrCreateType(FType, Unit); 307 FieldSize = M->getContext().getTypeSize(FType); 308 FieldAlign = M->getContext().getTypeAlign(FType); 309 FieldTy = DebugFactory.CreateDerivedType(llvm::dwarf::DW_TAG_member, Unit, 310 "__flags", DefUnit, 311 0, FieldSize, FieldAlign, 312 FieldOffset, 0, FieldTy); 313 EltTys.push_back(FieldTy); 314 315 FieldOffset += FieldSize; 316 FType = M->getContext().IntTy; 317 FieldTy = CGDebugInfo::getOrCreateType(FType, Unit); 318 FieldSize = M->getContext().getTypeSize(FType); 319 FieldAlign = M->getContext().getTypeAlign(FType); 320 FieldTy = DebugFactory.CreateDerivedType(llvm::dwarf::DW_TAG_member, Unit, 321 "__reserved", DefUnit, 322 0, FieldSize, FieldAlign, 323 FieldOffset, 0, FieldTy); 324 EltTys.push_back(FieldTy); 325 326 FieldOffset += FieldSize; 327 FType = M->getContext().getPointerType(M->getContext().VoidTy); 328 FieldTy = CGDebugInfo::getOrCreateType(FType, Unit); 329 FieldSize = M->getContext().getTypeSize(FType); 330 FieldAlign = M->getContext().getTypeAlign(FType); 331 FieldTy = DebugFactory.CreateDerivedType(llvm::dwarf::DW_TAG_member, Unit, 332 "__FuncPtr", DefUnit, 333 0, FieldSize, FieldAlign, 334 FieldOffset, 0, FieldTy); 335 EltTys.push_back(FieldTy); 336 337 FieldOffset += FieldSize; 338 FType = M->getContext().getPointerType(M->getContext().VoidTy); 339 FieldTy = DescTy; 340 FieldSize = M->getContext().getTypeSize(Ty); 341 FieldAlign = M->getContext().getTypeAlign(Ty); 342 FieldTy = DebugFactory.CreateDerivedType(llvm::dwarf::DW_TAG_member, Unit, 343 "__descriptor", DefUnit, 344 0, FieldSize, FieldAlign, 345 FieldOffset, 0, FieldTy); 346 EltTys.push_back(FieldTy); 347 348 FieldOffset += FieldSize; 349 Elements = DebugFactory.GetOrCreateArray(EltTys.data(), EltTys.size()); 350 351 EltTy = DebugFactory.CreateCompositeType(Tag, Unit, "__block_literal_generic", 352 DefUnit, 0, FieldOffset, 0, 0, 0, 353 llvm::DIType(), Elements); 354 355 BlockLiteralGenericSet = true; 356 BlockLiteralGeneric 357 = DebugFactory.CreateDerivedType(llvm::dwarf::DW_TAG_pointer_type, Unit, 358 "", llvm::DICompileUnit(), 359 0, Size, Align, 0, 0, EltTy); 360 return BlockLiteralGeneric; 361} 362 363llvm::DIType CGDebugInfo::CreateType(const TypedefType *Ty, 364 llvm::DICompileUnit Unit) { 365 // Typedefs are derived from some other type. If we have a typedef of a 366 // typedef, make sure to emit the whole chain. 367 llvm::DIType Src = getOrCreateType(Ty->getDecl()->getUnderlyingType(), Unit); 368 369 // We don't set size information, but do specify where the typedef was 370 // declared. 371 std::string TyName = Ty->getDecl()->getNameAsString(); 372 SourceLocation DefLoc = Ty->getDecl()->getLocation(); 373 llvm::DICompileUnit DefUnit = getOrCreateCompileUnit(DefLoc); 374 375 SourceManager &SM = M->getContext().getSourceManager(); 376 PresumedLoc PLoc = SM.getPresumedLoc(DefLoc); 377 unsigned Line = PLoc.isInvalid() ? 0 : PLoc.getLine(); 378 379 return DebugFactory.CreateDerivedType(llvm::dwarf::DW_TAG_typedef, Unit, 380 TyName, DefUnit, Line, 0, 0, 0, 0, Src); 381} 382 383llvm::DIType CGDebugInfo::CreateType(const FunctionType *Ty, 384 llvm::DICompileUnit Unit) { 385 llvm::SmallVector<llvm::DIDescriptor, 16> EltTys; 386 387 // Add the result type at least. 388 EltTys.push_back(getOrCreateType(Ty->getResultType(), Unit)); 389 390 // Set up remainder of arguments if there is a prototype. 391 // FIXME: IF NOT, HOW IS THIS REPRESENTED? llvm-gcc doesn't represent '...'! 392 if (const FunctionProtoType *FTP = dyn_cast<FunctionProtoType>(Ty)) { 393 for (unsigned i = 0, e = FTP->getNumArgs(); i != e; ++i) 394 EltTys.push_back(getOrCreateType(FTP->getArgType(i), Unit)); 395 } else { 396 // FIXME: Handle () case in C. llvm-gcc doesn't do it either. 397 } 398 399 llvm::DIArray EltTypeArray = 400 DebugFactory.GetOrCreateArray(EltTys.data(), EltTys.size()); 401 402 return DebugFactory.CreateCompositeType(llvm::dwarf::DW_TAG_subroutine_type, 403 Unit, "", llvm::DICompileUnit(), 404 0, 0, 0, 0, 0, 405 llvm::DIType(), EltTypeArray); 406} 407 408/// CreateType - get structure or union type. 409llvm::DIType CGDebugInfo::CreateType(const RecordType *Ty, 410 llvm::DICompileUnit Unit) { 411 RecordDecl *Decl = Ty->getDecl(); 412 413 unsigned Tag; 414 if (Decl->isStruct()) 415 Tag = llvm::dwarf::DW_TAG_structure_type; 416 else if (Decl->isUnion()) 417 Tag = llvm::dwarf::DW_TAG_union_type; 418 else { 419 assert(Decl->isClass() && "Unknown RecordType!"); 420 Tag = llvm::dwarf::DW_TAG_class_type; 421 } 422 423 SourceManager &SM = M->getContext().getSourceManager(); 424 425 // Get overall information about the record type for the debug info. 426 std::string Name = Decl->getNameAsString(); 427 428 PresumedLoc PLoc = SM.getPresumedLoc(Decl->getLocation()); 429 llvm::DICompileUnit DefUnit; 430 unsigned Line = 0; 431 if (!PLoc.isInvalid()) { 432 DefUnit = getOrCreateCompileUnit(Decl->getLocation()); 433 Line = PLoc.getLine(); 434 } 435 436 // Records and classes and unions can all be recursive. To handle them, we 437 // first generate a debug descriptor for the struct as a forward declaration. 438 // Then (if it is a definition) we go through and get debug info for all of 439 // its members. Finally, we create a descriptor for the complete type (which 440 // may refer to the forward decl if the struct is recursive) and replace all 441 // uses of the forward declaration with the final definition. 442 llvm::DIType FwdDecl = 443 DebugFactory.CreateCompositeType(Tag, Unit, Name, DefUnit, Line, 0, 0, 0, 0, 444 llvm::DIType(), llvm::DIArray()); 445 446 // If this is just a forward declaration, return it. 447 if (!Decl->getDefinition(M->getContext())) 448 return FwdDecl; 449 450 // Otherwise, insert it into the TypeCache so that recursive uses will find 451 // it. 452 TypeCache[QualType(Ty, 0).getAsOpaquePtr()] = FwdDecl; 453 454 // Convert all the elements. 455 llvm::SmallVector<llvm::DIDescriptor, 16> EltTys; 456 457 const ASTRecordLayout &RL = M->getContext().getASTRecordLayout(Decl); 458 459 unsigned FieldNo = 0; 460 for (RecordDecl::field_iterator I = Decl->field_begin(M->getContext()), 461 E = Decl->field_end(M->getContext()); 462 I != E; ++I, ++FieldNo) { 463 FieldDecl *Field = *I; 464 llvm::DIType FieldTy = getOrCreateType(Field->getType(), Unit); 465 466 std::string FieldName = Field->getNameAsString(); 467 468 // Ignore unnamed fields. 469 if (FieldName.empty()) 470 continue; 471 472 // Get the location for the field. 473 SourceLocation FieldDefLoc = Field->getLocation(); 474 PresumedLoc PLoc = SM.getPresumedLoc(FieldDefLoc); 475 llvm::DICompileUnit FieldDefUnit; 476 unsigned FieldLine = 0; 477 478 if (!PLoc.isInvalid()) { 479 FieldDefUnit = getOrCreateCompileUnit(FieldDefLoc); 480 FieldLine = PLoc.getLine(); 481 } 482 483 QualType FType = Field->getType(); 484 uint64_t FieldSize = 0; 485 unsigned FieldAlign = 0; 486 if (!FType->isIncompleteArrayType()) { 487 488 // Bit size, align and offset of the type. 489 FieldSize = M->getContext().getTypeSize(FType); 490 Expr *BitWidth = Field->getBitWidth(); 491 if (BitWidth) 492 FieldSize = BitWidth->EvaluateAsInt(M->getContext()).getZExtValue(); 493 494 FieldAlign = M->getContext().getTypeAlign(FType); 495 } 496 497 uint64_t FieldOffset = RL.getFieldOffset(FieldNo); 498 499 // Create a DW_TAG_member node to remember the offset of this field in the 500 // struct. FIXME: This is an absolutely insane way to capture this 501 // information. When we gut debug info, this should be fixed. 502 FieldTy = DebugFactory.CreateDerivedType(llvm::dwarf::DW_TAG_member, Unit, 503 FieldName, FieldDefUnit, 504 FieldLine, FieldSize, FieldAlign, 505 FieldOffset, 0, FieldTy); 506 EltTys.push_back(FieldTy); 507 } 508 509 llvm::DIArray Elements = 510 DebugFactory.GetOrCreateArray(EltTys.data(), EltTys.size()); 511 512 // Bit size, align and offset of the type. 513 uint64_t Size = M->getContext().getTypeSize(Ty); 514 uint64_t Align = M->getContext().getTypeAlign(Ty); 515 516 llvm::DIType RealDecl = 517 DebugFactory.CreateCompositeType(Tag, Unit, Name, DefUnit, Line, Size, 518 Align, 0, 0, llvm::DIType(), Elements); 519 520 // Now that we have a real decl for the struct, replace anything using the 521 // old decl with the new one. This will recursively update the debug info. 522 FwdDecl.getGV()->replaceAllUsesWith(RealDecl.getGV()); 523 FwdDecl.getGV()->eraseFromParent(); 524 525 return RealDecl; 526} 527 528/// CreateType - get objective-c interface type. 529llvm::DIType CGDebugInfo::CreateType(const ObjCInterfaceType *Ty, 530 llvm::DICompileUnit Unit) { 531 ObjCInterfaceDecl *Decl = Ty->getDecl(); 532 533 unsigned Tag = llvm::dwarf::DW_TAG_structure_type; 534 SourceManager &SM = M->getContext().getSourceManager(); 535 536 // Get overall information about the record type for the debug info. 537 std::string Name = Decl->getNameAsString(); 538 539 llvm::DICompileUnit DefUnit = getOrCreateCompileUnit(Decl->getLocation()); 540 PresumedLoc PLoc = SM.getPresumedLoc(Decl->getLocation()); 541 unsigned Line = PLoc.isInvalid() ? 0 : PLoc.getLine(); 542 543 544 unsigned RuntimeLang = DefUnit.getLanguage(); 545 546 // To handle recursive interface, we 547 // first generate a debug descriptor for the struct as a forward declaration. 548 // Then (if it is a definition) we go through and get debug info for all of 549 // its members. Finally, we create a descriptor for the complete type (which 550 // may refer to the forward decl if the struct is recursive) and replace all 551 // uses of the forward declaration with the final definition. 552 llvm::DIType FwdDecl = 553 DebugFactory.CreateCompositeType(Tag, Unit, Name, DefUnit, Line, 0, 0, 0, 0, 554 llvm::DIType(), llvm::DIArray(), 555 RuntimeLang); 556 557 // If this is just a forward declaration, return it. 558 if (Decl->isForwardDecl()) 559 return FwdDecl; 560 561 // Otherwise, insert it into the TypeCache so that recursive uses will find 562 // it. 563 TypeCache[QualType(Ty, 0).getAsOpaquePtr()] = FwdDecl; 564 565 // Convert all the elements. 566 llvm::SmallVector<llvm::DIDescriptor, 16> EltTys; 567 568 ObjCInterfaceDecl *SClass = Decl->getSuperClass(); 569 if (SClass) { 570 llvm::DIType SClassTy = 571 getOrCreateType(M->getContext().getObjCInterfaceType(SClass), Unit); 572 llvm::DIType InhTag = 573 DebugFactory.CreateDerivedType(llvm::dwarf::DW_TAG_inheritance, 574 Unit, "", llvm::DICompileUnit(), 0, 0, 0, 575 0 /* offset */, 0, SClassTy); 576 EltTys.push_back(InhTag); 577 } 578 579 const ASTRecordLayout &RL = M->getContext().getASTObjCInterfaceLayout(Decl); 580 581 unsigned FieldNo = 0; 582 for (ObjCInterfaceDecl::ivar_iterator I = Decl->ivar_begin(), 583 E = Decl->ivar_end(); I != E; ++I, ++FieldNo) { 584 ObjCIvarDecl *Field = *I; 585 llvm::DIType FieldTy = getOrCreateType(Field->getType(), Unit); 586 587 std::string FieldName = Field->getNameAsString(); 588 589 // Ignore unnamed fields. 590 if (FieldName.empty()) 591 continue; 592 593 // Get the location for the field. 594 SourceLocation FieldDefLoc = Field->getLocation(); 595 llvm::DICompileUnit FieldDefUnit = getOrCreateCompileUnit(FieldDefLoc); 596 PresumedLoc PLoc = SM.getPresumedLoc(FieldDefLoc); 597 unsigned FieldLine = PLoc.isInvalid() ? 0 : PLoc.getLine(); 598 599 600 QualType FType = Field->getType(); 601 uint64_t FieldSize = 0; 602 unsigned FieldAlign = 0; 603 604 if (!FType->isIncompleteArrayType()) { 605 606 // Bit size, align and offset of the type. 607 FieldSize = M->getContext().getTypeSize(FType); 608 Expr *BitWidth = Field->getBitWidth(); 609 if (BitWidth) 610 FieldSize = BitWidth->EvaluateAsInt(M->getContext()).getZExtValue(); 611 612 FieldAlign = M->getContext().getTypeAlign(FType); 613 } 614 615 uint64_t FieldOffset = RL.getFieldOffset(FieldNo); 616 617 unsigned Flags = 0; 618 if (Field->getAccessControl() == ObjCIvarDecl::Protected) 619 Flags = llvm::DIType::FlagProtected; 620 else if (Field->getAccessControl() == ObjCIvarDecl::Private) 621 Flags = llvm::DIType::FlagPrivate; 622 623 // Create a DW_TAG_member node to remember the offset of this field in the 624 // struct. FIXME: This is an absolutely insane way to capture this 625 // information. When we gut debug info, this should be fixed. 626 FieldTy = DebugFactory.CreateDerivedType(llvm::dwarf::DW_TAG_member, Unit, 627 FieldName, FieldDefUnit, 628 FieldLine, FieldSize, FieldAlign, 629 FieldOffset, Flags, FieldTy); 630 EltTys.push_back(FieldTy); 631 } 632 633 llvm::DIArray Elements = 634 DebugFactory.GetOrCreateArray(EltTys.data(), EltTys.size()); 635 636 // Bit size, align and offset of the type. 637 uint64_t Size = M->getContext().getTypeSize(Ty); 638 uint64_t Align = M->getContext().getTypeAlign(Ty); 639 640 llvm::DIType RealDecl = 641 DebugFactory.CreateCompositeType(Tag, Unit, Name, DefUnit, Line, Size, 642 Align, 0, 0, llvm::DIType(), Elements, 643 RuntimeLang); 644 645 // Now that we have a real decl for the struct, replace anything using the 646 // old decl with the new one. This will recursively update the debug info. 647 FwdDecl.getGV()->replaceAllUsesWith(RealDecl.getGV()); 648 FwdDecl.getGV()->eraseFromParent(); 649 650 return RealDecl; 651} 652 653llvm::DIType CGDebugInfo::CreateType(const EnumType *Ty, 654 llvm::DICompileUnit Unit) { 655 EnumDecl *Decl = Ty->getDecl(); 656 657 llvm::SmallVector<llvm::DIDescriptor, 32> Enumerators; 658 659 // Create DIEnumerator elements for each enumerator. 660 for (EnumDecl::enumerator_iterator 661 Enum = Decl->enumerator_begin(M->getContext()), 662 EnumEnd = Decl->enumerator_end(M->getContext()); 663 Enum != EnumEnd; ++Enum) { 664 Enumerators.push_back(DebugFactory.CreateEnumerator(Enum->getNameAsString(), 665 Enum->getInitVal().getZExtValue())); 666 } 667 668 // Return a CompositeType for the enum itself. 669 llvm::DIArray EltArray = 670 DebugFactory.GetOrCreateArray(Enumerators.data(), Enumerators.size()); 671 672 std::string EnumName = Decl->getNameAsString(); 673 SourceLocation DefLoc = Decl->getLocation(); 674 llvm::DICompileUnit DefUnit = getOrCreateCompileUnit(DefLoc); 675 SourceManager &SM = M->getContext().getSourceManager(); 676 PresumedLoc PLoc = SM.getPresumedLoc(DefLoc); 677 unsigned Line = PLoc.isInvalid() ? 0 : PLoc.getLine(); 678 679 680 // Size and align of the type. 681 uint64_t Size = 0; 682 unsigned Align = 0; 683 if (!Ty->isIncompleteType()) { 684 Size = M->getContext().getTypeSize(Ty); 685 Align = M->getContext().getTypeAlign(Ty); 686 } 687 688 return DebugFactory.CreateCompositeType(llvm::dwarf::DW_TAG_enumeration_type, 689 Unit, EnumName, DefUnit, Line, 690 Size, Align, 0, 0, 691 llvm::DIType(), EltArray); 692} 693 694llvm::DIType CGDebugInfo::CreateType(const TagType *Ty, 695 llvm::DICompileUnit Unit) { 696 if (const RecordType *RT = dyn_cast<RecordType>(Ty)) 697 return CreateType(RT, Unit); 698 else if (const EnumType *ET = dyn_cast<EnumType>(Ty)) 699 return CreateType(ET, Unit); 700 701 return llvm::DIType(); 702} 703 704llvm::DIType CGDebugInfo::CreateType(const ArrayType *Ty, 705 llvm::DICompileUnit Unit) { 706 uint64_t Size; 707 uint64_t Align; 708 709 710 // FIXME: make getTypeAlign() aware of VLAs and incomplete array types 711 if (const VariableArrayType *VAT = dyn_cast<VariableArrayType>(Ty)) { 712 Size = 0; 713 Align = 714 M->getContext().getTypeAlign(M->getContext().getBaseElementType(VAT)); 715 } else if (Ty->isIncompleteArrayType()) { 716 Size = 0; 717 Align = M->getContext().getTypeAlign(Ty->getElementType()); 718 } else { 719 // Size and align of the whole array, not the element type. 720 Size = M->getContext().getTypeSize(Ty); 721 Align = M->getContext().getTypeAlign(Ty); 722 } 723 724 // Add the dimensions of the array. FIXME: This loses CV qualifiers from 725 // interior arrays, do we care? Why aren't nested arrays represented the 726 // obvious/recursive way? 727 llvm::SmallVector<llvm::DIDescriptor, 8> Subscripts; 728 QualType EltTy(Ty, 0); 729 while ((Ty = dyn_cast<ArrayType>(EltTy))) { 730 uint64_t Upper = 0; 731 if (const ConstantArrayType *CAT = dyn_cast<ConstantArrayType>(Ty)) 732 Upper = CAT->getSize().getZExtValue() - 1; 733 // FIXME: Verify this is right for VLAs. 734 Subscripts.push_back(DebugFactory.GetOrCreateSubrange(0, Upper)); 735 EltTy = Ty->getElementType(); 736 } 737 738 llvm::DIArray SubscriptArray = 739 DebugFactory.GetOrCreateArray(Subscripts.data(), Subscripts.size()); 740 741 return DebugFactory.CreateCompositeType(llvm::dwarf::DW_TAG_array_type, 742 Unit, "", llvm::DICompileUnit(), 743 0, Size, Align, 0, 0, 744 getOrCreateType(EltTy, Unit), 745 SubscriptArray); 746} 747 748 749/// getOrCreateType - Get the type from the cache or create a new 750/// one if necessary. 751llvm::DIType CGDebugInfo::getOrCreateType(QualType Ty, 752 llvm::DICompileUnit Unit) { 753 if (Ty.isNull()) 754 return llvm::DIType(); 755 756 // Check to see if the compile unit already has created this type. 757 llvm::DIType &Slot = TypeCache[Ty.getAsOpaquePtr()]; 758 if (!Slot.isNull()) return Slot; 759 760 // Handle CVR qualifiers, which recursively handles what they refer to. 761 if (Ty.getCVRQualifiers()) 762 return Slot = CreateCVRType(Ty, Unit); 763 764 // Work out details of type. 765 switch (Ty->getTypeClass()) { 766#define TYPE(Class, Base) 767#define ABSTRACT_TYPE(Class, Base) 768#define NON_CANONICAL_TYPE(Class, Base) 769#define DEPENDENT_TYPE(Class, Base) case Type::Class: 770#include "clang/AST/TypeNodes.def" 771 assert(false && "Dependent types cannot show up in debug information"); 772 773 case Type::LValueReference: 774 case Type::RValueReference: 775 case Type::Vector: 776 case Type::ExtVector: 777 case Type::ExtQual: 778 case Type::FixedWidthInt: 779 case Type::MemberPointer: 780 case Type::TemplateSpecialization: 781 case Type::QualifiedName: 782 // Unsupported types 783 return llvm::DIType(); 784 case Type::ObjCQualifiedId: // Encode id<p> in debug info just like id. 785 return Slot = getOrCreateType(M->getContext().getObjCIdType(), Unit); 786 787 case Type::ObjCQualifiedInterface: // Drop protocols from interface. 788 case Type::ObjCInterface: 789 return Slot = CreateType(cast<ObjCInterfaceType>(Ty), Unit); 790 case Type::Builtin: return Slot = CreateType(cast<BuiltinType>(Ty), Unit); 791 case Type::Complex: return Slot = CreateType(cast<ComplexType>(Ty), Unit); 792 case Type::Pointer: return Slot = CreateType(cast<PointerType>(Ty), Unit); 793 case Type::BlockPointer: 794 return Slot = CreateType(cast<BlockPointerType>(Ty), Unit); 795 case Type::Typedef: return Slot = CreateType(cast<TypedefType>(Ty), Unit); 796 case Type::Record: 797 case Type::Enum: 798 return Slot = CreateType(cast<TagType>(Ty), Unit); 799 case Type::FunctionProto: 800 case Type::FunctionNoProto: 801 return Slot = CreateType(cast<FunctionType>(Ty), Unit); 802 803 case Type::ConstantArray: 804 case Type::VariableArray: 805 case Type::IncompleteArray: 806 return Slot = CreateType(cast<ArrayType>(Ty), Unit); 807 case Type::TypeOfExpr: 808 return Slot = getOrCreateType(cast<TypeOfExprType>(Ty)->getUnderlyingExpr() 809 ->getType(), Unit); 810 case Type::TypeOf: 811 return Slot = getOrCreateType(cast<TypeOfType>(Ty)->getUnderlyingType(), 812 Unit); 813 } 814 815 return Slot; 816} 817 818/// EmitFunctionStart - Constructs the debug code for entering a function - 819/// "llvm.dbg.func.start.". 820void CGDebugInfo::EmitFunctionStart(const char *Name, QualType ReturnType, 821 llvm::Function *Fn, 822 CGBuilderTy &Builder) { 823 const char *LinkageName = Name; 824 825 // Skip the asm prefix if it exists. 826 // 827 // FIXME: This should probably be the unmangled name? 828 if (Name[0] == '\01') 829 ++Name; 830 831 // FIXME: Why is this using CurLoc??? 832 llvm::DICompileUnit Unit = getOrCreateCompileUnit(CurLoc); 833 SourceManager &SM = M->getContext().getSourceManager(); 834 unsigned LineNo = SM.getPresumedLoc(CurLoc).getLine(); 835 836 llvm::DISubprogram SP = 837 DebugFactory.CreateSubprogram(Unit, Name, Name, LinkageName, Unit, LineNo, 838 getOrCreateType(ReturnType, Unit), 839 Fn->hasInternalLinkage(), true/*definition*/); 840 841 DebugFactory.InsertSubprogramStart(SP, Builder.GetInsertBlock()); 842 843 // Push function on region stack. 844 RegionStack.push_back(SP); 845} 846 847 848void CGDebugInfo::EmitStopPoint(llvm::Function *Fn, CGBuilderTy &Builder) { 849 if (CurLoc.isInvalid() || CurLoc.isMacroID()) return; 850 851 // Don't bother if things are the same as last time. 852 SourceManager &SM = M->getContext().getSourceManager(); 853 if (CurLoc == PrevLoc 854 || (SM.getInstantiationLineNumber(CurLoc) == 855 SM.getInstantiationLineNumber(PrevLoc) 856 && SM.isFromSameFile(CurLoc, PrevLoc))) 857 return; 858 859 // Update last state. 860 PrevLoc = CurLoc; 861 862 // Get the appropriate compile unit. 863 llvm::DICompileUnit Unit = getOrCreateCompileUnit(CurLoc); 864 PresumedLoc PLoc = SM.getPresumedLoc(CurLoc); 865 DebugFactory.InsertStopPoint(Unit, PLoc.getLine(), PLoc.getColumn(), 866 Builder.GetInsertBlock()); 867} 868 869/// EmitRegionStart- Constructs the debug code for entering a declarative 870/// region - "llvm.dbg.region.start.". 871void CGDebugInfo::EmitRegionStart(llvm::Function *Fn, CGBuilderTy &Builder) { 872 llvm::DIDescriptor D; 873 if (!RegionStack.empty()) 874 D = RegionStack.back(); 875 D = DebugFactory.CreateBlock(D); 876 RegionStack.push_back(D); 877 DebugFactory.InsertRegionStart(D, Builder.GetInsertBlock()); 878} 879 880/// EmitRegionEnd - Constructs the debug code for exiting a declarative 881/// region - "llvm.dbg.region.end." 882void CGDebugInfo::EmitRegionEnd(llvm::Function *Fn, CGBuilderTy &Builder) { 883 assert(!RegionStack.empty() && "Region stack mismatch, stack empty!"); 884 885 // Provide an region stop point. 886 EmitStopPoint(Fn, Builder); 887 888 DebugFactory.InsertRegionEnd(RegionStack.back(), Builder.GetInsertBlock()); 889 RegionStack.pop_back(); 890} 891 892/// EmitDeclare - Emit local variable declaration debug info. 893void CGDebugInfo::EmitDeclare(const VarDecl *Decl, unsigned Tag, 894 llvm::Value *Storage, CGBuilderTy &Builder) { 895 assert(!RegionStack.empty() && "Region stack mismatch, stack empty!"); 896 897 // Do not emit variable debug information while generating optimized code. 898 // The llvm optimizer and code generator are not yet ready to support 899 // optimized code debugging. 900 const CompileOptions &CO = M->getCompileOpts(); 901 if (CO.OptimizationLevel) 902 return; 903 904 llvm::DICompileUnit Unit = getOrCreateCompileUnit(Decl->getLocation()); 905 llvm::DIType Ty = getOrCreateType(Decl->getType(), Unit); 906 907 // Get location information. 908 SourceManager &SM = M->getContext().getSourceManager(); 909 PresumedLoc PLoc = SM.getPresumedLoc(Decl->getLocation()); 910 unsigned Line = 0; 911 if (!PLoc.isInvalid()) 912 Line = PLoc.getLine(); 913 else 914 Unit = llvm::DICompileUnit(); 915 916 917 // Create the descriptor for the variable. 918 llvm::DIVariable D = 919 DebugFactory.CreateVariable(Tag, RegionStack.back(),Decl->getNameAsString(), 920 Unit, Line, Ty); 921 // Insert an llvm.dbg.declare into the current block. 922 DebugFactory.InsertDeclare(Storage, D, Builder.GetInsertBlock()); 923} 924 925void CGDebugInfo::EmitDeclareOfAutoVariable(const VarDecl *Decl, 926 llvm::Value *Storage, 927 CGBuilderTy &Builder) { 928 EmitDeclare(Decl, llvm::dwarf::DW_TAG_auto_variable, Storage, Builder); 929} 930 931/// EmitDeclareOfArgVariable - Emit call to llvm.dbg.declare for an argument 932/// variable declaration. 933void CGDebugInfo::EmitDeclareOfArgVariable(const VarDecl *Decl, llvm::Value *AI, 934 CGBuilderTy &Builder) { 935 EmitDeclare(Decl, llvm::dwarf::DW_TAG_arg_variable, AI, Builder); 936} 937 938 939 940/// EmitGlobalVariable - Emit information about a global variable. 941void CGDebugInfo::EmitGlobalVariable(llvm::GlobalVariable *Var, 942 const VarDecl *Decl) { 943 944 // Do not emit variable debug information while generating optimized code. 945 // The llvm optimizer and code generator are not yet ready to support 946 // optimized code debugging. 947 const CompileOptions &CO = M->getCompileOpts(); 948 if (CO.OptimizationLevel) 949 return; 950 951 // Create global variable debug descriptor. 952 llvm::DICompileUnit Unit = getOrCreateCompileUnit(Decl->getLocation()); 953 SourceManager &SM = M->getContext().getSourceManager(); 954 PresumedLoc PLoc = SM.getPresumedLoc(Decl->getLocation()); 955 unsigned LineNo = PLoc.isInvalid() ? 0 : PLoc.getLine(); 956 957 std::string Name = Decl->getNameAsString(); 958 959 QualType T = Decl->getType(); 960 if (T->isIncompleteArrayType()) { 961 962 // CodeGen turns int[] into int[1] so we'll do the same here. 963 llvm::APSInt ConstVal(32); 964 965 ConstVal = 1; 966 QualType ET = M->getContext().getAsArrayType(T)->getElementType(); 967 968 T = M->getContext().getConstantArrayType(ET, ConstVal, 969 ArrayType::Normal, 0); 970 } 971 972 DebugFactory.CreateGlobalVariable(Unit, Name, Name, "", Unit, LineNo, 973 getOrCreateType(T, Unit), 974 Var->hasInternalLinkage(), 975 true/*definition*/, Var); 976} 977 978/// EmitGlobalVariable - Emit information about an objective-c interface. 979void CGDebugInfo::EmitGlobalVariable(llvm::GlobalVariable *Var, 980 ObjCInterfaceDecl *Decl) { 981 // Create global variable debug descriptor. 982 llvm::DICompileUnit Unit = getOrCreateCompileUnit(Decl->getLocation()); 983 SourceManager &SM = M->getContext().getSourceManager(); 984 PresumedLoc PLoc = SM.getPresumedLoc(Decl->getLocation()); 985 unsigned LineNo = PLoc.isInvalid() ? 0 : PLoc.getLine(); 986 987 std::string Name = Decl->getNameAsString(); 988 989 QualType T = M->getContext().getObjCInterfaceType(Decl); 990 if (T->isIncompleteArrayType()) { 991 992 // CodeGen turns int[] into int[1] so we'll do the same here. 993 llvm::APSInt ConstVal(32); 994 995 ConstVal = 1; 996 QualType ET = M->getContext().getAsArrayType(T)->getElementType(); 997 998 T = M->getContext().getConstantArrayType(ET, ConstVal, 999 ArrayType::Normal, 0); 1000 } 1001 1002 DebugFactory.CreateGlobalVariable(Unit, Name, Name, "", Unit, LineNo, 1003 getOrCreateType(T, Unit), 1004 Var->hasInternalLinkage(), 1005 true/*definition*/, Var); 1006} 1007 1008