Skip to content

Commit ad967dd

Browse files
authored
Merge pull request #31145 from CodaFi/all-bran
[Frontend][NFC] Formalize isModuleExternallyConsumed
2 parents db3e9fc + 37f016b commit ad967dd

File tree

4 files changed

+66
-35
lines changed

4 files changed

+66
-35
lines changed

include/swift/Frontend/Frontend.h

Lines changed: 18 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -386,9 +386,25 @@ class CompilerInvocation {
386386

387387
std::string getLdAddCFileOutputPathForWholeModule() const;
388388

389+
public:
390+
/// Given the current configuration of this frontend invocation, a set of
391+
/// supplementary output paths, and a module, compute the appropriate set of
392+
/// serialization options.
393+
///
394+
/// FIXME: The \p module parameter supports the
395+
/// \c SerializeOptionsForDebugging hack.
389396
SerializationOptions
390397
computeSerializationOptions(const SupplementaryOutputPaths &outs,
391-
bool moduleIsPublic) const;
398+
const ModuleDecl *module) const;
399+
400+
/// Returns an approximation of whether the given module could be
401+
/// redistributed and consumed by external clients.
402+
///
403+
/// FIXME: The scope of this computation should be limited entirely to
404+
/// PrintAsObjC. Unfortunately, it has been co-opted to support the
405+
/// \c SerializeOptionsForDebugging hack. Once this information can be
406+
/// transferred from module files to the dSYMs, remove this.
407+
bool isModuleExternallyConsumed(const ModuleDecl *mod) const;
392408
};
393409

394410
/// A class which manages the state and execution of the compiler.
@@ -555,9 +571,7 @@ class CompilerInstance {
555571
/// Returns true if there was an error during setup.
556572
bool setup(const CompilerInvocation &Invocation);
557573

558-
const CompilerInvocation &getInvocation() {
559-
return Invocation;
560-
}
574+
const CompilerInvocation &getInvocation() const { return Invocation; }
561575

562576
/// If a code completion buffer has been set, returns the corresponding source
563577
/// file.

lib/Frontend/CompilerInvocation.cpp

Lines changed: 27 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1701,3 +1701,30 @@ CompilerInvocation::setUpInputForSILTool(
17011701
}
17021702
return fileBufOrErr;
17031703
}
1704+
1705+
bool CompilerInvocation::isModuleExternallyConsumed(
1706+
const ModuleDecl *mod) const {
1707+
// Modules for executables aren't expected to be consumed by other modules.
1708+
// This picks up all kinds of entrypoints, including script mode,
1709+
// @UIApplicationMain and @NSApplicationMain.
1710+
if (mod->hasEntryPoint()) {
1711+
return false;
1712+
}
1713+
1714+
// If an implicit Objective-C header was needed to construct this module, it
1715+
// must be the product of a library target.
1716+
if (!getFrontendOptions().ImplicitObjCHeaderPath.empty()) {
1717+
return false;
1718+
}
1719+
1720+
// App extensions are special beasts because they build without entrypoints
1721+
// like library targets, but they behave like executable targets because
1722+
// their associated modules are not suitable for distribution.
1723+
if (mod->getASTContext().LangOpts.EnableAppExtensionRestrictions) {
1724+
return false;
1725+
}
1726+
1727+
// FIXME: This is still a lousy approximation of whether the module file will
1728+
// be externally consumed.
1729+
return true;
1730+
}

lib/Frontend/Frontend.cpp

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -152,7 +152,7 @@ CompilerInvocation::getPrivateModuleInterfaceOutputPathForWholeModule() const {
152152
}
153153

