Skip to content

Commit 2a84d6d

Browse files
authored
Merge pull request #61919 from artemcm/InheritExtraClangStateForInterfaceSubInvocation
Inherit parent's extra Clang arguments when creating an interface build sub-invocation.
2 parents 701701f + d1cbf9c commit 2a84d6d

File tree

9 files changed

+88
-13
lines changed

9 files changed

+88
-13
lines changed

include/swift/Frontend/FrontendOptions.h

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -345,6 +345,11 @@ class FrontendOptions {
345345
/// are errors. The resulting serialized AST may include errors types and
346346
/// skip nodes entirely, depending on the errors involved.
347347
bool AllowModuleWithCompilerErrors = false;
348+
349+
/// Whether or not the compiler must be strict in ensuring that implicit downstream
350+
/// module dependency build tasks must inherit the parent compiler invocation's context,
351+
/// such as `-Xcc` flags, etc.
352+
bool StrictImplicitModuleContext = false;
348353

349354
/// Downgrade all errors emitted in the module interface verification phase
350355
/// to warnings.

include/swift/Frontend/ModuleInterfaceLoader.h

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -300,13 +300,15 @@ struct ModuleInterfaceLoaderOptions {
300300
bool disableImplicitSwiftModule = false;
301301
bool disableBuildingInterface = false;
302302
bool downgradeInterfaceVerificationError = false;
303+
bool strictImplicitModuleContext = false;
303304
std::string mainExecutablePath;
304305
ModuleInterfaceLoaderOptions(const FrontendOptions &Opts):
305306
remarkOnRebuildFromInterface(Opts.RemarkOnRebuildFromModuleInterface),
306307
disableInterfaceLock(Opts.DisableInterfaceFileLock),
307308
disableImplicitSwiftModule(Opts.DisableImplicitModules),
308309
disableBuildingInterface(Opts.DisableBuildingInterface),
309310
downgradeInterfaceVerificationError(Opts.DowngradeInterfaceVerificationError),
311+
strictImplicitModuleContext(Opts.StrictImplicitModuleContext),
310312
mainExecutablePath(Opts.MainExecutablePath)
311313
{
312314
switch (Opts.RequestedAction) {
@@ -479,7 +481,8 @@ struct InterfaceSubContextDelegateImpl: InterfaceSubContextDelegate {
479481
}
480482
void
481483
inheritOptionsForBuildingInterface(const SearchPathOptions &SearchPathOpts,
482-
const LangOptions &LangOpts, bool suppressRemarks,
484+
const LangOptions &LangOpts,
485+
bool suppressRemarks,
483486
RequireOSSAModules_t requireOSSAModules);
484487
bool extractSwiftInterfaceVersionAndArgs(CompilerInvocation &subInvocation,
485488
SmallVectorImpl<const char *> &SubArgs,

include/swift/Option/Options.td

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -167,6 +167,16 @@ def verify_incremental_dependencies :
167167
Flag<["-"], "verify-incremental-dependencies">,
168168
Flags<[FrontendOption, HelpHidden]>,
169169
HelpText<"Enable the dependency verifier for each frontend job">;
170+
171+
def strict_implicit_module_context :
172+
Flag<["-"], "strict-implicit-module-context">,
173+
Flags<[FrontendOption, HelpHidden]>,
174+
HelpText<"Enable the strict forwarding of compilation context to downstream implicit module dependencies">;
175+
176+
def no_strict_implicit_module_context :
177+
Flag<["-"], "no-strict-implicit-module-context">,
178+
Flags<[FrontendOption, HelpHidden]>,
179+
HelpText<"Disable the strict forwarding of compilation context to downstream implicit module dependencies">;
170180

171181
def disallow_forwarding_driver :
172182
Flag<["-"], "disallow-use-new-driver">, Flags<[]>,

lib/Frontend/ArgsToFrontendOptionsConverter.cpp

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -293,6 +293,10 @@ bool ArgsToFrontendOptionsConverter::convert(
293293
}
294294
Opts.EnableExperimentalCxxInteropInClangHeader =
295295
Args.hasArg(OPT_enable_experimental_cxx_interop_in_clang_header);
296+
297+
Opts.StrictImplicitModuleContext = Args.hasArg(OPT_strict_implicit_module_context,
298+
OPT_no_strict_implicit_module_context,
299+
false);
296300

297301
computeImportObjCHeaderOptions();
298302
computeImplicitImportModuleNames(OPT_import_module, /*isTestable=*/false);

lib/Frontend/ModuleInterfaceLoader.cpp

Lines changed: 5 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -1598,18 +1598,11 @@ InterfaceSubContextDelegateImpl::InterfaceSubContextDelegateImpl(
15981598
subClangImporterOpts.DetailedPreprocessingRecord =
15991599
clangImporterOpts.DetailedPreprocessingRecord;
16001600

1601-
// We need to add these extra clang flags because explicit module building
1602-
// related flags are all there: -fno-implicit-modules, -fmodule-map-file=,
1603-
// and -fmodule-file=.
1604-
// If we don't add these flags, the interface will be built with implicit
1605-
// PCMs.
1606-
// FIXME: With Implicit Module Builds, if sub-invocations inherit `-fmodule-map-file=` options,
1607-
// those modulemaps become File dependencies of all downstream PCMs and their depending Swift
1608-
// modules, triggering unnecessary re-builds. We work around this by only inheriting these options
1609-
// when building with explicit modules. While this problem will not manifest with Explicit Modules
1610-
// (which do not use the ClangImporter to build PCMs), we may still need a better way to
1611-
// decide which options must be inherited here.
1612-
if (LoaderOpts.disableImplicitSwiftModule) {
1601+
// If the compiler has been asked to be strict with ensuring downstream dependencies
1602+
// get the parent invocation's context, or this is an Explicit build, inherit the
1603+
// extra Clang arguments also.
1604+
if (LoaderOpts.strictImplicitModuleContext || LoaderOpts.disableImplicitSwiftModule) {
1605+
// Inherit any clang-specific state of the compilation (macros, clang flags, etc.)
16131606
subClangImporterOpts.ExtraArgs = clangImporterOpts.ExtraArgs;
16141607
for (auto arg : subClangImporterOpts.ExtraArgs) {
16151608
GenericArgs.push_back("-Xcc");
Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,4 @@
1+
// swift-interface-format-version: 1.0
2+
// swift-module-flags: -enable-library-evolution -module-name ImportsMacroSpecificClangModule
3+
4+
import OnlyWithMacro
Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,4 @@
1+
#if TANGERINE
2+
#else
3+
#error ("Macro TANGERINE is required to compile.")
4+
#endif
Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
module OnlyWithMacro { header "OnlyWithMacro.h" }
Lines changed: 51 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,51 @@
1+
// This test ensures that the parent invocation's '-Xcc X' flags are inherited when building dependency modules
2+
// RUN: %empty-directory(%t)
3+
4+
// Just running a compile is useful to make sure it succeeds because that means the transitive Clang module dependency
5+
// received the TANGERINE macro
6+
// RUN: %target-swift-frontend -typecheck -strict-implicit-module-context %s -I %S/Inputs/macro-only-module -Xcc -DTANGERINE=1 -disable-implicit-concurrency-module-import -disable-implicit-string-processing-module-import
7+
8+
// RUN: %target-swift-frontend -scan-dependencies -strict-implicit-module-context %s -o %t/deps.json -I %S/Inputs/macro-only-module -Xcc -DTANGERINE=1 -disable-implicit-concurrency-module-import -disable-implicit-string-processing-module-import
9+
// RUN: %FileCheck %s < %t/deps.json
10+
11+
import ImportsMacroSpecificClangModule
12+
13+
// CHECK: "directDependencies": [
14+
// CHECK-NEXT: {
15+
// CHECK-DAG: "swift": "ImportsMacroSpecificClangModule"
16+
// CHECK-NEXT: },
17+
// CHECK-NEXT: {
18+
// CHECK-DAG: "swift": "Swift"
19+
// CHECK-NEXT: },
20+
// CHECK-NEXT: {
21+
// CHECK-DAG: "swift": "SwiftOnoneSupport"
22+
// CHECK-NEXT: }
23+
// CHECK-NEXT: ],
24+
25+
//CHECK: "swift": "ImportsMacroSpecificClangModule"
26+
//CHECK-NEXT: },
27+
//CHECK-NEXT: {
28+
//CHECK-NEXT: "modulePath": "ImportsMacroSpecificClangModule.swiftmodule",
29+
//CHECK-NEXT: "sourceFiles": [
30+
//CHECK-NEXT: ],
31+
//CHECK-NEXT: "directDependencies": [
32+
//CHECK-NEXT: {
33+
//CHECK-NEXT: "clang": "OnlyWithMacro"
34+
35+
// CHECK: "clang": "OnlyWithMacro"
36+
// CHECK-NEXT: },
37+
// CHECK-NEXT: {
38+
// CHECK-NEXT: "modulePath": "OnlyWithMacro.pcm",
39+
// CHECK-NEXT: "sourceFiles": [
40+
// CHECK-DAG: "{{.*}}OnlyWithMacro.h"
41+
// CHECK-DAG: "{{.*}}module.modulemap"
42+
// CHECK-NEXT: ],
43+
// CHECK-NEXT: "directDependencies": [
44+
// CHECK-NEXT: ],
45+
// CHECK-NEXT: "details": {
46+
// CHECK-NEXT: "clang": {
47+
// CHECK-NEXT: "moduleMapPath": "{{.*}}module.modulemap",
48+
// CHECK-NEXT: "contextHash": "{{.*}}",
49+
// CHECK-NEXT: "commandLine": [
50+
51+
// CHECK: "TANGERINE=1",

0 commit comments

Comments
 (0)