Skip to content

Commit 2383d7a

Browse files
committed
Introduce "-internal" variant of bridging header import flags
The flags "-import-bridging-header" and "-import-pch" import a bridging header, treating the contents as a public import. Introduce "internal-" variants of both flags that provide the same semantics, but are intended to treat the imported contents as if they came in through an internal import. This is just plumbing of the options for the moment.
1 parent ffa6d65 commit 2383d7a

File tree

12 files changed

+148
-19
lines changed

12 files changed

+148
-19
lines changed

include/swift/AST/DiagnosticsFrontend.def

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -636,6 +636,8 @@ NOTE(dependency_scan_unexpected_variant_module_map_note, none,
636636
NOTE(dependency_scan_unexpected_variant_extra_arg_note, none,
637637
"%select{first|second}0 module command-line has extra argument: '%1'", (bool, StringRef))
638638

639+
ERROR(bridging_header_and_pch_internal_mismatch,none,
640+
"bridging header and precompiled header options mismatch on internal vs. public import", ())
639641

640642
#define UNDEFINE_DIAGNOSTIC_MACROS
641643
#include "DefineDiagnosticMacros.h"

include/swift/Basic/LangOptions.h

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

1049+
/// Whether the bridging header and PCH file are considered to be
1050+
/// internal imports.
1051+
bool BridgingHeaderIsInternal = false;
1052+
10491053
/// When automatically generating a precompiled header from the bridging
10501054
/// header, place it in this directory.
10511055
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: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -353,10 +353,19 @@ def import_bridging_header : Separate<["-"], "import-bridging-header">,
353353
def import_objc_header : Separate<["-"], "import-objc-header">,
354354
Flags<[FrontendOption, HelpHidden, ArgumentIsPath]>,
355355
Alias<import_bridging_header>;
356+
357+
def internal_import_bridging_header : Separate<["-"], "internal-import-bridging-header">,
358+
Flags<[FrontendOption, ArgumentIsPath]>,
359+
HelpText<"Imports an 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/ClangImporter/ClangImporter.cpp

Lines changed: 1 addition & 0 deletions
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),

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_bridging_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_bridging_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_bridging_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_bridging_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_bridging_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_bridging_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_bridging_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_bridging_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);

lib/Frontend/ArgsToFrontendOptionsConverter.cpp

Lines changed: 26 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -913,18 +913,40 @@ static inline bool isPCHFilenameExtension(StringRef path) {
913913

914914
void ArgsToFrontendOptionsConverter::computeImportObjCHeaderOptions() {
915915
using namespace options;
916-
if (const Arg *A = Args.getLastArgNoClaim(OPT_import_bridging_header)) {
917-
// Legacy support for passing PCH file through `-import-objc-header`.
916+
bool hadNormalBridgingHeader = false;
917+
if (const Arg *A = Args.getLastArgNoClaim(
918+
OPT_import_bridging_header,
919+
OPT_internal_import_bridging_header)) {
920+
// Legacy support for passing PCH file through `-import-bridging-header`.
918921
if (isPCHFilenameExtension(A->getValue()))
919922
Opts.ImplicitObjCPCHPath = A->getValue();
920923
else
921924
Opts.ImplicitObjCHeaderPath = A->getValue();
922-
// If `-import-object-header` is used, it means the module has a direct
925+
// If `-import-bridging-header` is used, it means the module has a direct
923926
// bridging header dependency and it can be serialized into binary module.
924927
Opts.ModuleHasBridgingHeader |= true;
928+
929+
Opts.ImportHeaderAsInternal =
930+
A->getOption().getID() == OPT_internal_import_bridging_header;
931+
932+
hadNormalBridgingHeader = true;
925933
}
926-
if (const Arg *A = Args.getLastArgNoClaim(OPT_import_pch))
934+
if (const Arg *A = Args.getLastArgNoClaim(OPT_import_pch,
935+
OPT_internal_import_pch)) {
927936
Opts.ImplicitObjCPCHPath = A->getValue();
937+
938+
bool importAsInternal = A->getOption().getID() == OPT_internal_import_pch;
939+
940+
/// Don't let the bridging-header and precompiled-header options differ in
941+
/// whether they are treated as internal or public imports.
942+
if (hadNormalBridgingHeader &&
943+
importAsInternal != Opts.ImportHeaderAsInternal) {
944+
Diags.diagnose(SourceLoc(),
945+
diag::bridging_header_and_pch_internal_mismatch);
946+
}
947+
948+
Opts.ImportHeaderAsInternal = importAsInternal;
949+
}
928950
}
929951
void ArgsToFrontendOptionsConverter::
930952
computeImplicitImportModuleNames(OptSpecifier id, bool isTestable) {

lib/Frontend/CompilerInvocation.cpp

Lines changed: 18 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -363,6 +363,8 @@ setBridgingHeaderFromFrontendOptions(ClangImporterOptions &ImporterOpts,
363363
if (!FrontendOpts.InputsAndOutputs.hasInputs())
364364
return;
365365

366+
ImporterOpts.BridgingHeaderIsInternal = FrontendOpts.ImportHeaderAsInternal;
367+
366368
// If we aren't asked to output a bridging header, we don't need to set this.
367369
if (ImporterOpts.PrecompiledHeaderOutputDir.empty())
368370
return;
@@ -2109,10 +2111,24 @@ static bool ParseClangImporterArgs(ClangImporterOptions &Opts, ArgList &Args,
21092111
else if (Args.hasArg(OPT_emit_pcm) || Args.hasArg(OPT_dump_pcm))
21102112
Opts.Mode = ClangImporterOptions::Modes::PrecompiledModule;
21112113

2112-
if (auto *A = Args.getLastArg(OPT_import_bridging_header))
2114+
bool hadNormalBridgingHeader = false;
2115+
if (auto *A = Args.getLastArg(OPT_import_bridging_header,
2116+
OPT_internal_import_bridging_header)) {
21132117
Opts.BridgingHeader = A->getValue();
2114-
if (auto *A = Args.getLastArg(OPT_import_pch))
2118+
Opts.BridgingHeaderIsInternal =
2119+
A->getOption().getID() == OPT_internal_import_bridging_header;
2120+
}
2121+
if (auto *A = Args.getLastArg(OPT_import_pch, OPT_internal_import_pch)) {
21152122
Opts.BridgingHeaderPCH = A->getValue();
2123+
bool importAsInternal = A->getOption().getID() == OPT_internal_import_pch;
2124+
if (hadNormalBridgingHeader &&
2125+
importAsInternal != Opts.BridgingHeaderIsInternal) {
2126+
Diags.diagnose(SourceLoc(),
2127+
diag::bridging_header_and_pch_internal_mismatch);
2128+
}
2129+
Opts.BridgingHeaderIsInternal = importAsInternal;
2130+
}
2131+
21162132
Opts.DisableSwiftBridgeAttr |= Args.hasArg(OPT_disable_swift_bridge_attr);
21172133

21182134
Opts.DisableOverlayModules |= Args.hasArg(OPT_emit_imported_modules);

0 commit comments

Comments
 (0)