Skip to content

Commit 1a86d92

Browse files
authored
Merge pull request #61417 from hyp/eng/stdlib-guard
[interop][SwiftToCxx] guard inline emitted Stdlib module
2 parents 27c5787 + f8b54e7 commit 1a86d92

File tree

4 files changed

+17
-3
lines changed

4 files changed

+17
-3
lines changed

lib/PrintAsClang/ModuleContentsWriter.cpp

Lines changed: 6 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -773,7 +773,10 @@ EmittedClangHeaderDependencyInfo swift::printModuleContentsAsCxx(
773773

774774
// FIXME: refactor.
775775
if (!prologueOS.str().empty()) {
776-
os << "#endif\n";
776+
// FIXME: This is a workaround for prologue being emitted outside of
777+
// __cplusplus.
778+
if (!M.isStdlibModule())
779+
os << "#endif\n";
777780
os << "#ifdef __cplusplus\n";
778781
os << "namespace ";
779782
M.ValueDecl::getName().print(os);
@@ -784,7 +787,8 @@ EmittedClangHeaderDependencyInfo swift::printModuleContentsAsCxx(
784787

785788
os << prologueOS.str();
786789

787-
os << "\n#ifdef __cplusplus\n";
790+
if (!M.isStdlibModule())
791+
os << "\n#ifdef __cplusplus\n";
788792
os << "}\n";
789793
os << "}\n";
790794
os << "}\n";

lib/PrintAsClang/PrintAsClang.cpp

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -528,7 +528,12 @@ bool swift::printAsClangHeader(raw_ostream &os, ModuleDecl *M,
528528
if (defaultDependencyBehavior && deps.dependsOnStandardLibrary) {
529529
assert(!M->isStdlibModule());
530530
SwiftToClangInteropContext interopContext(*M->getASTContext().getStdlibModule(), irGenOpts);
531+
auto macroGuard =
532+
computeMacroGuard(M->getASTContext().getStdlibModule());
533+
os << "#ifndef " << macroGuard << "\n";
534+
os << "#define " << macroGuard << "\n";
531535
printModuleContentsAsCxx(os, *M->getASTContext().getStdlibModule(), interopContext, /*requiresExposedAttribute=*/true);
536+
os << "#endif // " << macroGuard << "\n";
532537
}
533538

534539
os << moduleContents.str();
Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1 +1 @@
1-
2
1+
3

test/Interop/SwiftToCxx/stdlib/stdlib-dep-inline-in-cxx.swift

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,9 +1,14 @@
11
// RUN: %empty-directory(%t)
22

33
// RUN: %target-swift-frontend -typecheck %s -typecheck -module-name UseOptional -enable-experimental-cxx-interop -emit-clang-header-path %t/stdlib.h
4+
// RUN: %target-swift-frontend -typecheck %s -typecheck -module-name UseOptional2 -enable-experimental-cxx-interop -emit-clang-header-path %t/stdlib2.h
45

56
// RUN: %FileCheck %s < %t/stdlib.h
67

8+
// RUN: cat %t/stdlib.h %t/stdlib2.h > %t/two_includes.h
9+
10+
// RUN: %check-generic-interop-cxx-header-in-clang(%t/two_includes.h -Wno-unused-private-field -Wno-unused-function -Wno-shadow -Wno-documentation)
11+
712
@_expose(Cxx)
813
public func test() -> String {
914
return ""

0 commit comments

Comments
 (0)