LLDB mainline
FileSpec.h
Go to the documentation of this file.
1//===-- FileSpec.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_FILESPEC_H
10#define LLDB_UTILITY_FILESPEC_H
11
12#include
13#include
14#include
15
17
18#include "llvm/ADT/StringRef.h"
19#include "llvm/Support/FileSystem.h"
20#include "llvm/Support/FormatVariadic.h"
21#include "llvm/Support/Path.h"
22
23#include
24#include
25
26namespace lldb_private {
27class Stream;
28}
29namespace llvm {
30class Triple;
31}
32namespace llvm {
33class raw_ostream;
34}
35namespace llvm {
36template <typename T> class SmallVectorImpl;
37}
38
39namespace lldb_private {
40
41/// \class FileSpec FileSpec.h "lldb/Utility/FileSpec.h"
42/// A file utility class.
43///
44/// A file specification class that divides paths up into a directory
45/// and basename. These string values of the paths are put into uniqued string
46/// pools for fast comparisons and efficient memory usage.
47///
48/// Another reason the paths are split into the directory and basename is to
49/// allow efficient debugger searching. Often in a debugger the user types in
50/// the basename of the file, for example setting a breakpoint by file and
51/// line, or specifying a module (shared library) to limit the scope in which
52/// to execute a command. The user rarely types in a full path. When the paths
53/// are already split up, it makes it easy for us to compare only the
54/// basenames of a lot of file specifications without having to split up the
55/// file path each time to get to the basename.
56class FileSpec {
57public:
58 using Style = llvm::sys::path::Style;
59
60 FileSpec();
61
62 /// Constructor with path.
63 ///
64 /// Takes a path to a file which can be just a filename, or a full path. If
65 /// \a path is not nullptr or empty, this function will call
66 /// FileSpec::SetFile (const char *path).
67 ///
68 /// \param[in] path
69 /// The full or partial path to a file.
70 ///
71 /// \param[in] style
72 /// The style of the path
73 ///
74 /// \see FileSpec::SetFile (const char *path)
75 explicit FileSpec(llvm::StringRef path, Style style = Style::native);
76
77 explicit FileSpec(llvm::StringRef path, const llvm::Triple &triple);
78
79 bool DirectoryEquals(const FileSpec &other) const;
80
81 bool FileEquals(const FileSpec &other) const;
82
83 /// Equal to operator
84 ///
85 /// Tests if this object is equal to \a rhs.
86 ///
87 /// \param[in] rhs
88 /// A const FileSpec object reference to compare this object
89 /// to.
90 ///
91 /// \return
92 /// \b true if this object is equal to \a rhs, \b false
93 /// otherwise.
94 bool operator==(const FileSpec &rhs) const;
95
96 /// Not equal to operator
97 ///
98 /// Tests if this object is not equal to \a rhs.
99 ///
100 /// \param[in] rhs
101 /// A const FileSpec object reference to compare this object
102 /// to.
103 ///
104 /// \return
105 /// \b true if this object is equal to \a rhs, \b false
106 /// otherwise.
107 bool operator!=(const FileSpec &rhs) const;
108
109 /// Less than to operator
110 ///
111 /// Tests if this object is less than \a rhs.
112 ///
113 /// \param[in] rhs
114 /// A const FileSpec object reference to compare this object
115 /// to.
116 ///
117 /// \return
118 /// \b true if this object is less than \a rhs, \b false
119 /// otherwise.
120 bool operator<(const FileSpec &rhs) const;
121
122 /// Convert to pointer operator.
123 ///
124 /// This allows code to check a FileSpec object to see if it contains
125 /// anything valid using code such as:
126 ///
127 /// \code
128 /// FileSpec file_spec(...);
129 /// if (file_spec)
130 /// { ...
131 /// \endcode
132 ///
133 /// \return
134 /// A pointer to this object if either the directory or filename
135 /// is valid, nullptr otherwise.
136 explicit operator bool() const;
137
138 /// Logical NOT operator.
139 ///
140 /// This allows code to check a FileSpec object to see if it is invalid
141 /// using code such as:
142 ///
143 /// \code
144 /// FileSpec file_spec(...);
145 /// if (!file_spec)
146 /// { ...
147 /// \endcode
148 ///
149 /// \return
150 /// Returns \b true if the object has an empty directory and
151 /// filename, \b false otherwise.
152 bool operator!() const;
153
154 /// Clears the object state.
155 ///
156 /// Clear this object by releasing both the directory and filename string
157 /// values and reverting them to empty strings.
158 void Clear();
159
160 /// Compare two FileSpec objects.
161 ///
162 /// If \a full is true, then both the directory and the filename must match.
163 /// If \a full is false, then the directory names for \a lhs and \a rhs are
164 /// only compared if they are both not empty. This allows a FileSpec object
165 /// to only contain a filename and it can match FileSpec objects that have
166 /// matching filenames with different paths.
167 ///
168 /// \param[in] lhs
169 /// A const reference to the Left Hand Side object to compare.
170 ///
171 /// \param[in] rhs
172 /// A const reference to the Right Hand Side object to compare.
173 ///
174 /// \param[in] full
175 /// If true, then both the directory and filenames will have to
176 /// match for a compare to return zero (equal to). If false
177 /// and either directory from \a lhs or \a rhs is empty, then
178 /// only the filename will be compared, else a full comparison
179 /// is done.
180 ///
181 /// \return -1 if \a lhs is less than \a rhs, 0 if \a lhs is equal to \a rhs,
182 /// 1 if \a lhs is greater than \a rhs
183 static int Compare(const FileSpec &lhs, const FileSpec &rhs, bool full);
184
185 static bool Equal(const FileSpec &a, const FileSpec &b, bool full);
186
187 /// Match FileSpec \a pattern against FileSpec \a file. If \a pattern has a
188 /// directory component, then the \a file must have the same directory
189 /// component. Otherwise, just it matches just the filename. An empty \a
190 /// pattern matches everything.
191 static bool Match(const FileSpec &pattern, const FileSpec &file);
192
193 /// Attempt to guess path style for a given path string. It returns a style,
194 /// if it was able to make a reasonable guess, or std::nullopt if it wasn't.
195 /// The guess will be correct if the input path was a valid absolute path on
196 /// the system which produced it. On other paths the result of this function
197 /// is unreliable (e.g. "c:\foo.txt" is a valid relative posix path).
198 static std::optional