Remove some checks of .empty()
[external/binutils.git] / gdb / source-cache.h
1 /* Cache of styled source file text
2    Copyright (C) 2018-2019 Free Software Foundation, Inc.
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
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.
29
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.
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.  */
36 class source_cache
37 {
38 public:
39
40   source_cache ()
41   {
42   }
43
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
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);
63
64   /* Remove all the items from the source cache.  */
65   void clear ()
66   {
67     m_source_map.clear ();
68     m_offset_cache.clear ();
69   }
70
71 private:
72
73   /* One element in the cache.  */
74   struct source_text
75   {
76     /* The full name of the file.  */
77     std::string fullname;
78     /* The contents of the file.  */
79     std::string contents;
80   };
81
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);
87
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);
91
92   /* The contents of the source text cache.  */
93   std::vector<source_text> m_source_map;
94
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;
98 };
99
100 /* The global source cache.  */
101 extern source_cache g_source_cache;
102
103 #endif /* SOURCE_CACHE_H */