Skip to content

Commit a6faa5a

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

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
@@ -489,13 +489,16 @@ static bool ShouldIncludeModuleInterfaceArg(const Arg *A) {
489489
return true;
490490
}
491491

492-
static bool ShouldIncludeArgInPackageInterfaceOnly(const Arg *A,
493-
ArgList &Args) {
492+
static bool IsPackageInterfaceFlag(const Arg *A, ArgList &Args) {
494493
return A->getOption().matches(options::OPT_package_name) &&
495494
Args.hasArg(
496495
options::OPT_disable_print_package_name_for_non_package_interface);
497496
}
498497

498+
static bool IsPrivateInterfaceFlag(const Arg *A, ArgList &Args) {
499+
return A->getOption().matches(options::OPT_project_name);
500+
}
501+
499502
/// Save a copy of any flags marked as ModuleInterfaceOption, if running
500503
/// in a mode that is going to emit a .swiftinterface file.
501504
static void SaveModuleInterfaceArgs(ModuleInterfaceOptions &Opts,
@@ -510,14 +513,18 @@ static void SaveModuleInterfaceArgs(ModuleInterfaceOptions &Opts,
510513
};
511514

512515
RenderedInterfaceArgs PublicArgs{};
516+
RenderedInterfaceArgs PrivateArgs{};
513517
RenderedInterfaceArgs PackageArgs{};
514518

515519
auto interfaceArgListForArg = [&](Arg *A) -> ArgStringList & {
516520
bool ignorable =
517521
A->getOption().hasFlag(options::ModuleInterfaceOptionIgnorable);
518-
if (ShouldIncludeArgInPackageInterfaceOnly(A, Args))
522+
if (IsPackageInterfaceFlag(A, Args))
519523
return ignorable ? PackageArgs.Ignorable : PackageArgs.Standard;
520524

525+
if (IsPrivateInterfaceFlag(A, Args))
526+
return ignorable ? PrivateArgs.Ignorable : PrivateArgs.Standard;
527+
521528
return ignorable ? PublicArgs.Ignorable : PublicArgs.Standard;
522529
};
523530

@@ -543,6 +550,7 @@ static void SaveModuleInterfaceArgs(ModuleInterfaceOptions &Opts,
543550
};
544551

545552
updateInterfaceOpts(Opts.PublicFlags, PublicArgs);
553+
updateInterfaceOpts(Opts.PrivateFlags, PrivateArgs);
546554
updateInterfaceOpts(Opts.PackageFlags, PackageArgs);
547555
}
548556

lib/Frontend/ModuleInterfaceSupport.cpp

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

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

7073
// Insert additional -module-alias flags
@@ -101,14 +104,29 @@ static void printToolVersionAndFlagsComment(raw_ostream &out,
101104
}
102105
out << "\n";
103106

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

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

114132
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)