Skip to content

Commit 90a5a0f

Browse files
authored
Merge pull request #84410 from DougGregor/internal-import-bridging-header
2 parents 6557efc + 87cbe5d commit 90a5a0f

31 files changed

+425
-64
lines changed

include/swift/AST/DiagnosticsFrontend.def

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -609,6 +609,9 @@ ERROR(no_swift_sources_with_embedded,none,
609609
ERROR(package_cmo_requires_library_evolution, none,
610610
"Library evolution must be enabled for Package CMO", ())
611611

612+
WARNING(internal_bridging_header_without_library_evolution,none,
613+
"using internal bridging headers without library evolution can cause instability", ())
614+
612615
ERROR(experimental_not_supported_in_production,none,
613616
"experimental feature '%0' cannot be enabled in production compiler",
614617
(StringRef))
@@ -636,6 +639,8 @@ NOTE(dependency_scan_unexpected_variant_module_map_note, none,
636639
NOTE(dependency_scan_unexpected_variant_extra_arg_note, none,
637640
"%select{first|second}0 module command-line has extra argument: '%1'", (bool, StringRef))
638641

642+
ERROR(bridging_header_and_pch_internal_mismatch,none,
643+
"bridging header and precompiled header options mismatch on internal vs. public import", ())
639644

640645
#define UNDEFINE_DIAGNOSTIC_MACROS
641646
#include "DefineDiagnosticMacros.h"

include/swift/AST/DiagnosticsSema.def

Lines changed: 9 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -2702,13 +2702,13 @@ NOTE(module_imported_here,none,
27022702
NOTE(decl_import_via_here,none,
27032703
"%kind0 imported as "
27042704
"'%select{private|fileprivate|internal|package|%ERROR|%ERROR}1' "
2705-
"from %2 here",
2706-
(const Decl *, AccessLevel, const ModuleDecl*))
2705+
"from %select{%2 here|bridging header}3",
2706+
(const Decl *, AccessLevel, const ModuleDecl*, bool))
27072707
NOTE(decl_import_via_local,none,
27082708
"%kind0 is imported by this file as "
27092709
"'%select{private|fileprivate|internal|package|%ERROR|%ERROR}1' "
2710-
"from %2",
2711-
(const Decl *, AccessLevel, const ModuleDecl*))
2710+
"from %select{%2|bridging header}3",
2711+
(const Decl *, AccessLevel, const ModuleDecl*, bool))
27122712

27132713
// Opaque return types
27142714
ERROR(opaque_type_invalid_constraint,none,
@@ -3834,6 +3834,7 @@ ERROR(decl_from_hidden_module,none,
38343834
"%2 was imported for SPI only|"
38353835
"%2 was not imported by this file|"
38363836
"C++ types from imported module %2 do not support library evolution|"
3837+
"it was imported via the internal bridging header|"
38373838
"%2 was not imported publicly}3",
38383839
(const Decl *, unsigned, Identifier, unsigned))
38393840
ERROR(typealias_desugars_to_type_from_hidden_module,none,
@@ -3850,6 +3851,7 @@ ERROR(typealias_desugars_to_type_from_hidden_module,none,
38503851
"%4 was imported for SPI only|"
38513852
"%4 was not imported by this file|"
38523853
"C++ types from imported module %4 do not support library evolution|"
3854+
"it was imported via the internal bridging header|"
38533855
"%4 was not imported publicly}5",
38543856
(const TypeAliasDecl *, StringRef, StringRef, unsigned, Identifier, unsigned))
38553857
ERROR(conformance_from_implementation_only_module,none,
@@ -3864,6 +3866,7 @@ ERROR(conformance_from_implementation_only_module,none,
38643866
"%3 was imported for SPI only|"
38653867
"%3 was not imported by this file|"
38663868
"C++ types from imported module %3 do not support library evolution|"
3869+
"it was imported via the internal bridging header|"
38673870
"%3 was not imported publicly}4",
38683871
(Type, Identifier, unsigned, Identifier, unsigned))
38693872
NOTE(assoc_conformance_from_implementation_only_module,none,
@@ -7317,6 +7320,7 @@ ERROR(inlinable_decl_ref_from_hidden_module,
73177320
"%2 was imported for SPI only|"
73187321
"%2 was not imported by this file|"
73197322
"C++ APIs from imported module %2 do not support library evolution|"
7323+
"it was imported via the internal bridging header|"
73207324
"%2 was not imported publicly}3",
73217325
(const ValueDecl *, unsigned, Identifier, unsigned))
73227326

@@ -7328,6 +7332,7 @@ ERROR(inlinable_typealias_desugars_to_type_from_hidden_module,
73287332
"%4 was imported for SPI only|"
73297333
"%4 was not imported by this file|"
73307334
"C++ types from imported module %4 do not support library evolution|"
7335+
"it was imported via the internal bridging header|"
73317336
"%4 was not imported publicly}5",
73327337
(const TypeAliasDecl *, StringRef, StringRef, unsigned, Identifier, unsigned))
73337338

include/swift/Basic/LangOptions.h

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1054,6 +1054,10 @@ namespace swift {
10541054
/// The bridging header PCH file.
10551055
std::string BridgingHeaderPCH;
10561056

1057+
/// Whether the bridging header and PCH file are considered to be
1058+
/// internal imports.
1059+
bool BridgingHeaderIsInternal = false;
1060+
10571061
/// When automatically generating a precompiled header from the bridging
10581062
/// header, place it in this directory.
10591063
std::string PrecompiledHeaderOutputDir;

include/swift/Frontend/FrontendOptions.h

Lines changed: 6 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -66,12 +66,16 @@ class FrontendOptions {
6666

6767
bool isOutputFileDirectory() const;
6868

69-
/// An Objective-C header to import and make implicitly visible.
69+
/// A C header to import and make implicitly visible.
7070
std::string ImplicitObjCHeaderPath;
7171

72-
/// An Objective-C pch to import and make implicitly visible.
72+
/// A C pch to import and make implicitly visible.
7373
std::string ImplicitObjCPCHPath;
7474

75+
/// Whether the imported C header or precompiled header is considered
76+
/// an internal import (vs. the default, a public import).
77+
bool ImportHeaderAsInternal = false;
78+
7579
/// The map of aliases and real names of imported or referenced modules.
7680
llvm::StringMap<std::string> ModuleAliasMap;
7781

include/swift/Option/Options.td

Lines changed: 13 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -347,16 +347,25 @@ def import_underlying_module : Flag<["-"], "import-underlying-module">,
347347
Flags<[FrontendOption, NoInteractiveOption]>,
348348
HelpText<"Implicitly imports the Objective-C half of a module">;
349349

350-
def import_objc_header : Separate<["-"], "import-objc-header">,
351-
Flags<[FrontendOption, HelpHidden, ArgumentIsPath]>,
352-
HelpText<"Implicitly imports an Objective-C header file">;
353350
def import_bridging_header : Separate<["-"], "import-bridging-header">,
351+
Flags<[FrontendOption, ArgumentIsPath]>,
352+
HelpText<"Implicitly imports a C header file">;
353+
def import_objc_header : Separate<["-"], "import-objc-header">,
354354
Flags<[FrontendOption, HelpHidden, ArgumentIsPath]>,
355-
Alias<import_objc_header>;
355+
Alias<import_bridging_header>;
356+
357+
def internal_import_bridging_header : Separate<["-"], "internal-import-bridging-header">,
358+
Flags<[FrontendOption, ArgumentIsPath]>,
359+
HelpText<"Implicitly imports a C header file as an internal import">;
360+
356361
def import_pch : Separate<["-"], "import-pch">,
357362
Flags<[FrontendOption, HelpHidden, ArgumentIsPath]>,
358363
HelpText<"Import bridging header PCH file">;
359364

365+
def internal_import_pch : Separate<["-"], "internal-import-pch">,
366+
Flags<[FrontendOption, HelpHidden, ArgumentIsPath]>,
367+
HelpText<"Import bridging header PCH file as internal">;
368+
360369
def pch_output_dir: Separate<["-"], "pch-output-dir">,
361370
Flags<[FrontendOption, HelpHidden, ArgumentIsPath]>,
362371
HelpText<"Directory to persist automatically created precompiled bridging headers">;

lib/AST/Module.cpp

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2996,7 +2996,9 @@ SourceFile::getImportAccessLevel(const ModuleDecl *targetModule) const {
29962996
// they are recommended over indirect imports.
29972997
if ((!restrictiveImport.has_value() ||
29982998
restrictiveImport->accessLevel < AccessLevel::Public) &&
2999-
imports.isImportedBy(targetModule, getParentModule()))
2999+
!(restrictiveImport &&
3000+
restrictiveImport->module.importedModule->isClangHeaderImportModule()) &&
3001+
imports.isImportedBy(targetModule, getParentModule()))
30003002
return std::nullopt;
30013003

30023004
return restrictiveImport;

lib/ClangImporter/ClangImporter.cpp

Lines changed: 7 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2714,6 +2714,7 @@ ClangImporter::Implementation::Implementation(
27142714
DisableSwiftBridgeAttr(ctx.ClangImporterOpts.DisableSwiftBridgeAttr),
27152715
BridgingHeaderExplicitlyRequested(
27162716
!ctx.ClangImporterOpts.BridgingHeader.empty()),
2717+
BridgingHeaderIsInternal(ctx.ClangImporterOpts.BridgingHeaderIsInternal),
27172718
DisableOverlayModules(ctx.ClangImporterOpts.DisableOverlayModules),
27182719
EnableClangSPI(ctx.ClangImporterOpts.EnableClangSPI),
27192720
IsReadingBridgingPCH(false),
@@ -3834,8 +3835,13 @@ ImportDecl *swift::createImportDecl(ASTContext &Ctx,
38343835
auto *ID = ImportDecl::create(Ctx, DC, SourceLoc(),
38353836
ImportKind::Module, SourceLoc(),
38363837
importPath.get(), ClangN);
3837-
if (IsExported)
3838+
if (Ctx.ClangImporterOpts.BridgingHeaderIsInternal) {
3839+
ID->getAttrs().add(
3840+
new (Ctx) AccessControlAttr(SourceLoc(), SourceRange(),
3841+
AccessLevel::Internal, /*implicit=*/true));
3842+
} else if (IsExported) {
38383843
ID->getAttrs().add(new (Ctx) ExportedAttr(/*IsImplicit=*/false));
3844+
}
38393845
return ID;
38403846
}
38413847

lib/ClangImporter/ImporterImpl.h

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -481,6 +481,7 @@ class LLVM_LIBRARY_VISIBILITY ClangImporter::Implementation
481481
const bool ImportForwardDeclarations;
482482
const bool DisableSwiftBridgeAttr;
483483
const bool BridgingHeaderExplicitlyRequested;
484+
const bool BridgingHeaderIsInternal;
484485
const bool DisableOverlayModules;
485486
const bool EnableClangSPI;
486487

lib/Driver/Driver.cpp

Lines changed: 5 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -144,7 +144,8 @@ static void validateLegacyUnsupportedArgs(DiagnosticEngine &diags,
144144

145145
static void validateBridgingHeaderArgs(DiagnosticEngine &diags,
146146
const ArgList &args) {
147-
if (!args.hasArgNoClaim(options::OPT_import_objc_header))
147+
if (!args.hasArgNoClaim(options::OPT_import_bridging_header,
148+
options::OPT_internal_import_bridging_header))
148149
return;
149150

150151
if (args.hasArgNoClaim(options::OPT_import_underlying_module))
@@ -1521,7 +1522,9 @@ void Driver::buildActions(SmallVectorImpl<const Action *> &TopLevelActions,
15211522
if (Args.hasFlag(options::OPT_enable_bridging_pch,
15221523
options::OPT_disable_bridging_pch,
15231524
true)) {
1524-
if (Arg *A = Args.getLastArg(options::OPT_import_objc_header)) {
1525+
if (Arg *A = Args.getLastArg(
1526+
options::OPT_import_bridging_header,
1527+
options::OPT_internal_import_bridging_header)) {
15251528
StringRef Value = A->getValue();
15261529
auto Ty = TC.lookupTypeForExtension(llvm::sys::path::extension(Value));
15271530
if (Ty == file_types::TY_ClangHeader) {

lib/Driver/ToolChains.cpp

Lines changed: 23 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -522,19 +522,28 @@ ToolChain::constructInvocation(const CompileJobAction &job,
522522
addCommonFrontendArgs(context.OI, context.Output, context.Args, Arguments);
523523
addRuntimeLibraryFlags(context.OI, Arguments);
524524

525-
// Pass along an -import-objc-header arg, replacing the argument with the name
526-
// of any input PCH to the current action if one is present.
527-
if (context.Args.hasArgNoClaim(options::OPT_import_objc_header)) {
525+
// Pass along an -(internal-)?import-bridging-header arg, replacing the
526+
// argument with the name of any input PCH to the current action if one is
527+
// present.
528+
if (context.Args.hasArgNoClaim(options::OPT_import_bridging_header,
529+
options::OPT_internal_import_bridging_header)) {
528530
bool ForwardAsIs = true;
529531
bool bridgingPCHIsEnabled =
530532
context.Args.hasFlag(options::OPT_enable_bridging_pch,
531533
options::OPT_disable_bridging_pch, true);
532534
bool usePersistentPCH = bridgingPCHIsEnabled &&
533535
context.Args.hasArg(options::OPT_pch_output_dir);
536+
bool isInternalImport = context.Args.getLastArgNoClaim(
537+
options::OPT_import_bridging_header,
538+
options::OPT_internal_import_bridging_header)
539+
->getOption().getID() == options::OPT_internal_import_bridging_header;
534540
if (!usePersistentPCH) {
535541
for (auto *IJ : context.Inputs) {
536542
if (!IJ->getOutput().getAnyOutputForType(file_types::TY_PCH).empty()) {
537-
Arguments.push_back("-import-objc-header");
543+
if (isInternalImport)
544+
Arguments.push_back("-internal-import-bridging-header");
545+
else
546+
Arguments.push_back("-import-bridging-header");
538547
addInputsOfType(Arguments, context.Inputs, context.Args,
539548
file_types::TY_PCH);
540549
ForwardAsIs = false;
@@ -543,7 +552,8 @@ ToolChain::constructInvocation(const CompileJobAction &job,
543552
}
544553
}
545554
if (ForwardAsIs) {
546-
context.Args.AddLastArg(Arguments, options::OPT_import_objc_header);
555+
context.Args.AddLastArg(Arguments, options::OPT_import_bridging_header,
556+
options::OPT_internal_import_bridging_header);
547557
}
548558
if (usePersistentPCH) {
549559
context.Args.AddLastArg(Arguments, options::OPT_pch_output_dir);
@@ -972,7 +982,8 @@ ToolChain::constructInvocation(const InterpretJobAction &job,
972982
addCommonFrontendArgs(context.OI, context.Output, context.Args, Arguments);
973983
addRuntimeLibraryFlags(context.OI, Arguments);
974984

975-
context.Args.AddLastArg(Arguments, options::OPT_import_objc_header);
985+
context.Args.AddLastArg(Arguments, options::OPT_import_bridging_header,
986+
options::OPT_internal_import_bridging_header);
976987

977988
context.Args.AddLastArg(Arguments, options::OPT_parse_sil);
978989

@@ -1233,7 +1244,8 @@ ToolChain::constructInvocation(const MergeModuleJobAction &job,
12331244
options::OPT_omit_extension_block_symbols);
12341245
context.Args.AddLastArg(Arguments, options::OPT_symbol_graph_minimum_access_level);
12351246

1236-
context.Args.AddLastArg(Arguments, options::OPT_import_objc_header);
1247+
context.Args.AddLastArg(Arguments, options::OPT_import_bridging_header,
1248+
options::OPT_internal_import_bridging_header);
12371249

12381250
Arguments.push_back("-module-name");
12391251
Arguments.push_back(context.Args.MakeArgString(context.OI.ModuleName));
@@ -1276,7 +1288,8 @@ ToolChain::constructInvocation(const VerifyModuleInterfaceJobAction &job,
12761288
file_types::TY_SerializedDiagnostics,
12771289
"-serialize-diagnostics-path");
12781290

1279-
context.Args.AddLastArg(Arguments, options::OPT_import_objc_header);
1291+
context.Args.AddLastArg(Arguments, options::OPT_import_bridging_header,
1292+
options::OPT_internal_import_bridging_header);
12801293

12811294
Arguments.push_back("-module-name");
12821295
Arguments.push_back(context.Args.MakeArgString(context.OI.ModuleName));
@@ -1342,7 +1355,8 @@ ToolChain::constructInvocation(const REPLJobAction &job,
13421355
addCommonFrontendArgs(context.OI, context.Output, context.Args, FrontendArgs);
13431356
addRuntimeLibraryFlags(context.OI, FrontendArgs);
13441357

1345-
context.Args.AddLastArg(FrontendArgs, options::OPT_import_objc_header);
1358+
context.Args.AddLastArg(FrontendArgs, options::OPT_import_bridging_header,
1359+
options::OPT_internal_import_bridging_header);
13461360
context.Args.addAllArgs(FrontendArgs,
13471361
{options::OPT_framework, options::OPT_L});
13481362
ToolChain::addLinkedLibArgs(context.Args, FrontendArgs);

0 commit comments

Comments
 (0)