Skip to content

Add error checks for input package name #62707

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 2 commits into from
Jan 5, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
6 changes: 6 additions & 0 deletions include/swift/AST/DiagnosticsFrontend.def
Original file line number Diff line number Diff line change
Expand Up @@ -180,6 +180,12 @@ ERROR(error_stdlib_module_name,none,
"module name \"%0\" is reserved for the standard library"
"%select{|; use -module-name flag to specify an alternate name}1",
(StringRef, bool))
ERROR(error_bad_package_name,none,
"package name \"%0\" is not a valid identifier",
(StringRef))
ERROR(error_stdlib_package_name,none,
"package name \"%0\" is reserved for the standard library",
(StringRef))
ERROR(error_stdlib_not_found,Fatal,
"unable to load standard library for target '%0'", (StringRef))
ERROR(error_module_alias_invalid_format,none,
Expand Down
11 changes: 9 additions & 2 deletions lib/Frontend/ArgsToFrontendOptionsConverter.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -269,8 +269,15 @@ bool ArgsToFrontendOptionsConverter::convert(
if (const Arg *A = Args.getLastArg(OPT_module_link_name))
Opts.ModuleLinkName = A->getValue();

if (const Arg *A = Args.getLastArg(OPT_package_name))
Opts.PackageName = A->getValue();
if (const Arg *A = Args.getLastArg(OPT_package_name)) {
auto pkgName = A->getValue();
if (!Lexer::isIdentifier(pkgName))
Diags.diagnose(SourceLoc(), diag::error_bad_package_name, pkgName);
else if (pkgName == STDLIB_NAME)
Diags.diagnose(SourceLoc(), diag::error_stdlib_package_name, pkgName);
else
Opts.PackageName = pkgName;
}

// This must be called after computing module name, module abi name,
// and module link name. If computing module aliases is unsuccessful,
Expand Down
16 changes: 16 additions & 0 deletions test/diagnostics/package-name-diagnostics.swift
Original file line number Diff line number Diff line change
@@ -0,0 +1,16 @@
// RUN: %empty-directory(%t)

// Package name should have valid characters
// 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
// CHECK-BAD: package name "My-Logging%Pkg" is not a valid identifier

// Package name cannot be a standard library name
// 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
// CHECK-STDLIB: package name "Swift" is reserved for the standard library

// Package name can be same as the module name
// RUN: %target-swift-frontend -module-name Logging -package-name Logging %s -emit-module -emit-module-path %t/Logging.swiftmodule
// RUN: test -f %t/Logging.swiftmodule

public func log() {}