Skip to content

[BPF] Fix issues with external declarations of C++ structor decls #137079

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 3 commits into from
Apr 24, 2025
Merged
Show file tree
Hide file tree
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Next Next commit
[BPF] Fix issues with external declarations of C++ structor decls
Use GetAddrOfGlobal, which is a more general API that takes a
GlobalDecl, and handles declaring C++ destructors and other types in a
general way. It's possible we can generalize this to handle external
variable declarations.

This fixes issues reported on #130674 by @lexi-nadia .
  • Loading branch information
rnk committed Apr 23, 2025
commit e78b8012376798746b5cf4d664e656e22591acdb
16 changes: 11 additions & 5 deletions clang/lib/CodeGen/CodeGenModule.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -5837,15 +5837,21 @@ void CodeGenModule::EmitExternalVarDeclaration(const VarDecl *D) {
}
}

static GlobalDecl getBaseVariantGlobalDecl(const FunctionDecl *FD) {
if (auto const *CD = dyn_cast<const CXXConstructorDecl>(FD))
return GlobalDecl(CD, CXXCtorType::Ctor_Base);
else if (auto const *DD = dyn_cast<const CXXDestructorDecl>(FD))
return GlobalDecl(DD, CXXDtorType::Dtor_Base);
return GlobalDecl(FD);
}

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));
GlobalDecl GD = getBaseVariantGlobalDecl(FD);
auto *Fn = cast<llvm::Function>(GetAddrOfGlobal(GD));
if (!Fn->getSubprogram())
DI->EmitFunctionDecl(FD, FD->getLocation(), FD->getType(), Fn);
DI->EmitFunctionDecl(GD, FD->getLocation(), FD->getType(), Fn);
}
}

Expand Down
14 changes: 14 additions & 0 deletions clang/test/CodeGenCXX/bpf-debug-structors.cpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,14 @@
// RUN: %clang_cc1 -debug-info-kind=constructor -triple bpf -emit-llvm %s -o - | FileCheck %s

class Foo {
public:
virtual ~Foo() noexcept;
};

class Bar : public Foo {
public:
Bar() noexcept {}
~Bar() noexcept override;
};

// CHECK: declare !dbg !{{[0-9]+}} void @_ZN3FooD2Ev(ptr noundef nonnull align 8 dereferenceable(8))