Skip to content

Commit

Permalink
gccrs: Add check for no_mangle attribute
Browse files Browse the repository at this point in the history
gcc/rust/ChangeLog:

	* lex/rust-input-source.h: Move constants from here...
	* util/rust-codepoint.h (struct Codepoint): ... to here
	* util/rust-attributes.cc (AttributeChecker::visit): Add check
	* util/rust-unicode.cc (is_ascii_only): New function.
	* util/rust-unicode.h (is_ascii_only): Likewise.
	* backend/rust-mangle.cc (legacy_mangle_name): Use it.
	* util/rust-punycode.cc (extract_basic_string): Likewise.
	* lex/rust-lex.cc (Lexer::parse_byte_char): Likewise.

Signed-off-by: Raiki Tamura <[email protected]>
  • Loading branch information
tamaroning committed Aug 18, 2023
1 parent 87c5cc1 commit a4743cc
Show file tree
Hide file tree
Showing 8 changed files with 32 additions and 6 deletions.
2 changes: 1 addition & 1 deletion gcc/rust/backend/rust-mangle.cc
Original file line number Diff line number Diff line change
Expand Up @@ -87,7 +87,7 @@ legacy_mangle_name (const std::string &name)
i++;
m = "..";
}
else if (c.value < 0x80)
else if (c.is_ascii ())
// ASCII
m.push_back (c.value);
else
Expand Down
3 changes: 0 additions & 3 deletions gcc/rust/lex/rust-input-source.h
Original file line number Diff line number Diff line change
Expand Up @@ -28,9 +28,6 @@ constexpr uint8_t UTF8_BOM1 = 0xEF;
constexpr uint8_t UTF8_BOM2 = 0xBB;
constexpr uint8_t UTF8_BOM3 = 0xBF;

constexpr uint32_t MAX_ASCII_CODEPOINT = 0x7F;
constexpr uint32_t CODEPOINT_INVALID = 0xFFFE;

// Input source wrapper thing.
class InputSource
{
Expand Down
2 changes: 1 addition & 1 deletion gcc/rust/lex/rust-lex.cc
Original file line number Diff line number Diff line change
Expand Up @@ -1734,7 +1734,7 @@ Lexer::parse_byte_char (location_t loc)
// otherwise, get character from direct input character
byte_char = current_char;

if (byte_char.value > 0x7f)
if (!byte_char.is_ascii ())
{
rust_error_at (get_current_location (),
"non-ASCII character in %<byte char%>");
Expand Down
12 changes: 12 additions & 0 deletions gcc/rust/util/rust-attributes.cc
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,7 @@
#include "rust-ast.h"
#include "rust-ast-full.h"
#include "rust-diagnostics.h"
#include "rust-unicode.h"

namespace Rust {
namespace Analysis {
Expand Down Expand Up @@ -623,6 +624,13 @@ AttributeChecker::visit (AST::Function &fun)
name);
};

auto check_function_name = [] (const char *name, const Identifier &ident) {
if (!is_ascii_only (ident.as_string ()))
rust_error_at (ident.get_locus (),
"the %<#[%s]%> attribute requires ASCII identifier",
name);
};

BuiltinAttrDefinition result;
for (auto &attribute : fun.get_outer_attrs ())
{
Expand All @@ -649,6 +657,10 @@ AttributeChecker::visit (AST::Function &fun)
{
check_crate_type (name, attribute);
}
else if (result.name == "no_mangle")
{
check_function_name (name, fun.get_function_name ());
}
}
fun.get_definition ()->accept_vis (*this);
}
Expand Down
4 changes: 4 additions & 0 deletions gcc/rust/util/rust-codepoint.h
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,9 @@

namespace Rust {

constexpr uint32_t MAX_ASCII_CODEPOINT = 0x7F;
constexpr uint32_t CODEPOINT_INVALID = 0xFFFE;

// FIXME: move this to rust-unicode.h?
struct Codepoint
{
Expand All @@ -36,6 +39,7 @@ struct Codepoint

static Codepoint eof () { return Codepoint (UINT32_MAX); }
bool is_eof () const { return value == UINT32_MAX; }
bool is_ascii () const { return value <= MAX_ASCII_CODEPOINT; }

// Returns a C++ string containing string value of codepoint.
std::string as_string ();
Expand Down
2 changes: 1 addition & 1 deletion gcc/rust/util/rust-punycode.cc
Original file line number Diff line number Diff line change
Expand Up @@ -42,7 +42,7 @@ extract_basic_string (const std::vector<Codepoint> &src)
std::string basic_string;
for (auto c : src)
{
if (c.value <= 0x7F)
if (c.is_ascii ())
basic_string += c.as_string ();
}
return basic_string;
Expand Down
10 changes: 10 additions & 0 deletions gcc/rust/util/rust-unicode.cc
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,7 @@
// along with GCC; see the file COPYING3. If not see
// <http://www.gnu.org/licenses/>.

#include "rust-input-source.h"
#include "rust-system.h"
#include "optional.h"
#include "selftest.h"
Expand Down Expand Up @@ -328,6 +329,15 @@ is_numeric (uint32_t codepoint)
return true;
}

bool
is_ascii_only (const std::string &str)
{
for (char c : str)
if (static_cast<uint32_t> (c) > MAX_ASCII_CODEPOINT)
return false;
return true;
}

} // namespace Rust

#if CHECKING_P
Expand Down
3 changes: 3 additions & 0 deletions gcc/rust/util/rust-unicode.h
Original file line number Diff line number Diff line change
Expand Up @@ -62,6 +62,9 @@ class Utf8String
bool
is_alphabetic (uint32_t codepoint);

bool
is_ascii_only (const std::string &str);

bool
is_numeric (uint32_t codepoint);

Expand Down

0 comments on commit a4743cc

Please sign in to comment.