Also eliminate redundant spills downstream of inserted reloads.
[oota-llvm.git] / lib / Support / PathV2.cpp
index 3b232abf7d00e7edfccfdcb1019268522d327971..896c94c071bcaa6acf792daca6498b43eccabd30 100644 (file)
 
 namespace {
   using llvm::StringRef;
-
-  bool is_separator(const char value) {
-    switch(value) {
-#ifdef LLVM_ON_WIN32
-    case '\\': // fall through
-#endif
-    case '/': return true;
-    default: return false;
-    }
-  }
+  using llvm::sys::path::is_separator;
 
 #ifdef LLVM_ON_WIN32
   const StringRef separators = "\\/";
@@ -154,7 +145,7 @@ namespace {
 
     return end_pos;
   }
-}
+} // end unnamed namespace
 
 namespace llvm {
 namespace sys  {
@@ -400,6 +391,12 @@ void append(SmallVectorImpl<char> &path, const Twine &a,
   }
 }
 
+void append(SmallVectorImpl<char> &path,
+            const_iterator begin, const_iterator end) {
+  for (; begin != end; ++begin)
+    path::append(path, *begin);
+}
+
 const StringRef parent_path(StringRef path) {
   size_t end_pos = parent_path_end(path);
   if (end_pos == StringRef::npos)
@@ -483,6 +480,16 @@ const StringRef extension(StringRef path) {
       return fname.substr(pos);
 }
 
+bool is_separator(char value) {
+  switch(value) {
+#ifdef LLVM_ON_WIN32
+    case '\\': // fall through
+#endif
+    case '/': return true;
+    default: return false;
+  }
+}
+
 bool has_root_name(const Twine &path) {
   SmallString<128> path_storage;
   StringRef p = path.toStringRef(path_storage);
@@ -636,14 +643,38 @@ bool is_directory(file_status status) {
   return status.type() == file_type::directory_file;
 }
 
+error_code is_directory(const Twine &path, bool &result) {
+  file_status st;
+  if (error_code ec = status(path, st))
+    return ec;
+  result = is_directory(st);
+  return success;
+}
+
 bool is_regular_file(file_status status) {
   return status.type() == file_type::regular_file;
 }
 
+error_code is_regular_file(const Twine &path, bool &result) {
+  file_status st;
+  if (error_code ec = status(path, st))
+    return ec;
+  result = is_regular_file(st);
+  return success;
+}
+
 bool is_symlink(file_status status) {
   return status.type() == file_type::symlink_file;
 }
 
+error_code is_symlink(const Twine &path, bool &result) {
+  file_status st;
+  if (error_code ec = status(path, st))
+    return ec;
+  result = is_symlink(st);
+  return success;
+}
+
 bool is_other(file_status status) {
   return exists(status) &&
          !is_regular_file(status) &&
@@ -662,38 +693,30 @@ void directory_entry::replace_filename(const Twine &filename, file_status st,
 }
 
 error_code has_magic(const Twine &path, const Twine &magic, bool &result) {
-  SmallString<128> PathStorage;
   SmallString<32>  MagicStorage;
-  StringRef Path  = path.toNullTerminatedStringRef(PathStorage);
-  StringRef Magic = magic.toNullTerminatedStringRef(MagicStorage);
-
-  assert(Magic.size() > 0 && "magic must be non-empty!");
-
-  SmallString<32> BufferStorage;
-  BufferStorage.reserve(Magic.size());
-
-  // Open file.
-  std::FILE *file = std::fopen(Path.data(), "rb");
-  if (file == 0)
-    return error_code(errno, posix_category());
-  size_t size = ::fread(BufferStorage.data(), 1, Magic.size(), file);
-  if (size != Magic.size()) {
-    int error = errno;
-    bool eof = std::feof(file) != 0;
-    std::fclose(file);
-    if (eof) {
-      // EOF, return false.
+  StringRef Magic = magic.toStringRef(MagicStorage);
+  SmallString<32> Buffer;
+
+  if (error_code ec = get_magic(path, Magic.size(), Buffer)) {
+    if (ec == errc::value_too_large) {
+      // Magic.size() > file_size(Path).
       result = false;
       return success;
     }
-    return error_code(error, posix_category());
+    return ec;
   }
-  std::fclose(file);
 
-  if (std::memcmp(BufferStorage.data(), Magic.data(), Magic.size()) != 0)
-    result = false;
-  else
-    result = true;
+  result = Magic == Buffer;
+  return success;
+}
+
+error_code identify_magic(const Twine &path, LLVMFileType &result) {
+  SmallString<32> Magic;
+  error_code ec = get_magic(path, Magic.capacity(), Magic);
+  if (ec && ec != errc::value_too_large)
+    return ec;
+
+  result = IdentifyFileType(Magic.data(), Magic.size());
   return success;
 }
 
@@ -721,7 +744,7 @@ error_code remove_all_r(StringRef path, file_type ft, uint32_t &count) {
 
   return success;
 }
-}
+} // end unnamed namespace
 
 error_code remove_all(const Twine &path, uint32_t &num_removed) {
   SmallString<128> path_storage;