CGDebugInfo.cpp revision 4db4c9cc3e668ab713a5d1a9bad342c939ec61c6
1//===--- CGDebugInfo.cpp - Emit Debug Information for a Module ------------===// 2// 3// The LLVM Compiler Infrastructure 4// 5// This file is distributed under the University of Illinois Open Source 6// License. See LICENSE.TXT for details. 7// 8//===----------------------------------------------------------------------===// 9// 10// This coordinates the debug information generation while generating code. 11// 12//===----------------------------------------------------------------------===// 13 14#include "CGDebugInfo.h" 15#include "CodeGenModule.h" 16#include "clang/AST/ASTContext.h" 17#include "clang/AST/DeclObjC.h" 18#include "clang/AST/Expr.h" 19#include "clang/AST/RecordLayout.h" 20#include "clang/Basic/SourceManager.h" 21#include "clang/Basic/FileManager.h" 22#include "llvm/Constants.h" 23#include "llvm/DerivedTypes.h" 24#include "llvm/Instructions.h" 25#include "llvm/Intrinsics.h" 26#include "llvm/Module.h" 27#include "llvm/ADT/StringExtras.h" 28#include "llvm/ADT/SmallVector.h" 29#include "llvm/Support/Dwarf.h" 30#include "llvm/Target/TargetMachine.h" 31using namespace clang; 32using namespace clang::CodeGen; 33 34CGDebugInfo::CGDebugInfo(CodeGenModule *m) 35 : M(m), DebugFactory(M->getModule()) { 36} 37 38CGDebugInfo::~CGDebugInfo() { 39 assert(RegionStack.empty() && "Region stack mismatch, stack not empty!"); 40} 41 42void CGDebugInfo::setLocation(SourceLocation Loc) { 43 if (Loc.isValid()) 44 CurLoc = M->getContext().getSourceManager().getInstantiationLoc(Loc); 45} 46 47/// getOrCreateCompileUnit - Get the compile unit from the cache or create a new 48/// one if necessary. This returns null for invalid source locations. 49llvm::DICompileUnit CGDebugInfo::getOrCreateCompileUnit(SourceLocation Loc) { 50 // FIXME: Until we do a complete job of emitting debug information, 51 // we need to support making dummy compile units so that we generate 52 // "well formed" debug info. 53 const FileEntry *FE = 0; 54 55 SourceManager &SM = M->getContext().getSourceManager(); 56 if (Loc.isValid()) { 57 Loc = SM.getInstantiationLoc(Loc); 58 FE = SM.getFileEntryForID(SM.getFileID(Loc)); 59 } else { 60 // If Loc is not valid then use main file id. 61 FE = SM.getFileEntryForID(SM.getMainFileID()); 62 } 63 64 // See if this compile unit has been used before. 65 llvm::DICompileUnit &Unit = CompileUnitCache[FE]; 66 if (!Unit.isNull()) return Unit; 67 68 // Get source file information. 69 const char *FileName = FE ? FE->getName() : "<unknown>"; 70 const char *DirName = FE ? FE->getDir()->getName() : "<unknown>"; 71 72 bool isMain = (FE == SM.getFileEntryForID(SM.getMainFileID())); 73 // Create new compile unit. 74 // FIXME: Handle other language IDs as well. 75 // FIXME: Do not know how to get clang version yet. 76 return Unit = DebugFactory.CreateCompileUnit(llvm::dwarf::DW_LANG_C89, 77 FileName, DirName, "clang", 78 isMain); 79} 80 81/// CreateType - Get the Basic type from the cache or create a new 82/// one if necessary. 83llvm::DIType CGDebugInfo::CreateType(const BuiltinType *BT, 84 llvm::DICompileUnit Unit) { 85 unsigned Encoding = 0; 86 switch (BT->getKind()) { 87 default: 88 case BuiltinType::Void: 89 return llvm::DIType(); 90 case BuiltinType::UChar: 91 case BuiltinType::Char_U: Encoding = llvm::dwarf::DW_ATE_unsigned_char; break; 92 case BuiltinType::Char_S: 93 case BuiltinType::SChar: Encoding = llvm::dwarf::DW_ATE_signed_char; break; 94 case BuiltinType::UShort: 95 case BuiltinType::UInt: 96 case BuiltinType::ULong: 97 case BuiltinType::ULongLong: Encoding = llvm::dwarf::DW_ATE_unsigned; break; 98 case BuiltinType::Short: 99 case BuiltinType::Int: 100 case BuiltinType::Long: 101 case BuiltinType::LongLong: Encoding = llvm::dwarf::DW_ATE_signed; break; 102 case BuiltinType::Bool: Encoding = llvm::dwarf::DW_ATE_boolean; break; 103 case BuiltinType::Float: 104 case BuiltinType::Double: Encoding = llvm::dwarf::DW_ATE_float; break; 105 } 106 // Bit size, align and offset of the type. 107 uint64_t Size = M->getContext().getTypeSize(BT); 108 uint64_t Align = M->getContext().getTypeAlign(BT); 109 uint64_t Offset = 0; 110 111 return DebugFactory.CreateBasicType(Unit, BT->getName(), Unit, 0, Size, Align, 112 Offset, /*flags*/ 0, Encoding); 113} 114 115/// getOrCreateCVRType - Get the CVR qualified type from the cache or create 116/// a new one if necessary. 117llvm::DIType CGDebugInfo::CreateCVRType(QualType Ty, llvm::DICompileUnit Unit) { 118 // We will create one Derived type for one qualifier and recurse to handle any 119 // additional ones. 120 llvm::DIType FromTy; 121 unsigned Tag; 122 if (Ty.isConstQualified()) { 123 Tag = llvm::dwarf::DW_TAG_const_type; 124 Ty.removeConst(); 125 FromTy = getOrCreateType(Ty, Unit); 126 } else if (Ty.isVolatileQualified()) { 127 Tag = llvm::dwarf::DW_TAG_volatile_type; 128 Ty.removeVolatile(); 129 FromTy = getOrCreateType(Ty, Unit); 130 } else { 131 assert(Ty.isRestrictQualified() && "Unknown type qualifier for debug info"); 132 Tag = llvm::dwarf::DW_TAG_restrict_type; 133 Ty.removeRestrict(); 134 FromTy = getOrCreateType(Ty, Unit); 135 } 136 137 // No need to fill in the Name, Line, Size, Alignment, Offset in case of 138 // CVR derived types. 139 return DebugFactory.CreateDerivedType(Tag, Unit, "", llvm::DICompileUnit(), 140 0, 0, 0, 0, 0, FromTy); 141} 142 143llvm::DIType CGDebugInfo::CreateType(const PointerType *Ty, 144 llvm::DICompileUnit Unit) { 145 llvm::DIType EltTy = getOrCreateType(Ty->getPointeeType(), Unit); 146 147 // Bit size, align and offset of the type. 148 uint64_t Size = M->getContext().getTypeSize(Ty); 149 uint64_t Align = M->getContext().getTypeAlign(Ty); 150 151 return DebugFactory.CreateDerivedType(llvm::dwarf::DW_TAG_pointer_type, Unit, 152 "", llvm::DICompileUnit(), 153 0, Size, Align, 0, 0, EltTy); 154} 155 156llvm::DIType CGDebugInfo::CreateType(const TypedefType *Ty, 157 llvm::DICompileUnit Unit) { 158 // Typedefs are derived from some other type. If we have a typedef of a 159 // typedef, make sure to emit the whole chain. 160 llvm::DIType Src = getOrCreateType(Ty->getDecl()->getUnderlyingType(), Unit); 161 162 // We don't set size information, but do specify where the typedef was 163 // declared. 164 std::string TyName = Ty->getDecl()->getNameAsString(); 165 SourceLocation DefLoc = Ty->getDecl()->getLocation(); 166 llvm::DICompileUnit DefUnit = getOrCreateCompileUnit(DefLoc); 167 168 SourceManager &SM = M->getContext().getSourceManager(); 169 uint64_t Line = SM.getInstantiationLineNumber(DefLoc); 170 171 return DebugFactory.CreateDerivedType(llvm::dwarf::DW_TAG_typedef, Unit, 172 TyName, DefUnit, Line, 0, 0, 0, 0, Src); 173} 174 175llvm::DIType CGDebugInfo::CreateType(const FunctionType *Ty, 176 llvm::DICompileUnit Unit) { 177 llvm::SmallVector<llvm::DIDescriptor, 16> EltTys; 178 179 // Add the result type at least. 180 EltTys.push_back(getOrCreateType(Ty->getResultType(), Unit)); 181 182 // Set up remainder of arguments if there is a prototype. 183 // FIXME: IF NOT, HOW IS THIS REPRESENTED? llvm-gcc doesn't represent '...'! 184 if (const FunctionProtoType *FTP = dyn_cast<FunctionProtoType>(Ty)) { 185 for (unsigned i = 0, e = FTP->getNumArgs(); i != e; ++i) 186 EltTys.push_back(getOrCreateType(FTP->getArgType(i), Unit)); 187 } else { 188 // FIXME: Handle () case in C. llvm-gcc doesn't do it either. 189 } 190 191 llvm::DIArray EltTypeArray = 192 DebugFactory.GetOrCreateArray(&EltTys[0], EltTys.size()); 193 194 return DebugFactory.CreateCompositeType(llvm::dwarf::DW_TAG_subroutine_type, 195 Unit, "", llvm::DICompileUnit(), 196 0, 0, 0, 0, 0, 197 llvm::DIType(), EltTypeArray); 198} 199 200/// CreateType - get structure or union type. 201llvm::DIType CGDebugInfo::CreateType(const RecordType *Ty, 202 llvm::DICompileUnit Unit) { 203 RecordDecl *Decl = Ty->getDecl(); 204 205 unsigned Tag; 206 if (Decl->isStruct()) 207 Tag = llvm::dwarf::DW_TAG_structure_type; 208 else if (Decl->isUnion()) 209 Tag = llvm::dwarf::DW_TAG_union_type; 210 else { 211 assert(Decl->isClass() && "Unknown RecordType!"); 212 Tag = llvm::dwarf::DW_TAG_class_type; 213 } 214 215 SourceManager &SM = M->getContext().getSourceManager(); 216 217 // Get overall information about the record type for the debug info. 218 std::string Name = Decl->getNameAsString(); 219 220 llvm::DICompileUnit DefUnit = getOrCreateCompileUnit(Decl->getLocation()); 221 unsigned Line = SM.getInstantiationLineNumber(Decl->getLocation()); 222 223 224 // Records and classes and unions can all be recursive. To handle them, we 225 // first generate a debug descriptor for the struct as a forward declaration. 226 // Then (if it is a definition) we go through and get debug info for all of 227 // its members. Finally, we create a descriptor for the complete type (which 228 // may refer to the forward decl if the struct is recursive) and replace all 229 // uses of the forward declaration with the final definition. 230 llvm::DIType FwdDecl = 231 DebugFactory.CreateCompositeType(Tag, Unit, Name, DefUnit, Line, 0, 0, 0, 0, 232 llvm::DIType(), llvm::DIArray()); 233 234 // If this is just a forward declaration, return it. 235 if (!Decl->getDefinition(M->getContext())) 236 return FwdDecl; 237 238 // Otherwise, insert it into the TypeCache so that recursive uses will find 239 // it. 240 TypeCache[QualType(Ty, 0).getAsOpaquePtr()] = FwdDecl; 241 242 // Convert all the elements. 243 llvm::SmallVector<llvm::DIDescriptor, 16> EltTys; 244 245 const ASTRecordLayout &RL = M->getContext().getASTRecordLayout(Decl); 246 247 unsigned FieldNo = 0; 248 for (RecordDecl::field_iterator I = Decl->field_begin(), 249 E = Decl->field_end(); 250 I != E; ++I, ++FieldNo) { 251 FieldDecl *Field = *I; 252 llvm::DIType FieldTy = getOrCreateType(Field->getType(), Unit); 253 254 std::string FieldName = Field->getNameAsString(); 255 256 // Get the location for the field. 257 SourceLocation FieldDefLoc = Field->getLocation(); 258 llvm::DICompileUnit FieldDefUnit = getOrCreateCompileUnit(FieldDefLoc); 259 unsigned FieldLine = SM.getInstantiationLineNumber(FieldDefLoc); 260 261 // Bit size, align and offset of the type. 262 uint64_t FieldSize = M->getContext().getTypeSize(Ty); 263 unsigned FieldAlign = M->getContext().getTypeAlign(Ty); 264 uint64_t FieldOffset = RL.getFieldOffset(FieldNo); 265 266 // Create a DW_TAG_member node to remember the offset of this field in the 267 // struct. FIXME: This is an absolutely insane way to capture this 268 // information. When we gut debug info, this should be fixed. 269 FieldTy = DebugFactory.CreateDerivedType(llvm::dwarf::DW_TAG_member, Unit, 270 FieldName, FieldDefUnit, 271 FieldLine, FieldSize, FieldAlign, 272 FieldOffset, 0, FieldTy); 273 EltTys.push_back(FieldTy); 274 } 275 276 llvm::DIArray Elements = 277 DebugFactory.GetOrCreateArray(&EltTys[0], EltTys.size()); 278 279 // Bit size, align and offset of the type. 280 uint64_t Size = M->getContext().getTypeSize(Ty); 281 uint64_t Align = M->getContext().getTypeAlign(Ty); 282 283 llvm::DIType RealDecl = 284 DebugFactory.CreateCompositeType(Tag, Unit, Name, DefUnit, Line, Size, 285 Align, 0, 0, llvm::DIType(), Elements); 286 287 // Now that we have a real decl for the struct, replace anything using the 288 // old decl with the new one. This will recursively update the debug info. 289 FwdDecl.getGV()->replaceAllUsesWith(RealDecl.getGV()); 290 FwdDecl.getGV()->eraseFromParent(); 291 292 return RealDecl; 293} 294 295/// CreateType - get objective-c interface type. 296llvm::DIType CGDebugInfo::CreateType(const ObjCInterfaceType *Ty, 297 llvm::DICompileUnit Unit) { 298 ObjCInterfaceDecl *Decl = Ty->getDecl(); 299 300 unsigned Tag = llvm::dwarf::DW_TAG_structure_type; 301 SourceManager &SM = M->getContext().getSourceManager(); 302 303 // Get overall information about the record type for the debug info. 304 std::string Name = Decl->getNameAsString(); 305 306 llvm::DICompileUnit DefUnit = getOrCreateCompileUnit(Decl->getLocation()); 307 unsigned Line = SM.getInstantiationLineNumber(Decl->getLocation()); 308 309 310 // To handle recursive interface, we 311 // first generate a debug descriptor for the struct as a forward declaration. 312 // Then (if it is a definition) we go through and get debug info for all of 313 // its members. Finally, we create a descriptor for the complete type (which 314 // may refer to the forward decl if the struct is recursive) and replace all 315 // uses of the forward declaration with the final definition. 316 llvm::DIType FwdDecl = 317 DebugFactory.CreateCompositeType(Tag, Unit, Name, DefUnit, Line, 0, 0, 0, 0, 318 llvm::DIType(), llvm::DIArray()); 319 320 // If this is just a forward declaration, return it. 321 if (Decl->isForwardDecl()) 322 return FwdDecl; 323 324 // Otherwise, insert it into the TypeCache so that recursive uses will find 325 // it. 326 TypeCache[QualType(Ty, 0).getAsOpaquePtr()] = FwdDecl; 327 328 // Convert all the elements. 329 llvm::SmallVector<llvm::DIDescriptor, 16> EltTys; 330 331 const ASTRecordLayout &RL = M->getContext().getASTObjCInterfaceLayout(Decl); 332 333 unsigned FieldNo = 0; 334 for (ObjCInterfaceDecl::ivar_iterator I = Decl->ivar_begin(), 335 E = Decl->ivar_end(); I != E; ++I, ++FieldNo) { 336 ObjCIvarDecl *Field = *I; 337 llvm::DIType FieldTy = getOrCreateType(Field->getType(), Unit); 338 339 std::string FieldName = Field->getNameAsString(); 340 341 // Get the location for the field. 342 SourceLocation FieldDefLoc = Field->getLocation(); 343 llvm::DICompileUnit FieldDefUnit = getOrCreateCompileUnit(FieldDefLoc); 344 unsigned FieldLine = SM.getInstantiationLineNumber(FieldDefLoc); 345 346 // Bit size, align and offset of the type. 347 uint64_t FieldSize = M->getContext().getTypeSize(Ty); 348 unsigned FieldAlign = M->getContext().getTypeAlign(Ty); 349 uint64_t FieldOffset = RL.getFieldOffset(FieldNo); 350 351 // Create a DW_TAG_member node to remember the offset of this field in the 352 // struct. FIXME: This is an absolutely insane way to capture this 353 // information. When we gut debug info, this should be fixed. 354 FieldTy = DebugFactory.CreateDerivedType(llvm::dwarf::DW_TAG_member, Unit, 355 FieldName, FieldDefUnit, 356 FieldLine, FieldSize, FieldAlign, 357 FieldOffset, 0, FieldTy); 358 EltTys.push_back(FieldTy); 359 } 360 361 llvm::DIArray Elements = 362 DebugFactory.GetOrCreateArray(&EltTys[0], EltTys.size()); 363 364 // Bit size, align and offset of the type. 365 uint64_t Size = M->getContext().getTypeSize(Ty); 366 uint64_t Align = M->getContext().getTypeAlign(Ty); 367 368 llvm::DIType RealDecl = 369 DebugFactory.CreateCompositeType(Tag, Unit, Name, DefUnit, Line, Size, 370 Align, 0, 0, llvm::DIType(), Elements); 371 372 // Now that we have a real decl for the struct, replace anything using the 373 // old decl with the new one. This will recursively update the debug info. 374 FwdDecl.getGV()->replaceAllUsesWith(RealDecl.getGV()); 375 FwdDecl.getGV()->eraseFromParent(); 376 377 return RealDecl; 378} 379 380llvm::DIType CGDebugInfo::CreateType(const EnumType *Ty, 381 llvm::DICompileUnit Unit) { 382 EnumDecl *Decl = Ty->getDecl(); 383 384 llvm::SmallVector<llvm::DIDescriptor, 32> Enumerators; 385 386 // Create DIEnumerator elements for each enumerator. 387 for (EnumDecl::enumerator_iterator Enum = Decl->enumerator_begin(), 388 EnumEnd = Decl->enumerator_end(); 389 Enum != EnumEnd; ++Enum) { 390 Enumerators.push_back(DebugFactory.CreateEnumerator(Enum->getNameAsString(), 391 Enum->getInitVal().getZExtValue())); 392 } 393 394 // Return a CompositeType for the enum itself. 395 llvm::DIArray EltArray = 396 DebugFactory.GetOrCreateArray(&Enumerators[0], Enumerators.size()); 397 398 std::string EnumName = Decl->getNameAsString(); 399 SourceLocation DefLoc = Decl->getLocation(); 400 llvm::DICompileUnit DefUnit = getOrCreateCompileUnit(DefLoc); 401 SourceManager &SM = M->getContext().getSourceManager(); 402 unsigned Line = SM.getInstantiationLineNumber(DefLoc); 403 404 // Size and align of the type. 405 uint64_t Size = M->getContext().getTypeSize(Ty); 406 unsigned Align = M->getContext().getTypeAlign(Ty); 407 408 return DebugFactory.CreateCompositeType(llvm::dwarf::DW_TAG_enumeration_type, 409 Unit, EnumName, DefUnit, Line, 410 Size, Align, 0, 0, 411 llvm::DIType(), EltArray); 412} 413 414llvm::DIType CGDebugInfo::CreateType(const TagType *Ty, 415 llvm::DICompileUnit Unit) { 416 if (const RecordType *RT = dyn_cast<RecordType>(Ty)) 417 return CreateType(RT, Unit); 418 else if (const EnumType *ET = dyn_cast<EnumType>(Ty)) 419 return CreateType(ET, Unit); 420 421 return llvm::DIType(); 422} 423 424llvm::DIType CGDebugInfo::CreateType(const ArrayType *Ty, 425 llvm::DICompileUnit Unit) { 426 uint64_t Size; 427 uint64_t Align; 428 429 430 // FIXME: make getTypeAlign() aware of VLAs and incomplete array types 431 if (const VariableArrayType *VAT = dyn_cast<VariableArrayType>(Ty)) { 432 Size = 0; 433 Align = 434 M->getContext().getTypeAlign(M->getContext().getBaseElementType(VAT)); 435 } else if (Ty->isIncompleteArrayType()) { 436 Size = 0; 437 Align = M->getContext().getTypeAlign(Ty->getElementType()); 438 } else { 439 // Size and align of the whole array, not the element type. 440 Size = M->getContext().getTypeSize(Ty); 441 Align = M->getContext().getTypeAlign(Ty); 442 } 443 444 // Add the dimensions of the array. FIXME: This loses CV qualifiers from 445 // interior arrays, do we care? Why aren't nested arrays represented the 446 // obvious/recursive way? 447 llvm::SmallVector<llvm::DIDescriptor, 8> Subscripts; 448 QualType EltTy(Ty, 0); 449 while ((Ty = dyn_cast<ArrayType>(EltTy))) { 450 uint64_t Upper = 0; 451 if (const ConstantArrayType *CAT = dyn_cast<ConstantArrayType>(Ty)) 452 Upper = CAT->getSize().getZExtValue() - 1; 453 // FIXME: Verify this is right for VLAs. 454 Subscripts.push_back(DebugFactory.GetOrCreateSubrange(0, Upper)); 455 EltTy = Ty->getElementType(); 456 } 457 458 llvm::DIArray SubscriptArray = 459 DebugFactory.GetOrCreateArray(&Subscripts[0], Subscripts.size()); 460 461 return DebugFactory.CreateCompositeType(llvm::dwarf::DW_TAG_array_type, 462 Unit, "", llvm::DICompileUnit(), 463 0, Size, Align, 0, 0, 464 getOrCreateType(EltTy, Unit), 465 SubscriptArray); 466} 467 468 469/// getOrCreateType - Get the type from the cache or create a new 470/// one if necessary. 471llvm::DIType CGDebugInfo::getOrCreateType(QualType Ty, 472 llvm::DICompileUnit Unit) { 473 if (Ty.isNull()) 474 return llvm::DIType(); 475 476 // Check to see if the compile unit already has created this type. 477 llvm::DIType &Slot = TypeCache[Ty.getAsOpaquePtr()]; 478 if (!Slot.isNull()) return Slot; 479 480 // Handle CVR qualifiers, which recursively handles what they refer to. 481 if (Ty.getCVRQualifiers()) 482 return Slot = CreateCVRType(Ty, Unit); 483 484 // Work out details of type. 485 switch (Ty->getTypeClass()) { 486#define TYPE(Class, Base) 487#define ABSTRACT_TYPE(Class, Base) 488#define NON_CANONICAL_TYPE(Class, Base) 489#define DEPENDENT_TYPE(Class, Base) case Type::Class: 490#include "clang/AST/TypeNodes.def" 491 assert(false && "Dependent types cannot show up in debug information"); 492 493 case Type::Complex: 494 case Type::Reference: 495 case Type::Vector: 496 case Type::ExtVector: 497 case Type::ExtQual: 498 case Type::ObjCQualifiedInterface: 499 case Type::ObjCQualifiedId: 500 case Type::FixedWidthInt: 501 case Type::BlockPointer: 502 case Type::MemberPointer: 503 case Type::ClassTemplateSpecialization: 504 case Type::ObjCQualifiedClass: 505 // Unsupported types 506 return llvm::DIType(); 507 508 case Type::ObjCInterface: 509 Slot = CreateType(cast<ObjCInterfaceType>(Ty), Unit); break; 510 case Type::Builtin: Slot = CreateType(cast<BuiltinType>(Ty), Unit); break; 511 case Type::Pointer: Slot = CreateType(cast<PointerType>(Ty), Unit); break; 512 case Type::Typedef: Slot = CreateType(cast<TypedefType>(Ty), Unit); break; 513 case Type::Record: 514 case Type::Enum: 515 Slot = CreateType(cast<TagType>(Ty), Unit); 516 break; 517 case Type::FunctionProto: 518 case Type::FunctionNoProto: 519 return Slot = CreateType(cast<FunctionType>(Ty), Unit); 520 521 case Type::ConstantArray: 522 case Type::VariableArray: 523 case Type::IncompleteArray: 524 return Slot = CreateType(cast<ArrayType>(Ty), Unit); 525 case Type::TypeOfExpr: 526 return Slot = getOrCreateType(cast<TypeOfExprType>(Ty)->getUnderlyingExpr() 527 ->getType(), Unit); 528 case Type::TypeOf: 529 return Slot = getOrCreateType(cast<TypeOfType>(Ty)->getUnderlyingType(), 530 Unit); 531 } 532 533 return Slot; 534} 535 536/// EmitFunctionStart - Constructs the debug code for entering a function - 537/// "llvm.dbg.func.start.". 538void CGDebugInfo::EmitFunctionStart(const char *Name, QualType ReturnType, 539 llvm::Function *Fn, 540 CGBuilderTy &Builder) { 541 // FIXME: Why is this using CurLoc??? 542 llvm::DICompileUnit Unit = getOrCreateCompileUnit(CurLoc); 543 SourceManager &SM = M->getContext().getSourceManager(); 544 unsigned LineNo = SM.getInstantiationLineNumber(CurLoc); 545 546 llvm::DISubprogram SP = 547 DebugFactory.CreateSubprogram(Unit, Name, Name, "", Unit, LineNo, 548 getOrCreateType(ReturnType, Unit), 549 Fn->hasInternalLinkage(), true/*definition*/); 550 551 DebugFactory.InsertSubprogramStart(SP, Builder.GetInsertBlock()); 552 553 // Push function on region stack. 554 RegionStack.push_back(SP); 555} 556 557 558void CGDebugInfo::EmitStopPoint(llvm::Function *Fn, CGBuilderTy &Builder) { 559 if (CurLoc.isInvalid() || CurLoc.isMacroID()) return; 560 561 // Don't bother if things are the same as last time. 562 SourceManager &SM = M->getContext().getSourceManager(); 563 if (CurLoc == PrevLoc 564 || (SM.getInstantiationLineNumber(CurLoc) == 565 SM.getInstantiationLineNumber(PrevLoc) 566 && SM.isFromSameFile(CurLoc, PrevLoc))) 567 return; 568 569 // Update last state. 570 PrevLoc = CurLoc; 571 572 // Get the appropriate compile unit. 573 llvm::DICompileUnit Unit = getOrCreateCompileUnit(CurLoc); 574 DebugFactory.InsertStopPoint(Unit, SM.getInstantiationLineNumber(CurLoc), 575 SM.getInstantiationColumnNumber(CurLoc), 576 Builder.GetInsertBlock()); 577} 578 579/// EmitRegionStart- Constructs the debug code for entering a declarative 580/// region - "llvm.dbg.region.start.". 581void CGDebugInfo::EmitRegionStart(llvm::Function *Fn, CGBuilderTy &Builder) { 582 llvm::DIDescriptor D; 583 if (!RegionStack.empty()) 584 D = RegionStack.back(); 585 D = DebugFactory.CreateBlock(D); 586 RegionStack.push_back(D); 587 DebugFactory.InsertRegionStart(D, Builder.GetInsertBlock()); 588} 589 590/// EmitRegionEnd - Constructs the debug code for exiting a declarative 591/// region - "llvm.dbg.region.end." 592void CGDebugInfo::EmitRegionEnd(llvm::Function *Fn, CGBuilderTy &Builder) { 593 assert(!RegionStack.empty() && "Region stack mismatch, stack empty!"); 594 595 // Provide an region stop point. 596 EmitStopPoint(Fn, Builder); 597 598 DebugFactory.InsertRegionEnd(RegionStack.back(), Builder.GetInsertBlock()); 599 RegionStack.pop_back(); 600} 601 602/// EmitDeclare - Emit local variable declaration debug info. 603void CGDebugInfo::EmitDeclare(const VarDecl *Decl, unsigned Tag, 604 llvm::Value *Storage, CGBuilderTy &Builder) { 605 assert(!RegionStack.empty() && "Region stack mismatch, stack empty!"); 606 607 // Get location information. 608 SourceManager &SM = M->getContext().getSourceManager(); 609 unsigned Line = SM.getInstantiationLineNumber(Decl->getLocation()); 610 llvm::DICompileUnit Unit = getOrCreateCompileUnit(Decl->getLocation()); 611 612 // Create the descriptor for the variable. 613 llvm::DIVariable D = 614 DebugFactory.CreateVariable(Tag, RegionStack.back(),Decl->getNameAsString(), 615 Unit, Line, 616 getOrCreateType(Decl->getType(), Unit)); 617 // Insert an llvm.dbg.declare into the current block. 618 DebugFactory.InsertDeclare(Storage, D, Builder.GetInsertBlock()); 619} 620 621void CGDebugInfo::EmitDeclareOfAutoVariable(const VarDecl *Decl, 622 llvm::Value *Storage, 623 CGBuilderTy &Builder) { 624 EmitDeclare(Decl, llvm::dwarf::DW_TAG_auto_variable, Storage, Builder); 625} 626 627/// EmitDeclareOfArgVariable - Emit call to llvm.dbg.declare for an argument 628/// variable declaration. 629void CGDebugInfo::EmitDeclareOfArgVariable(const VarDecl *Decl, llvm::Value *AI, 630 CGBuilderTy &Builder) { 631 EmitDeclare(Decl, llvm::dwarf::DW_TAG_arg_variable, AI, Builder); 632} 633 634 635 636/// EmitGlobalVariable - Emit information about a global variable. 637void CGDebugInfo::EmitGlobalVariable(llvm::GlobalVariable *Var, 638 const VarDecl *Decl) { 639 // Create global variable debug descriptor. 640 llvm::DICompileUnit Unit = getOrCreateCompileUnit(Decl->getLocation()); 641 SourceManager &SM = M->getContext().getSourceManager(); 642 unsigned LineNo = SM.getInstantiationLineNumber(Decl->getLocation()); 643 644 std::string Name = Decl->getNameAsString(); 645 646 QualType T = Decl->getType(); 647 if (T->isIncompleteArrayType()) { 648 649 // CodeGen turns int[] into int[1] so we'll do the same here. 650 llvm::APSInt ConstVal(32); 651 652 ConstVal = 1; 653 QualType ET = M->getContext().getAsArrayType(T)->getElementType(); 654 655 T = M->getContext().getConstantArrayType(ET, ConstVal, 656 ArrayType::Normal, 0); 657 } 658 659 DebugFactory.CreateGlobalVariable(Unit, Name, Name, "", Unit, LineNo, 660 getOrCreateType(T, Unit), 661 Var->hasInternalLinkage(), 662 true/*definition*/, Var); 663} 664 665/// EmitGlobalVariable - Emit information about an objective-c interface. 666void CGDebugInfo::EmitGlobalVariable(llvm::GlobalVariable *Var, 667 ObjCInterfaceDecl *Decl) { 668 // Create global variable debug descriptor. 669 llvm::DICompileUnit Unit = getOrCreateCompileUnit(Decl->getLocation()); 670 SourceManager &SM = M->getContext().getSourceManager(); 671 unsigned LineNo = SM.getInstantiationLineNumber(Decl->getLocation()); 672 673 std::string Name = Decl->getNameAsString(); 674 675 QualType T = M->getContext().buildObjCInterfaceType(Decl); 676 if (T->isIncompleteArrayType()) { 677 678 // CodeGen turns int[] into int[1] so we'll do the same here. 679 llvm::APSInt ConstVal(32); 680 681 ConstVal = 1; 682 QualType ET = M->getContext().getAsArrayType(T)->getElementType(); 683 684 T = M->getContext().getConstantArrayType(ET, ConstVal, 685 ArrayType::Normal, 0); 686 } 687 688 DebugFactory.CreateGlobalVariable(Unit, Name, Name, "", Unit, LineNo, 689 getOrCreateType(T, Unit), 690 Var->hasInternalLinkage(), 691 true/*definition*/, Var); 692} 693 694