[llvm-commits] [llvm] r150197 - in /llvm/trunk: include/llvm/Support/system_error.h lib/Support/DataStream.cpp lib/Support/FileUtilities.cpp lib/Support/MemoryBuffer.cpp lib/Support/PathV2.cpp lib/Support/Unix/PathV2.inc lib/Support/Windows/PathV2.inc

David Blaikie dblaikie at gmail.com
Thu Feb 9 11:24:12 PST 2012


Author: dblaikie
Date: Thu Feb  9 13:24:12 2012
New Revision: 150197

URL: http://llvm.org/viewvc/llvm-project?rev=150197&view=rev
Log:
Change default error_code ctor to a 'named ctor' so it's more self-documenting.

Unify default construction of error_code uses on this idiom so that users don't
feel compelled to make static globals for naming convenience. (unfortunately I
couldn't make the original ctor private as some APIs don't return their result,
instead using an out parameter (that makes sense to default construct) - which
is a bit of a pity. I did, however, find/fix some cases of unnecessary default
construction of error_code before I hit the unfixable cases)

Modified:
    llvm/trunk/include/llvm/Support/system_error.h
    llvm/trunk/lib/Support/DataStream.cpp
    llvm/trunk/lib/Support/FileUtilities.cpp
    llvm/trunk/lib/Support/MemoryBuffer.cpp
    llvm/trunk/lib/Support/PathV2.cpp
    llvm/trunk/lib/Support/Unix/PathV2.inc
    llvm/trunk/lib/Support/Windows/PathV2.inc

Modified: llvm/trunk/include/llvm/Support/system_error.h
URL: http://llvm.org/viewvc/llvm-project/llvm/trunk/include/llvm/Support/system_error.h?rev=150197&r1=150196&r2=150197&view=diff
==============================================================================
--- llvm/trunk/include/llvm/Support/system_error.h (original)
+++ llvm/trunk/include/llvm/Support/system_error.h Thu Feb  9 13:24:12 2012
@@ -738,6 +738,10 @@
 public:
   error_code() : _val_(0), _cat_(&system_category()) {}
 
+  static error_code success() {
+    return error_code();
+  }
+
   error_code(int _val, const error_category& _cat)
     : _val_(_val), _cat_(&_cat) {}
 

Modified: llvm/trunk/lib/Support/DataStream.cpp
URL: http://llvm.org/viewvc/llvm-project/llvm/trunk/lib/Support/DataStream.cpp?rev=150197&r1=150196&r2=150197&view=diff
==============================================================================
--- llvm/trunk/lib/Support/DataStream.cpp (original)
+++ llvm/trunk/lib/Support/DataStream.cpp Thu Feb  9 13:24:12 2012
@@ -67,7 +67,7 @@
     if (Filename == "-") {
       Fd = 0;
       sys::Program::ChangeStdinToBinary();
-      return error_code();
+      return error_code::success();
     }
   
     int OpenFlags = O_RDONLY;
@@ -77,7 +77,7 @@
     Fd = ::open(Filename.c_str(), OpenFlags);
     if (Fd == -1)
       return error_code(errno, posix_category());
-    return error_code();
+    return error_code::success();
   }
 };
 

Modified: llvm/trunk/lib/Support/FileUtilities.cpp
URL: http://llvm.org/viewvc/llvm-project/llvm/trunk/lib/Support/FileUtilities.cpp?rev=150197&r1=150196&r2=150197&view=diff
==============================================================================
--- llvm/trunk/lib/Support/FileUtilities.cpp (original)
+++ llvm/trunk/lib/Support/FileUtilities.cpp Thu Feb  9 13:24:12 2012
@@ -200,7 +200,6 @@
 
   // Now its safe to mmap the files into memory because both files
   // have a non-zero size.
-  error_code ec;
   OwningPtr<MemoryBuffer> F1;
   if (error_code ec = MemoryBuffer::getFile(FileA.c_str(), F1)) {
     if (Error)

Modified: llvm/trunk/lib/Support/MemoryBuffer.cpp
URL: http://llvm.org/viewvc/llvm-project/llvm/trunk/lib/Support/MemoryBuffer.cpp?rev=150197&r1=150196&r2=150197&view=diff
==============================================================================
--- llvm/trunk/lib/Support/MemoryBuffer.cpp (original)
+++ llvm/trunk/lib/Support/MemoryBuffer.cpp Thu Feb  9 13:24:12 2012
@@ -36,8 +36,6 @@
 #include <fcntl.h>
 using namespace llvm;
 
-namespace { const llvm::error_code success; }
-
 //===----------------------------------------------------------------------===//
 // MemoryBuffer implementation itself.
 //===----------------------------------------------------------------------===//
@@ -306,7 +304,7 @@
                                                       RealMapOffset)) {
       result.reset(GetNamedBuffer<MemoryBufferMMapFile>(
           StringRef(Pages + Delta, MapSize), Filename, RequiresNullTerminator));
-      return success;
+      return error_code::success();
     }
   }
 
