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" |
John McCall | de0fe07 | 2017-08-15 21:42:52 +0000 | [diff] [blame] | 21 | #include "ConstantEmitter.h" |
Guy Benyei | 11169dd | 2012-12-18 14:30:41 +0000 | [diff] [blame] | 22 | #include "clang/AST/ASTContext.h" |
| 23 | #include "clang/AST/DeclFriend.h" |
| 24 | #include "clang/AST/DeclObjC.h" |
| 25 | #include "clang/AST/DeclTemplate.h" |
| 26 | #include "clang/AST/Expr.h" |
| 27 | #include "clang/AST/RecordLayout.h" |
| 28 | #include "clang/Basic/FileManager.h" |
| 29 | #include "clang/Basic/SourceManager.h" |
| 30 | #include "clang/Basic/Version.h" |
Saleem Abdulrasool | 10a4972 | 2016-04-08 16:52:00 +0000 | [diff] [blame] | 31 | #include "clang/Frontend/CodeGenOptions.h" |
Taewook Oh | 0fb5b78 | 2017-08-16 19:36:24 +0000 | [diff] [blame^] | 32 | #include "clang/Frontend/FrontendOptions.h" |
Adrian Prantl | c4bb47e | 2015-06-30 17:39:51 +0000 | [diff] [blame] | 33 | #include "clang/Lex/HeaderSearchOptions.h" |
Adrian Prantl | 9402cef | 2015-09-20 16:51:35 +0000 | [diff] [blame] | 34 | #include "clang/Lex/ModuleMap.h" |
Adrian Prantl | c4bb47e | 2015-06-30 17:39:51 +0000 | [diff] [blame] | 35 | #include "clang/Lex/PreprocessorOptions.h" |
Bob Haarman | dff3673 | 2016-10-25 22:19:32 +0000 | [diff] [blame] | 36 | #include "llvm/ADT/DenseSet.h" |
Guy Benyei | 11169dd | 2012-12-18 14:30:41 +0000 | [diff] [blame] | 37 | #include "llvm/ADT/SmallVector.h" |
| 38 | #include "llvm/ADT/StringExtras.h" |
Chandler Carruth | ffd5551 | 2013-01-02 11:45:17 +0000 | [diff] [blame] | 39 | #include "llvm/IR/Constants.h" |
| 40 | #include "llvm/IR/DataLayout.h" |
| 41 | #include "llvm/IR/DerivedTypes.h" |
| 42 | #include "llvm/IR/Instructions.h" |
| 43 | #include "llvm/IR/Intrinsics.h" |
| 44 | #include "llvm/IR/Module.h" |
Guy Benyei | 11169dd | 2012-12-18 14:30:41 +0000 | [diff] [blame] | 45 | #include "llvm/Support/FileSystem.h" |
Amjad Aboud | e2aab8c | 2016-12-25 10:12:27 +0000 | [diff] [blame] | 46 | #include "llvm/Support/MD5.h" |
Adrian Prantl | 0630eb7 | 2013-12-18 21:48:18 +0000 | [diff] [blame] | 47 | #include "llvm/Support/Path.h" |
Guy Benyei | 11169dd | 2012-12-18 14:30:41 +0000 | [diff] [blame] | 48 | using namespace clang; |
| 49 | using namespace clang::CodeGen; |
| 50 | |
Victor Leschuk | a7ece03 | 2016-10-20 00:13:19 +0000 | [diff] [blame] | 51 | static uint32_t getTypeAlignIfRequired(const Type *Ty, const ASTContext &Ctx) { |
| 52 | auto TI = Ctx.getTypeInfo(Ty); |
| 53 | return TI.AlignIsRequired ? TI.Align : 0; |
| 54 | } |
| 55 | |
| 56 | static uint32_t getTypeAlignIfRequired(QualType Ty, const ASTContext &Ctx) { |
| 57 | return getTypeAlignIfRequired(Ty.getTypePtr(), Ctx); |
| 58 | } |
| 59 | |
| 60 | static uint32_t getDeclAlignIfRequired(const Decl *D, const ASTContext &Ctx) { |
| 61 | return D->hasAttr<AlignedAttr>() ? D->getMaxAlignment() : 0; |
| 62 | } |
| 63 | |
Guy Benyei | 11169dd | 2012-12-18 14:30:41 +0000 | [diff] [blame] | 64 | CGDebugInfo::CGDebugInfo(CodeGenModule &CGM) |
Eric Christopher | 324bbbd | 2013-07-14 21:12:44 +0000 | [diff] [blame] | 65 | : CGM(CGM), DebugKind(CGM.getCodeGenOpts().getDebugInfo()), |
Adrian Prantl | 6b21ab2 | 2015-08-27 19:46:20 +0000 | [diff] [blame] | 66 | DebugTypeExtRefs(CGM.getCodeGenOpts().DebugTypeExtRefs), |
Eric Christopher | 324bbbd | 2013-07-14 21:12:44 +0000 | [diff] [blame] | 67 | DBuilder(CGM.getModule()) { |
Saleem Abdulrasool | 436256a | 2015-10-12 20:21:08 +0000 | [diff] [blame] | 68 | for (const auto &KV : CGM.getCodeGenOpts().DebugPrefixMap) |
| 69 | DebugPrefixMap[KV.first] = KV.second; |
Guy Benyei | 11169dd | 2012-12-18 14:30:41 +0000 | [diff] [blame] | 70 | CreateCompileUnit(); |
| 71 | } |
| 72 | |
| 73 | CGDebugInfo::~CGDebugInfo() { |
| 74 | assert(LexicalBlockStack.empty() && |
| 75 | "Region stack mismatch, stack not empty!"); |
| 76 | } |
| 77 | |
David Blaikie | 66e4197 | 2015-01-14 07:38:27 +0000 | [diff] [blame] | 78 | ApplyDebugLocation::ApplyDebugLocation(CodeGenFunction &CGF, |
David Blaikie | 835afb2 | 2015-01-21 23:08:17 +0000 | [diff] [blame] | 79 | SourceLocation TemporaryLocation) |
David Blaikie | d7057d9 | 2015-08-12 23:49:57 +0000 | [diff] [blame] | 80 | : CGF(&CGF) { |
David Blaikie | 9b47966 | 2015-01-25 01:19:10 +0000 | [diff] [blame] | 81 | init(TemporaryLocation); |
| 82 | } |
| 83 | |
Adrian Prantl | 39428e7 | 2015-02-03 18:40:42 +0000 | [diff] [blame] | 84 | ApplyDebugLocation::ApplyDebugLocation(CodeGenFunction &CGF, |
Adrian Prantl | 95b24e9 | 2015-02-03 20:00:54 +0000 | [diff] [blame] | 85 | bool DefaultToEmpty, |
Adrian Prantl | 39428e7 | 2015-02-03 18:40:42 +0000 | [diff] [blame] | 86 | SourceLocation TemporaryLocation) |
David Blaikie | d7057d9 | 2015-08-12 23:49:57 +0000 | [diff] [blame] | 87 | : CGF(&CGF) { |
Adrian Prantl | 95b24e9 | 2015-02-03 20:00:54 +0000 | [diff] [blame] | 88 | init(TemporaryLocation, DefaultToEmpty); |
Adrian Prantl | 39428e7 | 2015-02-03 18:40:42 +0000 | [diff] [blame] | 89 | } |
| 90 | |
| 91 | void ApplyDebugLocation::init(SourceLocation TemporaryLocation, |
Adrian Prantl | 95b24e9 | 2015-02-03 20:00:54 +0000 | [diff] [blame] | 92 | bool DefaultToEmpty) { |
David Blaikie | d7057d9 | 2015-08-12 23:49:57 +0000 | [diff] [blame] | 93 | auto *DI = CGF->getDebugInfo(); |
| 94 | if (!DI) { |
| 95 | CGF = nullptr; |
| 96 | return; |
David Blaikie | 66e4197 | 2015-01-14 07:38:27 +0000 | [diff] [blame] | 97 | } |
David Blaikie | d7057d9 | 2015-08-12 23:49:57 +0000 | [diff] [blame] | 98 | |
| 99 | OriginalLocation = CGF->Builder.getCurrentDebugLocation(); |
| 100 | if (TemporaryLocation.isValid()) { |
| 101 | DI->EmitLocation(CGF->Builder, TemporaryLocation); |
| 102 | return; |
| 103 | } |
| 104 | |
| 105 | if (DefaultToEmpty) { |
| 106 | CGF->Builder.SetCurrentDebugLocation(llvm::DebugLoc()); |
| 107 | return; |
| 108 | } |
| 109 | |
| 110 | // Construct a location that has a valid scope, but no line info. |
| 111 | assert(!DI->LexicalBlockStack.empty()); |
Adrian Prantl | b7acfc0 | 2017-02-27 21:30:05 +0000 | [diff] [blame] | 112 | CGF->Builder.SetCurrentDebugLocation(llvm::DebugLoc::get( |
| 113 | 0, 0, DI->LexicalBlockStack.back(), DI->getInlinedAt())); |
Adrian Prantl | 2e0637f | 2013-07-18 00:28:02 +0000 | [diff] [blame] | 114 | } |
| 115 | |
David Blaikie | 9b47966 | 2015-01-25 01:19:10 +0000 | [diff] [blame] | 116 | ApplyDebugLocation::ApplyDebugLocation(CodeGenFunction &CGF, const Expr *E) |
David Blaikie | d7057d9 | 2015-08-12 23:49:57 +0000 | [diff] [blame] | 117 | : CGF(&CGF) { |
David Blaikie | 9b47966 | 2015-01-25 01:19:10 +0000 | [diff] [blame] | 118 | init(E->getExprLoc()); |
| 119 | } |
| 120 | |
David Blaikie | 66e4197 | 2015-01-14 07:38:27 +0000 | [diff] [blame] | 121 | ApplyDebugLocation::ApplyDebugLocation(CodeGenFunction &CGF, llvm::DebugLoc Loc) |
David Blaikie | d7057d9 | 2015-08-12 23:49:57 +0000 | [diff] [blame] | 122 | : CGF(&CGF) { |
| 123 | if (!CGF.getDebugInfo()) { |
| 124 | this->CGF = nullptr; |
| 125 | return; |
David Blaikie | 66e4197 | 2015-01-14 07:38:27 +0000 | [diff] [blame] | 126 | } |
David Blaikie | d7057d9 | 2015-08-12 23:49:57 +0000 | [diff] [blame] | 127 | OriginalLocation = CGF.Builder.getCurrentDebugLocation(); |
| 128 | if (Loc) |
| 129 | CGF.Builder.SetCurrentDebugLocation(std::move(Loc)); |
David Blaikie | 66e4197 | 2015-01-14 07:38:27 +0000 | [diff] [blame] | 130 | } |
| 131 | |
| 132 | ApplyDebugLocation::~ApplyDebugLocation() { |
| 133 | // Query CGF so the location isn't overwritten when location updates are |
| 134 | // temporarily disabled (for C++ default function arguments) |
David Blaikie | d7057d9 | 2015-08-12 23:49:57 +0000 | [diff] [blame] | 135 | if (CGF) |
| 136 | CGF->Builder.SetCurrentDebugLocation(std::move(OriginalLocation)); |
David Blaikie | 66e4197 | 2015-01-14 07:38:27 +0000 | [diff] [blame] | 137 | } |
| 138 | |
Adrian Prantl | b7acfc0 | 2017-02-27 21:30:05 +0000 | [diff] [blame] | 139 | ApplyInlineDebugLocation::ApplyInlineDebugLocation(CodeGenFunction &CGF, |
| 140 | GlobalDecl InlinedFn) |
| 141 | : CGF(&CGF) { |
| 142 | if (!CGF.getDebugInfo()) { |
| 143 | this->CGF = nullptr; |
| 144 | return; |
| 145 | } |
| 146 | auto &DI = *CGF.getDebugInfo(); |
| 147 | SavedLocation = DI.getLocation(); |
| 148 | assert((DI.getInlinedAt() == |
| 149 | CGF.Builder.getCurrentDebugLocation()->getInlinedAt()) && |
| 150 | "CGDebugInfo and IRBuilder are out of sync"); |
| 151 | |
| 152 | DI.EmitInlineFunctionStart(CGF.Builder, InlinedFn); |
| 153 | } |
| 154 | |
| 155 | ApplyInlineDebugLocation::~ApplyInlineDebugLocation() { |
| 156 | if (!CGF) |
| 157 | return; |
| 158 | auto &DI = *CGF->getDebugInfo(); |
| 159 | DI.EmitInlineFunctionEnd(CGF->Builder); |
| 160 | DI.EmitLocation(CGF->Builder, SavedLocation); |
| 161 | } |
| 162 | |
Guy Benyei | 11169dd | 2012-12-18 14:30:41 +0000 | [diff] [blame] | 163 | void CGDebugInfo::setLocation(SourceLocation Loc) { |
| 164 | // If the new location isn't valid return. |
Eric Christopher | e7b87e5 | 2014-10-26 23:40:33 +0000 | [diff] [blame] | 165 | if (Loc.isInvalid()) |
| 166 | return; |
Guy Benyei | 11169dd | 2012-12-18 14:30:41 +0000 | [diff] [blame] | 167 | |
| 168 | CurLoc = CGM.getContext().getSourceManager().getExpansionLoc(Loc); |
| 169 | |
| 170 | // If we've changed files in the middle of a lexical scope go ahead |
| 171 | // and create a new lexical scope with file node if it's different |
| 172 | // from the one in the scope. |
Eric Christopher | e7b87e5 | 2014-10-26 23:40:33 +0000 | [diff] [blame] | 173 | if (LexicalBlockStack.empty()) |
| 174 | return; |
Guy Benyei | 11169dd | 2012-12-18 14:30:41 +0000 | [diff] [blame] | 175 | |
| 176 | SourceManager &SM = CGM.getContext().getSourceManager(); |
Duncan P. N. Exon Smith | 9dd4e4e | 2015-04-29 16:40:08 +0000 | [diff] [blame] | 177 | auto *Scope = cast<llvm::DIScope>(LexicalBlockStack.back()); |
Guy Benyei | 11169dd | 2012-12-18 14:30:41 +0000 | [diff] [blame] | 178 | PresumedLoc PCLoc = SM.getPresumedLoc(CurLoc); |
Guy Benyei | 11169dd | 2012-12-18 14:30:41 +0000 | [diff] [blame] | 179 | |
Duncan P. N. Exon Smith | 373ee85 | 2015-04-16 01:36:36 +0000 | [diff] [blame] | 180 | if (PCLoc.isInvalid() || Scope->getFilename() == PCLoc.getFilename()) |
Guy Benyei | 11169dd | 2012-12-18 14:30:41 +0000 | [diff] [blame] | 181 | return; |
| 182 | |
Duncan P. N. Exon Smith | 9dd4e4e | 2015-04-29 16:40:08 +0000 | [diff] [blame] | 183 | if (auto *LBF = dyn_cast<llvm::DILexicalBlockFile>(Scope)) { |
Guy Benyei | 11169dd | 2012-12-18 14:30:41 +0000 | [diff] [blame] | 184 | LexicalBlockStack.pop_back(); |
Duncan P. N. Exon Smith | d899f6e | 2015-04-18 00:07:30 +0000 | [diff] [blame] | 185 | LexicalBlockStack.emplace_back(DBuilder.createLexicalBlockFile( |
| 186 | LBF->getScope(), getOrCreateFile(CurLoc))); |
Duncan P. N. Exon Smith | 9dd4e4e | 2015-04-29 16:40:08 +0000 | [diff] [blame] | 187 | } else if (isa<llvm::DILexicalBlock>(Scope) || |
| 188 | isa<llvm::DISubprogram>(Scope)) { |
Guy Benyei | 11169dd | 2012-12-18 14:30:41 +0000 | [diff] [blame] | 189 | LexicalBlockStack.pop_back(); |
Duncan P. N. Exon Smith | d899f6e | 2015-04-18 00:07:30 +0000 | [diff] [blame] | 190 | LexicalBlockStack.emplace_back( |
| 191 | DBuilder.createLexicalBlockFile(Scope, getOrCreateFile(CurLoc))); |
Guy Benyei | 11169dd | 2012-12-18 14:30:41 +0000 | [diff] [blame] | 192 | } |
| 193 | } |
| 194 | |
Adrian Prantl | 6ec370a | 2015-09-10 18:39:45 +0000 | [diff] [blame] | 195 | llvm::DIScope *CGDebugInfo::getDeclContextDescriptor(const Decl *D) { |
Adrian Prantl | 5c8bd88 | 2015-09-11 17:23:08 +0000 | [diff] [blame] | 196 | llvm::DIScope *Mod = getParentModuleOrNull(D); |
| 197 | return getContextDescriptor(cast<Decl>(D->getDeclContext()), |
| 198 | Mod ? Mod : TheCU); |
Adrian Prantl | 6ec370a | 2015-09-10 18:39:45 +0000 | [diff] [blame] | 199 | } |
| 200 | |
| 201 | llvm::DIScope *CGDebugInfo::getContextDescriptor(const Decl *Context, |
| 202 | llvm::DIScope *Default) { |
Guy Benyei | 11169dd | 2012-12-18 14:30:41 +0000 | [diff] [blame] | 203 | if (!Context) |
Adrian Prantl | 6ec370a | 2015-09-10 18:39:45 +0000 | [diff] [blame] | 204 | return Default; |
Guy Benyei | 11169dd | 2012-12-18 14:30:41 +0000 | [diff] [blame] | 205 | |
Duncan P. N. Exon Smith | fb49491 | 2014-12-09 18:39:32 +0000 | [diff] [blame] | 206 | auto I = RegionMap.find(Context); |
Guy Benyei | 11169dd | 2012-12-18 14:30:41 +0000 | [diff] [blame] | 207 | if (I != RegionMap.end()) { |
Duncan P. N. Exon Smith | fb49491 | 2014-12-09 18:39:32 +0000 | [diff] [blame] | 208 | llvm::Metadata *V = I->second; |
Duncan P. N. Exon Smith | 9dd4e4e | 2015-04-29 16:40:08 +0000 | [diff] [blame] | 209 | return dyn_cast_or_null<llvm::DIScope>(V); |
Guy Benyei | 11169dd | 2012-12-18 14:30:41 +0000 | [diff] [blame] | 210 | } |
| 211 | |
| 212 | // Check namespace. |
Adrian Prantl | ddb8e06 | 2017-05-12 16:23:53 +0000 | [diff] [blame] | 213 | if (const auto *NSDecl = dyn_cast<NamespaceDecl>(Context)) |
| 214 | return getOrCreateNamespace(NSDecl); |
Guy Benyei | 11169dd | 2012-12-18 14:30:41 +0000 | [diff] [blame] | 215 | |
David Majnemer | 58ed0f3 | 2016-07-17 00:39:12 +0000 | [diff] [blame] | 216 | if (const auto *RDecl = dyn_cast<RecordDecl>(Context)) |
David Blaikie | bfa5274 | 2013-04-19 06:56:38 +0000 | [diff] [blame] | 217 | if (!RDecl->isDependentType()) |
| 218 | return getOrCreateType(CGM.getContext().getTypeDeclType(RDecl), |
Eric Christopher | e7b87e5 | 2014-10-26 23:40:33 +0000 | [diff] [blame] | 219 | getOrCreateMainFile()); |
Adrian Prantl | 6ec370a | 2015-09-10 18:39:45 +0000 | [diff] [blame] | 220 | return Default; |
Guy Benyei | 11169dd | 2012-12-18 14:30:41 +0000 | [diff] [blame] | 221 | } |
| 222 | |
Reid Kleckner | 59d1220 | 2017-08-08 01:33:53 +0000 | [diff] [blame] | 223 | PrintingPolicy CGDebugInfo::getPrintingPolicy() const { |
| 224 | PrintingPolicy PP = CGM.getContext().getPrintingPolicy(); |
| 225 | |
| 226 | // If we're emitting codeview, it's important to try to match MSVC's naming so |
| 227 | // that visualizers written for MSVC will trigger for our class names. In |
| 228 | // particular, we can't have spaces between arguments of standard templates |
| 229 | // like basic_string and vector. |
| 230 | if (CGM.getCodeGenOpts().EmitCodeView) |
| 231 | PP.MSVCFormatting = true; |
| 232 | |
| 233 | return PP; |
| 234 | } |
| 235 | |
Guy Benyei | 11169dd | 2012-12-18 14:30:41 +0000 | [diff] [blame] | 236 | StringRef CGDebugInfo::getFunctionName(const FunctionDecl *FD) { |
Eric Christopher | e7b87e5 | 2014-10-26 23:40:33 +0000 | [diff] [blame] | 237 | assert(FD && "Invalid FunctionDecl!"); |
Guy Benyei | 11169dd | 2012-12-18 14:30:41 +0000 | [diff] [blame] | 238 | IdentifierInfo *FII = FD->getIdentifier(); |
Eric Christopher | e7b87e5 | 2014-10-26 23:40:33 +0000 | [diff] [blame] | 239 | FunctionTemplateSpecializationInfo *Info = |
| 240 | FD->getTemplateSpecializationInfo(); |
Reid Kleckner | 6010338 | 2015-12-16 02:04:40 +0000 | [diff] [blame] | 241 | |
Reid Kleckner | 31abd80 | 2016-06-30 17:41:31 +0000 | [diff] [blame] | 242 | // Emit the unqualified name in normal operation. LLVM and the debugger can |
| 243 | // compute the fully qualified name from the scope chain. If we're only |
| 244 | // emitting line table info, there won't be any scope chains, so emit the |
| 245 | // fully qualified name here so that stack traces are more accurate. |
| 246 | // FIXME: Do this when emitting DWARF as well as when emitting CodeView after |
| 247 | // evaluating the size impact. |
| 248 | bool UseQualifiedName = DebugKind == codegenoptions::DebugLineTablesOnly && |
| 249 | CGM.getCodeGenOpts().EmitCodeView; |
| 250 | |
| 251 | if (!Info && FII && !UseQualifiedName) |
Guy Benyei | 11169dd | 2012-12-18 14:30:41 +0000 | [diff] [blame] | 252 | return FII->getName(); |
| 253 | |
Benjamin Kramer | 9170e91 | 2013-02-22 15:46:01 +0000 | [diff] [blame] | 254 | SmallString<128> NS; |
| 255 | llvm::raw_svector_ostream OS(NS); |
Reid Kleckner | 31abd80 | 2016-06-30 17:41:31 +0000 | [diff] [blame] | 256 | if (!UseQualifiedName) |
| 257 | FD->printName(OS); |
| 258 | else |
Reid Kleckner | 59d1220 | 2017-08-08 01:33:53 +0000 | [diff] [blame] | 259 | FD->printQualifiedName(OS, getPrintingPolicy()); |
Guy Benyei | 11169dd | 2012-12-18 14:30:41 +0000 | [diff] [blame] | 260 | |
Reid Kleckner | 829398e | 2016-06-17 16:11:20 +0000 | [diff] [blame] | 261 | // Add any template specialization args. |
| 262 | if (Info) { |
| 263 | const TemplateArgumentList *TArgs = Info->TemplateArguments; |
David Majnemer | 6fbeee3 | 2016-07-07 04:43:07 +0000 | [diff] [blame] | 264 | TemplateSpecializationType::PrintTemplateArgumentList(OS, TArgs->asArray(), |
Reid Kleckner | 59d1220 | 2017-08-08 01:33:53 +0000 | [diff] [blame] | 265 | getPrintingPolicy()); |
Guy Benyei | 11169dd | 2012-12-18 14:30:41 +0000 | [diff] [blame] | 266 | } |
| 267 | |
| 268 | // Copy this name on the side and use its reference. |
Benjamin Kramer | 1b18a5e | 2013-09-09 16:39:06 +0000 | [diff] [blame] | 269 | return internString(OS.str()); |
Guy Benyei | 11169dd | 2012-12-18 14:30:41 +0000 | [diff] [blame] | 270 | } |
| 271 | |
| 272 | StringRef CGDebugInfo::getObjCMethodName(const ObjCMethodDecl *OMD) { |
| 273 | SmallString<256> MethodName; |
| 274 | llvm::raw_svector_ostream OS(MethodName); |
| 275 | OS << (OMD->isInstanceMethod() ? '-' : '+') << '['; |
| 276 | const DeclContext *DC = OMD->getDeclContext(); |
David Majnemer | 58ed0f3 | 2016-07-17 00:39:12 +0000 | [diff] [blame] | 277 | if (const auto *OID = dyn_cast<ObjCImplementationDecl>(DC)) { |
Eric Christopher | e7b87e5 | 2014-10-26 23:40:33 +0000 | [diff] [blame] | 278 | OS << OID->getName(); |
David Majnemer | 58ed0f3 | 2016-07-17 00:39:12 +0000 | [diff] [blame] | 279 | } else if (const auto *OID = dyn_cast<ObjCInterfaceDecl>(DC)) { |
Eric Christopher | e7b87e5 | 2014-10-26 23:40:33 +0000 | [diff] [blame] | 280 | OS << OID->getName(); |
David Majnemer | 58ed0f3 | 2016-07-17 00:39:12 +0000 | [diff] [blame] | 281 | } else if (const auto *OC = dyn_cast<ObjCCategoryDecl>(DC)) { |
Douglas Gregor | acf4fd3 | 2015-11-03 01:15:46 +0000 | [diff] [blame] | 282 | if (OC->IsClassExtension()) { |
| 283 | OS << OC->getClassInterface()->getName(); |
| 284 | } else { |
David Majnemer | 58ed0f3 | 2016-07-17 00:39:12 +0000 | [diff] [blame] | 285 | OS << OC->getIdentifier()->getNameStart() << '(' |
Douglas Gregor | acf4fd3 | 2015-11-03 01:15:46 +0000 | [diff] [blame] | 286 | << OC->getIdentifier()->getNameStart() << ')'; |
| 287 | } |
David Majnemer | 58ed0f3 | 2016-07-17 00:39:12 +0000 | [diff] [blame] | 288 | } else if (const auto *OCD = dyn_cast<ObjCCategoryImplDecl>(DC)) { |
Argyrios Kyrtzidis | a166a2b | 2017-03-07 09:26:07 +0000 | [diff] [blame] | 289 | OS << OCD->getClassInterface()->getName() << '(' |
| 290 | << OCD->getName() << ')'; |
Adrian Prantl | b39fc14 | 2013-05-17 23:58:45 +0000 | [diff] [blame] | 291 | } else if (isa<ObjCProtocolDecl>(DC)) { |
Adrian Prantl | 6e785ec | 2013-05-17 23:49:10 +0000 | [diff] [blame] | 292 | // We can extract the type of the class from the self pointer. |
Eric Christopher | e7b87e5 | 2014-10-26 23:40:33 +0000 | [diff] [blame] | 293 | if (ImplicitParamDecl *SelfDecl = OMD->getSelfDecl()) { |
Adrian Prantl | 6e785ec | 2013-05-17 23:49:10 +0000 | [diff] [blame] | 294 | QualType ClassTy = |
Eric Christopher | e7b87e5 | 2014-10-26 23:40:33 +0000 | [diff] [blame] | 295 | cast<ObjCObjectPointerType>(SelfDecl->getType())->getPointeeType(); |
Adrian Prantl | 6e785ec | 2013-05-17 23:49:10 +0000 | [diff] [blame] | 296 | ClassTy.print(OS, PrintingPolicy(LangOptions())); |
| 297 | } |
Guy Benyei | 11169dd | 2012-12-18 14:30:41 +0000 | [diff] [blame] | 298 | } |
| 299 | OS << ' ' << OMD->getSelector().getAsString() << ']'; |
| 300 | |
Benjamin Kramer | 1b18a5e | 2013-09-09 16:39:06 +0000 | [diff] [blame] | 301 | return internString(OS.str()); |
Guy Benyei | 11169dd | 2012-12-18 14:30:41 +0000 | [diff] [blame] | 302 | } |
| 303 | |
Guy Benyei | 11169dd | 2012-12-18 14:30:41 +0000 | [diff] [blame] | 304 | StringRef CGDebugInfo::getSelectorName(Selector S) { |
Benjamin Kramer | 1b18a5e | 2013-09-09 16:39:06 +0000 | [diff] [blame] | 305 | return internString(S.getAsString()); |
Guy Benyei | 11169dd | 2012-12-18 14:30:41 +0000 | [diff] [blame] | 306 | } |
| 307 | |
Eric Christopher | e7b87e5 | 2014-10-26 23:40:33 +0000 | [diff] [blame] | 308 | StringRef CGDebugInfo::getClassName(const RecordDecl *RD) { |
David Majnemer | bc5976a | 2016-07-01 23:12:54 +0000 | [diff] [blame] | 309 | if (isa<ClassTemplateSpecializationDecl>(RD)) { |
| 310 | SmallString<128> Name; |
David Blaikie | 65813a3 | 2014-04-02 18:21:09 +0000 | [diff] [blame] | 311 | llvm::raw_svector_ostream OS(Name); |
Reid Kleckner | 59d1220 | 2017-08-08 01:33:53 +0000 | [diff] [blame] | 312 | RD->getNameForDiagnostic(OS, getPrintingPolicy(), |
David Blaikie | 65813a3 | 2014-04-02 18:21:09 +0000 | [diff] [blame] | 313 | /*Qualified*/ false); |
David Majnemer | bc5976a | 2016-07-01 23:12:54 +0000 | [diff] [blame] | 314 | |
| 315 | // Copy this name on the side and use its reference. |
| 316 | return internString(Name); |
Benjamin Kramer | 9170e91 | 2013-02-22 15:46:01 +0000 | [diff] [blame] | 317 | } |
Guy Benyei | 11169dd | 2012-12-18 14:30:41 +0000 | [diff] [blame] | 318 | |
David Majnemer | bc5976a | 2016-07-01 23:12:54 +0000 | [diff] [blame] | 319 | // quick optimization to avoid having to intern strings that are already |
| 320 | // stored reliably elsewhere |
| 321 | if (const IdentifierInfo *II = RD->getIdentifier()) |
| 322 | return II->getName(); |
| 323 | |
| 324 | // The CodeView printer in LLVM wants to see the names of unnamed types: it is |
| 325 | // used to reconstruct the fully qualified type names. |
| 326 | if (CGM.getCodeGenOpts().EmitCodeView) { |
| 327 | if (const TypedefNameDecl *D = RD->getTypedefNameForAnonDecl()) { |
| 328 | assert(RD->getDeclContext() == D->getDeclContext() && |
| 329 | "Typedef should not be in another decl context!"); |
| 330 | assert(D->getDeclName().getAsIdentifierInfo() && |
| 331 | "Typedef was not named!"); |
| 332 | return D->getDeclName().getAsIdentifierInfo()->getName(); |
| 333 | } |
| 334 | |
| 335 | if (CGM.getLangOpts().CPlusPlus) { |
| 336 | StringRef Name; |
| 337 | |
| 338 | ASTContext &Context = CGM.getContext(); |
| 339 | if (const DeclaratorDecl *DD = Context.getDeclaratorForUnnamedTagDecl(RD)) |
| 340 | // Anonymous types without a name for linkage purposes have their |
| 341 | // declarator mangled in if they have one. |
| 342 | Name = DD->getName(); |
| 343 | else if (const TypedefNameDecl *TND = |
| 344 | Context.getTypedefNameForUnnamedTagDecl(RD)) |
| 345 | // Anonymous types without a name for linkage purposes have their |
| 346 | // associate typedef mangled in if they have one. |
| 347 | Name = TND->getName(); |
| 348 | |
| 349 | if (!Name.empty()) { |
| 350 | SmallString<256> UnnamedType("<unnamed-type-"); |
| 351 | UnnamedType += Name; |
| 352 | UnnamedType += '>'; |
| 353 | return internString(UnnamedType); |
| 354 | } |
| 355 | } |
| 356 | } |
| 357 | |
| 358 | return StringRef(); |
Guy Benyei | 11169dd | 2012-12-18 14:30:41 +0000 | [diff] [blame] | 359 | } |
| 360 | |
Amjad Aboud | e2aab8c | 2016-12-25 10:12:27 +0000 | [diff] [blame] | 361 | llvm::DIFile::ChecksumKind |
| 362 | CGDebugInfo::computeChecksum(FileID FID, SmallString<32> &Checksum) const { |
| 363 | Checksum.clear(); |
| 364 | |
| 365 | if (!CGM.getCodeGenOpts().EmitCodeView) |
| 366 | return llvm::DIFile::CSK_None; |
| 367 | |
| 368 | SourceManager &SM = CGM.getContext().getSourceManager(); |
| 369 | bool Invalid; |
| 370 | llvm::MemoryBuffer *MemBuffer = SM.getBuffer(FID, &Invalid); |
| 371 | if (Invalid) |
| 372 | return llvm::DIFile::CSK_None; |
| 373 | |
| 374 | llvm::MD5 Hash; |
| 375 | llvm::MD5::MD5Result Result; |
| 376 | |
| 377 | Hash.update(MemBuffer->getBuffer()); |
| 378 | Hash.final(Result); |
| 379 | |
| 380 | Hash.stringifyResult(Result, Checksum); |
| 381 | return llvm::DIFile::CSK_MD5; |
| 382 | } |
| 383 | |
Duncan P. N. Exon Smith | 9dd4e4e | 2015-04-29 16:40:08 +0000 | [diff] [blame] | 384 | llvm::DIFile *CGDebugInfo::getOrCreateFile(SourceLocation Loc) { |
Guy Benyei | 11169dd | 2012-12-18 14:30:41 +0000 | [diff] [blame] | 385 | if (!Loc.isValid()) |
| 386 | // If Location is not valid then use main input file. |
Saleem Abdulrasool | 436256a | 2015-10-12 20:21:08 +0000 | [diff] [blame] | 387 | return DBuilder.createFile(remapDIPath(TheCU->getFilename()), |
Amjad Aboud | e2aab8c | 2016-12-25 10:12:27 +0000 | [diff] [blame] | 388 | remapDIPath(TheCU->getDirectory()), |
| 389 | TheCU->getFile()->getChecksumKind(), |
| 390 | TheCU->getFile()->getChecksum()); |
Guy Benyei | 11169dd | 2012-12-18 14:30:41 +0000 | [diff] [blame] | 391 | |
| 392 | SourceManager &SM = CGM.getContext().getSourceManager(); |
| 393 | PresumedLoc PLoc = SM.getPresumedLoc(Loc); |
| 394 | |
| 395 | if (PLoc.isInvalid() || StringRef(PLoc.getFilename()).empty()) |
| 396 | // If the location is not valid then use main input file. |
Saleem Abdulrasool | 436256a | 2015-10-12 20:21:08 +0000 | [diff] [blame] | 397 | return DBuilder.createFile(remapDIPath(TheCU->getFilename()), |
Amjad Aboud | e2aab8c | 2016-12-25 10:12:27 +0000 | [diff] [blame] | 398 | remapDIPath(TheCU->getDirectory()), |
| 399 | TheCU->getFile()->getChecksumKind(), |
| 400 | TheCU->getFile()->getChecksum()); |
Guy Benyei | 11169dd | 2012-12-18 14:30:41 +0000 | [diff] [blame] | 401 | |
| 402 | // Cache the results. |
| 403 | const char *fname = PLoc.getFilename(); |
Duncan P. N. Exon Smith | fb49491 | 2014-12-09 18:39:32 +0000 | [diff] [blame] | 404 | auto it = DIFileCache.find(fname); |
Guy Benyei | 11169dd | 2012-12-18 14:30:41 +0000 | [diff] [blame] | 405 | |
| 406 | if (it != DIFileCache.end()) { |
| 407 | // Verify that the information still exists. |
Duncan P. N. Exon Smith | fb49491 | 2014-12-09 18:39:32 +0000 | [diff] [blame] | 408 | if (llvm::Metadata *V = it->second) |
Duncan P. N. Exon Smith | 9dd4e4e | 2015-04-29 16:40:08 +0000 | [diff] [blame] | 409 | return cast<llvm::DIFile>(V); |
Guy Benyei | 11169dd | 2012-12-18 14:30:41 +0000 | [diff] [blame] | 410 | } |
| 411 | |
Amjad Aboud | e2aab8c | 2016-12-25 10:12:27 +0000 | [diff] [blame] | 412 | SmallString<32> Checksum; |
| 413 | llvm::DIFile::ChecksumKind CSKind = |
| 414 | computeChecksum(SM.getFileID(Loc), Checksum); |
| 415 | |
Saleem Abdulrasool | 436256a | 2015-10-12 20:21:08 +0000 | [diff] [blame] | 416 | llvm::DIFile *F = DBuilder.createFile(remapDIPath(PLoc.getFilename()), |
Amjad Aboud | e2aab8c | 2016-12-25 10:12:27 +0000 | [diff] [blame] | 417 | remapDIPath(getCurrentDirname()), |
| 418 | CSKind, Checksum); |
Guy Benyei | 11169dd | 2012-12-18 14:30:41 +0000 | [diff] [blame] | 419 | |
Duncan P. N. Exon Smith | fb49491 | 2014-12-09 18:39:32 +0000 | [diff] [blame] | 420 | DIFileCache[fname].reset(F); |
Guy Benyei | 11169dd | 2012-12-18 14:30:41 +0000 | [diff] [blame] | 421 | return F; |
| 422 | } |
| 423 | |
Duncan P. N. Exon Smith | 9dd4e4e | 2015-04-29 16:40:08 +0000 | [diff] [blame] | 424 | llvm::DIFile *CGDebugInfo::getOrCreateMainFile() { |
Saleem Abdulrasool | 436256a | 2015-10-12 20:21:08 +0000 | [diff] [blame] | 425 | return DBuilder.createFile(remapDIPath(TheCU->getFilename()), |
Amjad Aboud | e2aab8c | 2016-12-25 10:12:27 +0000 | [diff] [blame] | 426 | remapDIPath(TheCU->getDirectory()), |
| 427 | TheCU->getFile()->getChecksumKind(), |
| 428 | TheCU->getFile()->getChecksum()); |
Saleem Abdulrasool | 436256a | 2015-10-12 20:21:08 +0000 | [diff] [blame] | 429 | } |
| 430 | |
| 431 | std::string CGDebugInfo::remapDIPath(StringRef Path) const { |
| 432 | for (const auto &Entry : DebugPrefixMap) |
| 433 | if (Path.startswith(Entry.first)) |
| 434 | return (Twine(Entry.second) + Path.substr(Entry.first.size())).str(); |
| 435 | return Path.str(); |
Guy Benyei | 11169dd | 2012-12-18 14:30:41 +0000 | [diff] [blame] | 436 | } |
| 437 | |
Guy Benyei | 11169dd | 2012-12-18 14:30:41 +0000 | [diff] [blame] | 438 | unsigned CGDebugInfo::getLineNumber(SourceLocation Loc) { |
| 439 | if (Loc.isInvalid() && CurLoc.isInvalid()) |
| 440 | return 0; |
| 441 | SourceManager &SM = CGM.getContext().getSourceManager(); |
| 442 | PresumedLoc PLoc = SM.getPresumedLoc(Loc.isValid() ? Loc : CurLoc); |
Eric Christopher | e7b87e5 | 2014-10-26 23:40:33 +0000 | [diff] [blame] | 443 | return PLoc.isValid() ? PLoc.getLine() : 0; |
Guy Benyei | 11169dd | 2012-12-18 14:30:41 +0000 | [diff] [blame] | 444 | } |
| 445 | |
Adrian Prantl | c782242 | 2013-03-12 20:43:25 +0000 | [diff] [blame] | 446 | unsigned CGDebugInfo::getColumnNumber(SourceLocation Loc, bool Force) { |
Guy Benyei | 11169dd | 2012-12-18 14:30:41 +0000 | [diff] [blame] | 447 | // We may not want column information at all. |
Adrian Prantl | c782242 | 2013-03-12 20:43:25 +0000 | [diff] [blame] | 448 | if (!Force && !CGM.getCodeGenOpts().DebugColumnInfo) |
Guy Benyei | 11169dd | 2012-12-18 14:30:41 +0000 | [diff] [blame] | 449 | return 0; |
| 450 | |
| 451 | // If the location is invalid then use the current column. |
| 452 | if (Loc.isInvalid() && CurLoc.isInvalid()) |
| 453 | return 0; |
| 454 | SourceManager &SM = CGM.getContext().getSourceManager(); |
| 455 | PresumedLoc PLoc = SM.getPresumedLoc(Loc.isValid() ? Loc : CurLoc); |
Eric Christopher | e7b87e5 | 2014-10-26 23:40:33 +0000 | [diff] [blame] | 456 | return PLoc.isValid() ? PLoc.getColumn() : 0; |
Guy Benyei | 11169dd | 2012-12-18 14:30:41 +0000 | [diff] [blame] | 457 | } |
| 458 | |
| 459 | StringRef CGDebugInfo::getCurrentDirname() { |
| 460 | if (!CGM.getCodeGenOpts().DebugCompilationDir.empty()) |
| 461 | return CGM.getCodeGenOpts().DebugCompilationDir; |
| 462 | |
| 463 | if (!CWDName.empty()) |
| 464 | return CWDName; |
| 465 | SmallString<256> CWD; |
| 466 | llvm::sys::fs::current_path(CWD); |
Benjamin Kramer | 1b18a5e | 2013-09-09 16:39:06 +0000 | [diff] [blame] | 467 | return CWDName = internString(CWD); |
Guy Benyei | 11169dd | 2012-12-18 14:30:41 +0000 | [diff] [blame] | 468 | } |
| 469 | |
Guy Benyei | 11169dd | 2012-12-18 14:30:41 +0000 | [diff] [blame] | 470 | void CGDebugInfo::CreateCompileUnit() { |
Amjad Aboud | e2aab8c | 2016-12-25 10:12:27 +0000 | [diff] [blame] | 471 | SmallString<32> Checksum; |
| 472 | llvm::DIFile::ChecksumKind CSKind = llvm::DIFile::CSK_None; |
Guy Benyei | 11169dd | 2012-12-18 14:30:41 +0000 | [diff] [blame] | 473 | |
David Blaikie | aabde05 | 2014-05-14 00:29:00 +0000 | [diff] [blame] | 474 | // Should we be asking the SourceManager for the main file name, instead of |
| 475 | // accepting it as an argument? This just causes the main file name to |
| 476 | // mismatch with source locations and create extra lexical scopes or |
| 477 | // mismatched debug info (a CU with a DW_AT_file of "-", because that's what |
| 478 | // the driver passed, but functions/other things have DW_AT_file of "<stdin>" |
| 479 | // because that's what the SourceManager says) |
| 480 | |
Guy Benyei | 11169dd | 2012-12-18 14:30:41 +0000 | [diff] [blame] | 481 | // Get absolute path name. |
| 482 | SourceManager &SM = CGM.getContext().getSourceManager(); |
| 483 | std::string MainFileName = CGM.getCodeGenOpts().MainFileName; |
| 484 | if (MainFileName.empty()) |
David Blaikie | aabde05 | 2014-05-14 00:29:00 +0000 | [diff] [blame] | 485 | MainFileName = "<stdin>"; |
Guy Benyei | 11169dd | 2012-12-18 14:30:41 +0000 | [diff] [blame] | 486 | |
| 487 | // The main file name provided via the "-main-file-name" option contains just |
| 488 | // the file name itself with no path information. This file name may have had |
| 489 | // a relative path, so we look into the actual file entry for the main |
| 490 | // file to determine the real absolute path for the file. |
| 491 | std::string MainFileDir; |
| 492 | if (const FileEntry *MainFile = SM.getFileEntryForID(SM.getMainFileID())) { |
Saleem Abdulrasool | 436256a | 2015-10-12 20:21:08 +0000 | [diff] [blame] | 493 | MainFileDir = remapDIPath(MainFile->getDir()->getName()); |
Yaron Keren | 9fb7e90 | 2013-10-21 20:07:37 +0000 | [diff] [blame] | 494 | if (MainFileDir != ".") { |
Eric Christopher | 0a1301f | 2014-02-26 02:49:36 +0000 | [diff] [blame] | 495 | llvm::SmallString<1024> MainFileDirSS(MainFileDir); |
| 496 | llvm::sys::path::append(MainFileDirSS, MainFileName); |
| 497 | MainFileName = MainFileDirSS.str(); |
Yaron Keren | 9fb7e90 | 2013-10-21 20:07:37 +0000 | [diff] [blame] | 498 | } |
Taewook Oh | 0fb5b78 | 2017-08-16 19:36:24 +0000 | [diff] [blame^] | 499 | // If the main file name provided is identical to the input file name, and |
| 500 | // if the input file is a preprocessed source, use the module name for |
| 501 | // debug info. The module name comes from the name specified in the first |
| 502 | // linemarker if the input is a preprocessed source. |
| 503 | if (MainFile->getName() == MainFileName && |
| 504 | FrontendOptions::getInputKindForExtension( |
| 505 | MainFile->getName().rsplit('.').second) |
| 506 | .isPreprocessed()) |
| 507 | MainFileName = CGM.getModule().getName().str(); |
| 508 | |
Amjad Aboud | e2aab8c | 2016-12-25 10:12:27 +0000 | [diff] [blame] | 509 | CSKind = computeChecksum(SM.getMainFileID(), Checksum); |
Guy Benyei | 11169dd | 2012-12-18 14:30:41 +0000 | [diff] [blame] | 510 | } |
| 511 | |
Ed Maste | da70602 | 2014-05-07 12:49:30 +0000 | [diff] [blame] | 512 | llvm::dwarf::SourceLanguage LangTag; |
Guy Benyei | 11169dd | 2012-12-18 14:30:41 +0000 | [diff] [blame] | 513 | const LangOptions &LO = CGM.getLangOpts(); |
| 514 | if (LO.CPlusPlus) { |
| 515 | if (LO.ObjC1) |
| 516 | LangTag = llvm::dwarf::DW_LANG_ObjC_plus_plus; |
| 517 | else |
| 518 | LangTag = llvm::dwarf::DW_LANG_C_plus_plus; |
| 519 | } else if (LO.ObjC1) { |
| 520 | LangTag = llvm::dwarf::DW_LANG_ObjC; |
Pirama Arumuga Nainar | a7484c9 | 2016-06-21 21:35:11 +0000 | [diff] [blame] | 521 | } else if (LO.RenderScript) { |
| 522 | LangTag = llvm::dwarf::DW_LANG_GOOGLE_RenderScript; |
Guy Benyei | 11169dd | 2012-12-18 14:30:41 +0000 | [diff] [blame] | 523 | } else if (LO.C99) { |
| 524 | LangTag = llvm::dwarf::DW_LANG_C99; |
| 525 | } else { |
| 526 | LangTag = llvm::dwarf::DW_LANG_C89; |
| 527 | } |
| 528 | |
| 529 | std::string Producer = getClangFullVersion(); |
| 530 | |
| 531 | // Figure out which version of the ObjC runtime we have. |
| 532 | unsigned RuntimeVers = 0; |
| 533 | if (LO.ObjC1) |
| 534 | RuntimeVers = LO.ObjCRuntime.isNonFragile() ? 2 : 1; |
| 535 | |
Adrian Prantl | 826824e | 2016-04-08 22:43:06 +0000 | [diff] [blame] | 536 | llvm::DICompileUnit::DebugEmissionKind EmissionKind; |
| 537 | switch (DebugKind) { |
| 538 | case codegenoptions::NoDebugInfo: |
| 539 | case codegenoptions::LocTrackingOnly: |
| 540 | EmissionKind = llvm::DICompileUnit::NoDebug; |
| 541 | break; |
| 542 | case codegenoptions::DebugLineTablesOnly: |
| 543 | EmissionKind = llvm::DICompileUnit::LineTablesOnly; |
| 544 | break; |
| 545 | case codegenoptions::LimitedDebugInfo: |
| 546 | case codegenoptions::FullDebugInfo: |
| 547 | EmissionKind = llvm::DICompileUnit::FullDebug; |
| 548 | break; |
| 549 | } |
| 550 | |
Guy Benyei | 11169dd | 2012-12-18 14:30:41 +0000 | [diff] [blame] | 551 | // Create new compile unit. |
Guy Benyei | 11169dd | 2012-12-18 14:30:41 +0000 | [diff] [blame] | 552 | // FIXME - Eliminate TheCU. |
Adrian Prantl | b442302 | 2017-08-04 23:08:57 +0000 | [diff] [blame] | 553 | auto &CGOpts = CGM.getCodeGenOpts(); |
Eric Christopher | e4200a2 | 2014-02-27 01:25:08 +0000 | [diff] [blame] | 554 | TheCU = DBuilder.createCompileUnit( |
David Blaikie | 8150355 | 2017-04-21 23:35:36 +0000 | [diff] [blame] | 555 | LangTag, |
| 556 | DBuilder.createFile(remapDIPath(MainFileName), |
| 557 | remapDIPath(getCurrentDirname()), CSKind, Checksum), |
Adrian Prantl | b442302 | 2017-08-04 23:08:57 +0000 | [diff] [blame] | 558 | Producer, LO.Optimize || CGOpts.PrepareForLTO || CGOpts.EmitSummaryIndex, |
| 559 | CGOpts.DwarfDebugFlags, RuntimeVers, |
| 560 | CGOpts.EnableSplitDwarf ? "" : CGOpts.SplitDwarfFile, EmissionKind, |
| 561 | 0 /* DWOid */, CGOpts.SplitDwarfInlining, CGOpts.DebugInfoForProfiling); |
Guy Benyei | 11169dd | 2012-12-18 14:30:41 +0000 | [diff] [blame] | 562 | } |
| 563 | |
Duncan P. N. Exon Smith | 9dd4e4e | 2015-04-29 16:40:08 +0000 | [diff] [blame] | 564 | llvm::DIType *CGDebugInfo::CreateType(const BuiltinType *BT) { |
Ed Maste | da70602 | 2014-05-07 12:49:30 +0000 | [diff] [blame] | 565 | llvm::dwarf::TypeKind Encoding; |
Guy Benyei | 11169dd | 2012-12-18 14:30:41 +0000 | [diff] [blame] | 566 | StringRef BTName; |
| 567 | switch (BT->getKind()) { |
| 568 | #define BUILTIN_TYPE(Id, SingletonId) |
Eric Christopher | e7b87e5 | 2014-10-26 23:40:33 +0000 | [diff] [blame] | 569 | #define PLACEHOLDER_TYPE(Id, SingletonId) case BuiltinType::Id: |
Guy Benyei | 11169dd | 2012-12-18 14:30:41 +0000 | [diff] [blame] | 570 | #include "clang/AST/BuiltinTypes.def" |
| 571 | case BuiltinType::Dependent: |
| 572 | llvm_unreachable("Unexpected builtin type"); |
| 573 | case BuiltinType::NullPtr: |
Peter Collingbourne | 5c5e617 | 2013-06-27 22:51:01 +0000 | [diff] [blame] | 574 | return DBuilder.createNullPtrType(); |
Guy Benyei | 11169dd | 2012-12-18 14:30:41 +0000 | [diff] [blame] | 575 | case BuiltinType::Void: |
Duncan P. N. Exon Smith | 0b6c369 | 2015-04-20 18:51:48 +0000 | [diff] [blame] | 576 | return nullptr; |
Guy Benyei | 11169dd | 2012-12-18 14:30:41 +0000 | [diff] [blame] | 577 | case BuiltinType::ObjCClass: |
David Blaikie | f427b00 | 2014-05-06 03:42:01 +0000 | [diff] [blame] | 578 | if (!ClassTy) |
| 579 | ClassTy = DBuilder.createForwardDecl(llvm::dwarf::DW_TAG_structure_type, |
| 580 | "objc_class", TheCU, |
| 581 | getOrCreateMainFile(), 0); |
Guy Benyei | 11169dd | 2012-12-18 14:30:41 +0000 | [diff] [blame] | 582 | return ClassTy; |
| 583 | case BuiltinType::ObjCId: { |
| 584 | // typedef struct objc_class *Class; |
| 585 | // typedef struct objc_object { |
| 586 | // Class isa; |
| 587 | // } *id; |
| 588 | |
Eric Christopher | f8bc4d8 | 2013-07-18 00:52:50 +0000 | [diff] [blame] | 589 | if (ObjTy) |
Guy Benyei | 11169dd | 2012-12-18 14:30:41 +0000 | [diff] [blame] | 590 | return ObjTy; |
| 591 | |
Eric Christopher | f8bc4d8 | 2013-07-18 00:52:50 +0000 | [diff] [blame] | 592 | if (!ClassTy) |
Guy Benyei | 11169dd | 2012-12-18 14:30:41 +0000 | [diff] [blame] | 593 | ClassTy = DBuilder.createForwardDecl(llvm::dwarf::DW_TAG_structure_type, |
| 594 | "objc_class", TheCU, |
| 595 | getOrCreateMainFile(), 0); |
| 596 | |
| 597 | unsigned Size = CGM.getContext().getTypeSize(CGM.getContext().VoidPtrTy); |
Eric Christopher | b2a008c | 2013-05-16 00:45:12 +0000 | [diff] [blame] | 598 | |
Duncan P. N. Exon Smith | 0b6c369 | 2015-04-20 18:51:48 +0000 | [diff] [blame] | 599 | auto *ISATy = DBuilder.createPointerType(ClassTy, Size); |
Guy Benyei | 11169dd | 2012-12-18 14:30:41 +0000 | [diff] [blame] | 600 | |
Leny Kholodov | df050fd | 2016-09-06 17:06:14 +0000 | [diff] [blame] | 601 | ObjTy = DBuilder.createStructType( |
| 602 | TheCU, "objc_object", getOrCreateMainFile(), 0, 0, 0, |
| 603 | llvm::DINode::FlagZero, nullptr, llvm::DINodeArray()); |
Guy Benyei | 11169dd | 2012-12-18 14:30:41 +0000 | [diff] [blame] | 604 | |
Duncan P. N. Exon Smith | c8ee63e | 2014-12-18 00:48:56 +0000 | [diff] [blame] | 605 | DBuilder.replaceArrays( |
Leny Kholodov | df050fd | 2016-09-06 17:06:14 +0000 | [diff] [blame] | 606 | ObjTy, DBuilder.getOrCreateArray(&*DBuilder.createMemberType( |
| 607 | ObjTy, "isa", getOrCreateMainFile(), 0, Size, 0, 0, |
| 608 | llvm::DINode::FlagZero, ISATy))); |
Guy Benyei | 11169dd | 2012-12-18 14:30:41 +0000 | [diff] [blame] | 609 | return ObjTy; |
| 610 | } |
| 611 | case BuiltinType::ObjCSel: { |
David Blaikie | f427b00 | 2014-05-06 03:42:01 +0000 | [diff] [blame] | 612 | if (!SelTy) |
| 613 | SelTy = DBuilder.createForwardDecl(llvm::dwarf::DW_TAG_structure_type, |
| 614 | "objc_selector", TheCU, |
| 615 | getOrCreateMainFile(), 0); |
Guy Benyei | 11169dd | 2012-12-18 14:30:41 +0000 | [diff] [blame] | 616 | return SelTy; |
| 617 | } |
Guy Benyei | d8a08ea | 2012-12-18 14:38:23 +0000 | [diff] [blame] | 618 | |
Alexey Bader | 954ba21 | 2016-04-08 13:40:33 +0000 | [diff] [blame] | 619 | #define IMAGE_TYPE(ImgType, Id, SingletonId, Access, Suffix) \ |
| 620 | case BuiltinType::Id: \ |
| 621 | return getOrCreateStructPtrType("opencl_" #ImgType "_" #Suffix "_t", \ |
| 622 | SingletonId); |
Alexey Bader | b62f144 | 2016-04-13 08:33:41 +0000 | [diff] [blame] | 623 | #include "clang/Basic/OpenCLImageTypes.def" |
Guy Benyei | 6105419 | 2013-02-07 10:55:47 +0000 | [diff] [blame] | 624 | case BuiltinType::OCLSampler: |
Yaxun Liu | 0bc4b2d | 2016-07-28 19:26:30 +0000 | [diff] [blame] | 625 | return getOrCreateStructPtrType("opencl_sampler_t", |
| 626 | OCLSamplerDITy); |
Guy Benyei | 1b4fb3e | 2013-01-20 12:31:11 +0000 | [diff] [blame] | 627 | case BuiltinType::OCLEvent: |
Eric Christopher | e7b87e5 | 2014-10-26 23:40:33 +0000 | [diff] [blame] | 628 | return getOrCreateStructPtrType("opencl_event_t", OCLEventDITy); |
Alexey Bader | 9c8453f | 2015-09-15 11:18:52 +0000 | [diff] [blame] | 629 | case BuiltinType::OCLClkEvent: |
| 630 | return getOrCreateStructPtrType("opencl_clk_event_t", OCLClkEventDITy); |
| 631 | case BuiltinType::OCLQueue: |
| 632 | return getOrCreateStructPtrType("opencl_queue_t", OCLQueueDITy); |
Alexey Bader | 9c8453f | 2015-09-15 11:18:52 +0000 | [diff] [blame] | 633 | case BuiltinType::OCLReserveID: |
| 634 | return getOrCreateStructPtrType("opencl_reserve_id_t", OCLReserveIDDITy); |
Guy Benyei | d8a08ea | 2012-12-18 14:38:23 +0000 | [diff] [blame] | 635 | |
Guy Benyei | 11169dd | 2012-12-18 14:30:41 +0000 | [diff] [blame] | 636 | case BuiltinType::UChar: |
Eric Christopher | e7b87e5 | 2014-10-26 23:40:33 +0000 | [diff] [blame] | 637 | case BuiltinType::Char_U: |
| 638 | Encoding = llvm::dwarf::DW_ATE_unsigned_char; |
| 639 | break; |
Guy Benyei | 11169dd | 2012-12-18 14:30:41 +0000 | [diff] [blame] | 640 | case BuiltinType::Char_S: |
Eric Christopher | e7b87e5 | 2014-10-26 23:40:33 +0000 | [diff] [blame] | 641 | case BuiltinType::SChar: |
| 642 | Encoding = llvm::dwarf::DW_ATE_signed_char; |
| 643 | break; |
Guy Benyei | 11169dd | 2012-12-18 14:30:41 +0000 | [diff] [blame] | 644 | case BuiltinType::Char16: |
Eric Christopher | e7b87e5 | 2014-10-26 23:40:33 +0000 | [diff] [blame] | 645 | case BuiltinType::Char32: |
| 646 | Encoding = llvm::dwarf::DW_ATE_UTF; |
| 647 | break; |
Guy Benyei | 11169dd | 2012-12-18 14:30:41 +0000 | [diff] [blame] | 648 | case BuiltinType::UShort: |
| 649 | case BuiltinType::UInt: |
| 650 | case BuiltinType::UInt128: |
| 651 | case BuiltinType::ULong: |
| 652 | case BuiltinType::WChar_U: |
Eric Christopher | e7b87e5 | 2014-10-26 23:40:33 +0000 | [diff] [blame] | 653 | case BuiltinType::ULongLong: |
| 654 | Encoding = llvm::dwarf::DW_ATE_unsigned; |
| 655 | break; |
Guy Benyei | 11169dd | 2012-12-18 14:30:41 +0000 | [diff] [blame] | 656 | case BuiltinType::Short: |
| 657 | case BuiltinType::Int: |
| 658 | case BuiltinType::Int128: |
| 659 | case BuiltinType::Long: |
| 660 | case BuiltinType::WChar_S: |
Eric Christopher | e7b87e5 | 2014-10-26 23:40:33 +0000 | [diff] [blame] | 661 | case BuiltinType::LongLong: |
| 662 | Encoding = llvm::dwarf::DW_ATE_signed; |
| 663 | break; |
| 664 | case BuiltinType::Bool: |
| 665 | Encoding = llvm::dwarf::DW_ATE_boolean; |
| 666 | break; |
Guy Benyei | 11169dd | 2012-12-18 14:30:41 +0000 | [diff] [blame] | 667 | case BuiltinType::Half: |
| 668 | case BuiltinType::Float: |
| 669 | case BuiltinType::LongDouble: |
Nemanja Ivanovic | bb1ea2d | 2016-05-09 08:52:33 +0000 | [diff] [blame] | 670 | case BuiltinType::Float128: |
Eric Christopher | e7b87e5 | 2014-10-26 23:40:33 +0000 | [diff] [blame] | 671 | case BuiltinType::Double: |
Nemanja Ivanovic | bb1ea2d | 2016-05-09 08:52:33 +0000 | [diff] [blame] | 672 | // FIXME: For targets where long double and __float128 have the same size, |
| 673 | // they are currently indistinguishable in the debugger without some |
| 674 | // special treatment. However, there is currently no consensus on encoding |
| 675 | // and this should be updated once a DWARF encoding exists for distinct |
| 676 | // floating point types of the same size. |
Eric Christopher | e7b87e5 | 2014-10-26 23:40:33 +0000 | [diff] [blame] | 677 | Encoding = llvm::dwarf::DW_ATE_float; |
| 678 | break; |
Guy Benyei | 11169dd | 2012-12-18 14:30:41 +0000 | [diff] [blame] | 679 | } |
| 680 | |
| 681 | switch (BT->getKind()) { |
Eric Christopher | e7b87e5 | 2014-10-26 23:40:33 +0000 | [diff] [blame] | 682 | case BuiltinType::Long: |
| 683 | BTName = "long int"; |
| 684 | break; |
| 685 | case BuiltinType::LongLong: |
| 686 | BTName = "long long int"; |
| 687 | break; |
| 688 | case BuiltinType::ULong: |
| 689 | BTName = "long unsigned int"; |
| 690 | break; |
| 691 | case BuiltinType::ULongLong: |
| 692 | BTName = "long long unsigned int"; |
| 693 | break; |
Guy Benyei | 11169dd | 2012-12-18 14:30:41 +0000 | [diff] [blame] | 694 | default: |
| 695 | BTName = BT->getName(CGM.getLangOpts()); |
| 696 | break; |
| 697 | } |
Victor Leschuk | a7ece03 | 2016-10-20 00:13:19 +0000 | [diff] [blame] | 698 | // Bit size and offset of the type. |
Guy Benyei | 11169dd | 2012-12-18 14:30:41 +0000 | [diff] [blame] | 699 | uint64_t Size = CGM.getContext().getTypeSize(BT); |
Victor Leschuk | a7ece03 | 2016-10-20 00:13:19 +0000 | [diff] [blame] | 700 | return DBuilder.createBasicType(BTName, Size, Encoding); |
Guy Benyei | 11169dd | 2012-12-18 14:30:41 +0000 | [diff] [blame] | 701 | } |
| 702 | |
Duncan P. N. Exon Smith | 9dd4e4e | 2015-04-29 16:40:08 +0000 | [diff] [blame] | 703 | llvm::DIType *CGDebugInfo::CreateType(const ComplexType *Ty) { |
Victor Leschuk | a7ece03 | 2016-10-20 00:13:19 +0000 | [diff] [blame] | 704 | // Bit size and offset of the type. |
Ed Maste | da70602 | 2014-05-07 12:49:30 +0000 | [diff] [blame] | 705 | llvm::dwarf::TypeKind Encoding = llvm::dwarf::DW_ATE_complex_float; |
Guy Benyei | 11169dd | 2012-12-18 14:30:41 +0000 | [diff] [blame] | 706 | if (Ty->isComplexIntegerType()) |
| 707 | Encoding = llvm::dwarf::DW_ATE_lo_user; |
| 708 | |
| 709 | uint64_t Size = CGM.getContext().getTypeSize(Ty); |
Victor Leschuk | a7ece03 | 2016-10-20 00:13:19 +0000 | [diff] [blame] | 710 | return DBuilder.createBasicType("complex", Size, Encoding); |
Guy Benyei | 11169dd | 2012-12-18 14:30:41 +0000 | [diff] [blame] | 711 | } |
| 712 | |
Duncan P. N. Exon Smith | 9dd4e4e | 2015-04-29 16:40:08 +0000 | [diff] [blame] | 713 | llvm::DIType *CGDebugInfo::CreateQualifiedType(QualType Ty, |
| 714 | llvm::DIFile *Unit) { |
Guy Benyei | 11169dd | 2012-12-18 14:30:41 +0000 | [diff] [blame] | 715 | QualifierCollector Qc; |
| 716 | const Type *T = Qc.strip(Ty); |
| 717 | |
| 718 | // Ignore these qualifiers for now. |
| 719 | Qc.removeObjCGCAttr(); |
| 720 | Qc.removeAddressSpace(); |
| 721 | Qc.removeObjCLifetime(); |
| 722 | |
| 723 | // We will create one Derived type for one qualifier and recurse to handle any |
| 724 | // additional ones. |
Ed Maste | da70602 | 2014-05-07 12:49:30 +0000 | [diff] [blame] | 725 | llvm::dwarf::Tag Tag; |
Guy Benyei | 11169dd | 2012-12-18 14:30:41 +0000 | [diff] [blame] | 726 | if (Qc.hasConst()) { |
| 727 | Tag = llvm::dwarf::DW_TAG_const_type; |
| 728 | Qc.removeConst(); |
| 729 | } else if (Qc.hasVolatile()) { |
| 730 | Tag = llvm::dwarf::DW_TAG_volatile_type; |
| 731 | Qc.removeVolatile(); |
| 732 | } else if (Qc.hasRestrict()) { |
| 733 | Tag = llvm::dwarf::DW_TAG_restrict_type; |
| 734 | Qc.removeRestrict(); |
| 735 | } else { |
| 736 | assert(Qc.empty() && "Unknown type qualifier for debug info"); |
| 737 | return getOrCreateType(QualType(T, 0), Unit); |
| 738 | } |
| 739 | |
Duncan P. N. Exon Smith | 0b6c369 | 2015-04-20 18:51:48 +0000 | [diff] [blame] | 740 | auto *FromTy = getOrCreateType(Qc.apply(CGM.getContext(), T), Unit); |
Guy Benyei | 11169dd | 2012-12-18 14:30:41 +0000 | [diff] [blame] | 741 | |
| 742 | // No need to fill in the Name, Line, Size, Alignment, Offset in case of |
| 743 | // CVR derived types. |
Duncan P. N. Exon Smith | 0b6c369 | 2015-04-20 18:51:48 +0000 | [diff] [blame] | 744 | return DBuilder.createQualifiedType(Tag, FromTy); |
Guy Benyei | 11169dd | 2012-12-18 14:30:41 +0000 | [diff] [blame] | 745 | } |
| 746 | |
Duncan P. N. Exon Smith | 9dd4e4e | 2015-04-29 16:40:08 +0000 | [diff] [blame] | 747 | llvm::DIType *CGDebugInfo::CreateType(const ObjCObjectPointerType *Ty, |
| 748 | llvm::DIFile *Unit) { |
Fariborz Jahanian | 65f1fa1 | 2013-02-21 20:42:11 +0000 | [diff] [blame] | 749 | |
| 750 | // The frontend treats 'id' as a typedef to an ObjCObjectType, |
| 751 | // whereas 'id<protocol>' is treated as an ObjCPointerType. For the |
| 752 | // debug info, we want to emit 'id' in both cases. |
| 753 | if (Ty->isObjCQualifiedIdType()) |
Eric Christopher | e7b87e5 | 2014-10-26 23:40:33 +0000 | [diff] [blame] | 754 | return getOrCreateType(CGM.getContext().getObjCIdType(), Unit); |
Fariborz Jahanian | 65f1fa1 | 2013-02-21 20:42:11 +0000 | [diff] [blame] | 755 | |
Duncan P. N. Exon Smith | 0b6c369 | 2015-04-20 18:51:48 +0000 | [diff] [blame] | 756 | return CreatePointerLikeType(llvm::dwarf::DW_TAG_pointer_type, Ty, |
| 757 | Ty->getPointeeType(), Unit); |
Guy Benyei | 11169dd | 2012-12-18 14:30:41 +0000 | [diff] [blame] | 758 | } |
| 759 | |
Duncan P. N. Exon Smith | 9dd4e4e | 2015-04-29 16:40:08 +0000 | [diff] [blame] | 760 | llvm::DIType *CGDebugInfo::CreateType(const PointerType *Ty, |
| 761 | llvm::DIFile *Unit) { |
Eric Christopher | b2a008c | 2013-05-16 00:45:12 +0000 | [diff] [blame] | 762 | return CreatePointerLikeType(llvm::dwarf::DW_TAG_pointer_type, Ty, |
Guy Benyei | 11169dd | 2012-12-18 14:30:41 +0000 | [diff] [blame] | 763 | Ty->getPointeeType(), Unit); |
| 764 | } |
| 765 | |
Adrian Prantl | c6f91a2 | 2015-06-15 23:18:16 +0000 | [diff] [blame] | 766 | /// \return whether a C++ mangling exists for the type defined by TD. |
| 767 | static bool hasCXXMangling(const TagDecl *TD, llvm::DICompileUnit *TheCU) { |
| 768 | switch (TheCU->getSourceLanguage()) { |
| 769 | case llvm::dwarf::DW_LANG_C_plus_plus: |
| 770 | return true; |
| 771 | case llvm::dwarf::DW_LANG_ObjC_plus_plus: |
| 772 | return isa<CXXRecordDecl>(TD) || isa<EnumDecl>(TD); |
| 773 | default: |
| 774 | return false; |
| 775 | } |
| 776 | } |
| 777 | |
Manman Ren | e0064d8 | 2013-08-29 23:19:58 +0000 | [diff] [blame] | 778 | /// In C++ mode, types have linkage, so we can rely on the ODR and |
| 779 | /// on their mangled names, if they're external. |
Eric Christopher | e7b87e5 | 2014-10-26 23:40:33 +0000 | [diff] [blame] | 780 | static SmallString<256> getUniqueTagTypeName(const TagType *Ty, |
| 781 | CodeGenModule &CGM, |
Duncan P. N. Exon Smith | 9dd4e4e | 2015-04-29 16:40:08 +0000 | [diff] [blame] | 782 | llvm::DICompileUnit *TheCU) { |
Manman Ren | e0064d8 | 2013-08-29 23:19:58 +0000 | [diff] [blame] | 783 | SmallString<256> FullName; |
Manman Ren | e0064d8 | 2013-08-29 23:19:58 +0000 | [diff] [blame] | 784 | const TagDecl *TD = Ty->getDecl(); |
Adrian Prantl | c6f91a2 | 2015-06-15 23:18:16 +0000 | [diff] [blame] | 785 | |
| 786 | if (!hasCXXMangling(TD, TheCU) || !TD->isExternallyVisible()) |
Manman Ren | e0064d8 | 2013-08-29 23:19:58 +0000 | [diff] [blame] | 787 | return FullName; |
Adrian Prantl | c6f91a2 | 2015-06-15 23:18:16 +0000 | [diff] [blame] | 788 | |
Manman Ren | e0064d8 | 2013-08-29 23:19:58 +0000 | [diff] [blame] | 789 | // TODO: This is using the RTTI name. Is there a better way to get |
| 790 | // a unique string for a type? |
| 791 | llvm::raw_svector_ostream Out(FullName); |
| 792 | CGM.getCXXABI().getMangleContext().mangleCXXRTTIName(QualType(Ty, 0), Out); |
Manman Ren | e0064d8 | 2013-08-29 23:19:58 +0000 | [diff] [blame] | 793 | return FullName; |
| 794 | } |
| 795 | |
Adrian Prantl | 3e8bad4 | 2015-07-08 21:18:34 +0000 | [diff] [blame] | 796 | /// \return the approproate DWARF tag for a composite type. |
Adrian Prantl | 5f66bae | 2015-02-11 17:45:15 +0000 | [diff] [blame] | 797 | static llvm::dwarf::Tag getTagForRecord(const RecordDecl *RD) { |
| 798 | llvm::dwarf::Tag Tag; |
| 799 | if (RD->isStruct() || RD->isInterface()) |
| 800 | Tag = llvm::dwarf::DW_TAG_structure_type; |
| 801 | else if (RD->isUnion()) |
| 802 | Tag = llvm::dwarf::DW_TAG_union_type; |
| 803 | else { |
| 804 | // FIXME: This could be a struct type giving a default visibility different |
| 805 | // than C++ class type, but needs llvm metadata changes first. |
| 806 | assert(RD->isClass()); |
| 807 | Tag = llvm::dwarf::DW_TAG_class_type; |
| 808 | } |
| 809 | return Tag; |
| 810 | } |
| 811 | |
Duncan P. N. Exon Smith | 9dd4e4e | 2015-04-29 16:40:08 +0000 | [diff] [blame] | 812 | llvm::DICompositeType * |
Manman Ren | 1b45702 | 2013-08-28 21:20:28 +0000 | [diff] [blame] | 813 | CGDebugInfo::getOrCreateRecordFwdDecl(const RecordType *Ty, |
Duncan P. N. Exon Smith | 9dd4e4e | 2015-04-29 16:40:08 +0000 | [diff] [blame] | 814 | llvm::DIScope *Ctx) { |
Manman Ren | 1b45702 | 2013-08-28 21:20:28 +0000 | [diff] [blame] | 815 | const RecordDecl *RD = Ty->getDecl(); |
Duncan P. N. Exon Smith | 9dd4e4e | 2015-04-29 16:40:08 +0000 | [diff] [blame] | 816 | if (llvm::DIType *T = getTypeOrNull(CGM.getContext().getRecordType(RD))) |
| 817 | return cast<llvm::DICompositeType>(T); |
| 818 | llvm::DIFile *DefUnit = getOrCreateFile(RD->getLocation()); |
Guy Benyei | 11169dd | 2012-12-18 14:30:41 +0000 | [diff] [blame] | 819 | unsigned Line = getLineNumber(RD->getLocation()); |
| 820 | StringRef RDName = getClassName(RD); |
| 821 | |
Peter Collingbourne | d251b0a | 2015-03-01 22:07:04 +0000 | [diff] [blame] | 822 | uint64_t Size = 0; |
Victor Leschuk | 802e4a5 | 2016-10-19 22:11:07 +0000 | [diff] [blame] | 823 | uint32_t Align = 0; |
Peter Collingbourne | d251b0a | 2015-03-01 22:07:04 +0000 | [diff] [blame] | 824 | |
Guy Benyei | 11169dd | 2012-12-18 14:30:41 +0000 | [diff] [blame] | 825 | // Create the type. |
Manman Ren | e0064d8 | 2013-08-29 23:19:58 +0000 | [diff] [blame] | 826 | SmallString<256> FullName = getUniqueTagTypeName(Ty, CGM, TheCU); |
Duncan P. N. Exon Smith | 9dd4e4e | 2015-04-29 16:40:08 +0000 | [diff] [blame] | 827 | llvm::DICompositeType *RetTy = DBuilder.createReplaceableCompositeType( |
Peter Collingbourne | d251b0a | 2015-03-01 22:07:04 +0000 | [diff] [blame] | 828 | getTagForRecord(RD), RDName, Ctx, DefUnit, Line, 0, Size, Align, |
Duncan P. N. Exon Smith | 9dd4e4e | 2015-04-29 16:40:08 +0000 | [diff] [blame] | 829 | llvm::DINode::FlagFwdDecl, FullName); |
Duncan P. N. Exon Smith | fb49491 | 2014-12-09 18:39:32 +0000 | [diff] [blame] | 830 | ReplaceMap.emplace_back( |
| 831 | std::piecewise_construct, std::make_tuple(Ty), |
| 832 | std::make_tuple(static_cast<llvm::Metadata *>(RetTy))); |
David Blaikie | f427b00 | 2014-05-06 03:42:01 +0000 | [diff] [blame] | 833 | return RetTy; |
Guy Benyei | 11169dd | 2012-12-18 14:30:41 +0000 | [diff] [blame] | 834 | } |
| 835 | |
Duncan P. N. Exon Smith | 9dd4e4e | 2015-04-29 16:40:08 +0000 | [diff] [blame] | 836 | llvm::DIType *CGDebugInfo::CreatePointerLikeType(llvm::dwarf::Tag Tag, |
Duncan P. N. Exon Smith | 0b6c369 | 2015-04-20 18:51:48 +0000 | [diff] [blame] | 837 | const Type *Ty, |
| 838 | QualType PointeeTy, |
Duncan P. N. Exon Smith | 9dd4e4e | 2015-04-29 16:40:08 +0000 | [diff] [blame] | 839 | llvm::DIFile *Unit) { |
Guy Benyei | 11169dd | 2012-12-18 14:30:41 +0000 | [diff] [blame] | 840 | // Bit size, align and offset of the type. |
| 841 | // Size is always the size of a pointer. We can't use getTypeSize here |
| 842 | // because that does not return the correct value for references. |
Konstantin Zhuravlyov | d1ba16e | 2017-03-08 23:56:48 +0000 | [diff] [blame] | 843 | unsigned AddressSpace = CGM.getContext().getTargetAddressSpace(PointeeTy); |
| 844 | uint64_t Size = CGM.getTarget().getPointerWidth(AddressSpace); |
Victor Leschuk | a7ece03 | 2016-10-20 00:13:19 +0000 | [diff] [blame] | 845 | auto Align = getTypeAlignIfRequired(Ty, CGM.getContext()); |
Konstantin Zhuravlyov | d1ba16e | 2017-03-08 23:56:48 +0000 | [diff] [blame] | 846 | Optional<unsigned> DWARFAddressSpace = |
| 847 | CGM.getTarget().getDWARFAddressSpace(AddressSpace); |
Guy Benyei | 11169dd | 2012-12-18 14:30:41 +0000 | [diff] [blame] | 848 | |
Keno Fischer | 87842f3 | 2015-11-16 09:04:13 +0000 | [diff] [blame] | 849 | if (Tag == llvm::dwarf::DW_TAG_reference_type || |
| 850 | Tag == llvm::dwarf::DW_TAG_rvalue_reference_type) |
| 851 | return DBuilder.createReferenceType(Tag, getOrCreateType(PointeeTy, Unit), |
Konstantin Zhuravlyov | d1ba16e | 2017-03-08 23:56:48 +0000 | [diff] [blame] | 852 | Size, Align, DWARFAddressSpace); |
Keno Fischer | 87842f3 | 2015-11-16 09:04:13 +0000 | [diff] [blame] | 853 | else |
| 854 | return DBuilder.createPointerType(getOrCreateType(PointeeTy, Unit), Size, |
Konstantin Zhuravlyov | d1ba16e | 2017-03-08 23:56:48 +0000 | [diff] [blame] | 855 | Align, DWARFAddressSpace); |
Guy Benyei | 11169dd | 2012-12-18 14:30:41 +0000 | [diff] [blame] | 856 | } |
| 857 | |
Duncan P. N. Exon Smith | 9dd4e4e | 2015-04-29 16:40:08 +0000 | [diff] [blame] | 858 | llvm::DIType *CGDebugInfo::getOrCreateStructPtrType(StringRef Name, |
| 859 | llvm::DIType *&Cache) { |
Eric Christopher | f8bc4d8 | 2013-07-18 00:52:50 +0000 | [diff] [blame] | 860 | if (Cache) |
Guy Benyei | d8a08ea | 2012-12-18 14:38:23 +0000 | [diff] [blame] | 861 | return Cache; |
David Blaikie | fefc7f7 | 2013-05-21 17:58:54 +0000 | [diff] [blame] | 862 | Cache = DBuilder.createForwardDecl(llvm::dwarf::DW_TAG_structure_type, Name, |
| 863 | TheCU, getOrCreateMainFile(), 0); |
| 864 | unsigned Size = CGM.getContext().getTypeSize(CGM.getContext().VoidPtrTy); |
| 865 | Cache = DBuilder.createPointerType(Cache, Size); |
| 866 | return Cache; |
Guy Benyei | d8a08ea | 2012-12-18 14:38:23 +0000 | [diff] [blame] | 867 | } |
| 868 | |
Duncan P. N. Exon Smith | 9dd4e4e | 2015-04-29 16:40:08 +0000 | [diff] [blame] | 869 | llvm::DIType *CGDebugInfo::CreateType(const BlockPointerType *Ty, |
| 870 | llvm::DIFile *Unit) { |
Duncan P. N. Exon Smith | fb49491 | 2014-12-09 18:39:32 +0000 | [diff] [blame] | 871 | SmallVector<llvm::Metadata *, 8> EltTys; |
Guy Benyei | 11169dd | 2012-12-18 14:30:41 +0000 | [diff] [blame] | 872 | QualType FType; |
| 873 | uint64_t FieldSize, FieldOffset; |
Victor Leschuk | 802e4a5 | 2016-10-19 22:11:07 +0000 | [diff] [blame] | 874 | uint32_t FieldAlign; |
Duncan P. N. Exon Smith | 9dd4e4e | 2015-04-29 16:40:08 +0000 | [diff] [blame] | 875 | llvm::DINodeArray Elements; |
Guy Benyei | 11169dd | 2012-12-18 14:30:41 +0000 | [diff] [blame] | 876 | |
| 877 | FieldOffset = 0; |
| 878 | FType = CGM.getContext().UnsignedLongTy; |
| 879 | EltTys.push_back(CreateMemberType(Unit, FType, "reserved", &FieldOffset)); |
| 880 | EltTys.push_back(CreateMemberType(Unit, FType, "Size", &FieldOffset)); |
| 881 | |
| 882 | Elements = DBuilder.getOrCreateArray(EltTys); |
| 883 | EltTys.clear(); |
| 884 | |
Leny Kholodov | 80c047d | 2016-09-06 10:48:04 +0000 | [diff] [blame] | 885 | llvm::DINode::DIFlags Flags = llvm::DINode::FlagAppleBlock; |
Adrian Prantl | 498fff6 | 2015-07-06 21:31:35 +0000 | [diff] [blame] | 886 | unsigned LineNo = 0; |
Guy Benyei | 11169dd | 2012-12-18 14:30:41 +0000 | [diff] [blame] | 887 | |
Duncan P. N. Exon Smith | 0b6c369 | 2015-04-20 18:51:48 +0000 | [diff] [blame] | 888 | auto *EltTy = |
Adrian Prantl | 498fff6 | 2015-07-06 21:31:35 +0000 | [diff] [blame] | 889 | DBuilder.createStructType(Unit, "__block_descriptor", nullptr, LineNo, |
Duncan P. N. Exon Smith | 0b6c369 | 2015-04-20 18:51:48 +0000 | [diff] [blame] | 890 | FieldOffset, 0, Flags, nullptr, Elements); |
Guy Benyei | 11169dd | 2012-12-18 14:30:41 +0000 | [diff] [blame] | 891 | |
| 892 | // Bit size, align and offset of the type. |
| 893 | uint64_t Size = CGM.getContext().getTypeSize(Ty); |
| 894 | |
Duncan P. N. Exon Smith | 0b6c369 | 2015-04-20 18:51:48 +0000 | [diff] [blame] | 895 | auto *DescTy = DBuilder.createPointerType(EltTy, Size); |
Guy Benyei | 11169dd | 2012-12-18 14:30:41 +0000 | [diff] [blame] | 896 | |
| 897 | FieldOffset = 0; |
| 898 | FType = CGM.getContext().getPointerType(CGM.getContext().VoidTy); |
| 899 | EltTys.push_back(CreateMemberType(Unit, FType, "__isa", &FieldOffset)); |
| 900 | FType = CGM.getContext().IntTy; |
| 901 | EltTys.push_back(CreateMemberType(Unit, FType, "__flags", &FieldOffset)); |
| 902 | EltTys.push_back(CreateMemberType(Unit, FType, "__reserved", &FieldOffset)); |
Adrian Prantl | 65d5d00 | 2014-11-05 01:01:30 +0000 | [diff] [blame] | 903 | FType = CGM.getContext().getPointerType(Ty->getPointeeType()); |
Guy Benyei | 11169dd | 2012-12-18 14:30:41 +0000 | [diff] [blame] | 904 | EltTys.push_back(CreateMemberType(Unit, FType, "__FuncPtr", &FieldOffset)); |
| 905 | |
| 906 | FType = CGM.getContext().getPointerType(CGM.getContext().VoidTy); |
Guy Benyei | 11169dd | 2012-12-18 14:30:41 +0000 | [diff] [blame] | 907 | FieldSize = CGM.getContext().getTypeSize(Ty); |
| 908 | FieldAlign = CGM.getContext().getTypeAlign(Ty); |
Leny Kholodov | df050fd | 2016-09-06 17:06:14 +0000 | [diff] [blame] | 909 | EltTys.push_back(DBuilder.createMemberType( |
| 910 | Unit, "__descriptor", nullptr, LineNo, FieldSize, FieldAlign, FieldOffset, |
| 911 | llvm::DINode::FlagZero, DescTy)); |
Guy Benyei | 11169dd | 2012-12-18 14:30:41 +0000 | [diff] [blame] | 912 | |
| 913 | FieldOffset += FieldSize; |
| 914 | Elements = DBuilder.getOrCreateArray(EltTys); |
| 915 | |
Adrian Prantl | 3d2c051 | 2015-07-07 00:49:35 +0000 | [diff] [blame] | 916 | // The __block_literal_generic structs are marked with a special |
| 917 | // DW_AT_APPLE_BLOCK attribute and are an implementation detail only |
| 918 | // the debugger needs to know about. To allow type uniquing, emit |
| 919 | // them without a name or a location. |
Duncan P. N. Exon Smith | 0b6c369 | 2015-04-20 18:51:48 +0000 | [diff] [blame] | 920 | EltTy = |
Adrian Prantl | 3d2c051 | 2015-07-07 00:49:35 +0000 | [diff] [blame] | 921 | DBuilder.createStructType(Unit, "", nullptr, LineNo, |
Duncan P. N. Exon Smith | 0b6c369 | 2015-04-20 18:51:48 +0000 | [diff] [blame] | 922 | FieldOffset, 0, Flags, nullptr, Elements); |
Guy Benyei | 11169dd | 2012-12-18 14:30:41 +0000 | [diff] [blame] | 923 | |
Adrian Prantl | 3d2c051 | 2015-07-07 00:49:35 +0000 | [diff] [blame] | 924 | return DBuilder.createPointerType(EltTy, Size); |
Guy Benyei | 11169dd | 2012-12-18 14:30:41 +0000 | [diff] [blame] | 925 | } |
| 926 | |
Duncan P. N. Exon Smith | 9dd4e4e | 2015-04-29 16:40:08 +0000 | [diff] [blame] | 927 | llvm::DIType *CGDebugInfo::CreateType(const TemplateSpecializationType *Ty, |
| 928 | llvm::DIFile *Unit) { |
David Blaikie | f1b382e | 2014-04-06 17:14:06 +0000 | [diff] [blame] | 929 | assert(Ty->isTypeAlias()); |
Duncan P. N. Exon Smith | 9dd4e4e | 2015-04-29 16:40:08 +0000 | [diff] [blame] | 930 | llvm::DIType *Src = getOrCreateType(Ty->getAliasedType(), Unit); |
David Blaikie | f1b382e | 2014-04-06 17:14:06 +0000 | [diff] [blame] | 931 | |
| 932 | SmallString<128> NS; |
| 933 | llvm::raw_svector_ostream OS(NS); |
Reid Kleckner | 59d1220 | 2017-08-08 01:33:53 +0000 | [diff] [blame] | 934 | Ty->getTemplateName().print(OS, getPrintingPolicy(), |
Eric Christopher | e7b87e5 | 2014-10-26 23:40:33 +0000 | [diff] [blame] | 935 | /*qualified*/ false); |
David Blaikie | f1b382e | 2014-04-06 17:14:06 +0000 | [diff] [blame] | 936 | |
| 937 | TemplateSpecializationType::PrintTemplateArgumentList( |
Reid Kleckner | 59d1220 | 2017-08-08 01:33:53 +0000 | [diff] [blame] | 938 | OS, Ty->template_arguments(), getPrintingPolicy()); |
David Blaikie | f1b382e | 2014-04-06 17:14:06 +0000 | [diff] [blame] | 939 | |
David Majnemer | 58ed0f3 | 2016-07-17 00:39:12 +0000 | [diff] [blame] | 940 | auto *AliasDecl = cast<TypeAliasTemplateDecl>( |
Eric Christopher | e7b87e5 | 2014-10-26 23:40:33 +0000 | [diff] [blame] | 941 | Ty->getTemplateName().getAsTemplateDecl())->getTemplatedDecl(); |
David Blaikie | f1b382e | 2014-04-06 17:14:06 +0000 | [diff] [blame] | 942 | |
| 943 | SourceLocation Loc = AliasDecl->getLocation(); |
Adrian Prantl | b67dbce | 2015-09-11 18:45:02 +0000 | [diff] [blame] | 944 | return DBuilder.createTypedef(Src, OS.str(), getOrCreateFile(Loc), |
| 945 | getLineNumber(Loc), |
| 946 | getDeclContextDescriptor(AliasDecl)); |
David Blaikie | f1b382e | 2014-04-06 17:14:06 +0000 | [diff] [blame] | 947 | } |
| 948 | |
Duncan P. N. Exon Smith | 9dd4e4e | 2015-04-29 16:40:08 +0000 | [diff] [blame] | 949 | llvm::DIType *CGDebugInfo::CreateType(const TypedefType *Ty, |
| 950 | llvm::DIFile *Unit) { |
Guy Benyei | 11169dd | 2012-12-18 14:30:41 +0000 | [diff] [blame] | 951 | // We don't set size information, but do specify where the typedef was |
| 952 | // declared. |
Amjad Aboud | dc4531e | 2016-04-30 01:44:38 +0000 | [diff] [blame] | 953 | SourceLocation Loc = Ty->getDecl()->getLocation(); |
Eric Christopher | b2a008c | 2013-05-16 00:45:12 +0000 | [diff] [blame] | 954 | |
Duncan P. N. Exon Smith | 4078ad4 | 2015-04-16 16:36:45 +0000 | [diff] [blame] | 955 | // Typedefs are derived from some other type. |
| 956 | return DBuilder.createTypedef( |
| 957 | getOrCreateType(Ty->getDecl()->getUnderlyingType(), Unit), |
| 958 | Ty->getDecl()->getName(), getOrCreateFile(Loc), getLineNumber(Loc), |
Amjad Aboud | dc4531e | 2016-04-30 01:44:38 +0000 | [diff] [blame] | 959 | getDeclContextDescriptor(Ty->getDecl())); |
Guy Benyei | 11169dd | 2012-12-18 14:30:41 +0000 | [diff] [blame] | 960 | } |
| 961 | |
Reid Kleckner | f00f803 | 2016-06-08 20:41:54 +0000 | [diff] [blame] | 962 | static unsigned getDwarfCC(CallingConv CC) { |
| 963 | switch (CC) { |
| 964 | case CC_C: |
| 965 | // Avoid emitting DW_AT_calling_convention if the C convention was used. |
| 966 | return 0; |
| 967 | |
| 968 | case CC_X86StdCall: |
| 969 | return llvm::dwarf::DW_CC_BORLAND_stdcall; |
| 970 | case CC_X86FastCall: |
| 971 | return llvm::dwarf::DW_CC_BORLAND_msfastcall; |
| 972 | case CC_X86ThisCall: |
| 973 | return llvm::dwarf::DW_CC_BORLAND_thiscall; |
| 974 | case CC_X86VectorCall: |
| 975 | return llvm::dwarf::DW_CC_LLVM_vectorcall; |
| 976 | case CC_X86Pascal: |
| 977 | return llvm::dwarf::DW_CC_BORLAND_pascal; |
| 978 | |
| 979 | // FIXME: Create new DW_CC_ codes for these calling conventions. |
Martin Storsjo | 022e782 | 2017-07-17 20:49:45 +0000 | [diff] [blame] | 980 | case CC_Win64: |
Reid Kleckner | f00f803 | 2016-06-08 20:41:54 +0000 | [diff] [blame] | 981 | case CC_X86_64SysV: |
| 982 | case CC_AAPCS: |
| 983 | case CC_AAPCS_VFP: |
| 984 | case CC_IntelOclBicc: |
| 985 | case CC_SpirFunction: |
Nikolay Haustov | 8c6538b | 2016-06-30 09:06:33 +0000 | [diff] [blame] | 986 | case CC_OpenCLKernel: |
Reid Kleckner | f00f803 | 2016-06-08 20:41:54 +0000 | [diff] [blame] | 987 | case CC_Swift: |
| 988 | case CC_PreserveMost: |
| 989 | case CC_PreserveAll: |
Erich Keane | 757d317 | 2016-11-02 18:29:35 +0000 | [diff] [blame] | 990 | case CC_X86RegCall: |
Reid Kleckner | f00f803 | 2016-06-08 20:41:54 +0000 | [diff] [blame] | 991 | return 0; |
| 992 | } |
| 993 | return 0; |
| 994 | } |
| 995 | |
Duncan P. N. Exon Smith | 9dd4e4e | 2015-04-29 16:40:08 +0000 | [diff] [blame] | 996 | llvm::DIType *CGDebugInfo::CreateType(const FunctionType *Ty, |
| 997 | llvm::DIFile *Unit) { |
Duncan P. N. Exon Smith | fb49491 | 2014-12-09 18:39:32 +0000 | [diff] [blame] | 998 | SmallVector<llvm::Metadata *, 16> EltTys; |
Guy Benyei | 11169dd | 2012-12-18 14:30:41 +0000 | [diff] [blame] | 999 | |
| 1000 | // Add the result type at least. |
Alp Toker | 314cc81 | 2014-01-25 16:55:45 +0000 | [diff] [blame] | 1001 | EltTys.push_back(getOrCreateType(Ty->getReturnType(), Unit)); |
Guy Benyei | 11169dd | 2012-12-18 14:30:41 +0000 | [diff] [blame] | 1002 | |
| 1003 | // Set up remainder of arguments if there is a prototype. |
Adrian Prantl | 800faef | 2014-02-25 23:42:18 +0000 | [diff] [blame] | 1004 | // otherwise emit it as a variadic function. |
Guy Benyei | 11169dd | 2012-12-18 14:30:41 +0000 | [diff] [blame] | 1005 | if (isa<FunctionNoProtoType>(Ty)) |
| 1006 | EltTys.push_back(DBuilder.createUnspecifiedParameter()); |
David Majnemer | 58ed0f3 | 2016-07-17 00:39:12 +0000 | [diff] [blame] | 1007 | else if (const auto *FPT = dyn_cast<FunctionProtoType>(Ty)) { |
| 1008 | for (const QualType &ParamType : FPT->param_types()) |
| 1009 | EltTys.push_back(getOrCreateType(ParamType, Unit)); |
Adrian Prantl | d45ba25 | 2014-02-25 19:38:11 +0000 | [diff] [blame] | 1010 | if (FPT->isVariadic()) |
| 1011 | EltTys.push_back(DBuilder.createUnspecifiedParameter()); |
Guy Benyei | 11169dd | 2012-12-18 14:30:41 +0000 | [diff] [blame] | 1012 | } |
| 1013 | |
Duncan P. N. Exon Smith | 9dd4e4e | 2015-04-29 16:40:08 +0000 | [diff] [blame] | 1014 | llvm::DITypeRefArray EltTypeArray = DBuilder.getOrCreateTypeArray(EltTys); |
Leny Kholodov | 80c047d | 2016-09-06 10:48:04 +0000 | [diff] [blame] | 1015 | return DBuilder.createSubroutineType(EltTypeArray, llvm::DINode::FlagZero, |
Reid Kleckner | f00f803 | 2016-06-08 20:41:54 +0000 | [diff] [blame] | 1016 | getDwarfCC(Ty->getCallConv())); |
Guy Benyei | 11169dd | 2012-12-18 14:30:41 +0000 | [diff] [blame] | 1017 | } |
| 1018 | |
Duncan P. N. Exon Smith | 9dd4e4e | 2015-04-29 16:40:08 +0000 | [diff] [blame] | 1019 | /// Convert an AccessSpecifier into the corresponding DINode flag. |
Adrian Prantl | 21361fb | 2014-08-29 22:44:27 +0000 | [diff] [blame] | 1020 | /// As an optimization, return 0 if the access specifier equals the |
| 1021 | /// default for the containing type. |
Leny Kholodov | df050fd | 2016-09-06 17:06:14 +0000 | [diff] [blame] | 1022 | static llvm::DINode::DIFlags getAccessFlag(AccessSpecifier Access, |
| 1023 | const RecordDecl *RD) { |
Adrian Prantl | 21361fb | 2014-08-29 22:44:27 +0000 | [diff] [blame] | 1024 | AccessSpecifier Default = clang::AS_none; |
| 1025 | if (RD && RD->isClass()) |
| 1026 | Default = clang::AS_private; |
| 1027 | else if (RD && (RD->isStruct() || RD->isUnion())) |
| 1028 | Default = clang::AS_public; |
| 1029 | |
| 1030 | if (Access == Default) |
Leny Kholodov | 80c047d | 2016-09-06 10:48:04 +0000 | [diff] [blame] | 1031 | return llvm::DINode::FlagZero; |
Adrian Prantl | 21361fb | 2014-08-29 22:44:27 +0000 | [diff] [blame] | 1032 | |
Eric Christopher | e7b87e5 | 2014-10-26 23:40:33 +0000 | [diff] [blame] | 1033 | switch (Access) { |
| 1034 | case clang::AS_private: |
Duncan P. N. Exon Smith | 9dd4e4e | 2015-04-29 16:40:08 +0000 | [diff] [blame] | 1035 | return llvm::DINode::FlagPrivate; |
Eric Christopher | e7b87e5 | 2014-10-26 23:40:33 +0000 | [diff] [blame] | 1036 | case clang::AS_protected: |
Duncan P. N. Exon Smith | 9dd4e4e | 2015-04-29 16:40:08 +0000 | [diff] [blame] | 1037 | return llvm::DINode::FlagProtected; |
Eric Christopher | e7b87e5 | 2014-10-26 23:40:33 +0000 | [diff] [blame] | 1038 | case clang::AS_public: |
Duncan P. N. Exon Smith | 9dd4e4e | 2015-04-29 16:40:08 +0000 | [diff] [blame] | 1039 | return llvm::DINode::FlagPublic; |
Eric Christopher | e7b87e5 | 2014-10-26 23:40:33 +0000 | [diff] [blame] | 1040 | case clang::AS_none: |
Leny Kholodov | 80c047d | 2016-09-06 10:48:04 +0000 | [diff] [blame] | 1041 | return llvm::DINode::FlagZero; |
Adrian Prantl | 21361fb | 2014-08-29 22:44:27 +0000 | [diff] [blame] | 1042 | } |
| 1043 | llvm_unreachable("unexpected access enumerator"); |
| 1044 | } |
Guy Benyei | 11169dd | 2012-12-18 14:30:41 +0000 | [diff] [blame] | 1045 | |
David Majnemer | b4b671e | 2016-06-30 03:01:59 +0000 | [diff] [blame] | 1046 | llvm::DIType *CGDebugInfo::createBitFieldType(const FieldDecl *BitFieldDecl, |
| 1047 | llvm::DIScope *RecordTy, |
| 1048 | const RecordDecl *RD) { |
| 1049 | StringRef Name = BitFieldDecl->getName(); |
| 1050 | QualType Ty = BitFieldDecl->getType(); |
| 1051 | SourceLocation Loc = BitFieldDecl->getLocation(); |
| 1052 | llvm::DIFile *VUnit = getOrCreateFile(Loc); |
| 1053 | llvm::DIType *DebugType = getOrCreateType(Ty, VUnit); |
| 1054 | |
| 1055 | // Get the location for the field. |
| 1056 | llvm::DIFile *File = getOrCreateFile(Loc); |
| 1057 | unsigned Line = getLineNumber(Loc); |
| 1058 | |
| 1059 | const CGBitFieldInfo &BitFieldInfo = |
| 1060 | CGM.getTypes().getCGRecordLayout(RD).getBitFieldInfo(BitFieldDecl); |
| 1061 | uint64_t SizeInBits = BitFieldInfo.Size; |
| 1062 | assert(SizeInBits > 0 && "found named 0-width bitfield"); |
David Majnemer | b4b671e | 2016-06-30 03:01:59 +0000 | [diff] [blame] | 1063 | uint64_t StorageOffsetInBits = |
| 1064 | CGM.getContext().toBits(BitFieldInfo.StorageOffset); |
Reid Kleckner | 06a4b2a | 2017-06-12 19:57:56 +0000 | [diff] [blame] | 1065 | uint64_t Offset = BitFieldInfo.Offset; |
| 1066 | // The bit offsets for big endian machines are reversed for big |
| 1067 | // endian target, compensate for that as the DIDerivedType requires |
| 1068 | // un-reversed offsets. |
| 1069 | if (CGM.getDataLayout().isBigEndian()) |
| 1070 | Offset = BitFieldInfo.StorageSize - BitFieldInfo.Size - Offset; |
| 1071 | uint64_t OffsetInBits = StorageOffsetInBits + Offset; |
Leny Kholodov | 80c047d | 2016-09-06 10:48:04 +0000 | [diff] [blame] | 1072 | llvm::DINode::DIFlags Flags = getAccessFlag(BitFieldDecl->getAccess(), RD); |
David Majnemer | b4b671e | 2016-06-30 03:01:59 +0000 | [diff] [blame] | 1073 | return DBuilder.createBitFieldMemberType( |
Victor Leschuk | a7ece03 | 2016-10-20 00:13:19 +0000 | [diff] [blame] | 1074 | RecordTy, Name, File, Line, SizeInBits, OffsetInBits, StorageOffsetInBits, |
| 1075 | Flags, DebugType); |
David Majnemer | b4b671e | 2016-06-30 03:01:59 +0000 | [diff] [blame] | 1076 | } |
| 1077 | |
| 1078 | llvm::DIType * |
| 1079 | CGDebugInfo::createFieldType(StringRef name, QualType type, SourceLocation loc, |
| 1080 | AccessSpecifier AS, uint64_t offsetInBits, |
Victor Leschuk | a7ece03 | 2016-10-20 00:13:19 +0000 | [diff] [blame] | 1081 | uint32_t AlignInBits, llvm::DIFile *tunit, |
| 1082 | llvm::DIScope *scope, const RecordDecl *RD) { |
Duncan P. N. Exon Smith | 9dd4e4e | 2015-04-29 16:40:08 +0000 | [diff] [blame] | 1083 | llvm::DIType *debugType = getOrCreateType(type, tunit); |
Guy Benyei | 11169dd | 2012-12-18 14:30:41 +0000 | [diff] [blame] | 1084 | |
| 1085 | // Get the location for the field. |
Duncan P. N. Exon Smith | 9dd4e4e | 2015-04-29 16:40:08 +0000 | [diff] [blame] | 1086 | llvm::DIFile *file = getOrCreateFile(loc); |
Guy Benyei | 11169dd | 2012-12-18 14:30:41 +0000 | [diff] [blame] | 1087 | unsigned line = getLineNumber(loc); |
| 1088 | |
David Majnemer | 34b5749 | 2014-07-30 01:30:47 +0000 | [diff] [blame] | 1089 | uint64_t SizeInBits = 0; |
Victor Leschuk | a7ece03 | 2016-10-20 00:13:19 +0000 | [diff] [blame] | 1090 | auto Align = AlignInBits; |
Guy Benyei | 11169dd | 2012-12-18 14:30:41 +0000 | [diff] [blame] | 1091 | if (!type->isIncompleteArrayType()) { |
David Majnemer | 34b5749 | 2014-07-30 01:30:47 +0000 | [diff] [blame] | 1092 | TypeInfo TI = CGM.getContext().getTypeInfo(type); |
| 1093 | SizeInBits = TI.Width; |
Victor Leschuk | a7ece03 | 2016-10-20 00:13:19 +0000 | [diff] [blame] | 1094 | if (!Align) |
| 1095 | Align = getTypeAlignIfRequired(type, CGM.getContext()); |
Guy Benyei | 11169dd | 2012-12-18 14:30:41 +0000 | [diff] [blame] | 1096 | } |
| 1097 | |
Leny Kholodov | 80c047d | 2016-09-06 10:48:04 +0000 | [diff] [blame] | 1098 | llvm::DINode::DIFlags flags = getAccessFlag(AS, RD); |
David Majnemer | 34b5749 | 2014-07-30 01:30:47 +0000 | [diff] [blame] | 1099 | return DBuilder.createMemberType(scope, name, file, line, SizeInBits, |
Victor Leschuk | a7ece03 | 2016-10-20 00:13:19 +0000 | [diff] [blame] | 1100 | Align, offsetInBits, flags, debugType); |
Guy Benyei | 11169dd | 2012-12-18 14:30:41 +0000 | [diff] [blame] | 1101 | } |
| 1102 | |
Duncan P. N. Exon Smith | fb49491 | 2014-12-09 18:39:32 +0000 | [diff] [blame] | 1103 | void CGDebugInfo::CollectRecordLambdaFields( |
| 1104 | const CXXRecordDecl *CXXDecl, SmallVectorImpl<llvm::Metadata *> &elements, |
Duncan P. N. Exon Smith | 9dd4e4e | 2015-04-29 16:40:08 +0000 | [diff] [blame] | 1105 | llvm::DIType *RecordTy) { |
Eric Christopher | 91a3190 | 2013-01-16 01:22:32 +0000 | [diff] [blame] | 1106 | // For C++11 Lambdas a Field will be the same as a Capture, but the Capture |
| 1107 | // has the name and the location of the variable so we should iterate over |
| 1108 | // both concurrently. |
| 1109 | const ASTRecordLayout &layout = CGM.getContext().getASTRecordLayout(CXXDecl); |
| 1110 | RecordDecl::field_iterator Field = CXXDecl->field_begin(); |
| 1111 | unsigned fieldno = 0; |
| 1112 | for (CXXRecordDecl::capture_const_iterator I = CXXDecl->captures_begin(), |
Eric Christopher | e7b87e5 | 2014-10-26 23:40:33 +0000 | [diff] [blame] | 1113 | E = CXXDecl->captures_end(); |
| 1114 | I != E; ++I, ++Field, ++fieldno) { |
Benjamin Kramer | f3ca2698 | 2014-05-10 16:31:55 +0000 | [diff] [blame] | 1115 | const LambdaCapture &C = *I; |
Eric Christopher | 91a3190 | 2013-01-16 01:22:32 +0000 | [diff] [blame] | 1116 | if (C.capturesVariable()) { |
David Majnemer | b4b671e | 2016-06-30 03:01:59 +0000 | [diff] [blame] | 1117 | SourceLocation Loc = C.getLocation(); |
| 1118 | assert(!Field->isBitField() && "lambdas don't have bitfield members!"); |
Eric Christopher | 91a3190 | 2013-01-16 01:22:32 +0000 | [diff] [blame] | 1119 | VarDecl *V = C.getCapturedVar(); |
Eric Christopher | 91a3190 | 2013-01-16 01:22:32 +0000 | [diff] [blame] | 1120 | StringRef VName = V->getName(); |
David Majnemer | b4b671e | 2016-06-30 03:01:59 +0000 | [diff] [blame] | 1121 | llvm::DIFile *VUnit = getOrCreateFile(Loc); |
Victor Leschuk | a7ece03 | 2016-10-20 00:13:19 +0000 | [diff] [blame] | 1122 | auto Align = getDeclAlignIfRequired(V, CGM.getContext()); |
David Majnemer | b4b671e | 2016-06-30 03:01:59 +0000 | [diff] [blame] | 1123 | llvm::DIType *FieldType = createFieldType( |
| 1124 | VName, Field->getType(), Loc, Field->getAccess(), |
Victor Leschuk | a7ece03 | 2016-10-20 00:13:19 +0000 | [diff] [blame] | 1125 | layout.getFieldOffset(fieldno), Align, VUnit, RecordTy, CXXDecl); |
David Majnemer | b4b671e | 2016-06-30 03:01:59 +0000 | [diff] [blame] | 1126 | elements.push_back(FieldType); |
Alexey Bataev | 39c81e2 | 2014-08-28 04:28:19 +0000 | [diff] [blame] | 1127 | } else if (C.capturesThis()) { |
Eric Christopher | 91a3190 | 2013-01-16 01:22:32 +0000 | [diff] [blame] | 1128 | // TODO: Need to handle 'this' in some way by probably renaming the |
| 1129 | // this of the lambda class and having a field member of 'this' or |
| 1130 | // by using AT_object_pointer for the function and having that be |
| 1131 | // used as 'this' for semantic references. |
Eric Christopher | 91a3190 | 2013-01-16 01:22:32 +0000 | [diff] [blame] | 1132 | FieldDecl *f = *Field; |
Duncan P. N. Exon Smith | 9dd4e4e | 2015-04-29 16:40:08 +0000 | [diff] [blame] | 1133 | llvm::DIFile *VUnit = getOrCreateFile(f->getLocation()); |
Eric Christopher | 91a3190 | 2013-01-16 01:22:32 +0000 | [diff] [blame] | 1134 | QualType type = f->getType(); |
Duncan P. N. Exon Smith | 9dd4e4e | 2015-04-29 16:40:08 +0000 | [diff] [blame] | 1135 | llvm::DIType *fieldType = createFieldType( |
David Majnemer | b4b671e | 2016-06-30 03:01:59 +0000 | [diff] [blame] | 1136 | "this", type, f->getLocation(), f->getAccess(), |
Eric Christopher | e7b87e5 | 2014-10-26 23:40:33 +0000 | [diff] [blame] | 1137 | layout.getFieldOffset(fieldno), VUnit, RecordTy, CXXDecl); |
Eric Christopher | 91a3190 | 2013-01-16 01:22:32 +0000 | [diff] [blame] | 1138 | |
| 1139 | elements.push_back(fieldType); |
| 1140 | } |
| 1141 | } |
| 1142 | } |
| 1143 | |
Duncan P. N. Exon Smith | 9dd4e4e | 2015-04-29 16:40:08 +0000 | [diff] [blame] | 1144 | llvm::DIDerivedType * |
| 1145 | CGDebugInfo::CreateRecordStaticField(const VarDecl *Var, llvm::DIType *RecordTy, |
Duncan P. N. Exon Smith | c09c548 | 2015-04-20 21:17:26 +0000 | [diff] [blame] | 1146 | const RecordDecl *RD) { |
Eric Christopher | 91a3190 | 2013-01-16 01:22:32 +0000 | [diff] [blame] | 1147 | // Create the descriptor for the static variable, with or without |
| 1148 | // constant initializers. |
David Blaikie | 8e707bb | 2014-10-14 22:22:17 +0000 | [diff] [blame] | 1149 | Var = Var->getCanonicalDecl(); |
Duncan P. N. Exon Smith | 9dd4e4e | 2015-04-29 16:40:08 +0000 | [diff] [blame] | 1150 | llvm::DIFile *VUnit = getOrCreateFile(Var->getLocation()); |
| 1151 | llvm::DIType *VTy = getOrCreateType(Var->getType(), VUnit); |
Eric Christopher | 91a3190 | 2013-01-16 01:22:32 +0000 | [diff] [blame] | 1152 | |
Eric Christopher | 91a3190 | 2013-01-16 01:22:32 +0000 | [diff] [blame] | 1153 | unsigned LineNumber = getLineNumber(Var->getLocation()); |
| 1154 | StringRef VName = Var->getName(); |
Craig Topper | 8a13c41 | 2014-05-21 05:09:00 +0000 | [diff] [blame] | 1155 | llvm::Constant *C = nullptr; |
Eric Christopher | 91a3190 | 2013-01-16 01:22:32 +0000 | [diff] [blame] | 1156 | if (Var->getInit()) { |
| 1157 | const APValue *Value = Var->evaluateValue(); |
David Blaikie | d42917f | 2013-01-20 01:19:17 +0000 | [diff] [blame] | 1158 | if (Value) { |
| 1159 | if (Value->isInt()) |
| 1160 | C = llvm::ConstantInt::get(CGM.getLLVMContext(), Value->getInt()); |
| 1161 | if (Value->isFloat()) |
| 1162 | C = llvm::ConstantFP::get(CGM.getLLVMContext(), Value->getFloat()); |
| 1163 | } |
Eric Christopher | 91a3190 | 2013-01-16 01:22:32 +0000 | [diff] [blame] | 1164 | } |
| 1165 | |
Leny Kholodov | 80c047d | 2016-09-06 10:48:04 +0000 | [diff] [blame] | 1166 | llvm::DINode::DIFlags Flags = getAccessFlag(Var->getAccess(), RD); |
Victor Leschuk | a7ece03 | 2016-10-20 00:13:19 +0000 | [diff] [blame] | 1167 | auto Align = getDeclAlignIfRequired(Var, CGM.getContext()); |
Duncan P. N. Exon Smith | 9dd4e4e | 2015-04-29 16:40:08 +0000 | [diff] [blame] | 1168 | llvm::DIDerivedType *GV = DBuilder.createStaticMemberType( |
Victor Leschuk | a7ece03 | 2016-10-20 00:13:19 +0000 | [diff] [blame] | 1169 | RecordTy, VName, VUnit, LineNumber, VTy, Flags, C, Align); |
Duncan P. N. Exon Smith | fb49491 | 2014-12-09 18:39:32 +0000 | [diff] [blame] | 1170 | StaticDataMemberCache[Var->getCanonicalDecl()].reset(GV); |
David Blaikie | ae01946 | 2013-08-15 22:50:29 +0000 | [diff] [blame] | 1171 | return GV; |
Eric Christopher | 91a3190 | 2013-01-16 01:22:32 +0000 | [diff] [blame] | 1172 | } |
| 1173 | |
Eric Christopher | e7b87e5 | 2014-10-26 23:40:33 +0000 | [diff] [blame] | 1174 | void CGDebugInfo::CollectRecordNormalField( |
Duncan P. N. Exon Smith | 9dd4e4e | 2015-04-29 16:40:08 +0000 | [diff] [blame] | 1175 | const FieldDecl *field, uint64_t OffsetInBits, llvm::DIFile *tunit, |
| 1176 | SmallVectorImpl<llvm::Metadata *> &elements, llvm::DIType *RecordTy, |
Eric Christopher | e7b87e5 | 2014-10-26 23:40:33 +0000 | [diff] [blame] | 1177 | const RecordDecl *RD) { |
Eric Christopher | 91a3190 | 2013-01-16 01:22:32 +0000 | [diff] [blame] | 1178 | StringRef name = field->getName(); |
| 1179 | QualType type = field->getType(); |
| 1180 | |
| 1181 | // Ignore unnamed fields unless they're anonymous structs/unions. |
| 1182 | if (name.empty() && !type->isRecordType()) |
| 1183 | return; |
| 1184 | |
David Majnemer | b4b671e | 2016-06-30 03:01:59 +0000 | [diff] [blame] | 1185 | llvm::DIType *FieldType; |
Eric Christopher | 91a3190 | 2013-01-16 01:22:32 +0000 | [diff] [blame] | 1186 | if (field->isBitField()) { |
David Majnemer | b4b671e | 2016-06-30 03:01:59 +0000 | [diff] [blame] | 1187 | FieldType = createBitFieldType(field, RecordTy, RD); |
| 1188 | } else { |
Victor Leschuk | a7ece03 | 2016-10-20 00:13:19 +0000 | [diff] [blame] | 1189 | auto Align = getDeclAlignIfRequired(field, CGM.getContext()); |
David Majnemer | b4b671e | 2016-06-30 03:01:59 +0000 | [diff] [blame] | 1190 | FieldType = |
| 1191 | createFieldType(name, type, field->getLocation(), field->getAccess(), |
Victor Leschuk | a7ece03 | 2016-10-20 00:13:19 +0000 | [diff] [blame] | 1192 | OffsetInBits, Align, tunit, RecordTy, RD); |
Eric Christopher | 91a3190 | 2013-01-16 01:22:32 +0000 | [diff] [blame] | 1193 | } |
| 1194 | |
David Majnemer | b4b671e | 2016-06-30 03:01:59 +0000 | [diff] [blame] | 1195 | elements.push_back(FieldType); |
Eric Christopher | 91a3190 | 2013-01-16 01:22:32 +0000 | [diff] [blame] | 1196 | } |
| 1197 | |
Reid Kleckner | e2e8206 | 2017-08-08 20:30:14 +0000 | [diff] [blame] | 1198 | void CGDebugInfo::CollectRecordNestedType( |
| 1199 | const TypeDecl *TD, SmallVectorImpl<llvm::Metadata *> &elements) { |
| 1200 | QualType Ty = CGM.getContext().getTypeDeclType(TD); |
Reid Kleckner | 755220b | 2016-08-01 18:56:13 +0000 | [diff] [blame] | 1201 | // Injected class names are not considered nested records. |
| 1202 | if (isa<InjectedClassNameType>(Ty)) |
| 1203 | return; |
Reid Kleckner | e2e8206 | 2017-08-08 20:30:14 +0000 | [diff] [blame] | 1204 | SourceLocation Loc = TD->getLocation(); |
Adrian McCarthy | ab1e786 | 2016-07-21 18:43:20 +0000 | [diff] [blame] | 1205 | llvm::DIType *nestedType = getOrCreateType(Ty, getOrCreateFile(Loc)); |
| 1206 | elements.push_back(nestedType); |
| 1207 | } |
| 1208 | |
Duncan P. N. Exon Smith | fb49491 | 2014-12-09 18:39:32 +0000 | [diff] [blame] | 1209 | void CGDebugInfo::CollectRecordFields( |
Duncan P. N. Exon Smith | 9dd4e4e | 2015-04-29 16:40:08 +0000 | [diff] [blame] | 1210 | const RecordDecl *record, llvm::DIFile *tunit, |
Duncan P. N. Exon Smith | fb49491 | 2014-12-09 18:39:32 +0000 | [diff] [blame] | 1211 | SmallVectorImpl<llvm::Metadata *> &elements, |
Duncan P. N. Exon Smith | 9dd4e4e | 2015-04-29 16:40:08 +0000 | [diff] [blame] | 1212 | llvm::DICompositeType *RecordTy) { |
David Majnemer | 58ed0f3 | 2016-07-17 00:39:12 +0000 | [diff] [blame] | 1213 | const auto *CXXDecl = dyn_cast<CXXRecordDecl>(record); |
Guy Benyei | 11169dd | 2012-12-18 14:30:41 +0000 | [diff] [blame] | 1214 | |
Eric Christopher | 91a3190 | 2013-01-16 01:22:32 +0000 | [diff] [blame] | 1215 | if (CXXDecl && CXXDecl->isLambda()) |
| 1216 | CollectRecordLambdaFields(CXXDecl, elements, RecordTy); |
| 1217 | else { |
| 1218 | const ASTRecordLayout &layout = CGM.getContext().getASTRecordLayout(record); |
Guy Benyei | 11169dd | 2012-12-18 14:30:41 +0000 | [diff] [blame] | 1219 | |
Reid Kleckner | e2e8206 | 2017-08-08 20:30:14 +0000 | [diff] [blame] | 1220 | // Debug info for nested types is included in the member list only for |
Adrian McCarthy | ab1e786 | 2016-07-21 18:43:20 +0000 | [diff] [blame] | 1221 | // CodeView. |
Reid Kleckner | e2e8206 | 2017-08-08 20:30:14 +0000 | [diff] [blame] | 1222 | bool IncludeNestedTypes = CGM.getCodeGenOpts().EmitCodeView; |
Adrian McCarthy | ab1e786 | 2016-07-21 18:43:20 +0000 | [diff] [blame] | 1223 | |
Eric Christopher | 91a3190 | 2013-01-16 01:22:32 +0000 | [diff] [blame] | 1224 | // Field number for non-static fields. |
Eric Christopher | 0f759437 | 2013-01-04 17:59:07 +0000 | [diff] [blame] | 1225 | unsigned fieldNo = 0; |
Eric Christopher | 91a3190 | 2013-01-16 01:22:32 +0000 | [diff] [blame] | 1226 | |
Eric Christopher | 91a3190 | 2013-01-16 01:22:32 +0000 | [diff] [blame] | 1227 | // Static and non-static members should appear in the same order as |
| 1228 | // the corresponding declarations in the source program. |
Aaron Ballman | 629afae | 2014-03-07 19:56:05 +0000 | [diff] [blame] | 1229 | for (const auto *I : record->decls()) |
| 1230 | if (const auto *V = dyn_cast<VarDecl>(I)) { |
Paul Robinson | b17327d | 2016-04-27 17:37:12 +0000 | [diff] [blame] | 1231 | if (V->hasAttr<NoDebugAttr>()) |
| 1232 | continue; |
David Blaikie | ce76304 | 2013-08-20 21:49:21 +0000 | [diff] [blame] | 1233 | // Reuse the existing static member declaration if one exists |
Duncan P. N. Exon Smith | fb49491 | 2014-12-09 18:39:32 +0000 | [diff] [blame] | 1234 | auto MI = StaticDataMemberCache.find(V->getCanonicalDecl()); |
David Blaikie | ce76304 | 2013-08-20 21:49:21 +0000 | [diff] [blame] | 1235 | if (MI != StaticDataMemberCache.end()) { |
| 1236 | assert(MI->second && |
| 1237 | "Static data member declaration should still exist"); |
Duncan P. N. Exon Smith | ac346ba | 2015-07-24 18:05:58 +0000 | [diff] [blame] | 1238 | elements.push_back(MI->second); |
Adrian Prantl | 21361fb | 2014-08-29 22:44:27 +0000 | [diff] [blame] | 1239 | } else { |
| 1240 | auto Field = CreateRecordStaticField(V, RecordTy, record); |
| 1241 | elements.push_back(Field); |
| 1242 | } |
Aaron Ballman | 629afae | 2014-03-07 19:56:05 +0000 | [diff] [blame] | 1243 | } else if (const auto *field = dyn_cast<FieldDecl>(I)) { |
Eric Christopher | e7b87e5 | 2014-10-26 23:40:33 +0000 | [diff] [blame] | 1244 | CollectRecordNormalField(field, layout.getFieldOffset(fieldNo), tunit, |
| 1245 | elements, RecordTy, record); |
Eric Christopher | 91a3190 | 2013-01-16 01:22:32 +0000 | [diff] [blame] | 1246 | |
| 1247 | // Bump field number for next field. |
| 1248 | ++fieldNo; |
Reid Kleckner | e2e8206 | 2017-08-08 20:30:14 +0000 | [diff] [blame] | 1249 | } else if (IncludeNestedTypes) { |
| 1250 | if (const auto *nestedType = dyn_cast<TypeDecl>(I)) |
| 1251 | if (!nestedType->isImplicit() && |
| 1252 | nestedType->getDeclContext() == record) |
| 1253 | CollectRecordNestedType(nestedType, elements); |
| 1254 | } |
Guy Benyei | 11169dd | 2012-12-18 14:30:41 +0000 | [diff] [blame] | 1255 | } |
| 1256 | } |
| 1257 | |
Duncan P. N. Exon Smith | 9dd4e4e | 2015-04-29 16:40:08 +0000 | [diff] [blame] | 1258 | llvm::DISubroutineType * |
Guy Benyei | 11169dd | 2012-12-18 14:30:41 +0000 | [diff] [blame] | 1259 | CGDebugInfo::getOrCreateMethodType(const CXXMethodDecl *Method, |
Duncan P. N. Exon Smith | 9dd4e4e | 2015-04-29 16:40:08 +0000 | [diff] [blame] | 1260 | llvm::DIFile *Unit) { |
David Blaikie | 7eb0685 | 2013-01-07 23:06:35 +0000 | [diff] [blame] | 1261 | const FunctionProtoType *Func = Method->getType()->getAs<FunctionProtoType>(); |
David Blaikie | 2aaf065 | 2013-01-07 22:24:59 +0000 | [diff] [blame] | 1262 | if (Method->isStatic()) |
Duncan P. N. Exon Smith | 9dd4e4e | 2015-04-29 16:40:08 +0000 | [diff] [blame] | 1263 | return cast_or_null<llvm::DISubroutineType>( |
Duncan P. N. Exon Smith | c755128 | 2015-04-06 23:21:33 +0000 | [diff] [blame] | 1264 | getOrCreateType(QualType(Func, 0), Unit)); |
David Blaikie | 7eb0685 | 2013-01-07 23:06:35 +0000 | [diff] [blame] | 1265 | return getOrCreateInstanceMethodType(Method->getThisType(CGM.getContext()), |
| 1266 | Func, Unit); |
| 1267 | } |
David Blaikie | 2aaf065 | 2013-01-07 22:24:59 +0000 | [diff] [blame] | 1268 | |
Duncan P. N. Exon Smith | 9dd4e4e | 2015-04-29 16:40:08 +0000 | [diff] [blame] | 1269 | llvm::DISubroutineType *CGDebugInfo::getOrCreateInstanceMethodType( |
| 1270 | QualType ThisPtr, const FunctionProtoType *Func, llvm::DIFile *Unit) { |
Guy Benyei | 11169dd | 2012-12-18 14:30:41 +0000 | [diff] [blame] | 1271 | // Add "this" pointer. |
Duncan P. N. Exon Smith | 9dd4e4e | 2015-04-29 16:40:08 +0000 | [diff] [blame] | 1272 | llvm::DITypeRefArray Args( |
| 1273 | cast<llvm::DISubroutineType>(getOrCreateType(QualType(Func, 0), Unit)) |
Duncan P. N. Exon Smith | c755128 | 2015-04-06 23:21:33 +0000 | [diff] [blame] | 1274 | ->getTypeArray()); |
Duncan P. N. Exon Smith | a98fac6 | 2015-04-07 04:14:45 +0000 | [diff] [blame] | 1275 | assert(Args.size() && "Invalid number of arguments!"); |
Guy Benyei | 11169dd | 2012-12-18 14:30:41 +0000 | [diff] [blame] | 1276 | |
Duncan P. N. Exon Smith | fb49491 | 2014-12-09 18:39:32 +0000 | [diff] [blame] | 1277 | SmallVector<llvm::Metadata *, 16> Elts; |
Guy Benyei | 11169dd | 2012-12-18 14:30:41 +0000 | [diff] [blame] | 1278 | |
| 1279 | // 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] | 1280 | Elts.push_back(Args[0]); |
Guy Benyei | 11169dd | 2012-12-18 14:30:41 +0000 | [diff] [blame] | 1281 | |
David Blaikie | 2aaf065 | 2013-01-07 22:24:59 +0000 | [diff] [blame] | 1282 | // "this" pointer is always first argument. |
David Blaikie | 7eb0685 | 2013-01-07 23:06:35 +0000 | [diff] [blame] | 1283 | const CXXRecordDecl *RD = ThisPtr->getPointeeCXXRecordDecl(); |
David Blaikie | 2aaf065 | 2013-01-07 22:24:59 +0000 | [diff] [blame] | 1284 | if (isa<ClassTemplateSpecializationDecl>(RD)) { |
| 1285 | // Create pointer type directly in this case. |
| 1286 | const PointerType *ThisPtrTy = cast<PointerType>(ThisPtr); |
| 1287 | QualType PointeeTy = ThisPtrTy->getPointeeType(); |
| 1288 | unsigned AS = CGM.getContext().getTargetAddressSpace(PointeeTy); |
John McCall | c8e0170 | 2013-04-16 22:48:15 +0000 | [diff] [blame] | 1289 | uint64_t Size = CGM.getTarget().getPointerWidth(AS); |
Victor Leschuk | a7ece03 | 2016-10-20 00:13:19 +0000 | [diff] [blame] | 1290 | auto Align = getTypeAlignIfRequired(ThisPtrTy, CGM.getContext()); |
Duncan P. N. Exon Smith | 9dd4e4e | 2015-04-29 16:40:08 +0000 | [diff] [blame] | 1291 | llvm::DIType *PointeeType = getOrCreateType(PointeeTy, Unit); |
| 1292 | llvm::DIType *ThisPtrType = |
Eric Christopher | e7b87e5 | 2014-10-26 23:40:33 +0000 | [diff] [blame] | 1293 | DBuilder.createPointerType(PointeeType, Size, Align); |
Duncan P. N. Exon Smith | fb49491 | 2014-12-09 18:39:32 +0000 | [diff] [blame] | 1294 | TypeCache[ThisPtr.getAsOpaquePtr()].reset(ThisPtrType); |
David Blaikie | 2aaf065 | 2013-01-07 22:24:59 +0000 | [diff] [blame] | 1295 | // TODO: This and the artificial type below are misleading, the |
| 1296 | // types aren't artificial the argument is, but the current |
| 1297 | // metadata doesn't represent that. |
| 1298 | ThisPtrType = DBuilder.createObjectPointerType(ThisPtrType); |
| 1299 | Elts.push_back(ThisPtrType); |
| 1300 | } else { |
Duncan P. N. Exon Smith | 9dd4e4e | 2015-04-29 16:40:08 +0000 | [diff] [blame] | 1301 | llvm::DIType *ThisPtrType = getOrCreateType(ThisPtr, Unit); |
Duncan P. N. Exon Smith | fb49491 | 2014-12-09 18:39:32 +0000 | [diff] [blame] | 1302 | TypeCache[ThisPtr.getAsOpaquePtr()].reset(ThisPtrType); |
David Blaikie | 2aaf065 | 2013-01-07 22:24:59 +0000 | [diff] [blame] | 1303 | ThisPtrType = DBuilder.createObjectPointerType(ThisPtrType); |
| 1304 | Elts.push_back(ThisPtrType); |
Guy Benyei | 11169dd | 2012-12-18 14:30:41 +0000 | [diff] [blame] | 1305 | } |
| 1306 | |
| 1307 | // Copy rest of the arguments. |
Duncan P. N. Exon Smith | a98fac6 | 2015-04-07 04:14:45 +0000 | [diff] [blame] | 1308 | for (unsigned i = 1, e = Args.size(); i != e; ++i) |
| 1309 | Elts.push_back(Args[i]); |
Guy Benyei | 11169dd | 2012-12-18 14:30:41 +0000 | [diff] [blame] | 1310 | |
Duncan P. N. Exon Smith | 9dd4e4e | 2015-04-29 16:40:08 +0000 | [diff] [blame] | 1311 | llvm::DITypeRefArray EltTypeArray = DBuilder.getOrCreateTypeArray(Elts); |
Guy Benyei | 11169dd | 2012-12-18 14:30:41 +0000 | [diff] [blame] | 1312 | |
Leny Kholodov | 80c047d | 2016-09-06 10:48:04 +0000 | [diff] [blame] | 1313 | llvm::DINode::DIFlags Flags = llvm::DINode::FlagZero; |
Adrian Prantl | 0630eb7 | 2013-12-18 21:48:18 +0000 | [diff] [blame] | 1314 | if (Func->getExtProtoInfo().RefQualifier == RQ_LValue) |
Duncan P. N. Exon Smith | 9dd4e4e | 2015-04-29 16:40:08 +0000 | [diff] [blame] | 1315 | Flags |= llvm::DINode::FlagLValueReference; |
Adrian Prantl | 0630eb7 | 2013-12-18 21:48:18 +0000 | [diff] [blame] | 1316 | if (Func->getExtProtoInfo().RefQualifier == RQ_RValue) |
Duncan P. N. Exon Smith | 9dd4e4e | 2015-04-29 16:40:08 +0000 | [diff] [blame] | 1317 | Flags |= llvm::DINode::FlagRValueReference; |
Adrian Prantl | 0630eb7 | 2013-12-18 21:48:18 +0000 | [diff] [blame] | 1318 | |
Reid Kleckner | f00f803 | 2016-06-08 20:41:54 +0000 | [diff] [blame] | 1319 | return DBuilder.createSubroutineType(EltTypeArray, Flags, |
| 1320 | getDwarfCC(Func->getCallConv())); |
Guy Benyei | 11169dd | 2012-12-18 14:30:41 +0000 | [diff] [blame] | 1321 | } |
| 1322 | |
Eric Christopher | b2a008c | 2013-05-16 00:45:12 +0000 | [diff] [blame] | 1323 | /// isFunctionLocalClass - Return true if CXXRecordDecl is defined |
Guy Benyei | 11169dd | 2012-12-18 14:30:41 +0000 | [diff] [blame] | 1324 | /// inside a function. |
| 1325 | static bool isFunctionLocalClass(const CXXRecordDecl *RD) { |
David Majnemer | 58ed0f3 | 2016-07-17 00:39:12 +0000 | [diff] [blame] | 1326 | if (const auto *NRD = dyn_cast<CXXRecordDecl>(RD->getDeclContext())) |
Guy Benyei | 11169dd | 2012-12-18 14:30:41 +0000 | [diff] [blame] | 1327 | return isFunctionLocalClass(NRD); |
| 1328 | if (isa<FunctionDecl>(RD->getDeclContext())) |
| 1329 | return true; |
| 1330 | return false; |
| 1331 | } |
| 1332 | |
Duncan P. N. Exon Smith | 9dd4e4e | 2015-04-29 16:40:08 +0000 | [diff] [blame] | 1333 | llvm::DISubprogram *CGDebugInfo::CreateCXXMemberFunction( |
| 1334 | const CXXMethodDecl *Method, llvm::DIFile *Unit, llvm::DIType *RecordTy) { |
Eric Christopher | b2a008c | 2013-05-16 00:45:12 +0000 | [diff] [blame] | 1335 | bool IsCtorOrDtor = |
Eric Christopher | e7b87e5 | 2014-10-26 23:40:33 +0000 | [diff] [blame] | 1336 | isa<CXXConstructorDecl>(Method) || isa<CXXDestructorDecl>(Method); |
Eric Christopher | b2a008c | 2013-05-16 00:45:12 +0000 | [diff] [blame] | 1337 | |
Guy Benyei | 11169dd | 2012-12-18 14:30:41 +0000 | [diff] [blame] | 1338 | StringRef MethodName = getFunctionName(Method); |
Duncan P. N. Exon Smith | 9dd4e4e | 2015-04-29 16:40:08 +0000 | [diff] [blame] | 1339 | llvm::DISubroutineType *MethodTy = getOrCreateMethodType(Method, Unit); |
Guy Benyei | 11169dd | 2012-12-18 14:30:41 +0000 | [diff] [blame] | 1340 | |
| 1341 | // Since a single ctor/dtor corresponds to multiple functions, it doesn't |
| 1342 | // make sense to give a single ctor/dtor a linkage name. |
| 1343 | StringRef MethodLinkageName; |
David Blaikie | 7164767 | 2016-04-12 21:22:48 +0000 | [diff] [blame] | 1344 | // FIXME: 'isFunctionLocalClass' seems like an arbitrary/unintentional |
| 1345 | // property to use here. It may've been intended to model "is non-external |
| 1346 | // type" but misses cases of non-function-local but non-external classes such |
| 1347 | // as those in anonymous namespaces as well as the reverse - external types |
| 1348 | // that are function local, such as those in (non-local) inline functions. |
Guy Benyei | 11169dd | 2012-12-18 14:30:41 +0000 | [diff] [blame] | 1349 | if (!IsCtorOrDtor && !isFunctionLocalClass(Method->getParent())) |
| 1350 | MethodLinkageName = CGM.getMangledName(Method); |
| 1351 | |
| 1352 | // Get the location for the method. |
Duncan P. N. Exon Smith | 9dd4e4e | 2015-04-29 16:40:08 +0000 | [diff] [blame] | 1353 | llvm::DIFile *MethodDefUnit = nullptr; |
David Blaikie | 7fceebf | 2013-08-19 03:37:48 +0000 | [diff] [blame] | 1354 | unsigned MethodLine = 0; |
| 1355 | if (!Method->isImplicit()) { |
| 1356 | MethodDefUnit = getOrCreateFile(Method->getLocation()); |
| 1357 | MethodLine = getLineNumber(Method->getLocation()); |
| 1358 | } |
Guy Benyei | 11169dd | 2012-12-18 14:30:41 +0000 | [diff] [blame] | 1359 | |
| 1360 | // Collect virtual method info. |
Duncan P. N. Exon Smith | 9dd4e4e | 2015-04-29 16:40:08 +0000 | [diff] [blame] | 1361 | llvm::DIType *ContainingType = nullptr; |
Eric Christopher | b2a008c | 2013-05-16 00:45:12 +0000 | [diff] [blame] | 1362 | unsigned Virtuality = 0; |
Guy Benyei | 11169dd | 2012-12-18 14:30:41 +0000 | [diff] [blame] | 1363 | unsigned VIndex = 0; |
Leny Kholodov | 80c047d | 2016-09-06 10:48:04 +0000 | [diff] [blame] | 1364 | llvm::DINode::DIFlags Flags = llvm::DINode::FlagZero; |
Reid Kleckner | 0358cbf | 2016-07-01 02:41:25 +0000 | [diff] [blame] | 1365 | int ThisAdjustment = 0; |
Eric Christopher | b2a008c | 2013-05-16 00:45:12 +0000 | [diff] [blame] | 1366 | |
Guy Benyei | 11169dd | 2012-12-18 14:30:41 +0000 | [diff] [blame] | 1367 | if (Method->isVirtual()) { |
| 1368 | if (Method->isPure()) |
| 1369 | Virtuality = llvm::dwarf::DW_VIRTUALITY_pure_virtual; |
| 1370 | else |
| 1371 | Virtuality = llvm::dwarf::DW_VIRTUALITY_virtual; |
Eric Christopher | b2a008c | 2013-05-16 00:45:12 +0000 | [diff] [blame] | 1372 | |
Reid Kleckner | 216d0a1 | 2016-06-16 20:08:51 +0000 | [diff] [blame] | 1373 | if (CGM.getTarget().getCXXABI().isItaniumFamily()) { |
| 1374 | // It doesn't make sense to give a virtual destructor a vtable index, |
| 1375 | // since a single destructor has two entries in the vtable. |
| 1376 | if (!isa<CXXDestructorDecl>(Method)) |
| 1377 | VIndex = CGM.getItaniumVTableContext().getMethodVTableIndex(Method); |
| 1378 | } else { |
| 1379 | // Emit MS ABI vftable information. There is only one entry for the |
| 1380 | // deleting dtor. |
| 1381 | const auto *DD = dyn_cast<CXXDestructorDecl>(Method); |
| 1382 | GlobalDecl GD = DD ? GlobalDecl(DD, Dtor_Deleting) : GlobalDecl(Method); |
| 1383 | MicrosoftVTableContext::MethodVFTableLocation ML = |
| 1384 | CGM.getMicrosoftVTableContext().getMethodVFTableLocation(GD); |
| 1385 | VIndex = ML.Index; |
Reid Kleckner | c4871ed | 2016-06-22 18:34:45 +0000 | [diff] [blame] | 1386 | |
| 1387 | // CodeView only records the vftable offset in the class that introduces |
| 1388 | // the virtual method. This is possible because, unlike Itanium, the MS |
| 1389 | // C++ ABI does not include all virtual methods from non-primary bases in |
| 1390 | // the vtable for the most derived class. For example, if C inherits from |
| 1391 | // A and B, C's primary vftable will not include B's virtual methods. |
| 1392 | if (Method->begin_overridden_methods() == Method->end_overridden_methods()) |
| 1393 | Flags |= llvm::DINode::FlagIntroducedVirtual; |
| 1394 | |
Reid Kleckner | 0358cbf | 2016-07-01 02:41:25 +0000 | [diff] [blame] | 1395 | // The 'this' adjustment accounts for both the virtual and non-virtual |
| 1396 | // portions of the adjustment. Presumably the debugger only uses it when |
| 1397 | // it knows the dynamic type of an object. |
| 1398 | ThisAdjustment = CGM.getCXXABI() |
| 1399 | .getVirtualFunctionPrologueThisAdjustment(GD) |
| 1400 | .getQuantity(); |
Reid Kleckner | 216d0a1 | 2016-06-16 20:08:51 +0000 | [diff] [blame] | 1401 | } |
Guy Benyei | 11169dd | 2012-12-18 14:30:41 +0000 | [diff] [blame] | 1402 | ContainingType = RecordTy; |
| 1403 | } |
| 1404 | |
Guy Benyei | 11169dd | 2012-12-18 14:30:41 +0000 | [diff] [blame] | 1405 | if (Method->isImplicit()) |
Duncan P. N. Exon Smith | 9dd4e4e | 2015-04-29 16:40:08 +0000 | [diff] [blame] | 1406 | Flags |= llvm::DINode::FlagArtificial; |
Adrian Prantl | 21361fb | 2014-08-29 22:44:27 +0000 | [diff] [blame] | 1407 | Flags |= getAccessFlag(Method->getAccess(), Method->getParent()); |
David Majnemer | 58ed0f3 | 2016-07-17 00:39:12 +0000 | [diff] [blame] | 1408 | if (const auto *CXXC = dyn_cast<CXXConstructorDecl>(Method)) { |
Guy Benyei | 11169dd | 2012-12-18 14:30:41 +0000 | [diff] [blame] | 1409 | if (CXXC->isExplicit()) |
Duncan P. N. Exon Smith | 9dd4e4e | 2015-04-29 16:40:08 +0000 | [diff] [blame] | 1410 | Flags |= llvm::DINode::FlagExplicit; |
David Majnemer | 58ed0f3 | 2016-07-17 00:39:12 +0000 | [diff] [blame] | 1411 | } else if (const auto *CXXC = dyn_cast<CXXConversionDecl>(Method)) { |
Guy Benyei | 11169dd | 2012-12-18 14:30:41 +0000 | [diff] [blame] | 1412 | if (CXXC->isExplicit()) |
Duncan P. N. Exon Smith | 9dd4e4e | 2015-04-29 16:40:08 +0000 | [diff] [blame] | 1413 | Flags |= llvm::DINode::FlagExplicit; |
Guy Benyei | 11169dd | 2012-12-18 14:30:41 +0000 | [diff] [blame] | 1414 | } |
| 1415 | if (Method->hasPrototype()) |
Duncan P. N. Exon Smith | 9dd4e4e | 2015-04-29 16:40:08 +0000 | [diff] [blame] | 1416 | Flags |= llvm::DINode::FlagPrototyped; |
Adrian Prantl | 0630eb7 | 2013-12-18 21:48:18 +0000 | [diff] [blame] | 1417 | if (Method->getRefQualifier() == RQ_LValue) |
Duncan P. N. Exon Smith | 9dd4e4e | 2015-04-29 16:40:08 +0000 | [diff] [blame] | 1418 | Flags |= llvm::DINode::FlagLValueReference; |
Adrian Prantl | 0630eb7 | 2013-12-18 21:48:18 +0000 | [diff] [blame] | 1419 | if (Method->getRefQualifier() == RQ_RValue) |
Duncan P. N. Exon Smith | 9dd4e4e | 2015-04-29 16:40:08 +0000 | [diff] [blame] | 1420 | Flags |= llvm::DINode::FlagRValueReference; |
Guy Benyei | 11169dd | 2012-12-18 14:30:41 +0000 | [diff] [blame] | 1421 | |
Duncan P. N. Exon Smith | 9dd4e4e | 2015-04-29 16:40:08 +0000 | [diff] [blame] | 1422 | llvm::DINodeArray TParamsArray = CollectFunctionTemplateParams(Method, Unit); |
| 1423 | llvm::DISubprogram *SP = DBuilder.createMethod( |
Eric Christopher | e7b87e5 | 2014-10-26 23:40:33 +0000 | [diff] [blame] | 1424 | RecordTy, MethodName, MethodLinkageName, MethodDefUnit, MethodLine, |
Reid Kleckner | 0358cbf | 2016-07-01 02:41:25 +0000 | [diff] [blame] | 1425 | MethodTy, /*isLocalToUnit=*/false, /*isDefinition=*/false, Virtuality, |
| 1426 | VIndex, ThisAdjustment, ContainingType, Flags, CGM.getLangOpts().Optimize, |
| 1427 | TParamsArray.get()); |
Eric Christopher | b2a008c | 2013-05-16 00:45:12 +0000 | [diff] [blame] | 1428 | |
Duncan P. N. Exon Smith | fb49491 | 2014-12-09 18:39:32 +0000 | [diff] [blame] | 1429 | SPCache[Method->getCanonicalDecl()].reset(SP); |
Guy Benyei | 11169dd | 2012-12-18 14:30:41 +0000 | [diff] [blame] | 1430 | |
| 1431 | return SP; |
| 1432 | } |
| 1433 | |
Eric Christopher | e7b87e5 | 2014-10-26 23:40:33 +0000 | [diff] [blame] | 1434 | void CGDebugInfo::CollectCXXMemberFunctions( |
Duncan P. N. Exon Smith | 9dd4e4e | 2015-04-29 16:40:08 +0000 | [diff] [blame] | 1435 | const CXXRecordDecl *RD, llvm::DIFile *Unit, |
| 1436 | SmallVectorImpl<llvm::Metadata *> &EltTys, llvm::DIType *RecordTy) { |
Guy Benyei | 11169dd | 2012-12-18 14:30:41 +0000 | [diff] [blame] | 1437 | |
| 1438 | // Since we want more than just the individual member decls if we |
| 1439 | // have templated functions iterate over every declaration to gather |
| 1440 | // the functions. |
Eric Christopher | e7b87e5 | 2014-10-26 23:40:33 +0000 | [diff] [blame] | 1441 | for (const auto *I : RD->decls()) { |
David Blaikie | fd58072 | 2014-10-06 05:18:55 +0000 | [diff] [blame] | 1442 | const auto *Method = dyn_cast<CXXMethodDecl>(I); |
| 1443 | // If the member is implicit, don't add it to the member list. This avoids |
| 1444 | // the member being added to type units by LLVM, while still allowing it |
| 1445 | // to be emitted into the type declaration/reference inside the compile |
| 1446 | // unit. |
Paul Robinson | 6a7511b | 2015-06-25 17:50:43 +0000 | [diff] [blame] | 1447 | // Ditto 'nodebug' methods, for consistency with CodeGenFunction.cpp. |
David Blaikie | 6dddfe3 | 2014-10-06 05:52:27 +0000 | [diff] [blame] | 1448 | // FIXME: Handle Using(Shadow?)Decls here to create |
| 1449 | // DW_TAG_imported_declarations inside the class for base decls brought into |
| 1450 | // derived classes. GDB doesn't seem to notice/leverage these when I tried |
| 1451 | // it, so I'm not rushing to fix this. (GCC seems to produce them, if |
| 1452 | // referenced) |
Paul Robinson | 6a7511b | 2015-06-25 17:50:43 +0000 | [diff] [blame] | 1453 | if (!Method || Method->isImplicit() || Method->hasAttr<NoDebugAttr>()) |
David Blaikie | fd58072 | 2014-10-06 05:18:55 +0000 | [diff] [blame] | 1454 | continue; |
David Blaikie | 42edade | 2014-11-11 20:44:45 +0000 | [diff] [blame] | 1455 | |
| 1456 | if (Method->getType()->getAs<FunctionProtoType>()->getContainedAutoType()) |
| 1457 | continue; |
| 1458 | |
David Blaikie | fd58072 | 2014-10-06 05:18:55 +0000 | [diff] [blame] | 1459 | // Reuse the existing member function declaration if it exists. |
| 1460 | // It may be associated with the declaration of the type & should be |
| 1461 | // reused as we're building the definition. |
| 1462 | // |
| 1463 | // This situation can arise in the vtable-based debug info reduction where |
| 1464 | // implicit members are emitted in a non-vtable TU. |
| 1465 | auto MI = SPCache.find(Method->getCanonicalDecl()); |
| 1466 | EltTys.push_back(MI == SPCache.end() |
| 1467 | ? CreateCXXMemberFunction(Method, Unit, RecordTy) |
Duncan P. N. Exon Smith | fb49491 | 2014-12-09 18:39:32 +0000 | [diff] [blame] | 1468 | : static_cast<llvm::Metadata *>(MI->second)); |
Guy Benyei | 11169dd | 2012-12-18 14:30:41 +0000 | [diff] [blame] | 1469 | } |
Eric Christopher | b2a008c | 2013-05-16 00:45:12 +0000 | [diff] [blame] | 1470 | } |
Guy Benyei | 11169dd | 2012-12-18 14:30:41 +0000 | [diff] [blame] | 1471 | |
Duncan P. N. Exon Smith | 9dd4e4e | 2015-04-29 16:40:08 +0000 | [diff] [blame] | 1472 | void CGDebugInfo::CollectCXXBases(const CXXRecordDecl *RD, llvm::DIFile *Unit, |
Duncan P. N. Exon Smith | fb49491 | 2014-12-09 18:39:32 +0000 | [diff] [blame] | 1473 | SmallVectorImpl<llvm::Metadata *> &EltTys, |
Duncan P. N. Exon Smith | 9dd4e4e | 2015-04-29 16:40:08 +0000 | [diff] [blame] | 1474 | llvm::DIType *RecordTy) { |
Bob Haarman | dff3673 | 2016-10-25 22:19:32 +0000 | [diff] [blame] | 1475 | llvm::DenseSet<CanonicalDeclPtr<const CXXRecordDecl>> SeenTypes; |
| 1476 | CollectCXXBasesAux(RD, Unit, EltTys, RecordTy, RD->bases(), SeenTypes, |
| 1477 | llvm::DINode::FlagZero); |
Eric Christopher | b2a008c | 2013-05-16 00:45:12 +0000 | [diff] [blame] | 1478 | |
Bob Haarman | dff3673 | 2016-10-25 22:19:32 +0000 | [diff] [blame] | 1479 | // If we are generating CodeView debug info, we also need to emit records for |
| 1480 | // indirect virtual base classes. |
| 1481 | if (CGM.getCodeGenOpts().EmitCodeView) { |
| 1482 | CollectCXXBasesAux(RD, Unit, EltTys, RecordTy, RD->vbases(), SeenTypes, |
| 1483 | llvm::DINode::FlagIndirectVirtualBase); |
| 1484 | } |
| 1485 | } |
| 1486 | |
| 1487 | void CGDebugInfo::CollectCXXBasesAux( |
| 1488 | const CXXRecordDecl *RD, llvm::DIFile *Unit, |
| 1489 | SmallVectorImpl<llvm::Metadata *> &EltTys, llvm::DIType *RecordTy, |
| 1490 | const CXXRecordDecl::base_class_const_range &Bases, |
| 1491 | llvm::DenseSet<CanonicalDeclPtr<const CXXRecordDecl>> &SeenTypes, |
| 1492 | llvm::DINode::DIFlags StartingFlags) { |
| 1493 | const ASTRecordLayout &RL = CGM.getContext().getASTRecordLayout(RD); |
| 1494 | for (const auto &BI : Bases) { |
David Majnemer | 58ed0f3 | 2016-07-17 00:39:12 +0000 | [diff] [blame] | 1495 | const auto *Base = |
Eric Christopher | e7b87e5 | 2014-10-26 23:40:33 +0000 | [diff] [blame] | 1496 | cast<CXXRecordDecl>(BI.getType()->getAs<RecordType>()->getDecl()); |
Bob Haarman | dff3673 | 2016-10-25 22:19:32 +0000 | [diff] [blame] | 1497 | if (!SeenTypes.insert(Base).second) |
| 1498 | continue; |
| 1499 | auto *BaseTy = getOrCreateType(BI.getType(), Unit); |
| 1500 | llvm::DINode::DIFlags BFlags = StartingFlags; |
| 1501 | uint64_t BaseOffset; |
Eric Christopher | b2a008c | 2013-05-16 00:45:12 +0000 | [diff] [blame] | 1502 | |
Aaron Ballman | 574705e | 2014-03-13 15:41:46 +0000 | [diff] [blame] | 1503 | if (BI.isVirtual()) { |
Reid Kleckner | d3b23d6 | 2014-08-07 21:29:25 +0000 | [diff] [blame] | 1504 | if (CGM.getTarget().getCXXABI().isItaniumFamily()) { |
| 1505 | // virtual base offset offset is -ve. The code generator emits dwarf |
| 1506 | // expression where it expects +ve number. |
Eric Christopher | e7b87e5 | 2014-10-26 23:40:33 +0000 | [diff] [blame] | 1507 | BaseOffset = 0 - CGM.getItaniumVTableContext() |
| 1508 | .getVirtualBaseOffsetOffset(RD, Base) |
| 1509 | .getQuantity(); |
Reid Kleckner | d3b23d6 | 2014-08-07 21:29:25 +0000 | [diff] [blame] | 1510 | } else { |
| 1511 | // In the MS ABI, store the vbtable offset, which is analogous to the |
| 1512 | // vbase offset offset in Itanium. |
| 1513 | BaseOffset = |
| 1514 | 4 * CGM.getMicrosoftVTableContext().getVBTableIndex(RD, Base); |
| 1515 | } |
Bob Haarman | dff3673 | 2016-10-25 22:19:32 +0000 | [diff] [blame] | 1516 | BFlags |= llvm::DINode::FlagVirtual; |
Guy Benyei | 11169dd | 2012-12-18 14:30:41 +0000 | [diff] [blame] | 1517 | } else |
| 1518 | BaseOffset = CGM.getContext().toBits(RL.getBaseClassOffset(Base)); |
| 1519 | // FIXME: Inconsistent units for BaseOffset. It is in bytes when |
| 1520 | // BI->isVirtual() and bits when not. |
Eric Christopher | b2a008c | 2013-05-16 00:45:12 +0000 | [diff] [blame] | 1521 | |
Adrian Prantl | 21361fb | 2014-08-29 22:44:27 +0000 | [diff] [blame] | 1522 | BFlags |= getAccessFlag(BI.getAccessSpecifier(), RD); |
Bob Haarman | dff3673 | 2016-10-25 22:19:32 +0000 | [diff] [blame] | 1523 | llvm::DIType *DTy = |
| 1524 | DBuilder.createInheritance(RecordTy, BaseTy, BaseOffset, BFlags); |
Guy Benyei | 11169dd | 2012-12-18 14:30:41 +0000 | [diff] [blame] | 1525 | EltTys.push_back(DTy); |
| 1526 | } |
| 1527 | } |
| 1528 | |
Duncan P. N. Exon Smith | 9dd4e4e | 2015-04-29 16:40:08 +0000 | [diff] [blame] | 1529 | llvm::DINodeArray |
Eric Christopher | e7b87e5 | 2014-10-26 23:40:33 +0000 | [diff] [blame] | 1530 | CGDebugInfo::CollectTemplateParams(const TemplateParameterList *TPList, |
| 1531 | ArrayRef<TemplateArgument> TAList, |
Duncan P. N. Exon Smith | 9dd4e4e | 2015-04-29 16:40:08 +0000 | [diff] [blame] | 1532 | llvm::DIFile *Unit) { |
Duncan P. N. Exon Smith | fb49491 | 2014-12-09 18:39:32 +0000 | [diff] [blame] | 1533 | SmallVector<llvm::Metadata *, 16> TemplateParams; |
Guy Benyei | 11169dd | 2012-12-18 14:30:41 +0000 | [diff] [blame] | 1534 | for (unsigned i = 0, e = TAList.size(); i != e; ++i) { |
| 1535 | const TemplateArgument &TA = TAList[i]; |
David Blaikie | 47c1150 | 2013-06-22 18:59:18 +0000 | [diff] [blame] | 1536 | StringRef Name; |
| 1537 | if (TPList) |
| 1538 | Name = TPList->getParam(i)->getName(); |
David Blaikie | 38079fd | 2013-05-10 21:53:14 +0000 | [diff] [blame] | 1539 | switch (TA.getKind()) { |
| 1540 | case TemplateArgument::Type: { |
Duncan P. N. Exon Smith | 9dd4e4e | 2015-04-29 16:40:08 +0000 | [diff] [blame] | 1541 | llvm::DIType *TTy = getOrCreateType(TA.getAsType(), Unit); |
Duncan P. N. Exon Smith | dadc2b6 | 2015-04-21 18:43:54 +0000 | [diff] [blame] | 1542 | TemplateParams.push_back( |
| 1543 | DBuilder.createTemplateTypeParameter(TheCU, Name, TTy)); |
David Blaikie | 38079fd | 2013-05-10 21:53:14 +0000 | [diff] [blame] | 1544 | } break; |
| 1545 | case TemplateArgument::Integral: { |
Duncan P. N. Exon Smith | 9dd4e4e | 2015-04-29 16:40:08 +0000 | [diff] [blame] | 1546 | llvm::DIType *TTy = getOrCreateType(TA.getIntegralType(), Unit); |
Duncan P. N. Exon Smith | dadc2b6 | 2015-04-21 18:43:54 +0000 | [diff] [blame] | 1547 | TemplateParams.push_back(DBuilder.createTemplateValueParameter( |
| 1548 | TheCU, Name, TTy, |
| 1549 | llvm::ConstantInt::get(CGM.getLLVMContext(), TA.getAsIntegral()))); |
David Blaikie | 38079fd | 2013-05-10 21:53:14 +0000 | [diff] [blame] | 1550 | } break; |
| 1551 | case TemplateArgument::Declaration: { |
| 1552 | const ValueDecl *D = TA.getAsDecl(); |
David Blaikie | b5c7e6a | 2014-10-18 02:21:26 +0000 | [diff] [blame] | 1553 | QualType T = TA.getParamTypeForDecl().getDesugaredType(CGM.getContext()); |
Duncan P. N. Exon Smith | 9dd4e4e | 2015-04-29 16:40:08 +0000 | [diff] [blame] | 1554 | llvm::DIType *TTy = getOrCreateType(T, Unit); |
Duncan P. N. Exon Smith | fb49491 | 2014-12-09 18:39:32 +0000 | [diff] [blame] | 1555 | llvm::Constant *V = nullptr; |
David Blaikie | 1a83db4 | 2014-10-20 18:56:54 +0000 | [diff] [blame] | 1556 | const CXXMethodDecl *MD; |
David Blaikie | 38079fd | 2013-05-10 21:53:14 +0000 | [diff] [blame] | 1557 | // Variable pointer template parameters have a value that is the address |
| 1558 | // of the variable. |
David Blaikie | 952a9b1 | 2014-10-17 18:00:12 +0000 | [diff] [blame] | 1559 | if (const auto *VD = dyn_cast<VarDecl>(D)) |
David Blaikie | 38079fd | 2013-05-10 21:53:14 +0000 | [diff] [blame] | 1560 | V = CGM.GetAddrOfGlobalVar(VD); |
| 1561 | // Member function pointers have special support for building them, though |
| 1562 | // this is currently unsupported in LLVM CodeGen. |
David Blaikie | 1a83db4 | 2014-10-20 18:56:54 +0000 | [diff] [blame] | 1563 | else if ((MD = dyn_cast<CXXMethodDecl>(D)) && MD->isInstance()) |
David Majnemer | e2be95b | 2015-06-23 07:31:01 +0000 | [diff] [blame] | 1564 | V = CGM.getCXXABI().EmitMemberFunctionPointer(MD); |
David Blaikie | 952a9b1 | 2014-10-17 18:00:12 +0000 | [diff] [blame] | 1565 | else if (const auto *FD = dyn_cast<FunctionDecl>(D)) |
David Blaikie | d900f98 | 2013-05-13 06:57:50 +0000 | [diff] [blame] | 1566 | V = CGM.GetAddrOfFunction(FD); |
David Blaikie | 38079fd | 2013-05-10 21:53:14 +0000 | [diff] [blame] | 1567 | // Member data pointers have special handling too to compute the fixed |
| 1568 | // offset within the object. |
David Blaikie | 952a9b1 | 2014-10-17 18:00:12 +0000 | [diff] [blame] | 1569 | else if (const auto *MPT = dyn_cast<MemberPointerType>(T.getTypePtr())) { |
David Blaikie | 38079fd | 2013-05-10 21:53:14 +0000 | [diff] [blame] | 1570 | // These five lines (& possibly the above member function pointer |
| 1571 | // handling) might be able to be refactored to use similar code in |
| 1572 | // CodeGenModule::getMemberPointerConstant |
| 1573 | uint64_t fieldOffset = CGM.getContext().getFieldOffset(D); |
| 1574 | CharUnits chars = |
Eric Christopher | e7b87e5 | 2014-10-26 23:40:33 +0000 | [diff] [blame] | 1575 | CGM.getContext().toCharUnitsFromBits((int64_t)fieldOffset); |
David Blaikie | 952a9b1 | 2014-10-17 18:00:12 +0000 | [diff] [blame] | 1576 | V = CGM.getCXXABI().EmitMemberDataPointer(MPT, chars); |
David Blaikie | 38079fd | 2013-05-10 21:53:14 +0000 | [diff] [blame] | 1577 | } |
Duncan P. N. Exon Smith | dadc2b6 | 2015-04-21 18:43:54 +0000 | [diff] [blame] | 1578 | TemplateParams.push_back(DBuilder.createTemplateValueParameter( |
| 1579 | TheCU, Name, TTy, |
| 1580 | cast_or_null<llvm::Constant>(V->stripPointerCasts()))); |
David Blaikie | 38079fd | 2013-05-10 21:53:14 +0000 | [diff] [blame] | 1581 | } break; |
| 1582 | case TemplateArgument::NullPtr: { |
| 1583 | QualType T = TA.getNullPtrType(); |
Duncan P. N. Exon Smith | 9dd4e4e | 2015-04-29 16:40:08 +0000 | [diff] [blame] | 1584 | llvm::DIType *TTy = getOrCreateType(T, Unit); |
Duncan P. N. Exon Smith | fb49491 | 2014-12-09 18:39:32 +0000 | [diff] [blame] | 1585 | llvm::Constant *V = nullptr; |
David Blaikie | 38079fd | 2013-05-10 21:53:14 +0000 | [diff] [blame] | 1586 | // Special case member data pointer null values since they're actually -1 |
| 1587 | // instead of zero. |
David Majnemer | 58ed0f3 | 2016-07-17 00:39:12 +0000 | [diff] [blame] | 1588 | if (const auto *MPT = dyn_cast<MemberPointerType>(T.getTypePtr())) |
David Blaikie | 38079fd | 2013-05-10 21:53:14 +0000 | [diff] [blame] | 1589 | // But treat member function pointers as simple zero integers because |
| 1590 | // it's easier than having a special case in LLVM's CodeGen. If LLVM |
| 1591 | // CodeGen grows handling for values of non-null member function |
| 1592 | // pointers then perhaps we could remove this special case and rely on |
| 1593 | // EmitNullMemberPointer for member function pointers. |
| 1594 | if (MPT->isMemberDataPointer()) |
| 1595 | V = CGM.getCXXABI().EmitNullMemberPointer(MPT); |
| 1596 | if (!V) |
| 1597 | V = llvm::ConstantInt::get(CGM.Int8Ty, 0); |
Duncan P. N. Exon Smith | dadc2b6 | 2015-04-21 18:43:54 +0000 | [diff] [blame] | 1598 | TemplateParams.push_back(DBuilder.createTemplateValueParameter( |
David Majnemer | 58ed0f3 | 2016-07-17 00:39:12 +0000 | [diff] [blame] | 1599 | TheCU, Name, TTy, V)); |
David Blaikie | 38079fd | 2013-05-10 21:53:14 +0000 | [diff] [blame] | 1600 | } break; |
Duncan P. N. Exon Smith | dadc2b6 | 2015-04-21 18:43:54 +0000 | [diff] [blame] | 1601 | case TemplateArgument::Template: |
| 1602 | TemplateParams.push_back(DBuilder.createTemplateTemplateParameter( |
Duncan P. N. Exon Smith | 0b6c369 | 2015-04-20 18:51:48 +0000 | [diff] [blame] | 1603 | TheCU, Name, nullptr, |
Duncan P. N. Exon Smith | dadc2b6 | 2015-04-21 18:43:54 +0000 | [diff] [blame] | 1604 | TA.getAsTemplate().getAsTemplateDecl()->getQualifiedNameAsString())); |
| 1605 | break; |
| 1606 | case TemplateArgument::Pack: |
| 1607 | TemplateParams.push_back(DBuilder.createTemplateParameterPack( |
| 1608 | TheCU, Name, nullptr, |
| 1609 | CollectTemplateParams(nullptr, TA.getPackAsArray(), Unit))); |
| 1610 | break; |
David Majnemer | 5559d47 | 2013-08-24 08:21:10 +0000 | [diff] [blame] | 1611 | case TemplateArgument::Expression: { |
| 1612 | const Expr *E = TA.getAsExpr(); |
| 1613 | QualType T = E->getType(); |
David Majnemer | 922ad9f | 2014-10-24 19:49:04 +0000 | [diff] [blame] | 1614 | if (E->isGLValue()) |
| 1615 | T = CGM.getContext().getLValueReferenceType(T); |
John McCall | de0fe07 | 2017-08-15 21:42:52 +0000 | [diff] [blame] | 1616 | llvm::Constant *V = ConstantEmitter(CGM).emitAbstract(E, T); |
David Majnemer | 5559d47 | 2013-08-24 08:21:10 +0000 | [diff] [blame] | 1617 | assert(V && "Expression in template argument isn't constant"); |
Duncan P. N. Exon Smith | 9dd4e4e | 2015-04-29 16:40:08 +0000 | [diff] [blame] | 1618 | llvm::DIType *TTy = getOrCreateType(T, Unit); |
Duncan P. N. Exon Smith | dadc2b6 | 2015-04-21 18:43:54 +0000 | [diff] [blame] | 1619 | TemplateParams.push_back(DBuilder.createTemplateValueParameter( |
David Majnemer | 58ed0f3 | 2016-07-17 00:39:12 +0000 | [diff] [blame] | 1620 | TheCU, Name, TTy, V->stripPointerCasts())); |
David Majnemer | 5559d47 | 2013-08-24 08:21:10 +0000 | [diff] [blame] | 1621 | } break; |
David Blaikie | 2b93c54 | 2013-05-10 23:36:06 +0000 | [diff] [blame] | 1622 | // And the following should never occur: |
David Blaikie | 38079fd | 2013-05-10 21:53:14 +0000 | [diff] [blame] | 1623 | case TemplateArgument::TemplateExpansion: |
David Blaikie | 38079fd | 2013-05-10 21:53:14 +0000 | [diff] [blame] | 1624 | case TemplateArgument::Null: |
| 1625 | llvm_unreachable( |
| 1626 | "These argument types shouldn't exist in concrete types"); |
Guy Benyei | 11169dd | 2012-12-18 14:30:41 +0000 | [diff] [blame] | 1627 | } |
| 1628 | } |
| 1629 | return DBuilder.getOrCreateArray(TemplateParams); |
| 1630 | } |
| 1631 | |
Duncan P. N. Exon Smith | 9dd4e4e | 2015-04-29 16:40:08 +0000 | [diff] [blame] | 1632 | llvm::DINodeArray |
Duncan P. N. Exon Smith | 8e47da4 | 2015-04-21 20:07:29 +0000 | [diff] [blame] | 1633 | CGDebugInfo::CollectFunctionTemplateParams(const FunctionDecl *FD, |
Duncan P. N. Exon Smith | 9dd4e4e | 2015-04-29 16:40:08 +0000 | [diff] [blame] | 1634 | llvm::DIFile *Unit) { |
Guy Benyei | 11169dd | 2012-12-18 14:30:41 +0000 | [diff] [blame] | 1635 | if (FD->getTemplatedKind() == |
| 1636 | FunctionDecl::TK_FunctionTemplateSpecialization) { |
Eric Christopher | e7b87e5 | 2014-10-26 23:40:33 +0000 | [diff] [blame] | 1637 | const TemplateParameterList *TList = FD->getTemplateSpecializationInfo() |
| 1638 | ->getTemplate() |
| 1639 | ->getTemplateParameters(); |
David Blaikie | 47c1150 | 2013-06-22 18:59:18 +0000 | [diff] [blame] | 1640 | return CollectTemplateParams( |
| 1641 | TList, FD->getTemplateSpecializationArgs()->asArray(), Unit); |
Guy Benyei | 11169dd | 2012-12-18 14:30:41 +0000 | [diff] [blame] | 1642 | } |
Duncan P. N. Exon Smith | 9dd4e4e | 2015-04-29 16:40:08 +0000 | [diff] [blame] | 1643 | return llvm::DINodeArray(); |
Guy Benyei | 11169dd | 2012-12-18 14:30:41 +0000 | [diff] [blame] | 1644 | } |
| 1645 | |
Duncan P. N. Exon Smith | 9dd4e4e | 2015-04-29 16:40:08 +0000 | [diff] [blame] | 1646 | llvm::DINodeArray CGDebugInfo::CollectCXXTemplateParams( |
| 1647 | const ClassTemplateSpecializationDecl *TSpecial, llvm::DIFile *Unit) { |
Adrian Prantl | 649f030 | 2014-04-17 01:04:01 +0000 | [diff] [blame] | 1648 | // Always get the full list of parameters, not just the ones from |
| 1649 | // the specialization. |
| 1650 | TemplateParameterList *TPList = |
Eric Christopher | e7b87e5 | 2014-10-26 23:40:33 +0000 | [diff] [blame] | 1651 | TSpecial->getSpecializedTemplate()->getTemplateParameters(); |
Adrian Prantl | 2c92e9c | 2014-04-17 00:30:48 +0000 | [diff] [blame] | 1652 | const TemplateArgumentList &TAList = TSpecial->getTemplateArgs(); |
David Blaikie | 47c1150 | 2013-06-22 18:59:18 +0000 | [diff] [blame] | 1653 | return CollectTemplateParams(TPList, TAList.asArray(), Unit); |
Guy Benyei | 11169dd | 2012-12-18 14:30:41 +0000 | [diff] [blame] | 1654 | } |
| 1655 | |
Duncan P. N. Exon Smith | 9dd4e4e | 2015-04-29 16:40:08 +0000 | [diff] [blame] | 1656 | llvm::DIType *CGDebugInfo::getOrCreateVTablePtrType(llvm::DIFile *Unit) { |
Duncan P. N. Exon Smith | b747023 | 2015-04-15 23:48:50 +0000 | [diff] [blame] | 1657 | if (VTablePtrType) |
Guy Benyei | 11169dd | 2012-12-18 14:30:41 +0000 | [diff] [blame] | 1658 | return VTablePtrType; |
| 1659 | |
| 1660 | ASTContext &Context = CGM.getContext(); |
| 1661 | |
| 1662 | /* Function type */ |
Duncan P. N. Exon Smith | fb49491 | 2014-12-09 18:39:32 +0000 | [diff] [blame] | 1663 | llvm::Metadata *STy = getOrCreateType(Context.IntTy, Unit); |
Duncan P. N. Exon Smith | 9dd4e4e | 2015-04-29 16:40:08 +0000 | [diff] [blame] | 1664 | llvm::DITypeRefArray SElements = DBuilder.getOrCreateTypeArray(STy); |
Eric Christopher | 28a6db5 | 2015-10-15 06:56:08 +0000 | [diff] [blame] | 1665 | llvm::DIType *SubTy = DBuilder.createSubroutineType(SElements); |
Guy Benyei | 11169dd | 2012-12-18 14:30:41 +0000 | [diff] [blame] | 1666 | unsigned Size = Context.getTypeSize(Context.VoidPtrTy); |
Konstantin Zhuravlyov | d1ba16e | 2017-03-08 23:56:48 +0000 | [diff] [blame] | 1667 | unsigned VtblPtrAddressSpace = CGM.getTarget().getVtblPtrAddressSpace(); |
| 1668 | Optional<unsigned> DWARFAddressSpace = |
| 1669 | CGM.getTarget().getDWARFAddressSpace(VtblPtrAddressSpace); |
| 1670 | |
Duncan P. N. Exon Smith | 9dd4e4e | 2015-04-29 16:40:08 +0000 | [diff] [blame] | 1671 | llvm::DIType *vtbl_ptr_type = |
Konstantin Zhuravlyov | d1ba16e | 2017-03-08 23:56:48 +0000 | [diff] [blame] | 1672 | DBuilder.createPointerType(SubTy, Size, 0, DWARFAddressSpace, |
| 1673 | "__vtbl_ptr_type"); |
Guy Benyei | 11169dd | 2012-12-18 14:30:41 +0000 | [diff] [blame] | 1674 | VTablePtrType = DBuilder.createPointerType(vtbl_ptr_type, Size); |
| 1675 | return VTablePtrType; |
| 1676 | } |
| 1677 | |
Guy Benyei | 11169dd | 2012-12-18 14:30:41 +0000 | [diff] [blame] | 1678 | StringRef CGDebugInfo::getVTableName(const CXXRecordDecl *RD) { |
Benjamin Kramer | 1b18a5e | 2013-09-09 16:39:06 +0000 | [diff] [blame] | 1679 | // Copy the gdb compatible name on the side and use its reference. |
| 1680 | return internString("_vptr$", RD->getNameAsString()); |
Guy Benyei | 11169dd | 2012-12-18 14:30:41 +0000 | [diff] [blame] | 1681 | } |
| 1682 | |
Duncan P. N. Exon Smith | 9dd4e4e | 2015-04-29 16:40:08 +0000 | [diff] [blame] | 1683 | void CGDebugInfo::CollectVTableInfo(const CXXRecordDecl *RD, llvm::DIFile *Unit, |
Reid Kleckner | dc12499 | 2016-08-31 16:11:43 +0000 | [diff] [blame] | 1684 | SmallVectorImpl<llvm::Metadata *> &EltTys, |
| 1685 | llvm::DICompositeType *RecordTy) { |
Guy Benyei | 11169dd | 2012-12-18 14:30:41 +0000 | [diff] [blame] | 1686 | // If this class is not dynamic then there is not any vtable info to collect. |
| 1687 | if (!RD->isDynamicClass()) |
| 1688 | return; |
| 1689 | |
Reid Kleckner | 5981242 | 2016-08-31 20:35:01 +0000 | [diff] [blame] | 1690 | // Don't emit any vtable shape or vptr info if this class doesn't have an |
| 1691 | // extendable vfptr. This can happen if the class doesn't have virtual |
| 1692 | // methods, or in the MS ABI if those virtual methods only come from virtually |
| 1693 | // inherited bases. |
| 1694 | const ASTRecordLayout &RL = CGM.getContext().getASTRecordLayout(RD); |
| 1695 | if (!RL.hasExtendableVFPtr()) |
| 1696 | return; |
| 1697 | |
Reid Kleckner | dc12499 | 2016-08-31 16:11:43 +0000 | [diff] [blame] | 1698 | // CodeView needs to know how large the vtable of every dynamic class is, so |
| 1699 | // emit a special named pointer type into the element list. The vptr type |
| 1700 | // points to this type as well. |
| 1701 | llvm::DIType *VPtrTy = nullptr; |
| 1702 | bool NeedVTableShape = CGM.getCodeGenOpts().EmitCodeView && |
| 1703 | CGM.getTarget().getCXXABI().isMicrosoft(); |
| 1704 | if (NeedVTableShape) { |
| 1705 | uint64_t PtrWidth = |
| 1706 | CGM.getContext().getTypeSize(CGM.getContext().VoidPtrTy); |
| 1707 | const VTableLayout &VFTLayout = |
| 1708 | CGM.getMicrosoftVTableContext().getVFTableLayout(RD, CharUnits::Zero()); |
| 1709 | unsigned VSlotCount = |
Peter Collingbourne | e53683f | 2016-09-08 01:14:39 +0000 | [diff] [blame] | 1710 | VFTLayout.vtable_components().size() - CGM.getLangOpts().RTTIData; |
Reid Kleckner | dc12499 | 2016-08-31 16:11:43 +0000 | [diff] [blame] | 1711 | unsigned VTableWidth = PtrWidth * VSlotCount; |
Konstantin Zhuravlyov | d1ba16e | 2017-03-08 23:56:48 +0000 | [diff] [blame] | 1712 | unsigned VtblPtrAddressSpace = CGM.getTarget().getVtblPtrAddressSpace(); |
| 1713 | Optional<unsigned> DWARFAddressSpace = |
| 1714 | CGM.getTarget().getDWARFAddressSpace(VtblPtrAddressSpace); |
Reid Kleckner | dc12499 | 2016-08-31 16:11:43 +0000 | [diff] [blame] | 1715 | |
| 1716 | // Create a very wide void* type and insert it directly in the element list. |
| 1717 | llvm::DIType *VTableType = |
Konstantin Zhuravlyov | d1ba16e | 2017-03-08 23:56:48 +0000 | [diff] [blame] | 1718 | DBuilder.createPointerType(nullptr, VTableWidth, 0, DWARFAddressSpace, |
| 1719 | "__vtbl_ptr_type"); |
Reid Kleckner | dc12499 | 2016-08-31 16:11:43 +0000 | [diff] [blame] | 1720 | EltTys.push_back(VTableType); |
| 1721 | |
| 1722 | // The vptr is a pointer to this special vtable type. |
| 1723 | VPtrTy = DBuilder.createPointerType(VTableType, PtrWidth); |
| 1724 | } |
| 1725 | |
| 1726 | // 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] | 1727 | if (RL.getPrimaryBase()) |
| 1728 | return; |
| 1729 | |
| 1730 | if (!VPtrTy) |
| 1731 | VPtrTy = getOrCreateVTablePtrType(Unit); |
| 1732 | |
Guy Benyei | 11169dd | 2012-12-18 14:30:41 +0000 | [diff] [blame] | 1733 | unsigned Size = CGM.getContext().getTypeSize(CGM.getContext().VoidPtrTy); |
Reid Kleckner | dc12499 | 2016-08-31 16:11:43 +0000 | [diff] [blame] | 1734 | llvm::DIType *VPtrMember = DBuilder.createMemberType( |
Eric Christopher | e7b87e5 | 2014-10-26 23:40:33 +0000 | [diff] [blame] | 1735 | Unit, getVTableName(RD), Unit, 0, Size, 0, 0, |
Reid Kleckner | dc12499 | 2016-08-31 16:11:43 +0000 | [diff] [blame] | 1736 | llvm::DINode::FlagArtificial, VPtrTy); |
| 1737 | EltTys.push_back(VPtrMember); |
Guy Benyei | 11169dd | 2012-12-18 14:30:41 +0000 | [diff] [blame] | 1738 | } |
| 1739 | |
Duncan P. N. Exon Smith | 9dd4e4e | 2015-04-29 16:40:08 +0000 | [diff] [blame] | 1740 | llvm::DIType *CGDebugInfo::getOrCreateRecordType(QualType RTy, |
Duncan P. N. Exon Smith | 0b6c369 | 2015-04-20 18:51:48 +0000 | [diff] [blame] | 1741 | SourceLocation Loc) { |
Benjamin Kramer | 8c30592 | 2016-02-02 11:06:51 +0000 | [diff] [blame] | 1742 | assert(DebugKind >= codegenoptions::LimitedDebugInfo); |
Duncan P. N. Exon Smith | 9dd4e4e | 2015-04-29 16:40:08 +0000 | [diff] [blame] | 1743 | llvm::DIType *T = getOrCreateType(RTy, getOrCreateFile(Loc)); |
Guy Benyei | 11169dd | 2012-12-18 14:30:41 +0000 | [diff] [blame] | 1744 | return T; |
| 1745 | } |
| 1746 | |
Duncan P. N. Exon Smith | 9dd4e4e | 2015-04-29 16:40:08 +0000 | [diff] [blame] | 1747 | llvm::DIType *CGDebugInfo::getOrCreateInterfaceType(QualType D, |
Duncan P. N. Exon Smith | 0b6c369 | 2015-04-20 18:51:48 +0000 | [diff] [blame] | 1748 | SourceLocation Loc) { |
Adrian Prantl | ad9a195e | 2015-08-27 21:21:19 +0000 | [diff] [blame] | 1749 | return getOrCreateStandaloneType(D, Loc); |
| 1750 | } |
| 1751 | |
| 1752 | llvm::DIType *CGDebugInfo::getOrCreateStandaloneType(QualType D, |
| 1753 | SourceLocation Loc) { |
Benjamin Kramer | 8c30592 | 2016-02-02 11:06:51 +0000 | [diff] [blame] | 1754 | assert(DebugKind >= codegenoptions::LimitedDebugInfo); |
Adrian Prantl | ad9a195e | 2015-08-27 21:21:19 +0000 | [diff] [blame] | 1755 | assert(!D.isNull() && "null type"); |
Duncan P. N. Exon Smith | 9dd4e4e | 2015-04-29 16:40:08 +0000 | [diff] [blame] | 1756 | llvm::DIType *T = getOrCreateType(D, getOrCreateFile(Loc)); |
Adrian Prantl | ad9a195e | 2015-08-27 21:21:19 +0000 | [diff] [blame] | 1757 | assert(T && "could not create debug info for type"); |
Adrian Prantl | 3a884fa | 2015-08-27 22:56:46 +0000 | [diff] [blame] | 1758 | |
Adrian Prantl | 73409ce | 2013-03-11 18:33:46 +0000 | [diff] [blame] | 1759 | RetainedTypes.push_back(D.getAsOpaquePtr()); |
Guy Benyei | 11169dd | 2012-12-18 14:30:41 +0000 | [diff] [blame] | 1760 | return T; |
| 1761 | } |
| 1762 | |
David Blaikie | 483a9da | 2014-05-06 18:35:21 +0000 | [diff] [blame] | 1763 | void CGDebugInfo::completeType(const EnumDecl *ED) { |
Benjamin Kramer | 8c30592 | 2016-02-02 11:06:51 +0000 | [diff] [blame] | 1764 | if (DebugKind <= codegenoptions::DebugLineTablesOnly) |
David Blaikie | 483a9da | 2014-05-06 18:35:21 +0000 | [diff] [blame] | 1765 | return; |
| 1766 | QualType Ty = CGM.getContext().getEnumType(ED); |
Eric Christopher | e7b87e5 | 2014-10-26 23:40:33 +0000 | [diff] [blame] | 1767 | void *TyPtr = Ty.getAsOpaquePtr(); |
David Blaikie | 483a9da | 2014-05-06 18:35:21 +0000 | [diff] [blame] | 1768 | auto I = TypeCache.find(TyPtr); |
Duncan P. N. Exon Smith | 9dd4e4e | 2015-04-29 16:40:08 +0000 | [diff] [blame] | 1769 | if (I == TypeCache.end() || !cast<llvm::DIType>(I->second)->isForwardDecl()) |
David Blaikie | 483a9da | 2014-05-06 18:35:21 +0000 | [diff] [blame] | 1770 | return; |
Duncan P. N. Exon Smith | 9dd4e4e | 2015-04-29 16:40:08 +0000 | [diff] [blame] | 1771 | llvm::DIType *Res = CreateTypeDefinition(Ty->castAs<EnumType>()); |
Duncan P. N. Exon Smith | 4caa7f2 | 2015-04-16 01:00:56 +0000 | [diff] [blame] | 1772 | assert(!Res->isForwardDecl()); |
Duncan P. N. Exon Smith | fb49491 | 2014-12-09 18:39:32 +0000 | [diff] [blame] | 1773 | TypeCache[TyPtr].reset(Res); |
David Blaikie | 483a9da | 2014-05-06 18:35:21 +0000 | [diff] [blame] | 1774 | } |
| 1775 | |
David Blaikie | b2e86eb | 2013-08-15 20:49:17 +0000 | [diff] [blame] | 1776 | void CGDebugInfo::completeType(const RecordDecl *RD) { |
Benjamin Kramer | 8c30592 | 2016-02-02 11:06:51 +0000 | [diff] [blame] | 1777 | if (DebugKind > codegenoptions::LimitedDebugInfo || |
David Blaikie | b2e86eb | 2013-08-15 20:49:17 +0000 | [diff] [blame] | 1778 | !CGM.getLangOpts().CPlusPlus) |
| 1779 | completeRequiredType(RD); |
| 1780 | } |
| 1781 | |
David Blaikie | b11c873 | 2017-01-30 06:36:08 +0000 | [diff] [blame] | 1782 | /// Return true if the class or any of its methods are marked dllimport. |
| 1783 | static bool isClassOrMethodDLLImport(const CXXRecordDecl *RD) { |
| 1784 | if (RD->hasAttr<DLLImportAttr>()) |
| 1785 | return true; |
| 1786 | for (const CXXMethodDecl *MD : RD->methods()) |
| 1787 | if (MD->hasAttr<DLLImportAttr>()) |
| 1788 | return true; |
| 1789 | return false; |
| 1790 | } |
| 1791 | |
Adrian Prantl | a43acdc | 2017-07-24 23:48:51 +0000 | [diff] [blame] | 1792 | /// Does a type definition exist in an imported clang module? |
| 1793 | static bool isDefinedInClangModule(const RecordDecl *RD) { |
| 1794 | // Only definitions that where imported from an AST file come from a module. |
| 1795 | if (!RD || !RD->isFromASTFile()) |
| 1796 | return false; |
| 1797 | // Anonymous entities cannot be addressed. Treat them as not from module. |
| 1798 | if (!RD->isExternallyVisible() && RD->getName().empty()) |
| 1799 | return false; |
| 1800 | if (auto *CXXDecl = dyn_cast<CXXRecordDecl>(RD)) { |
| 1801 | if (!CXXDecl->isCompleteDefinition()) |
| 1802 | return false; |
| 1803 | auto TemplateKind = CXXDecl->getTemplateSpecializationKind(); |
| 1804 | if (TemplateKind != TSK_Undeclared) { |
| 1805 | // This is a template, check the origin of the first member. |
| 1806 | if (CXXDecl->field_begin() == CXXDecl->field_end()) |
| 1807 | return TemplateKind == TSK_ExplicitInstantiationDeclaration; |
| 1808 | if (!CXXDecl->field_begin()->isFromASTFile()) |
| 1809 | return false; |
| 1810 | } |
| 1811 | } |
| 1812 | return true; |
| 1813 | } |
| 1814 | |
David Blaikie | 6943dea | 2013-08-20 01:28:15 +0000 | [diff] [blame] | 1815 | void CGDebugInfo::completeClassData(const RecordDecl *RD) { |
David Blaikie | b11c873 | 2017-01-30 06:36:08 +0000 | [diff] [blame] | 1816 | if (auto *CXXRD = dyn_cast<CXXRecordDecl>(RD)) |
| 1817 | if (CXXRD->isDynamicClass() && |
| 1818 | CGM.getVTableLinkage(CXXRD) == |
| 1819 | llvm::GlobalValue::AvailableExternallyLinkage && |
| 1820 | !isClassOrMethodDLLImport(CXXRD)) |
| 1821 | return; |
Adrian Prantl | a43acdc | 2017-07-24 23:48:51 +0000 | [diff] [blame] | 1822 | |
| 1823 | if (DebugTypeExtRefs && isDefinedInClangModule(RD->getDefinition())) |
| 1824 | return; |
| 1825 | |
David Blaikie | b11c873 | 2017-01-30 06:36:08 +0000 | [diff] [blame] | 1826 | completeClass(RD); |
| 1827 | } |
| 1828 | |
| 1829 | void CGDebugInfo::completeClass(const RecordDecl *RD) { |
Benjamin Kramer | 8c30592 | 2016-02-02 11:06:51 +0000 | [diff] [blame] | 1830 | if (DebugKind <= codegenoptions::DebugLineTablesOnly) |
Michael Gottesman | 349542b | 2013-08-19 18:46:16 +0000 | [diff] [blame] | 1831 | return; |
David Blaikie | 6943dea | 2013-08-20 01:28:15 +0000 | [diff] [blame] | 1832 | QualType Ty = CGM.getContext().getRecordType(RD); |
Eric Christopher | e7b87e5 | 2014-10-26 23:40:33 +0000 | [diff] [blame] | 1833 | void *TyPtr = Ty.getAsOpaquePtr(); |
David Blaikie | ef8a951 | 2014-05-05 23:23:53 +0000 | [diff] [blame] | 1834 | auto I = TypeCache.find(TyPtr); |
Duncan P. N. Exon Smith | 9dd4e4e | 2015-04-29 16:40:08 +0000 | [diff] [blame] | 1835 | if (I != TypeCache.end() && !cast<llvm::DIType>(I->second)->isForwardDecl()) |
David Blaikie | b2e86eb | 2013-08-15 20:49:17 +0000 | [diff] [blame] | 1836 | return; |
Duncan P. N. Exon Smith | 9dd4e4e | 2015-04-29 16:40:08 +0000 | [diff] [blame] | 1837 | llvm::DIType *Res = CreateTypeDefinition(Ty->castAs<RecordType>()); |
Duncan P. N. Exon Smith | 4caa7f2 | 2015-04-16 01:00:56 +0000 | [diff] [blame] | 1838 | assert(!Res->isForwardDecl()); |
Duncan P. N. Exon Smith | fb49491 | 2014-12-09 18:39:32 +0000 | [diff] [blame] | 1839 | TypeCache[TyPtr].reset(Res); |
David Blaikie | b2e86eb | 2013-08-15 20:49:17 +0000 | [diff] [blame] | 1840 | } |
| 1841 | |
David Blaikie | 0e716b4 | 2014-03-03 23:48:23 +0000 | [diff] [blame] | 1842 | static bool hasExplicitMemberDefinition(CXXRecordDecl::method_iterator I, |
| 1843 | CXXRecordDecl::method_iterator End) { |
David Majnemer | 58ed0f3 | 2016-07-17 00:39:12 +0000 | [diff] [blame] | 1844 | for (CXXMethodDecl *MD : llvm::make_range(I, End)) |
| 1845 | if (FunctionDecl *Tmpl = MD->getInstantiatedFromMemberFunction()) |
David Blaikie | f7f2185 | 2014-03-04 03:08:14 +0000 | [diff] [blame] | 1846 | if (!Tmpl->isImplicit() && Tmpl->isThisDeclarationADefinition() && |
David Majnemer | 58ed0f3 | 2016-07-17 00:39:12 +0000 | [diff] [blame] | 1847 | !MD->getMemberSpecializationInfo()->isExplicitSpecialization()) |
David Blaikie | 0e716b4 | 2014-03-03 23:48:23 +0000 | [diff] [blame] | 1848 | return true; |
| 1849 | return false; |
| 1850 | } |
| 1851 | |
Benjamin Kramer | 8c30592 | 2016-02-02 11:06:51 +0000 | [diff] [blame] | 1852 | static bool shouldOmitDefinition(codegenoptions::DebugInfoKind DebugKind, |
| 1853 | bool DebugTypeExtRefs, const RecordDecl *RD, |
David Blaikie | 0e716b4 | 2014-03-03 23:48:23 +0000 | [diff] [blame] | 1854 | const LangOptions &LangOpts) { |
Adrian Prantl | 88d7917 | 2016-04-26 21:58:18 +0000 | [diff] [blame] | 1855 | if (DebugTypeExtRefs && isDefinedInClangModule(RD->getDefinition())) |
Adrian Prantl | 43e0081 | 2016-01-19 23:42:53 +0000 | [diff] [blame] | 1856 | return true; |
Adrian Prantl | 5c8bd88 | 2015-09-11 17:23:08 +0000 | [diff] [blame] | 1857 | |
David Blaikie | 1ac9c98 | 2017-04-11 21:13:37 +0000 | [diff] [blame] | 1858 | if (auto *ES = RD->getASTContext().getExternalSource()) |
| 1859 | if (ES->hasExternalDefinitions(RD) == ExternalASTSource::EK_Always) |
| 1860 | return true; |
| 1861 | |
Benjamin Kramer | 8c30592 | 2016-02-02 11:06:51 +0000 | [diff] [blame] | 1862 | if (DebugKind > codegenoptions::LimitedDebugInfo) |
David Blaikie | 0e716b4 | 2014-03-03 23:48:23 +0000 | [diff] [blame] | 1863 | return false; |
| 1864 | |
| 1865 | if (!LangOpts.CPlusPlus) |
| 1866 | return false; |
| 1867 | |
| 1868 | if (!RD->isCompleteDefinitionRequired()) |
| 1869 | return true; |
| 1870 | |
David Majnemer | 58ed0f3 | 2016-07-17 00:39:12 +0000 | [diff] [blame] | 1871 | const auto *CXXDecl = dyn_cast<CXXRecordDecl>(RD); |
David Blaikie | 0e716b4 | 2014-03-03 23:48:23 +0000 | [diff] [blame] | 1872 | |
| 1873 | if (!CXXDecl) |
| 1874 | return false; |
| 1875 | |
Adrian McCarthy | 9924298 | 2016-08-16 22:11:18 +0000 | [diff] [blame] | 1876 | // Only emit complete debug info for a dynamic class when its vtable is |
| 1877 | // emitted. However, Microsoft debuggers don't resolve type information |
Reid Kleckner | c9404e1 | 2016-09-09 16:27:04 +0000 | [diff] [blame] | 1878 | // across DLL boundaries, so skip this optimization if the class or any of its |
| 1879 | // methods are marked dllimport. This isn't a complete solution, since objects |
| 1880 | // without any dllimport methods can be used in one DLL and constructed in |
| 1881 | // another, but it is the current behavior of LimitedDebugInfo. |
Adrian McCarthy | 9924298 | 2016-08-16 22:11:18 +0000 | [diff] [blame] | 1882 | if (CXXDecl->hasDefinition() && CXXDecl->isDynamicClass() && |
Reid Kleckner | c9404e1 | 2016-09-09 16:27:04 +0000 | [diff] [blame] | 1883 | !isClassOrMethodDLLImport(CXXDecl)) |
David Blaikie | 0e716b4 | 2014-03-03 23:48:23 +0000 | [diff] [blame] | 1884 | return true; |
| 1885 | |
| 1886 | TemplateSpecializationKind Spec = TSK_Undeclared; |
David Majnemer | 58ed0f3 | 2016-07-17 00:39:12 +0000 | [diff] [blame] | 1887 | if (const auto *SD = dyn_cast<ClassTemplateSpecializationDecl>(RD)) |
David Blaikie | 0e716b4 | 2014-03-03 23:48:23 +0000 | [diff] [blame] | 1888 | Spec = SD->getSpecializationKind(); |
| 1889 | |
| 1890 | if (Spec == TSK_ExplicitInstantiationDeclaration && |
| 1891 | hasExplicitMemberDefinition(CXXDecl->method_begin(), |
| 1892 | CXXDecl->method_end())) |
| 1893 | return true; |
| 1894 | |
| 1895 | return false; |
| 1896 | } |
| 1897 | |
Reid Kleckner | 6c7b1c6 | 2016-09-13 00:01:23 +0000 | [diff] [blame] | 1898 | void CGDebugInfo::completeRequiredType(const RecordDecl *RD) { |
| 1899 | if (shouldOmitDefinition(DebugKind, DebugTypeExtRefs, RD, CGM.getLangOpts())) |
| 1900 | return; |
| 1901 | |
| 1902 | QualType Ty = CGM.getContext().getRecordType(RD); |
| 1903 | llvm::DIType *T = getTypeOrNull(Ty); |
| 1904 | if (T && T->isForwardDecl()) |
| 1905 | completeClassData(RD); |
| 1906 | } |
| 1907 | |
Duncan P. N. Exon Smith | 9dd4e4e | 2015-04-29 16:40:08 +0000 | [diff] [blame] | 1908 | llvm::DIType *CGDebugInfo::CreateType(const RecordType *Ty) { |
Guy Benyei | 11169dd | 2012-12-18 14:30:41 +0000 | [diff] [blame] | 1909 | RecordDecl *RD = Ty->getDecl(); |
Duncan P. N. Exon Smith | 9dd4e4e | 2015-04-29 16:40:08 +0000 | [diff] [blame] | 1910 | llvm::DIType *T = cast_or_null<llvm::DIType>(getTypeOrNull(QualType(Ty, 0))); |
Adrian Prantl | 5c8bd88 | 2015-09-11 17:23:08 +0000 | [diff] [blame] | 1911 | if (T || shouldOmitDefinition(DebugKind, DebugTypeExtRefs, RD, |
| 1912 | CGM.getLangOpts())) { |
David Blaikie | 3b1cc9b | 2013-09-06 06:45:04 +0000 | [diff] [blame] | 1913 | if (!T) |
Adrian Prantl | 6ec370a | 2015-09-10 18:39:45 +0000 | [diff] [blame] | 1914 | T = getOrCreateRecordFwdDecl(Ty, getDeclContextDescriptor(RD)); |
David Blaikie | 3b1cc9b | 2013-09-06 06:45:04 +0000 | [diff] [blame] | 1915 | return T; |
David Blaikie | e36464c | 2013-06-05 05:32:23 +0000 | [diff] [blame] | 1916 | } |
Guy Benyei | 11169dd | 2012-12-18 14:30:41 +0000 | [diff] [blame] | 1917 | |
David Blaikie | b2e86eb | 2013-08-15 20:49:17 +0000 | [diff] [blame] | 1918 | return CreateTypeDefinition(Ty); |
| 1919 | } |
| 1920 | |
Duncan P. N. Exon Smith | 9dd4e4e | 2015-04-29 16:40:08 +0000 | [diff] [blame] | 1921 | llvm::DIType *CGDebugInfo::CreateTypeDefinition(const RecordType *Ty) { |
David Blaikie | b2e86eb | 2013-08-15 20:49:17 +0000 | [diff] [blame] | 1922 | RecordDecl *RD = Ty->getDecl(); |
| 1923 | |
Guy Benyei | 11169dd | 2012-12-18 14:30:41 +0000 | [diff] [blame] | 1924 | // 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] | 1925 | llvm::DIFile *DefUnit = getOrCreateFile(RD->getLocation()); |
Guy Benyei | 11169dd | 2012-12-18 14:30:41 +0000 | [diff] [blame] | 1926 | |
| 1927 | // Records and classes and unions can all be recursive. To handle them, we |
| 1928 | // first generate a debug descriptor for the struct as a forward declaration. |
| 1929 | // Then (if it is a definition) we go through and get debug info for all of |
| 1930 | // its members. Finally, we create a descriptor for the complete type (which |
| 1931 | // may refer to the forward decl if the struct is recursive) and replace all |
| 1932 | // uses of the forward declaration with the final definition. |
Duncan P. N. Exon Smith | bd210e6 | 2015-07-24 20:34:41 +0000 | [diff] [blame] | 1933 | llvm::DICompositeType *FwdDecl = getOrCreateLimitedType(Ty, DefUnit); |
Guy Benyei | 11169dd | 2012-12-18 14:30:41 +0000 | [diff] [blame] | 1934 | |
Adrian Prantl | 5f66bae | 2015-02-11 17:45:15 +0000 | [diff] [blame] | 1935 | const RecordDecl *D = RD->getDefinition(); |
| 1936 | if (!D || !D->isCompleteDefinition()) |
Guy Benyei | 11169dd | 2012-12-18 14:30:41 +0000 | [diff] [blame] | 1937 | return FwdDecl; |
| 1938 | |
David Majnemer | 58ed0f3 | 2016-07-17 00:39:12 +0000 | [diff] [blame] | 1939 | if (const auto *CXXDecl = dyn_cast<CXXRecordDecl>(RD)) |
David Blaikie | adfbf99 | 2013-08-18 16:55:33 +0000 | [diff] [blame] | 1940 | CollectContainingType(CXXDecl, FwdDecl); |
| 1941 | |
Guy Benyei | 11169dd | 2012-12-18 14:30:41 +0000 | [diff] [blame] | 1942 | // Push the struct on region stack. |
Duncan P. N. Exon Smith | fb49491 | 2014-12-09 18:39:32 +0000 | [diff] [blame] | 1943 | LexicalBlockStack.emplace_back(&*FwdDecl); |
| 1944 | RegionMap[Ty->getDecl()].reset(FwdDecl); |
Guy Benyei | 11169dd | 2012-12-18 14:30:41 +0000 | [diff] [blame] | 1945 | |
Guy Benyei | 11169dd | 2012-12-18 14:30:41 +0000 | [diff] [blame] | 1946 | // Convert all the elements. |
Duncan P. N. Exon Smith | fb49491 | 2014-12-09 18:39:32 +0000 | [diff] [blame] | 1947 | SmallVector<llvm::Metadata *, 16> EltTys; |
David Blaikie | 6943dea | 2013-08-20 01:28:15 +0000 | [diff] [blame] | 1948 | // what about nested types? |
Guy Benyei | 11169dd | 2012-12-18 14:30:41 +0000 | [diff] [blame] | 1949 | |
| 1950 | // Note: The split of CXXDecl information here is intentional, the |
| 1951 | // gdb tests will depend on a certain ordering at printout. The debug |
| 1952 | // information offsets are still correct if we merge them all together |
| 1953 | // though. |
David Majnemer | 58ed0f3 | 2016-07-17 00:39:12 +0000 | [diff] [blame] | 1954 | const auto *CXXDecl = dyn_cast<CXXRecordDecl>(RD); |
Guy Benyei | 11169dd | 2012-12-18 14:30:41 +0000 | [diff] [blame] | 1955 | if (CXXDecl) { |
| 1956 | CollectCXXBases(CXXDecl, DefUnit, EltTys, FwdDecl); |
Reid Kleckner | dc12499 | 2016-08-31 16:11:43 +0000 | [diff] [blame] | 1957 | CollectVTableInfo(CXXDecl, DefUnit, EltTys, FwdDecl); |
Guy Benyei | 11169dd | 2012-12-18 14:30:41 +0000 | [diff] [blame] | 1958 | } |
| 1959 | |
Eric Christopher | 91a3190 | 2013-01-16 01:22:32 +0000 | [diff] [blame] | 1960 | // Collect data fields (including static variables and any initializers). |
Guy Benyei | 11169dd | 2012-12-18 14:30:41 +0000 | [diff] [blame] | 1961 | CollectRecordFields(RD, DefUnit, EltTys, FwdDecl); |
Eric Christopher | 2df080e | 2013-10-11 18:16:51 +0000 | [diff] [blame] | 1962 | if (CXXDecl) |
Guy Benyei | 11169dd | 2012-12-18 14:30:41 +0000 | [diff] [blame] | 1963 | CollectCXXMemberFunctions(CXXDecl, DefUnit, EltTys, FwdDecl); |
Guy Benyei | 11169dd | 2012-12-18 14:30:41 +0000 | [diff] [blame] | 1964 | |
| 1965 | LexicalBlockStack.pop_back(); |
| 1966 | RegionMap.erase(Ty->getDecl()); |
| 1967 | |
Duncan P. N. Exon Smith | 9dd4e4e | 2015-04-29 16:40:08 +0000 | [diff] [blame] | 1968 | llvm::DINodeArray Elements = DBuilder.getOrCreateArray(EltTys); |
Duncan P. N. Exon Smith | c8ee63e | 2014-12-18 00:48:56 +0000 | [diff] [blame] | 1969 | DBuilder.replaceArrays(FwdDecl, Elements); |
Guy Benyei | 11169dd | 2012-12-18 14:30:41 +0000 | [diff] [blame] | 1970 | |
Adrian Prantl | 5f66bae | 2015-02-11 17:45:15 +0000 | [diff] [blame] | 1971 | if (FwdDecl->isTemporary()) |
Duncan P. N. Exon Smith | 4078ad4 | 2015-04-16 16:36:45 +0000 | [diff] [blame] | 1972 | FwdDecl = |
Duncan P. N. Exon Smith | 9dd4e4e | 2015-04-29 16:40:08 +0000 | [diff] [blame] | 1973 | llvm::MDNode::replaceWithPermanent(llvm::TempDICompositeType(FwdDecl)); |
Adrian Prantl | 5f66bae | 2015-02-11 17:45:15 +0000 | [diff] [blame] | 1974 | |
Duncan P. N. Exon Smith | fb49491 | 2014-12-09 18:39:32 +0000 | [diff] [blame] | 1975 | RegionMap[Ty->getDecl()].reset(FwdDecl); |
Eric Christopher | 5c7ee8b | 2013-04-02 22:59:11 +0000 | [diff] [blame] | 1976 | return FwdDecl; |
Guy Benyei | 11169dd | 2012-12-18 14:30:41 +0000 | [diff] [blame] | 1977 | } |
| 1978 | |
Duncan P. N. Exon Smith | 9dd4e4e | 2015-04-29 16:40:08 +0000 | [diff] [blame] | 1979 | llvm::DIType *CGDebugInfo::CreateType(const ObjCObjectType *Ty, |
| 1980 | llvm::DIFile *Unit) { |
Guy Benyei | 11169dd | 2012-12-18 14:30:41 +0000 | [diff] [blame] | 1981 | // Ignore protocols. |
| 1982 | return getOrCreateType(Ty->getBaseType(), Unit); |
| 1983 | } |
| 1984 | |
Manman Ren | e6be26c | 2016-09-13 17:25:08 +0000 | [diff] [blame] | 1985 | llvm::DIType *CGDebugInfo::CreateType(const ObjCTypeParamType *Ty, |
| 1986 | llvm::DIFile *Unit) { |
| 1987 | // Ignore protocols. |
| 1988 | SourceLocation Loc = Ty->getDecl()->getLocation(); |
| 1989 | |
| 1990 | // Use Typedefs to represent ObjCTypeParamType. |
| 1991 | return DBuilder.createTypedef( |
| 1992 | getOrCreateType(Ty->getDecl()->getUnderlyingType(), Unit), |
| 1993 | Ty->getDecl()->getName(), getOrCreateFile(Loc), getLineNumber(Loc), |
| 1994 | getDeclContextDescriptor(Ty->getDecl())); |
| 1995 | } |
| 1996 | |
Adrian Prantl | b8fad1a | 2013-06-07 01:10:45 +0000 | [diff] [blame] | 1997 | /// \return true if Getter has the default name for the property PD. |
| 1998 | static bool hasDefaultGetterName(const ObjCPropertyDecl *PD, |
| 1999 | const ObjCMethodDecl *Getter) { |
| 2000 | assert(PD); |
| 2001 | if (!Getter) |
| 2002 | return true; |
| 2003 | |
| 2004 | assert(Getter->getDeclName().isObjCZeroArgSelector()); |
| 2005 | return PD->getName() == |
Eric Christopher | e7b87e5 | 2014-10-26 23:40:33 +0000 | [diff] [blame] | 2006 | Getter->getDeclName().getObjCSelector().getNameForSlot(0); |
Adrian Prantl | b8fad1a | 2013-06-07 01:10:45 +0000 | [diff] [blame] | 2007 | } |
| 2008 | |
| 2009 | /// \return true if Setter has the default name for the property PD. |
| 2010 | static bool hasDefaultSetterName(const ObjCPropertyDecl *PD, |
| 2011 | const ObjCMethodDecl *Setter) { |
| 2012 | assert(PD); |
| 2013 | if (!Setter) |
| 2014 | return true; |
| 2015 | |
| 2016 | assert(Setter->getDeclName().isObjCOneArgSelector()); |
Adrian Prantl | a4ce906 | 2013-06-07 22:29:12 +0000 | [diff] [blame] | 2017 | return SelectorTable::constructSetterName(PD->getName()) == |
Eric Christopher | e7b87e5 | 2014-10-26 23:40:33 +0000 | [diff] [blame] | 2018 | Setter->getDeclName().getObjCSelector().getNameForSlot(0); |
Adrian Prantl | b8fad1a | 2013-06-07 01:10:45 +0000 | [diff] [blame] | 2019 | } |
| 2020 | |
Duncan P. N. Exon Smith | 9dd4e4e | 2015-04-29 16:40:08 +0000 | [diff] [blame] | 2021 | llvm::DIType *CGDebugInfo::CreateType(const ObjCInterfaceType *Ty, |
| 2022 | llvm::DIFile *Unit) { |
Guy Benyei | 11169dd | 2012-12-18 14:30:41 +0000 | [diff] [blame] | 2023 | ObjCInterfaceDecl *ID = Ty->getDecl(); |
| 2024 | if (!ID) |
Duncan P. N. Exon Smith | 0b6c369 | 2015-04-20 18:51:48 +0000 | [diff] [blame] | 2025 | return nullptr; |
Guy Benyei | 11169dd | 2012-12-18 14:30:41 +0000 | [diff] [blame] | 2026 | |
Adrian Prantl | 50fd1a8 | 2016-04-20 23:59:32 +0000 | [diff] [blame] | 2027 | // Return a forward declaration if this type was imported from a clang module, |
| 2028 | // and this is not the compile unit with the implementation of the type (which |
| 2029 | // may contain hidden ivars). |
| 2030 | if (DebugTypeExtRefs && ID->isFromASTFile() && ID->getDefinition() && |
| 2031 | !ID->getImplementation()) |
Adrian Prantl | 5c8bd88 | 2015-09-11 17:23:08 +0000 | [diff] [blame] | 2032 | return DBuilder.createForwardDecl(llvm::dwarf::DW_TAG_structure_type, |
| 2033 | ID->getName(), |
| 2034 | getDeclContextDescriptor(ID), Unit, 0); |
| 2035 | |
Guy Benyei | 11169dd | 2012-12-18 14:30:41 +0000 | [diff] [blame] | 2036 | // 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] | 2037 | llvm::DIFile *DefUnit = getOrCreateFile(ID->getLocation()); |
Guy Benyei | 11169dd | 2012-12-18 14:30:41 +0000 | [diff] [blame] | 2038 | unsigned Line = getLineNumber(ID->getLocation()); |
Duncan P. N. Exon Smith | 798d565 | 2015-04-15 23:19:15 +0000 | [diff] [blame] | 2039 | auto RuntimeLang = |
| 2040 | static_cast<llvm::dwarf::SourceLanguage>(TheCU->getSourceLanguage()); |
Guy Benyei | 11169dd | 2012-12-18 14:30:41 +0000 | [diff] [blame] | 2041 | |
| 2042 | // If this is just a forward declaration return a special forward-declaration |
| 2043 | // debug type since we won't be able to lay out the entire type. |
| 2044 | ObjCInterfaceDecl *Def = ID->getDefinition(); |
David Blaikie | ef8a951 | 2014-05-05 23:23:53 +0000 | [diff] [blame] | 2045 | if (!Def || !Def->getImplementation()) { |
Adrian Prantl | 42ce2d3 | 2015-10-01 16:57:02 +0000 | [diff] [blame] | 2046 | llvm::DIScope *Mod = getParentModuleOrNull(ID); |
Duncan P. N. Exon Smith | 9dd4e4e | 2015-04-29 16:40:08 +0000 | [diff] [blame] | 2047 | llvm::DIType *FwdDecl = DBuilder.createReplaceableCompositeType( |
Adrian Prantl | 42ce2d3 | 2015-10-01 16:57:02 +0000 | [diff] [blame] | 2048 | llvm::dwarf::DW_TAG_structure_type, ID->getName(), Mod ? Mod : TheCU, |
| 2049 | DefUnit, Line, RuntimeLang); |
David Blaikie | ef8a951 | 2014-05-05 23:23:53 +0000 | [diff] [blame] | 2050 | ObjCInterfaceCache.push_back(ObjCInterfaceCacheEntry(Ty, FwdDecl, Unit)); |
Guy Benyei | 11169dd | 2012-12-18 14:30:41 +0000 | [diff] [blame] | 2051 | return FwdDecl; |
| 2052 | } |
| 2053 | |
David Blaikie | ef8a951 | 2014-05-05 23:23:53 +0000 | [diff] [blame] | 2054 | return CreateTypeDefinition(Ty, Unit); |
| 2055 | } |
| 2056 | |
Adrian Prantl | c4bb47e | 2015-06-30 17:39:51 +0000 | [diff] [blame] | 2057 | llvm::DIModule * |
Adrian Prantl | 6668920 | 2015-09-18 23:01:45 +0000 | [diff] [blame] | 2058 | CGDebugInfo::getOrCreateModuleRef(ExternalASTSource::ASTSourceDescriptor Mod, |
| 2059 | bool CreateSkeletonCU) { |
Adrian Prantl | eb66a26 | 2015-09-24 16:10:04 +0000 | [diff] [blame] | 2060 | // Use the Module pointer as the key into the cache. This is a |
| 2061 | // nullptr if the "Module" is a PCH, which is safe because we don't |
| 2062 | // support chained PCH debug info, so there can only be a single PCH. |
| 2063 | const Module *M = Mod.getModuleOrNull(); |
Adrian Prantl | 9e8ea35 | 2015-09-29 20:44:46 +0000 | [diff] [blame] | 2064 | auto ModRef = ModuleCache.find(M); |
| 2065 | if (ModRef != ModuleCache.end()) |
| 2066 | return cast<llvm::DIModule>(ModRef->second); |
Adrian Prantl | 2388ead | 2015-06-30 18:01:05 +0000 | [diff] [blame] | 2067 | |
| 2068 | // Macro definitions that were defined with "-D" on the command line. |
| 2069 | SmallString<128> ConfigMacros; |
| 2070 | { |
| 2071 | llvm::raw_svector_ostream OS(ConfigMacros); |
| 2072 | const auto &PPOpts = CGM.getPreprocessorOpts(); |
| 2073 | unsigned I = 0; |
| 2074 | // Translate the macro definitions back into a commmand line. |
| 2075 | for (auto &M : PPOpts.Macros) { |
| 2076 | if (++I > 1) |
| 2077 | OS << " "; |
| 2078 | const std::string &Macro = M.first; |
| 2079 | bool Undef = M.second; |
| 2080 | OS << "\"-" << (Undef ? 'U' : 'D'); |
| 2081 | for (char c : Macro) |
| 2082 | switch (c) { |
| 2083 | case '\\' : OS << "\\\\"; break; |
| 2084 | case '"' : OS << "\\\""; break; |
| 2085 | default: OS << c; |
| 2086 | } |
| 2087 | OS << '\"'; |
Adrian Prantl | c4bb47e | 2015-06-30 17:39:51 +0000 | [diff] [blame] | 2088 | } |
Adrian Prantl | c4bb47e | 2015-06-30 17:39:51 +0000 | [diff] [blame] | 2089 | } |
Adrian Prantl | 6668920 | 2015-09-18 23:01:45 +0000 | [diff] [blame] | 2090 | |
Adrian Prantl | 835e663 | 2015-09-24 16:10:10 +0000 | [diff] [blame] | 2091 | bool IsRootModule = M ? !M->Parent : true; |
| 2092 | if (CreateSkeletonCU && IsRootModule) { |
Adrian Prantl | c96da8f | 2016-01-22 17:43:43 +0000 | [diff] [blame] | 2093 | // PCH files don't have a signature field in the control block, |
| 2094 | // but LLVM detects skeleton CUs by looking for a non-zero DWO id. |
Duncan P. N. Exon Smith | 60fa288 | 2017-03-13 18:45:08 +0000 | [diff] [blame] | 2095 | // We use the lower 64 bits for debug info. |
| 2096 | uint64_t Signature = |
| 2097 | Mod.getSignature() |
| 2098 | ? (uint64_t)Mod.getSignature()[1] << 32 | Mod.getSignature()[0] |
| 2099 | : ~1ULL; |
Adrian Prantl | 6668920 | 2015-09-18 23:01:45 +0000 | [diff] [blame] | 2100 | llvm::DIBuilder DIB(CGM.getModule()); |
Amjad Aboud | fa9a17e | 2016-12-14 20:24:40 +0000 | [diff] [blame] | 2101 | DIB.createCompileUnit(TheCU->getSourceLanguage(), |
| 2102 | DIB.createFile(Mod.getModuleName(), Mod.getPath()), |
| 2103 | TheCU->getProducer(), true, StringRef(), 0, |
| 2104 | Mod.getASTFile(), llvm::DICompileUnit::FullDebug, |
| 2105 | Signature); |
Adrian Prantl | 6668920 | 2015-09-18 23:01:45 +0000 | [diff] [blame] | 2106 | DIB.finalize(); |
Adrian Prantl | 2f957ac | 2015-09-19 00:59:22 +0000 | [diff] [blame] | 2107 | } |
Adrian Prantl | 835e663 | 2015-09-24 16:10:10 +0000 | [diff] [blame] | 2108 | llvm::DIModule *Parent = |
| 2109 | IsRootModule ? nullptr |
| 2110 | : getOrCreateModuleRef( |
| 2111 | ExternalASTSource::ASTSourceDescriptor(*M->Parent), |
| 2112 | CreateSkeletonCU); |
Adrian Prantl | eb66a26 | 2015-09-24 16:10:04 +0000 | [diff] [blame] | 2113 | llvm::DIModule *DIMod = |
Adrian Prantl | 835e663 | 2015-09-24 16:10:10 +0000 | [diff] [blame] | 2114 | DBuilder.createModule(Parent, Mod.getModuleName(), ConfigMacros, |
| 2115 | Mod.getPath(), CGM.getHeaderSearchOpts().Sysroot); |
Adrian Prantl | 9e8ea35 | 2015-09-29 20:44:46 +0000 | [diff] [blame] | 2116 | ModuleCache[M].reset(DIMod); |
Adrian Prantl | eb66a26 | 2015-09-24 16:10:04 +0000 | [diff] [blame] | 2117 | return DIMod; |
Adrian Prantl | c4bb47e | 2015-06-30 17:39:51 +0000 | [diff] [blame] | 2118 | } |
| 2119 | |
Duncan P. N. Exon Smith | 9dd4e4e | 2015-04-29 16:40:08 +0000 | [diff] [blame] | 2120 | llvm::DIType *CGDebugInfo::CreateTypeDefinition(const ObjCInterfaceType *Ty, |
| 2121 | llvm::DIFile *Unit) { |
David Blaikie | ef8a951 | 2014-05-05 23:23:53 +0000 | [diff] [blame] | 2122 | ObjCInterfaceDecl *ID = Ty->getDecl(); |
Duncan P. N. Exon Smith | 9dd4e4e | 2015-04-29 16:40:08 +0000 | [diff] [blame] | 2123 | llvm::DIFile *DefUnit = getOrCreateFile(ID->getLocation()); |
David Blaikie | ef8a951 | 2014-05-05 23:23:53 +0000 | [diff] [blame] | 2124 | unsigned Line = getLineNumber(ID->getLocation()); |
Duncan P. N. Exon Smith | 798d565 | 2015-04-15 23:19:15 +0000 | [diff] [blame] | 2125 | unsigned RuntimeLang = TheCU->getSourceLanguage(); |
Guy Benyei | 11169dd | 2012-12-18 14:30:41 +0000 | [diff] [blame] | 2126 | |
| 2127 | // Bit size, align and offset of the type. |
| 2128 | uint64_t Size = CGM.getContext().getTypeSize(Ty); |
Victor Leschuk | a7ece03 | 2016-10-20 00:13:19 +0000 | [diff] [blame] | 2129 | auto Align = getTypeAlignIfRequired(Ty, CGM.getContext()); |
Guy Benyei | 11169dd | 2012-12-18 14:30:41 +0000 | [diff] [blame] | 2130 | |
Leny Kholodov | 80c047d | 2016-09-06 10:48:04 +0000 | [diff] [blame] | 2131 | llvm::DINode::DIFlags Flags = llvm::DINode::FlagZero; |
Guy Benyei | 11169dd | 2012-12-18 14:30:41 +0000 | [diff] [blame] | 2132 | if (ID->getImplementation()) |
Duncan P. N. Exon Smith | 9dd4e4e | 2015-04-29 16:40:08 +0000 | [diff] [blame] | 2133 | Flags |= llvm::DINode::FlagObjcClassComplete; |
Guy Benyei | 11169dd | 2012-12-18 14:30:41 +0000 | [diff] [blame] | 2134 | |
Adrian Prantl | fd69611 | 2015-10-01 00:48:51 +0000 | [diff] [blame] | 2135 | llvm::DIScope *Mod = getParentModuleOrNull(ID); |
Duncan P. N. Exon Smith | 9dd4e4e | 2015-04-29 16:40:08 +0000 | [diff] [blame] | 2136 | llvm::DICompositeType *RealDecl = DBuilder.createStructType( |
Adrian Prantl | fd69611 | 2015-10-01 00:48:51 +0000 | [diff] [blame] | 2137 | Mod ? Mod : Unit, ID->getName(), DefUnit, Line, Size, Align, Flags, |
| 2138 | nullptr, llvm::DINodeArray(), RuntimeLang); |
Guy Benyei | 11169dd | 2012-12-18 14:30:41 +0000 | [diff] [blame] | 2139 | |
David Blaikie | ef8a951 | 2014-05-05 23:23:53 +0000 | [diff] [blame] | 2140 | QualType QTy(Ty, 0); |
Duncan P. N. Exon Smith | fb49491 | 2014-12-09 18:39:32 +0000 | [diff] [blame] | 2141 | TypeCache[QTy.getAsOpaquePtr()].reset(RealDecl); |
Guy Benyei | 11169dd | 2012-12-18 14:30:41 +0000 | [diff] [blame] | 2142 | |
Eric Christopher | 35f1f9f | 2013-07-14 21:00:07 +0000 | [diff] [blame] | 2143 | // Push the struct on region stack. |
Duncan P. N. Exon Smith | 4078ad4 | 2015-04-16 16:36:45 +0000 | [diff] [blame] | 2144 | LexicalBlockStack.emplace_back(RealDecl); |
Duncan P. N. Exon Smith | fb49491 | 2014-12-09 18:39:32 +0000 | [diff] [blame] | 2145 | RegionMap[Ty->getDecl()].reset(RealDecl); |
Guy Benyei | 11169dd | 2012-12-18 14:30:41 +0000 | [diff] [blame] | 2146 | |
| 2147 | // Convert all the elements. |
Duncan P. N. Exon Smith | fb49491 | 2014-12-09 18:39:32 +0000 | [diff] [blame] | 2148 | SmallVector<llvm::Metadata *, 16> EltTys; |
Guy Benyei | 11169dd | 2012-12-18 14:30:41 +0000 | [diff] [blame] | 2149 | |
| 2150 | ObjCInterfaceDecl *SClass = ID->getSuperClass(); |
| 2151 | if (SClass) { |
Duncan P. N. Exon Smith | 9dd4e4e | 2015-04-29 16:40:08 +0000 | [diff] [blame] | 2152 | llvm::DIType *SClassTy = |
Eric Christopher | e7b87e5 | 2014-10-26 23:40:33 +0000 | [diff] [blame] | 2153 | getOrCreateType(CGM.getContext().getObjCInterfaceType(SClass), Unit); |
Duncan P. N. Exon Smith | b747023 | 2015-04-15 23:48:50 +0000 | [diff] [blame] | 2154 | if (!SClassTy) |
Duncan P. N. Exon Smith | 0b6c369 | 2015-04-20 18:51:48 +0000 | [diff] [blame] | 2155 | return nullptr; |
Eric Christopher | b2a008c | 2013-05-16 00:45:12 +0000 | [diff] [blame] | 2156 | |
Leny Kholodov | df050fd | 2016-09-06 17:06:14 +0000 | [diff] [blame] | 2157 | llvm::DIType *InhTag = DBuilder.createInheritance(RealDecl, SClassTy, 0, |
| 2158 | llvm::DINode::FlagZero); |
Guy Benyei | 11169dd | 2012-12-18 14:30:41 +0000 | [diff] [blame] | 2159 | EltTys.push_back(InhTag); |
| 2160 | } |
| 2161 | |
Eric Christopher | 35f1f9f | 2013-07-14 21:00:07 +0000 | [diff] [blame] | 2162 | // Create entries for all of the properties. |
Nico Weber | 7123bca | 2015-12-04 19:14:14 +0000 | [diff] [blame] | 2163 | auto AddProperty = [&](const ObjCPropertyDecl *PD) { |
Guy Benyei | 11169dd | 2012-12-18 14:30:41 +0000 | [diff] [blame] | 2164 | SourceLocation Loc = PD->getLocation(); |
Duncan P. N. Exon Smith | 9dd4e4e | 2015-04-29 16:40:08 +0000 | [diff] [blame] | 2165 | llvm::DIFile *PUnit = getOrCreateFile(Loc); |
Guy Benyei | 11169dd | 2012-12-18 14:30:41 +0000 | [diff] [blame] | 2166 | unsigned PLine = getLineNumber(Loc); |
| 2167 | ObjCMethodDecl *Getter = PD->getGetterMethodDecl(); |
| 2168 | ObjCMethodDecl *Setter = PD->getSetterMethodDecl(); |
Eric Christopher | e7b87e5 | 2014-10-26 23:40:33 +0000 | [diff] [blame] | 2169 | llvm::MDNode *PropertyNode = DBuilder.createObjCProperty( |
| 2170 | PD->getName(), PUnit, PLine, |
| 2171 | hasDefaultGetterName(PD, Getter) ? "" |
| 2172 | : getSelectorName(PD->getGetterName()), |
| 2173 | hasDefaultSetterName(PD, Setter) ? "" |
| 2174 | : getSelectorName(PD->getSetterName()), |
| 2175 | PD->getPropertyAttributes(), getOrCreateType(PD->getType(), PUnit)); |
Guy Benyei | 11169dd | 2012-12-18 14:30:41 +0000 | [diff] [blame] | 2176 | EltTys.push_back(PropertyNode); |
Nico Weber | 7123bca | 2015-12-04 19:14:14 +0000 | [diff] [blame] | 2177 | }; |
| 2178 | { |
| 2179 | llvm::SmallPtrSet<const IdentifierInfo*, 16> PropertySet; |
Nico Weber | de059e1 | 2015-12-04 19:35:45 +0000 | [diff] [blame] | 2180 | for (const ObjCCategoryDecl *ClassExt : ID->known_extensions()) |
Manman Ren | efe1bac | 2016-01-27 20:00:32 +0000 | [diff] [blame] | 2181 | for (auto *PD : ClassExt->properties()) { |
Nico Weber | de059e1 | 2015-12-04 19:35:45 +0000 | [diff] [blame] | 2182 | PropertySet.insert(PD->getIdentifier()); |
| 2183 | AddProperty(PD); |
| 2184 | } |
Manman Ren | efe1bac | 2016-01-27 20:00:32 +0000 | [diff] [blame] | 2185 | for (const auto *PD : ID->properties()) { |
Nico Weber | 7123bca | 2015-12-04 19:14:14 +0000 | [diff] [blame] | 2186 | // Don't emit duplicate metadata for properties that were already in a |
| 2187 | // class extension. |
| 2188 | if (!PropertySet.insert(PD->getIdentifier()).second) |
| 2189 | continue; |
| 2190 | AddProperty(PD); |
| 2191 | } |
Guy Benyei | 11169dd | 2012-12-18 14:30:41 +0000 | [diff] [blame] | 2192 | } |
| 2193 | |
| 2194 | const ASTRecordLayout &RL = CGM.getContext().getASTObjCInterfaceLayout(ID); |
| 2195 | unsigned FieldNo = 0; |
| 2196 | for (ObjCIvarDecl *Field = ID->all_declared_ivar_begin(); Field; |
| 2197 | Field = Field->getNextIvar(), ++FieldNo) { |
Duncan P. N. Exon Smith | 9dd4e4e | 2015-04-29 16:40:08 +0000 | [diff] [blame] | 2198 | llvm::DIType *FieldTy = getOrCreateType(Field->getType(), Unit); |
Duncan P. N. Exon Smith | b747023 | 2015-04-15 23:48:50 +0000 | [diff] [blame] | 2199 | if (!FieldTy) |
Duncan P. N. Exon Smith | 0b6c369 | 2015-04-20 18:51:48 +0000 | [diff] [blame] | 2200 | return nullptr; |
Eric Christopher | b2a008c | 2013-05-16 00:45:12 +0000 | [diff] [blame] | 2201 | |
Guy Benyei | 11169dd | 2012-12-18 14:30:41 +0000 | [diff] [blame] | 2202 | StringRef FieldName = Field->getName(); |
| 2203 | |
| 2204 | // Ignore unnamed fields. |
| 2205 | if (FieldName.empty()) |
| 2206 | continue; |
| 2207 | |
| 2208 | // Get the location for the field. |
Duncan P. N. Exon Smith | 9dd4e4e | 2015-04-29 16:40:08 +0000 | [diff] [blame] | 2209 | llvm::DIFile *FieldDefUnit = getOrCreateFile(Field->getLocation()); |
Guy Benyei | 11169dd | 2012-12-18 14:30:41 +0000 | [diff] [blame] | 2210 | unsigned FieldLine = getLineNumber(Field->getLocation()); |
| 2211 | QualType FType = Field->getType(); |
| 2212 | uint64_t FieldSize = 0; |
Victor Leschuk | 802e4a5 | 2016-10-19 22:11:07 +0000 | [diff] [blame] | 2213 | uint32_t FieldAlign = 0; |
Guy Benyei | 11169dd | 2012-12-18 14:30:41 +0000 | [diff] [blame] | 2214 | |
| 2215 | if (!FType->isIncompleteArrayType()) { |
| 2216 | |
| 2217 | // Bit size, align and offset of the type. |
| 2218 | FieldSize = Field->isBitField() |
Eric Christopher | 35f1f9f | 2013-07-14 21:00:07 +0000 | [diff] [blame] | 2219 | ? Field->getBitWidthValue(CGM.getContext()) |
| 2220 | : CGM.getContext().getTypeSize(FType); |
Victor Leschuk | a7ece03 | 2016-10-20 00:13:19 +0000 | [diff] [blame] | 2221 | FieldAlign = getTypeAlignIfRequired(FType, CGM.getContext()); |
Guy Benyei | 11169dd | 2012-12-18 14:30:41 +0000 | [diff] [blame] | 2222 | } |
| 2223 | |
| 2224 | uint64_t FieldOffset; |
| 2225 | if (CGM.getLangOpts().ObjCRuntime.isNonFragile()) { |
| 2226 | // We don't know the runtime offset of an ivar if we're using the |
| 2227 | // non-fragile ABI. For bitfields, use the bit offset into the first |
| 2228 | // byte of storage of the bitfield. For other fields, use zero. |
| 2229 | if (Field->isBitField()) { |
Eric Christopher | e7b87e5 | 2014-10-26 23:40:33 +0000 | [diff] [blame] | 2230 | FieldOffset = |
| 2231 | CGM.getObjCRuntime().ComputeBitfieldBitOffset(CGM, ID, Field); |
Guy Benyei | 11169dd | 2012-12-18 14:30:41 +0000 | [diff] [blame] | 2232 | FieldOffset %= CGM.getContext().getCharWidth(); |
| 2233 | } else { |
| 2234 | FieldOffset = 0; |
| 2235 | } |
| 2236 | } else { |
| 2237 | FieldOffset = RL.getFieldOffset(FieldNo); |
| 2238 | } |
| 2239 | |
Leny Kholodov | 80c047d | 2016-09-06 10:48:04 +0000 | [diff] [blame] | 2240 | llvm::DINode::DIFlags Flags = llvm::DINode::FlagZero; |
Guy Benyei | 11169dd | 2012-12-18 14:30:41 +0000 | [diff] [blame] | 2241 | if (Field->getAccessControl() == ObjCIvarDecl::Protected) |
Duncan P. N. Exon Smith | 9dd4e4e | 2015-04-29 16:40:08 +0000 | [diff] [blame] | 2242 | Flags = llvm::DINode::FlagProtected; |
Guy Benyei | 11169dd | 2012-12-18 14:30:41 +0000 | [diff] [blame] | 2243 | else if (Field->getAccessControl() == ObjCIvarDecl::Private) |
Duncan P. N. Exon Smith | 9dd4e4e | 2015-04-29 16:40:08 +0000 | [diff] [blame] | 2244 | Flags = llvm::DINode::FlagPrivate; |
Adrian Prantl | 21361fb | 2014-08-29 22:44:27 +0000 | [diff] [blame] | 2245 | else if (Field->getAccessControl() == ObjCIvarDecl::Public) |
Duncan P. N. Exon Smith | 9dd4e4e | 2015-04-29 16:40:08 +0000 | [diff] [blame] | 2246 | Flags = llvm::DINode::FlagPublic; |
Guy Benyei | 11169dd | 2012-12-18 14:30:41 +0000 | [diff] [blame] | 2247 | |
Craig Topper | 8a13c41 | 2014-05-21 05:09:00 +0000 | [diff] [blame] | 2248 | llvm::MDNode *PropertyNode = nullptr; |
Guy Benyei | 11169dd | 2012-12-18 14:30:41 +0000 | [diff] [blame] | 2249 | if (ObjCImplementationDecl *ImpD = ID->getImplementation()) { |
Eric Christopher | b2a008c | 2013-05-16 00:45:12 +0000 | [diff] [blame] | 2250 | if (ObjCPropertyImplDecl *PImpD = |
Eric Christopher | e7b87e5 | 2014-10-26 23:40:33 +0000 | [diff] [blame] | 2251 | ImpD->FindPropertyImplIvarDecl(Field->getIdentifier())) { |
Guy Benyei | 11169dd | 2012-12-18 14:30:41 +0000 | [diff] [blame] | 2252 | if (ObjCPropertyDecl *PD = PImpD->getPropertyDecl()) { |
Eric Christopher | c0c5d46 | 2013-02-21 22:35:08 +0000 | [diff] [blame] | 2253 | SourceLocation Loc = PD->getLocation(); |
Duncan P. N. Exon Smith | 9dd4e4e | 2015-04-29 16:40:08 +0000 | [diff] [blame] | 2254 | llvm::DIFile *PUnit = getOrCreateFile(Loc); |
Eric Christopher | c0c5d46 | 2013-02-21 22:35:08 +0000 | [diff] [blame] | 2255 | unsigned PLine = getLineNumber(Loc); |
Guy Benyei | 11169dd | 2012-12-18 14:30:41 +0000 | [diff] [blame] | 2256 | ObjCMethodDecl *Getter = PD->getGetterMethodDecl(); |
| 2257 | ObjCMethodDecl *Setter = PD->getSetterMethodDecl(); |
Eric Christopher | e7b87e5 | 2014-10-26 23:40:33 +0000 | [diff] [blame] | 2258 | PropertyNode = DBuilder.createObjCProperty( |
| 2259 | PD->getName(), PUnit, PLine, |
| 2260 | hasDefaultGetterName(PD, Getter) ? "" : getSelectorName( |
| 2261 | PD->getGetterName()), |
| 2262 | hasDefaultSetterName(PD, Setter) ? "" : getSelectorName( |
| 2263 | PD->getSetterName()), |
| 2264 | PD->getPropertyAttributes(), |
| 2265 | getOrCreateType(PD->getType(), PUnit)); |
Guy Benyei | 11169dd | 2012-12-18 14:30:41 +0000 | [diff] [blame] | 2266 | } |
| 2267 | } |
| 2268 | } |
Eric Christopher | e7b87e5 | 2014-10-26 23:40:33 +0000 | [diff] [blame] | 2269 | FieldTy = DBuilder.createObjCIVar(FieldName, FieldDefUnit, FieldLine, |
| 2270 | FieldSize, FieldAlign, FieldOffset, Flags, |
| 2271 | FieldTy, PropertyNode); |
Guy Benyei | 11169dd | 2012-12-18 14:30:41 +0000 | [diff] [blame] | 2272 | EltTys.push_back(FieldTy); |
| 2273 | } |
| 2274 | |
Duncan P. N. Exon Smith | 9dd4e4e | 2015-04-29 16:40:08 +0000 | [diff] [blame] | 2275 | llvm::DINodeArray Elements = DBuilder.getOrCreateArray(EltTys); |
Duncan P. N. Exon Smith | c8ee63e | 2014-12-18 00:48:56 +0000 | [diff] [blame] | 2276 | DBuilder.replaceArrays(RealDecl, Elements); |
Adrian Prantl | a03a85a | 2013-03-06 22:03:30 +0000 | [diff] [blame] | 2277 | |
Guy Benyei | 11169dd | 2012-12-18 14:30:41 +0000 | [diff] [blame] | 2278 | LexicalBlockStack.pop_back(); |
Eric Christopher | 5c7ee8b | 2013-04-02 22:59:11 +0000 | [diff] [blame] | 2279 | return RealDecl; |
Guy Benyei | 11169dd | 2012-12-18 14:30:41 +0000 | [diff] [blame] | 2280 | } |
| 2281 | |
Duncan P. N. Exon Smith | 9dd4e4e | 2015-04-29 16:40:08 +0000 | [diff] [blame] | 2282 | llvm::DIType *CGDebugInfo::CreateType(const VectorType *Ty, |
| 2283 | llvm::DIFile *Unit) { |
| 2284 | llvm::DIType *ElementTy = getOrCreateType(Ty->getElementType(), Unit); |
Guy Benyei | 11169dd | 2012-12-18 14:30:41 +0000 | [diff] [blame] | 2285 | int64_t Count = Ty->getNumElements(); |
| 2286 | if (Count == 0) |
| 2287 | // If number of elements are not known then this is an unbounded array. |
| 2288 | // Use Count == -1 to express such arrays. |
| 2289 | Count = -1; |
| 2290 | |
Duncan P. N. Exon Smith | fb49491 | 2014-12-09 18:39:32 +0000 | [diff] [blame] | 2291 | llvm::Metadata *Subscript = DBuilder.getOrCreateSubrange(0, Count); |
Duncan P. N. Exon Smith | 9dd4e4e | 2015-04-29 16:40:08 +0000 | [diff] [blame] | 2292 | llvm::DINodeArray SubscriptArray = DBuilder.getOrCreateArray(Subscript); |
Guy Benyei | 11169dd | 2012-12-18 14:30:41 +0000 | [diff] [blame] | 2293 | |
| 2294 | uint64_t Size = CGM.getContext().getTypeSize(Ty); |
Victor Leschuk | a7ece03 | 2016-10-20 00:13:19 +0000 | [diff] [blame] | 2295 | auto Align = getTypeAlignIfRequired(Ty, CGM.getContext()); |
Guy Benyei | 11169dd | 2012-12-18 14:30:41 +0000 | [diff] [blame] | 2296 | |
| 2297 | return DBuilder.createVectorType(Size, Align, ElementTy, SubscriptArray); |
| 2298 | } |
| 2299 | |
Duncan P. N. Exon Smith | 9dd4e4e | 2015-04-29 16:40:08 +0000 | [diff] [blame] | 2300 | llvm::DIType *CGDebugInfo::CreateType(const ArrayType *Ty, llvm::DIFile *Unit) { |
Guy Benyei | 11169dd | 2012-12-18 14:30:41 +0000 | [diff] [blame] | 2301 | uint64_t Size; |
Victor Leschuk | 802e4a5 | 2016-10-19 22:11:07 +0000 | [diff] [blame] | 2302 | uint32_t Align; |
Guy Benyei | 11169dd | 2012-12-18 14:30:41 +0000 | [diff] [blame] | 2303 | |
| 2304 | // FIXME: make getTypeAlign() aware of VLAs and incomplete array types |
David Majnemer | 58ed0f3 | 2016-07-17 00:39:12 +0000 | [diff] [blame] | 2305 | if (const auto *VAT = dyn_cast<VariableArrayType>(Ty)) { |
Guy Benyei | 11169dd | 2012-12-18 14:30:41 +0000 | [diff] [blame] | 2306 | Size = 0; |
Victor Leschuk | a7ece03 | 2016-10-20 00:13:19 +0000 | [diff] [blame] | 2307 | Align = getTypeAlignIfRequired(CGM.getContext().getBaseElementType(VAT), |
| 2308 | CGM.getContext()); |
Guy Benyei | 11169dd | 2012-12-18 14:30:41 +0000 | [diff] [blame] | 2309 | } else if (Ty->isIncompleteArrayType()) { |
| 2310 | Size = 0; |
| 2311 | if (Ty->getElementType()->isIncompleteType()) |
| 2312 | Align = 0; |
| 2313 | else |
Victor Leschuk | a7ece03 | 2016-10-20 00:13:19 +0000 | [diff] [blame] | 2314 | Align = getTypeAlignIfRequired(Ty->getElementType(), CGM.getContext()); |
David Blaikie | f03b2e8 | 2013-05-09 20:48:12 +0000 | [diff] [blame] | 2315 | } else if (Ty->isIncompleteType()) { |
Guy Benyei | 11169dd | 2012-12-18 14:30:41 +0000 | [diff] [blame] | 2316 | Size = 0; |
| 2317 | Align = 0; |
| 2318 | } else { |
| 2319 | // Size and align of the whole array, not the element type. |
| 2320 | Size = CGM.getContext().getTypeSize(Ty); |
Victor Leschuk | a7ece03 | 2016-10-20 00:13:19 +0000 | [diff] [blame] | 2321 | Align = getTypeAlignIfRequired(Ty, CGM.getContext()); |
Guy Benyei | 11169dd | 2012-12-18 14:30:41 +0000 | [diff] [blame] | 2322 | } |
| 2323 | |
| 2324 | // Add the dimensions of the array. FIXME: This loses CV qualifiers from |
| 2325 | // interior arrays, do we care? Why aren't nested arrays represented the |
| 2326 | // obvious/recursive way? |
Duncan P. N. Exon Smith | fb49491 | 2014-12-09 18:39:32 +0000 | [diff] [blame] | 2327 | SmallVector<llvm::Metadata *, 8> Subscripts; |
Guy Benyei | 11169dd | 2012-12-18 14:30:41 +0000 | [diff] [blame] | 2328 | QualType EltTy(Ty, 0); |
| 2329 | while ((Ty = dyn_cast<ArrayType>(EltTy))) { |
| 2330 | // If the number of elements is known, then count is that number. Otherwise, |
| 2331 | // it's -1. This allows us to represent a subrange with an array of 0 |
| 2332 | // elements, like this: |
| 2333 | // |
| 2334 | // struct foo { |
| 2335 | // int x[0]; |
| 2336 | // }; |
Eric Christopher | e7b87e5 | 2014-10-26 23:40:33 +0000 | [diff] [blame] | 2337 | int64_t Count = -1; // Count == -1 is an unbounded array. |
David Majnemer | 58ed0f3 | 2016-07-17 00:39:12 +0000 | [diff] [blame] | 2338 | if (const auto *CAT = dyn_cast<ConstantArrayType>(Ty)) |
Guy Benyei | 11169dd | 2012-12-18 14:30:41 +0000 | [diff] [blame] | 2339 | Count = CAT->getSize().getZExtValue(); |
David Blaikie | 87173f1 | 2016-08-22 17:49:56 +0000 | [diff] [blame] | 2340 | else if (const auto *VAT = dyn_cast<VariableArrayType>(Ty)) { |
Eli Friedman | 01d6b96 | 2016-10-19 22:16:32 +0000 | [diff] [blame] | 2341 | if (Expr *Size = VAT->getSizeExpr()) { |
| 2342 | llvm::APSInt V; |
| 2343 | if (Size->EvaluateAsInt(V, CGM.getContext())) |
| 2344 | Count = V.getExtValue(); |
| 2345 | } |
David Blaikie | 87173f1 | 2016-08-22 17:49:56 +0000 | [diff] [blame] | 2346 | } |
Eric Christopher | b2a008c | 2013-05-16 00:45:12 +0000 | [diff] [blame] | 2347 | |
Guy Benyei | 11169dd | 2012-12-18 14:30:41 +0000 | [diff] [blame] | 2348 | // FIXME: Verify this is right for VLAs. |
| 2349 | Subscripts.push_back(DBuilder.getOrCreateSubrange(0, Count)); |
| 2350 | EltTy = Ty->getElementType(); |
| 2351 | } |
| 2352 | |
Duncan P. N. Exon Smith | 9dd4e4e | 2015-04-29 16:40:08 +0000 | [diff] [blame] | 2353 | llvm::DINodeArray SubscriptArray = DBuilder.getOrCreateArray(Subscripts); |
Guy Benyei | 11169dd | 2012-12-18 14:30:41 +0000 | [diff] [blame] | 2354 | |
Duncan P. N. Exon Smith | 0b6c369 | 2015-04-20 18:51:48 +0000 | [diff] [blame] | 2355 | return DBuilder.createArrayType(Size, Align, getOrCreateType(EltTy, Unit), |
| 2356 | SubscriptArray); |
Guy Benyei | 11169dd | 2012-12-18 14:30:41 +0000 | [diff] [blame] | 2357 | } |
| 2358 | |
Duncan P. N. Exon Smith | 9dd4e4e | 2015-04-29 16:40:08 +0000 | [diff] [blame] | 2359 | llvm::DIType *CGDebugInfo::CreateType(const LValueReferenceType *Ty, |
| 2360 | llvm::DIFile *Unit) { |
Eric Christopher | e7b87e5 | 2014-10-26 23:40:33 +0000 | [diff] [blame] | 2361 | return CreatePointerLikeType(llvm::dwarf::DW_TAG_reference_type, Ty, |
| 2362 | Ty->getPointeeType(), Unit); |
Guy Benyei | 11169dd | 2012-12-18 14:30:41 +0000 | [diff] [blame] | 2363 | } |
| 2364 | |
Duncan P. N. Exon Smith | 9dd4e4e | 2015-04-29 16:40:08 +0000 | [diff] [blame] | 2365 | llvm::DIType *CGDebugInfo::CreateType(const RValueReferenceType *Ty, |
| 2366 | llvm::DIFile *Unit) { |
Eric Christopher | e7b87e5 | 2014-10-26 23:40:33 +0000 | [diff] [blame] | 2367 | return CreatePointerLikeType(llvm::dwarf::DW_TAG_rvalue_reference_type, Ty, |
| 2368 | Ty->getPointeeType(), Unit); |
Guy Benyei | 11169dd | 2012-12-18 14:30:41 +0000 | [diff] [blame] | 2369 | } |
| 2370 | |
Duncan P. N. Exon Smith | 9dd4e4e | 2015-04-29 16:40:08 +0000 | [diff] [blame] | 2371 | llvm::DIType *CGDebugInfo::CreateType(const MemberPointerType *Ty, |
| 2372 | llvm::DIFile *U) { |
Leny Kholodov | 80c047d | 2016-09-06 10:48:04 +0000 | [diff] [blame] | 2373 | llvm::DINode::DIFlags Flags = llvm::DINode::FlagZero; |
Reid Kleckner | fb72718 | 2016-06-17 22:27:59 +0000 | [diff] [blame] | 2374 | uint64_t Size = 0; |
| 2375 | |
| 2376 | if (!Ty->isIncompleteType()) { |
| 2377 | Size = CGM.getContext().getTypeSize(Ty); |
| 2378 | |
| 2379 | // Set the MS inheritance model. There is no flag for the unspecified model. |
| 2380 | if (CGM.getTarget().getCXXABI().isMicrosoft()) { |
| 2381 | switch (Ty->getMostRecentCXXRecordDecl()->getMSInheritanceModel()) { |
| 2382 | case MSInheritanceAttr::Keyword_single_inheritance: |
| 2383 | Flags |= llvm::DINode::FlagSingleInheritance; |
| 2384 | break; |
| 2385 | case MSInheritanceAttr::Keyword_multiple_inheritance: |
| 2386 | Flags |= llvm::DINode::FlagMultipleInheritance; |
| 2387 | break; |
| 2388 | case MSInheritanceAttr::Keyword_virtual_inheritance: |
| 2389 | Flags |= llvm::DINode::FlagVirtualInheritance; |
| 2390 | break; |
| 2391 | case MSInheritanceAttr::Keyword_unspecified_inheritance: |
| 2392 | break; |
| 2393 | } |
| 2394 | } |
| 2395 | } |
| 2396 | |
Duncan P. N. Exon Smith | 9dd4e4e | 2015-04-29 16:40:08 +0000 | [diff] [blame] | 2397 | llvm::DIType *ClassType = getOrCreateType(QualType(Ty->getClass(), 0), U); |
David Majnemer | 5fd33e0 | 2015-04-24 01:25:08 +0000 | [diff] [blame] | 2398 | if (Ty->isMemberDataPointerType()) |
David Blaikie | 2c705ca | 2013-01-19 19:20:56 +0000 | [diff] [blame] | 2399 | return DBuilder.createMemberPointerType( |
Reid Kleckner | fb72718 | 2016-06-17 22:27:59 +0000 | [diff] [blame] | 2400 | getOrCreateType(Ty->getPointeeType(), U), ClassType, Size, /*Align=*/0, |
| 2401 | Flags); |
Adrian Prantl | 0866acd | 2013-12-19 01:38:47 +0000 | [diff] [blame] | 2402 | |
| 2403 | const FunctionProtoType *FPT = |
Eric Christopher | e7b87e5 | 2014-10-26 23:40:33 +0000 | [diff] [blame] | 2404 | Ty->getPointeeType()->getAs<FunctionProtoType>(); |
| 2405 | return DBuilder.createMemberPointerType( |
| 2406 | getOrCreateInstanceMethodType(CGM.getContext().getPointerType(QualType( |
| 2407 | Ty->getClass(), FPT->getTypeQuals())), |
| 2408 | FPT, U), |
Reid Kleckner | fb72718 | 2016-06-17 22:27:59 +0000 | [diff] [blame] | 2409 | ClassType, Size, /*Align=*/0, Flags); |
Guy Benyei | 11169dd | 2012-12-18 14:30:41 +0000 | [diff] [blame] | 2410 | } |
| 2411 | |
Duncan P. N. Exon Smith | 9dd4e4e | 2015-04-29 16:40:08 +0000 | [diff] [blame] | 2412 | llvm::DIType *CGDebugInfo::CreateType(const AtomicType *Ty, llvm::DIFile *U) { |
Victor Leschuk | 0df19037 | 2016-10-31 19:09:47 +0000 | [diff] [blame] | 2413 | auto *FromTy = getOrCreateType(Ty->getValueType(), U); |
| 2414 | return DBuilder.createQualifiedType(llvm::dwarf::DW_TAG_atomic_type, FromTy); |
Guy Benyei | 11169dd | 2012-12-18 14:30:41 +0000 | [diff] [blame] | 2415 | } |
| 2416 | |
Xiuli Pan | 9c14e28 | 2016-01-09 12:53:17 +0000 | [diff] [blame] | 2417 | llvm::DIType* CGDebugInfo::CreateType(const PipeType *Ty, |
| 2418 | llvm::DIFile *U) { |
| 2419 | return getOrCreateType(Ty->getElementType(), U); |
| 2420 | } |
| 2421 | |
Duncan P. N. Exon Smith | 9dd4e4e | 2015-04-29 16:40:08 +0000 | [diff] [blame] | 2422 | llvm::DIType *CGDebugInfo::CreateEnumType(const EnumType *Ty) { |
Manman Ren | 1b45702 | 2013-08-28 21:20:28 +0000 | [diff] [blame] | 2423 | const EnumDecl *ED = Ty->getDecl(); |
Adrian Prantl | 5c8bd88 | 2015-09-11 17:23:08 +0000 | [diff] [blame] | 2424 | |
Guy Benyei | 11169dd | 2012-12-18 14:30:41 +0000 | [diff] [blame] | 2425 | uint64_t Size = 0; |
Victor Leschuk | 802e4a5 | 2016-10-19 22:11:07 +0000 | [diff] [blame] | 2426 | uint32_t Align = 0; |
Guy Benyei | 11169dd | 2012-12-18 14:30:41 +0000 | [diff] [blame] | 2427 | if (!ED->getTypeForDecl()->isIncompleteType()) { |
| 2428 | Size = CGM.getContext().getTypeSize(ED->getTypeForDecl()); |
Victor Leschuk | a7ece03 | 2016-10-20 00:13:19 +0000 | [diff] [blame] | 2429 | Align = getDeclAlignIfRequired(ED, CGM.getContext()); |
Guy Benyei | 11169dd | 2012-12-18 14:30:41 +0000 | [diff] [blame] | 2430 | } |
| 2431 | |
Manman Ren | e0064d8 | 2013-08-29 23:19:58 +0000 | [diff] [blame] | 2432 | SmallString<256> FullName = getUniqueTagTypeName(Ty, CGM, TheCU); |
| 2433 | |
Adrian Prantl | 5c8bd88 | 2015-09-11 17:23:08 +0000 | [diff] [blame] | 2434 | bool isImportedFromModule = |
| 2435 | DebugTypeExtRefs && ED->isFromASTFile() && ED->getDefinition(); |
| 2436 | |
Guy Benyei | 11169dd | 2012-12-18 14:30:41 +0000 | [diff] [blame] | 2437 | // If this is just a forward declaration, construct an appropriately |
| 2438 | // marked node and just return it. |
Adrian Prantl | 5c8bd88 | 2015-09-11 17:23:08 +0000 | [diff] [blame] | 2439 | if (isImportedFromModule || !ED->getDefinition()) { |
Adrian Prantl | 4594606 | 2016-02-23 19:30:08 +0000 | [diff] [blame] | 2440 | // Note that it is possible for enums to be created as part of |
| 2441 | // their own declcontext. In this case a FwdDecl will be created |
| 2442 | // twice. This doesn't cause a problem because both FwdDecls are |
| 2443 | // entered into the ReplaceMap: finalize() will replace the first |
| 2444 | // FwdDecl with the second and then replace the second with |
| 2445 | // complete type. |
Amjad Aboud | dc4531e | 2016-04-30 01:44:38 +0000 | [diff] [blame] | 2446 | llvm::DIScope *EDContext = getDeclContextDescriptor(ED); |
Duncan P. N. Exon Smith | 9dd4e4e | 2015-04-29 16:40:08 +0000 | [diff] [blame] | 2447 | llvm::DIFile *DefUnit = getOrCreateFile(ED->getLocation()); |
Adrian Prantl | ff9d83c | 2016-02-08 17:03:28 +0000 | [diff] [blame] | 2448 | llvm::TempDIScope TmpContext(DBuilder.createReplaceableCompositeType( |
| 2449 | llvm::dwarf::DW_TAG_enumeration_type, "", TheCU, DefUnit, 0)); |
Adrian Prantl | a40030f | 2016-02-06 01:59:09 +0000 | [diff] [blame] | 2450 | |
Guy Benyei | 11169dd | 2012-12-18 14:30:41 +0000 | [diff] [blame] | 2451 | unsigned Line = getLineNumber(ED->getLocation()); |
| 2452 | StringRef EDName = ED->getName(); |
Duncan P. N. Exon Smith | 9dd4e4e | 2015-04-29 16:40:08 +0000 | [diff] [blame] | 2453 | llvm::DIType *RetTy = DBuilder.createReplaceableCompositeType( |
Adrian Prantl | 4594606 | 2016-02-23 19:30:08 +0000 | [diff] [blame] | 2454 | llvm::dwarf::DW_TAG_enumeration_type, EDName, EDContext, DefUnit, Line, |
| 2455 | 0, Size, Align, llvm::DINode::FlagFwdDecl, FullName); |
Adrian Prantl | a40030f | 2016-02-06 01:59:09 +0000 | [diff] [blame] | 2456 | |
Duncan P. N. Exon Smith | fb49491 | 2014-12-09 18:39:32 +0000 | [diff] [blame] | 2457 | ReplaceMap.emplace_back( |
| 2458 | std::piecewise_construct, std::make_tuple(Ty), |
| 2459 | std::make_tuple(static_cast<llvm::Metadata *>(RetTy))); |
David Blaikie | f427b00 | 2014-05-06 03:42:01 +0000 | [diff] [blame] | 2460 | return RetTy; |
Guy Benyei | 11169dd | 2012-12-18 14:30:41 +0000 | [diff] [blame] | 2461 | } |
| 2462 | |
David Blaikie | 483a9da | 2014-05-06 18:35:21 +0000 | [diff] [blame] | 2463 | return CreateTypeDefinition(Ty); |
| 2464 | } |
| 2465 | |
Duncan P. N. Exon Smith | 9dd4e4e | 2015-04-29 16:40:08 +0000 | [diff] [blame] | 2466 | llvm::DIType *CGDebugInfo::CreateTypeDefinition(const EnumType *Ty) { |
David Blaikie | 483a9da | 2014-05-06 18:35:21 +0000 | [diff] [blame] | 2467 | const EnumDecl *ED = Ty->getDecl(); |
| 2468 | uint64_t Size = 0; |
Victor Leschuk | 802e4a5 | 2016-10-19 22:11:07 +0000 | [diff] [blame] | 2469 | uint32_t Align = 0; |
David Blaikie | 483a9da | 2014-05-06 18:35:21 +0000 | [diff] [blame] | 2470 | if (!ED->getTypeForDecl()->isIncompleteType()) { |
| 2471 | Size = CGM.getContext().getTypeSize(ED->getTypeForDecl()); |
Victor Leschuk | a7ece03 | 2016-10-20 00:13:19 +0000 | [diff] [blame] | 2472 | Align = getDeclAlignIfRequired(ED, CGM.getContext()); |
David Blaikie | 483a9da | 2014-05-06 18:35:21 +0000 | [diff] [blame] | 2473 | } |
| 2474 | |
| 2475 | SmallString<256> FullName = getUniqueTagTypeName(Ty, CGM, TheCU); |
| 2476 | |
Duncan P. N. Exon Smith | dadc2b6 | 2015-04-21 18:43:54 +0000 | [diff] [blame] | 2477 | // Create elements for each enumerator. |
Duncan P. N. Exon Smith | fb49491 | 2014-12-09 18:39:32 +0000 | [diff] [blame] | 2478 | SmallVector<llvm::Metadata *, 16> Enumerators; |
Guy Benyei | 11169dd | 2012-12-18 14:30:41 +0000 | [diff] [blame] | 2479 | ED = ED->getDefinition(); |
Aaron Ballman | 23a6dcb | 2014-03-08 18:45:14 +0000 | [diff] [blame] | 2480 | for (const auto *Enum : ED->enumerators()) { |
Eric Christopher | e7b87e5 | 2014-10-26 23:40:33 +0000 | [diff] [blame] | 2481 | Enumerators.push_back(DBuilder.createEnumerator( |
| 2482 | Enum->getName(), Enum->getInitVal().getSExtValue())); |
Guy Benyei | 11169dd | 2012-12-18 14:30:41 +0000 | [diff] [blame] | 2483 | } |
| 2484 | |
| 2485 | // Return a CompositeType for the enum itself. |
Duncan P. N. Exon Smith | 9dd4e4e | 2015-04-29 16:40:08 +0000 | [diff] [blame] | 2486 | llvm::DINodeArray EltArray = DBuilder.getOrCreateArray(Enumerators); |
Guy Benyei | 11169dd | 2012-12-18 14:30:41 +0000 | [diff] [blame] | 2487 | |
Duncan P. N. Exon Smith | 9dd4e4e | 2015-04-29 16:40:08 +0000 | [diff] [blame] | 2488 | llvm::DIFile *DefUnit = getOrCreateFile(ED->getLocation()); |
Guy Benyei | 11169dd | 2012-12-18 14:30:41 +0000 | [diff] [blame] | 2489 | unsigned Line = getLineNumber(ED->getLocation()); |
Amjad Aboud | dc4531e | 2016-04-30 01:44:38 +0000 | [diff] [blame] | 2490 | llvm::DIScope *EnumContext = getDeclContextDescriptor(ED); |
Duncan P. N. Exon Smith | 9dd4e4e | 2015-04-29 16:40:08 +0000 | [diff] [blame] | 2491 | llvm::DIType *ClassTy = |
Duncan P. N. Exon Smith | 0b6c369 | 2015-04-20 18:51:48 +0000 | [diff] [blame] | 2492 | ED->isFixed() ? getOrCreateType(ED->getIntegerType(), DefUnit) : nullptr; |
| 2493 | return DBuilder.createEnumerationType(EnumContext, ED->getName(), DefUnit, |
| 2494 | Line, Size, Align, EltArray, ClassTy, |
| 2495 | FullName); |
Guy Benyei | 11169dd | 2012-12-18 14:30:41 +0000 | [diff] [blame] | 2496 | } |
| 2497 | |
Amjad Aboud | 546bc11 | 2017-02-09 22:07:24 +0000 | [diff] [blame] | 2498 | llvm::DIMacro *CGDebugInfo::CreateMacro(llvm::DIMacroFile *Parent, |
| 2499 | unsigned MType, SourceLocation LineLoc, |
| 2500 | StringRef Name, StringRef Value) { |
| 2501 | unsigned Line = LineLoc.isInvalid() ? 0 : getLineNumber(LineLoc); |
| 2502 | return DBuilder.createMacro(Parent, Line, MType, Name, Value); |
| 2503 | } |
| 2504 | |
| 2505 | llvm::DIMacroFile *CGDebugInfo::CreateTempMacroFile(llvm::DIMacroFile *Parent, |
| 2506 | SourceLocation LineLoc, |
| 2507 | SourceLocation FileLoc) { |
| 2508 | llvm::DIFile *FName = getOrCreateFile(FileLoc); |
| 2509 | unsigned Line = LineLoc.isInvalid() ? 0 : getLineNumber(LineLoc); |
| 2510 | return DBuilder.createTempMacroFile(Parent, Line, FName); |
| 2511 | } |
| 2512 | |
David Blaikie | 0549106 | 2013-01-21 04:37:12 +0000 | [diff] [blame] | 2513 | static QualType UnwrapTypeForDebugInfo(QualType T, const ASTContext &C) { |
| 2514 | Qualifiers Quals; |
Guy Benyei | 11169dd | 2012-12-18 14:30:41 +0000 | [diff] [blame] | 2515 | do { |
Adrian Prantl | 179af90 | 2013-09-26 21:35:50 +0000 | [diff] [blame] | 2516 | Qualifiers InnerQuals = T.getLocalQualifiers(); |
| 2517 | // Qualifiers::operator+() doesn't like it if you add a Qualifier |
| 2518 | // that is already there. |
| 2519 | Quals += Qualifiers::removeCommonQualifiers(Quals, InnerQuals); |
| 2520 | Quals += InnerQuals; |
Guy Benyei | 11169dd | 2012-12-18 14:30:41 +0000 | [diff] [blame] | 2521 | QualType LastT = T; |
| 2522 | switch (T->getTypeClass()) { |
| 2523 | default: |
David Blaikie | 0549106 | 2013-01-21 04:37:12 +0000 | [diff] [blame] | 2524 | return C.getQualifiedType(T.getTypePtr(), Quals); |
David Blaikie | f1b382e | 2014-04-06 17:14:06 +0000 | [diff] [blame] | 2525 | case Type::TemplateSpecialization: { |
| 2526 | const auto *Spec = cast<TemplateSpecializationType>(T); |
| 2527 | if (Spec->isTypeAlias()) |
| 2528 | return C.getQualifiedType(T.getTypePtr(), Quals); |
| 2529 | T = Spec->desugar(); |
Eric Christopher | e7b87e5 | 2014-10-26 23:40:33 +0000 | [diff] [blame] | 2530 | break; |
| 2531 | } |
Guy Benyei | 11169dd | 2012-12-18 14:30:41 +0000 | [diff] [blame] | 2532 | case Type::TypeOfExpr: |
| 2533 | T = cast<TypeOfExprType>(T)->getUnderlyingExpr()->getType(); |
| 2534 | break; |
| 2535 | case Type::TypeOf: |
| 2536 | T = cast<TypeOfType>(T)->getUnderlyingType(); |
| 2537 | break; |
| 2538 | case Type::Decltype: |
| 2539 | T = cast<DecltypeType>(T)->getUnderlyingType(); |
| 2540 | break; |
| 2541 | case Type::UnaryTransform: |
| 2542 | T = cast<UnaryTransformType>(T)->getUnderlyingType(); |
| 2543 | break; |
| 2544 | case Type::Attributed: |
| 2545 | T = cast<AttributedType>(T)->getEquivalentType(); |
| 2546 | break; |
| 2547 | case Type::Elaborated: |
| 2548 | T = cast<ElaboratedType>(T)->getNamedType(); |
| 2549 | break; |
| 2550 | case Type::Paren: |
| 2551 | T = cast<ParenType>(T)->getInnerType(); |
| 2552 | break; |
David Blaikie | 0549106 | 2013-01-21 04:37:12 +0000 | [diff] [blame] | 2553 | case Type::SubstTemplateTypeParm: |
Guy Benyei | 11169dd | 2012-12-18 14:30:41 +0000 | [diff] [blame] | 2554 | T = cast<SubstTemplateTypeParmType>(T)->getReplacementType(); |
Guy Benyei | 11169dd | 2012-12-18 14:30:41 +0000 | [diff] [blame] | 2555 | break; |
Richard Smith | a0abc42 | 2017-02-22 00:13:14 +0000 | [diff] [blame] | 2556 | case Type::Auto: |
| 2557 | case Type::DeducedTemplateSpecialization: { |
| 2558 | QualType DT = cast<DeducedType>(T)->getDeducedType(); |
David Blaikie | 42edade | 2014-11-11 20:44:45 +0000 | [diff] [blame] | 2559 | assert(!DT.isNull() && "Undeduced types shouldn't reach here."); |
David Blaikie | 22c460a0 | 2013-05-24 21:24:35 +0000 | [diff] [blame] | 2560 | T = DT; |
Guy Benyei | 11169dd | 2012-12-18 14:30:41 +0000 | [diff] [blame] | 2561 | break; |
| 2562 | } |
Jordan Rose | 303e2f1 | 2016-11-10 23:28:17 +0000 | [diff] [blame] | 2563 | case Type::Adjusted: |
| 2564 | case Type::Decayed: |
| 2565 | // Decayed and adjusted types use the adjusted type in LLVM and DWARF. |
| 2566 | T = cast<AdjustedType>(T)->getAdjustedType(); |
| 2567 | break; |
| 2568 | } |
Eric Christopher | b2a008c | 2013-05-16 00:45:12 +0000 | [diff] [blame] | 2569 | |
Guy Benyei | 11169dd | 2012-12-18 14:30:41 +0000 | [diff] [blame] | 2570 | assert(T != LastT && "Type unwrapping failed to unwrap!"); |
NAKAMURA Takumi | 3e0a363 | 2013-01-21 10:51:28 +0000 | [diff] [blame] | 2571 | (void)LastT; |
Guy Benyei | 11169dd | 2012-12-18 14:30:41 +0000 | [diff] [blame] | 2572 | } while (true); |
| 2573 | } |
| 2574 | |
Duncan P. N. Exon Smith | 9dd4e4e | 2015-04-29 16:40:08 +0000 | [diff] [blame] | 2575 | llvm::DIType *CGDebugInfo::getTypeOrNull(QualType Ty) { |
Guy Benyei | 11169dd | 2012-12-18 14:30:41 +0000 | [diff] [blame] | 2576 | |
| 2577 | // Unwrap the type as needed for debug information. |
David Blaikie | 0549106 | 2013-01-21 04:37:12 +0000 | [diff] [blame] | 2578 | Ty = UnwrapTypeForDebugInfo(Ty, CGM.getContext()); |
Eric Christopher | b2a008c | 2013-05-16 00:45:12 +0000 | [diff] [blame] | 2579 | |
David Blaikie | f427b00 | 2014-05-06 03:42:01 +0000 | [diff] [blame] | 2580 | auto it = TypeCache.find(Ty.getAsOpaquePtr()); |
Guy Benyei | 11169dd | 2012-12-18 14:30:41 +0000 | [diff] [blame] | 2581 | if (it != TypeCache.end()) { |
| 2582 | // Verify that the debug info still exists. |
Duncan P. N. Exon Smith | fb49491 | 2014-12-09 18:39:32 +0000 | [diff] [blame] | 2583 | if (llvm::Metadata *V = it->second) |
Duncan P. N. Exon Smith | 9dd4e4e | 2015-04-29 16:40:08 +0000 | [diff] [blame] | 2584 | return cast<llvm::DIType>(V); |
Guy Benyei | 11169dd | 2012-12-18 14:30:41 +0000 | [diff] [blame] | 2585 | } |
| 2586 | |
Duncan P. N. Exon Smith | c755128 | 2015-04-06 23:21:33 +0000 | [diff] [blame] | 2587 | return nullptr; |
Guy Benyei | 11169dd | 2012-12-18 14:30:41 +0000 | [diff] [blame] | 2588 | } |
| 2589 | |
David Blaikie | 0e716b4 | 2014-03-03 23:48:23 +0000 | [diff] [blame] | 2590 | void CGDebugInfo::completeTemplateDefinition( |
| 2591 | const ClassTemplateSpecializationDecl &SD) { |
Benjamin Kramer | 8c30592 | 2016-02-02 11:06:51 +0000 | [diff] [blame] | 2592 | if (DebugKind <= codegenoptions::DebugLineTablesOnly) |
David Blaikie | 0856f66 | 2014-03-04 22:01:08 +0000 | [diff] [blame] | 2593 | return; |
David Blaikie | 1ac9c98 | 2017-04-11 21:13:37 +0000 | [diff] [blame] | 2594 | completeUnusedClass(SD); |
| 2595 | } |
David Blaikie | 0856f66 | 2014-03-04 22:01:08 +0000 | [diff] [blame] | 2596 | |
David Blaikie | 1ac9c98 | 2017-04-11 21:13:37 +0000 | [diff] [blame] | 2597 | void CGDebugInfo::completeUnusedClass(const CXXRecordDecl &D) { |
| 2598 | if (DebugKind <= codegenoptions::DebugLineTablesOnly) |
| 2599 | return; |
| 2600 | |
| 2601 | completeClassData(&D); |
David Blaikie | 0e716b4 | 2014-03-03 23:48:23 +0000 | [diff] [blame] | 2602 | // In case this type has no member function definitions being emitted, ensure |
| 2603 | // it is retained |
David Blaikie | 1ac9c98 | 2017-04-11 21:13:37 +0000 | [diff] [blame] | 2604 | RetainedTypes.push_back(CGM.getContext().getRecordType(&D).getAsOpaquePtr()); |
David Blaikie | 0e716b4 | 2014-03-03 23:48:23 +0000 | [diff] [blame] | 2605 | } |
| 2606 | |
Duncan P. N. Exon Smith | 9dd4e4e | 2015-04-29 16:40:08 +0000 | [diff] [blame] | 2607 | llvm::DIType *CGDebugInfo::getOrCreateType(QualType Ty, llvm::DIFile *Unit) { |
Guy Benyei | 11169dd | 2012-12-18 14:30:41 +0000 | [diff] [blame] | 2608 | if (Ty.isNull()) |
Duncan P. N. Exon Smith | 0b6c369 | 2015-04-20 18:51:48 +0000 | [diff] [blame] | 2609 | return nullptr; |
Guy Benyei | 11169dd | 2012-12-18 14:30:41 +0000 | [diff] [blame] | 2610 | |
| 2611 | // Unwrap the type as needed for debug information. |
David Blaikie | 0549106 | 2013-01-21 04:37:12 +0000 | [diff] [blame] | 2612 | Ty = UnwrapTypeForDebugInfo(Ty, CGM.getContext()); |
Guy Benyei | 11169dd | 2012-12-18 14:30:41 +0000 | [diff] [blame] | 2613 | |
Duncan P. N. Exon Smith | 0b6c369 | 2015-04-20 18:51:48 +0000 | [diff] [blame] | 2614 | if (auto *T = getTypeOrNull(Ty)) |
Guy Benyei | 11169dd | 2012-12-18 14:30:41 +0000 | [diff] [blame] | 2615 | return T; |
| 2616 | |
Adrian Prantl | ca84418 | 2015-09-11 17:23:03 +0000 | [diff] [blame] | 2617 | llvm::DIType *Res = CreateTypeNode(Ty, Unit); |
Adrian Prantl | 5c8bd88 | 2015-09-11 17:23:08 +0000 | [diff] [blame] | 2618 | void* TyPtr = Ty.getAsOpaquePtr(); |
Adrian Prantl | 73409ce | 2013-03-11 18:33:46 +0000 | [diff] [blame] | 2619 | |
| 2620 | // And update the type cache. |
Duncan P. N. Exon Smith | fb49491 | 2014-12-09 18:39:32 +0000 | [diff] [blame] | 2621 | TypeCache[TyPtr].reset(Res); |
Guy Benyei | 11169dd | 2012-12-18 14:30:41 +0000 | [diff] [blame] | 2622 | |
Guy Benyei | 11169dd | 2012-12-18 14:30:41 +0000 | [diff] [blame] | 2623 | return Res; |
| 2624 | } |
| 2625 | |
Adrian Prantl | 5c8bd88 | 2015-09-11 17:23:08 +0000 | [diff] [blame] | 2626 | llvm::DIModule *CGDebugInfo::getParentModuleOrNull(const Decl *D) { |
Adrian Prantl | 335f5c7 | 2015-10-02 17:36:14 +0000 | [diff] [blame] | 2627 | // A forward declaration inside a module header does not belong to the module. |
| 2628 | if (isa<RecordDecl>(D) && !cast<RecordDecl>(D)->getDefinition()) |
| 2629 | return nullptr; |
Adrian Prantl | 85d938a | 2015-09-21 17:48:37 +0000 | [diff] [blame] | 2630 | if (DebugTypeExtRefs && D->isFromASTFile()) { |
| 2631 | // Record a reference to an imported clang module or precompiled header. |
| 2632 | auto *Reader = CGM.getContext().getExternalSource(); |
| 2633 | auto Idx = D->getOwningModuleID(); |
| 2634 | auto Info = Reader->getSourceDescriptor(Idx); |
| 2635 | if (Info) |
| 2636 | return getOrCreateModuleRef(*Info, /*SkeletonCU=*/true); |
| 2637 | } else if (ClangModuleMap) { |
Adrian Prantl | 9402cef | 2015-09-20 16:51:35 +0000 | [diff] [blame] | 2638 | // We are building a clang module or a precompiled header. |
| 2639 | // |
| 2640 | // TODO: When D is a CXXRecordDecl or a C++ Enum, the ODR applies |
| 2641 | // and it wouldn't be necessary to specify the parent scope |
| 2642 | // because the type is already unique by definition (it would look |
| 2643 | // like the output of -fno-standalone-debug). On the other hand, |
| 2644 | // the parent scope helps a consumer to quickly locate the object |
| 2645 | // file where the type's definition is located, so it might be |
| 2646 | // best to make this behavior a command line or debugger tuning |
| 2647 | // option. |
| 2648 | FullSourceLoc Loc(D->getLocation(), CGM.getContext().getSourceManager()); |
Richard Smith | 54f0440 | 2017-05-18 02:29:20 +0000 | [diff] [blame] | 2649 | if (Module *M = D->getOwningModule()) { |
Adrian Prantl | aa5d08d | 2016-01-22 21:14:41 +0000 | [diff] [blame] | 2650 | // This is a (sub-)module. |
Adrian Prantl | 9402cef | 2015-09-20 16:51:35 +0000 | [diff] [blame] | 2651 | auto Info = ExternalASTSource::ASTSourceDescriptor(*M); |
| 2652 | return getOrCreateModuleRef(Info, /*SkeletonCU=*/false); |
Adrian Prantl | aa5d08d | 2016-01-22 21:14:41 +0000 | [diff] [blame] | 2653 | } else { |
| 2654 | // This the precompiled header being built. |
| 2655 | return getOrCreateModuleRef(PCHDescriptor, /*SkeletonCU=*/false); |
Adrian Prantl | 9402cef | 2015-09-20 16:51:35 +0000 | [diff] [blame] | 2656 | } |
| 2657 | } |
Adrian Prantl | 5c8bd88 | 2015-09-11 17:23:08 +0000 | [diff] [blame] | 2658 | |
Adrian Prantl | 9402cef | 2015-09-20 16:51:35 +0000 | [diff] [blame] | 2659 | return nullptr; |
Adrian Prantl | 5c8bd88 | 2015-09-11 17:23:08 +0000 | [diff] [blame] | 2660 | } |
| 2661 | |
Duncan P. N. Exon Smith | 9dd4e4e | 2015-04-29 16:40:08 +0000 | [diff] [blame] | 2662 | llvm::DIType *CGDebugInfo::CreateTypeNode(QualType Ty, llvm::DIFile *Unit) { |
Guy Benyei | 11169dd | 2012-12-18 14:30:41 +0000 | [diff] [blame] | 2663 | // Handle qualifiers, which recursively handles what they refer to. |
| 2664 | if (Ty.hasLocalQualifiers()) |
David Blaikie | 99dab3b | 2013-09-04 22:03:57 +0000 | [diff] [blame] | 2665 | return CreateQualifiedType(Ty, Unit); |
Guy Benyei | 11169dd | 2012-12-18 14:30:41 +0000 | [diff] [blame] | 2666 | |
Guy Benyei | 11169dd | 2012-12-18 14:30:41 +0000 | [diff] [blame] | 2667 | // Work out details of type. |
| 2668 | switch (Ty->getTypeClass()) { |
| 2669 | #define TYPE(Class, Base) |
| 2670 | #define ABSTRACT_TYPE(Class, Base) |
| 2671 | #define NON_CANONICAL_TYPE(Class, Base) |
| 2672 | #define DEPENDENT_TYPE(Class, Base) case Type::Class: |
| 2673 | #include "clang/AST/TypeNodes.def" |
| 2674 | llvm_unreachable("Dependent types cannot show up in debug information"); |
| 2675 | |
| 2676 | case Type::ExtVector: |
| 2677 | case Type::Vector: |
| 2678 | return CreateType(cast<VectorType>(Ty), Unit); |
| 2679 | case Type::ObjCObjectPointer: |
| 2680 | return CreateType(cast<ObjCObjectPointerType>(Ty), Unit); |
| 2681 | case Type::ObjCObject: |
| 2682 | return CreateType(cast<ObjCObjectType>(Ty), Unit); |
Manman Ren | e6be26c | 2016-09-13 17:25:08 +0000 | [diff] [blame] | 2683 | case Type::ObjCTypeParam: |
| 2684 | return CreateType(cast<ObjCTypeParamType>(Ty), Unit); |
Guy Benyei | 11169dd | 2012-12-18 14:30:41 +0000 | [diff] [blame] | 2685 | case Type::ObjCInterface: |
| 2686 | return CreateType(cast<ObjCInterfaceType>(Ty), Unit); |
| 2687 | case Type::Builtin: |
| 2688 | return CreateType(cast<BuiltinType>(Ty)); |
| 2689 | case Type::Complex: |
| 2690 | return CreateType(cast<ComplexType>(Ty)); |
| 2691 | case Type::Pointer: |
| 2692 | return CreateType(cast<PointerType>(Ty), Unit); |
| 2693 | case Type::BlockPointer: |
| 2694 | return CreateType(cast<BlockPointerType>(Ty), Unit); |
| 2695 | case Type::Typedef: |
David Blaikie | 99dab3b | 2013-09-04 22:03:57 +0000 | [diff] [blame] | 2696 | return CreateType(cast<TypedefType>(Ty), Unit); |
Guy Benyei | 11169dd | 2012-12-18 14:30:41 +0000 | [diff] [blame] | 2697 | case Type::Record: |
David Blaikie | 99dab3b | 2013-09-04 22:03:57 +0000 | [diff] [blame] | 2698 | return CreateType(cast<RecordType>(Ty)); |
Guy Benyei | 11169dd | 2012-12-18 14:30:41 +0000 | [diff] [blame] | 2699 | case Type::Enum: |
Manman Ren | 1b45702 | 2013-08-28 21:20:28 +0000 | [diff] [blame] | 2700 | return CreateEnumType(cast<EnumType>(Ty)); |
Guy Benyei | 11169dd | 2012-12-18 14:30:41 +0000 | [diff] [blame] | 2701 | case Type::FunctionProto: |
| 2702 | case Type::FunctionNoProto: |
| 2703 | return CreateType(cast<FunctionType>(Ty), Unit); |
| 2704 | case Type::ConstantArray: |
| 2705 | case Type::VariableArray: |
| 2706 | case Type::IncompleteArray: |
| 2707 | return CreateType(cast<ArrayType>(Ty), Unit); |
| 2708 | |
| 2709 | case Type::LValueReference: |
| 2710 | return CreateType(cast<LValueReferenceType>(Ty), Unit); |
| 2711 | case Type::RValueReference: |
| 2712 | return CreateType(cast<RValueReferenceType>(Ty), Unit); |
| 2713 | |
| 2714 | case Type::MemberPointer: |
| 2715 | return CreateType(cast<MemberPointerType>(Ty), Unit); |
| 2716 | |
| 2717 | case Type::Atomic: |
| 2718 | return CreateType(cast<AtomicType>(Ty), Unit); |
| 2719 | |
Xiuli Pan | 9c14e28 | 2016-01-09 12:53:17 +0000 | [diff] [blame] | 2720 | case Type::Pipe: |
| 2721 | return CreateType(cast<PipeType>(Ty), Unit); |
| 2722 | |
Guy Benyei | 11169dd | 2012-12-18 14:30:41 +0000 | [diff] [blame] | 2723 | case Type::TemplateSpecialization: |
David Blaikie | f1b382e | 2014-04-06 17:14:06 +0000 | [diff] [blame] | 2724 | return CreateType(cast<TemplateSpecializationType>(Ty), Unit); |
| 2725 | |
David Blaikie | 42edade | 2014-11-11 20:44:45 +0000 | [diff] [blame] | 2726 | case Type::Auto: |
David Blaikie | f1b382e | 2014-04-06 17:14:06 +0000 | [diff] [blame] | 2727 | case Type::Attributed: |
Jordan Rose | 303e2f1 | 2016-11-10 23:28:17 +0000 | [diff] [blame] | 2728 | case Type::Adjusted: |
| 2729 | case Type::Decayed: |
Richard Smith | 600b526 | 2017-01-26 20:40:47 +0000 | [diff] [blame] | 2730 | case Type::DeducedTemplateSpecialization: |
Guy Benyei | 11169dd | 2012-12-18 14:30:41 +0000 | [diff] [blame] | 2731 | case Type::Elaborated: |
| 2732 | case Type::Paren: |
| 2733 | case Type::SubstTemplateTypeParm: |
| 2734 | case Type::TypeOfExpr: |
| 2735 | case Type::TypeOf: |
| 2736 | case Type::Decltype: |
| 2737 | case Type::UnaryTransform: |
David Blaikie | 66ed89d | 2013-07-13 21:08:08 +0000 | [diff] [blame] | 2738 | case Type::PackExpansion: |
David Blaikie | 22c460a0 | 2013-05-24 21:24:35 +0000 | [diff] [blame] | 2739 | break; |
Guy Benyei | 11169dd | 2012-12-18 14:30:41 +0000 | [diff] [blame] | 2740 | } |
Eric Christopher | b2a008c | 2013-05-16 00:45:12 +0000 | [diff] [blame] | 2741 | |
David Blaikie | 42edade | 2014-11-11 20:44:45 +0000 | [diff] [blame] | 2742 | llvm_unreachable("type should have been unwrapped!"); |
Guy Benyei | 11169dd | 2012-12-18 14:30:41 +0000 | [diff] [blame] | 2743 | } |
| 2744 | |
Duncan P. N. Exon Smith | bd210e6 | 2015-07-24 20:34:41 +0000 | [diff] [blame] | 2745 | llvm::DICompositeType *CGDebugInfo::getOrCreateLimitedType(const RecordType *Ty, |
| 2746 | llvm::DIFile *Unit) { |
David Blaikie | 4a2b5ef | 2013-08-12 22:24:20 +0000 | [diff] [blame] | 2747 | QualType QTy(Ty, 0); |
Guy Benyei | 11169dd | 2012-12-18 14:30:41 +0000 | [diff] [blame] | 2748 | |
Duncan P. N. Exon Smith | bd210e6 | 2015-07-24 20:34:41 +0000 | [diff] [blame] | 2749 | auto *T = cast_or_null<llvm::DICompositeType>(getTypeOrNull(QTy)); |
Guy Benyei | 11169dd | 2012-12-18 14:30:41 +0000 | [diff] [blame] | 2750 | |
| 2751 | // We may have cached a forward decl when we could have created |
| 2752 | // a non-forward decl. Go ahead and create a non-forward decl |
| 2753 | // now. |
Duncan P. N. Exon Smith | 4caa7f2 | 2015-04-16 01:00:56 +0000 | [diff] [blame] | 2754 | if (T && !T->isForwardDecl()) |
Eric Christopher | e7b87e5 | 2014-10-26 23:40:33 +0000 | [diff] [blame] | 2755 | return T; |
Guy Benyei | 11169dd | 2012-12-18 14:30:41 +0000 | [diff] [blame] | 2756 | |
| 2757 | // Otherwise create the type. |
Duncan P. N. Exon Smith | 9dd4e4e | 2015-04-29 16:40:08 +0000 | [diff] [blame] | 2758 | llvm::DICompositeType *Res = CreateLimitedType(Ty); |
David Blaikie | 8d5e128 | 2013-08-20 21:03:29 +0000 | [diff] [blame] | 2759 | |
| 2760 | // Propagate members from the declaration to the definition |
| 2761 | // CreateType(const RecordType*) will overwrite this with the members in the |
| 2762 | // correct order if the full type is needed. |
Duncan P. N. Exon Smith | 9dd4e4e | 2015-04-29 16:40:08 +0000 | [diff] [blame] | 2763 | DBuilder.replaceArrays(Res, T ? T->getElements() : llvm::DINodeArray()); |
Guy Benyei | 11169dd | 2012-12-18 14:30:41 +0000 | [diff] [blame] | 2764 | |
Guy Benyei | 11169dd | 2012-12-18 14:30:41 +0000 | [diff] [blame] | 2765 | // And update the type cache. |
Duncan P. N. Exon Smith | fb49491 | 2014-12-09 18:39:32 +0000 | [diff] [blame] | 2766 | TypeCache[QTy.getAsOpaquePtr()].reset(Res); |
Guy Benyei | 11169dd | 2012-12-18 14:30:41 +0000 | [diff] [blame] | 2767 | return Res; |
| 2768 | } |
| 2769 | |
| 2770 | // 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] | 2771 | llvm::DICompositeType *CGDebugInfo::CreateLimitedType(const RecordType *Ty) { |
Guy Benyei | 11169dd | 2012-12-18 14:30:41 +0000 | [diff] [blame] | 2772 | RecordDecl *RD = Ty->getDecl(); |
Eric Christopher | b2a008c | 2013-05-16 00:45:12 +0000 | [diff] [blame] | 2773 | |
Guy Benyei | 11169dd | 2012-12-18 14:30:41 +0000 | [diff] [blame] | 2774 | // 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] | 2775 | llvm::DIFile *DefUnit = getOrCreateFile(RD->getLocation()); |
Guy Benyei | 11169dd | 2012-12-18 14:30:41 +0000 | [diff] [blame] | 2776 | unsigned Line = getLineNumber(RD->getLocation()); |
| 2777 | StringRef RDName = getClassName(RD); |
| 2778 | |
Amjad Aboud | dc4531e | 2016-04-30 01:44:38 +0000 | [diff] [blame] | 2779 | llvm::DIScope *RDContext = getDeclContextDescriptor(RD); |
Guy Benyei | 11169dd | 2012-12-18 14:30:41 +0000 | [diff] [blame] | 2780 | |
David Blaikie | d278589 | 2013-08-18 17:36:19 +0000 | [diff] [blame] | 2781 | // If we ended up creating the type during the context chain construction, |
| 2782 | // just return that. |
Duncan P. N. Exon Smith | 9dd4e4e | 2015-04-29 16:40:08 +0000 | [diff] [blame] | 2783 | auto *T = cast_or_null<llvm::DICompositeType>( |
Duncan P. N. Exon Smith | c755128 | 2015-04-06 23:21:33 +0000 | [diff] [blame] | 2784 | getTypeOrNull(CGM.getContext().getRecordType(RD))); |
Duncan P. N. Exon Smith | 4caa7f2 | 2015-04-16 01:00:56 +0000 | [diff] [blame] | 2785 | if (T && (!T->isForwardDecl() || !RD->getDefinition())) |
Eric Christopher | e7b87e5 | 2014-10-26 23:40:33 +0000 | [diff] [blame] | 2786 | return T; |
David Blaikie | d278589 | 2013-08-18 17:36:19 +0000 | [diff] [blame] | 2787 | |
Adrian Prantl | 381e755 | 2014-02-04 21:29:50 +0000 | [diff] [blame] | 2788 | // If this is just a forward or incomplete declaration, construct an |
| 2789 | // appropriately marked node and just return it. |
| 2790 | const RecordDecl *D = RD->getDefinition(); |
| 2791 | if (!D || !D->isCompleteDefinition()) |
Manman Ren | 1b45702 | 2013-08-28 21:20:28 +0000 | [diff] [blame] | 2792 | return getOrCreateRecordFwdDecl(Ty, RDContext); |
Guy Benyei | 11169dd | 2012-12-18 14:30:41 +0000 | [diff] [blame] | 2793 | |
| 2794 | uint64_t Size = CGM.getContext().getTypeSize(Ty); |
Victor Leschuk | a7ece03 | 2016-10-20 00:13:19 +0000 | [diff] [blame] | 2795 | auto Align = getDeclAlignIfRequired(D, CGM.getContext()); |
Eric Christopher | b2a008c | 2013-05-16 00:45:12 +0000 | [diff] [blame] | 2796 | |
Manman Ren | e0064d8 | 2013-08-29 23:19:58 +0000 | [diff] [blame] | 2797 | SmallString<256> FullName = getUniqueTagTypeName(Ty, CGM, TheCU); |
| 2798 | |
Duncan P. N. Exon Smith | 9dd4e4e | 2015-04-29 16:40:08 +0000 | [diff] [blame] | 2799 | llvm::DICompositeType *RealDecl = DBuilder.createReplaceableCompositeType( |
Leny Kholodov | 80c047d | 2016-09-06 10:48:04 +0000 | [diff] [blame] | 2800 | getTagForRecord(RD), RDName, RDContext, DefUnit, Line, 0, Size, Align, |
| 2801 | llvm::DINode::FlagZero, FullName); |
Guy Benyei | 11169dd | 2012-12-18 14:30:41 +0000 | [diff] [blame] | 2802 | |
Duncan P. N. Exon Smith | f9521b0 | 2016-04-17 07:45:08 +0000 | [diff] [blame] | 2803 | // Elements of composite types usually have back to the type, creating |
| 2804 | // uniquing cycles. Distinct nodes are more efficient. |
| 2805 | switch (RealDecl->getTag()) { |
| 2806 | default: |
| 2807 | llvm_unreachable("invalid composite type tag"); |
| 2808 | |
| 2809 | case llvm::dwarf::DW_TAG_array_type: |
| 2810 | case llvm::dwarf::DW_TAG_enumeration_type: |
| 2811 | // Array elements and most enumeration elements don't have back references, |
| 2812 | // so they don't tend to be involved in uniquing cycles and there is some |
| 2813 | // chance of merging them when linking together two modules. Only make |
| 2814 | // them distinct if they are ODR-uniqued. |
| 2815 | if (FullName.empty()) |
| 2816 | break; |
Galina Kistanova | 0872d6c | 2017-06-03 06:30:46 +0000 | [diff] [blame] | 2817 | LLVM_FALLTHROUGH; |
Duncan P. N. Exon Smith | f9521b0 | 2016-04-17 07:45:08 +0000 | [diff] [blame] | 2818 | |
| 2819 | case llvm::dwarf::DW_TAG_structure_type: |
| 2820 | case llvm::dwarf::DW_TAG_union_type: |
| 2821 | case llvm::dwarf::DW_TAG_class_type: |
| 2822 | // Immediatley resolve to a distinct node. |
| 2823 | RealDecl = |
| 2824 | llvm::MDNode::replaceWithDistinct(llvm::TempDICompositeType(RealDecl)); |
| 2825 | break; |
| 2826 | } |
| 2827 | |
Duncan P. N. Exon Smith | fb49491 | 2014-12-09 18:39:32 +0000 | [diff] [blame] | 2828 | RegionMap[Ty->getDecl()].reset(RealDecl); |
| 2829 | TypeCache[QualType(Ty, 0).getAsOpaquePtr()].reset(RealDecl); |
Guy Benyei | 11169dd | 2012-12-18 14:30:41 +0000 | [diff] [blame] | 2830 | |
David Majnemer | 58ed0f3 | 2016-07-17 00:39:12 +0000 | [diff] [blame] | 2831 | if (const auto *TSpecial = dyn_cast<ClassTemplateSpecializationDecl>(RD)) |
Duncan P. N. Exon Smith | 9dd4e4e | 2015-04-29 16:40:08 +0000 | [diff] [blame] | 2832 | DBuilder.replaceArrays(RealDecl, llvm::DINodeArray(), |
Duncan P. N. Exon Smith | c8ee63e | 2014-12-18 00:48:56 +0000 | [diff] [blame] | 2833 | CollectCXXTemplateParams(TSpecial, DefUnit)); |
David Blaikie | 952dac3 | 2013-08-15 22:42:12 +0000 | [diff] [blame] | 2834 | return RealDecl; |
Guy Benyei | 11169dd | 2012-12-18 14:30:41 +0000 | [diff] [blame] | 2835 | } |
| 2836 | |
David Blaikie | adfbf99 | 2013-08-18 16:55:33 +0000 | [diff] [blame] | 2837 | void CGDebugInfo::CollectContainingType(const CXXRecordDecl *RD, |
Duncan P. N. Exon Smith | 9dd4e4e | 2015-04-29 16:40:08 +0000 | [diff] [blame] | 2838 | llvm::DICompositeType *RealDecl) { |
David Blaikie | adfbf99 | 2013-08-18 16:55:33 +0000 | [diff] [blame] | 2839 | // 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] | 2840 | llvm::DICompositeType *ContainingType = nullptr; |
David Blaikie | adfbf99 | 2013-08-18 16:55:33 +0000 | [diff] [blame] | 2841 | const ASTRecordLayout &RL = CGM.getContext().getASTRecordLayout(RD); |
| 2842 | if (const CXXRecordDecl *PBase = RL.getPrimaryBase()) { |
Alp Toker | d473363 | 2013-12-05 04:47:09 +0000 | [diff] [blame] | 2843 | // Seek non-virtual primary base root. |
David Blaikie | adfbf99 | 2013-08-18 16:55:33 +0000 | [diff] [blame] | 2844 | while (1) { |
| 2845 | const ASTRecordLayout &BRL = CGM.getContext().getASTRecordLayout(PBase); |
| 2846 | const CXXRecordDecl *PBT = BRL.getPrimaryBase(); |
| 2847 | if (PBT && !BRL.isPrimaryBaseVirtual()) |
| 2848 | PBase = PBT; |
| 2849 | else |
| 2850 | break; |
| 2851 | } |
Duncan P. N. Exon Smith | 9dd4e4e | 2015-04-29 16:40:08 +0000 | [diff] [blame] | 2852 | ContainingType = cast<llvm::DICompositeType>( |
David Blaikie | adfbf99 | 2013-08-18 16:55:33 +0000 | [diff] [blame] | 2853 | getOrCreateType(QualType(PBase->getTypeForDecl(), 0), |
| 2854 | getOrCreateFile(RD->getLocation()))); |
| 2855 | } else if (RD->isDynamicClass()) |
| 2856 | ContainingType = RealDecl; |
| 2857 | |
Duncan P. N. Exon Smith | c8ee63e | 2014-12-18 00:48:56 +0000 | [diff] [blame] | 2858 | DBuilder.replaceVTableHolder(RealDecl, ContainingType); |
David Blaikie | adfbf99 | 2013-08-18 16:55:33 +0000 | [diff] [blame] | 2859 | } |
| 2860 | |
Duncan P. N. Exon Smith | 9dd4e4e | 2015-04-29 16:40:08 +0000 | [diff] [blame] | 2861 | llvm::DIType *CGDebugInfo::CreateMemberType(llvm::DIFile *Unit, QualType FType, |
Duncan P. N. Exon Smith | 0b6c369 | 2015-04-20 18:51:48 +0000 | [diff] [blame] | 2862 | StringRef Name, uint64_t *Offset) { |
Duncan P. N. Exon Smith | 9dd4e4e | 2015-04-29 16:40:08 +0000 | [diff] [blame] | 2863 | llvm::DIType *FieldTy = CGDebugInfo::getOrCreateType(FType, Unit); |
Guy Benyei | 11169dd | 2012-12-18 14:30:41 +0000 | [diff] [blame] | 2864 | uint64_t FieldSize = CGM.getContext().getTypeSize(FType); |
Victor Leschuk | a7ece03 | 2016-10-20 00:13:19 +0000 | [diff] [blame] | 2865 | auto FieldAlign = getTypeAlignIfRequired(FType, CGM.getContext()); |
Leny Kholodov | df050fd | 2016-09-06 17:06:14 +0000 | [diff] [blame] | 2866 | llvm::DIType *Ty = |
| 2867 | DBuilder.createMemberType(Unit, Name, Unit, 0, FieldSize, FieldAlign, |
| 2868 | *Offset, llvm::DINode::FlagZero, FieldTy); |
Guy Benyei | 11169dd | 2012-12-18 14:30:41 +0000 | [diff] [blame] | 2869 | *Offset += FieldSize; |
| 2870 | return Ty; |
| 2871 | } |
| 2872 | |
Duncan P. N. Exon Smith | 9dd4e4e | 2015-04-29 16:40:08 +0000 | [diff] [blame] | 2873 | void CGDebugInfo::collectFunctionDeclProps(GlobalDecl GD, llvm::DIFile *Unit, |
Duncan P. N. Exon Smith | 8e47da4 | 2015-04-21 20:07:29 +0000 | [diff] [blame] | 2874 | StringRef &Name, |
| 2875 | StringRef &LinkageName, |
Duncan P. N. Exon Smith | 9dd4e4e | 2015-04-29 16:40:08 +0000 | [diff] [blame] | 2876 | llvm::DIScope *&FDContext, |
| 2877 | llvm::DINodeArray &TParamsArray, |
Leny Kholodov | 80c047d | 2016-09-06 10:48:04 +0000 | [diff] [blame] | 2878 | llvm::DINode::DIFlags &Flags) { |
David Majnemer | 58ed0f3 | 2016-07-17 00:39:12 +0000 | [diff] [blame] | 2879 | const auto *FD = cast<FunctionDecl>(GD.getDecl()); |
Frederic Riss | 9db79f1 | 2014-11-18 03:40:46 +0000 | [diff] [blame] | 2880 | Name = getFunctionName(FD); |
| 2881 | // Use mangled name as linkage name for C/C++ functions. |
| 2882 | if (FD->hasPrototype()) { |
| 2883 | LinkageName = CGM.getMangledName(GD); |
Duncan P. N. Exon Smith | 9dd4e4e | 2015-04-29 16:40:08 +0000 | [diff] [blame] | 2884 | Flags |= llvm::DINode::FlagPrototyped; |
Frederic Riss | 9db79f1 | 2014-11-18 03:40:46 +0000 | [diff] [blame] | 2885 | } |
| 2886 | // No need to replicate the linkage name if it isn't different from the |
| 2887 | // 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] | 2888 | // 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] | 2889 | if (LinkageName == Name || (!CGM.getCodeGenOpts().EmitGcovArcs && |
| 2890 | !CGM.getCodeGenOpts().EmitGcovNotes && |
Dehao Chen | b3a70de | 2017-01-19 00:44:21 +0000 | [diff] [blame] | 2891 | !CGM.getCodeGenOpts().DebugInfoForProfiling && |
Benjamin Kramer | 8c30592 | 2016-02-02 11:06:51 +0000 | [diff] [blame] | 2892 | DebugKind <= codegenoptions::DebugLineTablesOnly)) |
Frederic Riss | 9db79f1 | 2014-11-18 03:40:46 +0000 | [diff] [blame] | 2893 | LinkageName = StringRef(); |
| 2894 | |
Benjamin Kramer | 8c30592 | 2016-02-02 11:06:51 +0000 | [diff] [blame] | 2895 | if (DebugKind >= codegenoptions::LimitedDebugInfo) { |
Frederic Riss | 9db79f1 | 2014-11-18 03:40:46 +0000 | [diff] [blame] | 2896 | if (const NamespaceDecl *NSDecl = |
Adrian Prantl | 6fc8875 | 2017-05-16 23:46:10 +0000 | [diff] [blame] | 2897 | dyn_cast_or_null<NamespaceDecl>(FD->getDeclContext())) |
Adrian Prantl | ddb8e06 | 2017-05-12 16:23:53 +0000 | [diff] [blame] | 2898 | FDContext = getOrCreateNamespace(NSDecl); |
Frederic Riss | 9db79f1 | 2014-11-18 03:40:46 +0000 | [diff] [blame] | 2899 | else if (const RecordDecl *RDecl = |
Adrian Prantl | 5c8bd88 | 2015-09-11 17:23:08 +0000 | [diff] [blame] | 2900 | dyn_cast_or_null<RecordDecl>(FD->getDeclContext())) { |
| 2901 | llvm::DIScope *Mod = getParentModuleOrNull(RDecl); |
| 2902 | FDContext = getContextDescriptor(RDecl, Mod ? Mod : TheCU); |
| 2903 | } |
Adrian Prantl | fd5ac8a | 2016-08-17 16:20:32 +0000 | [diff] [blame] | 2904 | // Check if it is a noreturn-marked function |
| 2905 | if (FD->isNoReturn()) |
| 2906 | Flags |= llvm::DINode::FlagNoReturn; |
Frederic Riss | 9db79f1 | 2014-11-18 03:40:46 +0000 | [diff] [blame] | 2907 | // Collect template parameters. |
| 2908 | TParamsArray = CollectFunctionTemplateParams(FD, Unit); |
| 2909 | } |
| 2910 | } |
| 2911 | |
Duncan P. N. Exon Smith | 9dd4e4e | 2015-04-29 16:40:08 +0000 | [diff] [blame] | 2912 | void CGDebugInfo::collectVarDeclProps(const VarDecl *VD, llvm::DIFile *&Unit, |
Frederic Riss | 9db79f1 | 2014-11-18 03:40:46 +0000 | [diff] [blame] | 2913 | unsigned &LineNo, QualType &T, |
| 2914 | StringRef &Name, StringRef &LinkageName, |
Duncan P. N. Exon Smith | 9dd4e4e | 2015-04-29 16:40:08 +0000 | [diff] [blame] | 2915 | llvm::DIScope *&VDContext) { |
Frederic Riss | 9db79f1 | 2014-11-18 03:40:46 +0000 | [diff] [blame] | 2916 | Unit = getOrCreateFile(VD->getLocation()); |
| 2917 | LineNo = getLineNumber(VD->getLocation()); |
| 2918 | |
| 2919 | setLocation(VD->getLocation()); |
| 2920 | |
| 2921 | T = VD->getType(); |
| 2922 | if (T->isIncompleteArrayType()) { |
| 2923 | // CodeGen turns int[] into int[1] so we'll do the same here. |
| 2924 | llvm::APInt ConstVal(32, 1); |
| 2925 | QualType ET = CGM.getContext().getAsArrayType(T)->getElementType(); |
| 2926 | |
| 2927 | T = CGM.getContext().getConstantArrayType(ET, ConstVal, |
| 2928 | ArrayType::Normal, 0); |
| 2929 | } |
| 2930 | |
| 2931 | Name = VD->getName(); |
| 2932 | if (VD->getDeclContext() && !isa<FunctionDecl>(VD->getDeclContext()) && |
| 2933 | !isa<ObjCMethodDecl>(VD->getDeclContext())) |
| 2934 | LinkageName = CGM.getMangledName(VD); |
| 2935 | if (LinkageName == Name) |
| 2936 | LinkageName = StringRef(); |
| 2937 | |
| 2938 | // Since we emit declarations (DW_AT_members) for static members, place the |
| 2939 | // definition of those static members in the namespace they were declared in |
| 2940 | // in the source code (the lexical decl context). |
| 2941 | // FIXME: Generalize this for even non-member global variables where the |
| 2942 | // declaration and definition may have different lexical decl contexts, once |
| 2943 | // we have support for emitting declarations of (non-member) global variables. |
Saleem Abdulrasool | cd187f0 | 2015-02-28 00:13:13 +0000 | [diff] [blame] | 2944 | const DeclContext *DC = VD->isStaticDataMember() ? VD->getLexicalDeclContext() |
| 2945 | : VD->getDeclContext(); |
| 2946 | // When a record type contains an in-line initialization of a static data |
| 2947 | // member, and the record type is marked as __declspec(dllexport), an implicit |
| 2948 | // definition of the member will be created in the record context. DWARF |
| 2949 | // doesn't seem to have a nice way to describe this in a form that consumers |
| 2950 | // are likely to understand, so fake the "normal" situation of a definition |
| 2951 | // outside the class by putting it in the global scope. |
| 2952 | if (DC->isRecord()) |
| 2953 | DC = CGM.getContext().getTranslationUnitDecl(); |
Adrian Prantl | 5c8bd88 | 2015-09-11 17:23:08 +0000 | [diff] [blame] | 2954 | |
Amjad Aboud | dc4531e | 2016-04-30 01:44:38 +0000 | [diff] [blame] | 2955 | llvm::DIScope *Mod = getParentModuleOrNull(VD); |
| 2956 | VDContext = getContextDescriptor(cast<Decl>(DC), Mod ? Mod : TheCU); |
Frederic Riss | 9db79f1 | 2014-11-18 03:40:46 +0000 | [diff] [blame] | 2957 | } |
| 2958 | |
Adrian Prantl | b7acfc0 | 2017-02-27 21:30:05 +0000 | [diff] [blame] | 2959 | llvm::DISubprogram *CGDebugInfo::getFunctionFwdDeclOrStub(GlobalDecl GD, |
| 2960 | bool Stub) { |
Duncan P. N. Exon Smith | 9dd4e4e | 2015-04-29 16:40:08 +0000 | [diff] [blame] | 2961 | llvm::DINodeArray TParamsArray; |
Frederic Riss | d253ed6 | 2014-11-18 03:40:51 +0000 | [diff] [blame] | 2962 | StringRef Name, LinkageName; |
Leny Kholodov | 80c047d | 2016-09-06 10:48:04 +0000 | [diff] [blame] | 2963 | llvm::DINode::DIFlags Flags = llvm::DINode::FlagZero; |
Adrian Prantl | b7acfc0 | 2017-02-27 21:30:05 +0000 | [diff] [blame] | 2964 | SourceLocation Loc = GD.getDecl()->getLocation(); |
Duncan P. N. Exon Smith | 9dd4e4e | 2015-04-29 16:40:08 +0000 | [diff] [blame] | 2965 | llvm::DIFile *Unit = getOrCreateFile(Loc); |
| 2966 | llvm::DIScope *DContext = Unit; |
Frederic Riss | d253ed6 | 2014-11-18 03:40:51 +0000 | [diff] [blame] | 2967 | unsigned Line = getLineNumber(Loc); |
Adrian Prantl | b7acfc0 | 2017-02-27 21:30:05 +0000 | [diff] [blame] | 2968 | collectFunctionDeclProps(GD, Unit, Name, LinkageName, DContext, |
Frederic Riss | d253ed6 | 2014-11-18 03:40:51 +0000 | [diff] [blame] | 2969 | TParamsArray, Flags); |
Adrian Prantl | b7acfc0 | 2017-02-27 21:30:05 +0000 | [diff] [blame] | 2970 | auto *FD = dyn_cast<FunctionDecl>(GD.getDecl()); |
| 2971 | |
Frederic Riss | d253ed6 | 2014-11-18 03:40:51 +0000 | [diff] [blame] | 2972 | // Build function type. |
| 2973 | SmallVector<QualType, 16> ArgTypes; |
Adrian Prantl | b7acfc0 | 2017-02-27 21:30:05 +0000 | [diff] [blame] | 2974 | if (FD) |
| 2975 | for (const ParmVarDecl *Parm : FD->parameters()) |
| 2976 | ArgTypes.push_back(Parm->getType()); |
Reid Kleckner | f00f803 | 2016-06-08 20:41:54 +0000 | [diff] [blame] | 2977 | CallingConv CC = FD->getType()->castAs<FunctionType>()->getCallConv(); |
| 2978 | QualType FnType = CGM.getContext().getFunctionType( |
| 2979 | FD->getReturnType(), ArgTypes, FunctionProtoType::ExtProtoInfo(CC)); |
Adrian Prantl | b7acfc0 | 2017-02-27 21:30:05 +0000 | [diff] [blame] | 2980 | if (Stub) { |
| 2981 | return DBuilder.createFunction( |
| 2982 | DContext, Name, LinkageName, Unit, Line, |
| 2983 | getOrCreateFunctionType(GD.getDecl(), FnType, Unit), |
| 2984 | !FD->isExternallyVisible(), |
| 2985 | /* isDefinition = */ true, 0, Flags, CGM.getLangOpts().Optimize, |
| 2986 | TParamsArray.get(), getFunctionDeclaration(FD)); |
| 2987 | } |
| 2988 | |
Duncan P. N. Exon Smith | 9dd4e4e | 2015-04-29 16:40:08 +0000 | [diff] [blame] | 2989 | llvm::DISubprogram *SP = DBuilder.createTempFunctionFwdDecl( |
Duncan P. N. Exon Smith | ebad0aa | 2015-04-07 16:50:49 +0000 | [diff] [blame] | 2990 | DContext, Name, LinkageName, Unit, Line, |
Adrian Prantl | b7acfc0 | 2017-02-27 21:30:05 +0000 | [diff] [blame] | 2991 | getOrCreateFunctionType(GD.getDecl(), FnType, Unit), |
| 2992 | !FD->isExternallyVisible(), |
Peter Collingbourne | 0900fe0 | 2015-11-05 22:04:14 +0000 | [diff] [blame] | 2993 | /* isDefinition = */ false, 0, Flags, CGM.getLangOpts().Optimize, |
Duncan P. N. Exon Smith | ebad0aa | 2015-04-07 16:50:49 +0000 | [diff] [blame] | 2994 | TParamsArray.get(), getFunctionDeclaration(FD)); |
David Majnemer | 58ed0f3 | 2016-07-17 00:39:12 +0000 | [diff] [blame] | 2995 | const auto *CanonDecl = cast<FunctionDecl>(FD->getCanonicalDecl()); |
Duncan P. N. Exon Smith | 4078ad4 | 2015-04-16 16:36:45 +0000 | [diff] [blame] | 2996 | FwdDeclReplaceMap.emplace_back(std::piecewise_construct, |
| 2997 | std::make_tuple(CanonDecl), |
| 2998 | std::make_tuple(SP)); |
Frederic Riss | d253ed6 | 2014-11-18 03:40:51 +0000 | [diff] [blame] | 2999 | return SP; |
| 3000 | } |
| 3001 | |
Adrian Prantl | b7acfc0 | 2017-02-27 21:30:05 +0000 | [diff] [blame] | 3002 | llvm::DISubprogram * |
| 3003 | CGDebugInfo::getFunctionForwardDeclaration(GlobalDecl GD) { |
| 3004 | return getFunctionFwdDeclOrStub(GD, /* Stub = */ false); |
| 3005 | } |
| 3006 | |
| 3007 | llvm::DISubprogram * |
| 3008 | CGDebugInfo::getFunctionStub(GlobalDecl GD) { |
| 3009 | return getFunctionFwdDeclOrStub(GD, /* Stub = */ true); |
| 3010 | } |
| 3011 | |
Duncan P. N. Exon Smith | 9dd4e4e | 2015-04-29 16:40:08 +0000 | [diff] [blame] | 3012 | llvm::DIGlobalVariable * |
Frederic Riss | d253ed6 | 2014-11-18 03:40:51 +0000 | [diff] [blame] | 3013 | CGDebugInfo::getGlobalVariableForwardDeclaration(const VarDecl *VD) { |
| 3014 | QualType T; |
| 3015 | StringRef Name, LinkageName; |
| 3016 | SourceLocation Loc = VD->getLocation(); |
Duncan P. N. Exon Smith | 9dd4e4e | 2015-04-29 16:40:08 +0000 | [diff] [blame] | 3017 | llvm::DIFile *Unit = getOrCreateFile(Loc); |
| 3018 | llvm::DIScope *DContext = Unit; |
Frederic Riss | d253ed6 | 2014-11-18 03:40:51 +0000 | [diff] [blame] | 3019 | unsigned Line = getLineNumber(Loc); |
| 3020 | |
| 3021 | collectVarDeclProps(VD, Unit, Line, T, Name, LinkageName, DContext); |
Victor Leschuk | a7ece03 | 2016-10-20 00:13:19 +0000 | [diff] [blame] | 3022 | auto Align = getDeclAlignIfRequired(VD, CGM.getContext()); |
Duncan P. N. Exon Smith | dadc2b6 | 2015-04-21 18:43:54 +0000 | [diff] [blame] | 3023 | auto *GV = DBuilder.createTempGlobalVariableFwdDecl( |
| 3024 | DContext, Name, LinkageName, Unit, Line, getOrCreateType(T, Unit), |
Adrian Prantl | 5f4740d | 2016-12-20 02:10:02 +0000 | [diff] [blame] | 3025 | !VD->isExternallyVisible(), nullptr, Align); |
Duncan P. N. Exon Smith | fb49491 | 2014-12-09 18:39:32 +0000 | [diff] [blame] | 3026 | FwdDeclReplaceMap.emplace_back( |
| 3027 | std::piecewise_construct, |
| 3028 | std::make_tuple(cast<VarDecl>(VD->getCanonicalDecl())), |
| 3029 | std::make_tuple(static_cast<llvm::Metadata *>(GV))); |
Frederic Riss | d253ed6 | 2014-11-18 03:40:51 +0000 | [diff] [blame] | 3030 | return GV; |
| 3031 | } |
| 3032 | |
Duncan P. N. Exon Smith | 9dd4e4e | 2015-04-29 16:40:08 +0000 | [diff] [blame] | 3033 | llvm::DINode *CGDebugInfo::getDeclarationOrDefinition(const Decl *D) { |
David Blaikie | bd48376 | 2013-05-20 04:58:53 +0000 | [diff] [blame] | 3034 | // We only need a declaration (not a definition) of the type - so use whatever |
| 3035 | // we would otherwise do to get a type for a pointee. (forward declarations in |
| 3036 | // limited debug info, full definitions (if the type definition is available) |
| 3037 | // in unlimited debug info) |
David Majnemer | 58ed0f3 | 2016-07-17 00:39:12 +0000 | [diff] [blame] | 3038 | if (const auto *TD = dyn_cast<TypeDecl>(D)) |
David Blaikie | 6b7d060c | 2013-08-12 23:14:36 +0000 | [diff] [blame] | 3039 | return getOrCreateType(CGM.getContext().getTypeDeclType(TD), |
David Blaikie | 99dab3b | 2013-09-04 22:03:57 +0000 | [diff] [blame] | 3040 | getOrCreateFile(TD->getLocation())); |
Duncan P. N. Exon Smith | fb49491 | 2014-12-09 18:39:32 +0000 | [diff] [blame] | 3041 | auto I = DeclCache.find(D->getCanonicalDecl()); |
Frederic Riss | d253ed6 | 2014-11-18 03:40:51 +0000 | [diff] [blame] | 3042 | |
Adrian Prantl | 5f4740d | 2016-12-20 02:10:02 +0000 | [diff] [blame] | 3043 | if (I != DeclCache.end()) { |
| 3044 | auto N = I->second; |
| 3045 | if (auto *GVE = dyn_cast_or_null<llvm::DIGlobalVariableExpression>(N)) |
| 3046 | return GVE->getVariable(); |
| 3047 | return dyn_cast_or_null<llvm::DINode>(N); |
| 3048 | } |
Frederic Riss | d253ed6 | 2014-11-18 03:40:51 +0000 | [diff] [blame] | 3049 | |
| 3050 | // No definition for now. Emit a forward definition that might be |
| 3051 | // merged with a potential upcoming definition. |
David Majnemer | 58ed0f3 | 2016-07-17 00:39:12 +0000 | [diff] [blame] | 3052 | if (const auto *FD = dyn_cast<FunctionDecl>(D)) |
Frederic Riss | d253ed6 | 2014-11-18 03:40:51 +0000 | [diff] [blame] | 3053 | return getFunctionForwardDeclaration(FD); |
| 3054 | else if (const auto *VD = dyn_cast<VarDecl>(D)) |
| 3055 | return getGlobalVariableForwardDeclaration(VD); |
| 3056 | |
Duncan P. N. Exon Smith | 4078ad4 | 2015-04-16 16:36:45 +0000 | [diff] [blame] | 3057 | return nullptr; |
David Blaikie | bd48376 | 2013-05-20 04:58:53 +0000 | [diff] [blame] | 3058 | } |
| 3059 | |
Duncan P. N. Exon Smith | 9dd4e4e | 2015-04-29 16:40:08 +0000 | [diff] [blame] | 3060 | llvm::DISubprogram *CGDebugInfo::getFunctionDeclaration(const Decl *D) { |
Benjamin Kramer | 8c30592 | 2016-02-02 11:06:51 +0000 | [diff] [blame] | 3061 | if (!D || DebugKind <= codegenoptions::DebugLineTablesOnly) |
Duncan P. N. Exon Smith | a7fbcbf | 2015-04-20 22:09:57 +0000 | [diff] [blame] | 3062 | return nullptr; |
David Blaikie | 18cfbc5 | 2013-06-22 00:09:36 +0000 | [diff] [blame] | 3063 | |
David Majnemer | 58ed0f3 | 2016-07-17 00:39:12 +0000 | [diff] [blame] | 3064 | const auto *FD = dyn_cast<FunctionDecl>(D); |
Eric Christopher | e7b87e5 | 2014-10-26 23:40:33 +0000 | [diff] [blame] | 3065 | if (!FD) |
Duncan P. N. Exon Smith | a7fbcbf | 2015-04-20 22:09:57 +0000 | [diff] [blame] | 3066 | return nullptr; |
Guy Benyei | 11169dd | 2012-12-18 14:30:41 +0000 | [diff] [blame] | 3067 | |
| 3068 | // Setup context. |
Adrian Prantl | 6ec370a | 2015-09-10 18:39:45 +0000 | [diff] [blame] | 3069 | auto *S = getDeclContextDescriptor(D); |
Guy Benyei | 11169dd | 2012-12-18 14:30:41 +0000 | [diff] [blame] | 3070 | |
Duncan P. N. Exon Smith | fb49491 | 2014-12-09 18:39:32 +0000 | [diff] [blame] | 3071 | auto MI = SPCache.find(FD->getCanonicalDecl()); |
David Blaikie | fd07c60 | 2013-08-09 17:20:05 +0000 | [diff] [blame] | 3072 | if (MI == SPCache.end()) { |
David Majnemer | 58ed0f3 | 2016-07-17 00:39:12 +0000 | [diff] [blame] | 3073 | if (const auto *MD = dyn_cast<CXXMethodDecl>(FD->getCanonicalDecl())) { |
Duncan P. N. Exon Smith | c09c548 | 2015-04-20 21:17:26 +0000 | [diff] [blame] | 3074 | return CreateCXXMemberFunction(MD, getOrCreateFile(MD->getLocation()), |
Duncan P. N. Exon Smith | 9dd4e4e | 2015-04-29 16:40:08 +0000 | [diff] [blame] | 3075 | cast<llvm::DICompositeType>(S)); |
David Blaikie | fd07c60 | 2013-08-09 17:20:05 +0000 | [diff] [blame] | 3076 | } |
| 3077 | } |
Guy Benyei | 11169dd | 2012-12-18 14:30:41 +0000 | [diff] [blame] | 3078 | if (MI != SPCache.end()) { |
Duncan P. N. Exon Smith | 9dd4e4e | 2015-04-29 16:40:08 +0000 | [diff] [blame] | 3079 | 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] | 3080 | if (SP && !SP->isDefinition()) |
Guy Benyei | 11169dd | 2012-12-18 14:30:41 +0000 | [diff] [blame] | 3081 | return SP; |
| 3082 | } |
| 3083 | |
Aaron Ballman | 86c9390 | 2014-03-06 23:45:36 +0000 | [diff] [blame] | 3084 | for (auto NextFD : FD->redecls()) { |
Duncan P. N. Exon Smith | fb49491 | 2014-12-09 18:39:32 +0000 | [diff] [blame] | 3085 | auto MI = SPCache.find(NextFD->getCanonicalDecl()); |
Guy Benyei | 11169dd | 2012-12-18 14:30:41 +0000 | [diff] [blame] | 3086 | if (MI != SPCache.end()) { |
Duncan P. N. Exon Smith | 9dd4e4e | 2015-04-29 16:40:08 +0000 | [diff] [blame] | 3087 | 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] | 3088 | if (SP && !SP->isDefinition()) |
Guy Benyei | 11169dd | 2012-12-18 14:30:41 +0000 | [diff] [blame] | 3089 | return SP; |
| 3090 | } |
| 3091 | } |
Duncan P. N. Exon Smith | a7fbcbf | 2015-04-20 22:09:57 +0000 | [diff] [blame] | 3092 | return nullptr; |
Guy Benyei | 11169dd | 2012-12-18 14:30:41 +0000 | [diff] [blame] | 3093 | } |
| 3094 | |
Duncan P. N. Exon Smith | 0b6c369 | 2015-04-20 18:51:48 +0000 | [diff] [blame] | 3095 | // getOrCreateFunctionType - Construct type. If it is a c++ method, include |
Guy Benyei | 11169dd | 2012-12-18 14:30:41 +0000 | [diff] [blame] | 3096 | // implicit parameter "this". |
Duncan P. N. Exon Smith | 9dd4e4e | 2015-04-29 16:40:08 +0000 | [diff] [blame] | 3097 | llvm::DISubroutineType *CGDebugInfo::getOrCreateFunctionType(const Decl *D, |
Duncan P. N. Exon Smith | 4078ad4 | 2015-04-16 16:36:45 +0000 | [diff] [blame] | 3098 | QualType FnType, |
Duncan P. N. Exon Smith | 9dd4e4e | 2015-04-29 16:40:08 +0000 | [diff] [blame] | 3099 | llvm::DIFile *F) { |
Benjamin Kramer | 8c30592 | 2016-02-02 11:06:51 +0000 | [diff] [blame] | 3100 | if (!D || DebugKind <= codegenoptions::DebugLineTablesOnly) |
Duncan P. N. Exon Smith | a7fbcbf | 2015-04-20 22:09:57 +0000 | [diff] [blame] | 3101 | // Create fake but valid subroutine type. Otherwise -verify would fail, and |
| 3102 | // 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] | 3103 | return DBuilder.createSubroutineType(DBuilder.getOrCreateTypeArray(None)); |
Guy Benyei | 11169dd | 2012-12-18 14:30:41 +0000 | [diff] [blame] | 3104 | |
David Majnemer | 58ed0f3 | 2016-07-17 00:39:12 +0000 | [diff] [blame] | 3105 | if (const auto *Method = dyn_cast<CXXMethodDecl>(D)) |
Guy Benyei | 11169dd | 2012-12-18 14:30:41 +0000 | [diff] [blame] | 3106 | return getOrCreateMethodType(Method, F); |
Reid Kleckner | f00f803 | 2016-06-08 20:41:54 +0000 | [diff] [blame] | 3107 | |
| 3108 | const auto *FTy = FnType->getAs<FunctionType>(); |
| 3109 | CallingConv CC = FTy ? FTy->getCallConv() : CallingConv::CC_C; |
| 3110 | |
David Majnemer | 58ed0f3 | 2016-07-17 00:39:12 +0000 | [diff] [blame] | 3111 | if (const auto *OMethod = dyn_cast<ObjCMethodDecl>(D)) { |
Guy Benyei | 11169dd | 2012-12-18 14:30:41 +0000 | [diff] [blame] | 3112 | // Add "self" and "_cmd" |
Duncan P. N. Exon Smith | fb49491 | 2014-12-09 18:39:32 +0000 | [diff] [blame] | 3113 | SmallVector<llvm::Metadata *, 16> Elts; |
Guy Benyei | 11169dd | 2012-12-18 14:30:41 +0000 | [diff] [blame] | 3114 | |
| 3115 | // First element is always return type. For 'void' functions it is NULL. |
Alp Toker | 314cc81 | 2014-01-25 16:55:45 +0000 | [diff] [blame] | 3116 | QualType ResultTy = OMethod->getReturnType(); |
Adrian Prantl | 5f36010 | 2013-05-22 21:37:49 +0000 | [diff] [blame] | 3117 | |
| 3118 | // Replace the instancetype keyword with the actual type. |
| 3119 | if (ResultTy == CGM.getContext().getObjCInstanceType()) |
| 3120 | ResultTy = CGM.getContext().getPointerType( |
Eric Christopher | e7b87e5 | 2014-10-26 23:40:33 +0000 | [diff] [blame] | 3121 | QualType(OMethod->getClassInterface()->getTypeForDecl(), 0)); |
Adrian Prantl | 5f36010 | 2013-05-22 21:37:49 +0000 | [diff] [blame] | 3122 | |
Adrian Prantl | 7bec903 | 2013-05-10 21:08:31 +0000 | [diff] [blame] | 3123 | Elts.push_back(getOrCreateType(ResultTy, F)); |
Guy Benyei | 11169dd | 2012-12-18 14:30:41 +0000 | [diff] [blame] | 3124 | // "self" pointer is always first argument. |
Adrian Prantl | 748a6cd | 2015-09-08 20:41:52 +0000 | [diff] [blame] | 3125 | QualType SelfDeclTy; |
| 3126 | if (auto *SelfDecl = OMethod->getSelfDecl()) |
| 3127 | SelfDeclTy = SelfDecl->getType(); |
| 3128 | else if (auto *FPT = dyn_cast<FunctionProtoType>(FnType)) |
| 3129 | if (FPT->getNumParams() > 1) |
| 3130 | SelfDeclTy = FPT->getParamType(0); |
| 3131 | if (!SelfDeclTy.isNull()) |
| 3132 | Elts.push_back(CreateSelfType(SelfDeclTy, getOrCreateType(SelfDeclTy, F))); |
Guy Benyei | 11169dd | 2012-12-18 14:30:41 +0000 | [diff] [blame] | 3133 | // "_cmd" pointer is always second argument. |
Duncan P. N. Exon Smith | 0b6c369 | 2015-04-20 18:51:48 +0000 | [diff] [blame] | 3134 | Elts.push_back(DBuilder.createArtificialType( |
Adrian Prantl | 748a6cd | 2015-09-08 20:41:52 +0000 | [diff] [blame] | 3135 | getOrCreateType(CGM.getContext().getObjCSelType(), F))); |
Guy Benyei | 11169dd | 2012-12-18 14:30:41 +0000 | [diff] [blame] | 3136 | // Get rest of the arguments. |
David Majnemer | 59f7792 | 2016-06-24 04:05:48 +0000 | [diff] [blame] | 3137 | for (const auto *PI : OMethod->parameters()) |
Aaron Ballman | 43b68be | 2014-03-07 17:50:17 +0000 | [diff] [blame] | 3138 | Elts.push_back(getOrCreateType(PI->getType(), F)); |
Frederic Riss | 787d9d6 | 2014-08-12 04:42:23 +0000 | [diff] [blame] | 3139 | // Variadic methods need a special marker at the end of the type list. |
| 3140 | if (OMethod->isVariadic()) |
| 3141 | Elts.push_back(DBuilder.createUnspecifiedParameter()); |
Guy Benyei | 11169dd | 2012-12-18 14:30:41 +0000 | [diff] [blame] | 3142 | |
Duncan P. N. Exon Smith | 9dd4e4e | 2015-04-29 16:40:08 +0000 | [diff] [blame] | 3143 | llvm::DITypeRefArray EltTypeArray = DBuilder.getOrCreateTypeArray(Elts); |
Leny Kholodov | 80c047d | 2016-09-06 10:48:04 +0000 | [diff] [blame] | 3144 | return DBuilder.createSubroutineType(EltTypeArray, llvm::DINode::FlagZero, |
| 3145 | getDwarfCC(CC)); |
Guy Benyei | 11169dd | 2012-12-18 14:30:41 +0000 | [diff] [blame] | 3146 | } |
Adrian Prantl | d45ba25 | 2014-02-25 19:38:11 +0000 | [diff] [blame] | 3147 | |
Adrian Prantl | 800faef | 2014-02-25 23:42:18 +0000 | [diff] [blame] | 3148 | // Handle variadic function types; they need an additional |
| 3149 | // unspecified parameter. |
David Majnemer | 58ed0f3 | 2016-07-17 00:39:12 +0000 | [diff] [blame] | 3150 | if (const auto *FD = dyn_cast<FunctionDecl>(D)) |
Adrian Prantl | d45ba25 | 2014-02-25 19:38:11 +0000 | [diff] [blame] | 3151 | if (FD->isVariadic()) { |
Duncan P. N. Exon Smith | fb49491 | 2014-12-09 18:39:32 +0000 | [diff] [blame] | 3152 | SmallVector<llvm::Metadata *, 16> EltTys; |
Adrian Prantl | d45ba25 | 2014-02-25 19:38:11 +0000 | [diff] [blame] | 3153 | EltTys.push_back(getOrCreateType(FD->getReturnType(), F)); |
David Majnemer | 58ed0f3 | 2016-07-17 00:39:12 +0000 | [diff] [blame] | 3154 | if (const auto *FPT = dyn_cast<FunctionProtoType>(FnType)) |
| 3155 | for (QualType ParamType : FPT->param_types()) |
| 3156 | EltTys.push_back(getOrCreateType(ParamType, F)); |
Adrian Prantl | d45ba25 | 2014-02-25 19:38:11 +0000 | [diff] [blame] | 3157 | EltTys.push_back(DBuilder.createUnspecifiedParameter()); |
Duncan P. N. Exon Smith | 9dd4e4e | 2015-04-29 16:40:08 +0000 | [diff] [blame] | 3158 | llvm::DITypeRefArray EltTypeArray = DBuilder.getOrCreateTypeArray(EltTys); |
Leny Kholodov | 80c047d | 2016-09-06 10:48:04 +0000 | [diff] [blame] | 3159 | return DBuilder.createSubroutineType(EltTypeArray, llvm::DINode::FlagZero, |
| 3160 | getDwarfCC(CC)); |
Adrian Prantl | d45ba25 | 2014-02-25 19:38:11 +0000 | [diff] [blame] | 3161 | } |
| 3162 | |
Duncan P. N. Exon Smith | 9dd4e4e | 2015-04-29 16:40:08 +0000 | [diff] [blame] | 3163 | return cast<llvm::DISubroutineType>(getOrCreateType(FnType, F)); |
Guy Benyei | 11169dd | 2012-12-18 14:30:41 +0000 | [diff] [blame] | 3164 | } |
| 3165 | |
Eric Christopher | e7b87e5 | 2014-10-26 23:40:33 +0000 | [diff] [blame] | 3166 | void CGDebugInfo::EmitFunctionStart(GlobalDecl GD, SourceLocation Loc, |
| 3167 | SourceLocation ScopeLoc, QualType FnType, |
| 3168 | llvm::Function *Fn, CGBuilderTy &Builder) { |
Guy Benyei | 11169dd | 2012-12-18 14:30:41 +0000 | [diff] [blame] | 3169 | |
| 3170 | StringRef Name; |
| 3171 | StringRef LinkageName; |
| 3172 | |
| 3173 | FnBeginRegionCount.push_back(LexicalBlockStack.size()); |
| 3174 | |
| 3175 | const Decl *D = GD.getDecl(); |
Craig Topper | 8a13c41 | 2014-05-21 05:09:00 +0000 | [diff] [blame] | 3176 | bool HasDecl = (D != nullptr); |
Eric Christopher | 885c41b | 2014-04-01 22:25:28 +0000 | [diff] [blame] | 3177 | |
Leny Kholodov | 80c047d | 2016-09-06 10:48:04 +0000 | [diff] [blame] | 3178 | llvm::DINode::DIFlags Flags = llvm::DINode::FlagZero; |
Duncan P. N. Exon Smith | 9dd4e4e | 2015-04-29 16:40:08 +0000 | [diff] [blame] | 3179 | llvm::DIFile *Unit = getOrCreateFile(Loc); |
| 3180 | llvm::DIScope *FDContext = Unit; |
| 3181 | llvm::DINodeArray TParamsArray; |
Guy Benyei | 11169dd | 2012-12-18 14:30:41 +0000 | [diff] [blame] | 3182 | if (!HasDecl) { |
| 3183 | // Use llvm function name. |
David Blaikie | ebe87e1 | 2013-08-27 23:57:18 +0000 | [diff] [blame] | 3184 | LinkageName = Fn->getName(); |
David Majnemer | 58ed0f3 | 2016-07-17 00:39:12 +0000 | [diff] [blame] | 3185 | } else if (const auto *FD = dyn_cast<FunctionDecl>(D)) { |
Duncan P. N. Exon Smith | a7fbcbf | 2015-04-20 22:09:57 +0000 | [diff] [blame] | 3186 | // 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] | 3187 | auto FI = SPCache.find(FD->getCanonicalDecl()); |
Guy Benyei | 11169dd | 2012-12-18 14:30:41 +0000 | [diff] [blame] | 3188 | if (FI != SPCache.end()) { |
Duncan P. N. Exon Smith | 9dd4e4e | 2015-04-29 16:40:08 +0000 | [diff] [blame] | 3189 | 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] | 3190 | if (SP && SP->isDefinition()) { |
Duncan P. N. Exon Smith | d899f6e | 2015-04-18 00:07:30 +0000 | [diff] [blame] | 3191 | LexicalBlockStack.emplace_back(SP); |
Duncan P. N. Exon Smith | fb49491 | 2014-12-09 18:39:32 +0000 | [diff] [blame] | 3192 | RegionMap[D].reset(SP); |
Guy Benyei | 11169dd | 2012-12-18 14:30:41 +0000 | [diff] [blame] | 3193 | return; |
| 3194 | } |
| 3195 | } |
Frederic Riss | 9db79f1 | 2014-11-18 03:40:46 +0000 | [diff] [blame] | 3196 | collectFunctionDeclProps(GD, Unit, Name, LinkageName, FDContext, |
| 3197 | TParamsArray, Flags); |
David Majnemer | 58ed0f3 | 2016-07-17 00:39:12 +0000 | [diff] [blame] | 3198 | } else if (const auto *OMD = dyn_cast<ObjCMethodDecl>(D)) { |
Guy Benyei | 11169dd | 2012-12-18 14:30:41 +0000 | [diff] [blame] | 3199 | Name = getObjCMethodName(OMD); |
Duncan P. N. Exon Smith | 9dd4e4e | 2015-04-29 16:40:08 +0000 | [diff] [blame] | 3200 | Flags |= llvm::DINode::FlagPrototyped; |
Guy Benyei | 11169dd | 2012-12-18 14:30:41 +0000 | [diff] [blame] | 3201 | } else { |
| 3202 | // Use llvm function name. |
| 3203 | Name = Fn->getName(); |
Duncan P. N. Exon Smith | 9dd4e4e | 2015-04-29 16:40:08 +0000 | [diff] [blame] | 3204 | Flags |= llvm::DINode::FlagPrototyped; |
Guy Benyei | 11169dd | 2012-12-18 14:30:41 +0000 | [diff] [blame] | 3205 | } |
David Majnemer | 58ed0f3 | 2016-07-17 00:39:12 +0000 | [diff] [blame] | 3206 | if (Name.startswith("\01")) |
Guy Benyei | 11169dd | 2012-12-18 14:30:41 +0000 | [diff] [blame] | 3207 | Name = Name.substr(1); |
| 3208 | |
Adrian Prantl | 42d71b9 | 2014-04-10 23:21:53 +0000 | [diff] [blame] | 3209 | if (!HasDecl || D->isImplicit()) { |
Duncan P. N. Exon Smith | 9dd4e4e | 2015-04-29 16:40:08 +0000 | [diff] [blame] | 3210 | Flags |= llvm::DINode::FlagArtificial; |
Adrian Prantl | db76357 | 2016-11-09 21:43:51 +0000 | [diff] [blame] | 3211 | // Artificial functions should not silently reuse CurLoc. |
| 3212 | CurLoc = SourceLocation(); |
Adrian Prantl | 42d71b9 | 2014-04-10 23:21:53 +0000 | [diff] [blame] | 3213 | } |
| 3214 | unsigned LineNo = getLineNumber(Loc); |
| 3215 | unsigned ScopeLine = getLineNumber(ScopeLoc); |
Guy Benyei | 11169dd | 2012-12-18 14:30:41 +0000 | [diff] [blame] | 3216 | |
Eric Christopher | 8018e41 | 2014-03-27 18:50:35 +0000 | [diff] [blame] | 3217 | // FIXME: The function declaration we're constructing here is mostly reusing |
| 3218 | // declarations from CXXMethodDecl and not constructing new ones for arbitrary |
| 3219 | // FunctionDecls. When/if we fix this we can have FDContext be TheCU/null for |
| 3220 | // all subprograms instead of the actual context since subprogram definitions |
| 3221 | // are emitted as CU level entities by the backend. |
Duncan P. N. Exon Smith | 9dd4e4e | 2015-04-29 16:40:08 +0000 | [diff] [blame] | 3222 | llvm::DISubprogram *SP = DBuilder.createFunction( |
Eric Christopher | e7b87e5 | 2014-10-26 23:40:33 +0000 | [diff] [blame] | 3223 | FDContext, Name, LinkageName, Unit, LineNo, |
David Majnemer | 36a6e00 | 2016-07-06 21:07:53 +0000 | [diff] [blame] | 3224 | getOrCreateFunctionType(D, FnType, Unit), Fn->hasLocalLinkage(), |
Peter Collingbourne | 0900fe0 | 2015-11-05 22:04:14 +0000 | [diff] [blame] | 3225 | true /*definition*/, ScopeLine, Flags, CGM.getLangOpts().Optimize, |
Duncan P. N. Exon Smith | ebad0aa | 2015-04-07 16:50:49 +0000 | [diff] [blame] | 3226 | TParamsArray.get(), getFunctionDeclaration(D)); |
Peter Collingbourne | 0900fe0 | 2015-11-05 22:04:14 +0000 | [diff] [blame] | 3227 | Fn->setSubprogram(SP); |
Frederic Riss | b1ab28c | 2014-11-05 19:19:04 +0000 | [diff] [blame] | 3228 | // We might get here with a VarDecl in the case we're generating |
| 3229 | // code for the initialization of globals. Do not record these decls |
| 3230 | // as they will overwrite the actual VarDecl Decl in the cache. |
| 3231 | if (HasDecl && isa<FunctionDecl>(D)) |
David Majnemer | 58ed0f3 | 2016-07-17 00:39:12 +0000 | [diff] [blame] | 3232 | DeclCache[D->getCanonicalDecl()].reset(SP); |
Guy Benyei | 11169dd | 2012-12-18 14:30:41 +0000 | [diff] [blame] | 3233 | |
Adrian Prantl | bebb893 | 2014-03-21 21:01:58 +0000 | [diff] [blame] | 3234 | // Push the function onto the lexical block stack. |
Duncan P. N. Exon Smith | d899f6e | 2015-04-18 00:07:30 +0000 | [diff] [blame] | 3235 | LexicalBlockStack.emplace_back(SP); |
Adrian Prantl | bebb893 | 2014-03-21 21:01:58 +0000 | [diff] [blame] | 3236 | |
Guy Benyei | 11169dd | 2012-12-18 14:30:41 +0000 | [diff] [blame] | 3237 | if (HasDecl) |
Duncan P. N. Exon Smith | fb49491 | 2014-12-09 18:39:32 +0000 | [diff] [blame] | 3238 | RegionMap[D].reset(SP); |
Guy Benyei | 11169dd | 2012-12-18 14:30:41 +0000 | [diff] [blame] | 3239 | } |
| 3240 | |
Adrian Prantl | 748a6cd | 2015-09-08 20:41:52 +0000 | [diff] [blame] | 3241 | void CGDebugInfo::EmitFunctionDecl(GlobalDecl GD, SourceLocation Loc, |
| 3242 | QualType FnType) { |
| 3243 | StringRef Name; |
| 3244 | StringRef LinkageName; |
| 3245 | |
| 3246 | const Decl *D = GD.getDecl(); |
| 3247 | if (!D) |
| 3248 | return; |
| 3249 | |
Leny Kholodov | 80c047d | 2016-09-06 10:48:04 +0000 | [diff] [blame] | 3250 | llvm::DINode::DIFlags Flags = llvm::DINode::FlagZero; |
Adrian Prantl | 748a6cd | 2015-09-08 20:41:52 +0000 | [diff] [blame] | 3251 | llvm::DIFile *Unit = getOrCreateFile(Loc); |
Adrian Prantl | f0cd677 | 2015-10-04 23:23:04 +0000 | [diff] [blame] | 3252 | llvm::DIScope *FDContext = getDeclContextDescriptor(D); |
Adrian Prantl | 748a6cd | 2015-09-08 20:41:52 +0000 | [diff] [blame] | 3253 | llvm::DINodeArray TParamsArray; |
| 3254 | if (isa<FunctionDecl>(D)) { |
| 3255 | // If there is a DISubprogram for this function available then use it. |
| 3256 | collectFunctionDeclProps(GD, Unit, Name, LinkageName, FDContext, |
| 3257 | TParamsArray, Flags); |
David Majnemer | 58ed0f3 | 2016-07-17 00:39:12 +0000 | [diff] [blame] | 3258 | } else if (const auto *OMD = dyn_cast<ObjCMethodDecl>(D)) { |
Adrian Prantl | 748a6cd | 2015-09-08 20:41:52 +0000 | [diff] [blame] | 3259 | Name = getObjCMethodName(OMD); |
| 3260 | Flags |= llvm::DINode::FlagPrototyped; |
| 3261 | } else { |
| 3262 | llvm_unreachable("not a function or ObjC method"); |
| 3263 | } |
| 3264 | if (!Name.empty() && Name[0] == '\01') |
| 3265 | Name = Name.substr(1); |
| 3266 | |
| 3267 | if (D->isImplicit()) { |
| 3268 | Flags |= llvm::DINode::FlagArtificial; |
| 3269 | // Artificial functions without a location should not silently reuse CurLoc. |
| 3270 | if (Loc.isInvalid()) |
| 3271 | CurLoc = SourceLocation(); |
| 3272 | } |
| 3273 | unsigned LineNo = getLineNumber(Loc); |
| 3274 | unsigned ScopeLine = 0; |
| 3275 | |
Adrian Prantl | e76bda5 | 2016-04-15 15:55:45 +0000 | [diff] [blame] | 3276 | DBuilder.retainType(DBuilder.createFunction( |
| 3277 | FDContext, Name, LinkageName, Unit, LineNo, |
| 3278 | getOrCreateFunctionType(D, FnType, Unit), false /*internalLinkage*/, |
| 3279 | false /*definition*/, ScopeLine, Flags, CGM.getLangOpts().Optimize, |
| 3280 | TParamsArray.get(), getFunctionDeclaration(D))); |
Adrian Prantl | 748a6cd | 2015-09-08 20:41:52 +0000 | [diff] [blame] | 3281 | } |
| 3282 | |
Adrian Prantl | b7acfc0 | 2017-02-27 21:30:05 +0000 | [diff] [blame] | 3283 | void CGDebugInfo::EmitInlineFunctionStart(CGBuilderTy &Builder, GlobalDecl GD) { |
| 3284 | const auto *FD = cast<FunctionDecl>(GD.getDecl()); |
| 3285 | // If there is a subprogram for this function available then use it. |
| 3286 | auto FI = SPCache.find(FD->getCanonicalDecl()); |
| 3287 | llvm::DISubprogram *SP = nullptr; |
| 3288 | if (FI != SPCache.end()) |
| 3289 | SP = dyn_cast_or_null<llvm::DISubprogram>(FI->second); |
| 3290 | if (!SP) |
| 3291 | SP = getFunctionStub(GD); |
| 3292 | FnBeginRegionCount.push_back(LexicalBlockStack.size()); |
| 3293 | LexicalBlockStack.emplace_back(SP); |
| 3294 | setInlinedAt(Builder.getCurrentDebugLocation()); |
| 3295 | EmitLocation(Builder, FD->getLocation()); |
| 3296 | } |
| 3297 | |
| 3298 | void CGDebugInfo::EmitInlineFunctionEnd(CGBuilderTy &Builder) { |
| 3299 | assert(CurInlinedAt && "unbalanced inline scope stack"); |
Keno Fischer | 41d4b4e | 2017-06-01 21:14:03 +0000 | [diff] [blame] | 3300 | EmitFunctionEnd(Builder, nullptr); |
Adrian Prantl | b7acfc0 | 2017-02-27 21:30:05 +0000 | [diff] [blame] | 3301 | setInlinedAt(llvm::DebugLoc(CurInlinedAt).getInlinedAt()); |
| 3302 | } |
| 3303 | |
David Blaikie | 835afb2 | 2015-01-21 23:08:17 +0000 | [diff] [blame] | 3304 | void CGDebugInfo::EmitLocation(CGBuilderTy &Builder, SourceLocation Loc) { |
Guy Benyei | 11169dd | 2012-12-18 14:30:41 +0000 | [diff] [blame] | 3305 | // Update our current location |
| 3306 | setLocation(Loc); |
| 3307 | |
Eric Christopher | e7b87e5 | 2014-10-26 23:40:33 +0000 | [diff] [blame] | 3308 | if (CurLoc.isInvalid() || CurLoc.isMacroID()) |
| 3309 | return; |
Guy Benyei | 11169dd | 2012-12-18 14:30:41 +0000 | [diff] [blame] | 3310 | |
Adrian Prantl | e83b130 | 2014-01-07 22:05:52 +0000 | [diff] [blame] | 3311 | llvm::MDNode *Scope = LexicalBlockStack.back(); |
Eric Christopher | e7b87e5 | 2014-10-26 23:40:33 +0000 | [diff] [blame] | 3312 | Builder.SetCurrentDebugLocation(llvm::DebugLoc::get( |
Adrian Prantl | b7acfc0 | 2017-02-27 21:30:05 +0000 | [diff] [blame] | 3313 | getLineNumber(CurLoc), getColumnNumber(CurLoc), Scope, CurInlinedAt)); |
Guy Benyei | 11169dd | 2012-12-18 14:30:41 +0000 | [diff] [blame] | 3314 | } |
| 3315 | |
Guy Benyei | 11169dd | 2012-12-18 14:30:41 +0000 | [diff] [blame] | 3316 | void CGDebugInfo::CreateLexicalBlock(SourceLocation Loc) { |
Duncan P. N. Exon Smith | a66e305 | 2014-12-09 19:22:40 +0000 | [diff] [blame] | 3317 | llvm::MDNode *Back = nullptr; |
| 3318 | if (!LexicalBlockStack.empty()) |
| 3319 | Back = LexicalBlockStack.back().get(); |
Duncan P. N. Exon Smith | d899f6e | 2015-04-18 00:07:30 +0000 | [diff] [blame] | 3320 | LexicalBlockStack.emplace_back(DBuilder.createLexicalBlock( |
Duncan P. N. Exon Smith | 9dd4e4e | 2015-04-29 16:40:08 +0000 | [diff] [blame] | 3321 | cast<llvm::DIScope>(Back), getOrCreateFile(CurLoc), getLineNumber(CurLoc), |
Duncan P. N. Exon Smith | d899f6e | 2015-04-18 00:07:30 +0000 | [diff] [blame] | 3322 | getColumnNumber(CurLoc))); |
Guy Benyei | 11169dd | 2012-12-18 14:30:41 +0000 | [diff] [blame] | 3323 | } |
| 3324 | |
Konstantin Zhuravlyov | 2b4917f | 2017-03-09 18:06:23 +0000 | [diff] [blame] | 3325 | void CGDebugInfo::AppendAddressSpaceXDeref( |
| 3326 | unsigned AddressSpace, |
| 3327 | SmallVectorImpl<int64_t> &Expr) const { |
| 3328 | Optional<unsigned> DWARFAddressSpace = |
| 3329 | CGM.getTarget().getDWARFAddressSpace(AddressSpace); |
| 3330 | if (!DWARFAddressSpace) |
| 3331 | return; |
| 3332 | |
| 3333 | Expr.push_back(llvm::dwarf::DW_OP_constu); |
| 3334 | Expr.push_back(DWARFAddressSpace.getValue()); |
| 3335 | Expr.push_back(llvm::dwarf::DW_OP_swap); |
| 3336 | Expr.push_back(llvm::dwarf::DW_OP_xderef); |
| 3337 | } |
| 3338 | |
Eric Christopher | 0fdcb31 | 2013-05-16 00:52:20 +0000 | [diff] [blame] | 3339 | void CGDebugInfo::EmitLexicalBlockStart(CGBuilderTy &Builder, |
| 3340 | SourceLocation Loc) { |
Guy Benyei | 11169dd | 2012-12-18 14:30:41 +0000 | [diff] [blame] | 3341 | // Set our current location. |
| 3342 | setLocation(Loc); |
| 3343 | |
Guy Benyei | 11169dd | 2012-12-18 14:30:41 +0000 | [diff] [blame] | 3344 | // Emit a line table change for the current location inside the new scope. |
Adrian Prantl | b7acfc0 | 2017-02-27 21:30:05 +0000 | [diff] [blame] | 3345 | Builder.SetCurrentDebugLocation( |
| 3346 | llvm::DebugLoc::get(getLineNumber(Loc), getColumnNumber(Loc), |
| 3347 | LexicalBlockStack.back(), CurInlinedAt)); |
David Blaikie | 60a877b | 2014-10-22 19:34:33 +0000 | [diff] [blame] | 3348 | |
Benjamin Kramer | 8c30592 | 2016-02-02 11:06:51 +0000 | [diff] [blame] | 3349 | if (DebugKind <= codegenoptions::DebugLineTablesOnly) |
David Blaikie | 60a877b | 2014-10-22 19:34:33 +0000 | [diff] [blame] | 3350 | return; |
| 3351 | |
| 3352 | // Create a new lexical block and push it on the stack. |
| 3353 | CreateLexicalBlock(Loc); |
Guy Benyei | 11169dd | 2012-12-18 14:30:41 +0000 | [diff] [blame] | 3354 | } |
| 3355 | |
Eric Christopher | 0fdcb31 | 2013-05-16 00:52:20 +0000 | [diff] [blame] | 3356 | void CGDebugInfo::EmitLexicalBlockEnd(CGBuilderTy &Builder, |
| 3357 | SourceLocation Loc) { |
Guy Benyei | 11169dd | 2012-12-18 14:30:41 +0000 | [diff] [blame] | 3358 | assert(!LexicalBlockStack.empty() && "Region stack mismatch, stack empty!"); |
| 3359 | |
| 3360 | // Provide an entry in the line table for the end of the block. |
| 3361 | EmitLocation(Builder, Loc); |
| 3362 | |
Benjamin Kramer | 8c30592 | 2016-02-02 11:06:51 +0000 | [diff] [blame] | 3363 | if (DebugKind <= codegenoptions::DebugLineTablesOnly) |
David Blaikie | 60a877b | 2014-10-22 19:34:33 +0000 | [diff] [blame] | 3364 | return; |
| 3365 | |
Guy Benyei | 11169dd | 2012-12-18 14:30:41 +0000 | [diff] [blame] | 3366 | LexicalBlockStack.pop_back(); |
| 3367 | } |
| 3368 | |
Keno Fischer | 41d4b4e | 2017-06-01 21:14:03 +0000 | [diff] [blame] | 3369 | void CGDebugInfo::EmitFunctionEnd(CGBuilderTy &Builder, llvm::Function *Fn) { |
Guy Benyei | 11169dd | 2012-12-18 14:30:41 +0000 | [diff] [blame] | 3370 | assert(!LexicalBlockStack.empty() && "Region stack mismatch, stack empty!"); |
| 3371 | unsigned RCount = FnBeginRegionCount.back(); |
| 3372 | assert(RCount <= LexicalBlockStack.size() && "Region stack mismatch"); |
| 3373 | |
| 3374 | // Pop all regions for this function. |
David Blaikie | 60a877b | 2014-10-22 19:34:33 +0000 | [diff] [blame] | 3375 | while (LexicalBlockStack.size() != RCount) { |
| 3376 | // Provide an entry in the line table for the end of the block. |
| 3377 | EmitLocation(Builder, CurLoc); |
| 3378 | LexicalBlockStack.pop_back(); |
| 3379 | } |
Guy Benyei | 11169dd | 2012-12-18 14:30:41 +0000 | [diff] [blame] | 3380 | FnBeginRegionCount.pop_back(); |
Keno Fischer | 41d4b4e | 2017-06-01 21:14:03 +0000 | [diff] [blame] | 3381 | |
| 3382 | if (Fn && Fn->getSubprogram()) |
| 3383 | DBuilder.finalizeSubprogram(Fn->getSubprogram()); |
Guy Benyei | 11169dd | 2012-12-18 14:30:41 +0000 | [diff] [blame] | 3384 | } |
| 3385 | |
Duncan P. N. Exon Smith | 9dd4e4e | 2015-04-29 16:40:08 +0000 | [diff] [blame] | 3386 | llvm::DIType *CGDebugInfo::EmitTypeForVarWithBlocksAttr(const VarDecl *VD, |
Duncan P. N. Exon Smith | 0b6c369 | 2015-04-20 18:51:48 +0000 | [diff] [blame] | 3387 | uint64_t *XOffset) { |
Guy Benyei | 11169dd | 2012-12-18 14:30:41 +0000 | [diff] [blame] | 3388 | |
Duncan P. N. Exon Smith | fb49491 | 2014-12-09 18:39:32 +0000 | [diff] [blame] | 3389 | SmallVector<llvm::Metadata *, 5> EltTys; |
Guy Benyei | 11169dd | 2012-12-18 14:30:41 +0000 | [diff] [blame] | 3390 | QualType FType; |
| 3391 | uint64_t FieldSize, FieldOffset; |
Victor Leschuk | 802e4a5 | 2016-10-19 22:11:07 +0000 | [diff] [blame] | 3392 | uint32_t FieldAlign; |
Eric Christopher | b2a008c | 2013-05-16 00:45:12 +0000 | [diff] [blame] | 3393 | |
Duncan P. N. Exon Smith | 9dd4e4e | 2015-04-29 16:40:08 +0000 | [diff] [blame] | 3394 | llvm::DIFile *Unit = getOrCreateFile(VD->getLocation()); |
Eric Christopher | b2a008c | 2013-05-16 00:45:12 +0000 | [diff] [blame] | 3395 | QualType Type = VD->getType(); |
Guy Benyei | 11169dd | 2012-12-18 14:30:41 +0000 | [diff] [blame] | 3396 | |
| 3397 | FieldOffset = 0; |
| 3398 | FType = CGM.getContext().getPointerType(CGM.getContext().VoidTy); |
| 3399 | EltTys.push_back(CreateMemberType(Unit, FType, "__isa", &FieldOffset)); |
| 3400 | EltTys.push_back(CreateMemberType(Unit, FType, "__forwarding", &FieldOffset)); |
| 3401 | FType = CGM.getContext().IntTy; |
| 3402 | EltTys.push_back(CreateMemberType(Unit, FType, "__flags", &FieldOffset)); |
| 3403 | EltTys.push_back(CreateMemberType(Unit, FType, "__size", &FieldOffset)); |
| 3404 | |
| 3405 | bool HasCopyAndDispose = CGM.getContext().BlockRequiresCopying(Type, VD); |
| 3406 | if (HasCopyAndDispose) { |
| 3407 | FType = CGM.getContext().getPointerType(CGM.getContext().VoidTy); |
Eric Christopher | e7b87e5 | 2014-10-26 23:40:33 +0000 | [diff] [blame] | 3408 | EltTys.push_back( |
| 3409 | CreateMemberType(Unit, FType, "__copy_helper", &FieldOffset)); |
| 3410 | EltTys.push_back( |
| 3411 | CreateMemberType(Unit, FType, "__destroy_helper", &FieldOffset)); |
Guy Benyei | 11169dd | 2012-12-18 14:30:41 +0000 | [diff] [blame] | 3412 | } |
| 3413 | bool HasByrefExtendedLayout; |
| 3414 | Qualifiers::ObjCLifetime Lifetime; |
Eric Christopher | e7b87e5 | 2014-10-26 23:40:33 +0000 | [diff] [blame] | 3415 | if (CGM.getContext().getByrefLifetime(Type, Lifetime, |
| 3416 | HasByrefExtendedLayout) && |
| 3417 | HasByrefExtendedLayout) { |
Adrian Prantl | ead2ba4 | 2013-07-23 00:12:14 +0000 | [diff] [blame] | 3418 | FType = CGM.getContext().getPointerType(CGM.getContext().VoidTy); |
Eric Christopher | e7b87e5 | 2014-10-26 23:40:33 +0000 | [diff] [blame] | 3419 | EltTys.push_back( |
| 3420 | CreateMemberType(Unit, FType, "__byref_variable_layout", &FieldOffset)); |
Adrian Prantl | ead2ba4 | 2013-07-23 00:12:14 +0000 | [diff] [blame] | 3421 | } |
Eric Christopher | b2a008c | 2013-05-16 00:45:12 +0000 | [diff] [blame] | 3422 | |
Guy Benyei | 11169dd | 2012-12-18 14:30:41 +0000 | [diff] [blame] | 3423 | CharUnits Align = CGM.getContext().getDeclAlign(VD); |
| 3424 | if (Align > CGM.getContext().toCharUnitsFromBits( |
Eric Christopher | e7b87e5 | 2014-10-26 23:40:33 +0000 | [diff] [blame] | 3425 | CGM.getTarget().getPointerAlign(0))) { |
| 3426 | CharUnits FieldOffsetInBytes = |
| 3427 | CGM.getContext().toCharUnitsFromBits(FieldOffset); |
Rui Ueyama | 83aa979 | 2016-01-14 21:00:27 +0000 | [diff] [blame] | 3428 | CharUnits AlignedOffsetInBytes = FieldOffsetInBytes.alignTo(Align); |
Eric Christopher | e7b87e5 | 2014-10-26 23:40:33 +0000 | [diff] [blame] | 3429 | CharUnits NumPaddingBytes = AlignedOffsetInBytes - FieldOffsetInBytes; |
Eric Christopher | b2a008c | 2013-05-16 00:45:12 +0000 | [diff] [blame] | 3430 | |
Guy Benyei | 11169dd | 2012-12-18 14:30:41 +0000 | [diff] [blame] | 3431 | if (NumPaddingBytes.isPositive()) { |
| 3432 | llvm::APInt pad(32, NumPaddingBytes.getQuantity()); |
| 3433 | FType = CGM.getContext().getConstantArrayType(CGM.getContext().CharTy, |
| 3434 | pad, ArrayType::Normal, 0); |
| 3435 | EltTys.push_back(CreateMemberType(Unit, FType, "", &FieldOffset)); |
| 3436 | } |
| 3437 | } |
Eric Christopher | b2a008c | 2013-05-16 00:45:12 +0000 | [diff] [blame] | 3438 | |
Guy Benyei | 11169dd | 2012-12-18 14:30:41 +0000 | [diff] [blame] | 3439 | FType = Type; |
Duncan P. N. Exon Smith | 9dd4e4e | 2015-04-29 16:40:08 +0000 | [diff] [blame] | 3440 | llvm::DIType *FieldTy = getOrCreateType(FType, Unit); |
Guy Benyei | 11169dd | 2012-12-18 14:30:41 +0000 | [diff] [blame] | 3441 | FieldSize = CGM.getContext().getTypeSize(FType); |
| 3442 | FieldAlign = CGM.getContext().toBits(Align); |
| 3443 | |
Eric Christopher | b2a008c | 2013-05-16 00:45:12 +0000 | [diff] [blame] | 3444 | *XOffset = FieldOffset; |
Eric Christopher | e7b87e5 | 2014-10-26 23:40:33 +0000 | [diff] [blame] | 3445 | FieldTy = DBuilder.createMemberType(Unit, VD->getName(), Unit, 0, FieldSize, |
Leny Kholodov | 80c047d | 2016-09-06 10:48:04 +0000 | [diff] [blame] | 3446 | FieldAlign, FieldOffset, |
| 3447 | llvm::DINode::FlagZero, FieldTy); |
Guy Benyei | 11169dd | 2012-12-18 14:30:41 +0000 | [diff] [blame] | 3448 | EltTys.push_back(FieldTy); |
| 3449 | FieldOffset += FieldSize; |
Eric Christopher | b2a008c | 2013-05-16 00:45:12 +0000 | [diff] [blame] | 3450 | |
Duncan P. N. Exon Smith | 9dd4e4e | 2015-04-29 16:40:08 +0000 | [diff] [blame] | 3451 | llvm::DINodeArray Elements = DBuilder.getOrCreateArray(EltTys); |
Eric Christopher | b2a008c | 2013-05-16 00:45:12 +0000 | [diff] [blame] | 3452 | |
Leny Kholodov | 80c047d | 2016-09-06 10:48:04 +0000 | [diff] [blame] | 3453 | llvm::DINode::DIFlags Flags = llvm::DINode::FlagBlockByrefStruct; |
Eric Christopher | b2a008c | 2013-05-16 00:45:12 +0000 | [diff] [blame] | 3454 | |
Guy Benyei | 11169dd | 2012-12-18 14:30:41 +0000 | [diff] [blame] | 3455 | return DBuilder.createStructType(Unit, "", Unit, 0, FieldOffset, 0, Flags, |
Duncan P. N. Exon Smith | 0b6c369 | 2015-04-20 18:51:48 +0000 | [diff] [blame] | 3456 | nullptr, Elements); |
Guy Benyei | 11169dd | 2012-12-18 14:30:41 +0000 | [diff] [blame] | 3457 | } |
| 3458 | |
Duncan P. N. Exon Smith | e430654 | 2015-07-31 17:56:14 +0000 | [diff] [blame] | 3459 | void CGDebugInfo::EmitDeclare(const VarDecl *VD, llvm::Value *Storage, |
| 3460 | llvm::Optional<unsigned> ArgNo, |
Eric Christopher | e7b87e5 | 2014-10-26 23:40:33 +0000 | [diff] [blame] | 3461 | CGBuilderTy &Builder) { |
Benjamin Kramer | 8c30592 | 2016-02-02 11:06:51 +0000 | [diff] [blame] | 3462 | assert(DebugKind >= codegenoptions::LimitedDebugInfo); |
Guy Benyei | 11169dd | 2012-12-18 14:30:41 +0000 | [diff] [blame] | 3463 | assert(!LexicalBlockStack.empty() && "Region stack mismatch, stack empty!"); |
Paul Robinson | afd2dde | 2016-06-16 00:42:36 +0000 | [diff] [blame] | 3464 | if (VD->hasAttr<NoDebugAttr>()) |
| 3465 | return; |
Guy Benyei | 11169dd | 2012-12-18 14:30:41 +0000 | [diff] [blame] | 3466 | |
David Blaikie | 7fceebf | 2013-08-19 03:37:48 +0000 | [diff] [blame] | 3467 | bool Unwritten = |
| 3468 | VD->isImplicit() || (isa<Decl>(VD->getDeclContext()) && |
| 3469 | cast<Decl>(VD->getDeclContext())->isImplicit()); |
Duncan P. N. Exon Smith | 9dd4e4e | 2015-04-29 16:40:08 +0000 | [diff] [blame] | 3470 | llvm::DIFile *Unit = nullptr; |
David Blaikie | 7fceebf | 2013-08-19 03:37:48 +0000 | [diff] [blame] | 3471 | if (!Unwritten) |
| 3472 | Unit = getOrCreateFile(VD->getLocation()); |
Duncan P. N. Exon Smith | 9dd4e4e | 2015-04-29 16:40:08 +0000 | [diff] [blame] | 3473 | llvm::DIType *Ty; |
Guy Benyei | 11169dd | 2012-12-18 14:30:41 +0000 | [diff] [blame] | 3474 | uint64_t XOffset = 0; |
| 3475 | if (VD->hasAttr<BlocksAttr>()) |
| 3476 | Ty = EmitTypeForVarWithBlocksAttr(VD, &XOffset); |
Eric Christopher | b2a008c | 2013-05-16 00:45:12 +0000 | [diff] [blame] | 3477 | else |
Guy Benyei | 11169dd | 2012-12-18 14:30:41 +0000 | [diff] [blame] | 3478 | Ty = getOrCreateType(VD->getType(), Unit); |
| 3479 | |
| 3480 | // If there is no debug info for this type then do not emit debug info |
| 3481 | // for this variable. |
| 3482 | if (!Ty) |
| 3483 | return; |
| 3484 | |
Guy Benyei | 11169dd | 2012-12-18 14:30:41 +0000 | [diff] [blame] | 3485 | // Get location information. |
David Blaikie | 7fceebf | 2013-08-19 03:37:48 +0000 | [diff] [blame] | 3486 | unsigned Line = 0; |
| 3487 | unsigned Column = 0; |
| 3488 | if (!Unwritten) { |
| 3489 | Line = getLineNumber(VD->getLocation()); |
| 3490 | Column = getColumnNumber(VD->getLocation()); |
| 3491 | } |
Konstantin Zhuravlyov | 2b4917f | 2017-03-09 18:06:23 +0000 | [diff] [blame] | 3492 | SmallVector<int64_t, 13> Expr; |
Leny Kholodov | 80c047d | 2016-09-06 10:48:04 +0000 | [diff] [blame] | 3493 | llvm::DINode::DIFlags Flags = llvm::DINode::FlagZero; |
Guy Benyei | 11169dd | 2012-12-18 14:30:41 +0000 | [diff] [blame] | 3494 | if (VD->isImplicit()) |
Duncan P. N. Exon Smith | 9dd4e4e | 2015-04-29 16:40:08 +0000 | [diff] [blame] | 3495 | Flags |= llvm::DINode::FlagArtificial; |
Victor Leschuk | a7ece03 | 2016-10-20 00:13:19 +0000 | [diff] [blame] | 3496 | |
| 3497 | auto Align = getDeclAlignIfRequired(VD, CGM.getContext()); |
| 3498 | |
Konstantin Zhuravlyov | 2b4917f | 2017-03-09 18:06:23 +0000 | [diff] [blame] | 3499 | unsigned AddressSpace = CGM.getContext().getTargetAddressSpace(VD->getType()); |
| 3500 | AppendAddressSpaceXDeref(AddressSpace, Expr); |
| 3501 | |
Alexey Bataev | 24f71018 | 2017-06-09 13:55:08 +0000 | [diff] [blame] | 3502 | // If this is implicit parameter of CXXThis or ObjCSelf kind, then give it an |
| 3503 | // object pointer flag. |
Alexey Bataev | 5622323 | 2017-06-09 13:40:18 +0000 | [diff] [blame] | 3504 | if (const auto *IPD = dyn_cast<ImplicitParamDecl>(VD)) { |
| 3505 | if (IPD->getParameterKind() == ImplicitParamDecl::CXXThis || |
| 3506 | IPD->getParameterKind() == ImplicitParamDecl::ObjCSelf) |
| 3507 | Flags |= llvm::DINode::FlagObjectPointer; |
| 3508 | } |
Guy Benyei | 11169dd | 2012-12-18 14:30:41 +0000 | [diff] [blame] | 3509 | |
Adrian Prantl | c3782a1 | 2017-04-18 01:22:01 +0000 | [diff] [blame] | 3510 | // Note: Older versions of clang used to emit byval references with an extra |
| 3511 | // DW_OP_deref, because they referenced the IR arg directly instead of |
| 3512 | // referencing an alloca. Newer versions of LLVM don't treat allocas |
| 3513 | // differently from other function arguments when used in a dbg.declare. |
Duncan P. N. Exon Smith | 9dd4e4e | 2015-04-29 16:40:08 +0000 | [diff] [blame] | 3514 | auto *Scope = cast<llvm::DIScope>(LexicalBlockStack.back()); |
Guy Benyei | 11169dd | 2012-12-18 14:30:41 +0000 | [diff] [blame] | 3515 | StringRef Name = VD->getName(); |
| 3516 | if (!Name.empty()) { |
| 3517 | if (VD->hasAttr<BlocksAttr>()) { |
Adrian Prantl | c3782a1 | 2017-04-18 01:22:01 +0000 | [diff] [blame] | 3518 | // Here, we need an offset *into* the alloca. |
Guy Benyei | 11169dd | 2012-12-18 14:30:41 +0000 | [diff] [blame] | 3519 | CharUnits offset = CharUnits::fromQuantity(32); |
Florian Hahn | 3dbcced | 2017-06-13 18:06:15 +0000 | [diff] [blame] | 3520 | Expr.push_back(llvm::dwarf::DW_OP_plus_uconst); |
Guy Benyei | 11169dd | 2012-12-18 14:30:41 +0000 | [diff] [blame] | 3521 | // offset of __forwarding field |
| 3522 | offset = CGM.getContext().toCharUnitsFromBits( |
Eric Christopher | e7b87e5 | 2014-10-26 23:40:33 +0000 | [diff] [blame] | 3523 | CGM.getTarget().getPointerWidth(0)); |
Adrian Prantl | 7c6f944 | 2015-01-19 17:51:58 +0000 | [diff] [blame] | 3524 | Expr.push_back(offset.getQuantity()); |
| 3525 | Expr.push_back(llvm::dwarf::DW_OP_deref); |
Florian Hahn | 3dbcced | 2017-06-13 18:06:15 +0000 | [diff] [blame] | 3526 | Expr.push_back(llvm::dwarf::DW_OP_plus_uconst); |
Guy Benyei | 11169dd | 2012-12-18 14:30:41 +0000 | [diff] [blame] | 3527 | // offset of x field |
| 3528 | offset = CGM.getContext().toCharUnitsFromBits(XOffset); |
Adrian Prantl | 7c6f944 | 2015-01-19 17:51:58 +0000 | [diff] [blame] | 3529 | Expr.push_back(offset.getQuantity()); |
Adrian Prantl | c3782a1 | 2017-04-18 01:22:01 +0000 | [diff] [blame] | 3530 | } |
David Majnemer | 58ed0f3 | 2016-07-17 00:39:12 +0000 | [diff] [blame] | 3531 | } else if (const auto *RT = dyn_cast<RecordType>(VD->getType())) { |
Adrian Prantl | 0d82089 | 2015-04-29 15:05:50 +0000 | [diff] [blame] | 3532 | // If VD is an anonymous union then Storage represents value for |
| 3533 | // all union fields. |
David Majnemer | 58ed0f3 | 2016-07-17 00:39:12 +0000 | [diff] [blame] | 3534 | const auto *RD = cast<RecordDecl>(RT->getDecl()); |
Adrian Prantl | 0d82089 | 2015-04-29 15:05:50 +0000 | [diff] [blame] | 3535 | if (RD->isUnion() && RD->isAnonymousStructOrUnion()) { |
Adrian Prantl | 00820ab | 2015-04-29 16:52:31 +0000 | [diff] [blame] | 3536 | // GDB has trouble finding local variables in anonymous unions, so we emit |
| 3537 | // artifical local variables for each of the members. |
| 3538 | // |
| 3539 | // FIXME: Remove this code as soon as GDB supports this. |
| 3540 | // The debug info verifier in LLVM operates based on the assumption that a |
| 3541 | // variable has the same size as its storage and we had to disable the check |
| 3542 | // for artificial variables. |
Adrian Prantl | 0d82089 | 2015-04-29 15:05:50 +0000 | [diff] [blame] | 3543 | for (const auto *Field : RD->fields()) { |
Duncan P. N. Exon Smith | 9dd4e4e | 2015-04-29 16:40:08 +0000 | [diff] [blame] | 3544 | llvm::DIType *FieldTy = getOrCreateType(Field->getType(), Unit); |
Adrian Prantl | 0d82089 | 2015-04-29 15:05:50 +0000 | [diff] [blame] | 3545 | StringRef FieldName = Field->getName(); |
| 3546 | |
| 3547 | // Ignore unnamed fields. Do not ignore unnamed records. |
| 3548 | if (FieldName.empty() && !isa<RecordType>(Field->getType())) |
| 3549 | continue; |
| 3550 | |
| 3551 | // Use VarDecl's Tag, Scope and Line number. |
Victor Leschuk | a7ece03 | 2016-10-20 00:13:19 +0000 | [diff] [blame] | 3552 | auto FieldAlign = getDeclAlignIfRequired(Field, CGM.getContext()); |
Duncan P. N. Exon Smith | e430654 | 2015-07-31 17:56:14 +0000 | [diff] [blame] | 3553 | auto *D = DBuilder.createAutoVariable( |
| 3554 | Scope, FieldName, Unit, Line, FieldTy, CGM.getLangOpts().Optimize, |
Victor Leschuk | a7ece03 | 2016-10-20 00:13:19 +0000 | [diff] [blame] | 3555 | Flags | llvm::DINode::FlagArtificial, FieldAlign); |
Adrian Prantl | 0d82089 | 2015-04-29 15:05:50 +0000 | [diff] [blame] | 3556 | |
| 3557 | // Insert an llvm.dbg.declare into the current block. |
Adrian Prantl | b7acfc0 | 2017-02-27 21:30:05 +0000 | [diff] [blame] | 3558 | DBuilder.insertDeclare( |
| 3559 | Storage, D, DBuilder.createExpression(Expr), |
| 3560 | llvm::DebugLoc::get(Line, Column, Scope, CurInlinedAt), |
| 3561 | Builder.GetInsertBlock()); |
Adrian Prantl | 0d82089 | 2015-04-29 15:05:50 +0000 | [diff] [blame] | 3562 | } |
Adrian Prantl | 0d82089 | 2015-04-29 15:05:50 +0000 | [diff] [blame] | 3563 | } |
Guy Benyei | 11169dd | 2012-12-18 14:30:41 +0000 | [diff] [blame] | 3564 | } |
David Blaikie | a76a7c9 | 2013-01-05 05:58:35 +0000 | [diff] [blame] | 3565 | |
| 3566 | // Create the descriptor for the variable. |
Victor Leschuk | a7ece03 | 2016-10-20 00:13:19 +0000 | [diff] [blame] | 3567 | auto *D = ArgNo |
| 3568 | ? DBuilder.createParameterVariable( |
| 3569 | Scope, Name, *ArgNo, Unit, Line, Ty, |
| 3570 | CGM.getLangOpts().Optimize, Flags) |
| 3571 | : DBuilder.createAutoVariable(Scope, Name, Unit, Line, Ty, |
| 3572 | CGM.getLangOpts().Optimize, Flags, |
| 3573 | Align); |
David Blaikie | a76a7c9 | 2013-01-05 05:58:35 +0000 | [diff] [blame] | 3574 | |
| 3575 | // Insert an llvm.dbg.declare into the current block. |
Duncan P. N. Exon Smith | fe88b48 | 2015-04-15 21:18:30 +0000 | [diff] [blame] | 3576 | DBuilder.insertDeclare(Storage, D, DBuilder.createExpression(Expr), |
Adrian Prantl | b7acfc0 | 2017-02-27 21:30:05 +0000 | [diff] [blame] | 3577 | llvm::DebugLoc::get(Line, Column, Scope, CurInlinedAt), |
Duncan P. N. Exon Smith | fe88b48 | 2015-04-15 21:18:30 +0000 | [diff] [blame] | 3578 | Builder.GetInsertBlock()); |
Guy Benyei | 11169dd | 2012-12-18 14:30:41 +0000 | [diff] [blame] | 3579 | } |
| 3580 | |
| 3581 | void CGDebugInfo::EmitDeclareOfAutoVariable(const VarDecl *VD, |
| 3582 | llvm::Value *Storage, |
| 3583 | CGBuilderTy &Builder) { |
Benjamin Kramer | 8c30592 | 2016-02-02 11:06:51 +0000 | [diff] [blame] | 3584 | assert(DebugKind >= codegenoptions::LimitedDebugInfo); |
Duncan P. N. Exon Smith | e430654 | 2015-07-31 17:56:14 +0000 | [diff] [blame] | 3585 | EmitDeclare(VD, Storage, llvm::None, Builder); |
Guy Benyei | 11169dd | 2012-12-18 14:30:41 +0000 | [diff] [blame] | 3586 | } |
| 3587 | |
Duncan P. N. Exon Smith | 9dd4e4e | 2015-04-29 16:40:08 +0000 | [diff] [blame] | 3588 | llvm::DIType *CGDebugInfo::CreateSelfType(const QualType &QualTy, |
| 3589 | llvm::DIType *Ty) { |
| 3590 | llvm::DIType *CachedTy = getTypeOrNull(QualTy); |
Eric Christopher | e7b87e5 | 2014-10-26 23:40:33 +0000 | [diff] [blame] | 3591 | if (CachedTy) |
| 3592 | Ty = CachedTy; |
Adrian Prantl | de17db3 | 2013-03-29 19:20:29 +0000 | [diff] [blame] | 3593 | return DBuilder.createObjectPointerType(Ty); |
| 3594 | } |
| 3595 | |
Eric Christopher | e7b87e5 | 2014-10-26 23:40:33 +0000 | [diff] [blame] | 3596 | void CGDebugInfo::EmitDeclareOfBlockDeclRefVariable( |
| 3597 | const VarDecl *VD, llvm::Value *Storage, CGBuilderTy &Builder, |
Adrian Prantl | 88eec39 | 2014-11-21 00:35:25 +0000 | [diff] [blame] | 3598 | const CGBlockInfo &blockInfo, llvm::Instruction *InsertPoint) { |
Benjamin Kramer | 8c30592 | 2016-02-02 11:06:51 +0000 | [diff] [blame] | 3599 | assert(DebugKind >= codegenoptions::LimitedDebugInfo); |
Guy Benyei | 11169dd | 2012-12-18 14:30:41 +0000 | [diff] [blame] | 3600 | assert(!LexicalBlockStack.empty() && "Region stack mismatch, stack empty!"); |
Eric Christopher | b2a008c | 2013-05-16 00:45:12 +0000 | [diff] [blame] | 3601 | |
Craig Topper | 8a13c41 | 2014-05-21 05:09:00 +0000 | [diff] [blame] | 3602 | if (Builder.GetInsertBlock() == nullptr) |
Guy Benyei | 11169dd | 2012-12-18 14:30:41 +0000 | [diff] [blame] | 3603 | return; |
Paul Robinson | afd2dde | 2016-06-16 00:42:36 +0000 | [diff] [blame] | 3604 | if (VD->hasAttr<NoDebugAttr>()) |
| 3605 | return; |
Eric Christopher | b2a008c | 2013-05-16 00:45:12 +0000 | [diff] [blame] | 3606 | |
Guy Benyei | 11169dd | 2012-12-18 14:30:41 +0000 | [diff] [blame] | 3607 | bool isByRef = VD->hasAttr<BlocksAttr>(); |
Eric Christopher | b2a008c | 2013-05-16 00:45:12 +0000 | [diff] [blame] | 3608 | |
Guy Benyei | 11169dd | 2012-12-18 14:30:41 +0000 | [diff] [blame] | 3609 | uint64_t XOffset = 0; |
Duncan P. N. Exon Smith | 9dd4e4e | 2015-04-29 16:40:08 +0000 | [diff] [blame] | 3610 | llvm::DIFile *Unit = getOrCreateFile(VD->getLocation()); |
| 3611 | llvm::DIType *Ty; |
Guy Benyei | 11169dd | 2012-12-18 14:30:41 +0000 | [diff] [blame] | 3612 | if (isByRef) |
| 3613 | Ty = EmitTypeForVarWithBlocksAttr(VD, &XOffset); |
Eric Christopher | b2a008c | 2013-05-16 00:45:12 +0000 | [diff] [blame] | 3614 | else |
Guy Benyei | 11169dd | 2012-12-18 14:30:41 +0000 | [diff] [blame] | 3615 | Ty = getOrCreateType(VD->getType(), Unit); |
| 3616 | |
| 3617 | // Self is passed along as an implicit non-arg variable in a |
| 3618 | // block. Mark it as the object pointer. |
Alexey Bataev | 5622323 | 2017-06-09 13:40:18 +0000 | [diff] [blame] | 3619 | if (const auto *IPD = dyn_cast<ImplicitParamDecl>(VD)) |
| 3620 | if (IPD->getParameterKind() == ImplicitParamDecl::ObjCSelf) |
| 3621 | Ty = CreateSelfType(VD->getType(), Ty); |
Guy Benyei | 11169dd | 2012-12-18 14:30:41 +0000 | [diff] [blame] | 3622 | |
| 3623 | // Get location information. |
| 3624 | unsigned Line = getLineNumber(VD->getLocation()); |
| 3625 | unsigned Column = getColumnNumber(VD->getLocation()); |
| 3626 | |
| 3627 | const llvm::DataLayout &target = CGM.getDataLayout(); |
| 3628 | |
| 3629 | CharUnits offset = CharUnits::fromQuantity( |
Eric Christopher | e7b87e5 | 2014-10-26 23:40:33 +0000 | [diff] [blame] | 3630 | target.getStructLayout(blockInfo.StructureType) |
Guy Benyei | 11169dd | 2012-12-18 14:30:41 +0000 | [diff] [blame] | 3631 | ->getElementOffset(blockInfo.getCapture(VD).getIndex())); |
| 3632 | |
Duncan P. N. Exon Smith | f3dc429 | 2014-10-01 20:26:18 +0000 | [diff] [blame] | 3633 | SmallVector<int64_t, 9> addr; |
Adrian Prantl | c3782a1 | 2017-04-18 01:22:01 +0000 | [diff] [blame] | 3634 | addr.push_back(llvm::dwarf::DW_OP_deref); |
Florian Hahn | 3dbcced | 2017-06-13 18:06:15 +0000 | [diff] [blame] | 3635 | addr.push_back(llvm::dwarf::DW_OP_plus_uconst); |
Duncan P. N. Exon Smith | f3dc429 | 2014-10-01 20:26:18 +0000 | [diff] [blame] | 3636 | addr.push_back(offset.getQuantity()); |
Guy Benyei | 11169dd | 2012-12-18 14:30:41 +0000 | [diff] [blame] | 3637 | if (isByRef) { |
Duncan P. N. Exon Smith | f3dc429 | 2014-10-01 20:26:18 +0000 | [diff] [blame] | 3638 | addr.push_back(llvm::dwarf::DW_OP_deref); |
Florian Hahn | 3dbcced | 2017-06-13 18:06:15 +0000 | [diff] [blame] | 3639 | addr.push_back(llvm::dwarf::DW_OP_plus_uconst); |
Guy Benyei | 11169dd | 2012-12-18 14:30:41 +0000 | [diff] [blame] | 3640 | // offset of __forwarding field |
Eric Christopher | e7b87e5 | 2014-10-26 23:40:33 +0000 | [diff] [blame] | 3641 | offset = |
| 3642 | CGM.getContext().toCharUnitsFromBits(target.getPointerSizeInBits(0)); |
Duncan P. N. Exon Smith | f3dc429 | 2014-10-01 20:26:18 +0000 | [diff] [blame] | 3643 | addr.push_back(offset.getQuantity()); |
| 3644 | addr.push_back(llvm::dwarf::DW_OP_deref); |
Florian Hahn | 3dbcced | 2017-06-13 18:06:15 +0000 | [diff] [blame] | 3645 | addr.push_back(llvm::dwarf::DW_OP_plus_uconst); |
Guy Benyei | 11169dd | 2012-12-18 14:30:41 +0000 | [diff] [blame] | 3646 | // offset of x field |
| 3647 | offset = CGM.getContext().toCharUnitsFromBits(XOffset); |
Duncan P. N. Exon Smith | f3dc429 | 2014-10-01 20:26:18 +0000 | [diff] [blame] | 3648 | addr.push_back(offset.getQuantity()); |
Guy Benyei | 11169dd | 2012-12-18 14:30:41 +0000 | [diff] [blame] | 3649 | } |
| 3650 | |
| 3651 | // Create the descriptor for the variable. |
Victor Leschuk | a7ece03 | 2016-10-20 00:13:19 +0000 | [diff] [blame] | 3652 | auto Align = getDeclAlignIfRequired(VD, CGM.getContext()); |
Duncan P. N. Exon Smith | e430654 | 2015-07-31 17:56:14 +0000 | [diff] [blame] | 3653 | auto *D = DBuilder.createAutoVariable( |
Duncan P. N. Exon Smith | 9dd4e4e | 2015-04-29 16:40:08 +0000 | [diff] [blame] | 3654 | cast<llvm::DILocalScope>(LexicalBlockStack.back()), VD->getName(), Unit, |
Victor Leschuk | a7ece03 | 2016-10-20 00:13:19 +0000 | [diff] [blame] | 3655 | Line, Ty, false, llvm::DINode::FlagZero, Align); |
Adrian Prantl | 0f6df00 | 2013-03-29 19:20:35 +0000 | [diff] [blame] | 3656 | |
Guy Benyei | 11169dd | 2012-12-18 14:30:41 +0000 | [diff] [blame] | 3657 | // Insert an llvm.dbg.declare into the current block. |
Adrian Prantl | b7acfc0 | 2017-02-27 21:30:05 +0000 | [diff] [blame] | 3658 | auto DL = |
| 3659 | llvm::DebugLoc::get(Line, Column, LexicalBlockStack.back(), CurInlinedAt); |
Adrian Prantl | c3782a1 | 2017-04-18 01:22:01 +0000 | [diff] [blame] | 3660 | auto *Expr = DBuilder.createExpression(addr); |
Duncan P. N. Exon Smith | fe88b48 | 2015-04-15 21:18:30 +0000 | [diff] [blame] | 3661 | if (InsertPoint) |
Adrian Prantl | c3782a1 | 2017-04-18 01:22:01 +0000 | [diff] [blame] | 3662 | DBuilder.insertDeclare(Storage, D, Expr, DL, InsertPoint); |
Duncan P. N. Exon Smith | fe88b48 | 2015-04-15 21:18:30 +0000 | [diff] [blame] | 3663 | else |
Adrian Prantl | c3782a1 | 2017-04-18 01:22:01 +0000 | [diff] [blame] | 3664 | DBuilder.insertDeclare(Storage, D, Expr, DL, Builder.GetInsertBlock()); |
Guy Benyei | 11169dd | 2012-12-18 14:30:41 +0000 | [diff] [blame] | 3665 | } |
| 3666 | |
Guy Benyei | 11169dd | 2012-12-18 14:30:41 +0000 | [diff] [blame] | 3667 | void CGDebugInfo::EmitDeclareOfArgVariable(const VarDecl *VD, llvm::Value *AI, |
| 3668 | unsigned ArgNo, |
| 3669 | CGBuilderTy &Builder) { |
Benjamin Kramer | 8c30592 | 2016-02-02 11:06:51 +0000 | [diff] [blame] | 3670 | assert(DebugKind >= codegenoptions::LimitedDebugInfo); |
Duncan P. N. Exon Smith | e430654 | 2015-07-31 17:56:14 +0000 | [diff] [blame] | 3671 | EmitDeclare(VD, AI, ArgNo, Builder); |
Guy Benyei | 11169dd | 2012-12-18 14:30:41 +0000 | [diff] [blame] | 3672 | } |
| 3673 | |
| 3674 | namespace { |
Eric Christopher | e7b87e5 | 2014-10-26 23:40:33 +0000 | [diff] [blame] | 3675 | struct BlockLayoutChunk { |
| 3676 | uint64_t OffsetInBits; |
| 3677 | const BlockDecl::Capture *Capture; |
| 3678 | }; |
| 3679 | bool operator<(const BlockLayoutChunk &l, const BlockLayoutChunk &r) { |
| 3680 | return l.OffsetInBits < r.OffsetInBits; |
| 3681 | } |
Guy Benyei | 11169dd | 2012-12-18 14:30:41 +0000 | [diff] [blame] | 3682 | } |
| 3683 | |
| 3684 | void CGDebugInfo::EmitDeclareOfBlockLiteralArgVariable(const CGBlockInfo &block, |
Adrian Prantl | 51936dd | 2013-03-14 17:53:33 +0000 | [diff] [blame] | 3685 | llvm::Value *Arg, |
David Blaikie | 77bbb5f | 2014-08-08 17:10:14 +0000 | [diff] [blame] | 3686 | unsigned ArgNo, |
Adrian Prantl | 51936dd | 2013-03-14 17:53:33 +0000 | [diff] [blame] | 3687 | llvm::Value *LocalAddr, |
Guy Benyei | 11169dd | 2012-12-18 14:30:41 +0000 | [diff] [blame] | 3688 | CGBuilderTy &Builder) { |
Benjamin Kramer | 8c30592 | 2016-02-02 11:06:51 +0000 | [diff] [blame] | 3689 | assert(DebugKind >= codegenoptions::LimitedDebugInfo); |
Guy Benyei | 11169dd | 2012-12-18 14:30:41 +0000 | [diff] [blame] | 3690 | ASTContext &C = CGM.getContext(); |
| 3691 | const BlockDecl *blockDecl = block.getBlockDecl(); |
| 3692 | |
| 3693 | // Collect some general information about the block's location. |
| 3694 | SourceLocation loc = blockDecl->getCaretLocation(); |
Duncan P. N. Exon Smith | 9dd4e4e | 2015-04-29 16:40:08 +0000 | [diff] [blame] | 3695 | llvm::DIFile *tunit = getOrCreateFile(loc); |
Guy Benyei | 11169dd | 2012-12-18 14:30:41 +0000 | [diff] [blame] | 3696 | unsigned line = getLineNumber(loc); |
| 3697 | unsigned column = getColumnNumber(loc); |
Eric Christopher | b2a008c | 2013-05-16 00:45:12 +0000 | [diff] [blame] | 3698 | |
Guy Benyei | 11169dd | 2012-12-18 14:30:41 +0000 | [diff] [blame] | 3699 | // Build the debug-info type for the block literal. |
Adrian Prantl | 6ec370a | 2015-09-10 18:39:45 +0000 | [diff] [blame] | 3700 | getDeclContextDescriptor(blockDecl); |
Guy Benyei | 11169dd | 2012-12-18 14:30:41 +0000 | [diff] [blame] | 3701 | |
| 3702 | const llvm::StructLayout *blockLayout = |
Eric Christopher | e7b87e5 | 2014-10-26 23:40:33 +0000 | [diff] [blame] | 3703 | CGM.getDataLayout().getStructLayout(block.StructureType); |
Guy Benyei | 11169dd | 2012-12-18 14:30:41 +0000 | [diff] [blame] | 3704 | |
Duncan P. N. Exon Smith | fb49491 | 2014-12-09 18:39:32 +0000 | [diff] [blame] | 3705 | SmallVector<llvm::Metadata *, 16> fields; |
David Majnemer | b4b671e | 2016-06-30 03:01:59 +0000 | [diff] [blame] | 3706 | fields.push_back(createFieldType("__isa", C.VoidPtrTy, loc, AS_public, |
Guy Benyei | 11169dd | 2012-12-18 14:30:41 +0000 | [diff] [blame] | 3707 | blockLayout->getElementOffsetInBits(0), |
| 3708 | tunit, tunit)); |
David Majnemer | b4b671e | 2016-06-30 03:01:59 +0000 | [diff] [blame] | 3709 | fields.push_back(createFieldType("__flags", C.IntTy, loc, AS_public, |
Guy Benyei | 11169dd | 2012-12-18 14:30:41 +0000 | [diff] [blame] | 3710 | blockLayout->getElementOffsetInBits(1), |
| 3711 | tunit, tunit)); |
David Majnemer | b4b671e | 2016-06-30 03:01:59 +0000 | [diff] [blame] | 3712 | fields.push_back(createFieldType("__reserved", C.IntTy, loc, AS_public, |
Guy Benyei | 11169dd | 2012-12-18 14:30:41 +0000 | [diff] [blame] | 3713 | blockLayout->getElementOffsetInBits(2), |
| 3714 | tunit, tunit)); |
Adrian Prantl | 65d5d00 | 2014-11-05 01:01:30 +0000 | [diff] [blame] | 3715 | auto *FnTy = block.getBlockExpr()->getFunctionType(); |
| 3716 | auto FnPtrType = CGM.getContext().getPointerType(FnTy->desugar()); |
David Majnemer | b4b671e | 2016-06-30 03:01:59 +0000 | [diff] [blame] | 3717 | fields.push_back(createFieldType("__FuncPtr", FnPtrType, loc, AS_public, |
Guy Benyei | 11169dd | 2012-12-18 14:30:41 +0000 | [diff] [blame] | 3718 | blockLayout->getElementOffsetInBits(3), |
| 3719 | tunit, tunit)); |
Eric Christopher | e7b87e5 | 2014-10-26 23:40:33 +0000 | [diff] [blame] | 3720 | fields.push_back(createFieldType( |
| 3721 | "__descriptor", C.getPointerType(block.NeedsCopyDispose |
| 3722 | ? C.getBlockDescriptorExtendedType() |
| 3723 | : C.getBlockDescriptorType()), |
David Majnemer | b4b671e | 2016-06-30 03:01:59 +0000 | [diff] [blame] | 3724 | loc, AS_public, blockLayout->getElementOffsetInBits(4), tunit, tunit)); |
Guy Benyei | 11169dd | 2012-12-18 14:30:41 +0000 | [diff] [blame] | 3725 | |
| 3726 | // We want to sort the captures by offset, not because DWARF |
| 3727 | // requires this, but because we're paranoid about debuggers. |
| 3728 | SmallVector<BlockLayoutChunk, 8> chunks; |
| 3729 | |
| 3730 | // 'this' capture. |
| 3731 | if (blockDecl->capturesCXXThis()) { |
| 3732 | BlockLayoutChunk chunk; |
| 3733 | chunk.OffsetInBits = |
Eric Christopher | e7b87e5 | 2014-10-26 23:40:33 +0000 | [diff] [blame] | 3734 | blockLayout->getElementOffsetInBits(block.CXXThisIndex); |
Craig Topper | 8a13c41 | 2014-05-21 05:09:00 +0000 | [diff] [blame] | 3735 | chunk.Capture = nullptr; |
Guy Benyei | 11169dd | 2012-12-18 14:30:41 +0000 | [diff] [blame] | 3736 | chunks.push_back(chunk); |
| 3737 | } |
| 3738 | |
| 3739 | // Variable captures. |
Aaron Ballman | 9371dd2 | 2014-03-14 18:34:04 +0000 | [diff] [blame] | 3740 | for (const auto &capture : blockDecl->captures()) { |
Guy Benyei | 11169dd | 2012-12-18 14:30:41 +0000 | [diff] [blame] | 3741 | const VarDecl *variable = capture.getVariable(); |
| 3742 | const CGBlockInfo::Capture &captureInfo = block.getCapture(variable); |
| 3743 | |
| 3744 | // Ignore constant captures. |
| 3745 | if (captureInfo.isConstant()) |
| 3746 | continue; |
| 3747 | |
| 3748 | BlockLayoutChunk chunk; |
| 3749 | chunk.OffsetInBits = |
Eric Christopher | e7b87e5 | 2014-10-26 23:40:33 +0000 | [diff] [blame] | 3750 | blockLayout->getElementOffsetInBits(captureInfo.getIndex()); |
Guy Benyei | 11169dd | 2012-12-18 14:30:41 +0000 | [diff] [blame] | 3751 | chunk.Capture = &capture; |
| 3752 | chunks.push_back(chunk); |
| 3753 | } |
| 3754 | |
| 3755 | // Sort by offset. |
| 3756 | llvm::array_pod_sort(chunks.begin(), chunks.end()); |
| 3757 | |
David Majnemer | 58ed0f3 | 2016-07-17 00:39:12 +0000 | [diff] [blame] | 3758 | for (const BlockLayoutChunk &Chunk : chunks) { |
| 3759 | uint64_t offsetInBits = Chunk.OffsetInBits; |
| 3760 | const BlockDecl::Capture *capture = Chunk.Capture; |
Guy Benyei | 11169dd | 2012-12-18 14:30:41 +0000 | [diff] [blame] | 3761 | |
| 3762 | // If we have a null capture, this must be the C++ 'this' capture. |
| 3763 | if (!capture) { |
Adrian Prantl | 2526fca | 2016-04-18 23:48:16 +0000 | [diff] [blame] | 3764 | QualType type; |
| 3765 | if (auto *Method = |
| 3766 | cast_or_null<CXXMethodDecl>(blockDecl->getNonClosureContext())) |
| 3767 | type = Method->getThisType(C); |
| 3768 | else if (auto *RDecl = dyn_cast<CXXRecordDecl>(blockDecl->getParent())) |
| 3769 | type = QualType(RDecl->getTypeForDecl(), 0); |
| 3770 | else |
| 3771 | llvm_unreachable("unexpected block declcontext"); |
Guy Benyei | 11169dd | 2012-12-18 14:30:41 +0000 | [diff] [blame] | 3772 | |
David Majnemer | b4b671e | 2016-06-30 03:01:59 +0000 | [diff] [blame] | 3773 | fields.push_back(createFieldType("this", type, loc, AS_public, |
Guy Benyei | 11169dd | 2012-12-18 14:30:41 +0000 | [diff] [blame] | 3774 | offsetInBits, tunit, tunit)); |
| 3775 | continue; |
| 3776 | } |
| 3777 | |
| 3778 | const VarDecl *variable = capture->getVariable(); |
| 3779 | StringRef name = variable->getName(); |
| 3780 | |
Duncan P. N. Exon Smith | 9dd4e4e | 2015-04-29 16:40:08 +0000 | [diff] [blame] | 3781 | llvm::DIType *fieldType; |
Guy Benyei | 11169dd | 2012-12-18 14:30:41 +0000 | [diff] [blame] | 3782 | if (capture->isByRef()) { |
David Majnemer | 34b5749 | 2014-07-30 01:30:47 +0000 | [diff] [blame] | 3783 | TypeInfo PtrInfo = C.getTypeInfo(C.VoidPtrTy); |
Victor Leschuk | a7ece03 | 2016-10-20 00:13:19 +0000 | [diff] [blame] | 3784 | auto Align = PtrInfo.AlignIsRequired ? PtrInfo.Align : 0; |
Guy Benyei | 11169dd | 2012-12-18 14:30:41 +0000 | [diff] [blame] | 3785 | |
| 3786 | // FIXME: this creates a second copy of this type! |
| 3787 | uint64_t xoffset; |
| 3788 | fieldType = EmitTypeForVarWithBlocksAttr(variable, &xoffset); |
David Majnemer | 34b5749 | 2014-07-30 01:30:47 +0000 | [diff] [blame] | 3789 | fieldType = DBuilder.createPointerType(fieldType, PtrInfo.Width); |
Victor Leschuk | a7ece03 | 2016-10-20 00:13:19 +0000 | [diff] [blame] | 3790 | fieldType = DBuilder.createMemberType(tunit, name, tunit, line, |
| 3791 | PtrInfo.Width, Align, offsetInBits, |
| 3792 | llvm::DINode::FlagZero, fieldType); |
Guy Benyei | 11169dd | 2012-12-18 14:30:41 +0000 | [diff] [blame] | 3793 | } else { |
Victor Leschuk | a7ece03 | 2016-10-20 00:13:19 +0000 | [diff] [blame] | 3794 | auto Align = getDeclAlignIfRequired(variable, CGM.getContext()); |
David Majnemer | b4b671e | 2016-06-30 03:01:59 +0000 | [diff] [blame] | 3795 | fieldType = createFieldType(name, variable->getType(), loc, AS_public, |
Victor Leschuk | a7ece03 | 2016-10-20 00:13:19 +0000 | [diff] [blame] | 3796 | offsetInBits, Align, tunit, tunit); |
Guy Benyei | 11169dd | 2012-12-18 14:30:41 +0000 | [diff] [blame] | 3797 | } |
| 3798 | fields.push_back(fieldType); |
| 3799 | } |
| 3800 | |
| 3801 | SmallString<36> typeName; |
Eric Christopher | e7b87e5 | 2014-10-26 23:40:33 +0000 | [diff] [blame] | 3802 | llvm::raw_svector_ostream(typeName) << "__block_literal_" |
| 3803 | << CGM.getUniqueBlockCount(); |
Guy Benyei | 11169dd | 2012-12-18 14:30:41 +0000 | [diff] [blame] | 3804 | |
Duncan P. N. Exon Smith | 9dd4e4e | 2015-04-29 16:40:08 +0000 | [diff] [blame] | 3805 | llvm::DINodeArray fieldsArray = DBuilder.getOrCreateArray(fields); |
Guy Benyei | 11169dd | 2012-12-18 14:30:41 +0000 | [diff] [blame] | 3806 | |
Leny Kholodov | df050fd | 2016-09-06 17:06:14 +0000 | [diff] [blame] | 3807 | llvm::DIType *type = |
| 3808 | DBuilder.createStructType(tunit, typeName.str(), tunit, line, |
Victor Leschuk | a7ece03 | 2016-10-20 00:13:19 +0000 | [diff] [blame] | 3809 | CGM.getContext().toBits(block.BlockSize), 0, |
Leny Kholodov | df050fd | 2016-09-06 17:06:14 +0000 | [diff] [blame] | 3810 | llvm::DINode::FlagZero, nullptr, fieldsArray); |
Guy Benyei | 11169dd | 2012-12-18 14:30:41 +0000 | [diff] [blame] | 3811 | type = DBuilder.createPointerType(type, CGM.PointerWidthInBits); |
| 3812 | |
| 3813 | // Get overall information about the block. |
Leny Kholodov | 80c047d | 2016-09-06 10:48:04 +0000 | [diff] [blame] | 3814 | llvm::DINode::DIFlags flags = llvm::DINode::FlagArtificial; |
Duncan P. N. Exon Smith | 9dd4e4e | 2015-04-29 16:40:08 +0000 | [diff] [blame] | 3815 | auto *scope = cast<llvm::DILocalScope>(LexicalBlockStack.back()); |
Guy Benyei | 11169dd | 2012-12-18 14:30:41 +0000 | [diff] [blame] | 3816 | |
| 3817 | // Create the descriptor for the parameter. |
Duncan P. N. Exon Smith | e430654 | 2015-07-31 17:56:14 +0000 | [diff] [blame] | 3818 | auto *debugVar = DBuilder.createParameterVariable( |
| 3819 | scope, Arg->getName(), ArgNo, tunit, line, type, |
| 3820 | CGM.getLangOpts().Optimize, flags); |
Adrian Prantl | 51936dd | 2013-03-14 17:53:33 +0000 | [diff] [blame] | 3821 | |
Adrian Prantl | 616bef4 | 2013-03-14 21:52:59 +0000 | [diff] [blame] | 3822 | if (LocalAddr) { |
Adrian Prantl | 51936dd | 2013-03-14 17:53:33 +0000 | [diff] [blame] | 3823 | // Insert an llvm.dbg.value into the current block. |
Duncan P. N. Exon Smith | fe88b48 | 2015-04-15 21:18:30 +0000 | [diff] [blame] | 3824 | DBuilder.insertDbgValueIntrinsic( |
Adrian Prantl | 1fa1885 | 2017-07-28 20:21:08 +0000 | [diff] [blame] | 3825 | LocalAddr, debugVar, DBuilder.createExpression(), |
Adrian Prantl | b7acfc0 | 2017-02-27 21:30:05 +0000 | [diff] [blame] | 3826 | llvm::DebugLoc::get(line, column, scope, CurInlinedAt), |
| 3827 | Builder.GetInsertBlock()); |
Adrian Prantl | 616bef4 | 2013-03-14 21:52:59 +0000 | [diff] [blame] | 3828 | } |
Adrian Prantl | 51936dd | 2013-03-14 17:53:33 +0000 | [diff] [blame] | 3829 | |
Adrian Prantl | 616bef4 | 2013-03-14 21:52:59 +0000 | [diff] [blame] | 3830 | // Insert an llvm.dbg.declare into the current block. |
Duncan P. N. Exon Smith | fe88b48 | 2015-04-15 21:18:30 +0000 | [diff] [blame] | 3831 | DBuilder.insertDeclare(Arg, debugVar, DBuilder.createExpression(), |
Adrian Prantl | b7acfc0 | 2017-02-27 21:30:05 +0000 | [diff] [blame] | 3832 | llvm::DebugLoc::get(line, column, scope, CurInlinedAt), |
Duncan P. N. Exon Smith | fe88b48 | 2015-04-15 21:18:30 +0000 | [diff] [blame] | 3833 | Builder.GetInsertBlock()); |
Guy Benyei | 11169dd | 2012-12-18 14:30:41 +0000 | [diff] [blame] | 3834 | } |
| 3835 | |
Duncan P. N. Exon Smith | 9dd4e4e | 2015-04-29 16:40:08 +0000 | [diff] [blame] | 3836 | llvm::DIDerivedType * |
David Blaikie | 6943dea | 2013-08-20 01:28:15 +0000 | [diff] [blame] | 3837 | CGDebugInfo::getOrCreateStaticDataMemberDeclarationOrNull(const VarDecl *D) { |
| 3838 | if (!D->isStaticDataMember()) |
Duncan P. N. Exon Smith | c09c548 | 2015-04-20 21:17:26 +0000 | [diff] [blame] | 3839 | return nullptr; |
Saleem Abdulrasool | cd187f0 | 2015-02-28 00:13:13 +0000 | [diff] [blame] | 3840 | |
Duncan P. N. Exon Smith | fb49491 | 2014-12-09 18:39:32 +0000 | [diff] [blame] | 3841 | auto MI = StaticDataMemberCache.find(D->getCanonicalDecl()); |
David Blaikie | 6943dea | 2013-08-20 01:28:15 +0000 | [diff] [blame] | 3842 | if (MI != StaticDataMemberCache.end()) { |
| 3843 | 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] | 3844 | return MI->second; |
Evgeniy Stepanov | 37b3f73 | 2013-08-16 10:35:31 +0000 | [diff] [blame] | 3845 | } |
David Blaikie | ce76304 | 2013-08-20 21:49:21 +0000 | [diff] [blame] | 3846 | |
| 3847 | // If the member wasn't found in the cache, lazily construct and add it to the |
| 3848 | // type (used when a limited form of the type is emitted). |
Adrian Prantl | 21361fb | 2014-08-29 22:44:27 +0000 | [diff] [blame] | 3849 | auto DC = D->getDeclContext(); |
Adrian Prantl | 6ec370a | 2015-09-10 18:39:45 +0000 | [diff] [blame] | 3850 | auto *Ctxt = cast<llvm::DICompositeType>(getDeclContextDescriptor(D)); |
Adrian Prantl | 21361fb | 2014-08-29 22:44:27 +0000 | [diff] [blame] | 3851 | return CreateRecordStaticField(D, Ctxt, cast<RecordDecl>(DC)); |
David Blaikie | 6943dea | 2013-08-20 01:28:15 +0000 | [diff] [blame] | 3852 | } |
| 3853 | |
Adrian Prantl | 5f4740d | 2016-12-20 02:10:02 +0000 | [diff] [blame] | 3854 | llvm::DIGlobalVariableExpression *CGDebugInfo::CollectAnonRecordDecls( |
Duncan P. N. Exon Smith | 9dd4e4e | 2015-04-29 16:40:08 +0000 | [diff] [blame] | 3855 | const RecordDecl *RD, llvm::DIFile *Unit, unsigned LineNo, |
| 3856 | StringRef LinkageName, llvm::GlobalVariable *Var, llvm::DIScope *DContext) { |
Adrian Prantl | 5f4740d | 2016-12-20 02:10:02 +0000 | [diff] [blame] | 3857 | llvm::DIGlobalVariableExpression *GVE = nullptr; |
Eric Christopher | cab9fae | 2014-04-10 05:20:00 +0000 | [diff] [blame] | 3858 | |
| 3859 | for (const auto *Field : RD->fields()) { |
Duncan P. N. Exon Smith | 9dd4e4e | 2015-04-29 16:40:08 +0000 | [diff] [blame] | 3860 | llvm::DIType *FieldTy = getOrCreateType(Field->getType(), Unit); |
Eric Christopher | cab9fae | 2014-04-10 05:20:00 +0000 | [diff] [blame] | 3861 | StringRef FieldName = Field->getName(); |
| 3862 | |
| 3863 | // Ignore unnamed fields, but recurse into anonymous records. |
| 3864 | if (FieldName.empty()) { |
David Majnemer | 58ed0f3 | 2016-07-17 00:39:12 +0000 | [diff] [blame] | 3865 | if (const auto *RT = dyn_cast<RecordType>(Field->getType())) |
Adrian Prantl | 5f4740d | 2016-12-20 02:10:02 +0000 | [diff] [blame] | 3866 | GVE = CollectAnonRecordDecls(RT->getDecl(), Unit, LineNo, LinkageName, |
Eric Christopher | cab9fae | 2014-04-10 05:20:00 +0000 | [diff] [blame] | 3867 | Var, DContext); |
| 3868 | continue; |
| 3869 | } |
| 3870 | // Use VarDecl's Tag, Scope and Line number. |
Adrian Prantl | 5f4740d | 2016-12-20 02:10:02 +0000 | [diff] [blame] | 3871 | GVE = DBuilder.createGlobalVariableExpression( |
| 3872 | DContext, FieldName, LinkageName, Unit, LineNo, FieldTy, |
| 3873 | Var->hasLocalLinkage()); |
| 3874 | Var->addDebugInfo(GVE); |
Eric Christopher | cab9fae | 2014-04-10 05:20:00 +0000 | [diff] [blame] | 3875 | } |
Adrian Prantl | 5f4740d | 2016-12-20 02:10:02 +0000 | [diff] [blame] | 3876 | return GVE; |
Eric Christopher | cab9fae | 2014-04-10 05:20:00 +0000 | [diff] [blame] | 3877 | } |
| 3878 | |
Yunzhong Gao | 0ebf1bb | 2013-08-30 08:53:09 +0000 | [diff] [blame] | 3879 | void CGDebugInfo::EmitGlobalVariable(llvm::GlobalVariable *Var, |
Guy Benyei | 11169dd | 2012-12-18 14:30:41 +0000 | [diff] [blame] | 3880 | const VarDecl *D) { |
Benjamin Kramer | 8c30592 | 2016-02-02 11:06:51 +0000 | [diff] [blame] | 3881 | assert(DebugKind >= codegenoptions::LimitedDebugInfo); |
Paul Robinson | b17327d | 2016-04-27 17:37:12 +0000 | [diff] [blame] | 3882 | if (D->hasAttr<NoDebugAttr>()) |
| 3883 | return; |
Adrian Prantl | 338ef7a | 2016-11-09 00:42:03 +0000 | [diff] [blame] | 3884 | |
| 3885 | // If we already created a DIGlobalVariable for this declaration, just attach |
| 3886 | // it to the llvm::GlobalVariable. |
| 3887 | auto Cached = DeclCache.find(D->getCanonicalDecl()); |
| 3888 | if (Cached != DeclCache.end()) |
Adrian Prantl | 5f4740d | 2016-12-20 02:10:02 +0000 | [diff] [blame] | 3889 | return Var->addDebugInfo( |
| 3890 | cast<llvm::DIGlobalVariableExpression>(Cached->second)); |
Adrian Prantl | 338ef7a | 2016-11-09 00:42:03 +0000 | [diff] [blame] | 3891 | |
Guy Benyei | 11169dd | 2012-12-18 14:30:41 +0000 | [diff] [blame] | 3892 | // Create global variable debug descriptor. |
Duncan P. N. Exon Smith | 9dd4e4e | 2015-04-29 16:40:08 +0000 | [diff] [blame] | 3893 | llvm::DIFile *Unit = nullptr; |
| 3894 | llvm::DIScope *DContext = nullptr; |
Frederic Riss | 9db79f1 | 2014-11-18 03:40:46 +0000 | [diff] [blame] | 3895 | unsigned LineNo; |
| 3896 | StringRef DeclName, LinkageName; |
| 3897 | QualType T; |
| 3898 | collectVarDeclProps(D, Unit, LineNo, T, DeclName, LinkageName, DContext); |
Eric Christopher | cab9fae | 2014-04-10 05:20:00 +0000 | [diff] [blame] | 3899 | |
| 3900 | // Attempt to store one global variable for the declaration - even if we |
| 3901 | // emit a lot of fields. |
Adrian Prantl | 5f4740d | 2016-12-20 02:10:02 +0000 | [diff] [blame] | 3902 | llvm::DIGlobalVariableExpression *GVE = nullptr; |
Eric Christopher | cab9fae | 2014-04-10 05:20:00 +0000 | [diff] [blame] | 3903 | |
| 3904 | // If this is an anonymous union then we'll want to emit a global |
| 3905 | // variable for each member of the anonymous union so that it's possible |
| 3906 | // to find the name of any field in the union. |
| 3907 | if (T->isUnionType() && DeclName.empty()) { |
Reid Kleckner | 43ecd7c | 2015-11-20 17:41:12 +0000 | [diff] [blame] | 3908 | const RecordDecl *RD = T->castAs<RecordType>()->getDecl(); |
Eric Christopher | e7b87e5 | 2014-10-26 23:40:33 +0000 | [diff] [blame] | 3909 | assert(RD->isAnonymousStructOrUnion() && |
| 3910 | "unnamed non-anonymous struct or union?"); |
Adrian Prantl | 5f4740d | 2016-12-20 02:10:02 +0000 | [diff] [blame] | 3911 | GVE = CollectAnonRecordDecls(RD, Unit, LineNo, LinkageName, Var, DContext); |
Eric Christopher | cab9fae | 2014-04-10 05:20:00 +0000 | [diff] [blame] | 3912 | } else { |
Victor Leschuk | a7ece03 | 2016-10-20 00:13:19 +0000 | [diff] [blame] | 3913 | auto Align = getDeclAlignIfRequired(D, CGM.getContext()); |
Konstantin Zhuravlyov | 2b4917f | 2017-03-09 18:06:23 +0000 | [diff] [blame] | 3914 | |
| 3915 | SmallVector<int64_t, 4> Expr; |
| 3916 | unsigned AddressSpace = |
| 3917 | CGM.getContext().getTargetAddressSpace(D->getType()); |
| 3918 | AppendAddressSpaceXDeref(AddressSpace, Expr); |
| 3919 | |
Adrian Prantl | 5f4740d | 2016-12-20 02:10:02 +0000 | [diff] [blame] | 3920 | GVE = DBuilder.createGlobalVariableExpression( |
Eric Christopher | cab9fae | 2014-04-10 05:20:00 +0000 | [diff] [blame] | 3921 | DContext, DeclName, LinkageName, Unit, LineNo, getOrCreateType(T, Unit), |
Konstantin Zhuravlyov | 2b4917f | 2017-03-09 18:06:23 +0000 | [diff] [blame] | 3922 | Var->hasLocalLinkage(), |
| 3923 | Expr.empty() ? nullptr : DBuilder.createExpression(Expr), |
Victor Leschuk | a7ece03 | 2016-10-20 00:13:19 +0000 | [diff] [blame] | 3924 | getOrCreateStaticDataMemberDeclarationOrNull(D), Align); |
Adrian Prantl | 5f4740d | 2016-12-20 02:10:02 +0000 | [diff] [blame] | 3925 | Var->addDebugInfo(GVE); |
Eric Christopher | cab9fae | 2014-04-10 05:20:00 +0000 | [diff] [blame] | 3926 | } |
Adrian Prantl | 5f4740d | 2016-12-20 02:10:02 +0000 | [diff] [blame] | 3927 | DeclCache[D->getCanonicalDecl()].reset(GVE); |
Guy Benyei | 11169dd | 2012-12-18 14:30:41 +0000 | [diff] [blame] | 3928 | } |
| 3929 | |
Peter Collingbourne | eeb56ab | 2016-09-13 01:13:19 +0000 | [diff] [blame] | 3930 | void CGDebugInfo::EmitGlobalVariable(const ValueDecl *VD, const APValue &Init) { |
Benjamin Kramer | 8c30592 | 2016-02-02 11:06:51 +0000 | [diff] [blame] | 3931 | assert(DebugKind >= codegenoptions::LimitedDebugInfo); |
Paul Robinson | b17327d | 2016-04-27 17:37:12 +0000 | [diff] [blame] | 3932 | if (VD->hasAttr<NoDebugAttr>()) |
| 3933 | return; |
Victor Leschuk | a7ece03 | 2016-10-20 00:13:19 +0000 | [diff] [blame] | 3934 | auto Align = getDeclAlignIfRequired(VD, CGM.getContext()); |
Yunzhong Gao | 0ebf1bb | 2013-08-30 08:53:09 +0000 | [diff] [blame] | 3935 | // Create the descriptor for the variable. |
Duncan P. N. Exon Smith | 9dd4e4e | 2015-04-29 16:40:08 +0000 | [diff] [blame] | 3936 | llvm::DIFile *Unit = getOrCreateFile(VD->getLocation()); |
Yunzhong Gao | 0ebf1bb | 2013-08-30 08:53:09 +0000 | [diff] [blame] | 3937 | StringRef Name = VD->getName(); |
Duncan P. N. Exon Smith | 9dd4e4e | 2015-04-29 16:40:08 +0000 | [diff] [blame] | 3938 | llvm::DIType *Ty = getOrCreateType(VD->getType(), Unit); |
David Majnemer | 58ed0f3 | 2016-07-17 00:39:12 +0000 | [diff] [blame] | 3939 | if (const auto *ECD = dyn_cast<EnumConstantDecl>(VD)) { |
| 3940 | const auto *ED = cast<EnumDecl>(ECD->getDeclContext()); |
Yunzhong Gao | 0ebf1bb | 2013-08-30 08:53:09 +0000 | [diff] [blame] | 3941 | assert(isa<EnumType>(ED->getTypeForDecl()) && "Enum without EnumType?"); |
| 3942 | Ty = getOrCreateType(QualType(ED->getTypeForDecl(), 0), Unit); |
| 3943 | } |
Duncan P. N. Exon Smith | dadc2b6 | 2015-04-21 18:43:54 +0000 | [diff] [blame] | 3944 | // Do not use global variables for enums. |
| 3945 | // |
| 3946 | // FIXME: why not? |
Duncan P. N. Exon Smith | 4caa7f2 | 2015-04-16 01:00:56 +0000 | [diff] [blame] | 3947 | if (Ty->getTag() == llvm::dwarf::DW_TAG_enumeration_type) |
Yunzhong Gao | 0ebf1bb | 2013-08-30 08:53:09 +0000 | [diff] [blame] | 3948 | return; |
David Blaikie | a1556556 | 2014-04-04 20:56:17 +0000 | [diff] [blame] | 3949 | // Do not emit separate definitions for function local const/statics. |
| 3950 | if (isa<FunctionDecl>(VD->getDeclContext())) |
| 3951 | return; |
David Blaikie | bb11391 | 2014-04-05 07:23:17 +0000 | [diff] [blame] | 3952 | VD = cast<ValueDecl>(VD->getCanonicalDecl()); |
David Blaikie | 423eb5a | 2014-11-19 19:42:40 +0000 | [diff] [blame] | 3953 | auto *VarD = cast<VarDecl>(VD); |
David Blaikie | af08085 | 2014-11-21 00:20:58 +0000 | [diff] [blame] | 3954 | if (VarD->isStaticDataMember()) { |
| 3955 | auto *RD = cast<RecordDecl>(VarD->getDeclContext()); |
Adrian Prantl | 6ec370a | 2015-09-10 18:39:45 +0000 | [diff] [blame] | 3956 | getDeclContextDescriptor(VarD); |
David Blaikie | 423eb5a | 2014-11-19 19:42:40 +0000 | [diff] [blame] | 3957 | // 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] | 3958 | // |
| 3959 | // FIXME: This is probably unnecessary, since Ty should reference RD |
| 3960 | // through its scope. |
David Blaikie | 423eb5a | 2014-11-19 19:42:40 +0000 | [diff] [blame] | 3961 | RetainedTypes.push_back( |
David Blaikie | af08085 | 2014-11-21 00:20:58 +0000 | [diff] [blame] | 3962 | CGM.getContext().getRecordType(RD).getAsOpaquePtr()); |
David Blaikie | 423eb5a | 2014-11-19 19:42:40 +0000 | [diff] [blame] | 3963 | return; |
| 3964 | } |
| 3965 | |
Adrian Prantl | 6ec370a | 2015-09-10 18:39:45 +0000 | [diff] [blame] | 3966 | llvm::DIScope *DContext = getDeclContextDescriptor(VD); |
David Blaikie | af08085 | 2014-11-21 00:20:58 +0000 | [diff] [blame] | 3967 | |
Duncan P. N. Exon Smith | fb49491 | 2014-12-09 18:39:32 +0000 | [diff] [blame] | 3968 | auto &GV = DeclCache[VD]; |
| 3969 | if (GV) |
David Blaikie | bb11391 | 2014-04-05 07:23:17 +0000 | [diff] [blame] | 3970 | return; |
Peter Collingbourne | eeb56ab | 2016-09-13 01:13:19 +0000 | [diff] [blame] | 3971 | llvm::DIExpression *InitExpr = nullptr; |
Peter Collingbourne | b701363 | 2016-12-16 22:10:52 +0000 | [diff] [blame] | 3972 | if (CGM.getContext().getTypeSize(VD->getType()) <= 64) { |
| 3973 | // FIXME: Add a representation for integer constants wider than 64 bits. |
| 3974 | if (Init.isInt()) |
| 3975 | InitExpr = |
| 3976 | DBuilder.createConstantValueExpression(Init.getInt().getExtValue()); |
| 3977 | else if (Init.isFloat()) |
| 3978 | InitExpr = DBuilder.createConstantValueExpression( |
| 3979 | Init.getFloat().bitcastToAPInt().getZExtValue()); |
| 3980 | } |
Adrian Prantl | 5f4740d | 2016-12-20 02:10:02 +0000 | [diff] [blame] | 3981 | GV.reset(DBuilder.createGlobalVariableExpression( |
David Blaikie | 506a745 | 2014-04-05 07:46:57 +0000 | [diff] [blame] | 3982 | DContext, Name, StringRef(), Unit, getLineNumber(VD->getLocation()), Ty, |
Victor Leschuk | a7ece03 | 2016-10-20 00:13:19 +0000 | [diff] [blame] | 3983 | true, InitExpr, getOrCreateStaticDataMemberDeclarationOrNull(VarD), |
| 3984 | Align)); |
David Blaikie | bd48376 | 2013-05-20 04:58:53 +0000 | [diff] [blame] | 3985 | } |
| 3986 | |
Duncan P. N. Exon Smith | 9dd4e4e | 2015-04-29 16:40:08 +0000 | [diff] [blame] | 3987 | llvm::DIScope *CGDebugInfo::getCurrentContextDescriptor(const Decl *D) { |
David Blaikie | bd48376 | 2013-05-20 04:58:53 +0000 | [diff] [blame] | 3988 | if (!LexicalBlockStack.empty()) |
Duncan P. N. Exon Smith | fc8d9d9 | 2015-04-20 18:32:15 +0000 | [diff] [blame] | 3989 | return LexicalBlockStack.back(); |
Adrian Prantl | 5c8bd88 | 2015-09-11 17:23:08 +0000 | [diff] [blame] | 3990 | llvm::DIScope *Mod = getParentModuleOrNull(D); |
| 3991 | return getContextDescriptor(D, Mod ? Mod : TheCU); |
Guy Benyei | 11169dd | 2012-12-18 14:30:41 +0000 | [diff] [blame] | 3992 | } |
| 3993 | |
David Blaikie | 9f88fe8 | 2013-04-22 06:13:21 +0000 | [diff] [blame] | 3994 | void CGDebugInfo::EmitUsingDirective(const UsingDirectiveDecl &UD) { |
Benjamin Kramer | 8c30592 | 2016-02-02 11:06:51 +0000 | [diff] [blame] | 3995 | if (CGM.getCodeGenOpts().getDebugInfo() < codegenoptions::LimitedDebugInfo) |
David Blaikie | bd48376 | 2013-05-20 04:58:53 +0000 | [diff] [blame] | 3996 | return; |
Ekaterina Romanova | 9218a3b | 2015-12-10 18:52:50 +0000 | [diff] [blame] | 3997 | const NamespaceDecl *NSDecl = UD.getNominatedNamespace(); |
Adrian McCarthy | ab1e786 | 2016-07-21 18:43:20 +0000 | [diff] [blame] | 3998 | if (!NSDecl->isAnonymousNamespace() || |
| 3999 | CGM.getCodeGenOpts().DebugExplicitImport) { |
Adrian Prantl | 5649b0e | 2017-07-19 00:09:58 +0000 | [diff] [blame] | 4000 | auto Loc = UD.getLocation(); |
Ekaterina Romanova | 9218a3b | 2015-12-10 18:52:50 +0000 | [diff] [blame] | 4001 | DBuilder.createImportedModule( |
| 4002 | getCurrentContextDescriptor(cast<Decl>(UD.getDeclContext())), |
Adrian Prantl | 5649b0e | 2017-07-19 00:09:58 +0000 | [diff] [blame] | 4003 | getOrCreateNamespace(NSDecl), getOrCreateFile(Loc), getLineNumber(Loc)); |
Ekaterina Romanova | 9218a3b | 2015-12-10 18:52:50 +0000 | [diff] [blame] | 4004 | } |
David Blaikie | 9f88fe8 | 2013-04-22 06:13:21 +0000 | [diff] [blame] | 4005 | } |
| 4006 | |
David Blaikie | bd48376 | 2013-05-20 04:58:53 +0000 | [diff] [blame] | 4007 | void CGDebugInfo::EmitUsingDecl(const UsingDecl &UD) { |
Benjamin Kramer | 8c30592 | 2016-02-02 11:06:51 +0000 | [diff] [blame] | 4008 | if (CGM.getCodeGenOpts().getDebugInfo() < codegenoptions::LimitedDebugInfo) |
David Blaikie | bd48376 | 2013-05-20 04:58:53 +0000 | [diff] [blame] | 4009 | return; |
| 4010 | assert(UD.shadow_size() && |
| 4011 | "We shouldn't be codegening an invalid UsingDecl containing no decls"); |
| 4012 | // Emitting one decl is sufficient - debuggers can detect that this is an |
| 4013 | // overloaded name & provide lookup for all the overloads. |
| 4014 | const UsingShadowDecl &USD = **UD.shadow_begin(); |
David Blaikie | 2a58a18 | 2016-08-05 19:03:01 +0000 | [diff] [blame] | 4015 | |
| 4016 | // FIXME: Skip functions with undeduced auto return type for now since we |
| 4017 | // don't currently have the plumbing for separate declarations & definitions |
| 4018 | // of free functions and mismatched types (auto in the declaration, concrete |
| 4019 | // return type in the definition) |
| 4020 | if (const auto *FD = dyn_cast<FunctionDecl>(USD.getUnderlyingDecl())) |
| 4021 | if (const auto *AT = |
| 4022 | FD->getType()->getAs<FunctionProtoType>()->getContainedAutoType()) |
| 4023 | if (AT->getDeducedType().isNull()) |
| 4024 | return; |
Duncan P. N. Exon Smith | 9dd4e4e | 2015-04-29 16:40:08 +0000 | [diff] [blame] | 4025 | if (llvm::DINode *Target = |
Adrian Prantl | 5649b0e | 2017-07-19 00:09:58 +0000 | [diff] [blame] | 4026 | getDeclarationOrDefinition(USD.getUnderlyingDecl())) { |
| 4027 | auto Loc = USD.getLocation(); |
David Blaikie | bd48376 | 2013-05-20 04:58:53 +0000 | [diff] [blame] | 4028 | DBuilder.createImportedDeclaration( |
| 4029 | getCurrentContextDescriptor(cast<Decl>(USD.getDeclContext())), Target, |
Adrian Prantl | 5649b0e | 2017-07-19 00:09:58 +0000 | [diff] [blame] | 4030 | getOrCreateFile(Loc), getLineNumber(Loc)); |
| 4031 | } |
David Blaikie | bd48376 | 2013-05-20 04:58:53 +0000 | [diff] [blame] | 4032 | } |
| 4033 | |
Adrian Prantl | c4bb47e | 2015-06-30 17:39:51 +0000 | [diff] [blame] | 4034 | void CGDebugInfo::EmitImportDecl(const ImportDecl &ID) { |
David Blaikie | af09f4a | 2016-05-03 23:06:40 +0000 | [diff] [blame] | 4035 | if (CGM.getCodeGenOpts().getDebuggerTuning() != llvm::DebuggerKind::LLDB) |
| 4036 | return; |
Adrian Prantl | 8a634c1 | 2015-12-18 19:44:31 +0000 | [diff] [blame] | 4037 | if (Module *M = ID.getImportedModule()) { |
Chad Rosier | e5dafd1 | 2015-12-18 20:08:40 +0000 | [diff] [blame] | 4038 | auto Info = ExternalASTSource::ASTSourceDescriptor(*M); |
Adrian Prantl | 5649b0e | 2017-07-19 00:09:58 +0000 | [diff] [blame] | 4039 | auto Loc = ID.getLocation(); |
Adrian Prantl | 8a634c1 | 2015-12-18 19:44:31 +0000 | [diff] [blame] | 4040 | DBuilder.createImportedDeclaration( |
| 4041 | getCurrentContextDescriptor(cast<Decl>(ID.getDeclContext())), |
Adrian Prantl | 5649b0e | 2017-07-19 00:09:58 +0000 | [diff] [blame] | 4042 | getOrCreateModuleRef(Info, DebugTypeExtRefs), getOrCreateFile(Loc), |
| 4043 | getLineNumber(Loc)); |
Adrian Prantl | 8a634c1 | 2015-12-18 19:44:31 +0000 | [diff] [blame] | 4044 | } |
Adrian Prantl | c4bb47e | 2015-06-30 17:39:51 +0000 | [diff] [blame] | 4045 | } |
| 4046 | |
Duncan P. N. Exon Smith | 9dd4e4e | 2015-04-29 16:40:08 +0000 | [diff] [blame] | 4047 | llvm::DIImportedEntity * |
David Blaikie | f121b93 | 2013-05-20 22:50:41 +0000 | [diff] [blame] | 4048 | CGDebugInfo::EmitNamespaceAlias(const NamespaceAliasDecl &NA) { |
Benjamin Kramer | 8c30592 | 2016-02-02 11:06:51 +0000 | [diff] [blame] | 4049 | if (CGM.getCodeGenOpts().getDebugInfo() < codegenoptions::LimitedDebugInfo) |
Duncan P. N. Exon Smith | dadc2b6 | 2015-04-21 18:43:54 +0000 | [diff] [blame] | 4050 | return nullptr; |
Duncan P. N. Exon Smith | fb49491 | 2014-12-09 18:39:32 +0000 | [diff] [blame] | 4051 | auto &VH = NamespaceAliasCache[&NA]; |
David Blaikie | f121b93 | 2013-05-20 22:50:41 +0000 | [diff] [blame] | 4052 | if (VH) |
Duncan P. N. Exon Smith | 9dd4e4e | 2015-04-29 16:40:08 +0000 | [diff] [blame] | 4053 | return cast<llvm::DIImportedEntity>(VH); |
| 4054 | llvm::DIImportedEntity *R; |
Adrian Prantl | 5649b0e | 2017-07-19 00:09:58 +0000 | [diff] [blame] | 4055 | auto Loc = NA.getLocation(); |
David Majnemer | 58ed0f3 | 2016-07-17 00:39:12 +0000 | [diff] [blame] | 4056 | if (const auto *Underlying = |
David Blaikie | f121b93 | 2013-05-20 22:50:41 +0000 | [diff] [blame] | 4057 | dyn_cast<NamespaceAliasDecl>(NA.getAliasedNamespace())) |
| 4058 | // This could cache & dedup here rather than relying on metadata deduping. |
David Blaikie | 551fb0a | 2014-04-06 06:30:03 +0000 | [diff] [blame] | 4059 | R = DBuilder.createImportedDeclaration( |
David Blaikie | f121b93 | 2013-05-20 22:50:41 +0000 | [diff] [blame] | 4060 | getCurrentContextDescriptor(cast<Decl>(NA.getDeclContext())), |
Adrian Prantl | 5649b0e | 2017-07-19 00:09:58 +0000 | [diff] [blame] | 4061 | EmitNamespaceAlias(*Underlying), getOrCreateFile(Loc), |
| 4062 | getLineNumber(Loc), NA.getName()); |
David Blaikie | f121b93 | 2013-05-20 22:50:41 +0000 | [diff] [blame] | 4063 | else |
David Blaikie | 551fb0a | 2014-04-06 06:30:03 +0000 | [diff] [blame] | 4064 | R = DBuilder.createImportedDeclaration( |
David Blaikie | f121b93 | 2013-05-20 22:50:41 +0000 | [diff] [blame] | 4065 | getCurrentContextDescriptor(cast<Decl>(NA.getDeclContext())), |
Adrian Prantl | ddb8e06 | 2017-05-12 16:23:53 +0000 | [diff] [blame] | 4066 | getOrCreateNamespace(cast<NamespaceDecl>(NA.getAliasedNamespace())), |
Adrian Prantl | 5649b0e | 2017-07-19 00:09:58 +0000 | [diff] [blame] | 4067 | getOrCreateFile(Loc), getLineNumber(Loc), NA.getName()); |
Duncan P. N. Exon Smith | fb49491 | 2014-12-09 18:39:32 +0000 | [diff] [blame] | 4068 | VH.reset(R); |
David Blaikie | f121b93 | 2013-05-20 22:50:41 +0000 | [diff] [blame] | 4069 | return R; |
| 4070 | } |
| 4071 | |
Duncan P. N. Exon Smith | 9dd4e4e | 2015-04-29 16:40:08 +0000 | [diff] [blame] | 4072 | llvm::DINamespace * |
Adrian Prantl | ddb8e06 | 2017-05-12 16:23:53 +0000 | [diff] [blame] | 4073 | CGDebugInfo::getOrCreateNamespace(const NamespaceDecl *NSDecl) { |
| 4074 | // Don't canonicalize the NamespaceDecl here: The DINamespace will be uniqued |
| 4075 | // if necessary, and this way multiple declarations of the same namespace in |
| 4076 | // different parent modules stay distinct. |
| 4077 | auto I = NamespaceCache.find(NSDecl); |
Adrian Prantl | d887055 | 2017-05-11 22:59:19 +0000 | [diff] [blame] | 4078 | if (I != NamespaceCache.end()) |
Duncan P. N. Exon Smith | 9dd4e4e | 2015-04-29 16:40:08 +0000 | [diff] [blame] | 4079 | return cast<llvm::DINamespace>(I->second); |
Eric Christopher | b2a008c | 2013-05-16 00:45:12 +0000 | [diff] [blame] | 4080 | |
Adrian Prantl | 6ec370a | 2015-09-10 18:39:45 +0000 | [diff] [blame] | 4081 | llvm::DIScope *Context = getDeclContextDescriptor(NSDecl); |
Adrian Prantl | d887055 | 2017-05-11 22:59:19 +0000 | [diff] [blame] | 4082 | // Don't trust the context if it is a DIModule (see comment above). |
Adrian Prantl | ddb8e06 | 2017-05-12 16:23:53 +0000 | [diff] [blame] | 4083 | llvm::DINamespace *NS = |
| 4084 | DBuilder.createNameSpace(Context, NSDecl->getName(), NSDecl->isInline()); |
| 4085 | NamespaceCache[NSDecl].reset(NS); |
Guy Benyei | 11169dd | 2012-12-18 14:30:41 +0000 | [diff] [blame] | 4086 | return NS; |
| 4087 | } |
| 4088 | |
Adrian Prantl | a5206ce | 2015-09-22 23:26:43 +0000 | [diff] [blame] | 4089 | void CGDebugInfo::setDwoId(uint64_t Signature) { |
| 4090 | assert(TheCU && "no main compile unit"); |
| 4091 | TheCU->setDWOId(Signature); |
| 4092 | } |
| 4093 | |
| 4094 | |
Guy Benyei | 11169dd | 2012-12-18 14:30:41 +0000 | [diff] [blame] | 4095 | void CGDebugInfo::finalize() { |
David Blaikie | 87dab87 | 2014-05-07 16:56:58 +0000 | [diff] [blame] | 4096 | // Creating types might create further types - invalidating the current |
| 4097 | // element and the size(), so don't cache/reference them. |
| 4098 | for (size_t i = 0; i != ObjCInterfaceCache.size(); ++i) { |
| 4099 | ObjCInterfaceCacheEntry E = ObjCInterfaceCache[i]; |
Duncan P. N. Exon Smith | 9dd4e4e | 2015-04-29 16:40:08 +0000 | [diff] [blame] | 4100 | llvm::DIType *Ty = E.Type->getDecl()->getDefinition() |
Duncan P. N. Exon Smith | 497d4d46 | 2015-04-11 19:05:04 +0000 | [diff] [blame] | 4101 | ? CreateTypeDefinition(E.Type, E.Unit) |
| 4102 | : E.Decl; |
Duncan P. N. Exon Smith | 9dd4e4e | 2015-04-29 16:40:08 +0000 | [diff] [blame] | 4103 | DBuilder.replaceTemporary(llvm::TempDIType(E.Decl), Ty); |
David Blaikie | 87dab87 | 2014-05-07 16:56:58 +0000 | [diff] [blame] | 4104 | } |
| 4105 | |
David Blaikie | f427b00 | 2014-05-06 03:42:01 +0000 | [diff] [blame] | 4106 | for (auto p : ReplaceMap) { |
| 4107 | assert(p.second); |
Duncan P. N. Exon Smith | 9dd4e4e | 2015-04-29 16:40:08 +0000 | [diff] [blame] | 4108 | auto *Ty = cast<llvm::DIType>(p.second); |
Duncan P. N. Exon Smith | 4caa7f2 | 2015-04-16 01:00:56 +0000 | [diff] [blame] | 4109 | assert(Ty->isForwardDecl()); |
Eric Christopher | b2a008c | 2013-05-16 00:45:12 +0000 | [diff] [blame] | 4110 | |
David Blaikie | f427b00 | 2014-05-06 03:42:01 +0000 | [diff] [blame] | 4111 | auto it = TypeCache.find(p.first); |
David Blaikie | b814904 | 2014-05-05 21:21:39 +0000 | [diff] [blame] | 4112 | assert(it != TypeCache.end()); |
| 4113 | assert(it->second); |
Adrian Prantl | 73409ce | 2013-03-11 18:33:46 +0000 | [diff] [blame] | 4114 | |
Duncan P. N. Exon Smith | 9dd4e4e | 2015-04-29 16:40:08 +0000 | [diff] [blame] | 4115 | DBuilder.replaceTemporary(llvm::TempDIType(Ty), |
| 4116 | cast<llvm::DIType>(it->second)); |
Guy Benyei | 11169dd | 2012-12-18 14:30:41 +0000 | [diff] [blame] | 4117 | } |
Adrian Prantl | 73409ce | 2013-03-11 18:33:46 +0000 | [diff] [blame] | 4118 | |
Frederic Riss | d253ed6 | 2014-11-18 03:40:51 +0000 | [diff] [blame] | 4119 | for (const auto &p : FwdDeclReplaceMap) { |
| 4120 | assert(p.second); |
Duncan P. N. Exon Smith | d899f6e | 2015-04-18 00:07:30 +0000 | [diff] [blame] | 4121 | llvm::TempMDNode FwdDecl(cast<llvm::MDNode>(p.second)); |
Duncan P. N. Exon Smith | fb49491 | 2014-12-09 18:39:32 +0000 | [diff] [blame] | 4122 | llvm::Metadata *Repl; |
Frederic Riss | d253ed6 | 2014-11-18 03:40:51 +0000 | [diff] [blame] | 4123 | |
| 4124 | auto it = DeclCache.find(p.first); |
Adrian Prantl | 97f7685 | 2014-12-19 01:02:11 +0000 | [diff] [blame] | 4125 | // If there has been no definition for the declaration, call RAUW |
Frederic Riss | d253ed6 | 2014-11-18 03:40:51 +0000 | [diff] [blame] | 4126 | // with ourselves, that will destroy the temporary MDNode and |
| 4127 | // replace it with a standard one, avoiding leaking memory. |
| 4128 | if (it == DeclCache.end()) |
Duncan P. N. Exon Smith | fb49491 | 2014-12-09 18:39:32 +0000 | [diff] [blame] | 4129 | Repl = p.second; |
Frederic Riss | d253ed6 | 2014-11-18 03:40:51 +0000 | [diff] [blame] | 4130 | else |
Duncan P. N. Exon Smith | fb49491 | 2014-12-09 18:39:32 +0000 | [diff] [blame] | 4131 | Repl = it->second; |
Frederic Riss | dce60a7 | 2014-11-19 18:53:46 +0000 | [diff] [blame] | 4132 | |
Adrian Prantl | 5f4740d | 2016-12-20 02:10:02 +0000 | [diff] [blame] | 4133 | if (auto *GVE = dyn_cast_or_null<llvm::DIGlobalVariableExpression>(Repl)) |
| 4134 | Repl = GVE->getVariable(); |
Duncan P. N. Exon Smith | d899f6e | 2015-04-18 00:07:30 +0000 | [diff] [blame] | 4135 | DBuilder.replaceTemporary(std::move(FwdDecl), cast<llvm::MDNode>(Repl)); |
Frederic Riss | d253ed6 | 2014-11-18 03:40:51 +0000 | [diff] [blame] | 4136 | } |
| 4137 | |
Adrian Prantl | 73409ce | 2013-03-11 18:33:46 +0000 | [diff] [blame] | 4138 | // We keep our own list of retained types, because we need to look |
| 4139 | // up the final type in the type cache. |
Adrian Prantl | 3a884fa | 2015-08-27 22:56:46 +0000 | [diff] [blame] | 4140 | for (auto &RT : RetainedTypes) |
Adrian Prantl | ad9a195e | 2015-08-27 21:21:19 +0000 | [diff] [blame] | 4141 | if (auto MD = TypeCache[RT]) |
| 4142 | DBuilder.retainType(cast<llvm::DIType>(MD)); |
Adrian Prantl | 73409ce | 2013-03-11 18:33:46 +0000 | [diff] [blame] | 4143 | |
Guy Benyei | 11169dd | 2012-12-18 14:30:41 +0000 | [diff] [blame] | 4144 | DBuilder.finalize(); |
| 4145 | } |
David Blaikie | 66088d5 | 2014-09-24 17:01:27 +0000 | [diff] [blame] | 4146 | |
| 4147 | void CGDebugInfo::EmitExplicitCastType(QualType Ty) { |
Benjamin Kramer | 8c30592 | 2016-02-02 11:06:51 +0000 | [diff] [blame] | 4148 | if (CGM.getCodeGenOpts().getDebugInfo() < codegenoptions::LimitedDebugInfo) |
David Blaikie | 66088d5 | 2014-09-24 17:01:27 +0000 | [diff] [blame] | 4149 | return; |
Duncan P. N. Exon Smith | 5043f91 | 2015-03-27 22:58:05 +0000 | [diff] [blame] | 4150 | |
Duncan P. N. Exon Smith | 0b6c369 | 2015-04-20 18:51:48 +0000 | [diff] [blame] | 4151 | if (auto *DieTy = getOrCreateType(Ty, getOrCreateMainFile())) |
Duncan P. N. Exon Smith | 5043f91 | 2015-03-27 22:58:05 +0000 | [diff] [blame] | 4152 | // Don't ignore in case of explicit cast where it is referenced indirectly. |
| 4153 | DBuilder.retainType(DieTy); |
David Blaikie | 66088d5 | 2014-09-24 17:01:27 +0000 | [diff] [blame] | 4154 | } |
Amara Emerson | 652795d | 2016-11-10 14:44:30 +0000 | [diff] [blame] | 4155 | |
| 4156 | llvm::DebugLoc CGDebugInfo::SourceLocToDebugLoc(SourceLocation Loc) { |
| 4157 | if (LexicalBlockStack.empty()) |
| 4158 | return llvm::DebugLoc(); |
| 4159 | |
| 4160 | llvm::MDNode *Scope = LexicalBlockStack.back(); |
| 4161 | return llvm::DebugLoc::get( |
| 4162 | getLineNumber(Loc), getColumnNumber(Loc), Scope); |
| 4163 | } |