This is the mail archive of the gdb-patches@sourceware.org mailing list for the GDB project.


Index Nav: [Date Index] [Subject Index] [Author Index] [Thread Index]
Message Nav: [Date Prev] [Date Next] [Thread Prev] [Thread Next]
Other format: [Raw text]

[PATCH] Instantiate a single source highlighter


It occurred to me that there's no reason to make a new source
highlighter each time gdb needs to highlight some source code.
Instead, a single one can be created and then simply reused each time.

This patch implements this idea.  Tested on x86-64 Fedora 29.

gdb/ChangeLog
2019-06-18  Tom Tromey  <tromey@adacore.com>

	* source-cache.c (highlighter): New global.
	(source_cache::get_source_lines): Create a highlighter on demand.
---
 gdb/ChangeLog      |  5 +++++
 gdb/source-cache.c | 19 ++++++++++++++++---
 2 files changed, 21 insertions(+), 3 deletions(-)

diff --git a/gdb/source-cache.c b/gdb/source-cache.c
index 2d5b549d971..0fa456116b4 100644
--- a/gdb/source-cache.c
+++ b/gdb/source-cache.c
@@ -33,6 +33,7 @@
 #include <sstream>
 #include <srchilite/sourcehighlight.h>
 #include <srchilite/langmap.h>
+#include <srchilite/parserexception.h>
 #endif
 
 /* The number of source files we'll cache.  */
@@ -43,6 +44,14 @@
 
 source_cache g_source_cache;
 
+/* The global source highlight object, or null if one was never
+   constructed.  This is stored here rather than in the class so that
+   we don't need to include anything or do conditional compilation in
+   source-cache.h.  */
+#ifdef HAVE_SOURCE_HIGHLIGHT
+static srchilite::SourceHighlight *highlighter;
+#endif
+
 /* See source-cache.h.  */
 
 bool
@@ -209,11 +218,15 @@ source_cache::get_source_lines (struct symtab *s, int first_line,
 		     use-after-free.  */
 		  fullname = symtab_to_fullname (s);
 		}
-	      srchilite::SourceHighlight highlighter ("esc.outlang");
-	      highlighter.setStyleFile("esc.style");
+
+	      if (highlighter == nullptr)
+		{
+		  highlighter = new srchilite::SourceHighlight ("esc.outlang");
+		  highlighter->setStyleFile("esc.style");
+		}
 
 	      std::ostringstream output;
-	      highlighter.highlight (input, output, lang_name, fullname);
+	      highlighter->highlight (input, output, lang_name, fullname);
 
 	      source_text result = { fullname, output.str () };
 	      m_source_map.push_back (std::move (result));
-- 
2.20.1


Index Nav: [Date Index] [Subject Index] [Author Index] [Thread Index]
Message Nav: [Date Prev] [Date Next] [Thread Prev] [Thread Next]