From ffead5131aaa4d80a13e6650a5f5cd37f1332e54 Mon Sep 17 00:00:00 2001
From: David Malcolm <dmalcolm@redhat.com>
Date: Thu, 7 Jul 2016 05:52:37 -0400
Subject: [PATCH 14/25] FIXME: add selftest of cpp_interpret_string

---
 gcc/input.c | 89 ++++++++++++++++++++++++++++++++++++++++++++++++++++++++++---
 1 file changed, 86 insertions(+), 3 deletions(-)

diff --git a/gcc/input.c b/gcc/input.c
index d4e5c52..10d0a90 100644
--- a/gcc/input.c
+++ b/gcc/input.c
@@ -1834,6 +1834,13 @@ test_lexer_string_locations_simple (const line_table_case &case_)
 
   ASSERT_EQ (tok->val.str.len, 12);
 
+  /* Verify that cpp_interpret_string works.  */
+  cpp_string dst_string;
+  bool result = cpp_interpret_string (test.m_parser, &tok->val.str, 1,
+				      &dst_string, CPP_STRING);
+  ASSERT_TRUE (result);
+  ASSERT_STREQ ("0123456789", (const char *)dst_string.text);
+
   /* Verify ranges of individual characters.  This no longer includes the
      quotes.  */
   for (int i = 0; i < 9; i++)
@@ -1867,9 +1874,15 @@ test_lexer_string_locations_hex (const line_table_case &case_)
 
   /* At this point in lexing, the quote characters are treated as part of
      the string (they are stripped off by cpp_interpret_string).  */
-
   ASSERT_EQ (tok->val.str.len, 15);
 
+  /* Verify that cpp_interpret_string works.  */
+  cpp_string dst_string;
+  bool result = cpp_interpret_string (test.m_parser, &tok->val.str, 1,
+				      &dst_string, CPP_STRING);
+  ASSERT_TRUE (result);
+  ASSERT_STREQ ("012345 789", (const char *)dst_string.text);
+
   /* Verify ranges of individual characters.  This no longer includes the
      quotes.  */
   for (int i = 0; i < 5; i++)
@@ -1902,6 +1915,13 @@ test_lexer_string_locations_oct (const line_table_case &case_)
   ASSERT_EQ (tok->type, CPP_STRING);
   ASSERT_TOKEN_AS_TEXT_EQ (test.m_parser, tok, "\"01234\\065 789\"");
 
+  /* Verify that cpp_interpret_string works.  */
+  cpp_string dst_string;
+  bool result = cpp_interpret_string (test.m_parser, &tok->val.str, 1,
+				      &dst_string, CPP_STRING);
+  ASSERT_TRUE (result);
+  ASSERT_STREQ ("012345 789", (const char *)dst_string.text);
+
   /* Verify ranges of individual characters.  This no longer includes the
      quotes.  */
   for (int i = 0; i < 5; i++)
@@ -1933,7 +1953,8 @@ test_lexer_string_locations_ucn4 (const line_table_case &case_)
   ASSERT_EQ (tok->type, CPP_STRING);
   ASSERT_TOKEN_AS_TEXT_EQ (test.m_parser, tok, "\"01234\\u2174\\u2175789\"");
 
-  /* The string should now be encoded in the execution character
+  /* Verify that cpp_interpret_string works.
+     The string should be encoded in the execution character
      set.  Assuming that that is UTF-8, we should have the following:
      -----------  ----  -----  -------  ----------------
      Byte offset  Byte  Octal  Unicode  Source Column(s)
@@ -1954,6 +1975,13 @@ test_lexer_string_locations_ucn4 (const line_table_case &case_)
      13           0x39         '9'      29
      -----------  ----  -----  -------  ---------------.  */
 
+  cpp_string dst_string;
+  bool result = cpp_interpret_string (test.m_parser, &tok->val.str, 1,
+				      &dst_string, CPP_STRING);
+  ASSERT_TRUE (result);
+  ASSERT_STREQ ("01234\342\205\264\342\205\265789",
+		(const char *)dst_string.text);
+
   /* Verify ranges of individual characters.  This no longer includes the
      quotes.
      '01234'.  */
@@ -1992,9 +2020,16 @@ test_lexer_string_locations_ucn8 (const line_table_case &case_)
   ASSERT_TOKEN_AS_TEXT_EQ (test.m_parser, tok,
 			   "\"01234\\U00002174\\U00002175789\"");
 
-  /* The UTF-8 encoding of the string is identical to that from
+  /* Verify that cpp_interpret_string works.
+     The UTF-8 encoding of the string is identical to that from
      the ucn4 testcase above; the only difference is the column
      locations.  */
+  cpp_string dst_string;
+  bool result = cpp_interpret_string (test.m_parser, &tok->val.str, 1,
+				      &dst_string, CPP_STRING);
+  ASSERT_TRUE (result);
+  ASSERT_STREQ ("01234\342\205\264\342\205\265789",
+		(const char *)dst_string.text);
 
   /* Verify ranges of individual characters.  This no longer includes the
      quotes.
@@ -2033,6 +2068,13 @@ test_lexer_string_locations_u8 (const line_table_case &case_)
   ASSERT_EQ (tok->type, CPP_UTF8STRING);
   ASSERT_TOKEN_AS_TEXT_EQ (test.m_parser, tok, "u8\"0123456789\"");
 
+  /* Verify that cpp_interpret_string works.  */
+  cpp_string dst_string;
+  bool result = cpp_interpret_string (test.m_parser, &tok->val.str, 1,
+				      &dst_string, CPP_STRING);
+  ASSERT_TRUE (result);
+  ASSERT_STREQ ("0123456789", (const char *)dst_string.text);
+
   /* Verify ranges of individual characters.  This no longer includes the
      quotes.  */
   for (int i = 0; i < 9; i++)
