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