From f923702511528cd728e09d1b9191e6e25404a035 Mon Sep 17 00:00:00 2001 From: Arnold Schwaighofer Date: Tue, 1 Sep 2020 11:42:32 -0700 Subject: [PATCH] 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 --- lib/IRGen/GenClangDecl.cpp | 6 ++++++ test/IRGen/Inputs/c_functions.h | 5 +++++ test/IRGen/c_functions.swift | 5 +++++ 3 files changed, 16 insertions(+) diff --git a/lib/IRGen/GenClangDecl.cpp b/lib/IRGen/GenClangDecl.cpp index 577307ce502d9..4ba5ac641647e 100644 --- a/lib/IRGen/GenClangDecl.cpp +++ b/lib/IRGen/GenClangDecl.cpp @@ -76,6 +76,12 @@ void IRGenModule::emitClangDecl(const clang::Decl *decl) { ClangDeclRefFinder refFinder([&](const clang::DeclRefExpr *DRE) { const clang::Decl *D = DRE->getDecl(); + + // Ignore local declarations. + if (auto varDecl = dyn_cast(D)) + if (!varDecl->isFileVarDecl() && !varDecl->isStaticDataMember()) + return; + // 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 diff --git a/test/IRGen/Inputs/c_functions.h b/test/IRGen/Inputs/c_functions.h index b613f89e3b723..59493d8e0b0ad 100644 --- a/test/IRGen/Inputs/c_functions.h +++ b/test/IRGen/Inputs/c_functions.h @@ -24,3 +24,8 @@ static inline void log_a_thing(const a_thing thing) { static inline unsigned int return7(void) { return 7; } + +static inline int getExternGlobal() { + extern int global; + return global; +} diff --git a/test/IRGen/c_functions.swift b/test/IRGen/c_functions.swift index 3d42cfc1c3657..36e0f6507ebb5 100644 --- a/test/IRGen/c_functions.swift +++ b/test/IRGen/c_functions.swift @@ -35,3 +35,8 @@ func test_indirect_by_val_alignment() { // i386: define hidden swiftcc void @"$s11c_functions30test_indirect_by_val_alignmentyyF"() // s390x: define hidden swiftcc void @"$s11c_functions30test_indirect_by_val_alignmentyyF"() // powerpc64le: define hidden swiftcc void @"$s11c_functions30test_indirect_by_val_alignmentyyF"() + + +func dontAssertOnExternLocal() { + let x = getExternGlobal() +}