xref: /freebsd/contrib/llvm-project/clang/lib/CodeGen/CGDebugInfo.cpp (revision e64bea71c21eb42e97aa615188ba91f6cce0d36d)
1 //===--- CGDebugInfo.cpp - Emit Debug Information for a Module ------------===//
2 //
3 // Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions.
4 // See https://llvm.org/LICENSE.txt for license information.
5 // SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception
6 //
7 //===----------------------------------------------------------------------===//
8 //
9 // This coordinates the debug information generation while generating code.
10 //
11 //===----------------------------------------------------------------------===//
12 
13 #include "CGDebugInfo.h"
14 #include "CGBlocks.h"
15 #include "CGCXXABI.h"
16 #include "CGObjCRuntime.h"
17 #include "CGRecordLayout.h"
18 #include "CodeGenFunction.h"
19 #include "CodeGenModule.h"
20 #include "ConstantEmitter.h"
21 #include "TargetInfo.h"
22 #include "clang/AST/ASTContext.h"
23 #include "clang/AST/Attr.h"
24 #include "clang/AST/DeclCXX.h"
25 #include "clang/AST/DeclFriend.h"
26 #include "clang/AST/DeclObjC.h"
27 #include "clang/AST/DeclTemplate.h"
28 #include "clang/AST/Expr.h"
29 #include "clang/AST/RecordLayout.h"
30 #include "clang/AST/RecursiveASTVisitor.h"
31 #include "clang/AST/VTableBuilder.h"
32 #include "clang/Basic/CodeGenOptions.h"
33 #include "clang/Basic/SourceManager.h"
34 #include "clang/Basic/Version.h"
35 #include "clang/CodeGen/ModuleBuilder.h"
36 #include "clang/Frontend/FrontendOptions.h"
37 #include "clang/Lex/HeaderSearchOptions.h"
38 #include "clang/Lex/ModuleMap.h"
39 #include "clang/Lex/PreprocessorOptions.h"
40 #include "llvm/ADT/DenseSet.h"
41 #include "llvm/ADT/SmallVector.h"
42 #include "llvm/ADT/StringExtras.h"
43 #include "llvm/IR/Constants.h"
44 #include "llvm/IR/DataLayout.h"
45 #include "llvm/IR/DerivedTypes.h"
46 #include "llvm/IR/Instruction.h"
47 #include "llvm/IR/Instructions.h"
48 #include "llvm/IR/Intrinsics.h"
49 #include "llvm/IR/Metadata.h"
50 #include "llvm/IR/Module.h"
51 #include "llvm/Support/MD5.h"
52 #include "llvm/Support/Path.h"
53 #include "llvm/Support/SHA1.h"
54 #include "llvm/Support/SHA256.h"
55 #include "llvm/Support/TimeProfiler.h"
56 #include <cstdint>
57 #include <optional>
58 using namespace clang;
59 using namespace clang::CodeGen;
60 
61 // TODO: consider deprecating ClArrayBoundsPseudoFn; functionality is subsumed
62 //       by -fsanitize-annotate-debug-info
63 static llvm::cl::opt<bool> ClArrayBoundsPseudoFn(
64     "array-bounds-pseudofn", llvm::cl::Hidden, llvm::cl::Optional,
65     llvm::cl::desc("Emit debug info that places array-bounds instrumentation "
66                    "in an inline function called __ubsan_check_array_bounds."));
67 
getTypeAlignIfRequired(const Type * Ty,const ASTContext & Ctx)68 static uint32_t getTypeAlignIfRequired(const Type *Ty, const ASTContext &Ctx) {
69   auto TI = Ctx.getTypeInfo(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 
getTypeAlignIfRequired(QualType Ty,const ASTContext & Ctx)82 static uint32_t getTypeAlignIfRequired(QualType Ty, const ASTContext &Ctx) {
83   return getTypeAlignIfRequired(Ty.getTypePtr(), Ctx);
84 }
85 
getDeclAlignIfRequired(const Decl * D,const ASTContext & Ctx)86 static uint32_t getDeclAlignIfRequired(const Decl *D, const ASTContext &Ctx) {
87   return D->hasAttr<AlignedAttr>() ? D->getMaxAlignment() : 0;
88 }
89 
90 /// Returns true if \ref VD is a a holding variable (aka a
91 /// VarDecl retrieved using \ref BindingDecl::getHoldingVar).
IsDecomposedVarDecl(VarDecl const * VD)92 static bool IsDecomposedVarDecl(VarDecl const *VD) {
93   auto const *Init = VD->getInit();
94   if (!Init)
95     return false;
96 
97   auto const *RefExpr =
98       llvm::dyn_cast_or_null<DeclRefExpr>(Init->IgnoreUnlessSpelledInSource());
99   if (!RefExpr)
100     return false;
101 
102   return llvm::dyn_cast_or_null<DecompositionDecl>(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.
IsArtificial(VarDecl const * VD)108 static bool IsArtificial(VarDecl const *VD) {
109   // Tuple-like bindings are marked as implicit despite
110   // being spelled out in source. Don't treat them as artificial
111   // variables.
112   if (IsDecomposedVarDecl(VD))
113     return false;
114 
115   return VD->isImplicit() || (isa<Decl>(VD->getDeclContext()) &&
116                               cast<Decl>(VD->getDeclContext())->isImplicit());
117 }
118 
CGDebugInfo(CodeGenModule & CGM)119 CGDebugInfo::CGDebugInfo(CodeGenModule &CGM)
120     : CGM(CGM), DebugKind(CGM.getCodeGenOpts().getDebugInfo()),
121       DebugTypeExtRefs(CGM.getCodeGenOpts().DebugTypeExtRefs),
122       DBuilder(CGM.getModule()) {
123   CreateCompileUnit();
124 }
125 
~CGDebugInfo()126 CGDebugInfo::~CGDebugInfo() {
127   assert(LexicalBlockStack.empty() &&
128          "Region stack mismatch, stack not empty!");
129 }
130 
addInstSourceAtomMetadata(llvm::Instruction * I,uint64_t Group,uint8_t Rank)131 void CGDebugInfo::addInstSourceAtomMetadata(llvm::Instruction *I,
132                                             uint64_t Group, uint8_t Rank) {
133   if (!I->getDebugLoc() || Group == 0 || !I->getDebugLoc()->getLine())
134     return;
135 
136   // Saturate the 3-bit rank.
137   Rank = std::min<uint8_t>(Rank, 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(Group, KeyInstructionsInfo.HighestEmittedAtom);
153 
154   // Apply the new DILocation to the instruction.
155   llvm::DILocation *NewDL = llvm::DILocation::get(
156       I->getContext(), DL.getLine(), DL.getCol(), DL.getScope(),
157       DL.getInlinedAt(), DL.isImplicitCode(), Group, Rank);
158   I->setDebugLoc(NewDL);
159 }
160 
addInstToCurrentSourceAtom(llvm::Instruction * KeyInstruction,llvm::Value * Backup)161 void CGDebugInfo::addInstToCurrentSourceAtom(llvm::Instruction *KeyInstruction,
162                                              llvm::Value *Backup) {
163   addInstToSpecificSourceAtom(KeyInstruction, Backup,
164                               KeyInstructionsInfo.CurrentAtom);
165 }
166 
addInstToSpecificSourceAtom(llvm::Instruction * KeyInstruction,llvm::Value * Backup,uint64_t Group)167 void CGDebugInfo::addInstToSpecificSourceAtom(llvm::Instruction *KeyInstruction,
168                                               llvm::Value *Backup,
169                                               uint64_t Group) {
170   if (!Group || !CGM.getCodeGenOpts().DebugKeyInstructions)
171     return;
172 
173   llvm::DISubprogram *SP = KeyInstruction->getFunction()->getSubprogram();
174   if (!SP || !SP->getKeyInstructionsEnabled())
175     return;
176 
177   addInstSourceAtomMetadata(KeyInstruction, Group, /*Rank=*/1);
178 
179   llvm::Instruction *BackupI =
180       llvm::dyn_cast_or_null<llvm::Instruction>(Backup);
181   if (!BackupI)
182     return;
183 
184   // Add the backup instruction to the group.
185   addInstSourceAtomMetadata(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>(BackupI)) {
192     BackupI = dyn_cast<llvm::Instruction>(Cast->getOperand(0));
193     if (!BackupI)
194       break;
195     addInstSourceAtomMetadata(BackupI, Group, Rank++);
196   }
197 }
198 
completeFunction()199 void 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 
ApplyAtomGroup(CGDebugInfo * DI)206 ApplyAtomGroup::ApplyAtomGroup(CGDebugInfo *DI) : DI(DI) {
207   if (!DI)
208     return;
209   OriginalAtom = DI->KeyInstructionsInfo.CurrentAtom;
210   DI->KeyInstructionsInfo.CurrentAtom = DI->KeyInstructionsInfo.NextAtom++;
211 }
212 
~ApplyAtomGroup()213 ApplyAtomGroup::~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(DI->KeyInstructionsInfo.HighestEmittedAtom + 1,
220                DI->KeyInstructionsInfo.NextAtom);
221 
222   DI->KeyInstructionsInfo.CurrentAtom = OriginalAtom;
223 }
224 
ApplyDebugLocation(CodeGenFunction & CGF,SourceLocation TemporaryLocation)225 ApplyDebugLocation::ApplyDebugLocation(CodeGenFunction &CGF,
226                                        SourceLocation TemporaryLocation)
227     : CGF(&CGF) {
228   init(TemporaryLocation);
229 }
230 
ApplyDebugLocation(CodeGenFunction & CGF,bool DefaultToEmpty,SourceLocation TemporaryLocation)231 ApplyDebugLocation::ApplyDebugLocation(CodeGenFunction &CGF,
232                                        bool DefaultToEmpty,
233                                        SourceLocation TemporaryLocation)
234     : CGF(&CGF) {
235   init(TemporaryLocation, DefaultToEmpty);
236 }
237 
init(SourceLocation TemporaryLocation,bool DefaultToEmpty)238 void 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(CGF->Builder, 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(DI->LexicalBlockStack.back()->getContext(), 0, 0,
265                             DI->LexicalBlockStack.back(), DI->getInlinedAt()));
266 }
267 
ApplyDebugLocation(CodeGenFunction & CGF,const Expr * E)268 ApplyDebugLocation::ApplyDebugLocation(CodeGenFunction &CGF, const Expr *E)
269     : CGF(&CGF) {
270   init(E->getExprLoc());
271 }
272 
ApplyDebugLocation(CodeGenFunction & CGF,llvm::DebugLoc Loc)273 ApplyDebugLocation::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(Loc->getContext(), Loc.getLine(),
285                                   Loc->getColumn(), Loc->getScope(),
286                                   Loc->getInlinedAt(), Loc.isImplicitCode());
287     CGF.Builder.SetCurrentDebugLocation(std::move(Loc));
288   }
289 }
290 
~ApplyDebugLocation()291 ApplyDebugLocation::~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 
ApplyInlineDebugLocation(CodeGenFunction & CGF,GlobalDecl InlinedFn)298 ApplyInlineDebugLocation::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(CGF.Builder, InlinedFn);
312 }
313 
~ApplyInlineDebugLocation()314 ApplyInlineDebugLocation::~ApplyInlineDebugLocation() {
315   if (!CGF)
316     return;
317   auto &DI = *CGF->getDebugInfo();
318   DI.EmitInlineFunctionEnd(CGF->Builder);
319   DI.EmitLocation(CGF->Builder, SavedLocation);
320 }
321 
setLocation(SourceLocation Loc)322 void 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>(LexicalBlockStack.back());
337   PresumedLoc PCLoc = SM.getPresumedLoc(CurLoc);
338   if (PCLoc.isInvalid() || Scope->getFile() == getOrCreateFile(CurLoc))
339     return;
340 
341   if (auto *LBF = dyn_cast<llvm::DILexicalBlockFile>(Scope)) {
342     LexicalBlockStack.pop_back();
343     LexicalBlockStack.emplace_back(DBuilder.createLexicalBlockFile(
344         LBF->getScope(), getOrCreateFile(CurLoc)));
345   } else if (isa<llvm::DILexicalBlock>(Scope) ||
346              isa<llvm::DISubprogram>(Scope)) {
347     LexicalBlockStack.pop_back();
348     LexicalBlockStack.emplace_back(
349         DBuilder.createLexicalBlockFile(Scope, getOrCreateFile(CurLoc)));
350   }
351 }
352 
getDeclContextDescriptor(const Decl * D)353 llvm::DIScope *CGDebugInfo::getDeclContextDescriptor(const Decl *D) {
354   llvm::DIScope *Mod = getParentModuleOrNull(D);
355   return getContextDescriptor(cast<Decl>(D->getDeclContext()),
356                               Mod ? Mod : TheCU);
357 }
358 
getContextDescriptor(const Decl * Context,llvm::DIScope * Default)359 llvm::DIScope *CGDebugInfo::getContextDescriptor(const Decl *Context,
360                                                  llvm::DIScope *Default) {
361   if (!Context)
362     return Default;
363 
364   auto I = RegionMap.find(Context);
365   if (I != RegionMap.end()) {
366     llvm::Metadata *V = I->second;
367     return dyn_cast_or_null<llvm::DIScope>(V);
368   }
369 
370   // Check namespace.
371   if (const auto *NSDecl = dyn_cast<NamespaceDecl>(Context))
372     return getOrCreateNamespace(NSDecl);
373 
374   if (const auto *RDecl = dyn_cast<RecordDecl>(Context))
375     if (!RDecl->isDependentType())
376       return getOrCreateType(CGM.getContext().getTypeDeclType(RDecl),
377                              TheCU->getFile());
378   return Default;
379 }
380 
getPrintingPolicy() const381 PrintingPolicy 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 
getFunctionName(const FunctionDecl * FD)410 StringRef CGDebugInfo::getFunctionName(const FunctionDecl *FD) {
411   return internString(GetName(FD));
412 }
413 
getObjCMethodName(const ObjCMethodDecl * OMD)414 StringRef 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>(DC)) {
420     OS << OID->getName();
421   } else if (const auto *OID = dyn_cast<ObjCInterfaceDecl>(DC)) {
422     OS << OID->getName();
423   } else if (const auto *OC = dyn_cast<ObjCCategoryDecl>(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>(DC)) {
431     OS << OCD->getClassInterface()->getName() << '(' << OCD->getName() << ')';
432   }
433   OS << ' ' << OMD->getSelector().getAsString() << ']';
434 
435   return internString(OS.str());
436 }
437 
getSelectorName(Selector S)438 StringRef CGDebugInfo::getSelectorName(Selector S) {
439   return internString(S.getAsString());
440 }
441 
getClassName(const RecordDecl * RD)442 StringRef CGDebugInfo::getClassName(const RecordDecl *RD) {
443   if (isa<ClassTemplateSpecializationDecl>(RD)) {
444     // Copy this name on the side and use its reference.
445     return internString(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(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(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>(RD))
481         if (CXXRD->isLambda())
482           return internString(
483               CGM.getCXXABI().getMangleContext().getLambdaString(CXXRD));
484 
485       if (!Name.empty()) {
486         SmallString<256> UnnamedType("<unnamed-type-");
487         UnnamedType += Name;
488         UnnamedType += '>';
489         return internString(UnnamedType);
490       }
491     }
492   }
493 
494   return StringRef();
495 }
496 
497 std::optional<llvm::DIFile::ChecksumKind>
computeChecksum(FileID FID,SmallString<64> & Checksum) const498 CGDebugInfo::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(MemBuffer->getBuffer());
511   switch (CGM.getCodeGenOpts().getDebugSrcHash()) {
512   case clang::CodeGenOptions::DSH_MD5:
513     llvm::toHex(llvm::MD5::hash(Data), /*LowerCase=*/true, Checksum);
514     return llvm::DIFile::CSK_MD5;
515   case clang::CodeGenOptions::DSH_SHA1:
516     llvm::toHex(llvm::SHA1::hash(Data), /*LowerCase=*/true, Checksum);
517     return llvm::DIFile::CSK_SHA1;
518   case clang::CodeGenOptions::DSH_SHA256:
519     llvm::toHex(llvm::SHA256::hash(Data), /*LowerCase=*/true, 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 
getSource(const SourceManager & SM,FileID FID)527 std::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, &SourceInvalid);
534 
535   if (SourceInvalid)
536     return std::nullopt;
537 
538   return Source;
539 }
540 
getOrCreateFile(SourceLocation Loc)541 llvm::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(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>(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(*CSKind, Checksum);
580   }
581   return createFile(FileName, CSInfo, getSource(SM, SM.getFileID(Loc)));
582 }
583 
createFile(StringRef FileName,std::optional<llvm::DIFile::ChecksumInfo<StringRef>> CSInfo,std::optional<StringRef> Source)584 llvm::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(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(RemappedFile);
598     auto FileE = llvm::sys::path::end(RemappedFile);
599     auto CurDirIt = llvm::sys::path::begin(CurDir);
600     auto CurDirE = llvm::sys::path::end(CurDir);
601     for (; CurDirIt != CurDirE && *CurDirIt == *FileIt; ++CurDirIt, ++FileIt)
602       llvm::sys::path::append(DirBuf, *CurDirIt);
603     if (llvm::sys::path::root_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(FileBuf, *FileIt);
611       Dir = DirBuf;
612       File = FileBuf;
613     }
614   } else {
615     if (!llvm::sys::path::is_absolute(FileName))
616       Dir = CurDir;
617     File = RemappedFile;
618   }
619   llvm::DIFile *F = DBuilder.createFile(File, Dir, CSInfo, Source);
620   DIFileCache[FileName.data()].reset(F);
621   return F;
622 }
623 
remapDIPath(StringRef Path) const624 std::string CGDebugInfo::remapDIPath(StringRef Path) const {
625   SmallString<256> P = Path;
626   for (auto &[From, To] : llvm::reverse(CGM.getCodeGenOpts().DebugPrefixMap))
627     if (llvm::sys::path::replace_path_prefix(P, From, To))
628       break;
629   return P.str().str();
630 }
631 
getLineNumber(SourceLocation Loc)632 unsigned 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 
getColumnNumber(SourceLocation Loc,bool Force)639 unsigned 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.isValid() ? Loc : CurLoc);
649   return PLoc.isValid() ? PLoc.getColumn() : 0;
650 }
651 
getCurrentDirname()652 StringRef 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(*CWD);
663 }
664 
CreateCompileUnit()665 void 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(SM.getMainFileID())) {
692     MainFileDir = std::string(MainFile->getDir().getName());
693     if (!llvm::sys::path::is_absolute(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(MainFileDirSS, Style, MainFileName);
702       MainFileName = std::string(
703           llvm::sys::path::remove_leading_dotslash(MainFileDirSS, 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             MainFile->getName().rsplit('.').second)
713             .isPreprocessed()) {
714       MainFileName = CGM.getModule().getName().str();
715     } else {
716       CSKind = computeChecksum(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(*CSKind, Checksum);
780   llvm::DIFile *CUFile = DBuilder.createFile(
781       remapDIPath(MainFileName), remapDIPath(getCurrentDirname()), CSInfo,
782       getSource(SM, 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(Sysroot);
788     auto E = llvm::sys::path::rend(Sysroot);
789     auto It =
790         std::find_if(B, E, [](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       LangTag, CUFile, CGOpts.EmitVersionIdentMetadata ? Producer : "",
806       LO.Optimize || CGOpts.PrepareForLTO || CGOpts.PrepareForThinLTO,
807       CGOpts.DwarfDebugFlags, RuntimeVers, CGOpts.SplitDwarfFile, EmissionKind,
808       DwoId, CGOpts.SplitDwarfInlining, CGOpts.DebugInfoForProfiling,
809       NameTableKind, CGOpts.DebugRangesBaseAddress, remapDIPath(Sysroot), SDK);
810 }
811 
CreateType(const BuiltinType * BT)812 llvm::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(llvm::dwarf::DW_TAG_structure_type,
829                                      "objc_class", TheCU, TheCU->getFile(), 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(llvm::dwarf::DW_TAG_structure_type,
843                                      "objc_class", TheCU, TheCU->getFile(), 0);
844 
845     unsigned Size = CGM.getContext().getTypeSize(CGM.getContext().VoidPtrTy);
846 
847     auto *ISATy = DBuilder.createPointerType(ClassTy, Size);
848 
849     ObjTy = DBuilder.createStructType(TheCU, "objc_object", TheCU->getFile(), 0,
850                                       (uint64_t)0, 0, llvm::DINode::FlagZero,
851                                       nullptr, llvm::DINodeArray());
852 
853     DBuilder.replaceArrays(
854         ObjTy, DBuilder.getOrCreateArray(&*DBuilder.createMemberType(
855                    ObjTy, "isa", TheCU->getFile(), 0, Size, 0, 0,
856                    llvm::DINode::FlagZero, ISATy)));
857     return ObjTy;
858   }
859   case BuiltinType::ObjCSel: {
860     if (!SelTy)
861       SelTy = DBuilder.createForwardDecl(llvm::dwarf::DW_TAG_structure_type,
862                                          "objc_selector", TheCU,
863                                          TheCU->getFile(), 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("opencl_sampler_t", OCLSamplerDITy);
874   case BuiltinType::OCLEvent:
875     return getOrCreateStructPtrType("opencl_event_t", OCLEventDITy);
876   case BuiltinType::OCLClkEvent:
877     return getOrCreateStructPtrType("opencl_clk_event_t", OCLClkEventDITy);
878   case BuiltinType::OCLQueue:
879     return getOrCreateStructPtrType("opencl_queue_t", OCLQueueDITy);
880   case BuiltinType::OCLReserveID:
881     return getOrCreateStructPtrType("opencl_reserve_id_t", 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(CGM.getLangOpts());
897         // Bit size and offset of the type.
898         uint64_t Size = CGM.getContext().getTypeSize(BT);
899         return DBuilder.createBasicType(BTName, 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(16),
906                     1)
907               : CGM.getContext().getBuiltinVectorTypeInfo(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(llvm::ConstantInt::getSigned(
926           llvm::Type::getInt64Ty(CGM.getLLVMContext()), 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(Expr);
934       } else
935         UpperBound = llvm::ConstantAsMetadata::get(llvm::ConstantInt::getSigned(
936             llvm::Type::getInt64Ty(CGM.getLLVMContext()), NumElems - 1));
937 
938       llvm::Metadata *Subscript = DBuilder.getOrCreateSubrange(
939           /*count*/ nullptr, LowerBound, UpperBound, /*stride*/ nullptr);
940       llvm::DINodeArray SubscriptArray = DBuilder.getOrCreateArray(Subscript);
941       llvm::DIType *ElemTy =
942           getOrCreateType(Info.ElementType, TheCU->getFile());
943       auto Align = getTypeAlignIfRequired(BT, CGM.getContext());
944       return DBuilder.createVectorType(/*Size*/ 0, Align, ElemTy,
945                                        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(cast<const BuiltinType>(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(BT);
959 
960       unsigned ElementCount = Info.EC.getKnownMinValue();
961       unsigned SEW = CGM.getContext().getTypeSize(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(llvm::dwarf::DW_OP_div);
991       else
992         Expr.push_back(llvm::dwarf::DW_OP_mul);
993       // NFIELDS multiplier
994       if (NFIELDS > 1)
995         Expr.append({llvm::dwarf::DW_OP_constu, NFIELDS, llvm::dwarf::DW_OP_mul});
996       // Element max index = count - 1
997       Expr.append({llvm::dwarf::DW_OP_constu, 1, llvm::dwarf::DW_OP_minus});
998 
999       auto *LowerBound =
1000           llvm::ConstantAsMetadata::get(llvm::ConstantInt::getSigned(
1001               llvm::Type::getInt64Ty(CGM.getLLVMContext()), 0));
1002       auto *UpperBound = DBuilder.createExpression(Expr);
1003       llvm::Metadata *Subscript = DBuilder.getOrCreateSubrange(
1004           /*count*/ nullptr, LowerBound, UpperBound, /*stride*/ nullptr);
1005       llvm::DINodeArray SubscriptArray = DBuilder.getOrCreateArray(Subscript);
1006       llvm::DIType *ElemTy =
1007           getOrCreateType(Info.ElementType, TheCU->getFile());
1008 
1009       auto Align = getTypeAlignIfRequired(BT, CGM.getContext());
1010       return DBuilder.createVectorType(/*Size=*/0, Align, ElemTy,
1011                                        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(CGM.getLangOpts());
1117   // Bit size and offset of the type.
1118   uint64_t Size = CGM.getContext().getTypeSize(BT);
1119   return DBuilder.createBasicType(BTName, Size, Encoding);
1120 }
1121 
CreateType(const BitIntType * Ty)1122 llvm::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, CGM.getContext().getTypeSize(Ty),
1130                                   Encoding);
1131 }
1132 
CreateType(const ComplexType * Ty)1133 llvm::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(Ty);
1140   return DBuilder.createBasicType("complex", Size, Encoding);
1141 }
1142 
stripUnusedQualifiers(Qualifiers & Q)1143 static void stripUnusedQualifiers(Qualifiers &Q) {
1144   // Ignore these qualifiers for now.
1145   Q.removeObjCGCAttr();
1146   Q.removeAddressSpace();
1147   Q.removeObjCLifetime();
1148   Q.removeUnaligned();
1149 }
1150 
getNextQualifier(Qualifiers & Q)1151 static 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 
CreateQualifiedType(QualType Ty,llvm::DIFile * Unit)1167 llvm::DIType *CGDebugInfo::CreateQualifiedType(QualType Ty,
1168                                                llvm::DIFile *Unit) {
1169   QualifierCollector Qc;
1170   const Type *T = Qc.strip(Ty);
1171 
1172   stripUnusedQualifiers(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(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(QualType(T, 0), Unit);
1188       return DBuilder.createPtrAuthQualifiedType(FromTy, Key, IsDiscr,
1189                                                  ExtraDiscr, IsaPointer,
1190                                                  AuthenticatesNullValues);
1191     } else {
1192       assert(Qc.empty() && "Unknown type qualifier for debug info");
1193       return getOrCreateType(QualType(T, 0), Unit);
1194     }
1195   }
1196 
1197   auto *FromTy = getOrCreateType(Qc.apply(CGM.getContext(), T), 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 
CreateQualifiedType(const FunctionProtoType * F,llvm::DIFile * Unit)1204 llvm::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(CGM.getContext().getFunctionType(F->getReturnType(),
1220                                                        F->getParamTypes(), EPI),
1221                       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 
CreateType(const ObjCObjectPointerType * Ty,llvm::DIFile * Unit)1228 llvm::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(CGM.getContext().getObjCIdType(), Unit);
1236 
1237   return CreatePointerLikeType(llvm::dwarf::DW_TAG_pointer_type, Ty,
1238                                Ty->getPointeeType(), Unit);
1239 }
1240 
CreateType(const PointerType * Ty,llvm::DIFile * Unit)1241 llvm::DIType *CGDebugInfo::CreateType(const PointerType *Ty,
1242                                       llvm::DIFile *Unit) {
1243   return CreatePointerLikeType(llvm::dwarf::DW_TAG_pointer_type, Ty,
1244                                Ty->getPointeeType(), Unit);
1245 }
1246 
1247 /// \return whether a C++ mangling exists for the type defined by TD.
hasCXXMangling(const TagDecl * TD,llvm::DICompileUnit * TheCU)1248 static 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>(TD) || isa<EnumDecl>(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.
needsTypeIdentifier(const TagDecl * TD,CodeGenModule & CGM,llvm::DICompileUnit * TheCU)1282 static 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.
getTypeIdentifier(const TagType * Ty,CodeGenModule & CGM,llvm::DICompileUnit * TheCU)1300 static 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>(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(QualType(Ty, 0), Out);
1317   return Identifier;
1318 }
1319 
1320 /// \return the appropriate DWARF tag for a composite type.
getTagForRecord(const RecordDecl * RD)1321 static 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 
1336 llvm::DICompositeType *
getOrCreateRecordFwdDecl(const RecordType * Ty,llvm::DIScope * Ctx)1337 CGDebugInfo::getOrCreateRecordFwdDecl(const RecordType *Ty,
1338                                       llvm::DIScope *Ctx) {
1339   const RecordDecl *RD = Ty->getDecl();
1340   if (llvm::DIType *T = getTypeOrNull(CGM.getContext().getRecordType(RD)))
1341     return cast<llvm::DICompositeType>(T);
1342   llvm::DIFile *DefUnit = getOrCreateFile(RD->getLocation());
1343   const unsigned Line =
1344       getLineNumber(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(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>(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       getTagForRecord(RD), RDName, Ctx, DefUnit, Line, 0, Size, Align, Flags,
1371       Identifier);
1372   if (CGM.getCodeGenOpts().DebugFwdTemplateParams)
1373     if (auto *TSpecial = dyn_cast<ClassTemplateSpecializationDecl>(RD))
1374       DBuilder.replaceArrays(RetTy, llvm::DINodeArray(),
1375                              CollectCXXTemplateParams(TSpecial, DefUnit));
1376   ReplaceMap.emplace_back(
1377       std::piecewise_construct, std::make_tuple(Ty),
1378       std::make_tuple(static_cast<llvm::Metadata *>(RetTy)));
1379   return RetTy;
1380 }
1381 
CreatePointerLikeType(llvm::dwarf::Tag Tag,const Type * Ty,QualType PointeeTy,llvm::DIFile * Unit)1382 llvm::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(Ty);
1389   auto Align = getTypeAlignIfRequired(Ty, CGM.getContext());
1390   std::optional<unsigned> DWARFAddressSpace =
1391       CGM.getTarget().getDWARFAddressSpace(
1392           CGM.getTypes().getTargetAddressSpace(PointeeTy));
1393 
1394   const BTFTagAttributedType *BTFAttrTy;
1395   if (auto *Atomic = PointeeTy->getAs<AtomicType>())
1396     BTFAttrTy = dyn_cast<BTFTagAttributedType>(Atomic->getValueType());
1397   else
1398     BTFAttrTy = dyn_cast<BTFTagAttributedType>(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(CGM.getLLVMContext(), StringRef("btf_type_tag")),
1405           llvm::MDString::get(CGM.getLLVMContext(), Tag)};
1406       Annots.insert(Annots.begin(),
1407                     llvm::MDNode::get(CGM.getLLVMContext(), Ops));
1408     }
1409     BTFAttrTy = dyn_cast<BTFTagAttributedType>(BTFAttrTy->getWrappedType());
1410   }
1411 
1412   llvm::DINodeArray Annotations = nullptr;
1413   if (Annots.size() > 0)
1414     Annotations = DBuilder.getOrCreateArray(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, getOrCreateType(PointeeTy, Unit),
1419                                         Size, Align, DWARFAddressSpace);
1420   else
1421     return DBuilder.createPointerType(getOrCreateType(PointeeTy, Unit), Size,
1422                                       Align, DWARFAddressSpace, StringRef(),
1423                                       Annotations);
1424 }
1425 
getOrCreateStructPtrType(StringRef Name,llvm::DIType * & Cache)1426 llvm::DIType *CGDebugInfo::getOrCreateStructPtrType(StringRef Name,
1427                                                     llvm::DIType *&Cache) {
1428   if (Cache)
1429     return Cache;
1430   Cache = DBuilder.createForwardDecl(llvm::dwarf::DW_TAG_structure_type, Name,
1431                                      TheCU, TheCU->getFile(), 0);
1432   unsigned Size = CGM.getContext().getTypeSize(CGM.getContext().VoidPtrTy);
1433   Cache = DBuilder.createPointerType(Cache, Size);
1434   return Cache;
1435 }
1436 
collectDefaultElementTypesForBlockPointer(const BlockPointerType * Ty,llvm::DIFile * Unit,llvm::DIDerivedType * DescTy,unsigned LineNo,SmallVectorImpl<llvm::Metadata * > & EltTys)1437 uint64_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(CreateMemberType(Unit, FType, "__size", &FieldOffset));
1452     EltTys.push_back(CreateMemberType(Unit, FType, "__align", &FieldOffset));
1453   } else {
1454     FType = CGM.getContext().getPointerType(CGM.getContext().VoidTy);
1455     EltTys.push_back(CreateMemberType(Unit, FType, "__isa", &FieldOffset));
1456     FType = CGM.getContext().IntTy;
1457     EltTys.push_back(CreateMemberType(Unit, FType, "__flags", &FieldOffset));
1458     EltTys.push_back(CreateMemberType(Unit, FType, "__reserved", &FieldOffset));
1459     FType = CGM.getContext().getPointerType(Ty->getPointeeType());
1460     EltTys.push_back(CreateMemberType(Unit, FType, "__FuncPtr", &FieldOffset));
1461     FType = CGM.getContext().getPointerType(CGM.getContext().VoidTy);
1462     uint64_t FieldSize = CGM.getContext().getTypeSize(Ty);
1463     uint32_t FieldAlign = CGM.getContext().getTypeAlign(Ty);
1464     EltTys.push_back(DBuilder.createMemberType(
1465         Unit, "__descriptor", nullptr, LineNo, FieldSize, FieldAlign,
1466         FieldOffset, llvm::DINode::FlagZero, DescTy));
1467     FieldOffset += FieldSize;
1468   }
1469 
1470   return FieldOffset;
1471 }
1472 
CreateType(const BlockPointerType * Ty,llvm::DIFile * Unit)1473 llvm::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(CreateMemberType(Unit, FType, "reserved", &FieldOffset));
1483   EltTys.push_back(CreateMemberType(Unit, FType, "Size", &FieldOffset));
1484 
1485   Elements = DBuilder.getOrCreateArray(EltTys);
1486   EltTys.clear();
1487 
1488   llvm::DINode::DIFlags Flags = llvm::DINode::FlagAppleBlock;
1489 
1490   auto *EltTy =
1491       DBuilder.createStructType(Unit, "__block_descriptor", nullptr, 0,
1492                                 FieldOffset, 0, Flags, nullptr, Elements);
1493 
1494   // Bit size, align and offset of the type.
1495   uint64_t Size = CGM.getContext().getTypeSize(Ty);
1496 
1497   auto *DescTy = DBuilder.createPointerType(EltTy, Size);
1498 
1499   FieldOffset = collectDefaultElementTypesForBlockPointer(Ty, Unit, DescTy,
1500                                                           0, EltTys);
1501 
1502   Elements = DBuilder.getOrCreateArray(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(Unit, "", nullptr, 0, FieldOffset, 0,
1509                                     Flags, nullptr, Elements);
1510 
1511   return DBuilder.createPointerType(EltTy, Size);
1512 }
1513 
1514 static llvm::SmallVector<TemplateArgument>
GetTemplateArgs(const TemplateDecl * TD,const TemplateSpecializationType * Ty)1515 GetTemplateArgs(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(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(SubstArgs.front());
1547     SubstArgs = SubstArgs.drop_front();
1548   }
1549   return SpecArgs;
1550 }
1551 
CreateType(const TemplateSpecializationType * Ty,llvm::DIFile * Unit)1552 llvm::DIType *CGDebugInfo::CreateType(const TemplateSpecializationType *Ty,
1553                                       llvm::DIFile *Unit) {
1554   assert(Ty->isTypeAlias());
1555   llvm::DIType *Src = getOrCreateType(Ty->getAliasedType(), Unit);
1556 
1557   const TemplateDecl *TD = Ty->getTemplateName().getAsTemplateDecl();
1558   if (isa<BuiltinTemplateDecl>(TD))
1559     return Src;
1560 
1561   const auto *AliasDecl = cast<TypeAliasTemplateDecl>(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, PP, TemplateName::Qualified::None);
1570 
1571   SourceLocation Loc = AliasDecl->getLocation();
1572 
1573   if (CGM.getCodeGenOpts().DebugTemplateAlias) {
1574     auto ArgVector = ::GetTemplateArgs(TD, Ty);
1575     TemplateArgs Args = {TD->getTemplateParameters(), 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, PP, /*Qualified=*/false);
1585     if (CGM.getCodeGenOpts().getDebugSimpleTemplateNames() !=
1586             llvm::codegenoptions::DebugTemplateNamesKind::Simple ||
1587         !HasReconstitutableArgs(Args.Args))
1588       printTemplateArgumentList(OS, Args.Args, PP);
1589 
1590     llvm::DIDerivedType *AliasTy = DBuilder.createTemplateAlias(
1591         Src, Name, getOrCreateFile(Loc), getLineNumber(Loc),
1592         getDeclContextDescriptor(AliasDecl), CollectTemplateParams(Args, Unit));
1593     return AliasTy;
1594   }
1595 
1596   printTemplateArgumentList(OS, Ty->template_arguments(), PP,
1597                             TD->getTemplateParameters());
1598   return DBuilder.createTypedef(Src, OS.str(), getOrCreateFile(Loc),
1599                                 getLineNumber(Loc),
1600                                 getDeclContextDescriptor(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.
getAccessFlag(AccessSpecifier Access,const RecordDecl * RD)1606 static 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 
CreateType(const TypedefType * Ty,llvm::DIFile * Unit)1630 llvm::DIType *CGDebugInfo::CreateType(const TypedefType *Ty,
1631                                       llvm::DIFile *Unit) {
1632   llvm::DIType *Underlying =
1633       getOrCreateType(Ty->getDecl()->getUnderlyingType(), 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(Ty->getDecl(), CGM.getContext());
1643   // Typedefs are derived from some other type.
1644   llvm::DINodeArray Annotations = CollectBTFDeclTagAnnotations(Ty->getDecl());
1645 
1646   llvm::DINode::DIFlags Flags = llvm::DINode::FlagZero;
1647   const DeclContext *DC = Ty->getDecl()->getDeclContext();
1648   if (isa<RecordDecl>(DC))
1649     Flags = getAccessFlag(Ty->getDecl()->getAccess(), cast<RecordDecl>(DC));
1650 
1651   return DBuilder.createTypedef(Underlying, Ty->getDecl()->getName(),
1652                                 getOrCreateFile(Loc), getLineNumber(Loc),
1653                                 getDeclContextDescriptor(Ty->getDecl()), Align,
1654                                 Flags, Annotations);
1655 }
1656 
getDwarfCC(CallingConv CC)1657 static 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 
getRefFlags(const FunctionProtoType * Func)1724 static 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 
CreateType(const FunctionType * Ty,llvm::DIFile * Unit)1733 llvm::DIType *CGDebugInfo::CreateType(const FunctionType *Ty,
1734                                       llvm::DIFile *Unit) {
1735   const auto *FPT = dyn_cast<FunctionProtoType>(Ty);
1736   if (FPT) {
1737     if (llvm::DIType *QTy = CreateQualifiedType(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(getOrCreateType(Ty->getReturnType(), 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(DBuilder.createUnspecifiedParameter());
1753   } else {
1754     Flags = getRefFlags(FPT);
1755     for (const QualType &ParamType : FPT->param_types())
1756       EltTys.push_back(getOrCreateType(ParamType, Unit));
1757     if (FPT->isVariadic())
1758       EltTys.push_back(DBuilder.createUnspecifiedParameter());
1759   }
1760 
1761   llvm::DITypeRefArray EltTypeArray = DBuilder.getOrCreateTypeArray(EltTys);
1762   llvm::DIType *F = DBuilder.createSubroutineType(
1763       EltTypeArray, Flags, getDwarfCC(Ty->getCallConv()));
1764   return F;
1765 }
1766 
1767 llvm::DIDerivedType *
createBitFieldType(const FieldDecl * BitFieldDecl,llvm::DIScope * RecordTy,const RecordDecl * RD)1768 CGDebugInfo::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, 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(BitFieldDecl);
1784   uint64_t SizeInBits = BitFieldInfo.Size;
1785   assert(SizeInBits > 0 && "found named 0-width bitfield");
1786   uint64_t StorageOffsetInBits =
1787       CGM.getContext().toBits(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(BitFieldDecl->getAccess(), RD);
1796   llvm::DINodeArray Annotations = CollectBTFDeclTagAnnotations(BitFieldDecl);
1797   return DBuilder.createBitFieldMemberType(
1798       RecordTy, Name, File, Line, SizeInBits, OffsetInBits, StorageOffsetInBits,
1799       Flags, DebugType, Annotations);
1800 }
1801 
createBitFieldSeparatorIfNeeded(const FieldDecl * BitFieldDecl,const llvm::DIDerivedType * BitFieldDI,llvm::ArrayRef<llvm::Metadata * > PreviousFieldsDI,const RecordDecl * RD)1802 llvm::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>(PreviousMDEntry);
1839   if (!PreviousMDField || !PreviousMDField->isBitField() ||
1840       PreviousMDField->getSizeInBits() == 0)
1841     return nullptr;
1842 
1843   auto PreviousBitfield = RD->field_begin();
1844   std::advance(PreviousBitfield, 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, 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>(BitFieldDI->getStorageOffsetInBits())
1862           ->getZExtValue();
1863 
1864   llvm::DINode::DIFlags Flags =
1865       getAccessFlag(PreviousBitfield->getAccess(), RD);
1866   llvm::DINodeArray Annotations =
1867       CollectBTFDeclTagAnnotations(*PreviousBitfield);
1868   return DBuilder.createBitFieldMemberType(
1869       RecordTy, "", File, Line, 0, StorageOffsetInBits, StorageOffsetInBits,
1870       Flags, DebugType, Annotations);
1871 }
1872 
createFieldType(StringRef name,QualType type,SourceLocation loc,AccessSpecifier AS,uint64_t offsetInBits,uint32_t AlignInBits,llvm::DIFile * tunit,llvm::DIScope * scope,const RecordDecl * RD,llvm::DINodeArray Annotations)1873 llvm::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(type, tunit);
1878 
1879   // Get the location for the field.
1880   llvm::DIFile *file = getOrCreateFile(loc);
1881   const unsigned line = getLineNumber(loc.isValid() ? loc : CurLoc);
1882 
1883   uint64_t SizeInBits = 0;
1884   auto Align = AlignInBits;
1885   if (!type->isIncompleteArrayType()) {
1886     TypeInfo TI = CGM.getContext().getTypeInfo(type);
1887     SizeInBits = TI.Width;
1888     if (!Align)
1889       Align = getTypeAlignIfRequired(type, CGM.getContext());
1890   }
1891 
1892   llvm::DINode::DIFlags flags = getAccessFlag(AS, RD);
1893   return DBuilder.createMemberType(scope, name, file, line, SizeInBits, Align,
1894                                    offsetInBits, flags, debugType, Annotations);
1895 }
1896 
1897 llvm::DISubprogram *
createInlinedSubprogram(StringRef FuncName,llvm::DIFile * FileScope)1898 CGDebugInfo::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(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 
CollectRecordLambdaFields(const CXXRecordDecl * CXXDecl,SmallVectorImpl<llvm::Metadata * > & elements,llvm::DIType * RecordTy)1921 void 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(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(V, CGM.getContext());
1941       llvm::DIType *FieldType = createFieldType(
1942           VName, Field->getType(), Loc, Field->getAccess(),
1943           layout.getFieldOffset(fieldno), Align, VUnit, RecordTy, CXXDecl);
1944       elements.push_back(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(f->getLocation());
1952       QualType type = f->getType();
1953       StringRef ThisName =
1954           CGM.getCodeGenOpts().EmitCodeView ? "__this" : "this";
1955       llvm::DIType *fieldType = createFieldType(
1956           ThisName, type, f->getLocation(), f->getAccess(),
1957           layout.getFieldOffset(fieldno), VUnit, RecordTy, CXXDecl);
1958 
1959       elements.push_back(fieldType);
1960     }
1961   }
1962 }
1963 
1964 llvm::DIDerivedType *
CreateRecordStaticField(const VarDecl * Var,llvm::DIType * RecordTy,const RecordDecl * RD)1965 CGDebugInfo::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(Var->getLocation());
1971   llvm::DIType *VTy = getOrCreateType(Var->getType(), VUnit);
1972 
1973   unsigned LineNumber = getLineNumber(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(CGM.getLLVMContext(), Value->getInt());
1984       if (Value->isFloat())
1985         C = llvm::ConstantFP::get(CGM.getLLVMContext(), Value->getFloat());
1986     }
1987   }
1988 
1989   llvm::DINode::DIFlags Flags = getAccessFlag(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(Var, CGM.getContext());
1994   llvm::DIDerivedType *GV = DBuilder.createStaticMemberType(
1995       RecordTy, VName, VUnit, LineNumber, VTy, Flags, C, Tag, Align);
1996   StaticDataMemberCache[Var->getCanonicalDecl()].reset(GV);
1997   return GV;
1998 }
1999 
CollectRecordNormalField(const FieldDecl * field,uint64_t OffsetInBits,llvm::DIFile * tunit,SmallVectorImpl<llvm::Metadata * > & elements,llvm::DIType * RecordTy,const RecordDecl * RD)2000 void 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(field, RecordTy, RD);
2015     if (llvm::DIType *Separator =
2016             createBitFieldSeparatorIfNeeded(field, BitFieldType, elements, RD))
2017       elements.push_back(Separator);
2018   } else {
2019     auto Align = getDeclAlignIfRequired(field, CGM.getContext());
2020     llvm::DINodeArray Annotations = CollectBTFDeclTagAnnotations(field);
2021     FieldType =
2022         createFieldType(name, type, field->getLocation(), field->getAccess(),
2023                         OffsetInBits, Align, tunit, RecordTy, RD, Annotations);
2024   }
2025 
2026   elements.push_back(FieldType);
2027 }
2028 
CollectRecordNestedType(const TypeDecl * TD,SmallVectorImpl<llvm::Metadata * > & elements)2029 void CGDebugInfo::CollectRecordNestedType(
2030     const TypeDecl *TD, SmallVectorImpl<llvm::Metadata *> &elements) {
2031   QualType Ty = CGM.getContext().getTypeDeclType(TD);
2032   // Injected class names are not considered nested records.
2033   if (isa<InjectedClassNameType>(Ty))
2034     return;
2035   SourceLocation Loc = TD->getLocation();
2036   if (llvm::DIType *nestedType = getOrCreateType(Ty, getOrCreateFile(Loc)))
2037     elements.push_back(nestedType);
2038 }
2039 
CollectRecordFields(const RecordDecl * record,llvm::DIFile * tunit,SmallVectorImpl<llvm::Metadata * > & elements,llvm::DICompositeType * RecordTy)2040 void 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>(record);
2045 
2046   if (CXXDecl && CXXDecl->isLambda())
2047     CollectRecordLambdaFields(CXXDecl, elements, RecordTy);
2048   else {
2049     const ASTRecordLayout &layout = CGM.getContext().getASTRecordLayout(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>(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>(V))
2065           continue;
2066 
2067         if (isa<VarTemplatePartialSpecializationDecl>(V))
2068           continue;
2069 
2070         // Reuse the existing static member declaration if one exists
2071         auto MI = StaticDataMemberCache.find(V->getCanonicalDecl());
2072         if (MI != StaticDataMemberCache.end()) {
2073           assert(MI->second &&
2074                  "Static data member declaration should still exist");
2075           elements.push_back(MI->second);
2076         } else {
2077           auto Field = CreateRecordStaticField(V, RecordTy, record);
2078           elements.push_back(Field);
2079         }
2080       } else if (const auto *field = dyn_cast<FieldDecl>(I)) {
2081         CollectRecordNormalField(field, layout.getFieldOffset(fieldNo), tunit,
2082                                  elements, RecordTy, 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>(I)) {
2090           // MSVC doesn't generate nested type for anonymous struct/union.
2091           if (isa<RecordDecl>(I) &&
2092               cast<RecordDecl>(I)->isAnonymousStructOrUnion())
2093             continue;
2094           if (!nestedType->isImplicit() &&
2095               nestedType->getDeclContext() == record)
2096             CollectRecordNestedType(nestedType, elements);
2097         }
2098       }
2099   }
2100 }
2101 
2102 llvm::DISubroutineType *
getOrCreateMethodType(const CXXMethodDecl * Method,llvm::DIFile * Unit)2103 CGDebugInfo::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         getOrCreateType(QualType(Func, 0), Unit));
2109 
2110   QualType ThisType;
2111   if (!Method->hasCXXExplicitFunctionObjectParameter())
2112     ThisType = Method->getThisType();
2113 
2114   return getOrCreateInstanceMethodType(ThisType, Func, Unit);
2115 }
2116 
getOrCreateMethodTypeForDestructor(const CXXMethodDecl * Method,llvm::DIFile * Unit,QualType FNType)2117 llvm::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(Method->getThisType(), Func, Unit, true);
2122 }
2123 
2124 llvm::DISubroutineType *
getOrCreateInstanceMethodType(QualType ThisPtr,const FunctionProtoType * Func,llvm::DIFile * Unit,bool SkipFirst)2125 CGDebugInfo::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       getOrCreateType(CGM.getContext().getFunctionType(
2144                           Func->getReturnType(), Func->getParamTypes(), EPI),
2145                       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(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(ThisPtr, Unit);
2160     TypeCache[ThisPtr.getAsOpaquePtr()].reset(ThisPtrType);
2161     ThisPtrType =
2162         DBuilder.createObjectPointerType(ThisPtrType, /*Implicit=*/true);
2163     Elts.push_back(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(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(Args[1], /*Implicit=*/false);
2175   }
2176 
2177   llvm::DITypeRefArray EltTypeArray = DBuilder.getOrCreateTypeArray(Elts);
2178 
2179   return DBuilder.createSubroutineType(EltTypeArray, OriginalFunc->getFlags(),
2180                                        getDwarfCC(Func->getCallConv()));
2181 }
2182 
2183 /// isFunctionLocalClass - Return true if CXXRecordDecl is defined
2184 /// inside a function.
isFunctionLocalClass(const CXXRecordDecl * RD)2185 static bool isFunctionLocalClass(const CXXRecordDecl *RD) {
2186   if (const auto *NRD = dyn_cast<CXXRecordDecl>(RD->getDeclContext()))
2187     return isFunctionLocalClass(NRD);
2188   if (isa<FunctionDecl>(RD->getDeclContext()))
2189     return true;
2190   return false;
2191 }
2192 
CreateCXXMemberFunction(const CXXMethodDecl * Method,llvm::DIFile * Unit,llvm::DIType * RecordTy)2193 llvm::DISubprogram *CGDebugInfo::CreateCXXMemberFunction(
2194     const CXXMethodDecl *Method, llvm::DIFile *Unit, llvm::DIType *RecordTy) {
2195   bool IsCtorOrDtor =
2196       isa<CXXConstructorDecl>(Method) || isa<CXXDestructorDecl>(Method);
2197 
2198   StringRef MethodName = getFunctionName(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(Method->getParent()))
2210     MethodLinkageName = CGM.getMangledName(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(Method->getLocation());
2217     MethodLine = getLineNumber(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(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>(Method))
2237         VIndex = CGM.getItaniumVTableContext().getMethodVTableIndex(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>(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(Method->getAccess(), Method->getParent());
2276   if (const auto *CXXC = dyn_cast<CXXConstructorDecl>(Method)) {
2277     if (CXXC->isExplicit())
2278       Flags |= llvm::DINode::FlagExplicit;
2279   } else if (const auto *CXXC = dyn_cast<CXXConversionDecl>(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>(Method))
2298       completeUnusedClass(*CD->getParent());
2299 
2300   llvm::DINodeArray TParamsArray = CollectFunctionTemplateParams(Method, Unit);
2301   llvm::DISubprogram *SP = DBuilder.createMethod(
2302       RecordTy, MethodName, MethodLinkageName, MethodDefUnit, MethodLine,
2303       MethodTy, VIndex, ThisAdjustment, ContainingType, Flags, SPFlags,
2304       TParamsArray.get(), /*ThrownTypes*/ nullptr,
2305       CGM.getCodeGenOpts().DebugKeyInstructions);
2306 
2307   SPCache[Method->getCanonicalDecl()].reset(SP);
2308 
2309   return SP;
2310 }
2311 
CollectCXXMemberFunctions(const CXXRecordDecl * RD,llvm::DIFile * Unit,SmallVectorImpl<llvm::Metadata * > & EltTys,llvm::DIType * RecordTy)2312 void 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>(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(Method->getCanonicalDecl());
2344     EltTys.push_back(MI == SPCache.end()
2345                          ? CreateCXXMemberFunction(Method, Unit, RecordTy)
2346                          : static_cast<llvm::Metadata *>(MI->second));
2347   }
2348 }
2349 
CollectCXXBases(const CXXRecordDecl * RD,llvm::DIFile * Unit,SmallVectorImpl<llvm::Metadata * > & EltTys,llvm::DIType * RecordTy)2350 void 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, RD->bases(), SeenTypes,
2355                      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, RD->vbases(), SeenTypes,
2361                        llvm::DINode::FlagIndirectVirtualBase);
2362   }
2363 }
2364 
CollectCXXBasesAux(const CXXRecordDecl * RD,llvm::DIFile * Unit,SmallVectorImpl<llvm::Metadata * > & EltTys,llvm::DIType * RecordTy,const CXXRecordDecl::base_class_const_range & Bases,llvm::DenseSet<CanonicalDeclPtr<const CXXRecordDecl>> & SeenTypes,llvm::DINode::DIFlags StartingFlags)2365 void 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(RD);
2372   for (const auto &BI : Bases) {
2373     const auto *Base =
2374         cast<CXXRecordDecl>(BI.getType()->castAs<RecordType>()->getDecl());
2375     if (!SeenTypes.insert(Base).second)
2376       continue;
2377     auto *BaseTy = getOrCreateType(BI.getType(), 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, 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(RD, Base);
2394         VBPtrOffset = CGM.getContext()
2395                           .getASTRecordLayout(RD)
2396                           .getVBPtrOffset()
2397                           .getQuantity();
2398       }
2399       BFlags |= llvm::DINode::FlagVirtual;
2400     } else
2401       BaseOffset = CGM.getContext().toBits(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(BI.getAccessSpecifier(), RD);
2406     llvm::DIType *DTy = DBuilder.createInheritance(RecordTy, BaseTy, BaseOffset,
2407                                                    VBPtrOffset, BFlags);
2408     EltTys.push_back(DTy);
2409   }
2410 }
2411 
2412 llvm::DINodeArray
CollectTemplateParams(std::optional<TemplateArgs> OArgs,llvm::DIFile * Unit)2413 CGDebugInfo::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(i)->getName();
2425 
2426     switch (TA.getKind()) {
2427     case TemplateArgument::Type: {
2428       llvm::DIType *TTy = getOrCreateType(TA.getAsType(), Unit);
2429       TemplateParams.push_back(DBuilder.createTemplateTypeParameter(
2430           TheCU, Name, TTy, defaultParameter));
2431 
2432     } break;
2433     case TemplateArgument::Integral: {
2434       llvm::DIType *TTy = getOrCreateType(TA.getIntegralType(), Unit);
2435       TemplateParams.push_back(DBuilder.createTemplateValueParameter(
2436           TheCU, Name, TTy, defaultParameter,
2437           llvm::ConstantInt::get(CGM.getLLVMContext(), TA.getAsIntegral())));
2438     } break;
2439     case TemplateArgument::Declaration: {
2440       const ValueDecl *D = TA.getAsDecl();
2441       QualType T = TA.getParamTypeForDecl().getDesugaredType(CGM.getContext());
2442       llvm::DIType *TTy = getOrCreateType(T, 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>(D))
2451           V = CGM.GetAddrOfGlobalVar(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>(D);
2455                  MD && MD->isImplicitObjectMemberFunction())
2456           V = CGM.getCXXABI().EmitMemberFunctionPointer(MD);
2457         else if (const auto *FD = dyn_cast<FunctionDecl>(D))
2458           V = CGM.GetAddrOfFunction(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>(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(D);
2467           CharUnits chars =
2468               CGM.getContext().toCharUnitsFromBits((int64_t)fieldOffset);
2469           V = CGM.getCXXABI().EmitMemberDataPointer(MPT, chars);
2470         } else if (const auto *GD = dyn_cast<MSGuidDecl>(D)) {
2471           V = CGM.GetAddrOfMSGuidDecl(GD).getPointer();
2472         } else if (const auto *TPO = dyn_cast<TemplateParamObjectDecl>(D)) {
2473           if (T->isRecordType())
2474             V = ConstantEmitter(CGM).emitAbstract(
2475                 SourceLocation(), TPO->getValue(), 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(DBuilder.createTemplateValueParameter(
2483           TheCU, Name, TTy, defaultParameter, cast_or_null<llvm::Constant>(V)));
2484     } break;
2485     case TemplateArgument::NullPtr: {
2486       QualType T = TA.getNullPtrType();
2487       llvm::DIType *TTy = getOrCreateType(T, 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>(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(CGM.Int8Ty, 0);
2501       TemplateParams.push_back(DBuilder.createTemplateValueParameter(
2502           TheCU, Name, TTy, defaultParameter, V));
2503     } break;
2504     case TemplateArgument::StructuralValue: {
2505       QualType T = TA.getStructuralValueType();
2506       llvm::DIType *TTy = getOrCreateType(T, Unit);
2507       llvm::Constant *V = ConstantEmitter(CGM).emitAbstract(
2508           SourceLocation(), TA.getAsStructuralValue(), T);
2509       TemplateParams.push_back(DBuilder.createTemplateValueParameter(
2510           TheCU, Name, TTy, defaultParameter, 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, getPrintingPolicy());
2517       TemplateParams.push_back(DBuilder.createTemplateTemplateParameter(
2518           TheCU, Name, nullptr, QualName, defaultParameter));
2519       break;
2520     }
2521     case TemplateArgument::Pack:
2522       TemplateParams.push_back(DBuilder.createTemplateParameterPack(
2523           TheCU, Name, nullptr,
2524           CollectTemplateParams({{nullptr, 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(T, Unit);
2534       TemplateParams.push_back(DBuilder.createTemplateValueParameter(
2535           TheCU, Name, TTy, defaultParameter, 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(TemplateParams);
2545 }
2546 
2547 std::optional<CGDebugInfo::TemplateArgs>
GetTemplateArgs(const FunctionDecl * FD) const2548 CGDebugInfo::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, FD->getTemplateSpecializationArgs()->asArray()}};
2555   }
2556   return std::nullopt;
2557 }
2558 std::optional<CGDebugInfo::TemplateArgs>
GetTemplateArgs(const VarDecl * VD) const2559 CGDebugInfo::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>(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, TA}};
2570 }
2571 std::optional<CGDebugInfo::TemplateArgs>
GetTemplateArgs(const RecordDecl * RD) const2572 CGDebugInfo::GetTemplateArgs(const RecordDecl *RD) const {
2573   if (auto *TSpecial = dyn_cast<ClassTemplateSpecializationDecl>(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 {{TPList, TAList.asArray()}};
2581   }
2582   return std::nullopt;
2583 }
2584 
2585 llvm::DINodeArray
CollectFunctionTemplateParams(const FunctionDecl * FD,llvm::DIFile * Unit)2586 CGDebugInfo::CollectFunctionTemplateParams(const FunctionDecl *FD,
2587                                            llvm::DIFile *Unit) {
2588   return CollectTemplateParams(GetTemplateArgs(FD), Unit);
2589 }
2590 
CollectVarTemplateParams(const VarDecl * VL,llvm::DIFile * Unit)2591 llvm::DINodeArray CGDebugInfo::CollectVarTemplateParams(const VarDecl *VL,
2592                                                         llvm::DIFile *Unit) {
2593   return CollectTemplateParams(GetTemplateArgs(VL), Unit);
2594 }
2595 
CollectCXXTemplateParams(const RecordDecl * RD,llvm::DIFile * Unit)2596 llvm::DINodeArray CGDebugInfo::CollectCXXTemplateParams(const RecordDecl *RD,
2597                                                         llvm::DIFile *Unit) {
2598   return CollectTemplateParams(GetTemplateArgs(RD), Unit);
2599 }
2600 
CollectBTFDeclTagAnnotations(const Decl * D)2601 llvm::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(CGM.getLLVMContext(), StringRef("btf_decl_tag")),
2609         llvm::MDString::get(CGM.getLLVMContext(), I->getBTFDeclTag())};
2610     Annotations.push_back(llvm::MDNode::get(CGM.getLLVMContext(), Ops));
2611   }
2612   return DBuilder.getOrCreateArray(Annotations);
2613 }
2614 
getOrCreateVTablePtrType(llvm::DIFile * Unit)2615 llvm::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(Context.IntTy, Unit);
2623   llvm::DITypeRefArray SElements = DBuilder.getOrCreateTypeArray(STy);
2624   llvm::DIType *SubTy = DBuilder.createSubroutineType(SElements);
2625   unsigned Size = Context.getTypeSize(Context.VoidPtrTy);
2626   unsigned VtblPtrAddressSpace = CGM.getTarget().getVtblPtrAddressSpace();
2627   std::optional<unsigned> DWARFAddressSpace =
2628       CGM.getTarget().getDWARFAddressSpace(VtblPtrAddressSpace);
2629 
2630   llvm::DIType *vtbl_ptr_type = DBuilder.createPointerType(
2631       SubTy, Size, 0, DWARFAddressSpace, "__vtbl_ptr_type");
2632   VTablePtrType = DBuilder.createPointerType(vtbl_ptr_type, Size);
2633   return VTablePtrType;
2634 }
2635 
getVTableName(const CXXRecordDecl * RD)2636 StringRef CGDebugInfo::getVTableName(const CXXRecordDecl *RD) {
2637   // Copy the gdb compatible name on the side and use its reference.
2638   return internString("_vptr$", 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 *'.
emitVTableSymbol(llvm::GlobalVariable * VTable,const CXXRecordDecl * RD)2646 void 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(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(RD, TheCU);
2665   auto *Ctxt = cast<llvm::DICompositeType>(DContext);
2666   llvm::DIFile *Unit = getOrCreateFile(Loc);
2667   llvm::DIType *VTy = getOrCreateType(VoidPtr, Unit);
2668   llvm::DINode::DIFlags Flags = getAccessFlag(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       Ctxt, SymbolName, Unit, /*LineNumber=*/0, 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           TheCU, SymbolName, VTable->getName(), Unit, /*LineNo=*/0,
2685           getOrCreateType(VoidPtr, Unit), VTable->hasLocalLinkage(),
2686           /*isDefined=*/true, nullptr, DT, /*TemplateParameters=*/nullptr,
2687           PAlign);
2688   VTable->addDebugInfo(GVE);
2689 }
2690 
getDynamicInitializerName(const VarDecl * VD,DynamicInitKind StubKind,llvm::Function * InitFn)2691 StringRef 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, getPrintingPolicy());
2710     std::tie(Quals, GVName) = OS.str().rsplit("::");
2711     if (GVName.empty())
2712       std::swap(Quals, 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>(VD)) {
2736     printTemplateArgumentList(OS, VTpl->getTemplateArgs().asArray(),
2737                               getPrintingPolicy());
2738   }
2739 
2740   OS << '\'';
2741 
2742   return internString(OS.str());
2743 }
2744 
CollectVTableInfo(const CXXRecordDecl * RD,llvm::DIFile * Unit,SmallVectorImpl<llvm::Metadata * > & EltTys)2745 void 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(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(CGM.getContext().VoidPtrTy);
2768     const VTableLayout &VFTLayout =
2769         CGM.getMicrosoftVTableContext().getVFTableLayout(RD, 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(VtblPtrAddressSpace);
2776 
2777     // Create a very wide void* type and insert it directly in the element list.
2778     llvm::DIType *VTableType = DBuilder.createPointerType(
2779         nullptr, VTableWidth, 0, DWARFAddressSpace, "__vtbl_ptr_type");
2780     EltTys.push_back(VTableType);
2781 
2782     // The vptr is a pointer to this special vtable type.
2783     VPtrTy = DBuilder.createPointerType(VTableType, 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(CGM.getContext().VoidPtrTy);
2794   llvm::DIType *VPtrMember =
2795       DBuilder.createMemberType(Unit, getVTableName(RD), Unit, 0, Size, 0, 0,
2796                                 llvm::DINode::FlagArtificial, VPtrTy);
2797   EltTys.push_back(VPtrMember);
2798 }
2799 
getOrCreateRecordType(QualType RTy,SourceLocation Loc)2800 llvm::DIType *CGDebugInfo::getOrCreateRecordType(QualType RTy,
2801                                                  SourceLocation Loc) {
2802   assert(CGM.getCodeGenOpts().hasReducedDebugInfo());
2803   llvm::DIType *T = getOrCreateType(RTy, getOrCreateFile(Loc));
2804   return T;
2805 }
2806 
getOrCreateInterfaceType(QualType D,SourceLocation Loc)2807 llvm::DIType *CGDebugInfo::getOrCreateInterfaceType(QualType D,
2808                                                     SourceLocation Loc) {
2809   return getOrCreateStandaloneType(D, Loc);
2810 }
2811 
getOrCreateStandaloneType(QualType D,SourceLocation Loc)2812 llvm::DIType *CGDebugInfo::getOrCreateStandaloneType(QualType D,
2813                                                      SourceLocation Loc) {
2814   assert(CGM.getCodeGenOpts().hasReducedDebugInfo());
2815   assert(!D.isNull() && "null type");
2816   llvm::DIType *T = getOrCreateType(D, getOrCreateFile(Loc));
2817   assert(T && "could not create debug info for type");
2818 
2819   RetainedTypes.push_back(D.getAsOpaquePtr());
2820   return T;
2821 }
2822 
addHeapAllocSiteMetadata(llvm::CallBase * CI,QualType AllocatedTy,SourceLocation Loc)2823 void 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(CGM.getLLVMContext(), {});
2832   else
2833     node = getOrCreateType(AllocatedTy, getOrCreateFile(Loc));
2834 
2835   CI->setMetadata("heapallocsite", node);
2836 }
2837 
completeType(const EnumDecl * ED)2838 void CGDebugInfo::completeType(const EnumDecl *ED) {
2839   if (DebugKind <= llvm::codegenoptions::DebugLineTablesOnly)
2840     return;
2841   QualType Ty = CGM.getContext().getEnumType(ED);
2842   void *TyPtr = Ty.getAsOpaquePtr();
2843   auto I = TypeCache.find(TyPtr);
2844   if (I == TypeCache.end() || !cast<llvm::DIType>(I->second)->isForwardDecl())
2845     return;
2846   llvm::DIType *Res = CreateTypeDefinition(Ty->castAs<EnumType>());
2847   assert(!Res->isForwardDecl());
2848   TypeCache[TyPtr].reset(Res);
2849 }
2850 
completeType(const RecordDecl * RD)2851 void 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.
isClassOrMethodDLLImport(const CXXRecordDecl * RD)2858 static 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?
isDefinedInClangModule(const RecordDecl * RD)2868 static 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>(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>(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 
completeClassData(const RecordDecl * RD)2899 void CGDebugInfo::completeClassData(const RecordDecl *RD) {
2900   if (auto *CXXRD = dyn_cast<CXXRecordDecl>(RD))
2901     if (CXXRD->isDynamicClass() &&
2902         CGM.getVTableLinkage(CXXRD) ==
2903             llvm::GlobalValue::AvailableExternallyLinkage &&
2904         !isClassOrMethodDLLImport(CXXRD))
2905       return;
2906 
2907   if (DebugTypeExtRefs && isDefinedInClangModule(RD->getDefinition()))
2908     return;
2909 
2910   completeClass(RD);
2911 }
2912 
completeClass(const RecordDecl * RD)2913 void CGDebugInfo::completeClass(const RecordDecl *RD) {
2914   if (DebugKind <= llvm::codegenoptions::DebugLineTablesOnly)
2915     return;
2916   QualType Ty = CGM.getContext().getRecordType(RD);
2917   void *TyPtr = Ty.getAsOpaquePtr();
2918   auto I = TypeCache.find(TyPtr);
2919   if (I != TypeCache.end() && !cast<llvm::DIType>(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->castAs<RecordType>());
2926   assert(!Res->isForwardDecl());
2927   TypeCache[TyPtr].reset(Res);
2928 }
2929 
hasExplicitMemberDefinition(CXXRecordDecl::method_iterator I,CXXRecordDecl::method_iterator End)2930 static bool hasExplicitMemberDefinition(CXXRecordDecl::method_iterator I,
2931                                         CXXRecordDecl::method_iterator End) {
2932   for (CXXMethodDecl *MD : llvm::make_range(I, 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 
canUseCtorHoming(const CXXRecordDecl * RD)2940 static 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 
shouldOmitDefinition(llvm::codegenoptions::DebugInfoKind DebugKind,bool DebugTypeExtRefs,const RecordDecl * RD,const LangOptions & LangOpts)2966 static bool shouldOmitDefinition(llvm::codegenoptions::DebugInfoKind DebugKind,
2967                                  bool DebugTypeExtRefs, const RecordDecl *RD,
2968                                  const LangOptions &LangOpts) {
2969   if (DebugTypeExtRefs && isDefinedInClangModule(RD->getDefinition()))
2970     return true;
2971 
2972   if (auto *ES = RD->getASTContext().getExternalSource())
2973     if (ES->hasExternalDefinitions(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>(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(CXXDecl) && !CXXDecl->hasAttr<MSNoVTableAttr>())
3005     return true;
3006 
3007   TemplateSpecializationKind Spec = TSK_Undeclared;
3008   if (const auto *SD = dyn_cast<ClassTemplateSpecializationDecl>(RD))
3009     Spec = SD->getSpecializationKind();
3010 
3011   if (Spec == TSK_ExplicitInstantiationDeclaration &&
3012       hasExplicitMemberDefinition(CXXDecl->method_begin(),
3013                                   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(CXXDecl))
3020     return true;
3021 
3022   return false;
3023 }
3024 
completeRequiredType(const RecordDecl * RD)3025 void CGDebugInfo::completeRequiredType(const RecordDecl *RD) {
3026   if (shouldOmitDefinition(DebugKind, DebugTypeExtRefs, RD, CGM.getLangOpts()))
3027     return;
3028 
3029   QualType Ty = CGM.getContext().getRecordType(RD);
3030   llvm::DIType *T = getTypeOrNull(Ty);
3031   if (T && T->isForwardDecl())
3032     completeClassData(RD);
3033 }
3034 
CreateType(const RecordType * Ty)3035 llvm::DIType *CGDebugInfo::CreateType(const RecordType *Ty) {
3036   RecordDecl *RD = Ty->getDecl();
3037   llvm::DIType *T = cast_or_null<llvm::DIType>(getTypeOrNull(QualType(Ty, 0)));
3038   if (T || shouldOmitDefinition(DebugKind, DebugTypeExtRefs, RD,
3039                                 CGM.getLangOpts())) {
3040     if (!T)
3041       T = getOrCreateRecordFwdDecl(Ty, getDeclContextDescriptor(RD));
3042     return T;
3043   }
3044 
3045   auto [Def, Pref] = CreateTypeDefinition(Ty);
3046 
3047   return Pref ? Pref : Def;
3048 }
3049 
GetPreferredNameType(const CXXRecordDecl * RD,llvm::DIFile * Unit)3050 llvm::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(PNA->getTypedefType(), Unit);
3060 }
3061 
3062 std::pair<llvm::DIType *, llvm::DIType *>
CreateTypeDefinition(const RecordType * Ty)3063 CGDebugInfo::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(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>(RD))
3082     CollectContainingType(CXXDecl, FwdDecl);
3083 
3084   // Push the struct on region stack.
3085   LexicalBlockStack.emplace_back(&*FwdDecl);
3086   RegionMap[Ty->getDecl()].reset(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>(RD);
3097   if (CXXDecl) {
3098     CollectCXXBases(CXXDecl, DefUnit, EltTys, FwdDecl);
3099     CollectVTableInfo(CXXDecl, DefUnit, EltTys);
3100   }
3101 
3102   // Collect data fields (including static variables and any initializers).
3103   CollectRecordFields(RD, DefUnit, EltTys, FwdDecl);
3104   if (CXXDecl && !CGM.getCodeGenOpts().DebugOmitUnreferencedMethods)
3105     CollectCXXMemberFunctions(CXXDecl, DefUnit, EltTys, FwdDecl);
3106 
3107   LexicalBlockStack.pop_back();
3108   RegionMap.erase(Ty->getDecl());
3109 
3110   llvm::DINodeArray Elements = DBuilder.getOrCreateArray(EltTys);
3111   DBuilder.replaceArrays(FwdDecl, Elements);
3112 
3113   if (FwdDecl->isTemporary())
3114     FwdDecl =
3115         llvm::MDNode::replaceWithPermanent(llvm::TempDICompositeType(FwdDecl));
3116 
3117   RegionMap[Ty->getDecl()].reset(FwdDecl);
3118 
3119   if (CGM.getCodeGenOpts().getDebuggerTuning() == llvm::DebuggerKind::LLDB)
3120     if (auto *PrefDI = GetPreferredNameType(CXXDecl, DefUnit))
3121       return {FwdDecl, PrefDI};
3122 
3123   return {FwdDecl, nullptr};
3124 }
3125 
CreateType(const ObjCObjectType * Ty,llvm::DIFile * Unit)3126 llvm::DIType *CGDebugInfo::CreateType(const ObjCObjectType *Ty,
3127                                       llvm::DIFile *Unit) {
3128   // Ignore protocols.
3129   return getOrCreateType(Ty->getBaseType(), Unit);
3130 }
3131 
CreateType(const ObjCTypeParamType * Ty,llvm::DIFile * Unit)3132 llvm::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       getOrCreateType(Ty->getDecl()->getUnderlyingType(), Unit),
3140       Ty->getDecl()->getName(), getOrCreateFile(Loc), getLineNumber(Loc),
3141       getDeclContextDescriptor(Ty->getDecl()));
3142 }
3143 
3144 /// \return true if Getter has the default name for the property PD.
hasDefaultGetterName(const ObjCPropertyDecl * PD,const ObjCMethodDecl * Getter)3145 static 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(0);
3154 }
3155 
3156 /// \return true if Setter has the default name for the property PD.
hasDefaultSetterName(const ObjCPropertyDecl * PD,const ObjCMethodDecl * Setter)3157 static 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(PD->getName()) ==
3165          Setter->getDeclName().getObjCSelector().getNameForSlot(0);
3166 }
3167 
CreateType(const ObjCInterfaceType * Ty,llvm::DIFile * Unit)3168 llvm::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         llvm::dwarf::DW_TAG_structure_type, ID->getName(),
3184         getDeclContextDescriptor(ID), Unit, 0, RuntimeLang);
3185 
3186   // Get overall information about the record type for the debug info.
3187   llvm::DIFile *DefUnit = getOrCreateFile(ID->getLocation());
3188   unsigned Line = getLineNumber(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(ID);
3195     llvm::DIType *FwdDecl = DBuilder.createReplaceableCompositeType(
3196         llvm::dwarf::DW_TAG_structure_type, ID->getName(), Mod ? Mod : TheCU,
3197         DefUnit, Line, RuntimeLang);
3198     ObjCInterfaceCache.push_back(ObjCInterfaceCacheEntry(Ty, FwdDecl, Unit));
3199     return FwdDecl;
3200   }
3201 
3202   return CreateTypeDefinition(Ty, Unit);
3203 }
3204 
getOrCreateModuleRef(ASTSourceDescriptor Mod,bool CreateSkeletonCU)3205 llvm::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(M);
3212   if (ModRef != ModuleCache.end())
3213     return cast<llvm::DIModule>(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(CompDir))
3257       Relative.consume_front(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(Mod.getASTFile())) {
3276       if (CGM.getHeaderSearchOpts().ModuleFileHomeIsCwd)
3277         PCM = getCurrentDirname();
3278       else
3279         PCM = Mod.getPath();
3280     }
3281     llvm::sys::path::append(PCM, Mod.getASTFile());
3282     DIB.createCompileUnit(
3283         TheCU->getSourceLanguage(),
3284         // TODO: Support "Source" from external AST providers?
3285         DIB.createFile(Mod.getModuleName(), TheCU->getDirectory()),
3286         TheCU->getProducer(), false, StringRef(), 0, RemapPath(PCM),
3287         llvm::DICompileUnit::FullDebug, Signature);
3288     DIB.finalize();
3289   }
3290 
3291   llvm::DIModule *Parent =
3292       IsRootModule ? nullptr
3293                    : getOrCreateModuleRef(ASTSourceDescriptor(*M->Parent),
3294                                           CreateSkeletonCU);
3295   std::string IncludePath = Mod.getPath().str();
3296   llvm::DIModule *DIMod =
3297       DBuilder.createModule(Parent, Mod.getModuleName(), ConfigMacros,
3298                             RemapPath(IncludePath));
3299   ModuleCache[M].reset(DIMod);
3300   return DIMod;
3301 }
3302 
CreateTypeDefinition(const ObjCInterfaceType * Ty,llvm::DIFile * Unit)3303 llvm::DIType *CGDebugInfo::CreateTypeDefinition(const ObjCInterfaceType *Ty,
3304                                                 llvm::DIFile *Unit) {
3305   ObjCInterfaceDecl *ID = Ty->getDecl();
3306   llvm::DIFile *DefUnit = getOrCreateFile(ID->getLocation());
3307   unsigned Line = getLineNumber(ID->getLocation());
3308   unsigned RuntimeLang = TheCU->getSourceLanguage();
3309 
3310   // Bit size, align and offset of the type.
3311   uint64_t Size = CGM.getContext().getTypeSize(Ty);
3312   auto Align = getTypeAlignIfRequired(Ty, 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(ID);
3319   llvm::DICompositeType *RealDecl = DBuilder.createStructType(
3320       Mod ? Mod : Unit, ID->getName(), DefUnit, Line, Size, Align, Flags,
3321       nullptr, llvm::DINodeArray(), RuntimeLang);
3322 
3323   QualType QTy(Ty, 0);
3324   TypeCache[QTy.getAsOpaquePtr()].reset(RealDecl);
3325 
3326   // Push the struct on region stack.
3327   LexicalBlockStack.emplace_back(RealDecl);
3328   RegionMap[Ty->getDecl()].reset(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(CGM.getContext().getObjCInterfaceType(SClass), Unit);
3337     if (!SClassTy)
3338       return nullptr;
3339 
3340     llvm::DIType *InhTag = DBuilder.createInheritance(RealDecl, SClassTy, 0, 0,
3341                                                       llvm::DINode::FlagZero);
3342     EltTys.push_back(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         PD->getName(), PUnit, PLine,
3354         hasDefaultGetterName(PD, Getter) ? ""
3355                                          : getSelectorName(PD->getGetterName()),
3356         hasDefaultSetterName(PD, Setter) ? ""
3357                                          : getSelectorName(PD->getSetterName()),
3358         PD->getPropertyAttributes(), getOrCreateType(PD->getType(), PUnit));
3359     EltTys.push_back(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(PD->isClassProperty(), PD->getIdentifier());
3372     };
3373     for (const ObjCCategoryDecl *ClassExt : ID->known_extensions())
3374       for (auto *PD : ClassExt->properties()) {
3375         PropertySet.insert(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(GetIsClassAndIdent(PD)).second)
3382         continue;
3383       AddProperty(PD);
3384     }
3385   }
3386 
3387   const ASTRecordLayout &RL = CGM.getContext().getASTObjCInterfaceLayout(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(Field->getType(), 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(Field->getLocation());
3403     unsigned FieldLine = getLineNumber(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(FType);
3413       FieldAlign = getTypeAlignIfRequired(FType, 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, 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(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               PD->getName(), PUnit, PLine,
3455               hasDefaultGetterName(PD, Getter)
3456                   ? ""
3457                   : getSelectorName(PD->getGetterName()),
3458               hasDefaultSetterName(PD, Setter)
3459                   ? ""
3460                   : getSelectorName(PD->getSetterName()),
3461               PD->getPropertyAttributes(),
3462               getOrCreateType(PD->getType(), PUnit));
3463         }
3464       }
3465     }
3466     FieldTy = DBuilder.createObjCIVar(FieldName, FieldDefUnit, FieldLine,
3467                                       FieldSize, FieldAlign, FieldOffset, Flags,
3468                                       FieldTy, PropertyNode);
3469     EltTys.push_back(FieldTy);
3470   }
3471 
3472   llvm::DINodeArray Elements = DBuilder.getOrCreateArray(EltTys);
3473   DBuilder.replaceArrays(RealDecl, Elements);
3474 
3475   LexicalBlockStack.pop_back();
3476   return RealDecl;
3477 }
3478 
CreateType(const VectorType * Ty,llvm::DIFile * Unit)3479 llvm::DIType *CGDebugInfo::CreateType(const VectorType *Ty,
3480                                       llvm::DIFile *Unit) {
3481   if (Ty->isPackedVectorBoolType(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(Ty);
3490     uint64_t NumVectorBytes = Size / Ctx.getCharWidth();
3491 
3492     // Construct the vector of 'char' type.
3493     QualType CharVecTy =
3494         Ctx.getVectorType(Ctx.CharTy, NumVectorBytes, VectorKind::Generic);
3495     return CreateType(CharVecTy->getAs<VectorType>(), Unit);
3496   }
3497 
3498   llvm::DIType *ElementTy = getOrCreateType(Ty->getElementType(), Unit);
3499   int64_t Count = Ty->getNumElements();
3500 
3501   llvm::Metadata *Subscript;
3502   QualType QTy(Ty, 0);
3503   auto SizeExpr = SizeExprCache.find(QTy);
3504   if (SizeExpr != SizeExprCache.end())
3505     Subscript = DBuilder.getOrCreateSubrange(
3506         SizeExpr->getSecond() /*count*/, nullptr /*lowerBound*/,
3507         nullptr /*upperBound*/, nullptr /*stride*/);
3508   else {
3509     auto *CountNode =
3510         llvm::ConstantAsMetadata::get(llvm::ConstantInt::getSigned(
3511             llvm::Type::getInt64Ty(CGM.getLLVMContext()), Count ? Count : -1));
3512     Subscript = DBuilder.getOrCreateSubrange(
3513         CountNode /*count*/, nullptr /*lowerBound*/, nullptr /*upperBound*/,
3514         nullptr /*stride*/);
3515   }
3516   llvm::DINodeArray SubscriptArray = DBuilder.getOrCreateArray(Subscript);
3517 
3518   uint64_t Size = CGM.getContext().getTypeSize(Ty);
3519   auto Align = getTypeAlignIfRequired(Ty, CGM.getContext());
3520 
3521   return DBuilder.createVectorType(Size, Align, ElementTy, SubscriptArray);
3522 }
3523 
CreateType(const ConstantMatrixType * Ty,llvm::DIFile * Unit)3524 llvm::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->getElementType(), Unit);
3530   uint64_t Size = CGM.getContext().getTypeSize(Ty);
3531   uint32_t Align = getTypeAlignIfRequired(Ty, CGM.getContext());
3532 
3533   // Create ranges for both dimensions.
3534   llvm::SmallVector<llvm::Metadata *, 2> Subscripts;
3535   auto *ColumnCountNode =
3536       llvm::ConstantAsMetadata::get(llvm::ConstantInt::getSigned(
3537           llvm::Type::getInt64Ty(CGM.getLLVMContext()), Ty->getNumColumns()));
3538   auto *RowCountNode =
3539       llvm::ConstantAsMetadata::get(llvm::ConstantInt::getSigned(
3540           llvm::Type::getInt64Ty(CGM.getLLVMContext()), Ty->getNumRows()));
3541   Subscripts.push_back(DBuilder.getOrCreateSubrange(
3542       ColumnCountNode /*count*/, nullptr /*lowerBound*/, nullptr /*upperBound*/,
3543       nullptr /*stride*/));
3544   Subscripts.push_back(DBuilder.getOrCreateSubrange(
3545       RowCountNode /*count*/, nullptr /*lowerBound*/, nullptr /*upperBound*/,
3546       nullptr /*stride*/));
3547   llvm::DINodeArray SubscriptArray = DBuilder.getOrCreateArray(Subscripts);
3548   return DBuilder.createArrayType(Size, Align, ElementTy, SubscriptArray);
3549 }
3550 
CreateType(const ArrayType * Ty,llvm::DIFile * Unit)3551 llvm::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>(Ty)) {
3557     Size = 0;
3558     Align = getTypeAlignIfRequired(CGM.getContext().getBaseElementType(VAT),
3559                                    CGM.getContext());
3560   } else if (Ty->isIncompleteArrayType()) {
3561     Size = 0;
3562     if (Ty->getElementType()->isIncompleteType())
3563       Align = 0;
3564     else
3565       Align = getTypeAlignIfRequired(Ty->getElementType(), 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(Ty);
3572     Align = getTypeAlignIfRequired(Ty, 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>(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>(Ty))
3590       Count = CAT->getZExtSize();
3591     else if (const auto *VAT = dyn_cast<VariableArrayType>(Ty)) {
3592       if (Expr *Size = VAT->getSizeExpr()) {
3593         Expr::EvalResult Result;
3594         if (Size->EvaluateAsInt(Result, CGM.getContext()))
3595           Count = Result.Val.getInt().getExtValue();
3596       }
3597     }
3598 
3599     auto SizeNode = SizeExprCache.find(EltTy);
3600     if (SizeNode != SizeExprCache.end())
3601       Subscripts.push_back(DBuilder.getOrCreateSubrange(
3602           SizeNode->getSecond() /*count*/, nullptr /*lowerBound*/,
3603           nullptr /*upperBound*/, nullptr /*stride*/));
3604     else {
3605       auto *CountNode =
3606           llvm::ConstantAsMetadata::get(llvm::ConstantInt::getSigned(
3607               llvm::Type::getInt64Ty(CGM.getLLVMContext()), Count));
3608       Subscripts.push_back(DBuilder.getOrCreateSubrange(
3609           CountNode /*count*/, nullptr /*lowerBound*/, nullptr /*upperBound*/,
3610           nullptr /*stride*/));
3611     }
3612     EltTy = Ty->getElementType();
3613   }
3614 
3615   llvm::DINodeArray SubscriptArray = DBuilder.getOrCreateArray(Subscripts);
3616 
3617   return DBuilder.createArrayType(Size, Align, getOrCreateType(EltTy, Unit),
3618                                   SubscriptArray);
3619 }
3620 
CreateType(const LValueReferenceType * Ty,llvm::DIFile * Unit)3621 llvm::DIType *CGDebugInfo::CreateType(const LValueReferenceType *Ty,
3622                                       llvm::DIFile *Unit) {
3623   return CreatePointerLikeType(llvm::dwarf::DW_TAG_reference_type, Ty,
3624                                Ty->getPointeeType(), Unit);
3625 }
3626 
CreateType(const RValueReferenceType * Ty,llvm::DIFile * Unit)3627 llvm::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, Ty->getPointeeType(), Unit);
3636 }
3637 
CreateType(const MemberPointerType * Ty,llvm::DIFile * U)3638 llvm::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(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       QualType(Ty->getMostRecentCXXRecordDecl()->getTypeForDecl(), 0), U);
3666   if (Ty->isMemberDataPointerType())
3667     return DBuilder.createMemberPointerType(
3668         getOrCreateType(Ty->getPointeeType(), U), ClassType, Size, /*Align=*/0,
3669         Flags);
3670 
3671   const FunctionProtoType *FPT =
3672       Ty->getPointeeType()->castAs<FunctionProtoType>();
3673   return DBuilder.createMemberPointerType(
3674       getOrCreateInstanceMethodType(
3675           CXXMethodDecl::getThisType(FPT, Ty->getMostRecentCXXRecordDecl()),
3676           FPT, U),
3677       ClassType, Size, /*Align=*/0, Flags);
3678 }
3679 
CreateType(const AtomicType * Ty,llvm::DIFile * U)3680 llvm::DIType *CGDebugInfo::CreateType(const AtomicType *Ty, llvm::DIFile *U) {
3681   auto *FromTy = getOrCreateType(Ty->getValueType(), U);
3682   return DBuilder.createQualifiedType(llvm::dwarf::DW_TAG_atomic_type, FromTy);
3683 }
3684 
CreateType(const PipeType * Ty,llvm::DIFile * U)3685 llvm::DIType *CGDebugInfo::CreateType(const PipeType *Ty, llvm::DIFile *U) {
3686   return getOrCreateType(Ty->getElementType(), U);
3687 }
3688 
CreateType(const HLSLAttributedResourceType * Ty,llvm::DIFile * U)3689 llvm::DIType *CGDebugInfo::CreateType(const HLSLAttributedResourceType *Ty,
3690                                       llvm::DIFile *U) {
3691   return getOrCreateType(Ty->getWrappedType(), U);
3692 }
3693 
CreateType(const HLSLInlineSpirvType * Ty,llvm::DIFile * U)3694 llvm::DIType *CGDebugInfo::CreateType(const HLSLInlineSpirvType *Ty,
3695                                       llvm::DIFile *U) {
3696   // Debug information unneeded.
3697   return nullptr;
3698 }
3699 
CreateEnumType(const EnumType * Ty)3700 llvm::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(ED->getTypeForDecl());
3707     Align = getDeclAlignIfRequired(ED, 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(ED);
3725     llvm::DIFile *DefUnit = getOrCreateFile(ED->getLocation());
3726     llvm::TempDIScope TmpContext(DBuilder.createReplaceableCompositeType(
3727         llvm::dwarf::DW_TAG_enumeration_type, "", TheCU, DefUnit, 0));
3728 
3729     unsigned Line = getLineNumber(ED->getLocation());
3730     StringRef EDName = ED->getName();
3731     llvm::DIType *RetTy = DBuilder.createReplaceableCompositeType(
3732         llvm::dwarf::DW_TAG_enumeration_type, EDName, EDContext, DefUnit, Line,
3733         0, Size, Align, llvm::DINode::FlagFwdDecl, Identifier);
3734 
3735     ReplaceMap.emplace_back(
3736         std::piecewise_construct, std::make_tuple(Ty),
3737         std::make_tuple(static_cast<llvm::Metadata *>(RetTy)));
3738     return RetTy;
3739   }
3740 
3741   return CreateTypeDefinition(Ty);
3742 }
3743 
CreateTypeDefinition(const EnumType * Ty)3744 llvm::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(ED->getTypeForDecl());
3750     Align = getDeclAlignIfRequired(ED, 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         DBuilder.createEnumerator(Enum->getName(), 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(Enumerators);
3769 
3770   llvm::DIFile *DefUnit = getOrCreateFile(ED->getLocation());
3771   unsigned Line = getLineNumber(ED->getLocation());
3772   llvm::DIScope *EnumContext = getDeclContextDescriptor(ED);
3773   llvm::DIType *ClassTy = getOrCreateType(ED->getIntegerType(), DefUnit);
3774   return DBuilder.createEnumerationType(
3775       EnumContext, ED->getName(), DefUnit, Line, Size, Align, EltArray, ClassTy,
3776       /*RunTimeLang=*/0, Identifier, ED->isScoped(), EnumKind);
3777 }
3778 
CreateMacro(llvm::DIMacroFile * Parent,unsigned MType,SourceLocation LineLoc,StringRef Name,StringRef Value)3779 llvm::DIMacro *CGDebugInfo::CreateMacro(llvm::DIMacroFile *Parent,
3780                                         unsigned MType, SourceLocation LineLoc,
3781                                         StringRef Name, StringRef Value) {
3782   unsigned Line = LineLoc.isInvalid() ? 0 : getLineNumber(LineLoc);
3783   return DBuilder.createMacro(Parent, Line, MType, Name, Value);
3784 }
3785 
CreateTempMacroFile(llvm::DIMacroFile * Parent,SourceLocation LineLoc,SourceLocation FileLoc)3786 llvm::DIMacroFile *CGDebugInfo::CreateTempMacroFile(llvm::DIMacroFile *Parent,
3787                                                     SourceLocation LineLoc,
3788                                                     SourceLocation FileLoc) {
3789   llvm::DIFile *FName = getOrCreateFile(FileLoc);
3790   unsigned Line = LineLoc.isInvalid() ? 0 : getLineNumber(LineLoc);
3791   return DBuilder.createTempMacroFile(Parent, Line, FName);
3792 }
3793 
CreateSyntheticInlineAt(llvm::DebugLoc Location,StringRef FuncName)3794 llvm::DILocation *CGDebugInfo::CreateSyntheticInlineAt(llvm::DebugLoc Location,
3795                                                        StringRef FuncName) {
3796   llvm::DISubprogram *SP =
3797       createInlinedSubprogram(FuncName, Location->getFile());
3798   return llvm::DILocation::get(CGM.getLLVMContext(), /*Line=*/0, /*Column=*/0,
3799                                /*Scope=*/SP, /*InlinedAt=*/Location);
3800 }
3801 
CreateTrapFailureMessageFor(llvm::DebugLoc TrapLocation,StringRef Category,StringRef FailureMsg)3802 llvm::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(TrapLocation, FuncName);
3814 }
3815 
UnwrapTypeForDebugInfo(QualType T,const ASTContext & C)3816 static 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(Quals, InnerQuals);
3823     Quals += InnerQuals;
3824     QualType LastT = T;
3825     switch (T->getTypeClass()) {
3826     default:
3827       return C.getQualifiedType(T.getTypePtr(), Quals);
3828     case Type::TemplateSpecialization: {
3829       const auto *Spec = cast<TemplateSpecializationType>(T);
3830       if (Spec->isTypeAlias())
3831         return C.getQualifiedType(T.getTypePtr(), Quals);
3832       T = Spec->desugar();
3833       break;
3834     }
3835     case Type::TypeOfExpr:
3836       T = cast<TypeOfExprType>(T)->getUnderlyingExpr()->getType();
3837       break;
3838     case Type::TypeOf:
3839       T = cast<TypeOfType>(T)->getUnmodifiedType();
3840       break;
3841     case Type::Decltype:
3842       T = cast<DecltypeType>(T)->getUnderlyingType();
3843       break;
3844     case Type::UnaryTransform:
3845       T = cast<UnaryTransformType>(T)->getUnderlyingType();
3846       break;
3847     case Type::Attributed:
3848       T = cast<AttributedType>(T)->getEquivalentType();
3849       break;
3850     case Type::BTFTagAttributed:
3851       T = cast<BTFTagAttributedType>(T)->getWrappedType();
3852       break;
3853     case Type::CountAttributed:
3854       T = cast<CountAttributedType>(T)->desugar();
3855       break;
3856     case Type::Elaborated:
3857       T = cast<ElaboratedType>(T)->getNamedType();
3858       break;
3859     case Type::Using:
3860       T = cast<UsingType>(T)->getUnderlyingType();
3861       break;
3862     case Type::Paren:
3863       T = cast<ParenType>(T)->getInnerType();
3864       break;
3865     case Type::MacroQualified:
3866       T = cast<MacroQualifiedType>(T)->getUnderlyingType();
3867       break;
3868     case Type::SubstTemplateTypeParm:
3869       T = cast<SubstTemplateTypeParmType>(T)->getReplacementType();
3870       break;
3871     case Type::Auto:
3872     case Type::DeducedTemplateSpecialization: {
3873       QualType DT = cast<DeducedType>(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>(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>(T)->getAdjustedType();
3886       break;
3887     }
3888 
3889     assert(T != LastT && "Type unwrapping failed to unwrap!");
3890     (void)LastT;
3891   } while (true);
3892 }
3893 
getTypeOrNull(QualType Ty)3894 llvm::DIType *CGDebugInfo::getTypeOrNull(QualType Ty) {
3895   assert(Ty == UnwrapTypeForDebugInfo(Ty, CGM.getContext()));
3896   auto It = TypeCache.find(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>(V);
3901   }
3902 
3903   return nullptr;
3904 }
3905 
completeTemplateDefinition(const ClassTemplateSpecializationDecl & SD)3906 void CGDebugInfo::completeTemplateDefinition(
3907     const ClassTemplateSpecializationDecl &SD) {
3908   completeUnusedClass(SD);
3909 }
3910 
completeUnusedClass(const CXXRecordDecl & D)3911 void CGDebugInfo::completeUnusedClass(const CXXRecordDecl &D) {
3912   if (DebugKind <= llvm::codegenoptions::DebugLineTablesOnly ||
3913       D.isDynamicClass())
3914     return;
3915 
3916   completeClassData(&D);
3917   // In case this type has no member function definitions being emitted, ensure
3918   // it is retained
3919   RetainedTypes.push_back(CGM.getContext().getRecordType(&D).getAsOpaquePtr());
3920 }
3921 
getOrCreateType(QualType Ty,llvm::DIFile * Unit)3922 llvm::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, getPrintingPolicy());
3930     return Name;
3931   });
3932 
3933   // Unwrap the type as needed for debug information.
3934   Ty = UnwrapTypeForDebugInfo(Ty, CGM.getContext());
3935 
3936   if (auto *T = getTypeOrNull(Ty))
3937     return T;
3938 
3939   llvm::DIType *Res = CreateTypeNode(Ty, Unit);
3940   void *TyPtr = Ty.getAsOpaquePtr();
3941 
3942   // And update the type cache.
3943   TypeCache[TyPtr].reset(Res);
3944 
3945   return Res;
3946 }
3947 
getParentModuleOrNull(const Decl * D)3948 llvm::DIModule *CGDebugInfo::getParentModuleOrNull(const Decl *D) {
3949   // A forward declaration inside a module header does not belong to the module.
3950   if (isa<RecordDecl>(D) && !cast<RecordDecl>(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(Idx);
3957     if (Info)
3958       return getOrCreateModuleRef(*Info, /*SkeletonCU=*/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(Info, /*SkeletonCU=*/false);
3974     } else {
3975       // This the precompiled header being built.
3976       return getOrCreateModuleRef(PCHDescriptor, /*SkeletonCU=*/false);
3977     }
3978   }
3979 
3980   return nullptr;
3981 }
3982 
CreateTypeNode(QualType Ty,llvm::DIFile * Unit)3983 llvm::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(cast<VectorType>(Ty), Unit);
4000   case Type::ConstantMatrix:
4001     return CreateType(cast<ConstantMatrixType>(Ty), Unit);
4002   case Type::ObjCObjectPointer:
4003     return CreateType(cast<ObjCObjectPointerType>(Ty), Unit);
4004   case Type::ObjCObject:
4005     return CreateType(cast<ObjCObjectType>(Ty), Unit);
4006   case Type::ObjCTypeParam:
4007     return CreateType(cast<ObjCTypeParamType>(Ty), Unit);
4008   case Type::ObjCInterface:
4009     return CreateType(cast<ObjCInterfaceType>(Ty), Unit);
4010   case Type::Builtin:
4011     return CreateType(cast<BuiltinType>(Ty));
4012   case Type::Complex:
4013     return CreateType(cast<ComplexType>(Ty));
4014   case Type::Pointer:
4015     return CreateType(cast<PointerType>(Ty), Unit);
4016   case Type::BlockPointer:
4017     return CreateType(cast<BlockPointerType>(Ty), Unit);
4018   case Type::Typedef:
4019     return CreateType(cast<TypedefType>(Ty), Unit);
4020   case Type::Record:
4021     return CreateType(cast<RecordType>(Ty));
4022   case Type::Enum:
4023     return CreateEnumType(cast<EnumType>(Ty));
4024   case Type::FunctionProto:
4025   case Type::FunctionNoProto:
4026     return CreateType(cast<FunctionType>(Ty), Unit);
4027   case Type::ConstantArray:
4028   case Type::VariableArray:
4029   case Type::IncompleteArray:
4030   case Type::ArrayParameter:
4031     return CreateType(cast<ArrayType>(Ty), Unit);
4032 
4033   case Type::LValueReference:
4034     return CreateType(cast<LValueReferenceType>(Ty), Unit);
4035   case Type::RValueReference:
4036     return CreateType(cast<RValueReferenceType>(Ty), Unit);
4037 
4038   case Type::MemberPointer:
4039     return CreateType(cast<MemberPointerType>(Ty), Unit);
4040 
4041   case Type::Atomic:
4042     return CreateType(cast<AtomicType>(Ty), Unit);
4043 
4044   case Type::BitInt:
4045     return CreateType(cast<BitIntType>(Ty));
4046   case Type::Pipe:
4047     return CreateType(cast<PipeType>(Ty), Unit);
4048 
4049   case Type::TemplateSpecialization:
4050     return CreateType(cast<TemplateSpecializationType>(Ty), Unit);
4051   case Type::HLSLAttributedResource:
4052     return CreateType(cast<HLSLAttributedResourceType>(Ty), Unit);
4053   case Type::HLSLInlineSpirv:
4054     return CreateType(cast<HLSLInlineSpirvType>(Ty), 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 
4079 llvm::DICompositeType *
getOrCreateLimitedType(const RecordType * Ty)4080 CGDebugInfo::getOrCreateLimitedType(const RecordType *Ty) {
4081   QualType QTy(Ty, 0);
4082 
4083   auto *T = cast_or_null<llvm::DICompositeType>(getTypeOrNull(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(Res, T ? T->getElements() : llvm::DINodeArray());
4098 
4099   // And update the type cache.
4100   TypeCache[QTy.getAsOpaquePtr()].reset(Res);
4101   return Res;
4102 }
4103 
4104 // TODO: Currently used for context chains when limiting debug info.
CreateLimitedType(const RecordType * Ty)4105 llvm::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(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       getTypeOrNull(CGM.getContext().getRecordType(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, RDContext);
4132 
4133   uint64_t Size = CGM.getContext().getTypeSize(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, 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>(RD)) {
4146     if (CGM.getCXXABI().getRecordArgABI(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(CXXRD->getAccess(),
4160                            dyn_cast<CXXRecordDecl>(CXXRD->getDeclContext()));
4161   }
4162 
4163   llvm::DINodeArray Annotations = CollectBTFDeclTagAnnotations(D);
4164   llvm::DICompositeType *RealDecl = DBuilder.createReplaceableCompositeType(
4165       getTagForRecord(RD), RDName, RDContext, DefUnit, Line, 0, Size, Align,
4166       Flags, 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(llvm::TempDICompositeType(RealDecl));
4190     break;
4191   }
4192 
4193   RegionMap[Ty->getDecl()].reset(RealDecl);
4194   TypeCache[QualType(Ty, 0).getAsOpaquePtr()].reset(RealDecl);
4195 
4196   if (const auto *TSpecial = dyn_cast<ClassTemplateSpecializationDecl>(RD))
4197     DBuilder.replaceArrays(RealDecl, llvm::DINodeArray(),
4198                            CollectCXXTemplateParams(TSpecial, DefUnit));
4199   return RealDecl;
4200 }
4201 
CollectContainingType(const CXXRecordDecl * RD,llvm::DICompositeType * RealDecl)4202 void 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(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(PBase);
4211       const CXXRecordDecl *PBT = BRL.getPrimaryBase();
4212       if (PBT && !BRL.isPrimaryBaseVirtual())
4213         PBase = PBT;
4214       else
4215         break;
4216     }
4217     ContainingType = getOrCreateType(QualType(PBase->getTypeForDecl(), 0),
4218                                      getOrCreateFile(RD->getLocation()));
4219   } else if (RD->isDynamicClass())
4220     ContainingType = RealDecl;
4221 
4222   DBuilder.replaceVTableHolder(RealDecl, ContainingType);
4223 }
4224 
CreateMemberType(llvm::DIFile * Unit,QualType FType,StringRef Name,uint64_t * Offset)4225 llvm::DIType *CGDebugInfo::CreateMemberType(llvm::DIFile *Unit, QualType FType,
4226                                             StringRef Name, uint64_t *Offset) {
4227   llvm::DIType *FieldTy = CGDebugInfo::getOrCreateType(FType, Unit);
4228   uint64_t FieldSize = CGM.getContext().getTypeSize(FType);
4229   auto FieldAlign = getTypeAlignIfRequired(FType, CGM.getContext());
4230   llvm::DIType *Ty =
4231       DBuilder.createMemberType(Unit, Name, Unit, 0, FieldSize, FieldAlign,
4232                                 *Offset, llvm::DINode::FlagZero, FieldTy);
4233   *Offset += FieldSize;
4234   return Ty;
4235 }
4236 
collectFunctionDeclProps(GlobalDecl GD,llvm::DIFile * Unit,StringRef & Name,StringRef & LinkageName,llvm::DIScope * & FDContext,llvm::DINodeArray & TParamsArray,llvm::DINode::DIFlags & Flags)4237 void 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>(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>(FD->getDeclContext()))
4268       FDContext = getOrCreateNamespace(NSDecl);
4269     else if (const RecordDecl *RDecl =
4270                  dyn_cast_or_null<RecordDecl>(FD->getDeclContext())) {
4271       llvm::DIScope *Mod = getParentModuleOrNull(RDecl);
4272       FDContext = getContextDescriptor(RDecl, 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 
collectVarDeclProps(const VarDecl * VD,llvm::DIFile * & Unit,unsigned & LineNo,QualType & T,StringRef & Name,StringRef & LinkageName,llvm::MDTuple * & TemplateParameters,llvm::DIScope * & VDContext)4284 void 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(VD->getLocation());
4290   LineNo = getLineNumber(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(ET, ConstVal, nullptr,
4301                                               ArraySizeModifier::Normal, 0);
4302   }
4303 
4304   Name = VD->getName();
4305   if (VD->getDeclContext() && !isa<FunctionDecl>(VD->getDeclContext()) &&
4306       !isa<ObjCMethodDecl>(VD->getDeclContext()))
4307     LinkageName = CGM.getMangledName(VD);
4308   if (LinkageName == Name)
4309     LinkageName = StringRef();
4310 
4311   if (isa<VarTemplateSpecializationDecl>(VD)) {
4312     llvm::DINodeArray parameterNodes = CollectVarTemplateParams(VD, &*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(VD);
4336   VDContext = getContextDescriptor(cast<Decl>(DC), Mod ? Mod : TheCU);
4337 }
4338 
getFunctionFwdDeclOrStub(GlobalDecl GD,bool Stub)4339 llvm::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, DContext, TParamsArray,
4350                            Flags);
4351   auto *FD = cast<FunctionDecl>(GD.getDecl());
4352 
4353   // Build function type.
4354   SmallVector<QualType, 16> ArgTypes;
4355   for (const ParmVarDecl *Parm : FD->parameters())
4356     ArgTypes.push_back(Parm->getType());
4357 
4358   CallingConv CC = FD->getType()->castAs<FunctionType>()->getCallConv();
4359   QualType FnType = CGM.getContext().getFunctionType(
4360       FD->getReturnType(), ArgTypes, 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         DContext, Name, LinkageName, Unit, Line,
4371         getOrCreateFunctionType(GD.getDecl(), FnType, Unit), 0, Flags, SPFlags,
4372         TParamsArray.get(), getFunctionDeclaration(FD), /*ThrownTypes*/ nullptr,
4373         /*Annotations*/ nullptr, /*TargetFuncName*/ "",
4374         CGM.getCodeGenOpts().DebugKeyInstructions);
4375   }
4376 
4377   llvm::DISubprogram *SP = DBuilder.createTempFunctionFwdDecl(
4378       DContext, Name, LinkageName, Unit, Line,
4379       getOrCreateFunctionType(GD.getDecl(), FnType, Unit), 0, Flags, SPFlags,
4380       TParamsArray.get(), getFunctionDeclaration(FD));
4381   const FunctionDecl *CanonDecl = FD->getCanonicalDecl();
4382   FwdDeclReplaceMap.emplace_back(std::piecewise_construct,
4383                                  std::make_tuple(CanonDecl),
4384                                  std::make_tuple(SP));
4385   return SP;
4386 }
4387 
getFunctionForwardDeclaration(GlobalDecl GD)4388 llvm::DISubprogram *CGDebugInfo::getFunctionForwardDeclaration(GlobalDecl GD) {
4389   return getFunctionFwdDeclOrStub(GD, /* Stub = */ false);
4390 }
4391 
getFunctionStub(GlobalDecl GD)4392 llvm::DISubprogram *CGDebugInfo::getFunctionStub(GlobalDecl GD) {
4393   return getFunctionFwdDeclOrStub(GD, /* Stub = */ true);
4394 }
4395 
4396 llvm::DIGlobalVariable *
getGlobalVariableForwardDeclaration(const VarDecl * VD)4397 CGDebugInfo::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, Line, T, Name, LinkageName, TemplateParameters,
4407                       DContext);
4408   auto Align = getDeclAlignIfRequired(VD, CGM.getContext());
4409   auto *GV = DBuilder.createTempGlobalVariableFwdDecl(
4410       DContext, Name, LinkageName, Unit, Line, getOrCreateType(T, Unit),
4411       !VD->isExternallyVisible(), nullptr, TemplateParameters, Align);
4412   FwdDeclReplaceMap.emplace_back(
4413       std::piecewise_construct,
4414       std::make_tuple(cast<VarDecl>(VD->getCanonicalDecl())),
4415       std::make_tuple(static_cast<llvm::Metadata *>(GV)));
4416   return GV;
4417 }
4418 
getDeclarationOrDefinition(const Decl * D)4419 llvm::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>(D))
4425     return getOrCreateType(CGM.getContext().getTypeDeclType(TD),
4426                            getOrCreateFile(TD->getLocation()));
4427   auto I = DeclCache.find(D->getCanonicalDecl());
4428 
4429   if (I != DeclCache.end()) {
4430     auto N = I->second;
4431     if (auto *GVE = dyn_cast_or_null<llvm::DIGlobalVariableExpression>(N))
4432       return GVE->getVariable();
4433     return cast<llvm::DINode>(N);
4434   }
4435 
4436   // Search imported declaration cache if it is already defined
4437   // as imported declaration.
4438   auto IE = ImportedDeclCache.find(D->getCanonicalDecl());
4439 
4440   if (IE != ImportedDeclCache.end()) {
4441     auto N = IE->second;
4442     if (auto *GVE = dyn_cast_or_null<llvm::DIImportedEntity>(N))
4443       return cast<llvm::DINode>(GVE);
4444     return dyn_cast_or_null<llvm::DINode>(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>(D))
4450     return getFunctionForwardDeclaration(FD);
4451   else if (const auto *VD = dyn_cast<VarDecl>(D))
4452     return getGlobalVariableForwardDeclaration(VD);
4453 
4454   return nullptr;
4455 }
4456 
getFunctionDeclaration(const Decl * D)4457 llvm::DISubprogram *CGDebugInfo::getFunctionDeclaration(const Decl *D) {
4458   if (!D || DebugKind <= llvm::codegenoptions::DebugLineTablesOnly)
4459     return nullptr;
4460 
4461   const auto *FD = dyn_cast<FunctionDecl>(D);
4462   if (!FD)
4463     return nullptr;
4464 
4465   // Setup context.
4466   auto *S = getDeclContextDescriptor(D);
4467 
4468   auto MI = SPCache.find(FD->getCanonicalDecl());
4469   if (MI == SPCache.end()) {
4470     if (const auto *MD = dyn_cast<CXXMethodDecl>(FD->getCanonicalDecl())) {
4471       return CreateCXXMemberFunction(MD, getOrCreateFile(MD->getLocation()),
4472                                      cast<llvm::DICompositeType>(S));
4473     }
4474   }
4475   if (MI != SPCache.end()) {
4476     auto *SP = dyn_cast_or_null<llvm::DISubprogram>(MI->second);
4477     if (SP && !SP->isDefinition())
4478       return SP;
4479   }
4480 
4481   for (auto *NextFD : FD->redecls()) {
4482     auto MI = SPCache.find(NextFD->getCanonicalDecl());
4483     if (MI != SPCache.end()) {
4484       auto *SP = dyn_cast_or_null<llvm::DISubprogram>(MI->second);
4485       if (SP && !SP->isDefinition())
4486         return SP;
4487     }
4488   }
4489   return nullptr;
4490 }
4491 
getObjCMethodDeclaration(const Decl * D,llvm::DISubroutineType * FnType,unsigned LineNo,llvm::DINode::DIFlags Flags,llvm::DISubprogram::DISPFlags SPFlags)4492 llvm::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>(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>(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(QTy.getAsOpaquePtr());
4517   if (It == TypeCache.end())
4518     return nullptr;
4519   auto *InterfaceType = cast<llvm::DICompositeType>(It->second);
4520   llvm::DISubprogram *FD = DBuilder.createFunction(
4521       InterfaceType, getObjCMethodName(OMD), StringRef(),
4522       InterfaceType->getFile(), LineNo, FnType, LineNo, Flags, SPFlags);
4523   DBuilder.finalizeSubprogram(FD);
4524   ObjCMethodCache[ID].push_back({FD, OMD->isDirectMethod()});
4525   return FD;
4526 }
4527 
4528 // getOrCreateFunctionType - Construct type. If it is a c++ method, include
4529 // implicit parameter "this".
getOrCreateFunctionType(const Decl * D,QualType FnType,llvm::DIFile * F)4530 llvm::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(DBuilder.getOrCreateTypeArray({}));
4540 
4541   if (const auto *Method = dyn_cast<CXXDestructorDecl>(D)) {
4542     // Read method type from 'FnType' because 'D.getType()' does not cover
4543     // implicit arguments for destructors.
4544     return getOrCreateMethodTypeForDestructor(Method, F, FnType);
4545   }
4546 
4547   if (const auto *Method = dyn_cast<CXXMethodDecl>(D))
4548     return getOrCreateMethodType(Method, 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>(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           QualType(OMethod->getClassInterface()->getTypeForDecl(), 0));
4564 
4565     Elts.push_back(getOrCreateType(ResultTy, 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>(FnType))
4571       if (FPT->getNumParams() > 1)
4572         SelfDeclTy = FPT->getParamType(0);
4573     if (!SelfDeclTy.isNull())
4574       Elts.push_back(
4575           CreateSelfType(SelfDeclTy, getOrCreateType(SelfDeclTy, F)));
4576     // "_cmd" pointer is always second argument.
4577     Elts.push_back(DBuilder.createArtificialType(
4578         getOrCreateType(CGM.getContext().getObjCSelType(), F)));
4579     // Get rest of the arguments.
4580     for (const auto *PI : OMethod->parameters())
4581       Elts.push_back(getOrCreateType(PI->getType(), F));
4582     // Variadic methods need a special marker at the end of the type list.
4583     if (OMethod->isVariadic())
4584       Elts.push_back(DBuilder.createUnspecifiedParameter());
4585 
4586     llvm::DITypeRefArray EltTypeArray = DBuilder.getOrCreateTypeArray(Elts);
4587     return DBuilder.createSubroutineType(EltTypeArray, llvm::DINode::FlagZero,
4588                                          getDwarfCC(CC));
4589   }
4590 
4591   // Handle variadic function types; they need an additional
4592   // unspecified parameter.
4593   if (const auto *FD = dyn_cast<FunctionDecl>(D))
4594     if (FD->isVariadic()) {
4595       SmallVector<llvm::Metadata *, 16> EltTys;
4596       EltTys.push_back(getOrCreateType(FD->getReturnType(), F));
4597       if (const auto *FPT = dyn_cast<FunctionProtoType>(FnType))
4598         for (QualType ParamType : FPT->param_types())
4599           EltTys.push_back(getOrCreateType(ParamType, F));
4600       EltTys.push_back(DBuilder.createUnspecifiedParameter());
4601       llvm::DITypeRefArray EltTypeArray = DBuilder.getOrCreateTypeArray(EltTys);
4602       return DBuilder.createSubroutineType(EltTypeArray, llvm::DINode::FlagZero,
4603                                            getDwarfCC(CC));
4604     }
4605 
4606   return cast<llvm::DISubroutineType>(getOrCreateType(FnType, F));
4607 }
4608 
4609 QualType
getFunctionType(const FunctionDecl * FD,QualType RetTy,const SmallVectorImpl<const VarDecl * > & Args)4610 CGDebugInfo::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(VD->getType());
4619   return CGM.getContext().getFunctionType(RetTy, ArgTypes,
4620                                           FunctionProtoType::ExtProtoInfo(CC));
4621 }
4622 
emitFunctionStart(GlobalDecl GD,SourceLocation Loc,SourceLocation ScopeLoc,QualType FnType,llvm::Function * Fn,bool CurFuncIsThunk)4623 void 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(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>(D)) {
4644     // If there is a subprogram for this function available then use it.
4645     auto FI = SPCache.find(FD->getCanonicalDecl());
4646     if (FI != SPCache.end()) {
4647       auto *SP = dyn_cast_or_null<llvm::DISubprogram>(FI->second);
4648       if (SP && SP->isDefinition()) {
4649         LexicalBlockStack.emplace_back(SP);
4650         RegionMap[D].reset(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>(FD->getBody());
4659   } else if (const auto *OMD = dyn_cast<ObjCMethodDecl>(D)) {
4660     Name = getObjCMethodName(OMD);
4661     Flags |= llvm::DINode::FlagPrototyped;
4662   } else if (isa<VarDecl>(D) &&
4663              GD.getDynamicInitKind() != DynamicInitKind::NoStub) {
4664     // This is a global initializer or atexit destructor for a global variable.
4665     Name = getDynamicInitializerName(cast<VarDecl>(D), GD.getDynamicInitKind(),
4666                                      Fn);
4667   } else {
4668     Name = Fn->getName();
4669 
4670     if (isa<BlockDecl>(D))
4671       LinkageName = Name;
4672 
4673     Flags |= llvm::DINode::FlagPrototyped;
4674   }
4675   Name.consume_front("\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>(D) || isa<CapturedDecl>(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.isValid() ? Loc : CurLoc);
4701   unsigned ScopeLine = getLineNumber(ScopeLoc);
4702   llvm::DISubroutineType *DIFnType = getOrCreateFunctionType(D, FnType, Unit);
4703   llvm::DISubprogram *Decl = nullptr;
4704   llvm::DINodeArray Annotations = nullptr;
4705   if (D) {
4706     Decl = isa<ObjCMethodDecl>(D)
4707                ? getObjCMethodDeclaration(D, 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       FDContext, Name, LinkageName, Unit, LineNo, DIFnType, ScopeLine,
4719       FlagsForDef, SPFlagsForDef, TParamsArray.get(), Decl, nullptr,
4720       Annotations, "", 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>(D))
4727     DeclCache[D->getCanonicalDecl()].reset(SP);
4728 
4729   // Push the function onto the lexical block stack.
4730   LexicalBlockStack.emplace_back(SP);
4731 
4732   if (HasDecl)
4733     RegionMap[D].reset(SP);
4734 }
4735 
EmitFunctionDecl(GlobalDecl GD,SourceLocation Loc,QualType FnType,llvm::Function * Fn)4736 void 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, 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>(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>(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("\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, Unit);
4781   // Key Instructions: Don't set flag on declarations.
4782   assert(~SPFlags & llvm::DISubprogram::SPFlagDefinition);
4783   llvm::DISubprogram *SP = DBuilder.createFunction(
4784       FDContext, Name, LinkageName, Unit, LineNo, STy, ScopeLine, Flags,
4785       SPFlags, TParamsArray.get(), nullptr, nullptr, Annotations,
4786       /*TargetFunctionName*/ "", /*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>(D)) {
4793       llvm::DITypeRefArray ParamTypes = STy->getTypeArray();
4794       unsigned ArgNo = 1;
4795       for (ParmVarDecl *PD : FD->parameters()) {
4796         llvm::DINodeArray ParamAnnotations = CollectBTFDeclTagAnnotations(PD);
4797         DBuilder.createParameterVariable(
4798             SP, PD->getName(), ArgNo, Unit, LineNo, ParamTypes[ArgNo], true,
4799             llvm::DINode::FlagZero, ParamAnnotations);
4800         ++ArgNo;
4801       }
4802     }
4803   }
4804 
4805   if (IsDeclForCallSite)
4806     Fn->setSubprogram(SP);
4807 }
4808 
EmitFuncDeclForCallSite(llvm::CallBase * CallOrInvoke,QualType CalleeType,const FunctionDecl * CalleeDecl)4809 void 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(CalleeDecl, CalleeDecl->getLocation(), CalleeType, Func);
4831 }
4832 
EmitInlineFunctionStart(CGBuilderTy & Builder,GlobalDecl GD)4833 void CGDebugInfo::EmitInlineFunctionStart(CGBuilderTy &Builder, GlobalDecl GD) {
4834   const auto *FD = cast<FunctionDecl>(GD.getDecl());
4835   // If there is a subprogram for this function available then use it.
4836   auto FI = SPCache.find(FD->getCanonicalDecl());
4837   llvm::DISubprogram *SP = nullptr;
4838   if (FI != SPCache.end())
4839     SP = dyn_cast_or_null<llvm::DISubprogram>(FI->second);
4840   if (!SP || !SP->isDefinition())
4841     SP = getFunctionStub(GD);
4842   FnBeginRegionCount.push_back(LexicalBlockStack.size());
4843   LexicalBlockStack.emplace_back(SP);
4844   setInlinedAt(Builder.getCurrentDebugLocation());
4845   EmitLocation(Builder, FD->getLocation());
4846 }
4847 
EmitInlineFunctionEnd(CGBuilderTy & Builder)4848 void CGDebugInfo::EmitInlineFunctionEnd(CGBuilderTy &Builder) {
4849   assert(CurInlinedAt && "unbalanced inline scope stack");
4850   EmitFunctionEnd(Builder, nullptr);
4851   setInlinedAt(llvm::DebugLoc(CurInlinedAt).getInlinedAt());
4852 }
4853 
EmitLocation(CGBuilderTy & Builder,SourceLocation Loc)4854 void 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(CGM.getLLVMContext(), getLineNumber(CurLoc),
4864                             getColumnNumber(CurLoc), Scope, CurInlinedAt));
4865 }
4866 
CreateLexicalBlock(SourceLocation Loc)4867 void CGDebugInfo::CreateLexicalBlock(SourceLocation Loc) {
4868   llvm::MDNode *Back = nullptr;
4869   if (!LexicalBlockStack.empty())
4870     Back = LexicalBlockStack.back().get();
4871   LexicalBlockStack.emplace_back(DBuilder.createLexicalBlock(
4872       cast<llvm::DIScope>(Back), getOrCreateFile(CurLoc), getLineNumber(CurLoc),
4873       getColumnNumber(CurLoc)));
4874 }
4875 
AppendAddressSpaceXDeref(unsigned AddressSpace,SmallVectorImpl<uint64_t> & Expr) const4876 void 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(llvm::dwarf::DW_OP_constu);
4884   Expr.push_back(*DWARFAddressSpace);
4885   Expr.push_back(llvm::dwarf::DW_OP_swap);
4886   Expr.push_back(llvm::dwarf::DW_OP_xderef);
4887 }
4888 
EmitLexicalBlockStart(CGBuilderTy & Builder,SourceLocation Loc)4889 void 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       CGM.getLLVMContext(), getLineNumber(Loc), getColumnNumber(Loc),
4897       LexicalBlockStack.back(), 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 
EmitLexicalBlockEnd(CGBuilderTy & Builder,SourceLocation Loc)4906 void 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 
EmitFunctionEnd(CGBuilderTy & Builder,llvm::Function * Fn)4919 void 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, CurLoc);
4928     LexicalBlockStack.pop_back();
4929   }
4930   FnBeginRegionCount.pop_back();
4931 
4932   if (Fn && Fn->getSubprogram())
4933     DBuilder.finalizeSubprogram(Fn->getSubprogram());
4934 }
4935 
4936 CGDebugInfo::BlockByRefType
EmitTypeForVarWithBlocksAttr(const VarDecl * VD,uint64_t * XOffset)4937 CGDebugInfo::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(VD->getLocation());
4945   QualType Type = VD->getType();
4946 
4947   FieldOffset = 0;
4948   FType = CGM.getContext().getPointerType(CGM.getContext().VoidTy);
4949   EltTys.push_back(CreateMemberType(Unit, FType, "__isa", &FieldOffset));
4950   EltTys.push_back(CreateMemberType(Unit, FType, "__forwarding", &FieldOffset));
4951   FType = CGM.getContext().IntTy;
4952   EltTys.push_back(CreateMemberType(Unit, FType, "__flags", &FieldOffset));
4953   EltTys.push_back(CreateMemberType(Unit, FType, "__size", &FieldOffset));
4954 
4955   bool HasCopyAndDispose = CGM.getContext().BlockRequiresCopying(Type, VD);
4956   if (HasCopyAndDispose) {
4957     FType = CGM.getContext().getPointerType(CGM.getContext().VoidTy);
4958     EltTys.push_back(
4959         CreateMemberType(Unit, FType, "__copy_helper", &FieldOffset));
4960     EltTys.push_back(
4961         CreateMemberType(Unit, FType, "__destroy_helper", &FieldOffset));
4962   }
4963   bool HasByrefExtendedLayout;
4964   Qualifiers::ObjCLifetime Lifetime;
4965   if (CGM.getContext().getByrefLifetime(Type, Lifetime,
4966                                         HasByrefExtendedLayout) &&
4967       HasByrefExtendedLayout) {
4968     FType = CGM.getContext().getPointerType(CGM.getContext().VoidTy);
4969     EltTys.push_back(
4970         CreateMemberType(Unit, FType, "__byref_variable_layout", &FieldOffset));
4971   }
4972 
4973   CharUnits Align = CGM.getContext().getDeclAlign(VD);
4974   if (Align > CGM.getContext().toCharUnitsFromBits(
4975                   CGM.getTarget().getPointerAlign(LangAS::Default))) {
4976     CharUnits FieldOffsetInBytes =
4977         CGM.getContext().toCharUnitsFromBits(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           CGM.getContext().CharTy, pad, nullptr, ArraySizeModifier::Normal, 0);
4985       EltTys.push_back(CreateMemberType(Unit, FType, "", &FieldOffset));
4986     }
4987   }
4988 
4989   FType = Type;
4990   llvm::DIType *WrappedTy = getOrCreateType(FType, Unit);
4991   FieldSize = CGM.getContext().getTypeSize(FType);
4992   FieldAlign = CGM.getContext().toBits(Align);
4993 
4994   *XOffset = FieldOffset;
4995   llvm::DIType *FieldTy = DBuilder.createMemberType(
4996       Unit, VD->getName(), Unit, 0, FieldSize, FieldAlign, FieldOffset,
4997       llvm::DINode::FlagZero, WrappedTy);
4998   EltTys.push_back(FieldTy);
4999   FieldOffset += FieldSize;
5000 
5001   llvm::DINodeArray Elements = DBuilder.getOrCreateArray(EltTys);
5002   return {DBuilder.createStructType(Unit, "", Unit, 0, FieldOffset, 0,
5003                                     llvm::DINode::FlagZero, nullptr, Elements),
5004           WrappedTy};
5005 }
5006 
EmitDeclare(const VarDecl * VD,llvm::Value * Storage,std::optional<unsigned> ArgNo,CGBuilderTy & Builder,const bool UsePointerValue)5007 llvm::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(VD->getLocation());
5022   llvm::DIType *Ty;
5023   uint64_t XOffset = 0;
5024   if (VD->hasAttr<BlocksAttr>())
5025     Ty = EmitTypeForVarWithBlocksAttr(VD, &XOffset).WrappedType;
5026   else
5027     Ty = getOrCreateType(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(VD->getLocation());
5039     Column = getColumnNumber(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(VD, CGM.getContext());
5047 
5048   unsigned AddressSpace = CGM.getTypes().getTargetAddressSpace(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>(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>(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>(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(32);
5074       Expr.push_back(llvm::dwarf::DW_OP_plus_uconst);
5075       // offset of __forwarding field
5076       offset = CGM.getContext().toCharUnitsFromBits(
5077           CGM.getTarget().getPointerWidth(LangAS::Default));
5078       Expr.push_back(offset.getQuantity());
5079       Expr.push_back(llvm::dwarf::DW_OP_deref);
5080       Expr.push_back(llvm::dwarf::DW_OP_plus_uconst);
5081       // offset of x field
5082       offset = CGM.getContext().toCharUnitsFromBits(XOffset);
5083       Expr.push_back(offset.getQuantity());
5084     }
5085   } else if (const auto *RT = dyn_cast<RecordType>(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(Field->getType(), Unit);
5099         StringRef FieldName = Field->getName();
5100 
5101         // Ignore unnamed fields. Do not ignore unnamed records.
5102         if (FieldName.empty() && !isa<RecordType>(Field->getType()))
5103           continue;
5104 
5105         // Use VarDecl's Tag, Scope and Line number.
5106         auto FieldAlign = getDeclAlignIfRequired(Field, CGM.getContext());
5107         auto *D = DBuilder.createAutoVariable(
5108             Scope, FieldName, Unit, Line, FieldTy, CGM.getLangOpts().Optimize,
5109             Flags | llvm::DINode::FlagArtificial, FieldAlign);
5110 
5111         // Insert an llvm.dbg.declare into the current block.
5112         DBuilder.insertDeclare(Storage, D, DBuilder.createExpression(Expr),
5113                                llvm::DILocation::get(CGM.getLLVMContext(), Line,
5114                                                      Column, Scope,
5115                                                      CurInlinedAt),
5116                                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(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(VD);
5134     D = DBuilder.createParameterVariable(Scope, Name, *ArgNo, Unit, Line, Ty,
5135                                          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>(Scope) || !Scope->isDistinct())
5149         return nullptr;
5150 
5151       auto Iter = llvm::find_if(CoroutineParameterMappings, [&](auto &Pair) {
5152         Stmt *StmtPtr = const_cast<Stmt *>(Pair.second);
5153         if (DeclStmt *DeclStmtPtr = dyn_cast<DeclStmt>(StmtPtr)) {
5154           DeclGroupRef DeclGroup = DeclStmtPtr->getDeclGroup();
5155           Decl *Decl = DeclGroup.getSingleDecl();
5156           if (VD == dyn_cast_or_null<VarDecl>(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(ParamDbgMappings, [&](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, Unit, Line, Ty,
5178                                       CGM.getLangOpts().Optimize, Flags, Align);
5179   }
5180   // Insert an llvm.dbg.declare into the current block.
5181   DBuilder.insertDeclare(Storage, D, DBuilder.createExpression(Expr),
5182                          llvm::DILocation::get(CGM.getLLVMContext(), Line,
5183                                                Column, Scope, CurInlinedAt),
5184                          Builder.GetInsertBlock());
5185 
5186   return D;
5187 }
5188 
EmitDeclare(const BindingDecl * BD,llvm::Value * Storage,std::optional<unsigned> ArgNo,CGBuilderTy & Builder,const bool UsePointerValue)5189 llvm::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>(BD->getBinding()))
5201     return nullptr;
5202 
5203   llvm::DIFile *Unit = getOrCreateFile(BD->getLocation());
5204   llvm::DIType *Ty = getOrCreateType(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(BD, CGM.getContext());
5212   unsigned AddressSpace = CGM.getTypes().getTargetAddressSpace(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(llvm::dwarf::DW_OP_deref);
5224   }
5225 
5226   unsigned Line = getLineNumber(BD->getLocation());
5227   unsigned Column = getColumnNumber(BD->getLocation());
5228   StringRef Name = BD->getName();
5229   auto *Scope = cast<llvm::DIScope>(LexicalBlockStack.back());
5230   // Create the descriptor for the variable.
5231   llvm::DILocalVariable *D = DBuilder.createAutoVariable(
5232       Scope, Name, Unit, Line, Ty, CGM.getLangOpts().Optimize,
5233       llvm::DINode::FlagZero, Align);
5234 
5235   if (const MemberExpr *ME = dyn_cast<MemberExpr>(BD->getBinding())) {
5236     if (const FieldDecl *FD = dyn_cast<FieldDecl>(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(parent);
5242       const uint64_t fieldOffset = layout.getFieldOffset(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(llvm::dwarf::DW_OP_plus_uconst);
5251           Expr.push_back(Info.StorageOffset.getQuantity());
5252         }
5253         // Use LLVM_extract_bits to extract the appropriate bits from this
5254         // bitfield.
5255         Expr.push_back(Info.IsSigned
5256                            ? llvm::dwarf::DW_OP_LLVM_extract_bits_sext
5257                            : llvm::dwarf::DW_OP_LLVM_extract_bits_zext);
5258         Expr.push_back(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(BD->getType());
5262         Expr.push_back(std::min((uint64_t)Info.Size, 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(llvm::dwarf::DW_OP_plus_uconst);
5267         Expr.push_back(
5268             CGM.getContext().toCharUnitsFromBits(fieldOffset).getQuantity());
5269       }
5270     }
5271   } else if (const ArraySubscriptExpr *ASE =
5272                  dyn_cast<ArraySubscriptExpr>(BD->getBinding())) {
5273     if (const IntegerLiteral *IL = dyn_cast<IntegerLiteral>(ASE->getIdx())) {
5274       const uint64_t value = IL->getValue().getZExtValue();
5275       const uint64_t typeSize = CGM.getContext().getTypeSize(BD->getType());
5276 
5277       if (value != 0) {
5278         Expr.push_back(llvm::dwarf::DW_OP_plus_uconst);
5279         Expr.push_back(CGM.getContext()
5280                            .toCharUnitsFromBits(value * typeSize)
5281                            .getQuantity());
5282       }
5283     }
5284   }
5285 
5286   // Insert an llvm.dbg.declare into the current block.
5287   DBuilder.insertDeclare(Storage, D, DBuilder.createExpression(Expr),
5288                          llvm::DILocation::get(CGM.getLLVMContext(), Line,
5289                                                Column, Scope, CurInlinedAt),
5290                          Builder.GetInsertBlock());
5291 
5292   return D;
5293 }
5294 
5295 llvm::DILocalVariable *
EmitDeclareOfAutoVariable(const VarDecl * VD,llvm::Value * Storage,CGBuilderTy & Builder,const bool UsePointerValue)5296 CGDebugInfo::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>(VD)) {
5302     for (BindingDecl *B : DD->flat_bindings())
5303       EmitDeclare(B, Storage, std::nullopt, Builder,
5304                   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, std::nullopt, Builder, UsePointerValue);
5311 }
5312 
EmitLabel(const LabelDecl * D,CGBuilderTy & Builder)5313 void 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>(LexicalBlockStack.back());
5321   llvm::DIFile *Unit = getOrCreateFile(D->getLocation());
5322 
5323   // Get location information.
5324   unsigned Line = getLineNumber(D->getLocation());
5325   unsigned Column = getColumnNumber(D->getLocation());
5326 
5327   StringRef Name = D->getName();
5328 
5329   // Create the descriptor for the label.
5330   auto *L = DBuilder.createLabel(
5331       Scope, Name, Unit, Line, Column, /*IsArtificial=*/false,
5332       /*CoroSuspendIdx=*/std::nullopt, CGM.getLangOpts().Optimize);
5333 
5334   // Insert an llvm.dbg.label into the current block.
5335   DBuilder.insertLabel(L,
5336                        llvm::DILocation::get(CGM.getLLVMContext(), Line, Column,
5337                                              Scope, CurInlinedAt),
5338                        Builder.GetInsertBlock()->end());
5339 }
5340 
CreateSelfType(const QualType & QualTy,llvm::DIType * Ty)5341 llvm::DIType *CGDebugInfo::CreateSelfType(const QualType &QualTy,
5342                                           llvm::DIType *Ty) {
5343   llvm::DIType *CachedTy = getTypeOrNull(QualTy);
5344   if (CachedTy)
5345     Ty = CachedTy;
5346   return DBuilder.createObjectPointerType(Ty, /*Implicit=*/true);
5347 }
5348 
EmitDeclareOfBlockDeclRefVariable(const VarDecl * VD,llvm::Value * Storage,CGBuilderTy & Builder,const CGBlockInfo & blockInfo,llvm::Instruction * InsertPoint)5349 void 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(VD->getLocation());
5364   llvm::DIType *Ty;
5365   if (isByRef)
5366     Ty = EmitTypeForVarWithBlocksAttr(VD, &XOffset).WrappedType;
5367   else
5368     Ty = getOrCreateType(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>(VD))
5373     if (IPD->getParameterKind() == ImplicitParamKind::ObjCSelf)
5374       Ty = CreateSelfType(VD->getType(), Ty);
5375 
5376   // Get location information.
5377   const unsigned Line =
5378       getLineNumber(VD->getLocation().isValid() ? VD->getLocation() : CurLoc);
5379   unsigned Column = getColumnNumber(VD->getLocation());
5380 
5381   const llvm::DataLayout &target = CGM.getDataLayout();
5382 
5383   CharUnits offset = CharUnits::fromQuantity(
5384       target.getStructLayout(blockInfo.StructureType)
5385           ->getElementOffset(blockInfo.getCapture(VD).getIndex()));
5386 
5387   SmallVector<uint64_t, 9> addr;
5388   addr.push_back(llvm::dwarf::DW_OP_deref);
5389   addr.push_back(llvm::dwarf::DW_OP_plus_uconst);
5390   addr.push_back(offset.getQuantity());
5391   if (isByRef) {
5392     addr.push_back(llvm::dwarf::DW_OP_deref);
5393     addr.push_back(llvm::dwarf::DW_OP_plus_uconst);
5394     // offset of __forwarding field
5395     offset =
5396         CGM.getContext().toCharUnitsFromBits(target.getPointerSizeInBits(0));
5397     addr.push_back(offset.getQuantity());
5398     addr.push_back(llvm::dwarf::DW_OP_deref);
5399     addr.push_back(llvm::dwarf::DW_OP_plus_uconst);
5400     // offset of x field
5401     offset = CGM.getContext().toCharUnitsFromBits(XOffset);
5402     addr.push_back(offset.getQuantity());
5403   }
5404 
5405   // Create the descriptor for the variable.
5406   auto Align = getDeclAlignIfRequired(VD, CGM.getContext());
5407   auto *D = DBuilder.createAutoVariable(
5408       cast<llvm::DILocalScope>(LexicalBlockStack.back()), VD->getName(), Unit,
5409       Line, Ty, false, llvm::DINode::FlagZero, Align);
5410 
5411   // Insert an llvm.dbg.declare into the current block.
5412   auto DL = llvm::DILocation::get(CGM.getLLVMContext(), Line, Column,
5413                                   LexicalBlockStack.back(), CurInlinedAt);
5414   auto *Expr = DBuilder.createExpression(addr);
5415   if (InsertPoint)
5416     DBuilder.insertDeclare(Storage, D, Expr, DL, InsertPoint->getIterator());
5417   else
5418     DBuilder.insertDeclare(Storage, D, Expr, DL, Builder.GetInsertBlock());
5419 }
5420 
5421 llvm::DILocalVariable *
EmitDeclareOfArgVariable(const VarDecl * VD,llvm::Value * AI,unsigned ArgNo,CGBuilderTy & Builder,bool UsePointerValue)5422 CGDebugInfo::EmitDeclareOfArgVariable(const VarDecl *VD, llvm::Value *AI,
5423                                       unsigned ArgNo, CGBuilderTy &Builder,
5424                                       bool UsePointerValue) {
5425   assert(CGM.getCodeGenOpts().hasReducedDebugInfo());
5426   return EmitDeclare(VD, AI, ArgNo, Builder, UsePointerValue);
5427 }
5428 
5429 namespace {
5430 struct BlockLayoutChunk {
5431   uint64_t OffsetInBits;
5432   const BlockDecl::Capture *Capture;
5433 };
operator <(const BlockLayoutChunk & l,const BlockLayoutChunk & r)5434 bool operator<(const BlockLayoutChunk &l, const BlockLayoutChunk &r) {
5435   return l.OffsetInBits < r.OffsetInBits;
5436 }
5437 } // namespace
5438 
collectDefaultFieldsForBlockLiteralDeclare(const CGBlockInfo & Block,const ASTContext & Context,SourceLocation Loc,const llvm::StructLayout & BlockLayout,llvm::DIFile * Unit,SmallVectorImpl<llvm::Metadata * > & Fields)5439 void 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(createFieldType("__size", Context.IntTy, Loc, AS_public,
5448                                      BlockLayout.getElementOffsetInBits(0),
5449                                      Unit, Unit));
5450     Fields.push_back(createFieldType("__align", Context.IntTy, Loc, AS_public,
5451                                      BlockLayout.getElementOffsetInBits(1),
5452                                      Unit, Unit));
5453   } else {
5454     Fields.push_back(createFieldType("__isa", Context.VoidPtrTy, Loc, AS_public,
5455                                      BlockLayout.getElementOffsetInBits(0),
5456                                      Unit, Unit));
5457     Fields.push_back(createFieldType("__flags", Context.IntTy, Loc, AS_public,
5458                                      BlockLayout.getElementOffsetInBits(1),
5459                                      Unit, Unit));
5460     Fields.push_back(
5461         createFieldType("__reserved", Context.IntTy, Loc, AS_public,
5462                         BlockLayout.getElementOffsetInBits(2), Unit, Unit));
5463     auto *FnTy = Block.getBlockExpr()->getFunctionType();
5464     auto FnPtrType = CGM.getContext().getPointerType(FnTy->desugar());
5465     Fields.push_back(createFieldType("__FuncPtr", FnPtrType, Loc, AS_public,
5466                                      BlockLayout.getElementOffsetInBits(3),
5467                                      Unit, Unit));
5468     Fields.push_back(createFieldType(
5469         "__descriptor",
5470         Context.getPointerType(Block.NeedsCopyDispose
5471                                    ? Context.getBlockDescriptorExtendedType()
5472                                    : Context.getBlockDescriptorType()),
5473         Loc, AS_public, BlockLayout.getElementOffsetInBits(4), Unit, Unit));
5474   }
5475 }
5476 
EmitDeclareOfBlockLiteralArgVariable(const CGBlockInfo & block,StringRef Name,unsigned ArgNo,llvm::AllocaInst * Alloca,CGBuilderTy & Builder)5477 void 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);
5489   unsigned line = getLineNumber(loc);
5490   unsigned column = getColumnNumber(loc);
5491 
5492   // Build the debug-info type for the block literal.
5493   getDeclContextDescriptor(blockDecl);
5494 
5495   const llvm::StructLayout *blockLayout =
5496       CGM.getDataLayout().getStructLayout(block.StructureType);
5497 
5498   SmallVector<llvm::Metadata *, 16> fields;
5499   collectDefaultFieldsForBlockLiteralDeclare(block, C, loc, *blockLayout, tunit,
5500                                              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(block.CXXThisIndex);
5511     chunk.Capture = nullptr;
5512     chunks.push_back(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(variable);
5519 
5520     // Ignore constant captures.
5521     if (captureInfo.isConstant())
5522       continue;
5523 
5524     BlockLayoutChunk chunk;
5525     chunk.OffsetInBits =
5526         blockLayout->getElementOffsetInBits(captureInfo.getIndex());
5527     chunk.Capture = &capture;
5528     chunks.push_back(chunk);
5529   }
5530 
5531   // Sort by offset.
5532   llvm::array_pod_sort(chunks.begin(), 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>(blockDecl->getNonClosureContext()))
5543         type = Method->getThisType();
5544       else if (auto *RDecl = dyn_cast<CXXRecordDecl>(blockDecl->getParent()))
5545         type = QualType(RDecl->getTypeForDecl(), 0);
5546       else
5547         llvm_unreachable("unexpected block declcontext");
5548 
5549       fields.push_back(createFieldType("this", type, loc, AS_public,
5550                                        offsetInBits, tunit, 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(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(variable, &xoffset).BlockByRefWrapper;
5565       fieldType = DBuilder.createPointerType(fieldType, PtrInfo.Width);
5566       fieldType = DBuilder.createMemberType(tunit, name, tunit, line,
5567                                             PtrInfo.Width, Align, offsetInBits,
5568                                             llvm::DINode::FlagZero, fieldType);
5569     } else {
5570       auto Align = getDeclAlignIfRequired(variable, CGM.getContext());
5571       fieldType = createFieldType(name, variable->getType(), loc, AS_public,
5572                                   offsetInBits, Align, tunit, tunit);
5573     }
5574     fields.push_back(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(fields);
5582 
5583   llvm::DIType *type =
5584       DBuilder.createStructType(tunit, typeName.str(), tunit, line,
5585                                 CGM.getContext().toBits(block.BlockSize), 0,
5586                                 llvm::DINode::FlagZero, nullptr, fieldsArray);
5587   type = DBuilder.createPointerType(type, CGM.PointerWidthInBits);
5588 
5589   // Get overall information about the block.
5590   llvm::DINode::DIFlags flags = llvm::DINode::FlagArtificial;
5591   auto *scope = cast<llvm::DILocalScope>(LexicalBlockStack.back());
5592 
5593   // Create the descriptor for the parameter.
5594   auto *debugVar = DBuilder.createParameterVariable(
5595       scope, Name, ArgNo, tunit, line, type, CGM.getLangOpts().Optimize, flags);
5596 
5597   // Insert an llvm.dbg.declare into the current block.
5598   DBuilder.insertDeclare(Alloca, debugVar, DBuilder.createExpression(),
5599                          llvm::DILocation::get(CGM.getLLVMContext(), line,
5600                                                column, scope, CurInlinedAt),
5601                          Builder.GetInsertBlock());
5602 }
5603 
5604 llvm::DIDerivedType *
getOrCreateStaticDataMemberDeclarationOrNull(const VarDecl * D)5605 CGDebugInfo::getOrCreateStaticDataMemberDeclarationOrNull(const VarDecl *D) {
5606   if (!D || !D->isStaticDataMember())
5607     return nullptr;
5608 
5609   auto MI = StaticDataMemberCache.find(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>(getDeclContextDescriptor(D));
5619   return CreateRecordStaticField(D, Ctxt, cast<RecordDecl>(DC));
5620 }
5621 
CollectAnonRecordDecls(const RecordDecl * RD,llvm::DIFile * Unit,unsigned LineNo,StringRef LinkageName,llvm::GlobalVariable * Var,llvm::DIScope * DContext)5622 llvm::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(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>(Field->getType()))
5634         GVE = CollectAnonRecordDecls(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         DContext, FieldName, LinkageName, Unit, LineNo, FieldTy,
5641         Var->hasLocalLinkage());
5642     Var->addDebugInfo(GVE);
5643   }
5644   return GVE;
5645 }
5646 
5647 static bool ReferencesAnonymousEntity(ArrayRef<TemplateArgument> Args);
ReferencesAnonymousEntity(RecordType * RT)5648 static 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>(RT->getDecl());
5655   if (!RD)
5656     return false;
5657   if (!RD->getIdentifier())
5658     return true;
5659   auto *TSpecial = dyn_cast<ClassTemplateSpecializationDecl>(RD);
5660   if (!TSpecial)
5661     return false;
5662   return ReferencesAnonymousEntity(TSpecial->getTemplateArgs().asArray());
5663 }
ReferencesAnonymousEntity(ArrayRef<TemplateArgument> Args)5664 static bool ReferencesAnonymousEntity(ArrayRef<TemplateArgument> Args) {
5665   return llvm::any_of(Args, [&](const TemplateArgument &TA) {
5666     switch (TA.getKind()) {
5667     case TemplateArgument::Pack:
5668       return ReferencesAnonymousEntity(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(TA.getAsType());
5683       if (RT.RefAnon)
5684         return true;
5685       break;
5686     }
5687     default:
5688       break;
5689     }
5690     return false;
5691   });
5692 }
5693 namespace {
5694 struct ReconstitutableType : public RecursiveASTVisitor<ReconstitutableType> {
5695   bool Reconstitutable = true;
VisitVectorType__anon130eb37b0c11::ReconstitutableType5696   bool VisitVectorType(VectorType *FT) {
5697     Reconstitutable = false;
5698     return false;
5699   }
VisitAtomicType__anon130eb37b0c11::ReconstitutableType5700   bool VisitAtomicType(AtomicType *FT) {
5701     Reconstitutable = false;
5702     return false;
5703   }
VisitType__anon130eb37b0c11::ReconstitutableType5704   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   }
TraverseEnumType__anon130eb37b0c11::ReconstitutableType5713   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>(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   }
VisitFunctionProtoType__anon130eb37b0c11::ReconstitutableType5728   bool VisitFunctionProtoType(FunctionProtoType *FT) {
5729     // noexcept is not encoded in DWARF, so the reversi
5730     Reconstitutable &= !isNoexceptExceptionSpec(FT->getExceptionSpecType());
5731     Reconstitutable &= !FT->getNoReturnAttr();
5732     return Reconstitutable;
5733   }
VisitRecordType__anon130eb37b0c11::ReconstitutableType5734   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.
IsReconstitutableType(QualType QT)5745 static bool IsReconstitutableType(QualType QT) {
5746   ReconstitutableType T;
5747   T.TraverseType(QT);
5748   return T.Reconstitutable;
5749 }
5750 
HasReconstitutableArgs(ArrayRef<TemplateArgument> Args) const5751 bool CGDebugInfo::HasReconstitutableArgs(
5752     ArrayRef<TemplateArgument> Args) const {
5753   return llvm::all_of(Args, [&](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(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(TA.getIntegralType());
5786     case TemplateArgument::StructuralValue:
5787       return false;
5788     case TemplateArgument::Type:
5789       return IsReconstitutableType(TA.getAsType());
5790     case TemplateArgument::Expression:
5791       return IsReconstitutableType(TA.getAsExpr()->getType());
5792     default:
5793       llvm_unreachable("Other, unresolved, template arguments should "
5794                        "not be seen here");
5795     }
5796   });
5797 }
5798 
GetName(const Decl * D,bool Qualified) const5799 std::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>(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>(ND)) {
5815     Args = GetTemplateArgs(RD);
5816   } else if (auto *FD = dyn_cast<FunctionDecl>(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>(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) && !IsOperatorOverload;
5849 
5850   PrintingPolicy PP = getPrintingPolicy();
5851 
5852   if (TemplateNamesKind == llvm::codegenoptions::DebugTemplateNamesKind::Full ||
5853       !Reconstitutable) {
5854     ND->getNameForDiagnostic(OS, 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, PP);
5870       printTemplateArgumentList(EncodedOriginalNameOS, Args->Args, 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 
EmitGlobalVariable(llvm::GlobalVariable * Var,const VarDecl * D)5882 void 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, 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(D->getCanonicalDecl());
5895   if (Cached != DeclCache.end())
5896     return Var->addDebugInfo(
5897         cast<llvm::DIGlobalVariableExpression>(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(D, Unit, LineNo, T, DeclName, LinkageName,
5907                       TemplateParameters, 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, CGM.getContext());
5923 
5924     SmallVector<uint64_t, 4> Expr;
5925     unsigned AddressSpace = CGM.getTypes().getTargetAddressSpace(D->getType());
5926     if (CGM.getLangOpts().CUDA && CGM.getLangOpts().CUDAIsDevice) {
5927       if (D->hasAttr<CUDASharedAttr>())
5928         AddressSpace =
5929             CGM.getContext().getTargetAddressSpace(LangAS::cuda_shared);
5930       else if (D->hasAttr<CUDAConstantAttr>())
5931         AddressSpace =
5932             CGM.getContext().getTargetAddressSpace(LangAS::cuda_constant);
5933     }
5934     AppendAddressSpaceXDeref(AddressSpace, Expr);
5935 
5936     llvm::DINodeArray Annotations = CollectBTFDeclTagAnnotations(D);
5937     GVE = DBuilder.createGlobalVariableExpression(
5938         DContext, DeclName, LinkageName, Unit, LineNo, getOrCreateType(T, Unit),
5939         Var->hasLocalLinkage(), true,
5940         Expr.empty() ? nullptr : DBuilder.createExpression(Expr),
5941         getOrCreateStaticDataMemberDeclarationOrNull(D), TemplateParameters,
5942         Align, Annotations);
5943     Var->addDebugInfo(GVE);
5944   }
5945   DeclCache[D->getCanonicalDecl()].reset(GVE);
5946 }
5947 
EmitGlobalVariable(const ValueDecl * VD,const APValue & Init)5948 void 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(VD, true);
5954   });
5955 
5956   auto Align = getDeclAlignIfRequired(VD, CGM.getContext());
5957   // Create the descriptor for the variable.
5958   llvm::DIFile *Unit = getOrCreateFile(VD->getLocation());
5959   StringRef Name = VD->getName();
5960   llvm::DIType *Ty = getOrCreateType(VD->getType(), Unit);
5961 
5962   if (const auto *ECD = dyn_cast<EnumConstantDecl>(VD)) {
5963     const auto *ED = cast<EnumDecl>(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>(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(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>(VD->getDeclContext()))
5987     return;
5988 
5989   VD = cast<ValueDecl>(VD->getCanonicalDecl());
5990   auto *VarD = dyn_cast<VarDecl>(VD);
5991   if (VarD && VarD->isStaticDataMember()) {
5992     auto *RD = cast<RecordDecl>(VarD->getDeclContext());
5993     getDeclContextDescriptor(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         CGM.getContext().getRecordType(RD).getAsOpaquePtr());
6000 
6001     return;
6002   }
6003   llvm::DIScope *DContext = getDeclContextDescriptor(VD);
6004 
6005   auto &GV = DeclCache[VD];
6006   if (GV)
6007     return;
6008 
6009   llvm::DIExpression *InitExpr = createConstantValueExpression(VD, Init);
6010   llvm::MDTuple *TemplateParameters = nullptr;
6011 
6012   if (isa<VarTemplateSpecializationDecl>(VD))
6013     if (VarD) {
6014       llvm::DINodeArray parameterNodes = CollectVarTemplateParams(VarD, &*Unit);
6015       TemplateParameters = parameterNodes.get();
6016     }
6017 
6018   GV.reset(DBuilder.createGlobalVariableExpression(
6019       DContext, Name, StringRef(), Unit, getLineNumber(VD->getLocation()), Ty,
6020       true, true, InitExpr, getOrCreateStaticDataMemberDeclarationOrNull(VarD),
6021       TemplateParameters, Align));
6022 }
6023 
EmitExternalVariable(llvm::GlobalVariable * Var,const VarDecl * D)6024 void 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, CGM.getContext());
6031   llvm::DIFile *Unit = getOrCreateFile(D->getLocation());
6032   StringRef Name = D->getName();
6033   llvm::DIType *Ty = getOrCreateType(D->getType(), Unit);
6034 
6035   llvm::DIScope *DContext = getDeclContextDescriptor(D);
6036   llvm::DIGlobalVariableExpression *GVE =
6037       DBuilder.createGlobalVariableExpression(
6038           DContext, Name, StringRef(), Unit, getLineNumber(D->getLocation()),
6039           Ty, false, false, nullptr, nullptr, nullptr, Align);
6040   Var->addDebugInfo(GVE);
6041 }
6042 
EmitPseudoVariable(CGBuilderTy & Builder,llvm::Instruction * Value,QualType Ty)6043 void 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>(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(llvm::findDbgDeclares(Var), DeclareTypeMatches) ||
6071         any_of(llvm::findDVRDeclares(Var), DeclareTypeMatches))
6072       return;
6073   }
6074 
6075   llvm::DILocalVariable *D =
6076       DBuilder.createAutoVariable(LexicalBlockStack.back(), "", nullptr, 0,
6077                                   Type, false, llvm::DINode::FlagArtificial);
6078 
6079   if (auto InsertPoint = Value->getInsertionPointAfterDef()) {
6080     DBuilder.insertDbgValueIntrinsic(Value, D, DBuilder.createExpression(), DIL,
6081                                      *InsertPoint);
6082   }
6083 }
6084 
EmitGlobalAlias(const llvm::GlobalValue * GV,const GlobalDecl GD)6085 void 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>(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             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       DContext, DI, getOrCreateFile(Loc), getLineNumber(Loc), D->getName());
6121 
6122   // Record this DIE in the cache for nested declaration reference.
6123   ImportedDeclCache[GD.getCanonicalDecl().getDecl()].reset(ImportDI);
6124 }
6125 
AddStringLiteralDebugInfo(llvm::GlobalVariable * GV,const StringLiteral * S)6126 void CGDebugInfo::AddStringLiteralDebugInfo(llvm::GlobalVariable *GV,
6127                                             const StringLiteral *S) {
6128   SourceLocation Loc = S->getStrTokenLoc(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           nullptr, StringRef(), StringRef(), getOrCreateFile(Loc),
6137           getLineNumber(Loc), getOrCreateType(S->getType(), File), true);
6138   GV->addDebugInfo(Debug);
6139 }
6140 
getCurrentContextDescriptor(const Decl * D)6141 llvm::DIScope *CGDebugInfo::getCurrentContextDescriptor(const Decl *D) {
6142   if (!LexicalBlockStack.empty())
6143     return LexicalBlockStack.back();
6144   llvm::DIScope *Mod = getParentModuleOrNull(D);
6145   return getContextDescriptor(D, Mod ? Mod : TheCU);
6146 }
6147 
EmitUsingDirective(const UsingDirectiveDecl & UD)6148 void 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         getCurrentContextDescriptor(cast<Decl>(UD.getDeclContext())),
6159         getOrCreateNamespace(NSDecl), getOrCreateFile(Loc), getLineNumber(Loc));
6160   }
6161 }
6162 
EmitUsingShadowDecl(const UsingShadowDecl & USD)6163 void CGDebugInfo::EmitUsingShadowDecl(const UsingShadowDecl &USD) {
6164   if (llvm::DINode *Target =
6165           getDeclarationOrDefinition(USD.getUnderlyingDecl())) {
6166     auto Loc = USD.getLocation();
6167     DBuilder.createImportedDeclaration(
6168         getCurrentContextDescriptor(cast<Decl>(USD.getDeclContext())), Target,
6169         getOrCreateFile(Loc), getLineNumber(Loc));
6170   }
6171 }
6172 
EmitUsingDecl(const UsingDecl & UD)6173 void 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>(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);
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 
EmitUsingEnumDecl(const UsingEnumDecl & UD)6198 void 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);
6207 }
6208 
EmitImportDecl(const ImportDecl & ID)6209 void 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         getCurrentContextDescriptor(cast<Decl>(ID.getDeclContext())),
6217         getOrCreateModuleRef(Info, DebugTypeExtRefs), getOrCreateFile(Loc),
6218         getLineNumber(Loc));
6219   }
6220 }
6221 
6222 llvm::DIImportedEntity *
EmitNamespaceAlias(const NamespaceAliasDecl & NA)6223 CGDebugInfo::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>(VH);
6229   llvm::DIImportedEntity *R;
6230   auto Loc = NA.getLocation();
6231   if (const auto *Underlying =
6232           dyn_cast<NamespaceAliasDecl>(NA.getAliasedNamespace()))
6233     // This could cache & dedup here rather than relying on metadata deduping.
6234     R = DBuilder.createImportedDeclaration(
6235         getCurrentContextDescriptor(cast<Decl>(NA.getDeclContext())),
6236         EmitNamespaceAlias(*Underlying), getOrCreateFile(Loc),
6237         getLineNumber(Loc), NA.getName());
6238   else
6239     R = DBuilder.createImportedDeclaration(
6240         getCurrentContextDescriptor(cast<Decl>(NA.getDeclContext())),
6241         getOrCreateNamespace(cast<NamespaceDecl>(NA.getAliasedNamespace())),
6242         getOrCreateFile(Loc), getLineNumber(Loc), NA.getName());
6243   VH.reset(R);
6244   return R;
6245 }
6246 
6247 llvm::DINamespace *
getOrCreateNamespace(const NamespaceDecl * NSDecl)6248 CGDebugInfo::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(NSDecl);
6253   if (I != NamespaceCache.end())
6254     return cast<llvm::DINamespace>(I->second);
6255 
6256   llvm::DIScope *Context = getDeclContextDescriptor(NSDecl);
6257   // Don't trust the context if it is a DIModule (see comment above).
6258   llvm::DINamespace *NS =
6259       DBuilder.createNameSpace(Context, NSDecl->getName(), NSDecl->isInline());
6260   NamespaceCache[NSDecl].reset(NS);
6261   return NS;
6262 }
6263 
setDwoId(uint64_t Signature)6264 void CGDebugInfo::setDwoId(uint64_t Signature) {
6265   assert(TheCU && "no main compile unit");
6266   TheCU->setDWOId(Signature);
6267 }
6268 
finalize()6269 void 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(E.Type, E.Unit)
6276                            : E.Decl;
6277     DBuilder.replaceTemporary(llvm::TempDIType(E.Decl), 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(QTy.getAsOpaquePtr());
6287     assert(It != TypeCache.end());
6288 
6289     llvm::DICompositeType *InterfaceDecl =
6290         cast<llvm::DICompositeType>(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(SubprogramDirect.getPointer());
6299 
6300     llvm::DINodeArray Elements = DBuilder.getOrCreateArray(EltTys);
6301     DBuilder.replaceArrays(InterfaceDecl, Elements);
6302   }
6303 
6304   for (const auto &P : ReplaceMap) {
6305     assert(P.second);
6306     auto *Ty = cast<llvm::DIType>(P.second);
6307     assert(Ty->isForwardDecl());
6308 
6309     auto It = TypeCache.find(P.first);
6310     assert(It != TypeCache.end());
6311     assert(It->second);
6312 
6313     DBuilder.replaceTemporary(llvm::TempDIType(Ty),
6314                               cast<llvm::DIType>(It->second));
6315   }
6316 
6317   for (const auto &P : FwdDeclReplaceMap) {
6318     assert(P.second);
6319     llvm::TempMDNode FwdDecl(cast<llvm::MDNode>(P.second));
6320     llvm::Metadata *Repl;
6321 
6322     auto It = DeclCache.find(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>(Repl))
6332       Repl = GVE->getVariable();
6333     DBuilder.replaceTemporary(std::move(FwdDecl), cast<llvm::MDNode>(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(cast<llvm::DIType>(MD));
6341 
6342   DBuilder.finalize();
6343 }
6344 
6345 // Don't ignore in case of explicit cast where it is referenced indirectly.
EmitExplicitCastType(QualType Ty)6346 void CGDebugInfo::EmitExplicitCastType(QualType Ty) {
6347   if (CGM.getCodeGenOpts().hasReducedDebugInfo())
6348     if (auto *DieTy = getOrCreateType(Ty, TheCU->getFile()))
6349       DBuilder.retainType(DieTy);
6350 }
6351 
EmitAndRetainType(QualType Ty)6352 void CGDebugInfo::EmitAndRetainType(QualType Ty) {
6353   if (CGM.getCodeGenOpts().hasMaybeUnusedDebugInfo())
6354     if (auto *DieTy = getOrCreateType(Ty, TheCU->getFile()))
6355       DBuilder.retainType(DieTy);
6356 }
6357 
SourceLocToDebugLoc(SourceLocation Loc)6358 llvm::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(CGM.getLLVMContext(), getLineNumber(Loc),
6364                                getColumnNumber(Loc), Scope);
6365 }
6366 
getCallSiteRelatedAttrs() const6367 llvm::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 
6389 llvm::DIExpression *
createConstantValueExpression(const clang::ValueDecl * VD,const APValue & Val)6390 CGDebugInfo::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(VD->getType()) > 64)
6394     return nullptr;
6395 
6396   if (Val.isFloat())
6397     return DBuilder.createConstantValueExpression(
6398         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(ValIntOpt.value());
6414 
6415   return nullptr;
6416 }
6417 
LexicalScope(CodeGenFunction & CGF,SourceRange Range)6418 CodeGenFunction::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(CGF.Builder, Range.getBegin());
6424 }
6425 
~LexicalScope()6426 CodeGenFunction::LexicalScope::~LexicalScope() {
6427   if (CGDebugInfo *DI = CGF.getDebugInfo())
6428     DI->EmitLexicalBlockEnd(CGF.Builder, 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 
SanitizerHandlerToCheckLabel(SanitizerHandler Handler)6438 static 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 
6454 static std::string
SanitizerOrdinalToCheckLabel(SanitizerKind::SanitizerOrdinal Ordinal)6455 SanitizerOrdinalToCheckLabel(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 
SanitizerAnnotateDebugInfo(ArrayRef<SanitizerKind::SanitizerOrdinal> Ordinals,SanitizerHandler Handler)6476 llvm::DILocation *CodeGenFunction::SanitizerAnnotateDebugInfo(
6477     ArrayRef<SanitizerKind::SanitizerOrdinal> Ordinals,
6478     SanitizerHandler Handler) {
6479   std::string Label;
6480   if (Ordinals.size() == 1)
6481     Label = SanitizerOrdinalToCheckLabel(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(Ord)) &&
6491         CheckDI) {
6492       return getDebugInfo()->CreateSyntheticInlineAt(CheckDI, Label);
6493     }
6494   }
6495 
6496   return CheckDI;
6497 }
6498 
SanitizerDebugLocation(CodeGenFunction * CGF,ArrayRef<SanitizerKind::SanitizerOrdinal> Ordinals,SanitizerHandler Handler)6499 SanitizerDebugLocation::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 
~SanitizerDebugLocation()6508 SanitizerDebugLocation::~SanitizerDebugLocation() {
6509   assert(CGF->IsSanitizerScope);
6510   CGF->IsSanitizerScope = false;
6511 }
6512