Rename gdb/ChangeLog to gdb/ChangeLog-2021
[binutils-gdb.git] / gdb / source-cache.h
blob9fae575dc46043e1b0a4721372188f87bc6884d9
1 /* Cache of styled source file text
2 Copyright (C) 2018-2021 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. */
36 class source_cache
38 public:
40 source_cache ()
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. */
65 void clear ()
67 m_source_map.clear ();
68 m_offset_cache.clear ();
71 private:
73 /* One element in the cache. */
74 struct source_text
76 /* The full name of the file. */
77 std::string fullname;
78 /* The contents of the file. */
79 std::string contents;
82 /* A helper function for get_source_lines reads a source file.
83 Returns the contents of the file; or throws an exception on
84 error. This also updates m_offset_cache. */
85 std::string get_plain_source_lines (struct symtab *s,
86 const std::string &fullname);
88 /* A helper function that the data for the given symtab is entered
89 into both caches. Returns false on error. */
90 bool ensure (struct symtab *s);
92 /* The contents of the source text cache. */
93 std::vector<source_text> m_source_map;
95 /* The file offset cache. The key is the full name of the source
96 file. */
97 std::unordered_map<std::string, std::vector<off_t>> m_offset_cache;
100 /* The global source cache. */
101 extern source_cache g_source_cache;
103 #endif /* SOURCE_CACHE_H */