24 #if !defined(_MSC_VER) && !defined(__MINGW32__)
31 using namespace llvm::support::endian;
38 const char *separators =
"\\/";
39 const char preferred_separator =
'\\';
41 const char separators =
'/';
42 const char preferred_separator =
'/';
57 if (path.
size() >= 2 && std::isalpha(static_cast<unsigned char>(path[0])) &&
63 if ((path.
size() > 2) &&
69 return path.
substr(0, end);
78 return path.
substr(0, end);
82 if (str.
size() == 2 &&
88 return str.
size() - 1;
107 if (str.
size() > 2 &&
114 if (str.
size() == 2 &&
120 if (str.
size() > 3 &&
135 size_t end_pos = filename_pos(path);
140 size_t root_dir_pos = root_dir_start(path.
substr(0, end_pos));
143 (end_pos - 1) != root_dir_pos &&
147 if (end_pos == 1 && root_dir_pos == 0 && filename_was_sep)
162 bool MakeAbsolute,
unsigned Mode,
173 ModelStorage.
swap(TDir);
179 ResultPath = ModelStorage;
186 for (
unsigned i = 0, e = ModelStorage.
size(); i != e; ++i) {
187 if (ModelStorage[i] ==
'%')
194 if (std::error_code EC =
198 goto retry_random_path;
202 return std::error_code();
209 return std::error_code();
212 goto retry_random_path;
216 if (std::error_code EC =
219 goto retry_random_path;
222 return std::error_code();
235 i.Component = find_first_component(path);
243 i.Position = path.
size();
248 assert(Position < Path.size() &&
"Tried to increment past end!");
251 Position += Component.size();
254 if (Position == Path.size()) {
261 bool was_net = Component.size() > 2 &&
263 Component[1] == Component[0] &&
272 || Component.endswith(
":")
275 Component = Path.substr(Position, 1);
280 while (Position != Path.size() &&
286 if (Position == Path.size()) {
294 size_t end_pos = Path.find_first_of(separators, Position);
295 Component = Path.slice(Position, end_pos);
301 return Path.begin() == RHS.Path.
begin() && Position == RHS.Position;
305 return Position - RHS.Position;
311 I.Position = Path.
size();
318 I.Component = Path.
substr(0, 0);
326 size_t root_dir_pos = root_dir_start(Path);
327 if (Position == Path.size() &&
328 Path.size() > root_dir_pos + 1 &&
336 size_t end_pos = Position;
339 (end_pos - 1) != root_dir_pos &&
344 size_t start_pos = filename_pos(Path.substr(0, end_pos));
345 Component = Path.slice(start_pos, end_pos);
346 Position = start_pos;
351 return Path.begin() == RHS.Path.
begin() && Component == RHS.Component &&
352 Position == RHS.Position;
360 bool has_net = b->size() > 2 &&
is_separator((*b)[0]) && (*b)[1] == (*b)[0];
368 if (has_net || has_drive) {
371 return path.
substr(0, b->size() + pos->size());
391 bool has_net = b->size() > 2 &&
is_separator((*b)[0]) && (*b)[1] == (*b)[0];
399 if (has_net || has_drive) {
414 bool has_net = b->size() > 2 &&
is_separator((*b)[0]) && (*b)[1] == (*b)[0];
422 if ((has_net || has_drive) &&
459 e = components.
end();
462 bool component_has_sep = !i->empty() &&
is_separator((*i)[0]);
467 size_t loc = i->find_first_not_of(separators);
475 if (!component_has_sep && !(path.
empty() || is_root_name)) {
480 path.
append(i->begin(), i->end());
491 size_t end_pos = parent_path_end(path);
495 return path.
substr(0, end_pos);
515 if (ext.
size() > 0 && ext[0] !=
'.')
525 "path and result are not allowed to overlap!");
534 std::replace(Path.
begin(), Path.
end(),
'/',
'\\');
536 for (
auto PI = Path.
begin(), PE = Path.
end(); PI < PE; ++PI) {
539 if (PN < PE && *PN ==
'\\')
558 if ((fname.
size() == 1 && fname ==
".") ||
559 (fname.
size() == 2 && fname ==
".."))
562 return fname.
substr(0, pos);
571 if ((fname.
size() == 1 && fname ==
".") ||
572 (fname.
size() == 2 && fname ==
".."))
583 case '/':
return true;
584 default:
return false;
661 return rootDir && rootName;
674 std::error_code EC =
status(Path, Status);
678 return std::error_code();
693 static std::error_code
699 "Model must be a simple filename.");
705 static std::error_code
708 const char *Middle = Suffix.
empty() ?
"-%%%%%%" :
"-%%%%%%.";
746 if (rootName && rootDirectory)
747 return std::error_code();
755 if (!rootName && !rootDirectory) {
759 path.
swap(current_dir);
760 return std::error_code();
763 if (!rootName && rootDirectory) {
768 path.
swap(curDirRootName);
769 return std::error_code();
772 if (rootName && !rootDirectory) {
779 path::append(res, pRootName, bRootDirectory, bRelativePath, pRelativePath);
781 return std::error_code();
820 const size_t BufSize = 4096;
821 char *Buf =
new char[BufSize];
822 int BytesRead = 0, BytesWritten = 0;
824 BytesRead =
read(ReadFD, Buf, BufSize);
828 BytesWritten =
write(WriteFD, Buf, BytesRead);
829 if (BytesWritten < 0)
831 BytesRead -= BytesWritten;
833 if (BytesWritten < 0)
840 if (BytesRead < 0 || BytesWritten < 0)
841 return std::error_code(errno, std::generic_category());
842 return std::error_code();
850 return s.
type() != file_type::status_error;
854 return status.
type() == file_type::directory_file;
859 if (std::error_code ec =
status(path, st))
862 return std::error_code();
866 return status.
type() == file_type::regular_file;
871 if (std::error_code ec =
status(path, st))
874 return std::error_code();
885 if (std::error_code EC =
status(Path, FileStatus))
888 return std::error_code();
901 if (Magic.
size() < 4)
902 return file_magic::unknown;
903 switch ((
unsigned char)Magic[0]) {
906 if (Magic[1] == (
char)0x00 && Magic[2] == (char)0xff &&
907 Magic[3] == (
char)0xff) {
909 if (Magic.
size() < MinSize)
910 return file_magic::coff_import_library;
915 return file_magic::coff_import_library;
919 return file_magic::coff_import_library;
920 return file_magic::coff_object;
923 const char Expected[] = { 0, 0, 0, 0,
'\x20', 0, 0, 0,
'\xff' };
924 if (Magic.
size() >=
sizeof(Expected) &&
925 memcmp(Magic.
data(), Expected,
sizeof(Expected)) == 0)
926 return file_magic::windows_resource;
929 return file_magic::coff_object;
933 if (Magic[1] == (
char)0xC0 && Magic[2] == (char)0x17 &&
934 Magic[3] == (
char)0x0B)
935 return file_magic::bitcode;
938 if (Magic[1] ==
'C' && Magic[2] == (
char)0xC0 && Magic[3] == (char)0xDE)
939 return file_magic::bitcode;
942 if (Magic.
size() >= 8)
943 if (memcmp(Magic.
data(),
"!<arch>\n",8) == 0)
944 return file_magic::archive;
948 if (Magic.
size() >= 18 && Magic[1] ==
'E' && Magic[2] ==
'L' &&
950 bool Data2MSB = Magic[5] == 2;
951 unsigned high = Data2MSB ? 16 : 17;
952 unsigned low = Data2MSB ? 17 : 16;
953 if (Magic[high] == 0)
954 switch (Magic[low]) {
955 default:
return file_magic::elf;
956 case 1:
return file_magic::elf_relocatable;
957 case 2:
return file_magic::elf_executable;
958 case 3:
return file_magic::elf_shared_object;
959 case 4:
return file_magic::elf_core;
963 return file_magic::elf;
968 if (Magic[1] ==
char(0xFE) && Magic[2] == char(0xBA) &&
969 Magic[3] == char(0xBE)) {
972 if (Magic.
size() >= 8 && Magic[7] < 43)
973 return file_magic::macho_universal_binary;
984 if (Magic[0] ==
char(0xFE) && Magic[1] == char(0xED) &&
985 Magic[2] == char(0xFA) &&
986 (Magic[3] == char(0xCE) || Magic[3] == char(0xCF))) {
988 if (Magic.
size() >= 16) type = Magic[14] << 8 | Magic[15];
989 }
else if ((Magic[0] ==
char(0xCE) || Magic[0] ==
char(0xCF)) &&
990 Magic[1] == char(0xFA) && Magic[2] == char(0xED) &&
991 Magic[3] == char(0xFE)) {
993 if (Magic.
size() >= 14) type = Magic[13] << 8 | Magic[12];
997 case 1:
return file_magic::macho_object;
998 case 2:
return file_magic::macho_executable;
999 case 3:
return file_magic::macho_fixed_virtual_memory_shared_lib;
1000 case 4:
return file_magic::macho_core;
1001 case 5:
return file_magic::macho_preload_executable;
1002 case 6:
return file_magic::macho_dynamically_linked_shared_lib;
1003 case 7:
return file_magic::macho_dynamic_linker;
1004 case 8:
return file_magic::macho_bundle;
1005 case 9:
return file_magic::macho_dynamically_linked_shared_lib_stub;
1006 case 10:
return file_magic::macho_dsym_companion;
1007 case 11:
return file_magic::macho_kext_bundle;
1018 if (Magic[1] == 0x01)
1019 return file_magic::coff_object;
1023 if (Magic[1] == 0x02)
1024 return file_magic::coff_object;
1028 if (Magic[1] ==
'Z') {
1031 if (off < Magic.
size() &&
1033 return file_magic::pecoff_executable;
1038 if (Magic[1] ==
char(0
x86))
1039 return file_magic::coff_object;
1045 return file_magic::unknown;
1054 int Length =
read(FD, Buffer,
sizeof(Buffer));
1055 if (close(FD) != 0 || Length < 0)
1056 return std::error_code(errno, std::generic_category());
1059 return std::error_code();
1071 #if defined(LLVM_ON_UNIX)
1074 #if defined(LLVM_ON_WIN32)
void toVector(SmallVectorImpl< char > &Out) const
Append the concatenated string into the given SmallString or SmallVector.
void push_back(const T &Elt)
const_iterator end(StringRef path)
Get end iterator over path.
bool isSingleStringRef() const
Return true if this twine can be dynamically accessed as a single StringRef value with getSingleStrin...
StringRef root_name(StringRef path)
Get root name.
StringRef stem(StringRef path)
Get stem.
size_t size() const
size - Get the string size.
std::error_code create_directories(const Twine &path, bool IgnoreExisting=true)
Create all the non-existent directories in path.
value_type read(const void *memory)
Read a value of a particular endianness from memory.
std::error_code createUniqueFile(const Twine &Model, int &ResultFD, SmallVectorImpl< char > &ResultPath, unsigned Mode=all_read|all_write)
Create a uniquely named file.
F_Excl - When opening a file, this flag makes raw_fd_ostream report an error if the file already exis...
bool is_relative(const Twine &path)
Is path relative?
static std::error_code createTemporaryFile(const Twine &Prefix, StringRef Suffix, int &ResultFD, llvm::SmallVectorImpl< char > &ResultPath, FSEntity Type)
void replace_extension(SmallVectorImpl< char > &path, const Twine &extension)
Replace the file extension of path with extension.
UniqueID getUniqueID() const
bool has_stem(const Twine &path)
Has stem?
StringRef substr(size_t Start, size_t N=npos) const
Return a reference to the substring from [Start, Start + N).
StringRef root_directory(StringRef path)
Get root directory.
std::string str() const
str - Get the contents as an std::string.
std::error_code current_path(SmallVectorImpl< char > &result)
Get the current path.
const_iterator begin(StringRef path)
Get begin iterator over path.
std::error_code make_absolute(SmallVectorImpl< char > &path)
Make path an absolute path.
static std::error_code createUniqueEntity(const Twine &Model, int &ResultFD, SmallVectorImpl< char > &ResultPath, bool MakeAbsolute, unsigned Mode, FSEntity Type)
bool has_root_directory(const Twine &path)
Has root directory?
file_status - Represents the result of a call to stat and friends.
static const char BigObjMagic[]
bool status_known(file_status s)
Is status available?
void append(SmallVectorImpl< char > &path, const Twine &a, const Twine &b="", const Twine &c="", const Twine &d="")
Append to path.
void remove_filename(SmallVectorImpl< char > &path)
Remove the last component from path unless it is the root dir.
Twine - A lightweight data structure for efficiently representing the concatenation of temporary valu...
#define llvm_unreachable(msg)
Marks that the current location is not supposed to be reachable.
bool is_absolute(const Twine &path)
Is path absolute?
StringRef extension(StringRef path)
Get extension.
bool LLVM_ATTRIBUTE_UNUSED_RESULT empty() const
bool is_separator(char value)
Check whether the given char is a path separator on the host OS.
const char * data() const
data - Get a pointer to the start of the string (which may not be null terminated).
std::error_code create_directory(const Twine &path, bool IgnoreExisting=true)
Create the directory in path.
static const char preferred_separator_string[]
StringRef root_path(StringRef path)
Get root path.
bool has_relative_path(const Twine &path)
Has relative path?
StringRef getSingleStringRef() const
This returns the twine as a single StringRef.
Open the file for read and write.
std::error_code copy_file(const Twine &From, const Twine &To)
Copy the contents of From to To.
StringRef filename(StringRef path)
Get filename.
uint32_t read32le(const void *p)
The instances of the Type class are immutable: once they are created, they are never changed...
void swap(SmallVectorImpl &RHS)
bool has_filename(const Twine &path)
Has filename?
bool is_other(file_status status)
Does this status represent something that exists but is not a directory, regular file, or symlink?
std::error_code getUniqueID(const Twine Path, UniqueID &Result)
void append(in_iter in_start, in_iter in_end)
Add the specified range to the end of the SmallVector.
bool is_directory(file_status status)
Does status represent a directory?
static const char *const Magic
file_magic identify_magic(StringRef magic)
Identify the type of a binary file based on how magical it is.
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.
StringRef relative_path(StringRef path)
Get relative path.
StringRef toStringRef(SmallVectorImpl< char > &Out) const
This returns the twine as a single StringRef if it can be represented as such.
This is a 'vector' (really, a variable-sized array), optimized for the case when the array is small...
std::error_code createUniqueDirectory(const Twine &Prefix, SmallVectorImpl< char > &ResultPath)
StringRef parent_path(StringRef path)
Get parent path.
bool is_regular_file(file_status status)
Does status represent a regular file?
StringRef get_separator()
Return the preferred separator for this platform.
void write(void *memory, value_type value)
Write a value to memory with a particular endianness.
static unsigned GetRandomNumber()
Get the result of a process wide random number generator.
void set_size(size_type N)
Set the array size to N, which the current array must have enough capacity for.
Provides a library for accessing information about this process and other processes on the operating ...
file_magic - An "enum class" enumeration of file types based on magic (the first N bytes of the file)...
pointer data()
Return a pointer to the vector's buffer, even if empty().
std::error_code openFileForRead(const Twine &Name, int &ResultFD)
uint16_t read16le(const void *p)
reverse_iterator rbegin(StringRef path)
Get reverse begin iterator over path.
static const char PEMagic[]
bool has_root_path(const Twine &path)
Has root path?
bool has_root_name(const Twine &path)
Has root name?
reverse_iterator rend(StringRef path)
Get reverse end iterator over path.
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.
void system_temp_directory(bool erasedOnReboot, SmallVectorImpl< char > &result)
Get the typical temporary directory for the system, e.g., "/var/tmp" or "C:/TEMP".
StringRef toNullTerminatedStringRef(SmallVectorImpl< char > &Out) const
This returns the twine as a single null terminated StringRef if it can be represented as such...
bool isTriviallyEmpty() const
Check if this twine is trivially empty; a false return value does not necessarily mean the twine is e...
bool has_extension(const Twine &path)
Has extension?
std::error_code access(const Twine &Path, AccessMode Mode)
Can the file be accessed?
StringRef - Represent a constant reference to a string, i.e.
bool operator==(uint64_t V1, const APInt &V2)
std::error_code status(const Twine &path, file_status &result)
Get file status as if by POSIX stat().
bool exists(file_status status)
Does file exist?
void operator-(int, ilist_iterator< T >)=delete
bool has_parent_path(const Twine &path)
Has parent path?
bool empty() const
empty - Check if the string is empty.
std::error_code openFileForWrite(const Twine &Name, int &ResultFD, OpenFlags Flags, unsigned Mode=0666)