r368108 - [clang][DirectoryWatcher][NFC] Swapping asserts for llvm fatal_error in create
Puyan Lotfi via cfe-commits
cfe-commits at lists.llvm.org
Tue Aug 6 16:25:34 PDT 2019
Author: zer0
Date: Tue Aug 6 16:25:34 2019
New Revision: 368108
URL: http://llvm.org/viewvc/llvm-project?rev=368108&view=rev
Log:
[clang][DirectoryWatcher][NFC] Swapping asserts for llvm fatal_error in create
I also have replaced all the instances of
"auto DW = DirectoryWatcher::create" with
llvm::Expected<std::unique_ptr<DirectoryWatcher>> DW = DirectoryWatcher::create
to make it more clear that DirectoryWatcher::create is returning an Expected.
I've also allowed for logAllUnhandledErrors to consume errors in the case were
DirectoryWatcher::create produces them.
Differential Revision: https://reviews.llvm.org/D65829
Modified:
cfe/trunk/include/clang/DirectoryWatcher/DirectoryWatcher.h
cfe/trunk/lib/DirectoryWatcher/linux/DirectoryWatcher-linux.cpp
cfe/trunk/lib/DirectoryWatcher/mac/DirectoryWatcher-mac.cpp
cfe/trunk/unittests/DirectoryWatcher/DirectoryWatcherTest.cpp
Modified: cfe/trunk/include/clang/DirectoryWatcher/DirectoryWatcher.h
URL: http://llvm.org/viewvc/llvm-project/cfe/trunk/include/clang/DirectoryWatcher/DirectoryWatcher.h?rev=368108&r1=368107&r2=368108&view=diff
==============================================================================
--- cfe/trunk/include/clang/DirectoryWatcher/DirectoryWatcher.h (original)
+++ cfe/trunk/include/clang/DirectoryWatcher/DirectoryWatcher.h Tue Aug 6 16:25:34 2019
@@ -99,10 +99,10 @@ public:
: Kind(Kind), Filename(Filename) {}
};
- /// Asserts if \param Path doesn't exist or isn't a directory.
+ /// llvm fatal_error 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.
+ /// to succeed.
static llvm::Expected<std::unique_ptr<DirectoryWatcher>>
create(llvm::StringRef Path,
std::function<void(llvm::ArrayRef<DirectoryWatcher::Event> Events,
Modified: cfe/trunk/lib/DirectoryWatcher/linux/DirectoryWatcher-linux.cpp
URL: http://llvm.org/viewvc/llvm-project/cfe/trunk/lib/DirectoryWatcher/linux/DirectoryWatcher-linux.cpp?rev=368108&r1=368107&r2=368108&view=diff
==============================================================================
--- cfe/trunk/lib/DirectoryWatcher/linux/DirectoryWatcher-linux.cpp (original)
+++ cfe/trunk/lib/DirectoryWatcher/linux/DirectoryWatcher-linux.cpp Tue Aug 6 16:25:34 2019
@@ -325,7 +325,9 @@ llvm::Expected<std::unique_ptr<Directory
StringRef Path,
std::function<void(llvm::ArrayRef<DirectoryWatcher::Event>, bool)> Receiver,
bool WaitForInitialSync) {
- assert(!Path.empty() && "Path.empty()");
+ if (Path.empty())
+ llvm::report_fatal_error(
+ "DirectoryWatcher::create can not accept an empty Path.");
const int InotifyFD = inotify_init1(IN_CLOEXEC);
if (InotifyFD == -1)
Modified: cfe/trunk/lib/DirectoryWatcher/mac/DirectoryWatcher-mac.cpp
URL: http://llvm.org/viewvc/llvm-project/cfe/trunk/lib/DirectoryWatcher/mac/DirectoryWatcher-mac.cpp?rev=368108&r1=368107&r2=368108&view=diff
==============================================================================
--- cfe/trunk/lib/DirectoryWatcher/mac/DirectoryWatcher-mac.cpp (original)
+++ cfe/trunk/lib/DirectoryWatcher/mac/DirectoryWatcher-mac.cpp Tue Aug 6 16:25:34 2019
@@ -150,7 +150,8 @@ FSEventStreamRef createFSEventStream(
StringRef Path,
std::function<void(llvm::ArrayRef<DirectoryWatcher::Event>, bool)> Receiver,
dispatch_queue_t Queue) {
- assert(!Path.empty() && "Path.empty()");
+ if (Path.empty())
+ return nullptr;
CFMutableArrayRef PathsToWatch = [&]() {
CFMutableArrayRef PathsToWatch =
@@ -208,7 +209,10 @@ llvm::Expected<std::unique_ptr<Directory
dispatch_queue_t Queue =
dispatch_queue_create("DirectoryWatcher", DISPATCH_QUEUE_SERIAL);
- assert(!Path.empty() && "Path.empty()");
+ if (Path.empty())
+ llvm::report_fatal_error(
+ "DirectoryWatcher::create can not accept an empty Path.");
+
auto EventStream = createFSEventStream(Path, Receiver, Queue);
assert(EventStream && "EventStream expected to be non-null");
Modified: cfe/trunk/unittests/DirectoryWatcher/DirectoryWatcherTest.cpp
URL: http://llvm.org/viewvc/llvm-project/cfe/trunk/unittests/DirectoryWatcher/DirectoryWatcherTest.cpp?rev=368108&r1=368107&r2=368108&view=diff
==============================================================================
--- cfe/trunk/unittests/DirectoryWatcher/DirectoryWatcherTest.cpp (original)
+++ cfe/trunk/unittests/DirectoryWatcher/DirectoryWatcherTest.cpp Tue Aug 6 16:25:34 2019
@@ -277,14 +277,20 @@ TEST(DirectoryWatcherTest, InitialScanSy
{EventKind::Modified, "c"}}
};
- auto DW = DirectoryWatcher::create(
- fixture.TestWatchedDir,
- [&TestConsumer](llvm::ArrayRef<DirectoryWatcher::Event> Events,
- bool IsInitial) {
- TestConsumer.consume(Events, IsInitial);
- },
- /*waitForInitialSync=*/true);
- if (!DW) return;
+ llvm::Expected<std::unique_ptr<DirectoryWatcher>> DW =
+ DirectoryWatcher::create(
+ fixture.TestWatchedDir,
+ [&TestConsumer](llvm::ArrayRef<DirectoryWatcher::Event> Events,
+ bool IsInitial) {
+ TestConsumer.consume(Events, IsInitial);
+ },
+ /*waitForInitialSync=*/true);
+ if (!DW) {
+ logAllUnhandledErrors(
+ DW.takeError(), llvm::errs(),
+ "DirectoryWatcherTest Failure on DirectoryWatcher::create(): ");
+ exit(EXIT_FAILURE);
+ }
checkEventualResultWithTimeout(TestConsumer);
}
@@ -309,14 +315,20 @@ TEST(DirectoryWatcherTest, InitialScanAs
{EventKind::Modified, "c"}}
};
- auto DW = DirectoryWatcher::create(
- fixture.TestWatchedDir,
- [&TestConsumer](llvm::ArrayRef<DirectoryWatcher::Event> Events,
- bool IsInitial) {
- TestConsumer.consume(Events, IsInitial);
- },
- /*waitForInitialSync=*/false);
- if (!DW) return;
+ llvm::Expected<std::unique_ptr<DirectoryWatcher>> DW =
+ DirectoryWatcher::create(
+ fixture.TestWatchedDir,
+ [&TestConsumer](llvm::ArrayRef<DirectoryWatcher::Event> Events,
+ bool IsInitial) {
+ TestConsumer.consume(Events, IsInitial);
+ },
+ /*waitForInitialSync=*/false);
+ if (!DW) {
+ logAllUnhandledErrors(
+ DW.takeError(), llvm::errs(),
+ "DirectoryWatcherTest Failure on DirectoryWatcher::create(): ");
+ exit(EXIT_FAILURE);
+ }
checkEventualResultWithTimeout(TestConsumer);
}
@@ -330,14 +342,20 @@ TEST(DirectoryWatcherTest, AddFiles) {
{EventKind::Modified, "b"},
{EventKind::Modified, "c"}}};
- auto DW = DirectoryWatcher::create(
- fixture.TestWatchedDir,
- [&TestConsumer](llvm::ArrayRef<DirectoryWatcher::Event> Events,
- bool IsInitial) {
- TestConsumer.consume(Events, IsInitial);
- },
- /*waitForInitialSync=*/true);
- if (!DW) return;
+ llvm::Expected<std::unique_ptr<DirectoryWatcher>> DW =
+ DirectoryWatcher::create(
+ fixture.TestWatchedDir,
+ [&TestConsumer](llvm::ArrayRef<DirectoryWatcher::Event> Events,
+ bool IsInitial) {
+ TestConsumer.consume(Events, IsInitial);
+ },
+ /*waitForInitialSync=*/true);
+ if (!DW) {
+ logAllUnhandledErrors(
+ DW.takeError(), llvm::errs(),
+ "DirectoryWatcherTest Failure on DirectoryWatcher::create(): ");
+ exit(EXIT_FAILURE);
+ }
fixture.addFile("a");
fixture.addFile("b");
@@ -356,14 +374,20 @@ TEST(DirectoryWatcherTest, ModifyFile) {
{{EventKind::Modified, "a"}},
{{EventKind::Modified, "a"}}};
- auto DW = DirectoryWatcher::create(
- fixture.TestWatchedDir,
- [&TestConsumer](llvm::ArrayRef<DirectoryWatcher::Event> Events,
- bool IsInitial) {
- TestConsumer.consume(Events, IsInitial);
- },
- /*waitForInitialSync=*/true);
- if (!DW) return;
+ llvm::Expected<std::unique_ptr<DirectoryWatcher>> DW =
+ DirectoryWatcher::create(
+ fixture.TestWatchedDir,
+ [&TestConsumer](llvm::ArrayRef<DirectoryWatcher::Event> Events,
+ bool IsInitial) {
+ TestConsumer.consume(Events, IsInitial);
+ },
+ /*waitForInitialSync=*/true);
+ if (!DW) {
+ logAllUnhandledErrors(
+ DW.takeError(), llvm::errs(),
+ "DirectoryWatcherTest Failure on DirectoryWatcher::create(): ");
+ exit(EXIT_FAILURE);
+ }
// modify the file
{
@@ -387,14 +411,20 @@ TEST(DirectoryWatcherTest, DeleteFile) {
{{EventKind::Removed, "a"}},
{{EventKind::Modified, "a"}, {EventKind::Removed, "a"}}};
- auto DW = DirectoryWatcher::create(
- fixture.TestWatchedDir,
- [&TestConsumer](llvm::ArrayRef<DirectoryWatcher::Event> Events,
- bool IsInitial) {
- TestConsumer.consume(Events, IsInitial);
- },
- /*waitForInitialSync=*/true);
- if (!DW) return;
+ llvm::Expected<std::unique_ptr<DirectoryWatcher>> DW =
+ DirectoryWatcher::create(
+ fixture.TestWatchedDir,
+ [&TestConsumer](llvm::ArrayRef<DirectoryWatcher::Event> Events,
+ bool IsInitial) {
+ TestConsumer.consume(Events, IsInitial);
+ },
+ /*waitForInitialSync=*/true);
+ if (!DW) {
+ logAllUnhandledErrors(
+ DW.takeError(), llvm::errs(),
+ "DirectoryWatcherTest Failure on DirectoryWatcher::create(): ");
+ exit(EXIT_FAILURE);
+ }
fixture.deleteFile("a");
@@ -409,14 +439,20 @@ TEST(DirectoryWatcherTest, DeleteWatched
{{EventKind::WatchedDirRemoved, ""},
{EventKind::WatcherGotInvalidated, ""}}};
- auto DW = DirectoryWatcher::create(
- fixture.TestWatchedDir,
- [&TestConsumer](llvm::ArrayRef<DirectoryWatcher::Event> Events,
- bool IsInitial) {
- TestConsumer.consume(Events, IsInitial);
- },
- /*waitForInitialSync=*/true);
- if (!DW) return;
+ llvm::Expected<std::unique_ptr<DirectoryWatcher>> DW =
+ DirectoryWatcher::create(
+ fixture.TestWatchedDir,
+ [&TestConsumer](llvm::ArrayRef<DirectoryWatcher::Event> Events,
+ bool IsInitial) {
+ TestConsumer.consume(Events, IsInitial);
+ },
+ /*waitForInitialSync=*/true);
+ if (!DW) {
+ logAllUnhandledErrors(
+ DW.takeError(), llvm::errs(),
+ "DirectoryWatcherTest Failure on DirectoryWatcher::create(): ");
+ exit(EXIT_FAILURE);
+ }
remove_directories(fixture.TestWatchedDir);
@@ -430,15 +466,21 @@ TEST(DirectoryWatcherTest, InvalidatedWa
{}, {{EventKind::WatcherGotInvalidated, ""}}};
{
- auto DW = DirectoryWatcher::create(
- fixture.TestWatchedDir,
- [&TestConsumer](llvm::ArrayRef<DirectoryWatcher::Event> Events,
- bool IsInitial) {
- TestConsumer.consume(Events, IsInitial);
- },
- /*waitForInitialSync=*/true);
- if (!DW) return;
+ llvm::Expected<std::unique_ptr<DirectoryWatcher>> DW =
+ DirectoryWatcher::create(
+ fixture.TestWatchedDir,
+ [&TestConsumer](llvm::ArrayRef<DirectoryWatcher::Event> Events,
+ bool IsInitial) {
+ TestConsumer.consume(Events, IsInitial);
+ },
+ /*waitForInitialSync=*/true);
+ if (!DW) {
+ logAllUnhandledErrors(
+ DW.takeError(), llvm::errs(),
+ "DirectoryWatcherTest Failure on DirectoryWatcher::create(): ");
+ exit(EXIT_FAILURE);
+ }
} // DW is destructed here.
checkEventualResultWithTimeout(TestConsumer);
-}
\ No newline at end of file
+}
More information about the cfe-commits
mailing list