From 967857a7ed455bc3d4c84dd52b15bc96bdf54a7e Mon Sep 17 00:00:00 2001
From: David Malcolm <dmalcolm@redhat.com>
Date: Thu, 29 Sep 2016 19:46:49 -0400
Subject: [PATCH 03/42] read-md.c: track column numbers

This patch adds rudimentary column-number tracking to read-md.c, to
give more precise locations for messages for problems in .md files
(and in the RTL frontend I'm working on):

../../src/gcc/config/i386/i386.md:1204:22: error: unknown rtx code `define_mood_iterator'
../../src/gcc/config/i386/i386.md:1204:25: note: following context is `PTR'

(note the column numbers)

One wart is restoring the column when:
  unread_char ('\n')
It can handle a single one of these using m_last_line_colno.  More
than one could potentially be handled by using a vec, but that
complicates the dependencies, so I kept it simple by only handling one
of these (hence my use of the word "rudimentary" above).

gcc/ChangeLog:
	* genattrtab.c (make_internal_attr): Supply dummy column number to
	file_location ctor.
	(main): Likewise.
	* genoutput.c (init_insn_for_nothing): Likewise.
	* gensupport.c (add_define_attr): Likewise.
	* read-md.c (message_at_1): Print column number.
	(fatal_with_file_and_line): Likewise.
	(rtx_reader::read_char): Track column numbers.
	(rtx_reader::unread_char): Likewise.
	(rtx_reader::rtx_reader): Initialize m_read_md_colno.
	(rtx_reader::handle_include): Stash and restore m_read_md_colno.
	(rtx_reader::handle_file): Initialize m_read_md_colno.
	(rtx_reader::get_current_location): Supply column number to
	file_location ctor.
	* read-md.h (struct file_location): Add field "colno".
	(file_location::file_location): Likewise.
	(rtx_reader::get_colno): New accessor.
	(rtx_reader::m_read_md_colno): New field.
	(rtx_reader::m_last_line_colno): New field.
---
 gcc/genattrtab.c |  4 ++--
 gcc/genoutput.c  |  2 +-
 gcc/gensupport.c |  2 +-
 gcc/read-md.c    | 33 ++++++++++++++++++++++++---------
 gcc/read-md.h    | 16 +++++++++++++---
 5 files changed, 41 insertions(+), 16 deletions(-)

diff --git a/gcc/genattrtab.c b/gcc/genattrtab.c
index c8e166e..ccfcdfd 100644
--- a/gcc/genattrtab.c
+++ b/gcc/genattrtab.c
@@ -4657,7 +4657,7 @@ make_internal_attr (const char *name, rtx value, int special)
   attr->is_numeric = 1;
   attr->is_const = 0;
   attr->is_special = (special & ATTR_SPECIAL) != 0;
-  attr->default_val = get_attr_value (file_location ("<internal>", 0),
+  attr->default_val = get_attr_value (file_location ("<internal>", 0, 0),
 				      value, attr, -2);
 }
 
@@ -5279,7 +5279,7 @@ main (int argc, const char **argv)
       md_rtx_info info;
       info.def = rtx_alloc (DEFINE_ASM_ATTRIBUTES);
       XVEC (info.def, 0) = rtvec_alloc (0);
-      info.loc = file_location ("<internal>", 0);
+      info.loc = file_location ("<internal>", 0, 0);
       info.index = -1;
       gen_insn (&info);
     }
diff --git a/gcc/genoutput.c b/gcc/genoutput.c
index 5909258..f792cb4 100644
--- a/gcc/genoutput.c
+++ b/gcc/genoutput.c
@@ -980,7 +980,7 @@ init_insn_for_nothing (void)
   idata = XCNEW (struct data);
   new (idata) data ();
   idata->name = "*placeholder_for_nothing";
-  idata->loc = file_location ("<internal>", 0);
+  idata->loc = file_location ("<internal>", 0, 0);
   idata_end = &idata->next;
 }
 
diff --git a/gcc/gensupport.c b/gcc/gensupport.c
index 1648c9c..cb74aea 100644
--- a/gcc/gensupport.c
+++ b/gcc/gensupport.c
@@ -446,7 +446,7 @@ add_define_attr (const char *name)
   XEXP (t1, 2) = rtx_alloc (CONST_STRING);
   XSTR (XEXP (t1, 2), 0) = "yes";
   e->data = t1;
-  e->loc = file_location ("built-in", -1);
+  e->loc = file_location ("built-in", -1, -1);
   e->next = define_attr_queue;
   define_attr_queue = e;
 
diff --git a/gcc/read-md.c b/gcc/read-md.c
index f069ba5..e158be5 100644
--- a/gcc/read-md.c
+++ b/gcc/read-md.c
@@ -218,7 +218,7 @@ print_c_condition (const char *cond)
 static void ATTRIBUTE_PRINTF(2,0)
 message_at_1 (file_location loc, const char *msg, va_list ap)
 {
-  fprintf (stderr, "%s:%d: ", loc.filename, loc.lineno);
+  fprintf (stderr, "%s:%d:%d: ", loc.filename, loc.lineno, loc.colno);
   vfprintf (stderr, msg, ap);
   fputc ('\n', stderr);
 }
@@ -274,8 +274,8 @@ fatal_with_file_and_line (const char *msg, ...)
 
   va_start (ap, msg);
 
