2017-04-08 20:00:08 +00:00
|
|
|
#include "file_consumer.h"
|
|
|
|
|
2017-04-08 22:54:36 +00:00
|
|
|
#include "indexer.h"
|
2017-04-12 07:04:06 +00:00
|
|
|
#include "platform.h"
|
2017-04-08 22:54:36 +00:00
|
|
|
#include "utils.h"
|
|
|
|
|
2017-04-12 07:04:06 +00:00
|
|
|
namespace {
|
|
|
|
|
|
|
|
std::string FileName(CXFile file) {
|
|
|
|
CXString cx_name = clang_getFileName(file);
|
|
|
|
std::string name = clang::ToString(cx_name);
|
|
|
|
return NormalizePath(name);
|
|
|
|
}
|
|
|
|
|
|
|
|
} // namespace
|
|
|
|
|
|
|
|
bool operator==(const CXFileUniqueID& a, const CXFileUniqueID& b) {
|
|
|
|
return a.data[0] == b.data[0] && a.data[1] == b.data[1] && a.data[2] == b.data[2];
|
|
|
|
}
|
|
|
|
|
2017-04-08 20:00:08 +00:00
|
|
|
FileConsumer::FileConsumer(SharedState* shared_state) : shared_(shared_state) {}
|
|
|
|
|
2017-04-12 07:04:06 +00:00
|
|
|
IndexedFile* FileConsumer::TryConsumeFile(CXFile file, bool* is_first_ownership) {
|
2017-04-11 05:26:27 +00:00
|
|
|
assert(is_first_ownership);
|
|
|
|
|
2017-04-12 07:04:06 +00:00
|
|
|
CXFileUniqueID file_id;
|
|
|
|
if (clang_getFileUniqueID(file, &file_id) != 0) {
|
|
|
|
std::cerr << "Could not get unique file id for " << FileName(file) << std::endl;
|
|
|
|
return nullptr;
|
|
|
|
}
|
|
|
|
|
2017-04-08 20:00:08 +00:00
|
|
|
// Try to find cached local result.
|
2017-04-12 07:04:06 +00:00
|
|
|
auto it = local_.find(file_id);
|
2017-04-11 05:26:27 +00:00
|
|
|
if (it != local_.end()) {
|
|
|
|
*is_first_ownership = false;
|
2017-04-08 22:54:36 +00:00
|
|
|
return it->second.get();
|
2017-04-11 05:26:27 +00:00
|
|
|
}
|
2017-04-08 20:00:08 +00:00
|
|
|
|
2017-04-12 07:04:06 +00:00
|
|
|
std::string file_name = FileName(file);
|
|
|
|
|
2017-04-08 20:00:08 +00:00
|
|
|
// No result in local; we need to query global.
|
|
|
|
bool did_insert = false;
|
|
|
|
{
|
2017-04-08 22:54:36 +00:00
|
|
|
std::lock_guard<std::mutex> lock(shared_->mutex);
|
2017-04-12 07:04:06 +00:00
|
|
|
did_insert = shared_->files.insert(file_name).second;
|
2017-04-08 20:00:08 +00:00
|
|
|
}
|
2017-04-11 05:26:27 +00:00
|
|
|
*is_first_ownership = did_insert;
|
2017-04-12 07:04:06 +00:00
|
|
|
local_[file_id] = did_insert ? MakeUnique<IndexedFile>(file_name) : nullptr;
|
|
|
|
return local_[file_id].get();
|
2017-04-10 00:08:54 +00:00
|
|
|
}
|
|
|
|
|
2017-04-12 07:04:06 +00:00
|
|
|
IndexedFile* FileConsumer::ForceLocal(CXFile file) {
|
2017-04-20 07:53:33 +00:00
|
|
|
// Try to fetch the file using the normal system, which will insert the file
|
|
|
|
// usage into global storage.
|
|
|
|
{
|
|
|
|
bool is_first;
|
|
|
|
IndexedFile* cache = TryConsumeFile(file, &is_first);
|
|
|
|
if (cache)
|
|
|
|
return cache;
|
|
|
|
}
|
|
|
|
|
|
|
|
// It's already been taken before, just create a local copy.
|
2017-04-12 07:04:06 +00:00
|
|
|
CXFileUniqueID file_id;
|
|
|
|
if (clang_getFileUniqueID(file, &file_id) != 0) {
|
|
|
|
std::cerr << "Could not get unique file id for " << FileName(file) << std::endl;
|
|
|
|
return nullptr;
|
|
|
|
}
|
|
|
|
|
|
|
|
auto it = local_.find(file_id);
|
2017-04-20 07:53:33 +00:00
|
|
|
if (it == local_.end() || !it->second)
|
2017-04-12 07:04:06 +00:00
|
|
|
local_[file_id] = MakeUnique<IndexedFile>(FileName(file));
|
|
|
|
assert(local_.find(file_id) != local_.end());
|
|
|
|
return local_[file_id].get();
|
2017-04-10 00:08:54 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
std::vector<std::unique_ptr<IndexedFile>> FileConsumer::TakeLocalState() {
|
|
|
|
std::vector<std::unique_ptr<IndexedFile>> result;
|
|
|
|
for (auto& entry : local_) {
|
|
|
|
if (entry.second)
|
|
|
|
result.push_back(std::move(entry.second));
|
|
|
|
}
|
|
|
|
return result;
|
2017-04-08 20:00:08 +00:00
|
|
|
}
|