Instantiate a single source highlighter
authorTom Tromey <tromey@adacore.com>
Tue, 18 Jun 2019 18:18:24 +0000 (12:18 -0600)
committerTom Tromey <tromey@adacore.com>
Wed, 19 Jun 2019 11:34:51 +0000 (05:34 -0600)
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-19  Tom Tromey  <tromey@adacore.com>

* source-cache.c (highlighter): New global.
(source_cache::get_source_lines): Create a highlighter on demand.

gdb/ChangeLog
gdb/source-cache.c

index a2abef1c07694499eddebb605abfad6447a59469..55650974cd951009fa46976caa575da83c8aceb2 100644 (file)
@@ -1,3 +1,8 @@
+2019-06-19  Tom Tromey  <tromey@adacore.com>
+
+       * source-cache.c (highlighter): New global.
+       (source_cache::get_source_lines): Create a highlighter on demand.
+
 2019-06-18  Andrew Burgess  <andrew.burgess@embecosm.com>
 
        * defs.h (deprecated_interactive_hook): Delete declaration.
index 2d5b549d97171014ea3c99fae5f438da45f92f14..86efe83bf9a5679de9a58e2ff0ebe9a3dc37c11e 100644 (file)
@@ -197,6 +197,13 @@ source_cache::get_source_lines (struct symtab *s, int first_line,
          std::ifstream input (fullname);
          if (input.is_open ())
            {
+             /* 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.  */
+             static srchilite::SourceHighlight *highlighter;
+
              if (s->line_charpos == 0)
                {
                  scoped_fd desc (open_source_file_with_line_charpos (s));
@@ -209,11 +216,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));
This page took 0.030013 seconds and 4 git commands to generate.