File: | build/source/clang/lib/CodeGen/CGDebugInfo.cpp |
Warning: | line 3095, column 7 Called C++ object pointer is null |
Press '?' to see keyboard shortcuts
Keyboard shortcuts:
1 | //===--- CGDebugInfo.cpp - Emit Debug Information for a Module ------------===// | |||
2 | // | |||
3 | // Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions. | |||
4 | // See https://llvm.org/LICENSE.txt for license information. | |||
5 | // SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception | |||
6 | // | |||
7 | //===----------------------------------------------------------------------===// | |||
8 | // | |||
9 | // This coordinates the debug information generation while generating code. | |||
10 | // | |||
11 | //===----------------------------------------------------------------------===// | |||
12 | ||||
13 | #include "CGDebugInfo.h" | |||
14 | #include "CGBlocks.h" | |||
15 | #include "CGCXXABI.h" | |||
16 | #include "CGObjCRuntime.h" | |||
17 | #include "CGRecordLayout.h" | |||
18 | #include "CodeGenFunction.h" | |||
19 | #include "CodeGenModule.h" | |||
20 | #include "ConstantEmitter.h" | |||
21 | #include "TargetInfo.h" | |||
22 | #include "clang/AST/ASTContext.h" | |||
23 | #include "clang/AST/Attr.h" | |||
24 | #include "clang/AST/DeclFriend.h" | |||
25 | #include "clang/AST/DeclObjC.h" | |||
26 | #include "clang/AST/DeclTemplate.h" | |||
27 | #include "clang/AST/Expr.h" | |||
28 | #include "clang/AST/RecordLayout.h" | |||
29 | #include "clang/AST/RecursiveASTVisitor.h" | |||
30 | #include "clang/AST/VTableBuilder.h" | |||
31 | #include "clang/Basic/CodeGenOptions.h" | |||
32 | #include "clang/Basic/FileManager.h" | |||
33 | #include "clang/Basic/SourceManager.h" | |||
34 | #include "clang/Basic/Version.h" | |||
35 | #include "clang/Frontend/FrontendOptions.h" | |||
36 | #include "clang/Lex/HeaderSearchOptions.h" | |||
37 | #include "clang/Lex/ModuleMap.h" | |||
38 | #include "clang/Lex/PreprocessorOptions.h" | |||
39 | #include "llvm/ADT/DenseSet.h" | |||
40 | #include "llvm/ADT/SmallVector.h" | |||
41 | #include "llvm/ADT/StringExtras.h" | |||
42 | #include "llvm/IR/Constants.h" | |||
43 | #include "llvm/IR/DataLayout.h" | |||
44 | #include "llvm/IR/DerivedTypes.h" | |||
45 | #include "llvm/IR/Instructions.h" | |||
46 | #include "llvm/IR/Intrinsics.h" | |||
47 | #include "llvm/IR/Metadata.h" | |||
48 | #include "llvm/IR/Module.h" | |||
49 | #include "llvm/Support/FileSystem.h" | |||
50 | #include "llvm/Support/MD5.h" | |||
51 | #include "llvm/Support/Path.h" | |||
52 | #include "llvm/Support/SHA1.h" | |||
53 | #include "llvm/Support/SHA256.h" | |||
54 | #include "llvm/Support/TimeProfiler.h" | |||
55 | #include <optional> | |||
56 | using namespace clang; | |||
57 | using namespace clang::CodeGen; | |||
58 | ||||
59 | static uint32_t getTypeAlignIfRequired(const Type *Ty, const ASTContext &Ctx) { | |||
60 | auto TI = Ctx.getTypeInfo(Ty); | |||
61 | return TI.isAlignRequired() ? TI.Align : 0; | |||
62 | } | |||
63 | ||||
64 | static uint32_t getTypeAlignIfRequired(QualType Ty, const ASTContext &Ctx) { | |||
65 | return getTypeAlignIfRequired(Ty.getTypePtr(), Ctx); | |||
66 | } | |||
67 | ||||
68 | static uint32_t getDeclAlignIfRequired(const Decl *D, const ASTContext &Ctx) { | |||
69 | return D->hasAttr<AlignedAttr>() ? D->getMaxAlignment() : 0; | |||
70 | } | |||
71 | ||||
72 | CGDebugInfo::CGDebugInfo(CodeGenModule &CGM) | |||
73 | : CGM(CGM), DebugKind(CGM.getCodeGenOpts().getDebugInfo()), | |||
74 | DebugTypeExtRefs(CGM.getCodeGenOpts().DebugTypeExtRefs), | |||
75 | DBuilder(CGM.getModule()) { | |||
76 | for (const auto &KV : CGM.getCodeGenOpts().DebugPrefixMap) | |||
77 | DebugPrefixMap[KV.first] = KV.second; | |||
78 | CreateCompileUnit(); | |||
79 | } | |||
80 | ||||
81 | CGDebugInfo::~CGDebugInfo() { | |||
82 | assert(LexicalBlockStack.empty() &&(static_cast <bool> (LexicalBlockStack.empty() && "Region stack mismatch, stack not empty!") ? void (0) : __assert_fail ("LexicalBlockStack.empty() && \"Region stack mismatch, stack not empty!\"" , "clang/lib/CodeGen/CGDebugInfo.cpp", 83, __extension__ __PRETTY_FUNCTION__ )) | |||
83 | "Region stack mismatch, stack not empty!")(static_cast <bool> (LexicalBlockStack.empty() && "Region stack mismatch, stack not empty!") ? void (0) : __assert_fail ("LexicalBlockStack.empty() && \"Region stack mismatch, stack not empty!\"" , "clang/lib/CodeGen/CGDebugInfo.cpp", 83, __extension__ __PRETTY_FUNCTION__ )); | |||
84 | } | |||
85 | ||||
86 | ApplyDebugLocation::ApplyDebugLocation(CodeGenFunction &CGF, | |||
87 | SourceLocation TemporaryLocation) | |||
88 | : CGF(&CGF) { | |||
89 | init(TemporaryLocation); | |||
90 | } | |||
91 | ||||
92 | ApplyDebugLocation::ApplyDebugLocation(CodeGenFunction &CGF, | |||
93 | bool DefaultToEmpty, | |||
94 | SourceLocation TemporaryLocation) | |||
95 | : CGF(&CGF) { | |||
96 | init(TemporaryLocation, DefaultToEmpty); | |||
97 | } | |||
98 | ||||
99 | void ApplyDebugLocation::init(SourceLocation TemporaryLocation, | |||
100 | bool DefaultToEmpty) { | |||
101 | auto *DI = CGF->getDebugInfo(); | |||
102 | if (!DI) { | |||
103 | CGF = nullptr; | |||
104 | return; | |||
105 | } | |||
106 | ||||
107 | OriginalLocation = CGF->Builder.getCurrentDebugLocation(); | |||
108 | ||||
109 | if (OriginalLocation && !DI->CGM.getExpressionLocationsEnabled()) | |||
110 | return; | |||
111 | ||||
112 | if (TemporaryLocation.isValid()) { | |||
113 | DI->EmitLocation(CGF->Builder, TemporaryLocation); | |||
114 | return; | |||
115 | } | |||
116 | ||||
117 | if (DefaultToEmpty) { | |||
118 | CGF->Builder.SetCurrentDebugLocation(llvm::DebugLoc()); | |||
119 | return; | |||
120 | } | |||
121 | ||||
122 | // Construct a location that has a valid scope, but no line info. | |||
123 | assert(!DI->LexicalBlockStack.empty())(static_cast <bool> (!DI->LexicalBlockStack.empty()) ? void (0) : __assert_fail ("!DI->LexicalBlockStack.empty()" , "clang/lib/CodeGen/CGDebugInfo.cpp", 123, __extension__ __PRETTY_FUNCTION__ )); | |||
124 | CGF->Builder.SetCurrentDebugLocation( | |||
125 | llvm::DILocation::get(DI->LexicalBlockStack.back()->getContext(), 0, 0, | |||
126 | DI->LexicalBlockStack.back(), DI->getInlinedAt())); | |||
127 | } | |||
128 | ||||
129 | ApplyDebugLocation::ApplyDebugLocation(CodeGenFunction &CGF, const Expr *E) | |||
130 | : CGF(&CGF) { | |||
131 | init(E->getExprLoc()); | |||
132 | } | |||
133 | ||||
134 | ApplyDebugLocation::ApplyDebugLocation(CodeGenFunction &CGF, llvm::DebugLoc Loc) | |||
135 | : CGF(&CGF) { | |||
136 | if (!CGF.getDebugInfo()) { | |||
137 | this->CGF = nullptr; | |||
138 | return; | |||
139 | } | |||
140 | OriginalLocation = CGF.Builder.getCurrentDebugLocation(); | |||
141 | if (Loc) | |||
142 | CGF.Builder.SetCurrentDebugLocation(std::move(Loc)); | |||
143 | } | |||
144 | ||||
145 | ApplyDebugLocation::~ApplyDebugLocation() { | |||
146 | // Query CGF so the location isn't overwritten when location updates are | |||
147 | // temporarily disabled (for C++ default function arguments) | |||
148 | if (CGF) | |||
149 | CGF->Builder.SetCurrentDebugLocation(std::move(OriginalLocation)); | |||
150 | } | |||
151 | ||||
152 | ApplyInlineDebugLocation::ApplyInlineDebugLocation(CodeGenFunction &CGF, | |||
153 | GlobalDecl InlinedFn) | |||
154 | : CGF(&CGF) { | |||
155 | if (!CGF.getDebugInfo()) { | |||
156 | this->CGF = nullptr; | |||
157 | return; | |||
158 | } | |||
159 | auto &DI = *CGF.getDebugInfo(); | |||
160 | SavedLocation = DI.getLocation(); | |||
161 | assert((DI.getInlinedAt() ==(static_cast <bool> ((DI.getInlinedAt() == CGF.Builder. getCurrentDebugLocation()->getInlinedAt()) && "CGDebugInfo and IRBuilder are out of sync" ) ? void (0) : __assert_fail ("(DI.getInlinedAt() == CGF.Builder.getCurrentDebugLocation()->getInlinedAt()) && \"CGDebugInfo and IRBuilder are out of sync\"" , "clang/lib/CodeGen/CGDebugInfo.cpp", 163, __extension__ __PRETTY_FUNCTION__ )) | |||
162 | CGF.Builder.getCurrentDebugLocation()->getInlinedAt()) &&(static_cast <bool> ((DI.getInlinedAt() == CGF.Builder. getCurrentDebugLocation()->getInlinedAt()) && "CGDebugInfo and IRBuilder are out of sync" ) ? void (0) : __assert_fail ("(DI.getInlinedAt() == CGF.Builder.getCurrentDebugLocation()->getInlinedAt()) && \"CGDebugInfo and IRBuilder are out of sync\"" , "clang/lib/CodeGen/CGDebugInfo.cpp", 163, __extension__ __PRETTY_FUNCTION__ )) | |||
163 | "CGDebugInfo and IRBuilder are out of sync")(static_cast <bool> ((DI.getInlinedAt() == CGF.Builder. getCurrentDebugLocation()->getInlinedAt()) && "CGDebugInfo and IRBuilder are out of sync" ) ? void (0) : __assert_fail ("(DI.getInlinedAt() == CGF.Builder.getCurrentDebugLocation()->getInlinedAt()) && \"CGDebugInfo and IRBuilder are out of sync\"" , "clang/lib/CodeGen/CGDebugInfo.cpp", 163, __extension__ __PRETTY_FUNCTION__ )); | |||
164 | ||||
165 | DI.EmitInlineFunctionStart(CGF.Builder, InlinedFn); | |||
166 | } | |||
167 | ||||
168 | ApplyInlineDebugLocation::~ApplyInlineDebugLocation() { | |||
169 | if (!CGF) | |||
170 | return; | |||
171 | auto &DI = *CGF->getDebugInfo(); | |||
172 | DI.EmitInlineFunctionEnd(CGF->Builder); | |||
173 | DI.EmitLocation(CGF->Builder, SavedLocation); | |||
174 | } | |||
175 | ||||
176 | void CGDebugInfo::setLocation(SourceLocation Loc) { | |||
177 | // If the new location isn't valid return. | |||
178 | if (Loc.isInvalid()) | |||
179 | return; | |||
180 | ||||
181 | CurLoc = CGM.getContext().getSourceManager().getExpansionLoc(Loc); | |||
182 | ||||
183 | // If we've changed files in the middle of a lexical scope go ahead | |||
184 | // and create a new lexical scope with file node if it's different | |||
185 | // from the one in the scope. | |||
186 | if (LexicalBlockStack.empty()) | |||
187 | return; | |||
188 | ||||
189 | SourceManager &SM = CGM.getContext().getSourceManager(); | |||
190 | auto *Scope = cast<llvm::DIScope>(LexicalBlockStack.back()); | |||
191 | PresumedLoc PCLoc = SM.getPresumedLoc(CurLoc); | |||
192 | if (PCLoc.isInvalid() || Scope->getFile() == getOrCreateFile(CurLoc)) | |||
193 | return; | |||
194 | ||||
195 | if (auto *LBF = dyn_cast<llvm::DILexicalBlockFile>(Scope)) { | |||
196 | LexicalBlockStack.pop_back(); | |||
197 | LexicalBlockStack.emplace_back(DBuilder.createLexicalBlockFile( | |||
198 | LBF->getScope(), getOrCreateFile(CurLoc))); | |||
199 | } else if (isa<llvm::DILexicalBlock>(Scope) || | |||
200 | isa<llvm::DISubprogram>(Scope)) { | |||
201 | LexicalBlockStack.pop_back(); | |||
202 | LexicalBlockStack.emplace_back( | |||
203 | DBuilder.createLexicalBlockFile(Scope, getOrCreateFile(CurLoc))); | |||
204 | } | |||
205 | } | |||
206 | ||||
207 | llvm::DIScope *CGDebugInfo::getDeclContextDescriptor(const Decl *D) { | |||
208 | llvm::DIScope *Mod = getParentModuleOrNull(D); | |||
209 | return getContextDescriptor(cast<Decl>(D->getDeclContext()), | |||
210 | Mod ? Mod : TheCU); | |||
211 | } | |||
212 | ||||
213 | llvm::DIScope *CGDebugInfo::getContextDescriptor(const Decl *Context, | |||
214 | llvm::DIScope *Default) { | |||
215 | if (!Context) | |||
216 | return Default; | |||
217 | ||||
218 | auto I = RegionMap.find(Context); | |||
219 | if (I != RegionMap.end()) { | |||
220 | llvm::Metadata *V = I->second; | |||
221 | return dyn_cast_or_null<llvm::DIScope>(V); | |||
222 | } | |||
223 | ||||
224 | // Check namespace. | |||
225 | if (const auto *NSDecl = dyn_cast<NamespaceDecl>(Context)) | |||
226 | return getOrCreateNamespace(NSDecl); | |||
227 | ||||
228 | if (const auto *RDecl = dyn_cast<RecordDecl>(Context)) | |||
229 | if (!RDecl->isDependentType()) | |||
230 | return getOrCreateType(CGM.getContext().getTypeDeclType(RDecl), | |||
231 | TheCU->getFile()); | |||
232 | return Default; | |||
233 | } | |||
234 | ||||
235 | PrintingPolicy CGDebugInfo::getPrintingPolicy() const { | |||
236 | PrintingPolicy PP = CGM.getContext().getPrintingPolicy(); | |||
237 | ||||
238 | // If we're emitting codeview, it's important to try to match MSVC's naming so | |||
239 | // that visualizers written for MSVC will trigger for our class names. In | |||
240 | // particular, we can't have spaces between arguments of standard templates | |||
241 | // like basic_string and vector, but we must have spaces between consecutive | |||
242 | // angle brackets that close nested template argument lists. | |||
243 | if (CGM.getCodeGenOpts().EmitCodeView) { | |||
244 | PP.MSVCFormatting = true; | |||
245 | PP.SplitTemplateClosers = true; | |||
246 | } else { | |||
247 | // For DWARF, printing rules are underspecified. | |||
248 | // SplitTemplateClosers yields better interop with GCC and GDB (PR46052). | |||
249 | PP.SplitTemplateClosers = true; | |||
250 | } | |||
251 | ||||
252 | PP.SuppressInlineNamespace = false; | |||
253 | PP.PrintCanonicalTypes = true; | |||
254 | PP.UsePreferredNames = false; | |||
255 | PP.AlwaysIncludeTypeForTemplateArgument = true; | |||
256 | PP.UseEnumerators = false; | |||
257 | ||||
258 | // Apply -fdebug-prefix-map. | |||
259 | PP.Callbacks = &PrintCB; | |||
260 | return PP; | |||
261 | } | |||
262 | ||||
263 | StringRef CGDebugInfo::getFunctionName(const FunctionDecl *FD) { | |||
264 | return internString(GetName(FD)); | |||
265 | } | |||
266 | ||||
267 | StringRef CGDebugInfo::getObjCMethodName(const ObjCMethodDecl *OMD) { | |||
268 | SmallString<256> MethodName; | |||
269 | llvm::raw_svector_ostream OS(MethodName); | |||
270 | OS << (OMD->isInstanceMethod() ? '-' : '+') << '['; | |||
271 | const DeclContext *DC = OMD->getDeclContext(); | |||
272 | if (const auto *OID = dyn_cast<ObjCImplementationDecl>(DC)) { | |||
273 | OS << OID->getName(); | |||
274 | } else if (const auto *OID = dyn_cast<ObjCInterfaceDecl>(DC)) { | |||
275 | OS << OID->getName(); | |||
276 | } else if (const auto *OC = dyn_cast<ObjCCategoryDecl>(DC)) { | |||
277 | if (OC->IsClassExtension()) { | |||
278 | OS << OC->getClassInterface()->getName(); | |||
279 | } else { | |||
280 | OS << OC->getIdentifier()->getNameStart() << '(' | |||
281 | << OC->getIdentifier()->getNameStart() << ')'; | |||
282 | } | |||
283 | } else if (const auto *OCD = dyn_cast<ObjCCategoryImplDecl>(DC)) { | |||
284 | OS << OCD->getClassInterface()->getName() << '(' << OCD->getName() << ')'; | |||
285 | } | |||
286 | OS << ' ' << OMD->getSelector().getAsString() << ']'; | |||
287 | ||||
288 | return internString(OS.str()); | |||
289 | } | |||
290 | ||||
291 | StringRef CGDebugInfo::getSelectorName(Selector S) { | |||
292 | return internString(S.getAsString()); | |||
293 | } | |||
294 | ||||
295 | StringRef CGDebugInfo::getClassName(const RecordDecl *RD) { | |||
296 | if (isa<ClassTemplateSpecializationDecl>(RD)) { | |||
297 | // Copy this name on the side and use its reference. | |||
298 | return internString(GetName(RD)); | |||
299 | } | |||
300 | ||||
301 | // quick optimization to avoid having to intern strings that are already | |||
302 | // stored reliably elsewhere | |||
303 | if (const IdentifierInfo *II = RD->getIdentifier()) | |||
304 | return II->getName(); | |||
305 | ||||
306 | // The CodeView printer in LLVM wants to see the names of unnamed types | |||
307 | // because they need to have a unique identifier. | |||
308 | // These names are used to reconstruct the fully qualified type names. | |||
309 | if (CGM.getCodeGenOpts().EmitCodeView) { | |||
310 | if (const TypedefNameDecl *D = RD->getTypedefNameForAnonDecl()) { | |||
311 | assert(RD->getDeclContext() == D->getDeclContext() &&(static_cast <bool> (RD->getDeclContext() == D->getDeclContext () && "Typedef should not be in another decl context!" ) ? void (0) : __assert_fail ("RD->getDeclContext() == D->getDeclContext() && \"Typedef should not be in another decl context!\"" , "clang/lib/CodeGen/CGDebugInfo.cpp", 312, __extension__ __PRETTY_FUNCTION__ )) | |||
312 | "Typedef should not be in another decl context!")(static_cast <bool> (RD->getDeclContext() == D->getDeclContext () && "Typedef should not be in another decl context!" ) ? void (0) : __assert_fail ("RD->getDeclContext() == D->getDeclContext() && \"Typedef should not be in another decl context!\"" , "clang/lib/CodeGen/CGDebugInfo.cpp", 312, __extension__ __PRETTY_FUNCTION__ )); | |||
313 | assert(D->getDeclName().getAsIdentifierInfo() &&(static_cast <bool> (D->getDeclName().getAsIdentifierInfo () && "Typedef was not named!") ? void (0) : __assert_fail ("D->getDeclName().getAsIdentifierInfo() && \"Typedef was not named!\"" , "clang/lib/CodeGen/CGDebugInfo.cpp", 314, __extension__ __PRETTY_FUNCTION__ )) | |||
314 | "Typedef was not named!")(static_cast <bool> (D->getDeclName().getAsIdentifierInfo () && "Typedef was not named!") ? void (0) : __assert_fail ("D->getDeclName().getAsIdentifierInfo() && \"Typedef was not named!\"" , "clang/lib/CodeGen/CGDebugInfo.cpp", 314, __extension__ __PRETTY_FUNCTION__ )); | |||
315 | return D->getDeclName().getAsIdentifierInfo()->getName(); | |||
316 | } | |||
317 | ||||
318 | if (CGM.getLangOpts().CPlusPlus) { | |||
319 | StringRef Name; | |||
320 | ||||
321 | ASTContext &Context = CGM.getContext(); | |||
322 | if (const DeclaratorDecl *DD = Context.getDeclaratorForUnnamedTagDecl(RD)) | |||
323 | // Anonymous types without a name for linkage purposes have their | |||
324 | // declarator mangled in if they have one. | |||
325 | Name = DD->getName(); | |||
326 | else if (const TypedefNameDecl *TND = | |||
327 | Context.getTypedefNameForUnnamedTagDecl(RD)) | |||
328 | // Anonymous types without a name for linkage purposes have their | |||
329 | // associate typedef mangled in if they have one. | |||
330 | Name = TND->getName(); | |||
331 | ||||
332 | // Give lambdas a display name based on their name mangling. | |||
333 | if (const CXXRecordDecl *CXXRD = dyn_cast<CXXRecordDecl>(RD)) | |||
334 | if (CXXRD->isLambda()) | |||
335 | return internString( | |||
336 | CGM.getCXXABI().getMangleContext().getLambdaString(CXXRD)); | |||
337 | ||||
338 | if (!Name.empty()) { | |||
339 | SmallString<256> UnnamedType("<unnamed-type-"); | |||
340 | UnnamedType += Name; | |||
341 | UnnamedType += '>'; | |||
342 | return internString(UnnamedType); | |||
343 | } | |||
344 | } | |||
345 | } | |||
346 | ||||
347 | return StringRef(); | |||
348 | } | |||
349 | ||||
350 | std::optional<llvm::DIFile::ChecksumKind> | |||
351 | CGDebugInfo::computeChecksum(FileID FID, SmallString<64> &Checksum) const { | |||
352 | Checksum.clear(); | |||
353 | ||||
354 | if (!CGM.getCodeGenOpts().EmitCodeView && | |||
355 | CGM.getCodeGenOpts().DwarfVersion < 5) | |||
356 | return std::nullopt; | |||
357 | ||||
358 | SourceManager &SM = CGM.getContext().getSourceManager(); | |||
359 | std::optional<llvm::MemoryBufferRef> MemBuffer = SM.getBufferOrNone(FID); | |||
360 | if (!MemBuffer) | |||
361 | return std::nullopt; | |||
362 | ||||
363 | auto Data = llvm::arrayRefFromStringRef(MemBuffer->getBuffer()); | |||
364 | switch (CGM.getCodeGenOpts().getDebugSrcHash()) { | |||
365 | case clang::CodeGenOptions::DSH_MD5: | |||
366 | llvm::toHex(llvm::MD5::hash(Data), /*LowerCase=*/true, Checksum); | |||
367 | return llvm::DIFile::CSK_MD5; | |||
368 | case clang::CodeGenOptions::DSH_SHA1: | |||
369 | llvm::toHex(llvm::SHA1::hash(Data), /*LowerCase=*/true, Checksum); | |||
370 | return llvm::DIFile::CSK_SHA1; | |||
371 | case clang::CodeGenOptions::DSH_SHA256: | |||
372 | llvm::toHex(llvm::SHA256::hash(Data), /*LowerCase=*/true, Checksum); | |||
373 | return llvm::DIFile::CSK_SHA256; | |||
374 | } | |||
375 | llvm_unreachable("Unhandled DebugSrcHashKind enum")::llvm::llvm_unreachable_internal("Unhandled DebugSrcHashKind enum" , "clang/lib/CodeGen/CGDebugInfo.cpp", 375); | |||
376 | } | |||
377 | ||||
378 | std::optional<StringRef> CGDebugInfo::getSource(const SourceManager &SM, | |||
379 | FileID FID) { | |||
380 | if (!CGM.getCodeGenOpts().EmbedSource) | |||
381 | return std::nullopt; | |||
382 | ||||
383 | bool SourceInvalid = false; | |||
384 | StringRef Source = SM.getBufferData(FID, &SourceInvalid); | |||
385 | ||||
386 | if (SourceInvalid) | |||
387 | return std::nullopt; | |||
388 | ||||
389 | return Source; | |||
390 | } | |||
391 | ||||
392 | llvm::DIFile *CGDebugInfo::getOrCreateFile(SourceLocation Loc) { | |||
393 | SourceManager &SM = CGM.getContext().getSourceManager(); | |||
394 | StringRef FileName; | |||
395 | FileID FID; | |||
396 | ||||
397 | if (Loc.isInvalid()) { | |||
398 | // The DIFile used by the CU is distinct from the main source file. Call | |||
399 | // createFile() below for canonicalization if the source file was specified | |||
400 | // with an absolute path. | |||
401 | FileName = TheCU->getFile()->getFilename(); | |||
402 | } else { | |||
403 | PresumedLoc PLoc = SM.getPresumedLoc(Loc); | |||
404 | FileName = PLoc.getFilename(); | |||
405 | ||||
406 | if (FileName.empty()) { | |||
407 | FileName = TheCU->getFile()->getFilename(); | |||
408 | } else { | |||
409 | FileName = PLoc.getFilename(); | |||
410 | } | |||
411 | FID = PLoc.getFileID(); | |||
412 | } | |||
413 | ||||
414 | // Cache the results. | |||
415 | auto It = DIFileCache.find(FileName.data()); | |||
416 | if (It != DIFileCache.end()) { | |||
417 | // Verify that the information still exists. | |||
418 | if (llvm::Metadata *V = It->second) | |||
419 | return cast<llvm::DIFile>(V); | |||
420 | } | |||
421 | ||||
422 | SmallString<64> Checksum; | |||
423 | ||||
424 | std::optional<llvm::DIFile::ChecksumKind> CSKind = | |||
425 | computeChecksum(FID, Checksum); | |||
426 | std::optional<llvm::DIFile::ChecksumInfo<StringRef>> CSInfo; | |||
427 | if (CSKind) | |||
428 | CSInfo.emplace(*CSKind, Checksum); | |||
429 | return createFile(FileName, CSInfo, getSource(SM, SM.getFileID(Loc))); | |||
430 | } | |||
431 | ||||
432 | llvm::DIFile *CGDebugInfo::createFile( | |||
433 | StringRef FileName, | |||
434 | std::optional<llvm::DIFile::ChecksumInfo<StringRef>> CSInfo, | |||
435 | std::optional<StringRef> Source) { | |||
436 | StringRef Dir; | |||
437 | StringRef File; | |||
438 | std::string RemappedFile = remapDIPath(FileName); | |||
439 | std::string CurDir = remapDIPath(getCurrentDirname()); | |||
440 | SmallString<128> DirBuf; | |||
441 | SmallString<128> FileBuf; | |||
442 | if (llvm::sys::path::is_absolute(RemappedFile)) { | |||
443 | // Strip the common prefix (if it is more than just "/" or "C:\") from | |||
444 | // current directory and FileName for a more space-efficient encoding. | |||
445 | auto FileIt = llvm::sys::path::begin(RemappedFile); | |||
446 | auto FileE = llvm::sys::path::end(RemappedFile); | |||
447 | auto CurDirIt = llvm::sys::path::begin(CurDir); | |||
448 | auto CurDirE = llvm::sys::path::end(CurDir); | |||
449 | for (; CurDirIt != CurDirE && *CurDirIt == *FileIt; ++CurDirIt, ++FileIt) | |||
450 | llvm::sys::path::append(DirBuf, *CurDirIt); | |||
451 | if (llvm::sys::path::root_path(DirBuf) == DirBuf) { | |||
452 | // Don't strip the common prefix if it is only the root ("/" or "C:\") | |||
453 | // since that would make LLVM diagnostic locations confusing. | |||
454 | Dir = {}; | |||
455 | File = RemappedFile; | |||
456 | } else { | |||
457 | for (; FileIt != FileE; ++FileIt) | |||
458 | llvm::sys::path::append(FileBuf, *FileIt); | |||
459 | Dir = DirBuf; | |||
460 | File = FileBuf; | |||
461 | } | |||
462 | } else { | |||
463 | if (!llvm::sys::path::is_absolute(FileName)) | |||
464 | Dir = CurDir; | |||
465 | File = RemappedFile; | |||
466 | } | |||
467 | llvm::DIFile *F = DBuilder.createFile(File, Dir, CSInfo, Source); | |||
468 | DIFileCache[FileName.data()].reset(F); | |||
469 | return F; | |||
470 | } | |||
471 | ||||
472 | std::string CGDebugInfo::remapDIPath(StringRef Path) const { | |||
473 | if (DebugPrefixMap.empty()) | |||
474 | return Path.str(); | |||
475 | ||||
476 | SmallString<256> P = Path; | |||
477 | for (const auto &Entry : DebugPrefixMap) | |||
478 | if (llvm::sys::path::replace_path_prefix(P, Entry.first, Entry.second)) | |||
479 | break; | |||
480 | return P.str().str(); | |||
481 | } | |||
482 | ||||
483 | unsigned CGDebugInfo::getLineNumber(SourceLocation Loc) { | |||
484 | if (Loc.isInvalid()) | |||
485 | return 0; | |||
486 | SourceManager &SM = CGM.getContext().getSourceManager(); | |||
487 | return SM.getPresumedLoc(Loc).getLine(); | |||
488 | } | |||
489 | ||||
490 | unsigned CGDebugInfo::getColumnNumber(SourceLocation Loc, bool Force) { | |||
491 | // We may not want column information at all. | |||
492 | if (!Force && !CGM.getCodeGenOpts().DebugColumnInfo) | |||
493 | return 0; | |||
494 | ||||
495 | // If the location is invalid then use the current column. | |||
496 | if (Loc.isInvalid() && CurLoc.isInvalid()) | |||
497 | return 0; | |||
498 | SourceManager &SM = CGM.getContext().getSourceManager(); | |||
499 | PresumedLoc PLoc = SM.getPresumedLoc(Loc.isValid() ? Loc : CurLoc); | |||
500 | return PLoc.isValid() ? PLoc.getColumn() : 0; | |||
501 | } | |||
502 | ||||
503 | StringRef CGDebugInfo::getCurrentDirname() { | |||
504 | if (!CGM.getCodeGenOpts().DebugCompilationDir.empty()) | |||
505 | return CGM.getCodeGenOpts().DebugCompilationDir; | |||
506 | ||||
507 | if (!CWDName.empty()) | |||
508 | return CWDName; | |||
509 | llvm::ErrorOr<std::string> CWD = | |||
510 | CGM.getFileSystem()->getCurrentWorkingDirectory(); | |||
511 | if (!CWD) | |||
512 | return StringRef(); | |||
513 | return CWDName = internString(*CWD); | |||
514 | } | |||
515 | ||||
516 | void CGDebugInfo::CreateCompileUnit() { | |||
517 | SmallString<64> Checksum; | |||
518 | std::optional<llvm::DIFile::ChecksumKind> CSKind; | |||
519 | std::optional<llvm::DIFile::ChecksumInfo<StringRef>> CSInfo; | |||
520 | ||||
521 | // Should we be asking the SourceManager for the main file name, instead of | |||
522 | // accepting it as an argument? This just causes the main file name to | |||
523 | // mismatch with source locations and create extra lexical scopes or | |||
524 | // mismatched debug info (a CU with a DW_AT_file of "-", because that's what | |||
525 | // the driver passed, but functions/other things have DW_AT_file of "<stdin>" | |||
526 | // because that's what the SourceManager says) | |||
527 | ||||
528 | // Get absolute path name. | |||
529 | SourceManager &SM = CGM.getContext().getSourceManager(); | |||
530 | auto &CGO = CGM.getCodeGenOpts(); | |||
531 | std::string MainFileName = CGO.MainFileName; | |||
532 | if (MainFileName.empty()) | |||
533 | MainFileName = "<stdin>"; | |||
534 | ||||
535 | // The main file name provided via the "-main-file-name" option contains just | |||
536 | // the file name itself with no path information. This file name may have had | |||
537 | // a relative path, so we look into the actual file entry for the main | |||
538 | // file to determine the real absolute path for the file. | |||
539 | std::string MainFileDir; | |||
540 | if (OptionalFileEntryRef MainFile = | |||
541 | SM.getFileEntryRefForID(SM.getMainFileID())) { | |||
542 | MainFileDir = std::string(MainFile->getDir().getName()); | |||
543 | if (!llvm::sys::path::is_absolute(MainFileName)) { | |||
544 | llvm::SmallString<1024> MainFileDirSS(MainFileDir); | |||
545 | llvm::sys::path::append(MainFileDirSS, MainFileName); | |||
546 | MainFileName = | |||
547 | std::string(llvm::sys::path::remove_leading_dotslash(MainFileDirSS)); | |||
548 | } | |||
549 | // If the main file name provided is identical to the input file name, and | |||
550 | // if the input file is a preprocessed source, use the module name for | |||
551 | // debug info. The module name comes from the name specified in the first | |||
552 | // linemarker if the input is a preprocessed source. | |||
553 | if (MainFile->getName() == MainFileName && | |||
554 | FrontendOptions::getInputKindForExtension( | |||
555 | MainFile->getName().rsplit('.').second) | |||
556 | .isPreprocessed()) | |||
557 | MainFileName = CGM.getModule().getName().str(); | |||
558 | ||||
559 | CSKind = computeChecksum(SM.getMainFileID(), Checksum); | |||
560 | } | |||
561 | ||||
562 | llvm::dwarf::SourceLanguage LangTag; | |||
563 | const LangOptions &LO = CGM.getLangOpts(); | |||
564 | if (LO.CPlusPlus) { | |||
565 | if (LO.ObjC) | |||
566 | LangTag = llvm::dwarf::DW_LANG_ObjC_plus_plus; | |||
567 | else if (CGO.DebugStrictDwarf && CGO.DwarfVersion < 5) | |||
568 | LangTag = llvm::dwarf::DW_LANG_C_plus_plus; | |||
569 | else if (LO.CPlusPlus14) | |||
570 | LangTag = llvm::dwarf::DW_LANG_C_plus_plus_14; | |||
571 | else if (LO.CPlusPlus11) | |||
572 | LangTag = llvm::dwarf::DW_LANG_C_plus_plus_11; | |||
573 | else | |||
574 | LangTag = llvm::dwarf::DW_LANG_C_plus_plus; | |||
575 | } else if (LO.ObjC) { | |||
576 | LangTag = llvm::dwarf::DW_LANG_ObjC; | |||
577 | } else if (LO.OpenCL && (!CGM.getCodeGenOpts().DebugStrictDwarf || | |||
578 | CGM.getCodeGenOpts().DwarfVersion >= 5)) { | |||
579 | LangTag = llvm::dwarf::DW_LANG_OpenCL; | |||
580 | } else if (LO.RenderScript) { | |||
581 | LangTag = llvm::dwarf::DW_LANG_GOOGLE_RenderScript; | |||
582 | } else if (LO.C11 && !(CGO.DebugStrictDwarf && CGO.DwarfVersion < 5)) { | |||
583 | LangTag = llvm::dwarf::DW_LANG_C11; | |||
584 | } else if (LO.C99) { | |||
585 | LangTag = llvm::dwarf::DW_LANG_C99; | |||
586 | } else { | |||
587 | LangTag = llvm::dwarf::DW_LANG_C89; | |||
588 | } | |||
589 | ||||
590 | std::string Producer = getClangFullVersion(); | |||
591 | ||||
592 | // Figure out which version of the ObjC runtime we have. | |||
593 | unsigned RuntimeVers = 0; | |||
594 | if (LO.ObjC) | |||
595 | RuntimeVers = LO.ObjCRuntime.isNonFragile() ? 2 : 1; | |||
596 | ||||
597 | llvm::DICompileUnit::DebugEmissionKind EmissionKind; | |||
598 | switch (DebugKind) { | |||
599 | case llvm::codegenoptions::NoDebugInfo: | |||
600 | case llvm::codegenoptions::LocTrackingOnly: | |||
601 | EmissionKind = llvm::DICompileUnit::NoDebug; | |||
602 | break; | |||
603 | case llvm::codegenoptions::DebugLineTablesOnly: | |||
604 | EmissionKind = llvm::DICompileUnit::LineTablesOnly; | |||
605 | break; | |||
606 | case llvm::codegenoptions::DebugDirectivesOnly: | |||
607 | EmissionKind = llvm::DICompileUnit::DebugDirectivesOnly; | |||
608 | break; | |||
609 | case llvm::codegenoptions::DebugInfoConstructor: | |||
610 | case llvm::codegenoptions::LimitedDebugInfo: | |||
611 | case llvm::codegenoptions::FullDebugInfo: | |||
612 | case llvm::codegenoptions::UnusedTypeInfo: | |||
613 | EmissionKind = llvm::DICompileUnit::FullDebug; | |||
614 | break; | |||
615 | } | |||
616 | ||||
617 | uint64_t DwoId = 0; | |||
618 | auto &CGOpts = CGM.getCodeGenOpts(); | |||
619 | // The DIFile used by the CU is distinct from the main source | |||
620 | // file. Its directory part specifies what becomes the | |||
621 | // DW_AT_comp_dir (the compilation directory), even if the source | |||
622 | // file was specified with an absolute path. | |||
623 | if (CSKind) | |||
624 | CSInfo.emplace(*CSKind, Checksum); | |||
625 | llvm::DIFile *CUFile = DBuilder.createFile( | |||
626 | remapDIPath(MainFileName), remapDIPath(getCurrentDirname()), CSInfo, | |||
627 | getSource(SM, SM.getMainFileID())); | |||
628 | ||||
629 | StringRef Sysroot, SDK; | |||
630 | if (CGM.getCodeGenOpts().getDebuggerTuning() == llvm::DebuggerKind::LLDB) { | |||
631 | Sysroot = CGM.getHeaderSearchOpts().Sysroot; | |||
632 | auto B = llvm::sys::path::rbegin(Sysroot); | |||
633 | auto E = llvm::sys::path::rend(Sysroot); | |||
634 | auto It = std::find_if(B, E, [](auto SDK) { return SDK.endswith(".sdk"); }); | |||
635 | if (It != E) | |||
636 | SDK = *It; | |||
637 | } | |||
638 | ||||
639 | // Create new compile unit. | |||
640 | TheCU = DBuilder.createCompileUnit( | |||
641 | LangTag, CUFile, CGOpts.EmitVersionIdentMetadata ? Producer : "", | |||
642 | LO.Optimize || CGOpts.PrepareForLTO || CGOpts.PrepareForThinLTO, | |||
643 | CGOpts.DwarfDebugFlags, RuntimeVers, CGOpts.SplitDwarfFile, EmissionKind, | |||
644 | DwoId, CGOpts.SplitDwarfInlining, CGOpts.DebugInfoForProfiling, | |||
645 | CGM.getTarget().getTriple().isNVPTX() | |||
646 | ? llvm::DICompileUnit::DebugNameTableKind::None | |||
647 | : static_cast<llvm::DICompileUnit::DebugNameTableKind>( | |||
648 | CGOpts.DebugNameTable), | |||
649 | CGOpts.DebugRangesBaseAddress, remapDIPath(Sysroot), SDK); | |||
650 | } | |||
651 | ||||
652 | llvm::DIType *CGDebugInfo::CreateType(const BuiltinType *BT) { | |||
653 | llvm::dwarf::TypeKind Encoding; | |||
654 | StringRef BTName; | |||
655 | switch (BT->getKind()) { | |||
656 | #define BUILTIN_TYPE(Id, SingletonId) | |||
657 | #define PLACEHOLDER_TYPE(Id, SingletonId) case BuiltinType::Id: | |||
658 | #include "clang/AST/BuiltinTypes.def" | |||
659 | case BuiltinType::Dependent: | |||
660 | llvm_unreachable("Unexpected builtin type")::llvm::llvm_unreachable_internal("Unexpected builtin type", "clang/lib/CodeGen/CGDebugInfo.cpp" , 660); | |||
661 | case BuiltinType::NullPtr: | |||
662 | return DBuilder.createNullPtrType(); | |||
663 | case BuiltinType::Void: | |||
664 | return nullptr; | |||
665 | case BuiltinType::ObjCClass: | |||
666 | if (!ClassTy) | |||
667 | ClassTy = | |||
668 | DBuilder.createForwardDecl(llvm::dwarf::DW_TAG_structure_type, | |||
669 | "objc_class", TheCU, TheCU->getFile(), 0); | |||
670 | return ClassTy; | |||
671 | case BuiltinType::ObjCId: { | |||
672 | // typedef struct objc_class *Class; | |||
673 | // typedef struct objc_object { | |||
674 | // Class isa; | |||
675 | // } *id; | |||
676 | ||||
677 | if (ObjTy) | |||
678 | return ObjTy; | |||
679 | ||||
680 | if (!ClassTy) | |||
681 | ClassTy = | |||
682 | DBuilder.createForwardDecl(llvm::dwarf::DW_TAG_structure_type, | |||
683 | "objc_class", TheCU, TheCU->getFile(), 0); | |||
684 | ||||
685 | unsigned Size = CGM.getContext().getTypeSize(CGM.getContext().VoidPtrTy); | |||
686 | ||||
687 | auto *ISATy = DBuilder.createPointerType(ClassTy, Size); | |||
688 | ||||
689 | ObjTy = DBuilder.createStructType(TheCU, "objc_object", TheCU->getFile(), 0, | |||
690 | 0, 0, llvm::DINode::FlagZero, nullptr, | |||
691 | llvm::DINodeArray()); | |||
692 | ||||
693 | DBuilder.replaceArrays( | |||
694 | ObjTy, DBuilder.getOrCreateArray(&*DBuilder.createMemberType( | |||
695 | ObjTy, "isa", TheCU->getFile(), 0, Size, 0, 0, | |||
696 | llvm::DINode::FlagZero, ISATy))); | |||
697 | return ObjTy; | |||
698 | } | |||
699 | case BuiltinType::ObjCSel: { | |||
700 | if (!SelTy) | |||
701 | SelTy = DBuilder.createForwardDecl(llvm::dwarf::DW_TAG_structure_type, | |||
702 | "objc_selector", TheCU, | |||
703 | TheCU->getFile(), 0); | |||
704 | return SelTy; | |||
705 | } | |||
706 | ||||
707 | #define IMAGE_TYPE(ImgType, Id, SingletonId, Access, Suffix) \ | |||
708 | case BuiltinType::Id: \ | |||
709 | return getOrCreateStructPtrType("opencl_" #ImgType "_" #Suffix "_t", \ | |||
710 | SingletonId); | |||
711 | #include "clang/Basic/OpenCLImageTypes.def" | |||
712 | case BuiltinType::OCLSampler: | |||
713 | return getOrCreateStructPtrType("opencl_sampler_t", OCLSamplerDITy); | |||
714 | case BuiltinType::OCLEvent: | |||
715 | return getOrCreateStructPtrType("opencl_event_t", OCLEventDITy); | |||
716 | case BuiltinType::OCLClkEvent: | |||
717 | return getOrCreateStructPtrType("opencl_clk_event_t", OCLClkEventDITy); | |||
718 | case BuiltinType::OCLQueue: | |||
719 | return getOrCreateStructPtrType("opencl_queue_t", OCLQueueDITy); | |||
720 | case BuiltinType::OCLReserveID: | |||
721 | return getOrCreateStructPtrType("opencl_reserve_id_t", OCLReserveIDDITy); | |||
722 | #define EXT_OPAQUE_TYPE(ExtType, Id, Ext) \ | |||
723 | case BuiltinType::Id: \ | |||
724 | return getOrCreateStructPtrType("opencl_" #ExtType, Id##Ty); | |||
725 | #include "clang/Basic/OpenCLExtensionTypes.def" | |||
726 | ||||
727 | #define SVE_TYPE(Name, Id, SingletonId) case BuiltinType::Id: | |||
728 | #include "clang/Basic/AArch64SVEACLETypes.def" | |||
729 | { | |||
730 | ASTContext::BuiltinVectorTypeInfo Info = | |||
731 | // For svcount_t, only the lower 2 bytes are relevant. | |||
732 | BT->getKind() == BuiltinType::SveCount | |||
733 | ? ASTContext::BuiltinVectorTypeInfo( | |||
734 | CGM.getContext().BoolTy, llvm::ElementCount::getFixed(16), | |||
735 | 1) | |||
736 | : CGM.getContext().getBuiltinVectorTypeInfo(BT); | |||
737 | ||||
738 | // A single vector of bytes may not suffice as the representation of | |||
739 | // svcount_t tuples because of the gap between the active 16bits of | |||
740 | // successive tuple members. Currently no such tuples are defined for | |||
741 | // svcount_t, so assert that NumVectors is 1. | |||
742 | assert((BT->getKind() != BuiltinType::SveCount || Info.NumVectors == 1) &&(static_cast <bool> ((BT->getKind() != BuiltinType:: SveCount || Info.NumVectors == 1) && "Unsupported number of vectors for svcount_t" ) ? void (0) : __assert_fail ("(BT->getKind() != BuiltinType::SveCount || Info.NumVectors == 1) && \"Unsupported number of vectors for svcount_t\"" , "clang/lib/CodeGen/CGDebugInfo.cpp", 743, __extension__ __PRETTY_FUNCTION__ )) | |||
743 | "Unsupported number of vectors for svcount_t")(static_cast <bool> ((BT->getKind() != BuiltinType:: SveCount || Info.NumVectors == 1) && "Unsupported number of vectors for svcount_t" ) ? void (0) : __assert_fail ("(BT->getKind() != BuiltinType::SveCount || Info.NumVectors == 1) && \"Unsupported number of vectors for svcount_t\"" , "clang/lib/CodeGen/CGDebugInfo.cpp", 743, __extension__ __PRETTY_FUNCTION__ )); | |||
744 | ||||
745 | // Debuggers can't extract 1bit from a vector, so will display a | |||
746 | // bitpattern for predicates instead. | |||
747 | unsigned NumElems = Info.EC.getKnownMinValue() * Info.NumVectors; | |||
748 | if (Info.ElementType == CGM.getContext().BoolTy) { | |||
749 | NumElems /= 8; | |||
750 | Info.ElementType = CGM.getContext().UnsignedCharTy; | |||
751 | } | |||
752 | ||||
753 | llvm::Metadata *LowerBound, *UpperBound; | |||
754 | LowerBound = llvm::ConstantAsMetadata::get(llvm::ConstantInt::getSigned( | |||
755 | llvm::Type::getInt64Ty(CGM.getLLVMContext()), 0)); | |||
756 | if (Info.EC.isScalable()) { | |||
757 | unsigned NumElemsPerVG = NumElems / 2; | |||
758 | SmallVector<uint64_t, 9> Expr( | |||
759 | {llvm::dwarf::DW_OP_constu, NumElemsPerVG, llvm::dwarf::DW_OP_bregx, | |||
760 | /* AArch64::VG */ 46, 0, llvm::dwarf::DW_OP_mul, | |||
761 | llvm::dwarf::DW_OP_constu, 1, llvm::dwarf::DW_OP_minus}); | |||
762 | UpperBound = DBuilder.createExpression(Expr); | |||
763 | } else | |||
764 | UpperBound = llvm::ConstantAsMetadata::get(llvm::ConstantInt::getSigned( | |||
765 | llvm::Type::getInt64Ty(CGM.getLLVMContext()), NumElems - 1)); | |||
766 | ||||
767 | llvm::Metadata *Subscript = DBuilder.getOrCreateSubrange( | |||
768 | /*count*/ nullptr, LowerBound, UpperBound, /*stride*/ nullptr); | |||
769 | llvm::DINodeArray SubscriptArray = DBuilder.getOrCreateArray(Subscript); | |||
770 | llvm::DIType *ElemTy = | |||
771 | getOrCreateType(Info.ElementType, TheCU->getFile()); | |||
772 | auto Align = getTypeAlignIfRequired(BT, CGM.getContext()); | |||
773 | return DBuilder.createVectorType(/*Size*/ 0, Align, ElemTy, | |||
774 | SubscriptArray); | |||
775 | } | |||
776 | // It doesn't make sense to generate debug info for PowerPC MMA vector types. | |||
777 | // So we return a safe type here to avoid generating an error. | |||
778 | #define PPC_VECTOR_TYPE(Name, Id, size) \ | |||
779 | case BuiltinType::Id: | |||
780 | #include "clang/Basic/PPCTypes.def" | |||
781 | return CreateType(cast<const BuiltinType>(CGM.getContext().IntTy)); | |||
782 | ||||
783 | #define RVV_TYPE(Name, Id, SingletonId) case BuiltinType::Id: | |||
784 | #include "clang/Basic/RISCVVTypes.def" | |||
785 | { | |||
786 | ASTContext::BuiltinVectorTypeInfo Info = | |||
787 | CGM.getContext().getBuiltinVectorTypeInfo(BT); | |||
788 | ||||
789 | unsigned ElementCount = Info.EC.getKnownMinValue(); | |||
790 | unsigned SEW = CGM.getContext().getTypeSize(Info.ElementType); | |||
791 | ||||
792 | bool Fractional = false; | |||
793 | unsigned LMUL; | |||
794 | unsigned FixedSize = ElementCount * SEW; | |||
795 | if (Info.ElementType == CGM.getContext().BoolTy) { | |||
796 | // Mask type only occupies one vector register. | |||
797 | LMUL = 1; | |||
798 | } else if (FixedSize < 64) { | |||
799 | // In RVV scalable vector types, we encode 64 bits in the fixed part. | |||
800 | Fractional = true; | |||
801 | LMUL = 64 / FixedSize; | |||
802 | } else { | |||
803 | LMUL = FixedSize / 64; | |||
804 | } | |||
805 | ||||
806 | // Element count = (VLENB / SEW) x LMUL | |||
807 | SmallVector<uint64_t, 12> Expr( | |||
808 | // The DW_OP_bregx operation has two operands: a register which is | |||
809 | // specified by an unsigned LEB128 number, followed by a signed LEB128 | |||
810 | // offset. | |||
811 | {llvm::dwarf::DW_OP_bregx, // Read the contents of a register. | |||
812 | 4096 + 0xC22, // RISC-V VLENB CSR register. | |||
813 | 0, // Offset for DW_OP_bregx. It is dummy here. | |||
814 | llvm::dwarf::DW_OP_constu, | |||
815 | SEW / 8, // SEW is in bits. | |||
816 | llvm::dwarf::DW_OP_div, llvm::dwarf::DW_OP_constu, LMUL}); | |||
817 | if (Fractional) | |||
818 | Expr.push_back(llvm::dwarf::DW_OP_div); | |||
819 | else | |||
820 | Expr.push_back(llvm::dwarf::DW_OP_mul); | |||
821 | // Element max index = count - 1 | |||
822 | Expr.append({llvm::dwarf::DW_OP_constu, 1, llvm::dwarf::DW_OP_minus}); | |||
823 | ||||
824 | auto *LowerBound = | |||
825 | llvm::ConstantAsMetadata::get(llvm::ConstantInt::getSigned( | |||
826 | llvm::Type::getInt64Ty(CGM.getLLVMContext()), 0)); | |||
827 | auto *UpperBound = DBuilder.createExpression(Expr); | |||
828 | llvm::Metadata *Subscript = DBuilder.getOrCreateSubrange( | |||
829 | /*count*/ nullptr, LowerBound, UpperBound, /*stride*/ nullptr); | |||
830 | llvm::DINodeArray SubscriptArray = DBuilder.getOrCreateArray(Subscript); | |||
831 | llvm::DIType *ElemTy = | |||
832 | getOrCreateType(Info.ElementType, TheCU->getFile()); | |||
833 | ||||
834 | auto Align = getTypeAlignIfRequired(BT, CGM.getContext()); | |||
835 | return DBuilder.createVectorType(/*Size=*/0, Align, ElemTy, | |||
836 | SubscriptArray); | |||
837 | } | |||
838 | ||||
839 | #define WASM_REF_TYPE(Name, MangledName, Id, SingletonId, AS) \ | |||
840 | case BuiltinType::Id: { \ | |||
841 | if (!SingletonId) \ | |||
842 | SingletonId = \ | |||
843 | DBuilder.createForwardDecl(llvm::dwarf::DW_TAG_structure_type, \ | |||
844 | MangledName, TheCU, TheCU->getFile(), 0); \ | |||
845 | return SingletonId; \ | |||
846 | } | |||
847 | #include "clang/Basic/WebAssemblyReferenceTypes.def" | |||
848 | ||||
849 | case BuiltinType::UChar: | |||
850 | case BuiltinType::Char_U: | |||
851 | Encoding = llvm::dwarf::DW_ATE_unsigned_char; | |||
852 | break; | |||
853 | case BuiltinType::Char_S: | |||
854 | case BuiltinType::SChar: | |||
855 | Encoding = llvm::dwarf::DW_ATE_signed_char; | |||
856 | break; | |||
857 | case BuiltinType::Char8: | |||
858 | case BuiltinType::Char16: | |||
859 | case BuiltinType::Char32: | |||
860 | Encoding = llvm::dwarf::DW_ATE_UTF; | |||
861 | break; | |||
862 | case BuiltinType::UShort: | |||
863 | case BuiltinType::UInt: | |||
864 | case BuiltinType::UInt128: | |||
865 | case BuiltinType::ULong: | |||
866 | case BuiltinType::WChar_U: | |||
867 | case BuiltinType::ULongLong: | |||
868 | Encoding = llvm::dwarf::DW_ATE_unsigned; | |||
869 | break; | |||
870 | case BuiltinType::Short: | |||
871 | case BuiltinType::Int: | |||
872 | case BuiltinType::Int128: | |||
873 | case BuiltinType::Long: | |||
874 | case BuiltinType::WChar_S: | |||
875 | case BuiltinType::LongLong: | |||
876 | Encoding = llvm::dwarf::DW_ATE_signed; | |||
877 | break; | |||
878 | case BuiltinType::Bool: | |||
879 | Encoding = llvm::dwarf::DW_ATE_boolean; | |||
880 | break; | |||
881 | case BuiltinType::Half: | |||
882 | case BuiltinType::Float: | |||
883 | case BuiltinType::LongDouble: | |||
884 | case BuiltinType::Float16: | |||
885 | case BuiltinType::BFloat16: | |||
886 | case BuiltinType::Float128: | |||
887 | case BuiltinType::Double: | |||
888 | case BuiltinType::Ibm128: | |||
889 | // FIXME: For targets where long double, __ibm128 and __float128 have the | |||
890 | // same size, they are currently indistinguishable in the debugger without | |||
891 | // some special treatment. However, there is currently no consensus on | |||
892 | // encoding and this should be updated once a DWARF encoding exists for | |||
893 | // distinct floating point types of the same size. | |||
894 | Encoding = llvm::dwarf::DW_ATE_float; | |||
895 | break; | |||
896 | case BuiltinType::ShortAccum: | |||
897 | case BuiltinType::Accum: | |||
898 | case BuiltinType::LongAccum: | |||
899 | case BuiltinType::ShortFract: | |||
900 | case BuiltinType::Fract: | |||
901 | case BuiltinType::LongFract: | |||
902 | case BuiltinType::SatShortFract: | |||
903 | case BuiltinType::SatFract: | |||
904 | case BuiltinType::SatLongFract: | |||
905 | case BuiltinType::SatShortAccum: | |||
906 | case BuiltinType::SatAccum: | |||
907 | case BuiltinType::SatLongAccum: | |||
908 | Encoding = llvm::dwarf::DW_ATE_signed_fixed; | |||
909 | break; | |||
910 | case BuiltinType::UShortAccum: | |||
911 | case BuiltinType::UAccum: | |||
912 | case BuiltinType::ULongAccum: | |||
913 | case BuiltinType::UShortFract: | |||
914 | case BuiltinType::UFract: | |||
915 | case BuiltinType::ULongFract: | |||
916 | case BuiltinType::SatUShortAccum: | |||
917 | case BuiltinType::SatUAccum: | |||
918 | case BuiltinType::SatULongAccum: | |||
919 | case BuiltinType::SatUShortFract: | |||
920 | case BuiltinType::SatUFract: | |||
921 | case BuiltinType::SatULongFract: | |||
922 | Encoding = llvm::dwarf::DW_ATE_unsigned_fixed; | |||
923 | break; | |||
924 | } | |||
925 | ||||
926 | BTName = BT->getName(CGM.getLangOpts()); | |||
927 | // Bit size and offset of the type. | |||
928 | uint64_t Size = CGM.getContext().getTypeSize(BT); | |||
929 | return DBuilder.createBasicType(BTName, Size, Encoding); | |||
930 | } | |||
931 | ||||
932 | llvm::DIType *CGDebugInfo::CreateType(const BitIntType *Ty) { | |||
933 | ||||
934 | StringRef Name = Ty->isUnsigned() ? "unsigned _BitInt" : "_BitInt"; | |||
935 | llvm::dwarf::TypeKind Encoding = Ty->isUnsigned() | |||
936 | ? llvm::dwarf::DW_ATE_unsigned | |||
937 | : llvm::dwarf::DW_ATE_signed; | |||
938 | ||||
939 | return DBuilder.createBasicType(Name, CGM.getContext().getTypeSize(Ty), | |||
940 | Encoding); | |||
941 | } | |||
942 | ||||
943 | llvm::DIType *CGDebugInfo::CreateType(const ComplexType *Ty) { | |||
944 | // Bit size and offset of the type. | |||
945 | llvm::dwarf::TypeKind Encoding = llvm::dwarf::DW_ATE_complex_float; | |||
946 | if (Ty->isComplexIntegerType()) | |||
947 | Encoding = llvm::dwarf::DW_ATE_lo_user; | |||
948 | ||||
949 | uint64_t Size = CGM.getContext().getTypeSize(Ty); | |||
950 | return DBuilder.createBasicType("complex", Size, Encoding); | |||
951 | } | |||
952 | ||||
953 | static void stripUnusedQualifiers(Qualifiers &Q) { | |||
954 | // Ignore these qualifiers for now. | |||
955 | Q.removeObjCGCAttr(); | |||
956 | Q.removeAddressSpace(); | |||
957 | Q.removeObjCLifetime(); | |||
958 | Q.removeUnaligned(); | |||
959 | } | |||
960 | ||||
961 | static llvm::dwarf::Tag getNextQualifier(Qualifiers &Q) { | |||
962 | if (Q.hasConst()) { | |||
963 | Q.removeConst(); | |||
964 | return llvm::dwarf::DW_TAG_const_type; | |||
965 | } | |||
966 | if (Q.hasVolatile()) { | |||
967 | Q.removeVolatile(); | |||
968 | return llvm::dwarf::DW_TAG_volatile_type; | |||
969 | } | |||
970 | if (Q.hasRestrict()) { | |||
971 | Q.removeRestrict(); | |||
972 | return llvm::dwarf::DW_TAG_restrict_type; | |||
973 | } | |||
974 | return (llvm::dwarf::Tag)0; | |||
975 | } | |||
976 | ||||
977 | llvm::DIType *CGDebugInfo::CreateQualifiedType(QualType Ty, | |||
978 | llvm::DIFile *Unit) { | |||
979 | QualifierCollector Qc; | |||
980 | const Type *T = Qc.strip(Ty); | |||
981 | ||||
982 | stripUnusedQualifiers(Qc); | |||
983 | ||||
984 | // We will create one Derived type for one qualifier and recurse to handle any | |||
985 | // additional ones. | |||
986 | llvm::dwarf::Tag Tag = getNextQualifier(Qc); | |||
987 | if (!Tag) { | |||
988 | assert(Qc.empty() && "Unknown type qualifier for debug info")(static_cast <bool> (Qc.empty() && "Unknown type qualifier for debug info" ) ? void (0) : __assert_fail ("Qc.empty() && \"Unknown type qualifier for debug info\"" , "clang/lib/CodeGen/CGDebugInfo.cpp", 988, __extension__ __PRETTY_FUNCTION__ )); | |||
989 | return getOrCreateType(QualType(T, 0), Unit); | |||
990 | } | |||
991 | ||||
992 | auto *FromTy = getOrCreateType(Qc.apply(CGM.getContext(), T), Unit); | |||
993 | ||||
994 | // No need to fill in the Name, Line, Size, Alignment, Offset in case of | |||
995 | // CVR derived types. | |||
996 | return DBuilder.createQualifiedType(Tag, FromTy); | |||
997 | } | |||
998 | ||||
999 | llvm::DIType *CGDebugInfo::CreateQualifiedType(const FunctionProtoType *F, | |||
1000 | llvm::DIFile *Unit) { | |||
1001 | FunctionProtoType::ExtProtoInfo EPI = F->getExtProtoInfo(); | |||
1002 | Qualifiers &Q = EPI.TypeQuals; | |||
1003 | stripUnusedQualifiers(Q); | |||
1004 | ||||
1005 | // We will create one Derived type for one qualifier and recurse to handle any | |||
1006 | // additional ones. | |||
1007 | llvm::dwarf::Tag Tag = getNextQualifier(Q); | |||
1008 | if (!Tag) { | |||
1009 | assert(Q.empty() && "Unknown type qualifier for debug info")(static_cast <bool> (Q.empty() && "Unknown type qualifier for debug info" ) ? void (0) : __assert_fail ("Q.empty() && \"Unknown type qualifier for debug info\"" , "clang/lib/CodeGen/CGDebugInfo.cpp", 1009, __extension__ __PRETTY_FUNCTION__ )); | |||
1010 | return nullptr; | |||
1011 | } | |||
1012 | ||||
1013 | auto *FromTy = | |||
1014 | getOrCreateType(CGM.getContext().getFunctionType(F->getReturnType(), | |||
1015 | F->getParamTypes(), EPI), | |||
1016 | Unit); | |||
1017 | ||||
1018 | // No need to fill in the Name, Line, Size, Alignment, Offset in case of | |||
1019 | // CVR derived types. | |||
1020 | return DBuilder.createQualifiedType(Tag, FromTy); | |||
1021 | } | |||
1022 | ||||
1023 | llvm::DIType *CGDebugInfo::CreateType(const ObjCObjectPointerType *Ty, | |||
1024 | llvm::DIFile *Unit) { | |||
1025 | ||||
1026 | // The frontend treats 'id' as a typedef to an ObjCObjectType, | |||
1027 | // whereas 'id<protocol>' is treated as an ObjCPointerType. For the | |||
1028 | // debug info, we want to emit 'id' in both cases. | |||
1029 | if (Ty->isObjCQualifiedIdType()) | |||
1030 | return getOrCreateType(CGM.getContext().getObjCIdType(), Unit); | |||
1031 | ||||
1032 | return CreatePointerLikeType(llvm::dwarf::DW_TAG_pointer_type, Ty, | |||
1033 | Ty->getPointeeType(), Unit); | |||
1034 | } | |||
1035 | ||||
1036 | llvm::DIType *CGDebugInfo::CreateType(const PointerType *Ty, | |||
1037 | llvm::DIFile *Unit) { | |||
1038 | return CreatePointerLikeType(llvm::dwarf::DW_TAG_pointer_type, Ty, | |||
1039 | Ty->getPointeeType(), Unit); | |||
1040 | } | |||
1041 | ||||
1042 | /// \return whether a C++ mangling exists for the type defined by TD. | |||
1043 | static bool hasCXXMangling(const TagDecl *TD, llvm::DICompileUnit *TheCU) { | |||
1044 | switch (TheCU->getSourceLanguage()) { | |||
1045 | case llvm::dwarf::DW_LANG_C_plus_plus: | |||
1046 | case llvm::dwarf::DW_LANG_C_plus_plus_11: | |||
1047 | case llvm::dwarf::DW_LANG_C_plus_plus_14: | |||
1048 | return true; | |||
1049 | case llvm::dwarf::DW_LANG_ObjC_plus_plus: | |||
1050 | return isa<CXXRecordDecl>(TD) || isa<EnumDecl>(TD); | |||
1051 | default: | |||
1052 | return false; | |||
1053 | } | |||
1054 | } | |||
1055 | ||||
1056 | // Determines if the debug info for this tag declaration needs a type | |||
1057 | // identifier. The purpose of the unique identifier is to deduplicate type | |||
1058 | // information for identical types across TUs. Because of the C++ one definition | |||
1059 | // rule (ODR), it is valid to assume that the type is defined the same way in | |||
1060 | // every TU and its debug info is equivalent. | |||
1061 | // | |||
1062 | // C does not have the ODR, and it is common for codebases to contain multiple | |||
1063 | // different definitions of a struct with the same name in different TUs. | |||
1064 | // Therefore, if the type doesn't have a C++ mangling, don't give it an | |||
1065 | // identifer. Type information in C is smaller and simpler than C++ type | |||
1066 | // information, so the increase in debug info size is negligible. | |||
1067 | // | |||
1068 | // If the type is not externally visible, it should be unique to the current TU, | |||
1069 | // and should not need an identifier to participate in type deduplication. | |||
1070 | // However, when emitting CodeView, the format internally uses these | |||
1071 | // unique type name identifers for references between debug info. For example, | |||
1072 | // the method of a class in an anonymous namespace uses the identifer to refer | |||
1073 | // to its parent class. The Microsoft C++ ABI attempts to provide unique names | |||
1074 | // for such types, so when emitting CodeView, always use identifiers for C++ | |||
1075 | // types. This may create problems when attempting to emit CodeView when the MS | |||
1076 | // C++ ABI is not in use. | |||
1077 | static bool needsTypeIdentifier(const TagDecl *TD, CodeGenModule &CGM, | |||
1078 | llvm::DICompileUnit *TheCU) { | |||
1079 | // We only add a type identifier for types with C++ name mangling. | |||
1080 | if (!hasCXXMangling(TD, TheCU)) | |||
1081 | return false; | |||
1082 | ||||
1083 | // Externally visible types with C++ mangling need a type identifier. | |||
1084 | if (TD->isExternallyVisible()) | |||
1085 | return true; | |||
1086 | ||||
1087 | // CodeView types with C++ mangling need a type identifier. | |||
1088 | if (CGM.getCodeGenOpts().EmitCodeView) | |||
1089 | return true; | |||
1090 | ||||
1091 | return false; | |||
1092 | } | |||
1093 | ||||
1094 | // Returns a unique type identifier string if one exists, or an empty string. | |||
1095 | static SmallString<256> getTypeIdentifier(const TagType *Ty, CodeGenModule &CGM, | |||
1096 | llvm::DICompileUnit *TheCU) { | |||
1097 | SmallString<256> Identifier; | |||
1098 | const TagDecl *TD = Ty->getDecl(); | |||
1099 | ||||
1100 | if (!needsTypeIdentifier(TD, CGM, TheCU)) | |||
1101 | return Identifier; | |||
1102 | if (const auto *RD = dyn_cast<CXXRecordDecl>(TD)) | |||
1103 | if (RD->getDefinition()) | |||
1104 | if (RD->isDynamicClass() && | |||
1105 | CGM.getVTableLinkage(RD) == llvm::GlobalValue::ExternalLinkage) | |||
1106 | return Identifier; | |||
1107 | ||||
1108 | // TODO: This is using the RTTI name. Is there a better way to get | |||
1109 | // a unique string for a type? | |||
1110 | llvm::raw_svector_ostream Out(Identifier); | |||
1111 | CGM.getCXXABI().getMangleContext().mangleCXXRTTIName(QualType(Ty, 0), Out); | |||
1112 | return Identifier; | |||
1113 | } | |||
1114 | ||||
1115 | /// \return the appropriate DWARF tag for a composite type. | |||
1116 | static llvm::dwarf::Tag getTagForRecord(const RecordDecl *RD) { | |||
1117 | llvm::dwarf::Tag Tag; | |||
1118 | if (RD->isStruct() || RD->isInterface()) | |||
1119 | Tag = llvm::dwarf::DW_TAG_structure_type; | |||
1120 | else if (RD->isUnion()) | |||
1121 | Tag = llvm::dwarf::DW_TAG_union_type; | |||
1122 | else { | |||
1123 | // FIXME: This could be a struct type giving a default visibility different | |||
1124 | // than C++ class type, but needs llvm metadata changes first. | |||
1125 | assert(RD->isClass())(static_cast <bool> (RD->isClass()) ? void (0) : __assert_fail ("RD->isClass()", "clang/lib/CodeGen/CGDebugInfo.cpp", 1125 , __extension__ __PRETTY_FUNCTION__)); | |||
1126 | Tag = llvm::dwarf::DW_TAG_class_type; | |||
1127 | } | |||
1128 | return Tag; | |||
1129 | } | |||
1130 | ||||
1131 | llvm::DICompositeType * | |||
1132 | CGDebugInfo::getOrCreateRecordFwdDecl(const RecordType *Ty, | |||
1133 | llvm::DIScope *Ctx) { | |||
1134 | const RecordDecl *RD = Ty->getDecl(); | |||
1135 | if (llvm::DIType *T = getTypeOrNull(CGM.getContext().getRecordType(RD))) | |||
1136 | return cast<llvm::DICompositeType>(T); | |||
1137 | llvm::DIFile *DefUnit = getOrCreateFile(RD->getLocation()); | |||
1138 | const unsigned Line = | |||
1139 | getLineNumber(RD->getLocation().isValid() ? RD->getLocation() : CurLoc); | |||
1140 | StringRef RDName = getClassName(RD); | |||
1141 | ||||
1142 | uint64_t Size = 0; | |||
1143 | uint32_t Align = 0; | |||
1144 | ||||
1145 | const RecordDecl *D = RD->getDefinition(); | |||
1146 | if (D && D->isCompleteDefinition()) | |||
1147 | Size = CGM.getContext().getTypeSize(Ty); | |||
1148 | ||||
1149 | llvm::DINode::DIFlags Flags = llvm::DINode::FlagFwdDecl; | |||
1150 | ||||
1151 | // Add flag to nontrivial forward declarations. To be consistent with MSVC, | |||
1152 | // add the flag if a record has no definition because we don't know whether | |||
1153 | // it will be trivial or not. | |||
1154 | if (const CXXRecordDecl *CXXRD = dyn_cast<CXXRecordDecl>(RD)) | |||
1155 | if (!CXXRD->hasDefinition() || | |||
1156 | (CXXRD->hasDefinition() && !CXXRD->isTrivial())) | |||
1157 | Flags |= llvm::DINode::FlagNonTrivial; | |||
1158 | ||||
1159 | // Create the type. | |||
1160 | SmallString<256> Identifier; | |||
1161 | // Don't include a linkage name in line tables only. | |||
1162 | if (CGM.getCodeGenOpts().hasReducedDebugInfo()) | |||
1163 | Identifier = getTypeIdentifier(Ty, CGM, TheCU); | |||
1164 | llvm::DICompositeType *RetTy = DBuilder.createReplaceableCompositeType( | |||
1165 | getTagForRecord(RD), RDName, Ctx, DefUnit, Line, 0, Size, Align, Flags, | |||
1166 | Identifier); | |||
1167 | if (CGM.getCodeGenOpts().DebugFwdTemplateParams) | |||
1168 | if (auto *TSpecial = dyn_cast<ClassTemplateSpecializationDecl>(RD)) | |||
1169 | DBuilder.replaceArrays(RetTy, llvm::DINodeArray(), | |||
1170 | CollectCXXTemplateParams(TSpecial, DefUnit)); | |||
1171 | ReplaceMap.emplace_back( | |||
1172 | std::piecewise_construct, std::make_tuple(Ty), | |||
1173 | std::make_tuple(static_cast<llvm::Metadata *>(RetTy))); | |||
1174 | return RetTy; | |||
1175 | } | |||
1176 | ||||
1177 | llvm::DIType *CGDebugInfo::CreatePointerLikeType(llvm::dwarf::Tag Tag, | |||
1178 | const Type *Ty, | |||
1179 | QualType PointeeTy, | |||
1180 | llvm::DIFile *Unit) { | |||
1181 | // Bit size, align and offset of the type. | |||
1182 | // Size is always the size of a pointer. | |||
1183 | uint64_t Size = CGM.getContext().getTypeSize(Ty); | |||
1184 | auto Align = getTypeAlignIfRequired(Ty, CGM.getContext()); | |||
1185 | std::optional<unsigned> DWARFAddressSpace = | |||
1186 | CGM.getTarget().getDWARFAddressSpace( | |||
1187 | CGM.getTypes().getTargetAddressSpace(PointeeTy)); | |||
1188 | ||||
1189 | SmallVector<llvm::Metadata *, 4> Annots; | |||
1190 | auto *BTFAttrTy = dyn_cast<BTFTagAttributedType>(PointeeTy); | |||
1191 | while (BTFAttrTy) { | |||
1192 | StringRef Tag = BTFAttrTy->getAttr()->getBTFTypeTag(); | |||
1193 | if (!Tag.empty()) { | |||
1194 | llvm::Metadata *Ops[2] = { | |||
1195 | llvm::MDString::get(CGM.getLLVMContext(), StringRef("btf_type_tag")), | |||
1196 | llvm::MDString::get(CGM.getLLVMContext(), Tag)}; | |||
1197 | Annots.insert(Annots.begin(), | |||
1198 | llvm::MDNode::get(CGM.getLLVMContext(), Ops)); | |||
1199 | } | |||
1200 | BTFAttrTy = dyn_cast<BTFTagAttributedType>(BTFAttrTy->getWrappedType()); | |||
1201 | } | |||
1202 | ||||
1203 | llvm::DINodeArray Annotations = nullptr; | |||
1204 | if (Annots.size() > 0) | |||
1205 | Annotations = DBuilder.getOrCreateArray(Annots); | |||
1206 | ||||
1207 | if (Tag == llvm::dwarf::DW_TAG_reference_type || | |||
1208 | Tag == llvm::dwarf::DW_TAG_rvalue_reference_type) | |||
1209 | return DBuilder.createReferenceType(Tag, getOrCreateType(PointeeTy, Unit), | |||
1210 | Size, Align, DWARFAddressSpace); | |||
1211 | else | |||
1212 | return DBuilder.createPointerType(getOrCreateType(PointeeTy, Unit), Size, | |||
1213 | Align, DWARFAddressSpace, StringRef(), | |||
1214 | Annotations); | |||
1215 | } | |||
1216 | ||||
1217 | llvm::DIType *CGDebugInfo::getOrCreateStructPtrType(StringRef Name, | |||
1218 | llvm::DIType *&Cache) { | |||
1219 | if (Cache) | |||
1220 | return Cache; | |||
1221 | Cache = DBuilder.createForwardDecl(llvm::dwarf::DW_TAG_structure_type, Name, | |||
1222 | TheCU, TheCU->getFile(), 0); | |||
1223 | unsigned Size = CGM.getContext().getTypeSize(CGM.getContext().VoidPtrTy); | |||
1224 | Cache = DBuilder.createPointerType(Cache, Size); | |||
1225 | return Cache; | |||
1226 | } | |||
1227 | ||||
1228 | uint64_t CGDebugInfo::collectDefaultElementTypesForBlockPointer( | |||
1229 | const BlockPointerType *Ty, llvm::DIFile *Unit, llvm::DIDerivedType *DescTy, | |||
1230 | unsigned LineNo, SmallVectorImpl<llvm::Metadata *> &EltTys) { | |||
1231 | QualType FType; | |||
1232 | ||||
1233 | // Advanced by calls to CreateMemberType in increments of FType, then | |||
1234 | // returned as the overall size of the default elements. | |||
1235 | uint64_t FieldOffset = 0; | |||
1236 | ||||
1237 | // Blocks in OpenCL have unique constraints which make the standard fields | |||
1238 | // redundant while requiring size and align fields for enqueue_kernel. See | |||
1239 | // initializeForBlockHeader in CGBlocks.cpp | |||
1240 | if (CGM.getLangOpts().OpenCL) { | |||
1241 | FType = CGM.getContext().IntTy; | |||
1242 | EltTys.push_back(CreateMemberType(Unit, FType, "__size", &FieldOffset)); | |||
1243 | EltTys.push_back(CreateMemberType(Unit, FType, "__align", &FieldOffset)); | |||
1244 | } else { | |||
1245 | FType = CGM.getContext().getPointerType(CGM.getContext().VoidTy); | |||
1246 | EltTys.push_back(CreateMemberType(Unit, FType, "__isa", &FieldOffset)); | |||
1247 | FType = CGM.getContext().IntTy; | |||
1248 | EltTys.push_back(CreateMemberType(Unit, FType, "__flags", &FieldOffset)); | |||
1249 | EltTys.push_back(CreateMemberType(Unit, FType, "__reserved", &FieldOffset)); | |||
1250 | FType = CGM.getContext().getPointerType(Ty->getPointeeType()); | |||
1251 | EltTys.push_back(CreateMemberType(Unit, FType, "__FuncPtr", &FieldOffset)); | |||
1252 | FType = CGM.getContext().getPointerType(CGM.getContext().VoidTy); | |||
1253 | uint64_t FieldSize = CGM.getContext().getTypeSize(Ty); | |||
1254 | uint32_t FieldAlign = CGM.getContext().getTypeAlign(Ty); | |||
1255 | EltTys.push_back(DBuilder.createMemberType( | |||
1256 | Unit, "__descriptor", nullptr, LineNo, FieldSize, FieldAlign, | |||
1257 | FieldOffset, llvm::DINode::FlagZero, DescTy)); | |||
1258 | FieldOffset += FieldSize; | |||
1259 | } | |||
1260 | ||||
1261 | return FieldOffset; | |||
1262 | } | |||
1263 | ||||
1264 | llvm::DIType *CGDebugInfo::CreateType(const BlockPointerType *Ty, | |||
1265 | llvm::DIFile *Unit) { | |||
1266 | SmallVector<llvm::Metadata *, 8> EltTys; | |||
1267 | QualType FType; | |||
1268 | uint64_t FieldOffset; | |||
1269 | llvm::DINodeArray Elements; | |||
1270 | ||||
1271 | FieldOffset = 0; | |||
1272 | FType = CGM.getContext().UnsignedLongTy; | |||
1273 | EltTys.push_back(CreateMemberType(Unit, FType, "reserved", &FieldOffset)); | |||
1274 | EltTys.push_back(CreateMemberType(Unit, FType, "Size", &FieldOffset)); | |||
1275 | ||||
1276 | Elements = DBuilder.getOrCreateArray(EltTys); | |||
1277 | EltTys.clear(); | |||
1278 | ||||
1279 | llvm::DINode::DIFlags Flags = llvm::DINode::FlagAppleBlock; | |||
1280 | ||||
1281 | auto *EltTy = | |||
1282 | DBuilder.createStructType(Unit, "__block_descriptor", nullptr, 0, | |||
1283 | FieldOffset, 0, Flags, nullptr, Elements); | |||
1284 | ||||
1285 | // Bit size, align and offset of the type. | |||
1286 | uint64_t Size = CGM.getContext().getTypeSize(Ty); | |||
1287 | ||||
1288 | auto *DescTy = DBuilder.createPointerType(EltTy, Size); | |||
1289 | ||||
1290 | FieldOffset = collectDefaultElementTypesForBlockPointer(Ty, Unit, DescTy, | |||
1291 | 0, EltTys); | |||
1292 | ||||
1293 | Elements = DBuilder.getOrCreateArray(EltTys); | |||
1294 | ||||
1295 | // The __block_literal_generic structs are marked with a special | |||
1296 | // DW_AT_APPLE_BLOCK attribute and are an implementation detail only | |||
1297 | // the debugger needs to know about. To allow type uniquing, emit | |||
1298 | // them without a name or a location. | |||
1299 | EltTy = DBuilder.createStructType(Unit, "", nullptr, 0, FieldOffset, 0, | |||
1300 | Flags, nullptr, Elements); | |||
1301 | ||||
1302 | return DBuilder.createPointerType(EltTy, Size); | |||
1303 | } | |||
1304 | ||||
1305 | llvm::DIType *CGDebugInfo::CreateType(const TemplateSpecializationType *Ty, | |||
1306 | llvm::DIFile *Unit) { | |||
1307 | assert(Ty->isTypeAlias())(static_cast <bool> (Ty->isTypeAlias()) ? void (0) : __assert_fail ("Ty->isTypeAlias()", "clang/lib/CodeGen/CGDebugInfo.cpp" , 1307, __extension__ __PRETTY_FUNCTION__)); | |||
1308 | llvm::DIType *Src = getOrCreateType(Ty->getAliasedType(), Unit); | |||
1309 | ||||
1310 | const TemplateDecl *TD = Ty->getTemplateName().getAsTemplateDecl(); | |||
1311 | if (isa<BuiltinTemplateDecl>(TD)) | |||
1312 | return Src; | |||
1313 | ||||
1314 | const auto *AliasDecl = cast<TypeAliasTemplateDecl>(TD)->getTemplatedDecl(); | |||
1315 | if (AliasDecl->hasAttr<NoDebugAttr>()) | |||
1316 | return Src; | |||
1317 | ||||
1318 | SmallString<128> NS; | |||
1319 | llvm::raw_svector_ostream OS(NS); | |||
1320 | ||||
1321 | auto PP = getPrintingPolicy(); | |||
1322 | Ty->getTemplateName().print(OS, PP, TemplateName::Qualified::None); | |||
1323 | ||||
1324 | // Disable PrintCanonicalTypes here because we want | |||
1325 | // the DW_AT_name to benefit from the TypePrinter's ability | |||
1326 | // to skip defaulted template arguments. | |||
1327 | // | |||
1328 | // FIXME: Once -gsimple-template-names is enabled by default | |||
1329 | // and we attach template parameters to alias template DIEs | |||
1330 | // we don't need to worry about customizing the PrintingPolicy | |||
1331 | // here anymore. | |||
1332 | PP.PrintCanonicalTypes = false; | |||
1333 | printTemplateArgumentList(OS, Ty->template_arguments(), PP, | |||
1334 | TD->getTemplateParameters()); | |||
1335 | ||||
1336 | SourceLocation Loc = AliasDecl->getLocation(); | |||
1337 | return DBuilder.createTypedef(Src, OS.str(), getOrCreateFile(Loc), | |||
1338 | getLineNumber(Loc), | |||
1339 | getDeclContextDescriptor(AliasDecl)); | |||
1340 | } | |||
1341 | ||||
1342 | /// Convert an AccessSpecifier into the corresponding DINode flag. | |||
1343 | /// As an optimization, return 0 if the access specifier equals the | |||
1344 | /// default for the containing type. | |||
1345 | static llvm::DINode::DIFlags getAccessFlag(AccessSpecifier Access, | |||
1346 | const RecordDecl *RD) { | |||
1347 | AccessSpecifier Default = clang::AS_none; | |||
1348 | if (RD && RD->isClass()) | |||
1349 | Default = clang::AS_private; | |||
1350 | else if (RD && (RD->isStruct() || RD->isUnion())) | |||
1351 | Default = clang::AS_public; | |||
1352 | ||||
1353 | if (Access == Default) | |||
1354 | return llvm::DINode::FlagZero; | |||
1355 | ||||
1356 | switch (Access) { | |||
1357 | case clang::AS_private: | |||
1358 | return llvm::DINode::FlagPrivate; | |||
1359 | case clang::AS_protected: | |||
1360 | return llvm::DINode::FlagProtected; | |||
1361 | case clang::AS_public: | |||
1362 | return llvm::DINode::FlagPublic; | |||
1363 | case clang::AS_none: | |||
1364 | return llvm::DINode::FlagZero; | |||
1365 | } | |||
1366 | llvm_unreachable("unexpected access enumerator")::llvm::llvm_unreachable_internal("unexpected access enumerator" , "clang/lib/CodeGen/CGDebugInfo.cpp", 1366); | |||
1367 | } | |||
1368 | ||||
1369 | llvm::DIType *CGDebugInfo::CreateType(const TypedefType *Ty, | |||
1370 | llvm::DIFile *Unit) { | |||
1371 | llvm::DIType *Underlying = | |||
1372 | getOrCreateType(Ty->getDecl()->getUnderlyingType(), Unit); | |||
1373 | ||||
1374 | if (Ty->getDecl()->hasAttr<NoDebugAttr>()) | |||
1375 | return Underlying; | |||
1376 | ||||
1377 | // We don't set size information, but do specify where the typedef was | |||
1378 | // declared. | |||
1379 | SourceLocation Loc = Ty->getDecl()->getLocation(); | |||
1380 | ||||
1381 | uint32_t Align = getDeclAlignIfRequired(Ty->getDecl(), CGM.getContext()); | |||
1382 | // Typedefs are derived from some other type. | |||
1383 | llvm::DINodeArray Annotations = CollectBTFDeclTagAnnotations(Ty->getDecl()); | |||
1384 | ||||
1385 | llvm::DINode::DIFlags Flags = llvm::DINode::FlagZero; | |||
1386 | const DeclContext *DC = Ty->getDecl()->getDeclContext(); | |||
1387 | if (isa<RecordDecl>(DC)) | |||
1388 | Flags = getAccessFlag(Ty->getDecl()->getAccess(), cast<RecordDecl>(DC)); | |||
1389 | ||||
1390 | return DBuilder.createTypedef(Underlying, Ty->getDecl()->getName(), | |||
1391 | getOrCreateFile(Loc), getLineNumber(Loc), | |||
1392 | getDeclContextDescriptor(Ty->getDecl()), Align, | |||
1393 | Flags, Annotations); | |||
1394 | } | |||
1395 | ||||
1396 | static unsigned getDwarfCC(CallingConv CC) { | |||
1397 | switch (CC) { | |||
1398 | case CC_C: | |||
1399 | // Avoid emitting DW_AT_calling_convention if the C convention was used. | |||
1400 | return 0; | |||
1401 | ||||
1402 | case CC_X86StdCall: | |||
1403 | return llvm::dwarf::DW_CC_BORLAND_stdcall; | |||
1404 | case CC_X86FastCall: | |||
1405 | return llvm::dwarf::DW_CC_BORLAND_msfastcall; | |||
1406 | case CC_X86ThisCall: | |||
1407 | return llvm::dwarf::DW_CC_BORLAND_thiscall; | |||
1408 | case CC_X86VectorCall: | |||
1409 | return llvm::dwarf::DW_CC_LLVM_vectorcall; | |||
1410 | case CC_X86Pascal: | |||
1411 | return llvm::dwarf::DW_CC_BORLAND_pascal; | |||
1412 | case CC_Win64: | |||
1413 | return llvm::dwarf::DW_CC_LLVM_Win64; | |||
1414 | case CC_X86_64SysV: | |||
1415 | return llvm::dwarf::DW_CC_LLVM_X86_64SysV; | |||
1416 | case CC_AAPCS: | |||
1417 | case CC_AArch64VectorCall: | |||
1418 | case CC_AArch64SVEPCS: | |||
1419 | return llvm::dwarf::DW_CC_LLVM_AAPCS; | |||
1420 | case CC_AAPCS_VFP: | |||
1421 | return llvm::dwarf::DW_CC_LLVM_AAPCS_VFP; | |||
1422 | case CC_IntelOclBicc: | |||
1423 | return llvm::dwarf::DW_CC_LLVM_IntelOclBicc; | |||
1424 | case CC_SpirFunction: | |||
1425 | return llvm::dwarf::DW_CC_LLVM_SpirFunction; | |||
1426 | case CC_OpenCLKernel: | |||
1427 | case CC_AMDGPUKernelCall: | |||
1428 | return llvm::dwarf::DW_CC_LLVM_OpenCLKernel; | |||
1429 | case CC_Swift: | |||
1430 | return llvm::dwarf::DW_CC_LLVM_Swift; | |||
1431 | case CC_SwiftAsync: | |||
1432 | // [FIXME: swiftasynccc] Update to SwiftAsync once LLVM support lands. | |||
1433 | return llvm::dwarf::DW_CC_LLVM_Swift; | |||
1434 | case CC_PreserveMost: | |||
1435 | return llvm::dwarf::DW_CC_LLVM_PreserveMost; | |||
1436 | case CC_PreserveAll: | |||
1437 | return llvm::dwarf::DW_CC_LLVM_PreserveAll; | |||
1438 | case CC_X86RegCall: | |||
1439 | return llvm::dwarf::DW_CC_LLVM_X86RegCall; | |||
1440 | } | |||
1441 | return 0; | |||
1442 | } | |||
1443 | ||||
1444 | static llvm::DINode::DIFlags getRefFlags(const FunctionProtoType *Func) { | |||
1445 | llvm::DINode::DIFlags Flags = llvm::DINode::FlagZero; | |||
1446 | if (Func->getExtProtoInfo().RefQualifier == RQ_LValue) | |||
1447 | Flags |= llvm::DINode::FlagLValueReference; | |||
1448 | if (Func->getExtProtoInfo().RefQualifier == RQ_RValue) | |||
1449 | Flags |= llvm::DINode::FlagRValueReference; | |||
1450 | return Flags; | |||
1451 | } | |||
1452 | ||||
1453 | llvm::DIType *CGDebugInfo::CreateType(const FunctionType *Ty, | |||
1454 | llvm::DIFile *Unit) { | |||
1455 | const auto *FPT = dyn_cast<FunctionProtoType>(Ty); | |||
1456 | if (FPT) { | |||
1457 | if (llvm::DIType *QTy = CreateQualifiedType(FPT, Unit)) | |||
1458 | return QTy; | |||
1459 | } | |||
1460 | ||||
1461 | // Create the type without any qualifiers | |||
1462 | ||||
1463 | SmallVector<llvm::Metadata *, 16> EltTys; | |||
1464 | ||||
1465 | // Add the result type at least. | |||
1466 | EltTys.push_back(getOrCreateType(Ty->getReturnType(), Unit)); | |||
1467 | ||||
1468 | llvm::DINode::DIFlags Flags = llvm::DINode::FlagZero; | |||
1469 | // Set up remainder of arguments if there is a prototype. | |||
1470 | // otherwise emit it as a variadic function. | |||
1471 | if (!FPT) { | |||
1472 | EltTys.push_back(DBuilder.createUnspecifiedParameter()); | |||
1473 | } else { | |||
1474 | Flags = getRefFlags(FPT); | |||
1475 | for (const QualType &ParamType : FPT->param_types()) | |||
1476 | EltTys.push_back(getOrCreateType(ParamType, Unit)); | |||
1477 | if (FPT->isVariadic()) | |||
1478 | EltTys.push_back(DBuilder.createUnspecifiedParameter()); | |||
1479 | } | |||
1480 | ||||
1481 | llvm::DITypeRefArray EltTypeArray = DBuilder.getOrCreateTypeArray(EltTys); | |||
1482 | llvm::DIType *F = DBuilder.createSubroutineType( | |||
1483 | EltTypeArray, Flags, getDwarfCC(Ty->getCallConv())); | |||
1484 | return F; | |||
1485 | } | |||
1486 | ||||
1487 | llvm::DIDerivedType * | |||
1488 | CGDebugInfo::createBitFieldType(const FieldDecl *BitFieldDecl, | |||
1489 | llvm::DIScope *RecordTy, const RecordDecl *RD) { | |||
1490 | StringRef Name = BitFieldDecl->getName(); | |||
1491 | QualType Ty = BitFieldDecl->getType(); | |||
1492 | SourceLocation Loc = BitFieldDecl->getLocation(); | |||
1493 | llvm::DIFile *VUnit = getOrCreateFile(Loc); | |||
1494 | llvm::DIType *DebugType = getOrCreateType(Ty, VUnit); | |||
1495 | ||||
1496 | // Get the location for the field. | |||
1497 | llvm::DIFile *File = getOrCreateFile(Loc); | |||
1498 | unsigned Line = getLineNumber(Loc); | |||
1499 | ||||
1500 | const CGBitFieldInfo &BitFieldInfo = | |||
1501 | CGM.getTypes().getCGRecordLayout(RD).getBitFieldInfo(BitFieldDecl); | |||
1502 | uint64_t SizeInBits = BitFieldInfo.Size; | |||
1503 | assert(SizeInBits > 0 && "found named 0-width bitfield")(static_cast <bool> (SizeInBits > 0 && "found named 0-width bitfield" ) ? void (0) : __assert_fail ("SizeInBits > 0 && \"found named 0-width bitfield\"" , "clang/lib/CodeGen/CGDebugInfo.cpp", 1503, __extension__ __PRETTY_FUNCTION__ )); | |||
1504 | uint64_t StorageOffsetInBits = | |||
1505 | CGM.getContext().toBits(BitFieldInfo.StorageOffset); | |||
1506 | uint64_t Offset = BitFieldInfo.Offset; | |||
1507 | // The bit offsets for big endian machines are reversed for big | |||
1508 | // endian target, compensate for that as the DIDerivedType requires | |||
1509 | // un-reversed offsets. | |||
1510 | if (CGM.getDataLayout().isBigEndian()) | |||
1511 | Offset = BitFieldInfo.StorageSize - BitFieldInfo.Size - Offset; | |||
1512 | uint64_t OffsetInBits = StorageOffsetInBits + Offset; | |||
1513 | llvm::DINode::DIFlags Flags = getAccessFlag(BitFieldDecl->getAccess(), RD); | |||
1514 | llvm::DINodeArray Annotations = CollectBTFDeclTagAnnotations(BitFieldDecl); | |||
1515 | return DBuilder.createBitFieldMemberType( | |||
1516 | RecordTy, Name, File, Line, SizeInBits, OffsetInBits, StorageOffsetInBits, | |||
1517 | Flags, DebugType, Annotations); | |||
1518 | } | |||
1519 | ||||
1520 | llvm::DIDerivedType *CGDebugInfo::createBitFieldSeparatorIfNeeded( | |||
1521 | const FieldDecl *BitFieldDecl, const llvm::DIDerivedType *BitFieldDI, | |||
1522 | llvm::ArrayRef<llvm::Metadata *> PreviousFieldsDI, const RecordDecl *RD) { | |||
1523 | ||||
1524 | if (!CGM.getTargetCodeGenInfo().shouldEmitDWARFBitFieldSeparators()) | |||
1525 | return nullptr; | |||
1526 | ||||
1527 | /* | |||
1528 | Add a *single* zero-bitfield separator between two non-zero bitfields | |||
1529 | separated by one or more zero-bitfields. This is used to distinguish between | |||
1530 | structures such the ones below, where the memory layout is the same, but how | |||
1531 | the ABI assigns fields to registers differs. | |||
1532 | ||||
1533 | struct foo { | |||
1534 | int space[4]; | |||
1535 | char a : 8; // on amdgpu, passed on v4 | |||
1536 | char b : 8; | |||
1537 | char x : 8; | |||
1538 | char y : 8; | |||
1539 | }; | |||
1540 | struct bar { | |||
1541 | int space[4]; | |||
1542 | char a : 8; // on amdgpu, passed on v4 | |||
1543 | char b : 8; | |||
1544 | char : 0; | |||
1545 | char x : 8; // passed on v5 | |||
1546 | char y : 8; | |||
1547 | }; | |||
1548 | */ | |||
1549 | if (PreviousFieldsDI.empty()) | |||
1550 | return nullptr; | |||
1551 | ||||
1552 | // If we already emitted metadata for a 0-length bitfield, nothing to do here. | |||
1553 | auto *PreviousMDEntry = | |||
1554 | PreviousFieldsDI.empty() ? nullptr : PreviousFieldsDI.back(); | |||
1555 | auto *PreviousMDField = | |||
1556 | dyn_cast_or_null<llvm::DIDerivedType>(PreviousMDEntry); | |||
1557 | if (!PreviousMDField || !PreviousMDField->isBitField() || | |||
1558 | PreviousMDField->getSizeInBits() == 0) | |||
1559 | return nullptr; | |||
1560 | ||||
1561 | auto PreviousBitfield = RD->field_begin(); | |||
1562 | std::advance(PreviousBitfield, BitFieldDecl->getFieldIndex() - 1); | |||
1563 | ||||
1564 | assert(PreviousBitfield->isBitField())(static_cast <bool> (PreviousBitfield->isBitField()) ? void (0) : __assert_fail ("PreviousBitfield->isBitField()" , "clang/lib/CodeGen/CGDebugInfo.cpp", 1564, __extension__ __PRETTY_FUNCTION__ )); | |||
1565 | ||||
1566 | ASTContext &Context = CGM.getContext(); | |||
1567 | if (!PreviousBitfield->isZeroLengthBitField(Context)) | |||
1568 | return nullptr; | |||
1569 | ||||
1570 | QualType Ty = PreviousBitfield->getType(); | |||
1571 | SourceLocation Loc = PreviousBitfield->getLocation(); | |||
1572 | llvm::DIFile *VUnit = getOrCreateFile(Loc); | |||
1573 | llvm::DIType *DebugType = getOrCreateType(Ty, VUnit); | |||
1574 | llvm::DIScope *RecordTy = BitFieldDI->getScope(); | |||
1575 | ||||
1576 | llvm::DIFile *File = getOrCreateFile(Loc); | |||
1577 | unsigned Line = getLineNumber(Loc); | |||
1578 | ||||
1579 | uint64_t StorageOffsetInBits = | |||
1580 | cast<llvm::ConstantInt>(BitFieldDI->getStorageOffsetInBits()) | |||
1581 | ->getZExtValue(); | |||
1582 | ||||
1583 | llvm::DINode::DIFlags Flags = | |||
1584 | getAccessFlag(PreviousBitfield->getAccess(), RD); | |||
1585 | llvm::DINodeArray Annotations = | |||
1586 | CollectBTFDeclTagAnnotations(*PreviousBitfield); | |||
1587 | return DBuilder.createBitFieldMemberType( | |||
1588 | RecordTy, "", File, Line, 0, StorageOffsetInBits, StorageOffsetInBits, | |||
1589 | Flags, DebugType, Annotations); | |||
1590 | } | |||
1591 | ||||
1592 | llvm::DIType *CGDebugInfo::createFieldType( | |||
1593 | StringRef name, QualType type, SourceLocation loc, AccessSpecifier AS, | |||
1594 | uint64_t offsetInBits, uint32_t AlignInBits, llvm::DIFile *tunit, | |||
1595 | llvm::DIScope *scope, const RecordDecl *RD, llvm::DINodeArray Annotations) { | |||
1596 | llvm::DIType *debugType = getOrCreateType(type, tunit); | |||
1597 | ||||
1598 | // Get the location for the field. | |||
1599 | llvm::DIFile *file = getOrCreateFile(loc); | |||
1600 | const unsigned line = getLineNumber(loc.isValid() ? loc : CurLoc); | |||
1601 | ||||
1602 | uint64_t SizeInBits = 0; | |||
1603 | auto Align = AlignInBits; | |||
1604 | if (!type->isIncompleteArrayType()) { | |||
1605 | TypeInfo TI = CGM.getContext().getTypeInfo(type); | |||
1606 | SizeInBits = TI.Width; | |||
1607 | if (!Align) | |||
1608 | Align = getTypeAlignIfRequired(type, CGM.getContext()); | |||
1609 | } | |||
1610 | ||||
1611 | llvm::DINode::DIFlags flags = getAccessFlag(AS, RD); | |||
1612 | return DBuilder.createMemberType(scope, name, file, line, SizeInBits, Align, | |||
1613 | offsetInBits, flags, debugType, Annotations); | |||
1614 | } | |||
1615 | ||||
1616 | void CGDebugInfo::CollectRecordLambdaFields( | |||
1617 | const CXXRecordDecl *CXXDecl, SmallVectorImpl<llvm::Metadata *> &elements, | |||
1618 | llvm::DIType *RecordTy) { | |||
1619 | // For C++11 Lambdas a Field will be the same as a Capture, but the Capture | |||
1620 | // has the name and the location of the variable so we should iterate over | |||
1621 | // both concurrently. | |||
1622 | const ASTRecordLayout &layout = CGM.getContext().getASTRecordLayout(CXXDecl); | |||
1623 | RecordDecl::field_iterator Field = CXXDecl->field_begin(); | |||
1624 | unsigned fieldno = 0; | |||
1625 | for (CXXRecordDecl::capture_const_iterator I = CXXDecl->captures_begin(), | |||
1626 | E = CXXDecl->captures_end(); | |||
1627 | I != E; ++I, ++Field, ++fieldno) { | |||
1628 | const LambdaCapture &C = *I; | |||
1629 | if (C.capturesVariable()) { | |||
1630 | SourceLocation Loc = C.getLocation(); | |||
1631 | assert(!Field->isBitField() && "lambdas don't have bitfield members!")(static_cast <bool> (!Field->isBitField() && "lambdas don't have bitfield members!") ? void (0) : __assert_fail ("!Field->isBitField() && \"lambdas don't have bitfield members!\"" , "clang/lib/CodeGen/CGDebugInfo.cpp", 1631, __extension__ __PRETTY_FUNCTION__ )); | |||
1632 | ValueDecl *V = C.getCapturedVar(); | |||
1633 | StringRef VName = V->getName(); | |||
1634 | llvm::DIFile *VUnit = getOrCreateFile(Loc); | |||
1635 | auto Align = getDeclAlignIfRequired(V, CGM.getContext()); | |||
1636 | llvm::DIType *FieldType = createFieldType( | |||
1637 | VName, Field->getType(), Loc, Field->getAccess(), | |||
1638 | layout.getFieldOffset(fieldno), Align, VUnit, RecordTy, CXXDecl); | |||
1639 | elements.push_back(FieldType); | |||
1640 | } else if (C.capturesThis()) { | |||
1641 | // TODO: Need to handle 'this' in some way by probably renaming the | |||
1642 | // this of the lambda class and having a field member of 'this' or | |||
1643 | // by using AT_object_pointer for the function and having that be | |||
1644 | // used as 'this' for semantic references. | |||
1645 | FieldDecl *f = *Field; | |||
1646 | llvm::DIFile *VUnit = getOrCreateFile(f->getLocation()); | |||
1647 | QualType type = f->getType(); | |||
1648 | llvm::DIType *fieldType = createFieldType( | |||
1649 | "this", type, f->getLocation(), f->getAccess(), | |||
1650 | layout.getFieldOffset(fieldno), VUnit, RecordTy, CXXDecl); | |||
1651 | ||||
1652 | elements.push_back(fieldType); | |||
1653 | } | |||
1654 | } | |||
1655 | } | |||
1656 | ||||
1657 | llvm::DIDerivedType * | |||
1658 | CGDebugInfo::CreateRecordStaticField(const VarDecl *Var, llvm::DIType *RecordTy, | |||
1659 | const RecordDecl *RD) { | |||
1660 | // Create the descriptor for the static variable, with or without | |||
1661 | // constant initializers. | |||
1662 | Var = Var->getCanonicalDecl(); | |||
1663 | llvm::DIFile *VUnit = getOrCreateFile(Var->getLocation()); | |||
1664 | llvm::DIType *VTy = getOrCreateType(Var->getType(), VUnit); | |||
1665 | ||||
1666 | unsigned LineNumber = getLineNumber(Var->getLocation()); | |||
1667 | StringRef VName = Var->getName(); | |||
1668 | llvm::Constant *C = nullptr; | |||
1669 | if (Var->getInit()) { | |||
1670 | const APValue *Value = Var->evaluateValue(); | |||
1671 | if (Value) { | |||
1672 | if (Value->isInt()) | |||
1673 | C = llvm::ConstantInt::get(CGM.getLLVMContext(), Value->getInt()); | |||
1674 | if (Value->isFloat()) | |||
1675 | C = llvm::ConstantFP::get(CGM.getLLVMContext(), Value->getFloat()); | |||
1676 | } | |||
1677 | } | |||
1678 | ||||
1679 | llvm::DINode::DIFlags Flags = getAccessFlag(Var->getAccess(), RD); | |||
1680 | auto Align = getDeclAlignIfRequired(Var, CGM.getContext()); | |||
1681 | llvm::DIDerivedType *GV = DBuilder.createStaticMemberType( | |||
1682 | RecordTy, VName, VUnit, LineNumber, VTy, Flags, C, Align); | |||
1683 | StaticDataMemberCache[Var->getCanonicalDecl()].reset(GV); | |||
1684 | return GV; | |||
1685 | } | |||
1686 | ||||
1687 | void CGDebugInfo::CollectRecordNormalField( | |||
1688 | const FieldDecl *field, uint64_t OffsetInBits, llvm::DIFile *tunit, | |||
1689 | SmallVectorImpl<llvm::Metadata *> &elements, llvm::DIType *RecordTy, | |||
1690 | const RecordDecl *RD) { | |||
1691 | StringRef name = field->getName(); | |||
1692 | QualType type = field->getType(); | |||
1693 | ||||
1694 | // Ignore unnamed fields unless they're anonymous structs/unions. | |||
1695 | if (name.empty() && !type->isRecordType()) | |||
1696 | return; | |||
1697 | ||||
1698 | llvm::DIType *FieldType; | |||
1699 | if (field->isBitField()) { | |||
1700 | llvm::DIDerivedType *BitFieldType; | |||
1701 | FieldType = BitFieldType = createBitFieldType(field, RecordTy, RD); | |||
1702 | if (llvm::DIType *Separator = | |||
1703 | createBitFieldSeparatorIfNeeded(field, BitFieldType, elements, RD)) | |||
1704 | elements.push_back(Separator); | |||
1705 | } else { | |||
1706 | auto Align = getDeclAlignIfRequired(field, CGM.getContext()); | |||
1707 | llvm::DINodeArray Annotations = CollectBTFDeclTagAnnotations(field); | |||
1708 | FieldType = | |||
1709 | createFieldType(name, type, field->getLocation(), field->getAccess(), | |||
1710 | OffsetInBits, Align, tunit, RecordTy, RD, Annotations); | |||
1711 | } | |||
1712 | ||||
1713 | elements.push_back(FieldType); | |||
1714 | } | |||
1715 | ||||
1716 | void CGDebugInfo::CollectRecordNestedType( | |||
1717 | const TypeDecl *TD, SmallVectorImpl<llvm::Metadata *> &elements) { | |||
1718 | QualType Ty = CGM.getContext().getTypeDeclType(TD); | |||
1719 | // Injected class names are not considered nested records. | |||
1720 | if (isa<InjectedClassNameType>(Ty)) | |||
1721 | return; | |||
1722 | SourceLocation Loc = TD->getLocation(); | |||
1723 | llvm::DIType *nestedType = getOrCreateType(Ty, getOrCreateFile(Loc)); | |||
1724 | elements.push_back(nestedType); | |||
1725 | } | |||
1726 | ||||
1727 | void CGDebugInfo::CollectRecordFields( | |||
1728 | const RecordDecl *record, llvm::DIFile *tunit, | |||
1729 | SmallVectorImpl<llvm::Metadata *> &elements, | |||
1730 | llvm::DICompositeType *RecordTy) { | |||
1731 | const auto *CXXDecl = dyn_cast<CXXRecordDecl>(record); | |||
1732 | ||||
1733 | if (CXXDecl && CXXDecl->isLambda()) | |||
1734 | CollectRecordLambdaFields(CXXDecl, elements, RecordTy); | |||
1735 | else { | |||
1736 | const ASTRecordLayout &layout = CGM.getContext().getASTRecordLayout(record); | |||
1737 | ||||
1738 | // Field number for non-static fields. | |||
1739 | unsigned fieldNo = 0; | |||
1740 | ||||
1741 | // Static and non-static members should appear in the same order as | |||
1742 | // the corresponding declarations in the source program. | |||
1743 | for (const auto *I : record->decls()) | |||
1744 | if (const auto *V = dyn_cast<VarDecl>(I)) { | |||
1745 | if (V->hasAttr<NoDebugAttr>()) | |||
1746 | continue; | |||
1747 | ||||
1748 | // Skip variable template specializations when emitting CodeView. MSVC | |||
1749 | // doesn't emit them. | |||
1750 | if (CGM.getCodeGenOpts().EmitCodeView && | |||
1751 | isa<VarTemplateSpecializationDecl>(V)) | |||
1752 | continue; | |||
1753 | ||||
1754 | if (isa<VarTemplatePartialSpecializationDecl>(V)) | |||
1755 | continue; | |||
1756 | ||||
1757 | // Reuse the existing static member declaration if one exists | |||
1758 | auto MI = StaticDataMemberCache.find(V->getCanonicalDecl()); | |||
1759 | if (MI != StaticDataMemberCache.end()) { | |||
1760 | assert(MI->second &&(static_cast <bool> (MI->second && "Static data member declaration should still exist" ) ? void (0) : __assert_fail ("MI->second && \"Static data member declaration should still exist\"" , "clang/lib/CodeGen/CGDebugInfo.cpp", 1761, __extension__ __PRETTY_FUNCTION__ )) | |||
1761 | "Static data member declaration should still exist")(static_cast <bool> (MI->second && "Static data member declaration should still exist" ) ? void (0) : __assert_fail ("MI->second && \"Static data member declaration should still exist\"" , "clang/lib/CodeGen/CGDebugInfo.cpp", 1761, __extension__ __PRETTY_FUNCTION__ )); | |||
1762 | elements.push_back(MI->second); | |||
1763 | } else { | |||
1764 | auto Field = CreateRecordStaticField(V, RecordTy, record); | |||
1765 | elements.push_back(Field); | |||
1766 | } | |||
1767 | } else if (const auto *field = dyn_cast<FieldDecl>(I)) { | |||
1768 | CollectRecordNormalField(field, layout.getFieldOffset(fieldNo), tunit, | |||
1769 | elements, RecordTy, record); | |||
1770 | ||||
1771 | // Bump field number for next field. | |||
1772 | ++fieldNo; | |||
1773 | } else if (CGM.getCodeGenOpts().EmitCodeView) { | |||
1774 | // Debug info for nested types is included in the member list only for | |||
1775 | // CodeView. | |||
1776 | if (const auto *nestedType = dyn_cast<TypeDecl>(I)) { | |||
1777 | // MSVC doesn't generate nested type for anonymous struct/union. | |||
1778 | if (isa<RecordDecl>(I) && | |||
1779 | cast<RecordDecl>(I)->isAnonymousStructOrUnion()) | |||
1780 | continue; | |||
1781 | if (!nestedType->isImplicit() && | |||
1782 | nestedType->getDeclContext() == record) | |||
1783 | CollectRecordNestedType(nestedType, elements); | |||
1784 | } | |||
1785 | } | |||
1786 | } | |||
1787 | } | |||
1788 | ||||
1789 | llvm::DISubroutineType * | |||
1790 | CGDebugInfo::getOrCreateMethodType(const CXXMethodDecl *Method, | |||
1791 | llvm::DIFile *Unit) { | |||
1792 | const FunctionProtoType *Func = Method->getType()->getAs<FunctionProtoType>(); | |||
1793 | if (Method->isStatic()) | |||
1794 | return cast_or_null<llvm::DISubroutineType>( | |||
1795 | getOrCreateType(QualType(Func, 0), Unit)); | |||
1796 | return getOrCreateInstanceMethodType(Method->getThisType(), Func, Unit); | |||
1797 | } | |||
1798 | ||||
1799 | llvm::DISubroutineType *CGDebugInfo::getOrCreateInstanceMethodType( | |||
1800 | QualType ThisPtr, const FunctionProtoType *Func, llvm::DIFile *Unit) { | |||
1801 | FunctionProtoType::ExtProtoInfo EPI = Func->getExtProtoInfo(); | |||
1802 | Qualifiers &Qc = EPI.TypeQuals; | |||
1803 | Qc.removeConst(); | |||
1804 | Qc.removeVolatile(); | |||
1805 | Qc.removeRestrict(); | |||
1806 | Qc.removeUnaligned(); | |||
1807 | // Keep the removed qualifiers in sync with | |||
1808 | // CreateQualifiedType(const FunctionPrototype*, DIFile *Unit) | |||
1809 | // On a 'real' member function type, these qualifiers are carried on the type | |||
1810 | // of the first parameter, not as separate DW_TAG_const_type (etc) decorator | |||
1811 | // tags around them. (But, in the raw function types with qualifiers, they have | |||
1812 | // to use wrapper types.) | |||
1813 | ||||
1814 | // Add "this" pointer. | |||
1815 | const auto *OriginalFunc = cast<llvm::DISubroutineType>( | |||
1816 | getOrCreateType(CGM.getContext().getFunctionType( | |||
1817 | Func->getReturnType(), Func->getParamTypes(), EPI), | |||
1818 | Unit)); | |||
1819 | llvm::DITypeRefArray Args = OriginalFunc->getTypeArray(); | |||
1820 | assert(Args.size() && "Invalid number of arguments!")(static_cast <bool> (Args.size() && "Invalid number of arguments!" ) ? void (0) : __assert_fail ("Args.size() && \"Invalid number of arguments!\"" , "clang/lib/CodeGen/CGDebugInfo.cpp", 1820, __extension__ __PRETTY_FUNCTION__ )); | |||
1821 | ||||
1822 | SmallVector<llvm::Metadata *, 16> Elts; | |||
1823 | ||||
1824 | // First element is always return type. For 'void' functions it is NULL. | |||
1825 | Elts.push_back(Args[0]); | |||
1826 | ||||
1827 | // "this" pointer is always first argument. | |||
1828 | const CXXRecordDecl *RD = ThisPtr->getPointeeCXXRecordDecl(); | |||
1829 | if (isa<ClassTemplateSpecializationDecl>(RD)) { | |||
1830 | // Create pointer type directly in this case. | |||
1831 | const PointerType *ThisPtrTy = cast<PointerType>(ThisPtr); | |||
1832 | uint64_t Size = CGM.getContext().getTypeSize(ThisPtrTy); | |||
1833 | auto Align = getTypeAlignIfRequired(ThisPtrTy, CGM.getContext()); | |||
1834 | llvm::DIType *PointeeType = | |||
1835 | getOrCreateType(ThisPtrTy->getPointeeType(), Unit); | |||
1836 | llvm::DIType *ThisPtrType = | |||
1837 | DBuilder.createPointerType(PointeeType, Size, Align); | |||
1838 | TypeCache[ThisPtr.getAsOpaquePtr()].reset(ThisPtrType); | |||
1839 | // TODO: This and the artificial type below are misleading, the | |||
1840 | // types aren't artificial the argument is, but the current | |||
1841 | // metadata doesn't represent that. | |||
1842 | ThisPtrType = DBuilder.createObjectPointerType(ThisPtrType); | |||
1843 | Elts.push_back(ThisPtrType); | |||
1844 | } else { | |||
1845 | llvm::DIType *ThisPtrType = getOrCreateType(ThisPtr, Unit); | |||
1846 | TypeCache[ThisPtr.getAsOpaquePtr()].reset(ThisPtrType); | |||
1847 | ThisPtrType = DBuilder.createObjectPointerType(ThisPtrType); | |||
1848 | Elts.push_back(ThisPtrType); | |||
1849 | } | |||
1850 | ||||
1851 | // Copy rest of the arguments. | |||
1852 | for (unsigned i = 1, e = Args.size(); i != e; ++i) | |||
1853 | Elts.push_back(Args[i]); | |||
1854 | ||||
1855 | llvm::DITypeRefArray EltTypeArray = DBuilder.getOrCreateTypeArray(Elts); | |||
1856 | ||||
1857 | return DBuilder.createSubroutineType(EltTypeArray, OriginalFunc->getFlags(), | |||
1858 | getDwarfCC(Func->getCallConv())); | |||
1859 | } | |||
1860 | ||||
1861 | /// isFunctionLocalClass - Return true if CXXRecordDecl is defined | |||
1862 | /// inside a function. | |||
1863 | static bool isFunctionLocalClass(const CXXRecordDecl *RD) { | |||
1864 | if (const auto *NRD = dyn_cast<CXXRecordDecl>(RD->getDeclContext())) | |||
1865 | return isFunctionLocalClass(NRD); | |||
1866 | if (isa<FunctionDecl>(RD->getDeclContext())) | |||
1867 | return true; | |||
1868 | return false; | |||
1869 | } | |||
1870 | ||||
1871 | llvm::DISubprogram *CGDebugInfo::CreateCXXMemberFunction( | |||
1872 | const CXXMethodDecl *Method, llvm::DIFile *Unit, llvm::DIType *RecordTy) { | |||
1873 | bool IsCtorOrDtor = | |||
1874 | isa<CXXConstructorDecl>(Method) || isa<CXXDestructorDecl>(Method); | |||
1875 | ||||
1876 | StringRef MethodName = getFunctionName(Method); | |||
1877 | llvm::DISubroutineType *MethodTy = getOrCreateMethodType(Method, Unit); | |||
1878 | ||||
1879 | // Since a single ctor/dtor corresponds to multiple functions, it doesn't | |||
1880 | // make sense to give a single ctor/dtor a linkage name. | |||
1881 | StringRef MethodLinkageName; | |||
1882 | // FIXME: 'isFunctionLocalClass' seems like an arbitrary/unintentional | |||
1883 | // property to use here. It may've been intended to model "is non-external | |||
1884 | // type" but misses cases of non-function-local but non-external classes such | |||
1885 | // as those in anonymous namespaces as well as the reverse - external types | |||
1886 | // that are function local, such as those in (non-local) inline functions. | |||
1887 | if (!IsCtorOrDtor && !isFunctionLocalClass(Method->getParent())) | |||
1888 | MethodLinkageName = CGM.getMangledName(Method); | |||
1889 | ||||
1890 | // Get the location for the method. | |||
1891 | llvm::DIFile *MethodDefUnit = nullptr; | |||
1892 | unsigned MethodLine = 0; | |||
1893 | if (!Method->isImplicit()) { | |||
1894 | MethodDefUnit = getOrCreateFile(Method->getLocation()); | |||
1895 | MethodLine = getLineNumber(Method->getLocation()); | |||
1896 | } | |||
1897 | ||||
1898 | // Collect virtual method info. | |||
1899 | llvm::DIType *ContainingType = nullptr; | |||
1900 | unsigned VIndex = 0; | |||
1901 | llvm::DINode::DIFlags Flags = llvm::DINode::FlagZero; | |||
1902 | llvm::DISubprogram::DISPFlags SPFlags = llvm::DISubprogram::SPFlagZero; | |||
1903 | int ThisAdjustment = 0; | |||
1904 | ||||
1905 | if (VTableContextBase::hasVtableSlot(Method)) { | |||
1906 | if (Method->isPure()) | |||
1907 | SPFlags |= llvm::DISubprogram::SPFlagPureVirtual; | |||
1908 | else | |||
1909 | SPFlags |= llvm::DISubprogram::SPFlagVirtual; | |||
1910 | ||||
1911 | if (CGM.getTarget().getCXXABI().isItaniumFamily()) { | |||
1912 | // It doesn't make sense to give a virtual destructor a vtable index, | |||
1913 | // since a single destructor has two entries in the vtable. | |||
1914 | if (!isa<CXXDestructorDecl>(Method)) | |||
1915 | VIndex = CGM.getItaniumVTableContext().getMethodVTableIndex(Method); | |||
1916 | } else { | |||
1917 | // Emit MS ABI vftable information. There is only one entry for the | |||
1918 | // deleting dtor. | |||
1919 | const auto *DD = dyn_cast<CXXDestructorDecl>(Method); | |||
1920 | GlobalDecl GD = DD ? GlobalDecl(DD, Dtor_Deleting) : GlobalDecl(Method); | |||
1921 | MethodVFTableLocation ML = | |||
1922 | CGM.getMicrosoftVTableContext().getMethodVFTableLocation(GD); | |||
1923 | VIndex = ML.Index; | |||
1924 | ||||
1925 | // CodeView only records the vftable offset in the class that introduces | |||
1926 | // the virtual method. This is possible because, unlike Itanium, the MS | |||
1927 | // C++ ABI does not include all virtual methods from non-primary bases in | |||
1928 | // the vtable for the most derived class. For example, if C inherits from | |||
1929 | // A and B, C's primary vftable will not include B's virtual methods. | |||
1930 | if (Method->size_overridden_methods() == 0) | |||
1931 | Flags |= llvm::DINode::FlagIntroducedVirtual; | |||
1932 | ||||
1933 | // The 'this' adjustment accounts for both the virtual and non-virtual | |||
1934 | // portions of the adjustment. Presumably the debugger only uses it when | |||
1935 | // it knows the dynamic type of an object. | |||
1936 | ThisAdjustment = CGM.getCXXABI() | |||
1937 | .getVirtualFunctionPrologueThisAdjustment(GD) | |||
1938 | .getQuantity(); | |||
1939 | } | |||
1940 | ContainingType = RecordTy; | |||
1941 | } | |||
1942 | ||||
1943 | // We're checking for deleted C++ special member functions | |||
1944 | // [Ctors,Dtors, Copy/Move] | |||
1945 | auto checkAttrDeleted = [&](const auto *Method) { | |||
1946 | if (Method->getCanonicalDecl()->isDeleted()) | |||
1947 | SPFlags |= llvm::DISubprogram::SPFlagDeleted; | |||
1948 | }; | |||
1949 | ||||
1950 | switch (Method->getKind()) { | |||
1951 | ||||
1952 | case Decl::CXXConstructor: | |||
1953 | case Decl::CXXDestructor: | |||
1954 | checkAttrDeleted(Method); | |||
1955 | break; | |||
1956 | case Decl::CXXMethod: | |||
1957 | if (Method->isCopyAssignmentOperator() || | |||
1958 | Method->isMoveAssignmentOperator()) | |||
1959 | checkAttrDeleted(Method); | |||
1960 | break; | |||
1961 | default: | |||
1962 | break; | |||
1963 | } | |||
1964 | ||||
1965 | if (Method->isNoReturn()) | |||
1966 | Flags |= llvm::DINode::FlagNoReturn; | |||
1967 | ||||
1968 | if (Method->isStatic()) | |||
1969 | Flags |= llvm::DINode::FlagStaticMember; | |||
1970 | if (Method->isImplicit()) | |||
1971 | Flags |= llvm::DINode::FlagArtificial; | |||
1972 | Flags |= getAccessFlag(Method->getAccess(), Method->getParent()); | |||
1973 | if (const auto *CXXC = dyn_cast<CXXConstructorDecl>(Method)) { | |||
1974 | if (CXXC->isExplicit()) | |||
1975 | Flags |= llvm::DINode::FlagExplicit; | |||
1976 | } else if (const auto *CXXC = dyn_cast<CXXConversionDecl>(Method)) { | |||
1977 | if (CXXC->isExplicit()) | |||
1978 | Flags |= llvm::DINode::FlagExplicit; | |||
1979 | } | |||
1980 | if (Method->hasPrototype()) | |||
1981 | Flags |= llvm::DINode::FlagPrototyped; | |||
1982 | if (Method->getRefQualifier() == RQ_LValue) | |||
1983 | Flags |= llvm::DINode::FlagLValueReference; | |||
1984 | if (Method->getRefQualifier() == RQ_RValue) | |||
1985 | Flags |= llvm::DINode::FlagRValueReference; | |||
1986 | if (!Method->isExternallyVisible()) | |||
1987 | SPFlags |= llvm::DISubprogram::SPFlagLocalToUnit; | |||
1988 | if (CGM.getLangOpts().Optimize) | |||
1989 | SPFlags |= llvm::DISubprogram::SPFlagOptimized; | |||
1990 | ||||
1991 | // In this debug mode, emit type info for a class when its constructor type | |||
1992 | // info is emitted. | |||
1993 | if (DebugKind == llvm::codegenoptions::DebugInfoConstructor) | |||
1994 | if (const CXXConstructorDecl *CD = dyn_cast<CXXConstructorDecl>(Method)) | |||
1995 | completeUnusedClass(*CD->getParent()); | |||
1996 | ||||
1997 | llvm::DINodeArray TParamsArray = CollectFunctionTemplateParams(Method, Unit); | |||
1998 | llvm::DISubprogram *SP = DBuilder.createMethod( | |||
1999 | RecordTy, MethodName, MethodLinkageName, MethodDefUnit, MethodLine, | |||
2000 | MethodTy, VIndex, ThisAdjustment, ContainingType, Flags, SPFlags, | |||
2001 | TParamsArray.get()); | |||
2002 | ||||
2003 | SPCache[Method->getCanonicalDecl()].reset(SP); | |||
2004 | ||||
2005 | return SP; | |||
2006 | } | |||
2007 | ||||
2008 | void CGDebugInfo::CollectCXXMemberFunctions( | |||
2009 | const CXXRecordDecl *RD, llvm::DIFile *Unit, | |||
2010 | SmallVectorImpl<llvm::Metadata *> &EltTys, llvm::DIType *RecordTy) { | |||
2011 | ||||
2012 | // Since we want more than just the individual member decls if we | |||
2013 | // have templated functions iterate over every declaration to gather | |||
2014 | // the functions. | |||
2015 | for (const auto *I : RD->decls()) { | |||
2016 | const auto *Method = dyn_cast<CXXMethodDecl>(I); | |||
2017 | // If the member is implicit, don't add it to the member list. This avoids | |||
2018 | // the member being added to type units by LLVM, while still allowing it | |||
2019 | // to be emitted into the type declaration/reference inside the compile | |||
2020 | // unit. | |||
2021 | // Ditto 'nodebug' methods, for consistency with CodeGenFunction.cpp. | |||
2022 | // FIXME: Handle Using(Shadow?)Decls here to create | |||
2023 | // DW_TAG_imported_declarations inside the class for base decls brought into | |||
2024 | // derived classes. GDB doesn't seem to notice/leverage these when I tried | |||
2025 | // it, so I'm not rushing to fix this. (GCC seems to produce them, if | |||
2026 | // referenced) | |||
2027 | if (!Method || Method->isImplicit() || Method->hasAttr<NoDebugAttr>()) | |||
2028 | continue; | |||
2029 | ||||
2030 | if (Method->getType()->castAs<FunctionProtoType>()->getContainedAutoType()) | |||
2031 | continue; | |||
2032 | ||||
2033 | // Reuse the existing member function declaration if it exists. | |||
2034 | // It may be associated with the declaration of the type & should be | |||
2035 | // reused as we're building the definition. | |||
2036 | // | |||
2037 | // This situation can arise in the vtable-based debug info reduction where | |||
2038 | // implicit members are emitted in a non-vtable TU. | |||
2039 | auto MI = SPCache.find(Method->getCanonicalDecl()); | |||
2040 | EltTys.push_back(MI == SPCache.end() | |||
2041 | ? CreateCXXMemberFunction(Method, Unit, RecordTy) | |||
2042 | : static_cast<llvm::Metadata *>(MI->second)); | |||
2043 | } | |||
2044 | } | |||
2045 | ||||
2046 | void CGDebugInfo::CollectCXXBases(const CXXRecordDecl *RD, llvm::DIFile *Unit, | |||
2047 | SmallVectorImpl<llvm::Metadata *> &EltTys, | |||
2048 | llvm::DIType *RecordTy) { | |||
2049 | llvm::DenseSet<CanonicalDeclPtr<const CXXRecordDecl>> SeenTypes; | |||
2050 | CollectCXXBasesAux(RD, Unit, EltTys, RecordTy, RD->bases(), SeenTypes, | |||
2051 | llvm::DINode::FlagZero); | |||
2052 | ||||
2053 | // If we are generating CodeView debug info, we also need to emit records for | |||
2054 | // indirect virtual base classes. | |||
2055 | if (CGM.getCodeGenOpts().EmitCodeView) { | |||
2056 | CollectCXXBasesAux(RD, Unit, EltTys, RecordTy, RD->vbases(), SeenTypes, | |||
2057 | llvm::DINode::FlagIndirectVirtualBase); | |||
2058 | } | |||
2059 | } | |||
2060 | ||||
2061 | void CGDebugInfo::CollectCXXBasesAux( | |||
2062 | const CXXRecordDecl *RD, llvm::DIFile *Unit, | |||
2063 | SmallVectorImpl<llvm::Metadata *> &EltTys, llvm::DIType *RecordTy, | |||
2064 | const CXXRecordDecl::base_class_const_range &Bases, | |||
2065 | llvm::DenseSet<CanonicalDeclPtr<const CXXRecordDecl>> &SeenTypes, | |||
2066 | llvm::DINode::DIFlags StartingFlags) { | |||
2067 | const ASTRecordLayout &RL = CGM.getContext().getASTRecordLayout(RD); | |||
2068 | for (const auto &BI : Bases) { | |||
2069 | const auto *Base = | |||
2070 | cast<CXXRecordDecl>(BI.getType()->castAs<RecordType>()->getDecl()); | |||
2071 | if (!SeenTypes.insert(Base).second) | |||
2072 | continue; | |||
2073 | auto *BaseTy = getOrCreateType(BI.getType(), Unit); | |||
2074 | llvm::DINode::DIFlags BFlags = StartingFlags; | |||
2075 | uint64_t BaseOffset; | |||
2076 | uint32_t VBPtrOffset = 0; | |||
2077 | ||||
2078 | if (BI.isVirtual()) { | |||
2079 | if (CGM.getTarget().getCXXABI().isItaniumFamily()) { | |||
2080 | // virtual base offset offset is -ve. The code generator emits dwarf | |||
2081 | // expression where it expects +ve number. | |||
2082 | BaseOffset = 0 - CGM.getItaniumVTableContext() | |||
2083 | .getVirtualBaseOffsetOffset(RD, Base) | |||
2084 | .getQuantity(); | |||
2085 | } else { | |||
2086 | // In the MS ABI, store the vbtable offset, which is analogous to the | |||
2087 | // vbase offset offset in Itanium. | |||
2088 | BaseOffset = | |||
2089 | 4 * CGM.getMicrosoftVTableContext().getVBTableIndex(RD, Base); | |||
2090 | VBPtrOffset = CGM.getContext() | |||
2091 | .getASTRecordLayout(RD) | |||
2092 | .getVBPtrOffset() | |||
2093 | .getQuantity(); | |||
2094 | } | |||
2095 | BFlags |= llvm::DINode::FlagVirtual; | |||
2096 | } else | |||
2097 | BaseOffset = CGM.getContext().toBits(RL.getBaseClassOffset(Base)); | |||
2098 | // FIXME: Inconsistent units for BaseOffset. It is in bytes when | |||
2099 | // BI->isVirtual() and bits when not. | |||
2100 | ||||
2101 | BFlags |= getAccessFlag(BI.getAccessSpecifier(), RD); | |||
2102 | llvm::DIType *DTy = DBuilder.createInheritance(RecordTy, BaseTy, BaseOffset, | |||
2103 | VBPtrOffset, BFlags); | |||
2104 | EltTys.push_back(DTy); | |||
2105 | } | |||
2106 | } | |||
2107 | ||||
2108 | llvm::DINodeArray | |||
2109 | CGDebugInfo::CollectTemplateParams(std::optional<TemplateArgs> OArgs, | |||
2110 | llvm::DIFile *Unit) { | |||
2111 | if (!OArgs) | |||
2112 | return llvm::DINodeArray(); | |||
2113 | TemplateArgs &Args = *OArgs; | |||
2114 | SmallVector<llvm::Metadata *, 16> TemplateParams; | |||
2115 | for (unsigned i = 0, e = Args.Args.size(); i != e; ++i) { | |||
2116 | const TemplateArgument &TA = Args.Args[i]; | |||
2117 | StringRef Name; | |||
2118 | const bool defaultParameter = TA.getIsDefaulted(); | |||
2119 | if (Args.TList) | |||
2120 | Name = Args.TList->getParam(i)->getName(); | |||
2121 | ||||
2122 | switch (TA.getKind()) { | |||
2123 | case TemplateArgument::Type: { | |||
2124 | llvm::DIType *TTy = getOrCreateType(TA.getAsType(), Unit); | |||
2125 | TemplateParams.push_back(DBuilder.createTemplateTypeParameter( | |||
2126 | TheCU, Name, TTy, defaultParameter)); | |||
2127 | ||||
2128 | } break; | |||
2129 | case TemplateArgument::Integral: { | |||
2130 | llvm::DIType *TTy = getOrCreateType(TA.getIntegralType(), Unit); | |||
2131 | TemplateParams.push_back(DBuilder.createTemplateValueParameter( | |||
2132 | TheCU, Name, TTy, defaultParameter, | |||
2133 | llvm::ConstantInt::get(CGM.getLLVMContext(), TA.getAsIntegral()))); | |||
2134 | } break; | |||
2135 | case TemplateArgument::Declaration: { | |||
2136 | const ValueDecl *D = TA.getAsDecl(); | |||
2137 | QualType T = TA.getParamTypeForDecl().getDesugaredType(CGM.getContext()); | |||
2138 | llvm::DIType *TTy = getOrCreateType(T, Unit); | |||
2139 | llvm::Constant *V = nullptr; | |||
2140 | // Skip retrieve the value if that template parameter has cuda device | |||
2141 | // attribute, i.e. that value is not available at the host side. | |||
2142 | if (!CGM.getLangOpts().CUDA || CGM.getLangOpts().CUDAIsDevice || | |||
2143 | !D->hasAttr<CUDADeviceAttr>()) { | |||
2144 | const CXXMethodDecl *MD; | |||
2145 | // Variable pointer template parameters have a value that is the address | |||
2146 | // of the variable. | |||
2147 | if (const auto *VD = dyn_cast<VarDecl>(D)) | |||
2148 | V = CGM.GetAddrOfGlobalVar(VD); | |||
2149 | // Member function pointers have special support for building them, | |||
2150 | // though this is currently unsupported in LLVM CodeGen. | |||
2151 | else if ((MD = dyn_cast<CXXMethodDecl>(D)) && MD->isInstance()) | |||
2152 | V = CGM.getCXXABI().EmitMemberFunctionPointer(MD); | |||
2153 | else if (const auto *FD = dyn_cast<FunctionDecl>(D)) | |||
2154 | V = CGM.GetAddrOfFunction(FD); | |||
2155 | // Member data pointers have special handling too to compute the fixed | |||
2156 | // offset within the object. | |||
2157 | else if (const auto *MPT = | |||
2158 | dyn_cast<MemberPointerType>(T.getTypePtr())) { | |||
2159 | // These five lines (& possibly the above member function pointer | |||
2160 | // handling) might be able to be refactored to use similar code in | |||
2161 | // CodeGenModule::getMemberPointerConstant | |||
2162 | uint64_t fieldOffset = CGM.getContext().getFieldOffset(D); | |||
2163 | CharUnits chars = | |||
2164 | CGM.getContext().toCharUnitsFromBits((int64_t)fieldOffset); | |||
2165 | V = CGM.getCXXABI().EmitMemberDataPointer(MPT, chars); | |||
2166 | } else if (const auto *GD = dyn_cast<MSGuidDecl>(D)) { | |||
2167 | V = CGM.GetAddrOfMSGuidDecl(GD).getPointer(); | |||
2168 | } else if (const auto *TPO = dyn_cast<TemplateParamObjectDecl>(D)) { | |||
2169 | if (T->isRecordType()) | |||
2170 | V = ConstantEmitter(CGM).emitAbstract( | |||
2171 | SourceLocation(), TPO->getValue(), TPO->getType()); | |||
2172 | else | |||
2173 | V = CGM.GetAddrOfTemplateParamObject(TPO).getPointer(); | |||
2174 | } | |||
2175 | assert(V && "Failed to find template parameter pointer")(static_cast <bool> (V && "Failed to find template parameter pointer" ) ? void (0) : __assert_fail ("V && \"Failed to find template parameter pointer\"" , "clang/lib/CodeGen/CGDebugInfo.cpp", 2175, __extension__ __PRETTY_FUNCTION__ )); | |||
2176 | V = V->stripPointerCasts(); | |||
2177 | } | |||
2178 | TemplateParams.push_back(DBuilder.createTemplateValueParameter( | |||
2179 | TheCU, Name, TTy, defaultParameter, cast_or_null<llvm::Constant>(V))); | |||
2180 | } break; | |||
2181 | case TemplateArgument::NullPtr: { | |||
2182 | QualType T = TA.getNullPtrType(); | |||
2183 | llvm::DIType *TTy = getOrCreateType(T, Unit); | |||
2184 | llvm::Constant *V = nullptr; | |||
2185 | // Special case member data pointer null values since they're actually -1 | |||
2186 | // instead of zero. | |||
2187 | if (const auto *MPT = dyn_cast<MemberPointerType>(T.getTypePtr())) | |||
2188 | // But treat member function pointers as simple zero integers because | |||
2189 | // it's easier than having a special case in LLVM's CodeGen. If LLVM | |||
2190 | // CodeGen grows handling for values of non-null member function | |||
2191 | // pointers then perhaps we could remove this special case and rely on | |||
2192 | // EmitNullMemberPointer for member function pointers. | |||
2193 | if (MPT->isMemberDataPointer()) | |||
2194 | V = CGM.getCXXABI().EmitNullMemberPointer(MPT); | |||
2195 | if (!V) | |||
2196 | V = llvm::ConstantInt::get(CGM.Int8Ty, 0); | |||
2197 | TemplateParams.push_back(DBuilder.createTemplateValueParameter( | |||
2198 | TheCU, Name, TTy, defaultParameter, V)); | |||
2199 | } break; | |||
2200 | case TemplateArgument::Template: { | |||
2201 | std::string QualName; | |||
2202 | llvm::raw_string_ostream OS(QualName); | |||
2203 | TA.getAsTemplate().getAsTemplateDecl()->printQualifiedName( | |||
2204 | OS, getPrintingPolicy()); | |||
2205 | TemplateParams.push_back(DBuilder.createTemplateTemplateParameter( | |||
2206 | TheCU, Name, nullptr, OS.str(), defaultParameter)); | |||
2207 | break; | |||
2208 | } | |||
2209 | case TemplateArgument::Pack: | |||
2210 | TemplateParams.push_back(DBuilder.createTemplateParameterPack( | |||
2211 | TheCU, Name, nullptr, | |||
2212 | CollectTemplateParams({{nullptr, TA.getPackAsArray()}}, Unit))); | |||
2213 | break; | |||
2214 | case TemplateArgument::Expression: { | |||
2215 | const Expr *E = TA.getAsExpr(); | |||
2216 | QualType T = E->getType(); | |||
2217 | if (E->isGLValue()) | |||
2218 | T = CGM.getContext().getLValueReferenceType(T); | |||
2219 | llvm::Constant *V = ConstantEmitter(CGM).emitAbstract(E, T); | |||
2220 | assert(V && "Expression in template argument isn't constant")(static_cast <bool> (V && "Expression in template argument isn't constant" ) ? void (0) : __assert_fail ("V && \"Expression in template argument isn't constant\"" , "clang/lib/CodeGen/CGDebugInfo.cpp", 2220, __extension__ __PRETTY_FUNCTION__ )); | |||
2221 | llvm::DIType *TTy = getOrCreateType(T, Unit); | |||
2222 | TemplateParams.push_back(DBuilder.createTemplateValueParameter( | |||
2223 | TheCU, Name, TTy, defaultParameter, V->stripPointerCasts())); | |||
2224 | } break; | |||
2225 | // And the following should never occur: | |||
2226 | case TemplateArgument::TemplateExpansion: | |||
2227 | case TemplateArgument::Null: | |||
2228 | llvm_unreachable(::llvm::llvm_unreachable_internal("These argument types shouldn't exist in concrete types" , "clang/lib/CodeGen/CGDebugInfo.cpp", 2229) | |||
2229 | "These argument types shouldn't exist in concrete types")::llvm::llvm_unreachable_internal("These argument types shouldn't exist in concrete types" , "clang/lib/CodeGen/CGDebugInfo.cpp", 2229); | |||
2230 | } | |||
2231 | } | |||
2232 | return DBuilder.getOrCreateArray(TemplateParams); | |||
2233 | } | |||
2234 | ||||
2235 | std::optional<CGDebugInfo::TemplateArgs> | |||
2236 | CGDebugInfo::GetTemplateArgs(const FunctionDecl *FD) const { | |||
2237 | if (FD->getTemplatedKind() == | |||
2238 | FunctionDecl::TK_FunctionTemplateSpecialization) { | |||
2239 | const TemplateParameterList *TList = FD->getTemplateSpecializationInfo() | |||
2240 | ->getTemplate() | |||
2241 | ->getTemplateParameters(); | |||
2242 | return {{TList, FD->getTemplateSpecializationArgs()->asArray()}}; | |||
2243 | } | |||
2244 | return std::nullopt; | |||
2245 | } | |||
2246 | std::optional<CGDebugInfo::TemplateArgs> | |||
2247 | CGDebugInfo::GetTemplateArgs(const VarDecl *VD) const { | |||
2248 | // Always get the full list of parameters, not just the ones from the | |||
2249 | // specialization. A partial specialization may have fewer parameters than | |||
2250 | // there are arguments. | |||
2251 | auto *TS = dyn_cast<VarTemplateSpecializationDecl>(VD); | |||
2252 | if (!TS) | |||
2253 | return std::nullopt; | |||
2254 | VarTemplateDecl *T = TS->getSpecializedTemplate(); | |||
2255 | const TemplateParameterList *TList = T->getTemplateParameters(); | |||
2256 | auto TA = TS->getTemplateArgs().asArray(); | |||
2257 | return {{TList, TA}}; | |||
2258 | } | |||
2259 | std::optional<CGDebugInfo::TemplateArgs> | |||
2260 | CGDebugInfo::GetTemplateArgs(const RecordDecl *RD) const { | |||
2261 | if (auto *TSpecial = dyn_cast<ClassTemplateSpecializationDecl>(RD)) { | |||
2262 | // Always get the full list of parameters, not just the ones from the | |||
2263 | // specialization. A partial specialization may have fewer parameters than | |||
2264 | // there are arguments. | |||
2265 | TemplateParameterList *TPList = | |||
2266 | TSpecial->getSpecializedTemplate()->getTemplateParameters(); | |||
2267 | const TemplateArgumentList &TAList = TSpecial->getTemplateArgs(); | |||
2268 | return {{TPList, TAList.asArray()}}; | |||
2269 | } | |||
2270 | return std::nullopt; | |||
2271 | } | |||
2272 | ||||
2273 | llvm::DINodeArray | |||
2274 | CGDebugInfo::CollectFunctionTemplateParams(const FunctionDecl *FD, | |||
2275 | llvm::DIFile *Unit) { | |||
2276 | return CollectTemplateParams(GetTemplateArgs(FD), Unit); | |||
2277 | } | |||
2278 | ||||
2279 | llvm::DINodeArray CGDebugInfo::CollectVarTemplateParams(const VarDecl *VL, | |||
2280 | llvm::DIFile *Unit) { | |||
2281 | return CollectTemplateParams(GetTemplateArgs(VL), Unit); | |||
2282 | } | |||
2283 | ||||
2284 | llvm::DINodeArray CGDebugInfo::CollectCXXTemplateParams(const RecordDecl *RD, | |||
2285 | llvm::DIFile *Unit) { | |||
2286 | return CollectTemplateParams(GetTemplateArgs(RD), Unit); | |||
2287 | } | |||
2288 | ||||
2289 | llvm::DINodeArray CGDebugInfo::CollectBTFDeclTagAnnotations(const Decl *D) { | |||
2290 | if (!D->hasAttr<BTFDeclTagAttr>()) | |||
2291 | return nullptr; | |||
2292 | ||||
2293 | SmallVector<llvm::Metadata *, 4> Annotations; | |||
2294 | for (const auto *I : D->specific_attrs<BTFDeclTagAttr>()) { | |||
2295 | llvm::Metadata *Ops[2] = { | |||
2296 | llvm::MDString::get(CGM.getLLVMContext(), StringRef("btf_decl_tag")), | |||
2297 | llvm::MDString::get(CGM.getLLVMContext(), I->getBTFDeclTag())}; | |||
2298 | Annotations.push_back(llvm::MDNode::get(CGM.getLLVMContext(), Ops)); | |||
2299 | } | |||
2300 | return DBuilder.getOrCreateArray(Annotations); | |||
2301 | } | |||
2302 | ||||
2303 | llvm::DIType *CGDebugInfo::getOrCreateVTablePtrType(llvm::DIFile *Unit) { | |||
2304 | if (VTablePtrType) | |||
2305 | return VTablePtrType; | |||
2306 | ||||
2307 | ASTContext &Context = CGM.getContext(); | |||
2308 | ||||
2309 | /* Function type */ | |||
2310 | llvm::Metadata *STy = getOrCreateType(Context.IntTy, Unit); | |||
2311 | llvm::DITypeRefArray SElements = DBuilder.getOrCreateTypeArray(STy); | |||
2312 | llvm::DIType *SubTy = DBuilder.createSubroutineType(SElements); | |||
2313 | unsigned Size = Context.getTypeSize(Context.VoidPtrTy); | |||
2314 | unsigned VtblPtrAddressSpace = CGM.getTarget().getVtblPtrAddressSpace(); | |||
2315 | std::optional<unsigned> DWARFAddressSpace = | |||
2316 | CGM.getTarget().getDWARFAddressSpace(VtblPtrAddressSpace); | |||
2317 | ||||
2318 | llvm::DIType *vtbl_ptr_type = DBuilder.createPointerType( | |||
2319 | SubTy, Size, 0, DWARFAddressSpace, "__vtbl_ptr_type"); | |||
2320 | VTablePtrType = DBuilder.createPointerType(vtbl_ptr_type, Size); | |||
2321 | return VTablePtrType; | |||
2322 | } | |||
2323 | ||||
2324 | StringRef CGDebugInfo::getVTableName(const CXXRecordDecl *RD) { | |||
2325 | // Copy the gdb compatible name on the side and use its reference. | |||
2326 | return internString("_vptr$", RD->getNameAsString()); | |||
2327 | } | |||
2328 | ||||
2329 | StringRef CGDebugInfo::getDynamicInitializerName(const VarDecl *VD, | |||
2330 | DynamicInitKind StubKind, | |||
2331 | llvm::Function *InitFn) { | |||
2332 | // If we're not emitting codeview, use the mangled name. For Itanium, this is | |||
2333 | // arbitrary. | |||
2334 | if (!CGM.getCodeGenOpts().EmitCodeView || | |||
2335 | StubKind == DynamicInitKind::GlobalArrayDestructor) | |||
2336 | return InitFn->getName(); | |||
2337 | ||||
2338 | // Print the normal qualified name for the variable, then break off the last | |||
2339 | // NNS, and add the appropriate other text. Clang always prints the global | |||
2340 | // variable name without template arguments, so we can use rsplit("::") and | |||
2341 | // then recombine the pieces. | |||
2342 | SmallString<128> QualifiedGV; | |||
2343 | StringRef Quals; | |||
2344 | StringRef GVName; | |||
2345 | { | |||
2346 | llvm::raw_svector_ostream OS(QualifiedGV); | |||
2347 | VD->printQualifiedName(OS, getPrintingPolicy()); | |||
2348 | std::tie(Quals, GVName) = OS.str().rsplit("::"); | |||
2349 | if (GVName.empty()) | |||
2350 | std::swap(Quals, GVName); | |||
2351 | } | |||
2352 | ||||
2353 | SmallString<128> InitName; | |||
2354 | llvm::raw_svector_ostream OS(InitName); | |||
2355 | if (!Quals.empty()) | |||
2356 | OS << Quals << "::"; | |||
2357 | ||||
2358 | switch (StubKind) { | |||
2359 | case DynamicInitKind::NoStub: | |||
2360 | case DynamicInitKind::GlobalArrayDestructor: | |||
2361 | llvm_unreachable("not an initializer")::llvm::llvm_unreachable_internal("not an initializer", "clang/lib/CodeGen/CGDebugInfo.cpp" , 2361); | |||
2362 | case DynamicInitKind::Initializer: | |||
2363 | OS << "`dynamic initializer for '"; | |||
2364 | break; | |||
2365 | case DynamicInitKind::AtExit: | |||
2366 | OS << "`dynamic atexit destructor for '"; | |||
2367 | break; | |||
2368 | } | |||
2369 | ||||
2370 | OS << GVName; | |||
2371 | ||||
2372 | // Add any template specialization args. | |||
2373 | if (const auto *VTpl = dyn_cast<VarTemplateSpecializationDecl>(VD)) { | |||
2374 | printTemplateArgumentList(OS, VTpl->getTemplateArgs().asArray(), | |||
2375 | getPrintingPolicy()); | |||
2376 | } | |||
2377 | ||||
2378 | OS << '\''; | |||
2379 | ||||
2380 | return internString(OS.str()); | |||
2381 | } | |||
2382 | ||||
2383 | void CGDebugInfo::CollectVTableInfo(const CXXRecordDecl *RD, llvm::DIFile *Unit, | |||
2384 | SmallVectorImpl<llvm::Metadata *> &EltTys) { | |||
2385 | // If this class is not dynamic then there is not any vtable info to collect. | |||
2386 | if (!RD->isDynamicClass()) | |||
2387 | return; | |||
2388 | ||||
2389 | // Don't emit any vtable shape or vptr info if this class doesn't have an | |||
2390 | // extendable vfptr. This can happen if the class doesn't have virtual | |||
2391 | // methods, or in the MS ABI if those virtual methods only come from virtually | |||
2392 | // inherited bases. | |||
2393 | const ASTRecordLayout &RL = CGM.getContext().getASTRecordLayout(RD); | |||
2394 | if (!RL.hasExtendableVFPtr()) | |||
2395 | return; | |||
2396 | ||||
2397 | // CodeView needs to know how large the vtable of every dynamic class is, so | |||
2398 | // emit a special named pointer type into the element list. The vptr type | |||
2399 | // points to this type as well. | |||
2400 | llvm::DIType *VPtrTy = nullptr; | |||
2401 | bool NeedVTableShape = CGM.getCodeGenOpts().EmitCodeView && | |||
2402 | CGM.getTarget().getCXXABI().isMicrosoft(); | |||
2403 | if (NeedVTableShape) { | |||
2404 | uint64_t PtrWidth = | |||
2405 | CGM.getContext().getTypeSize(CGM.getContext().VoidPtrTy); | |||
2406 | const VTableLayout &VFTLayout = | |||
2407 | CGM.getMicrosoftVTableContext().getVFTableLayout(RD, CharUnits::Zero()); | |||
2408 | unsigned VSlotCount = | |||
2409 | VFTLayout.vtable_components().size() - CGM.getLangOpts().RTTIData; | |||
2410 | unsigned VTableWidth = PtrWidth * VSlotCount; | |||
2411 | unsigned VtblPtrAddressSpace = CGM.getTarget().getVtblPtrAddressSpace(); | |||
2412 | std::optional<unsigned> DWARFAddressSpace = | |||
2413 | CGM.getTarget().getDWARFAddressSpace(VtblPtrAddressSpace); | |||
2414 | ||||
2415 | // Create a very wide void* type and insert it directly in the element list. | |||
2416 | llvm::DIType *VTableType = DBuilder.createPointerType( | |||
2417 | nullptr, VTableWidth, 0, DWARFAddressSpace, "__vtbl_ptr_type"); | |||
2418 | EltTys.push_back(VTableType); | |||
2419 | ||||
2420 | // The vptr is a pointer to this special vtable type. | |||
2421 | VPtrTy = DBuilder.createPointerType(VTableType, PtrWidth); | |||
2422 | } | |||
2423 | ||||
2424 | // If there is a primary base then the artificial vptr member lives there. | |||
2425 | if (RL.getPrimaryBase()) | |||
2426 | return; | |||
2427 | ||||
2428 | if (!VPtrTy) | |||
2429 | VPtrTy = getOrCreateVTablePtrType(Unit); | |||
2430 | ||||
2431 | unsigned Size = CGM.getContext().getTypeSize(CGM.getContext().VoidPtrTy); | |||
2432 | llvm::DIType *VPtrMember = | |||
2433 | DBuilder.createMemberType(Unit, getVTableName(RD), Unit, 0, Size, 0, 0, | |||
2434 | llvm::DINode::FlagArtificial, VPtrTy); | |||
2435 | EltTys.push_back(VPtrMember); | |||
2436 | } | |||
2437 | ||||
2438 | llvm::DIType *CGDebugInfo::getOrCreateRecordType(QualType RTy, | |||
2439 | SourceLocation Loc) { | |||
2440 | assert(CGM.getCodeGenOpts().hasReducedDebugInfo())(static_cast <bool> (CGM.getCodeGenOpts().hasReducedDebugInfo ()) ? void (0) : __assert_fail ("CGM.getCodeGenOpts().hasReducedDebugInfo()" , "clang/lib/CodeGen/CGDebugInfo.cpp", 2440, __extension__ __PRETTY_FUNCTION__ )); | |||
2441 | llvm::DIType *T = getOrCreateType(RTy, getOrCreateFile(Loc)); | |||
2442 | return T; | |||
2443 | } | |||
2444 | ||||
2445 | llvm::DIType *CGDebugInfo::getOrCreateInterfaceType(QualType D, | |||
2446 | SourceLocation Loc) { | |||
2447 | return getOrCreateStandaloneType(D, Loc); | |||
2448 | } | |||
2449 | ||||
2450 | llvm::DIType *CGDebugInfo::getOrCreateStandaloneType(QualType D, | |||
2451 | SourceLocation Loc) { | |||
2452 | assert(CGM.getCodeGenOpts().hasReducedDebugInfo())(static_cast <bool> (CGM.getCodeGenOpts().hasReducedDebugInfo ()) ? void (0) : __assert_fail ("CGM.getCodeGenOpts().hasReducedDebugInfo()" , "clang/lib/CodeGen/CGDebugInfo.cpp", 2452, __extension__ __PRETTY_FUNCTION__ )); | |||
2453 | assert(!D.isNull() && "null type")(static_cast <bool> (!D.isNull() && "null type" ) ? void (0) : __assert_fail ("!D.isNull() && \"null type\"" , "clang/lib/CodeGen/CGDebugInfo.cpp", 2453, __extension__ __PRETTY_FUNCTION__ )); | |||
2454 | llvm::DIType *T = getOrCreateType(D, getOrCreateFile(Loc)); | |||
2455 | assert(T && "could not create debug info for type")(static_cast <bool> (T && "could not create debug info for type" ) ? void (0) : __assert_fail ("T && \"could not create debug info for type\"" , "clang/lib/CodeGen/CGDebugInfo.cpp", 2455, __extension__ __PRETTY_FUNCTION__ )); | |||
2456 | ||||
2457 | RetainedTypes.push_back(D.getAsOpaquePtr()); | |||
2458 | return T; | |||
2459 | } | |||
2460 | ||||
2461 | void CGDebugInfo::addHeapAllocSiteMetadata(llvm::CallBase *CI, | |||
2462 | QualType AllocatedTy, | |||
2463 | SourceLocation Loc) { | |||
2464 | if (CGM.getCodeGenOpts().getDebugInfo() <= | |||
2465 | llvm::codegenoptions::DebugLineTablesOnly) | |||
2466 | return; | |||
2467 | llvm::MDNode *node; | |||
2468 | if (AllocatedTy->isVoidType()) | |||
2469 | node = llvm::MDNode::get(CGM.getLLVMContext(), std::nullopt); | |||
2470 | else | |||
2471 | node = getOrCreateType(AllocatedTy, getOrCreateFile(Loc)); | |||
2472 | ||||
2473 | CI->setMetadata("heapallocsite", node); | |||
2474 | } | |||
2475 | ||||
2476 | void CGDebugInfo::completeType(const EnumDecl *ED) { | |||
2477 | if (DebugKind <= llvm::codegenoptions::DebugLineTablesOnly) | |||
2478 | return; | |||
2479 | QualType Ty = CGM.getContext().getEnumType(ED); | |||
2480 | void *TyPtr = Ty.getAsOpaquePtr(); | |||
2481 | auto I = TypeCache.find(TyPtr); | |||
2482 | if (I == TypeCache.end() || !cast<llvm::DIType>(I->second)->isForwardDecl()) | |||
2483 | return; | |||
2484 | llvm::DIType *Res = CreateTypeDefinition(Ty->castAs<EnumType>()); | |||
2485 | assert(!Res->isForwardDecl())(static_cast <bool> (!Res->isForwardDecl()) ? void ( 0) : __assert_fail ("!Res->isForwardDecl()", "clang/lib/CodeGen/CGDebugInfo.cpp" , 2485, __extension__ __PRETTY_FUNCTION__)); | |||
2486 | TypeCache[TyPtr].reset(Res); | |||
2487 | } | |||
2488 | ||||
2489 | void CGDebugInfo::completeType(const RecordDecl *RD) { | |||
2490 | if (DebugKind > llvm::codegenoptions::LimitedDebugInfo || | |||
2491 | !CGM.getLangOpts().CPlusPlus) | |||
2492 | completeRequiredType(RD); | |||
2493 | } | |||
2494 | ||||
2495 | /// Return true if the class or any of its methods are marked dllimport. | |||
2496 | static bool isClassOrMethodDLLImport(const CXXRecordDecl *RD) { | |||
2497 | if (RD->hasAttr<DLLImportAttr>()) | |||
2498 | return true; | |||
2499 | for (const CXXMethodDecl *MD : RD->methods()) | |||
2500 | if (MD->hasAttr<DLLImportAttr>()) | |||
2501 | return true; | |||
2502 | return false; | |||
2503 | } | |||
2504 | ||||
2505 | /// Does a type definition exist in an imported clang module? | |||
2506 | static bool isDefinedInClangModule(const RecordDecl *RD) { | |||
2507 | // Only definitions that where imported from an AST file come from a module. | |||
2508 | if (!RD || !RD->isFromASTFile()) | |||
2509 | return false; | |||
2510 | // Anonymous entities cannot be addressed. Treat them as not from module. | |||
2511 | if (!RD->isExternallyVisible() && RD->getName().empty()) | |||
2512 | return false; | |||
2513 | if (auto *CXXDecl = dyn_cast<CXXRecordDecl>(RD)) { | |||
2514 | if (!CXXDecl->isCompleteDefinition()) | |||
2515 | return false; | |||
2516 | // Check wether RD is a template. | |||
2517 | auto TemplateKind = CXXDecl->getTemplateSpecializationKind(); | |||
2518 | if (TemplateKind != TSK_Undeclared) { | |||
2519 | // Unfortunately getOwningModule() isn't accurate enough to find the | |||
2520 | // owning module of a ClassTemplateSpecializationDecl that is inside a | |||
2521 | // namespace spanning multiple modules. | |||
2522 | bool Explicit = false; | |||
2523 | if (auto *TD = dyn_cast<ClassTemplateSpecializationDecl>(CXXDecl)) | |||
2524 | Explicit = TD->isExplicitInstantiationOrSpecialization(); | |||
2525 | if (!Explicit && CXXDecl->getEnclosingNamespaceContext()) | |||
2526 | return false; | |||
2527 | // This is a template, check the origin of the first member. | |||
2528 | if (CXXDecl->field_begin() == CXXDecl->field_end()) | |||
2529 | return TemplateKind == TSK_ExplicitInstantiationDeclaration; | |||
2530 | if (!CXXDecl->field_begin()->isFromASTFile()) | |||
2531 | return false; | |||
2532 | } | |||
2533 | } | |||
2534 | return true; | |||
2535 | } | |||
2536 | ||||
2537 | void CGDebugInfo::completeClassData(const RecordDecl *RD) { | |||
2538 | if (auto *CXXRD = dyn_cast<CXXRecordDecl>(RD)) | |||
2539 | if (CXXRD->isDynamicClass() && | |||
2540 | CGM.getVTableLinkage(CXXRD) == | |||
2541 | llvm::GlobalValue::AvailableExternallyLinkage && | |||
2542 | !isClassOrMethodDLLImport(CXXRD)) | |||
2543 | return; | |||
2544 | ||||
2545 | if (DebugTypeExtRefs && isDefinedInClangModule(RD->getDefinition())) | |||
2546 | return; | |||
2547 | ||||
2548 | completeClass(RD); | |||
2549 | } | |||
2550 | ||||
2551 | void CGDebugInfo::completeClass(const RecordDecl *RD) { | |||
2552 | if (DebugKind <= llvm::codegenoptions::DebugLineTablesOnly) | |||
2553 | return; | |||
2554 | QualType Ty = CGM.getContext().getRecordType(RD); | |||
2555 | void *TyPtr = Ty.getAsOpaquePtr(); | |||
2556 | auto I = TypeCache.find(TyPtr); | |||
2557 | if (I != TypeCache.end() && !cast<llvm::DIType>(I->second)->isForwardDecl()) | |||
2558 | return; | |||
2559 | llvm::DIType *Res = CreateTypeDefinition(Ty->castAs<RecordType>()); | |||
2560 | assert(!Res->isForwardDecl())(static_cast <bool> (!Res->isForwardDecl()) ? void ( 0) : __assert_fail ("!Res->isForwardDecl()", "clang/lib/CodeGen/CGDebugInfo.cpp" , 2560, __extension__ __PRETTY_FUNCTION__)); | |||
2561 | TypeCache[TyPtr].reset(Res); | |||
2562 | } | |||
2563 | ||||
2564 | static bool hasExplicitMemberDefinition(CXXRecordDecl::method_iterator I, | |||
2565 | CXXRecordDecl::method_iterator End) { | |||
2566 | for (CXXMethodDecl *MD : llvm::make_range(I, End)) | |||
2567 | if (FunctionDecl *Tmpl = MD->getInstantiatedFromMemberFunction()) | |||
2568 | if (!Tmpl->isImplicit() && Tmpl->isThisDeclarationADefinition() && | |||
2569 | !MD->getMemberSpecializationInfo()->isExplicitSpecialization()) | |||
2570 | return true; | |||
2571 | return false; | |||
2572 | } | |||
2573 | ||||
2574 | static bool canUseCtorHoming(const CXXRecordDecl *RD) { | |||
2575 | // Constructor homing can be used for classes that cannnot be constructed | |||
2576 | // without emitting code for one of their constructors. This is classes that | |||
2577 | // don't have trivial or constexpr constructors, or can be created from | |||
2578 | // aggregate initialization. Also skip lambda objects because they don't call | |||
2579 | // constructors. | |||
2580 | ||||
2581 | // Skip this optimization if the class or any of its methods are marked | |||
2582 | // dllimport. | |||
2583 | if (isClassOrMethodDLLImport(RD)) | |||
2584 | return false; | |||
2585 | ||||
2586 | if (RD->isLambda() || RD->isAggregate() || | |||
2587 | RD->hasTrivialDefaultConstructor() || | |||
2588 | RD->hasConstexprNonCopyMoveConstructor()) | |||
2589 | return false; | |||
2590 | ||||
2591 | for (const CXXConstructorDecl *Ctor : RD->ctors()) { | |||
2592 | if (Ctor->isCopyOrMoveConstructor()) | |||
2593 | continue; | |||
2594 | if (!Ctor->isDeleted()) | |||
2595 | return true; | |||
2596 | } | |||
2597 | return false; | |||
2598 | } | |||
2599 | ||||
2600 | static bool shouldOmitDefinition(llvm::codegenoptions::DebugInfoKind DebugKind, | |||
2601 | bool DebugTypeExtRefs, const RecordDecl *RD, | |||
2602 | const LangOptions &LangOpts) { | |||
2603 | if (DebugTypeExtRefs && isDefinedInClangModule(RD->getDefinition())) | |||
2604 | return true; | |||
2605 | ||||
2606 | if (auto *ES = RD->getASTContext().getExternalSource()) | |||
2607 | if (ES->hasExternalDefinitions(RD) == ExternalASTSource::EK_Always) | |||
2608 | return true; | |||
2609 | ||||
2610 | // Only emit forward declarations in line tables only to keep debug info size | |||
2611 | // small. This only applies to CodeView, since we don't emit types in DWARF | |||
2612 | // line tables only. | |||
2613 | if (DebugKind == llvm::codegenoptions::DebugLineTablesOnly) | |||
2614 | return true; | |||
2615 | ||||
2616 | if (DebugKind > llvm::codegenoptions::LimitedDebugInfo || | |||
2617 | RD->hasAttr<StandaloneDebugAttr>()) | |||
2618 | return false; | |||
2619 | ||||
2620 | if (!LangOpts.CPlusPlus) | |||
2621 | return false; | |||
2622 | ||||
2623 | if (!RD->isCompleteDefinitionRequired()) | |||
2624 | return true; | |||
2625 | ||||
2626 | const auto *CXXDecl = dyn_cast<CXXRecordDecl>(RD); | |||
2627 | ||||
2628 | if (!CXXDecl) | |||
2629 | return false; | |||
2630 | ||||
2631 | // Only emit complete debug info for a dynamic class when its vtable is | |||
2632 | // emitted. However, Microsoft debuggers don't resolve type information | |||
2633 | // across DLL boundaries, so skip this optimization if the class or any of its | |||
2634 | // methods are marked dllimport. This isn't a complete solution, since objects | |||
2635 | // without any dllimport methods can be used in one DLL and constructed in | |||
2636 | // another, but it is the current behavior of LimitedDebugInfo. | |||
2637 | if (CXXDecl->hasDefinition() && CXXDecl->isDynamicClass() && | |||
2638 | !isClassOrMethodDLLImport(CXXDecl)) | |||
2639 | return true; | |||
2640 | ||||
2641 | TemplateSpecializationKind Spec = TSK_Undeclared; | |||
2642 | if (const auto *SD = dyn_cast<ClassTemplateSpecializationDecl>(RD)) | |||
2643 | Spec = SD->getSpecializationKind(); | |||
2644 | ||||
2645 | if (Spec == TSK_ExplicitInstantiationDeclaration && | |||
2646 | hasExplicitMemberDefinition(CXXDecl->method_begin(), | |||
2647 | CXXDecl->method_end())) | |||
2648 | return true; | |||
2649 | ||||
2650 | // In constructor homing mode, only emit complete debug info for a class | |||
2651 | // when its constructor is emitted. | |||
2652 | if ((DebugKind == llvm::codegenoptions::DebugInfoConstructor) && | |||
2653 | canUseCtorHoming(CXXDecl)) | |||
2654 | return true; | |||
2655 | ||||
2656 | return false; | |||
2657 | } | |||
2658 | ||||
2659 | void CGDebugInfo::completeRequiredType(const RecordDecl *RD) { | |||
2660 | if (shouldOmitDefinition(DebugKind, DebugTypeExtRefs, RD, CGM.getLangOpts())) | |||
2661 | return; | |||
2662 | ||||
2663 | QualType Ty = CGM.getContext().getRecordType(RD); | |||
2664 | llvm::DIType *T = getTypeOrNull(Ty); | |||
2665 | if (T && T->isForwardDecl()) | |||
2666 | completeClassData(RD); | |||
2667 | } | |||
2668 | ||||
2669 | llvm::DIType *CGDebugInfo::CreateType(const RecordType *Ty) { | |||
2670 | RecordDecl *RD = Ty->getDecl(); | |||
2671 | llvm::DIType *T = cast_or_null<llvm::DIType>(getTypeOrNull(QualType(Ty, 0))); | |||
2672 | if (T || shouldOmitDefinition(DebugKind, DebugTypeExtRefs, RD, | |||
2673 | CGM.getLangOpts())) { | |||
2674 | if (!T) | |||
2675 | T = getOrCreateRecordFwdDecl(Ty, getDeclContextDescriptor(RD)); | |||
2676 | return T; | |||
2677 | } | |||
2678 | ||||
2679 | return CreateTypeDefinition(Ty); | |||
2680 | } | |||
2681 | ||||
2682 | llvm::DIType *CGDebugInfo::CreateTypeDefinition(const RecordType *Ty) { | |||
2683 | RecordDecl *RD = Ty->getDecl(); | |||
2684 | ||||
2685 | // Get overall information about the record type for the debug info. | |||
2686 | llvm::DIFile *DefUnit = getOrCreateFile(RD->getLocation()); | |||
2687 | ||||
2688 | // Records and classes and unions can all be recursive. To handle them, we | |||
2689 | // first generate a debug descriptor for the struct as a forward declaration. | |||
2690 | // Then (if it is a definition) we go through and get debug info for all of | |||
2691 | // its members. Finally, we create a descriptor for the complete type (which | |||
2692 | // may refer to the forward decl if the struct is recursive) and replace all | |||
2693 | // uses of the forward declaration with the final definition. | |||
2694 | llvm::DICompositeType *FwdDecl = getOrCreateLimitedType(Ty); | |||
2695 | ||||
2696 | const RecordDecl *D = RD->getDefinition(); | |||
2697 | if (!D || !D->isCompleteDefinition()) | |||
2698 | return FwdDecl; | |||
2699 | ||||
2700 | if (const auto *CXXDecl = dyn_cast<CXXRecordDecl>(RD)) | |||
2701 | CollectContainingType(CXXDecl, FwdDecl); | |||
2702 | ||||
2703 | // Push the struct on region stack. | |||
2704 | LexicalBlockStack.emplace_back(&*FwdDecl); | |||
2705 | RegionMap[Ty->getDecl()].reset(FwdDecl); | |||
2706 | ||||
2707 | // Convert all the elements. | |||
2708 | SmallVector<llvm::Metadata *, 16> EltTys; | |||
2709 | // what about nested types? | |||
2710 | ||||
2711 | // Note: The split of CXXDecl information here is intentional, the | |||
2712 | // gdb tests will depend on a certain ordering at printout. The debug | |||
2713 | // information offsets are still correct if we merge them all together | |||
2714 | // though. | |||
2715 | const auto *CXXDecl = dyn_cast<CXXRecordDecl>(RD); | |||
2716 | if (CXXDecl) { | |||
2717 | CollectCXXBases(CXXDecl, DefUnit, EltTys, FwdDecl); | |||
2718 | CollectVTableInfo(CXXDecl, DefUnit, EltTys); | |||
2719 | } | |||
2720 | ||||
2721 | // Collect data fields (including static variables and any initializers). | |||
2722 | CollectRecordFields(RD, DefUnit, EltTys, FwdDecl); | |||
2723 | if (CXXDecl) | |||
2724 | CollectCXXMemberFunctions(CXXDecl, DefUnit, EltTys, FwdDecl); | |||
2725 | ||||
2726 | LexicalBlockStack.pop_back(); | |||
2727 | RegionMap.erase(Ty->getDecl()); | |||
2728 | ||||
2729 | llvm::DINodeArray Elements = DBuilder.getOrCreateArray(EltTys); | |||
2730 | DBuilder.replaceArrays(FwdDecl, Elements); | |||
2731 | ||||
2732 | if (FwdDecl->isTemporary()) | |||
2733 | FwdDecl = | |||
2734 | llvm::MDNode::replaceWithPermanent(llvm::TempDICompositeType(FwdDecl)); | |||
2735 | ||||
2736 | RegionMap[Ty->getDecl()].reset(FwdDecl); | |||
2737 | return FwdDecl; | |||
2738 | } | |||
2739 | ||||
2740 | llvm::DIType *CGDebugInfo::CreateType(const ObjCObjectType *Ty, | |||
2741 | llvm::DIFile *Unit) { | |||
2742 | // Ignore protocols. | |||
2743 | return getOrCreateType(Ty->getBaseType(), Unit); | |||
2744 | } | |||
2745 | ||||
2746 | llvm::DIType *CGDebugInfo::CreateType(const ObjCTypeParamType *Ty, | |||
2747 | llvm::DIFile *Unit) { | |||
2748 | // Ignore protocols. | |||
2749 | SourceLocation Loc = Ty->getDecl()->getLocation(); | |||
2750 | ||||
2751 | // Use Typedefs to represent ObjCTypeParamType. | |||
2752 | return DBuilder.createTypedef( | |||
2753 | getOrCreateType(Ty->getDecl()->getUnderlyingType(), Unit), | |||
2754 | Ty->getDecl()->getName(), getOrCreateFile(Loc), getLineNumber(Loc), | |||
2755 | getDeclContextDescriptor(Ty->getDecl())); | |||
2756 | } | |||
2757 | ||||
2758 | /// \return true if Getter has the default name for the property PD. | |||
2759 | static bool hasDefaultGetterName(const ObjCPropertyDecl *PD, | |||
2760 | const ObjCMethodDecl *Getter) { | |||
2761 | assert(PD)(static_cast <bool> (PD) ? void (0) : __assert_fail ("PD" , "clang/lib/CodeGen/CGDebugInfo.cpp", 2761, __extension__ __PRETTY_FUNCTION__ )); | |||
2762 | if (!Getter) | |||
2763 | return true; | |||
2764 | ||||
2765 | assert(Getter->getDeclName().isObjCZeroArgSelector())(static_cast <bool> (Getter->getDeclName().isObjCZeroArgSelector ()) ? void (0) : __assert_fail ("Getter->getDeclName().isObjCZeroArgSelector()" , "clang/lib/CodeGen/CGDebugInfo.cpp", 2765, __extension__ __PRETTY_FUNCTION__ )); | |||
2766 | return PD->getName() == | |||
2767 | Getter->getDeclName().getObjCSelector().getNameForSlot(0); | |||
2768 | } | |||
2769 | ||||
2770 | /// \return true if Setter has the default name for the property PD. | |||
2771 | static bool hasDefaultSetterName(const ObjCPropertyDecl *PD, | |||
2772 | const ObjCMethodDecl *Setter) { | |||
2773 | assert(PD)(static_cast <bool> (PD) ? void (0) : __assert_fail ("PD" , "clang/lib/CodeGen/CGDebugInfo.cpp", 2773, __extension__ __PRETTY_FUNCTION__ )); | |||
2774 | if (!Setter) | |||
2775 | return true; | |||
2776 | ||||
2777 | assert(Setter->getDeclName().isObjCOneArgSelector())(static_cast <bool> (Setter->getDeclName().isObjCOneArgSelector ()) ? void (0) : __assert_fail ("Setter->getDeclName().isObjCOneArgSelector()" , "clang/lib/CodeGen/CGDebugInfo.cpp", 2777, __extension__ __PRETTY_FUNCTION__ )); | |||
2778 | return SelectorTable::constructSetterName(PD->getName()) == | |||
2779 | Setter->getDeclName().getObjCSelector().getNameForSlot(0); | |||
2780 | } | |||
2781 | ||||
2782 | llvm::DIType *CGDebugInfo::CreateType(const ObjCInterfaceType *Ty, | |||
2783 | llvm::DIFile *Unit) { | |||
2784 | ObjCInterfaceDecl *ID = Ty->getDecl(); | |||
2785 | if (!ID) | |||
2786 | return nullptr; | |||
2787 | ||||
2788 | // Return a forward declaration if this type was imported from a clang module, | |||
2789 | // and this is not the compile unit with the implementation of the type (which | |||
2790 | // may contain hidden ivars). | |||
2791 | if (DebugTypeExtRefs && ID->isFromASTFile() && ID->getDefinition() && | |||
2792 | !ID->getImplementation()) | |||
2793 | return DBuilder.createForwardDecl(llvm::dwarf::DW_TAG_structure_type, | |||
2794 | ID->getName(), | |||
2795 | getDeclContextDescriptor(ID), Unit, 0); | |||
2796 | ||||
2797 | // Get overall information about the record type for the debug info. | |||
2798 | llvm::DIFile *DefUnit = getOrCreateFile(ID->getLocation()); | |||
2799 | unsigned Line = getLineNumber(ID->getLocation()); | |||
2800 | auto RuntimeLang = | |||
2801 | static_cast<llvm::dwarf::SourceLanguage>(TheCU->getSourceLanguage()); | |||
2802 | ||||
2803 | // If this is just a forward declaration return a special forward-declaration | |||
2804 | // debug type since we won't be able to lay out the entire type. | |||
2805 | ObjCInterfaceDecl *Def = ID->getDefinition(); | |||
2806 | if (!Def || !Def->getImplementation()) { | |||
2807 | llvm::DIScope *Mod = getParentModuleOrNull(ID); | |||
2808 | llvm::DIType *FwdDecl = DBuilder.createReplaceableCompositeType( | |||
2809 | llvm::dwarf::DW_TAG_structure_type, ID->getName(), Mod ? Mod : TheCU, | |||
2810 | DefUnit, Line, RuntimeLang); | |||
2811 | ObjCInterfaceCache.push_back(ObjCInterfaceCacheEntry(Ty, FwdDecl, Unit)); | |||
2812 | return FwdDecl; | |||
2813 | } | |||
2814 | ||||
2815 | return CreateTypeDefinition(Ty, Unit); | |||
2816 | } | |||
2817 | ||||
2818 | llvm::DIModule *CGDebugInfo::getOrCreateModuleRef(ASTSourceDescriptor Mod, | |||
2819 | bool CreateSkeletonCU) { | |||
2820 | // Use the Module pointer as the key into the cache. This is a | |||
2821 | // nullptr if the "Module" is a PCH, which is safe because we don't | |||
2822 | // support chained PCH debug info, so there can only be a single PCH. | |||
2823 | const Module *M = Mod.getModuleOrNull(); | |||
2824 | auto ModRef = ModuleCache.find(M); | |||
2825 | if (ModRef != ModuleCache.end()) | |||
2826 | return cast<llvm::DIModule>(ModRef->second); | |||
2827 | ||||
2828 | // Macro definitions that were defined with "-D" on the command line. | |||
2829 | SmallString<128> ConfigMacros; | |||
2830 | { | |||
2831 | llvm::raw_svector_ostream OS(ConfigMacros); | |||
2832 | const auto &PPOpts = CGM.getPreprocessorOpts(); | |||
2833 | unsigned I = 0; | |||
2834 | // Translate the macro definitions back into a command line. | |||
2835 | for (auto &M : PPOpts.Macros) { | |||
2836 | if (++I > 1) | |||
2837 | OS << " "; | |||
2838 | const std::string &Macro = M.first; | |||
2839 | bool Undef = M.second; | |||
2840 | OS << "\"-" << (Undef ? 'U' : 'D'); | |||
2841 | for (char c : Macro) | |||
2842 | switch (c) { | |||
2843 | case '\\': | |||
2844 | OS << "\\\\"; | |||
2845 | break; | |||
2846 | case '"': | |||
2847 | OS << "\\\""; | |||
2848 | break; | |||
2849 | default: | |||
2850 | OS << c; | |||
2851 | } | |||
2852 | OS << '\"'; | |||
2853 | } | |||
2854 | } | |||
2855 | ||||
2856 | bool IsRootModule = M ? !M->Parent : true; | |||
2857 | // When a module name is specified as -fmodule-name, that module gets a | |||
2858 | // clang::Module object, but it won't actually be built or imported; it will | |||
2859 | // be textual. | |||
2860 | if (CreateSkeletonCU && IsRootModule && Mod.getASTFile().empty() && M) | |||
2861 | assert(StringRef(M->Name).startswith(CGM.getLangOpts().ModuleName) &&(static_cast <bool> (StringRef(M->Name).startswith(CGM .getLangOpts().ModuleName) && "clang module without ASTFile must be specified by -fmodule-name" ) ? void (0) : __assert_fail ("StringRef(M->Name).startswith(CGM.getLangOpts().ModuleName) && \"clang module without ASTFile must be specified by -fmodule-name\"" , "clang/lib/CodeGen/CGDebugInfo.cpp", 2862, __extension__ __PRETTY_FUNCTION__ )) | |||
2862 | "clang module without ASTFile must be specified by -fmodule-name")(static_cast <bool> (StringRef(M->Name).startswith(CGM .getLangOpts().ModuleName) && "clang module without ASTFile must be specified by -fmodule-name" ) ? void (0) : __assert_fail ("StringRef(M->Name).startswith(CGM.getLangOpts().ModuleName) && \"clang module without ASTFile must be specified by -fmodule-name\"" , "clang/lib/CodeGen/CGDebugInfo.cpp", 2862, __extension__ __PRETTY_FUNCTION__ )); | |||
2863 | ||||
2864 | // Return a StringRef to the remapped Path. | |||
2865 | auto RemapPath = [this](StringRef Path) -> std::string { | |||
2866 | std::string Remapped = remapDIPath(Path); | |||
2867 | StringRef Relative(Remapped); | |||
2868 | StringRef CompDir = TheCU->getDirectory(); | |||
2869 | if (Relative.consume_front(CompDir)) | |||
2870 | Relative.consume_front(llvm::sys::path::get_separator()); | |||
2871 | ||||
2872 | return Relative.str(); | |||
2873 | }; | |||
2874 | ||||
2875 | if (CreateSkeletonCU && IsRootModule && !Mod.getASTFile().empty()) { | |||
2876 | // PCH files don't have a signature field in the control block, | |||
2877 | // but LLVM detects skeleton CUs by looking for a non-zero DWO id. | |||
2878 | // We use the lower 64 bits for debug info. | |||
2879 | ||||
2880 | uint64_t Signature = 0; | |||
2881 | if (const auto &ModSig = Mod.getSignature()) | |||
2882 | Signature = ModSig.truncatedValue(); | |||
2883 | else | |||
2884 | Signature = ~1ULL; | |||
2885 | ||||
2886 | llvm::DIBuilder DIB(CGM.getModule()); | |||
2887 | SmallString<0> PCM; | |||
2888 | if (!llvm::sys::path::is_absolute(Mod.getASTFile())) { | |||
2889 | if (CGM.getHeaderSearchOpts().ModuleFileHomeIsCwd) | |||
2890 | PCM = getCurrentDirname(); | |||
2891 | else | |||
2892 | PCM = Mod.getPath(); | |||
2893 | } | |||
2894 | llvm::sys::path::append(PCM, Mod.getASTFile()); | |||
2895 | DIB.createCompileUnit( | |||
2896 | TheCU->getSourceLanguage(), | |||
2897 | // TODO: Support "Source" from external AST providers? | |||
2898 | DIB.createFile(Mod.getModuleName(), TheCU->getDirectory()), | |||
2899 | TheCU->getProducer(), false, StringRef(), 0, RemapPath(PCM), | |||
2900 | llvm::DICompileUnit::FullDebug, Signature); | |||
2901 | DIB.finalize(); | |||
2902 | } | |||
2903 | ||||
2904 | llvm::DIModule *Parent = | |||
2905 | IsRootModule ? nullptr | |||
2906 | : getOrCreateModuleRef(ASTSourceDescriptor(*M->Parent), | |||
2907 | CreateSkeletonCU); | |||
2908 | std::string IncludePath = Mod.getPath().str(); | |||
2909 | llvm::DIModule *DIMod = | |||
2910 | DBuilder.createModule(Parent, Mod.getModuleName(), ConfigMacros, | |||
2911 | RemapPath(IncludePath)); | |||
2912 | ModuleCache[M].reset(DIMod); | |||
2913 | return DIMod; | |||
2914 | } | |||
2915 | ||||
2916 | llvm::DIType *CGDebugInfo::CreateTypeDefinition(const ObjCInterfaceType *Ty, | |||
2917 | llvm::DIFile *Unit) { | |||
2918 | ObjCInterfaceDecl *ID = Ty->getDecl(); | |||
2919 | llvm::DIFile *DefUnit = getOrCreateFile(ID->getLocation()); | |||
2920 | unsigned Line = getLineNumber(ID->getLocation()); | |||
2921 | unsigned RuntimeLang = TheCU->getSourceLanguage(); | |||
2922 | ||||
2923 | // Bit size, align and offset of the type. | |||
2924 | uint64_t Size = CGM.getContext().getTypeSize(Ty); | |||
2925 | auto Align = getTypeAlignIfRequired(Ty, CGM.getContext()); | |||
2926 | ||||
2927 | llvm::DINode::DIFlags Flags = llvm::DINode::FlagZero; | |||
2928 | if (ID->getImplementation()) | |||
2929 | Flags |= llvm::DINode::FlagObjcClassComplete; | |||
2930 | ||||
2931 | llvm::DIScope *Mod = getParentModuleOrNull(ID); | |||
2932 | llvm::DICompositeType *RealDecl = DBuilder.createStructType( | |||
2933 | Mod ? Mod : Unit, ID->getName(), DefUnit, Line, Size, Align, Flags, | |||
2934 | nullptr, llvm::DINodeArray(), RuntimeLang); | |||
2935 | ||||
2936 | QualType QTy(Ty, 0); | |||
2937 | TypeCache[QTy.getAsOpaquePtr()].reset(RealDecl); | |||
2938 | ||||
2939 | // Push the struct on region stack. | |||
2940 | LexicalBlockStack.emplace_back(RealDecl); | |||
2941 | RegionMap[Ty->getDecl()].reset(RealDecl); | |||
2942 | ||||
2943 | // Convert all the elements. | |||
2944 | SmallVector<llvm::Metadata *, 16> EltTys; | |||
2945 | ||||
2946 | ObjCInterfaceDecl *SClass = ID->getSuperClass(); | |||
2947 | if (SClass) { | |||
2948 | llvm::DIType *SClassTy = | |||
2949 | getOrCreateType(CGM.getContext().getObjCInterfaceType(SClass), Unit); | |||
2950 | if (!SClassTy) | |||
2951 | return nullptr; | |||
2952 | ||||
2953 | llvm::DIType *InhTag = DBuilder.createInheritance(RealDecl, SClassTy, 0, 0, | |||
2954 | llvm::DINode::FlagZero); | |||
2955 | EltTys.push_back(InhTag); | |||
2956 | } | |||
2957 | ||||
2958 | // Create entries for all of the properties. | |||
2959 | auto AddProperty = [&](const ObjCPropertyDecl *PD) { | |||
2960 | SourceLocation Loc = PD->getLocation(); | |||
2961 | llvm::DIFile *PUnit = getOrCreateFile(Loc); | |||
2962 | unsigned PLine = getLineNumber(Loc); | |||
2963 | ObjCMethodDecl *Getter = PD->getGetterMethodDecl(); | |||
2964 | ObjCMethodDecl *Setter = PD->getSetterMethodDecl(); | |||
2965 | llvm::MDNode *PropertyNode = DBuilder.createObjCProperty( | |||
2966 | PD->getName(), PUnit, PLine, | |||
2967 | hasDefaultGetterName(PD, Getter) ? "" | |||
2968 | : getSelectorName(PD->getGetterName()), | |||
2969 | hasDefaultSetterName(PD, Setter) ? "" | |||
2970 | : getSelectorName(PD->getSetterName()), | |||
2971 | PD->getPropertyAttributes(), getOrCreateType(PD->getType(), PUnit)); | |||
2972 | EltTys.push_back(PropertyNode); | |||
2973 | }; | |||
2974 | { | |||
2975 | // Use 'char' for the isClassProperty bit as DenseSet requires space for | |||
2976 | // empty/tombstone keys in the data type (and bool is too small for that). | |||
2977 | typedef std::pair<char, const IdentifierInfo *> IsClassAndIdent; | |||
2978 | /// List of already emitted properties. Two distinct class and instance | |||
2979 | /// properties can share the same identifier (but not two instance | |||
2980 | /// properties or two class properties). | |||
2981 | llvm::DenseSet<IsClassAndIdent> PropertySet; | |||
2982 | /// Returns the IsClassAndIdent key for the given property. | |||
2983 | auto GetIsClassAndIdent = [](const ObjCPropertyDecl *PD) { | |||
2984 | return std::make_pair(PD->isClassProperty(), PD->getIdentifier()); | |||
2985 | }; | |||
2986 | for (const ObjCCategoryDecl *ClassExt : ID->known_extensions()) | |||
2987 | for (auto *PD : ClassExt->properties()) { | |||
2988 | PropertySet.insert(GetIsClassAndIdent(PD)); | |||
2989 | AddProperty(PD); | |||
2990 | } | |||
2991 | for (const auto *PD : ID->properties()) { | |||
2992 | // Don't emit duplicate metadata for properties that were already in a | |||
2993 | // class extension. | |||
2994 | if (!PropertySet.insert(GetIsClassAndIdent(PD)).second) | |||
2995 | continue; | |||
2996 | AddProperty(PD); | |||
2997 | } | |||
2998 | } | |||
2999 | ||||
3000 | const ASTRecordLayout &RL = CGM.getContext().getASTObjCInterfaceLayout(ID); | |||
3001 | unsigned FieldNo = 0; | |||
3002 | for (ObjCIvarDecl *Field = ID->all_declared_ivar_begin(); Field; | |||
3003 | Field = Field->getNextIvar(), ++FieldNo) { | |||
3004 | llvm::DIType *FieldTy = getOrCreateType(Field->getType(), Unit); | |||
3005 | if (!FieldTy) | |||
3006 | return nullptr; | |||
3007 | ||||
3008 | StringRef FieldName = Field->getName(); | |||
3009 | ||||
3010 | // Ignore unnamed fields. | |||
3011 | if (FieldName.empty()) | |||
3012 | continue; | |||
3013 | ||||
3014 | // Get the location for the field. | |||
3015 | llvm::DIFile *FieldDefUnit = getOrCreateFile(Field->getLocation()); | |||
3016 | unsigned FieldLine = getLineNumber(Field->getLocation()); | |||
3017 | QualType FType = Field->getType(); | |||
3018 | uint64_t FieldSize = 0; | |||
3019 | uint32_t FieldAlign = 0; | |||
3020 | ||||
3021 | if (!FType->isIncompleteArrayType()) { | |||
3022 | ||||
3023 | // Bit size, align and offset of the type. | |||
3024 | FieldSize = Field->isBitField() | |||
3025 | ? Field->getBitWidthValue(CGM.getContext()) | |||
3026 | : CGM.getContext().getTypeSize(FType); | |||
3027 | FieldAlign = getTypeAlignIfRequired(FType, CGM.getContext()); | |||
3028 | } | |||
3029 | ||||
3030 | uint64_t FieldOffset; | |||
3031 | if (CGM.getLangOpts().ObjCRuntime.isNonFragile()) { | |||
3032 | // We don't know the runtime offset of an ivar if we're using the | |||
3033 | // non-fragile ABI. For bitfields, use the bit offset into the first | |||
3034 | // byte of storage of the bitfield. For other fields, use zero. | |||
3035 | if (Field->isBitField()) { | |||
3036 | FieldOffset = | |||
3037 | CGM.getObjCRuntime().ComputeBitfieldBitOffset(CGM, ID, Field); | |||
3038 | FieldOffset %= CGM.getContext().getCharWidth(); | |||
3039 | } else { | |||
3040 | FieldOffset = 0; | |||
3041 | } | |||
3042 | } else { | |||
3043 | FieldOffset = RL.getFieldOffset(FieldNo); | |||
3044 | } | |||
3045 | ||||
3046 | llvm::DINode::DIFlags Flags = llvm::DINode::FlagZero; | |||
3047 | if (Field->getAccessControl() == ObjCIvarDecl::Protected) | |||
3048 | Flags = llvm::DINode::FlagProtected; | |||
3049 | else if (Field->getAccessControl() == ObjCIvarDecl::Private) | |||
3050 | Flags = llvm::DINode::FlagPrivate; | |||
3051 | else if (Field->getAccessControl() == ObjCIvarDecl::Public) | |||
3052 | Flags = llvm::DINode::FlagPublic; | |||
3053 | ||||
3054 | if (Field->isBitField()) | |||
3055 | Flags |= llvm::DINode::FlagBitField; | |||
3056 | ||||
3057 | llvm::MDNode *PropertyNode = nullptr; | |||
3058 | if (ObjCImplementationDecl *ImpD = ID->getImplementation()) { | |||
3059 | if (ObjCPropertyImplDecl *PImpD = | |||
3060 | ImpD->FindPropertyImplIvarDecl(Field->getIdentifier())) { | |||
3061 | if (ObjCPropertyDecl *PD = PImpD->getPropertyDecl()) { | |||
3062 | SourceLocation Loc = PD->getLocation(); | |||
3063 | llvm::DIFile *PUnit = getOrCreateFile(Loc); | |||
3064 | unsigned PLine = getLineNumber(Loc); | |||
3065 | ObjCMethodDecl *Getter = PImpD->getGetterMethodDecl(); | |||
3066 | ObjCMethodDecl *Setter = PImpD->getSetterMethodDecl(); | |||
3067 | PropertyNode = DBuilder.createObjCProperty( | |||
3068 | PD->getName(), PUnit, PLine, | |||
3069 | hasDefaultGetterName(PD, Getter) | |||
3070 | ? "" | |||
3071 | : getSelectorName(PD->getGetterName()), | |||
3072 | hasDefaultSetterName(PD, Setter) | |||
3073 | ? "" | |||
3074 | : getSelectorName(PD->getSetterName()), | |||
3075 | PD->getPropertyAttributes(), | |||
3076 | getOrCreateType(PD->getType(), PUnit)); | |||
3077 | } | |||
3078 | } | |||
3079 | } | |||
3080 | FieldTy = DBuilder.createObjCIVar(FieldName, FieldDefUnit, FieldLine, | |||
3081 | FieldSize, FieldAlign, FieldOffset, Flags, | |||
3082 | FieldTy, PropertyNode); | |||
3083 | EltTys.push_back(FieldTy); | |||
3084 | } | |||
3085 | ||||
3086 | llvm::DINodeArray Elements = DBuilder.getOrCreateArray(EltTys); | |||
3087 | DBuilder.replaceArrays(RealDecl, Elements); | |||
3088 | ||||
3089 | LexicalBlockStack.pop_back(); | |||
3090 | return RealDecl; | |||
3091 | } | |||
3092 | ||||
3093 | llvm::DIType *CGDebugInfo::CreateType(const VectorType *Ty, | |||
3094 | llvm::DIFile *Unit) { | |||
3095 | if (Ty->isExtVectorBoolType()) { | |||
| ||||
3096 | // Boolean ext_vector_type(N) are special because their real element type | |||
3097 | // (bits of bit size) is not their Clang element type (_Bool of size byte). | |||
3098 | // For now, we pretend the boolean vector were actually a vector of bytes | |||
3099 | // (where each byte represents 8 bits of the actual vector). | |||
3100 | // FIXME Debug info should actually represent this proper as a vector mask | |||
3101 | // type. | |||
3102 | auto &Ctx = CGM.getContext(); | |||
3103 | uint64_t Size = CGM.getContext().getTypeSize(Ty); | |||
3104 | uint64_t NumVectorBytes = Size / Ctx.getCharWidth(); | |||
3105 | ||||
3106 | // Construct the vector of 'char' type. | |||
3107 | QualType CharVecTy = Ctx.getVectorType(Ctx.CharTy, NumVectorBytes, | |||
3108 | VectorType::GenericVector); | |||
3109 | return CreateType(CharVecTy->getAs<VectorType>(), Unit); | |||
3110 | } | |||
3111 | ||||
3112 | llvm::DIType *ElementTy = getOrCreateType(Ty->getElementType(), Unit); | |||
3113 | int64_t Count = Ty->getNumElements(); | |||
3114 | ||||
3115 | llvm::Metadata *Subscript; | |||
3116 | QualType QTy(Ty, 0); | |||
3117 | auto SizeExpr = SizeExprCache.find(QTy); | |||
3118 | if (SizeExpr != SizeExprCache.end()) | |||
3119 | Subscript = DBuilder.getOrCreateSubrange( | |||
3120 | SizeExpr->getSecond() /*count*/, nullptr /*lowerBound*/, | |||
3121 | nullptr /*upperBound*/, nullptr /*stride*/); | |||
3122 | else { | |||
3123 | auto *CountNode = | |||
3124 | llvm::ConstantAsMetadata::get(llvm::ConstantInt::getSigned( | |||
3125 | llvm::Type::getInt64Ty(CGM.getLLVMContext()), Count ? Count : -1)); | |||
3126 | Subscript = DBuilder.getOrCreateSubrange( | |||
3127 | CountNode /*count*/, nullptr /*lowerBound*/, nullptr /*upperBound*/, | |||
3128 | nullptr /*stride*/); | |||
3129 | } | |||
3130 | llvm::DINodeArray SubscriptArray = DBuilder.getOrCreateArray(Subscript); | |||
3131 | ||||
3132 | uint64_t Size = CGM.getContext().getTypeSize(Ty); | |||
3133 | auto Align = getTypeAlignIfRequired(Ty, CGM.getContext()); | |||
3134 | ||||
3135 | return DBuilder.createVectorType(Size, Align, ElementTy, SubscriptArray); | |||
3136 | } | |||
3137 | ||||
3138 | llvm::DIType *CGDebugInfo::CreateType(const ConstantMatrixType *Ty, | |||
3139 | llvm::DIFile *Unit) { | |||
3140 | // FIXME: Create another debug type for matrices | |||
3141 | // For the time being, it treats it like a nested ArrayType. | |||
3142 | ||||
3143 | llvm::DIType *ElementTy = getOrCreateType(Ty->getElementType(), Unit); | |||
3144 | uint64_t Size = CGM.getContext().getTypeSize(Ty); | |||
3145 | uint32_t Align = getTypeAlignIfRequired(Ty, CGM.getContext()); | |||
3146 | ||||
3147 | // Create ranges for both dimensions. | |||
3148 | llvm::SmallVector<llvm::Metadata *, 2> Subscripts; | |||
3149 | auto *ColumnCountNode = | |||
3150 | llvm::ConstantAsMetadata::get(llvm::ConstantInt::getSigned( | |||
3151 | llvm::Type::getInt64Ty(CGM.getLLVMContext()), Ty->getNumColumns())); | |||
3152 | auto *RowCountNode = | |||
3153 | llvm::ConstantAsMetadata::get(llvm::ConstantInt::getSigned( | |||
3154 | llvm::Type::getInt64Ty(CGM.getLLVMContext()), Ty->getNumRows())); | |||
3155 | Subscripts.push_back(DBuilder.getOrCreateSubrange( | |||
3156 | ColumnCountNode /*count*/, nullptr /*lowerBound*/, nullptr /*upperBound*/, | |||
3157 | nullptr /*stride*/)); | |||
3158 | Subscripts.push_back(DBuilder.getOrCreateSubrange( | |||
3159 | RowCountNode /*count*/, nullptr /*lowerBound*/, nullptr /*upperBound*/, | |||
3160 | nullptr /*stride*/)); | |||
3161 | llvm::DINodeArray SubscriptArray = DBuilder.getOrCreateArray(Subscripts); | |||
3162 | return DBuilder.createArrayType(Size, Align, ElementTy, SubscriptArray); | |||
3163 | } | |||
3164 | ||||
3165 | llvm::DIType *CGDebugInfo::CreateType(const ArrayType *Ty, llvm::DIFile *Unit) { | |||
3166 | uint64_t Size; | |||
3167 | uint32_t Align; | |||
3168 | ||||
3169 | // FIXME: make getTypeAlign() aware of VLAs and incomplete array types | |||
3170 | if (const auto *VAT = dyn_cast<VariableArrayType>(Ty)) { | |||
3171 | Size = 0; | |||
3172 | Align = getTypeAlignIfRequired(CGM.getContext().getBaseElementType(VAT), | |||
3173 | CGM.getContext()); | |||
3174 | } else if (Ty->isIncompleteArrayType()) { | |||
3175 | Size = 0; | |||
3176 | if (Ty->getElementType()->isIncompleteType()) | |||
3177 | Align = 0; | |||
3178 | else | |||
3179 | Align = getTypeAlignIfRequired(Ty->getElementType(), CGM.getContext()); | |||
3180 | } else if (Ty->isIncompleteType()) { | |||
3181 | Size = 0; | |||
3182 | Align = 0; | |||
3183 | } else { | |||
3184 | // Size and align of the whole array, not the element type. | |||
3185 | Size = CGM.getContext().getTypeSize(Ty); | |||
3186 | Align = getTypeAlignIfRequired(Ty, CGM.getContext()); | |||
3187 | } | |||
3188 | ||||
3189 | // Add the dimensions of the array. FIXME: This loses CV qualifiers from | |||
3190 | // interior arrays, do we care? Why aren't nested arrays represented the | |||
3191 | // obvious/recursive way? | |||
3192 | SmallVector<llvm::Metadata *, 8> Subscripts; | |||
3193 | QualType EltTy(Ty, 0); | |||
3194 | while ((Ty = dyn_cast<ArrayType>(EltTy))) { | |||
3195 | // If the number of elements is known, then count is that number. Otherwise, | |||
3196 | // it's -1. This allows us to represent a subrange with an array of 0 | |||
3197 | // elements, like this: | |||
3198 | // | |||
3199 | // struct foo { | |||
3200 | // int x[0]; | |||
3201 | // }; | |||
3202 | int64_t Count = -1; // Count == -1 is an unbounded array. | |||
3203 | if (const auto *CAT = dyn_cast<ConstantArrayType>(Ty)) | |||
3204 | Count = CAT->getSize().getZExtValue(); | |||
3205 | else if (const auto *VAT = dyn_cast<VariableArrayType>(Ty)) { | |||
3206 | if (Expr *Size = VAT->getSizeExpr()) { | |||
3207 | Expr::EvalResult Result; | |||
3208 | if (Size->EvaluateAsInt(Result, CGM.getContext())) | |||
3209 | Count = Result.Val.getInt().getExtValue(); | |||
3210 | } | |||
3211 | } | |||
3212 | ||||
3213 | auto SizeNode = SizeExprCache.find(EltTy); | |||
3214 | if (SizeNode != SizeExprCache.end()) | |||
3215 | Subscripts.push_back(DBuilder.getOrCreateSubrange( | |||
3216 | SizeNode->getSecond() /*count*/, nullptr /*lowerBound*/, | |||
3217 | nullptr /*upperBound*/, nullptr /*stride*/)); | |||
3218 | else { | |||
3219 | auto *CountNode = | |||
3220 | llvm::ConstantAsMetadata::get(llvm::ConstantInt::getSigned( | |||
3221 | llvm::Type::getInt64Ty(CGM.getLLVMContext()), Count)); | |||
3222 | Subscripts.push_back(DBuilder.getOrCreateSubrange( | |||
3223 | CountNode /*count*/, nullptr /*lowerBound*/, nullptr /*upperBound*/, | |||
3224 | nullptr /*stride*/)); | |||
3225 | } | |||
3226 | EltTy = Ty->getElementType(); | |||
3227 | } | |||
3228 | ||||
3229 | llvm::DINodeArray SubscriptArray = DBuilder.getOrCreateArray(Subscripts); | |||
3230 | ||||
3231 | return DBuilder.createArrayType(Size, Align, getOrCreateType(EltTy, Unit), | |||
3232 | SubscriptArray); | |||
3233 | } | |||
3234 | ||||
3235 | llvm::DIType *CGDebugInfo::CreateType(const LValueReferenceType *Ty, | |||
3236 | llvm::DIFile *Unit) { | |||
3237 | return CreatePointerLikeType(llvm::dwarf::DW_TAG_reference_type, Ty, | |||
3238 | Ty->getPointeeType(), Unit); | |||
3239 | } | |||
3240 | ||||
3241 | llvm::DIType *CGDebugInfo::CreateType(const RValueReferenceType *Ty, | |||
3242 | llvm::DIFile *Unit) { | |||
3243 | llvm::dwarf::Tag Tag = llvm::dwarf::DW_TAG_rvalue_reference_type; | |||
3244 | // DW_TAG_rvalue_reference_type was introduced in DWARF 4. | |||
3245 | if (CGM.getCodeGenOpts().DebugStrictDwarf && | |||
3246 | CGM.getCodeGenOpts().DwarfVersion < 4) | |||
3247 | Tag = llvm::dwarf::DW_TAG_reference_type; | |||
3248 | ||||
3249 | return CreatePointerLikeType(Tag, Ty, Ty->getPointeeType(), Unit); | |||
3250 | } | |||
3251 | ||||
3252 | llvm::DIType *CGDebugInfo::CreateType(const MemberPointerType *Ty, | |||
3253 | llvm::DIFile *U) { | |||
3254 | llvm::DINode::DIFlags Flags = llvm::DINode::FlagZero; | |||
3255 | uint64_t Size = 0; | |||
3256 | ||||
3257 | if (!Ty->isIncompleteType()) { | |||
3258 | Size = CGM.getContext().getTypeSize(Ty); | |||
3259 | ||||
3260 | // Set the MS inheritance model. There is no flag for the unspecified model. | |||
3261 | if (CGM.getTarget().getCXXABI().isMicrosoft()) { | |||
3262 | switch (Ty->getMostRecentCXXRecordDecl()->getMSInheritanceModel()) { | |||
3263 | case MSInheritanceModel::Single: | |||
3264 | Flags |= llvm::DINode::FlagSingleInheritance; | |||
3265 | break; | |||
3266 | case MSInheritanceModel::Multiple: | |||
3267 | Flags |= llvm::DINode::FlagMultipleInheritance; | |||
3268 | break; | |||
3269 | case MSInheritanceModel::Virtual: | |||
3270 | Flags |= llvm::DINode::FlagVirtualInheritance; | |||
3271 | break; | |||
3272 | case MSInheritanceModel::Unspecified: | |||
3273 | break; | |||
3274 | } | |||
3275 | } | |||
3276 | } | |||
3277 | ||||
3278 | llvm::DIType *ClassType = getOrCreateType(QualType(Ty->getClass(), 0), U); | |||
3279 | if (Ty->isMemberDataPointerType()) | |||
3280 | return DBuilder.createMemberPointerType( | |||
3281 | getOrCreateType(Ty->getPointeeType(), U), ClassType, Size, /*Align=*/0, | |||
3282 | Flags); | |||
3283 | ||||
3284 | const FunctionProtoType *FPT = | |||
3285 | Ty->getPointeeType()->getAs<FunctionProtoType>(); | |||
3286 | return DBuilder.createMemberPointerType( | |||
3287 | getOrCreateInstanceMethodType( | |||
3288 | CXXMethodDecl::getThisType(FPT, Ty->getMostRecentCXXRecordDecl()), | |||
3289 | FPT, U), | |||
3290 | ClassType, Size, /*Align=*/0, Flags); | |||
3291 | } | |||
3292 | ||||
3293 | llvm::DIType *CGDebugInfo::CreateType(const AtomicType *Ty, llvm::DIFile *U) { | |||
3294 | auto *FromTy = getOrCreateType(Ty->getValueType(), U); | |||
3295 | return DBuilder.createQualifiedType(llvm::dwarf::DW_TAG_atomic_type, FromTy); | |||
3296 | } | |||
3297 | ||||
3298 | llvm::DIType *CGDebugInfo::CreateType(const PipeType *Ty, llvm::DIFile *U) { | |||
3299 | return getOrCreateType(Ty->getElementType(), U); | |||
3300 | } | |||
3301 | ||||
3302 | llvm::DIType *CGDebugInfo::CreateEnumType(const EnumType *Ty) { | |||
3303 | const EnumDecl *ED = Ty->getDecl(); | |||
3304 | ||||
3305 | uint64_t Size = 0; | |||
3306 | uint32_t Align = 0; | |||
3307 | if (!ED->getTypeForDecl()->isIncompleteType()) { | |||
3308 | Size = CGM.getContext().getTypeSize(ED->getTypeForDecl()); | |||
3309 | Align = getDeclAlignIfRequired(ED, CGM.getContext()); | |||
3310 | } | |||
3311 | ||||
3312 | SmallString<256> Identifier = getTypeIdentifier(Ty, CGM, TheCU); | |||
3313 | ||||
3314 | bool isImportedFromModule = | |||
3315 | DebugTypeExtRefs && ED->isFromASTFile() && ED->getDefinition(); | |||
3316 | ||||
3317 | // If this is just a forward declaration, construct an appropriately | |||
3318 | // marked node and just return it. | |||
3319 | if (isImportedFromModule || !ED->getDefinition()) { | |||
3320 | // Note that it is possible for enums to be created as part of | |||
3321 | // their own declcontext. In this case a FwdDecl will be created | |||
3322 | // twice. This doesn't cause a problem because both FwdDecls are | |||
3323 | // entered into the ReplaceMap: finalize() will replace the first | |||
3324 | // FwdDecl with the second and then replace the second with | |||
3325 | // complete type. | |||
3326 | llvm::DIScope *EDContext = getDeclContextDescriptor(ED); | |||
3327 | llvm::DIFile *DefUnit = getOrCreateFile(ED->getLocation()); | |||
3328 | llvm::TempDIScope TmpContext(DBuilder.createReplaceableCompositeType( | |||
3329 | llvm::dwarf::DW_TAG_enumeration_type, "", TheCU, DefUnit, 0)); | |||
3330 | ||||
3331 | unsigned Line = getLineNumber(ED->getLocation()); | |||
3332 | StringRef EDName = ED->getName(); | |||
3333 | llvm::DIType *RetTy = DBuilder.createReplaceableCompositeType( | |||
3334 | llvm::dwarf::DW_TAG_enumeration_type, EDName, EDContext, DefUnit, Line, | |||
3335 | 0, Size, Align, llvm::DINode::FlagFwdDecl, Identifier); | |||
3336 | ||||
3337 | ReplaceMap.emplace_back( | |||
3338 | std::piecewise_construct, std::make_tuple(Ty), | |||
3339 | std::make_tuple(static_cast<llvm::Metadata *>(RetTy))); | |||
3340 | return RetTy; | |||
3341 | } | |||
3342 | ||||
3343 | return CreateTypeDefinition(Ty); | |||
3344 | } | |||
3345 | ||||
3346 | llvm::DIType *CGDebugInfo::CreateTypeDefinition(const EnumType *Ty) { | |||
3347 | const EnumDecl *ED = Ty->getDecl(); | |||
3348 | uint64_t Size = 0; | |||
3349 | uint32_t Align = 0; | |||
3350 | if (!ED->getTypeForDecl()->isIncompleteType()) { | |||
3351 | Size = CGM.getContext().getTypeSize(ED->getTypeForDecl()); | |||
3352 | Align = getDeclAlignIfRequired(ED, CGM.getContext()); | |||
3353 | } | |||
3354 | ||||
3355 | SmallString<256> Identifier = getTypeIdentifier(Ty, CGM, TheCU); | |||
3356 | ||||
3357 | SmallVector<llvm::Metadata *, 16> Enumerators; | |||
3358 | ED = ED->getDefinition(); | |||
3359 | for (const auto *Enum : ED->enumerators()) { | |||
3360 | Enumerators.push_back( | |||
3361 | DBuilder.createEnumerator(Enum->getName(), Enum->getInitVal())); | |||
3362 | } | |||
3363 | ||||
3364 | // Return a CompositeType for the enum itself. | |||
3365 | llvm::DINodeArray EltArray = DBuilder.getOrCreateArray(Enumerators); | |||
3366 | ||||
3367 | llvm::DIFile *DefUnit = getOrCreateFile(ED->getLocation()); | |||
3368 | unsigned Line = getLineNumber(ED->getLocation()); | |||
3369 | llvm::DIScope *EnumContext = getDeclContextDescriptor(ED); | |||
3370 | llvm::DIType *ClassTy = getOrCreateType(ED->getIntegerType(), DefUnit); | |||
3371 | return DBuilder.createEnumerationType(EnumContext, ED->getName(), DefUnit, | |||
3372 | Line, Size, Align, EltArray, ClassTy, | |||
3373 | Identifier, ED->isScoped()); | |||
3374 | } | |||
3375 | ||||
3376 | llvm::DIMacro *CGDebugInfo::CreateMacro(llvm::DIMacroFile *Parent, | |||
3377 | unsigned MType, SourceLocation LineLoc, | |||
3378 | StringRef Name, StringRef Value) { | |||
3379 | unsigned Line = LineLoc.isInvalid() ? 0 : getLineNumber(LineLoc); | |||
3380 | return DBuilder.createMacro(Parent, Line, MType, Name, Value); | |||
3381 | } | |||
3382 | ||||
3383 | llvm::DIMacroFile *CGDebugInfo::CreateTempMacroFile(llvm::DIMacroFile *Parent, | |||
3384 | SourceLocation LineLoc, | |||
3385 | SourceLocation FileLoc) { | |||
3386 | llvm::DIFile *FName = getOrCreateFile(FileLoc); | |||
3387 | unsigned Line = LineLoc.isInvalid() ? 0 : getLineNumber(LineLoc); | |||
3388 | return DBuilder.createTempMacroFile(Parent, Line, FName); | |||
3389 | } | |||
3390 | ||||
3391 | static QualType UnwrapTypeForDebugInfo(QualType T, const ASTContext &C) { | |||
3392 | Qualifiers Quals; | |||
3393 | do { | |||
3394 | Qualifiers InnerQuals = T.getLocalQualifiers(); | |||
3395 | // Qualifiers::operator+() doesn't like it if you add a Qualifier | |||
3396 | // that is already there. | |||
3397 | Quals += Qualifiers::removeCommonQualifiers(Quals, InnerQuals); | |||
3398 | Quals += InnerQuals; | |||
3399 | QualType LastT = T; | |||
3400 | switch (T->getTypeClass()) { | |||
3401 | default: | |||
3402 | return C.getQualifiedType(T.getTypePtr(), Quals); | |||
3403 | case Type::TemplateSpecialization: { | |||
3404 | const auto *Spec = cast<TemplateSpecializationType>(T); | |||
3405 | if (Spec->isTypeAlias()) | |||
3406 | return C.getQualifiedType(T.getTypePtr(), Quals); | |||
3407 | T = Spec->desugar(); | |||
3408 | break; | |||
3409 | } | |||
3410 | case Type::TypeOfExpr: | |||
3411 | T = cast<TypeOfExprType>(T)->getUnderlyingExpr()->getType(); | |||
3412 | break; | |||
3413 | case Type::TypeOf: | |||
3414 | T = cast<TypeOfType>(T)->getUnmodifiedType(); | |||
3415 | break; | |||
3416 | case Type::Decltype: | |||
3417 | T = cast<DecltypeType>(T)->getUnderlyingType(); | |||
3418 | break; | |||
3419 | case Type::UnaryTransform: | |||
3420 | T = cast<UnaryTransformType>(T)->getUnderlyingType(); | |||
3421 | break; | |||
3422 | case Type::Attributed: | |||
3423 | T = cast<AttributedType>(T)->getEquivalentType(); | |||
3424 | break; | |||
3425 | case Type::BTFTagAttributed: | |||
3426 | T = cast<BTFTagAttributedType>(T)->getWrappedType(); | |||
3427 | break; | |||
3428 | case Type::Elaborated: | |||
3429 | T = cast<ElaboratedType>(T)->getNamedType(); | |||
3430 | break; | |||
3431 | case Type::Using: | |||
3432 | T = cast<UsingType>(T)->getUnderlyingType(); | |||
3433 | break; | |||
3434 | case Type::Paren: | |||
3435 | T = cast<ParenType>(T)->getInnerType(); | |||
3436 | break; | |||
3437 | case Type::MacroQualified: | |||
3438 | T = cast<MacroQualifiedType>(T)->getUnderlyingType(); | |||
3439 | break; | |||
3440 | case Type::SubstTemplateTypeParm: | |||
3441 | T = cast<SubstTemplateTypeParmType>(T)->getReplacementType(); | |||
3442 | break; | |||
3443 | case Type::Auto: | |||
3444 | case Type::DeducedTemplateSpecialization: { | |||
3445 | QualType DT = cast<DeducedType>(T)->getDeducedType(); | |||
3446 | assert(!DT.isNull() && "Undeduced types shouldn't reach here.")(static_cast <bool> (!DT.isNull() && "Undeduced types shouldn't reach here." ) ? void (0) : __assert_fail ("!DT.isNull() && \"Undeduced types shouldn't reach here.\"" , "clang/lib/CodeGen/CGDebugInfo.cpp", 3446, __extension__ __PRETTY_FUNCTION__ )); | |||
3447 | T = DT; | |||
3448 | break; | |||
3449 | } | |||
3450 | case Type::Adjusted: | |||
3451 | case Type::Decayed: | |||
3452 | // Decayed and adjusted types use the adjusted type in LLVM and DWARF. | |||
3453 | T = cast<AdjustedType>(T)->getAdjustedType(); | |||
3454 | break; | |||
3455 | } | |||
3456 | ||||
3457 | assert(T != LastT && "Type unwrapping failed to unwrap!")(static_cast <bool> (T != LastT && "Type unwrapping failed to unwrap!" ) ? void (0) : __assert_fail ("T != LastT && \"Type unwrapping failed to unwrap!\"" , "clang/lib/CodeGen/CGDebugInfo.cpp", 3457, __extension__ __PRETTY_FUNCTION__ )); | |||
3458 | (void)LastT; | |||
3459 | } while (true); | |||
3460 | } | |||
3461 | ||||
3462 | llvm::DIType *CGDebugInfo::getTypeOrNull(QualType Ty) { | |||
3463 | assert(Ty == UnwrapTypeForDebugInfo(Ty, CGM.getContext()))(static_cast <bool> (Ty == UnwrapTypeForDebugInfo(Ty, CGM .getContext())) ? void (0) : __assert_fail ("Ty == UnwrapTypeForDebugInfo(Ty, CGM.getContext())" , "clang/lib/CodeGen/CGDebugInfo.cpp", 3463, __extension__ __PRETTY_FUNCTION__ )); | |||
3464 | auto It = TypeCache.find(Ty.getAsOpaquePtr()); | |||
3465 | if (It != TypeCache.end()) { | |||
3466 | // Verify that the debug info still exists. | |||
3467 | if (llvm::Metadata *V = It->second) | |||
3468 | return cast<llvm::DIType>(V); | |||
3469 | } | |||
3470 | ||||
3471 | return nullptr; | |||
3472 | } | |||
3473 | ||||
3474 | void CGDebugInfo::completeTemplateDefinition( | |||
3475 | const ClassTemplateSpecializationDecl &SD) { | |||
3476 | completeUnusedClass(SD); | |||
3477 | } | |||
3478 | ||||
3479 | void CGDebugInfo::completeUnusedClass(const CXXRecordDecl &D) { | |||
3480 | if (DebugKind <= llvm::codegenoptions::DebugLineTablesOnly || | |||
3481 | D.isDynamicClass()) | |||
3482 | return; | |||
3483 | ||||
3484 | completeClassData(&D); | |||
3485 | // In case this type has no member function definitions being emitted, ensure | |||
3486 | // it is retained | |||
3487 | RetainedTypes.push_back(CGM.getContext().getRecordType(&D).getAsOpaquePtr()); | |||
3488 | } | |||
3489 | ||||
3490 | llvm::DIType *CGDebugInfo::getOrCreateType(QualType Ty, llvm::DIFile *Unit) { | |||
3491 | if (Ty.isNull()) | |||
3492 | return nullptr; | |||
3493 | ||||
3494 | llvm::TimeTraceScope TimeScope("DebugType", [&]() { | |||
3495 | std::string Name; | |||
3496 | llvm::raw_string_ostream OS(Name); | |||
3497 | Ty.print(OS, getPrintingPolicy()); | |||
3498 | return Name; | |||
3499 | }); | |||
3500 | ||||
3501 | // Unwrap the type as needed for debug information. | |||
3502 | Ty = UnwrapTypeForDebugInfo(Ty, CGM.getContext()); | |||
3503 | ||||
3504 | if (auto *T
| |||
3505 | return T; | |||
3506 | ||||
3507 | llvm::DIType *Res = CreateTypeNode(Ty, Unit); | |||
3508 | void *TyPtr = Ty.getAsOpaquePtr(); | |||
3509 | ||||
3510 | // And update the type cache. | |||
3511 | TypeCache[TyPtr].reset(Res); | |||
3512 | ||||
3513 | return Res; | |||
3514 | } | |||
3515 | ||||
3516 | llvm::DIModule *CGDebugInfo::getParentModuleOrNull(const Decl *D) { | |||
3517 | // A forward declaration inside a module header does not belong to the module. | |||
3518 | if (isa<RecordDecl>(D) && !cast<RecordDecl>(D)->getDefinition()) | |||
3519 | return nullptr; | |||
3520 | if (DebugTypeExtRefs && D->isFromASTFile()) { | |||
3521 | // Record a reference to an imported clang module or precompiled header. | |||
3522 | auto *Reader = CGM.getContext().getExternalSource(); | |||
3523 | auto Idx = D->getOwningModuleID(); | |||
3524 | auto Info = Reader->getSourceDescriptor(Idx); | |||
3525 | if (Info) | |||
3526 | return getOrCreateModuleRef(*Info, /*SkeletonCU=*/true); | |||
3527 | } else if (ClangModuleMap) { | |||
3528 | // We are building a clang module or a precompiled header. | |||
3529 | // | |||
3530 | // TODO: When D is a CXXRecordDecl or a C++ Enum, the ODR applies | |||
3531 | // and it wouldn't be necessary to specify the parent scope | |||
3532 | // because the type is already unique by definition (it would look | |||
3533 | // like the output of -fno-standalone-debug). On the other hand, | |||
3534 | // the parent scope helps a consumer to quickly locate the object | |||
3535 | // file where the type's definition is located, so it might be | |||
3536 | // best to make this behavior a command line or debugger tuning | |||
3537 | // option. | |||
3538 | if (Module *M = D->getOwningModule()) { | |||
3539 | // This is a (sub-)module. | |||
3540 | auto Info = ASTSourceDescriptor(*M); | |||
3541 | return getOrCreateModuleRef(Info, /*SkeletonCU=*/false); | |||
3542 | } else { | |||
3543 | // This the precompiled header being built. | |||
3544 | return getOrCreateModuleRef(PCHDescriptor, /*SkeletonCU=*/false); | |||
3545 | } | |||
3546 | } | |||
3547 | ||||
3548 | return nullptr; | |||
3549 | } | |||
3550 | ||||
3551 | llvm::DIType *CGDebugInfo::CreateTypeNode(QualType Ty, llvm::DIFile *Unit) { | |||
3552 | // Handle qualifiers, which recursively handles what they refer to. | |||
3553 | if (Ty.hasLocalQualifiers()) | |||
3554 | return CreateQualifiedType(Ty, Unit); | |||
3555 | ||||
3556 | // Work out details of type. | |||
3557 | switch (Ty->getTypeClass()) { | |||
3558 | #define TYPE(Class, Base) | |||
3559 | #define ABSTRACT_TYPE(Class, Base) | |||
3560 | #define NON_CANONICAL_TYPE(Class, Base) | |||
3561 | #define DEPENDENT_TYPE(Class, Base) case Type::Class: | |||
3562 | #include "clang/AST/TypeNodes.inc" | |||
3563 | llvm_unreachable("Dependent types cannot show up in debug information")::llvm::llvm_unreachable_internal("Dependent types cannot show up in debug information" , "clang/lib/CodeGen/CGDebugInfo.cpp", 3563); | |||
3564 | ||||
3565 | case Type::ExtVector: | |||
3566 | case Type::Vector: | |||
3567 | return CreateType(cast<VectorType>(Ty), Unit); | |||
3568 | case Type::ConstantMatrix: | |||
3569 | return CreateType(cast<ConstantMatrixType>(Ty), Unit); | |||
3570 | case Type::ObjCObjectPointer: | |||
3571 | return CreateType(cast<ObjCObjectPointerType>(Ty), Unit); | |||
3572 | case Type::ObjCObject: | |||
3573 | return CreateType(cast<ObjCObjectType>(Ty), Unit); | |||
3574 | case Type::ObjCTypeParam: | |||
3575 | return CreateType(cast<ObjCTypeParamType>(Ty), Unit); | |||
3576 | case Type::ObjCInterface: | |||
3577 | return CreateType(cast<ObjCInterfaceType>(Ty), Unit); | |||
3578 | case Type::Builtin: | |||
3579 | return CreateType(cast<BuiltinType>(Ty)); | |||
3580 | case Type::Complex: | |||
3581 | return CreateType(cast<ComplexType>(Ty)); | |||
3582 | case Type::Pointer: | |||
3583 | return CreateType(cast<PointerType>(Ty), Unit); | |||
3584 | case Type::BlockPointer: | |||
3585 | return CreateType(cast<BlockPointerType>(Ty), Unit); | |||
3586 | case Type::Typedef: | |||
3587 | return CreateType(cast<TypedefType>(Ty), Unit); | |||
3588 | case Type::Record: | |||
3589 | return CreateType(cast<RecordType>(Ty)); | |||
3590 | case Type::Enum: | |||
3591 | return CreateEnumType(cast<EnumType>(Ty)); | |||
3592 | case Type::FunctionProto: | |||
3593 | case Type::FunctionNoProto: | |||
3594 | return CreateType(cast<FunctionType>(Ty), Unit); | |||
3595 | case Type::ConstantArray: | |||
3596 | case Type::VariableArray: | |||
3597 | case Type::IncompleteArray: | |||
3598 | return CreateType(cast<ArrayType>(Ty), Unit); | |||
3599 | ||||
3600 | case Type::LValueReference: | |||
3601 | return CreateType(cast<LValueReferenceType>(Ty), Unit); | |||
3602 | case Type::RValueReference: | |||
3603 | return CreateType(cast<RValueReferenceType>(Ty), Unit); | |||
3604 | ||||
3605 | case Type::MemberPointer: | |||
3606 | return CreateType(cast<MemberPointerType>(Ty), Unit); | |||
3607 | ||||
3608 | case Type::Atomic: | |||
3609 | return CreateType(cast<AtomicType>(Ty), Unit); | |||
3610 | ||||
3611 | case Type::BitInt: | |||
3612 | return CreateType(cast<BitIntType>(Ty)); | |||
3613 | case Type::Pipe: | |||
3614 | return CreateType(cast<PipeType>(Ty), Unit); | |||
3615 | ||||
3616 | case Type::TemplateSpecialization: | |||
3617 | return CreateType(cast<TemplateSpecializationType>(Ty), Unit); | |||
3618 | ||||
3619 | case Type::Auto: | |||
3620 | case Type::Attributed: | |||
3621 | case Type::BTFTagAttributed: | |||
3622 | case Type::Adjusted: | |||
3623 | case Type::Decayed: | |||
3624 | case Type::DeducedTemplateSpecialization: | |||
3625 | case Type::Elaborated: | |||
3626 | case Type::Using: | |||
3627 | case Type::Paren: | |||
3628 | case Type::MacroQualified: | |||
3629 | case Type::SubstTemplateTypeParm: | |||
3630 | case Type::TypeOfExpr: | |||
3631 | case Type::TypeOf: | |||
3632 | case Type::Decltype: | |||
3633 | case Type::UnaryTransform: | |||
3634 | break; | |||
3635 | } | |||
3636 | ||||
3637 | llvm_unreachable("type should have been unwrapped!")::llvm::llvm_unreachable_internal("type should have been unwrapped!" , "clang/lib/CodeGen/CGDebugInfo.cpp", 3637); | |||
3638 | } | |||
3639 | ||||
3640 | llvm::DICompositeType * | |||
3641 | CGDebugInfo::getOrCreateLimitedType(const RecordType *Ty) { | |||
3642 | QualType QTy(Ty, 0); | |||
3643 | ||||
3644 | auto *T = cast_or_null<llvm::DICompositeType>(getTypeOrNull(QTy)); | |||
3645 | ||||
3646 | // We may have cached a forward decl when we could have created | |||
3647 | // a non-forward decl. Go ahead and create a non-forward decl | |||
3648 | // now. | |||
3649 | if (T && !T->isForwardDecl()) | |||
3650 | return T; | |||
3651 | ||||
3652 | // Otherwise create the type. | |||
3653 | llvm::DICompositeType *Res = CreateLimitedType(Ty); | |||
3654 | ||||
3655 | // Propagate members from the declaration to the definition | |||
3656 | // CreateType(const RecordType*) will overwrite this with the members in the | |||
3657 | // correct order if the full type is needed. | |||
3658 | DBuilder.replaceArrays(Res, T ? T->getElements() : llvm::DINodeArray()); | |||
3659 | ||||
3660 | // And update the type cache. | |||
3661 | TypeCache[QTy.getAsOpaquePtr()].reset(Res); | |||
3662 | return Res; | |||
3663 | } | |||
3664 | ||||
3665 | // TODO: Currently used for context chains when limiting debug info. | |||
3666 | llvm::DICompositeType *CGDebugInfo::CreateLimitedType(const RecordType *Ty) { | |||
3667 | RecordDecl *RD = Ty->getDecl(); | |||
3668 | ||||
3669 | // Get overall information about the record type for the debug info. | |||
3670 | StringRef RDName = getClassName(RD); | |||
3671 | const SourceLocation Loc = RD->getLocation(); | |||
3672 | llvm::DIFile *DefUnit = nullptr; | |||
3673 | unsigned Line = 0; | |||
3674 | if (Loc.isValid()) { | |||
3675 | DefUnit = getOrCreateFile(Loc); | |||
3676 | Line = getLineNumber(Loc); | |||
3677 | } | |||
3678 | ||||
3679 | llvm::DIScope *RDContext = getDeclContextDescriptor(RD); | |||
3680 | ||||
3681 | // If we ended up creating the type during the context chain construction, | |||
3682 | // just return that. | |||
3683 | auto *T = cast_or_null<llvm::DICompositeType>( | |||
3684 | getTypeOrNull(CGM.getContext().getRecordType(RD))); | |||
3685 | if (T && (!T->isForwardDecl() || !RD->getDefinition())) | |||
3686 | return T; | |||
3687 | ||||
3688 | // If this is just a forward or incomplete declaration, construct an | |||
3689 | // appropriately marked node and just return it. | |||
3690 | const RecordDecl *D = RD->getDefinition(); | |||
3691 | if (!D || !D->isCompleteDefinition()) | |||
3692 | return getOrCreateRecordFwdDecl(Ty, RDContext); | |||
3693 | ||||
3694 | uint64_t Size = CGM.getContext().getTypeSize(Ty); | |||
3695 | // __attribute__((aligned)) can increase or decrease alignment *except* on a | |||
3696 | // struct or struct member, where it only increases alignment unless 'packed' | |||
3697 | // is also specified. To handle this case, the `getTypeAlignIfRequired` needs | |||
3698 | // to be used. | |||
3699 | auto Align = getTypeAlignIfRequired(Ty, CGM.getContext()); | |||
3700 | ||||
3701 | SmallString<256> Identifier = getTypeIdentifier(Ty, CGM, TheCU); | |||
3702 | ||||
3703 | // Explicitly record the calling convention and export symbols for C++ | |||
3704 | // records. | |||
3705 | auto Flags = llvm::DINode::FlagZero; | |||
3706 | if (auto CXXRD = dyn_cast<CXXRecordDecl>(RD)) { | |||
3707 | if (CGM.getCXXABI().getRecordArgABI(CXXRD) == CGCXXABI::RAA_Indirect) | |||
3708 | Flags |= llvm::DINode::FlagTypePassByReference; | |||
3709 | else | |||
3710 | Flags |= llvm::DINode::FlagTypePassByValue; | |||
3711 | ||||
3712 | // Record if a C++ record is non-trivial type. | |||
3713 | if (!CXXRD->isTrivial()) | |||
3714 | Flags |= llvm::DINode::FlagNonTrivial; | |||
3715 | ||||
3716 | // Record exports it symbols to the containing structure. | |||
3717 | if (CXXRD->isAnonymousStructOrUnion()) | |||
3718 | Flags |= llvm::DINode::FlagExportSymbols; | |||
3719 | ||||
3720 | Flags |= getAccessFlag(CXXRD->getAccess(), | |||
3721 | dyn_cast<CXXRecordDecl>(CXXRD->getDeclContext())); | |||
3722 | } | |||
3723 | ||||
3724 | llvm::DINodeArray Annotations = CollectBTFDeclTagAnnotations(D); | |||
3725 | llvm::DICompositeType *RealDecl = DBuilder.createReplaceableCompositeType( | |||
3726 | getTagForRecord(RD), RDName, RDContext, DefUnit, Line, 0, Size, Align, | |||
3727 | Flags, Identifier, Annotations); | |||
3728 | ||||
3729 | // Elements of composite types usually have back to the type, creating | |||
3730 | // uniquing cycles. Distinct nodes are more efficient. | |||
3731 | switch (RealDecl->getTag()) { | |||
3732 | default: | |||
3733 | llvm_unreachable("invalid composite type tag")::llvm::llvm_unreachable_internal("invalid composite type tag" , "clang/lib/CodeGen/CGDebugInfo.cpp", 3733); | |||
3734 | ||||
3735 | case llvm::dwarf::DW_TAG_array_type: | |||
3736 | case llvm::dwarf::DW_TAG_enumeration_type: | |||
3737 | // Array elements and most enumeration elements don't have back references, | |||
3738 | // so they don't tend to be involved in uniquing cycles and there is some | |||
3739 | // chance of merging them when linking together two modules. Only make | |||
3740 | // them distinct if they are ODR-uniqued. | |||
3741 | if (Identifier.empty()) | |||
3742 | break; | |||
3743 | [[fallthrough]]; | |||
3744 | ||||
3745 | case llvm::dwarf::DW_TAG_structure_type: | |||
3746 | case llvm::dwarf::DW_TAG_union_type: | |||
3747 | case llvm::dwarf::DW_TAG_class_type: | |||
3748 | // Immediately resolve to a distinct node. | |||
3749 | RealDecl = | |||
3750 | llvm::MDNode::replaceWithDistinct(llvm::TempDICompositeType(RealDecl)); | |||
3751 | break; | |||
3752 | } | |||
3753 | ||||
3754 | RegionMap[Ty->getDecl()].reset(RealDecl); | |||
3755 | TypeCache[QualType(Ty, 0).getAsOpaquePtr()].reset(RealDecl); | |||
3756 | ||||
3757 | if (const auto *TSpecial = dyn_cast<ClassTemplateSpecializationDecl>(RD)) | |||
3758 | DBuilder.replaceArrays(RealDecl, llvm::DINodeArray(), | |||
3759 | CollectCXXTemplateParams(TSpecial, DefUnit)); | |||
3760 | return RealDecl; | |||
3761 | } | |||
3762 | ||||
3763 | void CGDebugInfo::CollectContainingType(const CXXRecordDecl *RD, | |||
3764 | llvm::DICompositeType *RealDecl) { | |||
3765 | // A class's primary base or the class itself contains the vtable. | |||
3766 | llvm::DICompositeType *ContainingType = nullptr; | |||
3767 | const ASTRecordLayout &RL = CGM.getContext().getASTRecordLayout(RD); | |||
3768 | if (const CXXRecordDecl *PBase = RL.getPrimaryBase()) { | |||
3769 | // Seek non-virtual primary base root. | |||
3770 | while (true) { | |||
3771 | const ASTRecordLayout &BRL = CGM.getContext().getASTRecordLayout(PBase); | |||
3772 | const CXXRecordDecl *PBT = BRL.getPrimaryBase(); | |||
3773 | if (PBT && !BRL.isPrimaryBaseVirtual()) | |||
3774 | PBase = PBT; | |||
3775 | else | |||
3776 | break; | |||
3777 | } | |||
3778 | ContainingType = cast<llvm::DICompositeType>( | |||
3779 | getOrCreateType(QualType(PBase->getTypeForDecl(), 0), | |||
3780 | getOrCreateFile(RD->getLocation()))); | |||
3781 | } else if (RD->isDynamicClass()) | |||
3782 | ContainingType = RealDecl; | |||
3783 | ||||
3784 | DBuilder.replaceVTableHolder(RealDecl, ContainingType); | |||
3785 | } | |||
3786 | ||||
3787 | llvm::DIType *CGDebugInfo::CreateMemberType(llvm::DIFile *Unit, QualType FType, | |||
3788 | StringRef Name, uint64_t *Offset) { | |||
3789 | llvm::DIType *FieldTy = CGDebugInfo::getOrCreateType(FType, Unit); | |||
3790 | uint64_t FieldSize = CGM.getContext().getTypeSize(FType); | |||
3791 | auto FieldAlign = getTypeAlignIfRequired(FType, CGM.getContext()); | |||
3792 | llvm::DIType *Ty = | |||
3793 | DBuilder.createMemberType(Unit, Name, Unit, 0, FieldSize, FieldAlign, | |||
3794 | *Offset, llvm::DINode::FlagZero, FieldTy); | |||
3795 | *Offset += FieldSize; | |||
3796 | return Ty; | |||
3797 | } | |||
3798 | ||||
3799 | void CGDebugInfo::collectFunctionDeclProps(GlobalDecl GD, llvm::DIFile *Unit, | |||
3800 | StringRef &Name, | |||
3801 | StringRef &LinkageName, | |||
3802 | llvm::DIScope *&FDContext, | |||
3803 | llvm::DINodeArray &TParamsArray, | |||
3804 | llvm::DINode::DIFlags &Flags) { | |||
3805 | const auto *FD = cast<FunctionDecl>(GD.getCanonicalDecl().getDecl()); | |||
3806 | Name = getFunctionName(FD); | |||
3807 | // Use mangled name as linkage name for C/C++ functions. | |||
3808 | if (FD->getType()->getAs<FunctionProtoType>()) | |||
3809 | LinkageName = CGM.getMangledName(GD); | |||
3810 | if (FD->hasPrototype()) | |||
3811 | Flags |= llvm::DINode::FlagPrototyped; | |||
3812 | // No need to replicate the linkage name if it isn't different from the | |||
3813 | // subprogram name, no need to have it at all unless coverage is enabled or | |||
3814 | // debug is set to more than just line tables or extra debug info is needed. | |||
3815 | if (LinkageName == Name || | |||
3816 | (!CGM.getCodeGenOpts().EmitGcovArcs && | |||
3817 | !CGM.getCodeGenOpts().EmitGcovNotes && | |||
3818 | !CGM.getCodeGenOpts().DebugInfoForProfiling && | |||
3819 | !CGM.getCodeGenOpts().PseudoProbeForProfiling && | |||
3820 | DebugKind <= llvm::codegenoptions::DebugLineTablesOnly)) | |||
3821 | LinkageName = StringRef(); | |||
3822 | ||||
3823 | // Emit the function scope in line tables only mode (if CodeView) to | |||
3824 | // differentiate between function names. | |||
3825 | if (CGM.getCodeGenOpts().hasReducedDebugInfo() || | |||
3826 | (DebugKind == llvm::codegenoptions::DebugLineTablesOnly && | |||
3827 | CGM.getCodeGenOpts().EmitCodeView)) { | |||
3828 | if (const NamespaceDecl *NSDecl = | |||
3829 | dyn_cast_or_null<NamespaceDecl>(FD->getDeclContext())) | |||
3830 | FDContext = getOrCreateNamespace(NSDecl); | |||
3831 | else if (const RecordDecl *RDecl = | |||
3832 | dyn_cast_or_null<RecordDecl>(FD->getDeclContext())) { | |||
3833 | llvm::DIScope *Mod = getParentModuleOrNull(RDecl); | |||
3834 | FDContext = getContextDescriptor(RDecl, Mod ? Mod : TheCU); | |||
3835 | } | |||
3836 | } | |||
3837 | if (CGM.getCodeGenOpts().hasReducedDebugInfo()) { | |||
3838 | // Check if it is a noreturn-marked function | |||
3839 | if (FD->isNoReturn()) | |||
3840 | Flags |= llvm::DINode::FlagNoReturn; | |||
3841 | // Collect template parameters. | |||
3842 | TParamsArray = CollectFunctionTemplateParams(FD, Unit); | |||
3843 | } | |||
3844 | } | |||
3845 | ||||
3846 | void CGDebugInfo::collectVarDeclProps(const VarDecl *VD, llvm::DIFile *&Unit, | |||
3847 | unsigned &LineNo, QualType &T, | |||
3848 | StringRef &Name, StringRef &LinkageName, | |||
3849 | llvm::MDTuple *&TemplateParameters, | |||
3850 | llvm::DIScope *&VDContext) { | |||
3851 | Unit = getOrCreateFile(VD->getLocation()); | |||
3852 | LineNo = getLineNumber(VD->getLocation()); | |||
3853 | ||||
3854 | setLocation(VD->getLocation()); | |||
3855 | ||||
3856 | T = VD->getType(); | |||
3857 | if (T->isIncompleteArrayType()) { | |||
3858 | // CodeGen turns int[] into int[1] so we'll do the same here. | |||
3859 | llvm::APInt ConstVal(32, 1); | |||
3860 | QualType ET = CGM.getContext().getAsArrayType(T)->getElementType(); | |||
3861 | ||||
3862 | T = CGM.getContext().getConstantArrayType(ET, ConstVal, nullptr, | |||
3863 | ArrayType::Normal, 0); | |||
3864 | } | |||
3865 | ||||
3866 | Name = VD->getName(); | |||
3867 | if (VD->getDeclContext() && !isa<FunctionDecl>(VD->getDeclContext()) && | |||
3868 | !isa<ObjCMethodDecl>(VD->getDeclContext())) | |||
3869 | LinkageName = CGM.getMangledName(VD); | |||
3870 | if (LinkageName == Name) | |||
3871 | LinkageName = StringRef(); | |||
3872 | ||||
3873 | if (isa<VarTemplateSpecializationDecl>(VD)) { | |||
3874 | llvm::DINodeArray parameterNodes = CollectVarTemplateParams(VD, &*Unit); | |||
3875 | TemplateParameters = parameterNodes.get(); | |||
3876 | } else { | |||
3877 | TemplateParameters = nullptr; | |||
3878 | } | |||
3879 | ||||
3880 | // Since we emit declarations (DW_AT_members) for static members, place the | |||
3881 | // definition of those static members in the namespace they were declared in | |||
3882 | // in the source code (the lexical decl context). | |||
3883 | // FIXME: Generalize this for even non-member global variables where the | |||
3884 | // declaration and definition may have different lexical decl contexts, once | |||
3885 | // we have support for emitting declarations of (non-member) global variables. | |||
3886 | const DeclContext *DC = VD->isStaticDataMember() ? VD->getLexicalDeclContext() | |||
3887 | : VD->getDeclContext(); | |||
3888 | // When a record type contains an in-line initialization of a static data | |||
3889 | // member, and the record type is marked as __declspec(dllexport), an implicit | |||
3890 | // definition of the member will be created in the record context. DWARF | |||
3891 | // doesn't seem to have a nice way to describe this in a form that consumers | |||
3892 | // are likely to understand, so fake the "normal" situation of a definition | |||
3893 | // outside the class by putting it in the global scope. | |||
3894 | if (DC->isRecord()) | |||
3895 | DC = CGM.getContext().getTranslationUnitDecl(); | |||
3896 | ||||
3897 | llvm::DIScope *Mod = getParentModuleOrNull(VD); | |||
3898 | VDContext = getContextDescriptor(cast<Decl>(DC), Mod ? Mod : TheCU); | |||
3899 | } | |||
3900 | ||||
3901 | llvm::DISubprogram *CGDebugInfo::getFunctionFwdDeclOrStub(GlobalDecl GD, | |||
3902 | bool Stub) { | |||
3903 | llvm::DINodeArray TParamsArray; | |||
3904 | StringRef Name, LinkageName; | |||
3905 | llvm::DINode::DIFlags Flags = llvm::DINode::FlagZero; | |||
3906 | llvm::DISubprogram::DISPFlags SPFlags = llvm::DISubprogram::SPFlagZero; | |||
3907 | SourceLocation Loc = GD.getDecl()->getLocation(); | |||
3908 | llvm::DIFile *Unit = getOrCreateFile(Loc); | |||
3909 | llvm::DIScope *DContext = Unit; | |||
3910 | unsigned Line = getLineNumber(Loc); | |||
3911 | collectFunctionDeclProps(GD, Unit, Name, LinkageName, DContext, TParamsArray, | |||
3912 | Flags); | |||
3913 | auto *FD = cast<FunctionDecl>(GD.getDecl()); | |||
3914 | ||||
3915 | // Build function type. | |||
3916 | SmallVector<QualType, 16> ArgTypes; | |||
3917 | for (const ParmVarDecl *Parm : FD->parameters()) | |||
3918 | ArgTypes.push_back(Parm->getType()); | |||
3919 | ||||
3920 | CallingConv CC = FD->getType()->castAs<FunctionType>()->getCallConv(); | |||
3921 | QualType FnType = CGM.getContext().getFunctionType( | |||
3922 | FD->getReturnType(), ArgTypes, FunctionProtoType::ExtProtoInfo(CC)); | |||
3923 | if (!FD->isExternallyVisible()) | |||
3924 | SPFlags |= llvm::DISubprogram::SPFlagLocalToUnit; | |||
3925 | if (CGM.getLangOpts().Optimize) | |||
3926 | SPFlags |= llvm::DISubprogram::SPFlagOptimized; | |||
3927 | ||||
3928 | if (Stub) { | |||
3929 | Flags |= getCallSiteRelatedAttrs(); | |||
3930 | SPFlags |= llvm::DISubprogram::SPFlagDefinition; | |||
3931 | return DBuilder.createFunction( | |||
3932 | DContext, Name, LinkageName, Unit, Line, | |||
3933 | getOrCreateFunctionType(GD.getDecl(), FnType, Unit), 0, Flags, SPFlags, | |||
3934 | TParamsArray.get(), getFunctionDeclaration(FD)); | |||
3935 | } | |||
3936 | ||||
3937 | llvm::DISubprogram *SP = DBuilder.createTempFunctionFwdDecl( | |||
3938 | DContext, Name, LinkageName, Unit, Line, | |||
3939 | getOrCreateFunctionType(GD.getDecl(), FnType, Unit), 0, Flags, SPFlags, | |||
3940 | TParamsArray.get(), getFunctionDeclaration(FD)); | |||
3941 | const FunctionDecl *CanonDecl = FD->getCanonicalDecl(); | |||
3942 | FwdDeclReplaceMap.emplace_back(std::piecewise_construct, | |||
3943 | std::make_tuple(CanonDecl), | |||
3944 | std::make_tuple(SP)); | |||
3945 | return SP; | |||
3946 | } | |||
3947 | ||||
3948 | llvm::DISubprogram *CGDebugInfo::getFunctionForwardDeclaration(GlobalDecl GD) { | |||
3949 | return getFunctionFwdDeclOrStub(GD, /* Stub = */ false); | |||
3950 | } | |||
3951 | ||||
3952 | llvm::DISubprogram *CGDebugInfo::getFunctionStub(GlobalDecl GD) { | |||
3953 | return getFunctionFwdDeclOrStub(GD, /* Stub = */ true); | |||
3954 | } | |||
3955 | ||||
3956 | llvm::DIGlobalVariable * | |||
3957 | CGDebugInfo::getGlobalVariableForwardDeclaration(const VarDecl *VD) { | |||
3958 | QualType T; | |||
3959 | StringRef Name, LinkageName; | |||
3960 | SourceLocation Loc = VD->getLocation(); | |||
3961 | llvm::DIFile *Unit = getOrCreateFile(Loc); | |||
3962 | llvm::DIScope *DContext = Unit; | |||
3963 | unsigned Line = getLineNumber(Loc); | |||
3964 | llvm::MDTuple *TemplateParameters = nullptr; | |||
3965 | ||||
3966 | collectVarDeclProps(VD, Unit, Line, T, Name, LinkageName, TemplateParameters, | |||
3967 | DContext); | |||
3968 | auto Align = getDeclAlignIfRequired(VD, CGM.getContext()); | |||
3969 | auto *GV = DBuilder.createTempGlobalVariableFwdDecl( | |||
3970 | DContext, Name, LinkageName, Unit, Line, getOrCreateType(T, Unit), | |||
3971 | !VD->isExternallyVisible(), nullptr, TemplateParameters, Align); | |||
3972 | FwdDeclReplaceMap.emplace_back( | |||
3973 | std::piecewise_construct, | |||
3974 | std::make_tuple(cast<VarDecl>(VD->getCanonicalDecl())), | |||
3975 | std::make_tuple(static_cast<llvm::Metadata *>(GV))); | |||
3976 | return GV; | |||
3977 | } | |||
3978 | ||||
3979 | llvm::DINode *CGDebugInfo::getDeclarationOrDefinition(const Decl *D) { | |||
3980 | // We only need a declaration (not a definition) of the type - so use whatever | |||
3981 | // we would otherwise do to get a type for a pointee. (forward declarations in | |||
3982 | // limited debug info, full definitions (if the type definition is available) | |||
3983 | // in unlimited debug info) | |||
3984 | if (const auto *TD = dyn_cast<TypeDecl>(D)) | |||
3985 | return getOrCreateType(CGM.getContext().getTypeDeclType(TD), | |||
3986 | getOrCreateFile(TD->getLocation())); | |||
3987 | auto I = DeclCache.find(D->getCanonicalDecl()); | |||
3988 | ||||
3989 | if (I != DeclCache.end()) { | |||
3990 | auto N = I->second; | |||
3991 | if (auto *GVE = dyn_cast_or_null<llvm::DIGlobalVariableExpression>(N)) | |||
3992 | return GVE->getVariable(); | |||
3993 | return cast<llvm::DINode>(N); | |||
3994 | } | |||
3995 | ||||
3996 | // Search imported declaration cache if it is already defined | |||
3997 | // as imported declaration. | |||
3998 | auto IE = ImportedDeclCache.find(D->getCanonicalDecl()); | |||
3999 | ||||
4000 | if (IE != ImportedDeclCache.end()) { | |||
4001 | auto N = IE->second; | |||
4002 | if (auto *GVE = dyn_cast_or_null<llvm::DIImportedEntity>(N)) | |||
4003 | return cast<llvm::DINode>(GVE); | |||
4004 | return dyn_cast_or_null<llvm::DINode>(N); | |||
4005 | } | |||
4006 | ||||
4007 | // No definition for now. Emit a forward definition that might be | |||
4008 | // merged with a potential upcoming definition. | |||
4009 | if (const auto *FD = dyn_cast<FunctionDecl>(D)) | |||
4010 | return getFunctionForwardDeclaration(FD); | |||
4011 | else if (const auto *VD = dyn_cast<VarDecl>(D)) | |||
4012 | return getGlobalVariableForwardDeclaration(VD); | |||
4013 | ||||
4014 | return nullptr; | |||
4015 | } | |||
4016 | ||||
4017 | llvm::DISubprogram *CGDebugInfo::getFunctionDeclaration(const Decl *D) { | |||
4018 | if (!D || DebugKind <= llvm::codegenoptions::DebugLineTablesOnly) | |||
4019 | return nullptr; | |||
4020 | ||||
4021 | const auto *FD = dyn_cast<FunctionDecl>(D); | |||
4022 | if (!FD) | |||
4023 | return nullptr; | |||
4024 | ||||
4025 | // Setup context. | |||
4026 | auto *S = getDeclContextDescriptor(D); | |||
4027 | ||||
4028 | auto MI = SPCache.find(FD->getCanonicalDecl()); | |||
4029 | if (MI == SPCache.end()) { | |||
4030 | if (const auto *MD = dyn_cast<CXXMethodDecl>(FD->getCanonicalDecl())) { | |||
4031 | return CreateCXXMemberFunction(MD, getOrCreateFile(MD->getLocation()), | |||
4032 | cast<llvm::DICompositeType>(S)); | |||
4033 | } | |||
4034 | } | |||
4035 | if (MI != SPCache.end()) { | |||
4036 | auto *SP = dyn_cast_or_null<llvm::DISubprogram>(MI->second); | |||
4037 | if (SP && !SP->isDefinition()) | |||
4038 | return SP; | |||
4039 | } | |||
4040 | ||||
4041 | for (auto *NextFD : FD->redecls()) { | |||
4042 | auto MI = SPCache.find(NextFD->getCanonicalDecl()); | |||
4043 | if (MI != SPCache.end()) { | |||
4044 | auto *SP = dyn_cast_or_null<llvm::DISubprogram>(MI->second); | |||
4045 | if (SP && !SP->isDefinition()) | |||
4046 | return SP; | |||
4047 | } | |||
4048 | } | |||
4049 | return nullptr; | |||
4050 | } | |||
4051 | ||||
4052 | llvm::DISubprogram *CGDebugInfo::getObjCMethodDeclaration( | |||
4053 | const Decl *D, llvm::DISubroutineType *FnType, unsigned LineNo, | |||
4054 | llvm::DINode::DIFlags Flags, llvm::DISubprogram::DISPFlags SPFlags) { | |||
4055 | if (!D || DebugKind <= llvm::codegenoptions::DebugLineTablesOnly) | |||
4056 | return nullptr; | |||
4057 | ||||
4058 | const auto *OMD = dyn_cast<ObjCMethodDecl>(D); | |||
4059 | if (!OMD) | |||
4060 | return nullptr; | |||
4061 | ||||
4062 | if (CGM.getCodeGenOpts().DwarfVersion < 5 && !OMD->isDirectMethod()) | |||
4063 | return nullptr; | |||
4064 | ||||
4065 | if (OMD->isDirectMethod()) | |||
4066 | SPFlags |= llvm::DISubprogram::SPFlagObjCDirect; | |||
4067 | ||||
4068 | // Starting with DWARF V5 method declarations are emitted as children of | |||
4069 | // the interface type. | |||
4070 | auto *ID = dyn_cast_or_null<ObjCInterfaceDecl>(D->getDeclContext()); | |||
4071 | if (!ID) | |||
4072 | ID = OMD->getClassInterface(); | |||
4073 | if (!ID) | |||
4074 | return nullptr; | |||
4075 | QualType QTy(ID->getTypeForDecl(), 0); | |||
4076 | auto It = TypeCache.find(QTy.getAsOpaquePtr()); | |||
4077 | if (It == TypeCache.end()) | |||
4078 | return nullptr; | |||
4079 | auto *InterfaceType = cast<llvm::DICompositeType>(It->second); | |||
4080 | llvm::DISubprogram *FD = DBuilder.createFunction( | |||
4081 | InterfaceType, getObjCMethodName(OMD), StringRef(), | |||
4082 | InterfaceType->getFile(), LineNo, FnType, LineNo, Flags, SPFlags); | |||
4083 | DBuilder.finalizeSubprogram(FD); | |||
4084 | ObjCMethodCache[ID].push_back({FD, OMD->isDirectMethod()}); | |||
4085 | return FD; | |||
4086 | } | |||
4087 | ||||
4088 | // getOrCreateFunctionType - Construct type. If it is a c++ method, include | |||
4089 | // implicit parameter "this". | |||
4090 | llvm::DISubroutineType *CGDebugInfo::getOrCreateFunctionType(const Decl *D, | |||
4091 | QualType FnType, | |||
4092 | llvm::DIFile *F) { | |||
4093 | // In CodeView, we emit the function types in line tables only because the | |||
4094 | // only way to distinguish between functions is by display name and type. | |||
4095 | if (!D || (DebugKind <= llvm::codegenoptions::DebugLineTablesOnly && | |||
4096 | !CGM.getCodeGenOpts().EmitCodeView)) | |||
4097 | // Create fake but valid subroutine type. Otherwise -verify would fail, and | |||
4098 | // subprogram DIE will miss DW_AT_decl_file and DW_AT_decl_line fields. | |||
4099 | return DBuilder.createSubroutineType( | |||
4100 | DBuilder.getOrCreateTypeArray(std::nullopt)); | |||
4101 | ||||
4102 | if (const auto *Method = dyn_cast<CXXMethodDecl>(D)) | |||
4103 | return getOrCreateMethodType(Method, F); | |||
4104 | ||||
4105 | const auto *FTy = FnType->getAs<FunctionType>(); | |||
4106 | CallingConv CC = FTy ? FTy->getCallConv() : CallingConv::CC_C; | |||
4107 | ||||
4108 | if (const auto *OMethod = dyn_cast<ObjCMethodDecl>(D)) { | |||
4109 | // Add "self" and "_cmd" | |||
4110 | SmallVector<llvm::Metadata *, 16> Elts; | |||
4111 | ||||
4112 | // First element is always return type. For 'void' functions it is NULL. | |||
4113 | QualType ResultTy = OMethod->getReturnType(); | |||
4114 | ||||
4115 | // Replace the instancetype keyword with the actual type. | |||
4116 | if (ResultTy == CGM.getContext().getObjCInstanceType()) | |||
4117 | ResultTy = CGM.getContext().getPointerType( | |||
4118 | QualType(OMethod->getClassInterface()->getTypeForDecl(), 0)); | |||
4119 | ||||
4120 | Elts.push_back(getOrCreateType(ResultTy, F)); | |||
4121 | // "self" pointer is always first argument. | |||
4122 | QualType SelfDeclTy; | |||
4123 | if (auto *SelfDecl = OMethod->getSelfDecl()) | |||
4124 | SelfDeclTy = SelfDecl->getType(); | |||
4125 | else if (auto *FPT = dyn_cast<FunctionProtoType>(FnType)) | |||
4126 | if (FPT->getNumParams() > 1) | |||
4127 | SelfDeclTy = FPT->getParamType(0); | |||
4128 | if (!SelfDeclTy.isNull()) | |||
4129 | Elts.push_back( | |||
4130 | CreateSelfType(SelfDeclTy, getOrCreateType(SelfDeclTy, F))); | |||
4131 | // "_cmd" pointer is always second argument. | |||
4132 | Elts.push_back(DBuilder.createArtificialType( | |||
4133 | getOrCreateType(CGM.getContext().getObjCSelType(), F))); | |||
4134 | // Get rest of the arguments. | |||
4135 | for (const auto *PI : OMethod->parameters()) | |||
4136 | Elts.push_back(getOrCreateType(PI->getType(), F)); | |||
4137 | // Variadic methods need a special marker at the end of the type list. | |||
4138 | if (OMethod->isVariadic()) | |||
4139 | Elts.push_back(DBuilder.createUnspecifiedParameter()); | |||
4140 | ||||
4141 | llvm::DITypeRefArray EltTypeArray = DBuilder.getOrCreateTypeArray(Elts); | |||
4142 | return DBuilder.createSubroutineType(EltTypeArray, llvm::DINode::FlagZero, | |||
4143 | getDwarfCC(CC)); | |||
4144 | } | |||
4145 | ||||
4146 | // Handle variadic function types; they need an additional | |||
4147 | // unspecified parameter. | |||
4148 | if (const auto *FD = dyn_cast<FunctionDecl>(D)) | |||
4149 | if (FD->isVariadic()) { | |||
4150 | SmallVector<llvm::Metadata *, 16> EltTys; | |||
4151 | EltTys.push_back(getOrCreateType(FD->getReturnType(), F)); | |||
4152 | if (const auto *FPT = dyn_cast<FunctionProtoType>(FnType)) | |||
4153 | for (QualType ParamType : FPT->param_types()) | |||
4154 | EltTys.push_back(getOrCreateType(ParamType, F)); | |||
4155 | EltTys.push_back(DBuilder.createUnspecifiedParameter()); | |||
4156 | llvm::DITypeRefArray EltTypeArray = DBuilder.getOrCreateTypeArray(EltTys); | |||
4157 | return DBuilder.createSubroutineType(EltTypeArray, llvm::DINode::FlagZero, | |||
4158 | getDwarfCC(CC)); | |||
4159 | } | |||
4160 | ||||
4161 | return cast<llvm::DISubroutineType>(getOrCreateType(FnType, F)); | |||
4162 | } | |||
4163 | ||||
4164 | QualType | |||
4165 | CGDebugInfo::getFunctionType(const FunctionDecl *FD, QualType RetTy, | |||
4166 | const SmallVectorImpl<const VarDecl *> &Args) { | |||
4167 | CallingConv CC = CallingConv::CC_C; | |||
4168 | if (FD) | |||
4169 | if (const auto *SrcFnTy = FD->getType()->getAs<FunctionType>()) | |||
4170 | CC = SrcFnTy->getCallConv(); | |||
4171 | SmallVector<QualType, 16> ArgTypes; | |||
4172 | for (const VarDecl *VD : Args) | |||
4173 | ArgTypes.push_back(VD->getType()); | |||
4174 | return CGM.getContext().getFunctionType(RetTy, ArgTypes, | |||
4175 | FunctionProtoType::ExtProtoInfo(CC)); | |||
4176 | } | |||
4177 | ||||
4178 | void CGDebugInfo::emitFunctionStart(GlobalDecl GD, SourceLocation Loc, | |||
4179 | SourceLocation ScopeLoc, QualType FnType, | |||
4180 | llvm::Function *Fn, bool CurFuncIsThunk) { | |||
4181 | StringRef Name; | |||
4182 | StringRef LinkageName; | |||
4183 | ||||
4184 | FnBeginRegionCount.push_back(LexicalBlockStack.size()); | |||
4185 | ||||
4186 | const Decl *D = GD.getDecl(); | |||
4187 | bool HasDecl = (D != nullptr); | |||
4188 | ||||
4189 | llvm::DINode::DIFlags Flags = llvm::DINode::FlagZero; | |||
4190 | llvm::DISubprogram::DISPFlags SPFlags = llvm::DISubprogram::SPFlagZero; | |||
4191 | llvm::DIFile *Unit = getOrCreateFile(Loc); | |||
4192 | llvm::DIScope *FDContext = Unit; | |||
4193 | llvm::DINodeArray TParamsArray; | |||
4194 | if (!HasDecl) { | |||
4195 | // Use llvm function name. | |||
4196 | LinkageName = Fn->getName(); | |||
4197 | } else if (const auto *FD = dyn_cast<FunctionDecl>(D)) { | |||
4198 | // If there is a subprogram for this function available then use it. | |||
4199 | auto FI = SPCache.find(FD->getCanonicalDecl()); | |||
4200 | if (FI != SPCache.end()) { | |||
4201 | auto *SP = dyn_cast_or_null<llvm::DISubprogram>(FI->second); | |||
4202 | if (SP && SP->isDefinition()) { | |||
4203 | LexicalBlockStack.emplace_back(SP); | |||
4204 | RegionMap[D].reset(SP); | |||
4205 | return; | |||
4206 | } | |||
4207 | } | |||
4208 | collectFunctionDeclProps(GD, Unit, Name, LinkageName, FDContext, | |||
4209 | TParamsArray, Flags); | |||
4210 | } else if (const auto *OMD = dyn_cast<ObjCMethodDecl>(D)) { | |||
4211 | Name = getObjCMethodName(OMD); | |||
4212 | Flags |= llvm::DINode::FlagPrototyped; | |||
4213 | } else if (isa<VarDecl>(D) && | |||
4214 | GD.getDynamicInitKind() != DynamicInitKind::NoStub) { | |||
4215 | // This is a global initializer or atexit destructor for a global variable. | |||
4216 | Name = getDynamicInitializerName(cast<VarDecl>(D), GD.getDynamicInitKind(), | |||
4217 | Fn); | |||
4218 | } else { | |||
4219 | Name = Fn->getName(); | |||
4220 | ||||
4221 | if (isa<BlockDecl>(D)) | |||
4222 | LinkageName = Name; | |||
4223 | ||||
4224 | Flags |= llvm::DINode::FlagPrototyped; | |||
4225 | } | |||
4226 | if (Name.startswith("\01")) | |||
4227 | Name = Name.substr(1); | |||
4228 | ||||
4229 | assert((!D || !isa<VarDecl>(D) ||(static_cast <bool> ((!D || !isa<VarDecl>(D) || GD .getDynamicInitKind() != DynamicInitKind::NoStub) && "Unexpected DynamicInitKind !" ) ? void (0) : __assert_fail ("(!D || !isa<VarDecl>(D) || GD.getDynamicInitKind() != DynamicInitKind::NoStub) && \"Unexpected DynamicInitKind !\"" , "clang/lib/CodeGen/CGDebugInfo.cpp", 4231, __extension__ __PRETTY_FUNCTION__ )) | |||
4230 | GD.getDynamicInitKind() != DynamicInitKind::NoStub) &&(static_cast <bool> ((!D || !isa<VarDecl>(D) || GD .getDynamicInitKind() != DynamicInitKind::NoStub) && "Unexpected DynamicInitKind !" ) ? void (0) : __assert_fail ("(!D || !isa<VarDecl>(D) || GD.getDynamicInitKind() != DynamicInitKind::NoStub) && \"Unexpected DynamicInitKind !\"" , "clang/lib/CodeGen/CGDebugInfo.cpp", 4231, __extension__ __PRETTY_FUNCTION__ )) | |||
4231 | "Unexpected DynamicInitKind !")(static_cast <bool> ((!D || !isa<VarDecl>(D) || GD .getDynamicInitKind() != DynamicInitKind::NoStub) && "Unexpected DynamicInitKind !" ) ? void (0) : __assert_fail ("(!D || !isa<VarDecl>(D) || GD.getDynamicInitKind() != DynamicInitKind::NoStub) && \"Unexpected DynamicInitKind !\"" , "clang/lib/CodeGen/CGDebugInfo.cpp", 4231, __extension__ __PRETTY_FUNCTION__ )); | |||
4232 | ||||
4233 | if (!HasDecl || D->isImplicit() || D->hasAttr<ArtificialAttr>() || | |||
4234 | isa<VarDecl>(D) || isa<CapturedDecl>(D)) { | |||
4235 | Flags |= llvm::DINode::FlagArtificial; | |||
4236 | // Artificial functions should not silently reuse CurLoc. | |||
4237 | CurLoc = SourceLocation(); | |||
4238 | } | |||
4239 | ||||
4240 | if (CurFuncIsThunk) | |||
4241 | Flags |= llvm::DINode::FlagThunk; | |||
4242 | ||||
4243 | if (Fn->hasLocalLinkage()) | |||
4244 | SPFlags |= llvm::DISubprogram::SPFlagLocalToUnit; | |||
4245 | if (CGM.getLangOpts().Optimize) | |||
4246 | SPFlags |= llvm::DISubprogram::SPFlagOptimized; | |||
4247 | ||||
4248 | llvm::DINode::DIFlags FlagsForDef = Flags | getCallSiteRelatedAttrs(); | |||
4249 | llvm::DISubprogram::DISPFlags SPFlagsForDef = | |||
4250 | SPFlags | llvm::DISubprogram::SPFlagDefinition; | |||
4251 | ||||
4252 | const unsigned LineNo = getLineNumber(Loc.isValid() ? Loc : CurLoc); | |||
4253 | unsigned ScopeLine = getLineNumber(ScopeLoc); | |||
4254 | llvm::DISubroutineType *DIFnType = getOrCreateFunctionType(D, FnType, Unit); | |||
4255 | llvm::DISubprogram *Decl = nullptr; | |||
4256 | llvm::DINodeArray Annotations = nullptr; | |||
4257 | if (D) { | |||
4258 | Decl = isa<ObjCMethodDecl>(D) | |||
4259 | ? getObjCMethodDeclaration(D, DIFnType, LineNo, Flags, SPFlags) | |||
4260 | : getFunctionDeclaration(D); | |||
4261 | Annotations = CollectBTFDeclTagAnnotations(D); | |||
4262 | } | |||
4263 | ||||
4264 | // FIXME: The function declaration we're constructing here is mostly reusing | |||
4265 | // declarations from CXXMethodDecl and not constructing new ones for arbitrary | |||
4266 | // FunctionDecls. When/if we fix this we can have FDContext be TheCU/null for | |||
4267 | // all subprograms instead of the actual context since subprogram definitions | |||
4268 | // are emitted as CU level entities by the backend. | |||
4269 | llvm::DISubprogram *SP = DBuilder.createFunction( | |||
4270 | FDContext, Name, LinkageName, Unit, LineNo, DIFnType, ScopeLine, | |||
4271 | FlagsForDef, SPFlagsForDef, TParamsArray.get(), Decl, nullptr, | |||
4272 | Annotations); | |||
4273 | Fn->setSubprogram(SP); | |||
4274 | // We might get here with a VarDecl in the case we're generating | |||
4275 | // code for the initialization of globals. Do not record these decls | |||
4276 | // as they will overwrite the actual VarDecl Decl in the cache. | |||
4277 | if (HasDecl && isa<FunctionDecl>(D)) | |||
4278 | DeclCache[D->getCanonicalDecl()].reset(SP); | |||
4279 | ||||
4280 | // Push the function onto the lexical block stack. | |||
4281 | LexicalBlockStack.emplace_back(SP); | |||
4282 | ||||
4283 | if (HasDecl) | |||
4284 | RegionMap[D].reset(SP); | |||
4285 | } | |||
4286 | ||||
4287 | void CGDebugInfo::EmitFunctionDecl(GlobalDecl GD, SourceLocation Loc, | |||
4288 | QualType FnType, llvm::Function *Fn) { | |||
4289 | StringRef Name; | |||
4290 | StringRef LinkageName; | |||
4291 | ||||
4292 | const Decl *D = GD.getDecl(); | |||
4293 | if (!D) | |||
4294 | return; | |||
4295 | ||||
4296 | llvm::TimeTraceScope TimeScope("DebugFunction", [&]() { | |||
4297 | return GetName(D, true); | |||
4298 | }); | |||
4299 | ||||
4300 | llvm::DINode::DIFlags Flags = llvm::DINode::FlagZero; | |||
4301 | llvm::DIFile *Unit = getOrCreateFile(Loc); | |||
4302 | bool IsDeclForCallSite = Fn ? true : false; | |||
4303 | llvm::DIScope *FDContext = | |||
4304 | IsDeclForCallSite ? Unit : getDeclContextDescriptor(D); | |||
4305 | llvm::DINodeArray TParamsArray; | |||
4306 | if (isa<FunctionDecl>(D)) { | |||
4307 | // If there is a DISubprogram for this function available then use it. | |||
4308 | collectFunctionDeclProps(GD, Unit, Name, LinkageName, FDContext, | |||
4309 | TParamsArray, Flags); | |||
4310 | } else if (const auto *OMD = dyn_cast<ObjCMethodDecl>(D)) { | |||
4311 | Name = getObjCMethodName(OMD); | |||
4312 | Flags |= llvm::DINode::FlagPrototyped; | |||
4313 | } else { | |||
4314 | llvm_unreachable("not a function or ObjC method")::llvm::llvm_unreachable_internal("not a function or ObjC method" , "clang/lib/CodeGen/CGDebugInfo.cpp", 4314); | |||
4315 | } | |||
4316 | if (!Name.empty() && Name[0] == '\01') | |||
4317 | Name = Name.substr(1); | |||
4318 | ||||
4319 | if (D->isImplicit()) { | |||
4320 | Flags |= llvm::DINode::FlagArtificial; | |||
4321 | // Artificial functions without a location should not silently reuse CurLoc. | |||
4322 | if (Loc.isInvalid()) | |||
4323 | CurLoc = SourceLocation(); | |||
4324 | } | |||
4325 | unsigned LineNo = getLineNumber(Loc); | |||
4326 | unsigned ScopeLine = 0; | |||
4327 | llvm::DISubprogram::DISPFlags SPFlags = llvm::DISubprogram::SPFlagZero; | |||
4328 | if (CGM.getLangOpts().Optimize) | |||
4329 | SPFlags |= llvm::DISubprogram::SPFlagOptimized; | |||
4330 | ||||
4331 | llvm::DINodeArray Annotations = CollectBTFDeclTagAnnotations(D); | |||
4332 | llvm::DISubroutineType *STy = getOrCreateFunctionType(D, FnType, Unit); | |||
4333 | llvm::DISubprogram *SP = DBuilder.createFunction( | |||
4334 | FDContext, Name, LinkageName, Unit, LineNo, STy, ScopeLine, Flags, | |||
4335 | SPFlags, TParamsArray.get(), nullptr, nullptr, Annotations); | |||
4336 | ||||
4337 | // Preserve btf_decl_tag attributes for parameters of extern functions | |||
4338 | // for BPF target. The parameters created in this loop are attached as | |||
4339 | // DISubprogram's retainedNodes in the subsequent finalizeSubprogram call. | |||
4340 | if (IsDeclForCallSite && CGM.getTarget().getTriple().isBPF()) { | |||
4341 | if (auto *FD = dyn_cast<FunctionDecl>(D)) { | |||
4342 | llvm::DITypeRefArray ParamTypes = STy->getTypeArray(); | |||
4343 | unsigned ArgNo = 1; | |||
4344 | for (ParmVarDecl *PD : FD->parameters()) { | |||
4345 | llvm::DINodeArray ParamAnnotations = CollectBTFDeclTagAnnotations(PD); | |||
4346 | DBuilder.createParameterVariable( | |||
4347 | SP, PD->getName(), ArgNo, Unit, LineNo, ParamTypes[ArgNo], true, | |||
4348 | llvm::DINode::FlagZero, ParamAnnotations); | |||
4349 | ++ArgNo; | |||
4350 | } | |||
4351 | } | |||
4352 | } | |||
4353 | ||||
4354 | if (IsDeclForCallSite) | |||
4355 | Fn->setSubprogram(SP); | |||
4356 | ||||
4357 | DBuilder.finalizeSubprogram(SP); | |||
4358 | } | |||
4359 | ||||
4360 | void CGDebugInfo::EmitFuncDeclForCallSite(llvm::CallBase *CallOrInvoke, | |||
4361 | QualType CalleeType, | |||
4362 | const FunctionDecl *CalleeDecl) { | |||
4363 | if (!CallOrInvoke) | |||
4364 | return; | |||
4365 | auto *Func = CallOrInvoke->getCalledFunction(); | |||
4366 | if (!Func) | |||
4367 | return; | |||
4368 | if (Func->getSubprogram()) | |||
4369 | return; | |||
4370 | ||||
4371 | // Do not emit a declaration subprogram for a function with nodebug | |||
4372 | // attribute, or if call site info isn't required. | |||
4373 | if (CalleeDecl->hasAttr<NoDebugAttr>() || | |||
4374 | getCallSiteRelatedAttrs() == llvm::DINode::FlagZero) | |||
4375 | return; | |||
4376 | ||||
4377 | // If there is no DISubprogram attached to the function being called, | |||
4378 | // create the one describing the function in order to have complete | |||
4379 | // call site debug info. | |||
4380 | if (!CalleeDecl->isStatic() && !CalleeDecl->isInlined()) | |||
4381 | EmitFunctionDecl(CalleeDecl, CalleeDecl->getLocation(), CalleeType, Func); | |||
4382 | } | |||
4383 | ||||
4384 | void CGDebugInfo::EmitInlineFunctionStart(CGBuilderTy &Builder, GlobalDecl GD) { | |||
4385 | const auto *FD = cast<FunctionDecl>(GD.getDecl()); | |||
4386 | // If there is a subprogram for this function available then use it. | |||
4387 | auto FI = SPCache.find(FD->getCanonicalDecl()); | |||
4388 | llvm::DISubprogram *SP = nullptr; | |||
4389 | if (FI != SPCache.end()) | |||
4390 | SP = dyn_cast_or_null<llvm::DISubprogram>(FI->second); | |||
4391 | if (!SP || !SP->isDefinition()) | |||
4392 | SP = getFunctionStub(GD); | |||
4393 | FnBeginRegionCount.push_back(LexicalBlockStack.size()); | |||
4394 | LexicalBlockStack.emplace_back(SP); | |||
4395 | setInlinedAt(Builder.getCurrentDebugLocation()); | |||
4396 | EmitLocation(Builder, FD->getLocation()); | |||
4397 | } | |||
4398 | ||||
4399 | void CGDebugInfo::EmitInlineFunctionEnd(CGBuilderTy &Builder) { | |||
4400 | assert(CurInlinedAt && "unbalanced inline scope stack")(static_cast <bool> (CurInlinedAt && "unbalanced inline scope stack" ) ? void (0) : __assert_fail ("CurInlinedAt && \"unbalanced inline scope stack\"" , "clang/lib/CodeGen/CGDebugInfo.cpp", 4400, __extension__ __PRETTY_FUNCTION__ )); | |||
4401 | EmitFunctionEnd(Builder, nullptr); | |||
4402 | setInlinedAt(llvm::DebugLoc(CurInlinedAt).getInlinedAt()); | |||
4403 | } | |||
4404 | ||||
4405 | void CGDebugInfo::EmitLocation(CGBuilderTy &Builder, SourceLocation Loc) { | |||
4406 | // Update our current location | |||
4407 | setLocation(Loc); | |||
4408 | ||||
4409 | if (CurLoc.isInvalid() || CurLoc.isMacroID() || LexicalBlockStack.empty()) | |||
4410 | return; | |||
4411 | ||||
4412 | llvm::MDNode *Scope = LexicalBlockStack.back(); | |||
4413 | Builder.SetCurrentDebugLocation( | |||
4414 | llvm::DILocation::get(CGM.getLLVMContext(), getLineNumber(CurLoc), | |||
4415 | getColumnNumber(CurLoc), Scope, CurInlinedAt)); | |||
4416 | } | |||
4417 | ||||
4418 | void CGDebugInfo::CreateLexicalBlock(SourceLocation Loc) { | |||
4419 | llvm::MDNode *Back = nullptr; | |||
4420 | if (!LexicalBlockStack.empty()) | |||
4421 | Back = LexicalBlockStack.back().get(); | |||
4422 | LexicalBlockStack.emplace_back(DBuilder.createLexicalBlock( | |||
4423 | cast<llvm::DIScope>(Back), getOrCreateFile(CurLoc), getLineNumber(CurLoc), | |||
4424 | getColumnNumber(CurLoc))); | |||
4425 | } | |||
4426 | ||||
4427 | void CGDebugInfo::AppendAddressSpaceXDeref( | |||
4428 | unsigned AddressSpace, SmallVectorImpl<uint64_t> &Expr) const { | |||
4429 | std::optional<unsigned> DWARFAddressSpace = | |||
4430 | CGM.getTarget().getDWARFAddressSpace(AddressSpace); | |||
4431 | if (!DWARFAddressSpace) | |||
4432 | return; | |||
4433 | ||||
4434 | Expr.push_back(llvm::dwarf::DW_OP_constu); | |||
4435 | Expr.push_back(*DWARFAddressSpace); | |||
4436 | Expr.push_back(llvm::dwarf::DW_OP_swap); | |||
4437 | Expr.push_back(llvm::dwarf::DW_OP_xderef); | |||
4438 | } | |||
4439 | ||||
4440 | void CGDebugInfo::EmitLexicalBlockStart(CGBuilderTy &Builder, | |||
4441 | SourceLocation Loc) { | |||
4442 | // Set our current location. | |||
4443 | setLocation(Loc); | |||
4444 | ||||
4445 | // Emit a line table change for the current location inside the new scope. | |||
4446 | Builder.SetCurrentDebugLocation(llvm::DILocation::get( | |||
4447 | CGM.getLLVMContext(), getLineNumber(Loc), getColumnNumber(Loc), | |||
4448 | LexicalBlockStack.back(), CurInlinedAt)); | |||
4449 | ||||
4450 | if (DebugKind <= llvm::codegenoptions::DebugLineTablesOnly) | |||
4451 | return; | |||
4452 | ||||
4453 | // Create a new lexical block and push it on the stack. | |||
4454 | CreateLexicalBlock(Loc); | |||
4455 | } | |||
4456 | ||||
4457 | void CGDebugInfo::EmitLexicalBlockEnd(CGBuilderTy &Builder, | |||
4458 | SourceLocation Loc) { | |||
4459 | assert(!LexicalBlockStack.empty() && "Region stack mismatch, stack empty!")(static_cast <bool> (!LexicalBlockStack.empty() && "Region stack mismatch, stack empty!") ? void (0) : __assert_fail ("!LexicalBlockStack.empty() && \"Region stack mismatch, stack empty!\"" , "clang/lib/CodeGen/CGDebugInfo.cpp", 4459, __extension__ __PRETTY_FUNCTION__ )); | |||
4460 | ||||
4461 | // Provide an entry in the line table for the end of the block. | |||
4462 | EmitLocation(Builder, Loc); | |||
4463 | ||||
4464 | if (DebugKind <= llvm::codegenoptions::DebugLineTablesOnly) | |||
4465 | return; | |||
4466 | ||||
4467 | LexicalBlockStack.pop_back(); | |||
4468 | } | |||
4469 | ||||
4470 | void CGDebugInfo::EmitFunctionEnd(CGBuilderTy &Builder, llvm::Function *Fn) { | |||
4471 | assert(!LexicalBlockStack.empty() && "Region stack mismatch, stack empty!")(static_cast <bool> (!LexicalBlockStack.empty() && "Region stack mismatch, stack empty!") ? void (0) : __assert_fail ("!LexicalBlockStack.empty() && \"Region stack mismatch, stack empty!\"" , "clang/lib/CodeGen/CGDebugInfo.cpp", 4471, __extension__ __PRETTY_FUNCTION__ )); | |||
4472 | unsigned RCount = FnBeginRegionCount.back(); | |||
4473 | assert(RCount <= LexicalBlockStack.size() && "Region stack mismatch")(static_cast <bool> (RCount <= LexicalBlockStack.size () && "Region stack mismatch") ? void (0) : __assert_fail ("RCount <= LexicalBlockStack.size() && \"Region stack mismatch\"" , "clang/lib/CodeGen/CGDebugInfo.cpp", 4473, __extension__ __PRETTY_FUNCTION__ )); | |||
4474 | ||||
4475 | // Pop all regions for this function. | |||
4476 | while (LexicalBlockStack.size() != RCount) { | |||
4477 | // Provide an entry in the line table for the end of the block. | |||
4478 | EmitLocation(Builder, CurLoc); | |||
4479 | LexicalBlockStack.pop_back(); | |||
4480 | } | |||
4481 | FnBeginRegionCount.pop_back(); | |||
4482 | ||||
4483 | if (Fn && Fn->getSubprogram()) | |||
4484 | DBuilder.finalizeSubprogram(Fn->getSubprogram()); | |||
4485 | } | |||
4486 | ||||
4487 | CGDebugInfo::BlockByRefType | |||
4488 | CGDebugInfo::EmitTypeForVarWithBlocksAttr(const VarDecl *VD, | |||
4489 | uint64_t *XOffset) { | |||
4490 | SmallVector<llvm::Metadata *, 5> EltTys; | |||
4491 | QualType FType; | |||
4492 | uint64_t FieldSize, FieldOffset; | |||
4493 | uint32_t FieldAlign; | |||
4494 | ||||
4495 | llvm::DIFile *Unit = getOrCreateFile(VD->getLocation()); | |||
4496 | QualType Type = VD->getType(); | |||
4497 | ||||
4498 | FieldOffset = 0; | |||
4499 | FType = CGM.getContext().getPointerType(CGM.getContext().VoidTy); | |||
4500 | EltTys.push_back(CreateMemberType(Unit, FType, "__isa", &FieldOffset)); | |||
4501 | EltTys.push_back(CreateMemberType(Unit, FType, "__forwarding", &FieldOffset)); | |||
4502 | FType = CGM.getContext().IntTy; | |||
4503 | EltTys.push_back(CreateMemberType(Unit, FType, "__flags", &FieldOffset)); | |||
4504 | EltTys.push_back(CreateMemberType(Unit, FType, "__size", &FieldOffset)); | |||
4505 | ||||
4506 | bool HasCopyAndDispose = CGM.getContext().BlockRequiresCopying(Type, VD); | |||
4507 | if (HasCopyAndDispose) { | |||
4508 | FType = CGM.getContext().getPointerType(CGM.getContext().VoidTy); | |||
4509 | EltTys.push_back( | |||
4510 | CreateMemberType(Unit, FType, "__copy_helper", &FieldOffset)); | |||
4511 | EltTys.push_back( | |||
4512 | CreateMemberType(Unit, FType, "__destroy_helper", &FieldOffset)); | |||
4513 | } | |||
4514 | bool HasByrefExtendedLayout; | |||
4515 | Qualifiers::ObjCLifetime Lifetime; | |||
4516 | if (CGM.getContext().getByrefLifetime(Type, Lifetime, | |||
4517 | HasByrefExtendedLayout) && | |||
4518 | HasByrefExtendedLayout) { | |||
4519 | FType = CGM.getContext().getPointerType(CGM.getContext().VoidTy); | |||
4520 | EltTys.push_back( | |||
4521 | CreateMemberType(Unit, FType, "__byref_variable_layout", &FieldOffset)); | |||
4522 | } | |||
4523 | ||||
4524 | CharUnits Align = CGM.getContext().getDeclAlign(VD); | |||
4525 | if (Align > CGM.getContext().toCharUnitsFromBits( | |||
4526 | CGM.getTarget().getPointerAlign(LangAS::Default))) { | |||
4527 | CharUnits FieldOffsetInBytes = | |||
4528 | CGM.getContext().toCharUnitsFromBits(FieldOffset); | |||
4529 | CharUnits AlignedOffsetInBytes = FieldOffsetInBytes.alignTo(Align); | |||
4530 | CharUnits NumPaddingBytes = AlignedOffsetInBytes - FieldOffsetInBytes; | |||
4531 | ||||
4532 | if (NumPaddingBytes.isPositive()) { | |||
4533 | llvm::APInt pad(32, NumPaddingBytes.getQuantity()); | |||
4534 | FType = CGM.getContext().getConstantArrayType( | |||
4535 | CGM.getContext().CharTy, pad, nullptr, ArrayType::Normal, 0); | |||
4536 | EltTys.push_back(CreateMemberType(Unit, FType, "", &FieldOffset)); | |||
4537 | } | |||
4538 | } | |||
4539 | ||||
4540 | FType = Type; | |||
4541 | llvm::DIType *WrappedTy = getOrCreateType(FType, Unit); | |||
4542 | FieldSize = CGM.getContext().getTypeSize(FType); | |||
4543 | FieldAlign = CGM.getContext().toBits(Align); | |||
4544 | ||||
4545 | *XOffset = FieldOffset; | |||
4546 | llvm::DIType *FieldTy = DBuilder.createMemberType( | |||
4547 | Unit, VD->getName(), Unit, 0, FieldSize, FieldAlign, FieldOffset, | |||
4548 | llvm::DINode::FlagZero, WrappedTy); | |||
4549 | EltTys.push_back(FieldTy); | |||
4550 | FieldOffset += FieldSize; | |||
4551 | ||||
4552 | llvm::DINodeArray Elements = DBuilder.getOrCreateArray(EltTys); | |||
4553 | return {DBuilder.createStructType(Unit, "", Unit, 0, FieldOffset, 0, | |||
4554 | llvm::DINode::FlagZero, nullptr, Elements), | |||
4555 | WrappedTy}; | |||
4556 | } | |||
4557 | ||||
4558 | llvm::DILocalVariable *CGDebugInfo::EmitDeclare(const VarDecl *VD, | |||
4559 | llvm::Value *Storage, | |||
4560 | std::optional<unsigned> ArgNo, | |||
4561 | CGBuilderTy &Builder, | |||
4562 | const bool UsePointerValue) { | |||
4563 | assert(CGM.getCodeGenOpts().hasReducedDebugInfo())(static_cast <bool> (CGM.getCodeGenOpts().hasReducedDebugInfo ()) ? void (0) : __assert_fail ("CGM.getCodeGenOpts().hasReducedDebugInfo()" , "clang/lib/CodeGen/CGDebugInfo.cpp", 4563, __extension__ __PRETTY_FUNCTION__ )); | |||
4564 | assert(!LexicalBlockStack.empty() && "Region stack mismatch, stack empty!")(static_cast <bool> (!LexicalBlockStack.empty() && "Region stack mismatch, stack empty!") ? void (0) : __assert_fail ("!LexicalBlockStack.empty() && \"Region stack mismatch, stack empty!\"" , "clang/lib/CodeGen/CGDebugInfo.cpp", 4564, __extension__ __PRETTY_FUNCTION__ )); | |||
4565 | if (VD->hasAttr<NoDebugAttr>()) | |||
4566 | return nullptr; | |||
4567 | ||||
4568 | bool Unwritten = | |||
4569 | VD->isImplicit() || (isa<Decl>(VD->getDeclContext()) && | |||
4570 | cast<Decl>(VD->getDeclContext())->isImplicit()); | |||
4571 | llvm::DIFile *Unit = nullptr; | |||
4572 | if (!Unwritten) | |||
4573 | Unit = getOrCreateFile(VD->getLocation()); | |||
4574 | llvm::DIType *Ty; | |||
4575 | uint64_t XOffset = 0; | |||
4576 | if (VD->hasAttr<BlocksAttr>()) | |||
4577 | Ty = EmitTypeForVarWithBlocksAttr(VD, &XOffset).WrappedType; | |||
4578 | else | |||
4579 | Ty = getOrCreateType(VD->getType(), Unit); | |||
4580 | ||||
4581 | // If there is no debug info for this type then do not emit debug info | |||
4582 | // for this variable. | |||
4583 | if (!Ty) | |||
4584 | return nullptr; | |||
4585 | ||||
4586 | // Get location information. | |||
4587 | unsigned Line = 0; | |||
4588 | unsigned Column = 0; | |||
4589 | if (!Unwritten) { | |||
4590 | Line = getLineNumber(VD->getLocation()); | |||
4591 | Column = getColumnNumber(VD->getLocation()); | |||
4592 | } | |||
4593 | SmallVector<uint64_t, 13> Expr; | |||
4594 | llvm::DINode::DIFlags Flags = llvm::DINode::FlagZero; | |||
4595 | if (VD->isImplicit()) | |||
4596 | Flags |= llvm::DINode::FlagArtificial; | |||
4597 | ||||
4598 | auto Align = getDeclAlignIfRequired(VD, CGM.getContext()); | |||
4599 | ||||
4600 | unsigned AddressSpace = CGM.getTypes().getTargetAddressSpace(VD->getType()); | |||
4601 | AppendAddressSpaceXDeref(AddressSpace, Expr); | |||
4602 | ||||
4603 | // If this is implicit parameter of CXXThis or ObjCSelf kind, then give it an | |||
4604 | // object pointer flag. | |||
4605 | if (const auto *IPD = dyn_cast<ImplicitParamDecl>(VD)) { | |||
4606 | if (IPD->getParameterKind() == ImplicitParamDecl::CXXThis || | |||
4607 | IPD->getParameterKind() == ImplicitParamDecl::ObjCSelf) | |||
4608 | Flags |= llvm::DINode::FlagObjectPointer; | |||
4609 | } | |||
4610 | ||||
4611 | // Note: Older versions of clang used to emit byval references with an extra | |||
4612 | // DW_OP_deref, because they referenced the IR arg directly instead of | |||
4613 | // referencing an alloca. Newer versions of LLVM don't treat allocas | |||
4614 | // differently from other function arguments when used in a dbg.declare. | |||
4615 | auto *Scope = cast<llvm::DIScope>(LexicalBlockStack.back()); | |||
4616 | StringRef Name = VD->getName(); | |||
4617 | if (!Name.empty()) { | |||
4618 | // __block vars are stored on the heap if they are captured by a block that | |||
4619 | // can escape the local scope. | |||
4620 | if (VD->isEscapingByref()) { | |||
4621 | // Here, we need an offset *into* the alloca. | |||
4622 | CharUnits offset = CharUnits::fromQuantity(32); | |||
4623 | Expr.push_back(llvm::dwarf::DW_OP_plus_uconst); | |||
4624 | // offset of __forwarding field | |||
4625 | offset = CGM.getContext().toCharUnitsFromBits( | |||
4626 | CGM.getTarget().getPointerWidth(LangAS::Default)); | |||
4627 | Expr.push_back(offset.getQuantity()); | |||
4628 | Expr.push_back(llvm::dwarf::DW_OP_deref); | |||
4629 | Expr.push_back(llvm::dwarf::DW_OP_plus_uconst); | |||
4630 | // offset of x field | |||
4631 | offset = CGM.getContext().toCharUnitsFromBits(XOffset); | |||
4632 | Expr.push_back(offset.getQuantity()); | |||
4633 | } | |||
4634 | } else if (const auto *RT = dyn_cast<RecordType>(VD->getType())) { | |||
4635 | // If VD is an anonymous union then Storage represents value for | |||
4636 | // all union fields. | |||
4637 | const RecordDecl *RD = RT->getDecl(); | |||
4638 | if (RD->isUnion() && RD->isAnonymousStructOrUnion()) { | |||
4639 | // GDB has trouble finding local variables in anonymous unions, so we emit | |||
4640 | // artificial local variables for each of the members. | |||
4641 | // | |||
4642 | // FIXME: Remove this code as soon as GDB supports this. | |||
4643 | // The debug info verifier in LLVM operates based on the assumption that a | |||
4644 | // variable has the same size as its storage and we had to disable the | |||
4645 | // check for artificial variables. | |||
4646 | for (const auto *Field : RD->fields()) { | |||
4647 | llvm::DIType *FieldTy = getOrCreateType(Field->getType(), Unit); | |||
4648 | StringRef FieldName = Field->getName(); | |||
4649 | ||||
4650 | // Ignore unnamed fields. Do not ignore unnamed records. | |||
4651 | if (FieldName.empty() && !isa<RecordType>(Field->getType())) | |||
4652 | continue; | |||
4653 | ||||
4654 | // Use VarDecl's Tag, Scope and Line number. | |||
4655 | auto FieldAlign = getDeclAlignIfRequired(Field, CGM.getContext()); | |||
4656 | auto *D = DBuilder.createAutoVariable( | |||
4657 | Scope, FieldName, Unit, Line, FieldTy, CGM.getLangOpts().Optimize, | |||
4658 | Flags | llvm::DINode::FlagArtificial, FieldAlign); | |||
4659 | ||||
4660 | // Insert an llvm.dbg.declare into the current block. | |||
4661 | DBuilder.insertDeclare(Storage, D, DBuilder.createExpression(Expr), | |||
4662 | llvm::DILocation::get(CGM.getLLVMContext(), Line, | |||
4663 | Column, Scope, | |||
4664 | CurInlinedAt), | |||
4665 | Builder.GetInsertBlock()); | |||
4666 | } | |||
4667 | } | |||
4668 | } | |||
4669 | ||||
4670 | // Clang stores the sret pointer provided by the caller in a static alloca. | |||
4671 | // Use DW_OP_deref to tell the debugger to load the pointer and treat it as | |||
4672 | // the address of the variable. | |||
4673 | if (UsePointerValue) { | |||
4674 | assert(!llvm::is_contained(Expr, llvm::dwarf::DW_OP_deref) &&(static_cast <bool> (!llvm::is_contained(Expr, llvm::dwarf ::DW_OP_deref) && "Debug info already contains DW_OP_deref." ) ? void (0) : __assert_fail ("!llvm::is_contained(Expr, llvm::dwarf::DW_OP_deref) && \"Debug info already contains DW_OP_deref.\"" , "clang/lib/CodeGen/CGDebugInfo.cpp", 4675, __extension__ __PRETTY_FUNCTION__ )) | |||
4675 | "Debug info already contains DW_OP_deref.")(static_cast <bool> (!llvm::is_contained(Expr, llvm::dwarf ::DW_OP_deref) && "Debug info already contains DW_OP_deref." ) ? void (0) : __assert_fail ("!llvm::is_contained(Expr, llvm::dwarf::DW_OP_deref) && \"Debug info already contains DW_OP_deref.\"" , "clang/lib/CodeGen/CGDebugInfo.cpp", 4675, __extension__ __PRETTY_FUNCTION__ )); | |||
4676 | Expr.push_back(llvm::dwarf::DW_OP_deref); | |||
4677 | } | |||
4678 | ||||
4679 | // Create the descriptor for the variable. | |||
4680 | llvm::DILocalVariable *D = nullptr; | |||
4681 | if (ArgNo) { | |||
4682 | llvm::DINodeArray Annotations = CollectBTFDeclTagAnnotations(VD); | |||
4683 | D = DBuilder.createParameterVariable(Scope, Name, *ArgNo, Unit, Line, Ty, | |||
4684 | CGM.getLangOpts().Optimize, Flags, | |||
4685 | Annotations); | |||
4686 | } else { | |||
4687 | // For normal local variable, we will try to find out whether 'VD' is the | |||
4688 | // copy parameter of coroutine. | |||
4689 | // If yes, we are going to use DIVariable of the origin parameter instead | |||
4690 | // of creating the new one. | |||
4691 | // If no, it might be a normal alloc, we just create a new one for it. | |||
4692 | ||||
4693 | // Check whether the VD is move parameters. | |||
4694 | auto RemapCoroArgToLocalVar = [&]() -> llvm::DILocalVariable * { | |||
4695 | // The scope of parameter and move-parameter should be distinct | |||
4696 | // DISubprogram. | |||
4697 | if (!isa<llvm::DISubprogram>(Scope) || !Scope->isDistinct()) | |||
4698 | return nullptr; | |||
4699 | ||||
4700 | auto Iter = llvm::find_if(CoroutineParameterMappings, [&](auto &Pair) { | |||
4701 | Stmt *StmtPtr = const_cast<Stmt *>(Pair.second); | |||
4702 | if (DeclStmt *DeclStmtPtr = dyn_cast<DeclStmt>(StmtPtr)) { | |||
4703 | DeclGroupRef DeclGroup = DeclStmtPtr->getDeclGroup(); | |||
4704 | Decl *Decl = DeclGroup.getSingleDecl(); | |||
4705 | if (VD == dyn_cast_or_null<VarDecl>(Decl)) | |||
4706 | return true; | |||
4707 | } | |||
4708 | return false; | |||
4709 | }); | |||
4710 | ||||
4711 | if (Iter != CoroutineParameterMappings.end()) { | |||
4712 | ParmVarDecl *PD = const_cast<ParmVarDecl *>(Iter->first); | |||
4713 | auto Iter2 = llvm::find_if(ParamDbgMappings, [&](auto &DbgPair) { | |||
4714 | return DbgPair.first == PD && DbgPair.second->getScope() == Scope; | |||
4715 | }); | |||
4716 | if (Iter2 != ParamDbgMappings.end()) | |||
4717 | return const_cast<llvm::DILocalVariable *>(Iter2->second); | |||
4718 | } | |||
4719 | return nullptr; | |||
4720 | }; | |||
4721 | ||||
4722 | // If we couldn't find a move param DIVariable, create a new one. | |||
4723 | D = RemapCoroArgToLocalVar(); | |||
4724 | // Or we will create a new DIVariable for this Decl if D dose not exists. | |||
4725 | if (!D) | |||
4726 | D = DBuilder.createAutoVariable(Scope, Name, Unit, Line, Ty, | |||
4727 | CGM.getLangOpts().Optimize, Flags, Align); | |||
4728 | } | |||
4729 | // Insert an llvm.dbg.declare into the current block. | |||
4730 | DBuilder.insertDeclare(Storage, D, DBuilder.createExpression(Expr), | |||
4731 | llvm::DILocation::get(CGM.getLLVMContext(), Line, | |||
4732 | Column, Scope, CurInlinedAt), | |||
4733 | Builder.GetInsertBlock()); | |||
4734 | ||||
4735 | return D; | |||
4736 | } | |||
4737 | ||||
4738 | llvm::DILocalVariable *CGDebugInfo::EmitDeclare(const BindingDecl *BD, | |||
4739 | llvm::Value *Storage, | |||
4740 | std::optional<unsigned> ArgNo, | |||
4741 | CGBuilderTy &Builder, | |||
4742 | const bool UsePointerValue) { | |||
4743 | assert(CGM.getCodeGenOpts().hasReducedDebugInfo())(static_cast <bool> (CGM.getCodeGenOpts().hasReducedDebugInfo ()) ? void (0) : __assert_fail ("CGM.getCodeGenOpts().hasReducedDebugInfo()" , "clang/lib/CodeGen/CGDebugInfo.cpp", 4743, __extension__ __PRETTY_FUNCTION__ )); | |||
4744 | assert(!LexicalBlockStack.empty() && "Region stack mismatch, stack empty!")(static_cast <bool> (!LexicalBlockStack.empty() && "Region stack mismatch, stack empty!") ? void (0) : __assert_fail ("!LexicalBlockStack.empty() && \"Region stack mismatch, stack empty!\"" , "clang/lib/CodeGen/CGDebugInfo.cpp", 4744, __extension__ __PRETTY_FUNCTION__ )); | |||
4745 | if (BD->hasAttr<NoDebugAttr>()) | |||
4746 | return nullptr; | |||
4747 | ||||
4748 | // Skip the tuple like case, we don't handle that here | |||
4749 | if (isa<DeclRefExpr>(BD->getBinding())) | |||
4750 | return nullptr; | |||
4751 | ||||
4752 | llvm::DIFile *Unit = getOrCreateFile(BD->getLocation()); | |||
4753 | llvm::DIType *Ty = getOrCreateType(BD->getType(), Unit); | |||
4754 | ||||
4755 | // If there is no debug info for this type then do not emit debug info | |||
4756 | // for this variable. | |||
4757 | if (!Ty) | |||
4758 | return nullptr; | |||
4759 | ||||
4760 | auto Align = getDeclAlignIfRequired(BD, CGM.getContext()); | |||
4761 | unsigned AddressSpace = CGM.getTypes().getTargetAddressSpace(BD->getType()); | |||
4762 | ||||
4763 | SmallVector<uint64_t, 3> Expr; | |||
4764 | AppendAddressSpaceXDeref(AddressSpace, Expr); | |||
4765 | ||||
4766 | // Clang stores the sret pointer provided by the caller in a static alloca. | |||
4767 | // Use DW_OP_deref to tell the debugger to load the pointer and treat it as | |||
4768 | // the address of the variable. | |||
4769 | if (UsePointerValue) { | |||
4770 | assert(!llvm::is_contained(Expr, llvm::dwarf::DW_OP_deref) &&(static_cast <bool> (!llvm::is_contained(Expr, llvm::dwarf ::DW_OP_deref) && "Debug info already contains DW_OP_deref." ) ? void (0) : __assert_fail ("!llvm::is_contained(Expr, llvm::dwarf::DW_OP_deref) && \"Debug info already contains DW_OP_deref.\"" , "clang/lib/CodeGen/CGDebugInfo.cpp", 4771, __extension__ __PRETTY_FUNCTION__ )) | |||
4771 | "Debug info already contains DW_OP_deref.")(static_cast <bool> (!llvm::is_contained(Expr, llvm::dwarf ::DW_OP_deref) && "Debug info already contains DW_OP_deref." ) ? void (0) : __assert_fail ("!llvm::is_contained(Expr, llvm::dwarf::DW_OP_deref) && \"Debug info already contains DW_OP_deref.\"" , "clang/lib/CodeGen/CGDebugInfo.cpp", 4771, __extension__ __PRETTY_FUNCTION__ )); | |||
4772 | Expr.push_back(llvm::dwarf::DW_OP_deref); | |||
4773 | } | |||
4774 | ||||
4775 | unsigned Line = getLineNumber(BD->getLocation()); | |||
4776 | unsigned Column = getColumnNumber(BD->getLocation()); | |||
4777 | StringRef Name = BD->getName(); | |||
4778 | auto *Scope = cast<llvm::DIScope>(LexicalBlockStack.back()); | |||
4779 | // Create the descriptor for the variable. | |||
4780 | llvm::DILocalVariable *D = DBuilder.createAutoVariable( | |||
4781 | Scope, Name, Unit, Line, Ty, CGM.getLangOpts().Optimize, | |||
4782 | llvm::DINode::FlagZero, Align); | |||
4783 | ||||
4784 | if (const MemberExpr *ME = dyn_cast<MemberExpr>(BD->getBinding())) { | |||
4785 | if (const FieldDecl *FD = dyn_cast<FieldDecl>(ME->getMemberDecl())) { | |||
4786 | const unsigned fieldIndex = FD->getFieldIndex(); | |||
4787 | const clang::CXXRecordDecl *parent = | |||
4788 | (const CXXRecordDecl *)FD->getParent(); | |||
4789 | const ASTRecordLayout &layout = | |||
4790 | CGM.getContext().getASTRecordLayout(parent); | |||
4791 | const uint64_t fieldOffset = layout.getFieldOffset(fieldIndex); | |||
4792 | ||||
4793 | if (fieldOffset != 0) { | |||
4794 | Expr.push_back(llvm::dwarf::DW_OP_plus_uconst); | |||
4795 | Expr.push_back( | |||
4796 | CGM.getContext().toCharUnitsFromBits(fieldOffset).getQuantity()); | |||
4797 | } | |||
4798 | } | |||
4799 | } else if (const ArraySubscriptExpr *ASE = | |||
4800 | dyn_cast<ArraySubscriptExpr>(BD->getBinding())) { | |||
4801 | if (const IntegerLiteral *IL = dyn_cast<IntegerLiteral>(ASE->getIdx())) { | |||
4802 | const uint64_t value = IL->getValue().getZExtValue(); | |||
4803 | const uint64_t typeSize = CGM.getContext().getTypeSize(BD->getType()); | |||
4804 | ||||
4805 | if (value != 0) { | |||
4806 | Expr.push_back(llvm::dwarf::DW_OP_plus_uconst); | |||
4807 | Expr.push_back(CGM.getContext() | |||
4808 | .toCharUnitsFromBits(value * typeSize) | |||
4809 | .getQuantity()); | |||
4810 | } | |||
4811 | } | |||
4812 | } | |||
4813 | ||||
4814 | // Insert an llvm.dbg.declare into the current block. | |||
4815 | DBuilder.insertDeclare(Storage, D, DBuilder.createExpression(Expr), | |||
4816 | llvm::DILocation::get(CGM.getLLVMContext(), Line, | |||
4817 | Column, Scope, CurInlinedAt), | |||
4818 | Builder.GetInsertBlock()); | |||
4819 | ||||
4820 | return D; | |||
4821 | } | |||
4822 | ||||
4823 | llvm::DILocalVariable * | |||
4824 | CGDebugInfo::EmitDeclareOfAutoVariable(const VarDecl *VD, llvm::Value *Storage, | |||
4825 | CGBuilderTy &Builder, | |||
4826 | const bool UsePointerValue) { | |||
4827 | assert(CGM.getCodeGenOpts().hasReducedDebugInfo())(static_cast <bool> (CGM.getCodeGenOpts().hasReducedDebugInfo ()) ? void (0) : __assert_fail ("CGM.getCodeGenOpts().hasReducedDebugInfo()" , "clang/lib/CodeGen/CGDebugInfo.cpp", 4827, __extension__ __PRETTY_FUNCTION__ )); | |||
4828 | ||||
4829 | if (auto *DD = dyn_cast<DecompositionDecl>(VD)) | |||
4830 | for (auto *B : DD->bindings()) { | |||
4831 | EmitDeclare(B, Storage, std::nullopt, Builder, | |||
4832 | VD->getType()->isReferenceType()); | |||
4833 | } | |||
4834 | ||||
4835 | return EmitDeclare(VD, Storage, std::nullopt, Builder, UsePointerValue); | |||
4836 | } | |||
4837 | ||||
4838 | void CGDebugInfo::EmitLabel(const LabelDecl *D, CGBuilderTy &Builder) { | |||
4839 | assert(CGM.getCodeGenOpts().hasReducedDebugInfo())(static_cast <bool> (CGM.getCodeGenOpts().hasReducedDebugInfo ()) ? void (0) : __assert_fail ("CGM.getCodeGenOpts().hasReducedDebugInfo()" , "clang/lib/CodeGen/CGDebugInfo.cpp", 4839, __extension__ __PRETTY_FUNCTION__ )); | |||
4840 | assert(!LexicalBlockStack.empty() && "Region stack mismatch, stack empty!")(static_cast <bool> (!LexicalBlockStack.empty() && "Region stack mismatch, stack empty!") ? void (0) : __assert_fail ("!LexicalBlockStack.empty() && \"Region stack mismatch, stack empty!\"" , "clang/lib/CodeGen/CGDebugInfo.cpp", 4840, __extension__ __PRETTY_FUNCTION__ )); | |||
4841 | ||||
4842 | if (D->hasAttr<NoDebugAttr>()) | |||
4843 | return; | |||
4844 | ||||
4845 | auto *Scope = cast<llvm::DIScope>(LexicalBlockStack.back()); | |||
4846 | llvm::DIFile *Unit = getOrCreateFile(D->getLocation()); | |||
4847 | ||||
4848 | // Get location information. | |||
4849 | unsigned Line = getLineNumber(D->getLocation()); | |||
4850 | unsigned Column = getColumnNumber(D->getLocation()); | |||
4851 | ||||
4852 | StringRef Name = D->getName(); | |||
4853 | ||||
4854 | // Create the descriptor for the label. | |||
4855 | auto *L = | |||
4856 | DBuilder.createLabel(Scope, Name, Unit, Line, CGM.getLangOpts().Optimize); | |||
4857 | ||||
4858 | // Insert an llvm.dbg.label into the current block. | |||
4859 | DBuilder.insertLabel(L, | |||
4860 | llvm::DILocation::get(CGM.getLLVMContext(), Line, Column, | |||
4861 | Scope, CurInlinedAt), | |||
4862 | Builder.GetInsertBlock()); | |||
4863 | } | |||
4864 | ||||
4865 | llvm::DIType *CGDebugInfo::CreateSelfType(const QualType &QualTy, | |||
4866 | llvm::DIType *Ty) { | |||
4867 | llvm::DIType *CachedTy = getTypeOrNull(QualTy); | |||
4868 | if (CachedTy) | |||
4869 | Ty = CachedTy; | |||
4870 | return DBuilder.createObjectPointerType(Ty); | |||
4871 | } | |||
4872 | ||||
4873 | void CGDebugInfo::EmitDeclareOfBlockDeclRefVariable( | |||
4874 | const VarDecl *VD, llvm::Value *Storage, CGBuilderTy &Builder, | |||
4875 | const CGBlockInfo &blockInfo, llvm::Instruction *InsertPoint) { | |||
4876 | assert(CGM.getCodeGenOpts().hasReducedDebugInfo())(static_cast <bool> (CGM.getCodeGenOpts().hasReducedDebugInfo ()) ? void (0) : __assert_fail ("CGM.getCodeGenOpts().hasReducedDebugInfo()" , "clang/lib/CodeGen/CGDebugInfo.cpp", 4876, __extension__ __PRETTY_FUNCTION__ )); | |||
4877 | assert(!LexicalBlockStack.empty() && "Region stack mismatch, stack empty!")(static_cast <bool> (!LexicalBlockStack.empty() && "Region stack mismatch, stack empty!") ? void (0) : __assert_fail ("!LexicalBlockStack.empty() && \"Region stack mismatch, stack empty!\"" , "clang/lib/CodeGen/CGDebugInfo.cpp", 4877, __extension__ __PRETTY_FUNCTION__ )); | |||
4878 | ||||
4879 | if (Builder.GetInsertBlock() == nullptr) | |||
4880 | return; | |||
4881 | if (VD->hasAttr<NoDebugAttr>()) | |||
4882 | return; | |||
4883 | ||||
4884 | bool isByRef = VD->hasAttr<BlocksAttr>(); | |||
4885 | ||||
4886 | uint64_t XOffset = 0; | |||
4887 | llvm::DIFile *Unit = getOrCreateFile(VD->getLocation()); | |||
4888 | llvm::DIType *Ty; | |||
4889 | if (isByRef) | |||
4890 | Ty = EmitTypeForVarWithBlocksAttr(VD, &XOffset).WrappedType; | |||
4891 | else | |||
4892 | Ty = getOrCreateType(VD->getType(), Unit); | |||
4893 | ||||
4894 | // Self is passed along as an implicit non-arg variable in a | |||
4895 | // block. Mark it as the object pointer. | |||
4896 | if (const auto *IPD = dyn_cast<ImplicitParamDecl>(VD)) | |||
4897 | if (IPD->getParameterKind() == ImplicitParamDecl::ObjCSelf) | |||
4898 | Ty = CreateSelfType(VD->getType(), Ty); | |||
4899 | ||||
4900 | // Get location information. | |||
4901 | const unsigned Line = | |||
4902 | getLineNumber(VD->getLocation().isValid() ? VD->getLocation() : CurLoc); | |||
4903 | unsigned Column = getColumnNumber(VD->getLocation()); | |||
4904 | ||||
4905 | const llvm::DataLayout &target = CGM.getDataLayout(); | |||
4906 | ||||
4907 | CharUnits offset = CharUnits::fromQuantity( | |||
4908 | target.getStructLayout(blockInfo.StructureType) | |||
4909 | ->getElementOffset(blockInfo.getCapture(VD).getIndex())); | |||
4910 | ||||
4911 | SmallVector<uint64_t, 9> addr; | |||
4912 | addr.push_back(llvm::dwarf::DW_OP_deref); | |||
4913 | addr.push_back(llvm::dwarf::DW_OP_plus_uconst); | |||
4914 | addr.push_back(offset.getQuantity()); | |||
4915 | if (isByRef) { | |||
4916 | addr.push_back(llvm::dwarf::DW_OP_deref); | |||
4917 | addr.push_back(llvm::dwarf::DW_OP_plus_uconst); | |||
4918 | // offset of __forwarding field | |||
4919 | offset = | |||
4920 | CGM.getContext().toCharUnitsFromBits(target.getPointerSizeInBits(0)); | |||
4921 | addr.push_back(offset.getQuantity()); | |||
4922 | addr.push_back(llvm::dwarf::DW_OP_deref); | |||
4923 | addr.push_back(llvm::dwarf::DW_OP_plus_uconst); | |||
4924 | // offset of x field | |||
4925 | offset = CGM.getContext().toCharUnitsFromBits(XOffset); | |||
4926 | addr.push_back(offset.getQuantity()); | |||
4927 | } | |||
4928 | ||||
4929 | // Create the descriptor for the variable. | |||
4930 | auto Align = getDeclAlignIfRequired(VD, CGM.getContext()); | |||
4931 | auto *D = DBuilder.createAutoVariable( | |||
4932 | cast<llvm::DILocalScope>(LexicalBlockStack.back()), VD->getName(), Unit, | |||
4933 | Line, Ty, false, llvm::DINode::FlagZero, Align); | |||
4934 | ||||
4935 | // Insert an llvm.dbg.declare into the current block. | |||
4936 | auto DL = llvm::DILocation::get(CGM.getLLVMContext(), Line, Column, | |||
4937 | LexicalBlockStack.back(), CurInlinedAt); | |||
4938 | auto *Expr = DBuilder.createExpression(addr); | |||
4939 | if (InsertPoint) | |||
4940 | DBuilder.insertDeclare(Storage, D, Expr, DL, InsertPoint); | |||
4941 | else | |||
4942 | DBuilder.insertDeclare(Storage, D, Expr, DL, Builder.GetInsertBlock()); | |||
4943 | } | |||
4944 | ||||
4945 | llvm::DILocalVariable * | |||
4946 | CGDebugInfo::EmitDeclareOfArgVariable(const VarDecl *VD, llvm::Value *AI, | |||
4947 | unsigned ArgNo, CGBuilderTy &Builder, | |||
4948 | bool UsePointerValue) { | |||
4949 | assert(CGM.getCodeGenOpts().hasReducedDebugInfo())(static_cast <bool> (CGM.getCodeGenOpts().hasReducedDebugInfo ()) ? void (0) : __assert_fail ("CGM.getCodeGenOpts().hasReducedDebugInfo()" , "clang/lib/CodeGen/CGDebugInfo.cpp", 4949, __extension__ __PRETTY_FUNCTION__ )); | |||
4950 | return EmitDeclare(VD, AI, ArgNo, Builder, UsePointerValue); | |||
4951 | } | |||
4952 | ||||
4953 | namespace { | |||
4954 | struct BlockLayoutChunk { | |||
4955 | uint64_t OffsetInBits; | |||
4956 | const BlockDecl::Capture *Capture; | |||
4957 | }; | |||
4958 | bool operator<(const BlockLayoutChunk &l, const BlockLayoutChunk &r) { | |||
4959 | return l.OffsetInBits < r.OffsetInBits; | |||
4960 | } | |||
4961 | } // namespace | |||
4962 | ||||
4963 | void CGDebugInfo::collectDefaultFieldsForBlockLiteralDeclare( | |||
4964 | const CGBlockInfo &Block, const ASTContext &Context, SourceLocation Loc, | |||
4965 | const llvm::StructLayout &BlockLayout, llvm::DIFile *Unit, | |||
4966 | SmallVectorImpl<llvm::Metadata *> &Fields) { | |||
4967 | // Blocks in OpenCL have unique constraints which make the standard fields | |||
4968 | // redundant while requiring size and align fields for enqueue_kernel. See | |||
4969 | // initializeForBlockHeader in CGBlocks.cpp | |||
4970 | if (CGM.getLangOpts().OpenCL) { | |||
4971 | Fields.push_back(createFieldType("__size", Context.IntTy, Loc, AS_public, | |||
4972 | BlockLayout.getElementOffsetInBits(0), | |||
4973 | Unit, Unit)); | |||
4974 | Fields.push_back(createFieldType("__align", Context.IntTy, Loc, AS_public, | |||
4975 | BlockLayout.getElementOffsetInBits(1), | |||
4976 | Unit, Unit)); | |||
4977 | } else { | |||
4978 | Fields.push_back(createFieldType("__isa", Context.VoidPtrTy, Loc, AS_public, | |||
4979 | BlockLayout.getElementOffsetInBits(0), | |||
4980 | Unit, Unit)); | |||
4981 | Fields.push_back(createFieldType("__flags", Context.IntTy, Loc, AS_public, | |||
4982 | BlockLayout.getElementOffsetInBits(1), | |||
4983 | Unit, Unit)); | |||
4984 | Fields.push_back( | |||
4985 | createFieldType("__reserved", Context.IntTy, Loc, AS_public, | |||
4986 | BlockLayout.getElementOffsetInBits(2), Unit, Unit)); | |||
4987 | auto *FnTy = Block.getBlockExpr()->getFunctionType(); | |||
4988 | auto FnPtrType = CGM.getContext().getPointerType(FnTy->desugar()); | |||
4989 | Fields.push_back(createFieldType("__FuncPtr", FnPtrType, Loc, AS_public, | |||
4990 | BlockLayout.getElementOffsetInBits(3), | |||
4991 | Unit, Unit)); | |||
4992 | Fields.push_back(createFieldType( | |||
4993 | "__descriptor", | |||
4994 | Context.getPointerType(Block.NeedsCopyDispose | |||
4995 | ? Context.getBlockDescriptorExtendedType() | |||
4996 | : Context.getBlockDescriptorType()), | |||
4997 | Loc, AS_public, BlockLayout.getElementOffsetInBits(4), Unit, Unit)); | |||
4998 | } | |||
4999 | } | |||
5000 | ||||
5001 | void CGDebugInfo::EmitDeclareOfBlockLiteralArgVariable(const CGBlockInfo &block, | |||
5002 | StringRef Name, | |||
5003 | unsigned ArgNo, | |||
5004 | llvm::AllocaInst *Alloca, | |||
5005 | CGBuilderTy &Builder) { | |||
5006 | assert(CGM.getCodeGenOpts().hasReducedDebugInfo())(static_cast <bool> (CGM.getCodeGenOpts().hasReducedDebugInfo ()) ? void (0) : __assert_fail ("CGM.getCodeGenOpts().hasReducedDebugInfo()" , "clang/lib/CodeGen/CGDebugInfo.cpp", 5006, __extension__ __PRETTY_FUNCTION__ )); | |||
5007 | ASTContext &C = CGM.getContext(); | |||
5008 | const BlockDecl *blockDecl = block.getBlockDecl(); | |||
5009 | ||||
5010 | // Collect some general information about the block's location. | |||
5011 | SourceLocation loc = blockDecl->getCaretLocation(); | |||
5012 | llvm::DIFile *tunit = getOrCreateFile(loc); | |||
5013 | unsigned line = getLineNumber(loc); | |||
5014 | unsigned column = getColumnNumber(loc); | |||
5015 | ||||
5016 | // Build the debug-info type for the block literal. | |||
5017 | getDeclContextDescriptor(blockDecl); | |||
5018 | ||||
5019 | const llvm::StructLayout *blockLayout = | |||
5020 | CGM.getDataLayout().getStructLayout(block.StructureType); | |||
5021 | ||||
5022 | SmallVector<llvm::Metadata *, 16> fields; | |||
5023 | collectDefaultFieldsForBlockLiteralDeclare(block, C, loc, *blockLayout, tunit, | |||
5024 | fields); | |||
5025 | ||||
5026 | // We want to sort the captures by offset, not because DWARF | |||
5027 | // requires this, but because we're paranoid about debuggers. | |||
5028 | SmallVector<BlockLayoutChunk, 8> chunks; | |||
5029 | ||||
5030 | // 'this' capture. | |||
5031 | if (blockDecl->capturesCXXThis()) { | |||
5032 | BlockLayoutChunk chunk; | |||
5033 | chunk.OffsetInBits = | |||
5034 | blockLayout->getElementOffsetInBits(block.CXXThisIndex); | |||
5035 | chunk.Capture = nullptr; | |||
5036 | chunks.push_back(chunk); | |||
5037 | } | |||
5038 | ||||
5039 | // Variable captures. | |||
5040 | for (const auto &capture : blockDecl->captures()) { | |||
5041 | const VarDecl *variable = capture.getVariable(); | |||
5042 | const CGBlockInfo::Capture &captureInfo = block.getCapture(variable); | |||
5043 | ||||
5044 | // Ignore constant captures. | |||
5045 | if (captureInfo.isConstant()) | |||
5046 | continue; | |||
5047 | ||||
5048 | BlockLayoutChunk chunk; | |||
5049 | chunk.OffsetInBits = | |||
5050 | blockLayout->getElementOffsetInBits(captureInfo.getIndex()); | |||
5051 | chunk.Capture = &capture; | |||
5052 | chunks.push_back(chunk); | |||
5053 | } | |||
5054 | ||||
5055 | // Sort by offset. | |||
5056 | llvm::array_pod_sort(chunks.begin(), chunks.end()); | |||
5057 | ||||
5058 | for (const BlockLayoutChunk &Chunk : chunks) { | |||
5059 | uint64_t offsetInBits = Chunk.OffsetInBits; | |||
5060 | const BlockDecl::Capture *capture = Chunk.Capture; | |||
5061 | ||||
5062 | // If we have a null capture, this must be the C++ 'this' capture. | |||
5063 | if (!capture) { | |||
5064 | QualType type; | |||
5065 | if (auto *Method = | |||
5066 | cast_or_null<CXXMethodDecl>(blockDecl->getNonClosureContext())) | |||
5067 | type = Method->getThisType(); | |||
5068 | else if (auto *RDecl = dyn_cast<CXXRecordDecl>(blockDecl->getParent())) | |||
5069 | type = QualType(RDecl->getTypeForDecl(), 0); | |||
5070 | else | |||
5071 | llvm_unreachable("unexpected block declcontext")::llvm::llvm_unreachable_internal("unexpected block declcontext" , "clang/lib/CodeGen/CGDebugInfo.cpp", 5071); | |||
5072 | ||||
5073 | fields.push_back(createFieldType("this", type, loc, AS_public, | |||
5074 | offsetInBits, tunit, tunit)); | |||
5075 | continue; | |||
5076 | } | |||
5077 | ||||
5078 | const VarDecl *variable = capture->getVariable(); | |||
5079 | StringRef name = variable->getName(); | |||
5080 | ||||
5081 | llvm::DIType *fieldType; | |||
5082 | if (capture->isByRef()) { | |||
5083 | TypeInfo PtrInfo = C.getTypeInfo(C.VoidPtrTy); | |||
5084 | auto Align = PtrInfo.isAlignRequired() ? PtrInfo.Align : 0; | |||
5085 | // FIXME: This recomputes the layout of the BlockByRefWrapper. | |||
5086 | uint64_t xoffset; | |||
5087 | fieldType = | |||
5088 | EmitTypeForVarWithBlocksAttr(variable, &xoffset).BlockByRefWrapper; | |||
5089 | fieldType = DBuilder.createPointerType(fieldType, PtrInfo.Width); | |||
5090 | fieldType = DBuilder.createMemberType(tunit, name, tunit, line, | |||
5091 | PtrInfo.Width, Align, offsetInBits, | |||
5092 | llvm::DINode::FlagZero, fieldType); | |||
5093 | } else { | |||
5094 | auto Align = getDeclAlignIfRequired(variable, CGM.getContext()); | |||
5095 | fieldType = createFieldType(name, variable->getType(), loc, AS_public, | |||
5096 | offsetInBits, Align, tunit, tunit); | |||
5097 | } | |||
5098 | fields.push_back(fieldType); | |||
5099 | } | |||
5100 | ||||
5101 | SmallString<36> typeName; | |||
5102 | llvm::raw_svector_ostream(typeName) | |||
5103 | << "__block_literal_" << CGM.getUniqueBlockCount(); | |||
5104 | ||||
5105 | llvm::DINodeArray fieldsArray = DBuilder.getOrCreateArray(fields); | |||
5106 | ||||
5107 | llvm::DIType *type = | |||
5108 | DBuilder.createStructType(tunit, typeName.str(), tunit, line, | |||
5109 | CGM.getContext().toBits(block.BlockSize), 0, | |||
5110 | llvm::DINode::FlagZero, nullptr, fieldsArray); | |||
5111 | type = DBuilder.createPointerType(type, CGM.PointerWidthInBits); | |||
5112 | ||||
5113 | // Get overall information about the block. | |||
5114 | llvm::DINode::DIFlags flags = llvm::DINode::FlagArtificial; | |||
5115 | auto *scope = cast<llvm::DILocalScope>(LexicalBlockStack.back()); | |||
5116 | ||||
5117 | // Create the descriptor for the parameter. | |||
5118 | auto *debugVar = DBuilder.createParameterVariable( | |||
5119 | scope, Name, ArgNo, tunit, line, type, CGM.getLangOpts().Optimize, flags); | |||
5120 | ||||
5121 | // Insert an llvm.dbg.declare into the current block. | |||
5122 | DBuilder.insertDeclare(Alloca, debugVar, DBuilder.createExpression(), | |||
5123 | llvm::DILocation::get(CGM.getLLVMContext(), line, | |||
5124 | column, scope, CurInlinedAt), | |||
5125 | Builder.GetInsertBlock()); | |||
5126 | } | |||
5127 | ||||
5128 | llvm::DIDerivedType * | |||
5129 | CGDebugInfo::getOrCreateStaticDataMemberDeclarationOrNull(const VarDecl *D) { | |||
5130 | if (!D || !D->isStaticDataMember()) | |||
5131 | return nullptr; | |||
5132 | ||||
5133 | auto MI = StaticDataMemberCache.find(D->getCanonicalDecl()); | |||
5134 | if (MI != StaticDataMemberCache.end()) { | |||
5135 | assert(MI->second && "Static data member declaration should still exist")(static_cast <bool> (MI->second && "Static data member declaration should still exist" ) ? void (0) : __assert_fail ("MI->second && \"Static data member declaration should still exist\"" , "clang/lib/CodeGen/CGDebugInfo.cpp", 5135, __extension__ __PRETTY_FUNCTION__ )); | |||
5136 | return MI->second; | |||
5137 | } | |||
5138 | ||||
5139 | // If the member wasn't found in the cache, lazily construct and add it to the | |||
5140 | // type (used when a limited form of the type is emitted). | |||
5141 | auto DC = D->getDeclContext(); | |||
5142 | auto *Ctxt = cast<llvm::DICompositeType>(getDeclContextDescriptor(D)); | |||
5143 | return CreateRecordStaticField(D, Ctxt, cast<RecordDecl>(DC)); | |||
5144 | } | |||
5145 | ||||
5146 | llvm::DIGlobalVariableExpression *CGDebugInfo::CollectAnonRecordDecls( | |||
5147 | const RecordDecl *RD, llvm::DIFile *Unit, unsigned LineNo, | |||
5148 | StringRef LinkageName, llvm::GlobalVariable *Var, llvm::DIScope *DContext) { | |||
5149 | llvm::DIGlobalVariableExpression *GVE = nullptr; | |||
5150 | ||||
5151 | for (const auto *Field : RD->fields()) { | |||
5152 | llvm::DIType *FieldTy = getOrCreateType(Field->getType(), Unit); | |||
5153 | StringRef FieldName = Field->getName(); | |||
5154 | ||||
5155 | // Ignore unnamed fields, but recurse into anonymous records. | |||
5156 | if (FieldName.empty()) { | |||
5157 | if (const auto *RT = dyn_cast<RecordType>(Field->getType())) | |||
5158 | GVE = CollectAnonRecordDecls(RT->getDecl(), Unit, LineNo, LinkageName, | |||
5159 | Var, DContext); | |||
5160 | continue; | |||
5161 | } | |||
5162 | // Use VarDecl's Tag, Scope and Line number. | |||
5163 | GVE = DBuilder.createGlobalVariableExpression( | |||
5164 | DContext, FieldName, LinkageName, Unit, LineNo, FieldTy, | |||
5165 | Var->hasLocalLinkage()); | |||
5166 | Var->addDebugInfo(GVE); | |||
5167 | } | |||
5168 | return GVE; | |||
5169 | } | |||
5170 | ||||
5171 | static bool ReferencesAnonymousEntity(ArrayRef<TemplateArgument> Args); | |||
5172 | static bool ReferencesAnonymousEntity(RecordType *RT) { | |||
5173 | // Unnamed classes/lambdas can't be reconstituted due to a lack of column | |||
5174 | // info we produce in the DWARF, so we can't get Clang's full name back. | |||
5175 | // But so long as it's not one of those, it doesn't matter if some sub-type | |||
5176 | // of the record (a template parameter) can't be reconstituted - because the | |||
5177 | // un-reconstitutable type itself will carry its own name. | |||
5178 | const auto *RD = dyn_cast<CXXRecordDecl>(RT->getDecl()); | |||
5179 | if (!RD) | |||
5180 | return false; | |||
5181 | if (!RD->getIdentifier()) | |||
5182 | return true; | |||
5183 | auto *TSpecial = dyn_cast<ClassTemplateSpecializationDecl>(RD); | |||
5184 | if (!TSpecial) | |||
5185 | return false; | |||
5186 | return ReferencesAnonymousEntity(TSpecial->getTemplateArgs().asArray()); | |||
5187 | } | |||
5188 | static bool ReferencesAnonymousEntity(ArrayRef<TemplateArgument> Args) { | |||
5189 | return llvm::any_of(Args, [&](const TemplateArgument &TA) { | |||
5190 | switch (TA.getKind()) { | |||
5191 | case TemplateArgument::Pack: | |||
5192 | return ReferencesAnonymousEntity(TA.getPackAsArray()); | |||
5193 | case TemplateArgument::Type: { | |||
5194 | struct ReferencesAnonymous | |||
5195 | : public RecursiveASTVisitor<ReferencesAnonymous> { | |||
5196 | bool RefAnon = false; | |||
5197 | bool VisitRecordType(RecordType *RT) { | |||
5198 | if (ReferencesAnonymousEntity(RT)) { | |||
5199 | RefAnon = true; | |||
5200 | return false; | |||
5201 | } | |||
5202 | return true; | |||
5203 | } | |||
5204 | }; | |||
5205 | ReferencesAnonymous RT; | |||
5206 | RT.TraverseType(TA.getAsType()); | |||
5207 | if (RT.RefAnon) | |||
5208 | return true; | |||
5209 | break; | |||
5210 | } | |||
5211 | default: | |||
5212 | break; | |||
5213 | } | |||
5214 | return false; | |||
5215 | }); | |||
5216 | } | |||
5217 | namespace { | |||
5218 | struct ReconstitutableType : public RecursiveASTVisitor<ReconstitutableType> { | |||
5219 | bool Reconstitutable = true; | |||
5220 | bool VisitVectorType(VectorType *FT) { | |||
5221 | Reconstitutable = false; | |||
5222 | return false; | |||
5223 | } | |||
5224 | bool VisitAtomicType(AtomicType *FT) { | |||
5225 | Reconstitutable = false; | |||
5226 | return false; | |||
5227 | } | |||
5228 | bool VisitType(Type *T) { | |||
5229 | // _BitInt(N) isn't reconstitutable because the bit width isn't encoded in | |||
5230 | // the DWARF, only the byte width. | |||
5231 | if (T->isBitIntType()) { | |||
5232 | Reconstitutable = false; | |||
5233 | return false; | |||
5234 | } | |||
5235 | return true; | |||
5236 | } | |||
5237 | bool TraverseEnumType(EnumType *ET) { | |||
5238 | // Unnamed enums can't be reconstituted due to a lack of column info we | |||
5239 | // produce in the DWARF, so we can't get Clang's full name back. | |||
5240 | if (const auto *ED = dyn_cast<EnumDecl>(ET->getDecl())) { | |||
5241 | if (!ED->getIdentifier()) { | |||
5242 | Reconstitutable = false; | |||
5243 | return false; | |||
5244 | } | |||
5245 | if (!ED->isExternallyVisible()) { | |||
5246 | Reconstitutable = false; | |||
5247 | return false; | |||
5248 | } | |||
5249 | } | |||
5250 | return true; | |||
5251 | } | |||
5252 | bool VisitFunctionProtoType(FunctionProtoType *FT) { | |||
5253 | // noexcept is not encoded in DWARF, so the reversi | |||
5254 | Reconstitutable &= !isNoexceptExceptionSpec(FT->getExceptionSpecType()); | |||
5255 | Reconstitutable &= !FT->getNoReturnAttr(); | |||
5256 | return Reconstitutable; | |||
5257 | } | |||
5258 | bool VisitRecordType(RecordType *RT) { | |||
5259 | if (ReferencesAnonymousEntity(RT)) { | |||
5260 | Reconstitutable = false; | |||
5261 | return false; | |||
5262 | } | |||
5263 | return true; | |||
5264 | } | |||
5265 | }; | |||
5266 | } // anonymous namespace | |||
5267 | ||||
5268 | // Test whether a type name could be rebuilt from emitted debug info. | |||
5269 | static bool IsReconstitutableType(QualType QT) { | |||
5270 | ReconstitutableType T; | |||
5271 | T.TraverseType(QT); | |||
5272 | return T.Reconstitutable; | |||
5273 | } | |||
5274 | ||||
5275 | std::string CGDebugInfo::GetName(const Decl *D, bool Qualified) const { | |||
5276 | std::string Name; | |||
5277 | llvm::raw_string_ostream OS(Name); | |||
5278 | const NamedDecl *ND = dyn_cast<NamedDecl>(D); | |||
5279 | if (!ND) | |||
5280 | return Name; | |||
5281 | llvm::codegenoptions::DebugTemplateNamesKind TemplateNamesKind = | |||
5282 | CGM.getCodeGenOpts().getDebugSimpleTemplateNames(); | |||
5283 | ||||
5284 | if (!CGM.getCodeGenOpts().hasReducedDebugInfo()) | |||
5285 | TemplateNamesKind = llvm::codegenoptions::DebugTemplateNamesKind::Full; | |||
5286 | ||||
5287 | std::optional<TemplateArgs> Args; | |||
5288 | ||||
5289 | bool IsOperatorOverload = false; // isa<CXXConversionDecl>(ND); | |||
5290 | if (auto *RD = dyn_cast<CXXRecordDecl>(ND)) { | |||
5291 | Args = GetTemplateArgs(RD); | |||
5292 | } else if (auto *FD = dyn_cast<FunctionDecl>(ND)) { | |||
5293 | Args = GetTemplateArgs(FD); | |||
5294 | auto NameKind = ND->getDeclName().getNameKind(); | |||
5295 | IsOperatorOverload |= | |||
5296 | NameKind == DeclarationName::CXXOperatorName || | |||
5297 | NameKind == DeclarationName::CXXConversionFunctionName; | |||
5298 | } else if (auto *VD = dyn_cast<VarDecl>(ND)) { | |||
5299 | Args = GetTemplateArgs(VD); | |||
5300 | } | |||
5301 | std::function<bool(ArrayRef<TemplateArgument>)> HasReconstitutableArgs = | |||
5302 | [&](ArrayRef<TemplateArgument> Args) { | |||
5303 | return llvm::all_of(Args, [&](const TemplateArgument &TA) { | |||
5304 | switch (TA.getKind()) { | |||
5305 | case TemplateArgument::Template: | |||
5306 | // Easy to reconstitute - the value of the parameter in the debug | |||
5307 | // info is the string name of the template. (so the template name | |||
5308 | // itself won't benefit from any name rebuilding, but that's a | |||
5309 | // representational limitation - maybe DWARF could be | |||
5310 | // changed/improved to use some more structural representation) | |||
5311 | return true; | |||
5312 | case TemplateArgument::Declaration: | |||
5313 | // Reference and pointer non-type template parameters point to | |||
5314 | // variables, functions, etc and their value is, at best (for | |||
5315 | // variables) represented as an address - not a reference to the | |||
5316 | // DWARF describing the variable/function/etc. This makes it hard, | |||
5317 | // possibly impossible to rebuild the original name - looking up the | |||
5318 | // address in the executable file's symbol table would be needed. | |||
5319 | return false; | |||
5320 | case TemplateArgument::NullPtr: | |||
5321 | // These could be rebuilt, but figured they're close enough to the | |||
5322 | // declaration case, and not worth rebuilding. | |||
5323 | return false; | |||
5324 | case TemplateArgument::Pack: | |||
5325 | // A pack is invalid if any of the elements of the pack are invalid. | |||
5326 | return HasReconstitutableArgs(TA.getPackAsArray()); | |||
5327 | case TemplateArgument::Integral: | |||
5328 | // Larger integers get encoded as DWARF blocks which are a bit | |||
5329 | // harder to parse back into a large integer, etc - so punting on | |||
5330 | // this for now. Re-parsing the integers back into APInt is probably | |||
5331 | // feasible some day. | |||
5332 | return TA.getAsIntegral().getBitWidth() <= 64 && | |||
5333 | IsReconstitutableType(TA.getIntegralType()); | |||
5334 | case TemplateArgument::Type: | |||
5335 | return IsReconstitutableType(TA.getAsType()); | |||
5336 | default: | |||
5337 | llvm_unreachable("Other, unresolved, template arguments should "::llvm::llvm_unreachable_internal("Other, unresolved, template arguments should " "not be seen here", "clang/lib/CodeGen/CGDebugInfo.cpp", 5338 ) | |||
5338 | "not be seen here")::llvm::llvm_unreachable_internal("Other, unresolved, template arguments should " "not be seen here", "clang/lib/CodeGen/CGDebugInfo.cpp", 5338 ); | |||
5339 | } | |||
5340 | }); | |||
5341 | }; | |||
5342 | // A conversion operator presents complications/ambiguity if there's a | |||
5343 | // conversion to class template that is itself a template, eg: | |||
5344 | // template<typename T> | |||
5345 | // operator ns::t1<T, int>(); | |||
5346 | // This should be named, eg: "operator ns::t1<float, int><float>" | |||
5347 | // (ignoring clang bug that means this is currently "operator t1<float>") | |||
5348 | // but if the arguments were stripped, the consumer couldn't differentiate | |||
5349 | // whether the template argument list for the conversion type was the | |||
5350 | // function's argument list (& no reconstitution was needed) or not. | |||
5351 | // This could be handled if reconstitutable names had a separate attribute | |||
5352 | // annotating them as such - this would remove the ambiguity. | |||
5353 | // | |||
5354 | // Alternatively the template argument list could be parsed enough to check | |||
5355 | // whether there's one list or two, then compare that with the DWARF | |||
5356 | // description of the return type and the template argument lists to determine | |||
5357 | // how many lists there should be and if one is missing it could be assumed(?) | |||
5358 | // to be the function's template argument list & then be rebuilt. | |||
5359 | // | |||
5360 | // Other operator overloads that aren't conversion operators could be | |||
5361 | // reconstituted but would require a bit more nuance about detecting the | |||
5362 | // difference between these different operators during that rebuilding. | |||
5363 | bool Reconstitutable = | |||
5364 | Args && HasReconstitutableArgs(Args->Args) && !IsOperatorOverload; | |||
5365 | ||||
5366 | PrintingPolicy PP = getPrintingPolicy(); | |||
5367 | ||||
5368 | if (TemplateNamesKind == llvm::codegenoptions::DebugTemplateNamesKind::Full || | |||
5369 | !Reconstitutable) { | |||
5370 | ND->getNameForDiagnostic(OS, PP, Qualified); | |||
5371 | } else { | |||
5372 | bool Mangled = TemplateNamesKind == | |||
5373 | llvm::codegenoptions::DebugTemplateNamesKind::Mangled; | |||
5374 | // check if it's a template | |||
5375 | if (Mangled) | |||
5376 | OS << "_STN|"; | |||
5377 | ||||
5378 | OS << ND->getDeclName(); | |||
5379 | std::string EncodedOriginalName; | |||
5380 | llvm::raw_string_ostream EncodedOriginalNameOS(EncodedOriginalName); | |||
5381 | EncodedOriginalNameOS << ND->getDeclName(); | |||
5382 | ||||
5383 | if (Mangled) { | |||
5384 | OS << "|"; | |||
5385 | printTemplateArgumentList(OS, Args->Args, PP); | |||
5386 | printTemplateArgumentList(EncodedOriginalNameOS, Args->Args, PP); | |||
5387 | #ifndef NDEBUG | |||
5388 | std::string CanonicalOriginalName; | |||
5389 | llvm::raw_string_ostream OriginalOS(CanonicalOriginalName); | |||
5390 | ND->getNameForDiagnostic(OriginalOS, PP, Qualified); | |||
5391 | assert(EncodedOriginalNameOS.str() == OriginalOS.str())(static_cast <bool> (EncodedOriginalNameOS.str() == OriginalOS .str()) ? void (0) : __assert_fail ("EncodedOriginalNameOS.str() == OriginalOS.str()" , "clang/lib/CodeGen/CGDebugInfo.cpp", 5391, __extension__ __PRETTY_FUNCTION__ )); | |||
5392 | #endif | |||
5393 | } | |||
5394 | } | |||
5395 | return Name; | |||
5396 | } | |||
5397 | ||||
5398 | void CGDebugInfo::EmitGlobalVariable(llvm::GlobalVariable *Var, | |||
5399 | const VarDecl *D) { | |||
5400 | assert(CGM.getCodeGenOpts().hasReducedDebugInfo())(static_cast <bool> (CGM.getCodeGenOpts().hasReducedDebugInfo ()) ? void (0) : __assert_fail ("CGM.getCodeGenOpts().hasReducedDebugInfo()" , "clang/lib/CodeGen/CGDebugInfo.cpp", 5400, __extension__ __PRETTY_FUNCTION__ )); | |||
5401 | if (D->hasAttr<NoDebugAttr>()) | |||
5402 | return; | |||
5403 | ||||
5404 | llvm::TimeTraceScope TimeScope("DebugGlobalVariable", [&]() { | |||
5405 | return GetName(D, true); | |||
5406 | }); | |||
5407 | ||||
5408 | // If we already created a DIGlobalVariable for this declaration, just attach | |||
5409 | // it to the llvm::GlobalVariable. | |||
5410 | auto Cached = DeclCache.find(D->getCanonicalDecl()); | |||
5411 | if (Cached != DeclCache.end()) | |||
5412 | return Var->addDebugInfo( | |||
5413 | cast<llvm::DIGlobalVariableExpression>(Cached->second)); | |||
5414 | ||||
5415 | // Create global variable debug descriptor. | |||
5416 | llvm::DIFile *Unit = nullptr; | |||
5417 | llvm::DIScope *DContext = nullptr; | |||
5418 | unsigned LineNo; | |||
5419 | StringRef DeclName, LinkageName; | |||
5420 | QualType T; | |||
5421 | llvm::MDTuple *TemplateParameters = nullptr; | |||
5422 | collectVarDeclProps(D, Unit, LineNo, T, DeclName, LinkageName, | |||
5423 | TemplateParameters, DContext); | |||
5424 | ||||
5425 | // Attempt to store one global variable for the declaration - even if we | |||
5426 | // emit a lot of fields. | |||
5427 | llvm::DIGlobalVariableExpression *GVE = nullptr; | |||
5428 | ||||
5429 | // If this is an anonymous union then we'll want to emit a global | |||
5430 | // variable for each member of the anonymous union so that it's possible | |||
5431 | // to find the name of any field in the union. | |||
5432 | if (T->isUnionType() && DeclName.empty()) { | |||
5433 | const RecordDecl *RD = T->castAs<RecordType>()->getDecl(); | |||
5434 | assert(RD->isAnonymousStructOrUnion() &&(static_cast <bool> (RD->isAnonymousStructOrUnion() && "unnamed non-anonymous struct or union?") ? void (0) : __assert_fail ("RD->isAnonymousStructOrUnion() && \"unnamed non-anonymous struct or union?\"" , "clang/lib/CodeGen/CGDebugInfo.cpp", 5435, __extension__ __PRETTY_FUNCTION__ )) | |||
5435 | "unnamed non-anonymous struct or union?")(static_cast <bool> (RD->isAnonymousStructOrUnion() && "unnamed non-anonymous struct or union?") ? void (0) : __assert_fail ("RD->isAnonymousStructOrUnion() && \"unnamed non-anonymous struct or union?\"" , "clang/lib/CodeGen/CGDebugInfo.cpp", 5435, __extension__ __PRETTY_FUNCTION__ )); | |||
5436 | GVE = CollectAnonRecordDecls(RD, Unit, LineNo, LinkageName, Var, DContext); | |||
5437 | } else { | |||
5438 | auto Align = getDeclAlignIfRequired(D, CGM.getContext()); | |||
5439 | ||||
5440 | SmallVector<uint64_t, 4> Expr; | |||
5441 | unsigned AddressSpace = CGM.getTypes().getTargetAddressSpace(D->getType()); | |||
5442 | if (CGM.getLangOpts().CUDA && CGM.getLangOpts().CUDAIsDevice) { | |||
5443 | if (D->hasAttr<CUDASharedAttr>()) | |||
5444 | AddressSpace = | |||
5445 | CGM.getContext().getTargetAddressSpace(LangAS::cuda_shared); | |||
5446 | else if (D->hasAttr<CUDAConstantAttr>()) | |||
5447 | AddressSpace = | |||
5448 | CGM.getContext().getTargetAddressSpace(LangAS::cuda_constant); | |||
5449 | } | |||
5450 | AppendAddressSpaceXDeref(AddressSpace, Expr); | |||
5451 | ||||
5452 | llvm::DINodeArray Annotations = CollectBTFDeclTagAnnotations(D); | |||
5453 | GVE = DBuilder.createGlobalVariableExpression( | |||
5454 | DContext, DeclName, LinkageName, Unit, LineNo, getOrCreateType(T, Unit), | |||
5455 | Var->hasLocalLinkage(), true, | |||
5456 | Expr.empty() ? nullptr : DBuilder.createExpression(Expr), | |||
5457 | getOrCreateStaticDataMemberDeclarationOrNull(D), TemplateParameters, | |||
5458 | Align, Annotations); | |||
5459 | Var->addDebugInfo(GVE); | |||
5460 | } | |||
5461 | DeclCache[D->getCanonicalDecl()].reset(GVE); | |||
5462 | } | |||
5463 | ||||
5464 | void CGDebugInfo::EmitGlobalVariable(const ValueDecl *VD, const APValue &Init) { | |||
5465 | assert(CGM.getCodeGenOpts().hasReducedDebugInfo())(static_cast <bool> (CGM.getCodeGenOpts().hasReducedDebugInfo ()) ? void (0) : __assert_fail ("CGM.getCodeGenOpts().hasReducedDebugInfo()" , "clang/lib/CodeGen/CGDebugInfo.cpp", 5465, __extension__ __PRETTY_FUNCTION__ )); | |||
5466 | if (VD->hasAttr<NoDebugAttr>()) | |||
5467 | return; | |||
5468 | llvm::TimeTraceScope TimeScope("DebugConstGlobalVariable", [&]() { | |||
5469 | return GetName(VD, true); | |||
5470 | }); | |||
5471 | ||||
5472 | auto Align = getDeclAlignIfRequired(VD, CGM.getContext()); | |||
5473 | // Create the descriptor for the variable. | |||
5474 | llvm::DIFile *Unit = getOrCreateFile(VD->getLocation()); | |||
5475 | StringRef Name = VD->getName(); | |||
5476 | llvm::DIType *Ty = getOrCreateType(VD->getType(), Unit); | |||
5477 | ||||
5478 | if (const auto *ECD = dyn_cast<EnumConstantDecl>(VD)) { | |||
5479 | const auto *ED = cast<EnumDecl>(ECD->getDeclContext()); | |||
5480 | assert(isa<EnumType>(ED->getTypeForDecl()) && "Enum without EnumType?")(static_cast <bool> (isa<EnumType>(ED->getTypeForDecl ()) && "Enum without EnumType?") ? void (0) : __assert_fail ("isa<EnumType>(ED->getTypeForDecl()) && \"Enum without EnumType?\"" , "clang/lib/CodeGen/CGDebugInfo.cpp", 5480, __extension__ __PRETTY_FUNCTION__ )); | |||
5481 | ||||
5482 | if (CGM.getCodeGenOpts().EmitCodeView) { | |||
5483 | // If CodeView, emit enums as global variables, unless they are defined | |||
5484 | // inside a class. We do this because MSVC doesn't emit S_CONSTANTs for | |||
5485 | // enums in classes, and because it is difficult to attach this scope | |||
5486 | // information to the global variable. | |||
5487 | if (isa<RecordDecl>(ED->getDeclContext())) | |||
5488 | return; | |||
5489 | } else { | |||
5490 | // If not CodeView, emit DW_TAG_enumeration_type if necessary. For | |||
5491 | // example: for "enum { ZERO };", a DW_TAG_enumeration_type is created the | |||
5492 | // first time `ZERO` is referenced in a function. | |||
5493 | llvm::DIType *EDTy = | |||
5494 | getOrCreateType(QualType(ED->getTypeForDecl(), 0), Unit); | |||
5495 | assert (EDTy->getTag() == llvm::dwarf::DW_TAG_enumeration_type)(static_cast <bool> (EDTy->getTag() == llvm::dwarf:: DW_TAG_enumeration_type) ? void (0) : __assert_fail ("EDTy->getTag() == llvm::dwarf::DW_TAG_enumeration_type" , "clang/lib/CodeGen/CGDebugInfo.cpp", 5495, __extension__ __PRETTY_FUNCTION__ )); | |||
5496 | (void)EDTy; | |||
5497 | return; | |||
5498 | } | |||
5499 | } | |||
5500 | ||||
5501 | // Do not emit separate definitions for function local consts. | |||
5502 | if (isa<FunctionDecl>(VD->getDeclContext())) | |||
5503 | return; | |||
5504 | ||||
5505 | VD = cast<ValueDecl>(VD->getCanonicalDecl()); | |||
5506 | auto *VarD = dyn_cast<VarDecl>(VD); | |||
5507 | if (VarD && VarD->isStaticDataMember()) { | |||
5508 | auto *RD = cast<RecordDecl>(VarD->getDeclContext()); | |||
5509 | getDeclContextDescriptor(VarD); | |||
5510 | // Ensure that the type is retained even though it's otherwise unreferenced. | |||
5511 | // | |||
5512 | // FIXME: This is probably unnecessary, since Ty should reference RD | |||
5513 | // through its scope. | |||
5514 | RetainedTypes.push_back( | |||
5515 | CGM.getContext().getRecordType(RD).getAsOpaquePtr()); | |||
5516 | ||||
5517 | return; | |||
5518 | } | |||
5519 | llvm::DIScope *DContext = getDeclContextDescriptor(VD); | |||
5520 | ||||
5521 | auto &GV = DeclCache[VD]; | |||
5522 | if (GV) | |||
5523 | return; | |||
5524 | llvm::DIExpression *InitExpr = nullptr; | |||
5525 | if (CGM.getContext().getTypeSize(VD->getType()) <= 64) { | |||
5526 | // FIXME: Add a representation for integer constants wider than 64 bits. | |||
5527 | if (Init.isInt()) { | |||
5528 | const llvm::APSInt &InitInt = Init.getInt(); | |||
5529 | std::optional<uint64_t> InitIntOpt; | |||
5530 | if (InitInt.isUnsigned()) | |||
5531 | InitIntOpt = InitInt.tryZExtValue(); | |||
5532 | else if (auto tmp = InitInt.trySExtValue(); tmp.has_value()) | |||
5533 | // Transform a signed optional to unsigned optional. When cpp 23 comes, | |||
5534 | // use std::optional::transform | |||
5535 | InitIntOpt = (uint64_t)tmp.value(); | |||
5536 | if (InitIntOpt) | |||
5537 | InitExpr = DBuilder.createConstantValueExpression(InitIntOpt.value()); | |||
5538 | } else if (Init.isFloat()) | |||
5539 | InitExpr = DBuilder.createConstantValueExpression( | |||
5540 | Init.getFloat().bitcastToAPInt().getZExtValue()); | |||
5541 | } | |||
5542 | ||||
5543 | llvm::MDTuple *TemplateParameters = nullptr; | |||
5544 | ||||
5545 | if (isa<VarTemplateSpecializationDecl>(VD)) | |||
5546 | if (VarD) { | |||
5547 | llvm::DINodeArray parameterNodes = CollectVarTemplateParams(VarD, &*Unit); | |||
5548 | TemplateParameters = parameterNodes.get(); | |||
5549 | } | |||
5550 | ||||
5551 | GV.reset(DBuilder.createGlobalVariableExpression( | |||
5552 | DContext, Name, StringRef(), Unit, getLineNumber(VD->getLocation()), Ty, | |||
5553 | true, true, InitExpr, getOrCreateStaticDataMemberDeclarationOrNull(VarD), | |||
5554 | TemplateParameters, Align)); | |||
5555 | } | |||
5556 | ||||
5557 | void CGDebugInfo::EmitExternalVariable(llvm::GlobalVariable *Var, | |||
5558 | const VarDecl *D) { | |||
5559 | assert(CGM.getCodeGenOpts().hasReducedDebugInfo())(static_cast <bool> (CGM.getCodeGenOpts().hasReducedDebugInfo ()) ? void (0) : __assert_fail ("CGM.getCodeGenOpts().hasReducedDebugInfo()" , "clang/lib/CodeGen/CGDebugInfo.cpp", 5559, __extension__ __PRETTY_FUNCTION__ )); | |||
5560 | if (D->hasAttr<NoDebugAttr>()) | |||
5561 | return; | |||
5562 | ||||
5563 | auto Align = getDeclAlignIfRequired(D, CGM.getContext()); | |||
5564 | llvm::DIFile *Unit = getOrCreateFile(D->getLocation()); | |||
5565 | StringRef Name = D->getName(); | |||
5566 | llvm::DIType *Ty = getOrCreateType(D->getType(), Unit); | |||
5567 | ||||
5568 | llvm::DIScope *DContext = getDeclContextDescriptor(D); | |||
5569 | llvm::DIGlobalVariableExpression *GVE = | |||
5570 | DBuilder.createGlobalVariableExpression( | |||
5571 | DContext, Name, StringRef(), Unit, getLineNumber(D->getLocation()), | |||
5572 | Ty, false, false, nullptr, nullptr, nullptr, Align); | |||
5573 | Var->addDebugInfo(GVE); | |||
5574 | } | |||
5575 | ||||
5576 | void CGDebugInfo::EmitGlobalAlias(const llvm::GlobalValue *GV, | |||
5577 | const GlobalDecl GD) { | |||
5578 | ||||
5579 | assert(GV)(static_cast <bool> (GV) ? void (0) : __assert_fail ("GV" , "clang/lib/CodeGen/CGDebugInfo.cpp", 5579, __extension__ __PRETTY_FUNCTION__ )); | |||
5580 | ||||
5581 | if (!CGM.getCodeGenOpts().hasReducedDebugInfo()) | |||
5582 | return; | |||
5583 | ||||
5584 | const auto *D = cast<ValueDecl>(GD.getDecl()); | |||
5585 | if (D->hasAttr<NoDebugAttr>()) | |||
5586 | return; | |||
5587 | ||||
5588 | auto AliaseeDecl = CGM.getMangledNameDecl(GV->getName()); | |||
5589 | llvm::DINode *DI; | |||
5590 | ||||
5591 | if (!AliaseeDecl) | |||
5592 | // FIXME: Aliasee not declared yet - possibly declared later | |||
5593 | // For example, | |||
5594 | // | |||
5595 | // 1 extern int newname __attribute__((alias("oldname"))); | |||
5596 | // 2 int oldname = 1; | |||
5597 | // | |||
5598 | // No debug info would be generated for 'newname' in this case. | |||
5599 | // | |||
5600 | // Fix compiler to generate "newname" as imported_declaration | |||
5601 | // pointing to the DIE of "oldname". | |||
5602 | return; | |||
5603 | if (!(DI = getDeclarationOrDefinition( | |||
5604 | AliaseeDecl.getCanonicalDecl().getDecl()))) | |||
5605 | return; | |||
5606 | ||||
5607 | llvm::DIScope *DContext = getDeclContextDescriptor(D); | |||
5608 | auto Loc = D->getLocation(); | |||
5609 | ||||
5610 | llvm::DIImportedEntity *ImportDI = DBuilder.createImportedDeclaration( | |||
5611 | DContext, DI, getOrCreateFile(Loc), getLineNumber(Loc), D->getName()); | |||
5612 | ||||
5613 | // Record this DIE in the cache for nested declaration reference. | |||
5614 | ImportedDeclCache[GD.getCanonicalDecl().getDecl()].reset(ImportDI); | |||
5615 | } | |||
5616 | ||||
5617 | void CGDebugInfo::AddStringLiteralDebugInfo(llvm::GlobalVariable *GV, | |||
5618 | const StringLiteral *S) { | |||
5619 | SourceLocation Loc = S->getStrTokenLoc(0); | |||
5620 | PresumedLoc PLoc = CGM.getContext().getSourceManager().getPresumedLoc(Loc); | |||
5621 | if (!PLoc.isValid()) | |||
5622 | return; | |||
5623 | ||||
5624 | llvm::DIFile *File = getOrCreateFile(Loc); | |||
5625 | llvm::DIGlobalVariableExpression *Debug = | |||
5626 | DBuilder.createGlobalVariableExpression( | |||
5627 | nullptr, StringRef(), StringRef(), getOrCreateFile(Loc), | |||
5628 | getLineNumber(Loc), getOrCreateType(S->getType(), File), true); | |||
5629 | GV->addDebugInfo(Debug); | |||
5630 | } | |||
5631 | ||||
5632 | llvm::DIScope *CGDebugInfo::getCurrentContextDescriptor(const Decl *D) { | |||
5633 | if (!LexicalBlockStack.empty()) | |||
5634 | return LexicalBlockStack.back(); | |||
5635 | llvm::DIScope *Mod = getParentModuleOrNull(D); | |||
5636 | return getContextDescriptor(D, Mod ? Mod : TheCU); | |||
5637 | } | |||
5638 | ||||
5639 | void CGDebugInfo::EmitUsingDirective(const UsingDirectiveDecl &UD) { | |||
5640 | if (!CGM.getCodeGenOpts().hasReducedDebugInfo()) | |||
5641 | return; | |||
5642 | const NamespaceDecl *NSDecl = UD.getNominatedNamespace(); | |||
5643 | if (!NSDecl->isAnonymousNamespace() || | |||
5644 | CGM.getCodeGenOpts().DebugExplicitImport) { | |||
5645 | auto Loc = UD.getLocation(); | |||
5646 | if (!Loc.isValid()) | |||
5647 | Loc = CurLoc; | |||
5648 | DBuilder.createImportedModule( | |||
5649 | getCurrentContextDescriptor(cast<Decl>(UD.getDeclContext())), | |||
5650 | getOrCreateNamespace(NSDecl), getOrCreateFile(Loc), getLineNumber(Loc)); | |||
5651 | } | |||
5652 | } | |||
5653 | ||||
5654 | void CGDebugInfo::EmitUsingShadowDecl(const UsingShadowDecl &USD) { | |||
5655 | if (llvm::DINode *Target = | |||
5656 | getDeclarationOrDefinition(USD.getUnderlyingDecl())) { | |||
5657 | auto Loc = USD.getLocation(); | |||
5658 | DBuilder.createImportedDeclaration( | |||
5659 | getCurrentContextDescriptor(cast<Decl>(USD.getDeclContext())), Target, | |||
5660 | getOrCreateFile(Loc), getLineNumber(Loc)); | |||
5661 | } | |||
5662 | } | |||
5663 | ||||
5664 | void CGDebugInfo::EmitUsingDecl(const UsingDecl &UD) { | |||
5665 | if (!CGM.getCodeGenOpts().hasReducedDebugInfo()) | |||
5666 | return; | |||
5667 | assert(UD.shadow_size() &&(static_cast <bool> (UD.shadow_size() && "We shouldn't be codegening an invalid UsingDecl containing no decls" ) ? void (0) : __assert_fail ("UD.shadow_size() && \"We shouldn't be codegening an invalid UsingDecl containing no decls\"" , "clang/lib/CodeGen/CGDebugInfo.cpp", 5668, __extension__ __PRETTY_FUNCTION__ )) | |||
5668 | "We shouldn't be codegening an invalid UsingDecl containing no decls")(static_cast <bool> (UD.shadow_size() && "We shouldn't be codegening an invalid UsingDecl containing no decls" ) ? void (0) : __assert_fail ("UD.shadow_size() && \"We shouldn't be codegening an invalid UsingDecl containing no decls\"" , "clang/lib/CodeGen/CGDebugInfo.cpp", 5668, __extension__ __PRETTY_FUNCTION__ )); | |||
5669 | ||||
5670 | for (const auto *USD : UD.shadows()) { | |||
5671 | // FIXME: Skip functions with undeduced auto return type for now since we | |||
5672 | // don't currently have the plumbing for separate declarations & definitions | |||
5673 | // of free functions and mismatched types (auto in the declaration, concrete | |||
5674 | // return type in the definition) | |||
5675 | if (const auto *FD = dyn_cast<FunctionDecl>(USD->getUnderlyingDecl())) | |||
5676 | if (const auto *AT = FD->getType() | |||
5677 | ->castAs<FunctionProtoType>() | |||
5678 | ->getContainedAutoType()) | |||
5679 | if (AT->getDeducedType().isNull()) | |||
5680 | continue; | |||
5681 | ||||
5682 | EmitUsingShadowDecl(*USD); | |||
5683 | // Emitting one decl is sufficient - debuggers can detect that this is an | |||
5684 | // overloaded name & provide lookup for all the overloads. | |||
5685 | break; | |||
5686 | } | |||
5687 | } | |||
5688 | ||||
5689 | void CGDebugInfo::EmitUsingEnumDecl(const UsingEnumDecl &UD) { | |||
5690 | if (!CGM.getCodeGenOpts().hasReducedDebugInfo()) | |||
5691 | return; | |||
5692 | assert(UD.shadow_size() &&(static_cast <bool> (UD.shadow_size() && "We shouldn't be codegening an invalid UsingEnumDecl" " containing no decls") ? void (0) : __assert_fail ("UD.shadow_size() && \"We shouldn't be codegening an invalid UsingEnumDecl\" \" containing no decls\"" , "clang/lib/CodeGen/CGDebugInfo.cpp", 5694, __extension__ __PRETTY_FUNCTION__ )) | |||
5693 | "We shouldn't be codegening an invalid UsingEnumDecl"(static_cast <bool> (UD.shadow_size() && "We shouldn't be codegening an invalid UsingEnumDecl" " containing no decls") ? void (0) : __assert_fail ("UD.shadow_size() && \"We shouldn't be codegening an invalid UsingEnumDecl\" \" containing no decls\"" , "clang/lib/CodeGen/CGDebugInfo.cpp", 5694, __extension__ __PRETTY_FUNCTION__ )) | |||
5694 | " containing no decls")(static_cast <bool> (UD.shadow_size() && "We shouldn't be codegening an invalid UsingEnumDecl" " containing no decls") ? void (0) : __assert_fail ("UD.shadow_size() && \"We shouldn't be codegening an invalid UsingEnumDecl\" \" containing no decls\"" , "clang/lib/CodeGen/CGDebugInfo.cpp", 5694, __extension__ __PRETTY_FUNCTION__ )); | |||
5695 | ||||
5696 | for (const auto *USD : UD.shadows()) | |||
5697 | EmitUsingShadowDecl(*USD); | |||
5698 | } | |||
5699 | ||||
5700 | void CGDebugInfo::EmitImportDecl(const ImportDecl &ID) { | |||
5701 | if (CGM.getCodeGenOpts().getDebuggerTuning() != llvm::DebuggerKind::LLDB) | |||
5702 | return; | |||
5703 | if (Module *M = ID.getImportedModule()) { | |||
5704 | auto Info = ASTSourceDescriptor(*M); | |||
5705 | auto Loc = ID.getLocation(); | |||
5706 | DBuilder.createImportedDeclaration( | |||
5707 | getCurrentContextDescriptor(cast<Decl>(ID.getDeclContext())), | |||
5708 | getOrCreateModuleRef(Info, DebugTypeExtRefs), getOrCreateFile(Loc), | |||
5709 | getLineNumber(Loc)); | |||
5710 | } | |||
5711 | } | |||
5712 | ||||
5713 | llvm::DIImportedEntity * | |||
5714 | CGDebugInfo::EmitNamespaceAlias(const NamespaceAliasDecl &NA) { | |||
5715 | if (!CGM.getCodeGenOpts().hasReducedDebugInfo()) | |||
5716 | return nullptr; | |||
5717 | auto &VH = NamespaceAliasCache[&NA]; | |||
5718 | if (VH) | |||
5719 | return cast<llvm::DIImportedEntity>(VH); | |||
5720 | llvm::DIImportedEntity *R; | |||
5721 | auto Loc = NA.getLocation(); | |||
5722 | if (const auto *Underlying = | |||
5723 | dyn_cast<NamespaceAliasDecl>(NA.getAliasedNamespace())) | |||
5724 | // This could cache & dedup here rather than relying on metadata deduping. | |||
5725 | R = DBuilder.createImportedDeclaration( | |||
5726 | getCurrentContextDescriptor(cast<Decl>(NA.getDeclContext())), | |||
5727 | EmitNamespaceAlias(*Underlying), getOrCreateFile(Loc), | |||
5728 | getLineNumber(Loc), NA.getName()); | |||
5729 | else | |||
5730 | R = DBuilder.createImportedDeclaration( | |||
5731 | getCurrentContextDescriptor(cast<Decl>(NA.getDeclContext())), | |||
5732 | getOrCreateNamespace(cast<NamespaceDecl>(NA.getAliasedNamespace())), | |||
5733 | getOrCreateFile(Loc), getLineNumber(Loc), NA.getName()); | |||
5734 | VH.reset(R); | |||
5735 | return R; | |||
5736 | } | |||
5737 | ||||
5738 | llvm::DINamespace * | |||
5739 | CGDebugInfo::getOrCreateNamespace(const NamespaceDecl *NSDecl) { | |||
5740 | // Don't canonicalize the NamespaceDecl here: The DINamespace will be uniqued | |||
5741 | // if necessary, and this way multiple declarations of the same namespace in | |||
5742 | // different parent modules stay distinct. | |||
5743 | auto I = NamespaceCache.find(NSDecl); | |||
5744 | if (I != NamespaceCache.end()) | |||
5745 | return cast<llvm::DINamespace>(I->second); | |||
5746 | ||||
5747 | llvm::DIScope *Context = getDeclContextDescriptor(NSDecl); | |||
5748 | // Don't trust the context if it is a DIModule (see comment above). | |||
5749 | llvm::DINamespace *NS = | |||
5750 | DBuilder.createNameSpace(Context, NSDecl->getName(), NSDecl->isInline()); | |||
5751 | NamespaceCache[NSDecl].reset(NS); | |||
5752 | return NS; | |||
5753 | } | |||
5754 | ||||
5755 | void CGDebugInfo::setDwoId(uint64_t Signature) { | |||
5756 | assert(TheCU && "no main compile unit")(static_cast <bool> (TheCU && "no main compile unit" ) ? void (0) : __assert_fail ("TheCU && \"no main compile unit\"" , "clang/lib/CodeGen/CGDebugInfo.cpp", 5756, __extension__ __PRETTY_FUNCTION__ )); | |||
5757 | TheCU->setDWOId(Signature); | |||
5758 | } | |||
5759 | ||||
5760 | void CGDebugInfo::finalize() { | |||
5761 | // Creating types might create further types - invalidating the current | |||
5762 | // element and the size(), so don't cache/reference them. | |||
5763 | for (size_t i = 0; i != ObjCInterfaceCache.size(); ++i) { | |||
5764 | ObjCInterfaceCacheEntry E = ObjCInterfaceCache[i]; | |||
5765 | llvm::DIType *Ty = E.Type->getDecl()->getDefinition() | |||
5766 | ? CreateTypeDefinition(E.Type, E.Unit) | |||
5767 | : E.Decl; | |||
5768 | DBuilder.replaceTemporary(llvm::TempDIType(E.Decl), Ty); | |||
5769 | } | |||
5770 | ||||
5771 | // Add methods to interface. | |||
5772 | for (const auto &P : ObjCMethodCache) { | |||
5773 | if (P.second.empty()) | |||
5774 | continue; | |||
5775 | ||||
5776 | QualType QTy(P.first->getTypeForDecl(), 0); | |||
5777 | auto It = TypeCache.find(QTy.getAsOpaquePtr()); | |||
5778 | assert(It != TypeCache.end())(static_cast <bool> (It != TypeCache.end()) ? void (0) : __assert_fail ("It != TypeCache.end()", "clang/lib/CodeGen/CGDebugInfo.cpp" , 5778, __extension__ __PRETTY_FUNCTION__)); | |||
5779 | ||||
5780 | llvm::DICompositeType *InterfaceDecl = | |||
5781 | cast<llvm::DICompositeType>(It->second); | |||
5782 | ||||
5783 | auto CurElts = InterfaceDecl->getElements(); | |||
5784 | SmallVector<llvm::Metadata *, 16> EltTys(CurElts.begin(), CurElts.end()); | |||
5785 | ||||
5786 | // For DWARF v4 or earlier, only add objc_direct methods. | |||
5787 | for (auto &SubprogramDirect : P.second) | |||
5788 | if (CGM.getCodeGenOpts().DwarfVersion >= 5 || SubprogramDirect.getInt()) | |||
5789 | EltTys.push_back(SubprogramDirect.getPointer()); | |||
5790 | ||||
5791 | llvm::DINodeArray Elements = DBuilder.getOrCreateArray(EltTys); | |||
5792 | DBuilder.replaceArrays(InterfaceDecl, Elements); | |||
5793 | } | |||
5794 | ||||
5795 | for (const auto &P : ReplaceMap) { | |||
5796 | assert(P.second)(static_cast <bool> (P.second) ? void (0) : __assert_fail ("P.second", "clang/lib/CodeGen/CGDebugInfo.cpp", 5796, __extension__ __PRETTY_FUNCTION__)); | |||
5797 | auto *Ty = cast<llvm::DIType>(P.second); | |||
5798 | assert(Ty->isForwardDecl())(static_cast <bool> (Ty->isForwardDecl()) ? void (0) : __assert_fail ("Ty->isForwardDecl()", "clang/lib/CodeGen/CGDebugInfo.cpp" , 5798, __extension__ __PRETTY_FUNCTION__)); | |||
5799 | ||||
5800 | auto It = TypeCache.find(P.first); | |||
5801 | assert(It != TypeCache.end())(static_cast <bool> (It != TypeCache.end()) ? void (0) : __assert_fail ("It != TypeCache.end()", "clang/lib/CodeGen/CGDebugInfo.cpp" , 5801, __extension__ __PRETTY_FUNCTION__)); | |||
5802 | assert(It->second)(static_cast <bool> (It->second) ? void (0) : __assert_fail ("It->second", "clang/lib/CodeGen/CGDebugInfo.cpp", 5802, __extension__ __PRETTY_FUNCTION__)); | |||
5803 | ||||
5804 | DBuilder.replaceTemporary(llvm::TempDIType(Ty), | |||
5805 | cast<llvm::DIType>(It->second)); | |||
5806 | } | |||
5807 | ||||
5808 | for (const auto &P : FwdDeclReplaceMap) { | |||
5809 | assert(P.second)(static_cast <bool> (P.second) ? void (0) : __assert_fail ("P.second", "clang/lib/CodeGen/CGDebugInfo.cpp", 5809, __extension__ __PRETTY_FUNCTION__)); | |||
5810 | llvm::TempMDNode FwdDecl(cast<llvm::MDNode>(P.second)); | |||
5811 | llvm::Metadata *Repl; | |||
5812 | ||||
5813 | auto It = DeclCache.find(P.first); | |||
5814 | // If there has been no definition for the declaration, call RAUW | |||
5815 | // with ourselves, that will destroy the temporary MDNode and | |||
5816 | // replace it with a standard one, avoiding leaking memory. | |||
5817 | if (It == DeclCache.end()) | |||
5818 | Repl = P.second; | |||
5819 | else | |||
5820 | Repl = It->second; | |||
5821 | ||||
5822 | if (auto *GVE = dyn_cast_or_null<llvm::DIGlobalVariableExpression>(Repl)) | |||
5823 | Repl = GVE->getVariable(); | |||
5824 | DBuilder.replaceTemporary(std::move(FwdDecl), cast<llvm::MDNode>(Repl)); | |||
5825 | } | |||
5826 | ||||
5827 | // We keep our own list of retained types, because we need to look | |||
5828 | // up the final type in the type cache. | |||
5829 | for (auto &RT : RetainedTypes) | |||
5830 | if (auto MD = TypeCache[RT]) | |||
5831 | DBuilder.retainType(cast<llvm::DIType>(MD)); | |||
5832 | ||||
5833 | DBuilder.finalize(); | |||
5834 | } | |||
5835 | ||||
5836 | // Don't ignore in case of explicit cast where it is referenced indirectly. | |||
5837 | void CGDebugInfo::EmitExplicitCastType(QualType Ty) { | |||
5838 | if (CGM.getCodeGenOpts().hasReducedDebugInfo()) | |||
5839 | if (auto *DieTy = getOrCreateType(Ty, TheCU->getFile())) | |||
5840 | DBuilder.retainType(DieTy); | |||
5841 | } | |||
5842 | ||||
5843 | void CGDebugInfo::EmitAndRetainType(QualType Ty) { | |||
5844 | if (CGM.getCodeGenOpts().hasMaybeUnusedDebugInfo()) | |||
| ||||
5845 | if (auto *DieTy = getOrCreateType(Ty, TheCU->getFile())) | |||
5846 | DBuilder.retainType(DieTy); | |||
5847 | } | |||
5848 | ||||
5849 | llvm::DebugLoc CGDebugInfo::SourceLocToDebugLoc(SourceLocation Loc) { | |||
5850 | if (LexicalBlockStack.empty()) | |||
5851 | return llvm::DebugLoc(); | |||
5852 | ||||
5853 | llvm::MDNode *Scope = LexicalBlockStack.back(); | |||
5854 | return llvm::DILocation::get(CGM.getLLVMContext(), getLineNumber(Loc), | |||
5855 | getColumnNumber(Loc), Scope); | |||
5856 | } | |||
5857 | ||||
5858 | llvm::DINode::DIFlags CGDebugInfo::getCallSiteRelatedAttrs() const { | |||
5859 | // Call site-related attributes are only useful in optimized programs, and | |||
5860 | // when there's a possibility of debugging backtraces. | |||
5861 | if (!CGM.getLangOpts().Optimize || | |||
5862 | DebugKind == llvm::codegenoptions::NoDebugInfo || | |||
5863 | DebugKind == llvm::codegenoptions::LocTrackingOnly) | |||
5864 | return llvm::DINode::FlagZero; | |||
5865 | ||||
5866 | // Call site-related attributes are available in DWARF v5. Some debuggers, | |||
5867 | // while not fully DWARF v5-compliant, may accept these attributes as if they | |||
5868 | // were part of DWARF v4. | |||
5869 | bool SupportsDWARFv4Ext = | |||
5870 | CGM.getCodeGenOpts().DwarfVersion == 4 && | |||
5871 | (CGM.getCodeGenOpts().getDebuggerTuning() == llvm::DebuggerKind::LLDB || | |||
5872 | CGM.getCodeGenOpts().getDebuggerTuning() == llvm::DebuggerKind::GDB); | |||
5873 | ||||
5874 | if (!SupportsDWARFv4Ext && CGM.getCodeGenOpts().DwarfVersion < 5) | |||
5875 | return llvm::DINode::FlagZero; | |||
5876 | ||||
5877 | return llvm::DINode::FlagAllCallsDescribed; | |||
5878 | } |