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