diff options
| author | Dimitry Andric <dim@FreeBSD.org> | 2023-02-11 12:38:04 +0000 |
|---|---|---|
| committer | Dimitry Andric <dim@FreeBSD.org> | 2023-02-11 12:38:11 +0000 |
| commit | e3b557809604d036af6e00c60f012c2025b59a5e (patch) | |
| tree | 8a11ba2269a3b669601e2fd41145b174008f4da8 /lldb/source/Utility/FileSpec.cpp | |
| parent | 08e8dd7b9db7bb4a9de26d44c1cbfd24e869c014 (diff) | |
Diffstat (limited to 'lldb/source/Utility/FileSpec.cpp')
| -rw-r--r-- | lldb/source/Utility/FileSpec.cpp | 87 |
1 files changed, 47 insertions, 40 deletions
diff --git a/lldb/source/Utility/FileSpec.cpp b/lldb/source/Utility/FileSpec.cpp index c0dbc29bcd1f..7ff57307dec5 100644 --- a/lldb/source/Utility/FileSpec.cpp +++ b/lldb/source/Utility/FileSpec.cpp @@ -21,6 +21,7 @@ #include "llvm/Support/raw_ostream.h" #include <algorithm> +#include <optional> #include <system_error> #include <vector> @@ -170,9 +171,7 @@ void FileSpec::SetFile(llvm::StringRef pathname) { SetFile(pathname, m_style); } // up into a directory and filename and stored as uniqued string values for // quick comparison and efficient memory usage. void FileSpec::SetFile(llvm::StringRef pathname, Style style) { - m_filename.Clear(); - m_directory.Clear(); - m_is_resolved = false; + Clear(); m_style = (style == Style::native) ? GetNativeStyle() : style; if (pathname.empty()) @@ -259,6 +258,7 @@ Stream &lldb_private::operator<<(Stream &s, const FileSpec &f) { void FileSpec::Clear() { m_directory.Clear(); m_filename.Clear(); + PathWasModified(); } // Compare two FileSpec objects. If "full" is true, then both the directory and @@ -305,7 +305,8 @@ bool FileSpec::Match(const FileSpec &pattern, const FileSpec &file) { return true; } -llvm::Optional<FileSpec::Style> FileSpec::GuessPathStyle(llvm::StringRef absolute_path) { +std::optional<FileSpec::Style> +FileSpec::GuessPathStyle(llvm::StringRef absolute_path) { if (absolute_path.startswith("/")) return Style::posix; if (absolute_path.startswith(R"(\\)")) @@ -314,7 +315,7 @@ llvm::Optional<FileSpec::Style> FileSpec::GuessPathStyle(llvm::StringRef absolut (absolute_path.substr(1, 2) == R"(:\)" || absolute_path.substr(1, 2) == R"(:/)")) return Style::windows; - return llvm::None; + return std::nullopt; } // Dump the object to the supplied stream. If the object contains a valid @@ -330,17 +331,35 @@ void FileSpec::Dump(llvm::raw_ostream &s) const { FileSpec::Style FileSpec::GetPathStyle() const { return m_style; } -// Directory string get accessor. -ConstString &FileSpec::GetDirectory() { return m_directory; } +void FileSpec::SetDirectory(ConstString directory) { + m_directory = directory; + PathWasModified(); +} -// Directory string const get accessor. -ConstString FileSpec::GetDirectory() const { return m_directory; } +void FileSpec::SetDirectory(llvm::StringRef directory) { + m_directory = ConstString(directory); + PathWasModified(); +} -// Filename string get accessor. -ConstString &FileSpec::GetFilename() { return m_filename; } +void FileSpec::SetFilename(ConstString filename) { + m_filename = filename; + PathWasModified(); +} -// Filename string const get accessor. -ConstString FileSpec::GetFilename() const { return m_filename; } +void FileSpec::SetFilename(llvm::StringRef filename) { + m_filename = ConstString(filename); + PathWasModified(); +} + +void FileSpec::ClearFilename() { + m_filename.Clear(); + PathWasModified(); +} + +void FileSpec::ClearDirectory() { + m_directory.Clear(); + PathWasModified(); +} // Extract the directory and path into a fixed buffer. This is needed as the // directory and path are stored in separate string values. @@ -360,8 +379,8 @@ std::string FileSpec::GetPath(bool denormalize) const { return static_cast<std::string>(result); } -const char *FileSpec::GetCString(bool denormalize) const { - return ConstString{GetPath(denormalize)}.AsCString(nullptr); +ConstString FileSpec::GetPathAsConstString(bool denormalize) const { + return ConstString{GetPath(denormalize)}; } void FileSpec::GetPath(llvm::SmallVectorImpl<char> &path, @@ -476,18 +495,22 @@ bool FileSpec::IsRelative() const { } bool FileSpec::IsAbsolute() const { - llvm::SmallString<64> current_path; - GetPath(current_path, false); + // Check if we have cached if this path is absolute to avoid recalculating. + if (m_absolute != Absolute::Calculate) + return m_absolute == Absolute::Yes; - // Early return if the path is empty. - if (current_path.empty()) - return false; + m_absolute = Absolute::No; - // We consider paths starting with ~ to be absolute. - if (current_path[0] == '~') - return true; + llvm::SmallString<64> path; + GetPath(path, false); - return llvm::sys::path::is_absolute(current_path, m_style); + if (!path.empty()) { + // We consider paths starting with ~ to be absolute. + if (path[0] == '~' || llvm::sys::path::is_absolute(path, m_style)) + m_absolute = Absolute::Yes; + } + + return m_absolute == Absolute::Yes; } void FileSpec::MakeAbsolute(const FileSpec &dir) { @@ -536,19 +559,3 @@ void llvm::format_provider<FileSpec>::format(const FileSpec &F, if (!file.empty()) Stream << file; } - -void llvm::yaml::ScalarEnumerationTraits<FileSpecStyle>::enumeration( - IO &io, FileSpecStyle &value) { - io.enumCase(value, "windows", FileSpecStyle(FileSpec::Style::windows)); - io.enumCase(value, "posix", FileSpecStyle(FileSpec::Style::posix)); - io.enumCase(value, "native", FileSpecStyle(FileSpec::Style::native)); -} - -void llvm::yaml::MappingTraits<FileSpec>::mapping(IO &io, FileSpec &f) { - io.mapRequired("directory", f.m_directory); - io.mapRequired("file", f.m_filename); - io.mapRequired("resolved", f.m_is_resolved); - FileSpecStyle style = f.m_style; - io.mapRequired("style", style); - f.m_style = style; -} |
