2017-04-08 20:00:08 +00:00
|
|
|
#include "file_consumer.h"
|
|
|
|
|
2017-05-21 03:46:15 +00:00
|
|
|
#include "clang_utils.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-12-02 01:04:39 +00:00
|
|
|
#include <loguru.hpp>
|
|
|
|
|
2018-01-11 05:16:46 +00:00
|
|
|
namespace {
|
|
|
|
|
|
|
|
optional<std::string> GetFileContents(const std::string& path,
|
|
|
|
FileContentsMap* file_contents) {
|
|
|
|
auto it = file_contents->find(path);
|
|
|
|
if (it == file_contents->end()) {
|
|
|
|
optional<std::string> content = ReadContent(path);
|
|
|
|
if (content)
|
|
|
|
(*file_contents)[path] = FileContents(path, *content);
|
|
|
|
return content;
|
|
|
|
}
|
|
|
|
return it->second.content;
|
|
|
|
}
|
|
|
|
|
|
|
|
} // namespace
|
|
|
|
|
2017-04-12 07:04:06 +00:00
|
|
|
bool operator==(const CXFileUniqueID& a, const CXFileUniqueID& b) {
|
2017-09-22 01:14:57 +00:00
|
|
|
return a.data[0] == b.data[0] && a.data[1] == b.data[1] &&
|
|
|
|
a.data[2] == b.data[2];
|
2017-04-12 07:04:06 +00:00
|
|
|
}
|
|
|
|
|
2017-12-29 16:29:47 +00:00
|
|
|
bool FileConsumerSharedState::Mark(const std::string& file) {
|
2017-04-24 01:01:51 +00:00
|
|
|
std::lock_guard<std::mutex> lock(mutex);
|
2018-01-07 04:08:55 +00:00
|
|
|
return used_files.insert(file).second;
|
2017-04-24 01:01:51 +00:00
|
|
|
}
|
|
|
|
|
2017-12-29 16:29:47 +00:00
|
|
|
void FileConsumerSharedState::Reset(const std::string& file) {
|
2017-04-24 01:01:51 +00:00
|
|
|
std::lock_guard<std::mutex> lock(mutex);
|
2018-01-07 04:08:55 +00:00
|
|
|
auto it = used_files.find(file);
|
|
|
|
if (it != used_files.end())
|
|
|
|
used_files.erase(it);
|
2017-04-24 01:01:51 +00:00
|
|
|
}
|
|
|
|
|
2017-12-29 16:29:47 +00:00
|
|
|
FileConsumer::FileConsumer(FileConsumerSharedState* shared_state,
|
2017-09-22 01:14:57 +00:00
|
|
|
const std::string& parse_file)
|
2017-06-14 03:08:31 +00:00
|
|
|
: shared_(shared_state), parse_file_(parse_file) {}
|
2017-04-08 20:00:08 +00:00
|
|
|
|
2018-01-11 05:16:46 +00:00
|
|
|
IndexFile* FileConsumer::TryConsumeFile(CXFile file,
|
|
|
|
bool* is_first_ownership,
|
|
|
|
FileContentsMap* file_contents) {
|
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) {
|
2017-06-16 06:07:03 +00:00
|
|
|
EmitError(file);
|
2017-04-12 07:04:06 +00:00
|
|
|
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.
|
2017-04-24 01:01:51 +00:00
|
|
|
bool did_insert = shared_->Mark(file_name);
|
2017-04-11 05:26:27 +00:00
|
|
|
*is_first_ownership = did_insert;
|
2018-01-11 05:16:46 +00:00
|
|
|
local_[file_id] =
|
|
|
|
did_insert ? MakeUnique<IndexFile>(
|
|
|
|
file_name, GetFileContents(file_name, file_contents))
|
|
|
|
: nullptr;
|
2017-04-12 07:04:06 +00:00
|
|
|
return local_[file_id].get();
|
2017-04-10 00:08:54 +00:00
|
|
|
}
|
|
|
|
|
2018-01-11 05:16:46 +00:00
|
|
|
IndexFile* FileConsumer::ForceLocal(CXFile file,
|
|
|
|
FileContentsMap* file_contents) {
|
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;
|
2018-01-11 05:16:46 +00:00
|
|
|
IndexFile* cache = TryConsumeFile(file, &is_first, file_contents);
|
2017-04-20 07:53:33 +00:00
|
|
|
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) {
|
2017-06-16 06:07:03 +00:00
|
|
|
EmitError(file);
|
2017-04-12 07:04:06 +00:00
|
|
|
return nullptr;
|
|
|
|
}
|
|
|
|
|
|
|
|
auto it = local_.find(file_id);
|
2018-01-11 05:16:46 +00:00
|
|
|
if (it == local_.end() || !it->second) {
|
|
|
|
std::string file_name = FileName(file);
|
|
|
|
local_[file_id] = MakeUnique<IndexFile>(
|
|
|
|
file_name, GetFileContents(file_name, file_contents));
|
|
|
|
}
|
2017-04-12 07:04:06 +00:00
|
|
|
assert(local_.find(file_id) != local_.end());
|
|
|
|
return local_[file_id].get();
|
2017-04-10 00:08:54 +00:00
|
|
|
}
|
|
|
|
|
2017-05-12 06:08:15 +00:00
|
|
|
std::vector<std::unique_ptr<IndexFile>> FileConsumer::TakeLocalState() {
|
|
|
|
std::vector<std::unique_ptr<IndexFile>> result;
|
2017-04-10 00:08:54 +00:00
|
|
|
for (auto& entry : local_) {
|
|
|
|
if (entry.second)
|
|
|
|
result.push_back(std::move(entry.second));
|
|
|
|
}
|
|
|
|
return result;
|
2017-06-16 06:07:03 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
void FileConsumer::EmitError(CXFile file) const {
|
2017-11-04 22:29:03 +00:00
|
|
|
std::string file_name = ToString(clang_getFileName(file));
|
2017-06-20 01:51:56 +00:00
|
|
|
// TODO: Investigate this more, why can we get an empty file name?
|
|
|
|
if (!file_name.empty()) {
|
2017-12-02 01:04:39 +00:00
|
|
|
LOG_S(ERROR) << "Could not get unique file id for " << file_name
|
|
|
|
<< " when parsing " << parse_file_;
|
2017-06-20 01:51:56 +00:00
|
|
|
}
|
2017-04-08 20:00:08 +00:00
|
|
|
}
|