mirror of
https://github.com/intel/llvm.git
synced 2026-01-26 03:56:16 +08:00
[FileSystem] Move path resolution logic out of FileSpec
This patch removes the logic for resolving paths out of FileSpec and updates call sites to rely on the FileSystem class instead. Differential revision: https://reviews.llvm.org/D53915 llvm-svn: 345890
This commit is contained in:
@@ -10,7 +10,6 @@
|
||||
#include "lldb/Utility/FileSpec.h"
|
||||
#include "lldb/Utility/RegularExpression.h"
|
||||
#include "lldb/Utility/Stream.h"
|
||||
#include "lldb/Utility/TildeExpressionResolver.h"
|
||||
|
||||
#include "llvm/ADT/SmallString.h"
|
||||
#include "llvm/ADT/SmallVector.h"
|
||||
@@ -67,38 +66,17 @@ void Denormalize(llvm::SmallVectorImpl<char> &path, FileSpec::Style style) {
|
||||
|
||||
} // end anonymous namespace
|
||||
|
||||
void FileSpec::Resolve(llvm::SmallVectorImpl<char> &path) {
|
||||
if (path.empty())
|
||||
return;
|
||||
|
||||
llvm::SmallString<32> Source(path.begin(), path.end());
|
||||
StandardTildeExpressionResolver Resolver;
|
||||
Resolver.ResolveFullPath(Source, path);
|
||||
|
||||
// Save a copy of the original path that's passed in
|
||||
llvm::SmallString<128> original_path(path.begin(), path.end());
|
||||
|
||||
llvm::sys::fs::make_absolute(path);
|
||||
if (!llvm::sys::fs::exists(path)) {
|
||||
path.clear();
|
||||
path.append(original_path.begin(), original_path.end());
|
||||
}
|
||||
}
|
||||
|
||||
FileSpec::FileSpec() : m_style(GetNativeStyle()) {}
|
||||
|
||||
//------------------------------------------------------------------
|
||||
// Default constructor that can take an optional full path to a file on disk.
|
||||
//------------------------------------------------------------------
|
||||
FileSpec::FileSpec(llvm::StringRef path, bool resolve_path, Style style)
|
||||
: m_style(style) {
|
||||
SetFile(path, resolve_path, style);
|
||||
FileSpec::FileSpec(llvm::StringRef path, Style style) : m_style(style) {
|
||||
SetFile(path, style);
|
||||
}
|
||||
|
||||
FileSpec::FileSpec(llvm::StringRef path, bool resolve_path,
|
||||
const llvm::Triple &Triple)
|
||||
: FileSpec{path, resolve_path,
|
||||
Triple.isOSWindows() ? Style::windows : Style::posix} {}
|
||||
FileSpec::FileSpec(llvm::StringRef path, const llvm::Triple &Triple)
|
||||
: FileSpec{path, Triple.isOSWindows() ? Style::windows : Style::posix} {}
|
||||
|
||||
//------------------------------------------------------------------
|
||||
// Copy constructor
|
||||
@@ -227,16 +205,14 @@ const FileSpec &FileSpec::operator=(const FileSpec &rhs) {
|
||||
return *this;
|
||||
}
|
||||
|
||||
void FileSpec::SetFile(llvm::StringRef pathname, bool resolve) {
|
||||
SetFile(pathname, resolve, m_style);
|
||||
}
|
||||
void FileSpec::SetFile(llvm::StringRef pathname) { SetFile(pathname, m_style); }
|
||||
|
||||
//------------------------------------------------------------------
|
||||
// Update the contents of this object with a new path. The path will be split
|
||||
// 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, bool resolve, Style style) {
|
||||
void FileSpec::SetFile(llvm::StringRef pathname, Style style) {
|
||||
m_filename.Clear();
|
||||
m_directory.Clear();
|
||||
m_is_resolved = false;
|
||||
@@ -245,12 +221,7 @@ void FileSpec::SetFile(llvm::StringRef pathname, bool resolve, Style style) {
|
||||
if (pathname.empty())
|
||||
return;
|
||||
|
||||
llvm::SmallString<64> resolved(pathname);
|
||||
|
||||
if (resolve) {
|
||||
FileSpec::Resolve(resolved);
|
||||
m_is_resolved = true;
|
||||
}
|
||||
llvm::SmallString<128> resolved(pathname);
|
||||
|
||||
// Normalize the path by removing ".", ".." and other redundant components.
|
||||
if (needsNormalization(resolved))
|
||||
@@ -273,15 +244,14 @@ void FileSpec::SetFile(llvm::StringRef pathname, bool resolve, Style style) {
|
||||
llvm::StringRef filename = llvm::sys::path::filename(resolved, m_style);
|
||||
if(!filename.empty())
|
||||
m_filename.SetString(filename);
|
||||
|
||||
llvm::StringRef directory = llvm::sys::path::parent_path(resolved, m_style);
|
||||
if(!directory.empty())
|
||||
m_directory.SetString(directory);
|
||||
}
|
||||
|
||||
void FileSpec::SetFile(llvm::StringRef path, bool resolve,
|
||||
const llvm::Triple &Triple) {
|
||||
return SetFile(path, resolve,
|
||||
Triple.isOSWindows() ? Style::windows : Style::posix);
|
||||
void FileSpec::SetFile(llvm::StringRef path, const llvm::Triple &Triple) {
|
||||
return SetFile(path, Triple.isOSWindows() ? Style::windows : Style::posix);
|
||||
}
|
||||
|
||||
//----------------------------------------------------------------------
|
||||
@@ -316,49 +286,7 @@ bool FileSpec::FileEquals(const FileSpec &rhs) const {
|
||||
// Equal to operator
|
||||
//------------------------------------------------------------------
|
||||
bool FileSpec::operator==(const FileSpec &rhs) const {
|
||||
if (!FileEquals(rhs))
|
||||
return false;
|
||||
if (DirectoryEquals(rhs))
|
||||
return true;
|
||||
|
||||
// TODO: determine if we want to keep this code in here.
|
||||
// The code below was added to handle a case where we were trying to set a
|
||||
// file and line breakpoint and one path was resolved, and the other not and
|
||||
// the directory was in a mount point that resolved to a more complete path:
|
||||
// "/tmp/a.c" == "/private/tmp/a.c". I might end up pulling this out...
|
||||
if (IsResolved() && rhs.IsResolved()) {
|
||||
// Both paths are resolved, no need to look further...
|
||||
return false;
|
||||
}
|
||||
|
||||
FileSpec resolved_lhs(*this);
|
||||
|
||||
// If "this" isn't resolved, resolve it
|
||||
if (!IsResolved()) {
|
||||
if (resolved_lhs.ResolvePath()) {
|
||||
// This path wasn't resolved but now it is. Check if the resolved
|
||||
// directory is the same as our unresolved directory, and if so, we can
|
||||
// mark this object as resolved to avoid more future resolves
|
||||
m_is_resolved = (m_directory == resolved_lhs.m_directory);
|
||||
} else
|
||||
return false;
|
||||
}
|
||||
|
||||
FileSpec resolved_rhs(rhs);
|
||||
if (!rhs.IsResolved()) {
|
||||
if (resolved_rhs.ResolvePath()) {
|
||||
// rhs's path wasn't resolved but now it is. Check if the resolved
|
||||
// directory is the same as rhs's unresolved directory, and if so, we can
|
||||
// mark this object as resolved to avoid more future resolves
|
||||
rhs.m_is_resolved = (rhs.m_directory == resolved_rhs.m_directory);
|
||||
} else
|
||||
return false;
|
||||
}
|
||||
|
||||
// If we reach this point in the code we were able to resolve both paths and
|
||||
// since we only resolve the paths if the basenames are equal, then we can
|
||||
// just check if both directories are equal...
|
||||
return DirectoryEquals(rhs);
|
||||
return FileEquals(rhs) && DirectoryEquals(rhs);
|
||||
}
|
||||
|
||||
//------------------------------------------------------------------
|
||||
@@ -453,15 +381,6 @@ void FileSpec::Dump(Stream *s) const {
|
||||
}
|
||||
}
|
||||
|
||||
bool FileSpec::ResolvePath() {
|
||||
if (m_is_resolved)
|
||||
return true; // We have already resolved this path
|
||||
|
||||
// SetFile(...) will set m_is_resolved correctly if it can resolve the path
|
||||
SetFile(GetPath(false), true);
|
||||
return m_is_resolved;
|
||||
}
|
||||
|
||||
FileSpec::Style FileSpec::GetPathStyle() const { return m_style; }
|
||||
|
||||
//------------------------------------------------------------------
|
||||
@@ -552,7 +471,7 @@ FileSpec FileSpec::CopyByRemovingLastPathComponent() const {
|
||||
llvm::SmallString<64> current_path;
|
||||
GetPath(current_path, false);
|
||||
if (llvm::sys::path::has_parent_path(current_path, m_style))
|
||||
return FileSpec(llvm::sys::path::parent_path(current_path, m_style), false,
|
||||
return FileSpec(llvm::sys::path::parent_path(current_path, m_style),
|
||||
m_style);
|
||||
return *this;
|
||||
}
|
||||
@@ -570,7 +489,7 @@ void FileSpec::PrependPathComponent(llvm::StringRef component) {
|
||||
llvm::sys::path::append(new_path,
|
||||
llvm::sys::path::begin(current_path, m_style),
|
||||
llvm::sys::path::end(current_path), m_style);
|
||||
SetFile(new_path, false, m_style);
|
||||
SetFile(new_path, m_style);
|
||||
}
|
||||
|
||||
void FileSpec::PrependPathComponent(const FileSpec &new_path) {
|
||||
@@ -581,7 +500,7 @@ void FileSpec::AppendPathComponent(llvm::StringRef component) {
|
||||
llvm::SmallString<64> current_path;
|
||||
GetPath(current_path, false);
|
||||
llvm::sys::path::append(current_path, m_style, component);
|
||||
SetFile(current_path, false, m_style);
|
||||
SetFile(current_path, m_style);
|
||||
}
|
||||
|
||||
void FileSpec::AppendPathComponent(const FileSpec &new_path) {
|
||||
@@ -592,7 +511,7 @@ bool FileSpec::RemoveLastPathComponent() {
|
||||
llvm::SmallString<64> current_path;
|
||||
GetPath(current_path, false);
|
||||
if (llvm::sys::path::has_parent_path(current_path, m_style)) {
|
||||
SetFile(llvm::sys::path::parent_path(current_path, m_style), false);
|
||||
SetFile(llvm::sys::path::parent_path(current_path, m_style));
|
||||
return true;
|
||||
}
|
||||
return false;
|
||||
|
||||
Reference in New Issue
Block a user