2018-01-07 00:20:37 +00:00
|
|
|
#include "iindexer.h"
|
|
|
|
|
|
|
|
#include "indexer.h"
|
|
|
|
|
|
|
|
namespace {
|
|
|
|
struct ClangIndexer : IIndexer {
|
|
|
|
~ClangIndexer() override = default;
|
|
|
|
|
|
|
|
std::vector<std::unique_ptr<IndexFile>> Index(
|
|
|
|
Config* config,
|
|
|
|
FileConsumerSharedState* file_consumer_shared,
|
|
|
|
std::string file,
|
|
|
|
const std::vector<std::string>& args,
|
|
|
|
const std::vector<FileContents>& file_contents,
|
2018-01-07 00:51:55 +00:00
|
|
|
PerformanceImportFile* perf) override {
|
2018-01-08 07:51:36 +00:00
|
|
|
bool dump_ast = false;
|
|
|
|
for (const std::string& pattern : config->dumpAST)
|
|
|
|
if (file.find(pattern) != std::string::npos) {
|
|
|
|
dump_ast = true;
|
|
|
|
break;
|
|
|
|
}
|
2018-01-07 00:20:37 +00:00
|
|
|
return Parse(config, file_consumer_shared, file, args, file_contents, perf,
|
2018-01-08 07:51:36 +00:00
|
|
|
&index, dump_ast);
|
2018-01-07 00:20:37 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
// Note: constructing this acquires a global lock
|
|
|
|
ClangIndex index;
|
|
|
|
};
|
|
|
|
|
|
|
|
struct TestIndexer : IIndexer {
|
|
|
|
static std::unique_ptr<TestIndexer> FromEntries(
|
|
|
|
const std::vector<TestEntry>& entries) {
|
|
|
|
auto result = MakeUnique<TestIndexer>();
|
|
|
|
|
|
|
|
for (const TestEntry& entry : entries) {
|
|
|
|
std::vector<std::unique_ptr<IndexFile>> indexes;
|
|
|
|
|
|
|
|
if (entry.num_indexes > 0)
|
|
|
|
indexes.push_back(MakeUnique<IndexFile>(entry.path));
|
|
|
|
for (int i = 1; i < entry.num_indexes; ++i) {
|
|
|
|
indexes.push_back(MakeUnique<IndexFile>(entry.path + "_extra_" +
|
|
|
|
std::to_string(i) + ".h"));
|
|
|
|
}
|
|
|
|
|
|
|
|
result->indexes.insert(std::make_pair(entry.path, std::move(indexes)));
|
|
|
|
}
|
|
|
|
|
|
|
|
return result;
|
|
|
|
}
|
|
|
|
|
|
|
|
~TestIndexer() override = default;
|
|
|
|
|
|
|
|
std::vector<std::unique_ptr<IndexFile>> Index(
|
|
|
|
Config* config,
|
|
|
|
FileConsumerSharedState* file_consumer_shared,
|
|
|
|
std::string file,
|
|
|
|
const std::vector<std::string>& args,
|
|
|
|
const std::vector<FileContents>& file_contents,
|
2018-01-07 00:51:55 +00:00
|
|
|
PerformanceImportFile* perf) override {
|
2018-01-07 00:20:37 +00:00
|
|
|
auto it = indexes.find(file);
|
2018-01-07 00:30:57 +00:00
|
|
|
if (it == indexes.end()) {
|
|
|
|
// Don't return any indexes for unexpected data.
|
|
|
|
assert(false && "no indexes");
|
2018-01-07 00:20:37 +00:00
|
|
|
return {};
|
2018-01-07 00:30:57 +00:00
|
|
|
}
|
|
|
|
|
2018-01-07 00:20:37 +00:00
|
|
|
// FIXME: allow user to control how many times we return the index for a
|
|
|
|
// specific file (atm it is always 1)
|
|
|
|
auto result = std::move(it->second);
|
|
|
|
indexes.erase(it);
|
|
|
|
return result;
|
|
|
|
}
|
|
|
|
|
|
|
|
std::unordered_map<std::string, std::vector<std::unique_ptr<IndexFile>>>
|
|
|
|
indexes;
|
|
|
|
};
|
|
|
|
|
|
|
|
} // namespace
|
|
|
|
|
2018-01-07 01:11:34 +00:00
|
|
|
IIndexer::TestEntry::TestEntry(const std::string& path, int num_indexes)
|
|
|
|
: path(path), num_indexes(num_indexes) {}
|
|
|
|
|
2018-01-07 00:20:37 +00:00
|
|
|
// static
|
|
|
|
std::unique_ptr<IIndexer> IIndexer::MakeClangIndexer() {
|
|
|
|
return MakeUnique<ClangIndexer>();
|
|
|
|
}
|
|
|
|
|
|
|
|
// static
|
|
|
|
std::unique_ptr<IIndexer> IIndexer::MakeTestIndexer(
|
2018-01-07 01:11:34 +00:00
|
|
|
std::initializer_list<TestEntry> entries) {
|
2018-01-07 00:20:37 +00:00
|
|
|
return TestIndexer::FromEntries(entries);
|
|
|
|
}
|