• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1 //===--- Merge.h -------------------------------------------------*- C++-*-===//
2 //
3 // Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions.
4 // See https://llvm.org/LICENSE.txt for license information.
5 // SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception
6 //
7 //===----------------------------------------------------------------------===//
8 
9 #ifndef LLVM_CLANG_TOOLS_EXTRA_CLANGD_INDEX_MERGE_H
10 #define LLVM_CLANG_TOOLS_EXTRA_CLANGD_INDEX_MERGE_H
11 
12 #include "Index.h"
13 
14 namespace clang {
15 namespace clangd {
16 
17 // Merge symbols L and R, preferring data from L in case of conflict.
18 // The two symbols must have the same ID.
19 // Returned symbol may contain data owned by either source.
20 Symbol mergeSymbol(const Symbol &L, const Symbol &R);
21 
22 // MergedIndex is a composite index based on two provided Indexes:
23 //  - the Dynamic index covers few files, but is relatively up-to-date.
24 //  - the Static index covers a bigger set of files, but is relatively stale.
25 // The returned index attempts to combine results, and avoid duplicates.
26 //
27 // FIXME: We don't have a mechanism in Index to track deleted symbols and
28 // refs in dirty files, so the merged index may return stale symbols
29 // and refs from Static index.
30 class MergedIndex : public SymbolIndex {
31   const SymbolIndex *Dynamic, *Static;
32 
33 public:
34   // The constructor does not access the symbols.
35   // It's safe to inherit from this class and pass pointers to derived members.
MergedIndex(const SymbolIndex * Dynamic,const SymbolIndex * Static)36   MergedIndex(const SymbolIndex *Dynamic, const SymbolIndex *Static)
37       : Dynamic(Dynamic), Static(Static) {}
38 
39   bool fuzzyFind(const FuzzyFindRequest &,
40                  llvm::function_ref<void(const Symbol &)>) const override;
41   void lookup(const LookupRequest &,
42               llvm::function_ref<void(const Symbol &)>) const override;
43   bool refs(const RefsRequest &,
44             llvm::function_ref<void(const Ref &)>) const override;
45   void relations(const RelationsRequest &,
46                  llvm::function_ref<void(const SymbolID &, const Symbol &)>)
47       const override;
estimateMemoryUsage()48   size_t estimateMemoryUsage() const override {
49     return Dynamic->estimateMemoryUsage() + Static->estimateMemoryUsage();
50   }
51 };
52 
53 } // namespace clangd
54 } // namespace clang
55 
56 #endif // LLVM_CLANG_TOOLS_EXTRA_CLANGD_INDEX_MERGE_H
57