xref: /netbsd-src/external/gpl3/gdb.old/dist/gdb/source-cache.h (revision 6881a4007f077b54e5f51159c52b9b25f57deb0d)
17f2ac410Schristos /* Cache of styled source file text
2*6881a400Schristos    Copyright (C) 2018-2023 Free Software Foundation, Inc.
37f2ac410Schristos 
47f2ac410Schristos    This file is part of GDB.
57f2ac410Schristos 
67f2ac410Schristos    This program is free software; you can redistribute it and/or modify
77f2ac410Schristos    it under the terms of the GNU General Public License as published by
87f2ac410Schristos    the Free Software Foundation; either version 3 of the License, or
97f2ac410Schristos    (at your option) any later version.
107f2ac410Schristos 
117f2ac410Schristos    This program is distributed in the hope that it will be useful,
127f2ac410Schristos    but WITHOUT ANY WARRANTY; without even the implied warranty of
137f2ac410Schristos    MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
147f2ac410Schristos    GNU General Public License for more details.
157f2ac410Schristos 
167f2ac410Schristos    You should have received a copy of the GNU General Public License
177f2ac410Schristos    along with this program.  If not, see <http://www.gnu.org/licenses/>.  */
187f2ac410Schristos 
197f2ac410Schristos #ifndef SOURCE_CACHE_H
207f2ac410Schristos #define SOURCE_CACHE_H
217f2ac410Schristos 
227d62b00eSchristos #include <unordered_map>
237d62b00eSchristos #include <unordered_set>
247d62b00eSchristos 
257d62b00eSchristos /* This caches two things related to source files.
267d62b00eSchristos 
277d62b00eSchristos    First, it caches highlighted source text, keyed by the source
287d62b00eSchristos    file's full name.  A size-limited LRU cache is used.
297f2ac410Schristos 
307f2ac410Schristos    Highlighting depends on the GNU Source Highlight library.  When not
317d62b00eSchristos    available or when highlighting fails for some reason, this cache
327d62b00eSchristos    will instead store the un-highlighted source text.
337d62b00eSchristos 
347d62b00eSchristos    Second, this will cache the file offsets corresponding to the start
357d62b00eSchristos    of each line of a source file.  This cache is not size-limited.  */
367f2ac410Schristos class source_cache
377f2ac410Schristos {
387f2ac410Schristos public:
397f2ac410Schristos 
407f2ac410Schristos   source_cache ()
417f2ac410Schristos   {
427f2ac410Schristos   }
437f2ac410Schristos 
447d62b00eSchristos   /* This returns the vector of file offsets for the symtab S,
457d62b00eSchristos      computing the vector first if needed.
467d62b00eSchristos 
477d62b00eSchristos      On failure, returns false.
487d62b00eSchristos 
497d62b00eSchristos      On success, returns true and sets *OFFSETS.  This pointer is not
507d62b00eSchristos      guaranteed to remain valid across other calls to get_source_lines
517d62b00eSchristos      or get_line_charpos.  */
527d62b00eSchristos   bool get_line_charpos (struct symtab *s,
537d62b00eSchristos 			 const std::vector<off_t> **offsets);
547d62b00eSchristos 
557f2ac410Schristos   /* Get the source text for the source file in symtab S.  FIRST_LINE
567f2ac410Schristos      and LAST_LINE are the first and last lines to return; line
577d62b00eSchristos      numbers are 1-based.  If the file cannot be read, or if the line
587d62b00eSchristos      numbers are out of range, false is returned.  Otherwise,
597d62b00eSchristos      LINES_OUT is set to the desired text.  The returned text may
607d62b00eSchristos      include ANSI terminal escapes.  */
617f2ac410Schristos   bool get_source_lines (struct symtab *s, int first_line,
627f2ac410Schristos 			 int last_line, std::string *lines_out);
637f2ac410Schristos 
647f2ac410Schristos   /* Remove all the items from the source cache.  */
657f2ac410Schristos   void clear ()
667f2ac410Schristos   {
677f2ac410Schristos     m_source_map.clear ();
687d62b00eSchristos     m_offset_cache.clear ();
697f2ac410Schristos   }
707f2ac410Schristos 
717f2ac410Schristos private:
727f2ac410Schristos 
737f2ac410Schristos   /* One element in the cache.  */
747f2ac410Schristos   struct source_text
757f2ac410Schristos   {
767f2ac410Schristos     /* The full name of the file.  */
777f2ac410Schristos     std::string fullname;
787f2ac410Schristos     /* The contents of the file.  */
797f2ac410Schristos     std::string contents;
807f2ac410Schristos   };
817f2ac410Schristos 
827d62b00eSchristos   /* A helper function for get_source_lines reads a source file.
837d62b00eSchristos      Returns the contents of the file; or throws an exception on
847d62b00eSchristos      error.  This also updates m_offset_cache.  */
857d62b00eSchristos   std::string get_plain_source_lines (struct symtab *s,
867d62b00eSchristos 				      const std::string &fullname);
877f2ac410Schristos 
887d62b00eSchristos   /* A helper function that the data for the given symtab is entered
897d62b00eSchristos      into both caches.  Returns false on error.  */
907d62b00eSchristos   bool ensure (struct symtab *s);
917d62b00eSchristos 
927d62b00eSchristos   /* The contents of the source text cache.  */
937f2ac410Schristos   std::vector<source_text> m_source_map;
947d62b00eSchristos 
957d62b00eSchristos   /* The file offset cache.  The key is the full name of the source
967d62b00eSchristos      file.  */
977d62b00eSchristos   std::unordered_map<std::string, std::vector<off_t>> m_offset_cache;
987f2ac410Schristos };
997f2ac410Schristos 
1007f2ac410Schristos /* The global source cache.  */
1017f2ac410Schristos extern source_cache g_source_cache;
1027f2ac410Schristos 
1037f2ac410Schristos #endif /* SOURCE_CACHE_H */
104