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