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

source code of clang/lib/CodeGen/CGDebugInfo.cpp