aboutsummaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorliushuyu <liushuyu011@gmail.com>2022-04-08 04:08:14 -0600
committerliushuyu <liushuyu011@gmail.com>2022-04-13 02:30:46 -0600
commita125901c55ea18dc7aa1d34b5d938d1e8352d5f1 (patch)
tree164f782b120878a326893ae85ffce9480ef3ee33
parent1a7391d97ffef5efb797f3701a82577c7270ce66 (diff)
downloadgcc-a125901c55ea18dc7aa1d34b5d938d1e8352d5f1.zip
gcc-a125901c55ea18dc7aa1d34b5d938d1e8352d5f1.tar.gz
gcc-a125901c55ea18dc7aa1d34b5d938d1e8352d5f1.tar.bz2
rust-session-manager: address more comments
Co-authored-by: Arthur Cohen <arthur.cohen@embecosm.com> Signed-off-by: Zixing Liu <liushuyu011@gmail.com>
-rw-r--r--gcc/rust/rust-session-manager.cc44
-rw-r--r--gcc/rust/rust-session-manager.h10
2 files changed, 32 insertions, 22 deletions
diff --git a/gcc/rust/rust-session-manager.cc b/gcc/rust/rust-session-manager.cc
index 15a02f81..bdab0d8 100644
--- a/gcc/rust/rust-session-manager.cc
+++ b/gcc/rust/rust-session-manager.cc
@@ -85,17 +85,20 @@ infer_crate_name (const std::string &filename)
return crate;
}
-bool
-CompileOptions::validate_crate_name (const std::string &crate_name)
+/* Validate the crate name using the ASCII rules
+ TODO: Support Unicode version of the rules */
+
+static bool
+validate_crate_name (const std::string &crate_name, Error &error)
{
if (crate_name.empty ())
{
- rust_error_at (Location (), "crate name cannot be empty");
+ error = Error (Location (), "crate name cannot be empty");
return false;
}
if (crate_name.length () > kMaxNameLength)
{
- rust_error_at (Location (), "crate name cannot exceed %ld characters",
+ error = Error (Location (), "crate name cannot exceed %ld characters",
kMaxNameLength);
return false;
}
@@ -103,7 +106,7 @@ CompileOptions::validate_crate_name (const std::string &crate_name)
{
if (!(ISALNUM (c) || c == '_'))
{
- rust_error_at (Location (),
+ error = Error (Location (),
"invalid character %<%c%> in crate name: %<%s%>", c,
crate_name.c_str ());
return false;
@@ -399,7 +402,16 @@ Session::handle_option (
case OPT_frust_crate_:
// set the crate name
if (arg != nullptr)
- ret = options.set_crate_name (arg);
+ {
+ auto error = Error (Location (), std::string ());
+ if ((ret = validate_crate_name (arg, error)))
+ options.set_crate_name (arg);
+ else
+ {
+ rust_assert (!error.message.empty ());
+ error.emit_error ();
+ }
+ }
else
ret = false;
break;
@@ -541,17 +553,20 @@ Session::parse_files (int num_files, const char **files)
filename = files[0];
auto crate_name = infer_crate_name (filename);
+ Error error ((Location ()), std::string ());
rust_debug ("inferred crate name: %s", crate_name.c_str ());
- if (!options.set_crate_name (crate_name))
+ if (!validate_crate_name (crate_name, error))
{
// fake a linemapping so that we can show the filename
linemap->start_file (filename, 0);
linemap->start_line (0, 1);
+ error.emit_error ();
rust_inform (linemap->get_location (0),
"crate name inferred from this file");
linemap->stop ();
return;
}
+ options.set_crate_name (crate_name);
}
auto mappings = Analysis::Mappings::get ();
@@ -1202,16 +1217,17 @@ namespace selftest {
void
rust_crate_name_validation_test (void)
{
- ASSERT_TRUE (Rust::CompileOptions::validate_crate_name ("example"));
- ASSERT_TRUE (Rust::CompileOptions::validate_crate_name ("abcdefg_1234"));
- ASSERT_TRUE (Rust::CompileOptions::validate_crate_name ("1"));
+ auto error = Rust::Error (Location (), std::string ());
+ ASSERT_TRUE (Rust::validate_crate_name ("example", error));
+ ASSERT_TRUE (Rust::validate_crate_name ("abcdefg_1234", error));
+ ASSERT_TRUE (Rust::validate_crate_name ("1", error));
// FIXME: The next test does not pass as of current implementation
// ASSERT_TRUE (Rust::CompileOptions::validate_crate_name ("惊吓"));
// NOTE: - is not allowed in the crate name ...
- /*
- ASSERT_FALSE (Rust::CompileOptions::validate_crate_name ("abcdefg-1234"));
- ASSERT_FALSE (Rust::CompileOptions::validate_crate_name ("a+b"));
- ASSERT_FALSE (Rust::CompileOptions::validate_crate_name ("/a+b/")); */
+
+ ASSERT_FALSE (Rust::validate_crate_name ("abcdefg-1234", error));
+ ASSERT_FALSE (Rust::validate_crate_name ("a+b", error));
+ ASSERT_FALSE (Rust::validate_crate_name ("/a+b/", error));
/* Tests for crate name inference */
ASSERT_EQ (Rust::infer_crate_name ("c.rs"), "c");
diff --git a/gcc/rust/rust-session-manager.h b/gcc/rust/rust-session-manager.h
index 2b20486..6fa83d9 100644
--- a/gcc/rust/rust-session-manager.h
+++ b/gcc/rust/rust-session-manager.h
@@ -213,17 +213,11 @@ struct CompileOptions
enable_dump_option (DumpOption::TYPE_RESOLUTION_DUMP);
}
- /* Validate the crate name using the ASCII rules
- TODO: Support Unicode version of the rules */
- static bool validate_crate_name (const std::string &crate_name);
-
- bool set_crate_name (std::string name)
+ void set_crate_name (std::string name)
{
- if (!validate_crate_name (name))
- return false;
+ rust_assert (!name.empty ());
crate_name = std::move (name);
- return true;
}
void set_edition (int raw_edition)