source-cache.h 3.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103
  1. /* Cache of styled source file text
  2. Copyright (C) 2018-2022 Free Software Foundation, Inc.
  3. This file is part of GDB.
  4. This program is free software; you can redistribute it and/or modify
  5. it under the terms of the GNU General Public License as published by
  6. the Free Software Foundation; either version 3 of the License, or
  7. (at your option) any later version.
  8. This program is distributed in the hope that it will be useful,
  9. but WITHOUT ANY WARRANTY; without even the implied warranty of
  10. MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  11. GNU General Public License for more details.
  12. You should have received a copy of the GNU General Public License
  13. along with this program. If not, see <http://www.gnu.org/licenses/>. */
  14. #ifndef SOURCE_CACHE_H
  15. #define SOURCE_CACHE_H
  16. #include <unordered_map>
  17. #include <unordered_set>
  18. /* This caches two things related to source files.
  19. First, it caches highlighted source text, keyed by the source
  20. file's full name. A size-limited LRU cache is used.
  21. Highlighting depends on the GNU Source Highlight library. When not
  22. available or when highlighting fails for some reason, this cache
  23. will instead store the un-highlighted source text.
  24. Second, this will cache the file offsets corresponding to the start
  25. of each line of a source file. This cache is not size-limited. */
  26. class source_cache
  27. {
  28. public:
  29. source_cache ()
  30. {
  31. }
  32. /* This returns the vector of file offsets for the symtab S,
  33. computing the vector first if needed.
  34. On failure, returns false.
  35. On success, returns true and sets *OFFSETS. This pointer is not
  36. guaranteed to remain valid across other calls to get_source_lines
  37. or get_line_charpos. */
  38. bool get_line_charpos (struct symtab *s,
  39. const std::vector<off_t> **offsets);
  40. /* Get the source text for the source file in symtab S. FIRST_LINE
  41. and LAST_LINE are the first and last lines to return; line
  42. numbers are 1-based. If the file cannot be read, or if the line
  43. numbers are out of range, false is returned. Otherwise,
  44. LINES_OUT is set to the desired text. The returned text may
  45. include ANSI terminal escapes. */
  46. bool get_source_lines (struct symtab *s, int first_line,
  47. int last_line, std::string *lines_out);
  48. /* Remove all the items from the source cache. */
  49. void clear ()
  50. {
  51. m_source_map.clear ();
  52. m_offset_cache.clear ();
  53. }
  54. private:
  55. /* One element in the cache. */
  56. struct source_text
  57. {
  58. /* The full name of the file. */
  59. std::string fullname;
  60. /* The contents of the file. */
  61. std::string contents;
  62. };
  63. /* A helper function for get_source_lines reads a source file.
  64. Returns the contents of the file; or throws an exception on
  65. error. This also updates m_offset_cache. */
  66. std::string get_plain_source_lines (struct symtab *s,
  67. const std::string &fullname);
  68. /* A helper function that the data for the given symtab is entered
  69. into both caches. Returns false on error. */
  70. bool ensure (struct symtab *s);
  71. /* The contents of the source text cache. */
  72. std::vector<source_text> m_source_map;
  73. /* The file offset cache. The key is the full name of the source
  74. file. */
  75. std::unordered_map<std::string, std::vector<off_t>> m_offset_cache;
  76. };
  77. /* The global source cache. */
  78. extern source_cache g_source_cache;
  79. #endif /* SOURCE_CACHE_H */