aboutsummaryrefslogtreecommitdiff
path: root/gcc/rust/rust-session-manager.h
diff options
context:
space:
mode:
Diffstat (limited to 'gcc/rust/rust-session-manager.h')
-rw-r--r--gcc/rust/rust-session-manager.h40
1 files changed, 39 insertions, 1 deletions
diff --git a/gcc/rust/rust-session-manager.h b/gcc/rust/rust-session-manager.h
index 8fcc5dd..d4fae27 100644
--- a/gcc/rust/rust-session-manager.h
+++ b/gcc/rust/rust-session-manager.h
@@ -41,6 +41,8 @@ namespace HIR {
struct Crate;
}
+const size_t kMaxNameLength = 64;
+
/* Data related to target, most useful for conditional compilation and
* whatever. */
struct TargetOptions
@@ -212,9 +214,45 @@ struct CompileOptions
enable_dump_option (DumpOption::TYPE_RESOLUTION_DUMP);
}
+ /* Validate the crate name using the ASCII rules
+ TODO: Support Unicode version of the rules */
+ bool validate_crate_name (const std::string &crate_name)
+ {
+ if (crate_name.empty ())
+ {
+ rust_error_at (Location (), "crate name cannot be empty");
+ return false;
+ }
+ if (crate_name.length () > kMaxNameLength)
+ {
+ rust_error_at (Location (), "crate name cannot exceed %ld characters",
+ kMaxNameLength);
+ return false;
+ }
+ for (auto &c : crate_name)
+ {
+ if (!(ISALNUM (c) || c == '_' || c == '-'))
+ {
+ rust_error_at (Location (),
+ "invalid character %<%c%> in crate name: %<%s%>", c,
+ crate_name.c_str ());
+ return false;
+ }
+ }
+ return true;
+ }
+
bool set_crate_name (std::string name)
{
- // TODO: validate the crate name?
+ if (!validate_crate_name (name))
+ return false;
+
+ /* Replace all the '-' symbols with '_' per Rust rules */
+ for (auto &c : name)
+ {
+ if (c == '-')
+ c = '_';
+ }
crate_name = std::move (name);
return true;
}