Skip to content

Commit 28f1248

Browse files
authored
Merge pull request swiftlang#5977 from graydon/rdar-29016063-precompile-bridging-header
Rdar 29016063 precompile bridging header
2 parents 914356f + 5f747ea commit 28f1248

35 files changed

+499
-60
lines changed

docs/Lexicon.rst

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -231,6 +231,17 @@ source code, tests, and commit messages. See also the `LLVM lexicon`_.
231231
library on the system when the library on the system cannot be modified.
232232
Apple has a number of overlays for its own SDKs in stdlib/public/SDK/.
233233

234+
PCH
235+
Precompiled header, a type of file ending in .pch. A precompiled header is
236+
like a precompiled module, in the sense that it's the same file format and
237+
is just a cache file produced by clang and read by ``clang::ASTReader``. The
238+
difference is that PCH files are not "modular": they do not correspond to a
239+
named module, and cannot be read in any order or imported by module-name;
240+
rather they must be the first file parsed by the compiler. PCHs are used
241+
only to accelerate the process of reading C/C++/Objective-C headers, such as
242+
the bridging headers read in by the ``-import-objc-header`` command-line
243+
flag to swiftc.
244+
234245
PR
235246
1. "Problem Report": An issue reported in `LLVM's bug tracker`__.
236247
See also `SR`.

