Skip to content

Commit eefa43c

Browse files
committed
[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 <[email protected]> Approved-By: Tom Tromey <[email protected]> PR cli/30966 Bug: https://sourceware.org/bugzilla/show_bug.cgi?id=30966
1 parent fb8ea9d commit eefa43c

File tree

1 file changed

+16
-2
lines changed

1 file changed

+16
-2
lines changed

gdb/source-cache.c

Lines changed: 16 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -37,6 +37,7 @@
3737
#include <sstream>
3838
#include <srchilite/sourcehighlight.h>
3939
#include <srchilite/langmap.h>
40+
#include <srchilite/settings.h>
4041
#endif
4142

4243
/* The number of source files we'll cache. */
@@ -205,22 +206,35 @@ try_source_highlight (std::string &contents ATTRIBUTE_UNUSED,
205206
return false;
206207

207208
const char *lang_name = get_language_name (lang);
208-
if (lang_name == nullptr)
209-
return false;
210209

211210
/* The global source highlight object, or null if one was
212211
never constructed. This is stored here rather than in
213212
the class so that we don't need to include anything or do
214213
conditional compilation in source-cache.h. */
215214
static srchilite::SourceHighlight *highlighter;
216215

216+
/* The global source highlight language map object. */
217+
static srchilite::LangMap *langmap;
218+
217219
bool styled = false;
218220
try
219221
{
220222
if (highlighter == nullptr)
221223
{
222224
highlighter = new srchilite::SourceHighlight ("esc.outlang");
223225
highlighter->setStyleFile ("esc.style");
226+
227+
const std::string &datadir = srchilite::Settings::retrieveDataDir ();
228+
langmap = new srchilite::LangMap (datadir, "lang.map");
229+
}
230+
231+
std::string detected_lang;
232+
if (lang_name == nullptr)
233+
{
234+
detected_lang = langmap->getMappedFileNameFromFileName (fullname);
235+
if (detected_lang.empty ())
236+
return false;
237+
lang_name = detected_lang.c_str ();
224238
}
225239

226240
std::istringstream input (contents);

0 commit comments

Comments
 (0)