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