@@ -344,7 +342,7 @@
   }
 
   result.swap(SB);
-  return success;
+  return error_code::success();
 }
 
 //===----------------------------------------------------------------------===//
@@ -373,5 +371,5 @@
   } while (ReadBytes != 0);
 
   result.reset(getMemBufferCopy(Buffer, "<stdin>"));
-  return success;
+  return error_code::success();
 }

Modified: llvm/trunk/lib/Support/PathV2.cpp
URL: http://llvm.org/viewvc/llvm-project/llvm/trunk/lib/Support/PathV2.cpp?rev=150197&r1=150196&r2=150197&view=diff
==============================================================================
--- llvm/trunk/lib/Support/PathV2.cpp (original)
+++ llvm/trunk/lib/Support/PathV2.cpp Thu Feb  9 13:24:12 2012
@@ -31,8 +31,6 @@
   const char  prefered_separator = '/';
 #endif
 
-  const llvm::error_code success;
-
   StringRef find_first_component(StringRef path) {
     // Look for this first component in the following order.
     // * empty (in this case we return an empty string)
@@ -607,7 +605,7 @@
 
   // Already absolute.
   if (rootName && rootDirectory)
-    return success;
+    return error_code::success();
 
   // All of the following conditions will need the current directory.
   SmallString<128> current_dir;
@@ -619,7 +617,7 @@
     path::append(current_dir, p);
     // Set path to the result.
     path.swap(current_dir);
-    return success;
+    return error_code::success();
   }
 
   if (!rootName && rootDirectory) {
@@ -628,7 +626,7 @@
     path::append(curDirRootName, p);
     // Set path to the result.
     path.swap(curDirRootName);
-    return success;
+    return error_code::success();
   }
 
   if (rootName && !rootDirectory) {
@@ -640,7 +638,7 @@
     SmallString<128> res;
     path::append(res, pRootName, bRootDirectory, bRelativePath, pRelativePath);
     path.swap(res);
-    return success;
+    return error_code::success();
   }
 
   llvm_unreachable("All rootName and rootDirectory combinations should have "
@@ -679,7 +677,7 @@
   if (error_code ec = status(path, st))
     return ec;
   result = is_directory(st);
-  return success;
+  return error_code::success();
 }
 
 bool is_regular_file(file_status status) {
@@ -691,7 +689,7 @@
   if (error_code ec = status(path, st))
     return ec;
   result = is_regular_file(st);
-  return success;
+  return error_code::success();
 }
 
 bool is_symlink(file_status status) {
@@ -703,7 +701,7 @@
   if (error_code ec = status(path, st))
     return ec;
   result = is_symlink(st);
-  return success;
+  return error_code::success();
 }
 
 bool is_other(file_status status) {
@@ -730,13 +728,13 @@
     if (ec == errc::value_too_large) {
       // Magic.size() > file_size(Path).
       result = false;
-      return success;
+      return error_code::success();
     }
     return ec;
   }
 
   result = Magic == Buffer;
-  return success;
+  return error_code::success();
 }
 
 /// @brief Identify the magic in magic.
@@ -857,7 +855,7 @@
     return ec;
 
   result = identify_magic(Magic);
-  return success;
+  return error_code::success();
 }
 
 namespace {
@@ -884,7 +882,7 @@
     ++count;
   }
 
-  return success;
+  return error_code::success();
 }
 } // end unnamed namespace
 

Modified: llvm/trunk/lib/Support/Unix/PathV2.inc
URL: http://llvm.org/viewvc/llvm-project/llvm/trunk/lib/Support/Unix/PathV2.inc?rev=150197&r1=150196&r2=150197&view=diff
==============================================================================
--- llvm/trunk/lib/Support/Unix/PathV2.inc (original)
+++ llvm/trunk/lib/Support/Unix/PathV2.inc Thu Feb  9 13:24:12 2012
@@ -87,7 +87,7 @@
     result.clear();
     StringRef d(dir);
     result.append(d.begin(), d.end());
-    return success;
+    return error_code::success();
   }
 }
 
@@ -110,7 +110,7 @@
   }
 
   result.set_size(strlen(result.data()));