include/swift/AST/DiagnosticsClangImporter.def

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -53,6 +53,9 @@ ERROR(bridging_header_error,Fatal,
5353
WARNING(could_not_rewrite_bridging_header,none,
5454
"failed to serialize bridging header; "
5555
"target may not be debuggable outside of its original project", ())
56+
ERROR(bridging_header_pch_error,Fatal,
57+
"failed to emit precompiled header '%0' for bridging header '%1'",
58+
(StringRef, StringRef))
5659

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

82+
WARNING(implicit_bridging_header_imported_from_module,none,
83+
"implicit import of bridging header '%0' via module %1 "
84+
"is deprecated and will be removed in a later version of Swift",
85+
(StringRef, Identifier))
86+
7987
#ifndef DIAG_NO_UNDEF
8088
# if defined(DIAG)
8189
# undef DIAG

include/swift/ClangImporter/ClangImporter.h

Lines changed: 12 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -201,13 +201,16 @@ class ClangImporter final : public ClangModuleLoader {
201201
/// \param diagLoc A location to attach any diagnostics to if import fails.
202202
/// \param trackParsedSymbols If true, tracks decls and macros that were
203203
/// parsed from the bridging header.
204+
/// \param implicitImport If true, indicates that this import was implicit
205+
/// from a reference in a module file (deprecated behaviour).
204206
///
205207
/// \returns true if there was an error importing the header.
206208
///
207209
/// \sa getImportedHeaderModule
208210
bool importBridgingHeader(StringRef header, ModuleDecl *adapter,
209211
SourceLoc diagLoc = {},
210-
bool trackParsedSymbols = false);
212+
bool trackParsedSymbols = false,
213+
bool implicitImport = false);
211214

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

224+
/// Makes a temporary replica of the ClangImporter's CompilerInstance, reads
225+
/// an Objective-C header file into the replica and emits a PCH file of its
226+
/// content. Delegates to clang for everything except construction of the
227+
/// replica.
228+
///
229+
/// \sa clang::GeneratePCHAction
230+
bool emitBridgingPCH(StringRef headerPath, StringRef outputPCHPath);
231+
221232
const clang::Module *getClangOwningModule(ClangNode Node) const;
222233
bool hasTypedef(const clang::Decl *typeDecl) const;
223234

include/swift/ClangImporter/ClangImporterOptions.h

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -35,6 +35,9 @@ class ClangImporterOptions {
3535
/// Equivalent to Clang's -mcpu=.
3636
std::string TargetCPU;
3737

38+
// The bridging header or PCH that will be imported.
39+
std::string BridgingHeader;
40+
3841
/// \see Mode
3942
enum class Modes {
4043
/// Set up Clang for importing modules into Swift and generating IR from

include/swift/Driver/Action.h

Lines changed: 13 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -48,9 +48,10 @@ class Action {
4848
REPLJob,
4949
LinkJob,
5050
GenerateDSYMJob,
51+
GeneratePCHJob,
5152

5253
JobFirst=CompileJob,
53-
JobLast=GenerateDSYMJob
54+
JobLast=GeneratePCHJob
5455
};
5556

5657
static const char *getClassName(ActionClass AC);
@@ -268,6 +269,17 @@ class GenerateDSYMJobAction : public JobAction {
268269
}
269270
};
270271

272+
class GeneratePCHJobAction : public JobAction {
273+
virtual void anchor();
274+
public:
275+
explicit GeneratePCHJobAction(Action *Input)
276+
: JobAction(Action::GeneratePCHJob, Input, types::TY_PCH) {}
277+
278+
static bool classof(const Action *A) {
279+
return A->getKind() == Action::GeneratePCHJob;
280+
}
281+
};
282+
271283
class LinkJobAction : public JobAction {
272284
virtual void anchor();
273285
LinkKind Kind;

include/swift/Driver/ToolChain.h

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -122,6 +122,9 @@ class ToolChain {
122122
constructInvocation(const GenerateDSYMJobAction &job,
123123
const JobContext &context) const;
124124
virtual InvocationInfo
125+
constructInvocation(const GeneratePCHJobAction &job,
126+
const JobContext &context) const;
127+
virtual InvocationInfo
125128
constructInvocation(const AutolinkExtractJobAction &job,
126129
const JobContext &context) const;
127130
virtual InvocationInfo

include/swift/Driver/Types.def

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -60,6 +60,7 @@ TYPE("remap", Remapping, "remap", "")
6060

6161
// Misc types
6262
TYPE("pcm", ClangModuleFile, "pcm", "")
63+
TYPE("pch", PCH, "pch", "")
6364
TYPE("none", Nothing, "", "")
6465

6566
#undef TYPE

include/swift/Frontend/FrontendOptions.h

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -149,6 +149,8 @@ class FrontendOptions {
149149
/// Parse, type-check, and dump type refinement context hierarchy
150150
DumpTypeRefinementContexts,
151151

152+
EmitPCH, ///< Emit PCH of imported bridging header
153+
152154
EmitSILGen, ///< Emit raw SIL
153155
EmitSIL, ///< Emit canonical SIL
154156

include/swift/Option/FrontendOptions.td

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -235,6 +235,9 @@ def disable_modules_validate_system_headers : Flag<["-"], "disable-modules-valid
235235
def emit_verbose_sil : Flag<["-"], "emit-verbose-sil">,
236236
HelpText<"Emit locations during SIL emission">;
237237

238+
def emit_pch : Flag<["-"], "emit-pch">,
239+
HelpText<"Emit PCH for imported Objective-C header file">, ModeOpt;
240+
238241
def enable_sil_ownership : Flag<["-"], "enable-sil-ownership">,
239242
HelpText<"Enable the SIL Ownership Model">;
240243

include/swift/Option/Options.td

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -237,6 +237,14 @@ def disable_swift_bridge_attr : Flag<["-"], "disable-swift-bridge-attr">,
237237
Flags<[FrontendOption, HelpHidden]>,
238238
HelpText<"Disable using the swift bridge attribute">;
239239

240+
def enable_bridging_pch : Flag<["-"], "enable-bridging-pch">,
241+
Flags<[HelpHidden]>,
242+
HelpText<"Enable automatic generation of bridging PCH files">;
243+
244+
def disable_bridging_pch : Flag<["-"], "disable-bridging-pch">,
245+
Flags<[HelpHidden]>,
246+
HelpText<"Disable automatic generation of bridging PCH files">;
247+
240248
// Diagnostic control options
241249
def suppress_warnings : Flag<["-"], "suppress-warnings">,
242250
Flags<[FrontendOption]>,

include/swift/Strings.h

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -18,6 +18,8 @@ namespace swift {
1818
static const char SERIALIZED_MODULE_EXTENSION[] = "swiftmodule";
1919
/// The extension for serialized documentation comments.
2020
static const char SERIALIZED_MODULE_DOC_EXTENSION[] = "swiftdoc";
21+
/// The extension for PCH files.
22+
static const char PCH_EXTENSION[] = "pch";
2123
/// The extension for SIL files.
2224
static const char SIL_EXTENSION[] = "sil";
2325
/// The extension for SIB files.

0 commit comments

Comments
 (0)