https://gcc.gnu.org/g:b71fd2afa8316c3a247754963d801c4b7ca0050d
commit b71fd2afa8316c3a247754963d801c4b7ca0050d Author: Pierre-Emmanuel Patry <pierre-emmanuel.pa...@embecosm.com> Date: Wed Oct 4 12:01:44 2023 +0200 Change proc macro entrypoint Change proc macro entrypoint from a fixed constant declaration to a proper generation from the stable crate id. Although the stable crate id is not in use yet, the mechanism to handle it is. gcc/rust/ChangeLog: * expand/rust-proc-macro.cc (CustomDeriveProcMacro::CustomDeriveProcMacro): Remove constant string declaration. (load_macros_array): Add call to the new generation function. (generate_proc_macro_decls_symbol): Add a new function to generate the entrypoint symbol name from the stable crate id. (PROC_MACRO_DECLS_FMT_ARGS): New macro to keep formats arguments in sync between each call. * expand/rust-proc-macro.h (generate_proc_macro_decls_symbol): Add function prototype. Signed-off-by: Pierre-Emmanuel Patry <pierre-emmanuel.pa...@embecosm.com> Diff: --- gcc/rust/expand/rust-proc-macro.cc | 21 ++++++++++++++++++--- gcc/rust/expand/rust-proc-macro.h | 3 +++ 2 files changed, 21 insertions(+), 3 deletions(-) diff --git a/gcc/rust/expand/rust-proc-macro.cc b/gcc/rust/expand/rust-proc-macro.cc index 2fdfcb20b19b..3865b87b75b2 100644 --- a/gcc/rust/expand/rust-proc-macro.cc +++ b/gcc/rust/expand/rust-proc-macro.cc @@ -45,8 +45,6 @@ CustomDeriveProcMacro::CustomDeriveProcMacro (ProcMacro::CustomDerive macro) macro (macro.macro) {} -const std::string PROC_MACRO_DECL_PREFIX = "__gccrs_proc_macro_decls_"; - namespace { ProcMacro::Literal @@ -150,8 +148,10 @@ load_macros_array (std::string path) // FIXME: Add CrateStableId handling, right now all versions may be loaded, // even incompatible ones. + auto symbol_name = generate_proc_macro_decls_symbol (0 /* FIXME */); + return *reinterpret_cast<const ProcMacro::ProcmacroArray **> ( - dlsym (handle, PROC_MACRO_DECL_PREFIX.c_str ())); + dlsym (handle, symbol_name.c_str ())); #else rust_sorry_at (UNDEF_LOCATION, "Procedural macros are not yet supported on windows host"); @@ -175,4 +175,19 @@ load_macros (std::string path) array->macros + array->length); } +std::string +generate_proc_macro_decls_symbol (std::uint32_t stable_crate_id) +{ +#define PROC_MACRO_DECLS_FMT_ARGS \ + "__gccrs_proc_macro_decls_%08x__", stable_crate_id + // Size could be hardcoded since we know the input size but I elected to + // calculate it everytime so we won't have any desync between code and data. + int size = std::snprintf (nullptr, 0, PROC_MACRO_DECLS_FMT_ARGS); + std::vector<char> buf (size + 1); + std::sprintf (buf.data (), PROC_MACRO_DECLS_FMT_ARGS); +#undef PROC_MACRO_DECLS_FMT_ARGS + + return std::string (buf.cbegin (), buf.cend ()); +} + } // namespace Rust diff --git a/gcc/rust/expand/rust-proc-macro.h b/gcc/rust/expand/rust-proc-macro.h index d994ed9bf5e0..6ffaaf6c099f 100644 --- a/gcc/rust/expand/rust-proc-macro.h +++ b/gcc/rust/expand/rust-proc-macro.h @@ -85,6 +85,9 @@ public: const std::vector<ProcMacro::Procmacro> load_macros (std::string path); +std::string +generate_proc_macro_decls_symbol (std::uint32_t stable_crate_id); + } // namespace Rust #endif /* ! RUST_PROC_MACRO_H */