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