- Slightly more robust file parsing

- Make memory ownership slightly clearer in indexer
This commit is contained in:
Jacob Dufault 2017-04-12 23:01:42 -07:00
parent 94090b7b50
commit d5e5d96a9e
7 changed files with 43 additions and 12 deletions

View File

@ -58,6 +58,7 @@ QueryableFile* FindFile(QueryableDatabase* db, const std::string& filename, Quer
} }
QueryableFile* FindFile(QueryableDatabase* db, const std::string& filename) { QueryableFile* FindFile(QueryableDatabase* db, const std::string& filename) {
// TODO: consider calling NormalizePath here. It might add too much latency though.
auto it = db->usr_to_symbol.find(filename); auto it = db->usr_to_symbol.find(filename);
if (it != db->usr_to_symbol.end()) if (it != db->usr_to_symbol.end())
return &db->files[it->second.idx]; return &db->files[it->second.idx];
@ -538,7 +539,7 @@ void RegisterMessageTypes() {
MessageRegistry::instance()->Register<Ipc_WorkspaceSymbol>(); MessageRegistry::instance()->Register<Ipc_WorkspaceSymbol>();
} }
bool IndexMain_DoIndex(FileConsumer* file_consumer, bool IndexMain_DoIndex(FileConsumer::SharedState* file_consumer_shared,
Index_DoIndexQueue* queue_do_index, Index_DoIndexQueue* queue_do_index,
Index_DoIdMapQueue* queue_do_id_map) { Index_DoIdMapQueue* queue_do_id_map) {
optional<Index_DoIndex> index_request = queue_do_index->TryDequeue(); optional<Index_DoIndex> index_request = queue_do_index->TryDequeue();
@ -589,7 +590,7 @@ bool IndexMain_DoIndex(FileConsumer* file_consumer,
} }
// Parse request and send a response. // Parse request and send a response.
std::vector<std::unique_ptr<IndexedFile>> indexes = Parse(file_consumer, index_request->path, index_request->args); std::vector<std::unique_ptr<IndexedFile>> indexes = Parse(file_consumer_shared, index_request->path, index_request->args);
time.ResetAndPrint("Parsing/indexing " + index_request->path); time.ResetAndPrint("Parsing/indexing " + index_request->path);
for (auto& current_index : indexes) { for (auto& current_index : indexes) {
@ -657,8 +658,6 @@ void IndexMain(
Index_OnIdMappedQueue* queue_on_id_mapped, Index_OnIdMappedQueue* queue_on_id_mapped,
Index_OnIndexedQueue* queue_on_indexed) { Index_OnIndexedQueue* queue_on_indexed) {
FileConsumer file_consumer(file_consumer_shared);
while (true) { while (true) {
// TODO: process all off IndexMain_DoIndex before calling IndexMain_DoCreateIndexUpdate for // TODO: process all off IndexMain_DoIndex before calling IndexMain_DoCreateIndexUpdate for
// better icache behavior. We need to have some threads spinning on both though // better icache behavior. We need to have some threads spinning on both though
@ -666,7 +665,7 @@ void IndexMain(
int count = 0; int count = 0;
if (!IndexMain_DoIndex(&file_consumer, queue_do_index, queue_do_id_map) && if (!IndexMain_DoIndex(file_consumer_shared, queue_do_index, queue_do_id_map) &&
!IndexMain_DoCreateIndexUpdate(queue_on_id_mapped, queue_on_indexed)) { !IndexMain_DoCreateIndexUpdate(queue_on_id_mapped, queue_on_indexed)) {
//if (count++ > 2) { //if (count++ > 2) {

View File

@ -1394,7 +1394,7 @@ void indexEntityReference(CXClientData client_data,
std::vector<std::unique_ptr<IndexedFile>> Parse(FileConsumer* file_consumer, std::string filename, std::vector<std::string> args, bool dump_ast) { std::vector<std::unique_ptr<IndexedFile>> Parse(FileConsumer::SharedState* file_consumer_shared, std::string filename, std::vector<std::string> args, bool dump_ast) {
filename = NormalizePath(filename); filename = NormalizePath(filename);
//return {}; //return {};
@ -1415,10 +1415,11 @@ std::vector<std::unique_ptr<IndexedFile>> Parse(FileConsumer* file_consumer, std
&indexEntityReference} &indexEntityReference}
}; };
IndexParam param(file_consumer); FileConsumer file_consumer(file_consumer_shared);
IndexParam param(&file_consumer);
CXFile file = clang_getFile(tu.cx_tu, filename.c_str()); CXFile file = clang_getFile(tu.cx_tu, filename.c_str());
param.primary_file = file_consumer->ForceLocal(file); param.primary_file = file_consumer.ForceLocal(file);
std::cerr << "!! [START] Indexing " << filename << std::endl; std::cerr << "!! [START] Indexing " << filename << std::endl;
CXIndexAction index_action = clang_IndexAction_create(index.cx_index); CXIndexAction index_action = clang_IndexAction_create(index.cx_index);

View File

@ -444,4 +444,4 @@ struct IndexedFile {
std::string ToString(); std::string ToString();
}; };
std::vector<std::unique_ptr<IndexedFile>> Parse(FileConsumer* file_consumer, std::string filename, std::vector<std::string> args, bool dump_ast = false); std::vector<std::unique_ptr<IndexedFile>> Parse(FileConsumer::SharedState* file_consumer_shared, std::string filename, std::vector<std::string> args, bool dump_ast = false);

View File

@ -126,6 +126,10 @@ void lsDocumentUri::SetPath(const std::string& path) {
raw_uri.replace(raw_uri.begin() + index, raw_uri.begin() + index + 1, "%3A"); raw_uri.replace(raw_uri.begin() + index, raw_uri.begin() + index + 1, "%3A");
} }
raw_uri = ReplaceAll(raw_uri, " ", "%20");
raw_uri = ReplaceAll(raw_uri, "(", "%28");
raw_uri = ReplaceAll(raw_uri, ")", "%29");
// TODO: proper fix // TODO: proper fix
#if defined(_WIN32) #if defined(_WIN32)
raw_uri = "file:///" + raw_uri; raw_uri = "file:///" + raw_uri;
@ -136,9 +140,16 @@ void lsDocumentUri::SetPath(const std::string& path) {
} }
std::string lsDocumentUri::GetPath() const { std::string lsDocumentUri::GetPath() const {
// c:/Program%20Files%20%28x86%29/Microsoft%20Visual%20Studio%2014.0/VC/include/vcruntime.
// C:/Program Files (x86)
// TODO: make this not a hack. // TODO: make this not a hack.
std::string result = raw_uri; std::string result = raw_uri;
result = ReplaceAll(result, "%20", " ");
result = ReplaceAll(result, "%28", "(");
result = ReplaceAll(result, "%29", ")");
size_t index = result.find("%3A"); size_t index = result.find("%3A");
if (index != -1) { if (index != -1) {
result.replace(result.begin() + index, result.begin() + index + 3, ":"); result.replace(result.begin() + index, result.begin() + index + 3, ":");

View File

@ -130,11 +130,10 @@ void RunTests() {
std::unordered_map<std::string, std::string> all_expected_output = ParseTestExpectation(path); std::unordered_map<std::string, std::string> all_expected_output = ParseTestExpectation(path);
FileConsumer::SharedState file_consumer_shared; FileConsumer::SharedState file_consumer_shared;
FileConsumer file_consumer(&file_consumer_shared);
// Run test. // Run test.
std::cout << "[START] " << path << std::endl; std::cout << "[START] " << path << std::endl;
std::vector<std::unique_ptr<IndexedFile>> dbs = Parse(&file_consumer, path, { std::vector<std::unique_ptr<IndexedFile>> dbs = Parse(&file_consumer_shared, path, {
"-xc++", "-xc++",
"-std=c++11", "-std=c++11",
"-IC:/Users/jacob/Desktop/superindex/indexer/third_party/", "-IC:/Users/jacob/Desktop/superindex/indexer/third_party/",

View File

@ -20,6 +20,26 @@ bool StartsWith(const std::string& value, const std::string& start) {
return std::equal(start.begin(), start.end(), value.begin()); return std::equal(start.begin(), start.end(), value.begin());
} }
// See http://stackoverflow.com/a/29752943
std::string ReplaceAll(const std::string& source, const std::string& from, const std::string& to) {
std::string result;
result.reserve(source.length()); // avoids a few memory allocations
std::string::size_type last_pos = 0;
std::string::size_type find_pos;
while (std::string::npos != (find_pos = source.find(from, last_pos))) {
result.append(source, last_pos, find_pos - last_pos);
result += to;
last_pos = find_pos + from.length();
}
// Care for the rest after last occurrence
result += source.substr(last_pos);
return result;
}
static std::vector<std::string> GetFilesInFolderHelper(std::string folder, bool recursive, std::string output_prefix) { static std::vector<std::string> GetFilesInFolderHelper(std::string folder, bool recursive, std::string output_prefix) {
std::vector<std::string> result; std::vector<std::string> result;

View File

@ -10,6 +10,7 @@
// Returns true if |value| starts/ends with |start| or |ending|. // Returns true if |value| starts/ends with |start| or |ending|.
bool StartsWith(const std::string& value, const std::string& start); bool StartsWith(const std::string& value, const std::string& start);
bool EndsWith(const std::string& value, const std::string& ending); bool EndsWith(const std::string& value, const std::string& ending);
std::string ReplaceAll(const std::string& source, const std::string& from, const std::string& to);
// Finds all files in the given folder. This is recursive. // Finds all files in the given folder. This is recursive.
std::vector<std::string> GetFilesInFolder(std::string folder, bool recursive, bool add_folder_to_path); std::vector<std::string> GetFilesInFolder(std::string folder, bool recursive, bool add_folder_to_path);