2017-04-08 20:00:08 +00:00
|
|
|
#pragma once
|
|
|
|
|
2018-04-08 06:32:35 +00:00
|
|
|
#include "position.h"
|
2018-05-13 16:52:19 +00:00
|
|
|
#include "serializer.h"
|
2017-04-12 07:04:06 +00:00
|
|
|
#include "utils.h"
|
|
|
|
|
2018-07-06 00:53:33 +00:00
|
|
|
#include <clang/Basic/FileManager.h>
|
2017-04-12 07:04:06 +00:00
|
|
|
|
2017-04-08 20:00:08 +00:00
|
|
|
#include <mutex>
|
2018-07-08 07:46:53 +00:00
|
|
|
#include <unordered_map>
|
2018-04-08 06:32:35 +00:00
|
|
|
#include <vector>
|
2017-09-22 01:14:57 +00:00
|
|
|
|
2017-05-12 06:08:15 +00:00
|
|
|
struct IndexFile;
|
2017-04-08 22:54:36 +00:00
|
|
|
|
2018-04-08 06:32:35 +00:00
|
|
|
struct FileContents {
|
2018-04-22 17:01:44 +00:00
|
|
|
FileContents() = default;
|
2018-04-08 06:32:35 +00:00
|
|
|
FileContents(const std::string& path, const std::string& content);
|
|
|
|
|
|
|
|
std::optional<int> ToOffset(Position p) const;
|
|
|
|
std::optional<std::string> ContentsInRange(Range range) const;
|
|
|
|
|
|
|
|
std::string path;
|
|
|
|
std::string content;
|
|
|
|
// {0, 1 + position of first newline, 1 + position of second newline, ...}
|
|
|
|
std::vector<int> line_offsets_;
|
|
|
|
};
|
|
|
|
|
2018-05-05 22:29:17 +00:00
|
|
|
struct VFS {
|
|
|
|
struct State {
|
|
|
|
int64_t timestamp;
|
|
|
|
int owner;
|
|
|
|
int stage;
|
|
|
|
};
|
|
|
|
mutable std::unordered_map<std::string, State> state;
|
2017-12-29 16:29:47 +00:00
|
|
|
mutable std::mutex mutex;
|
|
|
|
|
2018-05-05 22:29:17 +00:00
|
|
|
State Get(const std::string& file);
|
|
|
|
bool Mark(const std::string& file, int owner, int stage);
|
|
|
|
bool Stamp(const std::string& file, int64_t ts);
|
|
|
|
void ResetLocked(const std::string& file);
|
2017-12-29 16:29:47 +00:00
|
|
|
void Reset(const std::string& file);
|
|
|
|
};
|
|
|
|
|
2018-07-08 07:46:53 +00:00
|
|
|
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;
|
|
|
|
}
|
|
|
|
};
|
|
|
|
}
|
|
|
|
|
2017-04-08 20:00:08 +00:00
|
|
|
// 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 {
|
2018-05-05 22:29:17 +00:00
|
|
|
FileConsumer(VFS* vfs, const std::string& parse_file);
|
2017-04-08 20:00:08 +00:00
|
|
|
|
2017-05-12 06:08:15 +00:00
|
|
|
// Returns IndexFile for the file or nullptr. |is_first_ownership| is set
|
2017-04-11 05:26:27 +00:00
|
|
|
// to true iff the function just took ownership over the file. Otherwise it
|
|
|
|
// is set to false.
|
2018-01-11 05:16:46 +00:00
|
|
|
//
|
|
|
|
// note: file_contents is passed as a parameter instead of as a member
|
|
|
|
// variable since it is large and we do not want to copy it.
|
2018-07-06 00:53:33 +00:00
|
|
|
IndexFile* TryConsumeFile(const clang::FileEntry& file,
|
2018-04-08 06:32:35 +00:00
|
|
|
std::unordered_map<std::string, FileContents>* file_contents);
|
2017-04-08 20:00:08 +00:00
|
|
|
|
2017-04-08 22:54:36 +00:00
|
|
|
// Returns and passes ownership of all local state.
|
2017-05-12 06:08:15 +00:00
|
|
|
std::vector<std::unique_ptr<IndexFile>> TakeLocalState();
|
2017-04-08 20:00:08 +00:00
|
|
|
|
|
|
|
private:
|
2018-07-08 07:46:53 +00:00
|
|
|
std::unordered_map<llvm::sys::fs::UniqueID, std::unique_ptr<IndexFile>> local_;
|
2018-05-05 22:29:17 +00:00
|
|
|
VFS* vfs_;
|
2017-06-14 03:08:31 +00:00
|
|
|
std::string parse_file_;
|
2018-05-05 22:29:17 +00:00
|
|
|
int thread_id_;
|
2018-04-08 06:32:35 +00:00
|
|
|
};
|