[interop][SwiftToCxx] guard inline emitted Stdlib module

This commit is contained in:
Alex Lorenz
2022-10-03 11:30:37 -07:00
parent 6c518bd311
commit f8b54e7ea7
4 changed files with 17 additions and 3 deletions

View File

@@ -773,6 +773,9 @@ EmittedClangHeaderDependencyInfo swift::printModuleContentsAsCxx(
// FIXME: refactor. // FIXME: refactor.
if (!prologueOS.str().empty()) { if (!prologueOS.str().empty()) {
// FIXME: This is a workaround for prologue being emitted outside of
// __cplusplus.
if (!M.isStdlibModule())
os << "#endif\n"; os << "#endif\n";
os << "#ifdef __cplusplus\n"; os << "#ifdef __cplusplus\n";
os << "namespace "; os << "namespace ";
@@ -784,6 +787,7 @@ EmittedClangHeaderDependencyInfo swift::printModuleContentsAsCxx(
os << prologueOS.str(); os << prologueOS.str();
if (!M.isStdlibModule())
os << "\n#ifdef __cplusplus\n"; os << "\n#ifdef __cplusplus\n";
os << "}\n"; os << "}\n";
os << "}\n"; os << "}\n";

View File

@@ -528,7 +528,12 @@ bool swift::printAsClangHeader(raw_ostream &os, ModuleDecl *M,
if (defaultDependencyBehavior && deps.dependsOnStandardLibrary) { if (defaultDependencyBehavior && deps.dependsOnStandardLibrary) {
assert(!M->isStdlibModule()); assert(!M->isStdlibModule());
SwiftToClangInteropContext interopContext(*M->getASTContext().getStdlibModule(), irGenOpts); SwiftToClangInteropContext interopContext(*M->getASTContext().getStdlibModule(), irGenOpts);
auto macroGuard =
computeMacroGuard(M->getASTContext().getStdlibModule());
os << "#ifndef " << macroGuard << "\n";
os << "#define " << macroGuard << "\n";
printModuleContentsAsCxx(os, *M->getASTContext().getStdlibModule(), interopContext, /*requiresExposedAttribute=*/true); printModuleContentsAsCxx(os, *M->getASTContext().getStdlibModule(), interopContext, /*requiresExposedAttribute=*/true);
os << "#endif // " << macroGuard << "\n";
} }
os << moduleContents.str(); os << moduleContents.str();

View File

@@ -1,9 +1,14 @@
// RUN: %empty-directory(%t) // RUN: %empty-directory(%t)
// RUN: %target-swift-frontend -typecheck %s -typecheck -module-name UseOptional -enable-experimental-cxx-interop -emit-clang-header-path %t/stdlib.h // RUN: %target-swift-frontend -typecheck %s -typecheck -module-name UseOptional -enable-experimental-cxx-interop -emit-clang-header-path %t/stdlib.h
// RUN: %target-swift-frontend -typecheck %s -typecheck -module-name UseOptional2 -enable-experimental-cxx-interop -emit-clang-header-path %t/stdlib2.h
// RUN: %FileCheck %s < %t/stdlib.h // RUN: %FileCheck %s < %t/stdlib.h
// RUN: cat %t/stdlib.h %t/stdlib2.h > %t/two_includes.h
// RUN: %check-generic-interop-cxx-header-in-clang(%t/two_includes.h -Wno-unused-private-field -Wno-unused-function -Wno-shadow -Wno-documentation)
@_expose(Cxx) @_expose(Cxx)
public func test() -> String { public func test() -> String {
return "" return ""