This revision was not accepted when it landed; it landed in state "Needs 
Review".
This revision was automatically updated to reflect the committed changes.
Closed by commit rL367979: [clang][DirectoryWatcher] Adding llvm::Expected 
error handling to create. (authored by zer0, committed by ).

Changed prior to commit:
  https://reviews.llvm.org/D65704?vs=213520&id=213521#toc

Repository:
  rL LLVM

CHANGES SINCE LAST ACTION
  https://reviews.llvm.org/D65704/new/

https://reviews.llvm.org/D65704

Files:
  cfe/trunk/include/clang/DirectoryWatcher/DirectoryWatcher.h
  cfe/trunk/lib/DirectoryWatcher/default/DirectoryWatcher-not-implemented.cpp
  cfe/trunk/lib/DirectoryWatcher/linux/DirectoryWatcher-linux.cpp
  cfe/trunk/lib/DirectoryWatcher/mac/DirectoryWatcher-mac.cpp
  cfe/trunk/unittests/DirectoryWatcher/DirectoryWatcherTest.cpp

Index: cfe/trunk/include/clang/DirectoryWatcher/DirectoryWatcher.h
===================================================================
--- cfe/trunk/include/clang/DirectoryWatcher/DirectoryWatcher.h
+++ cfe/trunk/include/clang/DirectoryWatcher/DirectoryWatcher.h
@@ -11,6 +11,7 @@
 
 #include "llvm/ADT/ArrayRef.h"
 #include "llvm/ADT/StringRef.h"
+#include "llvm/Support/Error.h"
 #include <functional>
 #include <memory>
 #include <string>
@@ -98,10 +99,11 @@
         : Kind(Kind), Filename(Filename) {}
   };
 
