Add siphash.h, HashUSR -> HashUsr

This commit is contained in:
Jacob Dufault 2018-01-14 13:18:12 -08:00
parent 54ac72115e
commit e20a6e9790
9 changed files with 41 additions and 33 deletions

View File

@ -161,7 +161,7 @@ std::string ClangCursor::get_usr() const {
Usr ClangCursor::get_usr_hash() const { Usr ClangCursor::get_usr_hash() const {
CXString usr = clang_getCursorUSR(cx_cursor); CXString usr = clang_getCursorUSR(cx_cursor);
Usr ret = HashUSR(clang_getCString(usr)); Usr ret = HashUsr(clang_getCString(usr));
clang_disposeString(usr); clang_disposeString(usr);
return ret; return ret;
} }

View File

@ -1,6 +1,5 @@
#include "import_pipeline.h" #include "import_pipeline.h"
#include "clang_cursor.h" // HashUSR
#include "cache_manager.h" #include "cache_manager.h"
#include "config.h" #include "config.h"
#include "iindexer.h" #include "iindexer.h"

View File

@ -742,7 +742,7 @@ void VisitDeclForTypeUsageVisitorHandler(ClangCursor cursor,
if (referenced_usr == "") if (referenced_usr == "")
return; 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) if (!param->initial_type)
param->initial_type = ref_type_id; param->initial_type = ref_type_id;
@ -965,7 +965,7 @@ ClangCursor::VisitResult AddDeclInitializerUsagesVisitor(ClangCursor cursor,
break; break;
Range loc = cursor.get_spelling_range(); 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); IndexVar* ref_def = db->Resolve(ref_id);
UniqueAdd(ref_def->uses, loc); UniqueAdd(ref_def->uses, loc);
break; break;
@ -1264,7 +1264,7 @@ void OnIndexDeclaration(CXClientData client_data, const CXIdxDeclInfo* decl) {
decl_cursor.template_specialization_to_template_definition()) decl_cursor.template_specialization_to_template_definition())
break; 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); IndexVar* var = db->Resolve(var_id);
// TODO: Eventually run with this if. Right now I want to iron out bugs // 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<IndexTypeId> alias_of = AddDeclTypeUsages( optional<IndexTypeId> alias_of = AddDeclTypeUsages(
db, decl->cursor, decl->semanticContainer, decl->lexicalContainer); 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); IndexType* type = db->Resolve(type_id);
if (alias_of) if (alias_of)
@ -1536,7 +1536,7 @@ void OnIndexDeclaration(CXClientData client_data, const CXIdxDeclInfo* decl) {
ClangCursor decl_cursor = decl->cursor; ClangCursor decl_cursor = decl->cursor;
Range decl_loc_spelling = decl_cursor.get_spelling_range(); 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); IndexType* type = db->Resolve(type_id);
// TODO: Eventually run with this if. Right now I want to iron out bugs // 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); ClangCursor ref_cursor(ref->cursor);
Range loc = ref_cursor.get_spelling_range(); 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); IndexFunc* called = db->Resolve(called_id);
// libclang doesn't provide a nice api to check if the given function // libclang doesn't provide a nice api to check if the given function

View File

