|
1 | 1 | #pragma once |
2 | 2 |
|
3 | 3 | #include "TUIndex.h" |
4 | | -#include "Support/Bitmap.h" |
5 | 4 | #include "llvm/Support/Allocator.h" |
| 5 | +#include "llvm/Support/MemoryBuffer.h" |
6 | 6 |
|
7 | | -namespace llvm { |
8 | | - |
9 | | -template <typename... Ts> |
10 | | -unsigned dense_hash(const Ts&... ts) { |
11 | | - return llvm::DenseMapInfo<std::tuple<Ts...>>::getHashValue(std::tuple{ts...}); |
12 | | -} |
13 | | - |
14 | | -template <> |
15 | | -struct DenseMapInfo<clice::index::Occurrence> { |
16 | | - using R = clice::LocalSourceRange; |
17 | | - using V = clice::index::Occurrence; |
18 | | - |
19 | | - inline static V getEmptyKey() { |
20 | | - return V(R(-1, 0), 0); |
21 | | - } |
22 | | - |
23 | | - inline static V getTombstoneKey() { |
24 | | - return V(R(-2, 0), 0); |
25 | | - } |
26 | | - |
27 | | - static auto getHashValue(const V& v) { |
28 | | - return dense_hash(v.range.begin, v.range.end, v.target); |
29 | | - } |
30 | | - |
31 | | - static bool isEqual(const V& lhs, const V& rhs) { |
32 | | - return lhs.range == rhs.range && lhs.target == rhs.target; |
33 | | - } |
34 | | -}; |
| 7 | +namespace clice::index { |
35 | 8 |
|
36 | | -template <> |
37 | | -struct DenseMapInfo<clice::index::Relation> { |
38 | | - using R = clice::index::Relation; |
| 9 | +class MergedIndex { |
| 10 | +private: |
| 11 | + struct Impl; |
39 | 12 |
|
40 | | - inline static R getEmptyKey() { |
41 | | - return R{ |
42 | | - .kind = clice::RelationKind(), |
43 | | - .range = clice::LocalSourceRange(-1, 0), |
44 | | - .target_symbol = 0, |
45 | | - }; |
46 | | - } |
| 13 | + using Self = MergedIndex; |
47 | 14 |
|
48 | | - inline static R getTombstoneKey() { |
49 | | - return R{ |
50 | | - .kind = clice::RelationKind(), |
51 | | - .range = clice::LocalSourceRange(-2, 0), |
52 | | - .target_symbol = 0, |
53 | | - }; |
54 | | - } |
| 15 | + MergedIndex(std::unique_ptr<llvm::MemoryBuffer> buffer, std::unique_ptr<Impl> impl); |
55 | 16 |
|
56 | | - /// Contextual doen't take part in hashing and equality. |
57 | | - static auto getHashValue(const R& relation) { |
58 | | - return dense_hash(relation.kind.value(), |
59 | | - relation.range.begin, |
60 | | - relation.range.end, |
61 | | - relation.target_symbol); |
62 | | - } |
| 17 | + void load_in_memory(this Self& self); |
63 | 18 |
|
64 | | - static bool isEqual(const R& lhs, const R& rhs) { |
65 | | - return lhs.kind == rhs.kind && lhs.range == rhs.range && |
66 | | - lhs.target_symbol == rhs.target_symbol; |
67 | | - } |
68 | | -}; |
| 19 | +public: |
| 20 | + MergedIndex(); |
69 | 21 |
|
70 | | -} // namespace llvm |
| 22 | + MergedIndex(llvm::StringRef data); |
71 | 23 |
|
72 | | -namespace clice::index { |
73 | | - |
74 | | -/// struct CompilationContext { |
75 | | -/// /// The target of this compilation. |
76 | | -/// llvm::StringRef target; |
77 | | -/// |
78 | | -/// /// The canonical compilation command. |
79 | | -/// llvm::StringRef command; |
80 | | -/// |
81 | | -/// /// A version field for verification. |
82 | | -/// std::uint32_t version; |
83 | | -/// }; |
84 | | -/// |
85 | | -/// struct HeaderContext : CompilationContext { |
86 | | -/// /// The include location in the include graph. |
87 | | -/// std::uint32_t include; |
88 | | -/// |
89 | | -/// /// The path of the file includes this header. |
90 | | -/// llvm::StringRef path; |
91 | | -/// }; |
92 | | - |
93 | | -struct HeaderContexts { |
94 | | - std::uint32_t version = 0; |
95 | | - |
96 | | - struct Context { |
97 | | - std::uint32_t include; |
98 | | - std::uint32_t canonical_id; |
99 | | - |
100 | | - friend bool operator== (const Context&, const Context&) = default; |
101 | | - }; |
102 | | - |
103 | | - /// A array of include location and its context id. |
104 | | - llvm::SmallVector<Context> includes; |
105 | | - |
106 | | - friend bool operator== (const HeaderContexts&, const HeaderContexts&) = default; |
107 | | -}; |
| 24 | + MergedIndex(const MergedIndex&) = delete; |
108 | 25 |
|
109 | | -struct MergedIndex { |
110 | | - /// For each merged index, we will give it a canonical id. |
111 | | - /// The max canonical id. |
112 | | - std::uint32_t max_canonical_id = 0; |
| 26 | + MergedIndex(MergedIndex&& other); |
113 | 27 |
|
114 | | - /// We use the value of SHA256 to judge whether two indices are same. |
115 | | - /// Index with same content will be given same canonical id. |
116 | | - llvm::StringMap<std::uint32_t> canonical_cache; |
| 28 | + MergedIndex& operator= (const MergedIndex&) = delete; |
117 | 29 |
|
118 | | - /// The reference count of each canonical id. |
119 | | - std::vector<std::uint32_t> canonical_ref_counts; |
| 30 | + MergedIndex& operator= (MergedIndex&& other); |
120 | 31 |
|
121 | | - /// The canonical id set of removed index. |
122 | | - roaring::Roaring removed; |
| 32 | + ~MergedIndex(); |
123 | 33 |
|
124 | | - /// A map between source file path and its header contexts. |
125 | | - llvm::StringMap<HeaderContexts> contexts; |
| 34 | + /// Load merged index from disk |
| 35 | + static MergedIndex load(llvm::StringRef path); |
126 | 36 |
|
127 | | - /// All merged symbol occurrences. |
128 | | - llvm::DenseMap<Occurrence, roaring::Roaring> occurrences; |
| 37 | + /// Serialize it to binary format. |
| 38 | + void serialize(this const Self& self, llvm::raw_ostream& out); |
129 | 39 |
|
130 | | - /// All merged symbol relations. |
131 | | - llvm::DenseMap<SymbolHash, llvm::DenseMap<Relation, roaring::Roaring>> relations; |
| 40 | + /// Lookup the occurrence in corresponding offset. |
| 41 | + void lookup(this const Self& self, |
| 42 | + std::uint32_t offset, |
| 43 | + llvm::function_ref<bool(const Occurrence&)> callback); |
132 | 44 |
|
133 | | - /// FIXME: The content of this file. |
134 | | - /// std::string content; |
| 45 | + /// Lookup the relations of given symbol. |
| 46 | + void lookup(this const Self& self, |
| 47 | + SymbolHash symbol, |
| 48 | + RelationKind kind, |
| 49 | + llvm::function_ref<bool(const Relation&)> callback); |
135 | 50 |
|
136 | | - /// Sorted occurrences cache for fast lookup. |
137 | | - std::vector<Occurrence> cache_occurrences; |
| 51 | + /// Whether this index needs rebuilding. |
| 52 | + bool need_update(this const Self& self, llvm::ArrayRef<llvm::StringRef> path_mapping); |
138 | 53 |
|
139 | | - void remove(llvm::StringRef path); |
| 54 | + bool need_rewrite() { |
| 55 | + return impl != nullptr; |
| 56 | + } |
140 | 57 |
|
141 | | - void merge(llvm::StringRef path, std::uint32_t include, FileIndex& index); |
| 58 | + /// Remove the index of specific path id. |
| 59 | + void remove(this Self& self, std::uint32_t path_id); |
142 | 60 |
|
143 | | - std::vector<Occurrence> lookup(std::uint32_t offset); |
| 61 | + /// Merge the index with given compilation context. |
| 62 | + void merge(this Self& self, |
| 63 | + std::uint32_t path_id, |
| 64 | + std::chrono::milliseconds build_at, |
| 65 | + std::vector<IncludeLocation> include_locations, |
| 66 | + FileIndex& index); |
144 | 67 |
|
145 | | - void serialize(this MergedIndex& self, llvm::raw_ostream& out); |
| 68 | + /// Merge the index with given header context. |
| 69 | + void merge(this Self& self, std::uint32_t path_id, std::uint32_t include_id, FileIndex& index); |
146 | 70 |
|
147 | | - friend bool operator== (const MergedIndex&, const MergedIndex&) = default; |
148 | | -}; |
| 71 | + friend bool operator== (MergedIndex& lhs, MergedIndex& rhs); |
149 | 72 |
|
150 | | -struct MergedIndexView { |
151 | | - const void* data; |
| 73 | +private: |
| 74 | + /// The binary serialization data of index. If you load merged index |
| 75 | + /// from disk, we use directly access the data without deserialization |
| 76 | + /// unless you want to modify it. |
| 77 | + std::unique_ptr<llvm::MemoryBuffer> buffer; |
152 | 78 |
|
153 | | - MergedIndex deserialize(); |
| 79 | + /// The in memory data of the index. |
| 80 | + std::unique_ptr<Impl> impl; |
154 | 81 | }; |
155 | 82 |
|
156 | 83 | } // namespace clice::index |
0 commit comments