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