]> git.ipfire.org Git - thirdparty/gcc.git/commitdiff
selftest: introduce class auto_fix_quotes
authorDavid Malcolm <dmalcolm@redhat.com>
Mon, 2 Jul 2018 20:05:21 +0000 (20:05 +0000)
committerDavid Malcolm <dmalcolm@gcc.gnu.org>
Mon, 2 Jul 2018 20:05:21 +0000 (20:05 +0000)
This patch moves a workaround for locale differences from a
selftest in pretty-print.c to selftest.h/c to make it reusable; I need
this for a selftest in a followup patch.

gcc/ChangeLog:
* pretty-print.c (selftest::test_pp_format): Move save and restore
of quotes to class auto_fix_quotes, and add an instance.
* selftest.c: Include "intl.h".
(selftest::auto_fix_quotes::auto_fix_quotes): New ctor.
(selftest::auto_fix_quotes::~auto_fix_quotes): New dtor.
* selftest.h (selftest::auto_fix_quotes): New class.

From-SVN: r262317

gcc/ChangeLog
gcc/pretty-print.c
gcc/selftest.c
gcc/selftest.h

index 0a8395949cf6d92d1f00bce2dd704ef940b6c090..e0b5c65062aa29edb619723f2e97477e48709dbf 100644 (file)
@@ -1,3 +1,12 @@
+2018-07-02  David Malcolm  <dmalcolm@redhat.com>
+
+       * pretty-print.c (selftest::test_pp_format): Move save and restore
+       of quotes to class auto_fix_quotes, and add an instance.
+       * selftest.c: Include "intl.h".
+       (selftest::auto_fix_quotes::auto_fix_quotes): New ctor.
+       (selftest::auto_fix_quotes::~auto_fix_quotes): New dtor.
+       * selftest.h (selftest::auto_fix_quotes): New class.
+
 2018-07-02  Richard Henderson  <richard.henderson@linaro.org>
 
        * config/aarch64/aarch64-protos.h, config/aarch64/aarch64.c
index 8babbffda71aaacda5e739b6be10693e81557a52..df3ee811fd591065d20b3599550acb26d4eca658 100644 (file)
@@ -2102,10 +2102,7 @@ test_pp_format ()
 {
   /* Avoid introducing locale-specific differences in the results
      by hardcoding open_quote and close_quote.  */
-  const char *old_open_quote = open_quote;
-  const char *old_close_quote = close_quote;
-  open_quote = "`";
-  close_quote = "'";
+  auto_fix_quotes fix_quotes;
 
   /* Verify that plain text is passed through unchanged.  */
   assert_pp_format (SELFTEST_LOCATION, "unformatted", "unformatted");
@@ -2187,10 +2184,6 @@ test_pp_format ()
   assert_pp_format (SELFTEST_LOCATION, "item 3 of 7", "item %i of %i", 3, 7);
   assert_pp_format (SELFTEST_LOCATION, "problem with `bar' at line 10",
                    "problem with %qs at line %i", "bar", 10);
-
-  /* Restore old values of open_quote and close_quote.  */
-  open_quote = old_open_quote;
-  close_quote = old_close_quote;
 }
 
 /* Run all of the selftests within this file.  */
index 27de9a41b794688952004fdd17808ade64896ca9..dc90557d79d3bda05d2cffade88071fbcb7a25c0 100644 (file)
@@ -21,6 +21,7 @@ along with GCC; see the file COPYING3.  If not see
 #include "system.h"
 #include "coretypes.h"
 #include "selftest.h"
+#include "intl.h"
 
 #if CHECKING_P
 
@@ -192,6 +193,25 @@ temp_source_file::temp_source_file (const location &loc,
   fclose (out);
 }
 
+/* Avoid introducing locale-specific differences in the results
+   by hardcoding open_quote and close_quote.  */
+
+auto_fix_quotes::auto_fix_quotes ()
+{
+  m_saved_open_quote = open_quote;
+  m_saved_close_quote = close_quote;
+  open_quote = "`";
+  close_quote = "'";
+}
+
+/* Restore old values of open_quote and close_quote.  */
+
+auto_fix_quotes::~auto_fix_quotes ()
+{
+  open_quote = m_saved_open_quote;
+  close_quote = m_saved_close_quote;
+}
+
 /* Read the contents of PATH into memory, returning a 0-terminated buffer
    that must be freed by the caller.
    Fail (and abort) if there are any problems, with LOC as the reported
index d66fb93d1a59a7ec88116186b63d1e9e2c9ca4b7..54fc488d8459cfd046652ffd59be58877cd34eef 100644 (file)
@@ -113,6 +113,26 @@ class temp_source_file : public named_temp_file
                    const char *content);
 };
 
+/* RAII-style class for avoiding introducing locale-specific differences
+   in strings containing localized quote marks, by temporarily overriding
+   the "open_quote" and "close_quote" globals to something hardcoded.
+
+   Specifically, the C locale's values are used:
+   - open_quote becomes "`"
+   - close_quote becomes "'"
+   for the lifetime of the object.  */
+
+class auto_fix_quotes
+{
+ public:
+  auto_fix_quotes ();
+  ~auto_fix_quotes ();
+
+ private:
+  const char *m_saved_open_quote;
+  const char *m_saved_close_quote;
+};
+
 /* Various selftests involving location-handling require constructing a
    line table and one or more line maps within it.