Guy Benyei | 11169dd | 2012-12-18 14:30:41 +0000 | [diff] [blame] | 1 | //===--- CGDebugInfo.cpp - Emit Debug Information for a Module ------------===// |
| 2 | // |
| 3 | // The LLVM Compiler Infrastructure |
| 4 | // |
| 5 | // This file is distributed under the University of Illinois Open Source |
| 6 | // License. See LICENSE.TXT for details. |
| 7 | // |
| 8 | //===----------------------------------------------------------------------===// |
| 9 | // |
| 10 | // This coordinates the debug information generation while generating code. |
| 11 | // |
| 12 | //===----------------------------------------------------------------------===// |
| 13 | |
| 14 | #include "CGDebugInfo.h" |
| 15 | #include "CGBlocks.h" |
David Blaikie | 38079fd | 2013-05-10 21:53:14 +0000 | [diff] [blame] | 16 | #include "CGCXXABI.h" |
Guy Benyei | 11169dd | 2012-12-18 14:30:41 +0000 | [diff] [blame] | 17 | #include "CGObjCRuntime.h" |
Bob Haarman | dff3673 | 2016-10-25 22:19:32 +0000 | [diff] [blame] | 18 | #include "CGRecordLayout.h" |
Guy Benyei | 11169dd | 2012-12-18 14:30:41 +0000 | [diff] [blame] | 19 | #include "CodeGenFunction.h" |
| 20 | #include "CodeGenModule.h" |
| 21 | #include "clang/AST/ASTContext.h" |
| 22 | #include "clang/AST/DeclFriend.h" |
| 23 | #include "clang/AST/DeclObjC.h" |
| 24 | #include "clang/AST/DeclTemplate.h" |
| 25 | #include "clang/AST/Expr.h" |
| 26 | #include "clang/AST/RecordLayout.h" |
| 27 | #include "clang/Basic/FileManager.h" |
| 28 | #include "clang/Basic/SourceManager.h" |
| 29 | #include "clang/Basic/Version.h" |
Saleem Abdulrasool | 10a4972 | 2016-04-08 16:52:00 +0000 | [diff] [blame] | 30 | #include "clang/Frontend/CodeGenOptions.h" |
Adrian Prantl | c4bb47e | 2015-06-30 17:39:51 +0000 | [diff] [blame] | 31 | #include "clang/Lex/HeaderSearchOptions.h" |
Adrian Prantl | 9402cef | 2015-09-20 16:51:35 +0000 | [diff] [blame] | 32 | #include "clang/Lex/ModuleMap.h" |
Adrian Prantl | c4bb47e | 2015-06-30 17:39:51 +0000 | [diff] [blame] | 33 | #include "clang/Lex/PreprocessorOptions.h" |
Bob Haarman | dff3673 | 2016-10-25 22:19:32 +0000 | [diff] [blame] | 34 | #include "llvm/ADT/DenseSet.h" |
Guy Benyei | 11169dd | 2012-12-18 14:30:41 +0000 | [diff] [blame] | 35 | #include "llvm/ADT/SmallVector.h" |
| 36 | #include "llvm/ADT/StringExtras.h" |
Chandler Carruth | ffd5551 | 2013-01-02 11:45:17 +0000 | [diff] [blame] | 37 | #include "llvm/IR/Constants.h" |
| 38 | #include "llvm/IR/DataLayout.h" |
| 39 | #include "llvm/IR/DerivedTypes.h" |
| 40 | #include "llvm/IR/Instructions.h" |
| 41 | #include "llvm/IR/Intrinsics.h" |
| 42 | #include "llvm/IR/Module.h" |
Guy Benyei | 11169dd | 2012-12-18 14:30:41 +0000 | [diff] [blame] | 43 | #include "llvm/Support/FileSystem.h" |
Amjad Aboud | e2aab8c | 2016-12-25 10:12:27 +0000 | [diff] [blame] | 44 | #include "llvm/Support/MD5.h" |
Adrian Prantl | 0630eb7 | 2013-12-18 21:48:18 +0000 | [diff] [blame] | 45 | #include "llvm/Support/Path.h" |
Guy Benyei | 11169dd | 2012-12-18 14:30:41 +0000 | [diff] [blame] | 46 | using namespace clang; |
| 47 | using namespace clang::CodeGen; |
| 48 | |
Victor Leschuk | a7ece03 | 2016-10-20 00:13:19 +0000 | [diff] [blame] | 49 | static uint32_t getTypeAlignIfRequired(const Type *Ty, const ASTContext &Ctx) { |
| 50 | auto TI = Ctx.getTypeInfo(Ty); |
| 51 | return TI.AlignIsRequired ? TI.Align : 0; |
| 52 | } |
| 53 | |
| 54 | static uint32_t getTypeAlignIfRequired(QualType Ty, const ASTContext &Ctx) { |
| 55 | return getTypeAlignIfRequired(Ty.getTypePtr(), Ctx); |
| 56 | } |
| 57 | |
| 58 | static uint32_t getDeclAlignIfRequired(const Decl *D, const ASTContext &Ctx) { |
| 59 | return D->hasAttr<AlignedAttr>() ? D->getMaxAlignment() : 0; |
| 60 | } |
| 61 | |
Guy Benyei | 11169dd | 2012-12-18 14:30:41 +0000 | [diff] [blame] | 62 | CGDebugInfo::CGDebugInfo(CodeGenModule &CGM) |
Eric Christopher | 324bbbd | 2013-07-14 21:12:44 +0000 | [diff] [blame] | 63 | : CGM(CGM), DebugKind(CGM.getCodeGenOpts().getDebugInfo()), |
Adrian Prantl | 6b21ab2 | 2015-08-27 19:46:20 +0000 | [diff] [blame] | 64 | DebugTypeExtRefs(CGM.getCodeGenOpts().DebugTypeExtRefs), |
Eric Christopher | 324bbbd | 2013-07-14 21:12:44 +0000 | [diff] [blame] | 65 | DBuilder(CGM.getModule()) { |
Saleem Abdulrasool | 436256a | 2015-10-12 20:21:08 +0000 | [diff] [blame] | 66 | for (const auto &KV : CGM.getCodeGenOpts().DebugPrefixMap) |
| 67 | DebugPrefixMap[KV.first] = KV.second; |
Guy Benyei | 11169dd | 2012-12-18 14:30:41 +0000 | [diff] [blame] | 68 | CreateCompileUnit(); |
| 69 | } |
| 70 | |
| 71 | CGDebugInfo::~CGDebugInfo() { |
| 72 | assert(LexicalBlockStack.empty() && |
| 73 | "Region stack mismatch, stack not empty!"); |
| 74 | } |
| 75 | |
David Blaikie | 66e4197 | 2015-01-14 07:38:27 +0000 | [diff] [blame] | 76 | ApplyDebugLocation::ApplyDebugLocation(CodeGenFunction &CGF, |
David Blaikie | 835afb2 | 2015-01-21 23:08:17 +0000 | [diff] [blame] | 77 | SourceLocation TemporaryLocation) |
David Blaikie | d7057d9 | 2015-08-12 23:49:57 +0000 | [diff] [blame] | 78 | : CGF(&CGF) { |
David Blaikie | 9b47966 | 2015-01-25 01:19:10 +0000 | [diff] [blame] | 79 | init(TemporaryLocation); |
| 80 | } |
| 81 | |
Adrian Prantl | 39428e7 | 2015-02-03 18:40:42 +0000 | [diff] [blame] | 82 | ApplyDebugLocation::ApplyDebugLocation(CodeGenFunction &CGF, |
Adrian Prantl | 95b24e9 | 2015-02-03 20:00:54 +0000 | [diff] [blame] | 83 | bool DefaultToEmpty, |
Adrian Prantl | 39428e7 | 2015-02-03 18:40:42 +0000 | [diff] [blame] | 84 | SourceLocation TemporaryLocation) |
David Blaikie | d7057d9 | 2015-08-12 23:49:57 +0000 | [diff] [blame] | 85 | : CGF(&CGF) { |
Adrian Prantl | 95b24e9 | 2015-02-03 20:00:54 +0000 | [diff] [blame] | 86 | init(TemporaryLocation, DefaultToEmpty); |
Adrian Prantl | 39428e7 | 2015-02-03 18:40:42 +0000 | [diff] [blame] | 87 | } |
| 88 | |
| 89 | void ApplyDebugLocation::init(SourceLocation TemporaryLocation, |
Adrian Prantl | 95b24e9 | 2015-02-03 20:00:54 +0000 | [diff] [blame] | 90 | bool DefaultToEmpty) { |
David Blaikie | d7057d9 | 2015-08-12 23:49:57 +0000 | [diff] [blame] | 91 | auto *DI = CGF->getDebugInfo(); |
| 92 | if (!DI) { |
| 93 | CGF = nullptr; |
| 94 | return; |
David Blaikie | 66e4197 | 2015-01-14 07:38:27 +0000 | [diff] [blame] | 95 | } |
David Blaikie | d7057d9 | 2015-08-12 23:49:57 +0000 | [diff] [blame] | 96 | |
| 97 | OriginalLocation = CGF->Builder.getCurrentDebugLocation(); |
| 98 | if (TemporaryLocation.isValid()) { |
| 99 | DI->EmitLocation(CGF->Builder, TemporaryLocation); |
| 100 | return; |
| 101 | } |
| 102 | |
| 103 | if (DefaultToEmpty) { |
| 104 | CGF->Builder.SetCurrentDebugLocation(llvm::DebugLoc()); |
| 105 | return; |
| 106 | } |
| 107 | |
| 108 | // Construct a location that has a valid scope, but no line info. |
| 109 | assert(!DI->LexicalBlockStack.empty()); |
| 110 | CGF->Builder.SetCurrentDebugLocation( |
| 111 | llvm::DebugLoc::get(0, 0, DI->LexicalBlockStack.back())); |
Adrian Prantl | 2e0637f | 2013-07-18 00:28:02 +0000 | [diff] [blame] | 112 | } |
| 113 | |
David Blaikie | 9b47966 | 2015-01-25 01:19:10 +0000 | [diff] [blame] | 114 | ApplyDebugLocation::ApplyDebugLocation(CodeGenFunction &CGF, const Expr *E) |
David Blaikie | d7057d9 | 2015-08-12 23:49:57 +0000 | [diff] [blame] | 115 | : CGF(&CGF) { |
David Blaikie | 9b47966 | 2015-01-25 01:19:10 +0000 | [diff] [blame] | 116 | init(E->getExprLoc()); |
| 117 | } |
| 118 | |
David Blaikie | 66e4197 | 2015-01-14 07:38:27 +0000 | [diff] [blame] | 119 | ApplyDebugLocation::ApplyDebugLocation(CodeGenFunction &CGF, llvm::DebugLoc Loc) |
David Blaikie | d7057d9 | 2015-08-12 23:49:57 +0000 | [diff] [blame] | 120 | : CGF(&CGF) { |
| 121 | if (!CGF.getDebugInfo()) { |
| 122 | this->CGF = nullptr; |
| 123 | return; |
David Blaikie | 66e4197 | 2015-01-14 07:38:27 +0000 | [diff] [blame] | 124 | } |
David Blaikie | d7057d9 | 2015-08-12 23:49:57 +0000 | [diff] [blame] | 125 | OriginalLocation = CGF.Builder.getCurrentDebugLocation(); |
| 126 | if (Loc) |
| 127 | CGF.Builder.SetCurrentDebugLocation(std::move(Loc)); |
David Blaikie | 66e4197 | 2015-01-14 07:38:27 +0000 | [diff] [blame] | 128 | } |
| 129 | |
| 130 | ApplyDebugLocation::~ApplyDebugLocation() { |
| 131 | // Query CGF so the location isn't overwritten when location updates are |
| 132 | // temporarily disabled (for C++ default function arguments) |
David Blaikie | d7057d9 | 2015-08-12 23:49:57 +0000 | [diff] [blame] | 133 | if (CGF) |
| 134 | CGF->Builder.SetCurrentDebugLocation(std::move(OriginalLocation)); |
David Blaikie | 66e4197 | 2015-01-14 07:38:27 +0000 | [diff] [blame] | 135 | } |
| 136 | |
Guy Benyei | 11169dd | 2012-12-18 14:30:41 +0000 | [diff] [blame] | 137 | void CGDebugInfo::setLocation(SourceLocation Loc) { |
| 138 | // If the new location isn't valid return. |
Eric Christopher | e7b87e5 | 2014-10-26 23:40:33 +0000 | [diff] [blame] | 139 | if (Loc.isInvalid()) |
| 140 | return; |
Guy Benyei | 11169dd | 2012-12-18 14:30:41 +0000 | [diff] [blame] | 141 | |
| 142 | CurLoc = CGM.getContext().getSourceManager().getExpansionLoc(Loc); |
| 143 | |
| 144 | // If we've changed files in the middle of a lexical scope go ahead |
| 145 | // and create a new lexical scope with file node if it's different |
| 146 | // from the one in the scope. |
Eric Christopher | e7b87e5 | 2014-10-26 23:40:33 +0000 | [diff] [blame] | 147 | if (LexicalBlockStack.empty()) |
| 148 | return; |
Guy Benyei | 11169dd | 2012-12-18 14:30:41 +0000 | [diff] [blame] | 149 | |
| 150 | SourceManager &SM = CGM.getContext().getSourceManager(); |
Duncan P. N. Exon Smith | 9dd4e4e | 2015-04-29 16:40:08 +0000 | [diff] [blame] | 151 | auto *Scope = cast<llvm::DIScope>(LexicalBlockStack.back()); |
Guy Benyei | 11169dd | 2012-12-18 14:30:41 +0000 | [diff] [blame] | 152 | PresumedLoc PCLoc = SM.getPresumedLoc(CurLoc); |
Guy Benyei | 11169dd | 2012-12-18 14:30:41 +0000 | [diff] [blame] | 153 | |
Duncan P. N. Exon Smith | 373ee85 | 2015-04-16 01:36:36 +0000 | [diff] [blame] | 154 | if (PCLoc.isInvalid() || Scope->getFilename() == PCLoc.getFilename()) |
Guy Benyei | 11169dd | 2012-12-18 14:30:41 +0000 | [diff] [blame] | 155 | return; |
| 156 | |
Duncan P. N. Exon Smith | 9dd4e4e | 2015-04-29 16:40:08 +0000 | [diff] [blame] | 157 | if (auto *LBF = dyn_cast<llvm::DILexicalBlockFile>(Scope)) { |
Guy Benyei | 11169dd | 2012-12-18 14:30:41 +0000 | [diff] [blame] | 158 | LexicalBlockStack.pop_back(); |
Duncan P. N. Exon Smith | d899f6e | 2015-04-18 00:07:30 +0000 | [diff] [blame] | 159 | LexicalBlockStack.emplace_back(DBuilder.createLexicalBlockFile( |
| 160 | LBF->getScope(), getOrCreateFile(CurLoc))); |
Duncan P. N. Exon Smith | 9dd4e4e | 2015-04-29 16:40:08 +0000 | [diff] [blame] | 161 | } else if (isa<llvm::DILexicalBlock>(Scope) || |
| 162 | isa<llvm::DISubprogram>(Scope)) { |
Guy Benyei | 11169dd | 2012-12-18 14:30:41 +0000 | [diff] [blame] | 163 | LexicalBlockStack.pop_back(); |
Duncan P. N. Exon Smith | d899f6e | 2015-04-18 00:07:30 +0000 | [diff] [blame] | 164 | LexicalBlockStack.emplace_back( |
| 165 | DBuilder.createLexicalBlockFile(Scope, getOrCreateFile(CurLoc))); |
Guy Benyei | 11169dd | 2012-12-18 14:30:41 +0000 | [diff] [blame] | 166 | } |
| 167 | } |
| 168 | |
Adrian Prantl | 6ec370a | 2015-09-10 18:39:45 +0000 | [diff] [blame] | 169 | llvm::DIScope *CGDebugInfo::getDeclContextDescriptor(const Decl *D) { |
Adrian Prantl | 5c8bd88 | 2015-09-11 17:23:08 +0000 | [diff] [blame] | 170 | llvm::DIScope *Mod = getParentModuleOrNull(D); |
| 171 | return getContextDescriptor(cast<Decl>(D->getDeclContext()), |
| 172 | Mod ? Mod : TheCU); |
Adrian Prantl | 6ec370a | 2015-09-10 18:39:45 +0000 | [diff] [blame] | 173 | } |
| 174 | |
| 175 | llvm::DIScope *CGDebugInfo::getContextDescriptor(const Decl *Context, |
| 176 | llvm::DIScope *Default) { |
Guy Benyei | 11169dd | 2012-12-18 14:30:41 +0000 | [diff] [blame] | 177 | if (!Context) |
Adrian Prantl | 6ec370a | 2015-09-10 18:39:45 +0000 | [diff] [blame] | 178 | return Default; |
Guy Benyei | 11169dd | 2012-12-18 14:30:41 +0000 | [diff] [blame] | 179 | |
Duncan P. N. Exon Smith | fb49491 | 2014-12-09 18:39:32 +0000 | [diff] [blame] | 180 | auto I = RegionMap.find(Context); |
Guy Benyei | 11169dd | 2012-12-18 14:30:41 +0000 | [diff] [blame] | 181 | if (I != RegionMap.end()) { |
Duncan P. N. Exon Smith | fb49491 | 2014-12-09 18:39:32 +0000 | [diff] [blame] | 182 | llvm::Metadata *V = I->second; |
Duncan P. N. Exon Smith | 9dd4e4e | 2015-04-29 16:40:08 +0000 | [diff] [blame] | 183 | return dyn_cast_or_null<llvm::DIScope>(V); |
Guy Benyei | 11169dd | 2012-12-18 14:30:41 +0000 | [diff] [blame] | 184 | } |
| 185 | |
| 186 | // Check namespace. |
David Majnemer | 58ed0f3 | 2016-07-17 00:39:12 +0000 | [diff] [blame] | 187 | if (const auto *NSDecl = dyn_cast<NamespaceDecl>(Context)) |
David Blaikie | bfa5274 | 2013-04-19 06:56:38 +0000 | [diff] [blame] | 188 | return getOrCreateNameSpace(NSDecl); |
Guy Benyei | 11169dd | 2012-12-18 14:30:41 +0000 | [diff] [blame] | 189 | |
David Majnemer | 58ed0f3 | 2016-07-17 00:39:12 +0000 | [diff] [blame] | 190 | if (const auto *RDecl = dyn_cast<RecordDecl>(Context)) |
David Blaikie | bfa5274 | 2013-04-19 06:56:38 +0000 | [diff] [blame] | 191 | if (!RDecl->isDependentType()) |
| 192 | return getOrCreateType(CGM.getContext().getTypeDeclType(RDecl), |
Eric Christopher | e7b87e5 | 2014-10-26 23:40:33 +0000 | [diff] [blame] | 193 | getOrCreateMainFile()); |
Adrian Prantl | 6ec370a | 2015-09-10 18:39:45 +0000 | [diff] [blame] | 194 | return Default; |
Guy Benyei | 11169dd | 2012-12-18 14:30:41 +0000 | [diff] [blame] | 195 | } |
| 196 | |
Guy Benyei | 11169dd | 2012-12-18 14:30:41 +0000 | [diff] [blame] | 197 | StringRef CGDebugInfo::getFunctionName(const FunctionDecl *FD) { |
Eric Christopher | e7b87e5 | 2014-10-26 23:40:33 +0000 | [diff] [blame] | 198 | assert(FD && "Invalid FunctionDecl!"); |
Guy Benyei | 11169dd | 2012-12-18 14:30:41 +0000 | [diff] [blame] | 199 | IdentifierInfo *FII = FD->getIdentifier(); |
Eric Christopher | e7b87e5 | 2014-10-26 23:40:33 +0000 | [diff] [blame] | 200 | FunctionTemplateSpecializationInfo *Info = |
| 201 | FD->getTemplateSpecializationInfo(); |
Reid Kleckner | 6010338 | 2015-12-16 02:04:40 +0000 | [diff] [blame] | 202 | |
Reid Kleckner | 31abd80 | 2016-06-30 17:41:31 +0000 | [diff] [blame] | 203 | // Emit the unqualified name in normal operation. LLVM and the debugger can |
| 204 | // compute the fully qualified name from the scope chain. If we're only |
| 205 | // emitting line table info, there won't be any scope chains, so emit the |
| 206 | // fully qualified name here so that stack traces are more accurate. |
| 207 | // FIXME: Do this when emitting DWARF as well as when emitting CodeView after |
| 208 | // evaluating the size impact. |
| 209 | bool UseQualifiedName = DebugKind == codegenoptions::DebugLineTablesOnly && |
| 210 | CGM.getCodeGenOpts().EmitCodeView; |
| 211 | |
| 212 | if (!Info && FII && !UseQualifiedName) |
Guy Benyei | 11169dd | 2012-12-18 14:30:41 +0000 | [diff] [blame] | 213 | return FII->getName(); |
| 214 | |
Benjamin Kramer | 9170e91 | 2013-02-22 15:46:01 +0000 | [diff] [blame] | 215 | SmallString<128> NS; |
| 216 | llvm::raw_svector_ostream OS(NS); |
Reid Kleckner | 6010338 | 2015-12-16 02:04:40 +0000 | [diff] [blame] | 217 | PrintingPolicy Policy(CGM.getLangOpts()); |
Reid Kleckner | 829398e | 2016-06-17 16:11:20 +0000 | [diff] [blame] | 218 | Policy.MSVCFormatting = CGM.getCodeGenOpts().EmitCodeView; |
Reid Kleckner | 31abd80 | 2016-06-30 17:41:31 +0000 | [diff] [blame] | 219 | if (!UseQualifiedName) |
| 220 | FD->printName(OS); |
| 221 | else |
| 222 | FD->printQualifiedName(OS, Policy); |
Guy Benyei | 11169dd | 2012-12-18 14:30:41 +0000 | [diff] [blame] | 223 | |
Reid Kleckner | 829398e | 2016-06-17 16:11:20 +0000 | [diff] [blame] | 224 | // Add any template specialization args. |
| 225 | if (Info) { |
| 226 | const TemplateArgumentList *TArgs = Info->TemplateArguments; |
David Majnemer | 6fbeee3 | 2016-07-07 04:43:07 +0000 | [diff] [blame] | 227 | TemplateSpecializationType::PrintTemplateArgumentList(OS, TArgs->asArray(), |
Reid Kleckner | 829398e | 2016-06-17 16:11:20 +0000 | [diff] [blame] | 228 | Policy); |
Guy Benyei | 11169dd | 2012-12-18 14:30:41 +0000 | [diff] [blame] | 229 | } |
| 230 | |
| 231 | // Copy this name on the side and use its reference. |
Benjamin Kramer | 1b18a5e | 2013-09-09 16:39:06 +0000 | [diff] [blame] | 232 | return internString(OS.str()); |
Guy Benyei | 11169dd | 2012-12-18 14:30:41 +0000 | [diff] [blame] | 233 | } |
| 234 | |
| 235 | StringRef CGDebugInfo::getObjCMethodName(const ObjCMethodDecl *OMD) { |
| 236 | SmallString<256> MethodName; |
| 237 | llvm::raw_svector_ostream OS(MethodName); |
| 238 | OS << (OMD->isInstanceMethod() ? '-' : '+') << '['; |
| 239 | const DeclContext *DC = OMD->getDeclContext(); |
David Majnemer | 58ed0f3 | 2016-07-17 00:39:12 +0000 | [diff] [blame] | 240 | if (const auto *OID = dyn_cast<ObjCImplementationDecl>(DC)) { |
Eric Christopher | e7b87e5 | 2014-10-26 23:40:33 +0000 | [diff] [blame] | 241 | OS << OID->getName(); |
David Majnemer | 58ed0f3 | 2016-07-17 00:39:12 +0000 | [diff] [blame] | 242 | } else if (const auto *OID = dyn_cast<ObjCInterfaceDecl>(DC)) { |
Eric Christopher | e7b87e5 | 2014-10-26 23:40:33 +0000 | [diff] [blame] | 243 | OS << OID->getName(); |
David Majnemer | 58ed0f3 | 2016-07-17 00:39:12 +0000 | [diff] [blame] | 244 | } else if (const auto *OC = dyn_cast<ObjCCategoryDecl>(DC)) { |
Douglas Gregor | acf4fd3 | 2015-11-03 01:15:46 +0000 | [diff] [blame] | 245 | if (OC->IsClassExtension()) { |
| 246 | OS << OC->getClassInterface()->getName(); |
| 247 | } else { |
David Majnemer | 58ed0f3 | 2016-07-17 00:39:12 +0000 | [diff] [blame] | 248 | OS << OC->getIdentifier()->getNameStart() << '(' |
Douglas Gregor | acf4fd3 | 2015-11-03 01:15:46 +0000 | [diff] [blame] | 249 | << OC->getIdentifier()->getNameStart() << ')'; |
| 250 | } |
David Majnemer | 58ed0f3 | 2016-07-17 00:39:12 +0000 | [diff] [blame] | 251 | } else if (const auto *OCD = dyn_cast<ObjCCategoryImplDecl>(DC)) { |
Eric Christopher | e7b87e5 | 2014-10-26 23:40:33 +0000 | [diff] [blame] | 252 | OS << ((const NamedDecl *)OCD)->getIdentifier()->getNameStart() << '(' |
| 253 | << OCD->getIdentifier()->getNameStart() << ')'; |
Adrian Prantl | b39fc14 | 2013-05-17 23:58:45 +0000 | [diff] [blame] | 254 | } else if (isa<ObjCProtocolDecl>(DC)) { |
Adrian Prantl | 6e785ec | 2013-05-17 23:49:10 +0000 | [diff] [blame] | 255 | // We can extract the type of the class from the self pointer. |
Eric Christopher | e7b87e5 | 2014-10-26 23:40:33 +0000 | [diff] [blame] | 256 | if (ImplicitParamDecl *SelfDecl = OMD->getSelfDecl()) { |
Adrian Prantl | 6e785ec | 2013-05-17 23:49:10 +0000 | [diff] [blame] | 257 | QualType ClassTy = |
Eric Christopher | e7b87e5 | 2014-10-26 23:40:33 +0000 | [diff] [blame] | 258 | cast<ObjCObjectPointerType>(SelfDecl->getType())->getPointeeType(); |
Adrian Prantl | 6e785ec | 2013-05-17 23:49:10 +0000 | [diff] [blame] | 259 | ClassTy.print(OS, PrintingPolicy(LangOptions())); |
| 260 | } |
Guy Benyei | 11169dd | 2012-12-18 14:30:41 +0000 | [diff] [blame] | 261 | } |
| 262 | OS << ' ' << OMD->getSelector().getAsString() << ']'; |
| 263 | |
Benjamin Kramer | 1b18a5e | 2013-09-09 16:39:06 +0000 | [diff] [blame] | 264 | return internString(OS.str()); |
Guy Benyei | 11169dd | 2012-12-18 14:30:41 +0000 | [diff] [blame] | 265 | } |
| 266 | |
Guy Benyei | 11169dd | 2012-12-18 14:30:41 +0000 | [diff] [blame] | 267 | StringRef CGDebugInfo::getSelectorName(Selector S) { |
Benjamin Kramer | 1b18a5e | 2013-09-09 16:39:06 +0000 | [diff] [blame] | 268 | return internString(S.getAsString()); |
Guy Benyei | 11169dd | 2012-12-18 14:30:41 +0000 | [diff] [blame] | 269 | } |
| 270 | |
Eric Christopher | e7b87e5 | 2014-10-26 23:40:33 +0000 | [diff] [blame] | 271 | StringRef CGDebugInfo::getClassName(const RecordDecl *RD) { |
David Majnemer | bc5976a | 2016-07-01 23:12:54 +0000 | [diff] [blame] | 272 | if (isa<ClassTemplateSpecializationDecl>(RD)) { |
| 273 | SmallString<128> Name; |
David Blaikie | 65813a3 | 2014-04-02 18:21:09 +0000 | [diff] [blame] | 274 | llvm::raw_svector_ostream OS(Name); |
| 275 | RD->getNameForDiagnostic(OS, CGM.getContext().getPrintingPolicy(), |
| 276 | /*Qualified*/ false); |
David Majnemer | bc5976a | 2016-07-01 23:12:54 +0000 | [diff] [blame] | 277 | |
| 278 | // Copy this name on the side and use its reference. |
| 279 | return internString(Name); |
Benjamin Kramer | 9170e91 | 2013-02-22 15:46:01 +0000 | [diff] [blame] | 280 | } |
Guy Benyei | 11169dd | 2012-12-18 14:30:41 +0000 | [diff] [blame] | 281 | |
David Majnemer | bc5976a | 2016-07-01 23:12:54 +0000 | [diff] [blame] | 282 | // quick optimization to avoid having to intern strings that are already |
| 283 | // stored reliably elsewhere |
| 284 | if (const IdentifierInfo *II = RD->getIdentifier()) |
| 285 | return II->getName(); |
| 286 | |
| 287 | // The CodeView printer in LLVM wants to see the names of unnamed types: it is |
| 288 | // used to reconstruct the fully qualified type names. |
| 289 | if (CGM.getCodeGenOpts().EmitCodeView) { |
| 290 | if (const TypedefNameDecl *D = RD->getTypedefNameForAnonDecl()) { |
| 291 | assert(RD->getDeclContext() == D->getDeclContext() && |
| 292 | "Typedef should not be in another decl context!"); |
| 293 | assert(D->getDeclName().getAsIdentifierInfo() && |
| 294 | "Typedef was not named!"); |
| 295 | return D->getDeclName().getAsIdentifierInfo()->getName(); |
| 296 | } |
| 297 | |
| 298 | if (CGM.getLangOpts().CPlusPlus) { |
| 299 | StringRef Name; |
| 300 | |
| 301 | ASTContext &Context = CGM.getContext(); |
| 302 | if (const DeclaratorDecl *DD = Context.getDeclaratorForUnnamedTagDecl(RD)) |
| 303 | // Anonymous types without a name for linkage purposes have their |
| 304 | // declarator mangled in if they have one. |
| 305 | Name = DD->getName(); |
| 306 | else if (const TypedefNameDecl *TND = |
| 307 | Context.getTypedefNameForUnnamedTagDecl(RD)) |
| 308 | // Anonymous types without a name for linkage purposes have their |
| 309 | // associate typedef mangled in if they have one. |
| 310 | Name = TND->getName(); |
| 311 | |
| 312 | if (!Name.empty()) { |
| 313 | SmallString<256> UnnamedType("<unnamed-type-"); |
| 314 | UnnamedType += Name; |
| 315 | UnnamedType += '>'; |
| 316 | return internString(UnnamedType); |
| 317 | } |
| 318 | } |
| 319 | } |
| 320 | |
| 321 | return StringRef(); |
Guy Benyei | 11169dd | 2012-12-18 14:30:41 +0000 | [diff] [blame] | 322 | } |
| 323 | |
Amjad Aboud | e2aab8c | 2016-12-25 10:12:27 +0000 | [diff] [blame] | 324 | llvm::DIFile::ChecksumKind |
| 325 | CGDebugInfo::computeChecksum(FileID FID, SmallString<32> &Checksum) const { |
| 326 | Checksum.clear(); |
| 327 | |
| 328 | if (!CGM.getCodeGenOpts().EmitCodeView) |
| 329 | return llvm::DIFile::CSK_None; |
| 330 | |
| 331 | SourceManager &SM = CGM.getContext().getSourceManager(); |
| 332 | bool Invalid; |
| 333 | llvm::MemoryBuffer *MemBuffer = SM.getBuffer(FID, &Invalid); |
| 334 | if (Invalid) |
| 335 | return llvm::DIFile::CSK_None; |
| 336 | |
| 337 | llvm::MD5 Hash; |
| 338 | llvm::MD5::MD5Result Result; |
| 339 | |
| 340 | Hash.update(MemBuffer->getBuffer()); |
| 341 | Hash.final(Result); |
| 342 | |
| 343 | Hash.stringifyResult(Result, Checksum); |
| 344 | return llvm::DIFile::CSK_MD5; |
| 345 | } |
| 346 | |
Duncan P. N. Exon Smith | 9dd4e4e | 2015-04-29 16:40:08 +0000 | [diff] [blame] | 347 | llvm::DIFile *CGDebugInfo::getOrCreateFile(SourceLocation Loc) { |
Guy Benyei | 11169dd | 2012-12-18 14:30:41 +0000 | [diff] [blame] | 348 | if (!Loc.isValid()) |
| 349 | // If Location is not valid then use main input file. |
Saleem Abdulrasool | 436256a | 2015-10-12 20:21:08 +0000 | [diff] [blame] | 350 | return DBuilder.createFile(remapDIPath(TheCU->getFilename()), |
Amjad Aboud | e2aab8c | 2016-12-25 10:12:27 +0000 | [diff] [blame] | 351 | remapDIPath(TheCU->getDirectory()), |
| 352 | TheCU->getFile()->getChecksumKind(), |
| 353 | TheCU->getFile()->getChecksum()); |
Guy Benyei | 11169dd | 2012-12-18 14:30:41 +0000 | [diff] [blame] | 354 | |
| 355 | SourceManager &SM = CGM.getContext().getSourceManager(); |
| 356 | PresumedLoc PLoc = SM.getPresumedLoc(Loc); |
| 357 | |
| 358 | if (PLoc.isInvalid() || StringRef(PLoc.getFilename()).empty()) |
| 359 | // If the location is not valid then use main input file. |
Saleem Abdulrasool | 436256a | 2015-10-12 20:21:08 +0000 | [diff] [blame] | 360 | return DBuilder.createFile(remapDIPath(TheCU->getFilename()), |
Amjad Aboud | e2aab8c | 2016-12-25 10:12:27 +0000 | [diff] [blame] | 361 | remapDIPath(TheCU->getDirectory()), |
| 362 | TheCU->getFile()->getChecksumKind(), |
| 363 | TheCU->getFile()->getChecksum()); |
Guy Benyei | 11169dd | 2012-12-18 14:30:41 +0000 | [diff] [blame] | 364 | |
| 365 | // Cache the results. |
| 366 | const char *fname = PLoc.getFilename(); |
Duncan P. N. Exon Smith | fb49491 | 2014-12-09 18:39:32 +0000 | [diff] [blame] | 367 | auto it = DIFileCache.find(fname); |
Guy Benyei | 11169dd | 2012-12-18 14:30:41 +0000 | [diff] [blame] | 368 | |
| 369 | if (it != DIFileCache.end()) { |
| 370 | // Verify that the information still exists. |
Duncan P. N. Exon Smith | fb49491 | 2014-12-09 18:39:32 +0000 | [diff] [blame] | 371 | if (llvm::Metadata *V = it->second) |
Duncan P. N. Exon Smith | 9dd4e4e | 2015-04-29 16:40:08 +0000 | [diff] [blame] | 372 | return cast<llvm::DIFile>(V); |
Guy Benyei | 11169dd | 2012-12-18 14:30:41 +0000 | [diff] [blame] | 373 | } |
| 374 | |
Amjad Aboud | e2aab8c | 2016-12-25 10:12:27 +0000 | [diff] [blame] | 375 | SmallString<32> Checksum; |
| 376 | llvm::DIFile::ChecksumKind CSKind = |
| 377 | computeChecksum(SM.getFileID(Loc), Checksum); |
| 378 | |
Saleem Abdulrasool | 436256a | 2015-10-12 20:21:08 +0000 | [diff] [blame] | 379 | llvm::DIFile *F = DBuilder.createFile(remapDIPath(PLoc.getFilename()), |
Amjad Aboud | e2aab8c | 2016-12-25 10:12:27 +0000 | [diff] [blame] | 380 | remapDIPath(getCurrentDirname()), |
| 381 | CSKind, Checksum); |
Guy Benyei | 11169dd | 2012-12-18 14:30:41 +0000 | [diff] [blame] | 382 | |
Duncan P. N. Exon Smith | fb49491 | 2014-12-09 18:39:32 +0000 | [diff] [blame] | 383 | DIFileCache[fname].reset(F); |
Guy Benyei | 11169dd | 2012-12-18 14:30:41 +0000 | [diff] [blame] | 384 | return F; |
| 385 | } |
| 386 | |
Duncan P. N. Exon Smith | 9dd4e4e | 2015-04-29 16:40:08 +0000 | [diff] [blame] | 387 | llvm::DIFile *CGDebugInfo::getOrCreateMainFile() { |
Saleem Abdulrasool | 436256a | 2015-10-12 20:21:08 +0000 | [diff] [blame] | 388 | return DBuilder.createFile(remapDIPath(TheCU->getFilename()), |
Amjad Aboud | e2aab8c | 2016-12-25 10:12:27 +0000 | [diff] [blame] | 389 | remapDIPath(TheCU->getDirectory()), |
| 390 | TheCU->getFile()->getChecksumKind(), |
| 391 | TheCU->getFile()->getChecksum()); |
Saleem Abdulrasool | 436256a | 2015-10-12 20:21:08 +0000 | [diff] [blame] | 392 | } |
| 393 | |
| 394 | std::string CGDebugInfo::remapDIPath(StringRef Path) const { |
| 395 | for (const auto &Entry : DebugPrefixMap) |
| 396 | if (Path.startswith(Entry.first)) |
| 397 | return (Twine(Entry.second) + Path.substr(Entry.first.size())).str(); |
| 398 | return Path.str(); |
Guy Benyei | 11169dd | 2012-12-18 14:30:41 +0000 | [diff] [blame] | 399 | } |
| 400 | |
Guy Benyei | 11169dd | 2012-12-18 14:30:41 +0000 | [diff] [blame] | 401 | unsigned CGDebugInfo::getLineNumber(SourceLocation Loc) { |
| 402 | if (Loc.isInvalid() && CurLoc.isInvalid()) |
| 403 | return 0; |
| 404 | SourceManager &SM = CGM.getContext().getSourceManager(); |
| 405 | PresumedLoc PLoc = SM.getPresumedLoc(Loc.isValid() ? Loc : CurLoc); |
Eric Christopher | e7b87e5 | 2014-10-26 23:40:33 +0000 | [diff] [blame] | 406 | return PLoc.isValid() ? PLoc.getLine() : 0; |
Guy Benyei | 11169dd | 2012-12-18 14:30:41 +0000 | [diff] [blame] | 407 | } |
| 408 | |
Adrian Prantl | c782242 | 2013-03-12 20:43:25 +0000 | [diff] [blame] | 409 | unsigned CGDebugInfo::getColumnNumber(SourceLocation Loc, bool Force) { |
Guy Benyei | 11169dd | 2012-12-18 14:30:41 +0000 | [diff] [blame] | 410 | // We may not want column information at all. |
Adrian Prantl | c782242 | 2013-03-12 20:43:25 +0000 | [diff] [blame] | 411 | if (!Force && !CGM.getCodeGenOpts().DebugColumnInfo) |
Guy Benyei | 11169dd | 2012-12-18 14:30:41 +0000 | [diff] [blame] | 412 | return 0; |
| 413 | |
| 414 | // If the location is invalid then use the current column. |
| 415 | if (Loc.isInvalid() && CurLoc.isInvalid()) |
| 416 | return 0; |
| 417 | SourceManager &SM = CGM.getContext().getSourceManager(); |
| 418 | PresumedLoc PLoc = SM.getPresumedLoc(Loc.isValid() ? Loc : CurLoc); |
Eric Christopher | e7b87e5 | 2014-10-26 23:40:33 +0000 | [diff] [blame] | 419 | return PLoc.isValid() ? PLoc.getColumn() : 0; |
Guy Benyei | 11169dd | 2012-12-18 14:30:41 +0000 | [diff] [blame] | 420 | } |
| 421 | |
| 422 | StringRef CGDebugInfo::getCurrentDirname() { |
| 423 | if (!CGM.getCodeGenOpts().DebugCompilationDir.empty()) |
| 424 | return CGM.getCodeGenOpts().DebugCompilationDir; |
| 425 | |
| 426 | if (!CWDName.empty()) |
| 427 | return CWDName; |
| 428 | SmallString<256> CWD; |
| 429 | llvm::sys::fs::current_path(CWD); |
Benjamin Kramer | 1b18a5e | 2013-09-09 16:39:06 +0000 | [diff] [blame] | 430 | return CWDName = internString(CWD); |
Guy Benyei | 11169dd | 2012-12-18 14:30:41 +0000 | [diff] [blame] | 431 | } |
| 432 | |
Guy Benyei | 11169dd | 2012-12-18 14:30:41 +0000 | [diff] [blame] | 433 | void CGDebugInfo::CreateCompileUnit() { |
Amjad Aboud | e2aab8c | 2016-12-25 10:12:27 +0000 | [diff] [blame] | 434 | SmallString<32> Checksum; |
| 435 | llvm::DIFile::ChecksumKind CSKind = llvm::DIFile::CSK_None; |
Guy Benyei | 11169dd | 2012-12-18 14:30:41 +0000 | [diff] [blame] | 436 | |
David Blaikie | aabde05 | 2014-05-14 00:29:00 +0000 | [diff] [blame] | 437 | // Should we be asking the SourceManager for the main file name, instead of |
| 438 | // accepting it as an argument? This just causes the main file name to |
| 439 | // mismatch with source locations and create extra lexical scopes or |
| 440 | // mismatched debug info (a CU with a DW_AT_file of "-", because that's what |
| 441 | // the driver passed, but functions/other things have DW_AT_file of "<stdin>" |
| 442 | // because that's what the SourceManager says) |
| 443 | |
Guy Benyei | 11169dd | 2012-12-18 14:30:41 +0000 | [diff] [blame] | 444 | // Get absolute path name. |
| 445 | SourceManager &SM = CGM.getContext().getSourceManager(); |
| 446 | std::string MainFileName = CGM.getCodeGenOpts().MainFileName; |
| 447 | if (MainFileName.empty()) |
David Blaikie | aabde05 | 2014-05-14 00:29:00 +0000 | [diff] [blame] | 448 | MainFileName = "<stdin>"; |
Guy Benyei | 11169dd | 2012-12-18 14:30:41 +0000 | [diff] [blame] | 449 | |
| 450 | // The main file name provided via the "-main-file-name" option contains just |
| 451 | // the file name itself with no path information. This file name may have had |
| 452 | // a relative path, so we look into the actual file entry for the main |
| 453 | // file to determine the real absolute path for the file. |
| 454 | std::string MainFileDir; |
| 455 | if (const FileEntry *MainFile = SM.getFileEntryForID(SM.getMainFileID())) { |
Saleem Abdulrasool | 436256a | 2015-10-12 20:21:08 +0000 | [diff] [blame] | 456 | MainFileDir = remapDIPath(MainFile->getDir()->getName()); |
Yaron Keren | 9fb7e90 | 2013-10-21 20:07:37 +0000 | [diff] [blame] | 457 | if (MainFileDir != ".") { |
Eric Christopher | 0a1301f | 2014-02-26 02:49:36 +0000 | [diff] [blame] | 458 | llvm::SmallString<1024> MainFileDirSS(MainFileDir); |
| 459 | llvm::sys::path::append(MainFileDirSS, MainFileName); |
| 460 | MainFileName = MainFileDirSS.str(); |
Yaron Keren | 9fb7e90 | 2013-10-21 20:07:37 +0000 | [diff] [blame] | 461 | } |
Amjad Aboud | e2aab8c | 2016-12-25 10:12:27 +0000 | [diff] [blame] | 462 | CSKind = computeChecksum(SM.getMainFileID(), Checksum); |
Guy Benyei | 11169dd | 2012-12-18 14:30:41 +0000 | [diff] [blame] | 463 | } |
| 464 | |
Ed Maste | da70602 | 2014-05-07 12:49:30 +0000 | [diff] [blame] | 465 | llvm::dwarf::SourceLanguage LangTag; |
Guy Benyei | 11169dd | 2012-12-18 14:30:41 +0000 | [diff] [blame] | 466 | const LangOptions &LO = CGM.getLangOpts(); |
| 467 | if (LO.CPlusPlus) { |
| 468 | if (LO.ObjC1) |
| 469 | LangTag = llvm::dwarf::DW_LANG_ObjC_plus_plus; |
| 470 | else |
| 471 | LangTag = llvm::dwarf::DW_LANG_C_plus_plus; |
| 472 | } else if (LO.ObjC1) { |
| 473 | LangTag = llvm::dwarf::DW_LANG_ObjC; |
Pirama Arumuga Nainar | a7484c9 | 2016-06-21 21:35:11 +0000 | [diff] [blame] | 474 | } else if (LO.RenderScript) { |
| 475 | LangTag = llvm::dwarf::DW_LANG_GOOGLE_RenderScript; |
Guy Benyei | 11169dd | 2012-12-18 14:30:41 +0000 | [diff] [blame] | 476 | } else if (LO.C99) { |
| 477 | LangTag = llvm::dwarf::DW_LANG_C99; |
| 478 | } else { |
| 479 | LangTag = llvm::dwarf::DW_LANG_C89; |
| 480 | } |
| 481 | |
| 482 | std::string Producer = getClangFullVersion(); |
| 483 | |
| 484 | // Figure out which version of the ObjC runtime we have. |
| 485 | unsigned RuntimeVers = 0; |
| 486 | if (LO.ObjC1) |
| 487 | RuntimeVers = LO.ObjCRuntime.isNonFragile() ? 2 : 1; |
| 488 | |
Adrian Prantl | 826824e | 2016-04-08 22:43:06 +0000 | [diff] [blame] | 489 | llvm::DICompileUnit::DebugEmissionKind EmissionKind; |
| 490 | switch (DebugKind) { |
| 491 | case codegenoptions::NoDebugInfo: |
| 492 | case codegenoptions::LocTrackingOnly: |
| 493 | EmissionKind = llvm::DICompileUnit::NoDebug; |
| 494 | break; |
| 495 | case codegenoptions::DebugLineTablesOnly: |
| 496 | EmissionKind = llvm::DICompileUnit::LineTablesOnly; |
| 497 | break; |
| 498 | case codegenoptions::LimitedDebugInfo: |
| 499 | case codegenoptions::FullDebugInfo: |
| 500 | EmissionKind = llvm::DICompileUnit::FullDebug; |
| 501 | break; |
| 502 | } |
| 503 | |
Guy Benyei | 11169dd | 2012-12-18 14:30:41 +0000 | [diff] [blame] | 504 | // Create new compile unit. |
Guy Benyei | 11169dd | 2012-12-18 14:30:41 +0000 | [diff] [blame] | 505 | // FIXME - Eliminate TheCU. |
Eric Christopher | e4200a2 | 2014-02-27 01:25:08 +0000 | [diff] [blame] | 506 | TheCU = DBuilder.createCompileUnit( |
Amjad Aboud | fa9a17e | 2016-12-14 20:24:40 +0000 | [diff] [blame] | 507 | LangTag, DBuilder.createFile(remapDIPath(MainFileName), |
Amjad Aboud | e2aab8c | 2016-12-25 10:12:27 +0000 | [diff] [blame] | 508 | remapDIPath(getCurrentDirname()), CSKind, |
| 509 | Checksum), |
Saleem Abdulrasool | 436256a | 2015-10-12 20:21:08 +0000 | [diff] [blame] | 510 | Producer, LO.Optimize, CGM.getCodeGenOpts().DwarfDebugFlags, RuntimeVers, |
David Blaikie | a45c31a | 2016-08-24 18:29:58 +0000 | [diff] [blame] | 511 | CGM.getCodeGenOpts().SplitDwarfFile, EmissionKind, 0 /* DWOid */, |
Dehao Chen | 5a3f890 | 2017-02-01 22:45:21 +0000 | [diff] [blame] | 512 | CGM.getCodeGenOpts().SplitDwarfInlining, |
| 513 | CGM.getCodeGenOpts().DebugInfoForProfiling); |
Guy Benyei | 11169dd | 2012-12-18 14:30:41 +0000 | [diff] [blame] | 514 | } |
| 515 | |
Duncan P. N. Exon Smith | 9dd4e4e | 2015-04-29 16:40:08 +0000 | [diff] [blame] | 516 | llvm::DIType *CGDebugInfo::CreateType(const BuiltinType *BT) { |
Ed Maste | da70602 | 2014-05-07 12:49:30 +0000 | [diff] [blame] | 517 | llvm::dwarf::TypeKind Encoding; |
Guy Benyei | 11169dd | 2012-12-18 14:30:41 +0000 | [diff] [blame] | 518 | StringRef BTName; |
| 519 | switch (BT->getKind()) { |
| 520 | #define BUILTIN_TYPE(Id, SingletonId) |
Eric Christopher | e7b87e5 | 2014-10-26 23:40:33 +0000 | [diff] [blame] | 521 | #define PLACEHOLDER_TYPE(Id, SingletonId) case BuiltinType::Id: |
Guy Benyei | 11169dd | 2012-12-18 14:30:41 +0000 | [diff] [blame] | 522 | #include "clang/AST/BuiltinTypes.def" |
| 523 | case BuiltinType::Dependent: |
| 524 | llvm_unreachable("Unexpected builtin type"); |
| 525 | case BuiltinType::NullPtr: |
Peter Collingbourne | 5c5e617 | 2013-06-27 22:51:01 +0000 | [diff] [blame] | 526 | return DBuilder.createNullPtrType(); |
Guy Benyei | 11169dd | 2012-12-18 14:30:41 +0000 | [diff] [blame] | 527 | case BuiltinType::Void: |
Duncan P. N. Exon Smith | 0b6c369 | 2015-04-20 18:51:48 +0000 | [diff] [blame] | 528 | return nullptr; |
Guy Benyei | 11169dd | 2012-12-18 14:30:41 +0000 | [diff] [blame] | 529 | case BuiltinType::ObjCClass: |
David Blaikie | f427b00 | 2014-05-06 03:42:01 +0000 | [diff] [blame] | 530 | if (!ClassTy) |
| 531 | ClassTy = DBuilder.createForwardDecl(llvm::dwarf::DW_TAG_structure_type, |
| 532 | "objc_class", TheCU, |
| 533 | getOrCreateMainFile(), 0); |
Guy Benyei | 11169dd | 2012-12-18 14:30:41 +0000 | [diff] [blame] | 534 | return ClassTy; |
| 535 | case BuiltinType::ObjCId: { |
| 536 | // typedef struct objc_class *Class; |
| 537 | // typedef struct objc_object { |
| 538 | // Class isa; |
| 539 | // } *id; |
| 540 | |
Eric Christopher | f8bc4d8 | 2013-07-18 00:52:50 +0000 | [diff] [blame] | 541 | if (ObjTy) |
Guy Benyei | 11169dd | 2012-12-18 14:30:41 +0000 | [diff] [blame] | 542 | return ObjTy; |
| 543 | |
Eric Christopher | f8bc4d8 | 2013-07-18 00:52:50 +0000 | [diff] [blame] | 544 | if (!ClassTy) |
Guy Benyei | 11169dd | 2012-12-18 14:30:41 +0000 | [diff] [blame] | 545 | ClassTy = DBuilder.createForwardDecl(llvm::dwarf::DW_TAG_structure_type, |
| 546 | "objc_class", TheCU, |
| 547 | getOrCreateMainFile(), 0); |
| 548 | |
| 549 | unsigned Size = CGM.getContext().getTypeSize(CGM.getContext().VoidPtrTy); |
Eric Christopher | b2a008c | 2013-05-16 00:45:12 +0000 | [diff] [blame] | 550 | |
Duncan P. N. Exon Smith | 0b6c369 | 2015-04-20 18:51:48 +0000 | [diff] [blame] | 551 | auto *ISATy = DBuilder.createPointerType(ClassTy, Size); |
Guy Benyei | 11169dd | 2012-12-18 14:30:41 +0000 | [diff] [blame] | 552 | |
Leny Kholodov | df050fd | 2016-09-06 17:06:14 +0000 | [diff] [blame] | 553 | ObjTy = DBuilder.createStructType( |
| 554 | TheCU, "objc_object", getOrCreateMainFile(), 0, 0, 0, |
| 555 | llvm::DINode::FlagZero, nullptr, llvm::DINodeArray()); |
Guy Benyei | 11169dd | 2012-12-18 14:30:41 +0000 | [diff] [blame] | 556 | |
Duncan P. N. Exon Smith | c8ee63e | 2014-12-18 00:48:56 +0000 | [diff] [blame] | 557 | DBuilder.replaceArrays( |
Leny Kholodov | df050fd | 2016-09-06 17:06:14 +0000 | [diff] [blame] | 558 | ObjTy, DBuilder.getOrCreateArray(&*DBuilder.createMemberType( |
| 559 | ObjTy, "isa", getOrCreateMainFile(), 0, Size, 0, 0, |
| 560 | llvm::DINode::FlagZero, ISATy))); |
Guy Benyei | 11169dd | 2012-12-18 14:30:41 +0000 | [diff] [blame] | 561 | return ObjTy; |
| 562 | } |
| 563 | case BuiltinType::ObjCSel: { |
David Blaikie | f427b00 | 2014-05-06 03:42:01 +0000 | [diff] [blame] | 564 | if (!SelTy) |
| 565 | SelTy = DBuilder.createForwardDecl(llvm::dwarf::DW_TAG_structure_type, |
| 566 | "objc_selector", TheCU, |
| 567 | getOrCreateMainFile(), 0); |
Guy Benyei | 11169dd | 2012-12-18 14:30:41 +0000 | [diff] [blame] | 568 | return SelTy; |
| 569 | } |
Guy Benyei | d8a08ea | 2012-12-18 14:38:23 +0000 | [diff] [blame] | 570 | |
Alexey Bader | 954ba21 | 2016-04-08 13:40:33 +0000 | [diff] [blame] | 571 | #define IMAGE_TYPE(ImgType, Id, SingletonId, Access, Suffix) \ |
| 572 | case BuiltinType::Id: \ |
| 573 | return getOrCreateStructPtrType("opencl_" #ImgType "_" #Suffix "_t", \ |
| 574 | SingletonId); |
Alexey Bader | b62f144 | 2016-04-13 08:33:41 +0000 | [diff] [blame] | 575 | #include "clang/Basic/OpenCLImageTypes.def" |
Guy Benyei | 6105419 | 2013-02-07 10:55:47 +0000 | [diff] [blame] | 576 | case BuiltinType::OCLSampler: |
Yaxun Liu | 0bc4b2d | 2016-07-28 19:26:30 +0000 | [diff] [blame] | 577 | return getOrCreateStructPtrType("opencl_sampler_t", |
| 578 | OCLSamplerDITy); |
Guy Benyei | 1b4fb3e | 2013-01-20 12:31:11 +0000 | [diff] [blame] | 579 | case BuiltinType::OCLEvent: |
Eric Christopher | e7b87e5 | 2014-10-26 23:40:33 +0000 | [diff] [blame] | 580 | return getOrCreateStructPtrType("opencl_event_t", OCLEventDITy); |
Alexey Bader | 9c8453f | 2015-09-15 11:18:52 +0000 | [diff] [blame] | 581 | case BuiltinType::OCLClkEvent: |
| 582 | return getOrCreateStructPtrType("opencl_clk_event_t", OCLClkEventDITy); |
| 583 | case BuiltinType::OCLQueue: |
| 584 | return getOrCreateStructPtrType("opencl_queue_t", OCLQueueDITy); |
Alexey Bader | 9c8453f | 2015-09-15 11:18:52 +0000 | [diff] [blame] | 585 | case BuiltinType::OCLReserveID: |
| 586 | return getOrCreateStructPtrType("opencl_reserve_id_t", OCLReserveIDDITy); |
Guy Benyei | d8a08ea | 2012-12-18 14:38:23 +0000 | [diff] [blame] | 587 | |
Guy Benyei | 11169dd | 2012-12-18 14:30:41 +0000 | [diff] [blame] | 588 | case BuiltinType::UChar: |
Eric Christopher | e7b87e5 | 2014-10-26 23:40:33 +0000 | [diff] [blame] | 589 | case BuiltinType::Char_U: |
| 590 | Encoding = llvm::dwarf::DW_ATE_unsigned_char; |
| 591 | break; |
Guy Benyei | 11169dd | 2012-12-18 14:30:41 +0000 | [diff] [blame] | 592 | case BuiltinType::Char_S: |
Eric Christopher | e7b87e5 | 2014-10-26 23:40:33 +0000 | [diff] [blame] | 593 | case BuiltinType::SChar: |
| 594 | Encoding = llvm::dwarf::DW_ATE_signed_char; |
| 595 | break; |
Guy Benyei | 11169dd | 2012-12-18 14:30:41 +0000 | [diff] [blame] | 596 | case BuiltinType::Char16: |
Eric Christopher | e7b87e5 | 2014-10-26 23:40:33 +0000 | [diff] [blame] | 597 | case BuiltinType::Char32: |
| 598 | Encoding = llvm::dwarf::DW_ATE_UTF; |
| 599 | break; |
Guy Benyei | 11169dd | 2012-12-18 14:30:41 +0000 | [diff] [blame] | 600 | case BuiltinType::UShort: |
| 601 | case BuiltinType::UInt: |
| 602 | case BuiltinType::UInt128: |
| 603 | case BuiltinType::ULong: |
| 604 | case BuiltinType::WChar_U: |
Eric Christopher | e7b87e5 | 2014-10-26 23:40:33 +0000 | [diff] [blame] | 605 | case BuiltinType::ULongLong: |
| 606 | Encoding = llvm::dwarf::DW_ATE_unsigned; |
| 607 | break; |
Guy Benyei | 11169dd | 2012-12-18 14:30:41 +0000 | [diff] [blame] | 608 | case BuiltinType::Short: |
| 609 | case BuiltinType::Int: |
| 610 | case BuiltinType::Int128: |
| 611 | case BuiltinType::Long: |
| 612 | case BuiltinType::WChar_S: |
Eric Christopher | e7b87e5 | 2014-10-26 23:40:33 +0000 | [diff] [blame] | 613 | case BuiltinType::LongLong: |
| 614 | Encoding = llvm::dwarf::DW_ATE_signed; |
| 615 | break; |
| 616 | case BuiltinType::Bool: |
| 617 | Encoding = llvm::dwarf::DW_ATE_boolean; |
| 618 | break; |
Guy Benyei | 11169dd | 2012-12-18 14:30:41 +0000 | [diff] [blame] | 619 | case BuiltinType::Half: |
| 620 | case BuiltinType::Float: |
| 621 | case BuiltinType::LongDouble: |
Nemanja Ivanovic | bb1ea2d | 2016-05-09 08:52:33 +0000 | [diff] [blame] | 622 | case BuiltinType::Float128: |
Eric Christopher | e7b87e5 | 2014-10-26 23:40:33 +0000 | [diff] [blame] | 623 | case BuiltinType::Double: |
Nemanja Ivanovic | bb1ea2d | 2016-05-09 08:52:33 +0000 | [diff] [blame] | 624 | // FIXME: For targets where long double and __float128 have the same size, |
| 625 | // they are currently indistinguishable in the debugger without some |
| 626 | // special treatment. However, there is currently no consensus on encoding |
| 627 | // and this should be updated once a DWARF encoding exists for distinct |
| 628 | // floating point types of the same size. |
Eric Christopher | e7b87e5 | 2014-10-26 23:40:33 +0000 | [diff] [blame] | 629 | Encoding = llvm::dwarf::DW_ATE_float; |
| 630 | break; |
Guy Benyei | 11169dd | 2012-12-18 14:30:41 +0000 | [diff] [blame] | 631 | } |
| 632 | |
| 633 | switch (BT->getKind()) { |
Eric Christopher | e7b87e5 | 2014-10-26 23:40:33 +0000 | [diff] [blame] | 634 | case BuiltinType::Long: |
| 635 | BTName = "long int"; |
| 636 | break; |
| 637 | case BuiltinType::LongLong: |
| 638 | BTName = "long long int"; |
| 639 | break; |
| 640 | case BuiltinType::ULong: |
| 641 | BTName = "long unsigned int"; |
| 642 | break; |
| 643 | case BuiltinType::ULongLong: |
| 644 | BTName = "long long unsigned int"; |
| 645 | break; |
Guy Benyei | 11169dd | 2012-12-18 14:30:41 +0000 | [diff] [blame] | 646 | default: |
| 647 | BTName = BT->getName(CGM.getLangOpts()); |
| 648 | break; |
| 649 | } |
Victor Leschuk | a7ece03 | 2016-10-20 00:13:19 +0000 | [diff] [blame] | 650 | // Bit size and offset of the type. |
Guy Benyei | 11169dd | 2012-12-18 14:30:41 +0000 | [diff] [blame] | 651 | uint64_t Size = CGM.getContext().getTypeSize(BT); |
Victor Leschuk | a7ece03 | 2016-10-20 00:13:19 +0000 | [diff] [blame] | 652 | return DBuilder.createBasicType(BTName, Size, Encoding); |
Guy Benyei | 11169dd | 2012-12-18 14:30:41 +0000 | [diff] [blame] | 653 | } |
| 654 | |
Duncan P. N. Exon Smith | 9dd4e4e | 2015-04-29 16:40:08 +0000 | [diff] [blame] | 655 | llvm::DIType *CGDebugInfo::CreateType(const ComplexType *Ty) { |
Victor Leschuk | a7ece03 | 2016-10-20 00:13:19 +0000 | [diff] [blame] | 656 | // Bit size and offset of the type. |
Ed Maste | da70602 | 2014-05-07 12:49:30 +0000 | [diff] [blame] | 657 | llvm::dwarf::TypeKind Encoding = llvm::dwarf::DW_ATE_complex_float; |
Guy Benyei | 11169dd | 2012-12-18 14:30:41 +0000 | [diff] [blame] | 658 | if (Ty->isComplexIntegerType()) |
| 659 | Encoding = llvm::dwarf::DW_ATE_lo_user; |
| 660 | |
| 661 | uint64_t Size = CGM.getContext().getTypeSize(Ty); |
Victor Leschuk | a7ece03 | 2016-10-20 00:13:19 +0000 | [diff] [blame] | 662 | return DBuilder.createBasicType("complex", Size, Encoding); |
Guy Benyei | 11169dd | 2012-12-18 14:30:41 +0000 | [diff] [blame] | 663 | } |
| 664 | |
Duncan P. N. Exon Smith | 9dd4e4e | 2015-04-29 16:40:08 +0000 | [diff] [blame] | 665 | llvm::DIType *CGDebugInfo::CreateQualifiedType(QualType Ty, |
| 666 | llvm::DIFile *Unit) { |
Guy Benyei | 11169dd | 2012-12-18 14:30:41 +0000 | [diff] [blame] | 667 | QualifierCollector Qc; |
| 668 | const Type *T = Qc.strip(Ty); |
| 669 | |
| 670 | // Ignore these qualifiers for now. |
| 671 | Qc.removeObjCGCAttr(); |
| 672 | Qc.removeAddressSpace(); |
| 673 | Qc.removeObjCLifetime(); |
| 674 | |
| 675 | // We will create one Derived type for one qualifier and recurse to handle any |
| 676 | // additional ones. |
Ed Maste | da70602 | 2014-05-07 12:49:30 +0000 | [diff] [blame] | 677 | llvm::dwarf::Tag Tag; |
Guy Benyei | 11169dd | 2012-12-18 14:30:41 +0000 | [diff] [blame] | 678 | if (Qc.hasConst()) { |
| 679 | Tag = llvm::dwarf::DW_TAG_const_type; |
| 680 | Qc.removeConst(); |
| 681 | } else if (Qc.hasVolatile()) { |
| 682 | Tag = llvm::dwarf::DW_TAG_volatile_type; |
| 683 | Qc.removeVolatile(); |
| 684 | } else if (Qc.hasRestrict()) { |
| 685 | Tag = llvm::dwarf::DW_TAG_restrict_type; |
| 686 | Qc.removeRestrict(); |
| 687 | } else { |
| 688 | assert(Qc.empty() && "Unknown type qualifier for debug info"); |
| 689 | return getOrCreateType(QualType(T, 0), Unit); |
| 690 | } |
| 691 | |
Duncan P. N. Exon Smith | 0b6c369 | 2015-04-20 18:51:48 +0000 | [diff] [blame] | 692 | auto *FromTy = getOrCreateType(Qc.apply(CGM.getContext(), T), Unit); |
Guy Benyei | 11169dd | 2012-12-18 14:30:41 +0000 | [diff] [blame] | 693 | |
| 694 | // No need to fill in the Name, Line, Size, Alignment, Offset in case of |
| 695 | // CVR derived types. |
Duncan P. N. Exon Smith | 0b6c369 | 2015-04-20 18:51:48 +0000 | [diff] [blame] | 696 | return DBuilder.createQualifiedType(Tag, FromTy); |
Guy Benyei | 11169dd | 2012-12-18 14:30:41 +0000 | [diff] [blame] | 697 | } |
| 698 | |
Duncan P. N. Exon Smith | 9dd4e4e | 2015-04-29 16:40:08 +0000 | [diff] [blame] | 699 | llvm::DIType *CGDebugInfo::CreateType(const ObjCObjectPointerType *Ty, |
| 700 | llvm::DIFile *Unit) { |
Fariborz Jahanian | 65f1fa1 | 2013-02-21 20:42:11 +0000 | [diff] [blame] | 701 | |
| 702 | // The frontend treats 'id' as a typedef to an ObjCObjectType, |
| 703 | // whereas 'id<protocol>' is treated as an ObjCPointerType. For the |
| 704 | // debug info, we want to emit 'id' in both cases. |
| 705 | if (Ty->isObjCQualifiedIdType()) |
Eric Christopher | e7b87e5 | 2014-10-26 23:40:33 +0000 | [diff] [blame] | 706 | return getOrCreateType(CGM.getContext().getObjCIdType(), Unit); |
Fariborz Jahanian | 65f1fa1 | 2013-02-21 20:42:11 +0000 | [diff] [blame] | 707 | |
Duncan P. N. Exon Smith | 0b6c369 | 2015-04-20 18:51:48 +0000 | [diff] [blame] | 708 | return CreatePointerLikeType(llvm::dwarf::DW_TAG_pointer_type, Ty, |
| 709 | Ty->getPointeeType(), Unit); |
Guy Benyei | 11169dd | 2012-12-18 14:30:41 +0000 | [diff] [blame] | 710 | } |
| 711 | |
Duncan P. N. Exon Smith | 9dd4e4e | 2015-04-29 16:40:08 +0000 | [diff] [blame] | 712 | llvm::DIType *CGDebugInfo::CreateType(const PointerType *Ty, |
| 713 | llvm::DIFile *Unit) { |
Eric Christopher | b2a008c | 2013-05-16 00:45:12 +0000 | [diff] [blame] | 714 | return CreatePointerLikeType(llvm::dwarf::DW_TAG_pointer_type, Ty, |
Guy Benyei | 11169dd | 2012-12-18 14:30:41 +0000 | [diff] [blame] | 715 | Ty->getPointeeType(), Unit); |
| 716 | } |
| 717 | |
Adrian Prantl | c6f91a2 | 2015-06-15 23:18:16 +0000 | [diff] [blame] | 718 | /// \return whether a C++ mangling exists for the type defined by TD. |
| 719 | static bool hasCXXMangling(const TagDecl *TD, llvm::DICompileUnit *TheCU) { |
| 720 | switch (TheCU->getSourceLanguage()) { |
| 721 | case llvm::dwarf::DW_LANG_C_plus_plus: |
| 722 | return true; |
| 723 | case llvm::dwarf::DW_LANG_ObjC_plus_plus: |
| 724 | return isa<CXXRecordDecl>(TD) || isa<EnumDecl>(TD); |
| 725 | default: |
| 726 | return false; |
| 727 | } |
| 728 | } |
| 729 | |
Manman Ren | e0064d8 | 2013-08-29 23:19:58 +0000 | [diff] [blame] | 730 | /// In C++ mode, types have linkage, so we can rely on the ODR and |
| 731 | /// on their mangled names, if they're external. |
Eric Christopher | e7b87e5 | 2014-10-26 23:40:33 +0000 | [diff] [blame] | 732 | static SmallString<256> getUniqueTagTypeName(const TagType *Ty, |
| 733 | CodeGenModule &CGM, |
Duncan P. N. Exon Smith | 9dd4e4e | 2015-04-29 16:40:08 +0000 | [diff] [blame] | 734 | llvm::DICompileUnit *TheCU) { |
Manman Ren | e0064d8 | 2013-08-29 23:19:58 +0000 | [diff] [blame] | 735 | SmallString<256> FullName; |
Manman Ren | e0064d8 | 2013-08-29 23:19:58 +0000 | [diff] [blame] | 736 | const TagDecl *TD = Ty->getDecl(); |
Adrian Prantl | c6f91a2 | 2015-06-15 23:18:16 +0000 | [diff] [blame] | 737 | |
| 738 | if (!hasCXXMangling(TD, TheCU) || !TD->isExternallyVisible()) |
Manman Ren | e0064d8 | 2013-08-29 23:19:58 +0000 | [diff] [blame] | 739 | return FullName; |
Adrian Prantl | c6f91a2 | 2015-06-15 23:18:16 +0000 | [diff] [blame] | 740 | |
Manman Ren | e0064d8 | 2013-08-29 23:19:58 +0000 | [diff] [blame] | 741 | // TODO: This is using the RTTI name. Is there a better way to get |
| 742 | // a unique string for a type? |
| 743 | llvm::raw_svector_ostream Out(FullName); |
| 744 | CGM.getCXXABI().getMangleContext().mangleCXXRTTIName(QualType(Ty, 0), Out); |
Manman Ren | e0064d8 | 2013-08-29 23:19:58 +0000 | [diff] [blame] | 745 | return FullName; |
| 746 | } |
| 747 | |
Adrian Prantl | 3e8bad4 | 2015-07-08 21:18:34 +0000 | [diff] [blame] | 748 | /// \return the approproate DWARF tag for a composite type. |
Adrian Prantl | 5f66bae | 2015-02-11 17:45:15 +0000 | [diff] [blame] | 749 | static llvm::dwarf::Tag getTagForRecord(const RecordDecl *RD) { |
| 750 | llvm::dwarf::Tag Tag; |
| 751 | if (RD->isStruct() || RD->isInterface()) |
| 752 | Tag = llvm::dwarf::DW_TAG_structure_type; |
| 753 | else if (RD->isUnion()) |
| 754 | Tag = llvm::dwarf::DW_TAG_union_type; |
| 755 | else { |
| 756 | // FIXME: This could be a struct type giving a default visibility different |
| 757 | // than C++ class type, but needs llvm metadata changes first. |
| 758 | assert(RD->isClass()); |
| 759 | Tag = llvm::dwarf::DW_TAG_class_type; |
| 760 | } |
| 761 | return Tag; |
| 762 | } |
| 763 | |
Duncan P. N. Exon Smith | 9dd4e4e | 2015-04-29 16:40:08 +0000 | [diff] [blame] | 764 | llvm::DICompositeType * |
Manman Ren | 1b45702 | 2013-08-28 21:20:28 +0000 | [diff] [blame] | 765 | CGDebugInfo::getOrCreateRecordFwdDecl(const RecordType *Ty, |
Duncan P. N. Exon Smith | 9dd4e4e | 2015-04-29 16:40:08 +0000 | [diff] [blame] | 766 | llvm::DIScope *Ctx) { |
Manman Ren | 1b45702 | 2013-08-28 21:20:28 +0000 | [diff] [blame] | 767 | const RecordDecl *RD = Ty->getDecl(); |
Duncan P. N. Exon Smith | 9dd4e4e | 2015-04-29 16:40:08 +0000 | [diff] [blame] | 768 | if (llvm::DIType *T = getTypeOrNull(CGM.getContext().getRecordType(RD))) |
| 769 | return cast<llvm::DICompositeType>(T); |
| 770 | llvm::DIFile *DefUnit = getOrCreateFile(RD->getLocation()); |
Guy Benyei | 11169dd | 2012-12-18 14:30:41 +0000 | [diff] [blame] | 771 | unsigned Line = getLineNumber(RD->getLocation()); |
| 772 | StringRef RDName = getClassName(RD); |
| 773 | |
Peter Collingbourne | d251b0a | 2015-03-01 22:07:04 +0000 | [diff] [blame] | 774 | uint64_t Size = 0; |
Victor Leschuk | 802e4a5 | 2016-10-19 22:11:07 +0000 | [diff] [blame] | 775 | uint32_t Align = 0; |
Peter Collingbourne | d251b0a | 2015-03-01 22:07:04 +0000 | [diff] [blame] | 776 | |
Guy Benyei | 11169dd | 2012-12-18 14:30:41 +0000 | [diff] [blame] | 777 | // Create the type. |
Manman Ren | e0064d8 | 2013-08-29 23:19:58 +0000 | [diff] [blame] | 778 | SmallString<256> FullName = getUniqueTagTypeName(Ty, CGM, TheCU); |
Duncan P. N. Exon Smith | 9dd4e4e | 2015-04-29 16:40:08 +0000 | [diff] [blame] | 779 | llvm::DICompositeType *RetTy = DBuilder.createReplaceableCompositeType( |
Peter Collingbourne | d251b0a | 2015-03-01 22:07:04 +0000 | [diff] [blame] | 780 | getTagForRecord(RD), RDName, Ctx, DefUnit, Line, 0, Size, Align, |
Duncan P. N. Exon Smith | 9dd4e4e | 2015-04-29 16:40:08 +0000 | [diff] [blame] | 781 | llvm::DINode::FlagFwdDecl, FullName); |
Duncan P. N. Exon Smith | fb49491 | 2014-12-09 18:39:32 +0000 | [diff] [blame] | 782 | ReplaceMap.emplace_back( |
| 783 | std::piecewise_construct, std::make_tuple(Ty), |
| 784 | std::make_tuple(static_cast<llvm::Metadata *>(RetTy))); |
David Blaikie | f427b00 | 2014-05-06 03:42:01 +0000 | [diff] [blame] | 785 | return RetTy; |
Guy Benyei | 11169dd | 2012-12-18 14:30:41 +0000 | [diff] [blame] | 786 | } |
| 787 | |
Duncan P. N. Exon Smith | 9dd4e4e | 2015-04-29 16:40:08 +0000 | [diff] [blame] | 788 | llvm::DIType *CGDebugInfo::CreatePointerLikeType(llvm::dwarf::Tag Tag, |
Duncan P. N. Exon Smith | 0b6c369 | 2015-04-20 18:51:48 +0000 | [diff] [blame] | 789 | const Type *Ty, |
| 790 | QualType PointeeTy, |
Duncan P. N. Exon Smith | 9dd4e4e | 2015-04-29 16:40:08 +0000 | [diff] [blame] | 791 | llvm::DIFile *Unit) { |
Guy Benyei | 11169dd | 2012-12-18 14:30:41 +0000 | [diff] [blame] | 792 | // Bit size, align and offset of the type. |
| 793 | // Size is always the size of a pointer. We can't use getTypeSize here |
| 794 | // because that does not return the correct value for references. |
| 795 | unsigned AS = CGM.getContext().getTargetAddressSpace(PointeeTy); |
John McCall | c8e0170 | 2013-04-16 22:48:15 +0000 | [diff] [blame] | 796 | uint64_t Size = CGM.getTarget().getPointerWidth(AS); |
Victor Leschuk | a7ece03 | 2016-10-20 00:13:19 +0000 | [diff] [blame] | 797 | auto Align = getTypeAlignIfRequired(Ty, CGM.getContext()); |
Guy Benyei | 11169dd | 2012-12-18 14:30:41 +0000 | [diff] [blame] | 798 | |
Keno Fischer | 87842f3 | 2015-11-16 09:04:13 +0000 | [diff] [blame] | 799 | if (Tag == llvm::dwarf::DW_TAG_reference_type || |
| 800 | Tag == llvm::dwarf::DW_TAG_rvalue_reference_type) |
| 801 | return DBuilder.createReferenceType(Tag, getOrCreateType(PointeeTy, Unit), |
| 802 | Size, Align); |
| 803 | else |
| 804 | return DBuilder.createPointerType(getOrCreateType(PointeeTy, Unit), Size, |
| 805 | Align); |
Guy Benyei | 11169dd | 2012-12-18 14:30:41 +0000 | [diff] [blame] | 806 | } |
| 807 | |
Duncan P. N. Exon Smith | 9dd4e4e | 2015-04-29 16:40:08 +0000 | [diff] [blame] | 808 | llvm::DIType *CGDebugInfo::getOrCreateStructPtrType(StringRef Name, |
| 809 | llvm::DIType *&Cache) { |
Eric Christopher | f8bc4d8 | 2013-07-18 00:52:50 +0000 | [diff] [blame] | 810 | if (Cache) |
Guy Benyei | d8a08ea | 2012-12-18 14:38:23 +0000 | [diff] [blame] | 811 | return Cache; |
David Blaikie | fefc7f7 | 2013-05-21 17:58:54 +0000 | [diff] [blame] | 812 | Cache = DBuilder.createForwardDecl(llvm::dwarf::DW_TAG_structure_type, Name, |
| 813 | TheCU, getOrCreateMainFile(), 0); |
| 814 | unsigned Size = CGM.getContext().getTypeSize(CGM.getContext().VoidPtrTy); |
| 815 | Cache = DBuilder.createPointerType(Cache, Size); |
| 816 | return Cache; |
Guy Benyei | d8a08ea | 2012-12-18 14:38:23 +0000 | [diff] [blame] | 817 | } |
| 818 | |
Duncan P. N. Exon Smith | 9dd4e4e | 2015-04-29 16:40:08 +0000 | [diff] [blame] | 819 | llvm::DIType *CGDebugInfo::CreateType(const BlockPointerType *Ty, |
| 820 | llvm::DIFile *Unit) { |
Duncan P. N. Exon Smith | fb49491 | 2014-12-09 18:39:32 +0000 | [diff] [blame] | 821 | SmallVector<llvm::Metadata *, 8> EltTys; |
Guy Benyei | 11169dd | 2012-12-18 14:30:41 +0000 | [diff] [blame] | 822 | QualType FType; |
| 823 | uint64_t FieldSize, FieldOffset; |
Victor Leschuk | 802e4a5 | 2016-10-19 22:11:07 +0000 | [diff] [blame] | 824 | uint32_t FieldAlign; |
Duncan P. N. Exon Smith | 9dd4e4e | 2015-04-29 16:40:08 +0000 | [diff] [blame] | 825 | llvm::DINodeArray Elements; |
Guy Benyei | 11169dd | 2012-12-18 14:30:41 +0000 | [diff] [blame] | 826 | |
| 827 | FieldOffset = 0; |
| 828 | FType = CGM.getContext().UnsignedLongTy; |
| 829 | EltTys.push_back(CreateMemberType(Unit, FType, "reserved", &FieldOffset)); |
| 830 | EltTys.push_back(CreateMemberType(Unit, FType, "Size", &FieldOffset)); |
| 831 | |
| 832 | Elements = DBuilder.getOrCreateArray(EltTys); |
| 833 | EltTys.clear(); |
| 834 | |
Leny Kholodov | 80c047d | 2016-09-06 10:48:04 +0000 | [diff] [blame] | 835 | llvm::DINode::DIFlags Flags = llvm::DINode::FlagAppleBlock; |
Adrian Prantl | 498fff6 | 2015-07-06 21:31:35 +0000 | [diff] [blame] | 836 | unsigned LineNo = 0; |
Guy Benyei | 11169dd | 2012-12-18 14:30:41 +0000 | [diff] [blame] | 837 | |
Duncan P. N. Exon Smith | 0b6c369 | 2015-04-20 18:51:48 +0000 | [diff] [blame] | 838 | auto *EltTy = |
Adrian Prantl | 498fff6 | 2015-07-06 21:31:35 +0000 | [diff] [blame] | 839 | DBuilder.createStructType(Unit, "__block_descriptor", nullptr, LineNo, |
Duncan P. N. Exon Smith | 0b6c369 | 2015-04-20 18:51:48 +0000 | [diff] [blame] | 840 | FieldOffset, 0, Flags, nullptr, Elements); |
Guy Benyei | 11169dd | 2012-12-18 14:30:41 +0000 | [diff] [blame] | 841 | |
| 842 | // Bit size, align and offset of the type. |
| 843 | uint64_t Size = CGM.getContext().getTypeSize(Ty); |
| 844 | |
Duncan P. N. Exon Smith | 0b6c369 | 2015-04-20 18:51:48 +0000 | [diff] [blame] | 845 | auto *DescTy = DBuilder.createPointerType(EltTy, Size); |
Guy Benyei | 11169dd | 2012-12-18 14:30:41 +0000 | [diff] [blame] | 846 | |
| 847 | FieldOffset = 0; |
| 848 | FType = CGM.getContext().getPointerType(CGM.getContext().VoidTy); |
| 849 | EltTys.push_back(CreateMemberType(Unit, FType, "__isa", &FieldOffset)); |
| 850 | FType = CGM.getContext().IntTy; |
| 851 | EltTys.push_back(CreateMemberType(Unit, FType, "__flags", &FieldOffset)); |
| 852 | EltTys.push_back(CreateMemberType(Unit, FType, "__reserved", &FieldOffset)); |
Adrian Prantl | 65d5d00 | 2014-11-05 01:01:30 +0000 | [diff] [blame] | 853 | FType = CGM.getContext().getPointerType(Ty->getPointeeType()); |
Guy Benyei | 11169dd | 2012-12-18 14:30:41 +0000 | [diff] [blame] | 854 | EltTys.push_back(CreateMemberType(Unit, FType, "__FuncPtr", &FieldOffset)); |
| 855 | |
| 856 | FType = CGM.getContext().getPointerType(CGM.getContext().VoidTy); |
Guy Benyei | 11169dd | 2012-12-18 14:30:41 +0000 | [diff] [blame] | 857 | FieldSize = CGM.getContext().getTypeSize(Ty); |
| 858 | FieldAlign = CGM.getContext().getTypeAlign(Ty); |
Leny Kholodov | df050fd | 2016-09-06 17:06:14 +0000 | [diff] [blame] | 859 | EltTys.push_back(DBuilder.createMemberType( |
| 860 | Unit, "__descriptor", nullptr, LineNo, FieldSize, FieldAlign, FieldOffset, |
| 861 | llvm::DINode::FlagZero, DescTy)); |
Guy Benyei | 11169dd | 2012-12-18 14:30:41 +0000 | [diff] [blame] | 862 | |
| 863 | FieldOffset += FieldSize; |
| 864 | Elements = DBuilder.getOrCreateArray(EltTys); |
| 865 | |
Adrian Prantl | 3d2c051 | 2015-07-07 00:49:35 +0000 | [diff] [blame] | 866 | // The __block_literal_generic structs are marked with a special |
| 867 | // DW_AT_APPLE_BLOCK attribute and are an implementation detail only |
| 868 | // the debugger needs to know about. To allow type uniquing, emit |
| 869 | // them without a name or a location. |
Duncan P. N. Exon Smith | 0b6c369 | 2015-04-20 18:51:48 +0000 | [diff] [blame] | 870 | EltTy = |
Adrian Prantl | 3d2c051 | 2015-07-07 00:49:35 +0000 | [diff] [blame] | 871 | DBuilder.createStructType(Unit, "", nullptr, LineNo, |
Duncan P. N. Exon Smith | 0b6c369 | 2015-04-20 18:51:48 +0000 | [diff] [blame] | 872 | FieldOffset, 0, Flags, nullptr, Elements); |
Guy Benyei | 11169dd | 2012-12-18 14:30:41 +0000 | [diff] [blame] | 873 | |
Adrian Prantl | 3d2c051 | 2015-07-07 00:49:35 +0000 | [diff] [blame] | 874 | return DBuilder.createPointerType(EltTy, Size); |
Guy Benyei | 11169dd | 2012-12-18 14:30:41 +0000 | [diff] [blame] | 875 | } |
| 876 | |
Duncan P. N. Exon Smith | 9dd4e4e | 2015-04-29 16:40:08 +0000 | [diff] [blame] | 877 | llvm::DIType *CGDebugInfo::CreateType(const TemplateSpecializationType *Ty, |
| 878 | llvm::DIFile *Unit) { |
David Blaikie | f1b382e | 2014-04-06 17:14:06 +0000 | [diff] [blame] | 879 | assert(Ty->isTypeAlias()); |
Duncan P. N. Exon Smith | 9dd4e4e | 2015-04-29 16:40:08 +0000 | [diff] [blame] | 880 | llvm::DIType *Src = getOrCreateType(Ty->getAliasedType(), Unit); |
David Blaikie | f1b382e | 2014-04-06 17:14:06 +0000 | [diff] [blame] | 881 | |
| 882 | SmallString<128> NS; |
| 883 | llvm::raw_svector_ostream OS(NS); |
Eric Christopher | e7b87e5 | 2014-10-26 23:40:33 +0000 | [diff] [blame] | 884 | Ty->getTemplateName().print(OS, CGM.getContext().getPrintingPolicy(), |
| 885 | /*qualified*/ false); |
David Blaikie | f1b382e | 2014-04-06 17:14:06 +0000 | [diff] [blame] | 886 | |
| 887 | TemplateSpecializationType::PrintTemplateArgumentList( |
David Majnemer | 6fbeee3 | 2016-07-07 04:43:07 +0000 | [diff] [blame] | 888 | OS, Ty->template_arguments(), |
David Blaikie | f1b382e | 2014-04-06 17:14:06 +0000 | [diff] [blame] | 889 | CGM.getContext().getPrintingPolicy()); |
| 890 | |
David Majnemer | 58ed0f3 | 2016-07-17 00:39:12 +0000 | [diff] [blame] | 891 | auto *AliasDecl = cast<TypeAliasTemplateDecl>( |
Eric Christopher | e7b87e5 | 2014-10-26 23:40:33 +0000 | [diff] [blame] | 892 | Ty->getTemplateName().getAsTemplateDecl())->getTemplatedDecl(); |
David Blaikie | f1b382e | 2014-04-06 17:14:06 +0000 | [diff] [blame] | 893 | |
| 894 | SourceLocation Loc = AliasDecl->getLocation(); |
Adrian Prantl | b67dbce | 2015-09-11 18:45:02 +0000 | [diff] [blame] | 895 | return DBuilder.createTypedef(Src, OS.str(), getOrCreateFile(Loc), |
| 896 | getLineNumber(Loc), |
| 897 | getDeclContextDescriptor(AliasDecl)); |
David Blaikie | f1b382e | 2014-04-06 17:14:06 +0000 | [diff] [blame] | 898 | } |
| 899 | |
Duncan P. N. Exon Smith | 9dd4e4e | 2015-04-29 16:40:08 +0000 | [diff] [blame] | 900 | llvm::DIType *CGDebugInfo::CreateType(const TypedefType *Ty, |
| 901 | llvm::DIFile *Unit) { |
Guy Benyei | 11169dd | 2012-12-18 14:30:41 +0000 | [diff] [blame] | 902 | // We don't set size information, but do specify where the typedef was |
| 903 | // declared. |
Amjad Aboud | dc4531e | 2016-04-30 01:44:38 +0000 | [diff] [blame] | 904 | SourceLocation Loc = Ty->getDecl()->getLocation(); |
Eric Christopher | b2a008c | 2013-05-16 00:45:12 +0000 | [diff] [blame] | 905 | |
Duncan P. N. Exon Smith | 4078ad4 | 2015-04-16 16:36:45 +0000 | [diff] [blame] | 906 | // Typedefs are derived from some other type. |
| 907 | return DBuilder.createTypedef( |
| 908 | getOrCreateType(Ty->getDecl()->getUnderlyingType(), Unit), |
| 909 | Ty->getDecl()->getName(), getOrCreateFile(Loc), getLineNumber(Loc), |
Amjad Aboud | dc4531e | 2016-04-30 01:44:38 +0000 | [diff] [blame] | 910 | getDeclContextDescriptor(Ty->getDecl())); |
Guy Benyei | 11169dd | 2012-12-18 14:30:41 +0000 | [diff] [blame] | 911 | } |
| 912 | |
Reid Kleckner | f00f803 | 2016-06-08 20:41:54 +0000 | [diff] [blame] | 913 | static unsigned getDwarfCC(CallingConv CC) { |
| 914 | switch (CC) { |
| 915 | case CC_C: |
| 916 | // Avoid emitting DW_AT_calling_convention if the C convention was used. |
| 917 | return 0; |
| 918 | |
| 919 | case CC_X86StdCall: |
| 920 | return llvm::dwarf::DW_CC_BORLAND_stdcall; |
| 921 | case CC_X86FastCall: |
| 922 | return llvm::dwarf::DW_CC_BORLAND_msfastcall; |
| 923 | case CC_X86ThisCall: |
| 924 | return llvm::dwarf::DW_CC_BORLAND_thiscall; |
| 925 | case CC_X86VectorCall: |
| 926 | return llvm::dwarf::DW_CC_LLVM_vectorcall; |
| 927 | case CC_X86Pascal: |
| 928 | return llvm::dwarf::DW_CC_BORLAND_pascal; |
| 929 | |
| 930 | // FIXME: Create new DW_CC_ codes for these calling conventions. |
| 931 | case CC_X86_64Win64: |
| 932 | case CC_X86_64SysV: |
| 933 | case CC_AAPCS: |
| 934 | case CC_AAPCS_VFP: |
| 935 | case CC_IntelOclBicc: |
| 936 | case CC_SpirFunction: |
Nikolay Haustov | 8c6538b | 2016-06-30 09:06:33 +0000 | [diff] [blame] | 937 | case CC_OpenCLKernel: |
Reid Kleckner | f00f803 | 2016-06-08 20:41:54 +0000 | [diff] [blame] | 938 | case CC_Swift: |
| 939 | case CC_PreserveMost: |
| 940 | case CC_PreserveAll: |
Erich Keane | 757d317 | 2016-11-02 18:29:35 +0000 | [diff] [blame] | 941 | case CC_X86RegCall: |
Reid Kleckner | f00f803 | 2016-06-08 20:41:54 +0000 | [diff] [blame] | 942 | return 0; |
| 943 | } |
| 944 | return 0; |
| 945 | } |
| 946 | |
Duncan P. N. Exon Smith | 9dd4e4e | 2015-04-29 16:40:08 +0000 | [diff] [blame] | 947 | llvm::DIType *CGDebugInfo::CreateType(const FunctionType *Ty, |
| 948 | llvm::DIFile *Unit) { |
Duncan P. N. Exon Smith | fb49491 | 2014-12-09 18:39:32 +0000 | [diff] [blame] | 949 | SmallVector<llvm::Metadata *, 16> EltTys; |
Guy Benyei | 11169dd | 2012-12-18 14:30:41 +0000 | [diff] [blame] | 950 | |
| 951 | // Add the result type at least. |
Alp Toker | 314cc81 | 2014-01-25 16:55:45 +0000 | [diff] [blame] | 952 | EltTys.push_back(getOrCreateType(Ty->getReturnType(), Unit)); |
Guy Benyei | 11169dd | 2012-12-18 14:30:41 +0000 | [diff] [blame] | 953 | |
| 954 | // Set up remainder of arguments if there is a prototype. |
Adrian Prantl | 800faef | 2014-02-25 23:42:18 +0000 | [diff] [blame] | 955 | // otherwise emit it as a variadic function. |
Guy Benyei | 11169dd | 2012-12-18 14:30:41 +0000 | [diff] [blame] | 956 | if (isa<FunctionNoProtoType>(Ty)) |
| 957 | EltTys.push_back(DBuilder.createUnspecifiedParameter()); |
David Majnemer | 58ed0f3 | 2016-07-17 00:39:12 +0000 | [diff] [blame] | 958 | else if (const auto *FPT = dyn_cast<FunctionProtoType>(Ty)) { |
| 959 | for (const QualType &ParamType : FPT->param_types()) |
| 960 | EltTys.push_back(getOrCreateType(ParamType, Unit)); |
Adrian Prantl | d45ba25 | 2014-02-25 19:38:11 +0000 | [diff] [blame] | 961 | if (FPT->isVariadic()) |
| 962 | EltTys.push_back(DBuilder.createUnspecifiedParameter()); |
Guy Benyei | 11169dd | 2012-12-18 14:30:41 +0000 | [diff] [blame] | 963 | } |
| 964 | |
Duncan P. N. Exon Smith | 9dd4e4e | 2015-04-29 16:40:08 +0000 | [diff] [blame] | 965 | llvm::DITypeRefArray EltTypeArray = DBuilder.getOrCreateTypeArray(EltTys); |
Leny Kholodov | 80c047d | 2016-09-06 10:48:04 +0000 | [diff] [blame] | 966 | return DBuilder.createSubroutineType(EltTypeArray, llvm::DINode::FlagZero, |
Reid Kleckner | f00f803 | 2016-06-08 20:41:54 +0000 | [diff] [blame] | 967 | getDwarfCC(Ty->getCallConv())); |
Guy Benyei | 11169dd | 2012-12-18 14:30:41 +0000 | [diff] [blame] | 968 | } |
| 969 | |
Duncan P. N. Exon Smith | 9dd4e4e | 2015-04-29 16:40:08 +0000 | [diff] [blame] | 970 | /// Convert an AccessSpecifier into the corresponding DINode flag. |
Adrian Prantl | 21361fb | 2014-08-29 22:44:27 +0000 | [diff] [blame] | 971 | /// As an optimization, return 0 if the access specifier equals the |
| 972 | /// default for the containing type. |
Leny Kholodov | df050fd | 2016-09-06 17:06:14 +0000 | [diff] [blame] | 973 | static llvm::DINode::DIFlags getAccessFlag(AccessSpecifier Access, |
| 974 | const RecordDecl *RD) { |
Adrian Prantl | 21361fb | 2014-08-29 22:44:27 +0000 | [diff] [blame] | 975 | AccessSpecifier Default = clang::AS_none; |
| 976 | if (RD && RD->isClass()) |
| 977 | Default = clang::AS_private; |
| 978 | else if (RD && (RD->isStruct() || RD->isUnion())) |
| 979 | Default = clang::AS_public; |
| 980 | |
| 981 | if (Access == Default) |
Leny Kholodov | 80c047d | 2016-09-06 10:48:04 +0000 | [diff] [blame] | 982 | return llvm::DINode::FlagZero; |
Adrian Prantl | 21361fb | 2014-08-29 22:44:27 +0000 | [diff] [blame] | 983 | |
Eric Christopher | e7b87e5 | 2014-10-26 23:40:33 +0000 | [diff] [blame] | 984 | switch (Access) { |
| 985 | case clang::AS_private: |
Duncan P. N. Exon Smith | 9dd4e4e | 2015-04-29 16:40:08 +0000 | [diff] [blame] | 986 | return llvm::DINode::FlagPrivate; |
Eric Christopher | e7b87e5 | 2014-10-26 23:40:33 +0000 | [diff] [blame] | 987 | case clang::AS_protected: |
Duncan P. N. Exon Smith | 9dd4e4e | 2015-04-29 16:40:08 +0000 | [diff] [blame] | 988 | return llvm::DINode::FlagProtected; |
Eric Christopher | e7b87e5 | 2014-10-26 23:40:33 +0000 | [diff] [blame] | 989 | case clang::AS_public: |
Duncan P. N. Exon Smith | 9dd4e4e | 2015-04-29 16:40:08 +0000 | [diff] [blame] | 990 | return llvm::DINode::FlagPublic; |
Eric Christopher | e7b87e5 | 2014-10-26 23:40:33 +0000 | [diff] [blame] | 991 | case clang::AS_none: |
Leny Kholodov | 80c047d | 2016-09-06 10:48:04 +0000 | [diff] [blame] | 992 | return llvm::DINode::FlagZero; |
Adrian Prantl | 21361fb | 2014-08-29 22:44:27 +0000 | [diff] [blame] | 993 | } |
| 994 | llvm_unreachable("unexpected access enumerator"); |
| 995 | } |
Guy Benyei | 11169dd | 2012-12-18 14:30:41 +0000 | [diff] [blame] | 996 | |
David Majnemer | b4b671e | 2016-06-30 03:01:59 +0000 | [diff] [blame] | 997 | llvm::DIType *CGDebugInfo::createBitFieldType(const FieldDecl *BitFieldDecl, |
| 998 | llvm::DIScope *RecordTy, |
| 999 | const RecordDecl *RD) { |
| 1000 | StringRef Name = BitFieldDecl->getName(); |
| 1001 | QualType Ty = BitFieldDecl->getType(); |
| 1002 | SourceLocation Loc = BitFieldDecl->getLocation(); |
| 1003 | llvm::DIFile *VUnit = getOrCreateFile(Loc); |
| 1004 | llvm::DIType *DebugType = getOrCreateType(Ty, VUnit); |
| 1005 | |
| 1006 | // Get the location for the field. |
| 1007 | llvm::DIFile *File = getOrCreateFile(Loc); |
| 1008 | unsigned Line = getLineNumber(Loc); |
| 1009 | |
| 1010 | const CGBitFieldInfo &BitFieldInfo = |
| 1011 | CGM.getTypes().getCGRecordLayout(RD).getBitFieldInfo(BitFieldDecl); |
| 1012 | uint64_t SizeInBits = BitFieldInfo.Size; |
| 1013 | assert(SizeInBits > 0 && "found named 0-width bitfield"); |
David Majnemer | b4b671e | 2016-06-30 03:01:59 +0000 | [diff] [blame] | 1014 | uint64_t StorageOffsetInBits = |
| 1015 | CGM.getContext().toBits(BitFieldInfo.StorageOffset); |
| 1016 | uint64_t OffsetInBits = StorageOffsetInBits + BitFieldInfo.Offset; |
Leny Kholodov | 80c047d | 2016-09-06 10:48:04 +0000 | [diff] [blame] | 1017 | llvm::DINode::DIFlags Flags = getAccessFlag(BitFieldDecl->getAccess(), RD); |
David Majnemer | b4b671e | 2016-06-30 03:01:59 +0000 | [diff] [blame] | 1018 | return DBuilder.createBitFieldMemberType( |
Victor Leschuk | a7ece03 | 2016-10-20 00:13:19 +0000 | [diff] [blame] | 1019 | RecordTy, Name, File, Line, SizeInBits, OffsetInBits, StorageOffsetInBits, |
| 1020 | Flags, DebugType); |
David Majnemer | b4b671e | 2016-06-30 03:01:59 +0000 | [diff] [blame] | 1021 | } |
| 1022 | |
| 1023 | llvm::DIType * |
| 1024 | CGDebugInfo::createFieldType(StringRef name, QualType type, SourceLocation loc, |
| 1025 | AccessSpecifier AS, uint64_t offsetInBits, |
Victor Leschuk | a7ece03 | 2016-10-20 00:13:19 +0000 | [diff] [blame] | 1026 | uint32_t AlignInBits, llvm::DIFile *tunit, |
| 1027 | llvm::DIScope *scope, const RecordDecl *RD) { |
Duncan P. N. Exon Smith | 9dd4e4e | 2015-04-29 16:40:08 +0000 | [diff] [blame] | 1028 | llvm::DIType *debugType = getOrCreateType(type, tunit); |
Guy Benyei | 11169dd | 2012-12-18 14:30:41 +0000 | [diff] [blame] | 1029 | |
| 1030 | // Get the location for the field. |
Duncan P. N. Exon Smith | 9dd4e4e | 2015-04-29 16:40:08 +0000 | [diff] [blame] | 1031 | llvm::DIFile *file = getOrCreateFile(loc); |
Guy Benyei | 11169dd | 2012-12-18 14:30:41 +0000 | [diff] [blame] | 1032 | unsigned line = getLineNumber(loc); |
| 1033 | |
David Majnemer | 34b5749 | 2014-07-30 01:30:47 +0000 | [diff] [blame] | 1034 | uint64_t SizeInBits = 0; |
Victor Leschuk | a7ece03 | 2016-10-20 00:13:19 +0000 | [diff] [blame] | 1035 | auto Align = AlignInBits; |
Guy Benyei | 11169dd | 2012-12-18 14:30:41 +0000 | [diff] [blame] | 1036 | if (!type->isIncompleteArrayType()) { |
David Majnemer | 34b5749 | 2014-07-30 01:30:47 +0000 | [diff] [blame] | 1037 | TypeInfo TI = CGM.getContext().getTypeInfo(type); |
| 1038 | SizeInBits = TI.Width; |
Victor Leschuk | a7ece03 | 2016-10-20 00:13:19 +0000 | [diff] [blame] | 1039 | if (!Align) |
| 1040 | Align = getTypeAlignIfRequired(type, CGM.getContext()); |
Guy Benyei | 11169dd | 2012-12-18 14:30:41 +0000 | [diff] [blame] | 1041 | } |
| 1042 | |
Leny Kholodov | 80c047d | 2016-09-06 10:48:04 +0000 | [diff] [blame] | 1043 | llvm::DINode::DIFlags flags = getAccessFlag(AS, RD); |
David Majnemer | 34b5749 | 2014-07-30 01:30:47 +0000 | [diff] [blame] | 1044 | return DBuilder.createMemberType(scope, name, file, line, SizeInBits, |
Victor Leschuk | a7ece03 | 2016-10-20 00:13:19 +0000 | [diff] [blame] | 1045 | Align, offsetInBits, flags, debugType); |
Guy Benyei | 11169dd | 2012-12-18 14:30:41 +0000 | [diff] [blame] | 1046 | } |
| 1047 | |
Duncan P. N. Exon Smith | fb49491 | 2014-12-09 18:39:32 +0000 | [diff] [blame] | 1048 | void CGDebugInfo::CollectRecordLambdaFields( |
| 1049 | const CXXRecordDecl *CXXDecl, SmallVectorImpl<llvm::Metadata *> &elements, |
Duncan P. N. Exon Smith | 9dd4e4e | 2015-04-29 16:40:08 +0000 | [diff] [blame] | 1050 | llvm::DIType *RecordTy) { |
Eric Christopher | 91a3190 | 2013-01-16 01:22:32 +0000 | [diff] [blame] | 1051 | // For C++11 Lambdas a Field will be the same as a Capture, but the Capture |
| 1052 | // has the name and the location of the variable so we should iterate over |
| 1053 | // both concurrently. |
| 1054 | const ASTRecordLayout &layout = CGM.getContext().getASTRecordLayout(CXXDecl); |
| 1055 | RecordDecl::field_iterator Field = CXXDecl->field_begin(); |
| 1056 | unsigned fieldno = 0; |
| 1057 | for (CXXRecordDecl::capture_const_iterator I = CXXDecl->captures_begin(), |
Eric Christopher | e7b87e5 | 2014-10-26 23:40:33 +0000 | [diff] [blame] | 1058 | E = CXXDecl->captures_end(); |
| 1059 | I != E; ++I, ++Field, ++fieldno) { |
Benjamin Kramer | f3ca2698 | 2014-05-10 16:31:55 +0000 | [diff] [blame] | 1060 | const LambdaCapture &C = *I; |
Eric Christopher | 91a3190 | 2013-01-16 01:22:32 +0000 | [diff] [blame] | 1061 | if (C.capturesVariable()) { |
David Majnemer | b4b671e | 2016-06-30 03:01:59 +0000 | [diff] [blame] | 1062 | SourceLocation Loc = C.getLocation(); |
| 1063 | assert(!Field->isBitField() && "lambdas don't have bitfield members!"); |
Eric Christopher | 91a3190 | 2013-01-16 01:22:32 +0000 | [diff] [blame] | 1064 | VarDecl *V = C.getCapturedVar(); |
Eric Christopher | 91a3190 | 2013-01-16 01:22:32 +0000 | [diff] [blame] | 1065 | StringRef VName = V->getName(); |
David Majnemer | b4b671e | 2016-06-30 03:01:59 +0000 | [diff] [blame] | 1066 | llvm::DIFile *VUnit = getOrCreateFile(Loc); |
Victor Leschuk | a7ece03 | 2016-10-20 00:13:19 +0000 | [diff] [blame] | 1067 | auto Align = getDeclAlignIfRequired(V, CGM.getContext()); |
David Majnemer | b4b671e | 2016-06-30 03:01:59 +0000 | [diff] [blame] | 1068 | llvm::DIType *FieldType = createFieldType( |
| 1069 | VName, Field->getType(), Loc, Field->getAccess(), |
Victor Leschuk | a7ece03 | 2016-10-20 00:13:19 +0000 | [diff] [blame] | 1070 | layout.getFieldOffset(fieldno), Align, VUnit, RecordTy, CXXDecl); |
David Majnemer | b4b671e | 2016-06-30 03:01:59 +0000 | [diff] [blame] | 1071 | elements.push_back(FieldType); |
Alexey Bataev | 39c81e2 | 2014-08-28 04:28:19 +0000 | [diff] [blame] | 1072 | } else if (C.capturesThis()) { |
Eric Christopher | 91a3190 | 2013-01-16 01:22:32 +0000 | [diff] [blame] | 1073 | // TODO: Need to handle 'this' in some way by probably renaming the |
| 1074 | // this of the lambda class and having a field member of 'this' or |
| 1075 | // by using AT_object_pointer for the function and having that be |
| 1076 | // used as 'this' for semantic references. |
Eric Christopher | 91a3190 | 2013-01-16 01:22:32 +0000 | [diff] [blame] | 1077 | FieldDecl *f = *Field; |
Duncan P. N. Exon Smith | 9dd4e4e | 2015-04-29 16:40:08 +0000 | [diff] [blame] | 1078 | llvm::DIFile *VUnit = getOrCreateFile(f->getLocation()); |
Eric Christopher | 91a3190 | 2013-01-16 01:22:32 +0000 | [diff] [blame] | 1079 | QualType type = f->getType(); |
Duncan P. N. Exon Smith | 9dd4e4e | 2015-04-29 16:40:08 +0000 | [diff] [blame] | 1080 | llvm::DIType *fieldType = createFieldType( |
David Majnemer | b4b671e | 2016-06-30 03:01:59 +0000 | [diff] [blame] | 1081 | "this", type, f->getLocation(), f->getAccess(), |
Eric Christopher | e7b87e5 | 2014-10-26 23:40:33 +0000 | [diff] [blame] | 1082 | layout.getFieldOffset(fieldno), VUnit, RecordTy, CXXDecl); |
Eric Christopher | 91a3190 | 2013-01-16 01:22:32 +0000 | [diff] [blame] | 1083 | |
| 1084 | elements.push_back(fieldType); |
| 1085 | } |
| 1086 | } |
| 1087 | } |
| 1088 | |
Duncan P. N. Exon Smith | 9dd4e4e | 2015-04-29 16:40:08 +0000 | [diff] [blame] | 1089 | llvm::DIDerivedType * |
| 1090 | CGDebugInfo::CreateRecordStaticField(const VarDecl *Var, llvm::DIType *RecordTy, |
Duncan P. N. Exon Smith | c09c548 | 2015-04-20 21:17:26 +0000 | [diff] [blame] | 1091 | const RecordDecl *RD) { |
Eric Christopher | 91a3190 | 2013-01-16 01:22:32 +0000 | [diff] [blame] | 1092 | // Create the descriptor for the static variable, with or without |
| 1093 | // constant initializers. |
David Blaikie | 8e707bb | 2014-10-14 22:22:17 +0000 | [diff] [blame] | 1094 | Var = Var->getCanonicalDecl(); |
Duncan P. N. Exon Smith | 9dd4e4e | 2015-04-29 16:40:08 +0000 | [diff] [blame] | 1095 | llvm::DIFile *VUnit = getOrCreateFile(Var->getLocation()); |
| 1096 | llvm::DIType *VTy = getOrCreateType(Var->getType(), VUnit); |
Eric Christopher | 91a3190 | 2013-01-16 01:22:32 +0000 | [diff] [blame] | 1097 | |
Eric Christopher | 91a3190 | 2013-01-16 01:22:32 +0000 | [diff] [blame] | 1098 | unsigned LineNumber = getLineNumber(Var->getLocation()); |
| 1099 | StringRef VName = Var->getName(); |
Craig Topper | 8a13c41 | 2014-05-21 05:09:00 +0000 | [diff] [blame] | 1100 | llvm::Constant *C = nullptr; |
Eric Christopher | 91a3190 | 2013-01-16 01:22:32 +0000 | [diff] [blame] | 1101 | if (Var->getInit()) { |
| 1102 | const APValue *Value = Var->evaluateValue(); |
David Blaikie | d42917f | 2013-01-20 01:19:17 +0000 | [diff] [blame] | 1103 | if (Value) { |
| 1104 | if (Value->isInt()) |
| 1105 | C = llvm::ConstantInt::get(CGM.getLLVMContext(), Value->getInt()); |
| 1106 | if (Value->isFloat()) |
| 1107 | C = llvm::ConstantFP::get(CGM.getLLVMContext(), Value->getFloat()); |
| 1108 | } |
Eric Christopher | 91a3190 | 2013-01-16 01:22:32 +0000 | [diff] [blame] | 1109 | } |
| 1110 | |
Leny Kholodov | 80c047d | 2016-09-06 10:48:04 +0000 | [diff] [blame] | 1111 | llvm::DINode::DIFlags Flags = getAccessFlag(Var->getAccess(), RD); |
Victor Leschuk | a7ece03 | 2016-10-20 00:13:19 +0000 | [diff] [blame] | 1112 | auto Align = getDeclAlignIfRequired(Var, CGM.getContext()); |
Duncan P. N. Exon Smith | 9dd4e4e | 2015-04-29 16:40:08 +0000 | [diff] [blame] | 1113 | llvm::DIDerivedType *GV = DBuilder.createStaticMemberType( |
Victor Leschuk | a7ece03 | 2016-10-20 00:13:19 +0000 | [diff] [blame] | 1114 | RecordTy, VName, VUnit, LineNumber, VTy, Flags, C, Align); |
Duncan P. N. Exon Smith | fb49491 | 2014-12-09 18:39:32 +0000 | [diff] [blame] | 1115 | StaticDataMemberCache[Var->getCanonicalDecl()].reset(GV); |
David Blaikie | ae01946 | 2013-08-15 22:50:29 +0000 | [diff] [blame] | 1116 | return GV; |
Eric Christopher | 91a3190 | 2013-01-16 01:22:32 +0000 | [diff] [blame] | 1117 | } |
| 1118 | |
Eric Christopher | e7b87e5 | 2014-10-26 23:40:33 +0000 | [diff] [blame] | 1119 | void CGDebugInfo::CollectRecordNormalField( |
Duncan P. N. Exon Smith | 9dd4e4e | 2015-04-29 16:40:08 +0000 | [diff] [blame] | 1120 | const FieldDecl *field, uint64_t OffsetInBits, llvm::DIFile *tunit, |
| 1121 | SmallVectorImpl<llvm::Metadata *> &elements, llvm::DIType *RecordTy, |
Eric Christopher | e7b87e5 | 2014-10-26 23:40:33 +0000 | [diff] [blame] | 1122 | const RecordDecl *RD) { |
Eric Christopher | 91a3190 | 2013-01-16 01:22:32 +0000 | [diff] [blame] | 1123 | StringRef name = field->getName(); |
| 1124 | QualType type = field->getType(); |
| 1125 | |
| 1126 | // Ignore unnamed fields unless they're anonymous structs/unions. |
| 1127 | if (name.empty() && !type->isRecordType()) |
| 1128 | return; |
| 1129 | |
David Majnemer | b4b671e | 2016-06-30 03:01:59 +0000 | [diff] [blame] | 1130 | llvm::DIType *FieldType; |
Eric Christopher | 91a3190 | 2013-01-16 01:22:32 +0000 | [diff] [blame] | 1131 | if (field->isBitField()) { |
David Majnemer | b4b671e | 2016-06-30 03:01:59 +0000 | [diff] [blame] | 1132 | FieldType = createBitFieldType(field, RecordTy, RD); |
| 1133 | } else { |
Victor Leschuk | a7ece03 | 2016-10-20 00:13:19 +0000 | [diff] [blame] | 1134 | auto Align = getDeclAlignIfRequired(field, CGM.getContext()); |
David Majnemer | b4b671e | 2016-06-30 03:01:59 +0000 | [diff] [blame] | 1135 | FieldType = |
| 1136 | createFieldType(name, type, field->getLocation(), field->getAccess(), |
Victor Leschuk | a7ece03 | 2016-10-20 00:13:19 +0000 | [diff] [blame] | 1137 | OffsetInBits, Align, tunit, RecordTy, RD); |
Eric Christopher | 91a3190 | 2013-01-16 01:22:32 +0000 | [diff] [blame] | 1138 | } |
| 1139 | |
David Majnemer | b4b671e | 2016-06-30 03:01:59 +0000 | [diff] [blame] | 1140 | elements.push_back(FieldType); |
Eric Christopher | 91a3190 | 2013-01-16 01:22:32 +0000 | [diff] [blame] | 1141 | } |
| 1142 | |
Adrian McCarthy | ab1e786 | 2016-07-21 18:43:20 +0000 | [diff] [blame] | 1143 | void CGDebugInfo::CollectRecordNestedRecord( |
| 1144 | const RecordDecl *RD, SmallVectorImpl<llvm::Metadata *> &elements) { |
| 1145 | QualType Ty = CGM.getContext().getTypeDeclType(RD); |
Reid Kleckner | 755220b | 2016-08-01 18:56:13 +0000 | [diff] [blame] | 1146 | // Injected class names are not considered nested records. |
| 1147 | if (isa<InjectedClassNameType>(Ty)) |
| 1148 | return; |
Adrian McCarthy | ab1e786 | 2016-07-21 18:43:20 +0000 | [diff] [blame] | 1149 | SourceLocation Loc = RD->getLocation(); |
| 1150 | llvm::DIType *nestedType = getOrCreateType(Ty, getOrCreateFile(Loc)); |
| 1151 | elements.push_back(nestedType); |
| 1152 | } |
| 1153 | |
Duncan P. N. Exon Smith | fb49491 | 2014-12-09 18:39:32 +0000 | [diff] [blame] | 1154 | void CGDebugInfo::CollectRecordFields( |
Duncan P. N. Exon Smith | 9dd4e4e | 2015-04-29 16:40:08 +0000 | [diff] [blame] | 1155 | const RecordDecl *record, llvm::DIFile *tunit, |
Duncan P. N. Exon Smith | fb49491 | 2014-12-09 18:39:32 +0000 | [diff] [blame] | 1156 | SmallVectorImpl<llvm::Metadata *> &elements, |
Duncan P. N. Exon Smith | 9dd4e4e | 2015-04-29 16:40:08 +0000 | [diff] [blame] | 1157 | llvm::DICompositeType *RecordTy) { |
David Majnemer | 58ed0f3 | 2016-07-17 00:39:12 +0000 | [diff] [blame] | 1158 | const auto *CXXDecl = dyn_cast<CXXRecordDecl>(record); |
Guy Benyei | 11169dd | 2012-12-18 14:30:41 +0000 | [diff] [blame] | 1159 | |
Eric Christopher | 91a3190 | 2013-01-16 01:22:32 +0000 | [diff] [blame] | 1160 | if (CXXDecl && CXXDecl->isLambda()) |
| 1161 | CollectRecordLambdaFields(CXXDecl, elements, RecordTy); |
| 1162 | else { |
| 1163 | const ASTRecordLayout &layout = CGM.getContext().getASTRecordLayout(record); |
Guy Benyei | 11169dd | 2012-12-18 14:30:41 +0000 | [diff] [blame] | 1164 | |
Adrian McCarthy | ab1e786 | 2016-07-21 18:43:20 +0000 | [diff] [blame] | 1165 | // Debug info for nested records is included in the member list only for |
| 1166 | // CodeView. |
| 1167 | bool IncludeNestedRecords = CGM.getCodeGenOpts().EmitCodeView; |
| 1168 | |
Eric Christopher | 91a3190 | 2013-01-16 01:22:32 +0000 | [diff] [blame] | 1169 | // Field number for non-static fields. |
Eric Christopher | 0f759437 | 2013-01-04 17:59:07 +0000 | [diff] [blame] | 1170 | unsigned fieldNo = 0; |
Eric Christopher | 91a3190 | 2013-01-16 01:22:32 +0000 | [diff] [blame] | 1171 | |
Eric Christopher | 91a3190 | 2013-01-16 01:22:32 +0000 | [diff] [blame] | 1172 | // Static and non-static members should appear in the same order as |
| 1173 | // the corresponding declarations in the source program. |
Aaron Ballman | 629afae | 2014-03-07 19:56:05 +0000 | [diff] [blame] | 1174 | for (const auto *I : record->decls()) |
| 1175 | if (const auto *V = dyn_cast<VarDecl>(I)) { |
Paul Robinson | b17327d | 2016-04-27 17:37:12 +0000 | [diff] [blame] | 1176 | if (V->hasAttr<NoDebugAttr>()) |
| 1177 | continue; |
David Blaikie | ce76304 | 2013-08-20 21:49:21 +0000 | [diff] [blame] | 1178 | // Reuse the existing static member declaration if one exists |
Duncan P. N. Exon Smith | fb49491 | 2014-12-09 18:39:32 +0000 | [diff] [blame] | 1179 | auto MI = StaticDataMemberCache.find(V->getCanonicalDecl()); |
David Blaikie | ce76304 | 2013-08-20 21:49:21 +0000 | [diff] [blame] | 1180 | if (MI != StaticDataMemberCache.end()) { |
| 1181 | assert(MI->second && |
| 1182 | "Static data member declaration should still exist"); |
Duncan P. N. Exon Smith | ac346ba | 2015-07-24 18:05:58 +0000 | [diff] [blame] | 1183 | elements.push_back(MI->second); |
Adrian Prantl | 21361fb | 2014-08-29 22:44:27 +0000 | [diff] [blame] | 1184 | } else { |
| 1185 | auto Field = CreateRecordStaticField(V, RecordTy, record); |
| 1186 | elements.push_back(Field); |
| 1187 | } |
Aaron Ballman | 629afae | 2014-03-07 19:56:05 +0000 | [diff] [blame] | 1188 | } else if (const auto *field = dyn_cast<FieldDecl>(I)) { |
Eric Christopher | e7b87e5 | 2014-10-26 23:40:33 +0000 | [diff] [blame] | 1189 | CollectRecordNormalField(field, layout.getFieldOffset(fieldNo), tunit, |
| 1190 | elements, RecordTy, record); |
Eric Christopher | 91a3190 | 2013-01-16 01:22:32 +0000 | [diff] [blame] | 1191 | |
| 1192 | // Bump field number for next field. |
| 1193 | ++fieldNo; |
Adrian McCarthy | ab1e786 | 2016-07-21 18:43:20 +0000 | [diff] [blame] | 1194 | } else if (const auto *nestedRec = dyn_cast<CXXRecordDecl>(I)) |
| 1195 | if (IncludeNestedRecords && !nestedRec->isImplicit() && |
| 1196 | nestedRec->getDeclContext() == record) |
| 1197 | CollectRecordNestedRecord(nestedRec, elements); |
Guy Benyei | 11169dd | 2012-12-18 14:30:41 +0000 | [diff] [blame] | 1198 | } |
| 1199 | } |
| 1200 | |
Duncan P. N. Exon Smith | 9dd4e4e | 2015-04-29 16:40:08 +0000 | [diff] [blame] | 1201 | llvm::DISubroutineType * |
Guy Benyei | 11169dd | 2012-12-18 14:30:41 +0000 | [diff] [blame] | 1202 | CGDebugInfo::getOrCreateMethodType(const CXXMethodDecl *Method, |
Duncan P. N. Exon Smith | 9dd4e4e | 2015-04-29 16:40:08 +0000 | [diff] [blame] | 1203 | llvm::DIFile *Unit) { |
David Blaikie | 7eb0685 | 2013-01-07 23:06:35 +0000 | [diff] [blame] | 1204 | const FunctionProtoType *Func = Method->getType()->getAs<FunctionProtoType>(); |
David Blaikie | 2aaf065 | 2013-01-07 22:24:59 +0000 | [diff] [blame] | 1205 | if (Method->isStatic()) |
Duncan P. N. Exon Smith | 9dd4e4e | 2015-04-29 16:40:08 +0000 | [diff] [blame] | 1206 | return cast_or_null<llvm::DISubroutineType>( |
Duncan P. N. Exon Smith | c755128 | 2015-04-06 23:21:33 +0000 | [diff] [blame] | 1207 | getOrCreateType(QualType(Func, 0), Unit)); |
David Blaikie | 7eb0685 | 2013-01-07 23:06:35 +0000 | [diff] [blame] | 1208 | return getOrCreateInstanceMethodType(Method->getThisType(CGM.getContext()), |
| 1209 | Func, Unit); |
| 1210 | } |
David Blaikie | 2aaf065 | 2013-01-07 22:24:59 +0000 | [diff] [blame] | 1211 | |
Duncan P. N. Exon Smith | 9dd4e4e | 2015-04-29 16:40:08 +0000 | [diff] [blame] | 1212 | llvm::DISubroutineType *CGDebugInfo::getOrCreateInstanceMethodType( |
| 1213 | QualType ThisPtr, const FunctionProtoType *Func, llvm::DIFile *Unit) { |
Guy Benyei | 11169dd | 2012-12-18 14:30:41 +0000 | [diff] [blame] | 1214 | // Add "this" pointer. |
Duncan P. N. Exon Smith | 9dd4e4e | 2015-04-29 16:40:08 +0000 | [diff] [blame] | 1215 | llvm::DITypeRefArray Args( |
| 1216 | cast<llvm::DISubroutineType>(getOrCreateType(QualType(Func, 0), Unit)) |
Duncan P. N. Exon Smith | c755128 | 2015-04-06 23:21:33 +0000 | [diff] [blame] | 1217 | ->getTypeArray()); |
Duncan P. N. Exon Smith | a98fac6 | 2015-04-07 04:14:45 +0000 | [diff] [blame] | 1218 | assert(Args.size() && "Invalid number of arguments!"); |
Guy Benyei | 11169dd | 2012-12-18 14:30:41 +0000 | [diff] [blame] | 1219 | |
Duncan P. N. Exon Smith | fb49491 | 2014-12-09 18:39:32 +0000 | [diff] [blame] | 1220 | SmallVector<llvm::Metadata *, 16> Elts; |
Guy Benyei | 11169dd | 2012-12-18 14:30:41 +0000 | [diff] [blame] | 1221 | |
| 1222 | // First element is always return type. For 'void' functions it is NULL. |
Duncan P. N. Exon Smith | 3732858 | 2015-04-07 18:41:26 +0000 | [diff] [blame] | 1223 | Elts.push_back(Args[0]); |
Guy Benyei | 11169dd | 2012-12-18 14:30:41 +0000 | [diff] [blame] | 1224 | |
David Blaikie | 2aaf065 | 2013-01-07 22:24:59 +0000 | [diff] [blame] | 1225 | // "this" pointer is always first argument. |
David Blaikie | 7eb0685 | 2013-01-07 23:06:35 +0000 | [diff] [blame] | 1226 | const CXXRecordDecl *RD = ThisPtr->getPointeeCXXRecordDecl(); |
David Blaikie | 2aaf065 | 2013-01-07 22:24:59 +0000 | [diff] [blame] | 1227 | if (isa<ClassTemplateSpecializationDecl>(RD)) { |
| 1228 | // Create pointer type directly in this case. |
| 1229 | const PointerType *ThisPtrTy = cast<PointerType>(ThisPtr); |
| 1230 | QualType PointeeTy = ThisPtrTy->getPointeeType(); |
| 1231 | unsigned AS = CGM.getContext().getTargetAddressSpace(PointeeTy); |
John McCall | c8e0170 | 2013-04-16 22:48:15 +0000 | [diff] [blame] | 1232 | uint64_t Size = CGM.getTarget().getPointerWidth(AS); |
Victor Leschuk | a7ece03 | 2016-10-20 00:13:19 +0000 | [diff] [blame] | 1233 | auto Align = getTypeAlignIfRequired(ThisPtrTy, CGM.getContext()); |
Duncan P. N. Exon Smith | 9dd4e4e | 2015-04-29 16:40:08 +0000 | [diff] [blame] | 1234 | llvm::DIType *PointeeType = getOrCreateType(PointeeTy, Unit); |
| 1235 | llvm::DIType *ThisPtrType = |
Eric Christopher | e7b87e5 | 2014-10-26 23:40:33 +0000 | [diff] [blame] | 1236 | DBuilder.createPointerType(PointeeType, Size, Align); |
Duncan P. N. Exon Smith | fb49491 | 2014-12-09 18:39:32 +0000 | [diff] [blame] | 1237 | TypeCache[ThisPtr.getAsOpaquePtr()].reset(ThisPtrType); |
David Blaikie | 2aaf065 | 2013-01-07 22:24:59 +0000 | [diff] [blame] | 1238 | // TODO: This and the artificial type below are misleading, the |
| 1239 | // types aren't artificial the argument is, but the current |
| 1240 | // metadata doesn't represent that. |
| 1241 | ThisPtrType = DBuilder.createObjectPointerType(ThisPtrType); |
| 1242 | Elts.push_back(ThisPtrType); |
| 1243 | } else { |
Duncan P. N. Exon Smith | 9dd4e4e | 2015-04-29 16:40:08 +0000 | [diff] [blame] | 1244 | llvm::DIType *ThisPtrType = getOrCreateType(ThisPtr, Unit); |
Duncan P. N. Exon Smith | fb49491 | 2014-12-09 18:39:32 +0000 | [diff] [blame] | 1245 | TypeCache[ThisPtr.getAsOpaquePtr()].reset(ThisPtrType); |
David Blaikie | 2aaf065 | 2013-01-07 22:24:59 +0000 | [diff] [blame] | 1246 | ThisPtrType = DBuilder.createObjectPointerType(ThisPtrType); |
| 1247 | Elts.push_back(ThisPtrType); |
Guy Benyei | 11169dd | 2012-12-18 14:30:41 +0000 | [diff] [blame] | 1248 | } |
| 1249 | |
| 1250 | // Copy rest of the arguments. |
Duncan P. N. Exon Smith | a98fac6 | 2015-04-07 04:14:45 +0000 | [diff] [blame] | 1251 | for (unsigned i = 1, e = Args.size(); i != e; ++i) |
| 1252 | Elts.push_back(Args[i]); |
Guy Benyei | 11169dd | 2012-12-18 14:30:41 +0000 | [diff] [blame] | 1253 | |
Duncan P. N. Exon Smith | 9dd4e4e | 2015-04-29 16:40:08 +0000 | [diff] [blame] | 1254 | llvm::DITypeRefArray EltTypeArray = DBuilder.getOrCreateTypeArray(Elts); |
Guy Benyei | 11169dd | 2012-12-18 14:30:41 +0000 | [diff] [blame] | 1255 | |
Leny Kholodov | 80c047d | 2016-09-06 10:48:04 +0000 | [diff] [blame] | 1256 | llvm::DINode::DIFlags Flags = llvm::DINode::FlagZero; |
Adrian Prantl | 0630eb7 | 2013-12-18 21:48:18 +0000 | [diff] [blame] | 1257 | if (Func->getExtProtoInfo().RefQualifier == RQ_LValue) |
Duncan P. N. Exon Smith | 9dd4e4e | 2015-04-29 16:40:08 +0000 | [diff] [blame] | 1258 | Flags |= llvm::DINode::FlagLValueReference; |
Adrian Prantl | 0630eb7 | 2013-12-18 21:48:18 +0000 | [diff] [blame] | 1259 | if (Func->getExtProtoInfo().RefQualifier == RQ_RValue) |
Duncan P. N. Exon Smith | 9dd4e4e | 2015-04-29 16:40:08 +0000 | [diff] [blame] | 1260 | Flags |= llvm::DINode::FlagRValueReference; |
Adrian Prantl | 0630eb7 | 2013-12-18 21:48:18 +0000 | [diff] [blame] | 1261 | |
Reid Kleckner | f00f803 | 2016-06-08 20:41:54 +0000 | [diff] [blame] | 1262 | return DBuilder.createSubroutineType(EltTypeArray, Flags, |
| 1263 | getDwarfCC(Func->getCallConv())); |
Guy Benyei | 11169dd | 2012-12-18 14:30:41 +0000 | [diff] [blame] | 1264 | } |
| 1265 | |
Eric Christopher | b2a008c | 2013-05-16 00:45:12 +0000 | [diff] [blame] | 1266 | /// isFunctionLocalClass - Return true if CXXRecordDecl is defined |
Guy Benyei | 11169dd | 2012-12-18 14:30:41 +0000 | [diff] [blame] | 1267 | /// inside a function. |
| 1268 | static bool isFunctionLocalClass(const CXXRecordDecl *RD) { |
David Majnemer | 58ed0f3 | 2016-07-17 00:39:12 +0000 | [diff] [blame] | 1269 | if (const auto *NRD = dyn_cast<CXXRecordDecl>(RD->getDeclContext())) |
Guy Benyei | 11169dd | 2012-12-18 14:30:41 +0000 | [diff] [blame] | 1270 | return isFunctionLocalClass(NRD); |
| 1271 | if (isa<FunctionDecl>(RD->getDeclContext())) |
| 1272 | return true; |
| 1273 | return false; |
| 1274 | } |
| 1275 | |
Duncan P. N. Exon Smith | 9dd4e4e | 2015-04-29 16:40:08 +0000 | [diff] [blame] | 1276 | llvm::DISubprogram *CGDebugInfo::CreateCXXMemberFunction( |
| 1277 | const CXXMethodDecl *Method, llvm::DIFile *Unit, llvm::DIType *RecordTy) { |
Eric Christopher | b2a008c | 2013-05-16 00:45:12 +0000 | [diff] [blame] | 1278 | bool IsCtorOrDtor = |
Eric Christopher | e7b87e5 | 2014-10-26 23:40:33 +0000 | [diff] [blame] | 1279 | isa<CXXConstructorDecl>(Method) || isa<CXXDestructorDecl>(Method); |
Eric Christopher | b2a008c | 2013-05-16 00:45:12 +0000 | [diff] [blame] | 1280 | |
Guy Benyei | 11169dd | 2012-12-18 14:30:41 +0000 | [diff] [blame] | 1281 | StringRef MethodName = getFunctionName(Method); |
Duncan P. N. Exon Smith | 9dd4e4e | 2015-04-29 16:40:08 +0000 | [diff] [blame] | 1282 | llvm::DISubroutineType *MethodTy = getOrCreateMethodType(Method, Unit); |
Guy Benyei | 11169dd | 2012-12-18 14:30:41 +0000 | [diff] [blame] | 1283 | |
| 1284 | // Since a single ctor/dtor corresponds to multiple functions, it doesn't |
| 1285 | // make sense to give a single ctor/dtor a linkage name. |
| 1286 | StringRef MethodLinkageName; |
David Blaikie | 7164767 | 2016-04-12 21:22:48 +0000 | [diff] [blame] | 1287 | // FIXME: 'isFunctionLocalClass' seems like an arbitrary/unintentional |
| 1288 | // property to use here. It may've been intended to model "is non-external |
| 1289 | // type" but misses cases of non-function-local but non-external classes such |
| 1290 | // as those in anonymous namespaces as well as the reverse - external types |
| 1291 | // that are function local, such as those in (non-local) inline functions. |
Guy Benyei | 11169dd | 2012-12-18 14:30:41 +0000 | [diff] [blame] | 1292 | if (!IsCtorOrDtor && !isFunctionLocalClass(Method->getParent())) |
| 1293 | MethodLinkageName = CGM.getMangledName(Method); |
| 1294 | |
| 1295 | // Get the location for the method. |
Duncan P. N. Exon Smith | 9dd4e4e | 2015-04-29 16:40:08 +0000 | [diff] [blame] | 1296 | llvm::DIFile *MethodDefUnit = nullptr; |
David Blaikie | 7fceebf | 2013-08-19 03:37:48 +0000 | [diff] [blame] | 1297 | unsigned MethodLine = 0; |
| 1298 | if (!Method->isImplicit()) { |
| 1299 | MethodDefUnit = getOrCreateFile(Method->getLocation()); |
| 1300 | MethodLine = getLineNumber(Method->getLocation()); |
| 1301 | } |
Guy Benyei | 11169dd | 2012-12-18 14:30:41 +0000 | [diff] [blame] | 1302 | |
| 1303 | // Collect virtual method info. |
Duncan P. N. Exon Smith | 9dd4e4e | 2015-04-29 16:40:08 +0000 | [diff] [blame] | 1304 | llvm::DIType *ContainingType = nullptr; |
Eric Christopher | b2a008c | 2013-05-16 00:45:12 +0000 | [diff] [blame] | 1305 | unsigned Virtuality = 0; |
Guy Benyei | 11169dd | 2012-12-18 14:30:41 +0000 | [diff] [blame] | 1306 | unsigned VIndex = 0; |
Leny Kholodov | 80c047d | 2016-09-06 10:48:04 +0000 | [diff] [blame] | 1307 | llvm::DINode::DIFlags Flags = llvm::DINode::FlagZero; |
Reid Kleckner | 0358cbf | 2016-07-01 02:41:25 +0000 | [diff] [blame] | 1308 | int ThisAdjustment = 0; |
Eric Christopher | b2a008c | 2013-05-16 00:45:12 +0000 | [diff] [blame] | 1309 | |
Guy Benyei | 11169dd | 2012-12-18 14:30:41 +0000 | [diff] [blame] | 1310 | if (Method->isVirtual()) { |
| 1311 | if (Method->isPure()) |
| 1312 | Virtuality = llvm::dwarf::DW_VIRTUALITY_pure_virtual; |
| 1313 | else |
| 1314 | Virtuality = llvm::dwarf::DW_VIRTUALITY_virtual; |
Eric Christopher | b2a008c | 2013-05-16 00:45:12 +0000 | [diff] [blame] | 1315 | |
Reid Kleckner | 216d0a1 | 2016-06-16 20:08:51 +0000 | [diff] [blame] | 1316 | if (CGM.getTarget().getCXXABI().isItaniumFamily()) { |
| 1317 | // It doesn't make sense to give a virtual destructor a vtable index, |
| 1318 | // since a single destructor has two entries in the vtable. |
| 1319 | if (!isa<CXXDestructorDecl>(Method)) |
| 1320 | VIndex = CGM.getItaniumVTableContext().getMethodVTableIndex(Method); |
| 1321 | } else { |
| 1322 | // Emit MS ABI vftable information. There is only one entry for the |
| 1323 | // deleting dtor. |
| 1324 | const auto *DD = dyn_cast<CXXDestructorDecl>(Method); |
| 1325 | GlobalDecl GD = DD ? GlobalDecl(DD, Dtor_Deleting) : GlobalDecl(Method); |
| 1326 | MicrosoftVTableContext::MethodVFTableLocation ML = |
| 1327 | CGM.getMicrosoftVTableContext().getMethodVFTableLocation(GD); |
| 1328 | VIndex = ML.Index; |
Reid Kleckner | c4871ed | 2016-06-22 18:34:45 +0000 | [diff] [blame] | 1329 | |
| 1330 | // CodeView only records the vftable offset in the class that introduces |
| 1331 | // the virtual method. This is possible because, unlike Itanium, the MS |
| 1332 | // C++ ABI does not include all virtual methods from non-primary bases in |
| 1333 | // the vtable for the most derived class. For example, if C inherits from |
| 1334 | // A and B, C's primary vftable will not include B's virtual methods. |
| 1335 | if (Method->begin_overridden_methods() == Method->end_overridden_methods()) |
| 1336 | Flags |= llvm::DINode::FlagIntroducedVirtual; |
| 1337 | |
Reid Kleckner | 0358cbf | 2016-07-01 02:41:25 +0000 | [diff] [blame] | 1338 | // The 'this' adjustment accounts for both the virtual and non-virtual |
| 1339 | // portions of the adjustment. Presumably the debugger only uses it when |
| 1340 | // it knows the dynamic type of an object. |
| 1341 | ThisAdjustment = CGM.getCXXABI() |
| 1342 | .getVirtualFunctionPrologueThisAdjustment(GD) |
| 1343 | .getQuantity(); |
Reid Kleckner | 216d0a1 | 2016-06-16 20:08:51 +0000 | [diff] [blame] | 1344 | } |
Guy Benyei | 11169dd | 2012-12-18 14:30:41 +0000 | [diff] [blame] | 1345 | ContainingType = RecordTy; |
| 1346 | } |
| 1347 | |
Guy Benyei | 11169dd | 2012-12-18 14:30:41 +0000 | [diff] [blame] | 1348 | if (Method->isImplicit()) |
Duncan P. N. Exon Smith | 9dd4e4e | 2015-04-29 16:40:08 +0000 | [diff] [blame] | 1349 | Flags |= llvm::DINode::FlagArtificial; |
Adrian Prantl | 21361fb | 2014-08-29 22:44:27 +0000 | [diff] [blame] | 1350 | Flags |= getAccessFlag(Method->getAccess(), Method->getParent()); |
David Majnemer | 58ed0f3 | 2016-07-17 00:39:12 +0000 | [diff] [blame] | 1351 | if (const auto *CXXC = dyn_cast<CXXConstructorDecl>(Method)) { |
Guy Benyei | 11169dd | 2012-12-18 14:30:41 +0000 | [diff] [blame] | 1352 | if (CXXC->isExplicit()) |
Duncan P. N. Exon Smith | 9dd4e4e | 2015-04-29 16:40:08 +0000 | [diff] [blame] | 1353 | Flags |= llvm::DINode::FlagExplicit; |
David Majnemer | 58ed0f3 | 2016-07-17 00:39:12 +0000 | [diff] [blame] | 1354 | } else if (const auto *CXXC = dyn_cast<CXXConversionDecl>(Method)) { |
Guy Benyei | 11169dd | 2012-12-18 14:30:41 +0000 | [diff] [blame] | 1355 | if (CXXC->isExplicit()) |
Duncan P. N. Exon Smith | 9dd4e4e | 2015-04-29 16:40:08 +0000 | [diff] [blame] | 1356 | Flags |= llvm::DINode::FlagExplicit; |
Guy Benyei | 11169dd | 2012-12-18 14:30:41 +0000 | [diff] [blame] | 1357 | } |
| 1358 | if (Method->hasPrototype()) |
Duncan P. N. Exon Smith | 9dd4e4e | 2015-04-29 16:40:08 +0000 | [diff] [blame] | 1359 | Flags |= llvm::DINode::FlagPrototyped; |
Adrian Prantl | 0630eb7 | 2013-12-18 21:48:18 +0000 | [diff] [blame] | 1360 | if (Method->getRefQualifier() == RQ_LValue) |
Duncan P. N. Exon Smith | 9dd4e4e | 2015-04-29 16:40:08 +0000 | [diff] [blame] | 1361 | Flags |= llvm::DINode::FlagLValueReference; |
Adrian Prantl | 0630eb7 | 2013-12-18 21:48:18 +0000 | [diff] [blame] | 1362 | if (Method->getRefQualifier() == RQ_RValue) |
Duncan P. N. Exon Smith | 9dd4e4e | 2015-04-29 16:40:08 +0000 | [diff] [blame] | 1363 | Flags |= llvm::DINode::FlagRValueReference; |
Guy Benyei | 11169dd | 2012-12-18 14:30:41 +0000 | [diff] [blame] | 1364 | |
Duncan P. N. Exon Smith | 9dd4e4e | 2015-04-29 16:40:08 +0000 | [diff] [blame] | 1365 | llvm::DINodeArray TParamsArray = CollectFunctionTemplateParams(Method, Unit); |
| 1366 | llvm::DISubprogram *SP = DBuilder.createMethod( |
Eric Christopher | e7b87e5 | 2014-10-26 23:40:33 +0000 | [diff] [blame] | 1367 | RecordTy, MethodName, MethodLinkageName, MethodDefUnit, MethodLine, |
Reid Kleckner | 0358cbf | 2016-07-01 02:41:25 +0000 | [diff] [blame] | 1368 | MethodTy, /*isLocalToUnit=*/false, /*isDefinition=*/false, Virtuality, |
| 1369 | VIndex, ThisAdjustment, ContainingType, Flags, CGM.getLangOpts().Optimize, |
| 1370 | TParamsArray.get()); |
Eric Christopher | b2a008c | 2013-05-16 00:45:12 +0000 | [diff] [blame] | 1371 | |
Duncan P. N. Exon Smith | fb49491 | 2014-12-09 18:39:32 +0000 | [diff] [blame] | 1372 | SPCache[Method->getCanonicalDecl()].reset(SP); |
Guy Benyei | 11169dd | 2012-12-18 14:30:41 +0000 | [diff] [blame] | 1373 | |
| 1374 | return SP; |
| 1375 | } |
| 1376 | |
Eric Christopher | e7b87e5 | 2014-10-26 23:40:33 +0000 | [diff] [blame] | 1377 | void CGDebugInfo::CollectCXXMemberFunctions( |
Duncan P. N. Exon Smith | 9dd4e4e | 2015-04-29 16:40:08 +0000 | [diff] [blame] | 1378 | const CXXRecordDecl *RD, llvm::DIFile *Unit, |
| 1379 | SmallVectorImpl<llvm::Metadata *> &EltTys, llvm::DIType *RecordTy) { |
Guy Benyei | 11169dd | 2012-12-18 14:30:41 +0000 | [diff] [blame] | 1380 | |
| 1381 | // Since we want more than just the individual member decls if we |
| 1382 | // have templated functions iterate over every declaration to gather |
| 1383 | // the functions. |
Eric Christopher | e7b87e5 | 2014-10-26 23:40:33 +0000 | [diff] [blame] | 1384 | for (const auto *I : RD->decls()) { |
David Blaikie | fd58072 | 2014-10-06 05:18:55 +0000 | [diff] [blame] | 1385 | const auto *Method = dyn_cast<CXXMethodDecl>(I); |
| 1386 | // If the member is implicit, don't add it to the member list. This avoids |
| 1387 | // the member being added to type units by LLVM, while still allowing it |
| 1388 | // to be emitted into the type declaration/reference inside the compile |
| 1389 | // unit. |
Paul Robinson | 6a7511b | 2015-06-25 17:50:43 +0000 | [diff] [blame] | 1390 | // Ditto 'nodebug' methods, for consistency with CodeGenFunction.cpp. |
David Blaikie | 6dddfe3 | 2014-10-06 05:52:27 +0000 | [diff] [blame] | 1391 | // FIXME: Handle Using(Shadow?)Decls here to create |
| 1392 | // DW_TAG_imported_declarations inside the class for base decls brought into |
| 1393 | // derived classes. GDB doesn't seem to notice/leverage these when I tried |
| 1394 | // it, so I'm not rushing to fix this. (GCC seems to produce them, if |
| 1395 | // referenced) |
Paul Robinson | 6a7511b | 2015-06-25 17:50:43 +0000 | [diff] [blame] | 1396 | if (!Method || Method->isImplicit() || Method->hasAttr<NoDebugAttr>()) |
David Blaikie | fd58072 | 2014-10-06 05:18:55 +0000 | [diff] [blame] | 1397 | continue; |
David Blaikie | 42edade | 2014-11-11 20:44:45 +0000 | [diff] [blame] | 1398 | |
| 1399 | if (Method->getType()->getAs<FunctionProtoType>()->getContainedAutoType()) |
| 1400 | continue; |
| 1401 | |
David Blaikie | fd58072 | 2014-10-06 05:18:55 +0000 | [diff] [blame] | 1402 | // Reuse the existing member function declaration if it exists. |
| 1403 | // It may be associated with the declaration of the type & should be |
| 1404 | // reused as we're building the definition. |
| 1405 | // |
| 1406 | // This situation can arise in the vtable-based debug info reduction where |
| 1407 | // implicit members are emitted in a non-vtable TU. |
| 1408 | auto MI = SPCache.find(Method->getCanonicalDecl()); |
| 1409 | EltTys.push_back(MI == SPCache.end() |
| 1410 | ? CreateCXXMemberFunction(Method, Unit, RecordTy) |
Duncan P. N. Exon Smith | fb49491 | 2014-12-09 18:39:32 +0000 | [diff] [blame] | 1411 | : static_cast<llvm::Metadata *>(MI->second)); |
Guy Benyei | 11169dd | 2012-12-18 14:30:41 +0000 | [diff] [blame] | 1412 | } |
Eric Christopher | b2a008c | 2013-05-16 00:45:12 +0000 | [diff] [blame] | 1413 | } |
Guy Benyei | 11169dd | 2012-12-18 14:30:41 +0000 | [diff] [blame] | 1414 | |
Duncan P. N. Exon Smith | 9dd4e4e | 2015-04-29 16:40:08 +0000 | [diff] [blame] | 1415 | void CGDebugInfo::CollectCXXBases(const CXXRecordDecl *RD, llvm::DIFile *Unit, |
Duncan P. N. Exon Smith | fb49491 | 2014-12-09 18:39:32 +0000 | [diff] [blame] | 1416 | SmallVectorImpl<llvm::Metadata *> &EltTys, |
Duncan P. N. Exon Smith | 9dd4e4e | 2015-04-29 16:40:08 +0000 | [diff] [blame] | 1417 | llvm::DIType *RecordTy) { |
Bob Haarman | dff3673 | 2016-10-25 22:19:32 +0000 | [diff] [blame] | 1418 | llvm::DenseSet<CanonicalDeclPtr<const CXXRecordDecl>> SeenTypes; |
| 1419 | CollectCXXBasesAux(RD, Unit, EltTys, RecordTy, RD->bases(), SeenTypes, |
| 1420 | llvm::DINode::FlagZero); |
Eric Christopher | b2a008c | 2013-05-16 00:45:12 +0000 | [diff] [blame] | 1421 | |
Bob Haarman | dff3673 | 2016-10-25 22:19:32 +0000 | [diff] [blame] | 1422 | // If we are generating CodeView debug info, we also need to emit records for |
| 1423 | // indirect virtual base classes. |
| 1424 | if (CGM.getCodeGenOpts().EmitCodeView) { |
| 1425 | CollectCXXBasesAux(RD, Unit, EltTys, RecordTy, RD->vbases(), SeenTypes, |
| 1426 | llvm::DINode::FlagIndirectVirtualBase); |
| 1427 | } |
| 1428 | } |
| 1429 | |
| 1430 | void CGDebugInfo::CollectCXXBasesAux( |
| 1431 | const CXXRecordDecl *RD, llvm::DIFile *Unit, |
| 1432 | SmallVectorImpl<llvm::Metadata *> &EltTys, llvm::DIType *RecordTy, |
| 1433 | const CXXRecordDecl::base_class_const_range &Bases, |
| 1434 | llvm::DenseSet<CanonicalDeclPtr<const CXXRecordDecl>> &SeenTypes, |
| 1435 | llvm::DINode::DIFlags StartingFlags) { |
| 1436 | const ASTRecordLayout &RL = CGM.getContext().getASTRecordLayout(RD); |
| 1437 | for (const auto &BI : Bases) { |
David Majnemer | 58ed0f3 | 2016-07-17 00:39:12 +0000 | [diff] [blame] | 1438 | const auto *Base = |
Eric Christopher | e7b87e5 | 2014-10-26 23:40:33 +0000 | [diff] [blame] | 1439 | cast<CXXRecordDecl>(BI.getType()->getAs<RecordType>()->getDecl()); |
Bob Haarman | dff3673 | 2016-10-25 22:19:32 +0000 | [diff] [blame] | 1440 | if (!SeenTypes.insert(Base).second) |
| 1441 | continue; |
| 1442 | auto *BaseTy = getOrCreateType(BI.getType(), Unit); |
| 1443 | llvm::DINode::DIFlags BFlags = StartingFlags; |
| 1444 | uint64_t BaseOffset; |
Eric Christopher | b2a008c | 2013-05-16 00:45:12 +0000 | [diff] [blame] | 1445 | |
Aaron Ballman | 574705e | 2014-03-13 15:41:46 +0000 | [diff] [blame] | 1446 | if (BI.isVirtual()) { |
Reid Kleckner | d3b23d6 | 2014-08-07 21:29:25 +0000 | [diff] [blame] | 1447 | if (CGM.getTarget().getCXXABI().isItaniumFamily()) { |
| 1448 | // virtual base offset offset is -ve. The code generator emits dwarf |
| 1449 | // expression where it expects +ve number. |
Eric Christopher | e7b87e5 | 2014-10-26 23:40:33 +0000 | [diff] [blame] | 1450 | BaseOffset = 0 - CGM.getItaniumVTableContext() |
| 1451 | .getVirtualBaseOffsetOffset(RD, Base) |
| 1452 | .getQuantity(); |
Reid Kleckner | d3b23d6 | 2014-08-07 21:29:25 +0000 | [diff] [blame] | 1453 | } else { |
| 1454 | // In the MS ABI, store the vbtable offset, which is analogous to the |
| 1455 | // vbase offset offset in Itanium. |
| 1456 | BaseOffset = |
| 1457 | 4 * CGM.getMicrosoftVTableContext().getVBTableIndex(RD, Base); |
| 1458 | } |
Bob Haarman | dff3673 | 2016-10-25 22:19:32 +0000 | [diff] [blame] | 1459 | BFlags |= llvm::DINode::FlagVirtual; |
Guy Benyei | 11169dd | 2012-12-18 14:30:41 +0000 | [diff] [blame] | 1460 | } else |
| 1461 | BaseOffset = CGM.getContext().toBits(RL.getBaseClassOffset(Base)); |
| 1462 | // FIXME: Inconsistent units for BaseOffset. It is in bytes when |
| 1463 | // BI->isVirtual() and bits when not. |
Eric Christopher | b2a008c | 2013-05-16 00:45:12 +0000 | [diff] [blame] | 1464 | |
Adrian Prantl | 21361fb | 2014-08-29 22:44:27 +0000 | [diff] [blame] | 1465 | BFlags |= getAccessFlag(BI.getAccessSpecifier(), RD); |
Bob Haarman | dff3673 | 2016-10-25 22:19:32 +0000 | [diff] [blame] | 1466 | llvm::DIType *DTy = |
| 1467 | DBuilder.createInheritance(RecordTy, BaseTy, BaseOffset, BFlags); |
Guy Benyei | 11169dd | 2012-12-18 14:30:41 +0000 | [diff] [blame] | 1468 | EltTys.push_back(DTy); |
| 1469 | } |
| 1470 | } |
| 1471 | |
Duncan P. N. Exon Smith | 9dd4e4e | 2015-04-29 16:40:08 +0000 | [diff] [blame] | 1472 | llvm::DINodeArray |
Eric Christopher | e7b87e5 | 2014-10-26 23:40:33 +0000 | [diff] [blame] | 1473 | CGDebugInfo::CollectTemplateParams(const TemplateParameterList *TPList, |
| 1474 | ArrayRef<TemplateArgument> TAList, |
Duncan P. N. Exon Smith | 9dd4e4e | 2015-04-29 16:40:08 +0000 | [diff] [blame] | 1475 | llvm::DIFile *Unit) { |
Duncan P. N. Exon Smith | fb49491 | 2014-12-09 18:39:32 +0000 | [diff] [blame] | 1476 | SmallVector<llvm::Metadata *, 16> TemplateParams; |
Guy Benyei | 11169dd | 2012-12-18 14:30:41 +0000 | [diff] [blame] | 1477 | for (unsigned i = 0, e = TAList.size(); i != e; ++i) { |
| 1478 | const TemplateArgument &TA = TAList[i]; |
David Blaikie | 47c1150 | 2013-06-22 18:59:18 +0000 | [diff] [blame] | 1479 | StringRef Name; |
| 1480 | if (TPList) |
| 1481 | Name = TPList->getParam(i)->getName(); |
David Blaikie | 38079fd | 2013-05-10 21:53:14 +0000 | [diff] [blame] | 1482 | switch (TA.getKind()) { |
| 1483 | case TemplateArgument::Type: { |
Duncan P. N. Exon Smith | 9dd4e4e | 2015-04-29 16:40:08 +0000 | [diff] [blame] | 1484 | llvm::DIType *TTy = getOrCreateType(TA.getAsType(), Unit); |
Duncan P. N. Exon Smith | dadc2b6 | 2015-04-21 18:43:54 +0000 | [diff] [blame] | 1485 | TemplateParams.push_back( |
| 1486 | DBuilder.createTemplateTypeParameter(TheCU, Name, TTy)); |
David Blaikie | 38079fd | 2013-05-10 21:53:14 +0000 | [diff] [blame] | 1487 | } break; |
| 1488 | case TemplateArgument::Integral: { |
Duncan P. N. Exon Smith | 9dd4e4e | 2015-04-29 16:40:08 +0000 | [diff] [blame] | 1489 | llvm::DIType *TTy = getOrCreateType(TA.getIntegralType(), Unit); |
Duncan P. N. Exon Smith | dadc2b6 | 2015-04-21 18:43:54 +0000 | [diff] [blame] | 1490 | TemplateParams.push_back(DBuilder.createTemplateValueParameter( |
| 1491 | TheCU, Name, TTy, |
| 1492 | llvm::ConstantInt::get(CGM.getLLVMContext(), TA.getAsIntegral()))); |
David Blaikie | 38079fd | 2013-05-10 21:53:14 +0000 | [diff] [blame] | 1493 | } break; |
| 1494 | case TemplateArgument::Declaration: { |
| 1495 | const ValueDecl *D = TA.getAsDecl(); |
David Blaikie | b5c7e6a | 2014-10-18 02:21:26 +0000 | [diff] [blame] | 1496 | QualType T = TA.getParamTypeForDecl().getDesugaredType(CGM.getContext()); |
Duncan P. N. Exon Smith | 9dd4e4e | 2015-04-29 16:40:08 +0000 | [diff] [blame] | 1497 | llvm::DIType *TTy = getOrCreateType(T, Unit); |
Duncan P. N. Exon Smith | fb49491 | 2014-12-09 18:39:32 +0000 | [diff] [blame] | 1498 | llvm::Constant *V = nullptr; |
David Blaikie | 1a83db4 | 2014-10-20 18:56:54 +0000 | [diff] [blame] | 1499 | const CXXMethodDecl *MD; |
David Blaikie | 38079fd | 2013-05-10 21:53:14 +0000 | [diff] [blame] | 1500 | // Variable pointer template parameters have a value that is the address |
| 1501 | // of the variable. |
David Blaikie | 952a9b1 | 2014-10-17 18:00:12 +0000 | [diff] [blame] | 1502 | if (const auto *VD = dyn_cast<VarDecl>(D)) |
David Blaikie | 38079fd | 2013-05-10 21:53:14 +0000 | [diff] [blame] | 1503 | V = CGM.GetAddrOfGlobalVar(VD); |
| 1504 | // Member function pointers have special support for building them, though |
| 1505 | // this is currently unsupported in LLVM CodeGen. |
David Blaikie | 1a83db4 | 2014-10-20 18:56:54 +0000 | [diff] [blame] | 1506 | else if ((MD = dyn_cast<CXXMethodDecl>(D)) && MD->isInstance()) |
David Majnemer | e2be95b | 2015-06-23 07:31:01 +0000 | [diff] [blame] | 1507 | V = CGM.getCXXABI().EmitMemberFunctionPointer(MD); |
David Blaikie | 952a9b1 | 2014-10-17 18:00:12 +0000 | [diff] [blame] | 1508 | else if (const auto *FD = dyn_cast<FunctionDecl>(D)) |
David Blaikie | d900f98 | 2013-05-13 06:57:50 +0000 | [diff] [blame] | 1509 | V = CGM.GetAddrOfFunction(FD); |
David Blaikie | 38079fd | 2013-05-10 21:53:14 +0000 | [diff] [blame] | 1510 | // Member data pointers have special handling too to compute the fixed |
| 1511 | // offset within the object. |
David Blaikie | 952a9b1 | 2014-10-17 18:00:12 +0000 | [diff] [blame] | 1512 | else if (const auto *MPT = dyn_cast<MemberPointerType>(T.getTypePtr())) { |
David Blaikie | 38079fd | 2013-05-10 21:53:14 +0000 | [diff] [blame] | 1513 | // These five lines (& possibly the above member function pointer |
| 1514 | // handling) might be able to be refactored to use similar code in |
| 1515 | // CodeGenModule::getMemberPointerConstant |
| 1516 | uint64_t fieldOffset = CGM.getContext().getFieldOffset(D); |
| 1517 | CharUnits chars = |
Eric Christopher | e7b87e5 | 2014-10-26 23:40:33 +0000 | [diff] [blame] | 1518 | CGM.getContext().toCharUnitsFromBits((int64_t)fieldOffset); |
David Blaikie | 952a9b1 | 2014-10-17 18:00:12 +0000 | [diff] [blame] | 1519 | V = CGM.getCXXABI().EmitMemberDataPointer(MPT, chars); |
David Blaikie | 38079fd | 2013-05-10 21:53:14 +0000 | [diff] [blame] | 1520 | } |
Duncan P. N. Exon Smith | dadc2b6 | 2015-04-21 18:43:54 +0000 | [diff] [blame] | 1521 | TemplateParams.push_back(DBuilder.createTemplateValueParameter( |
| 1522 | TheCU, Name, TTy, |
| 1523 | cast_or_null<llvm::Constant>(V->stripPointerCasts()))); |
David Blaikie | 38079fd | 2013-05-10 21:53:14 +0000 | [diff] [blame] | 1524 | } break; |
| 1525 | case TemplateArgument::NullPtr: { |
| 1526 | QualType T = TA.getNullPtrType(); |
Duncan P. N. Exon Smith | 9dd4e4e | 2015-04-29 16:40:08 +0000 | [diff] [blame] | 1527 | llvm::DIType *TTy = getOrCreateType(T, Unit); |
Duncan P. N. Exon Smith | fb49491 | 2014-12-09 18:39:32 +0000 | [diff] [blame] | 1528 | llvm::Constant *V = nullptr; |
David Blaikie | 38079fd | 2013-05-10 21:53:14 +0000 | [diff] [blame] | 1529 | // Special case member data pointer null values since they're actually -1 |
| 1530 | // instead of zero. |
David Majnemer | 58ed0f3 | 2016-07-17 00:39:12 +0000 | [diff] [blame] | 1531 | if (const auto *MPT = dyn_cast<MemberPointerType>(T.getTypePtr())) |
David Blaikie | 38079fd | 2013-05-10 21:53:14 +0000 | [diff] [blame] | 1532 | // But treat member function pointers as simple zero integers because |
| 1533 | // it's easier than having a special case in LLVM's CodeGen. If LLVM |
| 1534 | // CodeGen grows handling for values of non-null member function |
| 1535 | // pointers then perhaps we could remove this special case and rely on |
| 1536 | // EmitNullMemberPointer for member function pointers. |
| 1537 | if (MPT->isMemberDataPointer()) |
| 1538 | V = CGM.getCXXABI().EmitNullMemberPointer(MPT); |
| 1539 | if (!V) |
| 1540 | V = llvm::ConstantInt::get(CGM.Int8Ty, 0); |
Duncan P. N. Exon Smith | dadc2b6 | 2015-04-21 18:43:54 +0000 | [diff] [blame] | 1541 | TemplateParams.push_back(DBuilder.createTemplateValueParameter( |
David Majnemer | 58ed0f3 | 2016-07-17 00:39:12 +0000 | [diff] [blame] | 1542 | TheCU, Name, TTy, V)); |
David Blaikie | 38079fd | 2013-05-10 21:53:14 +0000 | [diff] [blame] | 1543 | } break; |
Duncan P. N. Exon Smith | dadc2b6 | 2015-04-21 18:43:54 +0000 | [diff] [blame] | 1544 | case TemplateArgument::Template: |
| 1545 | TemplateParams.push_back(DBuilder.createTemplateTemplateParameter( |
Duncan P. N. Exon Smith | 0b6c369 | 2015-04-20 18:51:48 +0000 | [diff] [blame] | 1546 | TheCU, Name, nullptr, |
Duncan P. N. Exon Smith | dadc2b6 | 2015-04-21 18:43:54 +0000 | [diff] [blame] | 1547 | TA.getAsTemplate().getAsTemplateDecl()->getQualifiedNameAsString())); |
| 1548 | break; |
| 1549 | case TemplateArgument::Pack: |
| 1550 | TemplateParams.push_back(DBuilder.createTemplateParameterPack( |
| 1551 | TheCU, Name, nullptr, |
| 1552 | CollectTemplateParams(nullptr, TA.getPackAsArray(), Unit))); |
| 1553 | break; |
David Majnemer | 5559d47 | 2013-08-24 08:21:10 +0000 | [diff] [blame] | 1554 | case TemplateArgument::Expression: { |
| 1555 | const Expr *E = TA.getAsExpr(); |
| 1556 | QualType T = E->getType(); |
David Majnemer | 922ad9f | 2014-10-24 19:49:04 +0000 | [diff] [blame] | 1557 | if (E->isGLValue()) |
| 1558 | T = CGM.getContext().getLValueReferenceType(T); |
Duncan P. N. Exon Smith | fb49491 | 2014-12-09 18:39:32 +0000 | [diff] [blame] | 1559 | llvm::Constant *V = CGM.EmitConstantExpr(E, T); |
David Majnemer | 5559d47 | 2013-08-24 08:21:10 +0000 | [diff] [blame] | 1560 | assert(V && "Expression in template argument isn't constant"); |
Duncan P. N. Exon Smith | 9dd4e4e | 2015-04-29 16:40:08 +0000 | [diff] [blame] | 1561 | llvm::DIType *TTy = getOrCreateType(T, Unit); |
Duncan P. N. Exon Smith | dadc2b6 | 2015-04-21 18:43:54 +0000 | [diff] [blame] | 1562 | TemplateParams.push_back(DBuilder.createTemplateValueParameter( |
David Majnemer | 58ed0f3 | 2016-07-17 00:39:12 +0000 | [diff] [blame] | 1563 | TheCU, Name, TTy, V->stripPointerCasts())); |
David Majnemer | 5559d47 | 2013-08-24 08:21:10 +0000 | [diff] [blame] | 1564 | } break; |
David Blaikie | 2b93c54 | 2013-05-10 23:36:06 +0000 | [diff] [blame] | 1565 | // And the following should never occur: |
David Blaikie | 38079fd | 2013-05-10 21:53:14 +0000 | [diff] [blame] | 1566 | case TemplateArgument::TemplateExpansion: |
David Blaikie | 38079fd | 2013-05-10 21:53:14 +0000 | [diff] [blame] | 1567 | case TemplateArgument::Null: |
| 1568 | llvm_unreachable( |
| 1569 | "These argument types shouldn't exist in concrete types"); |
Guy Benyei | 11169dd | 2012-12-18 14:30:41 +0000 | [diff] [blame] | 1570 | } |
| 1571 | } |
| 1572 | return DBuilder.getOrCreateArray(TemplateParams); |
| 1573 | } |
| 1574 | |
Duncan P. N. Exon Smith | 9dd4e4e | 2015-04-29 16:40:08 +0000 | [diff] [blame] | 1575 | llvm::DINodeArray |
Duncan P. N. Exon Smith | 8e47da4 | 2015-04-21 20:07:29 +0000 | [diff] [blame] | 1576 | CGDebugInfo::CollectFunctionTemplateParams(const FunctionDecl *FD, |
Duncan P. N. Exon Smith | 9dd4e4e | 2015-04-29 16:40:08 +0000 | [diff] [blame] | 1577 | llvm::DIFile *Unit) { |
Guy Benyei | 11169dd | 2012-12-18 14:30:41 +0000 | [diff] [blame] | 1578 | if (FD->getTemplatedKind() == |
| 1579 | FunctionDecl::TK_FunctionTemplateSpecialization) { |
Eric Christopher | e7b87e5 | 2014-10-26 23:40:33 +0000 | [diff] [blame] | 1580 | const TemplateParameterList *TList = FD->getTemplateSpecializationInfo() |
| 1581 | ->getTemplate() |
| 1582 | ->getTemplateParameters(); |
David Blaikie | 47c1150 | 2013-06-22 18:59:18 +0000 | [diff] [blame] | 1583 | return CollectTemplateParams( |
| 1584 | TList, FD->getTemplateSpecializationArgs()->asArray(), Unit); |
Guy Benyei | 11169dd | 2012-12-18 14:30:41 +0000 | [diff] [blame] | 1585 | } |
Duncan P. N. Exon Smith | 9dd4e4e | 2015-04-29 16:40:08 +0000 | [diff] [blame] | 1586 | return llvm::DINodeArray(); |
Guy Benyei | 11169dd | 2012-12-18 14:30:41 +0000 | [diff] [blame] | 1587 | } |
| 1588 | |
Duncan P. N. Exon Smith | 9dd4e4e | 2015-04-29 16:40:08 +0000 | [diff] [blame] | 1589 | llvm::DINodeArray CGDebugInfo::CollectCXXTemplateParams( |
| 1590 | const ClassTemplateSpecializationDecl *TSpecial, llvm::DIFile *Unit) { |
Adrian Prantl | 649f030 | 2014-04-17 01:04:01 +0000 | [diff] [blame] | 1591 | // Always get the full list of parameters, not just the ones from |
| 1592 | // the specialization. |
| 1593 | TemplateParameterList *TPList = |
Eric Christopher | e7b87e5 | 2014-10-26 23:40:33 +0000 | [diff] [blame] | 1594 | TSpecial->getSpecializedTemplate()->getTemplateParameters(); |
Adrian Prantl | 2c92e9c | 2014-04-17 00:30:48 +0000 | [diff] [blame] | 1595 | const TemplateArgumentList &TAList = TSpecial->getTemplateArgs(); |
David Blaikie | 47c1150 | 2013-06-22 18:59:18 +0000 | [diff] [blame] | 1596 | return CollectTemplateParams(TPList, TAList.asArray(), Unit); |
Guy Benyei | 11169dd | 2012-12-18 14:30:41 +0000 | [diff] [blame] | 1597 | } |
| 1598 | |
Duncan P. N. Exon Smith | 9dd4e4e | 2015-04-29 16:40:08 +0000 | [diff] [blame] | 1599 | llvm::DIType *CGDebugInfo::getOrCreateVTablePtrType(llvm::DIFile *Unit) { |
Duncan P. N. Exon Smith | b747023 | 2015-04-15 23:48:50 +0000 | [diff] [blame] | 1600 | if (VTablePtrType) |
Guy Benyei | 11169dd | 2012-12-18 14:30:41 +0000 | [diff] [blame] | 1601 | return VTablePtrType; |
| 1602 | |
| 1603 | ASTContext &Context = CGM.getContext(); |
| 1604 | |
| 1605 | /* Function type */ |
Duncan P. N. Exon Smith | fb49491 | 2014-12-09 18:39:32 +0000 | [diff] [blame] | 1606 | llvm::Metadata *STy = getOrCreateType(Context.IntTy, Unit); |
Duncan P. N. Exon Smith | 9dd4e4e | 2015-04-29 16:40:08 +0000 | [diff] [blame] | 1607 | llvm::DITypeRefArray SElements = DBuilder.getOrCreateTypeArray(STy); |
Eric Christopher | 28a6db5 | 2015-10-15 06:56:08 +0000 | [diff] [blame] | 1608 | llvm::DIType *SubTy = DBuilder.createSubroutineType(SElements); |
Guy Benyei | 11169dd | 2012-12-18 14:30:41 +0000 | [diff] [blame] | 1609 | unsigned Size = Context.getTypeSize(Context.VoidPtrTy); |
Duncan P. N. Exon Smith | 9dd4e4e | 2015-04-29 16:40:08 +0000 | [diff] [blame] | 1610 | llvm::DIType *vtbl_ptr_type = |
Eric Christopher | e7b87e5 | 2014-10-26 23:40:33 +0000 | [diff] [blame] | 1611 | DBuilder.createPointerType(SubTy, Size, 0, "__vtbl_ptr_type"); |
Guy Benyei | 11169dd | 2012-12-18 14:30:41 +0000 | [diff] [blame] | 1612 | VTablePtrType = DBuilder.createPointerType(vtbl_ptr_type, Size); |
| 1613 | return VTablePtrType; |
| 1614 | } |
| 1615 | |
Guy Benyei | 11169dd | 2012-12-18 14:30:41 +0000 | [diff] [blame] | 1616 | StringRef CGDebugInfo::getVTableName(const CXXRecordDecl *RD) { |
Benjamin Kramer | 1b18a5e | 2013-09-09 16:39:06 +0000 | [diff] [blame] | 1617 | // Copy the gdb compatible name on the side and use its reference. |
| 1618 | return internString("_vptr$", RD->getNameAsString()); |
Guy Benyei | 11169dd | 2012-12-18 14:30:41 +0000 | [diff] [blame] | 1619 | } |
| 1620 | |
Duncan P. N. Exon Smith | 9dd4e4e | 2015-04-29 16:40:08 +0000 | [diff] [blame] | 1621 | void CGDebugInfo::CollectVTableInfo(const CXXRecordDecl *RD, llvm::DIFile *Unit, |
Reid Kleckner | dc12499 | 2016-08-31 16:11:43 +0000 | [diff] [blame] | 1622 | SmallVectorImpl<llvm::Metadata *> &EltTys, |
| 1623 | llvm::DICompositeType *RecordTy) { |
Guy Benyei | 11169dd | 2012-12-18 14:30:41 +0000 | [diff] [blame] | 1624 | // If this class is not dynamic then there is not any vtable info to collect. |
| 1625 | if (!RD->isDynamicClass()) |
| 1626 | return; |
| 1627 | |
Reid Kleckner | 5981242 | 2016-08-31 20:35:01 +0000 | [diff] [blame] | 1628 | // Don't emit any vtable shape or vptr info if this class doesn't have an |
| 1629 | // extendable vfptr. This can happen if the class doesn't have virtual |
| 1630 | // methods, or in the MS ABI if those virtual methods only come from virtually |
| 1631 | // inherited bases. |
| 1632 | const ASTRecordLayout &RL = CGM.getContext().getASTRecordLayout(RD); |
| 1633 | if (!RL.hasExtendableVFPtr()) |
| 1634 | return; |
| 1635 | |
Reid Kleckner | dc12499 | 2016-08-31 16:11:43 +0000 | [diff] [blame] | 1636 | // CodeView needs to know how large the vtable of every dynamic class is, so |
| 1637 | // emit a special named pointer type into the element list. The vptr type |
| 1638 | // points to this type as well. |
| 1639 | llvm::DIType *VPtrTy = nullptr; |
| 1640 | bool NeedVTableShape = CGM.getCodeGenOpts().EmitCodeView && |
| 1641 | CGM.getTarget().getCXXABI().isMicrosoft(); |
| 1642 | if (NeedVTableShape) { |
| 1643 | uint64_t PtrWidth = |
| 1644 | CGM.getContext().getTypeSize(CGM.getContext().VoidPtrTy); |
| 1645 | const VTableLayout &VFTLayout = |
| 1646 | CGM.getMicrosoftVTableContext().getVFTableLayout(RD, CharUnits::Zero()); |
| 1647 | unsigned VSlotCount = |
Peter Collingbourne | e53683f | 2016-09-08 01:14:39 +0000 | [diff] [blame] | 1648 | VFTLayout.vtable_components().size() - CGM.getLangOpts().RTTIData; |
Reid Kleckner | dc12499 | 2016-08-31 16:11:43 +0000 | [diff] [blame] | 1649 | unsigned VTableWidth = PtrWidth * VSlotCount; |
| 1650 | |
| 1651 | // Create a very wide void* type and insert it directly in the element list. |
| 1652 | llvm::DIType *VTableType = |
| 1653 | DBuilder.createPointerType(nullptr, VTableWidth, 0, "__vtbl_ptr_type"); |
| 1654 | EltTys.push_back(VTableType); |
| 1655 | |
| 1656 | // The vptr is a pointer to this special vtable type. |
| 1657 | VPtrTy = DBuilder.createPointerType(VTableType, PtrWidth); |
| 1658 | } |
| 1659 | |
| 1660 | // If there is a primary base then the artificial vptr member lives there. |
Reid Kleckner | dc12499 | 2016-08-31 16:11:43 +0000 | [diff] [blame] | 1661 | if (RL.getPrimaryBase()) |
| 1662 | return; |
| 1663 | |
| 1664 | if (!VPtrTy) |
| 1665 | VPtrTy = getOrCreateVTablePtrType(Unit); |
| 1666 | |
Guy Benyei | 11169dd | 2012-12-18 14:30:41 +0000 | [diff] [blame] | 1667 | unsigned Size = CGM.getContext().getTypeSize(CGM.getContext().VoidPtrTy); |
Reid Kleckner | dc12499 | 2016-08-31 16:11:43 +0000 | [diff] [blame] | 1668 | llvm::DIType *VPtrMember = DBuilder.createMemberType( |
Eric Christopher | e7b87e5 | 2014-10-26 23:40:33 +0000 | [diff] [blame] | 1669 | Unit, getVTableName(RD), Unit, 0, Size, 0, 0, |
Reid Kleckner | dc12499 | 2016-08-31 16:11:43 +0000 | [diff] [blame] | 1670 | llvm::DINode::FlagArtificial, VPtrTy); |
| 1671 | EltTys.push_back(VPtrMember); |
Guy Benyei | 11169dd | 2012-12-18 14:30:41 +0000 | [diff] [blame] | 1672 | } |
| 1673 | |
Duncan P. N. Exon Smith | 9dd4e4e | 2015-04-29 16:40:08 +0000 | [diff] [blame] | 1674 | llvm::DIType *CGDebugInfo::getOrCreateRecordType(QualType RTy, |
Duncan P. N. Exon Smith | 0b6c369 | 2015-04-20 18:51:48 +0000 | [diff] [blame] | 1675 | SourceLocation Loc) { |
Benjamin Kramer | 8c30592 | 2016-02-02 11:06:51 +0000 | [diff] [blame] | 1676 | assert(DebugKind >= codegenoptions::LimitedDebugInfo); |
Duncan P. N. Exon Smith | 9dd4e4e | 2015-04-29 16:40:08 +0000 | [diff] [blame] | 1677 | llvm::DIType *T = getOrCreateType(RTy, getOrCreateFile(Loc)); |
Guy Benyei | 11169dd | 2012-12-18 14:30:41 +0000 | [diff] [blame] | 1678 | return T; |
| 1679 | } |
| 1680 | |
Duncan P. N. Exon Smith | 9dd4e4e | 2015-04-29 16:40:08 +0000 | [diff] [blame] | 1681 | llvm::DIType *CGDebugInfo::getOrCreateInterfaceType(QualType D, |
Duncan P. N. Exon Smith | 0b6c369 | 2015-04-20 18:51:48 +0000 | [diff] [blame] | 1682 | SourceLocation Loc) { |
Adrian Prantl | ad9a195e | 2015-08-27 21:21:19 +0000 | [diff] [blame] | 1683 | return getOrCreateStandaloneType(D, Loc); |
| 1684 | } |
| 1685 | |
| 1686 | llvm::DIType *CGDebugInfo::getOrCreateStandaloneType(QualType D, |
| 1687 | SourceLocation Loc) { |
Benjamin Kramer | 8c30592 | 2016-02-02 11:06:51 +0000 | [diff] [blame] | 1688 | assert(DebugKind >= codegenoptions::LimitedDebugInfo); |
Adrian Prantl | ad9a195e | 2015-08-27 21:21:19 +0000 | [diff] [blame] | 1689 | assert(!D.isNull() && "null type"); |
Duncan P. N. Exon Smith | 9dd4e4e | 2015-04-29 16:40:08 +0000 | [diff] [blame] | 1690 | llvm::DIType *T = getOrCreateType(D, getOrCreateFile(Loc)); |
Adrian Prantl | ad9a195e | 2015-08-27 21:21:19 +0000 | [diff] [blame] | 1691 | assert(T && "could not create debug info for type"); |
Adrian Prantl | 3a884fa | 2015-08-27 22:56:46 +0000 | [diff] [blame] | 1692 | |
Adrian Prantl | 73409ce | 2013-03-11 18:33:46 +0000 | [diff] [blame] | 1693 | RetainedTypes.push_back(D.getAsOpaquePtr()); |
Guy Benyei | 11169dd | 2012-12-18 14:30:41 +0000 | [diff] [blame] | 1694 | return T; |
| 1695 | } |
| 1696 | |
David Blaikie | 483a9da | 2014-05-06 18:35:21 +0000 | [diff] [blame] | 1697 | void CGDebugInfo::completeType(const EnumDecl *ED) { |
Benjamin Kramer | 8c30592 | 2016-02-02 11:06:51 +0000 | [diff] [blame] | 1698 | if (DebugKind <= codegenoptions::DebugLineTablesOnly) |
David Blaikie | 483a9da | 2014-05-06 18:35:21 +0000 | [diff] [blame] | 1699 | return; |
| 1700 | QualType Ty = CGM.getContext().getEnumType(ED); |
Eric Christopher | e7b87e5 | 2014-10-26 23:40:33 +0000 | [diff] [blame] | 1701 | void *TyPtr = Ty.getAsOpaquePtr(); |
David Blaikie | 483a9da | 2014-05-06 18:35:21 +0000 | [diff] [blame] | 1702 | auto I = TypeCache.find(TyPtr); |
Duncan P. N. Exon Smith | 9dd4e4e | 2015-04-29 16:40:08 +0000 | [diff] [blame] | 1703 | if (I == TypeCache.end() || !cast<llvm::DIType>(I->second)->isForwardDecl()) |
David Blaikie | 483a9da | 2014-05-06 18:35:21 +0000 | [diff] [blame] | 1704 | return; |
Duncan P. N. Exon Smith | 9dd4e4e | 2015-04-29 16:40:08 +0000 | [diff] [blame] | 1705 | llvm::DIType *Res = CreateTypeDefinition(Ty->castAs<EnumType>()); |
Duncan P. N. Exon Smith | 4caa7f2 | 2015-04-16 01:00:56 +0000 | [diff] [blame] | 1706 | assert(!Res->isForwardDecl()); |
Duncan P. N. Exon Smith | fb49491 | 2014-12-09 18:39:32 +0000 | [diff] [blame] | 1707 | TypeCache[TyPtr].reset(Res); |
David Blaikie | 483a9da | 2014-05-06 18:35:21 +0000 | [diff] [blame] | 1708 | } |
| 1709 | |
David Blaikie | b2e86eb | 2013-08-15 20:49:17 +0000 | [diff] [blame] | 1710 | void CGDebugInfo::completeType(const RecordDecl *RD) { |
Benjamin Kramer | 8c30592 | 2016-02-02 11:06:51 +0000 | [diff] [blame] | 1711 | if (DebugKind > codegenoptions::LimitedDebugInfo || |
David Blaikie | b2e86eb | 2013-08-15 20:49:17 +0000 | [diff] [blame] | 1712 | !CGM.getLangOpts().CPlusPlus) |
| 1713 | completeRequiredType(RD); |
| 1714 | } |
| 1715 | |
David Blaikie | b11c873 | 2017-01-30 06:36:08 +0000 | [diff] [blame] | 1716 | /// Return true if the class or any of its methods are marked dllimport. |
| 1717 | static bool isClassOrMethodDLLImport(const CXXRecordDecl *RD) { |
| 1718 | if (RD->hasAttr<DLLImportAttr>()) |
| 1719 | return true; |
| 1720 | for (const CXXMethodDecl *MD : RD->methods()) |
| 1721 | if (MD->hasAttr<DLLImportAttr>()) |
| 1722 | return true; |
| 1723 | return false; |
| 1724 | } |
| 1725 | |
David Blaikie | 6943dea | 2013-08-20 01:28:15 +0000 | [diff] [blame] | 1726 | void CGDebugInfo::completeClassData(const RecordDecl *RD) { |
David Blaikie | b11c873 | 2017-01-30 06:36:08 +0000 | [diff] [blame] | 1727 | if (auto *CXXRD = dyn_cast<CXXRecordDecl>(RD)) |
| 1728 | if (CXXRD->isDynamicClass() && |
| 1729 | CGM.getVTableLinkage(CXXRD) == |
| 1730 | llvm::GlobalValue::AvailableExternallyLinkage && |
| 1731 | !isClassOrMethodDLLImport(CXXRD)) |
| 1732 | return; |
| 1733 | completeClass(RD); |
| 1734 | } |
| 1735 | |
| 1736 | void CGDebugInfo::completeClass(const RecordDecl *RD) { |
Benjamin Kramer | 8c30592 | 2016-02-02 11:06:51 +0000 | [diff] [blame] | 1737 | if (DebugKind <= codegenoptions::DebugLineTablesOnly) |
Michael Gottesman | 349542b | 2013-08-19 18:46:16 +0000 | [diff] [blame] | 1738 | return; |
David Blaikie | 6943dea | 2013-08-20 01:28:15 +0000 | [diff] [blame] | 1739 | QualType Ty = CGM.getContext().getRecordType(RD); |
Eric Christopher | e7b87e5 | 2014-10-26 23:40:33 +0000 | [diff] [blame] | 1740 | void *TyPtr = Ty.getAsOpaquePtr(); |
David Blaikie | ef8a951 | 2014-05-05 23:23:53 +0000 | [diff] [blame] | 1741 | auto I = TypeCache.find(TyPtr); |
Duncan P. N. Exon Smith | 9dd4e4e | 2015-04-29 16:40:08 +0000 | [diff] [blame] | 1742 | if (I != TypeCache.end() && !cast<llvm::DIType>(I->second)->isForwardDecl()) |
David Blaikie | b2e86eb | 2013-08-15 20:49:17 +0000 | [diff] [blame] | 1743 | return; |
Duncan P. N. Exon Smith | 9dd4e4e | 2015-04-29 16:40:08 +0000 | [diff] [blame] | 1744 | llvm::DIType *Res = CreateTypeDefinition(Ty->castAs<RecordType>()); |
Duncan P. N. Exon Smith | 4caa7f2 | 2015-04-16 01:00:56 +0000 | [diff] [blame] | 1745 | assert(!Res->isForwardDecl()); |
Duncan P. N. Exon Smith | fb49491 | 2014-12-09 18:39:32 +0000 | [diff] [blame] | 1746 | TypeCache[TyPtr].reset(Res); |
David Blaikie | b2e86eb | 2013-08-15 20:49:17 +0000 | [diff] [blame] | 1747 | } |
| 1748 | |
David Blaikie | 0e716b4 | 2014-03-03 23:48:23 +0000 | [diff] [blame] | 1749 | static bool hasExplicitMemberDefinition(CXXRecordDecl::method_iterator I, |
| 1750 | CXXRecordDecl::method_iterator End) { |
David Majnemer | 58ed0f3 | 2016-07-17 00:39:12 +0000 | [diff] [blame] | 1751 | for (CXXMethodDecl *MD : llvm::make_range(I, End)) |
| 1752 | if (FunctionDecl *Tmpl = MD->getInstantiatedFromMemberFunction()) |
David Blaikie | f7f2185 | 2014-03-04 03:08:14 +0000 | [diff] [blame] | 1753 | if (!Tmpl->isImplicit() && Tmpl->isThisDeclarationADefinition() && |
David Majnemer | 58ed0f3 | 2016-07-17 00:39:12 +0000 | [diff] [blame] | 1754 | !MD->getMemberSpecializationInfo()->isExplicitSpecialization()) |
David Blaikie | 0e716b4 | 2014-03-03 23:48:23 +0000 | [diff] [blame] | 1755 | return true; |
| 1756 | return false; |
| 1757 | } |
| 1758 | |
Adrian Prantl | 05fefa4 | 2016-04-25 20:52:40 +0000 | [diff] [blame] | 1759 | /// Does a type definition exist in an imported clang module? |
| 1760 | static bool isDefinedInClangModule(const RecordDecl *RD) { |
Adrian Prantl | 09906a6 | 2016-08-22 22:38:16 +0000 | [diff] [blame] | 1761 | // Only definitions that where imported from an AST file come from a module. |
Adrian Prantl | 88d7917 | 2016-04-26 21:58:18 +0000 | [diff] [blame] | 1762 | if (!RD || !RD->isFromASTFile()) |
Adrian Prantl | 05fefa4 | 2016-04-25 20:52:40 +0000 | [diff] [blame] | 1763 | return false; |
Adrian Prantl | 09906a6 | 2016-08-22 22:38:16 +0000 | [diff] [blame] | 1764 | // Anonymous entities cannot be addressed. Treat them as not from module. |
Adrian Prantl | 05fefa4 | 2016-04-25 20:52:40 +0000 | [diff] [blame] | 1765 | if (!RD->isExternallyVisible() && RD->getName().empty()) |
| 1766 | return false; |
Adrian Prantl | 9491371 | 2016-04-26 23:42:43 +0000 | [diff] [blame] | 1767 | if (auto *CXXDecl = dyn_cast<CXXRecordDecl>(RD)) { |
Adrian Prantl | a72972b | 2016-08-22 22:23:58 +0000 | [diff] [blame] | 1768 | if (!CXXDecl->isCompleteDefinition()) |
| 1769 | return false; |
Adrian Prantl | 26cb1d2 | 2016-08-17 18:27:24 +0000 | [diff] [blame] | 1770 | auto TemplateKind = CXXDecl->getTemplateSpecializationKind(); |
| 1771 | if (TemplateKind != TSK_Undeclared) { |
| 1772 | // This is a template, check the origin of the first member. |
| 1773 | if (CXXDecl->field_begin() == CXXDecl->field_end()) |
| 1774 | return TemplateKind == TSK_ExplicitInstantiationDeclaration; |
| 1775 | if (!CXXDecl->field_begin()->isFromASTFile()) |
| 1776 | return false; |
| 1777 | } |
Adrian Prantl | 9491371 | 2016-04-26 23:42:43 +0000 | [diff] [blame] | 1778 | } |
Adrian Prantl | 05fefa4 | 2016-04-25 20:52:40 +0000 | [diff] [blame] | 1779 | return true; |
| 1780 | } |
| 1781 | |
Benjamin Kramer | 8c30592 | 2016-02-02 11:06:51 +0000 | [diff] [blame] | 1782 | static bool shouldOmitDefinition(codegenoptions::DebugInfoKind DebugKind, |
| 1783 | bool DebugTypeExtRefs, const RecordDecl *RD, |
David Blaikie | 0e716b4 | 2014-03-03 23:48:23 +0000 | [diff] [blame] | 1784 | const LangOptions &LangOpts) { |
Adrian Prantl | 88d7917 | 2016-04-26 21:58:18 +0000 | [diff] [blame] | 1785 | if (DebugTypeExtRefs && isDefinedInClangModule(RD->getDefinition())) |
Adrian Prantl | 43e0081 | 2016-01-19 23:42:53 +0000 | [diff] [blame] | 1786 | return true; |
Adrian Prantl | 5c8bd88 | 2015-09-11 17:23:08 +0000 | [diff] [blame] | 1787 | |
Benjamin Kramer | 8c30592 | 2016-02-02 11:06:51 +0000 | [diff] [blame] | 1788 | if (DebugKind > codegenoptions::LimitedDebugInfo) |
David Blaikie | 0e716b4 | 2014-03-03 23:48:23 +0000 | [diff] [blame] | 1789 | return false; |
| 1790 | |
| 1791 | if (!LangOpts.CPlusPlus) |
| 1792 | return false; |
| 1793 | |
| 1794 | if (!RD->isCompleteDefinitionRequired()) |
| 1795 | return true; |
| 1796 | |
David Majnemer | 58ed0f3 | 2016-07-17 00:39:12 +0000 | [diff] [blame] | 1797 | const auto *CXXDecl = dyn_cast<CXXRecordDecl>(RD); |
David Blaikie | 0e716b4 | 2014-03-03 23:48:23 +0000 | [diff] [blame] | 1798 | |
| 1799 | if (!CXXDecl) |
| 1800 | return false; |
| 1801 | |
Adrian McCarthy | 9924298 | 2016-08-16 22:11:18 +0000 | [diff] [blame] | 1802 | // Only emit complete debug info for a dynamic class when its vtable is |
| 1803 | // emitted. However, Microsoft debuggers don't resolve type information |
Reid Kleckner | c9404e1 | 2016-09-09 16:27:04 +0000 | [diff] [blame] | 1804 | // across DLL boundaries, so skip this optimization if the class or any of its |
| 1805 | // methods are marked dllimport. This isn't a complete solution, since objects |
| 1806 | // without any dllimport methods can be used in one DLL and constructed in |
| 1807 | // another, but it is the current behavior of LimitedDebugInfo. |
Adrian McCarthy | 9924298 | 2016-08-16 22:11:18 +0000 | [diff] [blame] | 1808 | if (CXXDecl->hasDefinition() && CXXDecl->isDynamicClass() && |
Reid Kleckner | c9404e1 | 2016-09-09 16:27:04 +0000 | [diff] [blame] | 1809 | !isClassOrMethodDLLImport(CXXDecl)) |
David Blaikie | 0e716b4 | 2014-03-03 23:48:23 +0000 | [diff] [blame] | 1810 | return true; |
| 1811 | |
| 1812 | TemplateSpecializationKind Spec = TSK_Undeclared; |
David Majnemer | 58ed0f3 | 2016-07-17 00:39:12 +0000 | [diff] [blame] | 1813 | if (const auto *SD = dyn_cast<ClassTemplateSpecializationDecl>(RD)) |
David Blaikie | 0e716b4 | 2014-03-03 23:48:23 +0000 | [diff] [blame] | 1814 | Spec = SD->getSpecializationKind(); |
| 1815 | |
| 1816 | if (Spec == TSK_ExplicitInstantiationDeclaration && |
| 1817 | hasExplicitMemberDefinition(CXXDecl->method_begin(), |
| 1818 | CXXDecl->method_end())) |
| 1819 | return true; |
| 1820 | |
| 1821 | return false; |
| 1822 | } |
| 1823 | |
Reid Kleckner | 6c7b1c6 | 2016-09-13 00:01:23 +0000 | [diff] [blame] | 1824 | void CGDebugInfo::completeRequiredType(const RecordDecl *RD) { |
| 1825 | if (shouldOmitDefinition(DebugKind, DebugTypeExtRefs, RD, CGM.getLangOpts())) |
| 1826 | return; |
| 1827 | |
| 1828 | QualType Ty = CGM.getContext().getRecordType(RD); |
| 1829 | llvm::DIType *T = getTypeOrNull(Ty); |
| 1830 | if (T && T->isForwardDecl()) |
| 1831 | completeClassData(RD); |
| 1832 | } |
| 1833 | |
Duncan P. N. Exon Smith | 9dd4e4e | 2015-04-29 16:40:08 +0000 | [diff] [blame] | 1834 | llvm::DIType *CGDebugInfo::CreateType(const RecordType *Ty) { |
Guy Benyei | 11169dd | 2012-12-18 14:30:41 +0000 | [diff] [blame] | 1835 | RecordDecl *RD = Ty->getDecl(); |
Duncan P. N. Exon Smith | 9dd4e4e | 2015-04-29 16:40:08 +0000 | [diff] [blame] | 1836 | llvm::DIType *T = cast_or_null<llvm::DIType>(getTypeOrNull(QualType(Ty, 0))); |
Adrian Prantl | 5c8bd88 | 2015-09-11 17:23:08 +0000 | [diff] [blame] | 1837 | if (T || shouldOmitDefinition(DebugKind, DebugTypeExtRefs, RD, |
| 1838 | CGM.getLangOpts())) { |
David Blaikie | 3b1cc9b | 2013-09-06 06:45:04 +0000 | [diff] [blame] | 1839 | if (!T) |
Adrian Prantl | 6ec370a | 2015-09-10 18:39:45 +0000 | [diff] [blame] | 1840 | T = getOrCreateRecordFwdDecl(Ty, getDeclContextDescriptor(RD)); |
David Blaikie | 3b1cc9b | 2013-09-06 06:45:04 +0000 | [diff] [blame] | 1841 | return T; |
David Blaikie | e36464c | 2013-06-05 05:32:23 +0000 | [diff] [blame] | 1842 | } |
Guy Benyei | 11169dd | 2012-12-18 14:30:41 +0000 | [diff] [blame] | 1843 | |
David Blaikie | b2e86eb | 2013-08-15 20:49:17 +0000 | [diff] [blame] | 1844 | return CreateTypeDefinition(Ty); |
| 1845 | } |
| 1846 | |
Duncan P. N. Exon Smith | 9dd4e4e | 2015-04-29 16:40:08 +0000 | [diff] [blame] | 1847 | llvm::DIType *CGDebugInfo::CreateTypeDefinition(const RecordType *Ty) { |
David Blaikie | b2e86eb | 2013-08-15 20:49:17 +0000 | [diff] [blame] | 1848 | RecordDecl *RD = Ty->getDecl(); |
| 1849 | |
Guy Benyei | 11169dd | 2012-12-18 14:30:41 +0000 | [diff] [blame] | 1850 | // Get overall information about the record type for the debug info. |
Duncan P. N. Exon Smith | 9dd4e4e | 2015-04-29 16:40:08 +0000 | [diff] [blame] | 1851 | llvm::DIFile *DefUnit = getOrCreateFile(RD->getLocation()); |
Guy Benyei | 11169dd | 2012-12-18 14:30:41 +0000 | [diff] [blame] | 1852 | |
| 1853 | // Records and classes and unions can all be recursive. To handle them, we |
| 1854 | // first generate a debug descriptor for the struct as a forward declaration. |
| 1855 | // Then (if it is a definition) we go through and get debug info for all of |
| 1856 | // its members. Finally, we create a descriptor for the complete type (which |
| 1857 | // may refer to the forward decl if the struct is recursive) and replace all |
| 1858 | // uses of the forward declaration with the final definition. |
Duncan P. N. Exon Smith | bd210e6 | 2015-07-24 20:34:41 +0000 | [diff] [blame] | 1859 | llvm::DICompositeType *FwdDecl = getOrCreateLimitedType(Ty, DefUnit); |
Guy Benyei | 11169dd | 2012-12-18 14:30:41 +0000 | [diff] [blame] | 1860 | |
Adrian Prantl | 5f66bae | 2015-02-11 17:45:15 +0000 | [diff] [blame] | 1861 | const RecordDecl *D = RD->getDefinition(); |
| 1862 | if (!D || !D->isCompleteDefinition()) |
Guy Benyei | 11169dd | 2012-12-18 14:30:41 +0000 | [diff] [blame] | 1863 | return FwdDecl; |
| 1864 | |
David Majnemer | 58ed0f3 | 2016-07-17 00:39:12 +0000 | [diff] [blame] | 1865 | if (const auto *CXXDecl = dyn_cast<CXXRecordDecl>(RD)) |
David Blaikie | adfbf99 | 2013-08-18 16:55:33 +0000 | [diff] [blame] | 1866 | CollectContainingType(CXXDecl, FwdDecl); |
| 1867 | |
Guy Benyei | 11169dd | 2012-12-18 14:30:41 +0000 | [diff] [blame] | 1868 | // Push the struct on region stack. |
Duncan P. N. Exon Smith | fb49491 | 2014-12-09 18:39:32 +0000 | [diff] [blame] | 1869 | LexicalBlockStack.emplace_back(&*FwdDecl); |
| 1870 | RegionMap[Ty->getDecl()].reset(FwdDecl); |
Guy Benyei | 11169dd | 2012-12-18 14:30:41 +0000 | [diff] [blame] | 1871 | |
Guy Benyei | 11169dd | 2012-12-18 14:30:41 +0000 | [diff] [blame] | 1872 | // Convert all the elements. |
Duncan P. N. Exon Smith | fb49491 | 2014-12-09 18:39:32 +0000 | [diff] [blame] | 1873 | SmallVector<llvm::Metadata *, 16> EltTys; |
David Blaikie | 6943dea | 2013-08-20 01:28:15 +0000 | [diff] [blame] | 1874 | // what about nested types? |
Guy Benyei | 11169dd | 2012-12-18 14:30:41 +0000 | [diff] [blame] | 1875 | |
| 1876 | // Note: The split of CXXDecl information here is intentional, the |
| 1877 | // gdb tests will depend on a certain ordering at printout. The debug |
| 1878 | // information offsets are still correct if we merge them all together |
| 1879 | // though. |
David Majnemer | 58ed0f3 | 2016-07-17 00:39:12 +0000 | [diff] [blame] | 1880 | const auto *CXXDecl = dyn_cast<CXXRecordDecl>(RD); |
Guy Benyei | 11169dd | 2012-12-18 14:30:41 +0000 | [diff] [blame] | 1881 | if (CXXDecl) { |
| 1882 | CollectCXXBases(CXXDecl, DefUnit, EltTys, FwdDecl); |
Reid Kleckner | dc12499 | 2016-08-31 16:11:43 +0000 | [diff] [blame] | 1883 | CollectVTableInfo(CXXDecl, DefUnit, EltTys, FwdDecl); |
Guy Benyei | 11169dd | 2012-12-18 14:30:41 +0000 | [diff] [blame] | 1884 | } |
| 1885 | |
Eric Christopher | 91a3190 | 2013-01-16 01:22:32 +0000 | [diff] [blame] | 1886 | // Collect data fields (including static variables and any initializers). |
Guy Benyei | 11169dd | 2012-12-18 14:30:41 +0000 | [diff] [blame] | 1887 | CollectRecordFields(RD, DefUnit, EltTys, FwdDecl); |
Eric Christopher | 2df080e | 2013-10-11 18:16:51 +0000 | [diff] [blame] | 1888 | if (CXXDecl) |
Guy Benyei | 11169dd | 2012-12-18 14:30:41 +0000 | [diff] [blame] | 1889 | CollectCXXMemberFunctions(CXXDecl, DefUnit, EltTys, FwdDecl); |
Guy Benyei | 11169dd | 2012-12-18 14:30:41 +0000 | [diff] [blame] | 1890 | |
| 1891 | LexicalBlockStack.pop_back(); |
| 1892 | RegionMap.erase(Ty->getDecl()); |
| 1893 | |
Duncan P. N. Exon Smith | 9dd4e4e | 2015-04-29 16:40:08 +0000 | [diff] [blame] | 1894 | llvm::DINodeArray Elements = DBuilder.getOrCreateArray(EltTys); |
Duncan P. N. Exon Smith | c8ee63e | 2014-12-18 00:48:56 +0000 | [diff] [blame] | 1895 | DBuilder.replaceArrays(FwdDecl, Elements); |
Guy Benyei | 11169dd | 2012-12-18 14:30:41 +0000 | [diff] [blame] | 1896 | |
Adrian Prantl | 5f66bae | 2015-02-11 17:45:15 +0000 | [diff] [blame] | 1897 | if (FwdDecl->isTemporary()) |
Duncan P. N. Exon Smith | 4078ad4 | 2015-04-16 16:36:45 +0000 | [diff] [blame] | 1898 | FwdDecl = |
Duncan P. N. Exon Smith | 9dd4e4e | 2015-04-29 16:40:08 +0000 | [diff] [blame] | 1899 | llvm::MDNode::replaceWithPermanent(llvm::TempDICompositeType(FwdDecl)); |
Adrian Prantl | 5f66bae | 2015-02-11 17:45:15 +0000 | [diff] [blame] | 1900 | |
Duncan P. N. Exon Smith | fb49491 | 2014-12-09 18:39:32 +0000 | [diff] [blame] | 1901 | RegionMap[Ty->getDecl()].reset(FwdDecl); |
Eric Christopher | 5c7ee8b | 2013-04-02 22:59:11 +0000 | [diff] [blame] | 1902 | return FwdDecl; |
Guy Benyei | 11169dd | 2012-12-18 14:30:41 +0000 | [diff] [blame] | 1903 | } |
| 1904 | |
Duncan P. N. Exon Smith | 9dd4e4e | 2015-04-29 16:40:08 +0000 | [diff] [blame] | 1905 | llvm::DIType *CGDebugInfo::CreateType(const ObjCObjectType *Ty, |
| 1906 | llvm::DIFile *Unit) { |
Guy Benyei | 11169dd | 2012-12-18 14:30:41 +0000 | [diff] [blame] | 1907 | // Ignore protocols. |
| 1908 | return getOrCreateType(Ty->getBaseType(), Unit); |
| 1909 | } |
| 1910 | |
Manman Ren | e6be26c | 2016-09-13 17:25:08 +0000 | [diff] [blame] | 1911 | llvm::DIType *CGDebugInfo::CreateType(const ObjCTypeParamType *Ty, |
| 1912 | llvm::DIFile *Unit) { |
| 1913 | // Ignore protocols. |
| 1914 | SourceLocation Loc = Ty->getDecl()->getLocation(); |
| 1915 | |
| 1916 | // Use Typedefs to represent ObjCTypeParamType. |
| 1917 | return DBuilder.createTypedef( |
| 1918 | getOrCreateType(Ty->getDecl()->getUnderlyingType(), Unit), |
| 1919 | Ty->getDecl()->getName(), getOrCreateFile(Loc), getLineNumber(Loc), |
| 1920 | getDeclContextDescriptor(Ty->getDecl())); |
| 1921 | } |
| 1922 | |
Adrian Prantl | b8fad1a | 2013-06-07 01:10:45 +0000 | [diff] [blame] | 1923 | /// \return true if Getter has the default name for the property PD. |
| 1924 | static bool hasDefaultGetterName(const ObjCPropertyDecl *PD, |
| 1925 | const ObjCMethodDecl *Getter) { |
| 1926 | assert(PD); |
| 1927 | if (!Getter) |
| 1928 | return true; |
| 1929 | |
| 1930 | assert(Getter->getDeclName().isObjCZeroArgSelector()); |
| 1931 | return PD->getName() == |
Eric Christopher | e7b87e5 | 2014-10-26 23:40:33 +0000 | [diff] [blame] | 1932 | Getter->getDeclName().getObjCSelector().getNameForSlot(0); |
Adrian Prantl | b8fad1a | 2013-06-07 01:10:45 +0000 | [diff] [blame] | 1933 | } |
| 1934 | |
| 1935 | /// \return true if Setter has the default name for the property PD. |
| 1936 | static bool hasDefaultSetterName(const ObjCPropertyDecl *PD, |
| 1937 | const ObjCMethodDecl *Setter) { |
| 1938 | assert(PD); |
| 1939 | if (!Setter) |
| 1940 | return true; |
| 1941 | |
| 1942 | assert(Setter->getDeclName().isObjCOneArgSelector()); |
Adrian Prantl | a4ce906 | 2013-06-07 22:29:12 +0000 | [diff] [blame] | 1943 | return SelectorTable::constructSetterName(PD->getName()) == |
Eric Christopher | e7b87e5 | 2014-10-26 23:40:33 +0000 | [diff] [blame] | 1944 | Setter->getDeclName().getObjCSelector().getNameForSlot(0); |
Adrian Prantl | b8fad1a | 2013-06-07 01:10:45 +0000 | [diff] [blame] | 1945 | } |
| 1946 | |
Duncan P. N. Exon Smith | 9dd4e4e | 2015-04-29 16:40:08 +0000 | [diff] [blame] | 1947 | llvm::DIType *CGDebugInfo::CreateType(const ObjCInterfaceType *Ty, |
| 1948 | llvm::DIFile *Unit) { |
Guy Benyei | 11169dd | 2012-12-18 14:30:41 +0000 | [diff] [blame] | 1949 | ObjCInterfaceDecl *ID = Ty->getDecl(); |
| 1950 | if (!ID) |
Duncan P. N. Exon Smith | 0b6c369 | 2015-04-20 18:51:48 +0000 | [diff] [blame] | 1951 | return nullptr; |
Guy Benyei | 11169dd | 2012-12-18 14:30:41 +0000 | [diff] [blame] | 1952 | |
Adrian Prantl | 50fd1a8 | 2016-04-20 23:59:32 +0000 | [diff] [blame] | 1953 | // Return a forward declaration if this type was imported from a clang module, |
| 1954 | // and this is not the compile unit with the implementation of the type (which |
| 1955 | // may contain hidden ivars). |
| 1956 | if (DebugTypeExtRefs && ID->isFromASTFile() && ID->getDefinition() && |
| 1957 | !ID->getImplementation()) |
Adrian Prantl | 5c8bd88 | 2015-09-11 17:23:08 +0000 | [diff] [blame] | 1958 | return DBuilder.createForwardDecl(llvm::dwarf::DW_TAG_structure_type, |
| 1959 | ID->getName(), |
| 1960 | getDeclContextDescriptor(ID), Unit, 0); |
| 1961 | |
Guy Benyei | 11169dd | 2012-12-18 14:30:41 +0000 | [diff] [blame] | 1962 | // Get overall information about the record type for the debug info. |
Duncan P. N. Exon Smith | 9dd4e4e | 2015-04-29 16:40:08 +0000 | [diff] [blame] | 1963 | llvm::DIFile *DefUnit = getOrCreateFile(ID->getLocation()); |
Guy Benyei | 11169dd | 2012-12-18 14:30:41 +0000 | [diff] [blame] | 1964 | unsigned Line = getLineNumber(ID->getLocation()); |
Duncan P. N. Exon Smith | 798d565 | 2015-04-15 23:19:15 +0000 | [diff] [blame] | 1965 | auto RuntimeLang = |
| 1966 | static_cast<llvm::dwarf::SourceLanguage>(TheCU->getSourceLanguage()); |
Guy Benyei | 11169dd | 2012-12-18 14:30:41 +0000 | [diff] [blame] | 1967 | |
| 1968 | // If this is just a forward declaration return a special forward-declaration |
| 1969 | // debug type since we won't be able to lay out the entire type. |
| 1970 | ObjCInterfaceDecl *Def = ID->getDefinition(); |
David Blaikie | ef8a951 | 2014-05-05 23:23:53 +0000 | [diff] [blame] | 1971 | if (!Def || !Def->getImplementation()) { |
Adrian Prantl | 42ce2d3 | 2015-10-01 16:57:02 +0000 | [diff] [blame] | 1972 | llvm::DIScope *Mod = getParentModuleOrNull(ID); |
Duncan P. N. Exon Smith | 9dd4e4e | 2015-04-29 16:40:08 +0000 | [diff] [blame] | 1973 | llvm::DIType *FwdDecl = DBuilder.createReplaceableCompositeType( |
Adrian Prantl | 42ce2d3 | 2015-10-01 16:57:02 +0000 | [diff] [blame] | 1974 | llvm::dwarf::DW_TAG_structure_type, ID->getName(), Mod ? Mod : TheCU, |
| 1975 | DefUnit, Line, RuntimeLang); |
David Blaikie | ef8a951 | 2014-05-05 23:23:53 +0000 | [diff] [blame] | 1976 | ObjCInterfaceCache.push_back(ObjCInterfaceCacheEntry(Ty, FwdDecl, Unit)); |
Guy Benyei | 11169dd | 2012-12-18 14:30:41 +0000 | [diff] [blame] | 1977 | return FwdDecl; |
| 1978 | } |
| 1979 | |
David Blaikie | ef8a951 | 2014-05-05 23:23:53 +0000 | [diff] [blame] | 1980 | return CreateTypeDefinition(Ty, Unit); |
| 1981 | } |
| 1982 | |
Adrian Prantl | c4bb47e | 2015-06-30 17:39:51 +0000 | [diff] [blame] | 1983 | llvm::DIModule * |
Adrian Prantl | 6668920 | 2015-09-18 23:01:45 +0000 | [diff] [blame] | 1984 | CGDebugInfo::getOrCreateModuleRef(ExternalASTSource::ASTSourceDescriptor Mod, |
| 1985 | bool CreateSkeletonCU) { |
Adrian Prantl | eb66a26 | 2015-09-24 16:10:04 +0000 | [diff] [blame] | 1986 | // Use the Module pointer as the key into the cache. This is a |
| 1987 | // nullptr if the "Module" is a PCH, which is safe because we don't |
| 1988 | // support chained PCH debug info, so there can only be a single PCH. |
| 1989 | const Module *M = Mod.getModuleOrNull(); |
Adrian Prantl | 9e8ea35 | 2015-09-29 20:44:46 +0000 | [diff] [blame] | 1990 | auto ModRef = ModuleCache.find(M); |
| 1991 | if (ModRef != ModuleCache.end()) |
| 1992 | return cast<llvm::DIModule>(ModRef->second); |
Adrian Prantl | 2388ead | 2015-06-30 18:01:05 +0000 | [diff] [blame] | 1993 | |
| 1994 | // Macro definitions that were defined with "-D" on the command line. |
| 1995 | SmallString<128> ConfigMacros; |
| 1996 | { |
| 1997 | llvm::raw_svector_ostream OS(ConfigMacros); |
| 1998 | const auto &PPOpts = CGM.getPreprocessorOpts(); |
| 1999 | unsigned I = 0; |
| 2000 | // Translate the macro definitions back into a commmand line. |
| 2001 | for (auto &M : PPOpts.Macros) { |
| 2002 | if (++I > 1) |
| 2003 | OS << " "; |
| 2004 | const std::string &Macro = M.first; |
| 2005 | bool Undef = M.second; |
| 2006 | OS << "\"-" << (Undef ? 'U' : 'D'); |
| 2007 | for (char c : Macro) |
| 2008 | switch (c) { |
| 2009 | case '\\' : OS << "\\\\"; break; |
| 2010 | case '"' : OS << "\\\""; break; |
| 2011 | default: OS << c; |
| 2012 | } |
| 2013 | OS << '\"'; |
Adrian Prantl | c4bb47e | 2015-06-30 17:39:51 +0000 | [diff] [blame] | 2014 | } |
Adrian Prantl | c4bb47e | 2015-06-30 17:39:51 +0000 | [diff] [blame] | 2015 | } |
Adrian Prantl | 6668920 | 2015-09-18 23:01:45 +0000 | [diff] [blame] | 2016 | |
Adrian Prantl | 835e663 | 2015-09-24 16:10:10 +0000 | [diff] [blame] | 2017 | bool IsRootModule = M ? !M->Parent : true; |
| 2018 | if (CreateSkeletonCU && IsRootModule) { |
Adrian Prantl | c96da8f | 2016-01-22 17:43:43 +0000 | [diff] [blame] | 2019 | // PCH files don't have a signature field in the control block, |
| 2020 | // but LLVM detects skeleton CUs by looking for a non-zero DWO id. |
Adrian Prantl | 98bfc82 | 2016-01-22 19:29:41 +0000 | [diff] [blame] | 2021 | uint64_t Signature = Mod.getSignature() ? Mod.getSignature() : ~1ULL; |
Adrian Prantl | 6668920 | 2015-09-18 23:01:45 +0000 | [diff] [blame] | 2022 | llvm::DIBuilder DIB(CGM.getModule()); |
Amjad Aboud | fa9a17e | 2016-12-14 20:24:40 +0000 | [diff] [blame] | 2023 | DIB.createCompileUnit(TheCU->getSourceLanguage(), |
| 2024 | DIB.createFile(Mod.getModuleName(), Mod.getPath()), |
| 2025 | TheCU->getProducer(), true, StringRef(), 0, |
| 2026 | Mod.getASTFile(), llvm::DICompileUnit::FullDebug, |
| 2027 | Signature); |
Adrian Prantl | 6668920 | 2015-09-18 23:01:45 +0000 | [diff] [blame] | 2028 | DIB.finalize(); |
Adrian Prantl | 2f957ac | 2015-09-19 00:59:22 +0000 | [diff] [blame] | 2029 | } |
Adrian Prantl | 835e663 | 2015-09-24 16:10:10 +0000 | [diff] [blame] | 2030 | llvm::DIModule *Parent = |
| 2031 | IsRootModule ? nullptr |
| 2032 | : getOrCreateModuleRef( |
| 2033 | ExternalASTSource::ASTSourceDescriptor(*M->Parent), |
| 2034 | CreateSkeletonCU); |
Adrian Prantl | eb66a26 | 2015-09-24 16:10:04 +0000 | [diff] [blame] | 2035 | llvm::DIModule *DIMod = |
Adrian Prantl | 835e663 | 2015-09-24 16:10:10 +0000 | [diff] [blame] | 2036 | DBuilder.createModule(Parent, Mod.getModuleName(), ConfigMacros, |
| 2037 | Mod.getPath(), CGM.getHeaderSearchOpts().Sysroot); |
Adrian Prantl | 9e8ea35 | 2015-09-29 20:44:46 +0000 | [diff] [blame] | 2038 | ModuleCache[M].reset(DIMod); |
Adrian Prantl | eb66a26 | 2015-09-24 16:10:04 +0000 | [diff] [blame] | 2039 | return DIMod; |
Adrian Prantl | c4bb47e | 2015-06-30 17:39:51 +0000 | [diff] [blame] | 2040 | } |
| 2041 | |
Duncan P. N. Exon Smith | 9dd4e4e | 2015-04-29 16:40:08 +0000 | [diff] [blame] | 2042 | llvm::DIType *CGDebugInfo::CreateTypeDefinition(const ObjCInterfaceType *Ty, |
| 2043 | llvm::DIFile *Unit) { |
David Blaikie | ef8a951 | 2014-05-05 23:23:53 +0000 | [diff] [blame] | 2044 | ObjCInterfaceDecl *ID = Ty->getDecl(); |
Duncan P. N. Exon Smith | 9dd4e4e | 2015-04-29 16:40:08 +0000 | [diff] [blame] | 2045 | llvm::DIFile *DefUnit = getOrCreateFile(ID->getLocation()); |
David Blaikie | ef8a951 | 2014-05-05 23:23:53 +0000 | [diff] [blame] | 2046 | unsigned Line = getLineNumber(ID->getLocation()); |
Duncan P. N. Exon Smith | 798d565 | 2015-04-15 23:19:15 +0000 | [diff] [blame] | 2047 | unsigned RuntimeLang = TheCU->getSourceLanguage(); |
Guy Benyei | 11169dd | 2012-12-18 14:30:41 +0000 | [diff] [blame] | 2048 | |
| 2049 | // Bit size, align and offset of the type. |
| 2050 | uint64_t Size = CGM.getContext().getTypeSize(Ty); |
Victor Leschuk | a7ece03 | 2016-10-20 00:13:19 +0000 | [diff] [blame] | 2051 | auto Align = getTypeAlignIfRequired(Ty, CGM.getContext()); |
Guy Benyei | 11169dd | 2012-12-18 14:30:41 +0000 | [diff] [blame] | 2052 | |
Leny Kholodov | 80c047d | 2016-09-06 10:48:04 +0000 | [diff] [blame] | 2053 | llvm::DINode::DIFlags Flags = llvm::DINode::FlagZero; |
Guy Benyei | 11169dd | 2012-12-18 14:30:41 +0000 | [diff] [blame] | 2054 | if (ID->getImplementation()) |
Duncan P. N. Exon Smith | 9dd4e4e | 2015-04-29 16:40:08 +0000 | [diff] [blame] | 2055 | Flags |= llvm::DINode::FlagObjcClassComplete; |
Guy Benyei | 11169dd | 2012-12-18 14:30:41 +0000 | [diff] [blame] | 2056 | |
Adrian Prantl | fd69611 | 2015-10-01 00:48:51 +0000 | [diff] [blame] | 2057 | llvm::DIScope *Mod = getParentModuleOrNull(ID); |
Duncan P. N. Exon Smith | 9dd4e4e | 2015-04-29 16:40:08 +0000 | [diff] [blame] | 2058 | llvm::DICompositeType *RealDecl = DBuilder.createStructType( |
Adrian Prantl | fd69611 | 2015-10-01 00:48:51 +0000 | [diff] [blame] | 2059 | Mod ? Mod : Unit, ID->getName(), DefUnit, Line, Size, Align, Flags, |
| 2060 | nullptr, llvm::DINodeArray(), RuntimeLang); |
Guy Benyei | 11169dd | 2012-12-18 14:30:41 +0000 | [diff] [blame] | 2061 | |
David Blaikie | ef8a951 | 2014-05-05 23:23:53 +0000 | [diff] [blame] | 2062 | QualType QTy(Ty, 0); |
Duncan P. N. Exon Smith | fb49491 | 2014-12-09 18:39:32 +0000 | [diff] [blame] | 2063 | TypeCache[QTy.getAsOpaquePtr()].reset(RealDecl); |
Guy Benyei | 11169dd | 2012-12-18 14:30:41 +0000 | [diff] [blame] | 2064 | |
Eric Christopher | 35f1f9f | 2013-07-14 21:00:07 +0000 | [diff] [blame] | 2065 | // Push the struct on region stack. |
Duncan P. N. Exon Smith | 4078ad4 | 2015-04-16 16:36:45 +0000 | [diff] [blame] | 2066 | LexicalBlockStack.emplace_back(RealDecl); |
Duncan P. N. Exon Smith | fb49491 | 2014-12-09 18:39:32 +0000 | [diff] [blame] | 2067 | RegionMap[Ty->getDecl()].reset(RealDecl); |
Guy Benyei | 11169dd | 2012-12-18 14:30:41 +0000 | [diff] [blame] | 2068 | |
| 2069 | // Convert all the elements. |
Duncan P. N. Exon Smith | fb49491 | 2014-12-09 18:39:32 +0000 | [diff] [blame] | 2070 | SmallVector<llvm::Metadata *, 16> EltTys; |
Guy Benyei | 11169dd | 2012-12-18 14:30:41 +0000 | [diff] [blame] | 2071 | |
| 2072 | ObjCInterfaceDecl *SClass = ID->getSuperClass(); |
| 2073 | if (SClass) { |
Duncan P. N. Exon Smith | 9dd4e4e | 2015-04-29 16:40:08 +0000 | [diff] [blame] | 2074 | llvm::DIType *SClassTy = |
Eric Christopher | e7b87e5 | 2014-10-26 23:40:33 +0000 | [diff] [blame] | 2075 | getOrCreateType(CGM.getContext().getObjCInterfaceType(SClass), Unit); |
Duncan P. N. Exon Smith | b747023 | 2015-04-15 23:48:50 +0000 | [diff] [blame] | 2076 | if (!SClassTy) |
Duncan P. N. Exon Smith | 0b6c369 | 2015-04-20 18:51:48 +0000 | [diff] [blame] | 2077 | return nullptr; |
Eric Christopher | b2a008c | 2013-05-16 00:45:12 +0000 | [diff] [blame] | 2078 | |
Leny Kholodov | df050fd | 2016-09-06 17:06:14 +0000 | [diff] [blame] | 2079 | llvm::DIType *InhTag = DBuilder.createInheritance(RealDecl, SClassTy, 0, |
| 2080 | llvm::DINode::FlagZero); |
Guy Benyei | 11169dd | 2012-12-18 14:30:41 +0000 | [diff] [blame] | 2081 | EltTys.push_back(InhTag); |
| 2082 | } |
| 2083 | |
Eric Christopher | 35f1f9f | 2013-07-14 21:00:07 +0000 | [diff] [blame] | 2084 | // Create entries for all of the properties. |
Nico Weber | 7123bca | 2015-12-04 19:14:14 +0000 | [diff] [blame] | 2085 | auto AddProperty = [&](const ObjCPropertyDecl *PD) { |
Guy Benyei | 11169dd | 2012-12-18 14:30:41 +0000 | [diff] [blame] | 2086 | SourceLocation Loc = PD->getLocation(); |
Duncan P. N. Exon Smith | 9dd4e4e | 2015-04-29 16:40:08 +0000 | [diff] [blame] | 2087 | llvm::DIFile *PUnit = getOrCreateFile(Loc); |
Guy Benyei | 11169dd | 2012-12-18 14:30:41 +0000 | [diff] [blame] | 2088 | unsigned PLine = getLineNumber(Loc); |
| 2089 | ObjCMethodDecl *Getter = PD->getGetterMethodDecl(); |
| 2090 | ObjCMethodDecl *Setter = PD->getSetterMethodDecl(); |
Eric Christopher | e7b87e5 | 2014-10-26 23:40:33 +0000 | [diff] [blame] | 2091 | llvm::MDNode *PropertyNode = DBuilder.createObjCProperty( |
| 2092 | PD->getName(), PUnit, PLine, |
| 2093 | hasDefaultGetterName(PD, Getter) ? "" |
| 2094 | : getSelectorName(PD->getGetterName()), |
| 2095 | hasDefaultSetterName(PD, Setter) ? "" |
| 2096 | : getSelectorName(PD->getSetterName()), |
| 2097 | PD->getPropertyAttributes(), getOrCreateType(PD->getType(), PUnit)); |
Guy Benyei | 11169dd | 2012-12-18 14:30:41 +0000 | [diff] [blame] | 2098 | EltTys.push_back(PropertyNode); |
Nico Weber | 7123bca | 2015-12-04 19:14:14 +0000 | [diff] [blame] | 2099 | }; |
| 2100 | { |
| 2101 | llvm::SmallPtrSet<const IdentifierInfo*, 16> PropertySet; |
Nico Weber | de059e1 | 2015-12-04 19:35:45 +0000 | [diff] [blame] | 2102 | for (const ObjCCategoryDecl *ClassExt : ID->known_extensions()) |
Manman Ren | efe1bac | 2016-01-27 20:00:32 +0000 | [diff] [blame] | 2103 | for (auto *PD : ClassExt->properties()) { |
Nico Weber | de059e1 | 2015-12-04 19:35:45 +0000 | [diff] [blame] | 2104 | PropertySet.insert(PD->getIdentifier()); |
| 2105 | AddProperty(PD); |
| 2106 | } |
Manman Ren | efe1bac | 2016-01-27 20:00:32 +0000 | [diff] [blame] | 2107 | for (const auto *PD : ID->properties()) { |
Nico Weber | 7123bca | 2015-12-04 19:14:14 +0000 | [diff] [blame] | 2108 | // Don't emit duplicate metadata for properties that were already in a |
| 2109 | // class extension. |
| 2110 | if (!PropertySet.insert(PD->getIdentifier()).second) |
| 2111 | continue; |
| 2112 | AddProperty(PD); |
| 2113 | } |
Guy Benyei | 11169dd | 2012-12-18 14:30:41 +0000 | [diff] [blame] | 2114 | } |
| 2115 | |
| 2116 | const ASTRecordLayout &RL = CGM.getContext().getASTObjCInterfaceLayout(ID); |
| 2117 | unsigned FieldNo = 0; |
| 2118 | for (ObjCIvarDecl *Field = ID->all_declared_ivar_begin(); Field; |
| 2119 | Field = Field->getNextIvar(), ++FieldNo) { |
Duncan P. N. Exon Smith | 9dd4e4e | 2015-04-29 16:40:08 +0000 | [diff] [blame] | 2120 | llvm::DIType *FieldTy = getOrCreateType(Field->getType(), Unit); |
Duncan P. N. Exon Smith | b747023 | 2015-04-15 23:48:50 +0000 | [diff] [blame] | 2121 | if (!FieldTy) |
Duncan P. N. Exon Smith | 0b6c369 | 2015-04-20 18:51:48 +0000 | [diff] [blame] | 2122 | return nullptr; |
Eric Christopher | b2a008c | 2013-05-16 00:45:12 +0000 | [diff] [blame] | 2123 | |
Guy Benyei | 11169dd | 2012-12-18 14:30:41 +0000 | [diff] [blame] | 2124 | StringRef FieldName = Field->getName(); |
| 2125 | |
| 2126 | // Ignore unnamed fields. |
| 2127 | if (FieldName.empty()) |
| 2128 | continue; |
| 2129 | |
| 2130 | // Get the location for the field. |
Duncan P. N. Exon Smith | 9dd4e4e | 2015-04-29 16:40:08 +0000 | [diff] [blame] | 2131 | llvm::DIFile *FieldDefUnit = getOrCreateFile(Field->getLocation()); |
Guy Benyei | 11169dd | 2012-12-18 14:30:41 +0000 | [diff] [blame] | 2132 | unsigned FieldLine = getLineNumber(Field->getLocation()); |
| 2133 | QualType FType = Field->getType(); |
| 2134 | uint64_t FieldSize = 0; |
Victor Leschuk | 802e4a5 | 2016-10-19 22:11:07 +0000 | [diff] [blame] | 2135 | uint32_t FieldAlign = 0; |
Guy Benyei | 11169dd | 2012-12-18 14:30:41 +0000 | [diff] [blame] | 2136 | |
| 2137 | if (!FType->isIncompleteArrayType()) { |
| 2138 | |
| 2139 | // Bit size, align and offset of the type. |
| 2140 | FieldSize = Field->isBitField() |
Eric Christopher | 35f1f9f | 2013-07-14 21:00:07 +0000 | [diff] [blame] | 2141 | ? Field->getBitWidthValue(CGM.getContext()) |
| 2142 | : CGM.getContext().getTypeSize(FType); |
Victor Leschuk | a7ece03 | 2016-10-20 00:13:19 +0000 | [diff] [blame] | 2143 | FieldAlign = getTypeAlignIfRequired(FType, CGM.getContext()); |
Guy Benyei | 11169dd | 2012-12-18 14:30:41 +0000 | [diff] [blame] | 2144 | } |
| 2145 | |
| 2146 | uint64_t FieldOffset; |
| 2147 | if (CGM.getLangOpts().ObjCRuntime.isNonFragile()) { |
| 2148 | // We don't know the runtime offset of an ivar if we're using the |
| 2149 | // non-fragile ABI. For bitfields, use the bit offset into the first |
| 2150 | // byte of storage of the bitfield. For other fields, use zero. |
| 2151 | if (Field->isBitField()) { |
Eric Christopher | e7b87e5 | 2014-10-26 23:40:33 +0000 | [diff] [blame] | 2152 | FieldOffset = |
| 2153 | CGM.getObjCRuntime().ComputeBitfieldBitOffset(CGM, ID, Field); |
Guy Benyei | 11169dd | 2012-12-18 14:30:41 +0000 | [diff] [blame] | 2154 | FieldOffset %= CGM.getContext().getCharWidth(); |
| 2155 | } else { |
| 2156 | FieldOffset = 0; |
| 2157 | } |
| 2158 | } else { |
| 2159 | FieldOffset = RL.getFieldOffset(FieldNo); |
| 2160 | } |
| 2161 | |
Leny Kholodov | 80c047d | 2016-09-06 10:48:04 +0000 | [diff] [blame] | 2162 | llvm::DINode::DIFlags Flags = llvm::DINode::FlagZero; |
Guy Benyei | 11169dd | 2012-12-18 14:30:41 +0000 | [diff] [blame] | 2163 | if (Field->getAccessControl() == ObjCIvarDecl::Protected) |
Duncan P. N. Exon Smith | 9dd4e4e | 2015-04-29 16:40:08 +0000 | [diff] [blame] | 2164 | Flags = llvm::DINode::FlagProtected; |
Guy Benyei | 11169dd | 2012-12-18 14:30:41 +0000 | [diff] [blame] | 2165 | else if (Field->getAccessControl() == ObjCIvarDecl::Private) |
Duncan P. N. Exon Smith | 9dd4e4e | 2015-04-29 16:40:08 +0000 | [diff] [blame] | 2166 | Flags = llvm::DINode::FlagPrivate; |
Adrian Prantl | 21361fb | 2014-08-29 22:44:27 +0000 | [diff] [blame] | 2167 | else if (Field->getAccessControl() == ObjCIvarDecl::Public) |
Duncan P. N. Exon Smith | 9dd4e4e | 2015-04-29 16:40:08 +0000 | [diff] [blame] | 2168 | Flags = llvm::DINode::FlagPublic; |
Guy Benyei | 11169dd | 2012-12-18 14:30:41 +0000 | [diff] [blame] | 2169 | |
Craig Topper | 8a13c41 | 2014-05-21 05:09:00 +0000 | [diff] [blame] | 2170 | llvm::MDNode *PropertyNode = nullptr; |
Guy Benyei | 11169dd | 2012-12-18 14:30:41 +0000 | [diff] [blame] | 2171 | if (ObjCImplementationDecl *ImpD = ID->getImplementation()) { |
Eric Christopher | b2a008c | 2013-05-16 00:45:12 +0000 | [diff] [blame] | 2172 | if (ObjCPropertyImplDecl *PImpD = |
Eric Christopher | e7b87e5 | 2014-10-26 23:40:33 +0000 | [diff] [blame] | 2173 | ImpD->FindPropertyImplIvarDecl(Field->getIdentifier())) { |
Guy Benyei | 11169dd | 2012-12-18 14:30:41 +0000 | [diff] [blame] | 2174 | if (ObjCPropertyDecl *PD = PImpD->getPropertyDecl()) { |
Eric Christopher | c0c5d46 | 2013-02-21 22:35:08 +0000 | [diff] [blame] | 2175 | SourceLocation Loc = PD->getLocation(); |
Duncan P. N. Exon Smith | 9dd4e4e | 2015-04-29 16:40:08 +0000 | [diff] [blame] | 2176 | llvm::DIFile *PUnit = getOrCreateFile(Loc); |
Eric Christopher | c0c5d46 | 2013-02-21 22:35:08 +0000 | [diff] [blame] | 2177 | unsigned PLine = getLineNumber(Loc); |
Guy Benyei | 11169dd | 2012-12-18 14:30:41 +0000 | [diff] [blame] | 2178 | ObjCMethodDecl *Getter = PD->getGetterMethodDecl(); |
| 2179 | ObjCMethodDecl *Setter = PD->getSetterMethodDecl(); |
Eric Christopher | e7b87e5 | 2014-10-26 23:40:33 +0000 | [diff] [blame] | 2180 | PropertyNode = DBuilder.createObjCProperty( |
| 2181 | PD->getName(), PUnit, PLine, |
| 2182 | hasDefaultGetterName(PD, Getter) ? "" : getSelectorName( |
| 2183 | PD->getGetterName()), |
| 2184 | hasDefaultSetterName(PD, Setter) ? "" : getSelectorName( |
| 2185 | PD->getSetterName()), |
| 2186 | PD->getPropertyAttributes(), |
| 2187 | getOrCreateType(PD->getType(), PUnit)); |
Guy Benyei | 11169dd | 2012-12-18 14:30:41 +0000 | [diff] [blame] | 2188 | } |
| 2189 | } |
| 2190 | } |
Eric Christopher | e7b87e5 | 2014-10-26 23:40:33 +0000 | [diff] [blame] | 2191 | FieldTy = DBuilder.createObjCIVar(FieldName, FieldDefUnit, FieldLine, |
| 2192 | FieldSize, FieldAlign, FieldOffset, Flags, |
| 2193 | FieldTy, PropertyNode); |
Guy Benyei | 11169dd | 2012-12-18 14:30:41 +0000 | [diff] [blame] | 2194 | EltTys.push_back(FieldTy); |
| 2195 | } |
| 2196 | |
Duncan P. N. Exon Smith | 9dd4e4e | 2015-04-29 16:40:08 +0000 | [diff] [blame] | 2197 | llvm::DINodeArray Elements = DBuilder.getOrCreateArray(EltTys); |
Duncan P. N. Exon Smith | c8ee63e | 2014-12-18 00:48:56 +0000 | [diff] [blame] | 2198 | DBuilder.replaceArrays(RealDecl, Elements); |
Adrian Prantl | a03a85a | 2013-03-06 22:03:30 +0000 | [diff] [blame] | 2199 | |
Guy Benyei | 11169dd | 2012-12-18 14:30:41 +0000 | [diff] [blame] | 2200 | LexicalBlockStack.pop_back(); |
Eric Christopher | 5c7ee8b | 2013-04-02 22:59:11 +0000 | [diff] [blame] | 2201 | return RealDecl; |
Guy Benyei | 11169dd | 2012-12-18 14:30:41 +0000 | [diff] [blame] | 2202 | } |
| 2203 | |
Duncan P. N. Exon Smith | 9dd4e4e | 2015-04-29 16:40:08 +0000 | [diff] [blame] | 2204 | llvm::DIType *CGDebugInfo::CreateType(const VectorType *Ty, |
| 2205 | llvm::DIFile *Unit) { |
| 2206 | llvm::DIType *ElementTy = getOrCreateType(Ty->getElementType(), Unit); |
Guy Benyei | 11169dd | 2012-12-18 14:30:41 +0000 | [diff] [blame] | 2207 | int64_t Count = Ty->getNumElements(); |
| 2208 | if (Count == 0) |
| 2209 | // If number of elements are not known then this is an unbounded array. |
| 2210 | // Use Count == -1 to express such arrays. |
| 2211 | Count = -1; |
| 2212 | |
Duncan P. N. Exon Smith | fb49491 | 2014-12-09 18:39:32 +0000 | [diff] [blame] | 2213 | llvm::Metadata *Subscript = DBuilder.getOrCreateSubrange(0, Count); |
Duncan P. N. Exon Smith | 9dd4e4e | 2015-04-29 16:40:08 +0000 | [diff] [blame] | 2214 | llvm::DINodeArray SubscriptArray = DBuilder.getOrCreateArray(Subscript); |
Guy Benyei | 11169dd | 2012-12-18 14:30:41 +0000 | [diff] [blame] | 2215 | |
| 2216 | uint64_t Size = CGM.getContext().getTypeSize(Ty); |
Victor Leschuk | a7ece03 | 2016-10-20 00:13:19 +0000 | [diff] [blame] | 2217 | auto Align = getTypeAlignIfRequired(Ty, CGM.getContext()); |
Guy Benyei | 11169dd | 2012-12-18 14:30:41 +0000 | [diff] [blame] | 2218 | |
| 2219 | return DBuilder.createVectorType(Size, Align, ElementTy, SubscriptArray); |
| 2220 | } |
| 2221 | |
Duncan P. N. Exon Smith | 9dd4e4e | 2015-04-29 16:40:08 +0000 | [diff] [blame] | 2222 | llvm::DIType *CGDebugInfo::CreateType(const ArrayType *Ty, llvm::DIFile *Unit) { |
Guy Benyei | 11169dd | 2012-12-18 14:30:41 +0000 | [diff] [blame] | 2223 | uint64_t Size; |
Victor Leschuk | 802e4a5 | 2016-10-19 22:11:07 +0000 | [diff] [blame] | 2224 | uint32_t Align; |
Guy Benyei | 11169dd | 2012-12-18 14:30:41 +0000 | [diff] [blame] | 2225 | |
| 2226 | // FIXME: make getTypeAlign() aware of VLAs and incomplete array types |
David Majnemer | 58ed0f3 | 2016-07-17 00:39:12 +0000 | [diff] [blame] | 2227 | if (const auto *VAT = dyn_cast<VariableArrayType>(Ty)) { |
Guy Benyei | 11169dd | 2012-12-18 14:30:41 +0000 | [diff] [blame] | 2228 | Size = 0; |
Victor Leschuk | a7ece03 | 2016-10-20 00:13:19 +0000 | [diff] [blame] | 2229 | Align = getTypeAlignIfRequired(CGM.getContext().getBaseElementType(VAT), |
| 2230 | CGM.getContext()); |
Guy Benyei | 11169dd | 2012-12-18 14:30:41 +0000 | [diff] [blame] | 2231 | } else if (Ty->isIncompleteArrayType()) { |
| 2232 | Size = 0; |
| 2233 | if (Ty->getElementType()->isIncompleteType()) |
| 2234 | Align = 0; |
| 2235 | else |
Victor Leschuk | a7ece03 | 2016-10-20 00:13:19 +0000 | [diff] [blame] | 2236 | Align = getTypeAlignIfRequired(Ty->getElementType(), CGM.getContext()); |
David Blaikie | f03b2e8 | 2013-05-09 20:48:12 +0000 | [diff] [blame] | 2237 | } else if (Ty->isIncompleteType()) { |
Guy Benyei | 11169dd | 2012-12-18 14:30:41 +0000 | [diff] [blame] | 2238 | Size = 0; |
| 2239 | Align = 0; |
| 2240 | } else { |
| 2241 | // Size and align of the whole array, not the element type. |
| 2242 | Size = CGM.getContext().getTypeSize(Ty); |
Victor Leschuk | a7ece03 | 2016-10-20 00:13:19 +0000 | [diff] [blame] | 2243 | Align = getTypeAlignIfRequired(Ty, CGM.getContext()); |
Guy Benyei | 11169dd | 2012-12-18 14:30:41 +0000 | [diff] [blame] | 2244 | } |
| 2245 | |
| 2246 | // Add the dimensions of the array. FIXME: This loses CV qualifiers from |
| 2247 | // interior arrays, do we care? Why aren't nested arrays represented the |
| 2248 | // obvious/recursive way? |
Duncan P. N. Exon Smith | fb49491 | 2014-12-09 18:39:32 +0000 | [diff] [blame] | 2249 | SmallVector<llvm::Metadata *, 8> Subscripts; |
Guy Benyei | 11169dd | 2012-12-18 14:30:41 +0000 | [diff] [blame] | 2250 | QualType EltTy(Ty, 0); |
| 2251 | while ((Ty = dyn_cast<ArrayType>(EltTy))) { |
| 2252 | // If the number of elements is known, then count is that number. Otherwise, |
| 2253 | // it's -1. This allows us to represent a subrange with an array of 0 |
| 2254 | // elements, like this: |
| 2255 | // |
| 2256 | // struct foo { |
| 2257 | // int x[0]; |
| 2258 | // }; |
Eric Christopher | e7b87e5 | 2014-10-26 23:40:33 +0000 | [diff] [blame] | 2259 | int64_t Count = -1; // Count == -1 is an unbounded array. |
David Majnemer | 58ed0f3 | 2016-07-17 00:39:12 +0000 | [diff] [blame] | 2260 | if (const auto *CAT = dyn_cast<ConstantArrayType>(Ty)) |
Guy Benyei | 11169dd | 2012-12-18 14:30:41 +0000 | [diff] [blame] | 2261 | Count = CAT->getSize().getZExtValue(); |
David Blaikie | 87173f1 | 2016-08-22 17:49:56 +0000 | [diff] [blame] | 2262 | else if (const auto *VAT = dyn_cast<VariableArrayType>(Ty)) { |
Eli Friedman | 01d6b96 | 2016-10-19 22:16:32 +0000 | [diff] [blame] | 2263 | if (Expr *Size = VAT->getSizeExpr()) { |
| 2264 | llvm::APSInt V; |
| 2265 | if (Size->EvaluateAsInt(V, CGM.getContext())) |
| 2266 | Count = V.getExtValue(); |
| 2267 | } |
David Blaikie | 87173f1 | 2016-08-22 17:49:56 +0000 | [diff] [blame] | 2268 | } |
Eric Christopher | b2a008c | 2013-05-16 00:45:12 +0000 | [diff] [blame] | 2269 | |
Guy Benyei | 11169dd | 2012-12-18 14:30:41 +0000 | [diff] [blame] | 2270 | // FIXME: Verify this is right for VLAs. |
| 2271 | Subscripts.push_back(DBuilder.getOrCreateSubrange(0, Count)); |
| 2272 | EltTy = Ty->getElementType(); |
| 2273 | } |
| 2274 | |
Duncan P. N. Exon Smith | 9dd4e4e | 2015-04-29 16:40:08 +0000 | [diff] [blame] | 2275 | llvm::DINodeArray SubscriptArray = DBuilder.getOrCreateArray(Subscripts); |
Guy Benyei | 11169dd | 2012-12-18 14:30:41 +0000 | [diff] [blame] | 2276 | |
Duncan P. N. Exon Smith | 0b6c369 | 2015-04-20 18:51:48 +0000 | [diff] [blame] | 2277 | return DBuilder.createArrayType(Size, Align, getOrCreateType(EltTy, Unit), |
| 2278 | SubscriptArray); |
Guy Benyei | 11169dd | 2012-12-18 14:30:41 +0000 | [diff] [blame] | 2279 | } |
| 2280 | |
Duncan P. N. Exon Smith | 9dd4e4e | 2015-04-29 16:40:08 +0000 | [diff] [blame] | 2281 | llvm::DIType *CGDebugInfo::CreateType(const LValueReferenceType *Ty, |
| 2282 | llvm::DIFile *Unit) { |
Eric Christopher | e7b87e5 | 2014-10-26 23:40:33 +0000 | [diff] [blame] | 2283 | return CreatePointerLikeType(llvm::dwarf::DW_TAG_reference_type, Ty, |
| 2284 | Ty->getPointeeType(), Unit); |
Guy Benyei | 11169dd | 2012-12-18 14:30:41 +0000 | [diff] [blame] | 2285 | } |
| 2286 | |
Duncan P. N. Exon Smith | 9dd4e4e | 2015-04-29 16:40:08 +0000 | [diff] [blame] | 2287 | llvm::DIType *CGDebugInfo::CreateType(const RValueReferenceType *Ty, |
| 2288 | llvm::DIFile *Unit) { |
Eric Christopher | e7b87e5 | 2014-10-26 23:40:33 +0000 | [diff] [blame] | 2289 | return CreatePointerLikeType(llvm::dwarf::DW_TAG_rvalue_reference_type, Ty, |
| 2290 | Ty->getPointeeType(), Unit); |
Guy Benyei | 11169dd | 2012-12-18 14:30:41 +0000 | [diff] [blame] | 2291 | } |
| 2292 | |
Duncan P. N. Exon Smith | 9dd4e4e | 2015-04-29 16:40:08 +0000 | [diff] [blame] | 2293 | llvm::DIType *CGDebugInfo::CreateType(const MemberPointerType *Ty, |
| 2294 | llvm::DIFile *U) { |
Leny Kholodov | 80c047d | 2016-09-06 10:48:04 +0000 | [diff] [blame] | 2295 | llvm::DINode::DIFlags Flags = llvm::DINode::FlagZero; |
Reid Kleckner | fb72718 | 2016-06-17 22:27:59 +0000 | [diff] [blame] | 2296 | uint64_t Size = 0; |
| 2297 | |
| 2298 | if (!Ty->isIncompleteType()) { |
| 2299 | Size = CGM.getContext().getTypeSize(Ty); |
| 2300 | |
| 2301 | // Set the MS inheritance model. There is no flag for the unspecified model. |
| 2302 | if (CGM.getTarget().getCXXABI().isMicrosoft()) { |
| 2303 | switch (Ty->getMostRecentCXXRecordDecl()->getMSInheritanceModel()) { |
| 2304 | case MSInheritanceAttr::Keyword_single_inheritance: |
| 2305 | Flags |= llvm::DINode::FlagSingleInheritance; |
| 2306 | break; |
| 2307 | case MSInheritanceAttr::Keyword_multiple_inheritance: |
| 2308 | Flags |= llvm::DINode::FlagMultipleInheritance; |
| 2309 | break; |
| 2310 | case MSInheritanceAttr::Keyword_virtual_inheritance: |
| 2311 | Flags |= llvm::DINode::FlagVirtualInheritance; |
| 2312 | break; |
| 2313 | case MSInheritanceAttr::Keyword_unspecified_inheritance: |
| 2314 | break; |
| 2315 | } |
| 2316 | } |
| 2317 | } |
| 2318 | |
Duncan P. N. Exon Smith | 9dd4e4e | 2015-04-29 16:40:08 +0000 | [diff] [blame] | 2319 | llvm::DIType *ClassType = getOrCreateType(QualType(Ty->getClass(), 0), U); |
David Majnemer | 5fd33e0 | 2015-04-24 01:25:08 +0000 | [diff] [blame] | 2320 | if (Ty->isMemberDataPointerType()) |
David Blaikie | 2c705ca | 2013-01-19 19:20:56 +0000 | [diff] [blame] | 2321 | return DBuilder.createMemberPointerType( |
Reid Kleckner | fb72718 | 2016-06-17 22:27:59 +0000 | [diff] [blame] | 2322 | getOrCreateType(Ty->getPointeeType(), U), ClassType, Size, /*Align=*/0, |
| 2323 | Flags); |
Adrian Prantl | 0866acd | 2013-12-19 01:38:47 +0000 | [diff] [blame] | 2324 | |
| 2325 | const FunctionProtoType *FPT = |
Eric Christopher | e7b87e5 | 2014-10-26 23:40:33 +0000 | [diff] [blame] | 2326 | Ty->getPointeeType()->getAs<FunctionProtoType>(); |
| 2327 | return DBuilder.createMemberPointerType( |
| 2328 | getOrCreateInstanceMethodType(CGM.getContext().getPointerType(QualType( |
| 2329 | Ty->getClass(), FPT->getTypeQuals())), |
| 2330 | FPT, U), |
Reid Kleckner | fb72718 | 2016-06-17 22:27:59 +0000 | [diff] [blame] | 2331 | ClassType, Size, /*Align=*/0, Flags); |
Guy Benyei | 11169dd | 2012-12-18 14:30:41 +0000 | [diff] [blame] | 2332 | } |
| 2333 | |
Duncan P. N. Exon Smith | 9dd4e4e | 2015-04-29 16:40:08 +0000 | [diff] [blame] | 2334 | llvm::DIType *CGDebugInfo::CreateType(const AtomicType *Ty, llvm::DIFile *U) { |
Victor Leschuk | 0df19037 | 2016-10-31 19:09:47 +0000 | [diff] [blame] | 2335 | auto *FromTy = getOrCreateType(Ty->getValueType(), U); |
| 2336 | return DBuilder.createQualifiedType(llvm::dwarf::DW_TAG_atomic_type, FromTy); |
Guy Benyei | 11169dd | 2012-12-18 14:30:41 +0000 | [diff] [blame] | 2337 | } |
| 2338 | |
Xiuli Pan | 9c14e28 | 2016-01-09 12:53:17 +0000 | [diff] [blame] | 2339 | llvm::DIType* CGDebugInfo::CreateType(const PipeType *Ty, |
| 2340 | llvm::DIFile *U) { |
| 2341 | return getOrCreateType(Ty->getElementType(), U); |
| 2342 | } |
| 2343 | |
Duncan P. N. Exon Smith | 9dd4e4e | 2015-04-29 16:40:08 +0000 | [diff] [blame] | 2344 | llvm::DIType *CGDebugInfo::CreateEnumType(const EnumType *Ty) { |
Manman Ren | 1b45702 | 2013-08-28 21:20:28 +0000 | [diff] [blame] | 2345 | const EnumDecl *ED = Ty->getDecl(); |
Adrian Prantl | 5c8bd88 | 2015-09-11 17:23:08 +0000 | [diff] [blame] | 2346 | |
Guy Benyei | 11169dd | 2012-12-18 14:30:41 +0000 | [diff] [blame] | 2347 | uint64_t Size = 0; |
Victor Leschuk | 802e4a5 | 2016-10-19 22:11:07 +0000 | [diff] [blame] | 2348 | uint32_t Align = 0; |
Guy Benyei | 11169dd | 2012-12-18 14:30:41 +0000 | [diff] [blame] | 2349 | if (!ED->getTypeForDecl()->isIncompleteType()) { |
| 2350 | Size = CGM.getContext().getTypeSize(ED->getTypeForDecl()); |
Victor Leschuk | a7ece03 | 2016-10-20 00:13:19 +0000 | [diff] [blame] | 2351 | Align = getDeclAlignIfRequired(ED, CGM.getContext()); |
Guy Benyei | 11169dd | 2012-12-18 14:30:41 +0000 | [diff] [blame] | 2352 | } |
| 2353 | |
Manman Ren | e0064d8 | 2013-08-29 23:19:58 +0000 | [diff] [blame] | 2354 | SmallString<256> FullName = getUniqueTagTypeName(Ty, CGM, TheCU); |
| 2355 | |
Adrian Prantl | 5c8bd88 | 2015-09-11 17:23:08 +0000 | [diff] [blame] | 2356 | bool isImportedFromModule = |
| 2357 | DebugTypeExtRefs && ED->isFromASTFile() && ED->getDefinition(); |
| 2358 | |
Guy Benyei | 11169dd | 2012-12-18 14:30:41 +0000 | [diff] [blame] | 2359 | // If this is just a forward declaration, construct an appropriately |
| 2360 | // marked node and just return it. |
Adrian Prantl | 5c8bd88 | 2015-09-11 17:23:08 +0000 | [diff] [blame] | 2361 | if (isImportedFromModule || !ED->getDefinition()) { |
Adrian Prantl | 4594606 | 2016-02-23 19:30:08 +0000 | [diff] [blame] | 2362 | // Note that it is possible for enums to be created as part of |
| 2363 | // their own declcontext. In this case a FwdDecl will be created |
| 2364 | // twice. This doesn't cause a problem because both FwdDecls are |
| 2365 | // entered into the ReplaceMap: finalize() will replace the first |
| 2366 | // FwdDecl with the second and then replace the second with |
| 2367 | // complete type. |
Amjad Aboud | dc4531e | 2016-04-30 01:44:38 +0000 | [diff] [blame] | 2368 | llvm::DIScope *EDContext = getDeclContextDescriptor(ED); |
Duncan P. N. Exon Smith | 9dd4e4e | 2015-04-29 16:40:08 +0000 | [diff] [blame] | 2369 | llvm::DIFile *DefUnit = getOrCreateFile(ED->getLocation()); |
Adrian Prantl | ff9d83c | 2016-02-08 17:03:28 +0000 | [diff] [blame] | 2370 | llvm::TempDIScope TmpContext(DBuilder.createReplaceableCompositeType( |
| 2371 | llvm::dwarf::DW_TAG_enumeration_type, "", TheCU, DefUnit, 0)); |
Adrian Prantl | a40030f | 2016-02-06 01:59:09 +0000 | [diff] [blame] | 2372 | |
Guy Benyei | 11169dd | 2012-12-18 14:30:41 +0000 | [diff] [blame] | 2373 | unsigned Line = getLineNumber(ED->getLocation()); |
| 2374 | StringRef EDName = ED->getName(); |
Duncan P. N. Exon Smith | 9dd4e4e | 2015-04-29 16:40:08 +0000 | [diff] [blame] | 2375 | llvm::DIType *RetTy = DBuilder.createReplaceableCompositeType( |
Adrian Prantl | 4594606 | 2016-02-23 19:30:08 +0000 | [diff] [blame] | 2376 | llvm::dwarf::DW_TAG_enumeration_type, EDName, EDContext, DefUnit, Line, |
| 2377 | 0, Size, Align, llvm::DINode::FlagFwdDecl, FullName); |
Adrian Prantl | a40030f | 2016-02-06 01:59:09 +0000 | [diff] [blame] | 2378 | |
Duncan P. N. Exon Smith | fb49491 | 2014-12-09 18:39:32 +0000 | [diff] [blame] | 2379 | ReplaceMap.emplace_back( |
| 2380 | std::piecewise_construct, std::make_tuple(Ty), |
| 2381 | std::make_tuple(static_cast<llvm::Metadata *>(RetTy))); |
David Blaikie | f427b00 | 2014-05-06 03:42:01 +0000 | [diff] [blame] | 2382 | return RetTy; |
Guy Benyei | 11169dd | 2012-12-18 14:30:41 +0000 | [diff] [blame] | 2383 | } |
| 2384 | |
David Blaikie | 483a9da | 2014-05-06 18:35:21 +0000 | [diff] [blame] | 2385 | return CreateTypeDefinition(Ty); |
| 2386 | } |
| 2387 | |
Duncan P. N. Exon Smith | 9dd4e4e | 2015-04-29 16:40:08 +0000 | [diff] [blame] | 2388 | llvm::DIType *CGDebugInfo::CreateTypeDefinition(const EnumType *Ty) { |
David Blaikie | 483a9da | 2014-05-06 18:35:21 +0000 | [diff] [blame] | 2389 | const EnumDecl *ED = Ty->getDecl(); |
| 2390 | uint64_t Size = 0; |
Victor Leschuk | 802e4a5 | 2016-10-19 22:11:07 +0000 | [diff] [blame] | 2391 | uint32_t Align = 0; |
David Blaikie | 483a9da | 2014-05-06 18:35:21 +0000 | [diff] [blame] | 2392 | if (!ED->getTypeForDecl()->isIncompleteType()) { |
| 2393 | Size = CGM.getContext().getTypeSize(ED->getTypeForDecl()); |
Victor Leschuk | a7ece03 | 2016-10-20 00:13:19 +0000 | [diff] [blame] | 2394 | Align = getDeclAlignIfRequired(ED, CGM.getContext()); |
David Blaikie | 483a9da | 2014-05-06 18:35:21 +0000 | [diff] [blame] | 2395 | } |
| 2396 | |
| 2397 | SmallString<256> FullName = getUniqueTagTypeName(Ty, CGM, TheCU); |
| 2398 | |
Duncan P. N. Exon Smith | dadc2b6 | 2015-04-21 18:43:54 +0000 | [diff] [blame] | 2399 | // Create elements for each enumerator. |
Duncan P. N. Exon Smith | fb49491 | 2014-12-09 18:39:32 +0000 | [diff] [blame] | 2400 | SmallVector<llvm::Metadata *, 16> Enumerators; |
Guy Benyei | 11169dd | 2012-12-18 14:30:41 +0000 | [diff] [blame] | 2401 | ED = ED->getDefinition(); |
Aaron Ballman | 23a6dcb | 2014-03-08 18:45:14 +0000 | [diff] [blame] | 2402 | for (const auto *Enum : ED->enumerators()) { |
Eric Christopher | e7b87e5 | 2014-10-26 23:40:33 +0000 | [diff] [blame] | 2403 | Enumerators.push_back(DBuilder.createEnumerator( |
| 2404 | Enum->getName(), Enum->getInitVal().getSExtValue())); |
Guy Benyei | 11169dd | 2012-12-18 14:30:41 +0000 | [diff] [blame] | 2405 | } |
| 2406 | |
| 2407 | // Return a CompositeType for the enum itself. |
Duncan P. N. Exon Smith | 9dd4e4e | 2015-04-29 16:40:08 +0000 | [diff] [blame] | 2408 | llvm::DINodeArray EltArray = DBuilder.getOrCreateArray(Enumerators); |
Guy Benyei | 11169dd | 2012-12-18 14:30:41 +0000 | [diff] [blame] | 2409 | |
Duncan P. N. Exon Smith | 9dd4e4e | 2015-04-29 16:40:08 +0000 | [diff] [blame] | 2410 | llvm::DIFile *DefUnit = getOrCreateFile(ED->getLocation()); |
Guy Benyei | 11169dd | 2012-12-18 14:30:41 +0000 | [diff] [blame] | 2411 | unsigned Line = getLineNumber(ED->getLocation()); |
Amjad Aboud | dc4531e | 2016-04-30 01:44:38 +0000 | [diff] [blame] | 2412 | llvm::DIScope *EnumContext = getDeclContextDescriptor(ED); |
Duncan P. N. Exon Smith | 9dd4e4e | 2015-04-29 16:40:08 +0000 | [diff] [blame] | 2413 | llvm::DIType *ClassTy = |
Duncan P. N. Exon Smith | 0b6c369 | 2015-04-20 18:51:48 +0000 | [diff] [blame] | 2414 | ED->isFixed() ? getOrCreateType(ED->getIntegerType(), DefUnit) : nullptr; |
| 2415 | return DBuilder.createEnumerationType(EnumContext, ED->getName(), DefUnit, |
| 2416 | Line, Size, Align, EltArray, ClassTy, |
| 2417 | FullName); |
Guy Benyei | 11169dd | 2012-12-18 14:30:41 +0000 | [diff] [blame] | 2418 | } |
| 2419 | |
Amjad Aboud | 546bc11 | 2017-02-09 22:07:24 +0000 | [diff] [blame] | 2420 | llvm::DIMacro *CGDebugInfo::CreateMacro(llvm::DIMacroFile *Parent, |
| 2421 | unsigned MType, SourceLocation LineLoc, |
| 2422 | StringRef Name, StringRef Value) { |
| 2423 | unsigned Line = LineLoc.isInvalid() ? 0 : getLineNumber(LineLoc); |
| 2424 | return DBuilder.createMacro(Parent, Line, MType, Name, Value); |
| 2425 | } |
| 2426 | |
| 2427 | llvm::DIMacroFile *CGDebugInfo::CreateTempMacroFile(llvm::DIMacroFile *Parent, |
| 2428 | SourceLocation LineLoc, |
| 2429 | SourceLocation FileLoc) { |
| 2430 | llvm::DIFile *FName = getOrCreateFile(FileLoc); |
| 2431 | unsigned Line = LineLoc.isInvalid() ? 0 : getLineNumber(LineLoc); |
| 2432 | return DBuilder.createTempMacroFile(Parent, Line, FName); |
| 2433 | } |
| 2434 | |
David Blaikie | 0549106 | 2013-01-21 04:37:12 +0000 | [diff] [blame] | 2435 | static QualType UnwrapTypeForDebugInfo(QualType T, const ASTContext &C) { |
| 2436 | Qualifiers Quals; |
Guy Benyei | 11169dd | 2012-12-18 14:30:41 +0000 | [diff] [blame] | 2437 | do { |
Adrian Prantl | 179af90 | 2013-09-26 21:35:50 +0000 | [diff] [blame] | 2438 | Qualifiers InnerQuals = T.getLocalQualifiers(); |
| 2439 | // Qualifiers::operator+() doesn't like it if you add a Qualifier |
| 2440 | // that is already there. |
| 2441 | Quals += Qualifiers::removeCommonQualifiers(Quals, InnerQuals); |
| 2442 | Quals += InnerQuals; |
Guy Benyei | 11169dd | 2012-12-18 14:30:41 +0000 | [diff] [blame] | 2443 | QualType LastT = T; |
| 2444 | switch (T->getTypeClass()) { |
| 2445 | default: |
David Blaikie | 0549106 | 2013-01-21 04:37:12 +0000 | [diff] [blame] | 2446 | return C.getQualifiedType(T.getTypePtr(), Quals); |
David Blaikie | f1b382e | 2014-04-06 17:14:06 +0000 | [diff] [blame] | 2447 | case Type::TemplateSpecialization: { |
| 2448 | const auto *Spec = cast<TemplateSpecializationType>(T); |
| 2449 | if (Spec->isTypeAlias()) |
| 2450 | return C.getQualifiedType(T.getTypePtr(), Quals); |
| 2451 | T = Spec->desugar(); |
Eric Christopher | e7b87e5 | 2014-10-26 23:40:33 +0000 | [diff] [blame] | 2452 | break; |
| 2453 | } |
Guy Benyei | 11169dd | 2012-12-18 14:30:41 +0000 | [diff] [blame] | 2454 | case Type::TypeOfExpr: |
| 2455 | T = cast<TypeOfExprType>(T)->getUnderlyingExpr()->getType(); |
| 2456 | break; |
| 2457 | case Type::TypeOf: |
| 2458 | T = cast<TypeOfType>(T)->getUnderlyingType(); |
| 2459 | break; |
| 2460 | case Type::Decltype: |
| 2461 | T = cast<DecltypeType>(T)->getUnderlyingType(); |
| 2462 | break; |
| 2463 | case Type::UnaryTransform: |
| 2464 | T = cast<UnaryTransformType>(T)->getUnderlyingType(); |
| 2465 | break; |
| 2466 | case Type::Attributed: |
| 2467 | T = cast<AttributedType>(T)->getEquivalentType(); |
| 2468 | break; |
| 2469 | case Type::Elaborated: |
| 2470 | T = cast<ElaboratedType>(T)->getNamedType(); |
| 2471 | break; |
| 2472 | case Type::Paren: |
| 2473 | T = cast<ParenType>(T)->getInnerType(); |
| 2474 | break; |
David Blaikie | 0549106 | 2013-01-21 04:37:12 +0000 | [diff] [blame] | 2475 | case Type::SubstTemplateTypeParm: |
Guy Benyei | 11169dd | 2012-12-18 14:30:41 +0000 | [diff] [blame] | 2476 | T = cast<SubstTemplateTypeParmType>(T)->getReplacementType(); |
Guy Benyei | 11169dd | 2012-12-18 14:30:41 +0000 | [diff] [blame] | 2477 | break; |
Richard Smith | a0abc42 | 2017-02-22 00:13:14 +0000 | [diff] [blame] | 2478 | case Type::Auto: |
| 2479 | case Type::DeducedTemplateSpecialization: { |
| 2480 | QualType DT = cast<DeducedType>(T)->getDeducedType(); |
David Blaikie | 42edade | 2014-11-11 20:44:45 +0000 | [diff] [blame] | 2481 | assert(!DT.isNull() && "Undeduced types shouldn't reach here."); |
David Blaikie | 22c460a0 | 2013-05-24 21:24:35 +0000 | [diff] [blame] | 2482 | T = DT; |
Guy Benyei | 11169dd | 2012-12-18 14:30:41 +0000 | [diff] [blame] | 2483 | break; |
| 2484 | } |
Jordan Rose | 303e2f1 | 2016-11-10 23:28:17 +0000 | [diff] [blame] | 2485 | case Type::Adjusted: |
| 2486 | case Type::Decayed: |
| 2487 | // Decayed and adjusted types use the adjusted type in LLVM and DWARF. |
| 2488 | T = cast<AdjustedType>(T)->getAdjustedType(); |
| 2489 | break; |
| 2490 | } |
Eric Christopher | b2a008c | 2013-05-16 00:45:12 +0000 | [diff] [blame] | 2491 | |
Guy Benyei | 11169dd | 2012-12-18 14:30:41 +0000 | [diff] [blame] | 2492 | assert(T != LastT && "Type unwrapping failed to unwrap!"); |
NAKAMURA Takumi | 3e0a363 | 2013-01-21 10:51:28 +0000 | [diff] [blame] | 2493 | (void)LastT; |
Guy Benyei | 11169dd | 2012-12-18 14:30:41 +0000 | [diff] [blame] | 2494 | } while (true); |
| 2495 | } |
| 2496 | |
Duncan P. N. Exon Smith | 9dd4e4e | 2015-04-29 16:40:08 +0000 | [diff] [blame] | 2497 | llvm::DIType *CGDebugInfo::getTypeOrNull(QualType Ty) { |
Guy Benyei | 11169dd | 2012-12-18 14:30:41 +0000 | [diff] [blame] | 2498 | |
| 2499 | // Unwrap the type as needed for debug information. |
David Blaikie | 0549106 | 2013-01-21 04:37:12 +0000 | [diff] [blame] | 2500 | Ty = UnwrapTypeForDebugInfo(Ty, CGM.getContext()); |
Eric Christopher | b2a008c | 2013-05-16 00:45:12 +0000 | [diff] [blame] | 2501 | |
David Blaikie | f427b00 | 2014-05-06 03:42:01 +0000 | [diff] [blame] | 2502 | auto it = TypeCache.find(Ty.getAsOpaquePtr()); |
Guy Benyei | 11169dd | 2012-12-18 14:30:41 +0000 | [diff] [blame] | 2503 | if (it != TypeCache.end()) { |
| 2504 | // Verify that the debug info still exists. |
Duncan P. N. Exon Smith | fb49491 | 2014-12-09 18:39:32 +0000 | [diff] [blame] | 2505 | if (llvm::Metadata *V = it->second) |
Duncan P. N. Exon Smith | 9dd4e4e | 2015-04-29 16:40:08 +0000 | [diff] [blame] | 2506 | return cast<llvm::DIType>(V); |
Guy Benyei | 11169dd | 2012-12-18 14:30:41 +0000 | [diff] [blame] | 2507 | } |
| 2508 | |
Duncan P. N. Exon Smith | c755128 | 2015-04-06 23:21:33 +0000 | [diff] [blame] | 2509 | return nullptr; |
Guy Benyei | 11169dd | 2012-12-18 14:30:41 +0000 | [diff] [blame] | 2510 | } |
| 2511 | |
David Blaikie | 0e716b4 | 2014-03-03 23:48:23 +0000 | [diff] [blame] | 2512 | void CGDebugInfo::completeTemplateDefinition( |
| 2513 | const ClassTemplateSpecializationDecl &SD) { |
Benjamin Kramer | 8c30592 | 2016-02-02 11:06:51 +0000 | [diff] [blame] | 2514 | if (DebugKind <= codegenoptions::DebugLineTablesOnly) |
David Blaikie | 0856f66 | 2014-03-04 22:01:08 +0000 | [diff] [blame] | 2515 | return; |
| 2516 | |
David Blaikie | 0e716b4 | 2014-03-03 23:48:23 +0000 | [diff] [blame] | 2517 | completeClassData(&SD); |
| 2518 | // In case this type has no member function definitions being emitted, ensure |
| 2519 | // it is retained |
| 2520 | RetainedTypes.push_back(CGM.getContext().getRecordType(&SD).getAsOpaquePtr()); |
| 2521 | } |
| 2522 | |
Duncan P. N. Exon Smith | 9dd4e4e | 2015-04-29 16:40:08 +0000 | [diff] [blame] | 2523 | llvm::DIType *CGDebugInfo::getOrCreateType(QualType Ty, llvm::DIFile *Unit) { |
Guy Benyei | 11169dd | 2012-12-18 14:30:41 +0000 | [diff] [blame] | 2524 | if (Ty.isNull()) |
Duncan P. N. Exon Smith | 0b6c369 | 2015-04-20 18:51:48 +0000 | [diff] [blame] | 2525 | return nullptr; |
Guy Benyei | 11169dd | 2012-12-18 14:30:41 +0000 | [diff] [blame] | 2526 | |
| 2527 | // Unwrap the type as needed for debug information. |
David Blaikie | 0549106 | 2013-01-21 04:37:12 +0000 | [diff] [blame] | 2528 | Ty = UnwrapTypeForDebugInfo(Ty, CGM.getContext()); |
Guy Benyei | 11169dd | 2012-12-18 14:30:41 +0000 | [diff] [blame] | 2529 | |
Duncan P. N. Exon Smith | 0b6c369 | 2015-04-20 18:51:48 +0000 | [diff] [blame] | 2530 | if (auto *T = getTypeOrNull(Ty)) |
Guy Benyei | 11169dd | 2012-12-18 14:30:41 +0000 | [diff] [blame] | 2531 | return T; |
| 2532 | |
Adrian Prantl | ca84418 | 2015-09-11 17:23:03 +0000 | [diff] [blame] | 2533 | llvm::DIType *Res = CreateTypeNode(Ty, Unit); |
Adrian Prantl | 5c8bd88 | 2015-09-11 17:23:08 +0000 | [diff] [blame] | 2534 | void* TyPtr = Ty.getAsOpaquePtr(); |
Adrian Prantl | 73409ce | 2013-03-11 18:33:46 +0000 | [diff] [blame] | 2535 | |
| 2536 | // And update the type cache. |
Duncan P. N. Exon Smith | fb49491 | 2014-12-09 18:39:32 +0000 | [diff] [blame] | 2537 | TypeCache[TyPtr].reset(Res); |
Guy Benyei | 11169dd | 2012-12-18 14:30:41 +0000 | [diff] [blame] | 2538 | |
Guy Benyei | 11169dd | 2012-12-18 14:30:41 +0000 | [diff] [blame] | 2539 | return Res; |
| 2540 | } |
| 2541 | |
Adrian Prantl | 5c8bd88 | 2015-09-11 17:23:08 +0000 | [diff] [blame] | 2542 | llvm::DIModule *CGDebugInfo::getParentModuleOrNull(const Decl *D) { |
Adrian Prantl | 335f5c7 | 2015-10-02 17:36:14 +0000 | [diff] [blame] | 2543 | // A forward declaration inside a module header does not belong to the module. |
| 2544 | if (isa<RecordDecl>(D) && !cast<RecordDecl>(D)->getDefinition()) |
| 2545 | return nullptr; |
Adrian Prantl | 85d938a | 2015-09-21 17:48:37 +0000 | [diff] [blame] | 2546 | if (DebugTypeExtRefs && D->isFromASTFile()) { |
| 2547 | // Record a reference to an imported clang module or precompiled header. |
| 2548 | auto *Reader = CGM.getContext().getExternalSource(); |
| 2549 | auto Idx = D->getOwningModuleID(); |
| 2550 | auto Info = Reader->getSourceDescriptor(Idx); |
| 2551 | if (Info) |
| 2552 | return getOrCreateModuleRef(*Info, /*SkeletonCU=*/true); |
| 2553 | } else if (ClangModuleMap) { |
Adrian Prantl | 9402cef | 2015-09-20 16:51:35 +0000 | [diff] [blame] | 2554 | // We are building a clang module or a precompiled header. |
| 2555 | // |
| 2556 | // TODO: When D is a CXXRecordDecl or a C++ Enum, the ODR applies |
| 2557 | // and it wouldn't be necessary to specify the parent scope |
| 2558 | // because the type is already unique by definition (it would look |
| 2559 | // like the output of -fno-standalone-debug). On the other hand, |
| 2560 | // the parent scope helps a consumer to quickly locate the object |
| 2561 | // file where the type's definition is located, so it might be |
| 2562 | // best to make this behavior a command line or debugger tuning |
| 2563 | // option. |
| 2564 | FullSourceLoc Loc(D->getLocation(), CGM.getContext().getSourceManager()); |
| 2565 | if (Module *M = ClangModuleMap->inferModuleFromLocation(Loc)) { |
Adrian Prantl | aa5d08d | 2016-01-22 21:14:41 +0000 | [diff] [blame] | 2566 | // This is a (sub-)module. |
Adrian Prantl | 9402cef | 2015-09-20 16:51:35 +0000 | [diff] [blame] | 2567 | auto Info = ExternalASTSource::ASTSourceDescriptor(*M); |
| 2568 | return getOrCreateModuleRef(Info, /*SkeletonCU=*/false); |
Adrian Prantl | aa5d08d | 2016-01-22 21:14:41 +0000 | [diff] [blame] | 2569 | } else { |
| 2570 | // This the precompiled header being built. |
| 2571 | return getOrCreateModuleRef(PCHDescriptor, /*SkeletonCU=*/false); |
Adrian Prantl | 9402cef | 2015-09-20 16:51:35 +0000 | [diff] [blame] | 2572 | } |
| 2573 | } |
Adrian Prantl | 5c8bd88 | 2015-09-11 17:23:08 +0000 | [diff] [blame] | 2574 | |
Adrian Prantl | 9402cef | 2015-09-20 16:51:35 +0000 | [diff] [blame] | 2575 | return nullptr; |
Adrian Prantl | 5c8bd88 | 2015-09-11 17:23:08 +0000 | [diff] [blame] | 2576 | } |
| 2577 | |
Duncan P. N. Exon Smith | 9dd4e4e | 2015-04-29 16:40:08 +0000 | [diff] [blame] | 2578 | llvm::DIType *CGDebugInfo::CreateTypeNode(QualType Ty, llvm::DIFile *Unit) { |
Guy Benyei | 11169dd | 2012-12-18 14:30:41 +0000 | [diff] [blame] | 2579 | // Handle qualifiers, which recursively handles what they refer to. |
| 2580 | if (Ty.hasLocalQualifiers()) |
David Blaikie | 99dab3b | 2013-09-04 22:03:57 +0000 | [diff] [blame] | 2581 | return CreateQualifiedType(Ty, Unit); |
Guy Benyei | 11169dd | 2012-12-18 14:30:41 +0000 | [diff] [blame] | 2582 | |
Guy Benyei | 11169dd | 2012-12-18 14:30:41 +0000 | [diff] [blame] | 2583 | // Work out details of type. |
| 2584 | switch (Ty->getTypeClass()) { |
| 2585 | #define TYPE(Class, Base) |
| 2586 | #define ABSTRACT_TYPE(Class, Base) |
| 2587 | #define NON_CANONICAL_TYPE(Class, Base) |
| 2588 | #define DEPENDENT_TYPE(Class, Base) case Type::Class: |
| 2589 | #include "clang/AST/TypeNodes.def" |
| 2590 | llvm_unreachable("Dependent types cannot show up in debug information"); |
| 2591 | |
| 2592 | case Type::ExtVector: |
| 2593 | case Type::Vector: |
| 2594 | return CreateType(cast<VectorType>(Ty), Unit); |
| 2595 | case Type::ObjCObjectPointer: |
| 2596 | return CreateType(cast<ObjCObjectPointerType>(Ty), Unit); |
| 2597 | case Type::ObjCObject: |
| 2598 | return CreateType(cast<ObjCObjectType>(Ty), Unit); |
Manman Ren | e6be26c | 2016-09-13 17:25:08 +0000 | [diff] [blame] | 2599 | case Type::ObjCTypeParam: |
| 2600 | return CreateType(cast<ObjCTypeParamType>(Ty), Unit); |
Guy Benyei | 11169dd | 2012-12-18 14:30:41 +0000 | [diff] [blame] | 2601 | case Type::ObjCInterface: |
| 2602 | return CreateType(cast<ObjCInterfaceType>(Ty), Unit); |
| 2603 | case Type::Builtin: |
| 2604 | return CreateType(cast<BuiltinType>(Ty)); |
| 2605 | case Type::Complex: |
| 2606 | return CreateType(cast<ComplexType>(Ty)); |
| 2607 | case Type::Pointer: |
| 2608 | return CreateType(cast<PointerType>(Ty), Unit); |
| 2609 | case Type::BlockPointer: |
| 2610 | return CreateType(cast<BlockPointerType>(Ty), Unit); |
| 2611 | case Type::Typedef: |
David Blaikie | 99dab3b | 2013-09-04 22:03:57 +0000 | [diff] [blame] | 2612 | return CreateType(cast<TypedefType>(Ty), Unit); |
Guy Benyei | 11169dd | 2012-12-18 14:30:41 +0000 | [diff] [blame] | 2613 | case Type::Record: |
David Blaikie | 99dab3b | 2013-09-04 22:03:57 +0000 | [diff] [blame] | 2614 | return CreateType(cast<RecordType>(Ty)); |
Guy Benyei | 11169dd | 2012-12-18 14:30:41 +0000 | [diff] [blame] | 2615 | case Type::Enum: |
Manman Ren | 1b45702 | 2013-08-28 21:20:28 +0000 | [diff] [blame] | 2616 | return CreateEnumType(cast<EnumType>(Ty)); |
Guy Benyei | 11169dd | 2012-12-18 14:30:41 +0000 | [diff] [blame] | 2617 | case Type::FunctionProto: |
| 2618 | case Type::FunctionNoProto: |
| 2619 | return CreateType(cast<FunctionType>(Ty), Unit); |
| 2620 | case Type::ConstantArray: |
| 2621 | case Type::VariableArray: |
| 2622 | case Type::IncompleteArray: |
| 2623 | return CreateType(cast<ArrayType>(Ty), Unit); |
| 2624 | |
| 2625 | case Type::LValueReference: |
| 2626 | return CreateType(cast<LValueReferenceType>(Ty), Unit); |
| 2627 | case Type::RValueReference: |
| 2628 | return CreateType(cast<RValueReferenceType>(Ty), Unit); |
| 2629 | |
| 2630 | case Type::MemberPointer: |
| 2631 | return CreateType(cast<MemberPointerType>(Ty), Unit); |
| 2632 | |
| 2633 | case Type::Atomic: |
| 2634 | return CreateType(cast<AtomicType>(Ty), Unit); |
| 2635 | |
Xiuli Pan | 9c14e28 | 2016-01-09 12:53:17 +0000 | [diff] [blame] | 2636 | case Type::Pipe: |
| 2637 | return CreateType(cast<PipeType>(Ty), Unit); |
| 2638 | |
Guy Benyei | 11169dd | 2012-12-18 14:30:41 +0000 | [diff] [blame] | 2639 | case Type::TemplateSpecialization: |
David Blaikie | f1b382e | 2014-04-06 17:14:06 +0000 | [diff] [blame] | 2640 | return CreateType(cast<TemplateSpecializationType>(Ty), Unit); |
| 2641 | |
David Blaikie | 42edade | 2014-11-11 20:44:45 +0000 | [diff] [blame] | 2642 | case Type::Auto: |
David Blaikie | f1b382e | 2014-04-06 17:14:06 +0000 | [diff] [blame] | 2643 | case Type::Attributed: |
Jordan Rose | 303e2f1 | 2016-11-10 23:28:17 +0000 | [diff] [blame] | 2644 | case Type::Adjusted: |
| 2645 | case Type::Decayed: |
Richard Smith | 600b526 | 2017-01-26 20:40:47 +0000 | [diff] [blame] | 2646 | case Type::DeducedTemplateSpecialization: |
Guy Benyei | 11169dd | 2012-12-18 14:30:41 +0000 | [diff] [blame] | 2647 | case Type::Elaborated: |
| 2648 | case Type::Paren: |
| 2649 | case Type::SubstTemplateTypeParm: |
| 2650 | case Type::TypeOfExpr: |
| 2651 | case Type::TypeOf: |
| 2652 | case Type::Decltype: |
| 2653 | case Type::UnaryTransform: |
David Blaikie | 66ed89d | 2013-07-13 21:08:08 +0000 | [diff] [blame] | 2654 | case Type::PackExpansion: |
David Blaikie | 22c460a0 | 2013-05-24 21:24:35 +0000 | [diff] [blame] | 2655 | break; |
Guy Benyei | 11169dd | 2012-12-18 14:30:41 +0000 | [diff] [blame] | 2656 | } |
Eric Christopher | b2a008c | 2013-05-16 00:45:12 +0000 | [diff] [blame] | 2657 | |
David Blaikie | 42edade | 2014-11-11 20:44:45 +0000 | [diff] [blame] | 2658 | llvm_unreachable("type should have been unwrapped!"); |
Guy Benyei | 11169dd | 2012-12-18 14:30:41 +0000 | [diff] [blame] | 2659 | } |
| 2660 | |
Duncan P. N. Exon Smith | bd210e6 | 2015-07-24 20:34:41 +0000 | [diff] [blame] | 2661 | llvm::DICompositeType *CGDebugInfo::getOrCreateLimitedType(const RecordType *Ty, |
| 2662 | llvm::DIFile *Unit) { |
David Blaikie | 4a2b5ef | 2013-08-12 22:24:20 +0000 | [diff] [blame] | 2663 | QualType QTy(Ty, 0); |
Guy Benyei | 11169dd | 2012-12-18 14:30:41 +0000 | [diff] [blame] | 2664 | |
Duncan P. N. Exon Smith | bd210e6 | 2015-07-24 20:34:41 +0000 | [diff] [blame] | 2665 | auto *T = cast_or_null<llvm::DICompositeType>(getTypeOrNull(QTy)); |
Guy Benyei | 11169dd | 2012-12-18 14:30:41 +0000 | [diff] [blame] | 2666 | |
| 2667 | // We may have cached a forward decl when we could have created |
| 2668 | // a non-forward decl. Go ahead and create a non-forward decl |
| 2669 | // now. |
Duncan P. N. Exon Smith | 4caa7f2 | 2015-04-16 01:00:56 +0000 | [diff] [blame] | 2670 | if (T && !T->isForwardDecl()) |
Eric Christopher | e7b87e5 | 2014-10-26 23:40:33 +0000 | [diff] [blame] | 2671 | return T; |
Guy Benyei | 11169dd | 2012-12-18 14:30:41 +0000 | [diff] [blame] | 2672 | |
| 2673 | // Otherwise create the type. |
Duncan P. N. Exon Smith | 9dd4e4e | 2015-04-29 16:40:08 +0000 | [diff] [blame] | 2674 | llvm::DICompositeType *Res = CreateLimitedType(Ty); |
David Blaikie | 8d5e128 | 2013-08-20 21:03:29 +0000 | [diff] [blame] | 2675 | |
| 2676 | // Propagate members from the declaration to the definition |
| 2677 | // CreateType(const RecordType*) will overwrite this with the members in the |
| 2678 | // correct order if the full type is needed. |
Duncan P. N. Exon Smith | 9dd4e4e | 2015-04-29 16:40:08 +0000 | [diff] [blame] | 2679 | DBuilder.replaceArrays(Res, T ? T->getElements() : llvm::DINodeArray()); |
Guy Benyei | 11169dd | 2012-12-18 14:30:41 +0000 | [diff] [blame] | 2680 | |
Guy Benyei | 11169dd | 2012-12-18 14:30:41 +0000 | [diff] [blame] | 2681 | // And update the type cache. |
Duncan P. N. Exon Smith | fb49491 | 2014-12-09 18:39:32 +0000 | [diff] [blame] | 2682 | TypeCache[QTy.getAsOpaquePtr()].reset(Res); |
Guy Benyei | 11169dd | 2012-12-18 14:30:41 +0000 | [diff] [blame] | 2683 | return Res; |
| 2684 | } |
| 2685 | |
| 2686 | // TODO: Currently used for context chains when limiting debug info. |
Duncan P. N. Exon Smith | 9dd4e4e | 2015-04-29 16:40:08 +0000 | [diff] [blame] | 2687 | llvm::DICompositeType *CGDebugInfo::CreateLimitedType(const RecordType *Ty) { |
Guy Benyei | 11169dd | 2012-12-18 14:30:41 +0000 | [diff] [blame] | 2688 | RecordDecl *RD = Ty->getDecl(); |
Eric Christopher | b2a008c | 2013-05-16 00:45:12 +0000 | [diff] [blame] | 2689 | |
Guy Benyei | 11169dd | 2012-12-18 14:30:41 +0000 | [diff] [blame] | 2690 | // Get overall information about the record type for the debug info. |
Duncan P. N. Exon Smith | 9dd4e4e | 2015-04-29 16:40:08 +0000 | [diff] [blame] | 2691 | llvm::DIFile *DefUnit = getOrCreateFile(RD->getLocation()); |
Guy Benyei | 11169dd | 2012-12-18 14:30:41 +0000 | [diff] [blame] | 2692 | unsigned Line = getLineNumber(RD->getLocation()); |
| 2693 | StringRef RDName = getClassName(RD); |
| 2694 | |
Amjad Aboud | dc4531e | 2016-04-30 01:44:38 +0000 | [diff] [blame] | 2695 | llvm::DIScope *RDContext = getDeclContextDescriptor(RD); |
Guy Benyei | 11169dd | 2012-12-18 14:30:41 +0000 | [diff] [blame] | 2696 | |
David Blaikie | d278589 | 2013-08-18 17:36:19 +0000 | [diff] [blame] | 2697 | // If we ended up creating the type during the context chain construction, |
| 2698 | // just return that. |
Duncan P. N. Exon Smith | 9dd4e4e | 2015-04-29 16:40:08 +0000 | [diff] [blame] | 2699 | auto *T = cast_or_null<llvm::DICompositeType>( |
Duncan P. N. Exon Smith | c755128 | 2015-04-06 23:21:33 +0000 | [diff] [blame] | 2700 | getTypeOrNull(CGM.getContext().getRecordType(RD))); |
Duncan P. N. Exon Smith | 4caa7f2 | 2015-04-16 01:00:56 +0000 | [diff] [blame] | 2701 | if (T && (!T->isForwardDecl() || !RD->getDefinition())) |
Eric Christopher | e7b87e5 | 2014-10-26 23:40:33 +0000 | [diff] [blame] | 2702 | return T; |
David Blaikie | d278589 | 2013-08-18 17:36:19 +0000 | [diff] [blame] | 2703 | |
Adrian Prantl | 381e755 | 2014-02-04 21:29:50 +0000 | [diff] [blame] | 2704 | // If this is just a forward or incomplete declaration, construct an |
| 2705 | // appropriately marked node and just return it. |
| 2706 | const RecordDecl *D = RD->getDefinition(); |
| 2707 | if (!D || !D->isCompleteDefinition()) |
Manman Ren | 1b45702 | 2013-08-28 21:20:28 +0000 | [diff] [blame] | 2708 | return getOrCreateRecordFwdDecl(Ty, RDContext); |
Guy Benyei | 11169dd | 2012-12-18 14:30:41 +0000 | [diff] [blame] | 2709 | |
| 2710 | uint64_t Size = CGM.getContext().getTypeSize(Ty); |
Victor Leschuk | a7ece03 | 2016-10-20 00:13:19 +0000 | [diff] [blame] | 2711 | auto Align = getDeclAlignIfRequired(D, CGM.getContext()); |
Eric Christopher | b2a008c | 2013-05-16 00:45:12 +0000 | [diff] [blame] | 2712 | |
Manman Ren | e0064d8 | 2013-08-29 23:19:58 +0000 | [diff] [blame] | 2713 | SmallString<256> FullName = getUniqueTagTypeName(Ty, CGM, TheCU); |
| 2714 | |
Duncan P. N. Exon Smith | 9dd4e4e | 2015-04-29 16:40:08 +0000 | [diff] [blame] | 2715 | llvm::DICompositeType *RealDecl = DBuilder.createReplaceableCompositeType( |
Leny Kholodov | 80c047d | 2016-09-06 10:48:04 +0000 | [diff] [blame] | 2716 | getTagForRecord(RD), RDName, RDContext, DefUnit, Line, 0, Size, Align, |
| 2717 | llvm::DINode::FlagZero, FullName); |
Guy Benyei | 11169dd | 2012-12-18 14:30:41 +0000 | [diff] [blame] | 2718 | |
Duncan P. N. Exon Smith | f9521b0 | 2016-04-17 07:45:08 +0000 | [diff] [blame] | 2719 | // Elements of composite types usually have back to the type, creating |
| 2720 | // uniquing cycles. Distinct nodes are more efficient. |
| 2721 | switch (RealDecl->getTag()) { |
| 2722 | default: |
| 2723 | llvm_unreachable("invalid composite type tag"); |
| 2724 | |
| 2725 | case llvm::dwarf::DW_TAG_array_type: |
| 2726 | case llvm::dwarf::DW_TAG_enumeration_type: |
| 2727 | // Array elements and most enumeration elements don't have back references, |
| 2728 | // so they don't tend to be involved in uniquing cycles and there is some |
| 2729 | // chance of merging them when linking together two modules. Only make |
| 2730 | // them distinct if they are ODR-uniqued. |
| 2731 | if (FullName.empty()) |
| 2732 | break; |
| 2733 | |
| 2734 | case llvm::dwarf::DW_TAG_structure_type: |
| 2735 | case llvm::dwarf::DW_TAG_union_type: |
| 2736 | case llvm::dwarf::DW_TAG_class_type: |
| 2737 | // Immediatley resolve to a distinct node. |
| 2738 | RealDecl = |
| 2739 | llvm::MDNode::replaceWithDistinct(llvm::TempDICompositeType(RealDecl)); |
| 2740 | break; |
| 2741 | } |
| 2742 | |
Duncan P. N. Exon Smith | fb49491 | 2014-12-09 18:39:32 +0000 | [diff] [blame] | 2743 | RegionMap[Ty->getDecl()].reset(RealDecl); |
| 2744 | TypeCache[QualType(Ty, 0).getAsOpaquePtr()].reset(RealDecl); |
Guy Benyei | 11169dd | 2012-12-18 14:30:41 +0000 | [diff] [blame] | 2745 | |
David Majnemer | 58ed0f3 | 2016-07-17 00:39:12 +0000 | [diff] [blame] | 2746 | if (const auto *TSpecial = dyn_cast<ClassTemplateSpecializationDecl>(RD)) |
Duncan P. N. Exon Smith | 9dd4e4e | 2015-04-29 16:40:08 +0000 | [diff] [blame] | 2747 | DBuilder.replaceArrays(RealDecl, llvm::DINodeArray(), |
Duncan P. N. Exon Smith | c8ee63e | 2014-12-18 00:48:56 +0000 | [diff] [blame] | 2748 | CollectCXXTemplateParams(TSpecial, DefUnit)); |
David Blaikie | 952dac3 | 2013-08-15 22:42:12 +0000 | [diff] [blame] | 2749 | return RealDecl; |
Guy Benyei | 11169dd | 2012-12-18 14:30:41 +0000 | [diff] [blame] | 2750 | } |
| 2751 | |
David Blaikie | adfbf99 | 2013-08-18 16:55:33 +0000 | [diff] [blame] | 2752 | void CGDebugInfo::CollectContainingType(const CXXRecordDecl *RD, |
Duncan P. N. Exon Smith | 9dd4e4e | 2015-04-29 16:40:08 +0000 | [diff] [blame] | 2753 | llvm::DICompositeType *RealDecl) { |
David Blaikie | adfbf99 | 2013-08-18 16:55:33 +0000 | [diff] [blame] | 2754 | // A class's primary base or the class itself contains the vtable. |
Duncan P. N. Exon Smith | 9dd4e4e | 2015-04-29 16:40:08 +0000 | [diff] [blame] | 2755 | llvm::DICompositeType *ContainingType = nullptr; |
David Blaikie | adfbf99 | 2013-08-18 16:55:33 +0000 | [diff] [blame] | 2756 | const ASTRecordLayout &RL = CGM.getContext().getASTRecordLayout(RD); |
| 2757 | if (const CXXRecordDecl *PBase = RL.getPrimaryBase()) { |
Alp Toker | d473363 | 2013-12-05 04:47:09 +0000 | [diff] [blame] | 2758 | // Seek non-virtual primary base root. |
David Blaikie | adfbf99 | 2013-08-18 16:55:33 +0000 | [diff] [blame] | 2759 | while (1) { |
| 2760 | const ASTRecordLayout &BRL = CGM.getContext().getASTRecordLayout(PBase); |
| 2761 | const CXXRecordDecl *PBT = BRL.getPrimaryBase(); |
| 2762 | if (PBT && !BRL.isPrimaryBaseVirtual()) |
| 2763 | PBase = PBT; |
| 2764 | else |
| 2765 | break; |
| 2766 | } |
Duncan P. N. Exon Smith | 9dd4e4e | 2015-04-29 16:40:08 +0000 | [diff] [blame] | 2767 | ContainingType = cast<llvm::DICompositeType>( |
David Blaikie | adfbf99 | 2013-08-18 16:55:33 +0000 | [diff] [blame] | 2768 | getOrCreateType(QualType(PBase->getTypeForDecl(), 0), |
| 2769 | getOrCreateFile(RD->getLocation()))); |
| 2770 | } else if (RD->isDynamicClass()) |
| 2771 | ContainingType = RealDecl; |
| 2772 | |
Duncan P. N. Exon Smith | c8ee63e | 2014-12-18 00:48:56 +0000 | [diff] [blame] | 2773 | DBuilder.replaceVTableHolder(RealDecl, ContainingType); |
David Blaikie | adfbf99 | 2013-08-18 16:55:33 +0000 | [diff] [blame] | 2774 | } |
| 2775 | |
Duncan P. N. Exon Smith | 9dd4e4e | 2015-04-29 16:40:08 +0000 | [diff] [blame] | 2776 | llvm::DIType *CGDebugInfo::CreateMemberType(llvm::DIFile *Unit, QualType FType, |
Duncan P. N. Exon Smith | 0b6c369 | 2015-04-20 18:51:48 +0000 | [diff] [blame] | 2777 | StringRef Name, uint64_t *Offset) { |
Duncan P. N. Exon Smith | 9dd4e4e | 2015-04-29 16:40:08 +0000 | [diff] [blame] | 2778 | llvm::DIType *FieldTy = CGDebugInfo::getOrCreateType(FType, Unit); |
Guy Benyei | 11169dd | 2012-12-18 14:30:41 +0000 | [diff] [blame] | 2779 | uint64_t FieldSize = CGM.getContext().getTypeSize(FType); |
Victor Leschuk | a7ece03 | 2016-10-20 00:13:19 +0000 | [diff] [blame] | 2780 | auto FieldAlign = getTypeAlignIfRequired(FType, CGM.getContext()); |
Leny Kholodov | df050fd | 2016-09-06 17:06:14 +0000 | [diff] [blame] | 2781 | llvm::DIType *Ty = |
| 2782 | DBuilder.createMemberType(Unit, Name, Unit, 0, FieldSize, FieldAlign, |
| 2783 | *Offset, llvm::DINode::FlagZero, FieldTy); |
Guy Benyei | 11169dd | 2012-12-18 14:30:41 +0000 | [diff] [blame] | 2784 | *Offset += FieldSize; |
| 2785 | return Ty; |
| 2786 | } |
| 2787 | |
Duncan P. N. Exon Smith | 9dd4e4e | 2015-04-29 16:40:08 +0000 | [diff] [blame] | 2788 | void CGDebugInfo::collectFunctionDeclProps(GlobalDecl GD, llvm::DIFile *Unit, |
Duncan P. N. Exon Smith | 8e47da4 | 2015-04-21 20:07:29 +0000 | [diff] [blame] | 2789 | StringRef &Name, |
| 2790 | StringRef &LinkageName, |
Duncan P. N. Exon Smith | 9dd4e4e | 2015-04-29 16:40:08 +0000 | [diff] [blame] | 2791 | llvm::DIScope *&FDContext, |
| 2792 | llvm::DINodeArray &TParamsArray, |
Leny Kholodov | 80c047d | 2016-09-06 10:48:04 +0000 | [diff] [blame] | 2793 | llvm::DINode::DIFlags &Flags) { |
David Majnemer | 58ed0f3 | 2016-07-17 00:39:12 +0000 | [diff] [blame] | 2794 | const auto *FD = cast<FunctionDecl>(GD.getDecl()); |
Frederic Riss | 9db79f1 | 2014-11-18 03:40:46 +0000 | [diff] [blame] | 2795 | Name = getFunctionName(FD); |
| 2796 | // Use mangled name as linkage name for C/C++ functions. |
| 2797 | if (FD->hasPrototype()) { |
| 2798 | LinkageName = CGM.getMangledName(GD); |
Duncan P. N. Exon Smith | 9dd4e4e | 2015-04-29 16:40:08 +0000 | [diff] [blame] | 2799 | Flags |= llvm::DINode::FlagPrototyped; |
Frederic Riss | 9db79f1 | 2014-11-18 03:40:46 +0000 | [diff] [blame] | 2800 | } |
| 2801 | // No need to replicate the linkage name if it isn't different from the |
| 2802 | // subprogram name, no need to have it at all unless coverage is enabled or |
Dehao Chen | b3a70de | 2017-01-19 00:44:21 +0000 | [diff] [blame] | 2803 | // debug is set to more than just line tables or extra debug info is needed. |
Benjamin Kramer | 8c30592 | 2016-02-02 11:06:51 +0000 | [diff] [blame] | 2804 | if (LinkageName == Name || (!CGM.getCodeGenOpts().EmitGcovArcs && |
| 2805 | !CGM.getCodeGenOpts().EmitGcovNotes && |
Dehao Chen | b3a70de | 2017-01-19 00:44:21 +0000 | [diff] [blame] | 2806 | !CGM.getCodeGenOpts().DebugInfoForProfiling && |
Benjamin Kramer | 8c30592 | 2016-02-02 11:06:51 +0000 | [diff] [blame] | 2807 | DebugKind <= codegenoptions::DebugLineTablesOnly)) |
Frederic Riss | 9db79f1 | 2014-11-18 03:40:46 +0000 | [diff] [blame] | 2808 | LinkageName = StringRef(); |
| 2809 | |
Benjamin Kramer | 8c30592 | 2016-02-02 11:06:51 +0000 | [diff] [blame] | 2810 | if (DebugKind >= codegenoptions::LimitedDebugInfo) { |
Frederic Riss | 9db79f1 | 2014-11-18 03:40:46 +0000 | [diff] [blame] | 2811 | if (const NamespaceDecl *NSDecl = |
| 2812 | dyn_cast_or_null<NamespaceDecl>(FD->getDeclContext())) |
| 2813 | FDContext = getOrCreateNameSpace(NSDecl); |
| 2814 | else if (const RecordDecl *RDecl = |
Adrian Prantl | 5c8bd88 | 2015-09-11 17:23:08 +0000 | [diff] [blame] | 2815 | dyn_cast_or_null<RecordDecl>(FD->getDeclContext())) { |
| 2816 | llvm::DIScope *Mod = getParentModuleOrNull(RDecl); |
| 2817 | FDContext = getContextDescriptor(RDecl, Mod ? Mod : TheCU); |
| 2818 | } |
Adrian Prantl | fd5ac8a | 2016-08-17 16:20:32 +0000 | [diff] [blame] | 2819 | // Check if it is a noreturn-marked function |
| 2820 | if (FD->isNoReturn()) |
| 2821 | Flags |= llvm::DINode::FlagNoReturn; |
Frederic Riss | 9db79f1 | 2014-11-18 03:40:46 +0000 | [diff] [blame] | 2822 | // Collect template parameters. |
| 2823 | TParamsArray = CollectFunctionTemplateParams(FD, Unit); |
| 2824 | } |
| 2825 | } |
| 2826 | |
Duncan P. N. Exon Smith | 9dd4e4e | 2015-04-29 16:40:08 +0000 | [diff] [blame] | 2827 | void CGDebugInfo::collectVarDeclProps(const VarDecl *VD, llvm::DIFile *&Unit, |
Frederic Riss | 9db79f1 | 2014-11-18 03:40:46 +0000 | [diff] [blame] | 2828 | unsigned &LineNo, QualType &T, |
| 2829 | StringRef &Name, StringRef &LinkageName, |
Duncan P. N. Exon Smith | 9dd4e4e | 2015-04-29 16:40:08 +0000 | [diff] [blame] | 2830 | llvm::DIScope *&VDContext) { |
Frederic Riss | 9db79f1 | 2014-11-18 03:40:46 +0000 | [diff] [blame] | 2831 | Unit = getOrCreateFile(VD->getLocation()); |
| 2832 | LineNo = getLineNumber(VD->getLocation()); |
| 2833 | |
| 2834 | setLocation(VD->getLocation()); |
| 2835 | |
| 2836 | T = VD->getType(); |
| 2837 | if (T->isIncompleteArrayType()) { |
| 2838 | // CodeGen turns int[] into int[1] so we'll do the same here. |
| 2839 | llvm::APInt ConstVal(32, 1); |
| 2840 | QualType ET = CGM.getContext().getAsArrayType(T)->getElementType(); |
| 2841 | |
| 2842 | T = CGM.getContext().getConstantArrayType(ET, ConstVal, |
| 2843 | ArrayType::Normal, 0); |
| 2844 | } |
| 2845 | |
| 2846 | Name = VD->getName(); |
| 2847 | if (VD->getDeclContext() && !isa<FunctionDecl>(VD->getDeclContext()) && |
| 2848 | !isa<ObjCMethodDecl>(VD->getDeclContext())) |
| 2849 | LinkageName = CGM.getMangledName(VD); |
| 2850 | if (LinkageName == Name) |
| 2851 | LinkageName = StringRef(); |
| 2852 | |
| 2853 | // Since we emit declarations (DW_AT_members) for static members, place the |
| 2854 | // definition of those static members in the namespace they were declared in |
| 2855 | // in the source code (the lexical decl context). |
| 2856 | // FIXME: Generalize this for even non-member global variables where the |
| 2857 | // declaration and definition may have different lexical decl contexts, once |
| 2858 | // we have support for emitting declarations of (non-member) global variables. |
Saleem Abdulrasool | cd187f0 | 2015-02-28 00:13:13 +0000 | [diff] [blame] | 2859 | const DeclContext *DC = VD->isStaticDataMember() ? VD->getLexicalDeclContext() |
| 2860 | : VD->getDeclContext(); |
| 2861 | // When a record type contains an in-line initialization of a static data |
| 2862 | // member, and the record type is marked as __declspec(dllexport), an implicit |
| 2863 | // definition of the member will be created in the record context. DWARF |
| 2864 | // doesn't seem to have a nice way to describe this in a form that consumers |
| 2865 | // are likely to understand, so fake the "normal" situation of a definition |
| 2866 | // outside the class by putting it in the global scope. |
| 2867 | if (DC->isRecord()) |
| 2868 | DC = CGM.getContext().getTranslationUnitDecl(); |
Adrian Prantl | 5c8bd88 | 2015-09-11 17:23:08 +0000 | [diff] [blame] | 2869 | |
Amjad Aboud | dc4531e | 2016-04-30 01:44:38 +0000 | [diff] [blame] | 2870 | llvm::DIScope *Mod = getParentModuleOrNull(VD); |
| 2871 | VDContext = getContextDescriptor(cast<Decl>(DC), Mod ? Mod : TheCU); |
Frederic Riss | 9db79f1 | 2014-11-18 03:40:46 +0000 | [diff] [blame] | 2872 | } |
| 2873 | |
Duncan P. N. Exon Smith | 9dd4e4e | 2015-04-29 16:40:08 +0000 | [diff] [blame] | 2874 | llvm::DISubprogram * |
Frederic Riss | d253ed6 | 2014-11-18 03:40:51 +0000 | [diff] [blame] | 2875 | CGDebugInfo::getFunctionForwardDeclaration(const FunctionDecl *FD) { |
Duncan P. N. Exon Smith | 9dd4e4e | 2015-04-29 16:40:08 +0000 | [diff] [blame] | 2876 | llvm::DINodeArray TParamsArray; |
Frederic Riss | d253ed6 | 2014-11-18 03:40:51 +0000 | [diff] [blame] | 2877 | StringRef Name, LinkageName; |
Leny Kholodov | 80c047d | 2016-09-06 10:48:04 +0000 | [diff] [blame] | 2878 | llvm::DINode::DIFlags Flags = llvm::DINode::FlagZero; |
Frederic Riss | d253ed6 | 2014-11-18 03:40:51 +0000 | [diff] [blame] | 2879 | SourceLocation Loc = FD->getLocation(); |
Duncan P. N. Exon Smith | 9dd4e4e | 2015-04-29 16:40:08 +0000 | [diff] [blame] | 2880 | llvm::DIFile *Unit = getOrCreateFile(Loc); |
| 2881 | llvm::DIScope *DContext = Unit; |
Frederic Riss | d253ed6 | 2014-11-18 03:40:51 +0000 | [diff] [blame] | 2882 | unsigned Line = getLineNumber(Loc); |
| 2883 | |
| 2884 | collectFunctionDeclProps(FD, Unit, Name, LinkageName, DContext, |
| 2885 | TParamsArray, Flags); |
| 2886 | // Build function type. |
| 2887 | SmallVector<QualType, 16> ArgTypes; |
| 2888 | for (const ParmVarDecl *Parm: FD->parameters()) |
| 2889 | ArgTypes.push_back(Parm->getType()); |
Reid Kleckner | f00f803 | 2016-06-08 20:41:54 +0000 | [diff] [blame] | 2890 | CallingConv CC = FD->getType()->castAs<FunctionType>()->getCallConv(); |
| 2891 | QualType FnType = CGM.getContext().getFunctionType( |
| 2892 | FD->getReturnType(), ArgTypes, FunctionProtoType::ExtProtoInfo(CC)); |
Duncan P. N. Exon Smith | 9dd4e4e | 2015-04-29 16:40:08 +0000 | [diff] [blame] | 2893 | llvm::DISubprogram *SP = DBuilder.createTempFunctionFwdDecl( |
Duncan P. N. Exon Smith | ebad0aa | 2015-04-07 16:50:49 +0000 | [diff] [blame] | 2894 | DContext, Name, LinkageName, Unit, Line, |
| 2895 | getOrCreateFunctionType(FD, FnType, Unit), !FD->isExternallyVisible(), |
Peter Collingbourne | 0900fe0 | 2015-11-05 22:04:14 +0000 | [diff] [blame] | 2896 | /* isDefinition = */ false, 0, Flags, CGM.getLangOpts().Optimize, |
Duncan P. N. Exon Smith | ebad0aa | 2015-04-07 16:50:49 +0000 | [diff] [blame] | 2897 | TParamsArray.get(), getFunctionDeclaration(FD)); |
David Majnemer | 58ed0f3 | 2016-07-17 00:39:12 +0000 | [diff] [blame] | 2898 | const auto *CanonDecl = cast<FunctionDecl>(FD->getCanonicalDecl()); |
Duncan P. N. Exon Smith | 4078ad4 | 2015-04-16 16:36:45 +0000 | [diff] [blame] | 2899 | FwdDeclReplaceMap.emplace_back(std::piecewise_construct, |
| 2900 | std::make_tuple(CanonDecl), |
| 2901 | std::make_tuple(SP)); |
Frederic Riss | d253ed6 | 2014-11-18 03:40:51 +0000 | [diff] [blame] | 2902 | return SP; |
| 2903 | } |
| 2904 | |
Duncan P. N. Exon Smith | 9dd4e4e | 2015-04-29 16:40:08 +0000 | [diff] [blame] | 2905 | llvm::DIGlobalVariable * |
Frederic Riss | d253ed6 | 2014-11-18 03:40:51 +0000 | [diff] [blame] | 2906 | CGDebugInfo::getGlobalVariableForwardDeclaration(const VarDecl *VD) { |
| 2907 | QualType T; |
| 2908 | StringRef Name, LinkageName; |
| 2909 | SourceLocation Loc = VD->getLocation(); |
Duncan P. N. Exon Smith | 9dd4e4e | 2015-04-29 16:40:08 +0000 | [diff] [blame] | 2910 | llvm::DIFile *Unit = getOrCreateFile(Loc); |
| 2911 | llvm::DIScope *DContext = Unit; |
Frederic Riss | d253ed6 | 2014-11-18 03:40:51 +0000 | [diff] [blame] | 2912 | unsigned Line = getLineNumber(Loc); |
| 2913 | |
| 2914 | collectVarDeclProps(VD, Unit, Line, T, Name, LinkageName, DContext); |
Victor Leschuk | a7ece03 | 2016-10-20 00:13:19 +0000 | [diff] [blame] | 2915 | auto Align = getDeclAlignIfRequired(VD, CGM.getContext()); |
Duncan P. N. Exon Smith | dadc2b6 | 2015-04-21 18:43:54 +0000 | [diff] [blame] | 2916 | auto *GV = DBuilder.createTempGlobalVariableFwdDecl( |
| 2917 | DContext, Name, LinkageName, Unit, Line, getOrCreateType(T, Unit), |
Adrian Prantl | 5f4740d | 2016-12-20 02:10:02 +0000 | [diff] [blame] | 2918 | !VD->isExternallyVisible(), nullptr, Align); |
Duncan P. N. Exon Smith | fb49491 | 2014-12-09 18:39:32 +0000 | [diff] [blame] | 2919 | FwdDeclReplaceMap.emplace_back( |
| 2920 | std::piecewise_construct, |
| 2921 | std::make_tuple(cast<VarDecl>(VD->getCanonicalDecl())), |
| 2922 | std::make_tuple(static_cast<llvm::Metadata *>(GV))); |
Frederic Riss | d253ed6 | 2014-11-18 03:40:51 +0000 | [diff] [blame] | 2923 | return GV; |
| 2924 | } |
| 2925 | |
Duncan P. N. Exon Smith | 9dd4e4e | 2015-04-29 16:40:08 +0000 | [diff] [blame] | 2926 | llvm::DINode *CGDebugInfo::getDeclarationOrDefinition(const Decl *D) { |
David Blaikie | bd48376 | 2013-05-20 04:58:53 +0000 | [diff] [blame] | 2927 | // We only need a declaration (not a definition) of the type - so use whatever |
| 2928 | // we would otherwise do to get a type for a pointee. (forward declarations in |
| 2929 | // limited debug info, full definitions (if the type definition is available) |
| 2930 | // in unlimited debug info) |
David Majnemer | 58ed0f3 | 2016-07-17 00:39:12 +0000 | [diff] [blame] | 2931 | if (const auto *TD = dyn_cast<TypeDecl>(D)) |
David Blaikie | 6b7d060c | 2013-08-12 23:14:36 +0000 | [diff] [blame] | 2932 | return getOrCreateType(CGM.getContext().getTypeDeclType(TD), |
David Blaikie | 99dab3b | 2013-09-04 22:03:57 +0000 | [diff] [blame] | 2933 | getOrCreateFile(TD->getLocation())); |
Duncan P. N. Exon Smith | fb49491 | 2014-12-09 18:39:32 +0000 | [diff] [blame] | 2934 | auto I = DeclCache.find(D->getCanonicalDecl()); |
Frederic Riss | d253ed6 | 2014-11-18 03:40:51 +0000 | [diff] [blame] | 2935 | |
Adrian Prantl | 5f4740d | 2016-12-20 02:10:02 +0000 | [diff] [blame] | 2936 | if (I != DeclCache.end()) { |
| 2937 | auto N = I->second; |
| 2938 | if (auto *GVE = dyn_cast_or_null<llvm::DIGlobalVariableExpression>(N)) |
| 2939 | return GVE->getVariable(); |
| 2940 | return dyn_cast_or_null<llvm::DINode>(N); |
| 2941 | } |
Frederic Riss | d253ed6 | 2014-11-18 03:40:51 +0000 | [diff] [blame] | 2942 | |
| 2943 | // No definition for now. Emit a forward definition that might be |
| 2944 | // merged with a potential upcoming definition. |
David Majnemer | 58ed0f3 | 2016-07-17 00:39:12 +0000 | [diff] [blame] | 2945 | if (const auto *FD = dyn_cast<FunctionDecl>(D)) |
Frederic Riss | d253ed6 | 2014-11-18 03:40:51 +0000 | [diff] [blame] | 2946 | return getFunctionForwardDeclaration(FD); |
| 2947 | else if (const auto *VD = dyn_cast<VarDecl>(D)) |
| 2948 | return getGlobalVariableForwardDeclaration(VD); |
| 2949 | |
Duncan P. N. Exon Smith | 4078ad4 | 2015-04-16 16:36:45 +0000 | [diff] [blame] | 2950 | return nullptr; |
David Blaikie | bd48376 | 2013-05-20 04:58:53 +0000 | [diff] [blame] | 2951 | } |
| 2952 | |
Duncan P. N. Exon Smith | 9dd4e4e | 2015-04-29 16:40:08 +0000 | [diff] [blame] | 2953 | llvm::DISubprogram *CGDebugInfo::getFunctionDeclaration(const Decl *D) { |
Benjamin Kramer | 8c30592 | 2016-02-02 11:06:51 +0000 | [diff] [blame] | 2954 | if (!D || DebugKind <= codegenoptions::DebugLineTablesOnly) |
Duncan P. N. Exon Smith | a7fbcbf | 2015-04-20 22:09:57 +0000 | [diff] [blame] | 2955 | return nullptr; |
David Blaikie | 18cfbc5 | 2013-06-22 00:09:36 +0000 | [diff] [blame] | 2956 | |
David Majnemer | 58ed0f3 | 2016-07-17 00:39:12 +0000 | [diff] [blame] | 2957 | const auto *FD = dyn_cast<FunctionDecl>(D); |
Eric Christopher | e7b87e5 | 2014-10-26 23:40:33 +0000 | [diff] [blame] | 2958 | if (!FD) |
Duncan P. N. Exon Smith | a7fbcbf | 2015-04-20 22:09:57 +0000 | [diff] [blame] | 2959 | return nullptr; |
Guy Benyei | 11169dd | 2012-12-18 14:30:41 +0000 | [diff] [blame] | 2960 | |
| 2961 | // Setup context. |
Adrian Prantl | 6ec370a | 2015-09-10 18:39:45 +0000 | [diff] [blame] | 2962 | auto *S = getDeclContextDescriptor(D); |
Guy Benyei | 11169dd | 2012-12-18 14:30:41 +0000 | [diff] [blame] | 2963 | |
Duncan P. N. Exon Smith | fb49491 | 2014-12-09 18:39:32 +0000 | [diff] [blame] | 2964 | auto MI = SPCache.find(FD->getCanonicalDecl()); |
David Blaikie | fd07c60 | 2013-08-09 17:20:05 +0000 | [diff] [blame] | 2965 | if (MI == SPCache.end()) { |
David Majnemer | 58ed0f3 | 2016-07-17 00:39:12 +0000 | [diff] [blame] | 2966 | if (const auto *MD = dyn_cast<CXXMethodDecl>(FD->getCanonicalDecl())) { |
Duncan P. N. Exon Smith | c09c548 | 2015-04-20 21:17:26 +0000 | [diff] [blame] | 2967 | return CreateCXXMemberFunction(MD, getOrCreateFile(MD->getLocation()), |
Duncan P. N. Exon Smith | 9dd4e4e | 2015-04-29 16:40:08 +0000 | [diff] [blame] | 2968 | cast<llvm::DICompositeType>(S)); |
David Blaikie | fd07c60 | 2013-08-09 17:20:05 +0000 | [diff] [blame] | 2969 | } |
| 2970 | } |
Guy Benyei | 11169dd | 2012-12-18 14:30:41 +0000 | [diff] [blame] | 2971 | if (MI != SPCache.end()) { |
Duncan P. N. Exon Smith | 9dd4e4e | 2015-04-29 16:40:08 +0000 | [diff] [blame] | 2972 | auto *SP = dyn_cast_or_null<llvm::DISubprogram>(MI->second); |
Duncan P. N. Exon Smith | 87afdeb | 2015-04-14 03:24:14 +0000 | [diff] [blame] | 2973 | if (SP && !SP->isDefinition()) |
Guy Benyei | 11169dd | 2012-12-18 14:30:41 +0000 | [diff] [blame] | 2974 | return SP; |
| 2975 | } |
| 2976 | |
Aaron Ballman | 86c9390 | 2014-03-06 23:45:36 +0000 | [diff] [blame] | 2977 | for (auto NextFD : FD->redecls()) { |
Duncan P. N. Exon Smith | fb49491 | 2014-12-09 18:39:32 +0000 | [diff] [blame] | 2978 | auto MI = SPCache.find(NextFD->getCanonicalDecl()); |
Guy Benyei | 11169dd | 2012-12-18 14:30:41 +0000 | [diff] [blame] | 2979 | if (MI != SPCache.end()) { |
Duncan P. N. Exon Smith | 9dd4e4e | 2015-04-29 16:40:08 +0000 | [diff] [blame] | 2980 | auto *SP = dyn_cast_or_null<llvm::DISubprogram>(MI->second); |
Duncan P. N. Exon Smith | 87afdeb | 2015-04-14 03:24:14 +0000 | [diff] [blame] | 2981 | if (SP && !SP->isDefinition()) |
Guy Benyei | 11169dd | 2012-12-18 14:30:41 +0000 | [diff] [blame] | 2982 | return SP; |
| 2983 | } |
| 2984 | } |
Duncan P. N. Exon Smith | a7fbcbf | 2015-04-20 22:09:57 +0000 | [diff] [blame] | 2985 | return nullptr; |
Guy Benyei | 11169dd | 2012-12-18 14:30:41 +0000 | [diff] [blame] | 2986 | } |
| 2987 | |
Duncan P. N. Exon Smith | 0b6c369 | 2015-04-20 18:51:48 +0000 | [diff] [blame] | 2988 | // getOrCreateFunctionType - Construct type. If it is a c++ method, include |
Guy Benyei | 11169dd | 2012-12-18 14:30:41 +0000 | [diff] [blame] | 2989 | // implicit parameter "this". |
Duncan P. N. Exon Smith | 9dd4e4e | 2015-04-29 16:40:08 +0000 | [diff] [blame] | 2990 | llvm::DISubroutineType *CGDebugInfo::getOrCreateFunctionType(const Decl *D, |
Duncan P. N. Exon Smith | 4078ad4 | 2015-04-16 16:36:45 +0000 | [diff] [blame] | 2991 | QualType FnType, |
Duncan P. N. Exon Smith | 9dd4e4e | 2015-04-29 16:40:08 +0000 | [diff] [blame] | 2992 | llvm::DIFile *F) { |
Benjamin Kramer | 8c30592 | 2016-02-02 11:06:51 +0000 | [diff] [blame] | 2993 | if (!D || DebugKind <= codegenoptions::DebugLineTablesOnly) |
Duncan P. N. Exon Smith | a7fbcbf | 2015-04-20 22:09:57 +0000 | [diff] [blame] | 2994 | // Create fake but valid subroutine type. Otherwise -verify would fail, and |
| 2995 | // subprogram DIE will miss DW_AT_decl_file and DW_AT_decl_line fields. |
Eric Christopher | 28a6db5 | 2015-10-15 06:56:08 +0000 | [diff] [blame] | 2996 | return DBuilder.createSubroutineType(DBuilder.getOrCreateTypeArray(None)); |
Guy Benyei | 11169dd | 2012-12-18 14:30:41 +0000 | [diff] [blame] | 2997 | |
David Majnemer | 58ed0f3 | 2016-07-17 00:39:12 +0000 | [diff] [blame] | 2998 | if (const auto *Method = dyn_cast<CXXMethodDecl>(D)) |
Guy Benyei | 11169dd | 2012-12-18 14:30:41 +0000 | [diff] [blame] | 2999 | return getOrCreateMethodType(Method, F); |
Reid Kleckner | f00f803 | 2016-06-08 20:41:54 +0000 | [diff] [blame] | 3000 | |
| 3001 | const auto *FTy = FnType->getAs<FunctionType>(); |
| 3002 | CallingConv CC = FTy ? FTy->getCallConv() : CallingConv::CC_C; |
| 3003 | |
David Majnemer | 58ed0f3 | 2016-07-17 00:39:12 +0000 | [diff] [blame] | 3004 | if (const auto *OMethod = dyn_cast<ObjCMethodDecl>(D)) { |
Guy Benyei | 11169dd | 2012-12-18 14:30:41 +0000 | [diff] [blame] | 3005 | // Add "self" and "_cmd" |
Duncan P. N. Exon Smith | fb49491 | 2014-12-09 18:39:32 +0000 | [diff] [blame] | 3006 | SmallVector<llvm::Metadata *, 16> Elts; |
Guy Benyei | 11169dd | 2012-12-18 14:30:41 +0000 | [diff] [blame] | 3007 | |
| 3008 | // First element is always return type. For 'void' functions it is NULL. |
Alp Toker | 314cc81 | 2014-01-25 16:55:45 +0000 | [diff] [blame] | 3009 | QualType ResultTy = OMethod->getReturnType(); |
Adrian Prantl | 5f36010 | 2013-05-22 21:37:49 +0000 | [diff] [blame] | 3010 | |
| 3011 | // Replace the instancetype keyword with the actual type. |
| 3012 | if (ResultTy == CGM.getContext().getObjCInstanceType()) |
| 3013 | ResultTy = CGM.getContext().getPointerType( |
Eric Christopher | e7b87e5 | 2014-10-26 23:40:33 +0000 | [diff] [blame] | 3014 | QualType(OMethod->getClassInterface()->getTypeForDecl(), 0)); |
Adrian Prantl | 5f36010 | 2013-05-22 21:37:49 +0000 | [diff] [blame] | 3015 | |
Adrian Prantl | 7bec903 | 2013-05-10 21:08:31 +0000 | [diff] [blame] | 3016 | Elts.push_back(getOrCreateType(ResultTy, F)); |
Guy Benyei | 11169dd | 2012-12-18 14:30:41 +0000 | [diff] [blame] | 3017 | // "self" pointer is always first argument. |
Adrian Prantl | 748a6cd | 2015-09-08 20:41:52 +0000 | [diff] [blame] | 3018 | QualType SelfDeclTy; |
| 3019 | if (auto *SelfDecl = OMethod->getSelfDecl()) |
| 3020 | SelfDeclTy = SelfDecl->getType(); |
| 3021 | else if (auto *FPT = dyn_cast<FunctionProtoType>(FnType)) |
| 3022 | if (FPT->getNumParams() > 1) |
| 3023 | SelfDeclTy = FPT->getParamType(0); |
| 3024 | if (!SelfDeclTy.isNull()) |
| 3025 | Elts.push_back(CreateSelfType(SelfDeclTy, getOrCreateType(SelfDeclTy, F))); |
Guy Benyei | 11169dd | 2012-12-18 14:30:41 +0000 | [diff] [blame] | 3026 | // "_cmd" pointer is always second argument. |
Duncan P. N. Exon Smith | 0b6c369 | 2015-04-20 18:51:48 +0000 | [diff] [blame] | 3027 | Elts.push_back(DBuilder.createArtificialType( |
Adrian Prantl | 748a6cd | 2015-09-08 20:41:52 +0000 | [diff] [blame] | 3028 | getOrCreateType(CGM.getContext().getObjCSelType(), F))); |
Guy Benyei | 11169dd | 2012-12-18 14:30:41 +0000 | [diff] [blame] | 3029 | // Get rest of the arguments. |
David Majnemer | 59f7792 | 2016-06-24 04:05:48 +0000 | [diff] [blame] | 3030 | for (const auto *PI : OMethod->parameters()) |
Aaron Ballman | 43b68be | 2014-03-07 17:50:17 +0000 | [diff] [blame] | 3031 | Elts.push_back(getOrCreateType(PI->getType(), F)); |
Frederic Riss | 787d9d6 | 2014-08-12 04:42:23 +0000 | [diff] [blame] | 3032 | // Variadic methods need a special marker at the end of the type list. |
| 3033 | if (OMethod->isVariadic()) |
| 3034 | Elts.push_back(DBuilder.createUnspecifiedParameter()); |
Guy Benyei | 11169dd | 2012-12-18 14:30:41 +0000 | [diff] [blame] | 3035 | |
Duncan P. N. Exon Smith | 9dd4e4e | 2015-04-29 16:40:08 +0000 | [diff] [blame] | 3036 | llvm::DITypeRefArray EltTypeArray = DBuilder.getOrCreateTypeArray(Elts); |
Leny Kholodov | 80c047d | 2016-09-06 10:48:04 +0000 | [diff] [blame] | 3037 | return DBuilder.createSubroutineType(EltTypeArray, llvm::DINode::FlagZero, |
| 3038 | getDwarfCC(CC)); |
Guy Benyei | 11169dd | 2012-12-18 14:30:41 +0000 | [diff] [blame] | 3039 | } |
Adrian Prantl | d45ba25 | 2014-02-25 19:38:11 +0000 | [diff] [blame] | 3040 | |
Adrian Prantl | 800faef | 2014-02-25 23:42:18 +0000 | [diff] [blame] | 3041 | // Handle variadic function types; they need an additional |
| 3042 | // unspecified parameter. |
David Majnemer | 58ed0f3 | 2016-07-17 00:39:12 +0000 | [diff] [blame] | 3043 | if (const auto *FD = dyn_cast<FunctionDecl>(D)) |
Adrian Prantl | d45ba25 | 2014-02-25 19:38:11 +0000 | [diff] [blame] | 3044 | if (FD->isVariadic()) { |
Duncan P. N. Exon Smith | fb49491 | 2014-12-09 18:39:32 +0000 | [diff] [blame] | 3045 | SmallVector<llvm::Metadata *, 16> EltTys; |
Adrian Prantl | d45ba25 | 2014-02-25 19:38:11 +0000 | [diff] [blame] | 3046 | EltTys.push_back(getOrCreateType(FD->getReturnType(), F)); |
David Majnemer | 58ed0f3 | 2016-07-17 00:39:12 +0000 | [diff] [blame] | 3047 | if (const auto *FPT = dyn_cast<FunctionProtoType>(FnType)) |
| 3048 | for (QualType ParamType : FPT->param_types()) |
| 3049 | EltTys.push_back(getOrCreateType(ParamType, F)); |
Adrian Prantl | d45ba25 | 2014-02-25 19:38:11 +0000 | [diff] [blame] | 3050 | EltTys.push_back(DBuilder.createUnspecifiedParameter()); |
Duncan P. N. Exon Smith | 9dd4e4e | 2015-04-29 16:40:08 +0000 | [diff] [blame] | 3051 | llvm::DITypeRefArray EltTypeArray = DBuilder.getOrCreateTypeArray(EltTys); |
Leny Kholodov | 80c047d | 2016-09-06 10:48:04 +0000 | [diff] [blame] | 3052 | return DBuilder.createSubroutineType(EltTypeArray, llvm::DINode::FlagZero, |
| 3053 | getDwarfCC(CC)); |
Adrian Prantl | d45ba25 | 2014-02-25 19:38:11 +0000 | [diff] [blame] | 3054 | } |
| 3055 | |
Duncan P. N. Exon Smith | 9dd4e4e | 2015-04-29 16:40:08 +0000 | [diff] [blame] | 3056 | return cast<llvm::DISubroutineType>(getOrCreateType(FnType, F)); |
Guy Benyei | 11169dd | 2012-12-18 14:30:41 +0000 | [diff] [blame] | 3057 | } |
| 3058 | |
Eric Christopher | e7b87e5 | 2014-10-26 23:40:33 +0000 | [diff] [blame] | 3059 | void CGDebugInfo::EmitFunctionStart(GlobalDecl GD, SourceLocation Loc, |
| 3060 | SourceLocation ScopeLoc, QualType FnType, |
| 3061 | llvm::Function *Fn, CGBuilderTy &Builder) { |
Guy Benyei | 11169dd | 2012-12-18 14:30:41 +0000 | [diff] [blame] | 3062 | |
| 3063 | StringRef Name; |
| 3064 | StringRef LinkageName; |
| 3065 | |
| 3066 | FnBeginRegionCount.push_back(LexicalBlockStack.size()); |
| 3067 | |
| 3068 | const Decl *D = GD.getDecl(); |
Craig Topper | 8a13c41 | 2014-05-21 05:09:00 +0000 | [diff] [blame] | 3069 | bool HasDecl = (D != nullptr); |
Eric Christopher | 885c41b | 2014-04-01 22:25:28 +0000 | [diff] [blame] | 3070 | |
Leny Kholodov | 80c047d | 2016-09-06 10:48:04 +0000 | [diff] [blame] | 3071 | llvm::DINode::DIFlags Flags = llvm::DINode::FlagZero; |
Duncan P. N. Exon Smith | 9dd4e4e | 2015-04-29 16:40:08 +0000 | [diff] [blame] | 3072 | llvm::DIFile *Unit = getOrCreateFile(Loc); |
| 3073 | llvm::DIScope *FDContext = Unit; |
| 3074 | llvm::DINodeArray TParamsArray; |
Guy Benyei | 11169dd | 2012-12-18 14:30:41 +0000 | [diff] [blame] | 3075 | if (!HasDecl) { |
| 3076 | // Use llvm function name. |
David Blaikie | ebe87e1 | 2013-08-27 23:57:18 +0000 | [diff] [blame] | 3077 | LinkageName = Fn->getName(); |
David Majnemer | 58ed0f3 | 2016-07-17 00:39:12 +0000 | [diff] [blame] | 3078 | } else if (const auto *FD = dyn_cast<FunctionDecl>(D)) { |
Duncan P. N. Exon Smith | a7fbcbf | 2015-04-20 22:09:57 +0000 | [diff] [blame] | 3079 | // If there is a subprogram for this function available then use it. |
Duncan P. N. Exon Smith | fb49491 | 2014-12-09 18:39:32 +0000 | [diff] [blame] | 3080 | auto FI = SPCache.find(FD->getCanonicalDecl()); |
Guy Benyei | 11169dd | 2012-12-18 14:30:41 +0000 | [diff] [blame] | 3081 | if (FI != SPCache.end()) { |
Duncan P. N. Exon Smith | 9dd4e4e | 2015-04-29 16:40:08 +0000 | [diff] [blame] | 3082 | auto *SP = dyn_cast_or_null<llvm::DISubprogram>(FI->second); |
Duncan P. N. Exon Smith | 87afdeb | 2015-04-14 03:24:14 +0000 | [diff] [blame] | 3083 | if (SP && SP->isDefinition()) { |
Duncan P. N. Exon Smith | d899f6e | 2015-04-18 00:07:30 +0000 | [diff] [blame] | 3084 | LexicalBlockStack.emplace_back(SP); |
Duncan P. N. Exon Smith | fb49491 | 2014-12-09 18:39:32 +0000 | [diff] [blame] | 3085 | RegionMap[D].reset(SP); |
Guy Benyei | 11169dd | 2012-12-18 14:30:41 +0000 | [diff] [blame] | 3086 | return; |
| 3087 | } |
| 3088 | } |
Frederic Riss | 9db79f1 | 2014-11-18 03:40:46 +0000 | [diff] [blame] | 3089 | collectFunctionDeclProps(GD, Unit, Name, LinkageName, FDContext, |
| 3090 | TParamsArray, Flags); |
David Majnemer | 58ed0f3 | 2016-07-17 00:39:12 +0000 | [diff] [blame] | 3091 | } else if (const auto *OMD = dyn_cast<ObjCMethodDecl>(D)) { |
Guy Benyei | 11169dd | 2012-12-18 14:30:41 +0000 | [diff] [blame] | 3092 | Name = getObjCMethodName(OMD); |
Duncan P. N. Exon Smith | 9dd4e4e | 2015-04-29 16:40:08 +0000 | [diff] [blame] | 3093 | Flags |= llvm::DINode::FlagPrototyped; |
Guy Benyei | 11169dd | 2012-12-18 14:30:41 +0000 | [diff] [blame] | 3094 | } else { |
| 3095 | // Use llvm function name. |
| 3096 | Name = Fn->getName(); |
Duncan P. N. Exon Smith | 9dd4e4e | 2015-04-29 16:40:08 +0000 | [diff] [blame] | 3097 | Flags |= llvm::DINode::FlagPrototyped; |
Guy Benyei | 11169dd | 2012-12-18 14:30:41 +0000 | [diff] [blame] | 3098 | } |
David Majnemer | 58ed0f3 | 2016-07-17 00:39:12 +0000 | [diff] [blame] | 3099 | if (Name.startswith("\01")) |
Guy Benyei | 11169dd | 2012-12-18 14:30:41 +0000 | [diff] [blame] | 3100 | Name = Name.substr(1); |
| 3101 | |
Adrian Prantl | 42d71b9 | 2014-04-10 23:21:53 +0000 | [diff] [blame] | 3102 | if (!HasDecl || D->isImplicit()) { |
Duncan P. N. Exon Smith | 9dd4e4e | 2015-04-29 16:40:08 +0000 | [diff] [blame] | 3103 | Flags |= llvm::DINode::FlagArtificial; |
Adrian Prantl | db76357 | 2016-11-09 21:43:51 +0000 | [diff] [blame] | 3104 | // Artificial functions should not silently reuse CurLoc. |
| 3105 | CurLoc = SourceLocation(); |
Adrian Prantl | 42d71b9 | 2014-04-10 23:21:53 +0000 | [diff] [blame] | 3106 | } |
| 3107 | unsigned LineNo = getLineNumber(Loc); |
| 3108 | unsigned ScopeLine = getLineNumber(ScopeLoc); |
Guy Benyei | 11169dd | 2012-12-18 14:30:41 +0000 | [diff] [blame] | 3109 | |
Eric Christopher | 8018e41 | 2014-03-27 18:50:35 +0000 | [diff] [blame] | 3110 | // FIXME: The function declaration we're constructing here is mostly reusing |
| 3111 | // declarations from CXXMethodDecl and not constructing new ones for arbitrary |
| 3112 | // FunctionDecls. When/if we fix this we can have FDContext be TheCU/null for |
| 3113 | // all subprograms instead of the actual context since subprogram definitions |
| 3114 | // are emitted as CU level entities by the backend. |
Duncan P. N. Exon Smith | 9dd4e4e | 2015-04-29 16:40:08 +0000 | [diff] [blame] | 3115 | llvm::DISubprogram *SP = DBuilder.createFunction( |
Eric Christopher | e7b87e5 | 2014-10-26 23:40:33 +0000 | [diff] [blame] | 3116 | FDContext, Name, LinkageName, Unit, LineNo, |
David Majnemer | 36a6e00 | 2016-07-06 21:07:53 +0000 | [diff] [blame] | 3117 | getOrCreateFunctionType(D, FnType, Unit), Fn->hasLocalLinkage(), |
Peter Collingbourne | 0900fe0 | 2015-11-05 22:04:14 +0000 | [diff] [blame] | 3118 | true /*definition*/, ScopeLine, Flags, CGM.getLangOpts().Optimize, |
Duncan P. N. Exon Smith | ebad0aa | 2015-04-07 16:50:49 +0000 | [diff] [blame] | 3119 | TParamsArray.get(), getFunctionDeclaration(D)); |
Peter Collingbourne | 0900fe0 | 2015-11-05 22:04:14 +0000 | [diff] [blame] | 3120 | Fn->setSubprogram(SP); |
Frederic Riss | b1ab28c | 2014-11-05 19:19:04 +0000 | [diff] [blame] | 3121 | // We might get here with a VarDecl in the case we're generating |
| 3122 | // code for the initialization of globals. Do not record these decls |
| 3123 | // as they will overwrite the actual VarDecl Decl in the cache. |
| 3124 | if (HasDecl && isa<FunctionDecl>(D)) |
David Majnemer | 58ed0f3 | 2016-07-17 00:39:12 +0000 | [diff] [blame] | 3125 | DeclCache[D->getCanonicalDecl()].reset(SP); |
Guy Benyei | 11169dd | 2012-12-18 14:30:41 +0000 | [diff] [blame] | 3126 | |
Adrian Prantl | bebb893 | 2014-03-21 21:01:58 +0000 | [diff] [blame] | 3127 | // Push the function onto the lexical block stack. |
Duncan P. N. Exon Smith | d899f6e | 2015-04-18 00:07:30 +0000 | [diff] [blame] | 3128 | LexicalBlockStack.emplace_back(SP); |
Adrian Prantl | bebb893 | 2014-03-21 21:01:58 +0000 | [diff] [blame] | 3129 | |
Guy Benyei | 11169dd | 2012-12-18 14:30:41 +0000 | [diff] [blame] | 3130 | if (HasDecl) |
Duncan P. N. Exon Smith | fb49491 | 2014-12-09 18:39:32 +0000 | [diff] [blame] | 3131 | RegionMap[D].reset(SP); |
Guy Benyei | 11169dd | 2012-12-18 14:30:41 +0000 | [diff] [blame] | 3132 | } |
| 3133 | |
Adrian Prantl | 748a6cd | 2015-09-08 20:41:52 +0000 | [diff] [blame] | 3134 | void CGDebugInfo::EmitFunctionDecl(GlobalDecl GD, SourceLocation Loc, |
| 3135 | QualType FnType) { |
| 3136 | StringRef Name; |
| 3137 | StringRef LinkageName; |
| 3138 | |
| 3139 | const Decl *D = GD.getDecl(); |
| 3140 | if (!D) |
| 3141 | return; |
| 3142 | |
Leny Kholodov | 80c047d | 2016-09-06 10:48:04 +0000 | [diff] [blame] | 3143 | llvm::DINode::DIFlags Flags = llvm::DINode::FlagZero; |
Adrian Prantl | 748a6cd | 2015-09-08 20:41:52 +0000 | [diff] [blame] | 3144 | llvm::DIFile *Unit = getOrCreateFile(Loc); |
Adrian Prantl | f0cd677 | 2015-10-04 23:23:04 +0000 | [diff] [blame] | 3145 | llvm::DIScope *FDContext = getDeclContextDescriptor(D); |
Adrian Prantl | 748a6cd | 2015-09-08 20:41:52 +0000 | [diff] [blame] | 3146 | llvm::DINodeArray TParamsArray; |
| 3147 | if (isa<FunctionDecl>(D)) { |
| 3148 | // If there is a DISubprogram for this function available then use it. |
| 3149 | collectFunctionDeclProps(GD, Unit, Name, LinkageName, FDContext, |
| 3150 | TParamsArray, Flags); |
David Majnemer | 58ed0f3 | 2016-07-17 00:39:12 +0000 | [diff] [blame] | 3151 | } else if (const auto *OMD = dyn_cast<ObjCMethodDecl>(D)) { |
Adrian Prantl | 748a6cd | 2015-09-08 20:41:52 +0000 | [diff] [blame] | 3152 | Name = getObjCMethodName(OMD); |
| 3153 | Flags |= llvm::DINode::FlagPrototyped; |
| 3154 | } else { |
| 3155 | llvm_unreachable("not a function or ObjC method"); |
| 3156 | } |
| 3157 | if (!Name.empty() && Name[0] == '\01') |
| 3158 | Name = Name.substr(1); |
| 3159 | |
| 3160 | if (D->isImplicit()) { |
| 3161 | Flags |= llvm::DINode::FlagArtificial; |
| 3162 | // Artificial functions without a location should not silently reuse CurLoc. |
| 3163 | if (Loc.isInvalid()) |
| 3164 | CurLoc = SourceLocation(); |
| 3165 | } |
| 3166 | unsigned LineNo = getLineNumber(Loc); |
| 3167 | unsigned ScopeLine = 0; |
| 3168 | |
Adrian Prantl | e76bda5 | 2016-04-15 15:55:45 +0000 | [diff] [blame] | 3169 | DBuilder.retainType(DBuilder.createFunction( |
| 3170 | FDContext, Name, LinkageName, Unit, LineNo, |
| 3171 | getOrCreateFunctionType(D, FnType, Unit), false /*internalLinkage*/, |
| 3172 | false /*definition*/, ScopeLine, Flags, CGM.getLangOpts().Optimize, |
| 3173 | TParamsArray.get(), getFunctionDeclaration(D))); |
Adrian Prantl | 748a6cd | 2015-09-08 20:41:52 +0000 | [diff] [blame] | 3174 | } |
| 3175 | |
David Blaikie | 835afb2 | 2015-01-21 23:08:17 +0000 | [diff] [blame] | 3176 | void CGDebugInfo::EmitLocation(CGBuilderTy &Builder, SourceLocation Loc) { |
Guy Benyei | 11169dd | 2012-12-18 14:30:41 +0000 | [diff] [blame] | 3177 | // Update our current location |
| 3178 | setLocation(Loc); |
| 3179 | |
Eric Christopher | e7b87e5 | 2014-10-26 23:40:33 +0000 | [diff] [blame] | 3180 | if (CurLoc.isInvalid() || CurLoc.isMacroID()) |
| 3181 | return; |
Guy Benyei | 11169dd | 2012-12-18 14:30:41 +0000 | [diff] [blame] | 3182 | |
Adrian Prantl | e83b130 | 2014-01-07 22:05:52 +0000 | [diff] [blame] | 3183 | llvm::MDNode *Scope = LexicalBlockStack.back(); |
Eric Christopher | e7b87e5 | 2014-10-26 23:40:33 +0000 | [diff] [blame] | 3184 | Builder.SetCurrentDebugLocation(llvm::DebugLoc::get( |
David Blaikie | 835afb2 | 2015-01-21 23:08:17 +0000 | [diff] [blame] | 3185 | getLineNumber(CurLoc), getColumnNumber(CurLoc), Scope)); |
Guy Benyei | 11169dd | 2012-12-18 14:30:41 +0000 | [diff] [blame] | 3186 | } |
| 3187 | |
Guy Benyei | 11169dd | 2012-12-18 14:30:41 +0000 | [diff] [blame] | 3188 | void CGDebugInfo::CreateLexicalBlock(SourceLocation Loc) { |
Duncan P. N. Exon Smith | a66e305 | 2014-12-09 19:22:40 +0000 | [diff] [blame] | 3189 | llvm::MDNode *Back = nullptr; |
| 3190 | if (!LexicalBlockStack.empty()) |
| 3191 | Back = LexicalBlockStack.back().get(); |
Duncan P. N. Exon Smith | d899f6e | 2015-04-18 00:07:30 +0000 | [diff] [blame] | 3192 | LexicalBlockStack.emplace_back(DBuilder.createLexicalBlock( |
Duncan P. N. Exon Smith | 9dd4e4e | 2015-04-29 16:40:08 +0000 | [diff] [blame] | 3193 | cast<llvm::DIScope>(Back), getOrCreateFile(CurLoc), getLineNumber(CurLoc), |
Duncan P. N. Exon Smith | d899f6e | 2015-04-18 00:07:30 +0000 | [diff] [blame] | 3194 | getColumnNumber(CurLoc))); |
Guy Benyei | 11169dd | 2012-12-18 14:30:41 +0000 | [diff] [blame] | 3195 | } |
| 3196 | |
Eric Christopher | 0fdcb31 | 2013-05-16 00:52:20 +0000 | [diff] [blame] | 3197 | void CGDebugInfo::EmitLexicalBlockStart(CGBuilderTy &Builder, |
| 3198 | SourceLocation Loc) { |
Guy Benyei | 11169dd | 2012-12-18 14:30:41 +0000 | [diff] [blame] | 3199 | // Set our current location. |
| 3200 | setLocation(Loc); |
| 3201 | |
Guy Benyei | 11169dd | 2012-12-18 14:30:41 +0000 | [diff] [blame] | 3202 | // Emit a line table change for the current location inside the new scope. |
Eric Christopher | e7b87e5 | 2014-10-26 23:40:33 +0000 | [diff] [blame] | 3203 | Builder.SetCurrentDebugLocation(llvm::DebugLoc::get( |
| 3204 | getLineNumber(Loc), getColumnNumber(Loc), LexicalBlockStack.back())); |
David Blaikie | 60a877b | 2014-10-22 19:34:33 +0000 | [diff] [blame] | 3205 | |
Benjamin Kramer | 8c30592 | 2016-02-02 11:06:51 +0000 | [diff] [blame] | 3206 | if (DebugKind <= codegenoptions::DebugLineTablesOnly) |
David Blaikie | 60a877b | 2014-10-22 19:34:33 +0000 | [diff] [blame] | 3207 | return; |
| 3208 | |
| 3209 | // Create a new lexical block and push it on the stack. |
| 3210 | CreateLexicalBlock(Loc); |
Guy Benyei | 11169dd | 2012-12-18 14:30:41 +0000 | [diff] [blame] | 3211 | } |
| 3212 | |
Eric Christopher | 0fdcb31 | 2013-05-16 00:52:20 +0000 | [diff] [blame] | 3213 | void CGDebugInfo::EmitLexicalBlockEnd(CGBuilderTy &Builder, |
| 3214 | SourceLocation Loc) { |
Guy Benyei | 11169dd | 2012-12-18 14:30:41 +0000 | [diff] [blame] | 3215 | assert(!LexicalBlockStack.empty() && "Region stack mismatch, stack empty!"); |
| 3216 | |
| 3217 | // Provide an entry in the line table for the end of the block. |
| 3218 | EmitLocation(Builder, Loc); |
| 3219 | |
Benjamin Kramer | 8c30592 | 2016-02-02 11:06:51 +0000 | [diff] [blame] | 3220 | if (DebugKind <= codegenoptions::DebugLineTablesOnly) |
David Blaikie | 60a877b | 2014-10-22 19:34:33 +0000 | [diff] [blame] | 3221 | return; |
| 3222 | |
Guy Benyei | 11169dd | 2012-12-18 14:30:41 +0000 | [diff] [blame] | 3223 | LexicalBlockStack.pop_back(); |
| 3224 | } |
| 3225 | |
Guy Benyei | 11169dd | 2012-12-18 14:30:41 +0000 | [diff] [blame] | 3226 | void CGDebugInfo::EmitFunctionEnd(CGBuilderTy &Builder) { |
| 3227 | assert(!LexicalBlockStack.empty() && "Region stack mismatch, stack empty!"); |
| 3228 | unsigned RCount = FnBeginRegionCount.back(); |
| 3229 | assert(RCount <= LexicalBlockStack.size() && "Region stack mismatch"); |
| 3230 | |
| 3231 | // Pop all regions for this function. |
David Blaikie | 60a877b | 2014-10-22 19:34:33 +0000 | [diff] [blame] | 3232 | while (LexicalBlockStack.size() != RCount) { |
| 3233 | // Provide an entry in the line table for the end of the block. |
| 3234 | EmitLocation(Builder, CurLoc); |
| 3235 | LexicalBlockStack.pop_back(); |
| 3236 | } |
Guy Benyei | 11169dd | 2012-12-18 14:30:41 +0000 | [diff] [blame] | 3237 | FnBeginRegionCount.pop_back(); |
| 3238 | } |
| 3239 | |
Duncan P. N. Exon Smith | 9dd4e4e | 2015-04-29 16:40:08 +0000 | [diff] [blame] | 3240 | llvm::DIType *CGDebugInfo::EmitTypeForVarWithBlocksAttr(const VarDecl *VD, |
Duncan P. N. Exon Smith | 0b6c369 | 2015-04-20 18:51:48 +0000 | [diff] [blame] | 3241 | uint64_t *XOffset) { |
Guy Benyei | 11169dd | 2012-12-18 14:30:41 +0000 | [diff] [blame] | 3242 | |
Duncan P. N. Exon Smith | fb49491 | 2014-12-09 18:39:32 +0000 | [diff] [blame] | 3243 | SmallVector<llvm::Metadata *, 5> EltTys; |
Guy Benyei | 11169dd | 2012-12-18 14:30:41 +0000 | [diff] [blame] | 3244 | QualType FType; |
| 3245 | uint64_t FieldSize, FieldOffset; |
Victor Leschuk | 802e4a5 | 2016-10-19 22:11:07 +0000 | [diff] [blame] | 3246 | uint32_t FieldAlign; |
Eric Christopher | b2a008c | 2013-05-16 00:45:12 +0000 | [diff] [blame] | 3247 | |
Duncan P. N. Exon Smith | 9dd4e4e | 2015-04-29 16:40:08 +0000 | [diff] [blame] | 3248 | llvm::DIFile *Unit = getOrCreateFile(VD->getLocation()); |
Eric Christopher | b2a008c | 2013-05-16 00:45:12 +0000 | [diff] [blame] | 3249 | QualType Type = VD->getType(); |
Guy Benyei | 11169dd | 2012-12-18 14:30:41 +0000 | [diff] [blame] | 3250 | |
| 3251 | FieldOffset = 0; |
| 3252 | FType = CGM.getContext().getPointerType(CGM.getContext().VoidTy); |
| 3253 | EltTys.push_back(CreateMemberType(Unit, FType, "__isa", &FieldOffset)); |
| 3254 | EltTys.push_back(CreateMemberType(Unit, FType, "__forwarding", &FieldOffset)); |
| 3255 | FType = CGM.getContext().IntTy; |
| 3256 | EltTys.push_back(CreateMemberType(Unit, FType, "__flags", &FieldOffset)); |
| 3257 | EltTys.push_back(CreateMemberType(Unit, FType, "__size", &FieldOffset)); |
| 3258 | |
| 3259 | bool HasCopyAndDispose = CGM.getContext().BlockRequiresCopying(Type, VD); |
| 3260 | if (HasCopyAndDispose) { |
| 3261 | FType = CGM.getContext().getPointerType(CGM.getContext().VoidTy); |
Eric Christopher | e7b87e5 | 2014-10-26 23:40:33 +0000 | [diff] [blame] | 3262 | EltTys.push_back( |
| 3263 | CreateMemberType(Unit, FType, "__copy_helper", &FieldOffset)); |
| 3264 | EltTys.push_back( |
| 3265 | CreateMemberType(Unit, FType, "__destroy_helper", &FieldOffset)); |
Guy Benyei | 11169dd | 2012-12-18 14:30:41 +0000 | [diff] [blame] | 3266 | } |
| 3267 | bool HasByrefExtendedLayout; |
| 3268 | Qualifiers::ObjCLifetime Lifetime; |
Eric Christopher | e7b87e5 | 2014-10-26 23:40:33 +0000 | [diff] [blame] | 3269 | if (CGM.getContext().getByrefLifetime(Type, Lifetime, |
| 3270 | HasByrefExtendedLayout) && |
| 3271 | HasByrefExtendedLayout) { |
Adrian Prantl | ead2ba4 | 2013-07-23 00:12:14 +0000 | [diff] [blame] | 3272 | FType = CGM.getContext().getPointerType(CGM.getContext().VoidTy); |
Eric Christopher | e7b87e5 | 2014-10-26 23:40:33 +0000 | [diff] [blame] | 3273 | EltTys.push_back( |
| 3274 | CreateMemberType(Unit, FType, "__byref_variable_layout", &FieldOffset)); |
Adrian Prantl | ead2ba4 | 2013-07-23 00:12:14 +0000 | [diff] [blame] | 3275 | } |
Eric Christopher | b2a008c | 2013-05-16 00:45:12 +0000 | [diff] [blame] | 3276 | |
Guy Benyei | 11169dd | 2012-12-18 14:30:41 +0000 | [diff] [blame] | 3277 | CharUnits Align = CGM.getContext().getDeclAlign(VD); |
| 3278 | if (Align > CGM.getContext().toCharUnitsFromBits( |
Eric Christopher | e7b87e5 | 2014-10-26 23:40:33 +0000 | [diff] [blame] | 3279 | CGM.getTarget().getPointerAlign(0))) { |
| 3280 | CharUnits FieldOffsetInBytes = |
| 3281 | CGM.getContext().toCharUnitsFromBits(FieldOffset); |
Rui Ueyama | 83aa979 | 2016-01-14 21:00:27 +0000 | [diff] [blame] | 3282 | CharUnits AlignedOffsetInBytes = FieldOffsetInBytes.alignTo(Align); |
Eric Christopher | e7b87e5 | 2014-10-26 23:40:33 +0000 | [diff] [blame] | 3283 | CharUnits NumPaddingBytes = AlignedOffsetInBytes - FieldOffsetInBytes; |
Eric Christopher | b2a008c | 2013-05-16 00:45:12 +0000 | [diff] [blame] | 3284 | |
Guy Benyei | 11169dd | 2012-12-18 14:30:41 +0000 | [diff] [blame] | 3285 | if (NumPaddingBytes.isPositive()) { |
| 3286 | llvm::APInt pad(32, NumPaddingBytes.getQuantity()); |
| 3287 | FType = CGM.getContext().getConstantArrayType(CGM.getContext().CharTy, |
| 3288 | pad, ArrayType::Normal, 0); |
| 3289 | EltTys.push_back(CreateMemberType(Unit, FType, "", &FieldOffset)); |
| 3290 | } |
| 3291 | } |
Eric Christopher | b2a008c | 2013-05-16 00:45:12 +0000 | [diff] [blame] | 3292 | |
Guy Benyei | 11169dd | 2012-12-18 14:30:41 +0000 | [diff] [blame] | 3293 | FType = Type; |
Duncan P. N. Exon Smith | 9dd4e4e | 2015-04-29 16:40:08 +0000 | [diff] [blame] | 3294 | llvm::DIType *FieldTy = getOrCreateType(FType, Unit); |
Guy Benyei | 11169dd | 2012-12-18 14:30:41 +0000 | [diff] [blame] | 3295 | FieldSize = CGM.getContext().getTypeSize(FType); |
| 3296 | FieldAlign = CGM.getContext().toBits(Align); |
| 3297 | |
Eric Christopher | b2a008c | 2013-05-16 00:45:12 +0000 | [diff] [blame] | 3298 | *XOffset = FieldOffset; |
Eric Christopher | e7b87e5 | 2014-10-26 23:40:33 +0000 | [diff] [blame] | 3299 | FieldTy = DBuilder.createMemberType(Unit, VD->getName(), Unit, 0, FieldSize, |
Leny Kholodov | 80c047d | 2016-09-06 10:48:04 +0000 | [diff] [blame] | 3300 | FieldAlign, FieldOffset, |
| 3301 | llvm::DINode::FlagZero, FieldTy); |
Guy Benyei | 11169dd | 2012-12-18 14:30:41 +0000 | [diff] [blame] | 3302 | EltTys.push_back(FieldTy); |
| 3303 | FieldOffset += FieldSize; |
Eric Christopher | b2a008c | 2013-05-16 00:45:12 +0000 | [diff] [blame] | 3304 | |
Duncan P. N. Exon Smith | 9dd4e4e | 2015-04-29 16:40:08 +0000 | [diff] [blame] | 3305 | llvm::DINodeArray Elements = DBuilder.getOrCreateArray(EltTys); |
Eric Christopher | b2a008c | 2013-05-16 00:45:12 +0000 | [diff] [blame] | 3306 | |
Leny Kholodov | 80c047d | 2016-09-06 10:48:04 +0000 | [diff] [blame] | 3307 | llvm::DINode::DIFlags Flags = llvm::DINode::FlagBlockByrefStruct; |
Eric Christopher | b2a008c | 2013-05-16 00:45:12 +0000 | [diff] [blame] | 3308 | |
Guy Benyei | 11169dd | 2012-12-18 14:30:41 +0000 | [diff] [blame] | 3309 | return DBuilder.createStructType(Unit, "", Unit, 0, FieldOffset, 0, Flags, |
Duncan P. N. Exon Smith | 0b6c369 | 2015-04-20 18:51:48 +0000 | [diff] [blame] | 3310 | nullptr, Elements); |
Guy Benyei | 11169dd | 2012-12-18 14:30:41 +0000 | [diff] [blame] | 3311 | } |
| 3312 | |
Duncan P. N. Exon Smith | e430654 | 2015-07-31 17:56:14 +0000 | [diff] [blame] | 3313 | void CGDebugInfo::EmitDeclare(const VarDecl *VD, llvm::Value *Storage, |
| 3314 | llvm::Optional<unsigned> ArgNo, |
Eric Christopher | e7b87e5 | 2014-10-26 23:40:33 +0000 | [diff] [blame] | 3315 | CGBuilderTy &Builder) { |
Benjamin Kramer | 8c30592 | 2016-02-02 11:06:51 +0000 | [diff] [blame] | 3316 | assert(DebugKind >= codegenoptions::LimitedDebugInfo); |
Guy Benyei | 11169dd | 2012-12-18 14:30:41 +0000 | [diff] [blame] | 3317 | assert(!LexicalBlockStack.empty() && "Region stack mismatch, stack empty!"); |
Paul Robinson | afd2dde | 2016-06-16 00:42:36 +0000 | [diff] [blame] | 3318 | if (VD->hasAttr<NoDebugAttr>()) |
| 3319 | return; |
Guy Benyei | 11169dd | 2012-12-18 14:30:41 +0000 | [diff] [blame] | 3320 | |
David Blaikie | 7fceebf | 2013-08-19 03:37:48 +0000 | [diff] [blame] | 3321 | bool Unwritten = |
| 3322 | VD->isImplicit() || (isa<Decl>(VD->getDeclContext()) && |
| 3323 | cast<Decl>(VD->getDeclContext())->isImplicit()); |
Duncan P. N. Exon Smith | 9dd4e4e | 2015-04-29 16:40:08 +0000 | [diff] [blame] | 3324 | llvm::DIFile *Unit = nullptr; |
David Blaikie | 7fceebf | 2013-08-19 03:37:48 +0000 | [diff] [blame] | 3325 | if (!Unwritten) |
| 3326 | Unit = getOrCreateFile(VD->getLocation()); |
Duncan P. N. Exon Smith | 9dd4e4e | 2015-04-29 16:40:08 +0000 | [diff] [blame] | 3327 | llvm::DIType *Ty; |
Guy Benyei | 11169dd | 2012-12-18 14:30:41 +0000 | [diff] [blame] | 3328 | uint64_t XOffset = 0; |
| 3329 | if (VD->hasAttr<BlocksAttr>()) |
| 3330 | Ty = EmitTypeForVarWithBlocksAttr(VD, &XOffset); |
Eric Christopher | b2a008c | 2013-05-16 00:45:12 +0000 | [diff] [blame] | 3331 | else |
Guy Benyei | 11169dd | 2012-12-18 14:30:41 +0000 | [diff] [blame] | 3332 | Ty = getOrCreateType(VD->getType(), Unit); |
| 3333 | |
| 3334 | // If there is no debug info for this type then do not emit debug info |
| 3335 | // for this variable. |
| 3336 | if (!Ty) |
| 3337 | return; |
| 3338 | |
Guy Benyei | 11169dd | 2012-12-18 14:30:41 +0000 | [diff] [blame] | 3339 | // Get location information. |
David Blaikie | 7fceebf | 2013-08-19 03:37:48 +0000 | [diff] [blame] | 3340 | unsigned Line = 0; |
| 3341 | unsigned Column = 0; |
| 3342 | if (!Unwritten) { |
| 3343 | Line = getLineNumber(VD->getLocation()); |
| 3344 | Column = getColumnNumber(VD->getLocation()); |
| 3345 | } |
Adrian Prantl | 7c6f944 | 2015-01-19 17:51:58 +0000 | [diff] [blame] | 3346 | SmallVector<int64_t, 9> Expr; |
Leny Kholodov | 80c047d | 2016-09-06 10:48:04 +0000 | [diff] [blame] | 3347 | llvm::DINode::DIFlags Flags = llvm::DINode::FlagZero; |
Guy Benyei | 11169dd | 2012-12-18 14:30:41 +0000 | [diff] [blame] | 3348 | if (VD->isImplicit()) |
Duncan P. N. Exon Smith | 9dd4e4e | 2015-04-29 16:40:08 +0000 | [diff] [blame] | 3349 | Flags |= llvm::DINode::FlagArtificial; |
Victor Leschuk | a7ece03 | 2016-10-20 00:13:19 +0000 | [diff] [blame] | 3350 | |
| 3351 | auto Align = getDeclAlignIfRequired(VD, CGM.getContext()); |
| 3352 | |
Guy Benyei | 11169dd | 2012-12-18 14:30:41 +0000 | [diff] [blame] | 3353 | // If this is the first argument and it is implicit then |
| 3354 | // give it an object pointer flag. |
| 3355 | // FIXME: There has to be a better way to do this, but for static |
| 3356 | // functions there won't be an implicit param at arg1 and |
| 3357 | // otherwise it is 'self' or 'this'. |
Duncan P. N. Exon Smith | e430654 | 2015-07-31 17:56:14 +0000 | [diff] [blame] | 3358 | if (isa<ImplicitParamDecl>(VD) && ArgNo && *ArgNo == 1) |
Duncan P. N. Exon Smith | 9dd4e4e | 2015-04-29 16:40:08 +0000 | [diff] [blame] | 3359 | Flags |= llvm::DINode::FlagObjectPointer; |
David Majnemer | 58ed0f3 | 2016-07-17 00:39:12 +0000 | [diff] [blame] | 3360 | if (auto *Arg = dyn_cast<llvm::Argument>(Storage)) |
Eric Christopher | ffdeb1e | 2013-07-17 22:52:53 +0000 | [diff] [blame] | 3361 | if (Arg->getType()->isPointerTy() && !Arg->hasByValAttr() && |
| 3362 | !VD->getType()->isPointerType()) |
Adrian Prantl | 7c6f944 | 2015-01-19 17:51:58 +0000 | [diff] [blame] | 3363 | Expr.push_back(llvm::dwarf::DW_OP_deref); |
Guy Benyei | 11169dd | 2012-12-18 14:30:41 +0000 | [diff] [blame] | 3364 | |
Duncan P. N. Exon Smith | 9dd4e4e | 2015-04-29 16:40:08 +0000 | [diff] [blame] | 3365 | auto *Scope = cast<llvm::DIScope>(LexicalBlockStack.back()); |
Guy Benyei | 11169dd | 2012-12-18 14:30:41 +0000 | [diff] [blame] | 3366 | |
| 3367 | StringRef Name = VD->getName(); |
| 3368 | if (!Name.empty()) { |
| 3369 | if (VD->hasAttr<BlocksAttr>()) { |
| 3370 | CharUnits offset = CharUnits::fromQuantity(32); |
Adrian Prantl | 7c6f944 | 2015-01-19 17:51:58 +0000 | [diff] [blame] | 3371 | Expr.push_back(llvm::dwarf::DW_OP_plus); |
Guy Benyei | 11169dd | 2012-12-18 14:30:41 +0000 | [diff] [blame] | 3372 | // offset of __forwarding field |
| 3373 | offset = CGM.getContext().toCharUnitsFromBits( |
Eric Christopher | e7b87e5 | 2014-10-26 23:40:33 +0000 | [diff] [blame] | 3374 | CGM.getTarget().getPointerWidth(0)); |
Adrian Prantl | 7c6f944 | 2015-01-19 17:51:58 +0000 | [diff] [blame] | 3375 | Expr.push_back(offset.getQuantity()); |
| 3376 | Expr.push_back(llvm::dwarf::DW_OP_deref); |
| 3377 | Expr.push_back(llvm::dwarf::DW_OP_plus); |
Guy Benyei | 11169dd | 2012-12-18 14:30:41 +0000 | [diff] [blame] | 3378 | // offset of x field |
| 3379 | offset = CGM.getContext().toCharUnitsFromBits(XOffset); |
Adrian Prantl | 7c6f944 | 2015-01-19 17:51:58 +0000 | [diff] [blame] | 3380 | Expr.push_back(offset.getQuantity()); |
Guy Benyei | 11169dd | 2012-12-18 14:30:41 +0000 | [diff] [blame] | 3381 | |
| 3382 | // Create the descriptor for the variable. |
Duncan P. N. Exon Smith | e430654 | 2015-07-31 17:56:14 +0000 | [diff] [blame] | 3383 | auto *D = ArgNo |
| 3384 | ? DBuilder.createParameterVariable(Scope, VD->getName(), |
| 3385 | *ArgNo, Unit, Line, Ty) |
| 3386 | : DBuilder.createAutoVariable(Scope, VD->getName(), Unit, |
Victor Leschuk | a7ece03 | 2016-10-20 00:13:19 +0000 | [diff] [blame] | 3387 | Line, Ty, Align); |
Eric Christopher | b2a008c | 2013-05-16 00:45:12 +0000 | [diff] [blame] | 3388 | |
Guy Benyei | 11169dd | 2012-12-18 14:30:41 +0000 | [diff] [blame] | 3389 | // Insert an llvm.dbg.declare into the current block. |
Duncan P. N. Exon Smith | fe88b48 | 2015-04-15 21:18:30 +0000 | [diff] [blame] | 3390 | DBuilder.insertDeclare(Storage, D, DBuilder.createExpression(Expr), |
| 3391 | llvm::DebugLoc::get(Line, Column, Scope), |
| 3392 | Builder.GetInsertBlock()); |
Guy Benyei | 11169dd | 2012-12-18 14:30:41 +0000 | [diff] [blame] | 3393 | return; |
Adrian Prantl | 7f2ef22 | 2013-09-18 22:18:17 +0000 | [diff] [blame] | 3394 | } else if (isa<VariableArrayType>(VD->getType())) |
Adrian Prantl | 7c6f944 | 2015-01-19 17:51:58 +0000 | [diff] [blame] | 3395 | Expr.push_back(llvm::dwarf::DW_OP_deref); |
David Majnemer | 58ed0f3 | 2016-07-17 00:39:12 +0000 | [diff] [blame] | 3396 | } else if (const auto *RT = dyn_cast<RecordType>(VD->getType())) { |
Adrian Prantl | 0d82089 | 2015-04-29 15:05:50 +0000 | [diff] [blame] | 3397 | // If VD is an anonymous union then Storage represents value for |
| 3398 | // all union fields. |
David Majnemer | 58ed0f3 | 2016-07-17 00:39:12 +0000 | [diff] [blame] | 3399 | const auto *RD = cast<RecordDecl>(RT->getDecl()); |
Adrian Prantl | 0d82089 | 2015-04-29 15:05:50 +0000 | [diff] [blame] | 3400 | if (RD->isUnion() && RD->isAnonymousStructOrUnion()) { |
Adrian Prantl | 00820ab | 2015-04-29 16:52:31 +0000 | [diff] [blame] | 3401 | // GDB has trouble finding local variables in anonymous unions, so we emit |
| 3402 | // artifical local variables for each of the members. |
| 3403 | // |
| 3404 | // FIXME: Remove this code as soon as GDB supports this. |
| 3405 | // The debug info verifier in LLVM operates based on the assumption that a |
| 3406 | // variable has the same size as its storage and we had to disable the check |
| 3407 | // for artificial variables. |
Adrian Prantl | 0d82089 | 2015-04-29 15:05:50 +0000 | [diff] [blame] | 3408 | for (const auto *Field : RD->fields()) { |
Duncan P. N. Exon Smith | 9dd4e4e | 2015-04-29 16:40:08 +0000 | [diff] [blame] | 3409 | llvm::DIType *FieldTy = getOrCreateType(Field->getType(), Unit); |
Adrian Prantl | 0d82089 | 2015-04-29 15:05:50 +0000 | [diff] [blame] | 3410 | StringRef FieldName = Field->getName(); |
| 3411 | |
| 3412 | // Ignore unnamed fields. Do not ignore unnamed records. |
| 3413 | if (FieldName.empty() && !isa<RecordType>(Field->getType())) |
| 3414 | continue; |
| 3415 | |
| 3416 | // Use VarDecl's Tag, Scope and Line number. |
Victor Leschuk | a7ece03 | 2016-10-20 00:13:19 +0000 | [diff] [blame] | 3417 | auto FieldAlign = getDeclAlignIfRequired(Field, CGM.getContext()); |
Duncan P. N. Exon Smith | e430654 | 2015-07-31 17:56:14 +0000 | [diff] [blame] | 3418 | auto *D = DBuilder.createAutoVariable( |
| 3419 | Scope, FieldName, Unit, Line, FieldTy, CGM.getLangOpts().Optimize, |
Victor Leschuk | a7ece03 | 2016-10-20 00:13:19 +0000 | [diff] [blame] | 3420 | Flags | llvm::DINode::FlagArtificial, FieldAlign); |
Adrian Prantl | 0d82089 | 2015-04-29 15:05:50 +0000 | [diff] [blame] | 3421 | |
| 3422 | // Insert an llvm.dbg.declare into the current block. |
| 3423 | DBuilder.insertDeclare(Storage, D, DBuilder.createExpression(Expr), |
| 3424 | llvm::DebugLoc::get(Line, Column, Scope), |
| 3425 | Builder.GetInsertBlock()); |
| 3426 | } |
Adrian Prantl | 0d82089 | 2015-04-29 15:05:50 +0000 | [diff] [blame] | 3427 | } |
Guy Benyei | 11169dd | 2012-12-18 14:30:41 +0000 | [diff] [blame] | 3428 | } |
David Blaikie | a76a7c9 | 2013-01-05 05:58:35 +0000 | [diff] [blame] | 3429 | |
| 3430 | // Create the descriptor for the variable. |
Victor Leschuk | a7ece03 | 2016-10-20 00:13:19 +0000 | [diff] [blame] | 3431 | auto *D = ArgNo |
| 3432 | ? DBuilder.createParameterVariable( |
| 3433 | Scope, Name, *ArgNo, Unit, Line, Ty, |
| 3434 | CGM.getLangOpts().Optimize, Flags) |
| 3435 | : DBuilder.createAutoVariable(Scope, Name, Unit, Line, Ty, |
| 3436 | CGM.getLangOpts().Optimize, Flags, |
| 3437 | Align); |
David Blaikie | a76a7c9 | 2013-01-05 05:58:35 +0000 | [diff] [blame] | 3438 | |
| 3439 | // Insert an llvm.dbg.declare into the current block. |
Duncan P. N. Exon Smith | fe88b48 | 2015-04-15 21:18:30 +0000 | [diff] [blame] | 3440 | DBuilder.insertDeclare(Storage, D, DBuilder.createExpression(Expr), |
| 3441 | llvm::DebugLoc::get(Line, Column, Scope), |
| 3442 | Builder.GetInsertBlock()); |
Guy Benyei | 11169dd | 2012-12-18 14:30:41 +0000 | [diff] [blame] | 3443 | } |
| 3444 | |
| 3445 | void CGDebugInfo::EmitDeclareOfAutoVariable(const VarDecl *VD, |
| 3446 | llvm::Value *Storage, |
| 3447 | CGBuilderTy &Builder) { |
Benjamin Kramer | 8c30592 | 2016-02-02 11:06:51 +0000 | [diff] [blame] | 3448 | assert(DebugKind >= codegenoptions::LimitedDebugInfo); |
Duncan P. N. Exon Smith | e430654 | 2015-07-31 17:56:14 +0000 | [diff] [blame] | 3449 | EmitDeclare(VD, Storage, llvm::None, Builder); |
Guy Benyei | 11169dd | 2012-12-18 14:30:41 +0000 | [diff] [blame] | 3450 | } |
| 3451 | |
Duncan P. N. Exon Smith | 9dd4e4e | 2015-04-29 16:40:08 +0000 | [diff] [blame] | 3452 | llvm::DIType *CGDebugInfo::CreateSelfType(const QualType &QualTy, |
| 3453 | llvm::DIType *Ty) { |
| 3454 | llvm::DIType *CachedTy = getTypeOrNull(QualTy); |
Eric Christopher | e7b87e5 | 2014-10-26 23:40:33 +0000 | [diff] [blame] | 3455 | if (CachedTy) |
| 3456 | Ty = CachedTy; |
Adrian Prantl | de17db3 | 2013-03-29 19:20:29 +0000 | [diff] [blame] | 3457 | return DBuilder.createObjectPointerType(Ty); |
| 3458 | } |
| 3459 | |
Eric Christopher | e7b87e5 | 2014-10-26 23:40:33 +0000 | [diff] [blame] | 3460 | void CGDebugInfo::EmitDeclareOfBlockDeclRefVariable( |
| 3461 | const VarDecl *VD, llvm::Value *Storage, CGBuilderTy &Builder, |
Adrian Prantl | 88eec39 | 2014-11-21 00:35:25 +0000 | [diff] [blame] | 3462 | const CGBlockInfo &blockInfo, llvm::Instruction *InsertPoint) { |
Benjamin Kramer | 8c30592 | 2016-02-02 11:06:51 +0000 | [diff] [blame] | 3463 | assert(DebugKind >= codegenoptions::LimitedDebugInfo); |
Guy Benyei | 11169dd | 2012-12-18 14:30:41 +0000 | [diff] [blame] | 3464 | assert(!LexicalBlockStack.empty() && "Region stack mismatch, stack empty!"); |
Eric Christopher | b2a008c | 2013-05-16 00:45:12 +0000 | [diff] [blame] | 3465 | |
Craig Topper | 8a13c41 | 2014-05-21 05:09:00 +0000 | [diff] [blame] | 3466 | if (Builder.GetInsertBlock() == nullptr) |
Guy Benyei | 11169dd | 2012-12-18 14:30:41 +0000 | [diff] [blame] | 3467 | return; |
Paul Robinson | afd2dde | 2016-06-16 00:42:36 +0000 | [diff] [blame] | 3468 | if (VD->hasAttr<NoDebugAttr>()) |
| 3469 | return; |
Eric Christopher | b2a008c | 2013-05-16 00:45:12 +0000 | [diff] [blame] | 3470 | |
Guy Benyei | 11169dd | 2012-12-18 14:30:41 +0000 | [diff] [blame] | 3471 | bool isByRef = VD->hasAttr<BlocksAttr>(); |
Eric Christopher | b2a008c | 2013-05-16 00:45:12 +0000 | [diff] [blame] | 3472 | |
Guy Benyei | 11169dd | 2012-12-18 14:30:41 +0000 | [diff] [blame] | 3473 | uint64_t XOffset = 0; |
Duncan P. N. Exon Smith | 9dd4e4e | 2015-04-29 16:40:08 +0000 | [diff] [blame] | 3474 | llvm::DIFile *Unit = getOrCreateFile(VD->getLocation()); |
| 3475 | llvm::DIType *Ty; |
Guy Benyei | 11169dd | 2012-12-18 14:30:41 +0000 | [diff] [blame] | 3476 | if (isByRef) |
| 3477 | Ty = EmitTypeForVarWithBlocksAttr(VD, &XOffset); |
Eric Christopher | b2a008c | 2013-05-16 00:45:12 +0000 | [diff] [blame] | 3478 | else |
Guy Benyei | 11169dd | 2012-12-18 14:30:41 +0000 | [diff] [blame] | 3479 | Ty = getOrCreateType(VD->getType(), Unit); |
| 3480 | |
| 3481 | // Self is passed along as an implicit non-arg variable in a |
| 3482 | // block. Mark it as the object pointer. |
| 3483 | if (isa<ImplicitParamDecl>(VD) && VD->getName() == "self") |
Adrian Prantl | de17db3 | 2013-03-29 19:20:29 +0000 | [diff] [blame] | 3484 | Ty = CreateSelfType(VD->getType(), Ty); |
Guy Benyei | 11169dd | 2012-12-18 14:30:41 +0000 | [diff] [blame] | 3485 | |
| 3486 | // Get location information. |
| 3487 | unsigned Line = getLineNumber(VD->getLocation()); |
| 3488 | unsigned Column = getColumnNumber(VD->getLocation()); |
| 3489 | |
| 3490 | const llvm::DataLayout &target = CGM.getDataLayout(); |
| 3491 | |
| 3492 | CharUnits offset = CharUnits::fromQuantity( |
Eric Christopher | e7b87e5 | 2014-10-26 23:40:33 +0000 | [diff] [blame] | 3493 | target.getStructLayout(blockInfo.StructureType) |
Guy Benyei | 11169dd | 2012-12-18 14:30:41 +0000 | [diff] [blame] | 3494 | ->getElementOffset(blockInfo.getCapture(VD).getIndex())); |
| 3495 | |
Duncan P. N. Exon Smith | f3dc429 | 2014-10-01 20:26:18 +0000 | [diff] [blame] | 3496 | SmallVector<int64_t, 9> addr; |
Adrian Prantl | 0f6df00 | 2013-03-29 19:20:35 +0000 | [diff] [blame] | 3497 | if (isa<llvm::AllocaInst>(Storage)) |
Duncan P. N. Exon Smith | f3dc429 | 2014-10-01 20:26:18 +0000 | [diff] [blame] | 3498 | addr.push_back(llvm::dwarf::DW_OP_deref); |
| 3499 | addr.push_back(llvm::dwarf::DW_OP_plus); |
| 3500 | addr.push_back(offset.getQuantity()); |
Guy Benyei | 11169dd | 2012-12-18 14:30:41 +0000 | [diff] [blame] | 3501 | if (isByRef) { |
Duncan P. N. Exon Smith | f3dc429 | 2014-10-01 20:26:18 +0000 | [diff] [blame] | 3502 | addr.push_back(llvm::dwarf::DW_OP_deref); |
| 3503 | addr.push_back(llvm::dwarf::DW_OP_plus); |
Guy Benyei | 11169dd | 2012-12-18 14:30:41 +0000 | [diff] [blame] | 3504 | // offset of __forwarding field |
Eric Christopher | e7b87e5 | 2014-10-26 23:40:33 +0000 | [diff] [blame] | 3505 | offset = |
| 3506 | CGM.getContext().toCharUnitsFromBits(target.getPointerSizeInBits(0)); |
Duncan P. N. Exon Smith | f3dc429 | 2014-10-01 20:26:18 +0000 | [diff] [blame] | 3507 | addr.push_back(offset.getQuantity()); |
| 3508 | addr.push_back(llvm::dwarf::DW_OP_deref); |
| 3509 | addr.push_back(llvm::dwarf::DW_OP_plus); |
Guy Benyei | 11169dd | 2012-12-18 14:30:41 +0000 | [diff] [blame] | 3510 | // offset of x field |
| 3511 | offset = CGM.getContext().toCharUnitsFromBits(XOffset); |
Duncan P. N. Exon Smith | f3dc429 | 2014-10-01 20:26:18 +0000 | [diff] [blame] | 3512 | addr.push_back(offset.getQuantity()); |
Guy Benyei | 11169dd | 2012-12-18 14:30:41 +0000 | [diff] [blame] | 3513 | } |
| 3514 | |
| 3515 | // Create the descriptor for the variable. |
Victor Leschuk | a7ece03 | 2016-10-20 00:13:19 +0000 | [diff] [blame] | 3516 | auto Align = getDeclAlignIfRequired(VD, CGM.getContext()); |
Duncan P. N. Exon Smith | e430654 | 2015-07-31 17:56:14 +0000 | [diff] [blame] | 3517 | auto *D = DBuilder.createAutoVariable( |
Duncan P. N. Exon Smith | 9dd4e4e | 2015-04-29 16:40:08 +0000 | [diff] [blame] | 3518 | cast<llvm::DILocalScope>(LexicalBlockStack.back()), VD->getName(), Unit, |
Victor Leschuk | a7ece03 | 2016-10-20 00:13:19 +0000 | [diff] [blame] | 3519 | Line, Ty, false, llvm::DINode::FlagZero, Align); |
Adrian Prantl | 0f6df00 | 2013-03-29 19:20:35 +0000 | [diff] [blame] | 3520 | |
Guy Benyei | 11169dd | 2012-12-18 14:30:41 +0000 | [diff] [blame] | 3521 | // Insert an llvm.dbg.declare into the current block. |
Duncan P. N. Exon Smith | fe88b48 | 2015-04-15 21:18:30 +0000 | [diff] [blame] | 3522 | auto DL = llvm::DebugLoc::get(Line, Column, LexicalBlockStack.back()); |
| 3523 | if (InsertPoint) |
| 3524 | DBuilder.insertDeclare(Storage, D, DBuilder.createExpression(addr), DL, |
| 3525 | InsertPoint); |
| 3526 | else |
| 3527 | DBuilder.insertDeclare(Storage, D, DBuilder.createExpression(addr), DL, |
| 3528 | Builder.GetInsertBlock()); |
Guy Benyei | 11169dd | 2012-12-18 14:30:41 +0000 | [diff] [blame] | 3529 | } |
| 3530 | |
Guy Benyei | 11169dd | 2012-12-18 14:30:41 +0000 | [diff] [blame] | 3531 | void CGDebugInfo::EmitDeclareOfArgVariable(const VarDecl *VD, llvm::Value *AI, |
| 3532 | unsigned ArgNo, |
| 3533 | CGBuilderTy &Builder) { |
Benjamin Kramer | 8c30592 | 2016-02-02 11:06:51 +0000 | [diff] [blame] | 3534 | assert(DebugKind >= codegenoptions::LimitedDebugInfo); |
Duncan P. N. Exon Smith | e430654 | 2015-07-31 17:56:14 +0000 | [diff] [blame] | 3535 | EmitDeclare(VD, AI, ArgNo, Builder); |
Guy Benyei | 11169dd | 2012-12-18 14:30:41 +0000 | [diff] [blame] | 3536 | } |
| 3537 | |
| 3538 | namespace { |
Eric Christopher | e7b87e5 | 2014-10-26 23:40:33 +0000 | [diff] [blame] | 3539 | struct BlockLayoutChunk { |
| 3540 | uint64_t OffsetInBits; |
| 3541 | const BlockDecl::Capture *Capture; |
| 3542 | }; |
| 3543 | bool operator<(const BlockLayoutChunk &l, const BlockLayoutChunk &r) { |
| 3544 | return l.OffsetInBits < r.OffsetInBits; |
| 3545 | } |
Guy Benyei | 11169dd | 2012-12-18 14:30:41 +0000 | [diff] [blame] | 3546 | } |
| 3547 | |
| 3548 | void CGDebugInfo::EmitDeclareOfBlockLiteralArgVariable(const CGBlockInfo &block, |
Adrian Prantl | 51936dd | 2013-03-14 17:53:33 +0000 | [diff] [blame] | 3549 | llvm::Value *Arg, |
David Blaikie | 77bbb5f | 2014-08-08 17:10:14 +0000 | [diff] [blame] | 3550 | unsigned ArgNo, |
Adrian Prantl | 51936dd | 2013-03-14 17:53:33 +0000 | [diff] [blame] | 3551 | llvm::Value *LocalAddr, |
Guy Benyei | 11169dd | 2012-12-18 14:30:41 +0000 | [diff] [blame] | 3552 | CGBuilderTy &Builder) { |
Benjamin Kramer | 8c30592 | 2016-02-02 11:06:51 +0000 | [diff] [blame] | 3553 | assert(DebugKind >= codegenoptions::LimitedDebugInfo); |
Guy Benyei | 11169dd | 2012-12-18 14:30:41 +0000 | [diff] [blame] | 3554 | ASTContext &C = CGM.getContext(); |
| 3555 | const BlockDecl *blockDecl = block.getBlockDecl(); |
| 3556 | |
| 3557 | // Collect some general information about the block's location. |
| 3558 | SourceLocation loc = blockDecl->getCaretLocation(); |
Duncan P. N. Exon Smith | 9dd4e4e | 2015-04-29 16:40:08 +0000 | [diff] [blame] | 3559 | llvm::DIFile *tunit = getOrCreateFile(loc); |
Guy Benyei | 11169dd | 2012-12-18 14:30:41 +0000 | [diff] [blame] | 3560 | unsigned line = getLineNumber(loc); |
| 3561 | unsigned column = getColumnNumber(loc); |
Eric Christopher | b2a008c | 2013-05-16 00:45:12 +0000 | [diff] [blame] | 3562 | |
Guy Benyei | 11169dd | 2012-12-18 14:30:41 +0000 | [diff] [blame] | 3563 | // Build the debug-info type for the block literal. |
Adrian Prantl | 6ec370a | 2015-09-10 18:39:45 +0000 | [diff] [blame] | 3564 | getDeclContextDescriptor(blockDecl); |
Guy Benyei | 11169dd | 2012-12-18 14:30:41 +0000 | [diff] [blame] | 3565 | |
| 3566 | const llvm::StructLayout *blockLayout = |
Eric Christopher | e7b87e5 | 2014-10-26 23:40:33 +0000 | [diff] [blame] | 3567 | CGM.getDataLayout().getStructLayout(block.StructureType); |
Guy Benyei | 11169dd | 2012-12-18 14:30:41 +0000 | [diff] [blame] | 3568 | |
Duncan P. N. Exon Smith | fb49491 | 2014-12-09 18:39:32 +0000 | [diff] [blame] | 3569 | SmallVector<llvm::Metadata *, 16> fields; |
David Majnemer | b4b671e | 2016-06-30 03:01:59 +0000 | [diff] [blame] | 3570 | fields.push_back(createFieldType("__isa", C.VoidPtrTy, loc, AS_public, |
Guy Benyei | 11169dd | 2012-12-18 14:30:41 +0000 | [diff] [blame] | 3571 | blockLayout->getElementOffsetInBits(0), |
| 3572 | tunit, tunit)); |
David Majnemer | b4b671e | 2016-06-30 03:01:59 +0000 | [diff] [blame] | 3573 | fields.push_back(createFieldType("__flags", C.IntTy, loc, AS_public, |
Guy Benyei | 11169dd | 2012-12-18 14:30:41 +0000 | [diff] [blame] | 3574 | blockLayout->getElementOffsetInBits(1), |
| 3575 | tunit, tunit)); |
David Majnemer | b4b671e | 2016-06-30 03:01:59 +0000 | [diff] [blame] | 3576 | fields.push_back(createFieldType("__reserved", C.IntTy, loc, AS_public, |
Guy Benyei | 11169dd | 2012-12-18 14:30:41 +0000 | [diff] [blame] | 3577 | blockLayout->getElementOffsetInBits(2), |
| 3578 | tunit, tunit)); |
Adrian Prantl | 65d5d00 | 2014-11-05 01:01:30 +0000 | [diff] [blame] | 3579 | auto *FnTy = block.getBlockExpr()->getFunctionType(); |
| 3580 | auto FnPtrType = CGM.getContext().getPointerType(FnTy->desugar()); |
David Majnemer | b4b671e | 2016-06-30 03:01:59 +0000 | [diff] [blame] | 3581 | fields.push_back(createFieldType("__FuncPtr", FnPtrType, loc, AS_public, |
Guy Benyei | 11169dd | 2012-12-18 14:30:41 +0000 | [diff] [blame] | 3582 | blockLayout->getElementOffsetInBits(3), |
| 3583 | tunit, tunit)); |
Eric Christopher | e7b87e5 | 2014-10-26 23:40:33 +0000 | [diff] [blame] | 3584 | fields.push_back(createFieldType( |
| 3585 | "__descriptor", C.getPointerType(block.NeedsCopyDispose |
| 3586 | ? C.getBlockDescriptorExtendedType() |
| 3587 | : C.getBlockDescriptorType()), |
David Majnemer | b4b671e | 2016-06-30 03:01:59 +0000 | [diff] [blame] | 3588 | loc, AS_public, blockLayout->getElementOffsetInBits(4), tunit, tunit)); |
Guy Benyei | 11169dd | 2012-12-18 14:30:41 +0000 | [diff] [blame] | 3589 | |
| 3590 | // We want to sort the captures by offset, not because DWARF |
| 3591 | // requires this, but because we're paranoid about debuggers. |
| 3592 | SmallVector<BlockLayoutChunk, 8> chunks; |
| 3593 | |
| 3594 | // 'this' capture. |
| 3595 | if (blockDecl->capturesCXXThis()) { |
| 3596 | BlockLayoutChunk chunk; |
| 3597 | chunk.OffsetInBits = |
Eric Christopher | e7b87e5 | 2014-10-26 23:40:33 +0000 | [diff] [blame] | 3598 | blockLayout->getElementOffsetInBits(block.CXXThisIndex); |
Craig Topper | 8a13c41 | 2014-05-21 05:09:00 +0000 | [diff] [blame] | 3599 | chunk.Capture = nullptr; |
Guy Benyei | 11169dd | 2012-12-18 14:30:41 +0000 | [diff] [blame] | 3600 | chunks.push_back(chunk); |
| 3601 | } |
| 3602 | |
| 3603 | // Variable captures. |
Aaron Ballman | 9371dd2 | 2014-03-14 18:34:04 +0000 | [diff] [blame] | 3604 | for (const auto &capture : blockDecl->captures()) { |
Guy Benyei | 11169dd | 2012-12-18 14:30:41 +0000 | [diff] [blame] | 3605 | const VarDecl *variable = capture.getVariable(); |
| 3606 | const CGBlockInfo::Capture &captureInfo = block.getCapture(variable); |
| 3607 | |
| 3608 | // Ignore constant captures. |
| 3609 | if (captureInfo.isConstant()) |
| 3610 | continue; |
| 3611 | |
| 3612 | BlockLayoutChunk chunk; |
| 3613 | chunk.OffsetInBits = |
Eric Christopher | e7b87e5 | 2014-10-26 23:40:33 +0000 | [diff] [blame] | 3614 | blockLayout->getElementOffsetInBits(captureInfo.getIndex()); |
Guy Benyei | 11169dd | 2012-12-18 14:30:41 +0000 | [diff] [blame] | 3615 | chunk.Capture = &capture; |
| 3616 | chunks.push_back(chunk); |
| 3617 | } |
| 3618 | |
| 3619 | // Sort by offset. |
| 3620 | llvm::array_pod_sort(chunks.begin(), chunks.end()); |
| 3621 | |
David Majnemer | 58ed0f3 | 2016-07-17 00:39:12 +0000 | [diff] [blame] | 3622 | for (const BlockLayoutChunk &Chunk : chunks) { |
| 3623 | uint64_t offsetInBits = Chunk.OffsetInBits; |
| 3624 | const BlockDecl::Capture *capture = Chunk.Capture; |
Guy Benyei | 11169dd | 2012-12-18 14:30:41 +0000 | [diff] [blame] | 3625 | |
| 3626 | // If we have a null capture, this must be the C++ 'this' capture. |
| 3627 | if (!capture) { |
Adrian Prantl | 2526fca | 2016-04-18 23:48:16 +0000 | [diff] [blame] | 3628 | QualType type; |
| 3629 | if (auto *Method = |
| 3630 | cast_or_null<CXXMethodDecl>(blockDecl->getNonClosureContext())) |
| 3631 | type = Method->getThisType(C); |
| 3632 | else if (auto *RDecl = dyn_cast<CXXRecordDecl>(blockDecl->getParent())) |
| 3633 | type = QualType(RDecl->getTypeForDecl(), 0); |
| 3634 | else |
| 3635 | llvm_unreachable("unexpected block declcontext"); |
Guy Benyei | 11169dd | 2012-12-18 14:30:41 +0000 | [diff] [blame] | 3636 | |
David Majnemer | b4b671e | 2016-06-30 03:01:59 +0000 | [diff] [blame] | 3637 | fields.push_back(createFieldType("this", type, loc, AS_public, |
Guy Benyei | 11169dd | 2012-12-18 14:30:41 +0000 | [diff] [blame] | 3638 | offsetInBits, tunit, tunit)); |
| 3639 | continue; |
| 3640 | } |
| 3641 | |
| 3642 | const VarDecl *variable = capture->getVariable(); |
| 3643 | StringRef name = variable->getName(); |
| 3644 | |
Duncan P. N. Exon Smith | 9dd4e4e | 2015-04-29 16:40:08 +0000 | [diff] [blame] | 3645 | llvm::DIType *fieldType; |
Guy Benyei | 11169dd | 2012-12-18 14:30:41 +0000 | [diff] [blame] | 3646 | if (capture->isByRef()) { |
David Majnemer | 34b5749 | 2014-07-30 01:30:47 +0000 | [diff] [blame] | 3647 | TypeInfo PtrInfo = C.getTypeInfo(C.VoidPtrTy); |
Victor Leschuk | a7ece03 | 2016-10-20 00:13:19 +0000 | [diff] [blame] | 3648 | auto Align = PtrInfo.AlignIsRequired ? PtrInfo.Align : 0; |
Guy Benyei | 11169dd | 2012-12-18 14:30:41 +0000 | [diff] [blame] | 3649 | |
| 3650 | // FIXME: this creates a second copy of this type! |
| 3651 | uint64_t xoffset; |
| 3652 | fieldType = EmitTypeForVarWithBlocksAttr(variable, &xoffset); |
David Majnemer | 34b5749 | 2014-07-30 01:30:47 +0000 | [diff] [blame] | 3653 | fieldType = DBuilder.createPointerType(fieldType, PtrInfo.Width); |
Victor Leschuk | a7ece03 | 2016-10-20 00:13:19 +0000 | [diff] [blame] | 3654 | fieldType = DBuilder.createMemberType(tunit, name, tunit, line, |
| 3655 | PtrInfo.Width, Align, offsetInBits, |
| 3656 | llvm::DINode::FlagZero, fieldType); |
Guy Benyei | 11169dd | 2012-12-18 14:30:41 +0000 | [diff] [blame] | 3657 | } else { |
Victor Leschuk | a7ece03 | 2016-10-20 00:13:19 +0000 | [diff] [blame] | 3658 | auto Align = getDeclAlignIfRequired(variable, CGM.getContext()); |
David Majnemer | b4b671e | 2016-06-30 03:01:59 +0000 | [diff] [blame] | 3659 | fieldType = createFieldType(name, variable->getType(), loc, AS_public, |
Victor Leschuk | a7ece03 | 2016-10-20 00:13:19 +0000 | [diff] [blame] | 3660 | offsetInBits, Align, tunit, tunit); |
Guy Benyei | 11169dd | 2012-12-18 14:30:41 +0000 | [diff] [blame] | 3661 | } |
| 3662 | fields.push_back(fieldType); |
| 3663 | } |
| 3664 | |
| 3665 | SmallString<36> typeName; |
Eric Christopher | e7b87e5 | 2014-10-26 23:40:33 +0000 | [diff] [blame] | 3666 | llvm::raw_svector_ostream(typeName) << "__block_literal_" |
| 3667 | << CGM.getUniqueBlockCount(); |
Guy Benyei | 11169dd | 2012-12-18 14:30:41 +0000 | [diff] [blame] | 3668 | |
Duncan P. N. Exon Smith | 9dd4e4e | 2015-04-29 16:40:08 +0000 | [diff] [blame] | 3669 | llvm::DINodeArray fieldsArray = DBuilder.getOrCreateArray(fields); |
Guy Benyei | 11169dd | 2012-12-18 14:30:41 +0000 | [diff] [blame] | 3670 | |
Leny Kholodov | df050fd | 2016-09-06 17:06:14 +0000 | [diff] [blame] | 3671 | llvm::DIType *type = |
| 3672 | DBuilder.createStructType(tunit, typeName.str(), tunit, line, |
Victor Leschuk | a7ece03 | 2016-10-20 00:13:19 +0000 | [diff] [blame] | 3673 | CGM.getContext().toBits(block.BlockSize), 0, |
Leny Kholodov | df050fd | 2016-09-06 17:06:14 +0000 | [diff] [blame] | 3674 | llvm::DINode::FlagZero, nullptr, fieldsArray); |
Guy Benyei | 11169dd | 2012-12-18 14:30:41 +0000 | [diff] [blame] | 3675 | type = DBuilder.createPointerType(type, CGM.PointerWidthInBits); |
| 3676 | |
| 3677 | // Get overall information about the block. |
Leny Kholodov | 80c047d | 2016-09-06 10:48:04 +0000 | [diff] [blame] | 3678 | llvm::DINode::DIFlags flags = llvm::DINode::FlagArtificial; |
Duncan P. N. Exon Smith | 9dd4e4e | 2015-04-29 16:40:08 +0000 | [diff] [blame] | 3679 | auto *scope = cast<llvm::DILocalScope>(LexicalBlockStack.back()); |
Guy Benyei | 11169dd | 2012-12-18 14:30:41 +0000 | [diff] [blame] | 3680 | |
| 3681 | // Create the descriptor for the parameter. |
Duncan P. N. Exon Smith | e430654 | 2015-07-31 17:56:14 +0000 | [diff] [blame] | 3682 | auto *debugVar = DBuilder.createParameterVariable( |
| 3683 | scope, Arg->getName(), ArgNo, tunit, line, type, |
| 3684 | CGM.getLangOpts().Optimize, flags); |
Adrian Prantl | 51936dd | 2013-03-14 17:53:33 +0000 | [diff] [blame] | 3685 | |
Adrian Prantl | 616bef4 | 2013-03-14 21:52:59 +0000 | [diff] [blame] | 3686 | if (LocalAddr) { |
Adrian Prantl | 51936dd | 2013-03-14 17:53:33 +0000 | [diff] [blame] | 3687 | // Insert an llvm.dbg.value into the current block. |
Duncan P. N. Exon Smith | fe88b48 | 2015-04-15 21:18:30 +0000 | [diff] [blame] | 3688 | DBuilder.insertDbgValueIntrinsic( |
Eric Christopher | e7b87e5 | 2014-10-26 23:40:33 +0000 | [diff] [blame] | 3689 | LocalAddr, 0, debugVar, DBuilder.createExpression(), |
Duncan P. N. Exon Smith | fe88b48 | 2015-04-15 21:18:30 +0000 | [diff] [blame] | 3690 | llvm::DebugLoc::get(line, column, scope), Builder.GetInsertBlock()); |
Adrian Prantl | 616bef4 | 2013-03-14 21:52:59 +0000 | [diff] [blame] | 3691 | } |
Adrian Prantl | 51936dd | 2013-03-14 17:53:33 +0000 | [diff] [blame] | 3692 | |
Adrian Prantl | 616bef4 | 2013-03-14 21:52:59 +0000 | [diff] [blame] | 3693 | // Insert an llvm.dbg.declare into the current block. |
Duncan P. N. Exon Smith | fe88b48 | 2015-04-15 21:18:30 +0000 | [diff] [blame] | 3694 | DBuilder.insertDeclare(Arg, debugVar, DBuilder.createExpression(), |
| 3695 | llvm::DebugLoc::get(line, column, scope), |
| 3696 | Builder.GetInsertBlock()); |
Guy Benyei | 11169dd | 2012-12-18 14:30:41 +0000 | [diff] [blame] | 3697 | } |
| 3698 | |
Duncan P. N. Exon Smith | 9dd4e4e | 2015-04-29 16:40:08 +0000 | [diff] [blame] | 3699 | llvm::DIDerivedType * |
David Blaikie | 6943dea | 2013-08-20 01:28:15 +0000 | [diff] [blame] | 3700 | CGDebugInfo::getOrCreateStaticDataMemberDeclarationOrNull(const VarDecl *D) { |
| 3701 | if (!D->isStaticDataMember()) |
Duncan P. N. Exon Smith | c09c548 | 2015-04-20 21:17:26 +0000 | [diff] [blame] | 3702 | return nullptr; |
Saleem Abdulrasool | cd187f0 | 2015-02-28 00:13:13 +0000 | [diff] [blame] | 3703 | |
Duncan P. N. Exon Smith | fb49491 | 2014-12-09 18:39:32 +0000 | [diff] [blame] | 3704 | auto MI = StaticDataMemberCache.find(D->getCanonicalDecl()); |
David Blaikie | 6943dea | 2013-08-20 01:28:15 +0000 | [diff] [blame] | 3705 | if (MI != StaticDataMemberCache.end()) { |
| 3706 | assert(MI->second && "Static data member declaration should still exist"); |
Duncan P. N. Exon Smith | ac346ba | 2015-07-24 18:05:58 +0000 | [diff] [blame] | 3707 | return MI->second; |
Evgeniy Stepanov | 37b3f73 | 2013-08-16 10:35:31 +0000 | [diff] [blame] | 3708 | } |
David Blaikie | ce76304 | 2013-08-20 21:49:21 +0000 | [diff] [blame] | 3709 | |
| 3710 | // If the member wasn't found in the cache, lazily construct and add it to the |
| 3711 | // type (used when a limited form of the type is emitted). |
Adrian Prantl | 21361fb | 2014-08-29 22:44:27 +0000 | [diff] [blame] | 3712 | auto DC = D->getDeclContext(); |
Adrian Prantl | 6ec370a | 2015-09-10 18:39:45 +0000 | [diff] [blame] | 3713 | auto *Ctxt = cast<llvm::DICompositeType>(getDeclContextDescriptor(D)); |
Adrian Prantl | 21361fb | 2014-08-29 22:44:27 +0000 | [diff] [blame] | 3714 | return CreateRecordStaticField(D, Ctxt, cast<RecordDecl>(DC)); |
David Blaikie | 6943dea | 2013-08-20 01:28:15 +0000 | [diff] [blame] | 3715 | } |
| 3716 | |
Adrian Prantl | 5f4740d | 2016-12-20 02:10:02 +0000 | [diff] [blame] | 3717 | llvm::DIGlobalVariableExpression *CGDebugInfo::CollectAnonRecordDecls( |
Duncan P. N. Exon Smith | 9dd4e4e | 2015-04-29 16:40:08 +0000 | [diff] [blame] | 3718 | const RecordDecl *RD, llvm::DIFile *Unit, unsigned LineNo, |
| 3719 | StringRef LinkageName, llvm::GlobalVariable *Var, llvm::DIScope *DContext) { |
Adrian Prantl | 5f4740d | 2016-12-20 02:10:02 +0000 | [diff] [blame] | 3720 | llvm::DIGlobalVariableExpression *GVE = nullptr; |
Eric Christopher | cab9fae | 2014-04-10 05:20:00 +0000 | [diff] [blame] | 3721 | |
| 3722 | for (const auto *Field : RD->fields()) { |
Duncan P. N. Exon Smith | 9dd4e4e | 2015-04-29 16:40:08 +0000 | [diff] [blame] | 3723 | llvm::DIType *FieldTy = getOrCreateType(Field->getType(), Unit); |
Eric Christopher | cab9fae | 2014-04-10 05:20:00 +0000 | [diff] [blame] | 3724 | StringRef FieldName = Field->getName(); |
| 3725 | |
| 3726 | // Ignore unnamed fields, but recurse into anonymous records. |
| 3727 | if (FieldName.empty()) { |
David Majnemer | 58ed0f3 | 2016-07-17 00:39:12 +0000 | [diff] [blame] | 3728 | if (const auto *RT = dyn_cast<RecordType>(Field->getType())) |
Adrian Prantl | 5f4740d | 2016-12-20 02:10:02 +0000 | [diff] [blame] | 3729 | GVE = CollectAnonRecordDecls(RT->getDecl(), Unit, LineNo, LinkageName, |
Eric Christopher | cab9fae | 2014-04-10 05:20:00 +0000 | [diff] [blame] | 3730 | Var, DContext); |
| 3731 | continue; |
| 3732 | } |
| 3733 | // Use VarDecl's Tag, Scope and Line number. |
Adrian Prantl | 5f4740d | 2016-12-20 02:10:02 +0000 | [diff] [blame] | 3734 | GVE = DBuilder.createGlobalVariableExpression( |
| 3735 | DContext, FieldName, LinkageName, Unit, LineNo, FieldTy, |
| 3736 | Var->hasLocalLinkage()); |
| 3737 | Var->addDebugInfo(GVE); |
Eric Christopher | cab9fae | 2014-04-10 05:20:00 +0000 | [diff] [blame] | 3738 | } |
Adrian Prantl | 5f4740d | 2016-12-20 02:10:02 +0000 | [diff] [blame] | 3739 | return GVE; |
Eric Christopher | cab9fae | 2014-04-10 05:20:00 +0000 | [diff] [blame] | 3740 | } |
| 3741 | |
Yunzhong Gao | 0ebf1bb | 2013-08-30 08:53:09 +0000 | [diff] [blame] | 3742 | void CGDebugInfo::EmitGlobalVariable(llvm::GlobalVariable *Var, |
Guy Benyei | 11169dd | 2012-12-18 14:30:41 +0000 | [diff] [blame] | 3743 | const VarDecl *D) { |
Benjamin Kramer | 8c30592 | 2016-02-02 11:06:51 +0000 | [diff] [blame] | 3744 | assert(DebugKind >= codegenoptions::LimitedDebugInfo); |
Paul Robinson | b17327d | 2016-04-27 17:37:12 +0000 | [diff] [blame] | 3745 | if (D->hasAttr<NoDebugAttr>()) |
| 3746 | return; |
Adrian Prantl | 338ef7a | 2016-11-09 00:42:03 +0000 | [diff] [blame] | 3747 | |
| 3748 | // If we already created a DIGlobalVariable for this declaration, just attach |
| 3749 | // it to the llvm::GlobalVariable. |
| 3750 | auto Cached = DeclCache.find(D->getCanonicalDecl()); |
| 3751 | if (Cached != DeclCache.end()) |
Adrian Prantl | 5f4740d | 2016-12-20 02:10:02 +0000 | [diff] [blame] | 3752 | return Var->addDebugInfo( |
| 3753 | cast<llvm::DIGlobalVariableExpression>(Cached->second)); |
Adrian Prantl | 338ef7a | 2016-11-09 00:42:03 +0000 | [diff] [blame] | 3754 | |
Guy Benyei | 11169dd | 2012-12-18 14:30:41 +0000 | [diff] [blame] | 3755 | // Create global variable debug descriptor. |
Duncan P. N. Exon Smith | 9dd4e4e | 2015-04-29 16:40:08 +0000 | [diff] [blame] | 3756 | llvm::DIFile *Unit = nullptr; |
| 3757 | llvm::DIScope *DContext = nullptr; |
Frederic Riss | 9db79f1 | 2014-11-18 03:40:46 +0000 | [diff] [blame] | 3758 | unsigned LineNo; |
| 3759 | StringRef DeclName, LinkageName; |
| 3760 | QualType T; |
| 3761 | collectVarDeclProps(D, Unit, LineNo, T, DeclName, LinkageName, DContext); |
Eric Christopher | cab9fae | 2014-04-10 05:20:00 +0000 | [diff] [blame] | 3762 | |
| 3763 | // Attempt to store one global variable for the declaration - even if we |
| 3764 | // emit a lot of fields. |
Adrian Prantl | 5f4740d | 2016-12-20 02:10:02 +0000 | [diff] [blame] | 3765 | llvm::DIGlobalVariableExpression *GVE = nullptr; |
Eric Christopher | cab9fae | 2014-04-10 05:20:00 +0000 | [diff] [blame] | 3766 | |
| 3767 | // If this is an anonymous union then we'll want to emit a global |
| 3768 | // variable for each member of the anonymous union so that it's possible |
| 3769 | // to find the name of any field in the union. |
| 3770 | if (T->isUnionType() && DeclName.empty()) { |
Reid Kleckner | 43ecd7c | 2015-11-20 17:41:12 +0000 | [diff] [blame] | 3771 | const RecordDecl *RD = T->castAs<RecordType>()->getDecl(); |
Eric Christopher | e7b87e5 | 2014-10-26 23:40:33 +0000 | [diff] [blame] | 3772 | assert(RD->isAnonymousStructOrUnion() && |
| 3773 | "unnamed non-anonymous struct or union?"); |
Adrian Prantl | 5f4740d | 2016-12-20 02:10:02 +0000 | [diff] [blame] | 3774 | GVE = CollectAnonRecordDecls(RD, Unit, LineNo, LinkageName, Var, DContext); |
Eric Christopher | cab9fae | 2014-04-10 05:20:00 +0000 | [diff] [blame] | 3775 | } else { |
Victor Leschuk | a7ece03 | 2016-10-20 00:13:19 +0000 | [diff] [blame] | 3776 | auto Align = getDeclAlignIfRequired(D, CGM.getContext()); |
Adrian Prantl | 5f4740d | 2016-12-20 02:10:02 +0000 | [diff] [blame] | 3777 | GVE = DBuilder.createGlobalVariableExpression( |
Eric Christopher | cab9fae | 2014-04-10 05:20:00 +0000 | [diff] [blame] | 3778 | DContext, DeclName, LinkageName, Unit, LineNo, getOrCreateType(T, Unit), |
Peter Collingbourne | eeb56ab | 2016-09-13 01:13:19 +0000 | [diff] [blame] | 3779 | Var->hasLocalLinkage(), /*Expr=*/nullptr, |
Victor Leschuk | a7ece03 | 2016-10-20 00:13:19 +0000 | [diff] [blame] | 3780 | getOrCreateStaticDataMemberDeclarationOrNull(D), Align); |
Adrian Prantl | 5f4740d | 2016-12-20 02:10:02 +0000 | [diff] [blame] | 3781 | Var->addDebugInfo(GVE); |
Eric Christopher | cab9fae | 2014-04-10 05:20:00 +0000 | [diff] [blame] | 3782 | } |
Adrian Prantl | 5f4740d | 2016-12-20 02:10:02 +0000 | [diff] [blame] | 3783 | DeclCache[D->getCanonicalDecl()].reset(GVE); |
Guy Benyei | 11169dd | 2012-12-18 14:30:41 +0000 | [diff] [blame] | 3784 | } |
| 3785 | |
Peter Collingbourne | eeb56ab | 2016-09-13 01:13:19 +0000 | [diff] [blame] | 3786 | void CGDebugInfo::EmitGlobalVariable(const ValueDecl *VD, const APValue &Init) { |
Benjamin Kramer | 8c30592 | 2016-02-02 11:06:51 +0000 | [diff] [blame] | 3787 | assert(DebugKind >= codegenoptions::LimitedDebugInfo); |
Paul Robinson | b17327d | 2016-04-27 17:37:12 +0000 | [diff] [blame] | 3788 | if (VD->hasAttr<NoDebugAttr>()) |
| 3789 | return; |
Victor Leschuk | a7ece03 | 2016-10-20 00:13:19 +0000 | [diff] [blame] | 3790 | auto Align = getDeclAlignIfRequired(VD, CGM.getContext()); |
Yunzhong Gao | 0ebf1bb | 2013-08-30 08:53:09 +0000 | [diff] [blame] | 3791 | // Create the descriptor for the variable. |
Duncan P. N. Exon Smith | 9dd4e4e | 2015-04-29 16:40:08 +0000 | [diff] [blame] | 3792 | llvm::DIFile *Unit = getOrCreateFile(VD->getLocation()); |
Yunzhong Gao | 0ebf1bb | 2013-08-30 08:53:09 +0000 | [diff] [blame] | 3793 | StringRef Name = VD->getName(); |
Duncan P. N. Exon Smith | 9dd4e4e | 2015-04-29 16:40:08 +0000 | [diff] [blame] | 3794 | llvm::DIType *Ty = getOrCreateType(VD->getType(), Unit); |
David Majnemer | 58ed0f3 | 2016-07-17 00:39:12 +0000 | [diff] [blame] | 3795 | if (const auto *ECD = dyn_cast<EnumConstantDecl>(VD)) { |
| 3796 | const auto *ED = cast<EnumDecl>(ECD->getDeclContext()); |
Yunzhong Gao | 0ebf1bb | 2013-08-30 08:53:09 +0000 | [diff] [blame] | 3797 | assert(isa<EnumType>(ED->getTypeForDecl()) && "Enum without EnumType?"); |
| 3798 | Ty = getOrCreateType(QualType(ED->getTypeForDecl(), 0), Unit); |
| 3799 | } |
Duncan P. N. Exon Smith | dadc2b6 | 2015-04-21 18:43:54 +0000 | [diff] [blame] | 3800 | // Do not use global variables for enums. |
| 3801 | // |
| 3802 | // FIXME: why not? |
Duncan P. N. Exon Smith | 4caa7f2 | 2015-04-16 01:00:56 +0000 | [diff] [blame] | 3803 | if (Ty->getTag() == llvm::dwarf::DW_TAG_enumeration_type) |
Yunzhong Gao | 0ebf1bb | 2013-08-30 08:53:09 +0000 | [diff] [blame] | 3804 | return; |
David Blaikie | a1556556 | 2014-04-04 20:56:17 +0000 | [diff] [blame] | 3805 | // Do not emit separate definitions for function local const/statics. |
| 3806 | if (isa<FunctionDecl>(VD->getDeclContext())) |
| 3807 | return; |
David Blaikie | bb11391 | 2014-04-05 07:23:17 +0000 | [diff] [blame] | 3808 | VD = cast<ValueDecl>(VD->getCanonicalDecl()); |
David Blaikie | 423eb5a | 2014-11-19 19:42:40 +0000 | [diff] [blame] | 3809 | auto *VarD = cast<VarDecl>(VD); |
David Blaikie | af08085 | 2014-11-21 00:20:58 +0000 | [diff] [blame] | 3810 | if (VarD->isStaticDataMember()) { |
| 3811 | auto *RD = cast<RecordDecl>(VarD->getDeclContext()); |
Adrian Prantl | 6ec370a | 2015-09-10 18:39:45 +0000 | [diff] [blame] | 3812 | getDeclContextDescriptor(VarD); |
David Blaikie | 423eb5a | 2014-11-19 19:42:40 +0000 | [diff] [blame] | 3813 | // Ensure that the type is retained even though it's otherwise unreferenced. |
Duncan P. N. Exon Smith | 383f841 | 2016-04-23 21:08:27 +0000 | [diff] [blame] | 3814 | // |
| 3815 | // FIXME: This is probably unnecessary, since Ty should reference RD |
| 3816 | // through its scope. |
David Blaikie | 423eb5a | 2014-11-19 19:42:40 +0000 | [diff] [blame] | 3817 | RetainedTypes.push_back( |
David Blaikie | af08085 | 2014-11-21 00:20:58 +0000 | [diff] [blame] | 3818 | CGM.getContext().getRecordType(RD).getAsOpaquePtr()); |
David Blaikie | 423eb5a | 2014-11-19 19:42:40 +0000 | [diff] [blame] | 3819 | return; |
| 3820 | } |
| 3821 | |
Adrian Prantl | 6ec370a | 2015-09-10 18:39:45 +0000 | [diff] [blame] | 3822 | llvm::DIScope *DContext = getDeclContextDescriptor(VD); |
David Blaikie | af08085 | 2014-11-21 00:20:58 +0000 | [diff] [blame] | 3823 | |
Duncan P. N. Exon Smith | fb49491 | 2014-12-09 18:39:32 +0000 | [diff] [blame] | 3824 | auto &GV = DeclCache[VD]; |
| 3825 | if (GV) |
David Blaikie | bb11391 | 2014-04-05 07:23:17 +0000 | [diff] [blame] | 3826 | return; |
Peter Collingbourne | eeb56ab | 2016-09-13 01:13:19 +0000 | [diff] [blame] | 3827 | llvm::DIExpression *InitExpr = nullptr; |
Peter Collingbourne | b701363 | 2016-12-16 22:10:52 +0000 | [diff] [blame] | 3828 | if (CGM.getContext().getTypeSize(VD->getType()) <= 64) { |
| 3829 | // FIXME: Add a representation for integer constants wider than 64 bits. |
| 3830 | if (Init.isInt()) |
| 3831 | InitExpr = |
| 3832 | DBuilder.createConstantValueExpression(Init.getInt().getExtValue()); |
| 3833 | else if (Init.isFloat()) |
| 3834 | InitExpr = DBuilder.createConstantValueExpression( |
| 3835 | Init.getFloat().bitcastToAPInt().getZExtValue()); |
| 3836 | } |
Adrian Prantl | 5f4740d | 2016-12-20 02:10:02 +0000 | [diff] [blame] | 3837 | GV.reset(DBuilder.createGlobalVariableExpression( |
David Blaikie | 506a745 | 2014-04-05 07:46:57 +0000 | [diff] [blame] | 3838 | DContext, Name, StringRef(), Unit, getLineNumber(VD->getLocation()), Ty, |
Victor Leschuk | a7ece03 | 2016-10-20 00:13:19 +0000 | [diff] [blame] | 3839 | true, InitExpr, getOrCreateStaticDataMemberDeclarationOrNull(VarD), |
| 3840 | Align)); |
David Blaikie | bd48376 | 2013-05-20 04:58:53 +0000 | [diff] [blame] | 3841 | } |
| 3842 | |
Duncan P. N. Exon Smith | 9dd4e4e | 2015-04-29 16:40:08 +0000 | [diff] [blame] | 3843 | llvm::DIScope *CGDebugInfo::getCurrentContextDescriptor(const Decl *D) { |
David Blaikie | bd48376 | 2013-05-20 04:58:53 +0000 | [diff] [blame] | 3844 | if (!LexicalBlockStack.empty()) |
Duncan P. N. Exon Smith | fc8d9d9 | 2015-04-20 18:32:15 +0000 | [diff] [blame] | 3845 | return LexicalBlockStack.back(); |
Adrian Prantl | 5c8bd88 | 2015-09-11 17:23:08 +0000 | [diff] [blame] | 3846 | llvm::DIScope *Mod = getParentModuleOrNull(D); |
| 3847 | return getContextDescriptor(D, Mod ? Mod : TheCU); |
Guy Benyei | 11169dd | 2012-12-18 14:30:41 +0000 | [diff] [blame] | 3848 | } |
| 3849 | |
David Blaikie | 9f88fe8 | 2013-04-22 06:13:21 +0000 | [diff] [blame] | 3850 | void CGDebugInfo::EmitUsingDirective(const UsingDirectiveDecl &UD) { |
Benjamin Kramer | 8c30592 | 2016-02-02 11:06:51 +0000 | [diff] [blame] | 3851 | if (CGM.getCodeGenOpts().getDebugInfo() < codegenoptions::LimitedDebugInfo) |
David Blaikie | bd48376 | 2013-05-20 04:58:53 +0000 | [diff] [blame] | 3852 | return; |
Ekaterina Romanova | 9218a3b | 2015-12-10 18:52:50 +0000 | [diff] [blame] | 3853 | const NamespaceDecl *NSDecl = UD.getNominatedNamespace(); |
Adrian McCarthy | ab1e786 | 2016-07-21 18:43:20 +0000 | [diff] [blame] | 3854 | if (!NSDecl->isAnonymousNamespace() || |
| 3855 | CGM.getCodeGenOpts().DebugExplicitImport) { |
Ekaterina Romanova | 9218a3b | 2015-12-10 18:52:50 +0000 | [diff] [blame] | 3856 | DBuilder.createImportedModule( |
| 3857 | getCurrentContextDescriptor(cast<Decl>(UD.getDeclContext())), |
| 3858 | getOrCreateNameSpace(NSDecl), |
| 3859 | getLineNumber(UD.getLocation())); |
| 3860 | } |
David Blaikie | 9f88fe8 | 2013-04-22 06:13:21 +0000 | [diff] [blame] | 3861 | } |
| 3862 | |
David Blaikie | bd48376 | 2013-05-20 04:58:53 +0000 | [diff] [blame] | 3863 | void CGDebugInfo::EmitUsingDecl(const UsingDecl &UD) { |
Benjamin Kramer | 8c30592 | 2016-02-02 11:06:51 +0000 | [diff] [blame] | 3864 | if (CGM.getCodeGenOpts().getDebugInfo() < codegenoptions::LimitedDebugInfo) |
David Blaikie | bd48376 | 2013-05-20 04:58:53 +0000 | [diff] [blame] | 3865 | return; |
| 3866 | assert(UD.shadow_size() && |
| 3867 | "We shouldn't be codegening an invalid UsingDecl containing no decls"); |
| 3868 | // Emitting one decl is sufficient - debuggers can detect that this is an |
| 3869 | // overloaded name & provide lookup for all the overloads. |
| 3870 | const UsingShadowDecl &USD = **UD.shadow_begin(); |
David Blaikie | 2a58a18 | 2016-08-05 19:03:01 +0000 | [diff] [blame] | 3871 | |
| 3872 | // FIXME: Skip functions with undeduced auto return type for now since we |
| 3873 | // don't currently have the plumbing for separate declarations & definitions |
| 3874 | // of free functions and mismatched types (auto in the declaration, concrete |
| 3875 | // return type in the definition) |
| 3876 | if (const auto *FD = dyn_cast<FunctionDecl>(USD.getUnderlyingDecl())) |
| 3877 | if (const auto *AT = |
| 3878 | FD->getType()->getAs<FunctionProtoType>()->getContainedAutoType()) |
| 3879 | if (AT->getDeducedType().isNull()) |
| 3880 | return; |
Duncan P. N. Exon Smith | 9dd4e4e | 2015-04-29 16:40:08 +0000 | [diff] [blame] | 3881 | if (llvm::DINode *Target = |
Eric Christopher | 1ecc563 | 2013-06-07 22:54:39 +0000 | [diff] [blame] | 3882 | getDeclarationOrDefinition(USD.getUnderlyingDecl())) |
David Blaikie | bd48376 | 2013-05-20 04:58:53 +0000 | [diff] [blame] | 3883 | DBuilder.createImportedDeclaration( |
| 3884 | getCurrentContextDescriptor(cast<Decl>(USD.getDeclContext())), Target, |
| 3885 | getLineNumber(USD.getLocation())); |
| 3886 | } |
| 3887 | |
Adrian Prantl | c4bb47e | 2015-06-30 17:39:51 +0000 | [diff] [blame] | 3888 | void CGDebugInfo::EmitImportDecl(const ImportDecl &ID) { |
David Blaikie | af09f4a | 2016-05-03 23:06:40 +0000 | [diff] [blame] | 3889 | if (CGM.getCodeGenOpts().getDebuggerTuning() != llvm::DebuggerKind::LLDB) |
| 3890 | return; |
Adrian Prantl | 8a634c1 | 2015-12-18 19:44:31 +0000 | [diff] [blame] | 3891 | if (Module *M = ID.getImportedModule()) { |
Chad Rosier | e5dafd1 | 2015-12-18 20:08:40 +0000 | [diff] [blame] | 3892 | auto Info = ExternalASTSource::ASTSourceDescriptor(*M); |
Adrian Prantl | 8a634c1 | 2015-12-18 19:44:31 +0000 | [diff] [blame] | 3893 | DBuilder.createImportedDeclaration( |
| 3894 | getCurrentContextDescriptor(cast<Decl>(ID.getDeclContext())), |
| 3895 | getOrCreateModuleRef(Info, DebugTypeExtRefs), |
| 3896 | getLineNumber(ID.getLocation())); |
| 3897 | } |
Adrian Prantl | c4bb47e | 2015-06-30 17:39:51 +0000 | [diff] [blame] | 3898 | } |
| 3899 | |
Duncan P. N. Exon Smith | 9dd4e4e | 2015-04-29 16:40:08 +0000 | [diff] [blame] | 3900 | llvm::DIImportedEntity * |
David Blaikie | f121b93 | 2013-05-20 22:50:41 +0000 | [diff] [blame] | 3901 | CGDebugInfo::EmitNamespaceAlias(const NamespaceAliasDecl &NA) { |
Benjamin Kramer | 8c30592 | 2016-02-02 11:06:51 +0000 | [diff] [blame] | 3902 | if (CGM.getCodeGenOpts().getDebugInfo() < codegenoptions::LimitedDebugInfo) |
Duncan P. N. Exon Smith | dadc2b6 | 2015-04-21 18:43:54 +0000 | [diff] [blame] | 3903 | return nullptr; |
Duncan P. N. Exon Smith | fb49491 | 2014-12-09 18:39:32 +0000 | [diff] [blame] | 3904 | auto &VH = NamespaceAliasCache[&NA]; |
David Blaikie | f121b93 | 2013-05-20 22:50:41 +0000 | [diff] [blame] | 3905 | if (VH) |
Duncan P. N. Exon Smith | 9dd4e4e | 2015-04-29 16:40:08 +0000 | [diff] [blame] | 3906 | return cast<llvm::DIImportedEntity>(VH); |
| 3907 | llvm::DIImportedEntity *R; |
David Majnemer | 58ed0f3 | 2016-07-17 00:39:12 +0000 | [diff] [blame] | 3908 | if (const auto *Underlying = |
David Blaikie | f121b93 | 2013-05-20 22:50:41 +0000 | [diff] [blame] | 3909 | dyn_cast<NamespaceAliasDecl>(NA.getAliasedNamespace())) |
| 3910 | // This could cache & dedup here rather than relying on metadata deduping. |
David Blaikie | 551fb0a | 2014-04-06 06:30:03 +0000 | [diff] [blame] | 3911 | R = DBuilder.createImportedDeclaration( |
David Blaikie | f121b93 | 2013-05-20 22:50:41 +0000 | [diff] [blame] | 3912 | getCurrentContextDescriptor(cast<Decl>(NA.getDeclContext())), |
| 3913 | EmitNamespaceAlias(*Underlying), getLineNumber(NA.getLocation()), |
| 3914 | NA.getName()); |
| 3915 | else |
David Blaikie | 551fb0a | 2014-04-06 06:30:03 +0000 | [diff] [blame] | 3916 | R = DBuilder.createImportedDeclaration( |
David Blaikie | f121b93 | 2013-05-20 22:50:41 +0000 | [diff] [blame] | 3917 | getCurrentContextDescriptor(cast<Decl>(NA.getDeclContext())), |
| 3918 | getOrCreateNameSpace(cast<NamespaceDecl>(NA.getAliasedNamespace())), |
| 3919 | getLineNumber(NA.getLocation()), NA.getName()); |
Duncan P. N. Exon Smith | fb49491 | 2014-12-09 18:39:32 +0000 | [diff] [blame] | 3920 | VH.reset(R); |
David Blaikie | f121b93 | 2013-05-20 22:50:41 +0000 | [diff] [blame] | 3921 | return R; |
| 3922 | } |
| 3923 | |
Duncan P. N. Exon Smith | 9dd4e4e | 2015-04-29 16:40:08 +0000 | [diff] [blame] | 3924 | llvm::DINamespace * |
Guy Benyei | 11169dd | 2012-12-18 14:30:41 +0000 | [diff] [blame] | 3925 | CGDebugInfo::getOrCreateNameSpace(const NamespaceDecl *NSDecl) { |
David Blaikie | 9fdedec | 2013-08-16 22:52:07 +0000 | [diff] [blame] | 3926 | NSDecl = NSDecl->getCanonicalDecl(); |
Duncan P. N. Exon Smith | fb49491 | 2014-12-09 18:39:32 +0000 | [diff] [blame] | 3927 | auto I = NameSpaceCache.find(NSDecl); |
Guy Benyei | 11169dd | 2012-12-18 14:30:41 +0000 | [diff] [blame] | 3928 | if (I != NameSpaceCache.end()) |
Duncan P. N. Exon Smith | 9dd4e4e | 2015-04-29 16:40:08 +0000 | [diff] [blame] | 3929 | return cast<llvm::DINamespace>(I->second); |
Eric Christopher | b2a008c | 2013-05-16 00:45:12 +0000 | [diff] [blame] | 3930 | |
Guy Benyei | 11169dd | 2012-12-18 14:30:41 +0000 | [diff] [blame] | 3931 | unsigned LineNo = getLineNumber(NSDecl->getLocation()); |
Duncan P. N. Exon Smith | 9dd4e4e | 2015-04-29 16:40:08 +0000 | [diff] [blame] | 3932 | llvm::DIFile *FileD = getOrCreateFile(NSDecl->getLocation()); |
Adrian Prantl | 6ec370a | 2015-09-10 18:39:45 +0000 | [diff] [blame] | 3933 | llvm::DIScope *Context = getDeclContextDescriptor(NSDecl); |
Adrian Prantl | bd87eb4 | 2016-11-03 19:42:14 +0000 | [diff] [blame] | 3934 | llvm::DINamespace *NS = DBuilder.createNameSpace( |
| 3935 | Context, NSDecl->getName(), FileD, LineNo, NSDecl->isInline()); |
Duncan P. N. Exon Smith | fb49491 | 2014-12-09 18:39:32 +0000 | [diff] [blame] | 3936 | NameSpaceCache[NSDecl].reset(NS); |
Guy Benyei | 11169dd | 2012-12-18 14:30:41 +0000 | [diff] [blame] | 3937 | return NS; |
| 3938 | } |
| 3939 | |
Adrian Prantl | a5206ce | 2015-09-22 23:26:43 +0000 | [diff] [blame] | 3940 | void CGDebugInfo::setDwoId(uint64_t Signature) { |
| 3941 | assert(TheCU && "no main compile unit"); |
| 3942 | TheCU->setDWOId(Signature); |
| 3943 | } |
| 3944 | |
| 3945 | |
Guy Benyei | 11169dd | 2012-12-18 14:30:41 +0000 | [diff] [blame] | 3946 | void CGDebugInfo::finalize() { |
David Blaikie | 87dab87 | 2014-05-07 16:56:58 +0000 | [diff] [blame] | 3947 | // Creating types might create further types - invalidating the current |
| 3948 | // element and the size(), so don't cache/reference them. |
| 3949 | for (size_t i = 0; i != ObjCInterfaceCache.size(); ++i) { |
| 3950 | ObjCInterfaceCacheEntry E = ObjCInterfaceCache[i]; |
Duncan P. N. Exon Smith | 9dd4e4e | 2015-04-29 16:40:08 +0000 | [diff] [blame] | 3951 | llvm::DIType *Ty = E.Type->getDecl()->getDefinition() |
Duncan P. N. Exon Smith | 497d4d46 | 2015-04-11 19:05:04 +0000 | [diff] [blame] | 3952 | ? CreateTypeDefinition(E.Type, E.Unit) |
| 3953 | : E.Decl; |
Duncan P. N. Exon Smith | 9dd4e4e | 2015-04-29 16:40:08 +0000 | [diff] [blame] | 3954 | DBuilder.replaceTemporary(llvm::TempDIType(E.Decl), Ty); |
David Blaikie | 87dab87 | 2014-05-07 16:56:58 +0000 | [diff] [blame] | 3955 | } |
| 3956 | |
David Blaikie | f427b00 | 2014-05-06 03:42:01 +0000 | [diff] [blame] | 3957 | for (auto p : ReplaceMap) { |
| 3958 | assert(p.second); |
Duncan P. N. Exon Smith | 9dd4e4e | 2015-04-29 16:40:08 +0000 | [diff] [blame] | 3959 | auto *Ty = cast<llvm::DIType>(p.second); |
Duncan P. N. Exon Smith | 4caa7f2 | 2015-04-16 01:00:56 +0000 | [diff] [blame] | 3960 | assert(Ty->isForwardDecl()); |
Eric Christopher | b2a008c | 2013-05-16 00:45:12 +0000 | [diff] [blame] | 3961 | |
David Blaikie | f427b00 | 2014-05-06 03:42:01 +0000 | [diff] [blame] | 3962 | auto it = TypeCache.find(p.first); |
David Blaikie | b814904 | 2014-05-05 21:21:39 +0000 | [diff] [blame] | 3963 | assert(it != TypeCache.end()); |
| 3964 | assert(it->second); |
Adrian Prantl | 73409ce | 2013-03-11 18:33:46 +0000 | [diff] [blame] | 3965 | |
Duncan P. N. Exon Smith | 9dd4e4e | 2015-04-29 16:40:08 +0000 | [diff] [blame] | 3966 | DBuilder.replaceTemporary(llvm::TempDIType(Ty), |
| 3967 | cast<llvm::DIType>(it->second)); |
Guy Benyei | 11169dd | 2012-12-18 14:30:41 +0000 | [diff] [blame] | 3968 | } |
Adrian Prantl | 73409ce | 2013-03-11 18:33:46 +0000 | [diff] [blame] | 3969 | |
Frederic Riss | d253ed6 | 2014-11-18 03:40:51 +0000 | [diff] [blame] | 3970 | for (const auto &p : FwdDeclReplaceMap) { |
| 3971 | assert(p.second); |
Duncan P. N. Exon Smith | d899f6e | 2015-04-18 00:07:30 +0000 | [diff] [blame] | 3972 | llvm::TempMDNode FwdDecl(cast<llvm::MDNode>(p.second)); |
Duncan P. N. Exon Smith | fb49491 | 2014-12-09 18:39:32 +0000 | [diff] [blame] | 3973 | llvm::Metadata *Repl; |
Frederic Riss | d253ed6 | 2014-11-18 03:40:51 +0000 | [diff] [blame] | 3974 | |
| 3975 | auto it = DeclCache.find(p.first); |
Adrian Prantl | 97f7685 | 2014-12-19 01:02:11 +0000 | [diff] [blame] | 3976 | // If there has been no definition for the declaration, call RAUW |
Frederic Riss | d253ed6 | 2014-11-18 03:40:51 +0000 | [diff] [blame] | 3977 | // with ourselves, that will destroy the temporary MDNode and |
| 3978 | // replace it with a standard one, avoiding leaking memory. |
| 3979 | if (it == DeclCache.end()) |
Duncan P. N. Exon Smith | fb49491 | 2014-12-09 18:39:32 +0000 | [diff] [blame] | 3980 | Repl = p.second; |
Frederic Riss | d253ed6 | 2014-11-18 03:40:51 +0000 | [diff] [blame] | 3981 | else |
Duncan P. N. Exon Smith | fb49491 | 2014-12-09 18:39:32 +0000 | [diff] [blame] | 3982 | Repl = it->second; |
Frederic Riss | dce60a7 | 2014-11-19 18:53:46 +0000 | [diff] [blame] | 3983 | |
Adrian Prantl | 5f4740d | 2016-12-20 02:10:02 +0000 | [diff] [blame] | 3984 | if (auto *GVE = dyn_cast_or_null<llvm::DIGlobalVariableExpression>(Repl)) |
| 3985 | Repl = GVE->getVariable(); |
Duncan P. N. Exon Smith | d899f6e | 2015-04-18 00:07:30 +0000 | [diff] [blame] | 3986 | DBuilder.replaceTemporary(std::move(FwdDecl), cast<llvm::MDNode>(Repl)); |
Frederic Riss | d253ed6 | 2014-11-18 03:40:51 +0000 | [diff] [blame] | 3987 | } |
| 3988 | |
Adrian Prantl | 73409ce | 2013-03-11 18:33:46 +0000 | [diff] [blame] | 3989 | // We keep our own list of retained types, because we need to look |
| 3990 | // up the final type in the type cache. |
Adrian Prantl | 3a884fa | 2015-08-27 22:56:46 +0000 | [diff] [blame] | 3991 | for (auto &RT : RetainedTypes) |
Adrian Prantl | ad9a195e | 2015-08-27 21:21:19 +0000 | [diff] [blame] | 3992 | if (auto MD = TypeCache[RT]) |
| 3993 | DBuilder.retainType(cast<llvm::DIType>(MD)); |
Adrian Prantl | 73409ce | 2013-03-11 18:33:46 +0000 | [diff] [blame] | 3994 | |
Guy Benyei | 11169dd | 2012-12-18 14:30:41 +0000 | [diff] [blame] | 3995 | DBuilder.finalize(); |
| 3996 | } |
David Blaikie | 66088d5 | 2014-09-24 17:01:27 +0000 | [diff] [blame] | 3997 | |
| 3998 | void CGDebugInfo::EmitExplicitCastType(QualType Ty) { |
Benjamin Kramer | 8c30592 | 2016-02-02 11:06:51 +0000 | [diff] [blame] | 3999 | if (CGM.getCodeGenOpts().getDebugInfo() < codegenoptions::LimitedDebugInfo) |
David Blaikie | 66088d5 | 2014-09-24 17:01:27 +0000 | [diff] [blame] | 4000 | return; |
Duncan P. N. Exon Smith | 5043f91 | 2015-03-27 22:58:05 +0000 | [diff] [blame] | 4001 | |
Duncan P. N. Exon Smith | 0b6c369 | 2015-04-20 18:51:48 +0000 | [diff] [blame] | 4002 | if (auto *DieTy = getOrCreateType(Ty, getOrCreateMainFile())) |
Duncan P. N. Exon Smith | 5043f91 | 2015-03-27 22:58:05 +0000 | [diff] [blame] | 4003 | // Don't ignore in case of explicit cast where it is referenced indirectly. |
| 4004 | DBuilder.retainType(DieTy); |
David Blaikie | 66088d5 | 2014-09-24 17:01:27 +0000 | [diff] [blame] | 4005 | } |
Amara Emerson | 652795d | 2016-11-10 14:44:30 +0000 | [diff] [blame] | 4006 | |
| 4007 | llvm::DebugLoc CGDebugInfo::SourceLocToDebugLoc(SourceLocation Loc) { |
| 4008 | if (LexicalBlockStack.empty()) |
| 4009 | return llvm::DebugLoc(); |
| 4010 | |
| 4011 | llvm::MDNode *Scope = LexicalBlockStack.back(); |
| 4012 | return llvm::DebugLoc::get( |
| 4013 | getLineNumber(Loc), getColumnNumber(Loc), Scope); |
| 4014 | } |