Skip to content

Commit 635581a

Browse files
authored
Merge pull request #6913 from aschwaighofer/fix_irgen_static_local_vars-3.1
[3.1] IRGen: Don't try to emit non-global variables of imported inline c functions
2 parents 4f9f0e8 + 61bcee3 commit 635581a

File tree

3 files changed

+21
-3
lines changed

3 files changed

+21
-3
lines changed

lib/IRGen/GenClangDecl.cpp

Lines changed: 12 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -50,9 +50,18 @@ void IRGenModule::emitClangDecl(const clang::Decl *decl) {
5050
stack.push_back(decl);
5151

5252
ClangDeclRefFinder refFinder([&](const clang::DeclRefExpr *DRE) {
53-
const clang::ValueDecl *D = DRE->getDecl();
54-
if (!D->hasLinkage() || D->isExternallyVisible())
55-
return;
53+
const clang::Decl *D = DRE->getDecl();
54+
// Check that this is a file-level declaration and not inside a function.
55+
// If it's a member of a file-level decl, like a C++ static member variable,
56+
// we want to add the entire file-level declaration because Clang doesn't
57+
// expect to see members directly here.
58+
for (auto *DC = D->getDeclContext();; DC = DC->getParent()) {
59+
if (DC->isFunctionOrMethod())
60+
return;
61+
if (DC->isFileContext())
62+
break;
63+
D = cast<const clang::Decl>(DC);
64+
}
5665
if (!GlobalClangDecls.insert(D->getCanonicalDecl()).second)
5766
return;
5867
stack.push_back(D);

test/IRGen/Inputs/c_functions.h

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2,3 +2,10 @@
22

33
void overloaded(void) __attribute__((overloadable));
44
void overloaded(int) __attribute__((overloadable));
5+
6+
extern void use(const char *);
7+
8+
static inline void test_my_log() {
9+
__attribute__((internal_linkage)) static const char fmt[] = "foobar";
10+
use(fmt);
11+
}

test/IRGen/c_functions.swift

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -9,4 +9,6 @@ func testOverloaded() {
99
overloaded()
1010
// CHECK: call void @_Z10overloadedi(i32{{( signext)?}} 42)
1111
overloaded(42)
12+
// CHECK: call void @{{.*}}test_my_log
13+
test_my_log()
1214
} // CHECK: {{^}$}}

0 commit comments

Comments
 (0)