Skip to content

Commit 69a2510

Browse files
committed
Use quoted string for package name input
Print package name flag only in private interface
1 parent 4cbd4a1 commit 69a2510

12 files changed

+153
-16
lines changed

include/swift/AST/DiagnosticsFrontend.def

Lines changed: 0 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -189,9 +189,6 @@ ERROR(error_bad_export_as_name,none,
189189
"export-as name \"%0\" is not a valid identifier",
190190
(StringRef))
191191

192-
ERROR(error_bad_package_name,none,
193-
"package name \"%0\" is not a valid identifier",
194-
(StringRef))
195192
ERROR(error_stdlib_not_found,Fatal,
196193
"unable to load standard library for target '%0'", (StringRef))
197194
ERROR(error_module_alias_invalid_format,none,

include/swift/Basic/QuotedString.h

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -40,6 +40,8 @@ namespace swift {
4040
printAsQuotedString(out, string.Text);
4141
return out;
4242
}
43+
44+
std::string getString();
4345
};
4446
} // end namespace swift
4547

include/swift/Frontend/ModuleInterfaceSupport.h

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -50,6 +50,10 @@ struct ModuleInterfaceOptions {
5050
/// ignored by the earlier version of the compiler.
5151
std::string IgnorableFlags;
5252

53+
/// Ignorable flags that should only be printed in .private.swiftinterface file;
54+
/// e.g. -package-name PACKAGE_ID
55+
std::string PrivateIgnorableFlags;
56+
5357
/// Print for a private swiftinterface file, SPI decls and attributes.
5458
bool PrintPrivateInterfaceContent = false;
5559

include/swift/Option/Options.h

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -42,6 +42,7 @@ namespace options {
4242
SwiftAPIDigesterOption = (1 << 17),
4343
NewDriverOnlyOption = (1 << 18),
4444
ModuleInterfaceOptionIgnorable = (1 << 19),
45+
ModuleInterfaceOptionIgnorablePrivate = (1 << 20),
4546
};
4647

4748
enum ID {

include/swift/Option/Options.td

Lines changed: 6 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -56,6 +56,11 @@ def ModuleInterfaceOption : OptionFlag;
5656
// The option can be safely ignored by the older compiler.
5757
def ModuleInterfaceOptionIgnorable : OptionFlag;
5858

59+
// The option should be written into a .private.swiftinterface module interface file,
60+
// and read/parsed from there when reconstituting a .swiftmodule from it.
61+
// The option can be safely ignored by the older compiler.
62+
def ModuleInterfaceOptionIgnorablePrivate : OptionFlag;
63+
5964
// The option causes the output of a supplementary output, or is the path option
6065
// for a supplementary output. E.g., `-emit-module` and `-emit-module-path`.
6166
def SupplementaryOutput : OptionFlag;
@@ -530,7 +535,7 @@ def module_abi_name : Separate<["-"], "module-abi-name">,
530535
Flags<[FrontendOption, ModuleInterfaceOption]>,
531536
HelpText<"ABI name to use for the contents of this module">;
532537
def package_name : Separate<["-"], "package-name">,
533-
Flags<[FrontendOption, ModuleInterfaceOptionIgnorable]>,
538+
Flags<[FrontendOption, ModuleInterfaceOptionIgnorablePrivate]>,
534539
HelpText<"Name of the package the module belongs to">;
535540
def export_as : Separate<["-"], "export-as">,
536541
Flags<[FrontendOption, ModuleInterfaceOptionIgnorable]>,

lib/Basic/QuotedString.cpp

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -11,6 +11,7 @@
1111
//===----------------------------------------------------------------------===//
1212

1313
#include "llvm/Support/raw_ostream.h"
14+
#include "llvm/ADT/SmallString.h"
1415
#include "swift/Basic/QuotedString.h"
1516

1617
using namespace swift;
@@ -43,3 +44,10 @@ void swift::printAsQuotedString(llvm::raw_ostream &out, llvm::StringRef text) {
4344
}
4445
out << '"';
4546
}
47+
48+
std::string QuotedString::getString() {
49+
llvm::SmallString<128> escapedBuf;
50+
llvm::raw_svector_ostream os(escapedBuf);
51+
swift::printAsQuotedString(os, Text);
52+
return std::string(escapedBuf.str());
53+
}

lib/Frontend/CompilerInvocation.cpp

Lines changed: 20 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -17,6 +17,7 @@
1717
#include "swift/AST/DiagnosticsFrontend.h"
1818
#include "swift/Basic/Feature.h"
1919
#include "swift/Basic/Platform.h"
20+
#include "swift/Basic/QuotedString.h"
2021
#include "swift/Option/Options.h"
2122
#include "swift/Option/SanitizerOptions.h"
2223
#include "swift/Parse/ParseVersion.h"
@@ -401,8 +402,19 @@ static void SaveModuleInterfaceArgs(ModuleInterfaceOptions &Opts,
401402
return;
402403
ArgStringList RenderedArgs;
403404
ArgStringList RenderedArgsIgnorable;
405+
ArgStringList RenderedArgsIgnorablePrivate;
404406
for (auto A : Args) {
405-
if (A->getOption().hasFlag(options::ModuleInterfaceOptionIgnorable)) {
407+
if (A->getOption().hasFlag(options::ModuleInterfaceOptionIgnorablePrivate)) {
408+
StringRef str = A->getValue();
409+
auto flag = A->getOption();
410+
if (flag.getName().startswith("package-name")) {
411+
auto quotedStr = QuotedString(str).getString();
412+
auto quotedArg = Arg(flag, A->getSpelling(), A->getIndex(), quotedStr.data());
413+
quotedArg.render(Args, RenderedArgsIgnorablePrivate);
414+
} else {
415+
A->render(Args, RenderedArgsIgnorablePrivate);
416+
}
417+
} else if (A->getOption().hasFlag(options::ModuleInterfaceOptionIgnorable)) {
406418
A->render(Args, RenderedArgsIgnorable);
407419
} else if (A->getOption().hasFlag(options::ModuleInterfaceOption)) {
408420
A->render(Args, RenderedArgs);
@@ -421,6 +433,12 @@ static void SaveModuleInterfaceArgs(ModuleInterfaceOptions &Opts,
421433
if (FOpts.ModuleName == "_Concurrency")
422434
OS << " -disable-availability-checking";
423435
}
436+
{
437+
llvm::raw_string_ostream OS(Opts.PrivateIgnorableFlags);
438+
interleave(RenderedArgsIgnorablePrivate,
439+
[&](const char *Argument) { PrintArg(OS, Argument, StringRef()); },
440+
[&] { OS << " "; });
441+
}
424442
{
425443
llvm::raw_string_ostream OS(Opts.IgnorableFlags);
426444
interleave(RenderedArgsIgnorable,
@@ -796,10 +814,7 @@ static bool ParseLangArgs(LangOptions &Opts, ArgList &Args,
796814

797815
if (const Arg *A = Args.getLastArg(OPT_package_name)) {
798816
auto pkgName = A->getValue();
799-
if (!Lexer::isIdentifier(pkgName))
800-
Diags.diagnose(SourceLoc(), diag::error_bad_package_name, pkgName);
801-
else
802-
Opts.PackageName = pkgName;
817+
Opts.PackageName = QuotedString(pkgName).getString();
803818
}
804819

805820
if (const Arg *A = Args.getLastArg(OPT_require_explicit_availability_EQ)) {

lib/Frontend/Frontend.cpp

Lines changed: 5 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -26,6 +26,7 @@
2626
#include "swift/Basic/FileTypes.h"
2727
#include "swift/Basic/SourceManager.h"
2828
#include "swift/Basic/Statistic.h"
29+
#include "swift/Basic/QuotedString.h"
2930
#include "swift/Frontend/ModuleInterfaceLoader.h"
3031
#include "swift/Parse/Lexer.h"
3132
#include "swift/SIL/SILModule.h"
@@ -1155,8 +1156,10 @@ ModuleDecl *CompilerInstance::getMainModule() const {
11551156
Invocation.getFrontendOptions().ModuleABIName));
11561157
}
11571158
if (!Invocation.getLangOptions().PackageName.empty()) {
1158-
MainModule->setPackageName(getASTContext().getIdentifier(
1159-
Invocation.getLangOptions().PackageName));
1159+
// Package name input needs to be a quoted string as all unicode chars are allowed
1160+
// including an escape char and a space.
1161+
auto pkgName = QuotedString(Invocation.getLangOptions().PackageName).getString();
1162+
MainModule->setPackageName(getASTContext().getIdentifier(pkgName));
11601163
}
11611164
if (!Invocation.getFrontendOptions().ExportAsName.empty()) {
11621165
MainModule->setExportAsName(getASTContext().getIdentifier(

lib/Frontend/ModuleInterfaceSupport.cpp

Lines changed: 12 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -94,9 +94,18 @@ static void printToolVersionAndFlagsComment(raw_ostream &out,
9494
}
9595
out << "\n";
9696

97-
if (!Opts.IgnorableFlags.empty()) {
98-
out << "// " SWIFT_MODULE_FLAGS_IGNORABLE_KEY ": "
99-
<< Opts.IgnorableFlags << "\n";
97+
auto hasIgnorableFlags = !Opts.IgnorableFlags.empty();
98+
auto hasPrivateIgnorableFlags = Opts.PrintPrivateInterfaceContent && !Opts.PrivateIgnorableFlags.empty();
99+
100+
if (hasIgnorableFlags || hasPrivateIgnorableFlags) {
101+
out << "// " SWIFT_MODULE_FLAGS_IGNORABLE_KEY ": ";
102+
if (hasIgnorableFlags) {
103+
out << Opts.IgnorableFlags;
104+
}
105+
if (hasPrivateIgnorableFlags) {
106+
out << Opts.PrivateIgnorableFlags;
107+
}
108+
out << "\n";
100109
}
101110
}
102111

test/Sema/accessibility_package_inline_interface.swift

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -10,7 +10,6 @@
1010

1111
// RUN: %FileCheck %s -check-prefix CHECK-UTILS < %t/Utils.swiftinterface
1212
// CHECK-UTILS: -module-name Utils -enable-library-evolution
13-
// CHECK-UTILS: swift-module-flags-ignorable: -package-name myLib
1413
// CHECK-UTILS: @usableFromInline
1514
// CHECK-UTILS: package class PackageKlassProto {
1615
// CHECK-UTILS: @usableFromInline
Lines changed: 94 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,94 @@
1+
// RUN: %empty-directory(%t)
2+
// RUN: split-file %s %t
3+
4+
// RUN: %target-swift-frontend -emit-module %t/Utils.swift \
5+
// RUN: -module-name Utils -swift-version 5 -I %t \
6+
// RUN: -package-name myLib
7+
// RUN: -enable-library-evolution \
8+
// RUN: -emit-module-path %t/Utils.swiftmodule \
9+
// RUN: -emit-module-interface-path %t/Utils.swiftinterface \
10+
// RUN: -emit-private-module-interface-path %t/Utils.private.swiftinterface
11+
12+
// RUN: %target-swift-typecheck-module-from-interface(%t/Utils.swiftinterface) -I %t
13+
// RUN: %FileCheck %s --check-prefix=CHECK-PUBLIC < %t/Utils.swiftinterface
14+
15+
// RUN: %target-swift-typecheck-module-from-interface(%t/Utils.private.swiftinterface) -module-name Utils -I %t
16+
// RUN: %FileCheck %s --check-prefix=CHECK-PRIVATE < %t/Utils.private.swiftinterface
17+
18+
// CHECK-PUBLIC: -module-name Utils -enable-library-evolution
19+
// CHECK-PRIVATE: swift-module-flags-ignorable: -package-name myLib
20+
21+
22+
//--- Utils.swift
23+
package protocol PackageProto {
24+
var pkgVar: Double { get set }
25+
func pkgFunc()
26+
}
27+
28+
package class PackageKlass {
29+
package init() {}
30+
package private(set) var pkgVar: Double = 1.0
31+
package func pkgFunc() {}
32+
}
33+
34+
@usableFromInline
35+
package class PackageKlassProto: PackageProto {
36+
@usableFromInline package init() {}
37+
@usableFromInline package var pkgVar = 1.0
38+
package func pkgFunc() {}
39+
}
40+
41+
@usableFromInline
42+
package class PackageKlassForInline {
43+
@usableFromInline package init() {}
44+
@usableFromInline package func foo1() {}
45+
package func foo2() {}
46+
}
47+
48+
protocol InternalProto {
49+
var internalVar: Double { get set }
50+
func internalFunc()
51+
}
52+
class InternalKlass {
53+
init() {}
54+
}
55+
@usableFromInline
56+
class InternalKlassProto: InternalProto {
57+
@usableFromInline init() {}
58+
@usableFromInline var internalVar = 1.0
59+
func internalFunc() {}
60+
}
61+
62+
@usableFromInline
63+
class InternalKlassForInline {
64+
@usableFromInline init() {}
65+
@usableFromInline func bar1() {}
66+
func bar2() {}
67+
}
68+
69+
@inlinable
70+
public func publicFunc() {
71+
let a = PackageKlassProto().pkgVar
72+
let b = InternalKlassProto().internalVar
73+
PackageKlassForInline().foo1()
74+
InternalKlassForInline().bar1()
75+
print(a, b)
76+
}
77+
78+
@inlinable
79+
func internalFunc() {
80+
let a = PackageKlassProto().pkgVar
81+
let b = InternalKlassProto().internalVar
82+
PackageKlassForInline().foo1()
83+
InternalKlassForInline().bar1()
84+
print(a, b)
85+
}
86+
87+
@inlinable
88+
package func packageFunc() {
89+
let a = PackageKlassProto().pkgVar
90+
let b = InternalKlassProto().internalVar
91+
PackageKlassForInline().foo1()
92+
InternalKlassForInline().bar1()
93+
print(a, b)
94+
}

test/diagnostics/package-name-diagnostics.swift

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -18,7 +18,7 @@
1818
// Package name can be same as the module name
1919
// RUN: %target-swift-frontend -module-name Logging -package-name Logging %s -emit-module -emit-module-path %t/Logging.swiftmodule
2020
// RUN: test -f %t/Logging.swiftmodule
21-
21+
2222
// Package name can be a standard library name
2323
// RUN: %target-swift-frontend -module-name Logging -package-name Swift %s -emit-module -emit-module-path %t/Logging.swiftmodule
2424
// RUN: test -f %t/Logging.swiftmodule

0 commit comments

Comments
 (0)