clang-tools  7.0.0
FileIndex.h
Go to the documentation of this file.
1 //===--- FileIndex.h - Index for files. ---------------------------- C++-*-===//
2 //
3 // The LLVM Compiler Infrastructure
4 //
5 // This file is distributed under the University of Illinois Open Source
6 // License. See LICENSE.TXT for details.
7 //
8 //===----------------------------------------------------------------------===//
9 //
10 // FileIndex implements SymbolIndex for symbols from a set of files. Symbols are
11 // maintained at source-file granuality (e.g. with ASTs), and files can be
12 // updated dynamically.
13 //
14 //===---------------------------------------------------------------------===//
15 
16 #ifndef LLVM_CLANG_TOOLS_EXTRA_CLANGD_INDEX_FILEINDEX_H
17 #define LLVM_CLANG_TOOLS_EXTRA_CLANGD_INDEX_FILEINDEX_H
18 
19 #include "../ClangdUnit.h"
20 #include "Index.h"
21 #include "MemIndex.h"
22 #include "clang/Lex/Preprocessor.h"
23 
24 namespace clang {
25 namespace clangd {
26 
27 /// \brief A container of Symbols from several source files. It can be updated
28 /// at source-file granularity, replacing all symbols from one file with a new
29 /// set.
30 ///
31 /// This implements a snapshot semantics for symbols in a file. Each update to a
32 /// file will create a new snapshot for all symbols in the file. Snapshots are
33 /// managed with shared pointers that are shared between this class and the
34 /// users. For each file, this class only stores a pointer pointing to the
35 /// newest snapshot, and an outdated snapshot is deleted by the last owner of
36 /// the snapshot, either this class or the symbol index.
37 ///
38 /// The snapshot semantics keeps critical sections minimal since we only need
39 /// locking when we swap or obtain refereces to snapshots.
40 class FileSymbols {
41 public:
42  /// \brief Updates all symbols in a file. If \p Slab is nullptr, symbols for
43  /// \p Path will be removed.
44  void update(PathRef Path, std::unique_ptr<SymbolSlab> Slab);
45 
46  // The shared_ptr keeps the symbols alive
47  std::shared_ptr<std::vector<const Symbol *>> allSymbols();
48 
49 private:
50  mutable std::mutex Mutex;
51 
52  /// \brief Stores the latest snapshots for all active files.
53  llvm::StringMap<std::shared_ptr<SymbolSlab>> FileToSlabs;
54 };
55 
56 /// \brief This manages symbls from files and an in-memory index on all symbols.
57 class FileIndex : public SymbolIndex {
58 public:
59  /// If URISchemes is empty, the default schemes in SymbolCollector will be
60  /// used.
61  FileIndex(std::vector<std::string> URISchemes = {});
62 
63  /// \brief Update symbols in \p Path with symbols in \p AST. If \p AST is
64  /// nullptr, this removes all symbols in the file.
65  /// If \p AST is not null, \p PP cannot be null and it should be the
66  /// preprocessor that was used to build \p AST.
67  void update(PathRef Path, ASTContext *AST, std::shared_ptr<Preprocessor> PP);
68 
69  bool
70  fuzzyFind(const FuzzyFindRequest &Req,
71  llvm::function_ref<void(const Symbol &)> Callback) const override;
72 
73  void lookup(const LookupRequest &Req,
74  llvm::function_ref<void(const Symbol &)> Callback) const override;
75 
76 private:
77  FileSymbols FSymbols;
78  MemIndex Index;
79  std::vector<std::string> URISchemes;
80 };
81 
82 /// Retrieves namespace and class level symbols in \p AST.
83 /// Exposed to assist in unit tests.
84 /// If URISchemes is empty, the default schemes in SymbolCollector will be used.
85 SymbolSlab indexAST(ASTContext &AST, std::shared_ptr<Preprocessor> PP,
86  llvm::ArrayRef<std::string> URISchemes = {});
87 
88 } // namespace clangd
89 } // namespace clang
90 
91 #endif // LLVM_CLANG_TOOLS_EXTRA_CLANGD_INDEX_FILEINDEX_H
SymbolSlab indexAST(ASTContext &AST, std::shared_ptr< Preprocessor > PP, llvm::ArrayRef< std::string > URISchemes)
Retrieves namespace and class level symbols in AST.
Definition: FileIndex.cpp:18
A container of Symbols from several source files.
Definition: FileIndex.h:40
std::shared_ptr< std::vector< const Symbol * > > allSymbols()
Definition: FileIndex.cpp:59
Interface for symbol indexes that can be used for searching or matching symbols among a set of symbol...
Definition: Index.h:317
This manages symbls from files and an in-memory index on all symbols.
Definition: FileIndex.h:57
void update(PathRef Path, std::unique_ptr< SymbolSlab > Slab)
Updates all symbols in a file.
Definition: FileIndex.cpp:51
llvm::StringRef PathRef
A typedef to represent a ref to file path.
Definition: Path.h:24
llvm::unique_function< void(llvm::Expected< T >)> Callback
A Callback<T> is a void function that accepts Expected<T>.
Definition: Function.h:28
This implements an index for a (relatively small) set of symbols that can be easily managed in memory...
Definition: MemIndex.h:21
std::string Path
A typedef to represent a file path.
Definition: Path.h:21
===– Representation.cpp - ClangDoc Representation --------—*- C++ -*-===//