Skip to content

Commit 3631430

Browse files
committed
gccrs: Add Unicode check for crate_name attributes
gcc/rust/ChangeLog: * lex/rust-codepoint.h: Add comment * lex/rust-lex.h: New method to get decoded characters * rust-session-manager.cc (validate_crate_name): Modify unicode check (rust_crate_name_validation_test): Add testcases * util/rust-unicode.h (RUST_UNICODE_H): New class Utf8String. (class Utf8String): New class. gcc/testsuite/ChangeLog: * rust/compile/bad-crate-name.rs: Moved to... * rust/compile/bad-crate-name1.rs: ...here. * rust/compile/bad-crate-name2.rs: New test. Signed-off-by: Raiki Tamura <[email protected]>
1 parent 86bfc84 commit 3631430

File tree

6 files changed

+51
-8
lines changed

6 files changed

+51
-8
lines changed

gcc/rust/lex/rust-codepoint.h

+2
Original file line numberDiff line numberDiff line change
@@ -22,6 +22,8 @@
2222
#include "rust-system.h"
2323

2424
namespace Rust {
25+
26+
// FIXME: move this to rust-unicode.h?
2527
struct Codepoint
2628
{
2729
uint32_t value;

gcc/rust/lex/rust-lex.h

+8
Original file line numberDiff line numberDiff line change
@@ -334,6 +334,14 @@ class Lexer
334334
return c;
335335
}
336336
}
337+
338+
tl::optional<std::vector<Codepoint>> get_chars ()
339+
{
340+
if (is_valid ())
341+
return {chars};
342+
else
343+
return tl::nullopt;
344+
}
337345
};
338346

339347
class FileInputSource : public InputSource

gcc/rust/rust-session-manager.cc

+20-8
Original file line numberDiff line numberDiff line change
@@ -42,6 +42,7 @@
4242
#include "rust-early-name-resolver.h"
4343
#include "rust-cfg-strip.h"
4444
#include "rust-expand-visitor.h"
45+
#include "rust-unicode.h"
4546

4647
#include "diagnostic.h"
4748
#include "input.h"
@@ -113,24 +114,32 @@ infer_crate_name (const std::string &filename)
113114
static bool
114115
validate_crate_name (const std::string &crate_name, Error &error)
115116
{
116-
if (crate_name.empty ())
117+
Utf8String utf8_name = {crate_name};
118+
auto uchars = utf8_name.get_chars ();
119+
120+
if (!uchars.has_value ())
121+
{
122+
error = Error (UNDEF_LOCATION, "crate name is not a valid UTF-8 string");
123+
return false;
124+
}
125+
if (uchars.value ().empty ())
117126
{
118127
error = Error (UNDEF_LOCATION, "crate name cannot be empty");
119128
return false;
120129
}
121-
if (crate_name.length () > kMaxNameLength)
130+
if (uchars.value ().size () > kMaxNameLength)
122131
{
123132
error = Error (UNDEF_LOCATION, "crate name cannot exceed %lu characters",
124133
(unsigned long) kMaxNameLength);
125134
return false;
126135
}
127-
for (auto &c : crate_name)
136+
for (Codepoint &c : uchars.value ())
128137
{
129-
if (!(ISALNUM (c) || c == '_'))
138+
if (!(is_alphabetic (c.value) || is_numeric (c.value) || c.value == '_'))
130139
{
131140
error = Error (UNDEF_LOCATION,
132-
"invalid character %<%c%> in crate name: %<%s%>", c,
133-
crate_name.c_str ());
141+
"invalid character %<%s%> in crate name: %<%s%>",
142+
c.as_string ().c_str (), crate_name.c_str ());
134143
return false;
135144
}
136145
}
@@ -1230,13 +1239,16 @@ rust_crate_name_validation_test (void)
12301239
ASSERT_TRUE (Rust::validate_crate_name ("example", error));
12311240
ASSERT_TRUE (Rust::validate_crate_name ("abcdefg_1234", error));
12321241
ASSERT_TRUE (Rust::validate_crate_name ("1", error));
1233-
// FIXME: The next test does not pass as of current implementation
1234-
// ASSERT_TRUE (Rust::CompileOptions::validate_crate_name ("惊吓"));
1242+
ASSERT_TRUE (Rust::validate_crate_name ("クレート", error));
1243+
ASSERT_TRUE (Rust::validate_crate_name ("Sōkrátēs", error));
1244+
12351245
// NOTE: - is not allowed in the crate name ...
12361246

12371247
ASSERT_FALSE (Rust::validate_crate_name ("abcdefg-1234", error));
12381248
ASSERT_FALSE (Rust::validate_crate_name ("a+b", error));
12391249
ASSERT_FALSE (Rust::validate_crate_name ("/a+b/", error));
1250+
ASSERT_FALSE (Rust::validate_crate_name ("😸++", error));
1251+
ASSERT_FALSE (Rust::validate_crate_name ("", error));
12401252

12411253
/* Tests for crate name inference */
12421254
ASSERT_EQ (Rust::infer_crate_name ("c.rs"), "c");

gcc/rust/util/rust-unicode.h

+19
Original file line numberDiff line numberDiff line change
@@ -19,10 +19,29 @@
1919
#ifndef RUST_UNICODE_H
2020
#define RUST_UNICODE_H
2121

22+
#include "optional.h"
2223
#include "rust-system.h"
24+
#include "rust-lex.h"
2325

2426
namespace Rust {
2527

28+
class Utf8String
29+
{
30+
private:
31+
tl::optional<std::vector<Codepoint>> chars;
32+
33+
public:
34+
Utf8String (const std::string &maybe_utf8)
35+
{
36+
Lexer::BufferInputSource input_source = {maybe_utf8, 0};
37+
chars = input_source.get_chars ();
38+
}
39+
40+
// Returns UTF codepoints when string is valid as UTF-8, returns nullopt
41+
// otherwise.
42+
tl::optional<std::vector<Codepoint>> get_chars () const { return chars; }
43+
};
44+
2645
// TODO: add function nfc_normalize
2746

2847
bool
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,2 @@
1+
#![crate_name = "😅"] // { dg-error "invalid character ...." "" }
2+
fn main() {}

0 commit comments

Comments
 (0)