@ -898,30 +898,30 @@ TEST_SUITE("query") {
IndexFile previous("foo.cc", nullopt); IndexFile previous("foo.cc", nullopt);
IndexFile current("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)); 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)); 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)); Range(Position(3, 0));
IndexUpdate update = GetDelta(previous, current); IndexUpdate update = GetDelta(previous, current);
REQUIRE(update.types_removed == std::vector<Usr>{HashUSR("usr1")}); REQUIRE(update.types_removed == std::vector<Usr>{HashUsr("usr1")});
REQUIRE(update.funcs_removed == std::vector<Usr>{HashUSR("usr2")}); REQUIRE(update.funcs_removed == std::vector<Usr>{HashUsr("usr2")});
REQUIRE(update.vars_removed == std::vector<Usr>{HashUSR("usr3")}); REQUIRE(update.vars_removed == std::vector<Usr>{HashUsr("usr3")});
} }
TEST_CASE("do not remove ref-only defs") { TEST_CASE("do not remove ref-only defs") {
IndexFile previous("foo.cc", nullopt); IndexFile previous("foo.cc", nullopt);
IndexFile current("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))); ->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)), ->callers.push_back(IndexFuncRef(IndexFuncId(0), Range(Position(2, 0)),
false /*is_implicit*/)); false /*is_implicit*/));
previous.Resolve(previous.ToVarId(HashUSR("usr3"))) previous.Resolve(previous.ToVarId(HashUsr("usr3")))
->uses.push_back(Range(Position(3, 0))); ->uses.push_back(Range(Position(3, 0)));
IndexUpdate update = GetDelta(previous, current); IndexUpdate update = GetDelta(previous, current);
@ -935,8 +935,8 @@ TEST_SUITE("query") {
IndexFile previous("foo.cc", nullopt); IndexFile previous("foo.cc", nullopt);
IndexFile current("foo.cc", nullopt); IndexFile current("foo.cc", nullopt);
IndexFunc* pf = previous.Resolve(previous.ToFuncId(HashUSR("usr"))); IndexFunc* pf = previous.Resolve(previous.ToFuncId(HashUsr("usr")));
IndexFunc* cf = current.Resolve(current.ToFuncId(HashUSR("usr"))); IndexFunc* cf = current.Resolve(current.ToFuncId(HashUsr("usr")));
pf->callers.push_back(IndexFuncRef(IndexFuncId(0), Range(Position(1, 0)), pf->callers.push_back(IndexFuncRef(IndexFuncId(0), Range(Position(1, 0)),
false /*is_implicit*/)); false /*is_implicit*/));
@ -960,8 +960,8 @@ TEST_SUITE("query") {
IndexFile previous("foo.cc", nullopt); IndexFile previous("foo.cc", nullopt);
IndexFile current("foo.cc", nullopt); IndexFile current("foo.cc", nullopt);
IndexType* pt = previous.Resolve(previous.ToTypeId(HashUSR("usr"))); IndexType* pt = previous.Resolve(previous.ToTypeId(HashUsr("usr")));
IndexType* ct = current.Resolve(current.ToTypeId(HashUSR("usr"))); IndexType* ct = current.Resolve(current.ToTypeId(HashUsr("usr")));
pt->uses.push_back(Range(Position(1, 0))); pt->uses.push_back(Range(Position(1, 0)));
ct->uses.push_back(Range(Position(2, 0))); ct->uses.push_back(Range(Position(2, 0)));
@ -981,8 +981,8 @@ TEST_SUITE("query") {
IndexFile previous("foo.cc", nullopt); IndexFile previous("foo.cc", nullopt);
IndexFile current("foo.cc", nullopt); IndexFile current("foo.cc", nullopt);
IndexFunc* pf = previous.Resolve(previous.ToFuncId(HashUSR("usr"))); IndexFunc* pf = previous.Resolve(previous.ToFuncId(HashUsr("usr")));
IndexFunc* cf = current.Resolve(current.ToFuncId(HashUSR("usr"))); IndexFunc* cf = current.Resolve(current.ToFuncId(HashUsr("usr")));
pf->callers.push_back(IndexFuncRef(IndexFuncId(0), Range(Position(1, 0)), pf->callers.push_back(IndexFuncRef(IndexFuncId(0), Range(Position(1, 0)),
false /*is_implicit*/)); false /*is_implicit*/));
pf->callers.push_back(IndexFuncRef(IndexFuncId(0), Range(Position(2, 0)), pf->callers.push_back(IndexFuncRef(IndexFuncId(0), Range(Position(2, 0)),

View File

@ -172,7 +172,7 @@ void Reflect(TVisitor& visitor, IndexVar& value) {
// IndexFile // IndexFile
bool ReflectMemberStart(Writer& visitor, IndexFile& value) { bool ReflectMemberStart(Writer& visitor, IndexFile& value) {
// FIXME // 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()) { if (it != value.id_cache.usr_to_type_id.end()) {
value.Resolve(it->second)->def.short_name = "<fundamental>"; value.Resolve(it->second)->def.short_name = "<fundamental>";
assert(value.Resolve(it->second)->uses.size() == 0); assert(value.Resolve(it->second)->uses.size() == 0);

View File

@ -5,6 +5,7 @@
#include <doctest/doctest.h> #include <doctest/doctest.h>
#include <tinydir.h> #include <tinydir.h>
#include <loguru/loguru.hpp> #include <loguru/loguru.hpp>
#include <siphash.h>
#include <algorithm> #include <algorithm>
#include <cassert> #include <cassert>
@ -52,17 +53,20 @@ std::string Trim(std::string s) {
return s; return s;
} }
uint64_t HashUSR(const char* s) { uint64_t HashUsr(const std::string& s) {
return HashUSR(s, strlen(s)); return HashUsr(s.c_str(), s.size());
} }
uint64_t HashUSR(const char* s, size_t n) { uint64_t HashUsr(const char* s) {
extern int siphash(const uint8_t *in, const size_t inlen, const uint8_t *k, return HashUsr(s, strlen(s));
uint8_t *out, const size_t outlen); }
uint64_t HashUsr(const char* s, size_t n) {
union { union {
uint64_t ret; uint64_t ret;
uint8_t out[8]; 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, const uint8_t k[16] = {0xd0, 0xe5, 0x4d, 0x61, 0x74, 0x63, 0x68, 0x52,
0x61, 0x79, 0xea, 0x70, 0xca, 0x70, 0xf0, 0x0d}; 0x61, 0x79, 0xea, 0x70, 0xca, 0x70, 0xf0, 0x0d};
(void)siphash(reinterpret_cast<const uint8_t*>(s), n, k, out, 8); (void)siphash(reinterpret_cast<const uint8_t*>(s), n, k, out, 8);

View File

@ -18,8 +18,9 @@ void TrimEndInPlace(std::string& s);
void TrimInPlace(std::string& s); void TrimInPlace(std::string& s);
std::string Trim(std::string s); std::string Trim(std::string s);
uint64_t HashUSR(const char* s); uint64_t HashUsr(const std::string& s);
uint64_t HashUSR(const char* s, size_t n); 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|. // 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);

View File

@ -144,7 +144,7 @@ void WorkingFile::ComputeLineMapping() {
int i = 0; int i = 0;
for (auto& line : index_lines) { for (auto& line : index_lines) {
std::string trimmed = Trim(line); 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); auto it = hash_to_unique.find(h);
if (it == hash_to_unique.end()) { if (it == hash_to_unique.end()) {
hash_to_unique[h] = i; hash_to_unique[h] = i;
@ -162,7 +162,7 @@ void WorkingFile::ComputeLineMapping() {
hash_to_unique.clear(); hash_to_unique.clear();
for (auto& line : buffer_lines) { for (auto& line : buffer_lines) {
std::string trimmed = Trim(line); 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); auto it = hash_to_unique.find(h);
if (it == hash_to_unique.end()) { if (it == hash_to_unique.end()) {
hash_to_unique[h] = i; hash_to_unique[h] = i;

4
third_party/siphash.h vendored Normal file
View File

@ -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);