Fangrui Song
6 years ago
9 changed files with 73 additions and 148 deletions
@ -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; |
|||
} |
@ -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_; |
|||
}; |
Loading…
Reference in new issue