Skip to content

Commit ee1f2fa

Browse files
authored
Merge pull request #61605 from hyp/eng/swiftToCxxHeaderOnlyWhneNeeded
[interop][SwiftToCxx] include the swiftToCxx shim header only when ac…
2 parents d3f6314 + de1e67d commit ee1f2fa

File tree

3 files changed

+47
-37
lines changed

3 files changed

+47
-37
lines changed

lib/PrintAsClang/PrintAsClang.cpp

Lines changed: 35 additions & 26 deletions
Original file line numberDiff line numberDiff line change
@@ -108,8 +108,6 @@ static void writePrologue(raw_ostream &out, ASTContext &ctx,
108108
out << "#include <stdlib.h>\n";
109109
out << "#include <new>\n";
110110
out << "#include <type_traits>\n";
111-
ClangSyntaxPrinter(out).printIncludeForShimHeader(
112-
"_SwiftCxxInteroperability.h");
113111
},
114112
[&] {
115113
out << "#include <stdint.h>\n"
@@ -511,33 +509,44 @@ bool swift::printAsClangHeader(raw_ostream &os, ModuleDecl *M,
511509
bool enableCxx = frontendOpts.ClangHeaderExposedDecls.hasValue() ||
512510
frontendOpts.EnableExperimentalCxxInteropInClangHeader ||
513511
M->DeclContext::getASTContext().LangOpts.EnableCXXInterop;
514-
if (enableCxx) {
515-
bool requiresExplicitExpose = !frontendOpts.ClangHeaderExposedDecls.hasValue() ||
516-
*frontendOpts.ClangHeaderExposedDecls == FrontendOptions::ClangHeaderExposeBehavior::HasExposeAttr;
517-
// Default dependency behavior is used when the -clang-header-expose-decls flag is not specified.
518-
bool defaultDependencyBehavior = !frontendOpts.ClangHeaderExposedDecls.hasValue();
519-
520-
std::string moduleContentsBuf;
521-
llvm::raw_string_ostream moduleContents{moduleContentsBuf};
522-
auto deps = printModuleContentsAsCxx(moduleContents, *M, interopContext,
523-
/*requiresExposedAttribute=*/requiresExplicitExpose);
524-
// FIXME: In ObjC++ mode, we do not need to reimport duplicate modules.
525-
writeImports(os, deps.imports, *M, bridgingHeader, /*useCxxImport=*/true);
526-
527-
// Embed the standard library directly.
528-
if (defaultDependencyBehavior && deps.dependsOnStandardLibrary) {
529-
assert(!M->isStdlibModule());
530-
SwiftToClangInteropContext interopContext(*M->getASTContext().getStdlibModule(), irGenOpts);
531-
auto macroGuard =
532-
computeMacroGuard(M->getASTContext().getStdlibModule());
533-
os << "#ifndef " << macroGuard << "\n";
534-
os << "#define " << macroGuard << "\n";
535-
printModuleContentsAsCxx(os, *M->getASTContext().getStdlibModule(), interopContext, /*requiresExposedAttribute=*/true);
536-
os << "#endif // " << macroGuard << "\n";
512+
if (!enableCxx)
513+
return;
514+
// Include the shim header only in the C++ mode.
515+
ClangSyntaxPrinter(os).printIncludeForShimHeader(
516+
"_SwiftCxxInteroperability.h");
517+
518+
bool requiresExplicitExpose =
519+
!frontendOpts.ClangHeaderExposedDecls.hasValue() ||
520+
*frontendOpts.ClangHeaderExposedDecls ==
521+
FrontendOptions::ClangHeaderExposeBehavior::HasExposeAttr;
522+
// Default dependency behavior is used when the -clang-header-expose-decls
523+
// flag is not specified.
524+
bool defaultDependencyBehavior =
525+
!frontendOpts.ClangHeaderExposedDecls.hasValue();
526+
527+
std::string moduleContentsBuf;
528+
llvm::raw_string_ostream moduleContents{moduleContentsBuf};
529+
auto deps = printModuleContentsAsCxx(
530+
moduleContents, *M, interopContext,
531+
/*requiresExposedAttribute=*/requiresExplicitExpose);
532+
// FIXME: In ObjC++ mode, we do not need to reimport duplicate modules.
533+
writeImports(os, deps.imports, *M, bridgingHeader, /*useCxxImport=*/true);
534+
535+
// Embed the standard library directly.
536+
if (defaultDependencyBehavior && deps.dependsOnStandardLibrary) {
537+
assert(!M->isStdlibModule());
538+
SwiftToClangInteropContext interopContext(
539+
*M->getASTContext().getStdlibModule(), irGenOpts);
540+
auto macroGuard = computeMacroGuard(M->getASTContext().getStdlibModule());
541+
os << "#ifndef " << macroGuard << "\n";
542+
os << "#define " << macroGuard << "\n";
543+
printModuleContentsAsCxx(os, *M->getASTContext().getStdlibModule(),
544+
interopContext,
545+
/*requiresExposedAttribute=*/true);
546+
os << "#endif // " << macroGuard << "\n";
537547
}
538548

539549
os << moduleContents.str();
540-
}
541550
});
542551
writeEpilogue(os);
543552

test/PrintAsCxx/empty.swift

Lines changed: 11 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -32,17 +32,6 @@
3232
// CHECK-NEXT: #include <stdlib.h>
3333
// CHECK-NEXT: #include <new>
3434
// CHECK-NEXT: #include <type_traits>
35-
// CHECK-NEXT: // Look for the C++ interop support header relative to clang's resource dir:
36-
// CHECK-NEXT: // '<toolchain>/usr/lib/clang/<version>/include/../../../swift/swiftToCxx'.
37-
// CHECK-NEXT: #if __has_include(<../../../swift/swiftToCxx/_SwiftCxxInteroperability.h>)
38-
// CHECK-NEXT: #include <../../../swift/swiftToCxx/_SwiftCxxInteroperability.h>
39-
// CHECK-NEXT: #elif __has_include(<../../../../../lib/swift/swiftToCxx/_SwiftCxxInteroperability.h>)
40-
// CHECK-NEXT: // '<toolchain>/usr/local/lib/clang/<version>/include/../../../../../lib/swift/swiftToCxx'.
41-
// CHECK-NEXT: #include <../../../../../lib/swift/swiftToCxx/_SwiftCxxInteroperability.h>
42-
// CHECK-NEXT: // Alternatively, allow user to find the header using additional include path into '<toolchain>/lib/swift'.
43-
// CHECK-NEXT: #elif __has_include(<swiftToCxx/_SwiftCxxInteroperability.h>)
44-
// CHECK-NEXT: #include <swiftToCxx/_SwiftCxxInteroperability.h>
45-
// CHECK-NEXT: #endif
4635
// CHECK-NEXT: #else
4736
// CHECK-NEXT: #include <stdint.h>
4837
// CHECK-NEXT: #include <stddef.h>
@@ -98,6 +87,17 @@
9887
// CHECK-LABEL: #if defined(__OBJC__)
9988
// CHECK-NEXT: #endif
10089
// CHECK-NEXT: #if defined(__cplusplus)
90+
// CHECK-NEXT: // Look for the C++ interop support header relative to clang's resource dir:
91+
// CHECK-NEXT: // '<toolchain>/usr/lib/clang/<version>/include/../../../swift/swiftToCxx'.
92+
// CHECK-NEXT: #if __has_include(<../../../swift/swiftToCxx/_SwiftCxxInteroperability.h>)
93+
// CHECK-NEXT: #include <../../../swift/swiftToCxx/_SwiftCxxInteroperability.h>
94+
// CHECK-NEXT: #elif __has_include(<../../../../../lib/swift/swiftToCxx/_SwiftCxxInteroperability.h>)
95+
// CHECK-NEXT: // '<toolchain>/usr/local/lib/clang/<version>/include/../../../../../lib/swift/swiftToCxx'.
96+
// CHECK-NEXT: #include <../../../../../lib/swift/swiftToCxx/_SwiftCxxInteroperability.h>
97+
// CHECK-NEXT: // Alternatively, allow user to find the header using additional include path into '<toolchain>/lib/swift'.
98+
// CHECK-NEXT: #elif __has_include(<swiftToCxx/_SwiftCxxInteroperability.h>)
99+
// CHECK-NEXT: #include <swiftToCxx/_SwiftCxxInteroperability.h>
100+
// CHECK-NEXT: #endif
101101
// CHECK-NEXT: #if __has_feature(objc_modules)
102102
// CHECK: #ifndef SWIFT_PRINTED_CORE
103103
// CHECK: } // namespace swift

test/PrintAsObjC/empty.swift

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,7 @@
44

55
// RUN: %check-in-clang -std=c99 %t/empty.h
66
// RUN: %check-in-clang -std=c11 %t/empty.h
7+
// RUN: %check-cxx-header-in-clang -x objective-c++-header -std=c++98 -D_LIBCPP_CSTDLIB %t/empty.h
78
// RUN: %check-cxx-header-in-clang -x objective-c++-header -std=c++11 -D_LIBCPP_CSTDLIB %t/empty.h
89
// RUN: %check-cxx-header-in-clang -x objective-c++-header -std=c++14 -D_LIBCPP_CSTDLIB %t/empty.h
910

0 commit comments

Comments
 (0)