15 #include "llvm/Config/llvm-config.h" 25 #if !defined(_MSC_VER) && !defined(__MINGW32__) 41 return (style == Style::posix) ? Style::posix : Style::windows;
43 return (style == Style::windows) ? Style::windows : Style::posix;
47 inline const char *separators(
Style style) {
48 if (real_style(style) == Style::windows)
53 inline char preferred_separator(
Style style) {
54 if (real_style(style) == Style::windows)
69 if (real_style(style) == Style::windows) {
71 if (path.
size() >= 2 &&
72 std::isalpha(static_cast<unsigned char>(path[0])) && path[1] ==
':')
81 return path.
substr(0, end);
90 return path.
substr(0, end);
97 return str.
size() - 1;
101 if (real_style(style) == Style::windows) {
116 if (real_style(style) == Style::windows) {
138 size_t end_pos = filename_pos(path, style);
140 bool filename_was_sep =
144 size_t root_dir_pos = root_dir_start(path, style);
145 while (end_pos > 0 &&
150 if (end_pos == root_dir_pos && !filename_was_sep) {
153 return root_dir_pos + 1;
167 static std::error_code
178 for (
int Retries = 128; Retries > 0; --Retries) {
193 return std::error_code();
199 return std::error_code();
212 return std::error_code();
227 i.Component = find_first_component(path, style);
236 i.Position = path.
size();
247 if (Position == Path.size()) {
254 bool was_net = Component.size() > 2 &&
is_separator(Component[0], S) &&
255 Component[1] == Component[0] && !
is_separator(Component[2], S);
262 (real_style(S) == Style::windows && Component.endswith(
":"))) {
263 Component = Path.substr(Position, 1);
268 while (Position != Path.size() &&
is_separator(Path[Position], S)) {
273 if (Position == Path.size() && Component !=
"/") {
281 size_t end_pos = Path.find_first_of(separators(S), Position);
282 Component = Path.slice(Position, end_pos);
288 return Path.begin() == RHS.Path.
begin() &&
Position == RHS.Position;
298 I.Position = Path.
size();
307 I.Component = Path.
substr(0, 0);
313 size_t root_dir_pos = root_dir_start(Path, S);
317 while (end_pos > 0 && (end_pos - 1) != root_dir_pos &&
322 if (
Position == Path.size() && !Path.empty() &&
331 size_t start_pos = filename_pos(Path.substr(0, end_pos), S);
332 Component = Path.slice(start_pos, end_pos);
338 return Path.begin() == RHS.Path.
begin() && Component == RHS.Component &&
350 b->size() > 2 &&
is_separator((*b)[0], style) && (*b)[1] == (*b)[0];
351 bool has_drive = (real_style(style) == Style::windows) && b->endswith(
":");
353 if (has_net || has_drive) {
356 return path.
substr(0, b->size() + pos->size());
376 b->size() > 2 &&
is_separator((*b)[0], style) && (*b)[1] == (*b)[0];
377 bool has_drive = (real_style(style) == Style::windows) && b->endswith(
":");
379 if (has_net || has_drive) {
393 b->size() > 2 &&
is_separator((*b)[0], style) && (*b)[1] == (*b)[0];
394 bool has_drive = (real_style(style) == Style::windows) && b->endswith(
":");
396 if ((has_net || has_drive) &&
430 for (
auto &component : components) {
435 size_t loc = component.find_first_not_of(separators(style));
443 bool component_has_sep =
444 !component.empty() &&
is_separator(component[0], style);
445 if (!component_has_sep &&
448 path.
push_back(preferred_separator(style));
451 path.
append(component.begin(), component.end());
467 size_t end_pos = parent_path_end(path, style);
471 return path.
substr(0, end_pos);
492 if (ext.
size() > 0 && ext[0] !=
'.')
501 Style style,
bool strict) {
508 if (!strict && OldPrefix.
size() > OrigPath.size())
515 OldPrefixDir = OldPrefix;
517 if (!OrigPath.startswith(OldPrefixDir))
520 if (OrigPath.size() > OldPrefixDir.size())
521 if (!
is_separator(OrigPath[OldPrefixDir.size()], style) && strict)
525 if (OldPrefixDir.size() == NewPrefix.
size() && !strict) {
533 if (!RelPath.
empty()) {
548 "path and result are not allowed to overlap!");
552 native(result, style);
558 if (real_style(style) == Style::windows) {
567 for (
auto PI = Path.
begin(), PE = Path.
end(); PI < PE; ++PI) {
570 if (PN < PE && *PN ==
'\\')
580 if (real_style(style) != Style::windows)
583 std::string s = path.
str();
596 if ((fname.
size() == 1 && fname ==
".") ||
597 (fname.
size() == 2 && fname ==
".."))
600 return fname.
substr(0, pos);
609 if ((fname.
size() == 1 && fname ==
".") ||
610 (fname.
size() == 2 && fname ==
".."))
619 if (real_style(style) == Style::windows)
620 return value ==
'\\';
625 if (real_style(style) == Style::windows)
692 (real_style(style) != Style::windows) ||
has_root_name(p, style);
694 return rootDir && rootName;
722 if (remove_dot_dot &&
C ==
"..") {
723 if (!components.
empty() && components.
back() !=
"..") {
757 std::error_code EC =
status(Path, Status);
761 return std::error_code();
775 ModelStorage.
swap(TDir);
779 ResultPath = ModelStorage;
784 for (
unsigned i = 0,
e = ModelStorage.
size(); i !=
e; ++i) {
785 if (ModelStorage[i] ==
'%')
815 static std::error_code
821 "Model must be a simple filename.");
827 static std::error_code
830 const char *Middle = Suffix.
empty() ?
"-%%%%%%" :
"-%%%%%%.";
884 if ((rootName || real_style(
Style::native) != Style::windows) &&
890 current_directory.
toVector(current_dir);
893 if (!rootName && !rootDirectory) {
897 path.
swap(current_dir);
901 if (!rootName && rootDirectory) {
906 path.
swap(curDirRootName);
910 if (rootName && !rootDirectory) {
917 path::append(res, pRootName, bRootDirectory, bRelativePath, pRelativePath);
963 const size_t BufSize = 4096;
964 char *Buf =
new char[BufSize];
965 int BytesRead = 0, BytesWritten = 0;
967 BytesRead =
read(ReadFD, Buf, BufSize);
971 BytesWritten =
write(WriteFD, Buf, BytesRead);
972 if (BytesWritten < 0)
974 BytesRead -= BytesWritten;
976 if (BytesWritten < 0)
981 if (BytesRead < 0 || BytesWritten < 0)
982 return std::error_code(errno, std::generic_category());
983 return std::error_code();
991 if (std::error_code EC =
1021 constexpr
size_t BufSize = 4096;
1022 std::vector<uint8_t> Buf(BufSize);
1025 BytesRead =
read(FD, Buf.data(), BufSize);
1032 return std::error_code(errno, std::generic_category());
1053 return s.
type() != file_type::status_error;
1058 if (
status(Path, st, Follow))
1059 return file_type::status_error;
1064 return status.
type() == file_type::directory_file;
1069 if (std::error_code ec =
status(path, st))
1072 return std::error_code();
1076 return status.
type() == file_type::regular_file;
1081 if (std::error_code ec =
status(path, st))
1084 return std::error_code();
1088 return status.
type() == file_type::symlink_file;
1093 if (std::error_code ec =
status(path, st,
false))
1096 return std::error_code();
1107 if (std::error_code EC =
status(Path, FileStatus))
1110 return std::error_code();
1117 this->Path = PathStr.
str();
1119 this->Status = Status;
1124 if (std::error_code EC =
status(Path, Status))
1135 #if defined(LLVM_ON_UNIX) 1145 TempFile::TempFile(
StringRef Name,
int FD) : TmpName(Name), FD(FD) {}
1148 TmpName = std::move(
Other.TmpName);
1159 if (FD != -1 && close(FD) == -1) {
1160 std::error_code EC = std::error_code(errno, std::generic_category());
1171 std::error_code RemoveEC;
1172 if (!TmpName.empty()) {
1188 auto H =
reinterpret_cast<HANDLE
>(_get_osfhandle(FD));
1189 std::error_code RenameEC = setDeleteDisposition(
H,
false);
1191 RenameEC = rename_fd(FD, Name);
1194 std::error_code(ERROR_NOT_SAME_DEVICE, std::system_category())) {
1196 setDeleteDisposition(
H,
true);
1202 setDeleteDisposition(
H,
true);
1204 std::error_code RenameEC =
fs::rename(TmpName, Name);
1218 if (close(FD) == -1) {
1219 std::error_code EC(errno, std::generic_category());
1232 auto H =
reinterpret_cast<HANDLE
>(_get_osfhandle(FD));
1233 if (std::error_code EC = setDeleteDisposition(
H,
false))
1241 if (close(FD) == -1) {
1242 std::error_code EC(errno, std::generic_category());
1253 if (std::error_code EC =
1266 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.
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.
amdgpu Simplify well known AMD library false FunctionCallee Value const Twine & Name
std::error_code remove(const Twine &path, bool IgnoreNonExisting=true)
Remove path.
void push_back(const T &Elt)
perms permissions() const
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)
Optional< std::vector< StOtherPiece > > Other
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().
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.
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?
void createUniquePath(const Twine &Model, SmallVectorImpl< char > &ResultPath, bool MakeAbsolute)
Create a potentially unique file name but does not create it.
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.
LLVM_NODISCARD StringRef substr(size_t Start, size_t N=npos) const
Return a reference to the substring from [Start, Start + N).
LLVM_NODISCARD bool empty() const
empty - Check if the string is empty.
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 size_t size() const
size - Get the string size.
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.
void set_size(size_t N)
Set the array size to N, which the current array must have enough capacity for.
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.
bool replace_path_prefix(SmallVectorImpl< char > &Path, const StringRef &OldPrefix, const StringRef &NewPrefix, Style style=Style::native, bool strict=false)
Replace matching path prefix with another 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.
LLVM_NODISCARD char back() const
back - Get the last character in the string.
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 operator==(const StringView &LHS, const StringView &RHS)
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?
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".
LLVM_NODISCARD const char * data() const
data - Get a pointer to the start of the string (which may not be null terminated).
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.
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 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)