]> git.ipfire.org Git - thirdparty/gcc.git/commitdiff
gccrs: Add check for no_mangle attribute
authorRaiki Tamura <tamaron1203@gmail.com>
Fri, 18 Aug 2023 04:00:47 +0000 (13:00 +0900)
committerArthur Cohen <arthur.cohen@embecosm.com>
Tue, 16 Jan 2024 18:04:30 +0000 (19:04 +0100)
gcc/rust/ChangeLog:

* lex/rust-input-source.h: Move constants from here...
* util/rust-codepoint.h (struct Codepoint): ... to here
* util/rust-attributes.cc (check_no_mangle_function): New function.
(AttributeChecker::visit): Use it.
* 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 <tamaron1203@gmail.com>
gcc/rust/backend/rust-mangle.cc
gcc/rust/lex/rust-input-source.h
gcc/rust/lex/rust-lex.cc
gcc/rust/util/rust-attributes.cc
gcc/rust/util/rust-codepoint.h
gcc/rust/util/rust-punycode.cc
gcc/rust/util/rust-unicode.cc
gcc/rust/util/rust-unicode.h

index 248d69b72a72b923585e9800dd3613323a16f2fc..6ca7e917f84b05a0df8a9c38d29496b4d1fa8406 100644 (file)
@@ -90,7 +90,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
index 32261a05cae34aa9207eedb339c05c3c93f12e85..03bf43b5b3b480afe2c8ab74d1d368bfbcf1c351 100644 (file)
@@ -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
 {
index cd966dd8e5cfe87f65d5c29b71a1195bd8151b17..3883f2379fc682769f06c4e9c640f2f972582a9e 100644 (file)
@@ -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%>");
index 33097a84e1f093c8d69660ce4e6ffebefbedd514..5a91e9da90f80370a0295a9555f125e1fc104fa2 100644 (file)
@@ -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 {
@@ -612,6 +613,22 @@ AttributeChecker::visit (AST::UseDeclaration &declaration)
   check_proc_macro_non_function (declaration.get_outer_attrs ());
 }
 
+static void
+check_no_mangle_function (const AST::Attribute &attribute,
+                         const AST::Function &fun)
+{
+  if (attribute.has_attr_input ())
+    {
+      rust_error_at (attribute.get_locus (), ErrorCode::E0754,
+                    "malformed %<no_mangle%> attribute input");
+      rust_inform (attribute.get_locus (),
+                  "must be of the form: %<#[no_mangle]%>");
+    }
+  if (!is_ascii_only (fun.get_function_name ().as_string ()))
+    rust_error_at (fun.get_function_name ().get_locus (),
+                  "the %<#[no_mangle]%> attribute requires ASCII identifier");
+}
+
 void
 AttributeChecker::visit (AST::Function &fun)
 {
@@ -649,6 +666,8 @@ AttributeChecker::visit (AST::Function &fun)
        {
          check_crate_type (name, attribute);
        }
+      else if (result.name == "no_mangle")
+       check_no_mangle_function (attribute, fun);
     }
   fun.get_definition ()->accept_vis (*this);
 }
index 755c8373383df761e8b2f0dc7f8c8d0fe94cc585..a75e99e7c0cefea6861a25da272fb6cfc23885d8 100644 (file)
@@ -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
 {
@@ -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 ();
index 8da724cc65e93c1860aa6b5f4f8568d652f98b31..a9c8cbfc5fca7e169fcf65cfbb787fcf46c7ab53 100644 (file)
@@ -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;
index 95653cb760db17451eafbfd75d8f9ce7ec4ddbea..999ecb042ca3c5b8ed64bf18b5ed7648ab68727e 100644 (file)
@@ -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"
@@ -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
index becf6fb6a0c76b0c2dca3c4b2c77327f79488402..2538436797fbae6182666aa4e74e6c65fcba0a98 100644 (file)
@@ -62,6 +62,9 @@ public:
 bool
 is_alphabetic (uint32_t codepoint);
 
+bool
+is_ascii_only (const std::string &str);
+
 bool
 is_numeric (uint32_t codepoint);