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 #ifndef SOURCE_CACHE_H
20 #define SOURCE_CACHE_H
22 #include <unordered_map>
23 #include <unordered_set>
25 /* This caches two things related to source files.
27 First, it caches highlighted source text, keyed by the source
28 file's full name. A size-limited LRU cache is used.
30 Highlighting depends on the GNU Source Highlight library. When not
31 available or when highlighting fails for some reason, this cache
32 will instead store the un-highlighted source text.
34 Second, this will cache the file offsets corresponding to the start
35 of each line of a source file. This cache is not size-limited. */
44 /* This returns the vector of file offsets for the symtab S,
45 computing the vector first if needed.
47 On failure, returns false.
49 On success, returns true and sets *OFFSETS. This pointer is not
50 guaranteed to remain valid across other calls to get_source_lines
51 or get_line_charpos. */
52 bool get_line_charpos (struct symtab
*s
,
53 const std::vector
<off_t
> **offsets
);
55 /* Get the source text for the source file in symtab S. FIRST_LINE
56 and LAST_LINE are the first and last lines to return; line
57 numbers are 1-based. If the file cannot be read, or if the line
58 numbers are out of range, false is returned. Otherwise,
59 LINES_OUT is set to the desired text. The returned text may
60 include ANSI terminal escapes. */
61 bool get_source_lines (struct symtab
*s
, int first_line
,
62 int last_line
, std::string
*lines_out
);
64 /* Remove all the items from the source cache. */
67 m_source_map
.clear ();
68 m_offset_cache
.clear ();
69 m_no_styling_files
.clear ();
74 /* One element in the cache. */
77 /* The full name of the file. */
79 /* The contents of the file. */
83 /* A helper function for get_source_lines reads a source file.
84 Returns the contents of the file; or throws an exception on
85 error. This also updates m_offset_cache. */
86 std::string
get_plain_source_lines (struct symtab
*s
,
87 const std::string
&fullname
);
89 /* A helper function that the data for the given symtab is entered
90 into both caches. Returns false on error. */
91 bool ensure (struct symtab
*s
);
93 /* The contents of the source text cache. */
94 std::vector
<source_text
> m_source_map
;
96 /* The file offset cache. The key is the full name of the source
98 std::unordered_map
<std::string
, std::vector
<off_t
>> m_offset_cache
;
100 /* The list of files where styling failed. */
101 std::unordered_set
<std::string
> m_no_styling_files
;
104 /* The global source cache. */
105 extern source_cache g_source_cache
;
107 #endif /* SOURCE_CACHE_H */