-  return success;
+  return error_code::success();
 }
 
 error_code copy_file(const Twine &from, const Twine &to, copy_option copt) {
@@ -169,7 +169,7 @@
   if (sz_read < 0)
     return error_code(errno, system_category());
 
-  return success;
+  return error_code::success();
 }
 
 error_code create_directory(const Twine &path, bool &existed) {
@@ -183,7 +183,7 @@
   } else
     existed = false;
 
-  return success;
+  return error_code::success();
 }
 
 error_code create_hard_link(const Twine &to, const Twine &from) {
@@ -196,7 +196,7 @@
   if (::link(t.begin(), f.begin()) == -1)
     return error_code(errno, system_category());
 
-  return success;
+  return error_code::success();
 }
 
 error_code create_symlink(const Twine &to, const Twine &from) {
@@ -209,7 +209,7 @@
   if (::symlink(t.begin(), f.begin()) == -1)
     return error_code(errno, system_category());
 
-  return success;
+  return error_code::success();
 }
 
 error_code remove(const Twine &path, bool &existed) {
@@ -223,7 +223,7 @@
   } else
     existed = true;
 
-  return success;
+  return error_code::success();
 }
 
 error_code rename(const Twine &from, const Twine &to) {
@@ -245,7 +245,7 @@
       return error_code(errno, system_category());
   }
 
-  return success;
+  return error_code::success();
 }
 
 error_code resize_file(const Twine &path, uint64_t size) {
@@ -255,7 +255,7 @@
   if (::truncate(p.begin(), size) == -1)
     return error_code(errno, system_category());
 
-  return success;
+  return error_code::success();
 }
 
 error_code exists(const Twine &path, bool &result) {
@@ -270,7 +270,7 @@
   } else
     result = true;
 
-  return success;
+  return error_code::success();
 }
 
 bool equivalent(file_status A, file_status B) {
@@ -284,7 +284,7 @@
   if (error_code ec = status(A, fsA)) return ec;
   if (error_code ec = status(B, fsB)) return ec;
   result = equivalent(fsA, fsB);
-  return success;
+  return error_code::success();
 }
 
 error_code file_size(const Twine &path, uint64_t &result) {
@@ -298,7 +298,7 @@
     return make_error_code(errc::operation_not_permitted);
 
   result = status.st_size;
-  return success;
+  return error_code::success();
 }
 
 error_code status(const Twine &path, file_status &result) {
@@ -333,7 +333,7 @@
   result.st_dev = status.st_dev;
   result.st_ino = status.st_ino;
 
-  return success;
+  return error_code::success();
 }
 
 error_code unique_file(const Twine &model, int &result_fd,
@@ -428,7 +428,7 @@
   result_path.append(d.begin(), d.end());
 
   result_fd = RandomFD;
-  return success;
+  return error_code::success();
 }
 
 error_code detail::directory_iterator_construct(detail::DirIterState &it,
@@ -450,7 +450,7 @@
     ::closedir(reinterpret_cast<DIR *>(it.IterationHandle));
   it.IterationHandle = 0;
   it.CurrentEntry = directory_entry();
-  return success;
+  return error_code::success();
 }
 
 error_code detail::directory_iterator_increment(detail::DirIterState &it) {
@@ -467,7 +467,7 @@
   } else
     return directory_iterator_destruct(it);
 
-  return success;
+  return error_code::success();
 }
 
 error_code get_magic(const Twine &path, uint32_t len,
@@ -498,7 +498,7 @@
   }
   std::fclose(file);
   result.set_size(len);
-  return success;
+  return error_code::success();
 }
 
 } // end namespace fs

Modified: llvm/trunk/lib/Support/Windows/PathV2.inc
URL: http://llvm.org/viewvc/llvm-project/llvm/trunk/lib/Support/Windows/PathV2.inc?rev=150197&r1=150196&r2=150197&view=diff
==============================================================================
--- llvm/trunk/lib/Support/Windows/PathV2.inc (original)
+++ llvm/trunk/lib/Support/Windows/PathV2.inc Thu Feb  9 13:24:12 2012
@@ -62,7 +62,7 @@
     utf16.push_back(0);
     utf16.pop_back();
 
-    return success;
+    return error_code::success();
   }
 
   error_code UTF16ToUTF8(const wchar_t *utf16, size_t utf16_len,
@@ -92,7 +92,7 @@
     utf8.push_back(0);
     utf8.pop_back();
 
-    return success;
+    return error_code::success();
   }
 
   error_code TempDir(SmallVectorImpl<wchar_t> &result) {
@@ -108,7 +108,7 @@
     }
 
     result.set_size(len);
