Delete file_consumer.*

This commit is contained in:
Fangrui Song 2018-09-20 18:04:55 -07:00
parent 28401961ae
commit f0400fdcf2
9 changed files with 73 additions and 148 deletions

View File

@ -183,7 +183,6 @@ target_sources(ccls PRIVATE
src/clang_tu.cc src/clang_tu.cc
src/clang_utils.cc src/clang_utils.cc
src/config.cc src/config.cc
src/file_consumer.cc
src/filesystem.cc src/filesystem.cc
src/fuzzy_match.cc src/fuzzy_match.cc
src/main.cc src/main.cc

View File

@ -1,63 +0,0 @@
// Copyright 2017-2018 ccls Authors
// SPDX-License-Identifier: Apache-2.0
#include "file_consumer.h"
#include "clang_utils.h"
#include "indexer.h"
#include "log.hh"
#include "platform.h"
#include "utils.h"
bool VFS::Loaded(const std::string &path) {
std::lock_guard lock(mutex);
return state[path].loaded;
}
bool VFS::Stamp(const std::string &path, int64_t ts, int step) {
std::lock_guard<std::mutex> lock(mutex);
State &st = state[path];
if (st.timestamp < ts || (st.timestamp == ts && st.step < step)) {
st.timestamp = ts;
st.step = step;
return true;
} else
return false;
}
FileConsumer::FileConsumer(VFS *vfs, const std::string &parse_file)
: vfs_(vfs), parse_file_(parse_file) {}
IndexFile *FileConsumer::TryConsumeFile(
const clang::FileEntry &File,
const std::unordered_map<llvm::sys::fs::UniqueID, FileConsumer::File>
&UID2File) {
auto UniqueID = File.getUniqueID();
{
auto it = local_.find(UniqueID);
if (it != local_.end())
return it->second.get();
}
auto it = UID2File.find(UniqueID);
assert(it != UID2File.end());
assert(it->second.mtime);
if (!vfs_->Stamp(it->second.path, it->second.mtime, 1)) {
local_[UniqueID] = nullptr;
return nullptr;
}
// Build IndexFile instance.
local_[UniqueID] =
std::make_unique<IndexFile>(UniqueID, it->second.path, it->second.content);
return local_[UniqueID].get();
}
std::vector<std::unique_ptr<IndexFile>> FileConsumer::TakeLocalState() {
std::vector<std::unique_ptr<IndexFile>> result;
for (auto &entry : local_) {
if (entry.second)
result.push_back(std::move(entry.second));
}
return result;
}

View File

@ -1,70 +0,0 @@
// Copyright 2017-2018 ccls Authors
// SPDX-License-Identifier: Apache-2.0
#pragma once
#include "position.h"
#include "serializer.h"
#include "utils.h"
#include <clang/Basic/FileManager.h>
#include <mutex>
#include <unordered_map>
#include <vector>
struct IndexFile;
struct VFS {
struct State {
int64_t timestamp;
int step;
bool loaded;
};
mutable std::unordered_map<std::string, State> state;
mutable std::mutex mutex;
bool Loaded(const std::string &path);
bool Stamp(const std::string &path, int64_t ts, int step);
};
namespace std {
template <> struct hash<llvm::sys::fs::UniqueID> {
std::size_t operator()(llvm::sys::fs::UniqueID ID) const {
size_t ret = ID.getDevice();
hash_combine(ret, ID.getFile());
return ret;
}
};
} // namespace std
// FileConsumer is used by the indexer. When it encouters a file, it tries to
// take ownership over it. If the indexer has ownership over a file, it will
// produce an index, otherwise, it will emit nothing for that declarations
// and references coming from that file.
//
// The indexer does this because header files do not have their own translation
// units but we still want to index them.
struct FileConsumer {
struct File {
std::string path;
int64_t mtime;
std::string content;
};
FileConsumer(VFS *vfs, const std::string &parse_file);
// Returns IndexFile or nullptr for the file or nullptr.
IndexFile *TryConsumeFile(
const clang::FileEntry &file,
const std::unordered_map<llvm::sys::fs::UniqueID, File> &);
// Returns and passes ownership of all local state.
std::vector<std::unique_ptr<IndexFile>> TakeLocalState();
private:
std::unordered_map<llvm::sys::fs::UniqueID, std::unique_ptr<IndexFile>>
local_;
VFS *vfs_;
std::string parse_file_;
};

View File

@ -7,6 +7,7 @@
#include "clang_tu.h" #include "clang_tu.h"
#include "log.hh" #include "log.hh"
#include "match.h" #include "match.h"
#include "pipeline.hh"
#include "platform.h" #include "platform.h"
#include "serializer.h" #include "serializer.h"
using namespace ccls; using namespace ccls;
@ -36,8 +37,15 @@ constexpr int kInitializerMaxLines = 3;
GroupMatch *multiVersionMatcher; GroupMatch *multiVersionMatcher;
struct File {
std::string path;
int64_t mtime;
std::string content;
std::unique_ptr<IndexFile> db;
};
struct IndexParam { struct IndexParam {
std::unordered_map<llvm::sys::fs::UniqueID, FileConsumer::File> UID2File; std::unordered_map<llvm::sys::fs::UniqueID, File> UID2File;
std::unordered_map<llvm::sys::fs::UniqueID, bool> UID2multi; std::unordered_map<llvm::sys::fs::UniqueID, bool> UID2multi;
struct DeclInfo { struct DeclInfo {
Usr usr; Usr usr;
@ -46,10 +54,9 @@ struct IndexParam {
}; };
std::unordered_map<const Decl *, DeclInfo> Decl2Info; std::unordered_map<const Decl *, DeclInfo> Decl2Info;
VFS &vfs;
ASTContext *Ctx; ASTContext *Ctx;
FileConsumer *file_consumer = nullptr; IndexParam(VFS &vfs) : vfs(vfs) {}
IndexParam(FileConsumer *file_consumer) : file_consumer(file_consumer) {}
void SeenFile(const FileEntry &File) { void SeenFile(const FileEntry &File) {
// If this is the first time we have seen the file (ignoring if we are // If this is the first time we have seen the file (ignoring if we are
@ -64,12 +71,17 @@ struct IndexParam {
it->second.mtime = *tim; it->second.mtime = *tim;
if (std::optional<std::string> content = ReadContent(path)) if (std::optional<std::string> content = ReadContent(path))
it->second.content = *content; it->second.content = *content;
if (!vfs.Stamp(path, it->second.mtime, 1))
return;
it->second.db = std::make_unique<IndexFile>(File.getUniqueID(), path,
it->second.content);
} }
} }
IndexFile *ConsumeFile(const FileEntry &FE) { IndexFile *ConsumeFile(const FileEntry &FE) {
SeenFile(FE); SeenFile(FE);
return file_consumer->TryConsumeFile(FE, UID2File); return UID2File[FE.getUniqueID()].db.get();
} }
bool UseMultiVersion(const FileEntry &FE) { bool UseMultiVersion(const FileEntry &FE) {
@ -1262,8 +1274,7 @@ Index(CompletionManager *completion, WorkingFiles *wfiles, VFS *vfs,
if (!Clang->hasTarget()) if (!Clang->hasTarget())
return {}; return {};
FileConsumer file_consumer(vfs, file); IndexParam param(*vfs);
IndexParam param(&file_consumer);
auto DataConsumer = std::make_shared<IndexDataConsumer>(param); auto DataConsumer = std::make_shared<IndexDataConsumer>(param);
index::IndexingOptions IndexOpts; index::IndexingOptions IndexOpts;
@ -1300,8 +1311,11 @@ Index(CompletionManager *completion, WorkingFiles *wfiles, VFS *vfs,
for (auto &Buf : Bufs) for (auto &Buf : Bufs)
Buf.release(); Buf.release();
auto result = param.file_consumer->TakeLocalState(); std::vector<std::unique_ptr<IndexFile>> result;
for (std::unique_ptr<IndexFile> &entry : result) { for (auto &it : param.UID2File) {
if (!it.second.db)
continue;
std::unique_ptr<IndexFile> &entry = it.second.db;
entry->import_file = file; entry->import_file = file;
entry->args = args; entry->args = args;
for (auto &[_, it] : entry->uid2lid_and_path) for (auto &[_, it] : entry->uid2lid_and_path)
@ -1331,6 +1345,7 @@ Index(CompletionManager *completion, WorkingFiles *wfiles, VFS *vfs,
entry->dependencies[llvm::CachedHashStringRef(Intern(path))] = entry->dependencies[llvm::CachedHashStringRef(Intern(path))] =
file.mtime; file.mtime;
} }
result.push_back(std::move(entry));
} }
return result; return result;

View File

@ -4,7 +4,6 @@
#pragma once #pragma once
#include "clang_utils.h" #include "clang_utils.h"
#include "file_consumer.h"
#include "language.h" #include "language.h"
#include "lsp.h" #include "lsp.h"
#include "lsp_diagnostic.h" #include "lsp_diagnostic.h"
@ -224,6 +223,16 @@ struct IndexInclude {
const char *resolved_path; const char *resolved_path;
}; };
namespace std {
template <> struct hash<llvm::sys::fs::UniqueID> {
std::size_t operator()(llvm::sys::fs::UniqueID ID) const {
size_t ret = ID.getDevice();
hash_combine(ret, ID.getFile());
return ret;
}
};
} // namespace std
struct IndexFile { struct IndexFile {
// For both JSON and MessagePack cache files. // For both JSON and MessagePack cache files.
static const int kMajorVersion; static const int kMajorVersion;
@ -275,6 +284,7 @@ struct IndexFile {
struct CompletionManager; struct CompletionManager;
struct WorkingFiles; struct WorkingFiles;
struct VFS;
namespace ccls::idx { namespace ccls::idx {
void Init(); void Init();

View File

@ -35,10 +35,7 @@ struct Handler_CclsReload : BaseMessageHandler<In_CclsReload> {
const auto &params = request->params; const auto &params = request->params;
// Send index requests for every file. // Send index requests for every file.
if (params.whitelist.empty() && params.blacklist.empty()) { if (params.whitelist.empty() && params.blacklist.empty()) {
{ vfs->Clear();
std::lock_guard lock(vfs->mutex);
vfs->state.clear();
}
db->clear(); db->clear();
project->Index(working_files, lsRequestId()); project->Index(working_files, lsRequestId());
return; return;

View File

@ -62,6 +62,27 @@ void DiagnosticsPublisher::Publish(WorkingFiles *working_files,
} }
} }
void VFS::Clear() {
std::lock_guard lock(mutex);
state.clear();
}
bool VFS::Loaded(const std::string &path) {
std::lock_guard lock(mutex);
return state[path].loaded;
}
bool VFS::Stamp(const std::string &path, int64_t ts, int step) {
std::lock_guard<std::mutex> lock(mutex);
State &st = state[path];
if (st.timestamp < ts || (st.timestamp == ts && st.step < step)) {
st.timestamp = ts;
st.step = step;
return true;
} else
return false;
}
namespace ccls::pipeline { namespace ccls::pipeline {
int64_t loaded_ts = 0, tick = 0; int64_t loaded_ts = 0, tick = 0;

View File

@ -7,6 +7,7 @@
#include "method.h" #include "method.h"
#include "query.h" #include "query.h"
#include <mutex>
#include <string> #include <string>
#include <unordered_map> #include <unordered_map>
#include <vector> #include <vector>
@ -30,6 +31,20 @@ class DiagnosticsPublisher {
std::vector<lsDiagnostic> diagnostics); std::vector<lsDiagnostic> diagnostics);
}; };
struct VFS {
struct State {
int64_t timestamp;
int step;
bool loaded;
};
std::unordered_map<std::string, State> state;
std::mutex mutex;
void Clear();
bool Loaded(const std::string &path);
bool Stamp(const std::string &path, int64_t ts, int step);
};
namespace ccls { namespace ccls {
enum class IndexMode { enum class IndexMode {
NonInteractive, NonInteractive,

View File

@ -6,6 +6,7 @@
#include "clang_complete.hh" #include "clang_complete.hh"
#include "filesystem.hh" #include "filesystem.hh"
#include "indexer.h" #include "indexer.h"
#include "pipeline.hh"
#include "platform.h" #include "platform.h"
#include "serializer.h" #include "serializer.h"
#include "utils.h" #include "utils.h"