Skip to content

[3.1] IRGen: Don't try to emit non-global variables of imported inline c functions #6913

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
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
15 changes: 12 additions & 3 deletions lib/IRGen/GenClangDecl.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -50,9 +50,18 @@ void IRGenModule::emitClangDecl(const clang::Decl *decl) {
stack.push_back(decl);

ClangDeclRefFinder refFinder([&](const clang::DeclRefExpr *DRE) {
const clang::ValueDecl *D = DRE->getDecl();
if (!D->hasLinkage() || D->isExternallyVisible())
return;
const clang::Decl *D = DRE->getDecl();
// Check that this is a file-level declaration and not inside a function.
// If it's a member of a file-level decl, like a C++ static member variable,
// we want to add the entire file-level declaration because Clang doesn't
// expect to see members directly here.
for (auto *DC = D->getDeclContext();; DC = DC->getParent()) {
if (DC->isFunctionOrMethod())
return;
if (DC->isFileContext())
break;
D = cast<const clang::Decl>(DC);
}
if (!GlobalClangDecls.insert(D->getCanonicalDecl()).second)
return;
stack.push_back(D);
Expand Down
7 changes: 7 additions & 0 deletions test/IRGen/Inputs/c_functions.h
Original file line number Diff line number Diff line change
Expand Up @@ -2,3 +2,10 @@

void overloaded(void) __attribute__((overloadable));
void overloaded(int) __attribute__((overloadable));

extern void use(const char *);

static inline void test_my_log() {
__attribute__((internal_linkage)) static const char fmt[] = "foobar";
use(fmt);
}
2 changes: 2 additions & 0 deletions test/IRGen/c_functions.swift
Original file line number Diff line number Diff line change
Expand Up @@ -9,4 +9,6 @@ func testOverloaded() {
overloaded()
// CHECK: call void @_Z10overloadedi(i32{{( signext)?}} 42)
overloaded(42)
// CHECK: call void @{{.*}}test_my_log
test_my_log()
} // CHECK: {{^}$}}