Skip to content

Commit f923702

Browse files
committed
IRGen: Ignore local variable declaration in inline c functions
The existing code that walks back the declcontext parents did not work for local declarations: ``` static inline void func() { extern int global; ... } ``` The global's declaration context is the file context. We would end up in code generation for the global decl and assert that it is not a `isFileVarDecl()`. rdar://67951491
1 parent 413eba8 commit f923702

File tree

3 files changed

+16
-0
lines changed

3 files changed

+16
-0
lines changed

lib/IRGen/GenClangDecl.cpp

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -76,6 +76,12 @@ void IRGenModule::emitClangDecl(const clang::Decl *decl) {
7676

7777
ClangDeclRefFinder refFinder([&](const clang::DeclRefExpr *DRE) {
7878
const clang::Decl *D = DRE->getDecl();
79+
80+
// Ignore local declarations.
81+
if (auto varDecl = dyn_cast<clang::VarDecl>(D))
82+
if (!varDecl->isFileVarDecl() && !varDecl->isStaticDataMember())
83+
return;
84+
7985
// Check that this is a file-level declaration and not inside a function.
8086
// If it's a member of a file-level decl, like a C++ static member variable,
8187
// we want to add the entire file-level declaration because Clang doesn't

test/IRGen/Inputs/c_functions.h

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -24,3 +24,8 @@ static inline void log_a_thing(const a_thing thing) {
2424
static inline unsigned int return7(void) {
2525
return 7;
2626
}
27+
28+
static inline int getExternGlobal() {
29+
extern int global;
30+
return global;
31+
}

test/IRGen/c_functions.swift

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -35,3 +35,8 @@ func test_indirect_by_val_alignment() {
3535
// i386: define hidden swiftcc void @"$s11c_functions30test_indirect_by_val_alignmentyyF"()
3636
// s390x: define hidden swiftcc void @"$s11c_functions30test_indirect_by_val_alignmentyyF"()
3737
// powerpc64le: define hidden swiftcc void @"$s11c_functions30test_indirect_by_val_alignmentyyF"()
38+
39+
40+
func dontAssertOnExternLocal() {
41+
let x = getExternGlobal()
42+
}

0 commit comments

Comments
 (0)