From f8c521b50dba8c988f51b9e3bdf65518be875209 Mon Sep 17 00:00:00 2001
From: David Malcolm <dmalcolm@redhat.com>
Date: Fri, 8 Apr 2016 10:43:28 -0400
Subject: [PATCH 48/55] FIXME: introduce require_char etc

---
 gcc/read-md.c  | 53 ++++++++++++++++++++++++++++++---------------
 gcc/read-md.h  |  3 +++
 gcc/read-rtl.c | 68 ++++++++++++++--------------------------------------------
 3 files changed, 55 insertions(+), 69 deletions(-)

diff --git a/gcc/read-md.c b/gcc/read-md.c
index 0ccd030..2dc88c2 100644
--- a/gcc/read-md.c
+++ b/gcc/read-md.c
@@ -406,6 +406,37 @@ read_skip_spaces (void)
     }
 }
 
+/* Consume the next character, issuing a fatal error if it is not
+   EXPECTED.  */
+
+void require_char (char expected)
+{
+  int ch = read_char ();
+  if (ch != expected)
+    fatal_expected_char (expected, ch);
+}
+
+/* Consume any whitespace, then consume the next non-whitespace
+   character, issuing a fatal error if it is not EXPECTED.  */
+
+void require_char_ws (char expected)
+{
+  int ch = read_skip_spaces ();
+  if (ch != expected)
+    fatal_expected_char (expected, ch);
+}
+
+/* Consume any whitespace, then consume the next word (as per read_name),
+   issuing a fatal error if it is not EXPECTED.  */
+
+void require_word_ws (const char *expected)
+{
+  struct md_name name;
+  read_name (&name);
+  if (strcmp (name.string, expected))
+    fatal_with_file_and_line ("missing '%s'", expected);
+}
+
 /* FIXME.  */
 
 file_location
@@ -706,11 +737,7 @@ read_string (int star_if_braced)
     fatal_with_file_and_line ("expected `\"' or `{', found `%c'", c);
 
   if (saw_paren)
-    {
-      c = read_skip_spaces ();
-      if (c != ')')
-	fatal_expected_char (')', c);
-    }
+    require_char_ws (')');
 
   set_md_ptr_loc (stringbuf, read_md_filename, old_lineno);
   return stringbuf;
@@ -867,9 +894,7 @@ handle_constants (void)
   int c;
   htab_t defs;
 
-  c = read_skip_spaces ();
-  if (c != '[')
-    fatal_expected_char ('[', c);
+  require_char_ws ('[');
 
   /* Disable constant expansion during definition processing.  */
   defs = md_constants;
@@ -885,9 +910,7 @@ handle_constants (void)
       read_name (&value);
       add_constant (defs, xstrdup (name.string), xstrdup (value.string), 0);
 
-      c = read_skip_spaces ();
-      if (c != ')')
-	fatal_expected_char (')', c);
+      require_char_ws (')');
     }
   md_constants = defs;
 }
@@ -949,9 +972,7 @@ handle_enum (file_location loc, bool md_p)
       *slot = def;
     }
 
-  c = read_skip_spaces ();
-  if (c != '[')
-    fatal_expected_char ('[', c);
+  require_char_ws ('[');
 
   while ((c = read_skip_spaces ()) != ']')
     {
@@ -1140,9 +1161,7 @@ rtx_reader::handle_file ()
       else
 	handle_unknown_directive (loc, directive.string);
 
-      c = read_skip_spaces ();
-      if (c != ')')
-	fatal_expected_char (')', c);
+      require_char_ws (')');
     }
   fclose (m_read_md_file);
 }
diff --git a/gcc/read-md.h b/gcc/read-md.h
index 7e6ad93..02a54ac 100644
--- a/gcc/read-md.h
+++ b/gcc/read-md.h
@@ -198,6 +198,9 @@ extern void fatal_with_file_and_line (const char *, ...)
   ATTRIBUTE_PRINTF_1 ATTRIBUTE_NORETURN;
 extern void fatal_expected_char (int, int) ATTRIBUTE_NORETURN;
 extern int read_skip_spaces (void);
+extern void require_char (char expected);
+extern void require_char_ws (char expected);
+extern void require_word_ws (const char *expected);
 extern file_location read_name (struct md_name *);
 extern file_location read_name_or_nil (struct md_name *);
 extern char *read_quoted_string (void);
