Back to home page

EIC code displayed by LXR

 
 

    


File indexing completed on 2026-05-10 08:42:46

0001 //===-- SourceManager.h -----------------------------------------*- C++ -*-===//
0002 //
0003 // Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions.
0004 // See https://llvm.org/LICENSE.txt for license information.
0005 // SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception
0006 //
0007 //===----------------------------------------------------------------------===//
0008 
0009 #ifndef LLDB_CORE_SOURCEMANAGER_H
0010 #define LLDB_CORE_SOURCEMANAGER_H
0011 
0012 #include "lldb/Utility/Checksum.h"
0013 #include "lldb/Utility/FileSpec.h"
0014 #include "lldb/lldb-defines.h"
0015 #include "lldb/lldb-forward.h"
0016 
0017 #include "llvm/Support/Chrono.h"
0018 #include "llvm/Support/RWMutex.h"
0019 
0020 #include <cstddef>
0021 #include <cstdint>
0022 #include <map>
0023 #include <memory>
0024 #include <optional>
0025 #include <string>
0026 #include <vector>
0027 
0028 namespace lldb_private {
0029 class RegularExpression;
0030 class Stream;
0031 class SymbolContextList;
0032 class Target;
0033 
0034 class SourceManager {
0035 public:
0036   class File {
0037     friend bool operator==(const SourceManager::File &lhs,
0038                            const SourceManager::File &rhs);
0039 
0040   public:
0041     File(lldb::SupportFileSP support_file_sp, lldb::TargetSP target_sp);
0042     File(lldb::SupportFileSP support_file_sp, lldb::DebuggerSP debugger_sp);
0043 
0044     bool ModificationTimeIsStale() const;
0045     bool PathRemappingIsStale() const;
0046 
0047     size_t DisplaySourceLines(uint32_t line, std::optional<size_t> column,
0048                               uint32_t context_before, uint32_t context_after,
0049                               Stream *s);
0050     void FindLinesMatchingRegex(RegularExpression &regex, uint32_t start_line,
0051                                 uint32_t end_line,
0052                                 std::vector<uint32_t> &match_lines);
0053 
0054     bool GetLine(uint32_t line_no, std::string &buffer);
0055 
0056     uint32_t GetLineOffset(uint32_t line);
0057 
0058     bool LineIsValid(uint32_t line);
0059 
0060     lldb::SupportFileSP GetSupportFile() const {
0061       assert(m_support_file_sp && "SupportFileSP must always be valid");
0062       return m_support_file_sp;
0063     }
0064 
0065     uint32_t GetSourceMapModificationID() const { return m_source_map_mod_id; }
0066 
0067     const char *PeekLineData(uint32_t line);
0068 
0069     uint32_t GetLineLength(uint32_t line, bool include_newline_chars);
0070 
0071     uint32_t GetNumLines();
0072 
0073     llvm::sys::TimePoint<> GetTimestamp() const { return m_mod_time; }
0074 
0075     const Checksum &GetChecksum() const { return m_checksum; }
0076 
0077     std::once_flag &GetChecksumWarningOnceFlag() {
0078       return m_checksum_warning_once_flag;
0079     }
0080 
0081   protected:
0082     /// Set file and update modification time.
0083     void SetSupportFile(lldb::SupportFileSP support_file_sp);
0084 
0085     bool CalculateLineOffsets(uint32_t line = UINT32_MAX);
0086 
0087     /// The support file. If the target has source mappings, this might be
0088     /// different from the original support file passed to the constructor.
0089     lldb::SupportFileSP m_support_file_sp;
0090 
0091     /// Keep track of the on-disk checksum.
0092     Checksum m_checksum;
0093 
0094     /// Once flag for emitting a checksum mismatch warning.
0095     std::once_flag m_checksum_warning_once_flag;
0096 
0097     // Keep the modification time that this file data is valid for
0098     llvm::sys::TimePoint<> m_mod_time;
0099 
0100     // If the target uses path remappings, be sure to clear our notion of a
0101     // source file if the path modification ID changes
0102     uint32_t m_source_map_mod_id = 0;
0103     lldb::DataBufferSP m_data_sp;
0104     typedef std::vector<uint32_t> LineOffsets;
0105     LineOffsets m_offsets;
0106     lldb::DebuggerWP m_debugger_wp;
0107     lldb::TargetWP m_target_wp;
0108 
0109   private:
0110     void CommonInitializer(lldb::SupportFileSP support_file_sp,
0111                            lldb::TargetSP target_sp);
0112   };
0113 
0114   typedef std::shared_ptr<File> FileSP;
0115 
0116   /// The SourceFileCache class separates the source manager from the cache of
0117   /// source files. There is one source manager per Target but both the Debugger
0118   /// and the Process have their own source caches.
0119   ///
0120   /// The SourceFileCache just handles adding, storing, removing and looking up
0121   /// source files. The caching policies are implemented in
0122   /// SourceManager::GetFile.
0123   class SourceFileCache {
0124   public:
0125     SourceFileCache() = default;
0126     ~SourceFileCache() = default;
0127 
0128     void AddSourceFile(const FileSpec &file_spec, FileSP file_sp);
0129     void RemoveSourceFile(const FileSP &file_sp);
0130 
0131     FileSP FindSourceFile(const FileSpec &file_spec) const;
0132 
0133     // Removes all elements from the cache.
0134     void Clear() { m_file_cache.clear(); }
0135 
0136     void Dump(Stream &stream) const;
0137 
0138   private:
0139     void AddSourceFileImpl(const FileSpec &file_spec, FileSP file_sp);
0140 
0141     typedef std::map<FileSpec, FileSP> FileCache;
0142     FileCache m_file_cache;
0143 
0144     mutable llvm::sys::RWMutex m_mutex;
0145   };
0146 
0147   /// A source manager can be made with a valid Target, in which case it can use
0148   /// the path remappings to find source files that are not in their build
0149   /// locations.  Without a target it won't be able to do this.
0150   /// @{
0151   SourceManager(const lldb::DebuggerSP &debugger_sp);
0152   SourceManager(const lldb::TargetSP &target_sp);
0153   /// @}
0154 
0155   ~SourceManager();
0156 
0157   FileSP GetLastFile() { return GetFile(m_last_support_file_sp); }
0158 
0159   size_t DisplaySourceLinesWithLineNumbers(
0160       lldb::SupportFileSP support_file_sp, uint32_t line, uint32_t column,
0161       uint32_t context_before, uint32_t context_after,
0162       const char *current_line_cstr, Stream *s,
0163       const SymbolContextList *bp_locs = nullptr);
0164 
0165   // This variant uses the last file we visited.
0166   size_t DisplaySourceLinesWithLineNumbersUsingLastFile(
0167       uint32_t start_line, uint32_t count, uint32_t curr_line, uint32_t column,
0168       const char *current_line_cstr, Stream *s,
0169       const SymbolContextList *bp_locs = nullptr);
0170 
0171   size_t DisplayMoreWithLineNumbers(Stream *s, uint32_t count, bool reverse,
0172                                     const SymbolContextList *bp_locs = nullptr);
0173 
0174   bool SetDefaultFileAndLine(lldb::SupportFileSP support_file_sp,
0175                              uint32_t line);
0176 
0177   struct SupportFileAndLine {
0178     lldb::SupportFileSP support_file_sp;
0179     uint32_t line;
0180     SupportFileAndLine(lldb::SupportFileSP support_file_sp, uint32_t line)
0181         : support_file_sp(support_file_sp), line(line) {}
0182   };
0183 
0184   std::optional<SupportFileAndLine> GetDefaultFileAndLine();
0185 
0186   bool DefaultFileAndLineSet() {
0187     return (GetFile(m_last_support_file_sp).get() != nullptr);
0188   }
0189 
0190   void FindLinesMatchingRegex(lldb::SupportFileSP support_file_sp,
0191                               RegularExpression &regex, uint32_t start_line,
0192                               uint32_t end_line,
0193                               std::vector<uint32_t> &match_lines);
0194 
0195   FileSP GetFile(lldb::SupportFileSP support_file_sp);
0196 
0197 protected:
0198   lldb::SupportFileSP m_last_support_file_sp;
0199   uint32_t m_last_line;
0200   uint32_t m_last_count;
0201   bool m_default_set;
0202   lldb::TargetWP m_target_wp;
0203   lldb::DebuggerWP m_debugger_wp;
0204 
0205 private:
0206   SourceManager(const SourceManager &) = delete;
0207   const SourceManager &operator=(const SourceManager &) = delete;
0208 };
0209 
0210 bool operator==(const SourceManager::File &lhs, const SourceManager::File &rhs);
0211 
0212 } // namespace lldb_private
0213 
0214 #endif // LLDB_CORE_SOURCEMANAGER_H