[binutils-gdb] Clean up source file error reporting

Tom Tromey tromey@sourceware.org
Tue Aug 6 14:09:00 GMT 2019


https://sourceware.org/git/gitweb.cgi?p=binutils-gdb.git;h=b08b16c8391bbcd706a4aaa4b09509e7c2b8c676

commit b08b16c8391bbcd706a4aaa4b09509e7c2b8c676
Author: Tom Tromey <tromey@adacore.com>
Date:   Mon Jul 22 15:20:24 2019 -0600

    Clean up source file error reporting
    
    print_source_lines_base reopens the source file every time that a
    source line is to be printed.  However, there's no need to do this so
    frequently -- it's enough to do it when switching source files, and
    otherwise rely on the cache.
    
    The code seems to try to avoid these multiple opens; at a guess I'd
    say something just got confused along the way.
    
    This patch fixes the problem by reorganizing the code both to make it
    more clear, and to ensure that reopens only occur when the "last
    source visited" changes.
    
    gdb/ChangeLog
    2019-08-06  Tom Tromey  <tromey@adacore.com>
    
    	* source.c (last_source_error): Now bool.
    	(print_source_lines_base): Make "noprint" bool.  Only open
    	source file when last_source_visited changes.

Diff:
---
 gdb/ChangeLog |  6 ++++++
 gdb/source.c  | 26 ++++++++++++--------------
 2 files changed, 18 insertions(+), 14 deletions(-)

diff --git a/gdb/ChangeLog b/gdb/ChangeLog
index 7f4c3ba..9bcd47d 100644
--- a/gdb/ChangeLog
+++ b/gdb/ChangeLog
@@ -1,5 +1,11 @@
 2019-08-06  Tom Tromey  <tromey@adacore.com>
 
+	* source.c (last_source_error): Now bool.
+	(print_source_lines_base): Make "noprint" bool.  Only open
+	source file when last_source_visited changes.
+
+2019-08-06  Tom Tromey  <tromey@adacore.com>
+
 	* annotate.c (annotate_source_line): Use g_source_cache.
 	* source-cache.c (source_cache::get_plain_source_lines): Change
 	parameters.  Populate m_offset_cache.
diff --git a/gdb/source.c b/gdb/source.c
index e0050f1..066666c 100644
--- a/gdb/source.c
+++ b/gdb/source.c
@@ -129,7 +129,7 @@ static int first_line_listed;
    Used to prevent repeating annoying "No such file or directories" msgs.  */
 
 static struct symtab *last_source_visited = NULL;
-static int last_source_error = 0;
+static bool last_source_error = false;
 
 /* Return the first line listed by print_source_lines.
    Used by command interpreters to request listing from
@@ -1129,8 +1129,7 @@ static void
 print_source_lines_base (struct symtab *s, int line, int stopline,
 			 print_source_lines_flags flags)
 {
-  scoped_fd desc;
-  int noprint = 0;
+  bool noprint = false;
   int nlines = stopline - line;
   struct ui_out *uiout = current_uiout;
 
@@ -1144,26 +1143,27 @@ print_source_lines_base (struct symtab *s, int line, int stopline,
   if (uiout->test_flags (ui_source_list))
     {
       /* Only prints "No such file or directory" once.  */
-      if ((s != last_source_visited) || (!last_source_error))
+      if (s == last_source_visited)
 	{
-	  last_source_visited = s;
-	  desc = open_source_file (s);
-	  if (desc.get () < 0)
+	  if (last_source_error)
 	    {
-	      last_source_error = desc.get ();
-	      noprint = 1;
+	      flags |= PRINT_SOURCE_LINES_NOERROR;
+	      noprint = true;
 	    }
 	}
       else
 	{
-	  flags |= PRINT_SOURCE_LINES_NOERROR;
-	  noprint = 1;
+	  last_source_visited = s;
+	  scoped_fd desc = open_source_file (s);
+	  last_source_error = desc.get () < 0;
+	  if (last_source_error)
+	    noprint = true;
 	}
     }
   else
     {
       flags |= PRINT_SOURCE_LINES_NOERROR;
-      noprint = 1;
+      noprint = true;
     }
 
   if (noprint)
@@ -1209,8 +1209,6 @@ print_source_lines_base (struct symtab *s, int line, int stopline,
       return;
     }
 
-  last_source_error = 0;
-
   /* If the user requested a sequence of lines that seems to go backward
      (from high to low line numbers) then we don't print anything.  */
   if (stopline <= line)



More information about the Gdb-cvs mailing list