From e20a6e9790eb97db8f3a5ed7b44b80f17d8b719e Mon Sep 17 00:00:00 2001 From: Jacob Dufault Date: Sun, 14 Jan 2018 13:18:12 -0800 Subject: [PATCH] Add siphash.h, HashUSR -> HashUsr --- src/clang_cursor.cc | 2 +- src/import_pipeline.cc | 1 - src/indexer.cc | 12 ++++++------ src/query.cc | 30 +++++++++++++++--------------- src/serializer.cc | 2 +- src/utils.cc | 14 +++++++++----- src/utils.h | 5 +++-- src/working_files.cc | 4 ++-- third_party/siphash.h | 4 ++++ 9 files changed, 41 insertions(+), 33 deletions(-) create mode 100644 third_party/siphash.h diff --git a/src/clang_cursor.cc b/src/clang_cursor.cc index d1d38b57..7a1eb14f 100644 --- a/src/clang_cursor.cc +++ b/src/clang_cursor.cc @@ -161,7 +161,7 @@ std::string ClangCursor::get_usr() const { Usr ClangCursor::get_usr_hash() const { CXString usr = clang_getCursorUSR(cx_cursor); - Usr ret = HashUSR(clang_getCString(usr)); + Usr ret = HashUsr(clang_getCString(usr)); clang_disposeString(usr); return ret; } diff --git a/src/import_pipeline.cc b/src/import_pipeline.cc index d0d3e6c8..8fad369a 100644 --- a/src/import_pipeline.cc +++ b/src/import_pipeline.cc @@ -1,6 +1,5 @@ #include "import_pipeline.h" -#include "clang_cursor.h" // HashUSR #include "cache_manager.h" #include "config.h" #include "iindexer.h" diff --git a/src/indexer.cc b/src/indexer.cc index bf225499..5a6f953a 100644 --- a/src/indexer.cc +++ b/src/indexer.cc @@ -742,7 +742,7 @@ void VisitDeclForTypeUsageVisitorHandler(ClangCursor cursor, if (referenced_usr == "") return; - IndexTypeId ref_type_id = db->ToTypeId(HashUSR(referenced_usr.c_str())); + IndexTypeId ref_type_id = db->ToTypeId(HashUsr(referenced_usr)); if (!param->initial_type) param->initial_type = ref_type_id; @@ -965,7 +965,7 @@ ClangCursor::VisitResult AddDeclInitializerUsagesVisitor(ClangCursor cursor, break; Range loc = cursor.get_spelling_range(); - IndexVarId ref_id = db->ToVarId(HashUSR(ref_usr.c_str())); + IndexVarId ref_id = db->ToVarId(HashUsr(ref_usr)); IndexVar* ref_def = db->Resolve(ref_id); UniqueAdd(ref_def->uses, loc); break; @@ -1264,7 +1264,7 @@ void OnIndexDeclaration(CXClientData client_data, const CXIdxDeclInfo* decl) { decl_cursor.template_specialization_to_template_definition()) break; - IndexVarId var_id = db->ToVarId(HashUSR(decl->entityInfo->USR)); + IndexVarId var_id = db->ToVarId(HashUsr(decl->entityInfo->USR)); IndexVar* var = db->Resolve(var_id); // TODO: Eventually run with this if. Right now I want to iron out bugs @@ -1484,7 +1484,7 @@ void OnIndexDeclaration(CXClientData client_data, const CXIdxDeclInfo* decl) { optional alias_of = AddDeclTypeUsages( db, decl->cursor, decl->semanticContainer, decl->lexicalContainer); - IndexTypeId type_id = db->ToTypeId(HashUSR(decl->entityInfo->USR)); + IndexTypeId type_id = db->ToTypeId(HashUsr(decl->entityInfo->USR)); IndexType* type = db->Resolve(type_id); if (alias_of) @@ -1536,7 +1536,7 @@ void OnIndexDeclaration(CXClientData client_data, const CXIdxDeclInfo* decl) { ClangCursor decl_cursor = decl->cursor; Range decl_loc_spelling = decl_cursor.get_spelling_range(); - IndexTypeId type_id = db->ToTypeId(HashUSR(decl->entityInfo->USR)); + IndexTypeId type_id = db->ToTypeId(HashUsr(decl->entityInfo->USR)); IndexType* type = db->Resolve(type_id); // TODO: Eventually run with this if. Right now I want to iron out bugs @@ -1726,7 +1726,7 @@ void OnIndexReference(CXClientData client_data, const CXIdxEntityRefInfo* ref) { ClangCursor ref_cursor(ref->cursor); Range loc = ref_cursor.get_spelling_range(); - IndexFuncId called_id = db->ToFuncId(HashUSR(ref->referencedEntity->USR)); + IndexFuncId called_id = db->ToFuncId(HashUsr(ref->referencedEntity->USR)); IndexFunc* called = db->Resolve(called_id); // libclang doesn't provide a nice api to check if the given function diff --git a/src/query.cc b/src/query.cc index 1fc9884e..23bceeca 100644 --- a/src/query.cc +++ b/src/query.cc @@ -898,30 +898,30 @@ TEST_SUITE("query") { IndexFile previous("foo.cc", nullopt); IndexFile current("foo.cc", nullopt); - previous.Resolve(previous.ToTypeId(HashUSR("usr1")))->def.definition_spelling = + previous.Resolve(previous.ToTypeId(HashUsr("usr1")))->def.definition_spelling = Range(Position(1, 0)); - previous.Resolve(previous.ToFuncId(HashUSR("usr2")))->def.definition_spelling = + previous.Resolve(previous.ToFuncId(HashUsr("usr2")))->def.definition_spelling = Range(Position(2, 0)); - previous.Resolve(previous.ToVarId(HashUSR("usr3")))->def.definition_spelling = + previous.Resolve(previous.ToVarId(HashUsr("usr3")))->def.definition_spelling = Range(Position(3, 0)); IndexUpdate update = GetDelta(previous, current); - REQUIRE(update.types_removed == std::vector{HashUSR("usr1")}); - REQUIRE(update.funcs_removed == std::vector{HashUSR("usr2")}); - REQUIRE(update.vars_removed == std::vector{HashUSR("usr3")}); + REQUIRE(update.types_removed == std::vector{HashUsr("usr1")}); + REQUIRE(update.funcs_removed == std::vector{HashUsr("usr2")}); + REQUIRE(update.vars_removed == std::vector{HashUsr("usr3")}); } TEST_CASE("do not remove ref-only defs") { IndexFile previous("foo.cc", nullopt); IndexFile current("foo.cc", nullopt); - previous.Resolve(previous.ToTypeId(HashUSR("usr1"))) + previous.Resolve(previous.ToTypeId(HashUsr("usr1"))) ->uses.push_back(Range(Position(1, 0))); - previous.Resolve(previous.ToFuncId(HashUSR("usr2"))) + previous.Resolve(previous.ToFuncId(HashUsr("usr2"))) ->callers.push_back(IndexFuncRef(IndexFuncId(0), Range(Position(2, 0)), false /*is_implicit*/)); - previous.Resolve(previous.ToVarId(HashUSR("usr3"))) + previous.Resolve(previous.ToVarId(HashUsr("usr3"))) ->uses.push_back(Range(Position(3, 0))); IndexUpdate update = GetDelta(previous, current); @@ -935,8 +935,8 @@ TEST_SUITE("query") { IndexFile previous("foo.cc", nullopt); IndexFile current("foo.cc", nullopt); - IndexFunc* pf = previous.Resolve(previous.ToFuncId(HashUSR("usr"))); - IndexFunc* cf = current.Resolve(current.ToFuncId(HashUSR("usr"))); + IndexFunc* pf = previous.Resolve(previous.ToFuncId(HashUsr("usr"))); + IndexFunc* cf = current.Resolve(current.ToFuncId(HashUsr("usr"))); pf->callers.push_back(IndexFuncRef(IndexFuncId(0), Range(Position(1, 0)), false /*is_implicit*/)); @@ -960,8 +960,8 @@ TEST_SUITE("query") { IndexFile previous("foo.cc", nullopt); IndexFile current("foo.cc", nullopt); - IndexType* pt = previous.Resolve(previous.ToTypeId(HashUSR("usr"))); - IndexType* ct = current.Resolve(current.ToTypeId(HashUSR("usr"))); + IndexType* pt = previous.Resolve(previous.ToTypeId(HashUsr("usr"))); + IndexType* ct = current.Resolve(current.ToTypeId(HashUsr("usr"))); pt->uses.push_back(Range(Position(1, 0))); ct->uses.push_back(Range(Position(2, 0))); @@ -981,8 +981,8 @@ TEST_SUITE("query") { IndexFile previous("foo.cc", nullopt); IndexFile current("foo.cc", nullopt); - IndexFunc* pf = previous.Resolve(previous.ToFuncId(HashUSR("usr"))); - IndexFunc* cf = current.Resolve(current.ToFuncId(HashUSR("usr"))); + IndexFunc* pf = previous.Resolve(previous.ToFuncId(HashUsr("usr"))); + IndexFunc* cf = current.Resolve(current.ToFuncId(HashUsr("usr"))); pf->callers.push_back(IndexFuncRef(IndexFuncId(0), Range(Position(1, 0)), false /*is_implicit*/)); pf->callers.push_back(IndexFuncRef(IndexFuncId(0), Range(Position(2, 0)), diff --git a/src/serializer.cc b/src/serializer.cc index d2c079f9..041f1cea 100644 --- a/src/serializer.cc +++ b/src/serializer.cc @@ -172,7 +172,7 @@ void Reflect(TVisitor& visitor, IndexVar& value) { // IndexFile bool ReflectMemberStart(Writer& visitor, IndexFile& value) { // FIXME - auto it = value.id_cache.usr_to_type_id.find(HashUSR("")); + auto it = value.id_cache.usr_to_type_id.find(HashUsr("")); if (it != value.id_cache.usr_to_type_id.end()) { value.Resolve(it->second)->def.short_name = ""; assert(value.Resolve(it->second)->uses.size() == 0); diff --git a/src/utils.cc b/src/utils.cc index 182403e6..7ed618f1 100644 --- a/src/utils.cc +++ b/src/utils.cc @@ -5,6 +5,7 @@ #include #include #include +#include #include #include @@ -52,17 +53,20 @@ std::string Trim(std::string s) { return s; } -uint64_t HashUSR(const char* s) { - return HashUSR(s, strlen(s)); +uint64_t HashUsr(const std::string& s) { + return HashUsr(s.c_str(), s.size()); } -uint64_t HashUSR(const char* s, size_t n) { - extern int siphash(const uint8_t *in, const size_t inlen, const uint8_t *k, - uint8_t *out, const size_t outlen); +uint64_t HashUsr(const char* s) { + return HashUsr(s, strlen(s)); +} + +uint64_t HashUsr(const char* s, size_t n) { union { uint64_t ret; uint8_t out[8]; }; + // k is an arbitrary key. Don't change it. const uint8_t k[16] = {0xd0, 0xe5, 0x4d, 0x61, 0x74, 0x63, 0x68, 0x52, 0x61, 0x79, 0xea, 0x70, 0xca, 0x70, 0xf0, 0x0d}; (void)siphash(reinterpret_cast(s), n, k, out, 8); diff --git a/src/utils.h b/src/utils.h index c1f5d6a0..d5fc55be 100644 --- a/src/utils.h +++ b/src/utils.h @@ -18,8 +18,9 @@ void TrimEndInPlace(std::string& s); void TrimInPlace(std::string& s); std::string Trim(std::string s); -uint64_t HashUSR(const char* s); -uint64_t HashUSR(const char* s, size_t n); +uint64_t HashUsr(const std::string& s); +uint64_t HashUsr(const char* s); +uint64_t HashUsr(const char* s, size_t n); // Returns true if |value| starts/ends with |start| or |ending|. bool StartsWith(const std::string& value, const std::string& start); diff --git a/src/working_files.cc b/src/working_files.cc index f86cc03d..63b3800f 100644 --- a/src/working_files.cc +++ b/src/working_files.cc @@ -144,7 +144,7 @@ void WorkingFile::ComputeLineMapping() { int i = 0; for (auto& line : index_lines) { std::string trimmed = Trim(line); - uint64_t h = HashUSR(trimmed.data(), trimmed.size()); + uint64_t h = HashUsr(trimmed); auto it = hash_to_unique.find(h); if (it == hash_to_unique.end()) { hash_to_unique[h] = i; @@ -162,7 +162,7 @@ void WorkingFile::ComputeLineMapping() { hash_to_unique.clear(); for (auto& line : buffer_lines) { std::string trimmed = Trim(line); - uint64_t h = HashUSR(trimmed.data(), trimmed.size()); + uint64_t h = HashUsr(trimmed); auto it = hash_to_unique.find(h); if (it == hash_to_unique.end()) { hash_to_unique[h] = i; diff --git a/third_party/siphash.h b/third_party/siphash.h new file mode 100644 index 00000000..8caab4a7 --- /dev/null +++ b/third_party/siphash.h @@ -0,0 +1,4 @@ +#pragma once + +int siphash(const uint8_t *in, const size_t inlen, const uint8_t *k, + uint8_t *out, const size_t outlen); \ No newline at end of file