@@ -2100,6 +2142,15 @@ test_lexer_string_locations_utf8_source (const line_table_case &case_)
     (test.m_parser, tok,
      "\"before \346\226\207\345\255\227\345\214\226\343\201\221 after\"");
 
+  /* Verify that cpp_interpret_string works.  */
+  cpp_string dst_string;
+  bool result = cpp_interpret_string (test.m_parser, &tok->val.str, 1,
+				      &dst_string, CPP_STRING);
+  ASSERT_TRUE (result);
+  ASSERT_STREQ
+    ("before \346\226\207\345\255\227\345\214\226\343\201\221 after",
+     (const char *)dst_string.text);
+
   /* Verify ranges of individual characters.  This no longer includes the
      quotes.
      Assuming that both source and execution encodings are UTF-8, we have
@@ -2125,11 +2176,13 @@ test_lexer_string_locations_concatenation_1 (const line_table_case &case_)
   location_t input_locs[2];
 
   /* Verify that we get the expected tokens back.  */
+  auto_vec <cpp_string> input_strings;
   const cpp_token *tok_a = test.get_token ();
   ASSERT_EQ (tok_a->type, CPP_STRING);
   ASSERT_TOKEN_AS_TEXT_EQ
     (test.m_parser, tok_a,
      "\"01234\"");
+  input_strings.safe_push (tok_a->val.str);
   input_locs[0] = tok_a->src_loc;
 
   const cpp_token *tok_b = test.get_token ();
@@ -2137,8 +2190,17 @@ test_lexer_string_locations_concatenation_1 (const line_table_case &case_)
   ASSERT_TOKEN_AS_TEXT_EQ
     (test.m_parser, tok_b,
      "\"56789\"");
+  input_strings.safe_push (tok_b->val.str);
   input_locs[1] = tok_b->src_loc;
 
+  /* Verify that cpp_interpret_string works.  */
+  cpp_string dst_string;
+  bool result = cpp_interpret_string (test.m_parser,
+				      input_strings.address (), 2,
+				      &dst_string, CPP_STRING);
+  ASSERT_TRUE (result);
+  ASSERT_STREQ ("0123456789", (const char *)dst_string.text);
+
   /* Simulate c-lex.c's lex_string in order to record concatenation.  */
   test.m_concats.record_string_concatenation (2, input_locs);
 
@@ -2167,6 +2229,7 @@ test_lexer_string_locations_concatenation_2 (const line_table_case &case_)
 			 "        \"89\" /* non-str */\n");
   lexer_test test (case_, content);
 
+  auto_vec <cpp_string> input_strings;
   location_t input_locs[5];
 
   /* Verify that we get the expected tokens back.  */
@@ -2174,9 +2237,18 @@ test_lexer_string_locations_concatenation_2 (const line_table_case &case_)
     {
       const cpp_token *tok = test.get_token ();
       ASSERT_EQ (tok->type, CPP_STRING);
+      input_strings.safe_push (tok->val.str);
       input_locs[i] = tok->src_loc;
     }
 
+  /* Verify that cpp_interpret_string works.  */
+  cpp_string dst_string;
+  bool result = cpp_interpret_string (test.m_parser,
+				      input_strings.address (), 5,
+				      &dst_string, CPP_STRING);
+  ASSERT_TRUE (result);
+  ASSERT_STREQ ("0123456789", (const char *)dst_string.text);
+
   /* Simulate c-lex.c's lex_string in order to record concatenation.  */
   test.m_concats.record_string_concatenation (5, input_locs);
 
@@ -2225,6 +2297,8 @@ test_lexer_string_locations_concatenation_3 (const line_table_case &case_)
        .123456789.012345.678.9.0123.456.7.8901.234.5678.90123456789012. */
     = ("        \"01234\"  \"\\x35\"  \"\\066\"  \"789\" /* non-str */\n");
   lexer_test test (case_, content);
+
+  auto_vec <cpp_string> input_strings;
   location_t input_locs[4];
 
   /* Verify that we get the expected tokens back.  */
@@ -2232,9 +2306,18 @@ test_lexer_string_locations_concatenation_3 (const line_table_case &case_)
     {
       const cpp_token *tok = test.get_token ();
       ASSERT_EQ (tok->type, CPP_STRING);
+      input_strings.safe_push (tok->val.str);
       input_locs[i] = tok->src_loc;
     }
 
+  /* Verify that cpp_interpret_string works.  */
+  cpp_string dst_string;
+  bool result = cpp_interpret_string (test.m_parser,
+				      input_strings.address (), 4,
+				      &dst_string, CPP_STRING);
+  ASSERT_TRUE (result);
+  ASSERT_STREQ ("0123456789", (const char *)dst_string.text);
+
   /* Simulate c-lex.c's lex_string in order to record concatenation.  */
   test.m_concats.record_string_concatenation (4, input_locs);
 
-- 
1.8.5.3

