Mirror of the gdb-patches mailing list
 help / color / mirror / Atom feed
From: Pedro Alves <palves@redhat.com>
To: Tom Tromey <tromey@adacore.com>, gdb-patches@sourceware.org
Subject: Re: [PATCH] Instantiate a single source highlighter
Date: Tue, 18 Jun 2019 20:20:00 -0000	[thread overview]
Message-ID: <a42f9fe0-28c5-a3f7-c2a9-9dddc071f9c3@redhat.com> (raw)
In-Reply-To: <20190618194053.7515-1-tromey@adacore.com>

On 6/18/19 8:40 PM, Tom Tromey wrote:
> 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.
> 

Assuming we won't need this from different threads anytime soon,
seems fine.  Comments below.

> 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

Should it be a unique_ptr so that valgrind doesn't complain about
it leaking when gdb exits?

> +
>  /* 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");

Preexisting, but missing space before parens.

> +		}

To keep the variable's definition and initialization close by,
I'd add a get_highlighter function:

 static std::unique_ptr<srchilite::SourceHighlight> highlighter;

 static srchilite::SourceHighlight *
 get_highlighter ()
 {
   if (highlighter == nullptr)
     {
       highlighter = new srchilite::SourceHighlight ("esc.outlang");
       highlighter->setStyleFile ("esc.style");
     }
 
   return highlighter.get ();
 }

>  
>  	      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));

Thanks,
Pedro Alves


  parent reply	other threads:[~2019-06-18 20:20 UTC|newest]

Thread overview: 5+ messages / expand[flat|nested]  mbox.gz  Atom feed  top
2019-06-18 19:40 Tom Tromey
2019-06-18 20:00 ` Tom Tromey
2019-06-18 20:20 ` Pedro Alves [this message]
2019-06-18 21:25   ` Philippe Waroquiers
2019-06-19 11:34   ` Tom Tromey

Reply instructions:

You may reply publicly to this message via plain-text email
using any one of the following methods:

* Save the following mbox file, import it into your mail client,
  and reply-to-all from there: mbox

  Avoid top-posting and favor interleaved quoting:
  https://en.wikipedia.org/wiki/Posting_style#Interleaved_style

* Reply using the --to, --cc, and --in-reply-to
  switches of git-send-email(1):

  git send-email \
    --in-reply-to=a42f9fe0-28c5-a3f7-c2a9-9dddc071f9c3@redhat.com \
    --to=palves@redhat.com \
    --cc=gdb-patches@sourceware.org \
    --cc=tromey@adacore.com \
    /path/to/YOUR_REPLY

  https://kernel.org/pub/software/scm/git/docs/git-send-email.html

* If your mail client supports setting the In-Reply-To header
  via mailto: links, try the mailto: link
Be sure your reply has a Subject: header at the top and a blank line before the message body.
This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox