Skip to content

Rdar 29016063 precompile bridging header #5977

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
11 changes: 11 additions & 0 deletions docs/Lexicon.rst
Original file line number Diff line number Diff line change
Expand Up @@ -231,6 +231,17 @@ source code, tests, and commit messages. See also the `LLVM lexicon`_.
library on the system when the library on the system cannot be modified.
Apple has a number of overlays for its own SDKs in stdlib/public/SDK/.

PCH
Precompiled header, a type of file ending in .pch. A precompiled header is
like a precompiled module, in the sense that it's the same file format and
is just a cache file produced by clang and read by ``clang::ASTReader``. The
difference is that PCH files are not "modular": they do not correspond to a
named module, and cannot be read in any order or imported by module-name;
rather they must be the first file parsed by the compiler. PCHs are used
only to accelerate the process of reading C/C++/Objective-C headers, such as
the bridging headers read in by the ``-import-objc-header`` command-line
flag to swiftc.

PR
1. "Problem Report": An issue reported in `LLVM's bug tracker`__.
See also `SR`.
Expand Down
8 changes: 8 additions & 0 deletions include/swift/AST/DiagnosticsClangImporter.def
Original file line number Diff line number Diff line change
Expand Up @@ -53,6 +53,9 @@ ERROR(bridging_header_error,Fatal,
WARNING(could_not_rewrite_bridging_header,none,
"failed to serialize bridging header; "
"target may not be debuggable outside of its original project", ())
ERROR(bridging_header_pch_error,Fatal,
"failed to emit precompiled header '%0' for bridging header '%1'",
(StringRef, StringRef))

WARNING(invalid_swift_name_method,none,
"too %select{few|many}0 parameters in swift_name attribute (expected %1; "
Expand All @@ -76,6 +79,11 @@ WARNING(unresolvable_clang_decl,none,
"imported declaration '%0' could not be mapped to '%1'",
(StringRef, StringRef))

WARNING(implicit_bridging_header_imported_from_module,none,
"implicit import of bridging header '%0' via module %1 "
"is deprecated and will be removed in a later version of Swift",
(StringRef, Identifier))

#ifndef DIAG_NO_UNDEF
# if defined(DIAG)
# undef DIAG
Expand Down
13 changes: 12 additions & 1 deletion include/swift/ClangImporter/ClangImporter.h
Original file line number Diff line number Diff line change
Expand Up @@ -201,13 +201,16 @@ class ClangImporter final : public ClangModuleLoader {
/// \param diagLoc A location to attach any diagnostics to if import fails.
/// \param trackParsedSymbols If true, tracks decls and macros that were
/// parsed from the bridging header.
/// \param implicitImport If true, indicates that this import was implicit
/// from a reference in a module file (deprecated behaviour).
///
/// \returns true if there was an error importing the header.
///
/// \sa getImportedHeaderModule
bool importBridgingHeader(StringRef header, ModuleDecl *adapter,
SourceLoc diagLoc = {},
bool trackParsedSymbols = false);
bool trackParsedSymbols = false,
bool implicitImport = false);

/// Returns the module that contains imports and declarations from all loaded
/// Objective-C header files.
Expand All @@ -218,6 +221,14 @@ class ClangImporter final : public ClangModuleLoader {
std::string getBridgingHeaderContents(StringRef headerPath, off_t &fileSize,
time_t &fileModTime);

/// Makes a temporary replica of the ClangImporter's CompilerInstance, reads
/// an Objective-C header file into the replica and emits a PCH file of its
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

These docblocks are fantastic!

nit-pick: Sorry to harp on about acronyms, but I noticed "PCH" isn't in docs/Lexicon.rst, either. I think that some Swift contributors might not be immediately familiar with the concept of a precompiled header. Since this pull request is the first use of "PCH" in a docblock in this codebase, I'd suggest adding the acronym to the Lexicon as well.

/// content. Delegates to clang for everything except construction of the
/// replica.
///
/// \sa clang::GeneratePCHAction
bool emitBridgingPCH(StringRef headerPath, StringRef outputPCHPath);

const clang::Module *getClangOwningModule(ClangNode Node) const;
bool hasTypedef(const clang::Decl *typeDecl) const;

Expand Down
3 changes: 3 additions & 0 deletions include/swift/ClangImporter/ClangImporterOptions.h
Original file line number Diff line number Diff line change
Expand Up @@ -35,6 +35,9 @@ class ClangImporterOptions {
/// Equivalent to Clang's -mcpu=.
std::string TargetCPU;

// The bridging header or PCH that will be imported.
std::string BridgingHeader;

/// \see Mode
enum class Modes {
/// Set up Clang for importing modules into Swift and generating IR from
Expand Down
14 changes: 13 additions & 1 deletion include/swift/Driver/Action.h
Original file line number Diff line number Diff line change
Expand Up @@ -48,9 +48,10 @@ class Action {
REPLJob,
LinkJob,
GenerateDSYMJob,
GeneratePCHJob,

JobFirst=CompileJob,
JobLast=GenerateDSYMJob
JobLast=GeneratePCHJob
};

static const char *getClassName(ActionClass AC);
Expand Down Expand Up @@ -268,6 +269,17 @@ class GenerateDSYMJobAction : public JobAction {
}
};

class GeneratePCHJobAction : public JobAction {
virtual void anchor();
public:
explicit GeneratePCHJobAction(Action *Input)
: JobAction(Action::GeneratePCHJob, Input, types::TY_PCH) {}

static bool classof(const Action *A) {
return A->getKind() == Action::GeneratePCHJob;
}
};

class LinkJobAction : public JobAction {
virtual void anchor();
LinkKind Kind;
Expand Down
3 changes: 3 additions & 0 deletions include/swift/Driver/ToolChain.h
Original file line number Diff line number Diff line change
Expand Up @@ -122,6 +122,9 @@ class ToolChain {
constructInvocation(const GenerateDSYMJobAction &job,
const JobContext &context) const;
virtual InvocationInfo
constructInvocation(const GeneratePCHJobAction &job,
const JobContext &context) const;
virtual InvocationInfo
constructInvocation(const AutolinkExtractJobAction &job,
const JobContext &context) const;
virtual InvocationInfo
Expand Down
1 change: 1 addition & 0 deletions include/swift/Driver/Types.def
Original file line number Diff line number Diff line change
Expand Up @@ -60,6 +60,7 @@ TYPE("remap", Remapping, "remap", "")

// Misc types
TYPE("pcm", ClangModuleFile, "pcm", "")
TYPE("pch", PCH, "pch", "")
TYPE("none", Nothing, "", "")

#undef TYPE
2 changes: 2 additions & 0 deletions include/swift/Frontend/FrontendOptions.h
Original file line number Diff line number Diff line change
Expand Up @@ -149,6 +149,8 @@ class FrontendOptions {
/// Parse, type-check, and dump type refinement context hierarchy
DumpTypeRefinementContexts,

EmitPCH, ///< Emit PCH of imported bridging header

EmitSILGen, ///< Emit raw SIL
EmitSIL, ///< Emit canonical SIL

Expand Down
3 changes: 3 additions & 0 deletions include/swift/Option/FrontendOptions.td
Original file line number Diff line number Diff line change
Expand Up @@ -235,6 +235,9 @@ def disable_modules_validate_system_headers : Flag<["-"], "disable-modules-valid
def emit_verbose_sil : Flag<["-"], "emit-verbose-sil">,
HelpText<"Emit locations during SIL emission">;

def emit_pch : Flag<["-"], "emit-pch">,
HelpText<"Emit PCH for imported Objective-C header file">, ModeOpt;

def enable_sil_ownership : Flag<["-"], "enable-sil-ownership">,
HelpText<"Enable the SIL Ownership Model">;

Expand Down
8 changes: 8 additions & 0 deletions include/swift/Option/Options.td
Original file line number Diff line number Diff line change
Expand Up @@ -237,6 +237,14 @@ def disable_swift_bridge_attr : Flag<["-"], "disable-swift-bridge-attr">,
Flags<[FrontendOption, HelpHidden]>,
HelpText<"Disable using the swift bridge attribute">;

def enable_bridging_pch : Flag<["-"], "enable-bridging-pch">,
Flags<[HelpHidden]>,
HelpText<"Enable automatic generation of bridging PCH files">;

def disable_bridging_pch : Flag<["-"], "disable-bridging-pch">,
Flags<[HelpHidden]>,
HelpText<"Disable automatic generation of bridging PCH files">;

// Diagnostic control options
def suppress_warnings : Flag<["-"], "suppress-warnings">,
Flags<[FrontendOption]>,
Expand Down
2 changes: 2 additions & 0 deletions include/swift/Strings.h
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,8 @@ namespace swift {
static const char SERIALIZED_MODULE_EXTENSION[] = "swiftmodule";
/// The extension for serialized documentation comments.
static const char SERIALIZED_MODULE_DOC_EXTENSION[] = "swiftdoc";
/// The extension for PCH files.
static const char PCH_EXTENSION[] = "pch";
/// The extension for SIL files.
static const char SIL_EXTENSION[] = "sil";
/// The extension for SIB files.
Expand Down
Loading