Skip to content

Commit

Permalink
gccrs: Check function names with no_mangle.
Browse files Browse the repository at this point in the history
gcc/rust/ChangeLog:

	* backend/rust-compile-base.cc (HIRCompileBase::setup_fndecl): Add parameter.
	* backend/rust-compile-base.h: Likewise.
	* backend/rust-compile-implitem.cc (CompileTraitItem::visit): Change type of parameter.
	* backend/rust-compile-item.cc (CompileItem::visit): Likewise.
	* lex/rust-input-source.h: Move constants to rust-codepoint.h
	* util/rust-codepoint.h (struct Codepoint): Add is_ascii method.
	* util/rust-punycode.cc (extract_basic_string): Use it.
	* lex/rust-lex.cc (Lexer::parse_byte_char): Likewise.
	* backend/rust-mangle.cc (legacy_mangle_name): Likewise.
	* util/rust-unicode.cc (is_ascii_only): New function.
	* util/rust-unicode.h (is_ascii_only): Likewise.

Signed-off-by: Raiki Tamura <[email protected]>
  • Loading branch information
tamaroning committed Aug 13, 2023
1 parent 87c5cc1 commit fa611cb
Show file tree
Hide file tree
Showing 11 changed files with 50 additions and 23 deletions.
28 changes: 19 additions & 9 deletions gcc/rust/backend/rust-compile-base.cc
Original file line number Diff line number Diff line change
Expand Up @@ -30,6 +30,7 @@
#include "rust-hir-path-probe.h"
#include "rust-type-util.h"
#include "rust-compile-implitem.h"
#include "rust-unicode.h"

#include "fold-const.h"
#include "stringpool.h"
Expand All @@ -46,8 +47,9 @@ bool inline should_mangle_item (const tree fndecl)
}

