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