1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245
|
//===--- IndexDataStore.cpp - Index data store info -----------------------===//
//
// Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions.
// See https://llvm.org/LICENSE.txt for license information.
// SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception
//
//===----------------------------------------------------------------------===//
#include "clang/Index/IndexDataStore.h"
#include "clang/Basic/PathRemapper.h"
#include "clang/DirectoryWatcher/DirectoryWatcher.h"
#include "../lib/Index/IndexDataStoreUtils.h"
#include "llvm/ADT/ArrayRef.h"
#include "llvm/ADT/STLExtras.h"
#include "llvm/Support/FileSystem.h"
#include "llvm/Support/Mutex.h"
#include "llvm/Support/Path.h"
#include "llvm/Support/raw_ostream.h"
using namespace clang;
using namespace clang::index;
using namespace clang::index::store;
using namespace llvm;
namespace {
class UnitEventHandlerData {
mutable sys::Mutex Mtx;
IndexDataStore::UnitEventHandler Handler;
public:
void setHandler(IndexDataStore::UnitEventHandler handler) {
sys::ScopedLock L(Mtx);
Handler = std::move(handler);
}
IndexDataStore::UnitEventHandler getHandler() const {
sys::ScopedLock L(Mtx);
return Handler;
}
};
class IndexDataStoreImpl {
std::string FilePath;
PathRemapper Remapper;
std::shared_ptr<UnitEventHandlerData> TheUnitEventHandlerData;
std::unique_ptr<DirectoryWatcher> DirWatcher;
public:
explicit IndexDataStoreImpl(StringRef indexStorePath, PathRemapper remapper)
: FilePath(indexStorePath), Remapper(remapper) {
TheUnitEventHandlerData = std::make_shared<UnitEventHandlerData>();
}
StringRef getFilePath() const { return FilePath; }
const PathRemapper &getPathRemapper() const {
return Remapper;
}
bool foreachUnitName(bool sorted,
llvm::function_ref<bool(StringRef unitName)> receiver);
void setUnitEventHandler(IndexDataStore::UnitEventHandler Handler);
bool startEventListening(bool waitInitialSync, std::string &Error);
void stopEventListening();
void discardUnit(StringRef UnitName);
void discardRecord(StringRef RecordName);
void purgeStaleData();
};
} // anonymous namespace
bool IndexDataStoreImpl::foreachUnitName(bool sorted,
llvm::function_ref<bool(StringRef unitName)> receiver) {
SmallString<128> UnitPath;
UnitPath = FilePath;
appendUnitSubDir(UnitPath);
std::vector<std::string> filenames;
std::error_code EC;
for (auto It = sys::fs::directory_iterator(UnitPath, EC),
End = sys::fs::directory_iterator();
!EC && It != End; It.increment(EC)) {
StringRef unitName = sys::path::filename(It->path());
if (!sorted) {
if (!receiver(unitName))
return false;
} else {
filenames.push_back(std::string(unitName));
}
}
if (sorted) {
std::sort(filenames.begin(), filenames.end());
for (auto &fname : filenames)
if (!receiver(fname))
return false;
}
return true;
}
void IndexDataStoreImpl::setUnitEventHandler(IndexDataStore::UnitEventHandler handler) {
TheUnitEventHandlerData->setHandler(std::move(handler));
}
bool IndexDataStoreImpl::startEventListening(bool waitInitialSync, std::string &Error) {
if (DirWatcher) {
Error = "event listener already active";
return true;
}
SmallString<128> UnitPath;
UnitPath = FilePath;
appendUnitSubDir(UnitPath);
auto localUnitEventHandlerData = TheUnitEventHandlerData;
auto OnUnitsChange = [localUnitEventHandlerData](ArrayRef<DirectoryWatcher::Event> Events, bool isInitial) {
SmallVector<IndexDataStore::UnitEvent, 16> UnitEvents;
UnitEvents.reserve(Events.size());
for (const DirectoryWatcher::Event &evt : Events) {
IndexDataStore::UnitEventKind K;
StringRef UnitName = sys::path::filename(evt.Filename);
switch (evt.Kind) {
case DirectoryWatcher::Event::EventKind::Removed:
K = IndexDataStore::UnitEventKind::Removed; break;
case DirectoryWatcher::Event::EventKind::Modified:
K = IndexDataStore::UnitEventKind::Modified; break;
case DirectoryWatcher::Event::EventKind::WatchedDirRemoved:
K = IndexDataStore::UnitEventKind::DirectoryDeleted;
UnitName = StringRef();
break;
case DirectoryWatcher::Event::EventKind::WatcherGotInvalidated:
K = IndexDataStore::UnitEventKind::Failure;
UnitName = StringRef();
break;
}
UnitEvents.push_back(IndexDataStore::UnitEvent{K, UnitName});
}
if (auto handler = localUnitEventHandlerData->getHandler()) {
IndexDataStore::UnitEventNotification EventNote{isInitial, UnitEvents};
handler(EventNote);
}
};
// Create the unit path if necessary so that the directory watcher can start
// even if the data has not been populated yet.
if (std::error_code EC = llvm::sys::fs::create_directories(UnitPath)) {
Error = EC.message();
return true;
}
llvm::Expected<std::unique_ptr<DirectoryWatcher>> ExpectedDirWatcher =
DirectoryWatcher::create(UnitPath.str(), OnUnitsChange, waitInitialSync);
if (!ExpectedDirWatcher) {
Error = llvm::toString(ExpectedDirWatcher.takeError());
return true;
}
DirWatcher = std::move(ExpectedDirWatcher.get());
return false;
}
void IndexDataStoreImpl::stopEventListening() {
DirWatcher.reset();
}
void IndexDataStoreImpl::discardUnit(StringRef UnitName) {
SmallString<128> UnitPath;
UnitPath = FilePath;
appendUnitSubDir(UnitPath);
appendInteriorUnitPath(UnitName, UnitPath);
sys::fs::remove(UnitPath);
}
void IndexDataStoreImpl::discardRecord(StringRef RecordName) {
SmallString<128> RecordPath;
RecordPath = FilePath;
appendRecordSubDir(RecordPath);
appendInteriorRecordPath(RecordName, RecordPath);
sys::fs::remove(RecordPath);
}
void IndexDataStoreImpl::purgeStaleData() {
// FIXME: Implement.
}
std::unique_ptr<IndexDataStore>
IndexDataStore::create(StringRef IndexStorePath, const PathRemapper &Remapper,
std::string &Error) {
if (!sys::fs::exists(IndexStorePath)) {
raw_string_ostream OS(Error);
OS << "index store path does not exist: " << IndexStorePath;
return nullptr;
}
return std::unique_ptr<IndexDataStore>(
new IndexDataStore(new IndexDataStoreImpl(IndexStorePath, Remapper)));
}
#define IMPL static_cast<IndexDataStoreImpl*>(Impl)
IndexDataStore::~IndexDataStore() {
delete IMPL;
}
StringRef IndexDataStore::getFilePath() const {
return IMPL->getFilePath();
}
const PathRemapper & IndexDataStore::getPathRemapper() const {
return IMPL->getPathRemapper();
}
bool IndexDataStore::foreachUnitName(bool sorted,
llvm::function_ref<bool(StringRef unitName)> receiver) {
return IMPL->foreachUnitName(sorted, std::move(receiver));
}
unsigned IndexDataStore::getFormatVersion() {
return STORE_FORMAT_VERSION;
}
void IndexDataStore::setUnitEventHandler(UnitEventHandler Handler) {
return IMPL->setUnitEventHandler(std::move(Handler));
}
bool IndexDataStore::startEventListening(bool waitInitialSync, std::string &Error) {
return IMPL->startEventListening(waitInitialSync, Error);
}
void IndexDataStore::stopEventListening() {
return IMPL->stopEventListening();
}
void IndexDataStore::discardUnit(StringRef UnitName) {
IMPL->discardUnit(UnitName);
}
void IndexDataStore::discardRecord(StringRef RecordName) {
IMPL->discardRecord(RecordName);
}
void IndexDataStore::purgeStaleData() {
IMPL->purgeStaleData();
}
|