summaryrefslogtreecommitdiff
path: root/gdb/source-cache.h
diff options
context:
space:
mode:
authorTom Tromey <tom@tromey.com>2018-10-09 22:21:05 -0600
committerTom Tromey <tom@tromey.com>2018-12-28 12:49:54 -0700
commit62f29fda90cf1d5a1899f57ef78452471c707fd6 (patch)
tree4d2e519c41de4248f7b4f169f8f379f73f3f0a51 /gdb/source-cache.h
parent4a3045920bbe4e50a0f4920b0fdc4e88ef23015c (diff)
downloadbinutils-gdb-62f29fda90cf1d5a1899f57ef78452471c707fd6.tar.gz
Highlight source code using GNU Source Highlight
This changes gdb to highlight source using GNU Source Highlight, if it is available. This affects the output of the "list" command and also the TUI source window. No new test because I didn't see a way to make it work when Source Highlight is not found. gdb/ChangeLog 2018-12-28 Tom Tromey <tom@tromey.com> * utils.h (can_emit_style_escape): Declare. * utils.c (can_emit_style_escape): No longer static. * cli/cli-style.c (set_style_enabled): New function. (_initialize_cli_style): Use it. * tui/tui-winsource.c (tui_show_source_line): Use tui_puts. (tui_alloc_source_buffer): Change how source lines are allocated. * tui/tui-source.c (copy_source_line): New function. (tui_set_source_content): Use source cache. * tui/tui-io.h (tui_puts): Update. * tui/tui-io.c (tui_puts_internal): Add window parameter. (tui_puts): Likewise. (tui_redisplay_readline): Update. * tui/tui-data.c (free_content_elements): Change how source window contents are freed. * source.c (forget_cached_source_info): Clear the source cache. (print_source_lines_base): Use the source cache. * source-cache.h: New file. * source-cache.c: New file. * configure.ac: Check for GNU Source Highlight library. * configure: Update. * config.in: Update. * Makefile.in (SRCHIGH_LIBS, SRCHIGH_CFLAGS): New variables. (INTERNAL_CFLAGS_BASE): Add SRCHIGH_CFLAGS. (CLIBS): Add SRCHIGH_LIBS. (COMMON_SFILES): Add source-cache.c. (HFILES_NO_SRCDIR): Add source-cache.h.
Diffstat (limited to 'gdb/source-cache.h')
-rw-r--r--gdb/source-cache.h79
1 files changed, 79 insertions, 0 deletions
diff --git a/gdb/source-cache.h b/gdb/source-cache.h
new file mode 100644
index 00000000000..2236d38846b
--- /dev/null
+++ b/gdb/source-cache.h
@@ -0,0 +1,79 @@
+/* Cache of styled source file text
+ Copyright (C) 2018 Free Software Foundation, Inc.
+
+ This file is part of GDB.
+
+ This program is free software; you can redistribute it and/or modify
+ it under the terms of the GNU General Public License as published by
+ the Free Software Foundation; either version 3 of the License, or
+ (at your option) any later version.
+
+ This program is distributed in the hope that it will be useful,
+ but WITHOUT ANY WARRANTY; without even the implied warranty of
+ MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
+ GNU General Public License for more details.
+
+ You should have received a copy of the GNU General Public License
+ along with this program. If not, see <http://www.gnu.org/licenses/>. */
+
+#ifndef SOURCE_CACHE_H
+#define SOURCE_CACHE_H
+
+/* This caches highlighted source text, keyed by the source file's
+ full name. A size-limited LRU cache is used.
+
+ Highlighting depends on the GNU Source Highlight library. When not
+ available, this cache will fall back on reading plain text from the
+ appropriate file. */
+class source_cache
+{
+public:
+
+ source_cache ()
+ {
+ }
+
+ /* Get the source text for the source file in symtab S. FIRST_LINE
+ and LAST_LINE are the first and last lines to return; line
+ numbers are 1-based. If the file cannot be read, false is
+ returned. Otherwise, LINES is set to the desired text. The
+ returned text may include ANSI terminal escapes. */
+ bool get_source_lines (struct symtab *s, int first_line,
+ int last_line, std::string *lines);
+
+ /* Remove all the items from the source cache. */
+ void clear ()
+ {
+ m_source_map.clear ();
+ }
+
+private:
+
+ /* One element in the cache. */
+ struct source_text
+ {
+ /* The full name of the file. */
+ std::string fullname;
+ /* The contents of the file. */
+ std::string contents;
+ };
+
+ /* A helper function for get_source_lines that is used when the
+ source lines are not highlighted. The arguments and return value
+ are as for get_source_lines. */
+ bool get_plain_source_lines (struct symtab *s, int first_line,
+ int last_line, std::string *lines);
+ /* A helper function for get_plain_source_lines that extracts the
+ desired source lines from TEXT, putting them into LINES. The
+ arguments and return value are as for get_source_lines. */
+ bool extract_lines (const struct source_text &text, int first_line,
+ int last_line, std::string *lines);
+
+ /* The contents of the cache. */
+ std::vector<source_text> m_source_map;
+};
+
+/* The global source cache. */
+extern source_cache g_source_cache;
+
+#endif /* SOURCE_CACHE_H */