From 67a508719f768479a401c20150b97f9ddab9b81d Mon Sep 17 00:00:00 2001 From: Jan Korous Date: Tue, 19 Feb 2019 17:05:36 -0800 Subject: [PATCH] [DirectoryWatcher][NFC] clang-format --- .../clang/DirectoryWatcher/DirectoryWatcher.h | 14 ++-- .../DirectoryWatcher-linux.inc.h | 29 ++++--- .../DirectoryWatcher-mac.inc.h | 84 +++++++++---------- lib/DirectoryWatcher/DirectoryWatcher.cpp | 44 +++++----- .../DirectoryWatcher/DirectoryWatcherTest.cpp | 75 +++++++++-------- 5 files changed, 127 insertions(+), 119 deletions(-) diff --git a/include/clang/DirectoryWatcher/DirectoryWatcher.h b/include/clang/DirectoryWatcher/DirectoryWatcher.h index 2739955a113..35d76f51944 100644 --- a/include/clang/DirectoryWatcher/DirectoryWatcher.h +++ b/include/clang/DirectoryWatcher/DirectoryWatcher.h @@ -44,13 +44,15 @@ class DirectoryWatcher { llvm::sys::TimePoint<> ModTime; }; - typedef std::function Events, bool isInitial)> EventReceiver; + typedef std::function Events, bool isInitial)> + EventReceiver; ~DirectoryWatcher(); - static std::unique_ptr - create(StringRef Path, EventReceiver Receiver, bool waitInitialSync, - std::string &Error); + static std::unique_ptr create(StringRef Path, + EventReceiver Receiver, + bool waitInitialSync, + std::string &Error); private: struct Implementation; @@ -58,8 +60,8 @@ class DirectoryWatcher { DirectoryWatcher(); - DirectoryWatcher(const DirectoryWatcher&) = delete; - DirectoryWatcher &operator =(const DirectoryWatcher&) = delete; + DirectoryWatcher(const DirectoryWatcher &) = delete; + DirectoryWatcher &operator=(const DirectoryWatcher &) = delete; }; } // namespace clang diff --git a/lib/DirectoryWatcher/DirectoryWatcher-linux.inc.h b/lib/DirectoryWatcher/DirectoryWatcher-linux.inc.h index ddb492ab2ff..c04fc7261bf 100644 --- a/lib/DirectoryWatcher/DirectoryWatcher-linux.inc.h +++ b/lib/DirectoryWatcher/DirectoryWatcher-linux.inc.h @@ -8,11 +8,11 @@ //===----------------------------------------------------------------------===// #include "llvm/Support/Errno.h" -#include "llvm/Support/Path.h" #include "llvm/Support/Mutex.h" +#include "llvm/Support/Path.h" +#include #include #include -#include namespace { @@ -30,7 +30,8 @@ class EventQueue { DirectoryWatcher::Event toDirEvent(const INotifyEvent &evt) { llvm::sys::TimePoint<> modTime{}; - if (evt.Status.hasValue()) modTime = evt.Status->getLastModificationTime(); + if (evt.Status.hasValue()) + modTime = evt.Status->getLastModificationTime(); return DirectoryWatcher::Event{evt.K, evt.Filename, modTime}; } @@ -76,14 +77,12 @@ class EventQueue { PendingEvents.clear(); } }; -} // namespace +} // namespace struct DirectoryWatcher::Implementation { - bool initialize(StringRef Path, EventReceiver Receiver, - bool waitInitialSync, std::string &Error); - ~Implementation() { - stopListening(); - }; + bool initialize(StringRef Path, EventReceiver Receiver, bool waitInitialSync, + std::string &Error); + ~Implementation() { stopListening(); }; private: int inotifyFD = -1; @@ -93,8 +92,8 @@ struct DirectoryWatcher::Implementation { static void runWatcher(std::string pathToWatch, int inotifyFD, std::shared_ptr evtQueue) { - #define EVT_BUF_LEN (30 * (sizeof(struct inotify_event) + NAME_MAX + 1)) - char buf[EVT_BUF_LEN] __attribute__ ((aligned(8))); +#define EVT_BUF_LEN (30 * (sizeof(struct inotify_event) + NAME_MAX + 1)) + char buf[EVT_BUF_LEN] __attribute__((aligned(8))); while (1) { ssize_t numRead = read(inotifyFD, buf, EVT_BUF_LEN); @@ -155,16 +154,16 @@ bool DirectoryWatcher::Implementation::initialize(StringRef Path, return true; }; - auto evtQueue = std::make_shared(std::move(Receiver)); + auto evtQueue = std::make_shared(std::move(Receiver)); inotifyFD = inotify_init(); if (inotifyFD == -1) return error("inotify_init failed"); std::string pathToWatch = Path; - int wd = inotify_add_watch( - inotifyFD, pathToWatch.c_str(), - IN_MOVED_TO | IN_DELETE | IN_MODIFY | IN_DELETE_SELF | IN_ONLYDIR); + int wd = inotify_add_watch(inotifyFD, pathToWatch.c_str(), + IN_MOVED_TO | IN_DELETE | IN_MODIFY | + IN_DELETE_SELF | IN_ONLYDIR); if (wd == -1) return error("inotify_add_watch failed"); diff --git a/lib/DirectoryWatcher/DirectoryWatcher-mac.inc.h b/lib/DirectoryWatcher/DirectoryWatcher-mac.inc.h index 826e9bea4fd..dabc25a6193 100644 --- a/lib/DirectoryWatcher/DirectoryWatcher-mac.inc.h +++ b/lib/DirectoryWatcher/DirectoryWatcher-mac.inc.h @@ -10,11 +10,10 @@ #include struct DirectoryWatcher::Implementation { - bool initialize(StringRef Path, EventReceiver Receiver, - bool waitInitialSync, std::string &Error); - ~Implementation() { - stopFSEventStream(); - }; + bool initialize(StringRef Path, EventReceiver Receiver, bool waitInitialSync, + std::string &Error); + ~Implementation() { stopFSEventStream(); }; + private: FSEventStreamRef EventStream = nullptr; @@ -30,35 +29,35 @@ struct EventStreamContextData { DirectoryWatcher::EventReceiver Receiver; std::shared_ptr InitialScan; - EventStreamContextData(std::string watchedPath, DirectoryWatcher::EventReceiver receiver, + EventStreamContextData(std::string watchedPath, + DirectoryWatcher::EventReceiver receiver, std::shared_ptr initialScanPtr) - : WatchedPath(std::move(watchedPath)), - Receiver(std::move(receiver)), - InitialScan(std::move(initialScanPtr)) { - } + : WatchedPath(std::move(watchedPath)), Receiver(std::move(receiver)), + InitialScan(std::move(initialScanPtr)) {} static void dispose(const void *ctx) { - delete static_cast(ctx); + delete static_cast(ctx); } }; -} +} // namespace -static void eventStreamCallback( - ConstFSEventStreamRef stream, - void *clientCallBackInfo, - size_t numEvents, - void *eventPaths, - const FSEventStreamEventFlags eventFlags[], - const FSEventStreamEventId eventIds[]) { - auto *ctx = static_cast(clientCallBackInfo); +static void eventStreamCallback(ConstFSEventStreamRef stream, + void *clientCallBackInfo, size_t numEvents, + void *eventPaths, + const FSEventStreamEventFlags eventFlags[], + const FSEventStreamEventId eventIds[]) { + auto *ctx = static_cast(clientCallBackInfo); std::vector Events; for (size_t i = 0; i < numEvents; ++i) { StringRef path = ((const char **)eventPaths)[i]; const FSEventStreamEventFlags flags = eventFlags[i]; if (!(flags & kFSEventStreamEventFlagItemIsFile)) { - if ((flags & kFSEventStreamEventFlagItemRemoved) && path == ctx->WatchedPath) { - DirectoryWatcher::Event Evt{DirectoryWatcher::EventKind::DirectoryDeleted, path, llvm::sys::TimePoint<>{} }; + if ((flags & kFSEventStreamEventFlagItemRemoved) && + path == ctx->WatchedPath) { + DirectoryWatcher::Event Evt{ + DirectoryWatcher::EventKind::DirectoryDeleted, path, + llvm::sys::TimePoint<>{}}; Events.push_back(Evt); break; } @@ -113,15 +112,17 @@ static void eventStreamCallback( } } -bool DirectoryWatcher::Implementation::setupFSEventStream(StringRef path, - EventReceiver receiver, - dispatch_queue_t queue, - std::shared_ptr initialScanPtr) { +bool DirectoryWatcher::Implementation::setupFSEventStream( + StringRef path, EventReceiver receiver, dispatch_queue_t queue, + std::shared_ptr initialScanPtr) { if (path.empty()) return true; - CFMutableArrayRef pathsToWatch = CFArrayCreateMutable(nullptr, 0, &kCFTypeArrayCallBacks); - CFStringRef cfPathStr = CFStringCreateWithBytes(nullptr, (const UInt8 *)path.data(), path.size(), kCFStringEncodingUTF8, false); + CFMutableArrayRef pathsToWatch = + CFArrayCreateMutable(nullptr, 0, &kCFTypeArrayCallBacks); + CFStringRef cfPathStr = + CFStringCreateWithBytes(nullptr, (const UInt8 *)path.data(), path.size(), + kCFStringEncodingUTF8, false); CFArrayAppendValue(pathsToWatch, cfPathStr); CFRelease(cfPathStr); CFAbsoluteTime latency = 0.0; // Latency in seconds. @@ -138,9 +139,8 @@ bool DirectoryWatcher::Implementation::setupFSEventStream(StringRef path, realPath = path; } - EventStreamContextData *ctxData = - new EventStreamContextData(std::move(realPath), std::move(receiver), - std::move(initialScanPtr)); + EventStreamContextData *ctxData = new EventStreamContextData( + std::move(realPath), std::move(receiver), std::move(initialScanPtr)); FSEventStreamContext context; context.version = 0; context.info = ctxData; @@ -148,14 +148,10 @@ bool DirectoryWatcher::Implementation::setupFSEventStream(StringRef path, context.release = EventStreamContextData::dispose; context.copyDescription = nullptr; - EventStream = FSEventStreamCreate(nullptr, - eventStreamCallback, - &context, - pathsToWatch, - kFSEventStreamEventIdSinceNow, - latency, - kFSEventStreamCreateFlagFileEvents | - kFSEventStreamCreateFlagNoDefer); + EventStream = FSEventStreamCreate( + nullptr, eventStreamCallback, &context, pathsToWatch, + kFSEventStreamEventIdSinceNow, latency, + kFSEventStreamCreateFlagFileEvents | kFSEventStreamCreateFlagNoDefer); CFRelease(pathsToWatch); if (!EventStream) { return true; @@ -175,10 +171,13 @@ void DirectoryWatcher::Implementation::stopFSEventStream() { } bool DirectoryWatcher::Implementation::initialize(StringRef Path, - EventReceiver Receiver, bool waitInitialSync, std::string &Error) { + EventReceiver Receiver, + bool waitInitialSync, + std::string &Error) { auto initialScan = std::make_shared(); - dispatch_queue_t queue = dispatch_queue_create("DirectoryWatcher", DISPATCH_QUEUE_SERIAL); + dispatch_queue_t queue = + dispatch_queue_create("DirectoryWatcher", DISPATCH_QUEUE_SERIAL); dispatch_semaphore_t initScanSema = dispatch_semaphore_create(0); dispatch_semaphore_t setupFSEventsSema = dispatch_semaphore_create(0); @@ -206,7 +205,8 @@ bool DirectoryWatcher::Implementation::initialize(StringRef Path, dispatch_release(queue); if (fsErr) { - raw_string_ostream(Error) << "failed to setup FSEvents stream for path: " << Path; + raw_string_ostream(Error) + << "failed to setup FSEvents stream for path: " << Path; return true; } diff --git a/lib/DirectoryWatcher/DirectoryWatcher.cpp b/lib/DirectoryWatcher/DirectoryWatcher.cpp index 0feaacefe7a..511b7ce6bc3 100644 --- a/lib/DirectoryWatcher/DirectoryWatcher.cpp +++ b/lib/DirectoryWatcher/DirectoryWatcher.cpp @@ -46,11 +46,12 @@ template <> struct DenseMapInfo { std::make_pair(val.getDevice(), val.getFile())); } - static bool isEqual(const sys::fs::UniqueID &LHS, const sys::fs::UniqueID &RHS) { + static bool isEqual(const sys::fs::UniqueID &LHS, + const sys::fs::UniqueID &RHS) { return LHS == RHS; } }; -} +} // namespace llvm namespace { /// Used for initial directory scan. @@ -65,12 +66,14 @@ struct DirectoryScan { using namespace llvm::sys; std::error_code EC; - for (auto It = fs::directory_iterator(Path, EC), End = fs::directory_iterator(); - !EC && It != End; It.increment(EC)) { + for (auto It = fs::directory_iterator(Path, EC), + End = fs::directory_iterator(); + !EC && It != End; It.increment(EC)) { auto status = getFileStatus(It->path()); if (!status.hasValue()) continue; - Files.push_back(std::make_tuple(It->path(), status->getLastModificationTime())); + Files.push_back( + std::make_tuple(It->path(), status->getLastModificationTime())); FileIDSet.insert(status->getUniqueID()); } } @@ -78,29 +81,30 @@ struct DirectoryScan { std::vector getAsFileEvents() const { std::vector Events; for (const auto &info : Files) { - DirectoryWatcher::Event Event{DirectoryWatcher::EventKind::Added, std::get<0>(info), std::get<1>(info)}; + DirectoryWatcher::Event Event{DirectoryWatcher::EventKind::Added, + std::get<0>(info), std::get<1>(info)}; Events.push_back(std::move(Event)); } return Events; } }; -} +} // namespace // Add platform-specific functionality. #if !defined(__has_include) -# define __has_include(x) 0 +#define __has_include(x) 0 #endif #if __has_include() -# include "DirectoryWatcher-mac.inc.h" +#include "DirectoryWatcher-mac.inc.h" #elif __has_include() -# include "DirectoryWatcher-linux.inc.h" +#include "DirectoryWatcher-linux.inc.h" #else struct DirectoryWatcher::Implementation { - bool initialize(StringRef Path, EventReceiver Receiver, - bool waitInitialSync, std::string &Error) { + bool initialize(StringRef Path, EventReceiver Receiver, bool waitInitialSync, + std::string &Error) { Error = "directory listening not supported for this platform"; return true; } @@ -108,16 +112,13 @@ struct DirectoryWatcher::Implementation { #endif +DirectoryWatcher::DirectoryWatcher() : Impl(*new Implementation()) {} -DirectoryWatcher::DirectoryWatcher() - : Impl(*new Implementation()) {} - -DirectoryWatcher::~DirectoryWatcher() { - delete &Impl; -} +DirectoryWatcher::~DirectoryWatcher() { delete &Impl; } -std::unique_ptr DirectoryWatcher::create(StringRef Path, - EventReceiver Receiver, bool waitInitialSync, std::string &Error) { +std::unique_ptr +DirectoryWatcher::create(StringRef Path, EventReceiver Receiver, + bool waitInitialSync, std::string &Error) { using namespace llvm::sys; if (!fs::exists(Path)) { @@ -143,7 +144,8 @@ std::unique_ptr DirectoryWatcher::create(StringRef Path, std::unique_ptr DirWatch; DirWatch.reset(new DirectoryWatcher()); auto &Impl = DirWatch->Impl; - bool hasError = Impl.initialize(Path, std::move(Receiver), waitInitialSync, Error); + bool hasError = + Impl.initialize(Path, std::move(Receiver), waitInitialSync, Error); if (hasError) return nullptr; diff --git a/unittests/DirectoryWatcher/DirectoryWatcherTest.cpp b/unittests/DirectoryWatcher/DirectoryWatcherTest.cpp index 5fec06a64d6..f6b19794ec2 100644 --- a/unittests/DirectoryWatcher/DirectoryWatcherTest.cpp +++ b/unittests/DirectoryWatcher/DirectoryWatcherTest.cpp @@ -24,6 +24,7 @@ namespace { class EventCollection { SmallVector Events; + public: EventCollection() = default; explicit EventCollection(ArrayRef events) { @@ -49,11 +50,12 @@ class EventCollection { StringRef fname = filenames[i]; DirectoryWatcher::EventKind kind = kinds[i]; file_status stat = stats[i]; - auto it = std::find_if(evts.begin(), evts.end(), [&](const DirectoryWatcher::Event &evt)->bool { - return path::filename(evt.Filename) == fname; - }); + auto it = std::find_if(evts.begin(), evts.end(), + [&](const DirectoryWatcher::Event &evt) -> bool { + return path::filename(evt.Filename) == fname; + }); if (it == evts.end()) { - hadError = err(Twine("expected filename '"+fname+"' not found")); + hadError = err(Twine("expected filename '" + fname + "' not found")); continue; } if (it->Kind != kind) { @@ -63,25 +65,28 @@ class EventCollection { } if (it->Kind != DirectoryWatcher::EventKind::Removed && it->ModTime != stat.getLastModificationTime()) - hadError = err(Twine("filename '"+fname+"' has different mod time")); + hadError = + err(Twine("filename '" + fname + "' has different mod time")); evts.erase(it); } for (const auto &evt : evts) { - hadError = err(Twine("unexpected filename '"+path::filename(evt.Filename)+"' found")); + hadError = err(Twine("unexpected filename '" + + path::filename(evt.Filename) + "' found")); } return !hadError; } - bool hasAdded(ArrayRef filenames, ArrayRef stats) const { + bool hasAdded(ArrayRef filenames, + ArrayRef stats) const { std::vector kinds{ - filenames.size(), DirectoryWatcher::EventKind::Added }; + filenames.size(), DirectoryWatcher::EventKind::Added}; return hasEvents(filenames, kinds, stats); } bool hasRemoved(ArrayRef filenames) const { std::vector kinds{ - filenames.size(), DirectoryWatcher::EventKind::Removed }; - std::vector stats{ filenames.size(), file_status{} }; + filenames.size(), DirectoryWatcher::EventKind::Removed}; + std::vector stats{filenames.size(), file_status{}}; return hasEvents(filenames, kinds, stats); } @@ -98,7 +103,8 @@ struct EventOccurrence { bool IsInitial; }; -class DirectoryWatcherTest: public std::enable_shared_from_this { +class DirectoryWatcherTest + : public std::enable_shared_from_this { std::string WatchedDir; std::string TempDir; std::unique_ptr DirWatcher; @@ -125,15 +131,14 @@ class DirectoryWatcherTest: public std::enable_shared_from_this pathBuf; pathBuf = TempDir; path::append(pathBuf, filename); - Expected ft = openNativeFileForWrite(pathBuf, CD_CreateNew, OF_None); + Expected ft = + openNativeFileForWrite(pathBuf, CD_CreateNew, OF_None); ASSERT_TRUE((bool)ft); closeFile(*ft); @@ -147,7 +152,8 @@ class DirectoryWatcherTest: public std::enable_shared_from_this filenames, std::vector &stats) { + void addFiles(ArrayRef filenames, + std::vector &stats) { for (auto fname : filenames) { file_status stat; addFile(fname, stat); @@ -177,7 +183,8 @@ class DirectoryWatcherTest: public std::enable_shared_from_this weakThis = shared_from_this(); - auto receiver = [weakThis](ArrayRef events, bool isInitial) { + auto receiver = [weakThis](ArrayRef events, + bool isInitial) { if (auto this_ = weakThis.lock()) this_->onEvents(events, isInitial); }; @@ -187,15 +194,14 @@ class DirectoryWatcherTest: public std::enable_shared_from_this getNextEvent(unsigned timeout_seconds = 5) { std::unique_lock lck(Mutex); - auto pred = [&]()->bool { return !EvtOccurs.empty(); }; - bool gotEvent = Condition.wait_for(lck, std::chrono::seconds(timeout_seconds), pred); + auto pred = [&]() -> bool { return !EvtOccurs.empty(); }; + bool gotEvent = + Condition.wait_for(lck, std::chrono::seconds(timeout_seconds), pred); if (!gotEvent) return None; @@ -220,7 +226,7 @@ class DirectoryWatcherTest: public std::enable_shared_from_this(); @@ -300,18 +306,17 @@ TEST(DirectoryWatcherTest, fileEvents) { coll.append(evt->Events); } - EXPECT_TRUE(coll.hasEvents( - std::vector{"a", "b", "c"}, - std::vector{ - DirectoryWatcher::EventKind::Added, - DirectoryWatcher::EventKind::Removed, - DirectoryWatcher::EventKind::Added, - }, - std::vector{ - stats[0], - file_status{}, - stats[1], - })); + EXPECT_TRUE(coll.hasEvents(std::vector{"a", "b", "c"}, + std::vector{ + DirectoryWatcher::EventKind::Added, + DirectoryWatcher::EventKind::Removed, + DirectoryWatcher::EventKind::Added, + }, + std::vector{ + stats[0], + file_status{}, + stats[1], + })); } { std::vector fnames = {"a", "c"};