Add translations for various sub-directories
[binutils-gdb.git] / gdb / source-cache.c
blobf08c872ed8c77c19bc2906381ec3a2a994ed8b5c
1 /* Cache of styled source file text
2 Copyright (C) 2018-2024 Free Software Foundation, Inc.
4 This file is part of GDB.
6 This program is free software; you can redistribute it and/or modify
7 it under the terms of the GNU General Public License as published by
8 the Free Software Foundation; either version 3 of the License, or
9 (at your option) any later version.
11 This program is distributed in the hope that it will be useful,
12 but WITHOUT ANY WARRANTY; without even the implied warranty of
13 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
14 GNU General Public License for more details.
16 You should have received a copy of the GNU General Public License
17 along with this program. If not, see <http://www.gnu.org/licenses/>. */
19 #include "source-cache.h"
20 #include "gdbsupport/scoped_fd.h"
21 #include "source.h"
22 #include "cli/cli-style.h"
23 #include "symtab.h"
24 #include "objfiles.h"
25 #include "exec.h"
26 #include "cli/cli-cmds.h"
28 #ifdef HAVE_SOURCE_HIGHLIGHT
29 /* If Gnulib redirects 'open' and 'close' to its replacements
30 'rpl_open' and 'rpl_close' via cpp macros, including <fstream>
31 below with those macros in effect will cause unresolved externals
32 when GDB is linked. Happens, e.g., in the MinGW build. */
33 #undef open
34 #undef close
35 #include <sstream>
36 #include <srchilite/sourcehighlight.h>
37 #include <srchilite/langmap.h>
38 #include <srchilite/settings.h>
39 #endif
41 #if GDB_SELF_TEST
42 #include "gdbsupport/selftest.h"
43 #endif
45 /* The number of source files we'll cache. */
47 #define MAX_ENTRIES 5
49 /* See source-cache.h. */
51 source_cache g_source_cache;
53 /* When this is true we will use the GNU Source Highlight to add styling to
54 source code (assuming the library is available). This is initialized to
55 true (if appropriate) in _initialize_source_cache below. */
57 static bool use_gnu_source_highlight;
59 /* The "maint show gnu-source-highlight enabled" command. */
61 static void
62 show_use_gnu_source_highlight_enabled (struct ui_file *file, int from_tty,
63 struct cmd_list_element *c,
64 const char *value)
66 gdb_printf (file,
67 _("Use of GNU Source Highlight library is \"%s\".\n"),
68 value);
71 /* The "maint set gnu-source-highlight enabled" command. */
73 static void
74 set_use_gnu_source_highlight_enabled (const char *ignore_args,
75 int from_tty,
76 struct cmd_list_element *c)
78 #ifndef HAVE_SOURCE_HIGHLIGHT
79 /* If the library is not available and the user tried to enable use of
80 the library, then disable use of the library, and give an error. */
81 if (use_gnu_source_highlight)
83 use_gnu_source_highlight = false;
84 error (_("the GNU Source Highlight library is not available"));
86 #else
87 /* We (might) have just changed how we style source code, discard any
88 previously cached contents. */
89 forget_cached_source_info ();
90 #endif
93 /* See source-cache.h. */
95 std::string
96 source_cache::get_plain_source_lines (struct symtab *s,
97 const std::string &fullname)
99 scoped_fd desc (open_source_file (s));
100 if (desc.get () < 0)
101 perror_with_name (symtab_to_filename_for_display (s), -desc.get ());
103 struct stat st;
104 if (fstat (desc.get (), &st) < 0)
105 perror_with_name (symtab_to_filename_for_display (s));
107 std::string lines;
108 lines.resize (st.st_size);
109 if (myread (desc.get (), &lines[0], lines.size ()) < 0)
110 perror_with_name (symtab_to_filename_for_display (s));
112 time_t mtime = 0;
113 if (s->compunit ()->objfile () != NULL
114 && s->compunit ()->objfile ()->obfd != NULL)
115 mtime = s->compunit ()->objfile ()->mtime;
116 else if (current_program_space->exec_bfd ())
117 mtime = current_program_space->ebfd_mtime;
119 if (mtime && mtime < st.st_mtime)
120 warning (_("Source file is more recent than executable."));
122 std::vector<off_t> offsets;
123 offsets.push_back (0);
124 for (size_t offset = lines.find ('\n');
125 offset != std::string::npos;
126 offset = lines.find ('\n', offset))
128 ++offset;
129 /* A newline at the end does not start a new line. It would
130 seem simpler to just strip the newline in this function, but
131 then "list" won't print the final newline. */
132 if (offset != lines.size ())
133 offsets.push_back (offset);
136 offsets.shrink_to_fit ();
137 m_offset_cache.emplace (fullname, std::move (offsets));
139 return lines;
142 #ifdef HAVE_SOURCE_HIGHLIGHT
144 /* Return the Source Highlight language name, given a gdb language
145 LANG. Returns NULL if the language is not known. */
147 static const char *
148 get_language_name (enum language lang)
150 switch (lang)
152 case language_c:
153 case language_objc:
154 return "c.lang";
156 case language_cplus:
157 return "cpp.lang";
159 case language_d:
160 return "d.lang";
162 case language_go:
163 return "go.lang";
165 case language_fortran:
166 return "fortran.lang";
168 case language_m2:
169 /* Not handled by Source Highlight. */
170 break;
172 case language_asm:
173 return "asm.lang";
175 case language_pascal:
176 return "pascal.lang";
178 case language_opencl:
179 /* Not handled by Source Highlight. */
180 break;
182 case language_rust:
183 return "rust.lang";
185 case language_ada:
186 return "ada.lang";
188 default:
189 break;
192 return nullptr;
195 #endif /* HAVE_SOURCE_HIGHLIGHT */
197 /* Try to highlight CONTENTS from file FULLNAME in language LANG using
198 the GNU source-highlight library. Return true if highlighting
199 succeeded. */
201 static bool
202 try_source_highlight (std::string &contents ATTRIBUTE_UNUSED,
203 enum language lang ATTRIBUTE_UNUSED,
204 const std::string &fullname ATTRIBUTE_UNUSED)
206 #ifdef HAVE_SOURCE_HIGHLIGHT
207 if (!use_gnu_source_highlight)
208 return false;
210 const char *lang_name = get_language_name (lang);
212 /* The global source highlight object, or null if one was
213 never constructed. This is stored here rather than in
214 the class so that we don't need to include anything or do
215 conditional compilation in source-cache.h. */
216 static srchilite::SourceHighlight *highlighter;
218 /* The global source highlight language map object. */
219 static srchilite::LangMap *langmap;
221 bool styled = false;
224 if (highlighter == nullptr)
226 highlighter = new srchilite::SourceHighlight ("esc.outlang");
227 highlighter->setStyleFile ("esc.style");
229 const std::string &datadir = srchilite::Settings::retrieveDataDir ();
230 langmap = new srchilite::LangMap (datadir, "lang.map");
233 std::string detected_lang;
234 if (lang_name == nullptr)
236 detected_lang = langmap->getMappedFileNameFromFileName (fullname);
237 if (detected_lang.empty ())
238 return false;
239 lang_name = detected_lang.c_str ();
242 std::istringstream input (contents);
243 std::ostringstream output;
244 highlighter->highlight (input, output, lang_name, fullname);
245 contents = std::move (output).str ();
246 styled = true;
248 catch (...)
250 /* Source Highlight will throw an exception if
251 highlighting fails. One possible reason it can fail
252 is if the language is unknown -- which matters to gdb
253 because Rust support wasn't added until after 3.1.8.
254 Ignore exceptions here. */
257 return styled;
258 #else
259 return false;
260 #endif /* HAVE_SOURCE_HIGHLIGHT */
263 #ifdef HAVE_SOURCE_HIGHLIGHT
264 #if GDB_SELF_TEST
265 namespace selftests
267 static void gnu_source_highlight_test ()
269 const std::string prog
270 = ("int\n"
271 "foo (void)\n"
272 "{\n"
273 " return 0;\n"
274 "}\n");
275 const std::string fullname = "test.c";
276 std::string styled_prog;
278 bool res = false;
279 bool saw_exception = false;
280 styled_prog = prog;
283 res = try_source_highlight (styled_prog, language_c, fullname);
285 catch (const gdb_exception &e)
287 if (e.reason != RETURN_ERROR)
288 throw;
289 saw_exception = true;
291 catch (...)
293 saw_exception = true;
296 SELF_CHECK (!saw_exception);
297 if (res)
298 SELF_CHECK (prog.size () < styled_prog.size ());
299 else
300 SELF_CHECK (prog == styled_prog);
303 #endif /* GDB_SELF_TEST */
304 #endif /* HAVE_SOURCE_HIGHLIGHT */
306 /* See source-cache.h. */
308 bool
309 source_cache::ensure (struct symtab *s)
311 std::string fullname = symtab_to_fullname (s);
313 size_t size = m_source_map.size ();
314 for (int i = 0; i < size; ++i)
316 if (m_source_map[i].fullname == fullname)
318 /* This should always hold, because we create the file offsets
319 when reading the file. */
320 gdb_assert (m_offset_cache.find (fullname)
321 != m_offset_cache.end ());
322 /* Not strictly LRU, but at least ensure that the most
323 recently used entry is always the last candidate for
324 deletion. Note that this property is relied upon by at
325 least one caller. */
326 if (i != size - 1)
327 std::swap (m_source_map[i], m_source_map[size - 1]);
328 return true;
332 std::string contents;
335 contents = get_plain_source_lines (s, fullname);
337 catch (const gdb_exception_error &e)
339 /* If 's' is not found, an exception is thrown. */
340 return false;
343 if (source_styling && gdb_stdout->can_emit_style_escape ()
344 && m_no_styling_files.count (fullname) == 0)
346 bool already_styled
347 = try_source_highlight (contents, s->language (), fullname);
349 if (!already_styled)
351 std::optional<std::string> ext_contents;
352 ext_contents = ext_lang_colorize (fullname, contents);
353 if (ext_contents.has_value ())
355 contents = std::move (*ext_contents);
356 already_styled = true;
360 if (!already_styled)
362 /* Styling failed. Styling can fail for instance for these
363 reasons:
364 - the language is not supported.
365 - the language cannot not be auto-detected from the file name.
366 - no stylers available.
368 Since styling failed, don't try styling the file again after it
369 drops from the cache.
371 Note that clearing the source cache also clears
372 m_no_styling_files. */
373 m_no_styling_files.insert (fullname);
377 source_text result = { std::move (fullname), std::move (contents) };
378 m_source_map.push_back (std::move (result));
380 if (m_source_map.size () > MAX_ENTRIES)
382 auto iter = m_source_map.begin ();
383 m_offset_cache.erase (iter->fullname);
384 m_source_map.erase (iter);
387 return true;
390 /* See source-cache.h. */
392 bool
393 source_cache::get_line_charpos (struct symtab *s,
394 const std::vector<off_t> **offsets)
396 std::string fullname = symtab_to_fullname (s);
398 auto iter = m_offset_cache.find (fullname);
399 if (iter == m_offset_cache.end ())
401 if (!ensure (s))
402 return false;
403 iter = m_offset_cache.find (fullname);
404 /* cache_source_text ensured this was entered. */
405 gdb_assert (iter != m_offset_cache.end ());
408 *offsets = &iter->second;
409 return true;
412 /* A helper function that extracts the desired source lines from TEXT,
413 putting them into LINES_OUT. The arguments are as for
414 get_source_lines. Returns true on success, false if the line
415 numbers are invalid. */
417 static bool
418 extract_lines (const std::string &text, int first_line, int last_line,
419 std::string *lines_out)
421 int lineno = 1;
422 std::string::size_type pos = 0;
423 std::string::size_type first_pos = std::string::npos;
425 while (pos != std::string::npos && lineno <= last_line)
427 std::string::size_type new_pos = text.find ('\n', pos);
429 if (lineno == first_line)
430 first_pos = pos;
432 pos = new_pos;
433 if (lineno == last_line || pos == std::string::npos)
435 /* A newline at the end does not start a new line. */
436 if (first_pos == std::string::npos
437 || first_pos == text.size ())
438 return false;
439 if (pos == std::string::npos)
440 pos = text.size ();
441 else
442 ++pos;
443 *lines_out = text.substr (first_pos, pos - first_pos);
444 return true;
446 ++lineno;
447 ++pos;
450 return false;
453 /* See source-cache.h. */
455 bool
456 source_cache::get_source_lines (struct symtab *s, int first_line,
457 int last_line, std::string *lines)
459 if (first_line < 1 || last_line < 1 || first_line > last_line)
460 return false;
462 if (!ensure (s))
463 return false;
465 return extract_lines (m_source_map.back ().contents,
466 first_line, last_line, lines);
469 /* Implement 'maint flush source-cache' command. */
471 static void
472 source_cache_flush_command (const char *command, int from_tty)
474 forget_cached_source_info ();
475 gdb_printf (_("Source cache flushed.\n"));
478 #if GDB_SELF_TEST
479 namespace selftests
481 static void extract_lines_test ()
483 std::string input_text = "abc\ndef\nghi\njkl\n";
484 std::string result;
486 SELF_CHECK (extract_lines (input_text, 1, 1, &result)
487 && result == "abc\n");
488 SELF_CHECK (!extract_lines (input_text, 2, 1, &result));
489 SELF_CHECK (extract_lines (input_text, 1, 2, &result)
490 && result == "abc\ndef\n");
491 SELF_CHECK (extract_lines ("abc", 1, 1, &result)
492 && result == "abc");
495 #endif
497 void _initialize_source_cache ();
498 void
499 _initialize_source_cache ()
501 add_cmd ("source-cache", class_maintenance, source_cache_flush_command,
502 _("Force gdb to flush its source code cache."),
503 &maintenanceflushlist);
505 /* All the 'maint set|show gnu-source-highlight' sub-commands. */
506 static struct cmd_list_element *maint_set_gnu_source_highlight_cmdlist;
507 static struct cmd_list_element *maint_show_gnu_source_highlight_cmdlist;
509 /* Adds 'maint set|show gnu-source-highlight'. */
510 add_setshow_prefix_cmd ("gnu-source-highlight", class_maintenance,
511 _("Set gnu-source-highlight specific variables."),
512 _("Show gnu-source-highlight specific variables."),
513 &maint_set_gnu_source_highlight_cmdlist,
514 &maint_show_gnu_source_highlight_cmdlist,
515 &maintenance_set_cmdlist,
516 &maintenance_show_cmdlist);
518 /* Adds 'maint set|show gnu-source-highlight enabled'. */
519 add_setshow_boolean_cmd ("enabled", class_maintenance,
520 &use_gnu_source_highlight, _("\
521 Set whether the GNU Source Highlight library should be used."), _("\
522 Show whether the GNU Source Highlight library is being used."),_("\
523 When enabled, GDB will use the GNU Source Highlight library to apply\n\
524 styling to source code lines that are shown."),
525 set_use_gnu_source_highlight_enabled,
526 show_use_gnu_source_highlight_enabled,
527 &maint_set_gnu_source_highlight_cmdlist,
528 &maint_show_gnu_source_highlight_cmdlist);
530 /* Enable use of GNU Source Highlight library, if we have it. */
531 #ifdef HAVE_SOURCE_HIGHLIGHT
532 use_gnu_source_highlight = true;
533 #endif
535 #if GDB_SELF_TEST
536 selftests::register_test ("source-cache", selftests::extract_lines_test);
537 #ifdef HAVE_SOURCE_HIGHLIGHT
538 selftests::register_test ("gnu-source-highlight",
539 selftests::gnu_source_highlight_test);
540 #endif
541 #endif