From 0a3f2a05f27097c47d45e16828b0da0dd51fad48 Mon Sep 17 00:00:00 2001 From: Reid Kleckner Date: Thu, 24 Apr 2025 14:40:14 -0700 Subject: [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 . --- clang/lib/CodeGen/CodeGenModule.cpp | 54 ++++++++++++++++++------------------- 1 file changed, 26 insertions(+), 28 deletions(-) (limited to 'clang/lib/CodeGen/CodeGenModule.cpp') 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(D)) + return GlobalDecl(CD, CXXCtorType::Ctor_Base); + else if (auto const *DD = dyn_cast(D)) + return GlobalDecl(DD, CXXDtorType::Dtor_Base); + return GlobalDecl(D); +} + void CodeGenModule::EmitExternalDeclaration(const DeclaratorDecl *D) { - if (auto const *V = dyn_cast(D)) - EmitExternalVarDeclaration(V); - if (auto const *FD = dyn_cast(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(D)) { + DI->EmitExternalVariable( + cast(Addr->stripPointerCasts()), VD); + } else if (const auto *FD = dyn_cast(D)) { + llvm::Function *Fn = cast(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(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( - 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) { -- cgit v1.1