CGDebugInfo.cpp revision 952b017601f9c82b51119c3a1600f1312a833db9
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/CodeGen/CodeGenOptions.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 &CGM) 39 : CGM(CGM), isMainCompileUnitCreated(false), DebugFactory(CGM.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 = CGM.getContext().getSourceManager().getInstantiationLoc(Loc); 50} 51 52/// getContextDescriptor - Get context info for the decl. 53llvm::DIDescriptor CGDebugInfo::getContextDescriptor(const Decl *Context, 54 llvm::DIDescriptor &CompileUnit) { 55 if (!Context) 56 return CompileUnit; 57 58 llvm::DenseMap<const Decl *, llvm::WeakVH>::iterator 59 I = RegionMap.find(Context); 60 if (I != RegionMap.end()) 61 return llvm::DIDescriptor(dyn_cast_or_null<llvm::MDNode>(I->second)); 62 63 // Check namespace. 64 if (const NamespaceDecl *NSDecl = dyn_cast<NamespaceDecl>(Context)) 65 return llvm::DIDescriptor(getOrCreateNameSpace(NSDecl, CompileUnit)); 66 67 return CompileUnit; 68} 69 70/// getFunctionName - Get function name for the given FunctionDecl. If the 71/// name is constructred on demand (e.g. C++ destructor) then the name 72/// is stored on the side. 73llvm::StringRef CGDebugInfo::getFunctionName(const FunctionDecl *FD) { 74 assert (FD && "Invalid FunctionDecl!"); 75 IdentifierInfo *FII = FD->getIdentifier(); 76 if (FII) 77 return FII->getName(); 78 79 // Otherwise construct human readable name for debug info. 80 std::string NS = FD->getNameAsString(); 81 82 // Copy this name on the side and use its reference. 83 char *StrPtr = DebugInfoNames.Allocate<char>(NS.length()); 84 memcpy(StrPtr, NS.data(), NS.length()); 85 return llvm::StringRef(StrPtr, NS.length()); 86} 87 88/// getOrCreateCompileUnit - Get the compile unit from the cache or create a new 89/// one if necessary. This returns null for invalid source locations. 90llvm::DICompileUnit CGDebugInfo::getOrCreateCompileUnit(SourceLocation Loc) { 91 // Get source file information. 92 const char *FileName = "<unknown>"; 93 SourceManager &SM = CGM.getContext().getSourceManager(); 94 if (Loc.isValid()) { 95 PresumedLoc PLoc = SM.getPresumedLoc(Loc); 96 FileName = PLoc.getFilename(); 97 unsigned FID = PLoc.getIncludeLoc().getRawEncoding(); 98 99 // See if this compile unit has been used before for this valid location. 100 llvm::DICompileUnit &Unit = CompileUnitCache[FID]; 101 if (!Unit.isNull()) return Unit; 102 } 103 104 // Get absolute path name. 105 llvm::sys::Path AbsFileName(FileName); 106 AbsFileName.makeAbsolute(); 107 108 // See if thie compile unit is representing main source file. Each source 109 // file has corresponding compile unit. There is only one main source 110 // file at a time. 111 bool isMain = false; 112 const LangOptions &LO = CGM.getLangOptions(); 113 const CodeGenOptions &CGO = CGM.getCodeGenOpts(); 114 if (isMainCompileUnitCreated == false) { 115 if (!CGO.MainFileName.empty()) { 116 if (AbsFileName.getLast() == CGO.MainFileName) 117 isMain = true; 118 } else { 119 if (Loc.isValid() && SM.isFromMainFile(Loc)) 120 isMain = true; 121 } 122 if (isMain) 123 isMainCompileUnitCreated = true; 124 } 125 126 unsigned LangTag; 127 if (LO.CPlusPlus) { 128 if (LO.ObjC1) 129 LangTag = llvm::dwarf::DW_LANG_ObjC_plus_plus; 130 else 131 LangTag = llvm::dwarf::DW_LANG_C_plus_plus; 132 } else if (LO.ObjC1) { 133 LangTag = llvm::dwarf::DW_LANG_ObjC; 134 } else if (LO.C99) { 135 LangTag = llvm::dwarf::DW_LANG_C99; 136 } else { 137 LangTag = llvm::dwarf::DW_LANG_C89; 138 } 139 140 const char *Producer = 141#ifdef CLANG_VENDOR 142 CLANG_VENDOR 143#endif 144 "clang " CLANG_VERSION_STRING; 145 146 // Figure out which version of the ObjC runtime we have. 147 unsigned RuntimeVers = 0; 148 if (LO.ObjC1) 149 RuntimeVers = LO.ObjCNonFragileABI ? 2 : 1; 150 151 // Create new compile unit. 152 return DebugFactory.CreateCompileUnit( 153 LangTag, AbsFileName.getLast(), AbsFileName.getDirname(), Producer, isMain, 154 LO.Optimize, CGM.getCodeGenOpts().DwarfDebugFlags, RuntimeVers); 155} 156 157/// CreateType - Get the Basic type from the cache or create a new 158/// one if necessary. 159llvm::DIType CGDebugInfo::CreateType(const BuiltinType *BT, 160 llvm::DICompileUnit Unit) { 161 unsigned Encoding = 0; 162 switch (BT->getKind()) { 163 default: 164 case BuiltinType::Void: 165 return llvm::DIType(); 166 case BuiltinType::UChar: 167 case BuiltinType::Char_U: Encoding = llvm::dwarf::DW_ATE_unsigned_char; break; 168 case BuiltinType::Char_S: 169 case BuiltinType::SChar: Encoding = llvm::dwarf::DW_ATE_signed_char; break; 170 case BuiltinType::UShort: 171 case BuiltinType::UInt: 172 case BuiltinType::ULong: 173 case BuiltinType::ULongLong: Encoding = llvm::dwarf::DW_ATE_unsigned; break; 174 case BuiltinType::Short: 175 case BuiltinType::Int: 176 case BuiltinType::Long: 177 case BuiltinType::LongLong: Encoding = llvm::dwarf::DW_ATE_signed; break; 178 case BuiltinType::Bool: Encoding = llvm::dwarf::DW_ATE_boolean; break; 179 case BuiltinType::Float: 180 case BuiltinType::LongDouble: 181 case BuiltinType::Double: Encoding = llvm::dwarf::DW_ATE_float; break; 182 } 183 // Bit size, align and offset of the type. 184 uint64_t Size = CGM.getContext().getTypeSize(BT); 185 uint64_t Align = CGM.getContext().getTypeAlign(BT); 186 uint64_t Offset = 0; 187 188 llvm::DIType DbgTy = 189 DebugFactory.CreateBasicType(Unit, 190 BT->getName(CGM.getContext().getLangOptions()), 191 Unit, 0, Size, Align, 192 Offset, /*flags*/ 0, Encoding); 193 return DbgTy; 194} 195 196llvm::DIType CGDebugInfo::CreateType(const ComplexType *Ty, 197 llvm::DICompileUnit Unit) { 198 // Bit size, align and offset of the type. 199 unsigned Encoding = llvm::dwarf::DW_ATE_complex_float; 200 if (Ty->isComplexIntegerType()) 201 Encoding = llvm::dwarf::DW_ATE_lo_user; 202 203 uint64_t Size = CGM.getContext().getTypeSize(Ty); 204 uint64_t Align = CGM.getContext().getTypeAlign(Ty); 205 uint64_t Offset = 0; 206 207 llvm::DIType DbgTy = 208 DebugFactory.CreateBasicType(Unit, "complex", 209 Unit, 0, Size, Align, 210 Offset, /*flags*/ 0, Encoding); 211 return DbgTy; 212} 213 214/// CreateCVRType - Get the qualified type from the cache or create 215/// a new one if necessary. 216llvm::DIType CGDebugInfo::CreateQualifiedType(QualType Ty, llvm::DICompileUnit Unit) { 217 QualifierCollector Qc; 218 const Type *T = Qc.strip(Ty); 219 220 // Ignore these qualifiers for now. 221 Qc.removeObjCGCAttr(); 222 Qc.removeAddressSpace(); 223 224 // We will create one Derived type for one qualifier and recurse to handle any 225 // additional ones. 226 unsigned Tag; 227 if (Qc.hasConst()) { 228 Tag = llvm::dwarf::DW_TAG_const_type; 229 Qc.removeConst(); 230 } else if (Qc.hasVolatile()) { 231 Tag = llvm::dwarf::DW_TAG_volatile_type; 232 Qc.removeVolatile(); 233 } else if (Qc.hasRestrict()) { 234 Tag = llvm::dwarf::DW_TAG_restrict_type; 235 Qc.removeRestrict(); 236 } else { 237 assert(Qc.empty() && "Unknown type qualifier for debug info"); 238 return getOrCreateType(QualType(T, 0), Unit); 239 } 240 241 llvm::DIType FromTy = getOrCreateType(Qc.apply(T), Unit); 242 243 // No need to fill in the Name, Line, Size, Alignment, Offset in case of 244 // CVR derived types. 245 llvm::DIType DbgTy = 246 DebugFactory.CreateDerivedType(Tag, Unit, "", llvm::DICompileUnit(), 247 0, 0, 0, 0, 0, FromTy); 248 return DbgTy; 249} 250 251llvm::DIType CGDebugInfo::CreateType(const ObjCObjectPointerType *Ty, 252 llvm::DICompileUnit Unit) { 253 llvm::DIType DbgTy = 254 CreatePointerLikeType(llvm::dwarf::DW_TAG_pointer_type, Ty, 255 Ty->getPointeeType(), Unit); 256 return DbgTy; 257} 258 259llvm::DIType CGDebugInfo::CreateType(const PointerType *Ty, 260 llvm::DICompileUnit Unit) { 261 return CreatePointerLikeType(llvm::dwarf::DW_TAG_pointer_type, Ty, 262 Ty->getPointeeType(), Unit); 263} 264 265llvm::DIType CGDebugInfo::CreatePointerLikeType(unsigned Tag, 266 const Type *Ty, 267 QualType PointeeTy, 268 llvm::DICompileUnit Unit) { 269 llvm::DIType EltTy = getOrCreateType(PointeeTy, Unit); 270 271 // Bit size, align and offset of the type. 272 273 // Size is always the size of a pointer. We can't use getTypeSize here 274 // because that does not return the correct value for references. 275 uint64_t Size = 276 CGM.getContext().Target.getPointerWidth(PointeeTy.getAddressSpace()); 277 uint64_t Align = CGM.getContext().getTypeAlign(Ty); 278 279 return 280 DebugFactory.CreateDerivedType(Tag, Unit, "", llvm::DICompileUnit(), 281 0, Size, Align, 0, 0, EltTy); 282 283} 284 285llvm::DIType CGDebugInfo::CreateType(const BlockPointerType *Ty, 286 llvm::DICompileUnit Unit) { 287 if (BlockLiteralGenericSet) 288 return BlockLiteralGeneric; 289 290 llvm::DICompileUnit DefUnit; 291 unsigned Tag = llvm::dwarf::DW_TAG_structure_type; 292 293 llvm::SmallVector<llvm::DIDescriptor, 5> EltTys; 294 295 llvm::DIType FieldTy; 296 297 QualType FType; 298 uint64_t FieldSize, FieldOffset; 299 unsigned FieldAlign; 300 301 llvm::DIArray Elements; 302 llvm::DIType EltTy, DescTy; 303 304 FieldOffset = 0; 305 FType = CGM.getContext().UnsignedLongTy; 306 FieldTy = CGDebugInfo::getOrCreateType(FType, Unit); 307 FieldSize = CGM.getContext().getTypeSize(FType); 308 FieldAlign = CGM.getContext().getTypeAlign(FType); 309 FieldTy = DebugFactory.CreateDerivedType(llvm::dwarf::DW_TAG_member, Unit, 310 "reserved", DefUnit, 311 0, FieldSize, FieldAlign, 312 FieldOffset, 0, FieldTy); 313 EltTys.push_back(FieldTy); 314 315 FieldOffset += FieldSize; 316 FType = CGM.getContext().UnsignedLongTy; 317 FieldTy = CGDebugInfo::getOrCreateType(FType, Unit); 318 FieldSize = CGM.getContext().getTypeSize(FType); 319 FieldAlign = CGM.getContext().getTypeAlign(FType); 320 FieldTy = DebugFactory.CreateDerivedType(llvm::dwarf::DW_TAG_member, Unit, 321 "Size", DefUnit, 322 0, FieldSize, FieldAlign, 323 FieldOffset, 0, FieldTy); 324 EltTys.push_back(FieldTy); 325 326 FieldOffset += FieldSize; 327 Elements = DebugFactory.GetOrCreateArray(EltTys.data(), EltTys.size()); 328 EltTys.clear(); 329 330 unsigned Flags = llvm::DIType::FlagAppleBlock; 331 332 EltTy = DebugFactory.CreateCompositeType(Tag, Unit, "__block_descriptor", 333 DefUnit, 0, FieldOffset, 0, 0, Flags, 334 llvm::DIType(), Elements); 335 336 // Bit size, align and offset of the type. 337 uint64_t Size = CGM.getContext().getTypeSize(Ty); 338 uint64_t Align = CGM.getContext().getTypeAlign(Ty); 339 340 DescTy = DebugFactory.CreateDerivedType(llvm::dwarf::DW_TAG_pointer_type, 341 Unit, "", llvm::DICompileUnit(), 342 0, Size, Align, 0, 0, EltTy); 343 344 FieldOffset = 0; 345 FType = CGM.getContext().getPointerType(CGM.getContext().VoidTy); 346 FieldTy = CGDebugInfo::getOrCreateType(FType, Unit); 347 FieldSize = CGM.getContext().getTypeSize(FType); 348 FieldAlign = CGM.getContext().getTypeAlign(FType); 349 FieldTy = DebugFactory.CreateDerivedType(llvm::dwarf::DW_TAG_member, Unit, 350 "__isa", DefUnit, 351 0, FieldSize, FieldAlign, 352 FieldOffset, 0, FieldTy); 353 EltTys.push_back(FieldTy); 354 355 FieldOffset += FieldSize; 356 FType = CGM.getContext().IntTy; 357 FieldTy = CGDebugInfo::getOrCreateType(FType, Unit); 358 FieldSize = CGM.getContext().getTypeSize(FType); 359 FieldAlign = CGM.getContext().getTypeAlign(FType); 360 FieldTy = DebugFactory.CreateDerivedType(llvm::dwarf::DW_TAG_member, Unit, 361 "__flags", DefUnit, 362 0, FieldSize, FieldAlign, 363 FieldOffset, 0, FieldTy); 364 EltTys.push_back(FieldTy); 365 366 FieldOffset += FieldSize; 367 FType = CGM.getContext().IntTy; 368 FieldTy = CGDebugInfo::getOrCreateType(FType, Unit); 369 FieldSize = CGM.getContext().getTypeSize(FType); 370 FieldAlign = CGM.getContext().getTypeAlign(FType); 371 FieldTy = DebugFactory.CreateDerivedType(llvm::dwarf::DW_TAG_member, Unit, 372 "__reserved", DefUnit, 373 0, FieldSize, FieldAlign, 374 FieldOffset, 0, FieldTy); 375 EltTys.push_back(FieldTy); 376 377 FieldOffset += FieldSize; 378 FType = CGM.getContext().getPointerType(CGM.getContext().VoidTy); 379 FieldTy = CGDebugInfo::getOrCreateType(FType, Unit); 380 FieldSize = CGM.getContext().getTypeSize(FType); 381 FieldAlign = CGM.getContext().getTypeAlign(FType); 382 FieldTy = DebugFactory.CreateDerivedType(llvm::dwarf::DW_TAG_member, Unit, 383 "__FuncPtr", DefUnit, 384 0, FieldSize, FieldAlign, 385 FieldOffset, 0, FieldTy); 386 EltTys.push_back(FieldTy); 387 388 FieldOffset += FieldSize; 389 FType = CGM.getContext().getPointerType(CGM.getContext().VoidTy); 390 FieldTy = DescTy; 391 FieldSize = CGM.getContext().getTypeSize(Ty); 392 FieldAlign = CGM.getContext().getTypeAlign(Ty); 393 FieldTy = DebugFactory.CreateDerivedType(llvm::dwarf::DW_TAG_member, Unit, 394 "__descriptor", DefUnit, 395 0, FieldSize, FieldAlign, 396 FieldOffset, 0, FieldTy); 397 EltTys.push_back(FieldTy); 398 399 FieldOffset += FieldSize; 400 Elements = DebugFactory.GetOrCreateArray(EltTys.data(), EltTys.size()); 401 402 EltTy = DebugFactory.CreateCompositeType(Tag, Unit, "__block_literal_generic", 403 DefUnit, 0, FieldOffset, 0, 0, Flags, 404 llvm::DIType(), Elements); 405 406 BlockLiteralGenericSet = true; 407 BlockLiteralGeneric 408 = DebugFactory.CreateDerivedType(llvm::dwarf::DW_TAG_pointer_type, Unit, 409 "", llvm::DICompileUnit(), 410 0, Size, Align, 0, 0, EltTy); 411 return BlockLiteralGeneric; 412} 413 414llvm::DIType CGDebugInfo::CreateType(const TypedefType *Ty, 415 llvm::DICompileUnit Unit) { 416 // Typedefs are derived from some other type. If we have a typedef of a 417 // typedef, make sure to emit the whole chain. 418 llvm::DIType Src = getOrCreateType(Ty->getDecl()->getUnderlyingType(), Unit); 419 420 // We don't set size information, but do specify where the typedef was 421 // declared. 422 SourceManager &SM = CGM.getContext().getSourceManager(); 423 PresumedLoc PLoc = SM.getPresumedLoc(Ty->getDecl()->getLocation()); 424 unsigned Line = PLoc.isInvalid() ? 0 : PLoc.getLine(); 425 426 llvm::DIDescriptor TyContext 427 = getContextDescriptor(dyn_cast<Decl>(Ty->getDecl()->getDeclContext()), 428 Unit); 429 llvm::DIType DbgTy = 430 DebugFactory.CreateDerivedType(llvm::dwarf::DW_TAG_typedef, 431 TyContext, 432 Ty->getDecl()->getName(), Unit, 433 Line, 0, 0, 0, 0, Src); 434 return DbgTy; 435} 436 437llvm::DIType CGDebugInfo::CreateType(const FunctionType *Ty, 438 llvm::DICompileUnit Unit) { 439 llvm::SmallVector<llvm::DIDescriptor, 16> EltTys; 440 441 // Add the result type at least. 442 EltTys.push_back(getOrCreateType(Ty->getResultType(), Unit)); 443 444 // Set up remainder of arguments if there is a prototype. 445 // FIXME: IF NOT, HOW IS THIS REPRESENTED? llvm-gcc doesn't represent '...'! 446 if (const FunctionProtoType *FTP = dyn_cast<FunctionProtoType>(Ty)) { 447 for (unsigned i = 0, e = FTP->getNumArgs(); i != e; ++i) 448 EltTys.push_back(getOrCreateType(FTP->getArgType(i), Unit)); 449 } else { 450 // FIXME: Handle () case in C. llvm-gcc doesn't do it either. 451 } 452 453 llvm::DIArray EltTypeArray = 454 DebugFactory.GetOrCreateArray(EltTys.data(), EltTys.size()); 455 456 llvm::DIType DbgTy = 457 DebugFactory.CreateCompositeType(llvm::dwarf::DW_TAG_subroutine_type, 458 Unit, "", llvm::DICompileUnit(), 459 0, 0, 0, 0, 0, 460 llvm::DIType(), EltTypeArray); 461 return DbgTy; 462} 463 464/// CollectRecordFields - A helper function to collect debug info for 465/// record fields. This is used while creating debug info entry for a Record. 466void CGDebugInfo:: 467CollectRecordFields(const RecordDecl *RD, llvm::DICompileUnit Unit, 468 llvm::SmallVectorImpl<llvm::DIDescriptor> &EltTys) { 469 unsigned FieldNo = 0; 470 SourceManager &SM = CGM.getContext().getSourceManager(); 471 const ASTRecordLayout &RL = CGM.getContext().getASTRecordLayout(RD); 472 for (RecordDecl::field_iterator I = RD->field_begin(), 473 E = RD->field_end(); 474 I != E; ++I, ++FieldNo) { 475 FieldDecl *Field = *I; 476 llvm::DIType FieldTy = getOrCreateType(Field->getType(), Unit); 477 478 llvm::StringRef FieldName = Field->getName(); 479 480 // Ignore unnamed fields. 481 if (FieldName.empty()) 482 continue; 483 484 // Get the location for the field. 485 SourceLocation FieldDefLoc = Field->getLocation(); 486 PresumedLoc PLoc = SM.getPresumedLoc(FieldDefLoc); 487 llvm::DICompileUnit FieldDefUnit; 488 unsigned FieldLine = 0; 489 490 if (!PLoc.isInvalid()) { 491 FieldDefUnit = getOrCreateCompileUnit(FieldDefLoc); 492 FieldLine = PLoc.getLine(); 493 } 494 495 QualType FType = Field->getType(); 496 uint64_t FieldSize = 0; 497 unsigned FieldAlign = 0; 498 if (!FType->isIncompleteArrayType()) { 499 500 // Bit size, align and offset of the type. 501 FieldSize = CGM.getContext().getTypeSize(FType); 502 Expr *BitWidth = Field->getBitWidth(); 503 if (BitWidth) 504 FieldSize = BitWidth->EvaluateAsInt(CGM.getContext()).getZExtValue(); 505 506 FieldAlign = CGM.getContext().getTypeAlign(FType); 507 } 508 509 uint64_t FieldOffset = RL.getFieldOffset(FieldNo); 510 511 // Create a DW_TAG_member node to remember the offset of this field in the 512 // struct. FIXME: This is an absolutely insane way to capture this 513 // information. When we gut debug info, this should be fixed. 514 FieldTy = DebugFactory.CreateDerivedType(llvm::dwarf::DW_TAG_member, Unit, 515 FieldName, FieldDefUnit, 516 FieldLine, FieldSize, FieldAlign, 517 FieldOffset, 0, FieldTy); 518 EltTys.push_back(FieldTy); 519 } 520} 521 522/// getOrCreateMethodType - CXXMethodDecl's type is a FunctionType. This 523/// function type is not updated to include implicit "this" pointer. Use this 524/// routine to get a method type which includes "this" pointer. 525llvm::DIType 526CGDebugInfo::getOrCreateMethodType(const CXXMethodDecl *Method, 527 llvm::DICompileUnit Unit) { 528 llvm::DIType FnTy = getOrCreateType(Method->getType(), Unit); 529 530 // Static methods do not need "this" pointer argument. 531 if (Method->isStatic()) 532 return FnTy; 533 534 // Add "this" pointer. 535 536 llvm::DIArray Args = llvm::DICompositeType(FnTy.getNode()).getTypeArray(); 537 assert (Args.getNumElements() && "Invalid number of arguments!"); 538 539 llvm::SmallVector<llvm::DIDescriptor, 16> Elts; 540 541 // First element is always return type. For 'void' functions it is NULL. 542 Elts.push_back(Args.getElement(0)); 543 544 // "this" pointer is always first argument. 545 ASTContext &Context = CGM.getContext(); 546 QualType ThisPtr = 547 Context.getPointerType(Context.getTagDeclType(Method->getParent())); 548 llvm::DIType ThisPtrType = 549 DebugFactory.CreateArtificialType(getOrCreateType(ThisPtr, Unit)); 550 TypeCache[ThisPtr.getAsOpaquePtr()] = ThisPtrType.getNode(); 551 Elts.push_back(ThisPtrType); 552 553 // Copy rest of the arguments. 554 for (unsigned i = 1, e = Args.getNumElements(); i != e; ++i) 555 Elts.push_back(Args.getElement(i)); 556 557 llvm::DIArray EltTypeArray = 558 DebugFactory.GetOrCreateArray(Elts.data(), Elts.size()); 559 560 return 561 DebugFactory.CreateCompositeType(llvm::dwarf::DW_TAG_subroutine_type, 562 Unit, "", llvm::DICompileUnit(), 563 0, 0, 0, 0, 0, 564 llvm::DIType(), EltTypeArray); 565} 566 567/// CreateCXXMemberFunction - A helper function to create a DISubprogram for 568/// a single member function GlobalDecl. 569llvm::DISubprogram 570CGDebugInfo::CreateCXXMemberFunction(const CXXMethodDecl *Method, 571 llvm::DICompileUnit Unit, 572 llvm::DICompositeType &RecordTy) { 573 bool IsCtorOrDtor = 574 isa<CXXConstructorDecl>(Method) || isa<CXXDestructorDecl>(Method); 575 576 llvm::StringRef MethodName = getFunctionName(Method); 577 llvm::StringRef MethodLinkageName; 578 llvm::DIType MethodTy = getOrCreateMethodType(Method, Unit); 579 580 // Since a single ctor/dtor corresponds to multiple functions, it doesn't 581 // make sense to give a single ctor/dtor a linkage name. 582 if (!IsCtorOrDtor) 583 MethodLinkageName = CGM.getMangledName(Method); 584 585 SourceManager &SM = CGM.getContext().getSourceManager(); 586 587 // Get the location for the method. 588 SourceLocation MethodDefLoc = Method->getLocation(); 589 PresumedLoc PLoc = SM.getPresumedLoc(MethodDefLoc); 590 llvm::DICompileUnit MethodDefUnit; 591 unsigned MethodLine = 0; 592 593 if (!PLoc.isInvalid()) { 594 MethodDefUnit = getOrCreateCompileUnit(MethodDefLoc); 595 MethodLine = PLoc.getLine(); 596 } 597 598 // Collect virtual method info. 599 llvm::DIType ContainingType; 600 unsigned Virtuality = 0; 601 unsigned VIndex = 0; 602 603 if (Method->isVirtual()) { 604 if (Method->isPure()) 605 Virtuality = llvm::dwarf::DW_VIRTUALITY_pure_virtual; 606 else 607 Virtuality = llvm::dwarf::DW_VIRTUALITY_virtual; 608 609 // It doesn't make sense to give a virtual destructor a vtable index, 610 // since a single destructor has two entries in the vtable. 611 if (!isa<CXXDestructorDecl>(Method)) 612 VIndex = CGM.getVtableInfo().getMethodVtableIndex(Method); 613 ContainingType = RecordTy; 614 } 615 616 llvm::DISubprogram SP = 617 DebugFactory.CreateSubprogram(RecordTy , MethodName, MethodName, 618 MethodLinkageName, 619 MethodDefUnit, MethodLine, 620 MethodTy, /*isLocalToUnit=*/false, 621 Method->isThisDeclarationADefinition(), 622 Virtuality, VIndex, ContainingType); 623 624 // Don't cache ctors or dtors since we have to emit multiple functions for 625 // a single ctor or dtor. 626 if (!IsCtorOrDtor && Method->isThisDeclarationADefinition()) 627 SPCache[Method] = llvm::WeakVH(SP.getNode()); 628 629 return SP; 630} 631 632/// CollectCXXMemberFunctions - A helper function to collect debug info for 633/// C++ member functions.This is used while creating debug info entry for 634/// a Record. 635void CGDebugInfo:: 636CollectCXXMemberFunctions(const CXXRecordDecl *RD, llvm::DICompileUnit Unit, 637 llvm::SmallVectorImpl<llvm::DIDescriptor> &EltTys, 638 llvm::DICompositeType &RecordTy) { 639 for(CXXRecordDecl::method_iterator I = RD->method_begin(), 640 E = RD->method_end(); I != E; ++I) { 641 const CXXMethodDecl *Method = *I; 642 643 if (Method->isImplicit() && !Method->isUsed()) 644 continue; 645 646 EltTys.push_back(CreateCXXMemberFunction(Method, Unit, RecordTy)); 647 } 648} 649 650/// CollectCXXBases - A helper function to collect debug info for 651/// C++ base classes. This is used while creating debug info entry for 652/// a Record. 653void CGDebugInfo:: 654CollectCXXBases(const CXXRecordDecl *RD, llvm::DICompileUnit Unit, 655 llvm::SmallVectorImpl<llvm::DIDescriptor> &EltTys, 656 llvm::DICompositeType &RecordTy) { 657 658 const ASTRecordLayout &RL = CGM.getContext().getASTRecordLayout(RD); 659 for (CXXRecordDecl::base_class_const_iterator BI = RD->bases_begin(), 660 BE = RD->bases_end(); BI != BE; ++BI) { 661 unsigned BFlags = 0; 662 uint64_t BaseOffset; 663 664 const CXXRecordDecl *Base = 665 cast<CXXRecordDecl>(BI->getType()->getAs<RecordType>()->getDecl()); 666 667 if (BI->isVirtual()) { 668 // virtual base offset index is -ve. The code generator emits dwarf 669 // expression where it expects +ve number. 670 BaseOffset = 0 - CGM.getVtableInfo().getVirtualBaseOffsetIndex(RD, Base); 671 BFlags = llvm::DIType::FlagVirtual; 672 } else 673 BaseOffset = RL.getBaseClassOffset(Base); 674 675 AccessSpecifier Access = BI->getAccessSpecifier(); 676 if (Access == clang::AS_private) 677 BFlags |= llvm::DIType::FlagPrivate; 678 else if (Access == clang::AS_protected) 679 BFlags |= llvm::DIType::FlagProtected; 680 681 llvm::DIType DTy = 682 DebugFactory.CreateDerivedType(llvm::dwarf::DW_TAG_inheritance, 683 RecordTy, llvm::StringRef(), 684 llvm::DICompileUnit(), 0, 0, 0, 685 BaseOffset, BFlags, 686 getOrCreateType(BI->getType(), 687 Unit)); 688 EltTys.push_back(DTy); 689 } 690} 691 692/// getOrCreateVTablePtrType - Return debug info descriptor for vtable. 693llvm::DIType CGDebugInfo::getOrCreateVTablePtrType(llvm::DICompileUnit Unit) { 694 if (!VTablePtrType.isNull()) 695 return VTablePtrType; 696 697 ASTContext &Context = CGM.getContext(); 698 699 /* Function type */ 700 llvm::SmallVector<llvm::DIDescriptor, 16> STys; 701 STys.push_back(getOrCreateType(Context.IntTy, Unit)); 702 llvm::DIArray SElements = 703 DebugFactory.GetOrCreateArray(STys.data(), STys.size()); 704 llvm::DIType SubTy = 705 DebugFactory.CreateCompositeType(llvm::dwarf::DW_TAG_subroutine_type, 706 Unit, "", llvm::DICompileUnit(), 707 0, 0, 0, 0, 0, llvm::DIType(), SElements); 708 709 unsigned Size = Context.getTypeSize(Context.VoidPtrTy); 710 llvm::DIType vtbl_ptr_type 711 = DebugFactory.CreateDerivedType(llvm::dwarf::DW_TAG_pointer_type, 712 Unit, "__vtbl_ptr_type", llvm::DICompileUnit(), 713 0, Size, 0, 0, 0, SubTy); 714 715 VTablePtrType = DebugFactory.CreateDerivedType(llvm::dwarf::DW_TAG_pointer_type, 716 Unit, "", llvm::DICompileUnit(), 717 0, Size, 0, 0, 0, vtbl_ptr_type); 718 return VTablePtrType; 719} 720 721/// getVtableName - Get vtable name for the given Class. 722llvm::StringRef CGDebugInfo::getVtableName(const CXXRecordDecl *RD) { 723 // Otherwise construct gdb compatible name name. 724 std::string Name = "_vptr$" + RD->getNameAsString(); 725 726 // Copy this name on the side and use its reference. 727 char *StrPtr = DebugInfoNames.Allocate<char>(Name.length()); 728 memcpy(StrPtr, Name.data(), Name.length()); 729 return llvm::StringRef(StrPtr, Name.length()); 730} 731 732 733/// CollectVtableInfo - If the C++ class has vtable info then insert appropriate 734/// debug info entry in EltTys vector. 735void CGDebugInfo:: 736CollectVtableInfo(const CXXRecordDecl *RD, llvm::DICompileUnit Unit, 737 llvm::SmallVectorImpl<llvm::DIDescriptor> &EltTys) { 738 const ASTRecordLayout &RL = CGM.getContext().getASTRecordLayout(RD); 739 740 // If there is a primary base then it will hold vtable info. 741 if (RL.getPrimaryBase()) 742 return; 743 744 // If this class is not dynamic then there is not any vtable info to collect. 745 if (!RD->isDynamicClass()) 746 return; 747 748 unsigned Size = CGM.getContext().getTypeSize(CGM.getContext().VoidPtrTy); 749 llvm::DIType VPTR 750 = DebugFactory.CreateDerivedType(llvm::dwarf::DW_TAG_member, Unit, 751 getVtableName(RD), llvm::DICompileUnit(), 752 0, Size, 0, 0, 0, 753 getOrCreateVTablePtrType(Unit)); 754 EltTys.push_back(VPTR); 755} 756 757/// CreateType - get structure or union type. 758llvm::DIType CGDebugInfo::CreateType(const RecordType *Ty, 759 llvm::DICompileUnit Unit) { 760 RecordDecl *RD = Ty->getDecl(); 761 762 unsigned Tag; 763 if (RD->isStruct()) 764 Tag = llvm::dwarf::DW_TAG_structure_type; 765 else if (RD->isUnion()) 766 Tag = llvm::dwarf::DW_TAG_union_type; 767 else { 768 assert(RD->isClass() && "Unknown RecordType!"); 769 Tag = llvm::dwarf::DW_TAG_class_type; 770 } 771 772 SourceManager &SM = CGM.getContext().getSourceManager(); 773 774 // Get overall information about the record type for the debug info. 775 PresumedLoc PLoc = SM.getPresumedLoc(RD->getLocation()); 776 llvm::DICompileUnit DefUnit; 777 unsigned Line = 0; 778 if (!PLoc.isInvalid()) { 779 DefUnit = getOrCreateCompileUnit(RD->getLocation()); 780 Line = PLoc.getLine(); 781 } 782 783 // Records and classes and unions can all be recursive. To handle them, we 784 // first generate a debug descriptor for the struct as a forward declaration. 785 // Then (if it is a definition) we go through and get debug info for all of 786 // its members. Finally, we create a descriptor for the complete type (which 787 // may refer to the forward decl if the struct is recursive) and replace all 788 // uses of the forward declaration with the final definition. 789 790 // A RD->getName() is not unique. However, the debug info descriptors 791 // are uniqued so use type name to ensure uniquness. 792 std::string STy = QualType(Ty, 0).getAsString(); 793 llvm::DIDescriptor FDContext = 794 getContextDescriptor(dyn_cast<Decl>(RD->getDeclContext()), Unit); 795 llvm::DICompositeType FwdDecl = 796 DebugFactory.CreateCompositeType(Tag, FDContext, 797 STy.c_str(), 798 DefUnit, Line, 0, 0, 0, 0, 799 llvm::DIType(), llvm::DIArray()); 800 801 // If this is just a forward declaration, return it. 802 if (!RD->getDefinition()) 803 return FwdDecl; 804 805 llvm::TrackingVH<llvm::MDNode> FwdDeclNode = FwdDecl.getNode(); 806 // Otherwise, insert it into the TypeCache so that recursive uses will find 807 // it. 808 TypeCache[QualType(Ty, 0).getAsOpaquePtr()] = FwdDecl.getNode(); 809 810 // Convert all the elements. 811 llvm::SmallVector<llvm::DIDescriptor, 16> EltTys; 812 813 const CXXRecordDecl *CXXDecl = dyn_cast<CXXRecordDecl>(RD); 814 if (CXXDecl) { 815 CollectCXXBases(CXXDecl, Unit, EltTys, FwdDecl); 816 CollectVtableInfo(CXXDecl, Unit, EltTys); 817 } 818 CollectRecordFields(RD, Unit, EltTys); 819 llvm::MDNode *ContainingType = NULL; 820 if (CXXDecl) { 821 CollectCXXMemberFunctions(CXXDecl, Unit, EltTys, FwdDecl); 822 823 // A class's primary base or the class itself contains the vtable. 824 const ASTRecordLayout &RL = CGM.getContext().getASTRecordLayout(RD); 825 if (const CXXRecordDecl *PBase = RL.getPrimaryBase()) 826 ContainingType = 827 getOrCreateType(QualType(PBase->getTypeForDecl(), 0), Unit).getNode(); 828 else if (CXXDecl->isDynamicClass()) 829 ContainingType = FwdDecl.getNode(); 830 } 831 832 llvm::DIArray Elements = 833 DebugFactory.GetOrCreateArray(EltTys.data(), EltTys.size()); 834 835 // Bit size, align and offset of the type. 836 uint64_t Size = CGM.getContext().getTypeSize(Ty); 837 uint64_t Align = CGM.getContext().getTypeAlign(Ty); 838 839 llvm::DIDescriptor RDContext = 840 getContextDescriptor(dyn_cast<Decl>(RD->getDeclContext()), Unit); 841 llvm::DICompositeType RealDecl = 842 DebugFactory.CreateCompositeType(Tag, RDContext, 843 RD->getName(), 844 DefUnit, Line, Size, Align, 0, 0, 845 llvm::DIType(), Elements, 846 0, ContainingType); 847 848 // Now that we have a real decl for the struct, replace anything using the 849 // old decl with the new one. This will recursively update the debug info. 850 llvm::DIDerivedType(FwdDeclNode).replaceAllUsesWith(RealDecl); 851 852 return RealDecl; 853} 854 855/// CreateType - get objective-c interface type. 856llvm::DIType CGDebugInfo::CreateType(const ObjCInterfaceType *Ty, 857 llvm::DICompileUnit Unit) { 858 ObjCInterfaceDecl *ID = Ty->getDecl(); 859 860 unsigned Tag = llvm::dwarf::DW_TAG_structure_type; 861 SourceManager &SM = CGM.getContext().getSourceManager(); 862 863 // Get overall information about the record type for the debug info. 864 llvm::DICompileUnit DefUnit = getOrCreateCompileUnit(ID->getLocation()); 865 PresumedLoc PLoc = SM.getPresumedLoc(ID->getLocation()); 866 unsigned Line = PLoc.isInvalid() ? 0 : PLoc.getLine(); 867 868 869 unsigned RuntimeLang = DefUnit.getLanguage(); 870 871 // To handle recursive interface, we 872 // first generate a debug descriptor for the struct as a forward declaration. 873 // Then (if it is a definition) we go through and get debug info for all of 874 // its members. Finally, we create a descriptor for the complete type (which 875 // may refer to the forward decl if the struct is recursive) and replace all 876 // uses of the forward declaration with the final definition. 877 llvm::DICompositeType FwdDecl = 878 DebugFactory.CreateCompositeType(Tag, Unit, ID->getName(), 879 DefUnit, Line, 0, 0, 0, 0, 880 llvm::DIType(), llvm::DIArray(), 881 RuntimeLang); 882 883 // If this is just a forward declaration, return it. 884 if (ID->isForwardDecl()) 885 return FwdDecl; 886 887 llvm::TrackingVH<llvm::MDNode> FwdDeclNode = FwdDecl.getNode(); 888 // Otherwise, insert it into the TypeCache so that recursive uses will find 889 // it. 890 TypeCache[QualType(Ty, 0).getAsOpaquePtr()] = FwdDecl.getNode(); 891 892 // Convert all the elements. 893 llvm::SmallVector<llvm::DIDescriptor, 16> EltTys; 894 895 ObjCInterfaceDecl *SClass = ID->getSuperClass(); 896 if (SClass) { 897 llvm::DIType SClassTy = 898 getOrCreateType(CGM.getContext().getObjCInterfaceType(SClass), Unit); 899 llvm::DIType InhTag = 900 DebugFactory.CreateDerivedType(llvm::dwarf::DW_TAG_inheritance, 901 Unit, "", llvm::DICompileUnit(), 0, 0, 0, 902 0 /* offset */, 0, SClassTy); 903 EltTys.push_back(InhTag); 904 } 905 906 const ASTRecordLayout &RL = CGM.getContext().getASTObjCInterfaceLayout(ID); 907 908 unsigned FieldNo = 0; 909 for (ObjCInterfaceDecl::ivar_iterator I = ID->ivar_begin(), 910 E = ID->ivar_end(); I != E; ++I, ++FieldNo) { 911 ObjCIvarDecl *Field = *I; 912 llvm::DIType FieldTy = getOrCreateType(Field->getType(), Unit); 913 914 llvm::StringRef FieldName = Field->getName(); 915 916 // Ignore unnamed fields. 917 if (FieldName.empty()) 918 continue; 919 920 // Get the location for the field. 921 SourceLocation FieldDefLoc = Field->getLocation(); 922 llvm::DICompileUnit FieldDefUnit = getOrCreateCompileUnit(FieldDefLoc); 923 PresumedLoc PLoc = SM.getPresumedLoc(FieldDefLoc); 924 unsigned FieldLine = PLoc.isInvalid() ? 0 : PLoc.getLine(); 925 926 927 QualType FType = Field->getType(); 928 uint64_t FieldSize = 0; 929 unsigned FieldAlign = 0; 930 931 if (!FType->isIncompleteArrayType()) { 932 933 // Bit size, align and offset of the type. 934 FieldSize = CGM.getContext().getTypeSize(FType); 935 Expr *BitWidth = Field->getBitWidth(); 936 if (BitWidth) 937 FieldSize = BitWidth->EvaluateAsInt(CGM.getContext()).getZExtValue(); 938 939 FieldAlign = CGM.getContext().getTypeAlign(FType); 940 } 941 942 uint64_t FieldOffset = RL.getFieldOffset(FieldNo); 943 944 unsigned Flags = 0; 945 if (Field->getAccessControl() == ObjCIvarDecl::Protected) 946 Flags = llvm::DIType::FlagProtected; 947 else if (Field->getAccessControl() == ObjCIvarDecl::Private) 948 Flags = llvm::DIType::FlagPrivate; 949 950 // Create a DW_TAG_member node to remember the offset of this field in the 951 // struct. FIXME: This is an absolutely insane way to capture this 952 // information. When we gut debug info, this should be fixed. 953 FieldTy = DebugFactory.CreateDerivedType(llvm::dwarf::DW_TAG_member, Unit, 954 FieldName, FieldDefUnit, 955 FieldLine, FieldSize, FieldAlign, 956 FieldOffset, Flags, FieldTy); 957 EltTys.push_back(FieldTy); 958 } 959 960 llvm::DIArray Elements = 961 DebugFactory.GetOrCreateArray(EltTys.data(), EltTys.size()); 962 963 // Bit size, align and offset of the type. 964 uint64_t Size = CGM.getContext().getTypeSize(Ty); 965 uint64_t Align = CGM.getContext().getTypeAlign(Ty); 966 967 llvm::DICompositeType RealDecl = 968 DebugFactory.CreateCompositeType(Tag, Unit, ID->getName(), DefUnit, 969 Line, Size, Align, 0, 0, llvm::DIType(), 970 Elements, RuntimeLang); 971 972 // Now that we have a real decl for the struct, replace anything using the 973 // old decl with the new one. This will recursively update the debug info. 974 llvm::DIDerivedType(FwdDeclNode).replaceAllUsesWith(RealDecl); 975 976 return RealDecl; 977} 978 979llvm::DIType CGDebugInfo::CreateType(const EnumType *Ty, 980 llvm::DICompileUnit Unit) { 981 EnumDecl *ED = Ty->getDecl(); 982 983 llvm::SmallVector<llvm::DIDescriptor, 32> Enumerators; 984 985 // Create DIEnumerator elements for each enumerator. 986 for (EnumDecl::enumerator_iterator 987 Enum = ED->enumerator_begin(), EnumEnd = ED->enumerator_end(); 988 Enum != EnumEnd; ++Enum) { 989 Enumerators.push_back(DebugFactory.CreateEnumerator(Enum->getName(), 990 Enum->getInitVal().getZExtValue())); 991 } 992 993 // Return a CompositeType for the enum itself. 994 llvm::DIArray EltArray = 995 DebugFactory.GetOrCreateArray(Enumerators.data(), Enumerators.size()); 996 997 SourceLocation DefLoc = ED->getLocation(); 998 llvm::DICompileUnit DefUnit = getOrCreateCompileUnit(DefLoc); 999 SourceManager &SM = CGM.getContext().getSourceManager(); 1000 PresumedLoc PLoc = SM.getPresumedLoc(DefLoc); 1001 unsigned Line = PLoc.isInvalid() ? 0 : PLoc.getLine(); 1002 1003 1004 // Size and align of the type. 1005 uint64_t Size = 0; 1006 unsigned Align = 0; 1007 if (!Ty->isIncompleteType()) { 1008 Size = CGM.getContext().getTypeSize(Ty); 1009 Align = CGM.getContext().getTypeAlign(Ty); 1010 } 1011 1012 llvm::DIType DbgTy = 1013 DebugFactory.CreateCompositeType(llvm::dwarf::DW_TAG_enumeration_type, 1014 Unit, ED->getName(), DefUnit, Line, 1015 Size, Align, 0, 0, 1016 llvm::DIType(), EltArray); 1017 return DbgTy; 1018} 1019 1020llvm::DIType CGDebugInfo::CreateType(const TagType *Ty, 1021 llvm::DICompileUnit Unit) { 1022 if (const RecordType *RT = dyn_cast<RecordType>(Ty)) 1023 return CreateType(RT, Unit); 1024 else if (const EnumType *ET = dyn_cast<EnumType>(Ty)) 1025 return CreateType(ET, Unit); 1026 1027 return llvm::DIType(); 1028} 1029 1030llvm::DIType CGDebugInfo::CreateType(const ArrayType *Ty, 1031 llvm::DICompileUnit Unit) { 1032 uint64_t Size; 1033 uint64_t Align; 1034 1035 1036 // FIXME: make getTypeAlign() aware of VLAs and incomplete array types 1037 if (const VariableArrayType *VAT = dyn_cast<VariableArrayType>(Ty)) { 1038 Size = 0; 1039 Align = 1040 CGM.getContext().getTypeAlign(CGM.getContext().getBaseElementType(VAT)); 1041 } else if (Ty->isIncompleteArrayType()) { 1042 Size = 0; 1043 Align = CGM.getContext().getTypeAlign(Ty->getElementType()); 1044 } else { 1045 // Size and align of the whole array, not the element type. 1046 Size = CGM.getContext().getTypeSize(Ty); 1047 Align = CGM.getContext().getTypeAlign(Ty); 1048 } 1049 1050 // Add the dimensions of the array. FIXME: This loses CV qualifiers from 1051 // interior arrays, do we care? Why aren't nested arrays represented the 1052 // obvious/recursive way? 1053 llvm::SmallVector<llvm::DIDescriptor, 8> Subscripts; 1054 QualType EltTy(Ty, 0); 1055 while ((Ty = dyn_cast<ArrayType>(EltTy))) { 1056 uint64_t Upper = 0; 1057 if (const ConstantArrayType *CAT = dyn_cast<ConstantArrayType>(Ty)) 1058 if (CAT->getSize().getZExtValue()) 1059 Upper = CAT->getSize().getZExtValue() - 1; 1060 // FIXME: Verify this is right for VLAs. 1061 Subscripts.push_back(DebugFactory.GetOrCreateSubrange(0, Upper)); 1062 EltTy = Ty->getElementType(); 1063 } 1064 1065 llvm::DIArray SubscriptArray = 1066 DebugFactory.GetOrCreateArray(Subscripts.data(), Subscripts.size()); 1067 1068 llvm::DIType DbgTy = 1069 DebugFactory.CreateCompositeType(llvm::dwarf::DW_TAG_array_type, 1070 Unit, "", llvm::DICompileUnit(), 1071 0, Size, Align, 0, 0, 1072 getOrCreateType(EltTy, Unit), 1073 SubscriptArray); 1074 return DbgTy; 1075} 1076 1077llvm::DIType CGDebugInfo::CreateType(const LValueReferenceType *Ty, 1078 llvm::DICompileUnit Unit) { 1079 return CreatePointerLikeType(llvm::dwarf::DW_TAG_reference_type, 1080 Ty, Ty->getPointeeType(), Unit); 1081} 1082 1083llvm::DIType CGDebugInfo::CreateType(const MemberPointerType *Ty, 1084 llvm::DICompileUnit U) { 1085 QualType PointerDiffTy = CGM.getContext().getPointerDiffType(); 1086 llvm::DIType PointerDiffDITy = getOrCreateType(PointerDiffTy, U); 1087 1088 if (!Ty->getPointeeType()->isFunctionType()) { 1089 // We have a data member pointer type. 1090 return PointerDiffDITy; 1091 } 1092 1093 // We have a member function pointer type. Treat it as a struct with two 1094 // ptrdiff_t members. 1095 std::pair<uint64_t, unsigned> Info = CGM.getContext().getTypeInfo(Ty); 1096 1097 uint64_t FieldOffset = 0; 1098 llvm::DIDescriptor ElementTypes[2]; 1099 1100 // FIXME: This should probably be a function type instead. 1101 ElementTypes[0] = 1102 DebugFactory.CreateDerivedType(llvm::dwarf::DW_TAG_member, U, 1103 "ptr", llvm::DICompileUnit(), 0, 1104 Info.first, Info.second, FieldOffset, 0, 1105 PointerDiffDITy); 1106 FieldOffset += Info.first; 1107 1108 ElementTypes[1] = 1109 DebugFactory.CreateDerivedType(llvm::dwarf::DW_TAG_member, U, 1110 "ptr", llvm::DICompileUnit(), 0, 1111 Info.first, Info.second, FieldOffset, 0, 1112 PointerDiffDITy); 1113 1114 llvm::DIArray Elements = 1115 DebugFactory.GetOrCreateArray(&ElementTypes[0], 1116 llvm::array_lengthof(ElementTypes)); 1117 1118 return DebugFactory.CreateCompositeType(llvm::dwarf::DW_TAG_structure_type, 1119 U, llvm::StringRef("test"), 1120 llvm::DICompileUnit(), 0, FieldOffset, 1121 0, 0, 0, llvm::DIType(), Elements); 1122} 1123 1124static QualType UnwrapTypeForDebugInfo(QualType T) { 1125 do { 1126 QualType LastT = T; 1127 switch (T->getTypeClass()) { 1128 default: 1129 return T; 1130 case Type::TemplateSpecialization: 1131 T = cast<TemplateSpecializationType>(T)->desugar(); 1132 break; 1133 case Type::TypeOfExpr: { 1134 TypeOfExprType *Ty = cast<TypeOfExprType>(T); 1135 T = Ty->getUnderlyingExpr()->getType(); 1136 break; 1137 } 1138 case Type::TypeOf: 1139 T = cast<TypeOfType>(T)->getUnderlyingType(); 1140 break; 1141 case Type::Decltype: 1142 T = cast<DecltypeType>(T)->getUnderlyingType(); 1143 break; 1144 case Type::QualifiedName: 1145 T = cast<QualifiedNameType>(T)->getNamedType(); 1146 break; 1147 case Type::SubstTemplateTypeParm: 1148 T = cast<SubstTemplateTypeParmType>(T)->getReplacementType(); 1149 break; 1150 case Type::Elaborated: 1151 T = cast<ElaboratedType>(T)->getUnderlyingType(); 1152 break; 1153 } 1154 1155 assert(T != LastT && "Type unwrapping failed to unwrap!"); 1156 if (T == LastT) 1157 return T; 1158 } while (true); 1159 1160 return T; 1161} 1162 1163/// getOrCreateType - Get the type from the cache or create a new 1164/// one if necessary. 1165llvm::DIType CGDebugInfo::getOrCreateType(QualType Ty, 1166 llvm::DICompileUnit Unit) { 1167 if (Ty.isNull()) 1168 return llvm::DIType(); 1169 1170 // Unwrap the type as needed for debug information. 1171 Ty = UnwrapTypeForDebugInfo(Ty); 1172 1173 // Check for existing entry. 1174 std::map<void *, llvm::WeakVH>::iterator it = 1175 TypeCache.find(Ty.getAsOpaquePtr()); 1176 if (it != TypeCache.end()) { 1177 // Verify that the debug info still exists. 1178 if (&*it->second) 1179 return llvm::DIType(cast<llvm::MDNode>(it->second)); 1180 } 1181 1182 // Otherwise create the type. 1183 llvm::DIType Res = CreateTypeNode(Ty, Unit); 1184 1185 // And update the type cache. 1186 TypeCache[Ty.getAsOpaquePtr()] = Res.getNode(); 1187 return Res; 1188} 1189 1190/// CreateTypeNode - Create a new debug type node. 1191llvm::DIType CGDebugInfo::CreateTypeNode(QualType Ty, 1192 llvm::DICompileUnit Unit) { 1193 // Handle qualifiers, which recursively handles what they refer to. 1194 if (Ty.hasLocalQualifiers()) 1195 return CreateQualifiedType(Ty, Unit); 1196 1197 const char *Diag = 0; 1198 1199 // Work out details of type. 1200 switch (Ty->getTypeClass()) { 1201#define TYPE(Class, Base) 1202#define ABSTRACT_TYPE(Class, Base) 1203#define NON_CANONICAL_TYPE(Class, Base) 1204#define DEPENDENT_TYPE(Class, Base) case Type::Class: 1205#include "clang/AST/TypeNodes.def" 1206 assert(false && "Dependent types cannot show up in debug information"); 1207 1208 // FIXME: Handle these. 1209 case Type::ExtVector: 1210 case Type::Vector: 1211 return llvm::DIType(); 1212 1213 case Type::ObjCObjectPointer: 1214 return CreateType(cast<ObjCObjectPointerType>(Ty), Unit); 1215 case Type::ObjCInterface: 1216 return CreateType(cast<ObjCInterfaceType>(Ty), Unit); 1217 case Type::Builtin: return CreateType(cast<BuiltinType>(Ty), Unit); 1218 case Type::Complex: return CreateType(cast<ComplexType>(Ty), Unit); 1219 case Type::Pointer: return CreateType(cast<PointerType>(Ty), Unit); 1220 case Type::BlockPointer: 1221 return CreateType(cast<BlockPointerType>(Ty), Unit); 1222 case Type::Typedef: return CreateType(cast<TypedefType>(Ty), Unit); 1223 case Type::Record: 1224 case Type::Enum: 1225 return CreateType(cast<TagType>(Ty), Unit); 1226 case Type::FunctionProto: 1227 case Type::FunctionNoProto: 1228 return CreateType(cast<FunctionType>(Ty), Unit); 1229 case Type::ConstantArray: 1230 case Type::VariableArray: 1231 case Type::IncompleteArray: 1232 return CreateType(cast<ArrayType>(Ty), Unit); 1233 1234 case Type::LValueReference: 1235 return CreateType(cast<LValueReferenceType>(Ty), Unit); 1236 1237 case Type::MemberPointer: 1238 return CreateType(cast<MemberPointerType>(Ty), Unit); 1239 1240 case Type::TemplateSpecialization: 1241 case Type::Elaborated: 1242 case Type::QualifiedName: 1243 case Type::SubstTemplateTypeParm: 1244 case Type::TypeOfExpr: 1245 case Type::TypeOf: 1246 case Type::Decltype: 1247 llvm_unreachable("type should have been unwrapped!"); 1248 return llvm::DIType(); 1249 1250 case Type::RValueReference: 1251 // FIXME: Implement! 1252 Diag = "rvalue references"; 1253 break; 1254 } 1255 1256 assert(Diag && "Fall through without a diagnostic?"); 1257 unsigned DiagID = CGM.getDiags().getCustomDiagID(Diagnostic::Error, 1258 "debug information for %0 is not yet supported"); 1259 CGM.getDiags().Report(FullSourceLoc(), DiagID) 1260 << Diag; 1261 return llvm::DIType(); 1262} 1263 1264/// EmitFunctionStart - Constructs the debug code for entering a function - 1265/// "llvm.dbg.func.start.". 1266void CGDebugInfo::EmitFunctionStart(GlobalDecl GD, QualType FnType, 1267 llvm::Function *Fn, 1268 CGBuilderTy &Builder) { 1269 1270 llvm::StringRef Name; 1271 llvm::StringRef LinkageName; 1272 1273 const Decl *D = GD.getDecl(); 1274 if (const FunctionDecl *FD = dyn_cast<FunctionDecl>(D)) { 1275 // If there is a DISubprogram for this function available then use it. 1276 llvm::DenseMap<const FunctionDecl *, llvm::WeakVH>::iterator 1277 FI = SPCache.find(FD); 1278 if (FI != SPCache.end()) { 1279 llvm::DISubprogram SP(dyn_cast_or_null<llvm::MDNode>(FI->second)); 1280 if (!SP.isNull() && SP.isSubprogram() && SP.isDefinition()) { 1281 RegionStack.push_back(SP.getNode()); 1282 RegionMap[D] = llvm::WeakVH(SP.getNode()); 1283 return; 1284 } 1285 } 1286 Name = getFunctionName(FD); 1287 if (!Name.empty() && Name[0] == '\01') 1288 Name = Name.substr(1); 1289 // Use mangled name as linkage name for c/c++ functions. 1290 LinkageName = CGM.getMangledName(GD); 1291 } else { 1292 // Use llvm function name as linkage name. 1293 Name = Fn->getName(); 1294 LinkageName = Name; 1295 if (!Name.empty() && Name[0] == '\01') 1296 Name = Name.substr(1); 1297 } 1298 1299 // It is expected that CurLoc is set before using EmitFunctionStart. 1300 // Usually, CurLoc points to the left bracket location of compound 1301 // statement representing function body. 1302 llvm::DICompileUnit Unit = getOrCreateCompileUnit(CurLoc); 1303 SourceManager &SM = CGM.getContext().getSourceManager(); 1304 unsigned LineNo = SM.getPresumedLoc(CurLoc).getLine(); 1305 1306 llvm::DISubprogram SP = 1307 DebugFactory.CreateSubprogram(Unit, Name, Name, LinkageName, Unit, LineNo, 1308 getOrCreateType(FnType, Unit), 1309 Fn->hasInternalLinkage(), true/*definition*/); 1310 1311 // Push function on region stack. 1312 RegionStack.push_back(SP.getNode()); 1313 RegionMap[D] = llvm::WeakVH(SP.getNode()); 1314} 1315 1316 1317void CGDebugInfo::EmitStopPoint(llvm::Function *Fn, CGBuilderTy &Builder) { 1318 if (CurLoc.isInvalid() || CurLoc.isMacroID()) return; 1319 1320 // Don't bother if things are the same as last time. 1321 SourceManager &SM = CGM.getContext().getSourceManager(); 1322 if (CurLoc == PrevLoc 1323 || (SM.getInstantiationLineNumber(CurLoc) == 1324 SM.getInstantiationLineNumber(PrevLoc) 1325 && SM.isFromSameFile(CurLoc, PrevLoc))) 1326 return; 1327 1328 // Update last state. 1329 PrevLoc = CurLoc; 1330 1331 // Get the appropriate compile unit. 1332 llvm::DICompileUnit Unit = getOrCreateCompileUnit(CurLoc); 1333 PresumedLoc PLoc = SM.getPresumedLoc(CurLoc); 1334 1335 llvm::DIDescriptor DR(RegionStack.back()); 1336 llvm::DIScope DS = llvm::DIScope(DR.getNode()); 1337 llvm::DILocation DO(NULL); 1338 llvm::DILocation DL = 1339 DebugFactory.CreateLocation(PLoc.getLine(), PLoc.getColumn(), 1340 DS, DO); 1341 Builder.SetCurrentDebugLocation(DL.getNode()); 1342} 1343 1344/// EmitRegionStart- Constructs the debug code for entering a declarative 1345/// region - "llvm.dbg.region.start.". 1346void CGDebugInfo::EmitRegionStart(llvm::Function *Fn, CGBuilderTy &Builder) { 1347 llvm::DIDescriptor D = 1348 DebugFactory.CreateLexicalBlock(RegionStack.empty() ? 1349 llvm::DIDescriptor() : 1350 llvm::DIDescriptor(RegionStack.back())); 1351 RegionStack.push_back(D.getNode()); 1352} 1353 1354/// EmitRegionEnd - Constructs the debug code for exiting a declarative 1355/// region - "llvm.dbg.region.end." 1356void CGDebugInfo::EmitRegionEnd(llvm::Function *Fn, CGBuilderTy &Builder) { 1357 assert(!RegionStack.empty() && "Region stack mismatch, stack empty!"); 1358 1359 // Provide an region stop point. 1360 EmitStopPoint(Fn, Builder); 1361 1362 RegionStack.pop_back(); 1363} 1364 1365// EmitTypeForVarWithBlocksAttr - Build up structure info for the byref. 1366// See BuildByRefType. 1367llvm::DIType CGDebugInfo::EmitTypeForVarWithBlocksAttr(const ValueDecl *VD, 1368 uint64_t *XOffset) { 1369 1370 llvm::SmallVector<llvm::DIDescriptor, 5> EltTys; 1371 1372 QualType FType; 1373 uint64_t FieldSize, FieldOffset; 1374 unsigned FieldAlign; 1375 1376 llvm::DICompileUnit Unit = getOrCreateCompileUnit(VD->getLocation()); 1377 QualType Type = VD->getType(); 1378 1379 FieldOffset = 0; 1380 FType = CGM.getContext().getPointerType(CGM.getContext().VoidTy); 1381 llvm::DIType FieldTy = CGDebugInfo::getOrCreateType(FType, Unit); 1382 FieldSize = CGM.getContext().getTypeSize(FType); 1383 FieldAlign = CGM.getContext().getTypeAlign(FType); 1384 FieldTy = DebugFactory.CreateDerivedType(llvm::dwarf::DW_TAG_member, Unit, 1385 "__isa", llvm::DICompileUnit(), 1386 0, FieldSize, FieldAlign, 1387 FieldOffset, 0, FieldTy); 1388 EltTys.push_back(FieldTy); 1389 FieldOffset += FieldSize; 1390 1391 FType = CGM.getContext().getPointerType(CGM.getContext().VoidTy); 1392 FieldTy = CGDebugInfo::getOrCreateType(FType, Unit); 1393 FieldSize = CGM.getContext().getTypeSize(FType); 1394 FieldAlign = CGM.getContext().getTypeAlign(FType); 1395 FieldTy = DebugFactory.CreateDerivedType(llvm::dwarf::DW_TAG_member, Unit, 1396 "__forwarding", llvm::DICompileUnit(), 1397 0, FieldSize, FieldAlign, 1398 FieldOffset, 0, FieldTy); 1399 EltTys.push_back(FieldTy); 1400 FieldOffset += FieldSize; 1401 1402 FType = CGM.getContext().IntTy; 1403 FieldTy = CGDebugInfo::getOrCreateType(FType, Unit); 1404 FieldSize = CGM.getContext().getTypeSize(FType); 1405 FieldAlign = CGM.getContext().getTypeAlign(FType); 1406 FieldTy = DebugFactory.CreateDerivedType(llvm::dwarf::DW_TAG_member, Unit, 1407 "__flags", llvm::DICompileUnit(), 1408 0, FieldSize, FieldAlign, 1409 FieldOffset, 0, FieldTy); 1410 EltTys.push_back(FieldTy); 1411 FieldOffset += FieldSize; 1412 1413 FType = CGM.getContext().IntTy; 1414 FieldTy = CGDebugInfo::getOrCreateType(FType, Unit); 1415 FieldSize = CGM.getContext().getTypeSize(FType); 1416 FieldAlign = CGM.getContext().getTypeAlign(FType); 1417 FieldTy = DebugFactory.CreateDerivedType(llvm::dwarf::DW_TAG_member, Unit, 1418 "__size", llvm::DICompileUnit(), 1419 0, FieldSize, FieldAlign, 1420 FieldOffset, 0, FieldTy); 1421 EltTys.push_back(FieldTy); 1422 FieldOffset += FieldSize; 1423 1424 bool HasCopyAndDispose = CGM.BlockRequiresCopying(Type); 1425 if (HasCopyAndDispose) { 1426 FType = CGM.getContext().getPointerType(CGM.getContext().VoidTy); 1427 FieldTy = CGDebugInfo::getOrCreateType(FType, Unit); 1428 FieldSize = CGM.getContext().getTypeSize(FType); 1429 FieldAlign = CGM.getContext().getTypeAlign(FType); 1430 FieldTy = DebugFactory.CreateDerivedType(llvm::dwarf::DW_TAG_member, Unit, 1431 "__copy_helper", 1432 llvm::DICompileUnit(), 1433 0, FieldSize, FieldAlign, 1434 FieldOffset, 0, FieldTy); 1435 EltTys.push_back(FieldTy); 1436 FieldOffset += FieldSize; 1437 1438 FType = CGM.getContext().getPointerType(CGM.getContext().VoidTy); 1439 FieldTy = CGDebugInfo::getOrCreateType(FType, Unit); 1440 FieldSize = CGM.getContext().getTypeSize(FType); 1441 FieldAlign = CGM.getContext().getTypeAlign(FType); 1442 FieldTy = DebugFactory.CreateDerivedType(llvm::dwarf::DW_TAG_member, Unit, 1443 "__destroy_helper", 1444 llvm::DICompileUnit(), 1445 0, FieldSize, FieldAlign, 1446 FieldOffset, 0, FieldTy); 1447 EltTys.push_back(FieldTy); 1448 FieldOffset += FieldSize; 1449 } 1450 1451 CharUnits Align = CGM.getContext().getDeclAlign(VD); 1452 if (Align > CharUnits::fromQuantity( 1453 CGM.getContext().Target.getPointerAlign(0) / 8)) { 1454 unsigned AlignedOffsetInBytes 1455 = llvm::RoundUpToAlignment(FieldOffset/8, Align.getQuantity()); 1456 unsigned NumPaddingBytes 1457 = AlignedOffsetInBytes - FieldOffset/8; 1458 1459 if (NumPaddingBytes > 0) { 1460 llvm::APInt pad(32, NumPaddingBytes); 1461 FType = CGM.getContext().getConstantArrayType(CGM.getContext().CharTy, 1462 pad, ArrayType::Normal, 0); 1463 FieldTy = CGDebugInfo::getOrCreateType(FType, Unit); 1464 FieldSize = CGM.getContext().getTypeSize(FType); 1465 FieldAlign = CGM.getContext().getTypeAlign(FType); 1466 FieldTy = DebugFactory.CreateDerivedType(llvm::dwarf::DW_TAG_member, 1467 Unit, "", llvm::DICompileUnit(), 1468 0, FieldSize, FieldAlign, 1469 FieldOffset, 0, FieldTy); 1470 EltTys.push_back(FieldTy); 1471 FieldOffset += FieldSize; 1472 } 1473 } 1474 1475 FType = Type; 1476 FieldTy = CGDebugInfo::getOrCreateType(FType, Unit); 1477 FieldSize = CGM.getContext().getTypeSize(FType); 1478 FieldAlign = Align.getQuantity()*8; 1479 1480 *XOffset = FieldOffset; 1481 FieldTy = DebugFactory.CreateDerivedType(llvm::dwarf::DW_TAG_member, Unit, 1482 VD->getName(), llvm::DICompileUnit(), 1483 0, FieldSize, FieldAlign, 1484 FieldOffset, 0, FieldTy); 1485 EltTys.push_back(FieldTy); 1486 FieldOffset += FieldSize; 1487 1488 llvm::DIArray Elements = 1489 DebugFactory.GetOrCreateArray(EltTys.data(), EltTys.size()); 1490 1491 unsigned Flags = llvm::DIType::FlagBlockByrefStruct; 1492 1493 return DebugFactory.CreateCompositeType(llvm::dwarf::DW_TAG_structure_type, 1494 Unit, "", 1495 llvm::DICompileUnit(), 1496 0, FieldOffset, 0, 0, Flags, 1497 llvm::DIType(), Elements); 1498 1499} 1500/// EmitDeclare - Emit local variable declaration debug info. 1501void CGDebugInfo::EmitDeclare(const VarDecl *VD, unsigned Tag, 1502 llvm::Value *Storage, CGBuilderTy &Builder) { 1503 assert(!RegionStack.empty() && "Region stack mismatch, stack empty!"); 1504 1505 // Do not emit variable debug information while generating optimized code. 1506 // The llvm optimizer and code generator are not yet ready to support 1507 // optimized code debugging. 1508 const CodeGenOptions &CGO = CGM.getCodeGenOpts(); 1509 if (CGO.OptimizationLevel) 1510 return; 1511 1512 llvm::DICompileUnit Unit = getOrCreateCompileUnit(VD->getLocation()); 1513 llvm::DIType Ty; 1514 uint64_t XOffset = 0; 1515 if (VD->hasAttr<BlocksAttr>()) 1516 Ty = EmitTypeForVarWithBlocksAttr(VD, &XOffset); 1517 else 1518 Ty = getOrCreateType(VD->getType(), Unit); 1519 1520 // Get location information. 1521 SourceManager &SM = CGM.getContext().getSourceManager(); 1522 PresumedLoc PLoc = SM.getPresumedLoc(VD->getLocation()); 1523 unsigned Line = 0; 1524 unsigned Column = 0; 1525 if (PLoc.isInvalid()) 1526 PLoc = SM.getPresumedLoc(CurLoc); 1527 if (PLoc.isValid()) { 1528 Line = PLoc.getLine(); 1529 Column = PLoc.getColumn(); 1530 Unit = getOrCreateCompileUnit(CurLoc); 1531 } else { 1532 Unit = llvm::DICompileUnit(); 1533 } 1534 1535 // Create the descriptor for the variable. 1536 llvm::DIVariable D = 1537 DebugFactory.CreateVariable(Tag, llvm::DIDescriptor(RegionStack.back()), 1538 VD->getName(), 1539 Unit, Line, Ty); 1540 // Insert an llvm.dbg.declare into the current block. 1541 llvm::Instruction *Call = 1542 DebugFactory.InsertDeclare(Storage, D, Builder.GetInsertBlock()); 1543 1544 llvm::DIScope DS(RegionStack.back()); 1545 llvm::DILocation DO(NULL); 1546 llvm::DILocation DL = DebugFactory.CreateLocation(Line, Column, DS, DO); 1547 1548 Call->setMetadata("dbg", DL.getNode()); 1549} 1550 1551/// EmitDeclare - Emit local variable declaration debug info. 1552void CGDebugInfo::EmitDeclare(const BlockDeclRefExpr *BDRE, unsigned Tag, 1553 llvm::Value *Storage, CGBuilderTy &Builder, 1554 CodeGenFunction *CGF) { 1555 const ValueDecl *VD = BDRE->getDecl(); 1556 assert(!RegionStack.empty() && "Region stack mismatch, stack empty!"); 1557 1558 // Do not emit variable debug information while generating optimized code. 1559 // The llvm optimizer and code generator are not yet ready to support 1560 // optimized code debugging. 1561 const CodeGenOptions &CGO = CGM.getCodeGenOpts(); 1562 if (CGO.OptimizationLevel || Builder.GetInsertBlock() == 0) 1563 return; 1564 1565 uint64_t XOffset = 0; 1566 llvm::DICompileUnit Unit = getOrCreateCompileUnit(VD->getLocation()); 1567 llvm::DIType Ty; 1568 if (VD->hasAttr<BlocksAttr>()) 1569 Ty = EmitTypeForVarWithBlocksAttr(VD, &XOffset); 1570 else 1571 Ty = getOrCreateType(VD->getType(), Unit); 1572 1573 // Get location information. 1574 SourceManager &SM = CGM.getContext().getSourceManager(); 1575 PresumedLoc PLoc = SM.getPresumedLoc(VD->getLocation()); 1576 unsigned Line = 0; 1577 if (!PLoc.isInvalid()) 1578 Line = PLoc.getLine(); 1579 else 1580 Unit = llvm::DICompileUnit(); 1581 1582 CharUnits offset = CGF->BlockDecls[VD]; 1583 llvm::SmallVector<llvm::Value *, 9> addr; 1584 const llvm::Type *Int64Ty = llvm::Type::getInt64Ty(CGM.getLLVMContext()); 1585 addr.push_back(llvm::ConstantInt::get(Int64Ty, llvm::DIFactory::OpDeref)); 1586 addr.push_back(llvm::ConstantInt::get(Int64Ty, llvm::DIFactory::OpPlus)); 1587 addr.push_back(llvm::ConstantInt::get(Int64Ty, offset.getQuantity())); 1588 if (BDRE->isByRef()) { 1589 addr.push_back(llvm::ConstantInt::get(Int64Ty, llvm::DIFactory::OpDeref)); 1590 addr.push_back(llvm::ConstantInt::get(Int64Ty, llvm::DIFactory::OpPlus)); 1591 // offset of __forwarding field 1592 offset = CharUnits::fromQuantity(CGF->LLVMPointerWidth/8); 1593 addr.push_back(llvm::ConstantInt::get(Int64Ty, offset.getQuantity())); 1594 addr.push_back(llvm::ConstantInt::get(Int64Ty, llvm::DIFactory::OpDeref)); 1595 addr.push_back(llvm::ConstantInt::get(Int64Ty, llvm::DIFactory::OpPlus)); 1596 // offset of x field 1597 offset = CharUnits::fromQuantity(XOffset/8); 1598 addr.push_back(llvm::ConstantInt::get(Int64Ty, offset.getQuantity())); 1599 } 1600 1601 // Create the descriptor for the variable. 1602 llvm::DIVariable D = 1603 DebugFactory.CreateComplexVariable(Tag, 1604 llvm::DIDescriptor(RegionStack.back()), 1605 VD->getName(), Unit, Line, Ty, 1606 addr); 1607 // Insert an llvm.dbg.declare into the current block. 1608 llvm::Instruction *Call = 1609 DebugFactory.InsertDeclare(Storage, D, Builder.GetInsertBlock()); 1610 1611 llvm::DIScope DS(RegionStack.back()); 1612 llvm::DILocation DO(NULL); 1613 llvm::DILocation DL = 1614 DebugFactory.CreateLocation(Line, PLoc.getColumn(), DS, DO); 1615 1616 Call->setMetadata("dbg", DL.getNode()); 1617} 1618 1619void CGDebugInfo::EmitDeclareOfAutoVariable(const VarDecl *VD, 1620 llvm::Value *Storage, 1621 CGBuilderTy &Builder) { 1622 EmitDeclare(VD, llvm::dwarf::DW_TAG_auto_variable, Storage, Builder); 1623} 1624 1625void CGDebugInfo::EmitDeclareOfBlockDeclRefVariable( 1626 const BlockDeclRefExpr *BDRE, llvm::Value *Storage, CGBuilderTy &Builder, 1627 CodeGenFunction *CGF) { 1628 EmitDeclare(BDRE, llvm::dwarf::DW_TAG_auto_variable, Storage, Builder, CGF); 1629} 1630 1631/// EmitDeclareOfArgVariable - Emit call to llvm.dbg.declare for an argument 1632/// variable declaration. 1633void CGDebugInfo::EmitDeclareOfArgVariable(const VarDecl *VD, llvm::Value *AI, 1634 CGBuilderTy &Builder) { 1635 EmitDeclare(VD, llvm::dwarf::DW_TAG_arg_variable, AI, Builder); 1636} 1637 1638 1639 1640/// EmitGlobalVariable - Emit information about a global variable. 1641void CGDebugInfo::EmitGlobalVariable(llvm::GlobalVariable *Var, 1642 const VarDecl *D) { 1643 1644 // Create global variable debug descriptor. 1645 llvm::DICompileUnit Unit = getOrCreateCompileUnit(D->getLocation()); 1646 SourceManager &SM = CGM.getContext().getSourceManager(); 1647 PresumedLoc PLoc = SM.getPresumedLoc(D->getLocation()); 1648 unsigned LineNo = PLoc.isInvalid() ? 0 : PLoc.getLine(); 1649 1650 QualType T = D->getType(); 1651 if (T->isIncompleteArrayType()) { 1652 1653 // CodeGen turns int[] into int[1] so we'll do the same here. 1654 llvm::APSInt ConstVal(32); 1655 1656 ConstVal = 1; 1657 QualType ET = CGM.getContext().getAsArrayType(T)->getElementType(); 1658 1659 T = CGM.getContext().getConstantArrayType(ET, ConstVal, 1660 ArrayType::Normal, 0); 1661 } 1662 llvm::StringRef DeclName = D->getName(); 1663 llvm::DIDescriptor DContext = 1664 getContextDescriptor(dyn_cast<Decl>(D->getDeclContext()), Unit); 1665 DebugFactory.CreateGlobalVariable(DContext, DeclName, 1666 DeclName, llvm::StringRef(), Unit, LineNo, 1667 getOrCreateType(T, Unit), 1668 Var->hasInternalLinkage(), 1669 true/*definition*/, Var); 1670} 1671 1672/// EmitGlobalVariable - Emit information about an objective-c interface. 1673void CGDebugInfo::EmitGlobalVariable(llvm::GlobalVariable *Var, 1674 ObjCInterfaceDecl *ID) { 1675 // Create global variable debug descriptor. 1676 llvm::DICompileUnit Unit = getOrCreateCompileUnit(ID->getLocation()); 1677 SourceManager &SM = CGM.getContext().getSourceManager(); 1678 PresumedLoc PLoc = SM.getPresumedLoc(ID->getLocation()); 1679 unsigned LineNo = PLoc.isInvalid() ? 0 : PLoc.getLine(); 1680 1681 llvm::StringRef Name = ID->getName(); 1682 1683 QualType T = CGM.getContext().getObjCInterfaceType(ID); 1684 if (T->isIncompleteArrayType()) { 1685 1686 // CodeGen turns int[] into int[1] so we'll do the same here. 1687 llvm::APSInt ConstVal(32); 1688 1689 ConstVal = 1; 1690 QualType ET = CGM.getContext().getAsArrayType(T)->getElementType(); 1691 1692 T = CGM.getContext().getConstantArrayType(ET, ConstVal, 1693 ArrayType::Normal, 0); 1694 } 1695 1696 DebugFactory.CreateGlobalVariable(Unit, Name, Name, Name, Unit, LineNo, 1697 getOrCreateType(T, Unit), 1698 Var->hasInternalLinkage(), 1699 true/*definition*/, Var); 1700} 1701 1702/// getOrCreateNamesSpace - Return namespace descriptor for the given 1703/// namespace decl. 1704llvm::DINameSpace 1705CGDebugInfo::getOrCreateNameSpace(const NamespaceDecl *NSDecl, 1706 llvm::DIDescriptor Unit) { 1707 llvm::DenseMap<const NamespaceDecl *, llvm::WeakVH>::iterator I = 1708 NameSpaceCache.find(NSDecl); 1709 if (I != NameSpaceCache.end()) 1710 return llvm::DINameSpace(cast<llvm::MDNode>(I->second)); 1711 1712 SourceManager &SM = CGM.getContext().getSourceManager(); 1713 PresumedLoc PLoc = SM.getPresumedLoc(NSDecl->getLocation()); 1714 unsigned LineNo = PLoc.isInvalid() ? 0 : PLoc.getLine(); 1715 1716 llvm::DIDescriptor Context = 1717 getContextDescriptor(dyn_cast<Decl>(NSDecl->getDeclContext()), Unit); 1718 llvm::DINameSpace NS = 1719 DebugFactory.CreateNameSpace(Context, NSDecl->getName(), 1720 llvm::DICompileUnit(Unit.getNode()), LineNo); 1721 NameSpaceCache[NSDecl] = llvm::WeakVH(NS.getNode()); 1722 return NS; 1723} 1724