~pali/llvm/clang-tools-extra-trunk

« back to all changes in this revision

Viewing changes to clangd/index/FileIndex.h

  • Committer: ioeric
  • Date: 2018-09-18 10:30:44 UTC
  • Revision ID: svn-v4:91177308-0d34-0410-b5e6-96231b3b80d8:clang-tools-extra/trunk:342460
[clangd] Merge ClangdServer::DynamicIndex into FileIndex. NFC.

Summary:
FileIndex now provides explicit interfaces for preamble and main file updates.
This avoids growing parameter list when preamble and main symbols diverge
further (e.g. D52078). This also gets rid of the hack in `indexAST` that
inferred main file index based on `TopLevelDecls`.

Also separate `indexMainDecls` from `indexAST`.

Reviewers: sammccall

Reviewed By: sammccall

Subscribers: ilya-biryukov, MaskRay, jkorous, arphaman, kadircet, cfe-commits

Differential Revision: https://reviews.llvm.org/D52222

Show diffs side-by-side

added added

removed removed

Lines of Context:
20
20
#include "Index.h"
21
21
#include "MemIndex.h"
22
22
#include "clang/Lex/Preprocessor.h"
 
23
#include <memory>
23
24
 
24
25
namespace clang {
25
26
namespace clangd {
57
58
};
58
59
 
59
60
/// This manages symbols from files and an in-memory index on all symbols.
60
 
class FileIndex : public SwapIndex {
 
61
/// FIXME: Expose an interface to remove files that are closed.
 
62
class FileIndex {
61
63
public:
62
64
  /// If URISchemes is empty, the default schemes in SymbolCollector will be
63
65
  /// used.
64
66
  FileIndex(std::vector<std::string> URISchemes = {});
65
67
 
66
 
  /// Update symbols in \p Path with symbols in \p AST. If \p AST is
67
 
  /// nullptr, this removes all symbols in the file.
68
 
  /// If \p AST is not null, \p PP cannot be null and it should be the
69
 
  /// preprocessor that was used to build \p AST.
70
 
  /// If \p TopLevelDecls is set, only these decls are indexed. Otherwise, all
71
 
  /// top level decls obtained from \p AST are indexed.
72
 
  void
73
 
  update(PathRef Path, ASTContext *AST, std::shared_ptr<Preprocessor> PP,
74
 
         llvm::Optional<llvm::ArrayRef<Decl *>> TopLevelDecls = llvm::None);
 
68
  // Presents a merged view of the supplied main-file and preamble ASTs.
 
69
  const SymbolIndex &index() const { return *MergedIndex; }
 
70
 
 
71
  /// Update preamble symbols of file \p Path with all declarations in \p AST
 
72
  /// and macros in \p PP.
 
73
  void updatePreamble(PathRef Path, ASTContext &AST,
 
74
                      std::shared_ptr<Preprocessor> PP);
 
75
 
 
76
  /// Update symbols from main file \p Path with symbols in \p TopLevelDecls.
 
77
  void updateMain(PathRef Path, ParsedAST &AST,
 
78
                  llvm::ArrayRef<Decl *> TopLevelDecls);
75
79
 
76
80
private:
77
 
  // Only update() should swap the index.
78
 
  using SwapIndex::reset;
79
 
 
80
 
  FileSymbols FSymbols;
81
81
  std::vector<std::string> URISchemes;
 
82
 
 
83
  // Contains information from each file's preamble only.
 
84
  // These are large, but update fairly infrequently (preambles are stable).
 
85
  // Missing information:
 
86
  //  - symbol refs (these are always "from the main file")
 
87
  //  - definition locations in the main file
 
88
  //
 
89
  // FIXME: Because the preambles for different TUs have large overlap and
 
90
  // FileIndex doesn't deduplicate, this uses lots of extra RAM.
 
91
  // The biggest obstacle in fixing this: the obvious approach of partitioning
 
92
  // by declaring file (rather than main file) fails if headers provide
 
93
  // different symbols based on preprocessor state.
 
94
  FileSymbols PreambleSymbols;
 
95
  SwapIndex PreambleIndex;
 
96
 
 
97
  // Contains information from each file's main AST.
 
98
  // These are updated frequently (on file change), but are relatively small.
 
99
  // Mostly contains:
 
100
  //  - refs to symbols declared in the preamble and referenced from main
 
101
  //  - symbols declared both in the main file and the preamble
 
102
  // (Note that symbols *only* in the main file are not indexed).
 
103
  FileSymbols MainFileSymbols;
 
104
  SwapIndex MainFileIndex;
 
105
 
 
106
  std::unique_ptr<SymbolIndex> MergedIndex;  // Merge preamble and main index.
82
107
};
83
108
 
84
 
/// Retrieves symbols and refs in \p AST.
 
109
/// Retrieves symbols and refs of \p Decls in \p AST.
85
110
/// Exposed to assist in unit tests.
86
111
/// If URISchemes is empty, the default schemes in SymbolCollector will be used.
87
 
/// If \p TopLevelDecls is set, only these decls are indexed. Otherwise, all top
88
 
/// level decls obtained from \p AST are indexed.
89
112
std::pair<SymbolSlab, RefSlab>
90
 
indexAST(ASTContext &AST, std::shared_ptr<Preprocessor> PP,
91
 
         llvm::Optional<llvm::ArrayRef<Decl *>> TopLevelDecls = llvm::None,
92
 
         llvm::ArrayRef<std::string> URISchemes = {});
 
113
indexMainDecls(ParsedAST &AST, llvm::ArrayRef<Decl *> Decls,
 
114
               llvm::ArrayRef<std::string> URISchemes = {});
 
115
 
 
116
/// Idex declarations from \p AST and macros from \p PP that are declared in
 
117
/// included headers.
 
118
/// If URISchemes is empty, the default schemes in SymbolCollector will be used.
 
119
SymbolSlab indexHeaderSymbols(ASTContext &AST, std::shared_ptr<Preprocessor> PP,
 
120
                              llvm::ArrayRef<std::string> URISchemes = {});
93
121
 
94
122
} // namespace clangd
95
123
} // namespace clang