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