]> git.ipfire.org Git - thirdparty/gcc.git/commitdiff
expand: C++ify proc macro decls generation
authorArthur Cohen <arthur.cohen@embecosm.com>
Fri, 12 Jan 2024 12:48:43 +0000 (13:48 +0100)
committerCohenArthur <arthur.cohen@embecosm.com>
Mon, 15 Jan 2024 10:09:01 +0000 (10:09 +0000)
This uses a stringstream instead of the older, better and more readable
`sprintf` version as the format overflow warning in current GCC is
overzealous and triggered on that code, despite the non-zero allocation.

Even using an unsigned value instead of a signed one for the `size`
variable caused issues, so switching to this is simpler.

gcc/rust/ChangeLog:

* expand/rust-proc-macro.cc: Use stringstream instead of sprintf.
* rust-system.h: Include <iomanip>.

gcc/rust/expand/rust-proc-macro.cc
gcc/rust/rust-system.h

index c5bd87a05a5171e65a574c7744a6e7bffa06c32d..e8618485b71dabe27858b7cfa3f9bbcfca3e6447 100644 (file)
 // along with GCC; see the file COPYING3.  If not see
 // <http://www.gnu.org/licenses/>.
 
+#include "rust-system.h"
 #include "rust-diagnostics.h"
 #include "rust-proc-macro.h"
 #include "rust-session-manager.h"
 #include "rust-lex.h"
 #include "rust-token-converter.h"
 #include "rust-attributes.h"
+
 #ifndef _WIN32
 #include <dlfcn.h>
 #endif
@@ -178,17 +180,11 @@ load_macros (std::string path)
 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;
-  buf.resize (size + 1, '\0');
-  std::sprintf (buf.data (), PROC_MACRO_DECLS_FMT_ARGS);
-#undef PROC_MACRO_DECLS_FMT_ARGS
-
-  return std::string (buf.cbegin (), buf.cend ());
+  std::ostringstream stream;
+  stream << "__gccrs_proc_macro_decls_" << std::setfill ('0') << std::hex
+        << std::setw (8) << stable_crate_id << "__";
+
+  return stream.str ();
 }
 
 } // namespace Rust
index 75fa39826b2f90415dd13ad8e37b1268c5b4db87..6ee4531ebfa62831793477509eaca528845284ae 100644 (file)
@@ -54,6 +54,7 @@
  * before the macro magic of safe-ctype.h, which is included by
  * system.h. */
 #include <iostream>
+#include <iomanip>
 
 #include "system.h"
 #include "ansidecl.h"