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