void
HIRCompileBase::setup_fndecl (tree fndecl, bool is_main_entry_point,
bool is_generic_fn, HIR::Visibility &visibility,
HIRCompileBase::setup_fndecl (tree fndecl, const Rust::Identifier &fn_name,
bool is_main_entry_point, bool is_generic_fn,
HIR::Visibility &visibility,
const HIR::FunctionQualifiers &qualifiers,
const AST::AttrVec &attrs)
{
Expand Down Expand Up @@ -101,7 +103,7 @@ HIRCompileBase::setup_fndecl (tree fndecl, bool is_main_entry_point,
}
else if (no_mangle)
{
handle_no_mangle_attribute_on_fndecl (fndecl, attr);
handle_no_mangle_attribute_on_fndecl (fndecl, attr, fn_name);
}
}
}
Expand Down Expand Up @@ -151,7 +153,7 @@ HIRCompileBase::handle_link_section_attribute_on_fndecl (

void
HIRCompileBase::handle_no_mangle_attribute_on_fndecl (
tree fndecl, const AST::Attribute &attr)
tree fndecl, const AST::Attribute &attr, const Rust::Identifier &fn_name)
{
if (attr.has_attr_input ())
{
Expand All @@ -160,6 +162,13 @@ HIRCompileBase::handle_no_mangle_attribute_on_fndecl (
return;
}

if (!is_ascii_only (fn_name.as_string ()))
{
rust_error_at (fn_name.get_locus (), ErrorCode::E0754,
"attribute %<no_mangle%> requires ASCII identifier");
return;
}

DECL_ATTRIBUTES (fndecl) = tree_cons (get_identifier ("no_mangle"), NULL_TREE,
DECL_ATTRIBUTES (fndecl));
}
Expand Down Expand Up @@ -534,7 +543,7 @@ HIRCompileBase::compile_function_body (tree fndecl,

tree
HIRCompileBase::compile_function (
const std::string &fn_name, HIR::SelfParam &self_param,
const Rust::Identifier &fn_name, HIR::SelfParam &self_param,
std::vector<HIR::FunctionParam> &function_params,
const HIR::FunctionQualifiers &qualifiers, HIR::Visibility &visibility,
AST::AttrVec &outer_attrs, location_t locus, HIR::BlockExpr *function_body,
Expand All @@ -545,15 +554,16 @@ HIRCompileBase::compile_function (
= canonical_path->get () + fntype->subst_as_string ();

// we don't mangle the main fn since we haven't implemented the main shim
bool is_main_fn = fn_name.compare ("main") == 0;
std::string asm_name = fn_name;
bool is_main_fn = fn_name.as_string ().compare ("main") == 0;
std::string asm_name = fn_name.as_string ();

unsigned int flags = 0;
tree fndecl = ctx->get_backend ()->function (compiled_fn_type, ir_symbol_name,
"" /* asm_name */, flags, locus);

setup_fndecl (fndecl, is_main_fn, fntype->has_subsititions_defined (),
visibility, qualifiers, outer_attrs);
setup_fndecl (fndecl, fn_name, is_main_fn,
fntype->has_subsititions_defined (), visibility, qualifiers,
outer_attrs);
setup_abi_options (fndecl, qualifiers.get_abi ());

// conditionally mangle the function name
Expand Down
13 changes: 8 additions & 5 deletions gcc/rust/backend/rust-compile-base.h
Original file line number Diff line number Diff line change
Expand Up @@ -92,7 +92,8 @@ class HIRCompileBase
const Resolver::CanonicalPath *canonical_path,
HIR::Expr *const_value_expr, location_t locus);

tree compile_function (const std::string &fn_name, HIR::SelfParam &self_param,
tree compile_function (const Rust::Identifier &fn_name,
HIR::SelfParam &self_param,
std::vector<HIR::FunctionParam> &function_params,
const HIR::FunctionQualifiers &qualifiers,
HIR::Visibility &visibility, AST::AttrVec &outer_attrs,
Expand All @@ -102,8 +103,9 @@ class HIRCompileBase

static tree unit_expression (Context *ctx, location_t locus);

static void setup_fndecl (tree fndecl, bool is_main_entry_point,
bool is_generic_fn, HIR::Visibility &visibility,
static void setup_fndecl (tree fndecl, const Rust::Identifier &fn_name,
bool is_main_entry_point, bool is_generic_fn,
HIR::Visibility &visibility,
const HIR::FunctionQualifiers &qualifiers,
const AST::AttrVec &attrs);

Expand All @@ -123,8 +125,9 @@ class HIRCompileBase
handle_deprecated_attribute_on_fndecl (tree fndecl,
const AST::Attribute &attr);

static void handle_no_mangle_attribute_on_fndecl (tree fndecl,
const AST::Attribute &attr);
static void
handle_no_mangle_attribute_on_fndecl (tree fndecl, const AST::Attribute &attr,
const Rust::Identifier &fn_name);

static void setup_abi_options (tree fndecl, ABI abi);

Expand Down
4 changes: 2 additions & 2 deletions gcc/rust/backend/rust-compile-implitem.cc
Original file line number Diff line number Diff line change
Expand Up @@ -86,8 +86,8 @@ CompileTraitItem::visit (HIR::TraitItemFunc &func)
auto vis = HIR::Visibility (HIR::Visibility::VisType::PUBLIC);
HIR::TraitFunctionDecl &function = func.get_decl ();
tree fndecl
= compile_function (function.get_function_name ().as_string (),
function.get_self (), function.get_function_params (),
= compile_function (function.get_function_name (), function.get_self (),
function.get_function_params (),
function.get_qualifiers (), vis,
func.get_outer_attrs (), func.get_locus (),
func.get_block_expr ().get (), canonical_path, fntype);
Expand Down
2 changes: 1 addition & 1 deletion gcc/rust/backend/rust-compile-item.cc
Original file line number Diff line number Diff line change
Expand Up @@ -178,7 +178,7 @@ CompileItem::visit (HIR::Function &function)
ctx->push_const_context ();

tree fndecl
= compile_function (function.get_function_name ().as_string (),
= compile_function (function.get_function_name (),
function.get_self_param (),
function.get_function_params (),
function.get_qualifiers (), function.get_visibility (),
Expand Down
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
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 fa611cb

Please sign in to comment.