#include "llvm/Support/Path.h"
#include "llvm/ADT/ArrayRef.h"
#include "llvm/Config/llvm-config.h"
#include "llvm/Support/Endian.h"
#include "llvm/Support/Errc.h"
#include "llvm/Support/ErrorHandling.h"
#include "llvm/Support/FileSystem.h"
#include "llvm/Support/Process.h"
#include "llvm/Support/Signals.h"
#include <cctype>
#include <cstring>
#if !defined(_MSC_VER) && !defined(__MINGW32__)
#include <unistd.h>
#else
#include <io.h>
#endif
using namespace llvm;
using namespace llvm::support::endian;
namespace {
using llvm::StringRef;
using llvm::sys::path::is_separator;
using llvm::sys::path::Style;
inline Style real_style(Style style) {
#ifdef _WIN32
return (style == Style::posix) ? Style::posix : Style::windows;
#else
return (style == Style::windows) ? Style::windows : Style::posix;
#endif
}
inline const char *separators(Style style) {
if (real_style(style) == Style::windows)
return "\\/";
return "/";
}
inline char preferred_separator(Style style) {
if (real_style(style) == Style::windows)
return '\\';
return '/';
}
StringRef find_first_component(StringRef path, Style style) {
if (path.empty())
return path;
if (real_style(style) == Style::windows) {
if (path.size() >= 2 &&
std::isalpha(static_cast<unsigned char>(path[0])) && path[1] == ':')
return path.substr(0, 2);
}
if ((path.size() > 2) && is_separator(path[0], style) &&
path[0] == path[1] && !is_separator(path[2], style)) {
size_t end = path.find_first_of(separators(style), 2);
return path.substr(0, end);
}
if (is_separator(path[0], style))
return path.substr(0, 1);
size_t end = path.find_first_of(separators(style));
return path.substr(0, end);
}
size_t filename_pos(StringRef str, Style style) {
if (str.size() > 0 && is_separator(str[str.size() - 1], style))
return str.size() - 1;
size_t pos = str.find_last_of(separators(style), str.size() - 1);
if (real_style(style) == Style::windows) {
if (pos == StringRef::npos)
pos = str.find_last_of(':', str.size() - 2);
}
if (pos == StringRef::npos || (pos == 1 && is_separator(str[0], style)))
return 0;
return pos + 1;
}
size_t root_dir_start(StringRef str, Style style) {
if (real_style(style) == Style::windows) {
if (str.size() > 2 && str[1] == ':' && is_separator(str[2], style))
return 2;
}
if (str.size() > 3 && is_separator(str[0], style) && str[0] == str[1] &&
!is_separator(str[2], style)) {
return str.find_first_of(separators(style), 2);
}
if (str.size() > 0 && is_separator(str[0], style))
return 0;
return StringRef::npos;
}
size_t parent_path_end(StringRef path, Style style) {
size_t end_pos = filename_pos(path, style);
bool filename_was_sep =
path.size() > 0 && is_separator(path[end_pos], style);
size_t root_dir_pos = root_dir_start(path, style);
while (end_pos > 0 &&
(root_dir_pos == StringRef::npos || end_pos > root_dir_pos) &&
is_separator(path[end_pos - 1], style))
--end_pos;
if (end_pos == root_dir_pos && !filename_was_sep) {
return root_dir_pos + 1;
}
return end_pos;
}
}
enum FSEntity {
FS_Dir,
FS_File,
FS_Name
};
static std::error_code
createUniqueEntity(const Twine &Model, int &ResultFD,
SmallVectorImpl<char> &ResultPath, bool MakeAbsolute,
unsigned Mode, FSEntity Type,
sys::fs::OpenFlags Flags = sys::fs::OF_None) {
llvm_unreachable("createUniqueEntity"); }
namespace llvm {
namespace sys {
namespace path {
const_iterator begin(StringRef path, Style style) {
const_iterator i;
i.Path = path;
i.Component = find_first_component(path, style);
i.Position = 0;
i.S = style;
return i;
}
const_iterator end(StringRef path) {
const_iterator i;
i.Path = path;
i.Position = path.size();
return i;
}
const_iterator &const_iterator::operator++() {
assert(Position < Path.size() && "Tried to increment past end!");
Position += Component.size();
if (Position == Path.size()) {
Component = StringRef();
return *this;
}
bool was_net = Component.size() > 2 && is_separator(Component[0], S) &&
Component[1] == Component[0] && !is_separator(Component[2], S);
if (is_separator(Path[Position], S)) {
if (was_net ||
(real_style(S) == Style::windows && Component.endswith(":"))) {
Component = Path.substr(Position, 1);
return *this;
}
while (Position != Path.size() && is_separator(Path[Position], S)) {
++Position;
}
if (Position == Path.size() && Component != "/") {
--Position;
Component = ".";
return *this;
}
}
size_t end_pos = Path.find_first_of(separators(S), Position);
Component = Path.slice(Position, end_pos);
return *this;
}
bool const_iterator::operator==(const const_iterator &RHS) const {
return Path.begin() == RHS.Path.begin() && Position == RHS.Position;
}
ptrdiff_t const_iterator::operator-(const const_iterator &RHS) const {
return Position - RHS.Position;
}
reverse_iterator rbegin(StringRef Path, Style style) {
reverse_iterator I;
I.Path = Path;
I.Position = Path.size();
I.S = style;
++I;
return I;
}
reverse_iterator rend(StringRef Path) {
reverse_iterator I;
I.Path = Path;
I.Component = Path.substr(0, 0);
I.Position = 0;
return I;
}
reverse_iterator &reverse_iterator::operator++() {
size_t root_dir_pos = root_dir_start(Path, S);
size_t end_pos = Position;
while (end_pos > 0 && (end_pos - 1) != root_dir_pos &&
is_separator(Path[end_pos - 1], S))
--end_pos;
if (Position == Path.size() && !Path.empty() &&
is_separator(Path.back(), S) &&
(root_dir_pos == StringRef::npos || end_pos - 1 > root_dir_pos)) {
--Position;
Component = ".";
return *this;
}
size_t start_pos = filename_pos(Path.substr(0, end_pos), S);
Component = Path.slice(start_pos, end_pos);
Position = start_pos;
return *this;
}
bool reverse_iterator::operator==(const reverse_iterator &RHS) const {
return Path.begin() == RHS.Path.begin() && Component == RHS.Component &&
Position == RHS.Position;
}
ptrdiff_t reverse_iterator::operator-(const reverse_iterator &RHS) const {
return Position - RHS.Position;
}
StringRef root_path(StringRef path, Style style) {
const_iterator b = begin(path, style), pos = b, e = end(path);
if (b != e) {
bool has_net =
b->size() > 2 && is_separator((*b)[0], style) && (*b)[1] == (*b)[0];
bool has_drive = (real_style(style) == Style::windows) && b->endswith(":");
if (has_net || has_drive) {
if ((++pos != e) && is_separator((*pos)[0], style)) {
return path.substr(0, b->size() + pos->size());
} else {
return *b;
}
}
if (is_separator((*b)[0], style)) {
return *b;
}
}
return StringRef();
}
StringRef root_name(StringRef path, Style style) {
const_iterator b = begin(path, style), e = end(path);
if (b != e) {
bool has_net =
b->size() > 2 && is_separator((*b)[0], style) && (*b)[1] == (*b)[0];
bool has_drive = (real_style(style) == Style::windows) && b->endswith(":");
if (has_net || has_drive) {
return *b;
}
}
return StringRef();
}
StringRef root_directory(StringRef path, Style style) {
const_iterator b = begin(path, style), pos = b, e = end(path);
if (b != e) {
bool has_net =
b->size() > 2 && is_separator((*b)[0], style) && (*b)[1] == (*b)[0];
bool has_drive = (real_style(style) == Style::windows) && b->endswith(":");
if ((has_net || has_drive) &&
(++pos != e) && is_separator((*pos)[0], style)) {
return *pos;
}
if (!has_net && is_separator((*b)[0], style)) {
return *b;
}
}
return StringRef();
}
StringRef relative_path(StringRef path, Style style) {
StringRef root = root_path(path, style);
return path.substr(root.size());
}
void append(SmallVectorImpl<char> &path, Style style, const Twine &a,
const Twine &b, const Twine &c, const Twine &d) {
SmallString<32> a_storage;
SmallString<32> b_storage;
SmallString<32> c_storage;
SmallString<32> d_storage;
SmallVector<StringRef, 4> components;
if (!a.isTriviallyEmpty()) components.push_back(a.toStringRef(a_storage));
if (!b.isTriviallyEmpty()) components.push_back(b.toStringRef(b_storage));
if (!c.isTriviallyEmpty()) components.push_back(c.toStringRef(c_storage));
if (!d.isTriviallyEmpty()) components.push_back(d.toStringRef(d_storage));
for (auto &component : components) {
bool path_has_sep =
!path.empty() && is_separator(path[path.size() - 1], style);
if (path_has_sep) {
size_t loc = component.find_first_not_of(separators(style));
StringRef c = component.substr(loc);
path.append(c.begin(), c.end());
continue;
}
bool component_has_sep =
!component.empty() && is_separator(component[0], style);
if (!component_has_sep &&
!(path.empty() || has_root_name(component, style))) {
path.push_back(preferred_separator(style));
}
path.append(component.begin(), component.end());
}
}
void append(SmallVectorImpl<char> &path, const Twine &a, const Twine &b,
const Twine &c, const Twine &d) {
append(path, Style::native, a, b, c, d);
}
void append(SmallVectorImpl<char> &path, const_iterator begin,
const_iterator end, Style style) {
for (; begin != end; ++begin)
path::append(path, style, *begin);
}
StringRef parent_path(StringRef path, Style style) {
size_t end_pos = parent_path_end(path, style);
if (end_pos == StringRef::npos)
return StringRef();
else
return path.substr(0, end_pos);
}
void remove_filename(SmallVectorImpl<char> &path, Style style) {
size_t end_pos = parent_path_end(StringRef(path.begin(), path.size()), style);
if (end_pos != StringRef::npos)
path.set_size(end_pos);
}
void replace_extension(SmallVectorImpl<char> &path, const Twine &extension,
Style style) {
StringRef p(path.begin(), path.size());
SmallString<32> ext_storage;
StringRef ext = extension.toStringRef(ext_storage);
size_t pos = p.find_last_of('.');
if (pos != StringRef::npos && pos >= filename_pos(p, style))
path.set_size(pos);
if (ext.size() > 0 && ext[0] != '.')
path.push_back('.');
path.append(ext.begin(), ext.end());
}
void replace_path_prefix(SmallVectorImpl<char> &Path,
const StringRef &OldPrefix, const StringRef &NewPrefix,
Style style) {
if (OldPrefix.empty() && NewPrefix.empty())
return;
StringRef OrigPath(Path.begin(), Path.size());
if (!OrigPath.startswith(OldPrefix))
return;
if (OldPrefix.size() == NewPrefix.size()) {
llvm::copy(NewPrefix, Path.begin());
return;
}
StringRef RelPath = OrigPath.substr(OldPrefix.size());
SmallString<256> NewPath;
path::append(NewPath, style, NewPrefix);
path::append(NewPath, style, RelPath);
Path.swap(NewPath);
}
void native(const Twine &path, SmallVectorImpl<char> &result, Style style) {
assert((!path.isSingleStringRef() ||
path.getSingleStringRef().data() != result.data()) &&
"path and result are not allowed to overlap!");
result.clear();
path.toVector(result);
native(result, style);
}
void native(SmallVectorImpl<char> &Path, Style style) {
if (Path.empty())
return;
if (real_style(style) == Style::windows) {
std::replace(Path.begin(), Path.end(), '/', '\\');
if (Path[0] == '~' && (Path.size() == 1 || is_separator(Path[1], style))) {
llvm_unreachable("BINARYEN native");
#if 0#endif
}
} else {
for (auto PI = Path.begin(), PE = Path.end(); PI < PE; ++PI) {
if (*PI == '\\') {
auto PN = PI + 1;
if (PN < PE && *PN == '\\')
++PI; else
*PI = '/';
}
}
}
}
std::string convert_to_slash(StringRef path, Style style) {
if (real_style(style) != Style::windows)
return path;
std::string s = path.str();
std::replace(s.begin(), s.end(), '\\', '/');
return s;
}
StringRef filename(StringRef path, Style style) { return *rbegin(path, style); }
StringRef stem(StringRef path, Style style) {
StringRef fname = filename(path, style);
size_t pos = fname.find_last_of('.');
if (pos == StringRef::npos)
return fname;
else
if ((fname.size() == 1 && fname == ".") ||
(fname.size() == 2 && fname == ".."))
return fname;
else
return fname.substr(0, pos);
}
StringRef extension(StringRef path, Style style) {
StringRef fname = filename(path, style);
size_t pos = fname.find_last_of('.');
if (pos == StringRef::npos)
return StringRef();
else
if ((fname.size() == 1 && fname == ".") ||
(fname.size() == 2 && fname == ".."))
return StringRef();
else
return fname.substr(pos);
}
bool is_separator(char value, Style style) {
if (value == '/')
return true;
if (real_style(style) == Style::windows)
return value == '\\';
return false;
}
StringRef get_separator(Style style) {
if (real_style(style) == Style::windows)
return "\\";
return "/";
}
bool has_root_name(const Twine &path, Style style) {
SmallString<128> path_storage;
StringRef p = path.toStringRef(path_storage);
return !root_name(p, style).empty();
}
bool has_root_directory(const Twine &path, Style style) {
SmallString<128> path_storage;
StringRef p = path.toStringRef(path_storage);
return !root_directory(p, style).empty();
}
bool has_root_path(const Twine &path, Style style) {
SmallString<128> path_storage;
StringRef p = path.toStringRef(path_storage);
return !root_path(p, style).empty();
}
bool has_relative_path(const Twine &path, Style style) {
SmallString<128> path_storage;
StringRef p = path.toStringRef(path_storage);
return !relative_path(p, style).empty();
}
bool has_filename(const Twine &path, Style style) {
SmallString<128> path_storage;
StringRef p = path.toStringRef(path_storage);
return !filename(p, style).empty();
}
bool has_parent_path(const Twine &path, Style style) {
SmallString<128> path_storage;
StringRef p = path.toStringRef(path_storage);
return !parent_path(p, style).empty();
}
bool has_stem(const Twine &path, Style style) {
SmallString<128> path_storage;
StringRef p = path.toStringRef(path_storage);
return !stem(p, style).empty();
}
bool has_extension(const Twine &path, Style style) {
SmallString<128> path_storage;
StringRef p = path.toStringRef(path_storage);
return !extension(p, style).empty();
}
bool is_absolute(const Twine &path, Style style) {
SmallString<128> path_storage;
StringRef p = path.toStringRef(path_storage);
bool rootDir = has_root_directory(p, style);
bool rootName =
(real_style(style) != Style::windows) || has_root_name(p, style);
return rootDir && rootName;
}
bool is_relative(const Twine &path, Style style) {
return !is_absolute(path, style);
}
StringRef remove_leading_dotslash(StringRef Path, Style style) {
while (Path.size() > 2 && Path[0] == '.' && is_separator(Path[1], style)) {
Path = Path.substr(2);
while (Path.size() > 0 && is_separator(Path[0], style))
Path = Path.substr(1);
}
return Path;
}
static SmallString<256> remove_dots(StringRef path, bool remove_dot_dot,
Style style) {
SmallVector<StringRef, 16> components;
StringRef rel = path::relative_path(path, style);
for (StringRef C :
llvm::make_range(path::begin(rel, style), path::end(rel))) {
if (C == ".")
continue;
if (remove_dot_dot && C == "..") {
if (!components.empty() && components.back() != "..") {
components.pop_back();
continue;
}
if (path::is_absolute(path, style))
continue;
}
components.push_back(C);
}
SmallString<256> buffer = path::root_path(path, style);
for (StringRef C : components)
path::append(buffer, style, C);
return buffer;
}
bool remove_dots(SmallVectorImpl<char> &path, bool remove_dot_dot,
Style style) {
StringRef p(path.data(), path.size());
SmallString<256> result = remove_dots(p, remove_dot_dot, style);
if (result == path)
return false;
path.swap(result);
return true;
}
}
#if 0#endif
} }
#if 0#endif
#if 0#endif