@@ -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
0 commit comments