Skip to content

Commit c39316f

Browse files
committed
[ClangImporter] Import extern "C" declarations from the bridging header
We do iterate into extern C declarations when building the Swift lookup table during PCH generation. https://github.com/apple/swift/blob/0fad799f51f6c5408696379b1fe35cd10c131136/lib/ClangImporter/SwiftLookupTable.cpp#L2140-L2146 However, we don’t import `extern "C"` declarations while parsing the bridging header (eg. when no `-pch-output-dir` is specified during code completion). This caused us to miss functions annotated as `extern "C"` in code completion. rdar://127512985
1 parent 34c2f92 commit c39316f

File tree

2 files changed

+41
-4
lines changed

2 files changed

+41
-4
lines changed

lib/ClangImporter/ClangImporter.cpp

Lines changed: 17 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1655,11 +1655,24 @@ bool ClangImporter::Implementation::importHeader(
16551655

16561656
// We can't do this as we're parsing because we may want to resolve naming
16571657
// conflicts between the things we've parsed.
1658-
for (auto group : allParsedDecls)
1659-
for (auto *D : group)
1660-
if (auto named = dyn_cast<clang::NamedDecl>(D))
1661-
addEntryToLookupTable(*BridgingHeaderLookupTable, named,
1658+
1659+
std::function<void(clang::Decl *)> visit = [&](clang::Decl *decl) {
1660+
// Iterate into extern "C" {} type declarations.
1661+
if (auto linkageDecl = dyn_cast<clang::LinkageSpecDecl>(decl)) {
1662+
for (auto *decl : linkageDecl->noload_decls()) {
1663+
visit(decl);
1664+
}
1665+
}
1666+
if (auto named = dyn_cast<clang::NamedDecl>(decl)) {
1667+
addEntryToLookupTable(*BridgingHeaderLookupTable, named,
16621668
getNameImporter());
1669+
}
1670+
};
1671+
for (auto group : allParsedDecls) {
1672+
for (auto *D : group) {
1673+
visit(D);
1674+
}
1675+
}
16631676

16641677
pp.EndSourceFile();
16651678
bumpGeneration();
Lines changed: 24 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,24 @@
1+
// RUN: %empty-directory(%t)
2+
// RUN: split-file --leading-lines %s %t
3+
4+
//--- Bridging-Header.h
5+
6+
void func_from_bridging_header(void);
7+
8+
extern "C"
9+
void extern_c_func_from_bridging_header(void);
10+
11+
extern "C" {
12+
extern "C" {
13+
void nested_extern_c_func_from_bridging_header(void);
14+
}
15+
}
16+
17+
//--- test.swift
18+
19+
// Passes
20+
// RUN: %sourcekitd-test -req=complete -pos 1:1 %t/test.swift -- %t/test.swift -import-bridging-header %t/Bridging-Header.h -cxx-interoperability-mode=default -pch-output-dir %t/pch-dir | %FileCheck %s
21+
// RUN: %sourcekitd-test -req=complete -pos 1:1 %t/test.swift -- %t/test.swift -import-bridging-header %t/Bridging-Header.h -cxx-interoperability-mode=default | %FileCheck %s
22+
// CHECK-DAG: func_from_bridging_header
23+
// CHECK-DAG: extern_c_func_from_bridging_header
24+
// CHECK-DAG: nested_extern_c_func_from_bridging_header

0 commit comments

Comments
 (0)