Skip to content

Commit 05a6d11

Browse files
committed
Frontend: Only print the -project-name flag in private and package interfaces.
Resolves rdar://130992944.
1 parent b1418f6 commit 05a6d11

File tree

4 files changed

+66
-13
lines changed

4 files changed

+66
-13
lines changed

include/swift/Frontend/ModuleInterfaceSupport.h

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -55,6 +55,10 @@ struct ModuleInterfaceOptions {
5555
/// Flags which appear in all .swiftinterface files.
5656
InterfaceFlags PublicFlags = {};
5757

58+
/// Flags which appear in both the private and package .swiftinterface files,
59+
/// but not the public interface.
60+
InterfaceFlags PrivateFlags = {};
61+
5862
/// Flags which appear only in the .package.swiftinterface.
5963
InterfaceFlags PackageFlags = {};
6064

lib/Frontend/CompilerInvocation.cpp

Lines changed: 11 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -449,13 +449,16 @@ static bool ShouldIncludeModuleInterfaceArg(const Arg *A) {
449449
return true;
450450
}
451451

452-
static bool ShouldIncludeArgInPackageInterfaceOnly(const Arg *A,
453-
ArgList &Args) {
452+
static bool IsPackageInterfaceFlag(const Arg *A, ArgList &Args) {
454453
return A->getOption().matches(options::OPT_package_name) &&
455454
Args.hasArg(
456455
options::OPT_disable_print_package_name_for_non_package_interface);
457456
}
458457

458+
static bool IsPrivateInterfaceFlag(const Arg *A, ArgList &Args) {
459+
return A->getOption().matches(options::OPT_project_name);
460+
}
461+
459462
/// Save a copy of any flags marked as ModuleInterfaceOption, if running
460463
/// in a mode that is going to emit a .swiftinterface file.
461464
static void SaveModuleInterfaceArgs(ModuleInterfaceOptions &Opts,
@@ -470,14 +473,18 @@ static void SaveModuleInterfaceArgs(ModuleInterfaceOptions &Opts,
470473
};
471474

472475
RenderedInterfaceArgs PublicArgs{};
476+
RenderedInterfaceArgs PrivateArgs{};
473477
RenderedInterfaceArgs PackageArgs{};
474478

475479
auto interfaceArgListForArg = [&](Arg *A) -> ArgStringList & {
476480
bool ignorable =
477481
A->getOption().hasFlag(options::ModuleInterfaceOptionIgnorable);
478-
if (ShouldIncludeArgInPackageInterfaceOnly(A, Args))
482+
if (IsPackageInterfaceFlag(A, Args))
479483
return ignorable ? PackageArgs.Ignorable : PackageArgs.Standard;
480484

485+
if (IsPrivateInterfaceFlag(A, Args))
486+
return ignorable ? PrivateArgs.Ignorable : PrivateArgs.Standard;
487+
481488
return ignorable ? PublicArgs.Ignorable : PublicArgs.Standard;
482489
};
483490

@@ -503,6 +510,7 @@ static void SaveModuleInterfaceArgs(ModuleInterfaceOptions &Opts,
503510
};
504511

505512
updateInterfaceOpts(Opts.PublicFlags, PublicArgs);
513+
updateInterfaceOpts(Opts.PrivateFlags, PrivateArgs);
506514
updateInterfaceOpts(Opts.PackageFlags, PackageArgs);
507515
}
508516

lib/Frontend/ModuleInterfaceSupport.cpp

Lines changed: 28 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -61,9 +61,12 @@ static void printToolVersionAndFlagsComment(raw_ostream &out,
6161
<< ToolsVersion << "\n";
6262
out << "// " SWIFT_MODULE_FLAGS_KEY ": " << Opts.PublicFlags.Flags;
6363

64-
// Append flags that are for the package interface only (e.g. -package-name
65-
// when -disable-print-package-name-for-non-package-interface is specified).
66-
if (Opts.printPackageInterface() && !Opts.PackageFlags.Flags.empty())
64+
if (Opts.InterfaceContentMode >= PrintOptions::InterfaceMode::Private &&
65+
!Opts.PrivateFlags.Flags.empty())
66+
out << " " << Opts.PrivateFlags.Flags;
67+
68+
if (Opts.InterfaceContentMode >= PrintOptions::InterfaceMode::Package &&
69+
!Opts.PackageFlags.Flags.empty())
6770
out << " " << Opts.PackageFlags.Flags;
6871

6972
// Insert additional -module-alias flags
@@ -100,14 +103,29 @@ static void printToolVersionAndFlagsComment(raw_ostream &out,
100103
}
101104
out << "\n";
102105

103-
if (!Opts.PublicFlags.IgnorableFlags.empty()) {
104-
out << "// " SWIFT_MODULE_FLAGS_IGNORABLE_KEY ": "
105-
<< Opts.PublicFlags.IgnorableFlags << "\n";
106-
}
106+
// Add swift-module-flags-ignorable: if non-empty.
107+
{
108+
llvm::SmallVector<StringRef, 4> ignorableFlags;
109+
110+
if (!Opts.PublicFlags.IgnorableFlags.empty())
111+
ignorableFlags.push_back(Opts.PublicFlags.IgnorableFlags);
112+
113+
if (Opts.InterfaceContentMode >= PrintOptions::InterfaceMode::Private &&
114+
!Opts.PrivateFlags.IgnorableFlags.empty())
115+
ignorableFlags.push_back(Opts.PrivateFlags.IgnorableFlags);
107116

108-
// Append ignorable flags that are for the package interface only.
109-
if (Opts.printPackageInterface() && !Opts.PackageFlags.IgnorableFlags.empty())
110-
out << " " << Opts.PackageFlags.IgnorableFlags;
117+
if (Opts.InterfaceContentMode >= PrintOptions::InterfaceMode::Package &&
118+
!Opts.PackageFlags.IgnorableFlags.empty())
119+
ignorableFlags.push_back(Opts.PackageFlags.IgnorableFlags);
120+
121+
if (!ignorableFlags.empty()) {
122+
out << "// " SWIFT_MODULE_FLAGS_IGNORABLE_KEY ": ";
123+
llvm::interleave(
124+
ignorableFlags, [&out](StringRef str) { out << str; },
125+
[&out] { out << " "; });
126+
out << "\n";
127+
}
128+
}
111129
}
112130

113131
std::string
Lines changed: 23 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,23 @@
1+
// RUN: %empty-directory(%t)
2+
3+
// RUN: %target-swift-frontend -emit-module %s -I %t \
4+
// RUN: -module-name Library -project-name ProjectName \
5+
// RUN: -enable-library-evolution -swift-version 5 \
6+
// RUN: -emit-module-interface-path %t/Library.swiftinterface \
7+
// RUN: -emit-private-module-interface-path %t/Library.private.swiftinterface \
8+
// RUN: -emit-package-module-interface-path %t/Library.package.swiftinterface
9+
10+
// RUN: %target-swift-typecheck-module-from-interface(%t/Library.swiftinterface) -module-name Library
11+
// RUN: %target-swift-typecheck-module-from-interface(%t/Library.private.swiftinterface) -module-name Library
12+
// RUN: %target-swift-typecheck-module-from-interface(%t/Library.package.swiftinterface) -module-name Library
13+
14+
// RUN: %FileCheck %s < %t/Library.swiftinterface --check-prefix CHECK-PUBLIC
15+
// RUN: %FileCheck %s < %t/Library.private.swiftinterface --check-prefix CHECK-NONPUBLIC
16+
// RUN: %FileCheck %s < %t/Library.package.swiftinterface --check-prefix CHECK-NONPUBLIC
17+
18+
// CHECK-PUBLIC-NOT: -project-name
19+
20+
// CHECK-NONPUBLIC: swift-module-flags-ignorable:
21+
// CHECK-NONPUBLIC-SAME: -project-name ProjectName
22+
23+
public func foo() {}

0 commit comments

Comments
 (0)