154154
SerializationOptions CompilerInvocation::computeSerializationOptions(
155-
const SupplementaryOutputPaths &outs, bool moduleIsPublic) const {
155+
const SupplementaryOutputPaths &outs, const ModuleDecl *module) const {
156156
const FrontendOptions &opts = getFrontendOptions();
157157

158158
SerializationOptions serializationOpts;
@@ -171,7 +171,8 @@ SerializationOptions CompilerInvocation::computeSerializationOptions(
171171
// so only serialize them if the module isn't going to be shipped to
172172
// the public.
173173
serializationOpts.SerializeOptionsForDebugging =
174-
opts.SerializeOptionsForDebugging.getValueOr(!moduleIsPublic);
174+
opts.SerializeOptionsForDebugging.getValueOr(
175+
!isModuleExternallyConsumed(module));
175176

176177
return serializationOpts;
177178
}

lib/FrontendTool/FrontendTool.cpp

Lines changed: 18 additions & 29 deletions
Original file line numberDiff line numberDiff line change
@@ -1074,20 +1074,19 @@ static bool performCompileStepsPostSILGen(CompilerInstance &Instance,
10741074
std::unique_ptr<SILModule> SM,
10751075
ModuleOrSourceFile MSF,
10761076
const PrimarySpecificPaths &PSPs,
1077-
bool moduleIsPublic, int &ReturnValue,
1077+
int &ReturnValue,
10781078
FrontendObserver *observer);
10791079

1080-
static bool
1081-
performCompileStepsPostSema(const CompilerInvocation &Invocation,
1082-
CompilerInstance &Instance,
1083-
bool moduleIsPublic, int &ReturnValue,
1084-
FrontendObserver *observer) {
1080+
static bool performCompileStepsPostSema(const CompilerInvocation &Invocation,
1081+
CompilerInstance &Instance,
1082+
int &ReturnValue,
1083+
FrontendObserver *observer) {
10851084
auto mod = Instance.getMainModule();
10861085
if (auto SM = Instance.takeSILModule()) {
10871086
const PrimarySpecificPaths PSPs =
10881087
Instance.getPrimarySpecificPathsForAtMostOnePrimary();
10891088
return performCompileStepsPostSILGen(Instance, Invocation, std::move(SM),
1090-
mod, PSPs, moduleIsPublic,
1089+
mod, PSPs,
10911090
ReturnValue, observer);
10921091
}
10931092

@@ -1100,7 +1099,7 @@ performCompileStepsPostSema(const CompilerInvocation &Invocation,
11001099
const PrimarySpecificPaths PSPs =
11011100
Instance.getPrimarySpecificPathsForWholeModuleOptimizationMode();
11021101
return performCompileStepsPostSILGen(Instance, Invocation, std::move(SM),
1103-
mod, PSPs, moduleIsPublic,
1102+
mod, PSPs,
11041103
ReturnValue, observer);
11051104
}
11061105
// If there are primary source files, build a separate SILModule for
@@ -1113,7 +1112,7 @@ performCompileStepsPostSema(const CompilerInvocation &Invocation,
11131112
const PrimarySpecificPaths PSPs =
11141113
Instance.getPrimarySpecificPathsForSourceFile(*PrimaryFile);
11151114
result |= performCompileStepsPostSILGen(Instance, Invocation, std::move(SM),
1116-
PrimaryFile, PSPs, moduleIsPublic,
1115+
PrimaryFile, PSPs,
11171116
ReturnValue, observer);
11181117
}
11191118

@@ -1131,7 +1130,7 @@ performCompileStepsPostSema(const CompilerInvocation &Invocation,
11311130
const PrimarySpecificPaths &PSPs =
11321131
Instance.getPrimarySpecificPathsForPrimary(SASTF->getFilename());
11331132
result |= performCompileStepsPostSILGen(Instance, Invocation, std::move(SM),
1134-
mod, PSPs, moduleIsPublic,
1133+
mod, PSPs,
11351134
ReturnValue, observer);
11361135
}
11371136
}
@@ -1158,8 +1157,7 @@ emitIndexData(const CompilerInvocation &Invocation, const CompilerInstance &Inst
11581157
/// `-typecheck`, but skipped for any mode that runs SIL diagnostics if there's
11591158
/// an error found there (to get those diagnostics back to the user faster).
11601159
static bool emitAnyWholeModulePostTypeCheckSupplementaryOutputs(
1161-
CompilerInstance &Instance, const CompilerInvocation &Invocation,
1162-
bool moduleIsPublic) {
1160+
CompilerInstance &Instance, const CompilerInvocation &Invocation) {
11631161
const FrontendOptions &opts = Invocation.getFrontendOptions();
11641162

11651163
// Record whether we failed to emit any of these outputs, but keep going; one
@@ -1182,7 +1180,8 @@ static bool emitAnyWholeModulePostTypeCheckSupplementaryOutputs(
11821180
}
11831181
hadAnyError |= printAsObjCIfNeeded(
11841182
Invocation.getObjCHeaderOutputPathForAtMostOnePrimary(),
1185-
Instance.getMainModule(), BridgingHeaderPathForPrint, moduleIsPublic);
1183+
Instance.getMainModule(), BridgingHeaderPathForPrint,
1184+
Invocation.isModuleExternallyConsumed(Instance.getMainModule()));
11861185
}
11871186

11881187
if (opts.InputsAndOutputs.hasModuleInterfaceOutputPath()) {
@@ -1315,13 +1314,6 @@ static bool performCompile(CompilerInstance &Instance,
13151314
(void)emitLoadedModuleTraceForAllPrimariesIfNeeded(
13161315
Instance.getMainModule(), Instance.getDependencyTracker(), opts);
13171316

1318-
// FIXME: This is still a lousy approximation of whether the module file will
1319-
// be externally consumed.
1320-
bool moduleIsPublic =
1321-
!Instance.getMainModule()->hasEntryPoint() &&
1322-
opts.ImplicitObjCHeaderPath.empty() &&
1323-
!Context.LangOpts.EnableAppExtensionRestrictions;
1324-
13251317
// We've just been told to perform a typecheck, so we can return now.
13261318
if (Action == FrontendOptions::ActionType::Typecheck) {
13271319
if (emitIndexData(Invocation, Instance))
@@ -1336,8 +1328,7 @@ static bool performCompile(CompilerInstance &Instance,
13361328
// guarding the emission of whole-module supplementary outputs.
13371329
if (opts.InputsAndOutputs.isWholeModule()) {
13381330
if (emitAnyWholeModulePostTypeCheckSupplementaryOutputs(Instance,
1339-
Invocation,
1340-
moduleIsPublic)) {
1331+
Invocation)) {
13411332
return true;
13421333
}
13431334
}
@@ -1347,8 +1338,8 @@ static bool performCompile(CompilerInstance &Instance,
13471338
assert(FrontendOptions::doesActionGenerateSIL(Action) &&
13481339
"All actions not requiring SILGen must have been handled!");
13491340

1350-
return performCompileStepsPostSema(Invocation, Instance, moduleIsPublic,
1351-
ReturnValue, observer);
1341+
return performCompileStepsPostSema(Invocation, Instance, ReturnValue,
1342+
observer);
13521343
}
13531344

13541345
static bool serializeSIB(SILModule *SM, const PrimarySpecificPaths &PSPs,
@@ -1575,9 +1566,8 @@ static bool performCompileStepsPostSILGen(CompilerInstance &Instance,
15751566
std::unique_ptr<SILModule> SM,
15761567
ModuleOrSourceFile MSF,
15771568
const PrimarySpecificPaths &PSPs,
1578-
bool moduleIsPublic, int &ReturnValue,
1569+
int &ReturnValue,
15791570
FrontendObserver *observer) {
1580-
15811571
FrontendOptions opts = Invocation.getFrontendOptions();
15821572
FrontendOptions::ActionType Action = opts.RequestedAction;
15831573
const ASTContext &Context = Instance.getASTContext();
@@ -1618,7 +1608,7 @@ static bool performCompileStepsPostSILGen(CompilerInstance &Instance,
16181608
return;
16191609

16201610
SerializationOptions serializationOpts =
1621-
Invocation.computeSerializationOptions(outs, moduleIsPublic);
1611+
Invocation.computeSerializationOptions(outs, Instance.getMainModule());
16221612
serialize(MSF, serializationOpts, SM.get());
16231613
};
16241614

@@ -1633,8 +1623,7 @@ static bool performCompileStepsPostSILGen(CompilerInstance &Instance,
16331623
if (observer)
16341624
observer->performedSILProcessing(*SM);
16351625

1636-
emitAnyWholeModulePostTypeCheckSupplementaryOutputs(Instance, Invocation,
1637-
moduleIsPublic);
1626+
emitAnyWholeModulePostTypeCheckSupplementaryOutputs(Instance, Invocation);
16381627

16391628
if (Action == FrontendOptions::ActionType::EmitSIB)
16401629
return serializeSIB(SM.get(), PSPs, Context, MSF);

0 commit comments

Comments
 (0)