-  fprintf (stderr, "%s:%d: error: ", rtx_reader_ptr->get_filename (),
-	   rtx_reader_ptr->get_lineno ());
+  fprintf (stderr, "%s:%d:%d: error: ", rtx_reader_ptr->get_filename (),
+	   rtx_reader_ptr->get_lineno (), rtx_reader_ptr->get_colno ());
   vfprintf (stderr, msg, ap);
   putc ('\n', stderr);
 
@@ -294,9 +294,9 @@ fatal_with_file_and_line (const char *msg, ...)
     }
   context[i] = '\0';
 
-  fprintf (stderr, "%s:%d: note: following context is `%s'\n",
+  fprintf (stderr, "%s:%d:%d: note: following context is `%s'\n",
 	   rtx_reader_ptr->get_filename (), rtx_reader_ptr->get_lineno (),
-	   context);
+	   rtx_reader_ptr->get_colno (), context);
 
   va_end (ap);
   exit (1);
@@ -384,7 +384,13 @@ rtx_reader::read_char (void)
 
   ch = getc (m_read_md_file);
   if (ch == '\n')
-    m_read_md_lineno++;
+    {
+      m_read_md_lineno++;
+      m_last_line_colno = m_read_md_colno;
+      m_read_md_colno = 0;
+    }
+  else
+    m_read_md_colno++;
 
   return ch;
 }
@@ -395,7 +401,12 @@ void
 rtx_reader::unread_char (int ch)
 {
   if (ch == '\n')
-    m_read_md_lineno--;
+    {
+      m_read_md_lineno--;
+      m_read_md_colno = m_last_line_colno;
+    }
+  else
+    m_read_md_colno--;
   ungetc (ch, m_read_md_file);
 }
 
@@ -908,6 +919,7 @@ rtx_reader::rtx_reader ()
   m_read_md_file (NULL),
   m_read_md_filename (NULL),
   m_read_md_lineno (0),
+  m_read_md_colno (0),
   m_first_dir_md_include (NULL),
   m_last_dir_md_include_ptr (&m_first_dir_md_include)
 {
@@ -933,7 +945,7 @@ rtx_reader::handle_include (file_location loc)
 {
   const char *filename;
   const char *old_filename;
-  int old_lineno;
+  int old_lineno, old_colno;
   char *pathname;
   FILE *input_file, *old_file;
 
@@ -982,6 +994,7 @@ rtx_reader::handle_include (file_location loc)
   old_file = m_read_md_file;
   old_filename = m_read_md_filename;
   old_lineno = m_read_md_lineno;
+  old_colno = m_read_md_colno;
 
   if (include_callback)
     include_callback (pathname);
@@ -995,6 +1008,7 @@ rtx_reader::handle_include (file_location loc)
   m_read_md_file = old_file;
   m_read_md_filename = old_filename;
   m_read_md_lineno = old_lineno;
+  m_read_md_colno = old_colno;
 
   /* Do not free the pathname.  It is attached to the various rtx
      queue elements.  */
@@ -1011,6 +1025,7 @@ rtx_reader::handle_file ()
   int c;
 
   m_read_md_lineno = 1;
+  m_read_md_colno = 0;
   while ((c = read_skip_spaces ()) != EOF)
     {
       file_location loc = get_current_location ();
@@ -1055,7 +1070,7 @@ rtx_reader::handle_toplevel_file ()
 file_location
 rtx_reader::get_current_location () const
 {
-  return file_location (m_read_md_filename, m_read_md_lineno);
+  return file_location (m_read_md_filename, m_read_md_lineno, m_read_md_colno);
 }
 
 /* Parse a -I option with argument ARG.  */
diff --git a/gcc/read-md.h b/gcc/read-md.h
index 82a628b..a74cc72 100644
--- a/gcc/read-md.h
+++ b/gcc/read-md.h
@@ -25,14 +25,15 @@ along with GCC; see the file COPYING3.  If not see
 /* Records a position in the file.  */
 struct file_location {
   file_location () {}
-  file_location (const char *, int);
+  file_location (const char *, int, int);
 
   const char *filename;
   int lineno;
+  int colno;
 };
 
-inline file_location::file_location (const char *filename_in, int lineno_in)
-  : filename (filename_in), lineno (lineno_in) {}
+inline file_location::file_location (const char *filename_in, int lineno_in, int colno_in)
+: filename (filename_in), lineno (lineno_in), colno (colno_in) {}
 
 /* Holds one symbol or number in the .md file.  */
 struct md_name {
@@ -112,6 +113,7 @@ class rtx_reader
   const char *get_top_level_filename () const { return m_toplevel_fname; }
   const char *get_filename () const { return m_read_md_filename; }
   int get_lineno () const { return m_read_md_lineno; }
+  int get_colno () const { return m_read_md_colno; }
 
  private:
   /* A singly-linked list of filenames.  */
@@ -144,6 +146,14 @@ class rtx_reader
   /* The current line number in m_read_md_file.  */
   int m_read_md_lineno;
 
+  /* The current column number in m_read_md_file.  */
+  int m_read_md_colno;
+
+  /* The column number before the last newline, so that
+     we can handle unread_char ('\n') at least once whilst
+     retaining column information.  */
+  int m_last_line_colno;
+
   /* The first directory to search.  */
   file_name_list *m_first_dir_md_include;
 
-- 
1.8.5.3

