]> git.ipfire.org Git - thirdparty/binutils-gdb.git/blame - gdb/source-cache.h
Update copyright year range in header of all files managed by GDB
[thirdparty/binutils-gdb.git] / gdb / source-cache.h
CommitLineData
62f29fda 1/* Cache of styled source file text
1d506c26 2 Copyright (C) 2018-2024 Free Software Foundation, Inc.
62f29fda
TT
3
4 This file is part of GDB.
5
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.
10
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.
15
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/>. */
18
19#ifndef SOURCE_CACHE_H
20#define SOURCE_CACHE_H
21
cb44333d
TT
22#include <unordered_map>
23#include <unordered_set>
24
25/* This caches two things related to source files.
26
27 First, it caches highlighted source text, keyed by the source
28 file's full name. A size-limited LRU cache is used.
62f29fda
TT
29
30 Highlighting depends on the GNU Source Highlight library. When not
cb44333d
TT
31 available or when highlighting fails for some reason, this cache
32 will instead store the un-highlighted source text.
33
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. */
62f29fda
TT
36class source_cache
37{
38public:
39
40 source_cache ()
41 {
42 }
43
cb44333d
TT
44 /* This returns the vector of file offsets for the symtab S,
45 computing the vector first if needed.
46
47 On failure, returns false.
48
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);
54
62f29fda
TT
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
cb44333d
TT
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. */
62f29fda 61 bool get_source_lines (struct symtab *s, int first_line,
38561778 62 int last_line, std::string *lines_out);
62f29fda
TT
63
64 /* Remove all the items from the source cache. */
65 void clear ()
66 {
67 m_source_map.clear ();
cb44333d 68 m_offset_cache.clear ();
dcbdb080 69 m_no_styling_files.clear ();
62f29fda
TT
70 }
71
72private:
73
74 /* One element in the cache. */
75 struct source_text
76 {
77 /* The full name of the file. */
78 std::string fullname;
79 /* The contents of the file. */
80 std::string contents;
81 };
82
872dceaa 83 /* A helper function for get_source_lines reads a source file.
cb44333d
TT
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);
62f29fda 88
cb44333d
TT
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);
92
93 /* The contents of the source text cache. */
62f29fda 94 std::vector<source_text> m_source_map;
cb44333d
TT
95
96 /* The file offset cache. The key is the full name of the source
97 file. */
98 std::unordered_map<std::string, std::vector<off_t>> m_offset_cache;
dcbdb080
TV
99
100 /* The list of files where styling failed. */
101 std::unordered_set<std::string> m_no_styling_files;
62f29fda
TT
102};
103
104/* The global source cache. */
105extern source_cache g_source_cache;
106
107#endif /* SOURCE_CACHE_H */