aboutsummaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorTom de Vries <tdevries@suse.de>2023-10-20 22:23:13 +0200
committerTom de Vries <tdevries@suse.de>2023-10-20 22:23:13 +0200
commiteefa43c9366e3665f1451d6bbfd46b84dea0d15d (patch)
treebed4944b487c08c98e5f89a6ed36e3edd4ca1304
parentfb8ea9d2ca2eafa83390d44d444d0193b32e19dc (diff)
downloadfsf-binutils-gdb-eefa43c9366e3665f1451d6bbfd46b84dea0d15d.zip
fsf-binutils-gdb-eefa43c9366e3665f1451d6bbfd46b84dea0d15d.tar.gz
fsf-binutils-gdb-eefa43c9366e3665f1451d6bbfd46b84dea0d15d.tar.bz2
[gdb/cli] Allow source-highlight to autodetect language
Currently when gdb asks the source-highlight library to highlight a file, it tells it what language file to use. For instance, if gdb learns from the debug info that the file is language_c, the language file "c.lang" is used. This mapping is hardcoded in get_language_name. However, if gdb doesn't know what language file to use, it falls back to using python pygments, and in absence of that, unhighlighted source text. In the case of python pygments, it autodetects which language to use based on the file name. Add the same capability when using the source-highlight library. Tested on x86_64-linux. Verified that it works by: - making get_language_name return nullptr for language_c, and - checking that source-highlight still manages to highlight a hello world. Reviewed-By: Guinevere Larsen <blarsen@redhat.com> Approved-By: Tom Tromey <tom@tromey.com> PR cli/30966 Bug: https://sourceware.org/bugzilla/show_bug.cgi?id=30966
-rw-r--r--gdb/source-cache.c18
1 files changed, 16 insertions, 2 deletions
diff --git a/gdb/source-cache.c b/gdb/source-cache.c
index 99be333..92acb10 100644
--- a/gdb/source-cache.c
+++ b/gdb/source-cache.c
@@ -37,6 +37,7 @@
#include <sstream>
#include <srchilite/sourcehighlight.h>
#include <srchilite/langmap.h>
+#include <srchilite/settings.h>
#endif
/* The number of source files we'll cache. */
@@ -205,8 +206,6 @@ try_source_highlight (std::string &contents ATTRIBUTE_UNUSED,
return false;
const char *lang_name = get_language_name (lang);
- if (lang_name == nullptr)
- return false;
/* The global source highlight object, or null if one was
never constructed. This is stored here rather than in
@@ -214,6 +213,9 @@ try_source_highlight (std::string &contents ATTRIBUTE_UNUSED,
conditional compilation in source-cache.h. */
static srchilite::SourceHighlight *highlighter;
+ /* The global source highlight language map object. */
+ static srchilite::LangMap *langmap;
+
bool styled = false;
try
{
@@ -221,6 +223,18 @@ try_source_highlight (std::string &contents ATTRIBUTE_UNUSED,
{
highlighter = new srchilite::SourceHighlight ("esc.outlang");
highlighter->setStyleFile ("esc.style");
+
+ const std::string &datadir = srchilite::Settings::retrieveDataDir ();
+ langmap = new srchilite::LangMap (datadir, "lang.map");
+ }
+
+ std::string detected_lang;
+ if (lang_name == nullptr)
+ {
+ detected_lang = langmap->getMappedFileNameFromFileName (fullname);
+ if (detected_lang.empty ())
+ return false;
+ lang_name = detected_lang.c_str ();
}
std::istringstream input (contents);