diff --git a/gcc/read-rtl.c b/gcc/read-rtl.c
index 2429201..653aa97 100644
--- a/gcc/read-rtl.c
+++ b/gcc/read-rtl.c
@@ -762,9 +762,7 @@ read_conditions (void)
 {
   int c;
 
-  c = read_skip_spaces ();
-  if (c != '[')
-    fatal_expected_char ('[', c);
+  require_char_ws ('[');
 
   while ( (c = read_skip_spaces ()) != ']')
     {
@@ -779,14 +777,10 @@ read_conditions (void)
       validate_const_int (name.string);
       value = atoi (name.string);
 
-      c = read_skip_spaces ();
-      if (c != '"')
-	fatal_expected_char ('"', c);
+      require_char_ws ('"');
       expr = read_quoted_string ();
 
-      c = read_skip_spaces ();
-      if (c != ')')
-	fatal_expected_char (')', c);
+      require_char_ws (')');
 
       add_c_test (expr, value);
     }
@@ -911,9 +905,7 @@ read_mapping (struct iterator_group *group, htab_t table)
   read_name (&name);
   m = add_mapping (group, table, name.string);
 
-  c = read_skip_spaces ();
-  if (c != '[')
-    fatal_expected_char ('[', c);
+  require_char_ws ('[');
 
   /* Read each value.  */
   end_ptr = &m->values;
@@ -933,9 +925,7 @@ read_mapping (struct iterator_group *group, htab_t table)
 	  /* A "(name string)" pair.  */
 	  read_name (&name);
 	  string = read_string (false);
-	  c = read_skip_spaces ();
-	  if (c != ')')
-	    fatal_expected_char (')', c);
+	  require_char_ws (')');
 	}
       number = group->find_builtin (name.string);
       end_ptr = add_map_value (end_ptr, number, string);
@@ -1359,9 +1349,7 @@ read_rtx_code (const char *code_name)
 		rtx_reader_ptr->add_fixup_note_insn_basic_block (bb_loc,
 								 return_rtx, i,
 								 bb_idx);
-		c = read_skip_spaces ();
-		if (c != ']')
-		  fatal_expected_char (']', c);
+		require_char_ws (']');
 	      }
 	    else
 	      unread_char (c);
@@ -1379,9 +1367,7 @@ read_rtx_code (const char *code_name)
 		unread_char (c);
 		break;
 	      }
-	    c = read_char ();
-	    if (c != '>')
-	      fatal_expected_char ('>', c);
+	    require_char ('>');
 	    file_location loc = read_name (&name);
 	    rtx_reader_ptr->add_fixup_jump_label (loc, return_rtx, i, name.string);
 #endif /* #ifndef GENERATOR_FILE */
@@ -1427,9 +1413,7 @@ read_rtx_code (const char *code_name)
 	  int list_counter = 0;
 	  rtvec return_vec = NULL_RTVEC;
 
-	  c = read_skip_spaces ();
-	  if (c != '[')
-	    fatal_expected_char ('[', c);
+	  require_char_ws ('[');
 
 	  /* Add expressions to a list, while keeping a count.  */
 	  obstack_init (&vector_stack);
@@ -1562,14 +1546,10 @@ read_rtx_code (const char *code_name)
 #ifndef GENERATOR_FILE
 	/* Strip away the hex.  */
 	{
-	  char ch = read_char ();
-	  if (ch != '[')
-	    fatal_with_file_and_line ("missing '['");
+	  require_char_ws ('[');
 	  read_name (&name);
 	  fprintf (stderr, "FIXME: ignoring `%s'\n", name.string);
-	  ch = read_char ();
-	  if (ch != ']')
-	    fatal_with_file_and_line ("missing ']'");
+	  require_char_ws (']');
 	}
 #endif
 	break;
@@ -1669,12 +1649,7 @@ read_rtx_code (const char *code_name)
       case MEM:
 	{
 	  char ch;
-	  ch = read_char ();
-	  if (ch != ' ')
-	    fatal_with_file_and_line ("missing ' '");
-	  ch = read_char ();
-	  if (ch != '[')
-	    fatal_with_file_and_line ("missing '['");
+	  require_char_ws ('[');
 	  read_name (&name);
 	  MEM_ALIAS_SET (return_rtx) = atoi (name.string);
 	  // FIXME etc...
@@ -1691,17 +1666,11 @@ read_rtx_code (const char *code_name)
       case CODE_LABEL:
 	{
 	  /* Parse LABEL_NUSES.  */
-	  int ch = read_skip_spaces ();
-	  if (ch != '[')
-	    fatal_with_file_and_line ("missing '['");
+	  require_char_ws ('[');
 	  read_name (&name);
 	  LABEL_NUSES (return_rtx) = atoi (name.string);
-	  read_name (&name);
-	  if (strcmp (name.string, "uses"))
-	    fatal_with_file_and_line ("missing 'uses'");
-	  ch = read_char ();
-	  if (ch != ']')
-	    fatal_with_file_and_line ("missing ']'");
+	  require_word_ws ("uses");
+	  require_char_ws (']');
 	  /* TODO: parse LABEL_KIND.  */
 	  /* For now, skip until closing ')'.  */
 	  do
@@ -1788,12 +1757,9 @@ static rtx
 read_nested_rtx (void)
 {
   struct md_name name;
-  int c;
   rtx return_rtx;
 
-  c = read_skip_spaces ();
-  if (c != '(')
-    fatal_expected_char ('(', c);
+  require_char_ws ('(');
 
   read_name (&name);
   if (strcmp (name.string, "nil") == 0)
@@ -1801,9 +1767,7 @@ read_nested_rtx (void)
   else
     return_rtx = read_rtx_code (name.string);
 
-  c = read_skip_spaces ();
-  if (c != ')')
-    fatal_expected_char (')', c);
+  require_char_ws (')');
 
   return return_rtx;
 }
-- 
1.8.5.3

