Skip to content
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

Enable pragma(LDC_extern_weak) on function declarations. #2984

Merged
merged 2 commits into from
Jan 27, 2019
Merged
Show file tree
Hide file tree
Changes from all commits
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
9 changes: 6 additions & 3 deletions gen/functions.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -551,9 +551,12 @@ void DtoDeclareFunction(FuncDeclaration *fdecl) {
LLFunction *func = vafunc ? vafunc : gIR->module.getFunction(irMangle);
if (!func) {
// All function declarations are "external" - any other linkage type
// is set when actually defining the function.
func = LLFunction::Create(functype, llvm::GlobalValue::ExternalLinkage,
irMangle, &gIR->module);
// is set when actually defining the function, except extern_weak.
auto linkage = llvm::GlobalValue::ExternalLinkage;
// Apply pragma(LDC_extern_weak)
if (fdecl->llvmInternal == LLVMextern_weak)
linkage = llvm::GlobalValue::ExternalWeakLinkage;
func = LLFunction::Create(functype, linkage, irMangle, &gIR->module);
} else if (func->getFunctionType() != functype) {
const auto existingTypeString = llvmTypeToString(func->getFunctionType());
const auto newTypeString = llvmTypeToString(functype);
Expand Down
14 changes: 12 additions & 2 deletions gen/pragma.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -499,7 +499,7 @@ void DtoCheckPragma(PragmaDeclaration *decl, Dsymbol *s,
}

case LLVMextern_weak: {
const int count = applyVariablePragma(s, [=](VarDeclaration *vd) {
int count = applyVariablePragma(s, [=](VarDeclaration *vd) {
if (!vd->isDataseg() || !(vd->storage_class & STCextern)) {
error(s->loc, "`%s` requires storage class `extern`", ident->toChars());
fatal();
Expand All @@ -516,8 +516,18 @@ void DtoCheckPragma(PragmaDeclaration *decl, Dsymbol *s,
}
vd->llvmInternal = llvm_internal;
});
count += applyFunctionPragma(s, [=](FuncDeclaration *fd) {
if (fd->fbody) {
error(s->loc, "`%s` cannot be applied to function definitions", ident->toChars());
fatal();
}
fd->llvmInternal = llvm_internal;
});

if (count == 0) {
error(s->loc, "the `%s` pragma doesn't affect any variable declarations",
error(s->loc,
"the `%s` pragma doesn't affect any variable or function "
"declarations",
ident->toChars());
fatal();
}
Expand Down
21 changes: 21 additions & 0 deletions tests/codegen/pragma_LDC_extern_weak.d
Original file line number Diff line number Diff line change
@@ -0,0 +1,21 @@
// Test pragma(LDC_extern_weak) on function declarations.

// RUN: %ldc -d-version=DECLARATION -c -output-ll -of=%t.ll %s && FileCheck %s < %t.ll --check-prefix=DECLARATION
// RUN: not %ldc -d-version=DEFINITION %s 2>&1 | FileCheck %s --check-prefix=DEFINITION

version(DECLARATION)
{
// DECLARATION: declare{{.*}} extern_weak {{.*}}weakreffunction
pragma(LDC_extern_weak) extern(C) void weakreffunction();
}

version(DEFINITION)
{
// DEFINITION: Error: `LDC_extern_weak` cannot be applied to function definitions
pragma(LDC_extern_weak) extern(C) void weakreffunction() {};
}

void foo()
{
auto a = &weakreffunction;
}