| //===--- CGDebugInfo.cpp - Emit Debug Information for a Module ------------===// |
| // |
| // The LLVM Compiler Infrastructure |
| // |
| // This file is distributed under the University of Illinois Open Source |
| // License. See LICENSE.TXT for details. |
| // |
| //===----------------------------------------------------------------------===// |
| // |
| // This coordinates the debug information generation while generating code. |
| // |
| //===----------------------------------------------------------------------===// |
| |
| #include "CGDebugInfo.h" |
| #include "CGBlocks.h" |
| #include "CGCXXABI.h" |
| #include "CGObjCRuntime.h" |
| #include "CGRecordLayout.h" |
| #include "CodeGenFunction.h" |
| #include "CodeGenModule.h" |
| #include "ConstantEmitter.h" |
| #include "clang/AST/ASTContext.h" |
| #include "clang/AST/DeclFriend.h" |
| #include "clang/AST/DeclObjC.h" |
| #include "clang/AST/DeclTemplate.h" |
| #include "clang/AST/Expr.h" |
| #include "clang/AST/RecordLayout.h" |
| #include "clang/Basic/FileManager.h" |
| #include "clang/Basic/SourceManager.h" |
| #include "clang/Basic/Version.h" |
| #include "clang/Frontend/CodeGenOptions.h" |
| #include "clang/Frontend/FrontendOptions.h" |
| #include "clang/Lex/HeaderSearchOptions.h" |
| #include "clang/Lex/ModuleMap.h" |
| #include "clang/Lex/PreprocessorOptions.h" |
| #include "llvm/ADT/DenseSet.h" |
| #include "llvm/ADT/SmallVector.h" |
| #include "llvm/ADT/StringExtras.h" |
| #include "llvm/IR/Constants.h" |
| #include "llvm/IR/DataLayout.h" |
| #include "llvm/IR/DerivedTypes.h" |
| #include "llvm/IR/Instructions.h" |
| #include "llvm/IR/Intrinsics.h" |
| #include "llvm/IR/Module.h" |
| #include "llvm/Support/FileSystem.h" |
| #include "llvm/Support/MD5.h" |
| #include "llvm/Support/Path.h" |
| using namespace clang; |
| using namespace clang::CodeGen; |
| |
| static uint32_t getTypeAlignIfRequired(const Type *Ty, const ASTContext &Ctx) { |
| auto TI = Ctx.getTypeInfo(Ty); |
| return TI.AlignIsRequired ? TI.Align : 0; |
| } |
| |
| static uint32_t getTypeAlignIfRequired(QualType Ty, const ASTContext &Ctx) { |
| return getTypeAlignIfRequired(Ty.getTypePtr(), Ctx); |
| } |
| |
| static uint32_t getDeclAlignIfRequired(const Decl *D, const ASTContext &Ctx) { |
| return D->hasAttr<AlignedAttr>() ? D->getMaxAlignment() : 0; |
| } |
| |
| CGDebugInfo::CGDebugInfo(CodeGenModule &CGM) |
| : CGM(CGM), DebugKind(CGM.getCodeGenOpts().getDebugInfo()), |
| DebugTypeExtRefs(CGM.getCodeGenOpts().DebugTypeExtRefs), |
| DBuilder(CGM.getModule()) { |
| for (const auto &KV : CGM.getCodeGenOpts().DebugPrefixMap) |
| DebugPrefixMap[KV.first] = KV.second; |
| CreateCompileUnit(); |
| } |
| |
| CGDebugInfo::~CGDebugInfo() { |
| assert(LexicalBlockStack.empty() && |
| "Region stack mismatch, stack not empty!"); |
| } |
| |
| ApplyDebugLocation::ApplyDebugLocation(CodeGenFunction &CGF, |
| SourceLocation TemporaryLocation) |
| : CGF(&CGF) { |
| init(TemporaryLocation); |
| } |
| |
| ApplyDebugLocation::ApplyDebugLocation(CodeGenFunction &CGF, |
| bool DefaultToEmpty, |
| SourceLocation TemporaryLocation) |
| : CGF(&CGF) { |
| init(TemporaryLocation, DefaultToEmpty); |
| } |
| |
| void ApplyDebugLocation::init(SourceLocation TemporaryLocation, |
| bool DefaultToEmpty) { |
| auto *DI = CGF->getDebugInfo(); |
| if (!DI) { |
| CGF = nullptr; |
| return; |
| } |
| |
| OriginalLocation = CGF->Builder.getCurrentDebugLocation(); |
| |
| if (OriginalLocation && !DI->CGM.getExpressionLocationsEnabled()) |
| return; |
| |
| if (TemporaryLocation.isValid()) { |
| DI->EmitLocation(CGF->Builder, TemporaryLocation); |
| return; |
| } |
| |
| if (DefaultToEmpty) { |
| CGF->Builder.SetCurrentDebugLocation(llvm::DebugLoc()); |
| return; |
| } |
| |
| // Construct a location that has a valid scope, but no line info. |
| assert(!DI->LexicalBlockStack.empty()); |
| CGF->Builder.SetCurrentDebugLocation(llvm::DebugLoc::get( |
| 0, 0, DI->LexicalBlockStack.back(), DI->getInlinedAt())); |
| } |
| |
| ApplyDebugLocation::ApplyDebugLocation(CodeGenFunction &CGF, const Expr *E) |
| : CGF(&CGF) { |
| init(E->getExprLoc()); |
| } |
| |
| ApplyDebugLocation::ApplyDebugLocation(CodeGenFunction &CGF, llvm::DebugLoc Loc) |
| : CGF(&CGF) { |
| if (!CGF.getDebugInfo()) { |
| this->CGF = nullptr; |
| return; |
| } |
| OriginalLocation = CGF.Builder.getCurrentDebugLocation(); |
| if (Loc) |
| CGF.Builder.SetCurrentDebugLocation(std::move(Loc)); |
| } |
| |
| ApplyDebugLocation::~ApplyDebugLocation() { |
| // Query CGF so the location isn't overwritten when location updates are |
| // temporarily disabled (for C++ default function arguments) |
| if (CGF) |
| CGF->Builder.SetCurrentDebugLocation(std::move(OriginalLocation)); |
| } |
| |
| ApplyInlineDebugLocation::ApplyInlineDebugLocation(CodeGenFunction &CGF, |
| GlobalDecl InlinedFn) |
| : CGF(&CGF) { |
| if (!CGF.getDebugInfo()) { |
| this->CGF = nullptr; |
| return; |
| } |
| auto &DI = *CGF.getDebugInfo(); |
| SavedLocation = DI.getLocation(); |
| assert((DI.getInlinedAt() == |
| CGF.Builder.getCurrentDebugLocation()->getInlinedAt()) && |
| "CGDebugInfo and IRBuilder are out of sync"); |
| |
| DI.EmitInlineFunctionStart(CGF.Builder, InlinedFn); |
| } |
| |
| ApplyInlineDebugLocation::~ApplyInlineDebugLocation() { |
| if (!CGF) |
| return; |
| auto &DI = *CGF->getDebugInfo(); |
| DI.EmitInlineFunctionEnd(CGF->Builder); |
| DI.EmitLocation(CGF->Builder, SavedLocation); |
| } |
| |
| void CGDebugInfo::setLocation(SourceLocation Loc) { |
| // If the new location isn't valid return. |
| if (Loc.isInvalid()) |
| return; |
| |
| CurLoc = CGM.getContext().getSourceManager().getExpansionLoc(Loc); |
| |
| // If we've changed files in the middle of a lexical scope go ahead |
| // and create a new lexical scope with file node if it's different |
| // from the one in the scope. |
| if (LexicalBlockStack.empty()) |
| return; |
| |
| SourceManager &SM = CGM.getContext().getSourceManager(); |
| auto *Scope = cast<llvm::DIScope>(LexicalBlockStack.back()); |
| PresumedLoc PCLoc = SM.getPresumedLoc(CurLoc); |
| |
| if (PCLoc.isInvalid() || Scope->getFilename() == PCLoc.getFilename()) |
| return; |
| |
| if (auto *LBF = dyn_cast<llvm::DILexicalBlockFile>(Scope)) { |
| LexicalBlockStack.pop_back(); |
| LexicalBlockStack.emplace_back(DBuilder.createLexicalBlockFile( |
| LBF->getScope(), getOrCreateFile(CurLoc))); |
| } else if (isa<llvm::DILexicalBlock>(Scope) || |
| isa<llvm::DISubprogram>(Scope)) { |
| LexicalBlockStack.pop_back(); |
| LexicalBlockStack.emplace_back( |
| DBuilder.createLexicalBlockFile(Scope, getOrCreateFile(CurLoc))); |
| } |
| } |
| |
| llvm::DIScope *CGDebugInfo::getDeclContextDescriptor(const Decl *D) { |
| llvm::DIScope *Mod = getParentModuleOrNull(D); |
| return getContextDescriptor(cast<Decl>(D->getDeclContext()), |
| Mod ? Mod : TheCU); |
| } |
| |
| llvm::DIScope *CGDebugInfo::getContextDescriptor(const Decl *Context, |
| llvm::DIScope *Default) { |
| if (!Context) |
| return Default; |
| |
| auto I = RegionMap.find(Context); |
| if (I != RegionMap.end()) { |
| llvm::Metadata *V = I->second; |
| return dyn_cast_or_null<llvm::DIScope>(V); |
| } |
| |
| // Check namespace. |
| if (const auto *NSDecl = dyn_cast<NamespaceDecl>(Context)) |
| return getOrCreateNamespace(NSDecl); |
| |
| if (const auto *RDecl = dyn_cast<RecordDecl>(Context)) |
| if (!RDecl->isDependentType()) |
| return getOrCreateType(CGM.getContext().getTypeDeclType(RDecl), |
| getOrCreateMainFile()); |
| return Default; |
| } |
| |
| PrintingPolicy CGDebugInfo::getPrintingPolicy() const { |
| PrintingPolicy PP = CGM.getContext().getPrintingPolicy(); |
| |
| // If we're emitting codeview, it's important to try to match MSVC's naming so |
| // that visualizers written for MSVC will trigger for our class names. In |
| // particular, we can't have spaces between arguments of standard templates |
| // like basic_string and vector. |
| if (CGM.getCodeGenOpts().EmitCodeView) |
| PP.MSVCFormatting = true; |
| |
| return PP; |
| } |
| |
| StringRef CGDebugInfo::getFunctionName(const FunctionDecl *FD) { |
| assert(FD && "Invalid FunctionDecl!"); |
| IdentifierInfo *FII = FD->getIdentifier(); |
| FunctionTemplateSpecializationInfo *Info = |
| FD->getTemplateSpecializationInfo(); |
| |
| // Emit the unqualified name in normal operation. LLVM and the debugger can |
| // compute the fully qualified name from the scope chain. If we're only |
| // emitting line table info, there won't be any scope chains, so emit the |
| // fully qualified name here so that stack traces are more accurate. |
| // FIXME: Do this when emitting DWARF as well as when emitting CodeView after |
| // evaluating the size impact. |
| bool UseQualifiedName = DebugKind == codegenoptions::DebugLineTablesOnly && |
| CGM.getCodeGenOpts().EmitCodeView; |
| |
| if (!Info && FII && !UseQualifiedName) |
| return FII->getName(); |
| |
| SmallString<128> NS; |
| llvm::raw_svector_ostream OS(NS); |
| if (!UseQualifiedName) |
| FD->printName(OS); |
| else |
| FD->printQualifiedName(OS, getPrintingPolicy()); |
| |
| // Add any template specialization args. |
| if (Info) { |
| const TemplateArgumentList *TArgs = Info->TemplateArguments; |
| printTemplateArgumentList(OS, TArgs->asArray(), getPrintingPolicy()); |
| } |
| |
| // Copy this name on the side and use its reference. |
| return internString(OS.str()); |
| } |
| |
| StringRef CGDebugInfo::getObjCMethodName(const ObjCMethodDecl *OMD) { |
| SmallString<256> MethodName; |
| llvm::raw_svector_ostream OS(MethodName); |
| OS << (OMD->isInstanceMethod() ? '-' : '+') << '['; |
| const DeclContext *DC = OMD->getDeclContext(); |
| if (const auto *OID = dyn_cast<ObjCImplementationDecl>(DC)) { |
| OS << OID->getName(); |
| } else if (const auto *OID = dyn_cast<ObjCInterfaceDecl>(DC)) { |
| OS << OID->getName(); |
| } else if (const auto *OC = dyn_cast<ObjCCategoryDecl>(DC)) { |
| if (OC->IsClassExtension()) { |
| OS << OC->getClassInterface()->getName(); |
| } else { |
| OS << OC->getIdentifier()->getNameStart() << '(' |
| << OC->getIdentifier()->getNameStart() << ')'; |
| } |
| } else if (const auto *OCD = dyn_cast<ObjCCategoryImplDecl>(DC)) { |
| OS << OCD->getClassInterface()->getName() << '(' |
| << OCD->getName() << ')'; |
| } else if (isa<ObjCProtocolDecl>(DC)) { |
| // We can extract the type of the class from the self pointer. |
| if (ImplicitParamDecl *SelfDecl = OMD->getSelfDecl()) { |
| QualType ClassTy = |
| cast<ObjCObjectPointerType>(SelfDecl->getType())->getPointeeType(); |
| ClassTy.print(OS, PrintingPolicy(LangOptions())); |
| } |
| } |
| OS << ' ' << OMD->getSelector().getAsString() << ']'; |
| |
| return internString(OS.str()); |
| } |
| |
| StringRef CGDebugInfo::getSelectorName(Selector S) { |
| return internString(S.getAsString()); |
| } |
| |
| StringRef CGDebugInfo::getClassName(const RecordDecl *RD) { |
| if (isa<ClassTemplateSpecializationDecl>(RD)) { |
| SmallString<128> Name; |
| llvm::raw_svector_ostream OS(Name); |
| RD->getNameForDiagnostic(OS, getPrintingPolicy(), |
| /*Qualified*/ false); |
| |
| // Copy this name on the side and use its reference. |
| return internString(Name); |
| } |
| |
| // quick optimization to avoid having to intern strings that are already |
| // stored reliably elsewhere |
| if (const IdentifierInfo *II = RD->getIdentifier()) |
| return II->getName(); |
| |
| // The CodeView printer in LLVM wants to see the names of unnamed types: it is |
| // used to reconstruct the fully qualified type names. |
| if (CGM.getCodeGenOpts().EmitCodeView) { |
| if (const TypedefNameDecl *D = RD->getTypedefNameForAnonDecl()) { |
| assert(RD->getDeclContext() == D->getDeclContext() && |
| "Typedef should not be in another decl context!"); |
| assert(D->getDeclName().getAsIdentifierInfo() && |
| "Typedef was not named!"); |
| return D->getDeclName().getAsIdentifierInfo()->getName(); |
| } |
| |
| if (CGM.getLangOpts().CPlusPlus) { |
| StringRef Name; |
| |
| ASTContext &Context = CGM.getContext(); |
| if (const DeclaratorDecl *DD = Context.getDeclaratorForUnnamedTagDecl(RD)) |
| // Anonymous types without a name for linkage purposes have their |
| // declarator mangled in if they have one. |
| Name = DD->getName(); |
| else if (const TypedefNameDecl *TND = |
| Context.getTypedefNameForUnnamedTagDecl(RD)) |
| // Anonymous types without a name for linkage purposes have their |
| // associate typedef mangled in if they have one. |
| Name = TND->getName(); |
| |
| if (!Name.empty()) { |
| SmallString<256> UnnamedType("<unnamed-type-"); |
| UnnamedType += Name; |
| UnnamedType += '>'; |
| return internString(UnnamedType); |
| } |
| } |
| } |
| |
| return StringRef(); |
| } |
| |
| llvm::DIFile::ChecksumKind |
| CGDebugInfo::computeChecksum(FileID FID, SmallString<32> &Checksum) const { |
| Checksum.clear(); |
| |
| if (!CGM.getCodeGenOpts().EmitCodeView) |
| return llvm::DIFile::CSK_None; |
| |
| SourceManager &SM = CGM.getContext().getSourceManager(); |
| bool Invalid; |
| llvm::MemoryBuffer *MemBuffer = SM.getBuffer(FID, &Invalid); |
| if (Invalid) |
| return llvm::DIFile::CSK_None; |
| |
| llvm::MD5 Hash; |
| llvm::MD5::MD5Result Result; |
| |
| Hash.update(MemBuffer->getBuffer()); |
| Hash.final(Result); |
| |
| Hash.stringifyResult(Result, Checksum); |
| return llvm::DIFile::CSK_MD5; |
| } |
| |
| llvm::DIFile *CGDebugInfo::getOrCreateFile(SourceLocation Loc) { |
| if (!Loc.isValid()) |
| // If Location is not valid then use main input file. |
| return DBuilder.createFile(remapDIPath(TheCU->getFilename()), |
| remapDIPath(TheCU->getDirectory()), |
| TheCU->getFile()->getChecksumKind(), |
| TheCU->getFile()->getChecksum()); |
| |
| SourceManager &SM = CGM.getContext().getSourceManager(); |
| PresumedLoc PLoc = SM.getPresumedLoc(Loc); |
| |
| if (PLoc.isInvalid() || StringRef(PLoc.getFilename()).empty()) |
| // If the location is not valid then use main input file. |
| return DBuilder.createFile(remapDIPath(TheCU->getFilename()), |
| remapDIPath(TheCU->getDirectory()), |
| TheCU->getFile()->getChecksumKind(), |
| TheCU->getFile()->getChecksum()); |
| |
| // Cache the results. |
| const char *fname = PLoc.getFilename(); |
| auto it = DIFileCache.find(fname); |
| |
| if (it != DIFileCache.end()) { |
| // Verify that the information still exists. |
| if (llvm::Metadata *V = it->second) |
| return cast<llvm::DIFile>(V); |
| } |
| |
| SmallString<32> Checksum; |
| llvm::DIFile::ChecksumKind CSKind = |
| computeChecksum(SM.getFileID(Loc), Checksum); |
| |
| llvm::DIFile *F = DBuilder.createFile(remapDIPath(PLoc.getFilename()), |
| remapDIPath(getCurrentDirname()), |
| CSKind, Checksum); |
| |
| DIFileCache[fname].reset(F); |
| return F; |
| } |
| |
| llvm::DIFile *CGDebugInfo::getOrCreateMainFile() { |
| return DBuilder.createFile(remapDIPath(TheCU->getFilename()), |
| remapDIPath(TheCU->getDirectory()), |
| TheCU->getFile()->getChecksumKind(), |
| TheCU->getFile()->getChecksum()); |
| } |
| |
| std::string CGDebugInfo::remapDIPath(StringRef Path) const { |
| for (const auto &Entry : DebugPrefixMap) |
| if (Path.startswith(Entry.first)) |
| return (Twine(Entry.second) + Path.substr(Entry.first.size())).str(); |
| return Path.str(); |
| } |
| |
| unsigned CGDebugInfo::getLineNumber(SourceLocation Loc) { |
| if (Loc.isInvalid() && CurLoc.isInvalid()) |
| return 0; |
| SourceManager &SM = CGM.getContext().getSourceManager(); |
| PresumedLoc PLoc = SM.getPresumedLoc(Loc.isValid() ? Loc : CurLoc); |
| return PLoc.isValid() ? PLoc.getLine() : 0; |
| } |
| |
| unsigned CGDebugInfo::getColumnNumber(SourceLocation Loc, bool Force) { |
| // We may not want column information at all. |
| if (!Force && !CGM.getCodeGenOpts().DebugColumnInfo) |
| return 0; |
| |
| // If the location is invalid then use the current column. |
| if (Loc.isInvalid() && CurLoc.isInvalid()) |
| return 0; |
| SourceManager &SM = CGM.getContext().getSourceManager(); |
| PresumedLoc PLoc = SM.getPresumedLoc(Loc.isValid() ? Loc : CurLoc); |
| return PLoc.isValid() ? PLoc.getColumn() : 0; |
| } |
| |
| StringRef CGDebugInfo::getCurrentDirname() { |
| if (!CGM.getCodeGenOpts().DebugCompilationDir.empty()) |
| return CGM.getCodeGenOpts().DebugCompilationDir; |
| |
| if (!CWDName.empty()) |
| return CWDName; |
| SmallString<256> CWD; |
| llvm::sys::fs::current_path(CWD); |
| return CWDName = internString(CWD); |
| } |
| |
| void CGDebugInfo::CreateCompileUnit() { |
| SmallString<32> Checksum; |
| llvm::DIFile::ChecksumKind CSKind = llvm::DIFile::CSK_None; |
| |
| // Should we be asking the SourceManager for the main file name, instead of |
| // accepting it as an argument? This just causes the main file name to |
| // mismatch with source locations and create extra lexical scopes or |
| // mismatched debug info (a CU with a DW_AT_file of "-", because that's what |
| // the driver passed, but functions/other things have DW_AT_file of "<stdin>" |
| // because that's what the SourceManager says) |
| |
| // Get absolute path name. |
| SourceManager &SM = CGM.getContext().getSourceManager(); |
| std::string MainFileName = CGM.getCodeGenOpts().MainFileName; |
| if (MainFileName.empty()) |
| MainFileName = "<stdin>"; |
| |
| // The main file name provided via the "-main-file-name" option contains just |
| // the file name itself with no path information. This file name may have had |
| // a relative path, so we look into the actual file entry for the main |
| // file to determine the real absolute path for the file. |
| std::string MainFileDir; |
| if (const FileEntry *MainFile = SM.getFileEntryForID(SM.getMainFileID())) { |
| MainFileDir = remapDIPath(MainFile->getDir()->getName()); |
| if (MainFileDir != ".") { |
| llvm::SmallString<1024> MainFileDirSS(MainFileDir); |
| llvm::sys::path::append(MainFileDirSS, MainFileName); |
| MainFileName = MainFileDirSS.str(); |
| } |
| // If the main file name provided is identical to the input file name, and |
| // if the input file is a preprocessed source, use the module name for |
| // debug info. The module name comes from the name specified in the first |
| // linemarker if the input is a preprocessed source. |
| if (MainFile->getName() == MainFileName && |
| FrontendOptions::getInputKindForExtension( |
| MainFile->getName().rsplit('.').second) |
| .isPreprocessed()) |
| MainFileName = CGM.getModule().getName().str(); |
| |
| CSKind = computeChecksum(SM.getMainFileID(), Checksum); |
| } |
| |
| llvm::dwarf::SourceLanguage LangTag; |
| const LangOptions &LO = CGM.getLangOpts(); |
| if (LO.CPlusPlus) { |
| if (LO.ObjC1) |
| LangTag = llvm::dwarf::DW_LANG_ObjC_plus_plus; |
| else |
| LangTag = llvm::dwarf::DW_LANG_C_plus_plus; |
| } else if (LO.ObjC1) { |
| LangTag = llvm::dwarf::DW_LANG_ObjC; |
| } else if (LO.RenderScript) { |
| LangTag = llvm::dwarf::DW_LANG_GOOGLE_RenderScript; |
| } else if (LO.C99) { |
| LangTag = llvm::dwarf::DW_LANG_C99; |
| } else { |
| LangTag = llvm::dwarf::DW_LANG_C89; |
| } |
| |
| std::string Producer = getClangFullVersion(); |
| |
| // Figure out which version of the ObjC runtime we have. |
| unsigned RuntimeVers = 0; |
| if (LO.ObjC1) |
| RuntimeVers = LO.ObjCRuntime.isNonFragile() ? 2 : 1; |
| |
| llvm::DICompileUnit::DebugEmissionKind EmissionKind; |
| switch (DebugKind) { |
| case codegenoptions::NoDebugInfo: |
| case codegenoptions::LocTrackingOnly: |
| EmissionKind = llvm::DICompileUnit::NoDebug; |
| break; |
| case codegenoptions::DebugLineTablesOnly: |
| EmissionKind = llvm::DICompileUnit::LineTablesOnly; |
| break; |
| case codegenoptions::LimitedDebugInfo: |
| case codegenoptions::FullDebugInfo: |
| EmissionKind = llvm::DICompileUnit::FullDebug; |
| break; |
| } |
| |
| // Create new compile unit. |
| // FIXME - Eliminate TheCU. |
| auto &CGOpts = CGM.getCodeGenOpts(); |
| TheCU = DBuilder.createCompileUnit( |
| LangTag, |
| DBuilder.createFile(remapDIPath(MainFileName), |
| remapDIPath(getCurrentDirname()), CSKind, Checksum), |
| Producer, LO.Optimize || CGOpts.PrepareForLTO || CGOpts.EmitSummaryIndex, |
| CGOpts.DwarfDebugFlags, RuntimeVers, |
| CGOpts.EnableSplitDwarf ? "" : CGOpts.SplitDwarfFile, EmissionKind, |
| 0 /* DWOid */, CGOpts.SplitDwarfInlining, CGOpts.DebugInfoForProfiling, |
| CGOpts.GnuPubnames); |
| } |
| |
| llvm::DIType *CGDebugInfo::CreateType(const BuiltinType *BT) { |
| llvm::dwarf::TypeKind Encoding; |
| StringRef BTName; |
| switch (BT->getKind()) { |
| #define BUILTIN_TYPE(Id, SingletonId) |
| #define PLACEHOLDER_TYPE(Id, SingletonId) case BuiltinType::Id: |
| #include "clang/AST/BuiltinTypes.def" |
| case BuiltinType::Dependent: |
| llvm_unreachable("Unexpected builtin type"); |
| case BuiltinType::NullPtr: |
| return DBuilder.createNullPtrType(); |
| case BuiltinType::Void: |
| return nullptr; |
| case BuiltinType::ObjCClass: |
| if (!ClassTy) |
| ClassTy = DBuilder.createForwardDecl(llvm::dwarf::DW_TAG_structure_type, |
| "objc_class", TheCU, |
| getOrCreateMainFile(), 0); |
| return ClassTy; |
| case BuiltinType::ObjCId: { |
| // typedef struct objc_class *Class; |
| // typedef struct objc_object { |
| // Class isa; |
| // } *id; |
| |
| if (ObjTy) |
| return ObjTy; |
| |
| if (!ClassTy) |
| ClassTy = DBuilder.createForwardDecl(llvm::dwarf::DW_TAG_structure_type, |
| "objc_class", TheCU, |
| getOrCreateMainFile(), 0); |
| |
| unsigned Size = CGM.getContext().getTypeSize(CGM.getContext().VoidPtrTy); |
| |
| auto *ISATy = DBuilder.createPointerType(ClassTy, Size); |
| |
| ObjTy = DBuilder.createStructType( |
| TheCU, "objc_object", getOrCreateMainFile(), 0, 0, 0, |
| llvm::DINode::FlagZero, nullptr, llvm::DINodeArray()); |
| |
| DBuilder.replaceArrays( |
| ObjTy, DBuilder.getOrCreateArray(&*DBuilder.createMemberType( |
| ObjTy, "isa", getOrCreateMainFile(), 0, Size, 0, 0, |
| llvm::DINode::FlagZero, ISATy))); |
| return ObjTy; |
| } |
| case BuiltinType::ObjCSel: { |
| if (!SelTy) |
| SelTy = DBuilder.createForwardDecl(llvm::dwarf::DW_TAG_structure_type, |
| "objc_selector", TheCU, |
| getOrCreateMainFile(), 0); |
| return SelTy; |
| } |
| |
| #define IMAGE_TYPE(ImgType, Id, SingletonId, Access, Suffix) \ |
| case BuiltinType::Id: \ |
| return getOrCreateStructPtrType("opencl_" #ImgType "_" #Suffix "_t", \ |
| SingletonId); |
| #include "clang/Basic/OpenCLImageTypes.def" |
| case BuiltinType::OCLSampler: |
| return getOrCreateStructPtrType("opencl_sampler_t", |
| OCLSamplerDITy); |
| case BuiltinType::OCLEvent: |
| return getOrCreateStructPtrType("opencl_event_t", OCLEventDITy); |
| case BuiltinType::OCLClkEvent: |
| return getOrCreateStructPtrType("opencl_clk_event_t", OCLClkEventDITy); |
| case BuiltinType::OCLQueue: |
| return getOrCreateStructPtrType("opencl_queue_t", OCLQueueDITy); |
| case BuiltinType::OCLReserveID: |
| return getOrCreateStructPtrType("opencl_reserve_id_t", OCLReserveIDDITy); |
| |
| case BuiltinType::UChar: |
| case BuiltinType::Char_U: |
| Encoding = llvm::dwarf::DW_ATE_unsigned_char; |
| break; |
| case BuiltinType::Char_S: |
| case BuiltinType::SChar: |
| Encoding = llvm::dwarf::DW_ATE_signed_char; |
| break; |
| case BuiltinType::Char16: |
| case BuiltinType::Char32: |
| Encoding = llvm::dwarf::DW_ATE_UTF; |
| break; |
| case BuiltinType::UShort: |
| case BuiltinType::UInt: |
| case BuiltinType::UInt128: |
| case BuiltinType::ULong: |
| case BuiltinType::WChar_U: |
| case BuiltinType::ULongLong: |
| Encoding = llvm::dwarf::DW_ATE_unsigned; |
| break; |
| case BuiltinType::Short: |
| case BuiltinType::Int: |
| case BuiltinType::Int128: |
| case BuiltinType::Long: |
| case BuiltinType::WChar_S: |
| case BuiltinType::LongLong: |
| Encoding = llvm::dwarf::DW_ATE_signed; |
| break; |
| case BuiltinType::Bool: |
| Encoding = llvm::dwarf::DW_ATE_boolean; |
| break; |
| case BuiltinType::Half: |
| case BuiltinType::Float: |
| case BuiltinType::LongDouble: |
| case BuiltinType::Float16: |
| case BuiltinType::Float128: |
| case BuiltinType::Double: |
| // FIXME: For targets where long double and __float128 have the same size, |
| // they are currently indistinguishable in the debugger without some |
| // special treatment. However, there is currently no consensus on encoding |
| // and this should be updated once a DWARF encoding exists for distinct |
| // floating point types of the same size. |
| Encoding = llvm::dwarf::DW_ATE_float; |
| break; |
| } |
| |
| switch (BT->getKind()) { |
| case BuiltinType::Long: |
| BTName = "long int"; |
| break; |
| case BuiltinType::LongLong: |
| BTName = "long long int"; |
| break; |
| case BuiltinType::ULong: |
| BTName = "long unsigned int"; |
| break; |
| case BuiltinType::ULongLong: |
| BTName = "long long unsigned int"; |
| break; |
| default: |
| BTName = BT->getName(CGM.getLangOpts()); |
| break; |
| } |
| // Bit size and offset of the type. |
| uint64_t Size = CGM.getContext().getTypeSize(BT); |
| return DBuilder.createBasicType(BTName, Size, Encoding); |
| } |
| |
| llvm::DIType *CGDebugInfo::CreateType(const ComplexType *Ty) { |
| // Bit size and offset of the type. |
| llvm::dwarf::TypeKind Encoding = llvm::dwarf::DW_ATE_complex_float; |
| if (Ty->isComplexIntegerType()) |
| Encoding = llvm::dwarf::DW_ATE_lo_user; |
| |
| uint64_t Size = CGM.getContext().getTypeSize(Ty); |
| return DBuilder.createBasicType("complex", Size, Encoding); |
| } |
| |
| llvm::DIType *CGDebugInfo::CreateQualifiedType(QualType Ty, |
| llvm::DIFile *Unit) { |
| QualifierCollector Qc; |
| const Type *T = Qc.strip(Ty); |
| |
| // Ignore these qualifiers for now. |
| Qc.removeObjCGCAttr(); |
| Qc.removeAddressSpace(); |
| Qc.removeObjCLifetime(); |
| |
| // We will create one Derived type for one qualifier and recurse to handle any |
| // additional ones. |
| llvm::dwarf::Tag Tag; |
| if (Qc.hasConst()) { |
| Tag = llvm::dwarf::DW_TAG_const_type; |
| Qc.removeConst(); |
| } else if (Qc.hasVolatile()) { |
| Tag = llvm::dwarf::DW_TAG_volatile_type; |
| Qc.removeVolatile(); |
| } else if (Qc.hasRestrict()) { |
| Tag = llvm::dwarf::DW_TAG_restrict_type; |
| Qc.removeRestrict(); |
| } else { |
| assert(Qc.empty() && "Unknown type qualifier for debug info"); |
| return getOrCreateType(QualType(T, 0), Unit); |
| } |
| |
| auto *FromTy = getOrCreateType(Qc.apply(CGM.getContext(), T), Unit); |
| |
| // No need to fill in the Name, Line, Size, Alignment, Offset in case of |
| // CVR derived types. |
| return DBuilder.createQualifiedType(Tag, FromTy); |
| } |
| |
| llvm::DIType *CGDebugInfo::CreateType(const ObjCObjectPointerType *Ty, |
| llvm::DIFile *Unit) { |
| |
| // The frontend treats 'id' as a typedef to an ObjCObjectType, |
| // whereas 'id<protocol>' is treated as an ObjCPointerType. For the |
| // debug info, we want to emit 'id' in both cases. |
| if (Ty->isObjCQualifiedIdType()) |
| return getOrCreateType(CGM.getContext().getObjCIdType(), Unit); |
| |
| return CreatePointerLikeType(llvm::dwarf::DW_TAG_pointer_type, Ty, |
| Ty->getPointeeType(), Unit); |
| } |
| |
| llvm::DIType *CGDebugInfo::CreateType(const PointerType *Ty, |
| llvm::DIFile *Unit) { |
| return CreatePointerLikeType(llvm::dwarf::DW_TAG_pointer_type, Ty, |
| Ty->getPointeeType(), Unit); |
| } |
| |
| /// \return whether a C++ mangling exists for the type defined by TD. |
| static bool hasCXXMangling(const TagDecl *TD, llvm::DICompileUnit *TheCU) { |
| switch (TheCU->getSourceLanguage()) { |
| case llvm::dwarf::DW_LANG_C_plus_plus: |
| return true; |
| case llvm::dwarf::DW_LANG_ObjC_plus_plus: |
| return isa<CXXRecordDecl>(TD) || isa<EnumDecl>(TD); |
| default: |
| return false; |
| } |
| } |
| |
| /// In C++ mode, types have linkage, so we can rely on the ODR and |
| /// on their mangled names, if they're external. |
| static SmallString<256> getUniqueTagTypeName(const TagType *Ty, |
| CodeGenModule &CGM, |
| llvm::DICompileUnit *TheCU) { |
| SmallString<256> FullName; |
| const TagDecl *TD = Ty->getDecl(); |
| |
| if (!hasCXXMangling(TD, TheCU) || !TD->isExternallyVisible()) |
| return FullName; |
| |
| // TODO: This is using the RTTI name. Is there a better way to get |
| // a unique string for a type? |
| llvm::raw_svector_ostream Out(FullName); |
| CGM.getCXXABI().getMangleContext().mangleCXXRTTIName(QualType(Ty, 0), Out); |
| return FullName; |
| } |
| |
| /// \return the approproate DWARF tag for a composite type. |
| static llvm::dwarf::Tag getTagForRecord(const RecordDecl *RD) { |
| llvm::dwarf::Tag Tag; |
| if (RD->isStruct() || RD->isInterface()) |
| Tag = llvm::dwarf::DW_TAG_structure_type; |
| else if (RD->isUnion()) |
| Tag = llvm::dwarf::DW_TAG_union_type; |
| else { |
| // FIXME: This could be a struct type giving a default visibility different |
| // than C++ class type, but needs llvm metadata changes first. |
| assert(RD->isClass()); |
| Tag = llvm::dwarf::DW_TAG_class_type; |
| } |
| return Tag; |
| } |
| |
| llvm::DICompositeType * |
| CGDebugInfo::getOrCreateRecordFwdDecl(const RecordType *Ty, |
| llvm::DIScope *Ctx) { |
| const RecordDecl *RD = Ty->getDecl(); |
| if (llvm::DIType *T = getTypeOrNull(CGM.getContext().getRecordType(RD))) |
| return cast<llvm::DICompositeType>(T); |
| llvm::DIFile *DefUnit = getOrCreateFile(RD->getLocation()); |
| unsigned Line = getLineNumber(RD->getLocation()); |
| StringRef RDName = getClassName(RD); |
| |
| uint64_t Size = 0; |
| uint32_t Align = 0; |
| |
| // Create the type. |
| SmallString<256> FullName = getUniqueTagTypeName(Ty, CGM, TheCU); |
| llvm::DICompositeType *RetTy = DBuilder.createReplaceableCompositeType( |
| getTagForRecord(RD), RDName, Ctx, DefUnit, Line, 0, Size, Align, |
| llvm::DINode::FlagFwdDecl, FullName); |
| if (CGM.getCodeGenOpts().DebugFwdTemplateParams) |
| if (auto *TSpecial = dyn_cast<ClassTemplateSpecializationDecl>(RD)) |
| DBuilder.replaceArrays(RetTy, llvm::DINodeArray(), |
| CollectCXXTemplateParams(TSpecial, DefUnit)); |
| ReplaceMap.emplace_back( |
| std::piecewise_construct, std::make_tuple(Ty), |
| std::make_tuple(static_cast<llvm::Metadata *>(RetTy))); |
| return RetTy; |
| } |
| |
| llvm::DIType *CGDebugInfo::CreatePointerLikeType(llvm::dwarf::Tag Tag, |
| const Type *Ty, |
| QualType PointeeTy, |
| llvm::DIFile *Unit) { |
| // Bit size, align and offset of the type. |
| // Size is always the size of a pointer. We can't use getTypeSize here |
| // because that does not return the correct value for references. |
| unsigned AddressSpace = CGM.getContext().getTargetAddressSpace(PointeeTy); |
| uint64_t Size = CGM.getTarget().getPointerWidth(AddressSpace); |
| auto Align = getTypeAlignIfRequired(Ty, CGM.getContext()); |
| Optional<unsigned> DWARFAddressSpace = |
| CGM.getTarget().getDWARFAddressSpace(AddressSpace); |
| |
| if (Tag == llvm::dwarf::DW_TAG_reference_type || |
| Tag == llvm::dwarf::DW_TAG_rvalue_reference_type) |
| return DBuilder.createReferenceType(Tag, getOrCreateType(PointeeTy, Unit), |
| Size, Align, DWARFAddressSpace); |
| else |
| return DBuilder.createPointerType(getOrCreateType(PointeeTy, Unit), Size, |
| Align, DWARFAddressSpace); |
| } |
| |
| llvm::DIType *CGDebugInfo::getOrCreateStructPtrType(StringRef Name, |
| llvm::DIType *&Cache) { |
| if (Cache) |
| return Cache; |
| Cache = DBuilder.createForwardDecl(llvm::dwarf::DW_TAG_structure_type, Name, |
| TheCU, getOrCreateMainFile(), 0); |
| unsigned Size = CGM.getContext().getTypeSize(CGM.getContext().VoidPtrTy); |
| Cache = DBuilder.createPointerType(Cache, Size); |
| return Cache; |
| } |
| |
| llvm::DIType *CGDebugInfo::CreateType(const BlockPointerType *Ty, |
| llvm::DIFile *Unit) { |
| SmallVector<llvm::Metadata *, 8> EltTys; |
| QualType FType; |
| uint64_t FieldSize, FieldOffset; |
| uint32_t FieldAlign; |
| llvm::DINodeArray Elements; |
| |
| FieldOffset = 0; |
| FType = CGM.getContext().UnsignedLongTy; |
| EltTys.push_back(CreateMemberType(Unit, FType, "reserved", &FieldOffset)); |
| EltTys.push_back(CreateMemberType(Unit, FType, "Size", &FieldOffset)); |
| |
| Elements = DBuilder.getOrCreateArray(EltTys); |
| EltTys.clear(); |
| |
| llvm::DINode::DIFlags Flags = llvm::DINode::FlagAppleBlock; |
| unsigned LineNo = 0; |
| |
| auto *EltTy = |
| DBuilder.createStructType(Unit, "__block_descriptor", nullptr, LineNo, |
| FieldOffset, 0, Flags, nullptr, Elements); |
| |
| // Bit size, align and offset of the type. |
| uint64_t Size = CGM.getContext().getTypeSize(Ty); |
| |
| auto *DescTy = DBuilder.createPointerType(EltTy, Size); |
| |
| FieldOffset = 0; |
| FType = CGM.getContext().getPointerType(CGM.getContext().VoidTy); |
| EltTys.push_back(CreateMemberType(Unit, FType, "__isa", &FieldOffset)); |
| FType = CGM.getContext().IntTy; |
| EltTys.push_back(CreateMemberType(Unit, FType, "__flags", &FieldOffset)); |
| EltTys.push_back(CreateMemberType(Unit, FType, "__reserved", &FieldOffset)); |
| FType = CGM.getContext().getPointerType(Ty->getPointeeType()); |
| EltTys.push_back(CreateMemberType(Unit, FType, "__FuncPtr", &FieldOffset)); |
| |
| FType = CGM.getContext().getPointerType(CGM.getContext().VoidTy); |
| FieldSize = CGM.getContext().getTypeSize(Ty); |
| FieldAlign = CGM.getContext().getTypeAlign(Ty); |
| EltTys.push_back(DBuilder.createMemberType( |
| Unit, "__descriptor", nullptr, LineNo, FieldSize, FieldAlign, FieldOffset, |
| llvm::DINode::FlagZero, DescTy)); |
| |
| FieldOffset += FieldSize; |
| Elements = DBuilder.getOrCreateArray(EltTys); |
| |
| // The __block_literal_generic structs are marked with a special |
| // DW_AT_APPLE_BLOCK attribute and are an implementation detail only |
| // the debugger needs to know about. To allow type uniquing, emit |
| // them without a name or a location. |
| EltTy = |
| DBuilder.createStructType(Unit, "", nullptr, LineNo, |
| FieldOffset, 0, Flags, nullptr, Elements); |
| |
| return DBuilder.createPointerType(EltTy, Size); |
| } |
| |
| llvm::DIType *CGDebugInfo::CreateType(const TemplateSpecializationType *Ty, |
| llvm::DIFile *Unit) { |
| assert(Ty->isTypeAlias()); |
| llvm::DIType *Src = getOrCreateType(Ty->getAliasedType(), Unit); |
| |
| SmallString<128> NS; |
| llvm::raw_svector_ostream OS(NS); |
| Ty->getTemplateName().print(OS, getPrintingPolicy(), /*qualified*/ false); |
| printTemplateArgumentList(OS, Ty->template_arguments(), getPrintingPolicy()); |
| |
| auto *AliasDecl = cast<TypeAliasTemplateDecl>( |
| Ty->getTemplateName().getAsTemplateDecl())->getTemplatedDecl(); |
| |
| SourceLocation Loc = AliasDecl->getLocation(); |
| return DBuilder.createTypedef(Src, OS.str(), getOrCreateFile(Loc), |
| getLineNumber(Loc), |
| getDeclContextDescriptor(AliasDecl)); |
| } |
| |
| llvm::DIType *CGDebugInfo::CreateType(const TypedefType *Ty, |
| llvm::DIFile *Unit) { |
| // We don't set size information, but do specify where the typedef was |
| // declared. |
| SourceLocation Loc = Ty->getDecl()->getLocation(); |
| |
| // Typedefs are derived from some other type. |
| return DBuilder.createTypedef( |
| getOrCreateType(Ty->getDecl()->getUnderlyingType(), Unit), |
| Ty->getDecl()->getName(), getOrCreateFile(Loc), getLineNumber(Loc), |
| getDeclContextDescriptor(Ty->getDecl())); |
| } |
| |
| static unsigned getDwarfCC(CallingConv CC) { |
| switch (CC) { |
| case CC_C: |
| // Avoid emitting DW_AT_calling_convention if the C convention was used. |
| return 0; |
| |
| case CC_X86StdCall: |
| return llvm::dwarf::DW_CC_BORLAND_stdcall; |
| case CC_X86FastCall: |
| return llvm::dwarf::DW_CC_BORLAND_msfastcall; |
| case CC_X86ThisCall: |
| return llvm::dwarf::DW_CC_BORLAND_thiscall; |
| case CC_X86VectorCall: |
| return llvm::dwarf::DW_CC_LLVM_vectorcall; |
| case CC_X86Pascal: |
| return llvm::dwarf::DW_CC_BORLAND_pascal; |
| |
| // FIXME: Create new DW_CC_ codes for these calling conventions. |
| case CC_Win64: |
| case CC_X86_64SysV: |
| case CC_AAPCS: |
| case CC_AAPCS_VFP: |
| case CC_IntelOclBicc: |
| case CC_SpirFunction: |
| case CC_OpenCLKernel: |
| case CC_Swift: |
| case CC_PreserveMost: |
| case CC_PreserveAll: |
| case CC_X86RegCall: |
| return 0; |
| } |
| return 0; |
| } |
| |
| llvm::DIType *CGDebugInfo::CreateType(const FunctionType *Ty, |
| llvm::DIFile *Unit) { |
| SmallVector<llvm::Metadata *, 16> EltTys; |
| |
| // Add the result type at least. |
| EltTys.push_back(getOrCreateType(Ty->getReturnType(), Unit)); |
| |
| // Set up remainder of arguments if there is a prototype. |
| // otherwise emit it as a variadic function. |
| if (isa<FunctionNoProtoType>(Ty)) |
| EltTys.push_back(DBuilder.createUnspecifiedParameter()); |
| else if (const auto *FPT = dyn_cast<FunctionProtoType>(Ty)) { |
| for (const QualType &ParamType : FPT->param_types()) |
| EltTys.push_back(getOrCreateType(ParamType, Unit)); |
| if (FPT->isVariadic()) |
| EltTys.push_back(DBuilder.createUnspecifiedParameter()); |
| } |
| |
| llvm::DITypeRefArray EltTypeArray = DBuilder.getOrCreateTypeArray(EltTys); |
| return DBuilder.createSubroutineType(EltTypeArray, llvm::DINode::FlagZero, |
| getDwarfCC(Ty->getCallConv())); |
| } |
| |
| /// Convert an AccessSpecifier into the corresponding DINode flag. |
| /// As an optimization, return 0 if the access specifier equals the |
| /// default for the containing type. |
| static llvm::DINode::DIFlags getAccessFlag(AccessSpecifier Access, |
| const RecordDecl *RD) { |
| AccessSpecifier Default = clang::AS_none; |
| if (RD && RD->isClass()) |
| Default = clang::AS_private; |
| else if (RD && (RD->isStruct() || RD->isUnion())) |
| Default = clang::AS_public; |
| |
| if (Access == Default) |
| return llvm::DINode::FlagZero; |
| |
| switch (Access) { |
| case clang::AS_private: |
| return llvm::DINode::FlagPrivate; |
| case clang::AS_protected: |
| return llvm::DINode::FlagProtected; |
| case clang::AS_public: |
| return llvm::DINode::FlagPublic; |
| case clang::AS_none: |
| return llvm::DINode::FlagZero; |
| } |
| llvm_unreachable("unexpected access enumerator"); |
| } |
| |
| llvm::DIType *CGDebugInfo::createBitFieldType(const FieldDecl *BitFieldDecl, |
| llvm::DIScope *RecordTy, |
| const RecordDecl *RD) { |
| StringRef Name = BitFieldDecl->getName(); |
| QualType Ty = BitFieldDecl->getType(); |
| SourceLocation Loc = BitFieldDecl->getLocation(); |
| llvm::DIFile *VUnit = getOrCreateFile(Loc); |
| llvm::DIType *DebugType = getOrCreateType(Ty, VUnit); |
| |
| // Get the location for the field. |
| llvm::DIFile *File = getOrCreateFile(Loc); |
| unsigned Line = getLineNumber(Loc); |
| |
| const CGBitFieldInfo &BitFieldInfo = |
| CGM.getTypes().getCGRecordLayout(RD).getBitFieldInfo(BitFieldDecl); |
| uint64_t SizeInBits = BitFieldInfo.Size; |
| assert(SizeInBits > 0 && "found named 0-width bitfield"); |
| uint64_t StorageOffsetInBits = |
| CGM.getContext().toBits(BitFieldInfo.StorageOffset); |
| uint64_t Offset = BitFieldInfo.Offset; |
| // The bit offsets for big endian machines are reversed for big |
| // endian target, compensate for that as the DIDerivedType requires |
| // un-reversed offsets. |
| if (CGM.getDataLayout().isBigEndian()) |
| Offset = BitFieldInfo.StorageSize - BitFieldInfo.Size - Offset; |
| uint64_t OffsetInBits = StorageOffsetInBits + Offset; |
| llvm::DINode::DIFlags Flags = getAccessFlag(BitFieldDecl->getAccess(), RD); |
| return DBuilder.createBitFieldMemberType( |
| RecordTy, Name, File, Line, SizeInBits, OffsetInBits, StorageOffsetInBits, |
| Flags, DebugType); |
| } |
| |
| llvm::DIType * |
| CGDebugInfo::createFieldType(StringRef name, QualType type, SourceLocation loc, |
| AccessSpecifier AS, uint64_t offsetInBits, |
| uint32_t AlignInBits, llvm::DIFile *tunit, |
| llvm::DIScope *scope, const RecordDecl *RD) { |
| llvm::DIType *debugType = getOrCreateType(type, tunit); |
| |
| // Get the location for the field. |
| llvm::DIFile *file = getOrCreateFile(loc); |
| unsigned line = getLineNumber(loc); |
| |
| uint64_t SizeInBits = 0; |
| auto Align = AlignInBits; |
| if (!type->isIncompleteArrayType()) { |
| TypeInfo TI = CGM.getContext().getTypeInfo(type); |
| SizeInBits = TI.Width; |
| if (!Align) |
| Align = getTypeAlignIfRequired(type, CGM.getContext()); |
| } |
| |
| llvm::DINode::DIFlags flags = getAccessFlag(AS, RD); |
| return DBuilder.createMemberType(scope, name, file, line, SizeInBits, |
| Align, offsetInBits, flags, debugType); |
| } |
| |
| void CGDebugInfo::CollectRecordLambdaFields( |
| const CXXRecordDecl *CXXDecl, SmallVectorImpl<llvm::Metadata *> &elements, |
| llvm::DIType *RecordTy) { |
| // For C++11 Lambdas a Field will be the same as a Capture, but the Capture |
| // has the name and the location of the variable so we should iterate over |
| // both concurrently. |
| const ASTRecordLayout &layout = CGM.getContext().getASTRecordLayout(CXXDecl); |
| RecordDecl::field_iterator Field = CXXDecl->field_begin(); |
| unsigned fieldno = 0; |
| for (CXXRecordDecl::capture_const_iterator I = CXXDecl->captures_begin(), |
| E = CXXDecl->captures_end(); |
| I != E; ++I, ++Field, ++fieldno) { |
| const LambdaCapture &C = *I; |
| if (C.capturesVariable()) { |
| SourceLocation Loc = C.getLocation(); |
| assert(!Field->isBitField() && "lambdas don't have bitfield members!"); |
| VarDecl *V = C.getCapturedVar(); |
| StringRef VName = V->getName(); |
| llvm::DIFile *VUnit = getOrCreateFile(Loc); |
| auto Align = getDeclAlignIfRequired(V, CGM.getContext()); |
| llvm::DIType *FieldType = createFieldType( |
| VName, Field->getType(), Loc, Field->getAccess(), |
| layout.getFieldOffset(fieldno), Align, VUnit, RecordTy, CXXDecl); |
| elements.push_back(FieldType); |
| } else if (C.capturesThis()) { |
| // TODO: Need to handle 'this' in some way by probably renaming the |
| // this of the lambda class and having a field member of 'this' or |
| // by using AT_object_pointer for the function and having that be |
| // used as 'this' for semantic references. |
| FieldDecl *f = *Field; |
| llvm::DIFile *VUnit = getOrCreateFile(f->getLocation()); |
| QualType type = f->getType(); |
| llvm::DIType *fieldType = createFieldType( |
| "this", type, f->getLocation(), f->getAccess(), |
| layout.getFieldOffset(fieldno), VUnit, RecordTy, CXXDecl); |
| |
| elements.push_back(fieldType); |
| } |
| } |
| } |
| |
| llvm::DIDerivedType * |
| CGDebugInfo::CreateRecordStaticField(const VarDecl *Var, llvm::DIType *RecordTy, |
| const RecordDecl *RD) { |
| // Create the descriptor for the static variable, with or without |
| // constant initializers. |
| Var = Var->getCanonicalDecl(); |
| llvm::DIFile *VUnit = getOrCreateFile(Var->getLocation()); |
| llvm::DIType *VTy = getOrCreateType(Var->getType(), VUnit); |
| |
| unsigned LineNumber = getLineNumber(Var->getLocation()); |
| StringRef VName = Var->getName(); |
| llvm::Constant *C = nullptr; |
| if (Var->getInit()) { |
| const APValue *Value = Var->evaluateValue(); |
| if (Value) { |
| if (Value->isInt()) |
| C = llvm::ConstantInt::get(CGM.getLLVMContext(), Value->getInt()); |
| if (Value->isFloat()) |
| C = llvm::ConstantFP::get(CGM.getLLVMContext(), Value->getFloat()); |
| } |
| } |
| |
| llvm::DINode::DIFlags Flags = getAccessFlag(Var->getAccess(), RD); |
| auto Align = getDeclAlignIfRequired(Var, CGM.getContext()); |
| llvm::DIDerivedType *GV = DBuilder.createStaticMemberType( |
| RecordTy, VName, VUnit, LineNumber, VTy, Flags, C, Align); |
| StaticDataMemberCache[Var->getCanonicalDecl()].reset(GV); |
| return GV; |
| } |
| |
| void CGDebugInfo::CollectRecordNormalField( |
| const FieldDecl *field, uint64_t OffsetInBits, llvm::DIFile *tunit, |
| SmallVectorImpl<llvm::Metadata *> &elements, llvm::DIType *RecordTy, |
| const RecordDecl *RD) { |
| StringRef name = field->getName(); |
| QualType type = field->getType(); |
| |
| // Ignore unnamed fields unless they're anonymous structs/unions. |
| if (name.empty() && !type->isRecordType()) |
| return; |
| |
| llvm::DIType *FieldType; |
| if (field->isBitField()) { |
| FieldType = createBitFieldType(field, RecordTy, RD); |
| } else { |
| auto Align = getDeclAlignIfRequired(field, CGM.getContext()); |
| FieldType = |
| createFieldType(name, type, field->getLocation(), field->getAccess(), |
| OffsetInBits, Align, tunit, RecordTy, RD); |
| } |
| |
| elements.push_back(FieldType); |
| } |
| |
| void CGDebugInfo::CollectRecordNestedType( |
| const TypeDecl *TD, SmallVectorImpl<llvm::Metadata *> &elements) { |
| QualType Ty = CGM.getContext().getTypeDeclType(TD); |
| // Injected class names are not considered nested records. |
| if (isa<InjectedClassNameType>(Ty)) |
| return; |
| SourceLocation Loc = TD->getLocation(); |
| llvm::DIType *nestedType = getOrCreateType(Ty, getOrCreateFile(Loc)); |
| elements.push_back(nestedType); |
| } |
| |
| void CGDebugInfo::CollectRecordFields( |
| const RecordDecl *record, llvm::DIFile *tunit, |
| SmallVectorImpl<llvm::Metadata *> &elements, |
| llvm::DICompositeType *RecordTy) { |
| const auto *CXXDecl = dyn_cast<CXXRecordDecl>(record); |
| |
| if (CXXDecl && CXXDecl->isLambda()) |
| CollectRecordLambdaFields(CXXDecl, elements, RecordTy); |
| else { |
| const ASTRecordLayout &layout = CGM.getContext().getASTRecordLayout(record); |
| |
| // Debug info for nested types is included in the member list only for |
| // CodeView. |
| bool IncludeNestedTypes = CGM.getCodeGenOpts().EmitCodeView; |
| |
| // Field number for non-static fields. |
| unsigned fieldNo = 0; |
| |
| // Static and non-static members should appear in the same order as |
| // the corresponding declarations in the source program. |
| for (const auto *I : record->decls()) |
| if (const auto *V = dyn_cast<VarDecl>(I)) { |
| if (V->hasAttr<NoDebugAttr>()) |
| continue; |
| // Reuse the existing static member declaration if one exists |
| auto MI = StaticDataMemberCache.find(V->getCanonicalDecl()); |
| if (MI != StaticDataMemberCache.end()) { |
| assert(MI->second && |
| "Static data member declaration should still exist"); |
| elements.push_back(MI->second); |
| } else { |
| auto Field = CreateRecordStaticField(V, RecordTy, record); |
| elements.push_back(Field); |
| } |
| } else if (const auto *field = dyn_cast<FieldDecl>(I)) { |
| CollectRecordNormalField(field, layout.getFieldOffset(fieldNo), tunit, |
| elements, RecordTy, record); |
| |
| // Bump field number for next field. |
| ++fieldNo; |
| } else if (IncludeNestedTypes) { |
| if (const auto *nestedType = dyn_cast<TypeDecl>(I)) |
| if (!nestedType->isImplicit() && |
| nestedType->getDeclContext() == record) |
| CollectRecordNestedType(nestedType, elements); |
| } |
| } |
| } |
| |
| llvm::DISubroutineType * |
| CGDebugInfo::getOrCreateMethodType(const CXXMethodDecl *Method, |
| llvm::DIFile *Unit) { |
| const FunctionProtoType *Func = Method->getType()->getAs<FunctionProtoType>(); |
| if (Method->isStatic()) |
| return cast_or_null<llvm::DISubroutineType>( |
| getOrCreateType(QualType(Func, 0), Unit)); |
| return getOrCreateInstanceMethodType(Method->getThisType(CGM.getContext()), |
| Func, Unit); |
| } |
| |
| llvm::DISubroutineType *CGDebugInfo::getOrCreateInstanceMethodType( |
| QualType ThisPtr, const FunctionProtoType *Func, llvm::DIFile *Unit) { |
| // Add "this" pointer. |
| llvm::DITypeRefArray Args( |
| cast<llvm::DISubroutineType>(getOrCreateType(QualType(Func, 0), Unit)) |
| ->getTypeArray()); |
| assert(Args.size() && "Invalid number of arguments!"); |
| |
| SmallVector<llvm::Metadata *, 16> Elts; |
| |
| // First element is always return type. For 'void' functions it is NULL. |
| Elts.push_back(Args[0]); |
| |
| // "this" pointer is always first argument. |
| const CXXRecordDecl *RD = ThisPtr->getPointeeCXXRecordDecl(); |
| if (isa<ClassTemplateSpecializationDecl>(RD)) { |
| // Create pointer type directly in this case. |
| const PointerType *ThisPtrTy = cast<PointerType>(ThisPtr); |
| QualType PointeeTy = ThisPtrTy->getPointeeType(); |
| unsigned AS = CGM.getContext().getTargetAddressSpace(PointeeTy); |
| uint64_t Size = CGM.getTarget().getPointerWidth(AS); |
| auto Align = getTypeAlignIfRequired(ThisPtrTy, CGM.getContext()); |
| llvm::DIType *PointeeType = getOrCreateType(PointeeTy, Unit); |
| llvm::DIType *ThisPtrType = |
| DBuilder.createPointerType(PointeeType, Size, Align); |
| TypeCache[ThisPtr.getAsOpaquePtr()].reset(ThisPtrType); |
| // TODO: This and the artificial type below are misleading, the |
| // types aren't artificial the argument is, but the current |
| // metadata doesn't represent that. |
| ThisPtrType = DBuilder.createObjectPointerType(ThisPtrType); |
| Elts.push_back(ThisPtrType); |
| } else { |
| llvm::DIType *ThisPtrType = getOrCreateType(ThisPtr, Unit); |
| TypeCache[ThisPtr.getAsOpaquePtr()].reset(ThisPtrType); |
| ThisPtrType = DBuilder.createObjectPointerType(ThisPtrType); |
| Elts.push_back(ThisPtrType); |
| } |
| |
| // Copy rest of the arguments. |
| for (unsigned i = 1, e = Args.size(); i != e; ++i) |
| Elts.push_back(Args[i]); |
| |
| llvm::DITypeRefArray EltTypeArray = DBuilder.getOrCreateTypeArray(Elts); |
| |
| llvm::DINode::DIFlags Flags = llvm::DINode::FlagZero; |
| if (Func->getExtProtoInfo().RefQualifier == RQ_LValue) |
| Flags |= llvm::DINode::FlagLValueReference; |
| if (Func->getExtProtoInfo().RefQualifier == RQ_RValue) |
| Flags |= llvm::DINode::FlagRValueReference; |
| |
| return DBuilder.createSubroutineType(EltTypeArray, Flags, |
| getDwarfCC(Func->getCallConv())); |
| } |
| |
| /// isFunctionLocalClass - Return true if CXXRecordDecl is defined |
| /// inside a function. |
| static bool isFunctionLocalClass(const CXXRecordDecl *RD) { |
| if (const auto *NRD = dyn_cast<CXXRecordDecl>(RD->getDeclContext())) |
| return isFunctionLocalClass(NRD); |
| if (isa<FunctionDecl>(RD->getDeclContext())) |
| return true; |
| return false; |
| } |
| |
| llvm::DISubprogram *CGDebugInfo::CreateCXXMemberFunction( |
| const CXXMethodDecl *Method, llvm::DIFile *Unit, llvm::DIType *RecordTy) { |
| bool IsCtorOrDtor = |
| isa<CXXConstructorDecl>(Method) || isa<CXXDestructorDecl>(Method); |
| |
| StringRef MethodName = getFunctionName(Method); |
| llvm::DISubroutineType *MethodTy = getOrCreateMethodType(Method, Unit); |
| |
| // Since a single ctor/dtor corresponds to multiple functions, it doesn't |
| // make sense to give a single ctor/dtor a linkage name. |
| StringRef MethodLinkageName; |
| // FIXME: 'isFunctionLocalClass' seems like an arbitrary/unintentional |
| // property to use here. It may've been intended to model "is non-external |
| // type" but misses cases of non-function-local but non-external classes such |
| // as those in anonymous namespaces as well as the reverse - external types |
| // that are function local, such as those in (non-local) inline functions. |
| if (!IsCtorOrDtor && !isFunctionLocalClass(Method->getParent())) |
| MethodLinkageName = CGM.getMangledName(Method); |
| |
| // Get the location for the method. |
| llvm::DIFile *MethodDefUnit = nullptr; |
| unsigned MethodLine = 0; |
| if (!Method->isImplicit()) { |
| MethodDefUnit = getOrCreateFile(Method->getLocation()); |
| MethodLine = getLineNumber(Method->getLocation()); |
| } |
| |
| // Collect virtual method info. |
| llvm::DIType *ContainingType = nullptr; |
| unsigned Virtuality = 0; |
| unsigned VIndex = 0; |
| llvm::DINode::DIFlags Flags = llvm::DINode::FlagZero; |
| int ThisAdjustment = 0; |
| |
| if (Method->isVirtual()) { |
| if (Method->isPure()) |
| Virtuality = llvm::dwarf::DW_VIRTUALITY_pure_virtual; |
| else |
| Virtuality = llvm::dwarf::DW_VIRTUALITY_virtual; |
| |
| if (CGM.getTarget().getCXXABI().isItaniumFamily()) { |
| // It doesn't make sense to give a virtual destructor a vtable index, |
| // since a single destructor has two entries in the vtable. |
| if (!isa<CXXDestructorDecl>(Method)) |
| VIndex = CGM.getItaniumVTableContext().getMethodVTableIndex(Method); |
| } else { |
| // Emit MS ABI vftable information. There is only one entry for the |
| // deleting dtor. |
| const auto *DD = dyn_cast<CXXDestructorDecl>(Method); |
| GlobalDecl GD = DD ? GlobalDecl(DD, Dtor_Deleting) : GlobalDecl(Method); |
| MicrosoftVTableContext::MethodVFTableLocation ML = |
| CGM.getMicrosoftVTableContext().getMethodVFTableLocation(GD); |
| VIndex = ML.Index; |
| |
| // CodeView only records the vftable offset in the class that introduces |
| // the virtual method. This is possible because, unlike Itanium, the MS |
| // C++ ABI does not include all virtual methods from non-primary bases in |
| // the vtable for the most derived class. For example, if C inherits from |
| // A and B, C's primary vftable will not include B's virtual methods. |
| if (Method->size_overridden_methods() == 0) |
| Flags |= llvm::DINode::FlagIntroducedVirtual; |
| |
| // The 'this' adjustment accounts for both the virtual and non-virtual |
| // portions of the adjustment. Presumably the debugger only uses it when |
| // it knows the dynamic type of an object. |
| ThisAdjustment = CGM.getCXXABI() |
| .getVirtualFunctionPrologueThisAdjustment(GD) |
| .getQuantity(); |
| } |
| ContainingType = RecordTy; |
| } |
| |
| if (Method->isStatic()) |
| Flags |= llvm::DINode::FlagStaticMember; |
| if (Method->isImplicit()) |
| Flags |= llvm::DINode::FlagArtificial; |
| Flags |= getAccessFlag(Method->getAccess(), Method->getParent()); |
| if (const auto *CXXC = dyn_cast<CXXConstructorDecl>(Method)) { |
| if (CXXC->isExplicit()) |
| Flags |= llvm::DINode::FlagExplicit; |
| } else if (const auto *CXXC = dyn_cast<CXXConversionDecl>(Method)) { |
| if (CXXC->isExplicit()) |
| Flags |= llvm::DINode::FlagExplicit; |
| } |
| if (Method->hasPrototype()) |
| Flags |= llvm::DINode::FlagPrototyped; |
| if (Method->getRefQualifier() == RQ_LValue) |
| Flags |= llvm::DINode::FlagLValueReference; |
| if (Method->getRefQualifier() == RQ_RValue) |
| Flags |= llvm::DINode::FlagRValueReference; |
| |
| llvm::DINodeArray TParamsArray = CollectFunctionTemplateParams(Method, Unit); |
| llvm::DISubprogram *SP = DBuilder.createMethod( |
| RecordTy, MethodName, MethodLinkageName, MethodDefUnit, MethodLine, |
| MethodTy, /*isLocalToUnit=*/false, /*isDefinition=*/false, Virtuality, |
| VIndex, ThisAdjustment, ContainingType, Flags, CGM.getLangOpts().Optimize, |
| TParamsArray.get()); |
| |
| SPCache[Method->getCanonicalDecl()].reset(SP); |
| |
| return SP; |
| } |
| |
| void CGDebugInfo::CollectCXXMemberFunctions( |
| const CXXRecordDecl *RD, llvm::DIFile *Unit, |
| SmallVectorImpl<llvm::Metadata *> &EltTys, llvm::DIType *RecordTy) { |
| |
| // Since we want more than just the individual member decls if we |
| // have templated functions iterate over every declaration to gather |
| // the functions. |
| for (const auto *I : RD->decls()) { |
| const auto *Method = dyn_cast<CXXMethodDecl>(I); |
| // If the member is implicit, don't add it to the member list. This avoids |
| // the member being added to type units by LLVM, while still allowing it |
| // to be emitted into the type declaration/reference inside the compile |
| // unit. |
| // Ditto 'nodebug' methods, for consistency with CodeGenFunction.cpp. |
| // FIXME: Handle Using(Shadow?)Decls here to create |
| // DW_TAG_imported_declarations inside the class for base decls brought into |
| // derived classes. GDB doesn't seem to notice/leverage these when I tried |
| // it, so I'm not rushing to fix this. (GCC seems to produce them, if |
| // referenced) |
| if (!Method || Method->isImplicit() || Method->hasAttr<NoDebugAttr>()) |
| continue; |
| |
| if (Method->getType()->getAs<FunctionProtoType>()->getContainedAutoType()) |
| continue; |
| |
| // Reuse the existing member function declaration if it exists. |
| // It may be associated with the declaration of the type & should be |
| // reused as we're building the definition. |
| // |
| // This situation can arise in the vtable-based debug info reduction where |
| // implicit members are emitted in a non-vtable TU. |
| auto MI = SPCache.find(Method->getCanonicalDecl()); |
| EltTys.push_back(MI == SPCache.end() |
| ? CreateCXXMemberFunction(Method, Unit, RecordTy) |
| : static_cast<llvm::Metadata *>(MI->second)); |
| } |
| } |
| |
| void CGDebugInfo::CollectCXXBases(const CXXRecordDecl *RD, llvm::DIFile *Unit, |
| SmallVectorImpl<llvm::Metadata *> &EltTys, |
| llvm::DIType *RecordTy) { |
| llvm::DenseSet<CanonicalDeclPtr<const CXXRecordDecl>> SeenTypes; |
| CollectCXXBasesAux(RD, Unit, EltTys, RecordTy, RD->bases(), SeenTypes, |
| llvm::DINode::FlagZero); |
| |
| // If we are generating CodeView debug info, we also need to emit records for |
| // indirect virtual base classes. |
| if (CGM.getCodeGenOpts().EmitCodeView) { |
| CollectCXXBasesAux(RD, Unit, EltTys, RecordTy, RD->vbases(), SeenTypes, |
| llvm::DINode::FlagIndirectVirtualBase); |
| } |
| } |
| |
| void CGDebugInfo::CollectCXXBasesAux( |
| const CXXRecordDecl *RD, llvm::DIFile *Unit, |
| SmallVectorImpl<llvm::Metadata *> &EltTys, llvm::DIType *RecordTy, |
| const CXXRecordDecl::base_class_const_range &Bases, |
| llvm::DenseSet<CanonicalDeclPtr<const CXXRecordDecl>> &SeenTypes, |
| llvm::DINode::DIFlags StartingFlags) { |
| const ASTRecordLayout &RL = CGM.getContext().getASTRecordLayout(RD); |
| for (const auto &BI : Bases) { |
| const auto *Base = |
| cast<CXXRecordDecl>(BI.getType()->getAs<RecordType>()->getDecl()); |
| if (!SeenTypes.insert(Base).second) |
| continue; |
| auto *BaseTy = getOrCreateType(BI.getType(), Unit); |
| llvm::DINode::DIFlags BFlags = StartingFlags; |
| uint64_t BaseOffset; |
| |
| if (BI.isVirtual()) { |
| if (CGM.getTarget().getCXXABI().isItaniumFamily()) { |
| // virtual base offset offset is -ve. The code generator emits dwarf |
| // expression where it expects +ve number. |
| BaseOffset = 0 - CGM.getItaniumVTableContext() |
| .getVirtualBaseOffsetOffset(RD, Base) |
| .getQuantity(); |
| } else { |
| // In the MS ABI, store the vbtable offset, which is analogous to the |
| // vbase offset offset in Itanium. |
| BaseOffset = |
| 4 * CGM.getMicrosoftVTableContext().getVBTableIndex(RD, Base); |
| } |
| BFlags |= llvm::DINode::FlagVirtual; |
| } else |
| BaseOffset = CGM.getContext().toBits(RL.getBaseClassOffset(Base)); |
| // FIXME: Inconsistent units for BaseOffset. It is in bytes when |
| // BI->isVirtual() and bits when not. |
| |
| BFlags |= getAccessFlag(BI.getAccessSpecifier(), RD); |
| llvm::DIType *DTy = |
| DBuilder.createInheritance(RecordTy, BaseTy, BaseOffset, BFlags); |
| EltTys.push_back(DTy); |
| } |
| } |
| |
| llvm::DINodeArray |
| CGDebugInfo::CollectTemplateParams(const TemplateParameterList *TPList, |
| ArrayRef<TemplateArgument> TAList, |
| llvm::DIFile *Unit) { |
| SmallVector<llvm::Metadata *, 16> TemplateParams; |
| for (unsigned i = 0, e = TAList.size(); i != e; ++i) { |
| const TemplateArgument &TA = TAList[i]; |
| StringRef Name; |
| if (TPList) |
| Name = TPList->getParam(i)->getName(); |
| switch (TA.getKind()) { |
| case TemplateArgument::Type: { |
| llvm::DIType *TTy = getOrCreateType(TA.getAsType(), Unit); |
| TemplateParams.push_back( |
| DBuilder.createTemplateTypeParameter(TheCU, Name, TTy)); |
| } break; |
| case TemplateArgument::Integral: { |
| llvm::DIType *TTy = getOrCreateType(TA.getIntegralType(), Unit); |
| TemplateParams.push_back(DBuilder.createTemplateValueParameter( |
| TheCU, Name, TTy, |
| llvm::ConstantInt::get(CGM.getLLVMContext(), TA.getAsIntegral()))); |
| } break; |
| case TemplateArgument::Declaration: { |
| const ValueDecl *D = TA.getAsDecl(); |
| QualType T = TA.getParamTypeForDecl().getDesugaredType(CGM.getContext()); |
| llvm::DIType *TTy = getOrCreateType(T, Unit); |
| llvm::Constant *V = nullptr; |
| const CXXMethodDecl *MD; |
| // Variable pointer template parameters have a value that is the address |
| // of the variable. |
| if (const auto *VD = dyn_cast<VarDecl>(D)) |
| V = CGM.GetAddrOfGlobalVar(VD); |
| // Member function pointers have special support for building them, though |
| // this is currently unsupported in LLVM CodeGen. |
| else if ((MD = dyn_cast<CXXMethodDecl>(D)) && MD->isInstance()) |
| V = CGM.getCXXABI().EmitMemberFunctionPointer(MD); |
| else if (const auto *FD = dyn_cast<FunctionDecl>(D)) |
| V = CGM.GetAddrOfFunction(FD); |
| // Member data pointers have special handling too to compute the fixed |
| // offset within the object. |
| else if (const auto *MPT = dyn_cast<MemberPointerType>(T.getTypePtr())) { |
| // These five lines (& possibly the above member function pointer |
| // handling) might be able to be refactored to use similar code in |
| // CodeGenModule::getMemberPointerConstant |
| uint64_t fieldOffset = CGM.getContext().getFieldOffset(D); |
| CharUnits chars = |
| CGM.getContext().toCharUnitsFromBits((int64_t)fieldOffset); |
| V = CGM.getCXXABI().EmitMemberDataPointer(MPT, chars); |
| } |
| TemplateParams.push_back(DBuilder.createTemplateValueParameter( |
| TheCU, Name, TTy, |
| cast_or_null<llvm::Constant>(V->stripPointerCasts()))); |
| } break; |
| case TemplateArgument::NullPtr: { |
| QualType T = TA.getNullPtrType(); |
| llvm::DIType *TTy = getOrCreateType(T, Unit); |
| llvm::Constant *V = nullptr; |
| // Special case member data pointer null values since they're actually -1 |
| // instead of zero. |
| if (const auto *MPT = dyn_cast<MemberPointerType>(T.getTypePtr())) |
| // But treat member function pointers as simple zero integers because |
| // it's easier than having a special case in LLVM's CodeGen. If LLVM |
| // CodeGen grows handling for values of non-null member function |
| // pointers then perhaps we could remove this special case and rely on |
| // EmitNullMemberPointer for member function pointers. |
| if (MPT->isMemberDataPointer()) |
| V = CGM.getCXXABI().EmitNullMemberPointer(MPT); |
| if (!V) |
| V = llvm::ConstantInt::get(CGM.Int8Ty, 0); |
| TemplateParams.push_back(DBuilder.createTemplateValueParameter( |
| TheCU, Name, TTy, V)); |
| } break; |
| case TemplateArgument::Template: |
| TemplateParams.push_back(DBuilder.createTemplateTemplateParameter( |
| TheCU, Name, nullptr, |
| TA.getAsTemplate().getAsTemplateDecl()->getQualifiedNameAsString())); |
| break; |
| case TemplateArgument::Pack: |
| TemplateParams.push_back(DBuilder.createTemplateParameterPack( |
| TheCU, Name, nullptr, |
| CollectTemplateParams(nullptr, TA.getPackAsArray(), Unit))); |
| break; |
| case TemplateArgument::Expression: { |
| const Expr *E = TA.getAsExpr(); |
| QualType T = E->getType(); |
| if (E->isGLValue()) |
| T = CGM.getContext().getLValueReferenceType(T); |
| llvm::Constant *V = ConstantEmitter(CGM).emitAbstract(E, T); |
| assert(V && "Expression in template argument isn't constant"); |
| llvm::DIType *TTy = getOrCreateType(T, Unit); |
| TemplateParams.push_back(DBuilder.createTemplateValueParameter( |
| TheCU, Name, TTy, V->stripPointerCasts())); |
| } break; |
| // And the following should never occur: |
| case TemplateArgument::TemplateExpansion: |
| case TemplateArgument::Null: |
| llvm_unreachable( |
| "These argument types shouldn't exist in concrete types"); |
| } |
| } |
| return DBuilder.getOrCreateArray(TemplateParams); |
| } |
| |
| llvm::DINodeArray |
| CGDebugInfo::CollectFunctionTemplateParams(const FunctionDecl *FD, |
| llvm::DIFile *Unit) { |
| if (FD->getTemplatedKind() == |
| FunctionDecl::TK_FunctionTemplateSpecialization) { |
| const TemplateParameterList *TList = FD->getTemplateSpecializationInfo() |
| ->getTemplate() |
| ->getTemplateParameters(); |
| return CollectTemplateParams( |
| TList, FD->getTemplateSpecializationArgs()->asArray(), Unit); |
| } |
| return llvm::DINodeArray(); |
| } |
| |
| llvm::DINodeArray CGDebugInfo::CollectCXXTemplateParams( |
| const ClassTemplateSpecializationDecl *TSpecial, llvm::DIFile *Unit) { |
| // Always get the full list of parameters, not just the ones from |
| // the specialization. |
| TemplateParameterList *TPList = |
| TSpecial->getSpecializedTemplate()->getTemplateParameters(); |
| const TemplateArgumentList &TAList = TSpecial->getTemplateArgs(); |
| return CollectTemplateParams(TPList, TAList.asArray(), Unit); |
| } |
| |
| llvm::DIType *CGDebugInfo::getOrCreateVTablePtrType(llvm::DIFile *Unit) { |
| if (VTablePtrType) |
| return VTablePtrType; |
| |
| ASTContext &Context = CGM.getContext(); |
| |
| /* Function type */ |
| llvm::Metadata *STy = getOrCreateType(Context.IntTy, Unit); |
| llvm::DITypeRefArray SElements = DBuilder.getOrCreateTypeArray(STy); |
| llvm::DIType *SubTy = DBuilder.createSubroutineType(SElements); |
| unsigned Size = Context.getTypeSize(Context.VoidPtrTy); |
| unsigned VtblPtrAddressSpace = CGM.getTarget().getVtblPtrAddressSpace(); |
| Optional<unsigned> DWARFAddressSpace = |
| CGM.getTarget().getDWARFAddressSpace(VtblPtrAddressSpace); |
| |
| llvm::DIType *vtbl_ptr_type = |
| DBuilder.createPointerType(SubTy, Size, 0, DWARFAddressSpace, |
| "__vtbl_ptr_type"); |
| VTablePtrType = DBuilder.createPointerType(vtbl_ptr_type, Size); |
| return VTablePtrType; |
| } |
| |
| StringRef CGDebugInfo::getVTableName(const CXXRecordDecl *RD) { |
| // Copy the gdb compatible name on the side and use its reference. |
| return internString("_vptr$", RD->getNameAsString()); |
| } |
| |
| void CGDebugInfo::CollectVTableInfo(const CXXRecordDecl *RD, llvm::DIFile *Unit, |
| SmallVectorImpl<llvm::Metadata *> &EltTys, |
| llvm::DICompositeType *RecordTy) { |
| // If this class is not dynamic then there is not any vtable info to collect. |
| if (!RD->isDynamicClass()) |
| return; |
| |
| // Don't emit any vtable shape or vptr info if this class doesn't have an |
| // extendable vfptr. This can happen if the class doesn't have virtual |
| // methods, or in the MS ABI if those virtual methods only come from virtually |
| // inherited bases. |
| const ASTRecordLayout &RL = CGM.getContext().getASTRecordLayout(RD); |
| if (!RL.hasExtendableVFPtr()) |
| return; |
| |
| // CodeView needs to know how large the vtable of every dynamic class is, so |
| // emit a special named pointer type into the element list. The vptr type |
| // points to this type as well. |
| llvm::DIType *VPtrTy = nullptr; |
| bool NeedVTableShape = CGM.getCodeGenOpts().EmitCodeView && |
| CGM.getTarget().getCXXABI().isMicrosoft(); |
| if (NeedVTableShape) { |
| uint64_t PtrWidth = |
| CGM.getContext().getTypeSize(CGM.getContext().VoidPtrTy); |
| const VTableLayout &VFTLayout = |
| CGM.getMicrosoftVTableContext().getVFTableLayout(RD, CharUnits::Zero()); |
| unsigned VSlotCount = |
| VFTLayout.vtable_components().size() - CGM.getLangOpts().RTTIData; |
| unsigned VTableWidth = PtrWidth * VSlotCount; |
| unsigned VtblPtrAddressSpace = CGM.getTarget().getVtblPtrAddressSpace(); |
| Optional<unsigned> DWARFAddressSpace = |
| CGM.getTarget().getDWARFAddressSpace(VtblPtrAddressSpace); |
| |
| // Create a very wide void* type and insert it directly in the element list. |
| llvm::DIType *VTableType = |
| DBuilder.createPointerType(nullptr, VTableWidth, 0, DWARFAddressSpace, |
| "__vtbl_ptr_type"); |
| EltTys.push_back(VTableType); |
| |
| // The vptr is a pointer to this special vtable type. |
| VPtrTy = DBuilder.createPointerType(VTableType, PtrWidth); |
| } |
| |
| // If there is a primary base then the artificial vptr member lives there. |
| if (RL.getPrimaryBase()) |
| return; |
| |
| if (!VPtrTy) |
| VPtrTy = getOrCreateVTablePtrType(Unit); |
| |
| unsigned Size = CGM.getContext().getTypeSize(CGM.getContext().VoidPtrTy); |
| llvm::DIType *VPtrMember = DBuilder.createMemberType( |
| Unit, getVTableName(RD), Unit, 0, Size, 0, 0, |
| llvm::DINode::FlagArtificial, VPtrTy); |
| EltTys.push_back(VPtrMember); |
| } |
| |
| llvm::DIType *CGDebugInfo::getOrCreateRecordType(QualType RTy, |
| SourceLocation Loc) { |
| assert(DebugKind >= codegenoptions::LimitedDebugInfo); |
| llvm::DIType *T = getOrCreateType(RTy, getOrCreateFile(Loc)); |
| return T; |
| } |
| |
| llvm::DIType *CGDebugInfo::getOrCreateInterfaceType(QualType D, |
| SourceLocation Loc) { |
| return getOrCreateStandaloneType(D, Loc); |
| } |
| |
| llvm::DIType *CGDebugInfo::getOrCreateStandaloneType(QualType D, |
| SourceLocation Loc) { |
| assert(DebugKind >= codegenoptions::LimitedDebugInfo); |
| assert(!D.isNull() && "null type"); |
| llvm::DIType *T = getOrCreateType(D, getOrCreateFile(Loc)); |
| assert(T && "could not create debug info for type"); |
| |
| RetainedTypes.push_back(D.getAsOpaquePtr()); |
| return T; |
| } |
| |
| void CGDebugInfo::completeType(const EnumDecl *ED) { |
| if (DebugKind <= codegenoptions::DebugLineTablesOnly) |
| return; |
| QualType Ty = CGM.getContext().getEnumType(ED); |
| void *TyPtr = Ty.getAsOpaquePtr(); |
| auto I = TypeCache.find(TyPtr); |
| if (I == TypeCache.end() || !cast<llvm::DIType>(I->second)->isForwardDecl()) |
| return; |
| llvm::DIType *Res = CreateTypeDefinition(Ty->castAs<EnumType>()); |
| assert(!Res->isForwardDecl()); |
| TypeCache[TyPtr].reset(Res); |
| } |
| |
| void CGDebugInfo::completeType(const RecordDecl *RD) { |
| if (DebugKind > codegenoptions::LimitedDebugInfo || |
| !CGM.getLangOpts().CPlusPlus) |
| completeRequiredType(RD); |
| } |
| |
| /// Return true if the class or any of its methods are marked dllimport. |
| static bool isClassOrMethodDLLImport(const CXXRecordDecl *RD) { |
| if (RD->hasAttr<DLLImportAttr>()) |
| return true; |
| for (const CXXMethodDecl *MD : RD->methods()) |
| if (MD->hasAttr<DLLImportAttr>()) |
| return true; |
| return false; |
| } |
| |
| /// Does a type definition exist in an imported clang module? |
| static bool isDefinedInClangModule(const RecordDecl *RD) { |
| // Only definitions that where imported from an AST file come from a module. |
| if (!RD || !RD->isFromASTFile()) |
| return false; |
| // Anonymous entities cannot be addressed. Treat them as not from module. |
| if (!RD->isExternallyVisible() && RD->getName().empty()) |
| return false; |
| if (auto *CXXDecl = dyn_cast<CXXRecordDecl>(RD)) { |
| if (!CXXDecl->isCompleteDefinition()) |
| return false; |
| auto TemplateKind = CXXDecl->getTemplateSpecializationKind(); |
| if (TemplateKind != TSK_Undeclared) { |
| // This is a template, check the origin of the first member. |
| if (CXXDecl->field_begin() == CXXDecl->field_end()) |
| return TemplateKind == TSK_ExplicitInstantiationDeclaration; |
| if (!CXXDecl->field_begin()->isFromASTFile()) |
| return false; |
| } |
| } |
| return true; |
| } |
| |
| void CGDebugInfo::completeClassData(const RecordDecl *RD) { |
| if (auto *CXXRD = dyn_cast<CXXRecordDecl>(RD)) |
| if (CXXRD->isDynamicClass() && |
| CGM.getVTableLinkage(CXXRD) == |
| llvm::GlobalValue::AvailableExternallyLinkage && |
| !isClassOrMethodDLLImport(CXXRD)) |
| return; |
| |
| if (DebugTypeExtRefs && isDefinedInClangModule(RD->getDefinition())) |
| return; |
| |
| completeClass(RD); |
| } |
| |
| void CGDebugInfo::completeClass(const RecordDecl *RD) { |
| if (DebugKind <= codegenoptions::DebugLineTablesOnly) |
| return; |
| QualType Ty = CGM.getContext().getRecordType(RD); |
| void *TyPtr = Ty.getAsOpaquePtr(); |
| auto I = TypeCache.find(TyPtr); |
| if (I != TypeCache.end() && !cast<llvm::DIType>(I->second)->isForwardDecl()) |
| return; |
| llvm::DIType *Res = CreateTypeDefinition(Ty->castAs<RecordType>()); |
| assert(!Res->isForwardDecl()); |
| TypeCache[TyPtr].reset(Res); |
| } |
| |
| static bool hasExplicitMemberDefinition(CXXRecordDecl::method_iterator I, |
| CXXRecordDecl::method_iterator End) { |
| for (CXXMethodDecl *MD : llvm::make_range(I, End)) |
| if (FunctionDecl *Tmpl = MD->getInstantiatedFromMemberFunction()) |
| if (!Tmpl->isImplicit() && Tmpl->isThisDeclarationADefinition() && |
| !MD->getMemberSpecializationInfo()->isExplicitSpecialization()) |
| return true; |
| return false; |
| } |
| |
| static bool shouldOmitDefinition(codegenoptions::DebugInfoKind DebugKind, |
| bool DebugTypeExtRefs, const RecordDecl *RD, |
| const LangOptions &LangOpts) { |
| if (DebugTypeExtRefs && isDefinedInClangModule(RD->getDefinition())) |
| return true; |
| |
| if (auto *ES = RD->getASTContext().getExternalSource()) |
| if (ES->hasExternalDefinitions(RD) == ExternalASTSource::EK_Always) |
| return true; |
| |
| if (DebugKind > codegenoptions::LimitedDebugInfo) |
| return false; |
| |
| if (!LangOpts.CPlusPlus) |
| return false; |
| |
| if (!RD->isCompleteDefinitionRequired()) |
| return true; |
| |
| const auto *CXXDecl = dyn_cast<CXXRecordDecl>(RD); |
| |
| if (!CXXDecl) |
| return false; |
| |
| // Only emit complete debug info for a dynamic class when its vtable is |
| // emitted. However, Microsoft debuggers don't resolve type information |
| // across DLL boundaries, so skip this optimization if the class or any of its |
| // methods are marked dllimport. This isn't a complete solution, since objects |
| // without any dllimport methods can be used in one DLL and constructed in |
| // another, but it is the current behavior of LimitedDebugInfo. |
| if (CXXDecl->hasDefinition() && CXXDecl->isDynamicClass() && |
| !isClassOrMethodDLLImport(CXXDecl)) |
| return true; |
| |
| TemplateSpecializationKind Spec = TSK_Undeclared; |
| if (const auto *SD = dyn_cast<ClassTemplateSpecializationDecl>(RD)) |
| Spec = SD->getSpecializationKind(); |
| |
| if (Spec == TSK_ExplicitInstantiationDeclaration && |
| hasExplicitMemberDefinition(CXXDecl->method_begin(), |
| CXXDecl->method_end())) |
| return true; |
| |
| return false; |
| } |
| |
| void CGDebugInfo::completeRequiredType(const RecordDecl *RD) { |
| if (shouldOmitDefinition(DebugKind, DebugTypeExtRefs, RD, CGM.getLangOpts())) |
| return; |
| |
| QualType Ty = CGM.getContext().getRecordType(RD); |
| llvm::DIType *T = getTypeOrNull(Ty); |
| if (T && T->isForwardDecl()) |
| completeClassData(RD); |
| } |
| |
| llvm::DIType *CGDebugInfo::CreateType(const RecordType *Ty) { |
| RecordDecl *RD = Ty->getDecl(); |
| llvm::DIType *T = cast_or_null<llvm::DIType>(getTypeOrNull(QualType(Ty, 0))); |
| if (T || shouldOmitDefinition(DebugKind, DebugTypeExtRefs, RD, |
| CGM.getLangOpts())) { |
| if (!T) |
| T = getOrCreateRecordFwdDecl(Ty, getDeclContextDescriptor(RD)); |
| return T; |
| } |
| |
| return CreateTypeDefinition(Ty); |
| } |
| |
| llvm::DIType *CGDebugInfo::CreateTypeDefinition(const RecordType *Ty) { |
| RecordDecl *RD = Ty->getDecl(); |
| |
| // Get overall information about the record type for the debug info. |
| llvm::DIFile *DefUnit = getOrCreateFile(RD->getLocation()); |
| |
| // Records and classes and unions can all be recursive. To handle them, we |
| // first generate a debug descriptor for the struct as a forward declaration. |
| // Then (if it is a definition) we go through and get debug info for all of |
| // its members. Finally, we create a descriptor for the complete type (which |
| // may refer to the forward decl if the struct is recursive) and replace all |
| // uses of the forward declaration with the final definition. |
| llvm::DICompositeType *FwdDecl = getOrCreateLimitedType(Ty, DefUnit); |
| |
| const RecordDecl *D = RD->getDefinition(); |
| if (!D || !D->isCompleteDefinition()) |
| return FwdDecl; |
| |
| if (const auto *CXXDecl = dyn_cast<CXXRecordDecl>(RD)) |
| CollectContainingType(CXXDecl, FwdDecl); |
| |
| // Push the struct on region stack. |
| LexicalBlockStack.emplace_back(&*FwdDecl); |
| RegionMap[Ty->getDecl()].reset(FwdDecl); |
| |
| // Convert all the elements. |
| SmallVector<llvm::Metadata *, 16> EltTys; |
| // what about nested types? |
| |
| // Note: The split of CXXDecl information here is intentional, the |
| // gdb tests will depend on a certain ordering at printout. The debug |
| // information offsets are still correct if we merge them all together |
| // though. |
| const auto *CXXDecl = dyn_cast<CXXRecordDecl>(RD); |
| if (CXXDecl) { |
| CollectCXXBases(CXXDecl, DefUnit, EltTys, FwdDecl); |
| CollectVTableInfo(CXXDecl, DefUnit, EltTys, FwdDecl); |
| } |
| |
| // Collect data fields (including static variables and any initializers). |
| CollectRecordFields(RD, DefUnit, EltTys, FwdDecl); |
| if (CXXDecl) |
| CollectCXXMemberFunctions(CXXDecl, DefUnit, EltTys, FwdDecl); |
| |
| LexicalBlockStack.pop_back(); |
| RegionMap.erase(Ty->getDecl()); |
| |
| llvm::DINodeArray Elements = DBuilder.getOrCreateArray(EltTys); |
| DBuilder.replaceArrays(FwdDecl, Elements); |
| |
| if (FwdDecl->isTemporary()) |
| FwdDecl = |
| llvm::MDNode::replaceWithPermanent(llvm::TempDICompositeType(FwdDecl)); |
| |
| RegionMap[Ty->getDecl()].reset(FwdDecl); |
| return FwdDecl; |
| } |
| |
| llvm::DIType *CGDebugInfo::CreateType(const ObjCObjectType *Ty, |
| llvm::DIFile *Unit) { |
| // Ignore protocols. |
| return getOrCreateType(Ty->getBaseType(), Unit); |
| } |
| |
| llvm::DIType *CGDebugInfo::CreateType(const ObjCTypeParamType *Ty, |
| llvm::DIFile *Unit) { |
| // Ignore protocols. |
| SourceLocation Loc = Ty->getDecl()->getLocation(); |
| |
| // Use Typedefs to represent ObjCTypeParamType. |
| return DBuilder.createTypedef( |
| getOrCreateType(Ty->getDecl()->getUnderlyingType(), Unit), |
| Ty->getDecl()->getName(), getOrCreateFile(Loc), getLineNumber(Loc), |
| getDeclContextDescriptor(Ty->getDecl())); |
| } |
| |
| /// \return true if Getter has the default name for the property PD. |
| static bool hasDefaultGetterName(const ObjCPropertyDecl *PD, |
| const ObjCMethodDecl *Getter) { |
| assert(PD); |
| if (!Getter) |
| return true; |
| |
| assert(Getter->getDeclName().isObjCZeroArgSelector()); |
| return PD->getName() == |
| Getter->getDeclName().getObjCSelector().getNameForSlot(0); |
| } |
| |
| /// \return true if Setter has the default name for the property PD. |
| static bool hasDefaultSetterName(const ObjCPropertyDecl *PD, |
| const ObjCMethodDecl *Setter) { |
| assert(PD); |
| if (!Setter) |
| return true; |
| |
| assert(Setter->getDeclName().isObjCOneArgSelector()); |
| return SelectorTable::constructSetterName(PD->getName()) == |
| Setter->getDeclName().getObjCSelector().getNameForSlot(0); |
| } |
| |
| llvm::DIType *CGDebugInfo::CreateType(const ObjCInterfaceType *Ty, |
| llvm::DIFile *Unit) { |
| ObjCInterfaceDecl *ID = Ty->getDecl(); |
| if (!ID) |
| return nullptr; |
| |
| // Return a forward declaration if this type was imported from a clang module, |
| // and this is not the compile unit with the implementation of the type (which |
| // may contain hidden ivars). |
| if (DebugTypeExtRefs && ID->isFromASTFile() && ID->getDefinition() && |
| !ID->getImplementation()) |
| return DBuilder.createForwardDecl(llvm::dwarf::DW_TAG_structure_type, |
| ID->getName(), |
| getDeclContextDescriptor(ID), Unit, 0); |
| |
| // Get overall information about the record type for the debug info. |
| llvm::DIFile *DefUnit = getOrCreateFile(ID->getLocation()); |
| unsigned Line = getLineNumber(ID->getLocation()); |
| auto RuntimeLang = |
| static_cast<llvm::dwarf::SourceLanguage>(TheCU->getSourceLanguage()); |
| |
| // If this is just a forward declaration return a special forward-declaration |
| // debug type since we won't be able to lay out the entire type. |
| ObjCInterfaceDecl *Def = ID->getDefinition(); |
| if (!Def || !Def->getImplementation()) { |
| llvm::DIScope *Mod = getParentModuleOrNull(ID); |
| llvm::DIType *FwdDecl = DBuilder.createReplaceableCompositeType( |
| llvm::dwarf::DW_TAG_structure_type, ID->getName(), Mod ? Mod : TheCU, |
| DefUnit, Line, RuntimeLang); |
| ObjCInterfaceCache.push_back(ObjCInterfaceCacheEntry(Ty, FwdDecl, Unit)); |
| return FwdDecl; |
| } |
| |
| return CreateTypeDefinition(Ty, Unit); |
| } |
| |
| llvm::DIModule * |
| CGDebugInfo::getOrCreateModuleRef(ExternalASTSource::ASTSourceDescriptor Mod, |
| bool CreateSkeletonCU) { |
| // Use the Module pointer as the key into the cache. This is a |
| // nullptr if the "Module" is a PCH, which is safe because we don't |
| // support chained PCH debug info, so there can only be a single PCH. |
| const Module *M = Mod.getModuleOrNull(); |
| auto ModRef = ModuleCache.find(M); |
| if (ModRef != ModuleCache.end()) |
| return cast<llvm::DIModule>(ModRef->second); |
| |
| // Macro definitions that were defined with "-D" on the command line. |
| SmallString<128> ConfigMacros; |
| { |
| llvm::raw_svector_ostream OS(ConfigMacros); |
| const auto &PPOpts = CGM.getPreprocessorOpts(); |
| unsigned I = 0; |
| // Translate the macro definitions back into a commmand line. |
| for (auto &M : PPOpts.Macros) { |
| if (++I > 1) |
| OS << " "; |
| const std::string &Macro = M.first; |
| bool Undef = M.second; |
| OS << "\"-" << (Undef ? 'U' : 'D'); |
| for (char c : Macro) |
| switch (c) { |
| case '\\' : OS << "\\\\"; break; |
| case '"' : OS << "\\\""; break; |
| default: OS << c; |
| } |
| OS << '\"'; |
| } |
| } |
| |
| bool IsRootModule = M ? !M->Parent : true; |
| if (CreateSkeletonCU && IsRootModule) { |
| // PCH files don't have a signature field in the control block, |
| // but LLVM detects skeleton CUs by looking for a non-zero DWO id. |
| // We use the lower 64 bits for debug info. |
| uint64_t Signature = |
| Mod.getSignature() |
| ? (uint64_t)Mod.getSignature()[1] << 32 | Mod.getSignature()[0] |
| : ~1ULL; |
| llvm::DIBuilder DIB(CGM.getModule()); |
| DIB.createCompileUnit(TheCU->getSourceLanguage(), |
| DIB.createFile(Mod.getModuleName(), Mod.getPath()), |
| TheCU->getProducer(), true, StringRef(), 0, |
| Mod.getASTFile(), llvm::DICompileUnit::FullDebug, |
| Signature); |
| DIB.finalize(); |
| } |
| llvm::DIModule *Parent = |
| IsRootModule ? nullptr |
| : getOrCreateModuleRef( |
| ExternalASTSource::ASTSourceDescriptor(*M->Parent), |
| CreateSkeletonCU); |
| llvm::DIModule *DIMod = |
| DBuilder.createModule(Parent, Mod.getModuleName(), ConfigMacros, |
| Mod.getPath(), CGM.getHeaderSearchOpts().Sysroot); |
| ModuleCache[M].reset(DIMod); |
| return DIMod; |
| } |
| |
| llvm::DIType *CGDebugInfo::CreateTypeDefinition(const ObjCInterfaceType *Ty, |
| llvm::DIFile *Unit) { |
| ObjCInterfaceDecl *ID = Ty->getDecl(); |
| llvm::DIFile *DefUnit = getOrCreateFile(ID->getLocation()); |
| unsigned Line = getLineNumber(ID->getLocation()); |
| unsigned RuntimeLang = TheCU->getSourceLanguage(); |
| |
| // Bit size, align and offset of the type. |
| uint64_t Size = CGM.getContext().getTypeSize(Ty); |
| auto Align = getTypeAlignIfRequired(Ty, CGM.getContext()); |
| |
| llvm::DINode::DIFlags Flags = llvm::DINode::FlagZero; |
| if (ID->getImplementation()) |
| Flags |= llvm::DINode::FlagObjcClassComplete; |
| |
| llvm::DIScope *Mod = getParentModuleOrNull(ID); |
| llvm::DICompositeType *RealDecl = DBuilder.createStructType( |
| Mod ? Mod : Unit, ID->getName(), DefUnit, Line, Size, Align, Flags, |
| nullptr, llvm::DINodeArray(), RuntimeLang); |
| |
| QualType QTy(Ty, 0); |
| TypeCache[QTy.getAsOpaquePtr()].reset(RealDecl); |
| |
| // Push the struct on region stack. |
| LexicalBlockStack.emplace_back(RealDecl); |
| RegionMap[Ty->getDecl()].reset(RealDecl); |
| |
| // Convert all the elements. |
| SmallVector<llvm::Metadata *, 16> EltTys; |
| |
| ObjCInterfaceDecl *SClass = ID->getSuperClass(); |
| if (SClass) { |
| llvm::DIType *SClassTy = |
| getOrCreateType(CGM.getContext().getObjCInterfaceType(SClass), Unit); |
| if (!SClassTy) |
| return nullptr; |
| |
| llvm::DIType *InhTag = DBuilder.createInheritance(RealDecl, SClassTy, 0, |
| llvm::DINode::FlagZero); |
| EltTys.push_back(InhTag); |
| } |
| |
| // Create entries for all of the properties. |
| auto AddProperty = [&](const ObjCPropertyDecl *PD) { |
| SourceLocation Loc = PD->getLocation(); |
| llvm::DIFile *PUnit = getOrCreateFile(Loc); |
| unsigned PLine = getLineNumber(Loc); |
| ObjCMethodDecl *Getter = PD->getGetterMethodDecl(); |
| ObjCMethodDecl *Setter = PD->getSetterMethodDecl(); |
| llvm::MDNode *PropertyNode = DBuilder.createObjCProperty( |
| PD->getName(), PUnit, PLine, |
| hasDefaultGetterName(PD, Getter) ? "" |
| : getSelectorName(PD->getGetterName()), |
| hasDefaultSetterName(PD, Setter) ? "" |
| : getSelectorName(PD->getSetterName()), |
| PD->getPropertyAttributes(), getOrCreateType(PD->getType(), PUnit)); |
| EltTys.push_back(PropertyNode); |
| }; |
| { |
| llvm::SmallPtrSet<const IdentifierInfo*, 16> PropertySet; |
| for (const ObjCCategoryDecl *ClassExt : ID->known_extensions()) |
| for (auto *PD : ClassExt->properties()) { |
| PropertySet.insert(PD->getIdentifier()); |
| AddProperty(PD); |
| } |
| for (const auto *PD : ID->properties()) { |
| // Don't emit duplicate metadata for properties that were already in a |
| // class extension. |
| if (!PropertySet.insert(PD->getIdentifier()).second) |
| continue; |
| AddProperty(PD); |
| } |
| } |
| |
| const ASTRecordLayout &RL = CGM.getContext().getASTObjCInterfaceLayout(ID); |
| unsigned FieldNo = 0; |
| for (ObjCIvarDecl *Field = ID->all_declared_ivar_begin(); Field; |
| Field = Field->getNextIvar(), ++FieldNo) { |
| llvm::DIType *FieldTy = getOrCreateType(Field->getType(), Unit); |
| if (!FieldTy) |
| return nullptr; |
| |
| StringRef FieldName = Field->getName(); |
| |
| // Ignore unnamed fields. |
| if (FieldName.empty()) |
| continue; |
| |
| // Get the location for the field. |
| llvm::DIFile *FieldDefUnit = getOrCreateFile(Field->getLocation()); |
| unsigned FieldLine = getLineNumber(Field->getLocation()); |
| QualType FType = Field->getType(); |
| uint64_t FieldSize = 0; |
| uint32_t FieldAlign = 0; |
| |
| if (!FType->isIncompleteArrayType()) { |
| |
| // Bit size, align and offset of the type. |
| FieldSize = Field->isBitField() |
| ? Field->getBitWidthValue(CGM.getContext()) |
| : CGM.getContext().getTypeSize(FType); |
| FieldAlign = getTypeAlignIfRequired(FType, CGM.getContext()); |
| } |
| |
| uint64_t FieldOffset; |
| if (CGM.getLangOpts().ObjCRuntime.isNonFragile()) { |
| // We don't know the runtime offset of an ivar if we're using the |
| // non-fragile ABI. For bitfields, use the bit offset into the first |
| // byte of storage of the bitfield. For other fields, use zero. |
| if (Field->isBitField()) { |
| FieldOffset = |
| CGM.getObjCRuntime().ComputeBitfieldBitOffset(CGM, ID, Field); |
| FieldOffset %= CGM.getContext().getCharWidth(); |
| } else { |
| FieldOffset = 0; |
| } |
| } else { |
| FieldOffset = RL.getFieldOffset(FieldNo); |
| } |
| |
| llvm::DINode::DIFlags Flags = llvm::DINode::FlagZero; |
| if (Field->getAccessControl() == ObjCIvarDecl::Protected) |
| Flags = llvm::DINode::FlagProtected; |
| else if (Field->getAccessControl() == ObjCIvarDecl::Private) |
| Flags = llvm::DINode::FlagPrivate; |
| else if (Field->getAccessControl() == ObjCIvarDecl::Public) |
| Flags = llvm::DINode::FlagPublic; |
| |
| llvm::MDNode *PropertyNode = nullptr; |
| if (ObjCImplementationDecl *ImpD = ID->getImplementation()) { |
| if (ObjCPropertyImplDecl *PImpD = |
| ImpD->FindPropertyImplIvarDecl(Field->getIdentifier())) { |
| if (ObjCPropertyDecl *PD = PImpD->getPropertyDecl()) { |
| SourceLocation Loc = PD->getLocation(); |
| llvm::DIFile *PUnit = getOrCreateFile(Loc); |
| unsigned PLine = getLineNumber(Loc); |
| ObjCMethodDecl *Getter = PD->getGetterMethodDecl(); |
| ObjCMethodDecl *Setter = PD->getSetterMethodDecl(); |
| PropertyNode = DBuilder.createObjCProperty( |
| PD->getName(), PUnit, PLine, |
| hasDefaultGetterName(PD, Getter) ? "" : getSelectorName( |
| PD->getGetterName()), |
| hasDefaultSetterName(PD, Setter) ? "" : getSelectorName( |
| PD->getSetterName()), |
| PD->getPropertyAttributes(), |
| getOrCreateType(PD->getType(), PUnit)); |
| } |
| } |
| } |
| FieldTy = DBuilder.createObjCIVar(FieldName, FieldDefUnit, FieldLine, |
| FieldSize, FieldAlign, FieldOffset, Flags, |
| FieldTy, PropertyNode); |
| EltTys.push_back(FieldTy); |
| } |
| |
| llvm::DINodeArray Elements = DBuilder.getOrCreateArray(EltTys); |
| DBuilder.replaceArrays(RealDecl, Elements); |
| |
| LexicalBlockStack.pop_back(); |
| return RealDecl; |
| } |
| |
| llvm::DIType *CGDebugInfo::CreateType(const VectorType *Ty, |
| llvm::DIFile *Unit) { |
| llvm::DIType *ElementTy = getOrCreateType(Ty->getElementType(), Unit); |
| int64_t Count = Ty->getNumElements(); |
| if (Count == 0) |
| // If number of elements are not known then this is an unbounded array. |
| // Use Count == -1 to express such arrays. |
| Count = -1; |
| |
| llvm::Metadata *Subscript = DBuilder.getOrCreateSubrange(0, Count); |
| llvm::DINodeArray SubscriptArray = DBuilder.getOrCreateArray(Subscript); |
| |
| uint64_t Size = CGM.getContext().getTypeSize(Ty); |
| auto Align = getTypeAlignIfRequired(Ty, CGM.getContext()); |
| |
| return DBuilder.createVectorType(Size, Align, ElementTy, SubscriptArray); |
| } |
| |
| llvm::DIType *CGDebugInfo::CreateType(const ArrayType *Ty, llvm::DIFile *Unit) { |
| uint64_t Size; |
| uint32_t Align; |
| |
| // FIXME: make getTypeAlign() aware of VLAs and incomplete array types |
| if (const auto *VAT = dyn_cast<VariableArrayType>(Ty)) { |
| Size = 0; |
| Align = getTypeAlignIfRequired(CGM.getContext().getBaseElementType(VAT), |
| CGM.getContext()); |
| } else if (Ty->isIncompleteArrayType()) { |
| Size = 0; |
| if (Ty->getElementType()->isIncompleteType()) |
| Align = 0; |
| else |
| Align = getTypeAlignIfRequired(Ty->getElementType(), CGM.getContext()); |
| } else if (Ty->isIncompleteType()) { |
| Size = 0; |
| Align = 0; |
| } else { |
| // Size and align of the whole array, not the element type. |
| Size = CGM.getContext().getTypeSize(Ty); |
| Align = getTypeAlignIfRequired(Ty, CGM.getContext()); |
| } |
| |
| // Add the dimensions of the array. FIXME: This loses CV qualifiers from |
| // interior arrays, do we care? Why aren't nested arrays represented the |
| // obvious/recursive way? |
| SmallVector<llvm::Metadata *, 8> Subscripts; |
| QualType EltTy(Ty, 0); |
| while ((Ty = dyn_cast<ArrayType>(EltTy))) { |
| // If the number of elements is known, then count is that number. Otherwise, |
| // it's -1. This allows us to represent a subrange with an array of 0 |
| // elements, like this: |
| // |
| // struct foo { |
| // int x[0]; |
| // }; |
| int64_t Count = -1; // Count == -1 is an unbounded array. |
| if (const auto *CAT = dyn_cast<ConstantArrayType>(Ty)) |
| Count = CAT->getSize().getZExtValue(); |
| else if (const auto *VAT = dyn_cast<VariableArrayType>(Ty)) { |
| if (Expr *Size = VAT->getSizeExpr()) { |
| llvm::APSInt V; |
| if (Size->EvaluateAsInt(V, CGM.getContext())) |
| Count = V.getExtValue(); |
| } |
| } |
| |
| // FIXME: Verify this is right for VLAs. |
| Subscripts.push_back(DBuilder.getOrCreateSubrange(0, Count)); |
| EltTy = Ty->getElementType(); |
| } |
| |
| llvm::DINodeArray SubscriptArray = DBuilder.getOrCreateArray(Subscripts); |
| |
| return DBuilder.createArrayType(Size, Align, getOrCreateType(EltTy, Unit), |
| SubscriptArray); |
| } |
| |
| llvm::DIType *CGDebugInfo::CreateType(const LValueReferenceType *Ty, |
| llvm::DIFile *Unit) { |
| return CreatePointerLikeType(llvm::dwarf::DW_TAG_reference_type, Ty, |
| Ty->getPointeeType(), Unit); |
| } |
| |
| llvm::DIType *CGDebugInfo::CreateType(const RValueReferenceType *Ty, |
| llvm::DIFile *Unit) { |
| return CreatePointerLikeType(llvm::dwarf::DW_TAG_rvalue_reference_type, Ty, |
| Ty->getPointeeType(), Unit); |
| } |
| |
| llvm::DIType *CGDebugInfo::CreateType(const MemberPointerType *Ty, |
| llvm::DIFile *U) { |
| llvm::DINode::DIFlags Flags = llvm::DINode::FlagZero; |
| uint64_t Size = 0; |
| |
| if (!Ty->isIncompleteType()) { |
| Size = CGM.getContext().getTypeSize(Ty); |
| |
| // Set the MS inheritance model. There is no flag for the unspecified model. |
| if (CGM.getTarget().getCXXABI().isMicrosoft()) { |
| switch (Ty->getMostRecentCXXRecordDecl()->getMSInheritanceModel()) { |
| case MSInheritanceAttr::Keyword_single_inheritance: |
| Flags |= llvm::DINode::FlagSingleInheritance; |
| break; |
| case MSInheritanceAttr::Keyword_multiple_inheritance: |
| Flags |= llvm::DINode::FlagMultipleInheritance; |
| break; |
| case MSInheritanceAttr::Keyword_virtual_inheritance: |
| Flags |= llvm::DINode::FlagVirtualInheritance; |
| break; |
| case MSInheritanceAttr::Keyword_unspecified_inheritance: |
| break; |
| } |
| } |
| } |
| |
| llvm::DIType *ClassType = getOrCreateType(QualType(Ty->getClass(), 0), U); |
| if (Ty->isMemberDataPointerType()) |
| return DBuilder.createMemberPointerType( |
| getOrCreateType(Ty->getPointeeType(), U), ClassType, Size, /*Align=*/0, |
| Flags); |
| |
| const FunctionProtoType *FPT = |
| Ty->getPointeeType()->getAs<FunctionProtoType>(); |
| return DBuilder.createMemberPointerType( |
| getOrCreateInstanceMethodType(CGM.getContext().getPointerType(QualType( |
| Ty->getClass(), FPT->getTypeQuals())), |
| FPT, U), |
| ClassType, Size, /*Align=*/0, Flags); |
| } |
| |
| llvm::DIType *CGDebugInfo::CreateType(const AtomicType *Ty, llvm::DIFile *U) { |
| auto *FromTy = getOrCreateType(Ty->getValueType(), U); |
| return DBuilder.createQualifiedType(llvm::dwarf::DW_TAG_atomic_type, FromTy); |
| } |
| |
| llvm::DIType* CGDebugInfo::CreateType(const PipeType *Ty, |
| llvm::DIFile *U) { |
| return getOrCreateType(Ty->getElementType(), U); |
| } |
| |
| llvm::DIType *CGDebugInfo::CreateEnumType(const EnumType *Ty) { |
| const EnumDecl *ED = Ty->getDecl(); |
| |
| uint64_t Size = 0; |
| uint32_t Align = 0; |
| if (!ED->getTypeForDecl()->isIncompleteType()) { |
| Size = CGM.getContext().getTypeSize(ED->getTypeForDecl()); |
| Align = getDeclAlignIfRequired(ED, CGM.getContext()); |
| } |
| |
| SmallString<256> FullName = getUniqueTagTypeName(Ty, CGM, TheCU); |
| |
| bool isImportedFromModule = |
| DebugTypeExtRefs && ED->isFromASTFile() && ED->getDefinition(); |
| |
| // If this is just a forward declaration, construct an appropriately |
| // marked node and just return it. |
| if (isImportedFromModule || !ED->getDefinition()) { |
| // Note that it is possible for enums to be created as part of |
| // their own declcontext. In this case a FwdDecl will be created |
| // twice. This doesn't cause a problem because both FwdDecls are |
| // entered into the ReplaceMap: finalize() will replace the first |
| // FwdDecl with the second and then replace the second with |
| // complete type. |
| llvm::DIScope *EDContext = getDeclContextDescriptor(ED); |
| llvm::DIFile *DefUnit = getOrCreateFile(ED->getLocation()); |
| llvm::TempDIScope TmpContext(DBuilder.createReplaceableCompositeType( |
| llvm::dwarf::DW_TAG_enumeration_type, "", TheCU, DefUnit, 0)); |
| |
| unsigned Line = getLineNumber(ED->getLocation()); |
| StringRef EDName = ED->getName(); |
| llvm::DIType *RetTy = DBuilder.createReplaceableCompositeType( |
| llvm::dwarf::DW_TAG_enumeration_type, EDName, EDContext, DefUnit, Line, |
| 0, Size, Align, llvm::DINode::FlagFwdDecl, FullName); |
| |
| ReplaceMap.emplace_back( |
| std::piecewise_construct, std::make_tuple(Ty), |
| std::make_tuple(static_cast<llvm::Metadata *>(RetTy))); |
| return RetTy; |
| } |
| |
| return CreateTypeDefinition(Ty); |
| } |
| |
| llvm::DIType *CGDebugInfo::CreateTypeDefinition(const EnumType *Ty) { |
| const EnumDecl *ED = Ty->getDecl(); |
| uint64_t Size = 0; |
| uint32_t Align = 0; |
| if (!ED->getTypeForDecl()->isIncompleteType()) { |
| Size = CGM.getContext().getTypeSize(ED->getTypeForDecl()); |
| Align = getDeclAlignIfRequired(ED, CGM.getContext()); |
| } |
| |
| SmallString<256> FullName = getUniqueTagTypeName(Ty, CGM, TheCU); |
| |
| // Create elements for each enumerator. |
| SmallVector<llvm::Metadata *, 16> Enumerators; |
| ED = ED->getDefinition(); |
| for (const auto *Enum : ED->enumerators()) { |
| Enumerators.push_back(DBuilder.createEnumerator( |
| Enum->getName(), Enum->getInitVal().getSExtValue())); |
| } |
| |
| // Return a CompositeType for the enum itself. |
| llvm::DINodeArray EltArray = DBuilder.getOrCreateArray(Enumerators); |
| |
| llvm::DIFile *DefUnit = getOrCreateFile(ED->getLocation()); |
| unsigned Line = getLineNumber(ED->getLocation()); |
| llvm::DIScope *EnumContext = getDeclContextDescriptor(ED); |
| llvm::DIType *ClassTy = |
| ED->isFixed() ? getOrCreateType(ED->getIntegerType(), DefUnit) : nullptr; |
| return DBuilder.createEnumerationType(EnumContext, ED->getName(), DefUnit, |
| Line, Size, Align, EltArray, ClassTy, |
| FullName); |
| } |
| |
| llvm::DIMacro *CGDebugInfo::CreateMacro(llvm::DIMacroFile *Parent, |
| unsigned MType, SourceLocation LineLoc, |
| StringRef Name, StringRef Value) { |
| unsigned Line = LineLoc.isInvalid() ? 0 : getLineNumber(LineLoc); |
| return DBuilder.createMacro(Parent, Line, MType, Name, Value); |
| } |
| |
| llvm::DIMacroFile *CGDebugInfo::CreateTempMacroFile(llvm::DIMacroFile *Parent, |
| SourceLocation LineLoc, |
| SourceLocation FileLoc) { |
| llvm::DIFile *FName = getOrCreateFile(FileLoc); |
| unsigned Line = LineLoc.isInvalid() ? 0 : getLineNumber(LineLoc); |
| return DBuilder.createTempMacroFile(Parent, Line, FName); |
| } |
| |
| static QualType UnwrapTypeForDebugInfo(QualType T, const ASTContext &C) { |
| Qualifiers Quals; |
| do { |
| Qualifiers InnerQuals = T.getLocalQualifiers(); |
| // Qualifiers::operator+() doesn't like it if you add a Qualifier |
| // that is already there. |
| Quals += Qualifiers::removeCommonQualifiers(Quals, InnerQuals); |
| Quals += InnerQuals; |
| QualType LastT = T; |
| switch (T->getTypeClass()) { |
| default: |
| return C.getQualifiedType(T.getTypePtr(), Quals); |
| case Type::TemplateSpecialization: { |
| const auto *Spec = cast<TemplateSpecializationType>(T); |
| if (Spec->isTypeAlias()) |
| return C.getQualifiedType(T.getTypePtr(), Quals); |
| T = Spec->desugar(); |
| break; |
| } |
| case Type::TypeOfExpr: |
| T = cast<TypeOfExprType>(T)->getUnderlyingExpr()->getType(); |
| break; |
| case Type::TypeOf: |
| T = cast<TypeOfType>(T)->getUnderlyingType(); |
| break; |
| case Type::Decltype: |
| T = cast<DecltypeType>(T)->getUnderlyingType(); |
| break; |
| case Type::UnaryTransform: |
| T = cast<UnaryTransformType>(T)->getUnderlyingType(); |
| break; |
| case Type::Attributed: |
| T = cast<AttributedType>(T)->getEquivalentType(); |
| break; |
| case Type::Elaborated: |
| T = cast<ElaboratedType>(T)->getNamedType(); |
| break; |
| case Type::Paren: |
| T = cast<ParenType>(T)->getInnerType(); |
| break; |
| case Type::SubstTemplateTypeParm: |
| T = cast<SubstTemplateTypeParmType>(T)->getReplacementType(); |
| break; |
| case Type::Auto: |
| case Type::DeducedTemplateSpecialization: { |
| QualType DT = cast<DeducedType>(T)->getDeducedType(); |
|