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