3535#include " clang/Index/IndexingAction.h"
3636#include " clang/Lex/Preprocessor.h"
3737#include " clang/Serialization/ASTReader.h"
38+ #include " llvm/Support/BLAKE3.h"
3839#include " llvm/Support/Error.h"
40+ #include " llvm/Support/HashBuilder.h"
3941#include " llvm/Support/Path.h"
4042
4143using namespace swift ;
@@ -50,29 +52,48 @@ using clang::index::SymbolRoleSet;
5052// ===----------------------------------------------------------------------===//
5153
5254namespace {
55+
56+ using HashBuilderTy =
57+ llvm::HashBuilder<llvm::TruncatedBLAKE3<8 >, llvm::endianness::little>;
58+
5359class SymbolTracker {
5460public:
5561 struct SymbolRelation {
5662 size_t symbolIndex;
5763 SymbolRoleSet roles;
5864
59- llvm::hash_code hash () const { return llvm::hash_combine (symbolIndex, roles); }
65+ SymbolRelation (size_t symbolIndex, SymbolRoleSet roles)
66+ : symbolIndex(symbolIndex), roles(roles) {}
67+
68+ void hash (HashBuilderTy &HashBuilder) {
69+ HashBuilder.add (symbolIndex);
70+ HashBuilder.add (roles);
71+ }
6072 };
73+
6174 struct SymbolOccurrence {
6275 size_t symbolIndex;
6376 SymbolRoleSet roles;
6477 unsigned line;
6578 unsigned column;
6679 SmallVector<SymbolRelation, 3 > related;
6780
68- llvm::hash_code hash () const {
69- auto hash = llvm::hash_combine (symbolIndex, roles, line, column);
81+ SymbolOccurrence (size_t symbolIndex, SymbolRoleSet roles, unsigned line,
82+ unsigned column, SmallVector<SymbolRelation, 3 > related)
83+ : symbolIndex(symbolIndex), roles(roles), line(line), column(column),
84+ related (std::move(related)) {}
85+
86+ void hash (HashBuilderTy &HashBuilder) {
87+ HashBuilder.add (symbolIndex);
88+ HashBuilder.add (roles);
89+ HashBuilder.add (line);
90+ HashBuilder.add (column);
7091 for (auto &relation : related) {
71- hash = llvm::hash_combine (hash, relation.hash () );
92+ relation.hash (HashBuilder );
7293 }
73- return hash;
7494 }
7595 };
96+
7697 struct Symbol {
7798 StringRef name;
7899 StringRef USR;
@@ -81,12 +102,19 @@ class SymbolTracker {
81102 SymbolInfo symInfo;
82103 unsigned isTestCandidate : 1 ;
83104
84- llvm::hash_code hash () const {
85- return llvm::hash_combine (
86- name, USR, group,
87- static_cast <unsigned >(symInfo.Kind ),
88- static_cast <unsigned >(symInfo.SubKind ),
89- symInfo.Properties , isTestCandidate);
105+ Symbol (StringRef name, StringRef usr, StringRef group, SymbolInfo symInfo,
106+ bool isTestCandidate)
107+ : name(name), USR(usr), group(group), symInfo(std::move(symInfo)),
108+ isTestCandidate (isTestCandidate) {}
109+
110+ void hash (HashBuilderTy &HashBuilder) {
111+ HashBuilder.add (name);
112+ HashBuilder.add (USR);
113+ HashBuilder.add (group);
114+ HashBuilder.add (symInfo.Kind );
115+ HashBuilder.add (symInfo.SubKind );
116+ HashBuilder.add (symInfo.Properties );
117+ HashBuilder.add (isTestCandidate);
90118 }
91119 };
92120
@@ -114,13 +142,9 @@ class SymbolTracker {
114142 auto pair = USRToSymbol.insert (std::make_pair (indexSym.USR .data (),
115143 symbols.size ()));
116144 if (pair.second ) {
117- Symbol symbol{indexSym.name ,
118- indexSym.USR ,
119- indexSym.group ,
120- indexSym.symInfo ,
121- 0 };
122- recordHash = llvm::hash_combine (recordHash, symbol.hash ());
123- symbols.push_back (std::move (symbol));
145+ symbols.emplace_back (indexSym.name , indexSym.USR , indexSym.group ,
146+ indexSym.symInfo , 0 );
147+ symbols.back ().hash (HashBuilder);
124148 }
125149
126150 return pair.first ->second ;
@@ -131,26 +155,28 @@ class SymbolTracker {
131155
132156 SmallVector<SymbolRelation, 3 > relations;
133157 for (IndexRelation indexRel: indexOccur.Relations ) {
134- relations.push_back ({ addSymbol (indexRel), indexRel.roles } );
158+ relations.emplace_back ( addSymbol (indexRel), indexRel.roles );
135159 }
136160
137- occurrences.push_back ({/* symbolIndex=*/ addSymbol (indexOccur),
138- indexOccur.roles ,
139- indexOccur.line ,
140- indexOccur.column ,
141- std::move (relations)});
142-
143- recordHash = llvm::hash_combine (recordHash, occurrences.back ().hash ());
161+ occurrences.emplace_back (addSymbol (indexOccur), indexOccur.roles ,
162+ indexOccur.line , indexOccur.column ,
163+ std::move (relations));
164+ occurrences.back ().hash (HashBuilder);
144165 }
145166
146- llvm::hash_code hashRecord () const { return recordHash; }
167+ uint64_t hashRecord () {
168+ std::array<uint8_t , 8 > recordHashArr = HashBuilder.final ();
169+ uint64_t recordHash = 0 ;
170+ std::memcpy (&recordHash, recordHashArr.data (), recordHashArr.size ());
171+ return recordHash;
172+ }
147173
148174private:
149175 llvm::DenseMap<const char *, size_t > USRToSymbol;
150176 std::vector<Symbol> symbols;
151177 std::vector<SymbolOccurrence> occurrences;
152178 bool sorted = false ;
153- llvm::hash_code recordHash = 0 ;
179+ HashBuilderTy HashBuilder ;
154180};
155181
156182class IndexRecordingConsumer : public IndexDataConsumer {
0 commit comments