16 #include "llvm/Config/llvm-config.h" 26 #if !defined(_MSC_VER) && !defined(__MINGW32__) 42 return (style == Style::posix) ? Style::posix : Style::windows;
44 return (style == Style::windows) ? Style::windows : Style::posix;
48 inline const char *separators(
Style style) {
49 if (real_style(style) == Style::windows)
54 inline char preferred_separator(
Style style) {
55 if (real_style(style) == Style::windows)
70 if (real_style(style) == Style::windows) {
72 if (path.
size() >= 2 &&
73 std::isalpha(static_cast<unsigned char>(path[0])) && path[1] ==
':')
82 return path.
substr(0, end);
91 return path.
substr(0, end);
98 return str.
size() - 1;
102 if (real_style(style) == Style::windows) {
117 if (real_style(style) == Style::windows) {
139 size_t end_pos = filename_pos(path, style);
141 bool filename_was_sep =
145 size_t root_dir_pos = root_dir_start(path, style);
146 while (end_pos > 0 &&
151 if (end_pos == root_dir_pos && !filename_was_sep) {
154 return root_dir_pos + 1;
168 static std::error_code
182 ModelStorage.
swap(TDir);
188 ResultPath = ModelStorage;
198 for (
int Retries = 128; Retries > 0; --Retries) {
200 for (
unsigned i = 0, e = ModelStorage.
size(); i != e; ++i) {
201 if (ModelStorage[i] ==
'%')
219 return std::error_code();
225 return std::error_code();
238 return std::error_code();
253 i.Component = find_first_component(path, style);
262 i.Position = path.
size();
273 if (Position == Path.size()) {
280 bool was_net = Component.size() > 2 &&
is_separator(Component[0], S) &&
281 Component[1] == Component[0] && !
is_separator(Component[2], S);
288 (real_style(S) == Style::windows && Component.endswith(
":"))) {
289 Component = Path.substr(Position, 1);
294 while (Position != Path.size() &&
is_separator(Path[Position], S)) {
299 if (Position == Path.size() && Component !=
"/") {
307 size_t end_pos = Path.find_first_of(separators(S), Position);
308 Component = Path.slice(Position, end_pos);
314 return Path.begin() == RHS.Path.
begin() &&
Position == RHS.Position;
324 I.Position = Path.
size();
332 I.Component = Path.
substr(0, 0);
338 size_t root_dir_pos = root_dir_start(Path, S);
342 while (end_pos > 0 && (end_pos - 1) != root_dir_pos &&
347 if (
Position == Path.size() && !Path.empty() &&
356 size_t start_pos = filename_pos(Path.substr(0, end_pos), S);
357 Component = Path.slice(start_pos, end_pos);
363 return Path.begin() == RHS.Path.
begin() && Component == RHS.Component &&
375 b->size() > 2 &&
is_separator((*b)[0], style) && (*b)[1] == (*b)[0];
376 bool has_drive = (real_style(style) == Style::windows) && b->endswith(
":");
378 if (has_net || has_drive) {
381 return path.
substr(0, b->size() + pos->size());
401 b->size() > 2 &&
is_separator((*b)[0], style) && (*b)[1] == (*b)[0];
402 bool has_drive = (real_style(style) == Style::windows) && b->endswith(
":");
404 if (has_net || has_drive) {
418 b->size() > 2 &&
is_separator((*b)[0], style) && (*b)[1] == (*b)[0];
419 bool has_drive = (real_style(style) == Style::windows) && b->endswith(
":");
421 if ((has_net || has_drive) &&
455 for (
auto &component : components) {
460 size_t loc = component.find_first_not_of(separators(style));
468 bool component_has_sep =
469 !component.empty() &&
is_separator(component[0], style);
470 if (!component_has_sep &&
473 path.
push_back(preferred_separator(style));
476 path.
append(component.begin(), component.end());
492 size_t end_pos = parent_path_end(path, style);
496 return path.
substr(0, end_pos);
517 if (ext.
size() > 0 && ext[0] !=
'.')
531 if (!OrigPath.startswith(OldPrefix))
535 if (OldPrefix.
size() == NewPrefix.
size()) {
550 "path and result are not allowed to overlap!");
554 native(result, style);
560 if (real_style(style) == Style::windows) {
569 for (
auto PI = Path.
begin(), PE = Path.
end(); PI < PE; ++PI) {
572 if (PN < PE && *PN ==
'\\')
582 if (real_style(style) != Style::windows)
585 std::string s = path.
str();
598 if ((fname.
size() == 1 && fname ==
".") ||
599 (fname.
size() == 2 && fname ==
".."))
602 return fname.
substr(0, pos);
611 if ((fname.
size() == 1 && fname ==
".") ||
612 (fname.
size() == 2 && fname ==
".."))
621 if (real_style(style) == Style::windows)
622 return value ==
'\\';
627 if (real_style(style) == Style::windows)
694 (real_style(style) != Style::windows) ||
has_root_name(p, style);
696 return rootDir && rootName;
724 if (remove_dot_dot &&
C ==
"..") {
725 if (!components.
empty() && components.
back() !=
"..") {
759 std::error_code EC =
status(Path, Status);
763 return std::error_code();
791 static std::error_code
797 "Model must be a simple filename.");
803 static std::error_code
806 const char *Middle = Suffix.
empty() ?
"-%%%%%%" :
"-%%%%%%.";
861 if (rootName && rootDirectory)
866 current_directory.
toVector(current_dir);
869 if (!rootName && !rootDirectory) {
873 path.
swap(current_dir);
877 if (!rootName && rootDirectory) {
882 path.
swap(curDirRootName);
886 if (rootName && !rootDirectory) {
893 path::append(res, pRootName, bRootDirectory, bRelativePath, pRelativePath);
939 const size_t BufSize = 4096;
940 char *Buf =
new char[BufSize];
941 int BytesRead = 0, BytesWritten = 0;
943 BytesRead =
read(ReadFD, Buf, BufSize);
947 BytesWritten =
write(WriteFD, Buf, BytesRead);
948 if (BytesWritten < 0)
950 BytesRead -= BytesWritten;
952 if (BytesWritten < 0)
957 if (BytesRead < 0 || BytesWritten < 0)
958 return std::error_code(errno, std::generic_category());
959 return std::error_code();
966 if (std::error_code EC =
995 constexpr
size_t BufSize = 4096;
996 std::vector<uint8_t> Buf(BufSize);
999 BytesRead =
read(FD, Buf.data(), BufSize);
1006 return std::error_code(errno, std::generic_category());
1027 return s.
type() != file_type::status_error;
1032 if (
status(Path, st, Follow))
1033 return file_type::status_error;
1038 return status.
type() == file_type::directory_file;
1043 if (std::error_code ec =
status(path, st))
1046 return std::error_code();
1050 return status.
type() == file_type::regular_file;
1055 if (std::error_code ec =
status(path, st))
1058 return std::error_code();
1062 return status.
type() == file_type::symlink_file;
1067 if (std::error_code ec =
status(path, st,
false))
1070 return std::error_code();
1081 if (std::error_code EC =
status(Path, FileStatus))
1084 return std::error_code();
1091 this->Path = PathStr.
str();
1093 this->Status = Status;
1098 if (std::error_code EC =
status(Path, Status))
1109 #if defined(LLVM_ON_UNIX) 1119 TempFile::TempFile(
StringRef Name,
int FD) : TmpName(Name), FD(FD) {}
1122 TmpName = std::move(
Other.TmpName);
1132 std::error_code RemoveEC;
1136 if (!TmpName.empty()) {
1145 if (FD != -1 && close(FD) == -1) {
1146 std::error_code EC = std::error_code(errno, std::generic_category());
1160 auto H =
reinterpret_cast<HANDLE
>(_get_osfhandle(FD));
1161 std::error_code RenameEC = setDeleteDisposition(
H,
false);
1163 RenameEC = rename_fd(FD, Name);
1166 std::error_code(ERROR_NOT_SAME_DEVICE, std::system_category())) {
1168 setDeleteDisposition(
H,
true);
1174 setDeleteDisposition(
H,
true);
1176 std::error_code RenameEC =
fs::rename(TmpName, Name);
1190 if (close(FD) == -1) {
1191 std::error_code EC(errno, std::generic_category());
1204 auto H =
reinterpret_cast<HANDLE
>(_get_osfhandle(FD));
1205 if (std::error_code EC = setDeleteDisposition(
H,
false))
1213 if (close(FD) == -1) {
1214 std::error_code EC(errno, std::generic_category());
1225 if (std::error_code EC =
1238 return std::move(Ret);
bool is_separator(char value, Style style=Style::native)
Check whether the given char is a path separator on the host OS.
std::error_code create_directories(const Twine &path, bool IgnoreExisting=true, perms Perms=owner_all|group_all)
Create all the non-existent directories in path.
const_iterator end(StringRef path)
Get end iterator over path.
std::error_code openFileForReadWrite(const Twine &Name, int &ResultFD, CreationDisposition Disp, OpenFlags Flags, unsigned Mode=0666)
Opens the file with the given name in a write-only or read-write mode, returning its open file descri...
Represents either an error or a value T.
void remove_filename(SmallVectorImpl< char > &path, Style style=Style::native)
Remove the last component from path unless it is the root dir.
LLVM_NODISCARD std::string str() const
str - Get the contents as an std::string.
const_iterator begin(StringRef path, Style style=Style::native)
Get begin iterator over path.
void replace_path_prefix(SmallVectorImpl< char > &Path, const StringRef &OldPrefix, const StringRef &NewPrefix, Style style=Style::native)
Replace matching path prefix with another path.
bool status_known(const basic_file_status &s)
Is status available?
This class represents lattice values for constants.
std::error_code openFileForRead(const Twine &Name, int &ResultFD, OpenFlags Flags=OF_None, SmallVectorImpl< char > *RealPath=nullptr)
Opens the file with the given name in a read-only mode, returning its open file descriptor.
std::error_code remove(const Twine &path, bool IgnoreNonExisting=true)
Remove path.
void push_back(const T &Elt)
perms permissions() const
LLVM_NODISCARD LLVM_ATTRIBUTE_ALWAYS_INLINE size_t size() const
size - Get the string size.
bool isTriviallyEmpty() const
Check if this twine is trivially empty; a false return value does not necessarily mean the twine is e...
StringRef toStringRef(SmallVectorImpl< char > &Out) const
This returns the twine as a single StringRef if it can be represented as such.
static std::error_code createTemporaryFile(const Twine &Prefix, StringRef Suffix, int &ResultFD, llvm::SmallVectorImpl< char > &ResultPath, FSEntity Type)
bool has_extension(const Twine &path, Style style=Style::native)
Has extension?
LLVM_NODISCARD size_t find_last_of(char C, size_t From=npos) const
Find the last character in the string that is C, or npos if not found.
std::error_code current_path(SmallVectorImpl< char > &result)
Get the current path.
bool is_directory(const basic_file_status &status)
Does status represent a directory?
std::string convert_to_slash(StringRef path, Style style=Style::native)
Replaces backslashes with slashes if Windows.
bool is_regular_file(const basic_file_status &status)
Does status represent a regular file?
StringRef remove_leading_dotslash(StringRef path, Style style=Style::native)
Remove redundant leading "./" pieces and consecutive separators.
Represents the result of a call to sys::fs::status().
LLVM_NODISCARD LLVM_ATTRIBUTE_ALWAYS_INLINE const char * data() const
data - Get a pointer to the start of the string (which may not be null terminated).
ErrorOr< MD5::MD5Result > md5_contents(int FD)
Compute an MD5 hash of a file's contents.
ErrorOr< perms > getPermissions(const Twine &Path)
Get file permissions.
bool has_stem(const Twine &path, Style style=Style::native)
Has stem?
void append(SmallVectorImpl< char > &path, const Twine &a, const Twine &b="", const Twine &c="", const Twine &d="")
Append to path.
amdgpu Simplify well known AMD library false Value Value const Twine & Name
reverse_iterator rbegin(StringRef path, Style style=Style::native)
Get reverse begin iterator over path.
Twine - A lightweight data structure for efficiently representing the concatenation of temporary valu...
ArrayRef< T > makeArrayRef(const T &OneElt)
Construct an ArrayRef from a single element.
void update(ArrayRef< uint8_t > Data)
Updates the hash for the byte stream provided.
Represents a temporary file.
void make_absolute(const Twine ¤t_directory, SmallVectorImpl< char > &path)
Make path an absolute path.
bool is_absolute(const Twine &path, Style style=Style::native)
Is path absolute?
Tagged union holding either a T or a Error.
StringRef root_path(StringRef path, Style style=Style::native)
Get root path.
StringRef getSingleStringRef() const
This returns the twine as a single StringRef.
Position
Position to insert a new instruction relative to an existing instruction.
StringRef str() const
Explicit conversion to StringRef.
CD_CreateNew - When opening a file:
bool isSingleStringRef() const
Return true if this twine can be dynamically accessed as a single StringRef value with getSingleStrin...
LLVM_NODISCARD LLVM_ATTRIBUTE_ALWAYS_INLINE bool empty() const
empty - Check if the string is empty.
LLVM_NODISCARD LLVM_ATTRIBUTE_ALWAYS_INLINE StringRef substr(size_t Start, size_t N=npos) const
Return a reference to the substring from [Start, Start + N).
Delete the file on close. Only makes a difference on windows.
void write(void *memory, value_type value, endianness endian)
Write a value to memory with a particular endianness.
void append(in_iter S, in_iter E)
Append from an iterator pair.
Represents the result of a call to directory_iterator::status().
std::error_code copy_file(const Twine &From, const Twine &To)
Copy the contents of From to To.
static SmallString< 256 > remove_dots(StringRef path, bool remove_dot_dot, Style style)
The instances of the Type class are immutable: once they are created, they are never changed...
void swap(SmallVectorImpl &RHS)
bool has_parent_path(const Twine &path, Style style=Style::native)
Has parent path?
UniqueID getUniqueID() const
std::error_code status(const Twine &path, file_status &result, bool follow=true)
Get file status as if by POSIX stat().
Error errorCodeToError(std::error_code EC)
Helper for converting an std::error_code to a Error.
LLVM_ATTRIBUTE_ALWAYS_INLINE iterator begin()
bool has_filename(const Twine &path, Style style=Style::native)
Has filename?
StringRef toNullTerminatedStringRef(SmallVectorImpl< char > &Out) const
This returns the twine as a single null terminated StringRef if it can be represented as such...
std::error_code getUniqueID(const Twine Path, UniqueID &Result)
static void replace(Module &M, GlobalVariable *Old, GlobalVariable *New)
file_type get_file_type(const Twine &Path, bool Follow=true)
Does status represent a directory?
void DontRemoveFileOnSignal(StringRef Filename)
This function removes a file from the list of files to be removed on signal delivery.
StringRef parent_path(StringRef path, Style style=Style::native)
Get parent path.
void toVector(SmallVectorImpl< char > &Out) const
Append the concatenated string into the given SmallString or SmallVector.
std::error_code openFileForWrite(const Twine &Name, int &ResultFD, CreationDisposition Disp=CD_CreateAlways, OpenFlags Flags=OF_None, unsigned Mode=0666)
Opens the file with the given name in a write-only or read-write mode, returning its open file descri...
void consumeError(Error Err)
Consume a Error without doing anything.
StringRef get_separator(Style style=Style::native)
Return the preferred separator for this platform.
#define llvm_unreachable(msg)
Marks that the current location is not supposed to be reachable.
iterator_range< T > make_range(T x, T y)
Convenience function for iterating over sub-ranges.
static ErrorSuccess success()
Create a success value.
BlockVerifier::State From
static std::error_code createUniqueFile(const Twine &Model, int &ResultFd, SmallVectorImpl< char > &ResultPath, unsigned Mode, OpenFlags Flags)
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)
This is a 'vector' (really, a variable-sized array), optimized for the case when the array is small...
StringRef root_directory(StringRef path, Style style=Style::native)
Get root directory.
void replace_extension(SmallVectorImpl< char > &path, const Twine &extension, Style style=Style::native)
Replace the file extension of path with extension.
bool RemoveFileOnSignal(StringRef Filename, std::string *ErrMsg=nullptr)
This function registers signal handlers to ensure that if a signal gets delivered that the named file...
StringRef root_name(StringRef path, Style style=Style::native)
Get root name.
std::error_code createUniqueDirectory(const Twine &Prefix, SmallVectorImpl< char > &ResultPath)
bool is_other(const basic_file_status &status)
Does this status represent something that exists but is not a directory or regular file...
bool has_root_directory(const Twine &path, Style style=Style::native)
Has root directory?
void append(in_iter in_start, in_iter in_end)
Add the specified range to the end of the SmallVector.
bool has_root_path(const Twine &path, Style style=Style::native)
Has root path?
static unsigned GetRandomNumber()
Get the result of a process wide random number generator.
bool has_relative_path(const Twine &path, Style style=Style::native)
Has relative path?
LLVM_ATTRIBUTE_ALWAYS_INLINE iterator end()
Provides a library for accessing information about this process and other processes on the operating ...
pointer data()
Return a pointer to the vector's buffer, even if empty().
file_type
An enumeration for the file system's view of the type.
StringRef filename(StringRef path, Style style=Style::native)
Get filename.
LLVM_NODISCARD bool empty() const
value_type read(const void *memory, endianness endian)
Read a value of a particular endianness from memory.
LLVM_NODISCARD size_t find_first_of(char C, size_t From=0) const
Find the first character in the string that is C, or npos if not found.
bool is_relative(const Twine &path, Style style=Style::native)
Is path relative?
std::error_code getPotentiallyUniqueTempFileName(const Twine &Prefix, StringRef Suffix, SmallVectorImpl< char > &ResultPath)
Get a unique temporary file name, not currently exisiting in the filesystem.
static void rename(GlobalValue *GV)
std::error_code create_directory(const Twine &path, bool IgnoreExisting=true, perms Perms=owner_all|group_all)
Create the directory in path.
reverse_iterator rend(StringRef path)
Get reverse end iterator over path.
std::error_code getPotentiallyUniqueFileName(const Twine &Model, SmallVectorImpl< char > &ResultPath)
Get a unique name, not currently exisiting in the filesystem.
void system_temp_directory(bool erasedOnReboot, SmallVectorImpl< char > &result)
Get the typical temporary directory for the system, e.g., "/var/tmp" or "C:/TEMP".
assert(ImpDefSCC.getReg()==AMDGPU::SCC &&ImpDefSCC.isDef())
CD_CreateAlways - When opening a file:
StringRef relative_path(StringRef path, Style style=Style::native)
Get relative path.
void set_size(size_t Size)
Set the array size to N, which the current array must have enough capacity for.
Lightweight error class with error context and mandatory checking.
bool home_directory(SmallVectorImpl< char > &result)
Get the user's home directory.
std::error_code access(const Twine &Path, AccessMode Mode)
Can the file be accessed?
void final(MD5Result &Result)
Finishes off the hash and puts the result in result.
StringRef stem(StringRef path, Style style=Style::native)
Get stem.
StringRef - Represent a constant reference to a string, i.e.
bool operator==(uint64_t V1, const APInt &V2)
bool has_root_name(const Twine &path, Style style=Style::native)
Has root name?
bool is_symlink_file(const basic_file_status &status)
Does status represent a symlink file?
bool exists(const basic_file_status &status)
Does file exist?
OutputIt copy(R &&Range, OutputIt Out)
StringRef extension(StringRef path, Style style=Style::native)
Get extension.
static std::error_code copy_file_internal(int ReadFD, int WriteFD)