aboutsummaryrefslogtreecommitdiff
path: root/clang/lib/CodeGen/CodeGenModule.cpp
diff options
context:
space:
mode:
authorReid Kleckner <rnk@google.com>2025-04-24 14:40:14 -0700
committerGitHub <noreply@github.com>2025-04-24 14:40:14 -0700
commit0a3f2a05f27097c47d45e16828b0da0dd51fad48 (patch)
treeabca010b8b0a34658bfdf3bd4e3d797ba848c0b4 /clang/lib/CodeGen/CodeGenModule.cpp
parent80182a7d5d66c8dc90bb4623c1f722aba7ebe45b (diff)
downloadllvm-0a3f2a05f27097c47d45e16828b0da0dd51fad48.zip
llvm-0a3f2a05f27097c47d45e16828b0da0dd51fad48.tar.gz
llvm-0a3f2a05f27097c47d45e16828b0da0dd51fad48.tar.bz2
[BPF] Fix issues with external declarations of C++ structor decls (#137079)
Use GetAddrOfGlobal, which is a more general API that takes a GlobalDecl, and handles declaring C++ destructors and other types in a general way. We can use this to generalize over functions and variable declarations. This fixes issues reported on #130674 by @lexi-nadia .
Diffstat (limited to 'clang/lib/CodeGen/CodeGenModule.cpp')
-rw-r--r--clang/lib/CodeGen/CodeGenModule.cpp54
1 files changed, 26 insertions, 28 deletions
diff --git a/clang/lib/CodeGen/CodeGenModule.cpp b/clang/lib/CodeGen/CodeGenModule.cpp
index 83d8d4f..b46eaf3 100644
--- a/clang/lib/CodeGen/CodeGenModule.cpp
+++ b/clang/lib/CodeGen/CodeGenModule.cpp
@@ -5378,11 +5378,33 @@ void CodeGenModule::EmitTentativeDefinition(const VarDecl *D) {
EmitGlobalVarDefinition(D);
}
+// Return a GlobalDecl. Use the base variants for destructors and constructors.
+static GlobalDecl getBaseVariantGlobalDecl(const NamedDecl *D) {
+ if (auto const *CD = dyn_cast<const CXXConstructorDecl>(D))
+ return GlobalDecl(CD, CXXCtorType::Ctor_Base);
+ else if (auto const *DD = dyn_cast<const CXXDestructorDecl>(D))
+ return GlobalDecl(DD, CXXDtorType::Dtor_Base);
+ return GlobalDecl(D);
+}
+
void CodeGenModule::EmitExternalDeclaration(const DeclaratorDecl *D) {
- if (auto const *V = dyn_cast<const VarDecl>(D))
- EmitExternalVarDeclaration(V);
- if (auto const *FD = dyn_cast<const FunctionDecl>(D))
- EmitExternalFunctionDeclaration(FD);
+ CGDebugInfo *DI = getModuleDebugInfo();
+ if (!DI || !getCodeGenOpts().hasReducedDebugInfo())
+ return;
+
+ GlobalDecl GD = getBaseVariantGlobalDecl(D);
+ if (!GD)
+ return;
+
+ llvm::Constant *Addr = GetAddrOfGlobal(GD)->stripPointerCasts();
+ if (const auto *VD = dyn_cast<VarDecl>(D)) {
+ DI->EmitExternalVariable(
+ cast<llvm::GlobalVariable>(Addr->stripPointerCasts()), VD);
+ } else if (const auto *FD = dyn_cast<FunctionDecl>(D)) {
+ llvm::Function *Fn = cast<llvm::Function>(Addr);
+ if (!Fn->getSubprogram())
+ DI->EmitFunctionDecl(GD, FD->getLocation(), FD->getType(), Fn);
+ }
}
CharUnits CodeGenModule::GetTargetTypeStoreSize(llvm::Type *Ty) const {
@@ -5825,30 +5847,6 @@ void CodeGenModule::EmitGlobalVarDefinition(const VarDecl *D,
DI->EmitGlobalVariable(GV, D);
}
-void CodeGenModule::EmitExternalVarDeclaration(const VarDecl *D) {
- if (CGDebugInfo *DI = getModuleDebugInfo())
- if (getCodeGenOpts().hasReducedDebugInfo()) {
- QualType ASTTy = D->getType();
- llvm::Type *Ty = getTypes().ConvertTypeForMem(D->getType());
- llvm::Constant *GV =
- GetOrCreateLLVMGlobal(D->getName(), Ty, ASTTy.getAddressSpace(), D);
- DI->EmitExternalVariable(
- cast<llvm::GlobalVariable>(GV->stripPointerCasts()), D);
- }
-}
-
-void CodeGenModule::EmitExternalFunctionDeclaration(const FunctionDecl *FD) {
- if (CGDebugInfo *DI = getModuleDebugInfo())
- if (getCodeGenOpts().hasReducedDebugInfo()) {
- auto *Ty = getTypes().ConvertType(FD->getType());
- StringRef MangledName = getMangledName(FD);
- auto *Fn = cast<llvm::Function>(
- GetOrCreateLLVMFunction(MangledName, Ty, FD, /* ForVTable */ false));
- if (!Fn->getSubprogram())
- DI->EmitFunctionDecl(FD, FD->getLocation(), FD->getType(), Fn);
- }
-}
-
static bool isVarDeclStrongDefinition(const ASTContext &Context,
CodeGenModule &CGM, const VarDecl *D,
bool NoCommon) {