-  /// Returns nullptr if \param Path doesn't exist or isn't a directory.
-  /// Returns nullptr if OS kernel API told us we can't start watching. In such
-  /// case it's unclear whether just retrying has any chance to succeeed.
-  static std::unique_ptr<DirectoryWatcher>
+  /// Asserts if \param Path doesn't exist or isn't a directory.
+  /// Returns llvm::Expected Error if OS kernel API told us we can't start
+  /// watching. In such case it's unclear whether just retrying has any chance
+  /// to succeeed.
+  static llvm::Expected<std::unique_ptr<DirectoryWatcher>>
   create(llvm::StringRef Path,
          std::function<void(llvm::ArrayRef<DirectoryWatcher::Event> Events,
                             bool IsInitial)>
Index: cfe/trunk/lib/DirectoryWatcher/default/DirectoryWatcher-not-implemented.cpp
===================================================================
--- cfe/trunk/lib/DirectoryWatcher/default/DirectoryWatcher-not-implemented.cpp
+++ cfe/trunk/lib/DirectoryWatcher/default/DirectoryWatcher-not-implemented.cpp
@@ -11,9 +11,11 @@
 using namespace llvm;
 using namespace clang;
 
-std::unique_ptr<DirectoryWatcher> clang::DirectoryWatcher::create(
+llvm::Expected<std::unique_ptr<DirectoryWatcher>> clang::DirectoryWatcher::create(
     StringRef Path,
     std::function<void(llvm::ArrayRef<DirectoryWatcher::Event>, bool)> Receiver,
     bool WaitForInitialSync) {
-  return nullptr;
+  return llvm::make_error<llvm::StringError>(
+      "DirectoryWatcher is not implemented for this platform!",
+      llvm::inconvertibleErrorCode());
 }
\ No newline at end of file
Index: cfe/trunk/lib/DirectoryWatcher/mac/DirectoryWatcher-mac.cpp
===================================================================
--- cfe/trunk/lib/DirectoryWatcher/mac/DirectoryWatcher-mac.cpp
+++ cfe/trunk/lib/DirectoryWatcher/mac/DirectoryWatcher-mac.cpp
@@ -11,16 +11,13 @@
 
 #include "llvm/ADT/STLExtras.h"
 #include "llvm/ADT/StringRef.h"
+#include "llvm/Support/Error.h"
 #include "llvm/Support/Path.h"
 #include <CoreServices/CoreServices.h>
 
 using namespace llvm;
 using namespace clang;
 
-static FSEventStreamRef createFSEventStream(
-    StringRef Path,
-    std::function<void(llvm::ArrayRef<DirectoryWatcher::Event>, bool)>,
-    dispatch_queue_t);
 static void stopFSEventStream(FSEventStreamRef);
 
 namespace {
@@ -153,8 +150,7 @@
     StringRef Path,
     std::function<void(llvm::ArrayRef<DirectoryWatcher::Event>, bool)> Receiver,
     dispatch_queue_t Queue) {
-  if (Path.empty())
-    return nullptr;
+  assert(!Path.empty() && "Path.empty()");
 
   CFMutableArrayRef PathsToWatch = [&]() {
     CFMutableArrayRef PathsToWatch =
@@ -205,20 +201,16 @@
   FSEventStreamRelease(EventStream);
 }
 
-std::unique_ptr<DirectoryWatcher> clang::DirectoryWatcher::create(
+llvm::Expected<std::unique_ptr<DirectoryWatcher>> clang::DirectoryWatcher::create(
     StringRef Path,
     std::function<void(llvm::ArrayRef<DirectoryWatcher::Event>, bool)> Receiver,
     bool WaitForInitialSync) {
   dispatch_queue_t Queue =
       dispatch_queue_create("DirectoryWatcher", DISPATCH_QUEUE_SERIAL);
 
-  if (Path.empty())
-    return nullptr;
-
+  assert(!Path.empty() && "Path.empty()");
   auto EventStream = createFSEventStream(Path, Receiver, Queue);
-  if (!EventStream) {
-    return nullptr;
-  }
+  assert(EventStream && "EventStream expected to be non-null.")
 
   std::unique_ptr<DirectoryWatcher> Result =
       llvm::make_unique<DirectoryWatcherMac>(EventStream, Receiver, Path);
Index: cfe/trunk/lib/DirectoryWatcher/linux/DirectoryWatcher-linux.cpp
===================================================================
--- cfe/trunk/lib/DirectoryWatcher/linux/DirectoryWatcher-linux.cpp
+++ cfe/trunk/lib/DirectoryWatcher/linux/DirectoryWatcher-linux.cpp
@@ -13,6 +13,7 @@
 #include "llvm/ADT/ScopeExit.h"
 #include "llvm/Support/AlignOf.h"
 #include "llvm/Support/Errno.h"
+#include "llvm/Support/Error.h"
 #include "llvm/Support/Mutex.h"
 #include "llvm/Support/Path.h"
 #include <atomic>
@@ -320,16 +321,17 @@
 
 } // namespace
 
-std::unique_ptr<DirectoryWatcher> clang::DirectoryWatcher::create(
+llvm::Expected<std::unique_ptr<DirectoryWatcher>> clang::DirectoryWatcher::create(
     StringRef Path,
     std::function<void(llvm::ArrayRef<DirectoryWatcher::Event>, bool)> Receiver,
     bool WaitForInitialSync) {
-  if (Path.empty())
-    return nullptr;
+  assert(!Path.empty() && "Path.empty()");
 
   const int InotifyFD = inotify_init1(IN_CLOEXEC);
   if (InotifyFD == -1)
-    return nullptr;
+    return llvm::make_error<llvm::StringError>(
+        std::string("inotify_init1() error: ") + strerror(errno),
+        llvm::inconvertibleErrorCode());
 
   const int InotifyWD = inotify_add_watch(
       InotifyFD, Path.str().c_str(),
@@ -340,12 +342,16 @@
 #endif
       );
   if (InotifyWD == -1)
-    return nullptr;
+    return llvm::make_error<llvm::StringError>(
+        std::string("inotify_add_watch() error: ") + strerror(errno),
+        llvm::inconvertibleErrorCode());
 
   auto InotifyPollingStopper = SemaphorePipe::create();
 
   if (!InotifyPollingStopper)
-    return nullptr;
+    return llvm::make_error<llvm::StringError>(
+        std::string("SemaphorePipe::create() error: ") + strerror(errno),
+        llvm::inconvertibleErrorCode());
 
   return llvm::make_unique<DirectoryWatcherLinux>(
       Path, Receiver, WaitForInitialSync, InotifyFD, InotifyWD,
Index: cfe/trunk/unittests/DirectoryWatcher/DirectoryWatcherTest.cpp
===================================================================
--- cfe/trunk/unittests/DirectoryWatcher/DirectoryWatcherTest.cpp
+++ cfe/trunk/unittests/DirectoryWatcher/DirectoryWatcherTest.cpp
@@ -284,6 +284,7 @@
         TestConsumer.consume(Events, IsInitial);
       },
       /*waitForInitialSync=*/true);
+  if (!DW) return;
 
   checkEventualResultWithTimeout(TestConsumer);
 }
@@ -315,6 +316,7 @@
         TestConsumer.consume(Events, IsInitial);
       },
       /*waitForInitialSync=*/false);
+  if (!DW) return;
 
   checkEventualResultWithTimeout(TestConsumer);
 }
@@ -335,6 +337,7 @@
         TestConsumer.consume(Events, IsInitial);
       },
       /*waitForInitialSync=*/true);
+  if (!DW) return;
 
   fixture.addFile("a");
   fixture.addFile("b");
@@ -360,6 +363,7 @@
         TestConsumer.consume(Events, IsInitial);
       },
       /*waitForInitialSync=*/true);
+  if (!DW) return;
 
   // modify the file
   {
@@ -390,6 +394,7 @@
         TestConsumer.consume(Events, IsInitial);
       },
       /*waitForInitialSync=*/true);
+  if (!DW) return;
 
   fixture.deleteFile("a");
 
@@ -411,6 +416,7 @@
         TestConsumer.consume(Events, IsInitial);
       },
       /*waitForInitialSync=*/true);
+  if (!DW) return;
 
   remove_directories(fixture.TestWatchedDir);
 
@@ -431,7 +437,8 @@
           TestConsumer.consume(Events, IsInitial);
         },
         /*waitForInitialSync=*/true);
+    if (!DW) return;
   } // DW is destructed here.
 
   checkEventualResultWithTimeout(TestConsumer);
 }
\ No newline at end of file
_______________________________________________
cfe-commits mailing list
cfe-commits@lists.llvm.org
https://lists.llvm.org/cgi-bin/mailman/listinfo/cfe-commits

Reply via email to