-    return success;
+    return error_code::success();
   }
 
   bool is_separator(const wchar_t value) {
@@ -167,7 +167,7 @@
   if (len == 0)
     return windows_error(::GetLastError());
 
-  return success;
+  return error_code::success();
 }
 
 error_code copy_file(const Twine &from, const Twine &to, copy_option copt) {
@@ -190,7 +190,7 @@
   if (res == 0)
     return windows_error(::GetLastError());
 
-  return success;
+  return error_code::success();
 }
 
 error_code create_directory(const Twine &path, bool &existed) {
@@ -210,7 +210,7 @@
   } else
     existed = false;
 
-  return success;
+  return error_code::success();
 }
 
 error_code create_hard_link(const Twine &to, const Twine &from) {
@@ -229,7 +229,7 @@
   if (!::CreateHardLinkW(wide_from.begin(), wide_to.begin(), NULL))
     return windows_error(::GetLastError());
 
-  return success;
+  return error_code::success();
 }
 
 error_code create_symlink(const Twine &to, const Twine &from) {
@@ -252,7 +252,7 @@
   if (!create_symbolic_link_api(wide_from.begin(), wide_to.begin(), 0))
     return windows_error(::GetLastError());
 
-  return success;
+  return error_code::success();
 }
 
 error_code remove(const Twine &path, bool &existed) {
@@ -285,7 +285,7 @@
       existed = true;
   }
 
-  return success;
+  return error_code::success();
 }
 
 error_code rename(const Twine &from, const Twine &to) {
@@ -305,7 +305,7 @@
                      MOVEFILE_COPY_ALLOWED | MOVEFILE_REPLACE_EXISTING))
     return windows_error(::GetLastError());
 
-  return success;
+  return error_code::success();
 }
 
 error_code resize_file(const Twine &path, uint64_t size) {
@@ -347,7 +347,7 @@
     result = false;
   } else
     result = true;
-  return success;
+  return error_code::success();
 }
 
 bool equivalent(file_status A, file_status B) {
@@ -366,7 +366,7 @@
   if (error_code ec = status(A, fsA)) return ec;
   if (error_code ec = status(B, fsB)) return ec;
   result = equivalent(fsA, fsB);
-  return success;
+  return error_code::success();
 }
 
 error_code file_size(const Twine &path, uint64_t &result) {
@@ -387,7 +387,7 @@
     (uint64_t(FileData.nFileSizeHigh) << (sizeof(FileData.nFileSizeLow) * 8))
     + FileData.nFileSizeLow;
 
-  return success;
+  return error_code::success();
 }
 
 static bool isReservedName(StringRef path) {
@@ -420,7 +420,7 @@
   StringRef path8 = path.toStringRef(path_storage);
   if (isReservedName(path8)) {
     result = file_status(file_type::character_file);
-    return success;
+    return error_code::success();
   }
 
   if (error_code ec = UTF8ToUTF16(path8, path_utf16))
@@ -470,7 +470,7 @@
     result.VolumeSerialNumber = Info.dwVolumeSerialNumber;
   }
 
-  return success;
+  return error_code::success();
 
 handle_status_error:
   error_code ec = windows_error(::GetLastError());
@@ -484,7 +484,7 @@
     return ec;
   }
 
-  return success;
+  return error_code::success();
 }
 
 error_code unique_file(const Twine &model, int &result_fd,
@@ -611,7 +611,7 @@
   }
 
   result_fd = fd;
-  return success;
+  return error_code::success();
 }
 
 error_code get_magic(const Twine &path, uint32_t len,
@@ -653,7 +653,7 @@
   }
 
   result.set_size(len);
-  return success;
+  return error_code::success();
 }
 
 error_code detail::directory_iterator_construct(detail::DirIterState &it,
@@ -705,7 +705,7 @@
   path::append(directory_entry_path, directory_entry_name_utf8.str());
   it.CurrentEntry = directory_entry(directory_entry_path.str());
 
-  return success;
+  return error_code::success();
 }
 
 error_code detail::directory_iterator_destruct(detail::DirIterState &it) {
@@ -714,7 +714,7 @@
     ScopedFindHandle close(HANDLE(it.IterationHandle));
   it.IterationHandle = 0;
   it.CurrentEntry = directory_entry();
-  return success;
+  return error_code::success();
 }
 
 error_code detail::directory_iterator_increment(detail::DirIterState &it) {
@@ -740,7 +740,7 @@
     return ec;
 
   it.CurrentEntry.replace_filename(Twine(directory_entry_path_utf8));
-  return success;
+  return error_code::success();
 }
 
 } // end namespace fs





More information about the llvm-commits mailing list