Re: GDB 7.99.91 MinGW compilation error in cli-script.c

Eli Zaretskii
2017-05-24T18:28:49Z

> Cc: brobecker@adacore.com, simon.marchi@polymtl.ca, gdb-patches@sourceware.org
> From: Pedro Alves <palves@redhat.com>
> Date: Tue, 23 May 2017 10:53:47 +0100
>
> >> It'd be useful for the archives if you expanded on which mingw versions
> >> and compilers you tested this on. Also likewise a short comment to
> >> the effect in the code would be likewise handy for future readers
> >
> > OK, will do.

Here's the final version; okay to commit?

diff --git a/gdb/ChangeLog b/gdb/ChangeLog
index a96e71f..d3a251b 100644
--- a/gdb/ChangeLog
+++ b/gdb/ChangeLog
@@ -1,3 +1,10 @@
+2017-05-24 Eli Zaretskii <eliz@gnu.org>
+
+ * common/common-utils.h (REPLACE_TO_STRING) [__MINGW32__]: Define
+ to 1 if std::to_string is not available.
+ (std::to_string) [REPLACE_TO_STRING]: Provide a replacement
+ implementation.
+
2017-05-24 Yao Qi <yao.qi@linaro.org>

* rs6000-tdep.c (gdb_print_insn_powerpc): Remove.
diff --git a/gdb/cli/cli-script.c b/gdb/cli/cli-script.c
index e0e27ef..2e5e397 100644
--- a/gdb/cli/cli-script.c
+++ b/gdb/cli/cli-script.c
@@ -806,7 +818,7 @@ user_args::insert_args (const char *line) const

if (p[4] == 'c')
{
- new_line += std::to_string (m_args.size ());
+ new_line += gdb::to_string (m_args.size ());
line = p + 5;
}
else
diff --git a/gdb/common/common-utils.h b/gdb/common/common-utils.h
index c331f0d..e3cd66e 100644
--- a/gdb/common/common-utils.h
+++ b/gdb/common/common-utils.h
@@ -22,6 +22,7 @@

#include <string>
#include <vector>
+#include <sstream>

/* If possible, define FUNCTION_NAME, a macro containing the name of
the function being defined. Since this macro may not always be
@@ -63,6 +64,47 @@ int xsnprintf (char *str, size_t size, const char *format, ...)
std::string string_printf (const char* fmt, ...)
ATTRIBUTE_PRINTF (1, 2);

+/* Returns a string representation of VAL. Replacement for C++11
+ std::to_string for hosts that lack it. */
+
+namespace gdb {
+
+#define REPLACE_TO_STRING 0
+
+#ifdef __MINGW32__
+/* mingw.org's MinGW runtime before version 5.0 needs the replacement
+ below. Tested with mingwrt-3.22.2 and mingwrt-5.0, and with
+ libstdc++ included with MinGW GCC 5.3.0. */
+# include <_mingw.h>
+/* We test __MINGW64_VERSION_MAJOR to exclude MinGW64, which doesn't
+ need this replacement. */
+# if !defined __MINGW64_VERSION_MAJOR && !defined _GLIBCXX_USE_C99
+# undef REPLACE_TO_STRING
+# define REPLACE_TO_STRING 1
+# endif
+#endif
+
+#if REPLACE_TO_STRING
+
+template <class T>
+inline std::string
+to_string (const T &val)
+{
+ std::stringstream ss;
+
+ ss << val;
+ return ss.str ();
+}
+
+#else /* !REPLACE_TO_STRING */
+
+using std::to_string;
+
+#endif
+
+#undef REPLACE_TO_STRING
+}
+
/* Make a copy of the string at PTR with LEN characters
(and add a null character at the end in the copy).
Uses malloc to get the space. Returns the address of the copy. */