mirror of
				https://github.com/MaskRay/ccls.git
				synced 2025-10-30 20:22:38 +00:00 
			
		
		
		
	Add siphash.h, HashUSR -> HashUsr
This commit is contained in:
		
							parent
							
								
									54ac72115e
								
							
						
					
					
						commit
						e20a6e9790
					
				| @ -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; | ||||
| } | ||||
|  | ||||
| @ -1,6 +1,5 @@ | ||||
| #include "import_pipeline.h" | ||||
| 
 | ||||
| #include "clang_cursor.h"  // HashUSR
 | ||||
| #include "cache_manager.h" | ||||
| #include "config.h" | ||||
| #include "iindexer.h" | ||||
|  | ||||
| @ -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<IndexTypeId> 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
 | ||||
|  | ||||
							
								
								
									
										30
									
								
								src/query.cc
									
									
									
									
									
								
							
							
						
						
									
										30
									
								
								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<Usr>{HashUSR("usr1")}); | ||||
|     REQUIRE(update.funcs_removed == std::vector<Usr>{HashUSR("usr2")}); | ||||
|     REQUIRE(update.vars_removed == std::vector<Usr>{HashUSR("usr3")}); | ||||
|     REQUIRE(update.types_removed == std::vector<Usr>{HashUsr("usr1")}); | ||||
|     REQUIRE(update.funcs_removed == std::vector<Usr>{HashUsr("usr2")}); | ||||
|     REQUIRE(update.vars_removed == std::vector<Usr>{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)), | ||||
|  | ||||
| @ -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 = "<fundamental>"; | ||||
|     assert(value.Resolve(it->second)->uses.size() == 0); | ||||
|  | ||||
							
								
								
									
										14
									
								
								src/utils.cc
									
									
									
									
									
								
							
							
						
						
									
										14
									
								
								src/utils.cc
									
									
									
									
									
								
							| @ -5,6 +5,7 @@ | ||||
| #include <doctest/doctest.h> | ||||
| #include <tinydir.h> | ||||
| #include <loguru/loguru.hpp> | ||||
| #include <siphash.h> | ||||
| 
 | ||||
| #include <algorithm> | ||||
| #include <cassert> | ||||
| @ -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<const uint8_t*>(s), n, k, out, 8); | ||||
|  | ||||
| @ -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); | ||||
|  | ||||
| @ -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; | ||||
|  | ||||
							
								
								
									
										4
									
								
								third_party/siphash.h
									
									
									
									
										vendored
									
									
										Normal file
									
								
							
							
						
						
									
										4
									
								
								third_party/siphash.h
									
									
									
									
										vendored
									
									
										Normal 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); | ||||
		Loading…
	
		Reference in New Issue
	
	Block a user