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