Skip to content

Commit f315e11

Browse files
authored
Merge pull request #62707 from apple/es-pkg-err
Add error checks for input to -package-name Resolves rdar://103531208
2 parents 0bdc84b + 3c5ea2e commit f315e11

File tree

3 files changed

+31
-2
lines changed

3 files changed

+31
-2
lines changed

include/swift/AST/DiagnosticsFrontend.def

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -180,6 +180,12 @@ ERROR(error_stdlib_module_name,none,
180180
"module name \"%0\" is reserved for the standard library"
181181
"%select{|; use -module-name flag to specify an alternate name}1",
182182
(StringRef, bool))
183+
ERROR(error_bad_package_name,none,
184+
"package name \"%0\" is not a valid identifier",
185+
(StringRef))
186+
ERROR(error_stdlib_package_name,none,
187+
"package name \"%0\" is reserved for the standard library",
188+
(StringRef))
183189
ERROR(error_stdlib_not_found,Fatal,
184190
"unable to load standard library for target '%0'", (StringRef))
185191
ERROR(error_module_alias_invalid_format,none,

lib/Frontend/ArgsToFrontendOptionsConverter.cpp

Lines changed: 9 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -269,8 +269,15 @@ bool ArgsToFrontendOptionsConverter::convert(
269269
if (const Arg *A = Args.getLastArg(OPT_module_link_name))
270270
Opts.ModuleLinkName = A->getValue();
271271

272-
if (const Arg *A = Args.getLastArg(OPT_package_name))
273-
Opts.PackageName = A->getValue();
272+
if (const Arg *A = Args.getLastArg(OPT_package_name)) {
273+
auto pkgName = A->getValue();
274+
if (!Lexer::isIdentifier(pkgName))
275+
Diags.diagnose(SourceLoc(), diag::error_bad_package_name, pkgName);
276+
else if (pkgName == STDLIB_NAME)
277+
Diags.diagnose(SourceLoc(), diag::error_stdlib_package_name, pkgName);
278+
else
279+
Opts.PackageName = pkgName;
280+
}
274281

275282
// This must be called after computing module name, module abi name,
276283
// and module link name. If computing module aliases is unsuccessful,
Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,16 @@
1+
// RUN: %empty-directory(%t)
2+
3+
// Package name should have valid characters
4+
// RUN: not %target-swift-frontend -module-name Logging -package-name My-Logging%Pkg %s -emit-module -emit-module-path %t/Logging.swiftmodule 2>&1 | %FileCheck %s -check-prefix CHECK-BAD
5+
// CHECK-BAD: package name "My-Logging%Pkg" is not a valid identifier
6+
7+
// Package name cannot be a standard library name
8+
// RUN: not %target-swift-frontend -module-name Logging -package-name Swift %s -emit-module -emit-module-path %t/Logging.swiftmodule 2>&1 | %FileCheck %s -check-prefix CHECK-STDLIB
9+
// CHECK-STDLIB: package name "Swift" is reserved for the standard library
10+
11+
// Package name can be same as the module name
12+
// RUN: %target-swift-frontend -module-name Logging -package-name Logging %s -emit-module -emit-module-path %t/Logging.swiftmodule
13+
// RUN: test -f %t/Logging.swiftmodule
14+
15+
public func log() {}
16+

0 commit comments

Comments
 (0)