• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1 //===-- RealpathPrefixes.h --------------------------------------*- C++ -*-===//
2 //
3 // Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions.
4 // See https://llvm.org/LICENSE.txt for license information.
5 // SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception
6 //
7 //===----------------------------------------------------------------------===//
8 
9 #ifndef LLDB_UTILITY_REALPATHPREFIXES_H
10 #define LLDB_UTILITY_REALPATHPREFIXES_H
11 
12 #include "lldb/lldb-forward.h"
13 #include "llvm/ADT/IntrusiveRefCntPtr.h"
14 #include "llvm/Support/VirtualFileSystem.h"
15 
16 #include <optional>
17 #include <string>
18 #include <vector>
19 
20 namespace lldb_private {
21 
22 class RealpathPrefixes {
23 public:
24   /// \param[in] file_spec_list
25   ///     Prefixes are obtained from FileSpecList, through FileSpec::GetPath(),
26   ///     which ensures that the paths are normalized. For example:
27   ///     "./foo/.." -> ""
28   ///     "./foo/../bar" -> "bar"
29   ///
30   /// \param[in] fs
31   ///     An optional filesystem to use for realpath'ing. If not set, the real
32   ///     filesystem will be used.
33   explicit RealpathPrefixes(const FileSpecList &file_spec_list,
34                             llvm::IntrusiveRefCntPtr<llvm::vfs::FileSystem> fs =
35                                 llvm::vfs::getRealFileSystem());
36 
37   std::optional<FileSpec> ResolveSymlinks(const FileSpec &file_spec);
38 
39   // If/when Statistics.h/cpp is moved into Utility, we can remove these
40   // methods, hold a (weak) pointer to `TargetStats` and directly increment
41   // on that object.
IncreaseSourceRealpathAttemptCount()42   void IncreaseSourceRealpathAttemptCount() {
43     ++m_source_realpath_attempt_count;
44   }
GetSourceRealpathAttemptCount()45   uint32_t GetSourceRealpathAttemptCount() const {
46     return m_source_realpath_attempt_count;
47   }
IncreaseSourceRealpathCompatibleCount()48   void IncreaseSourceRealpathCompatibleCount() {
49     ++m_source_realpath_compatible_count;
50   }
GetSourceRealpathCompatibleCount()51   uint32_t GetSourceRealpathCompatibleCount() const {
52     return m_source_realpath_compatible_count;
53   }
54 
55 private:
56   // Paths that start with one of the prefixes in this list will be realpath'ed
57   // to resolve any symlinks.
58   //
59   // Wildcard prefixes:
60   // - "" (empty string) will match all paths.
61   // - "/" will match all absolute paths.
62   std::vector<std::string> m_prefixes;
63 
64   // The filesystem to use for realpath'ing.
65   llvm::IntrusiveRefCntPtr<llvm::vfs::FileSystem> m_fs;
66 
67   // The optional Target instance to gather statistics.
68   lldb::TargetWP m_target;
69 
70   // Statistics that we temprarily hold here, to be gathered into TargetStats
71   uint32_t m_source_realpath_attempt_count = 0;
72   uint32_t m_source_realpath_compatible_count = 0;
73 };
74 
75 } // namespace lldb_private
76 
77 #endif // LLDB_UTILITY_REALPATHPREFIXES_H
78