clang-tools  9.0.0
FileIndex.h
Go to the documentation of this file.
1 //===--- FileIndex.h - Index for files. ---------------------------- 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 // FileIndex implements SymbolIndex for symbols from a set of files. Symbols are
10 // maintained at source-file granuality (e.g. with ASTs), and files can be
11 // updated dynamically.
12 //
13 //===----------------------------------------------------------------------===//
14 
15 #ifndef LLVM_CLANG_TOOLS_EXTRA_CLANGD_INDEX_FILEINDEX_H
16 #define LLVM_CLANG_TOOLS_EXTRA_CLANGD_INDEX_FILEINDEX_H
17 
18 #include "ClangdUnit.h"
19 #include "Index.h"
20 #include "MemIndex.h"
21 #include "Merge.h"
23 #include "index/Symbol.h"
24 #include "clang/Lex/Preprocessor.h"
25 #include <memory>
26 
27 namespace clang {
28 namespace clangd {
29 
30 /// Select between in-memory index implementations, which have tradeoffs.
31 enum class IndexType {
32  // MemIndex is trivially cheap to build, but has poor query performance.
33  Light,
34  // Dex is relatively expensive to build and uses more memory, but is fast.
35  Heavy,
36 };
37 
38 /// How to handle duplicated symbols across multiple files.
39 enum class DuplicateHandling {
40  // Pick a random symbol. Less accurate but faster.
41  PickOne,
42  // Merge symbols. More accurate but slower.
43  Merge,
44 };
45 
46 /// A container of Symbols from several source files. It can be updated
47 /// at source-file granularity, replacing all symbols from one file with a new
48 /// set.
49 ///
50 /// This implements a snapshot semantics for symbols in a file. Each update to a
51 /// file will create a new snapshot for all symbols in the file. Snapshots are
52 /// managed with shared pointers that are shared between this class and the
53 /// users. For each file, this class only stores a pointer pointing to the
54 /// newest snapshot, and an outdated snapshot is deleted by the last owner of
55 /// the snapshot, either this class or the symbol index.
56 ///
57 /// The snapshot semantics keeps critical sections minimal since we only need
58 /// locking when we swap or obtain references to snapshots.
59 class FileSymbols {
60 public:
61  /// Updates all symbols and refs in a file.
62  /// If either is nullptr, corresponding data for \p Path will be removed.
63  /// If CountReferences is true, \p Refs will be used for counting References
64  /// during merging.
65  void update(PathRef Path, std::unique_ptr<SymbolSlab> Slab,
66  std::unique_ptr<RefSlab> Refs,
67  std::unique_ptr<RelationSlab> Relations, bool CountReferences);
68 
69  /// The index keeps the symbols alive.
70  /// Will count Symbol::References based on number of references in the main
71  /// files, while building the index with DuplicateHandling::Merge option.
72  std::unique_ptr<SymbolIndex>
73  buildIndex(IndexType,
75 
76 private:
77  struct RefSlabAndCountReferences {
78  std::shared_ptr<RefSlab> Slab;
79  bool CountReferences = false;
80  };
81  mutable std::mutex Mutex;
82 
83  /// Stores the latest symbol snapshots for all active files.
84  llvm::StringMap<std::shared_ptr<SymbolSlab>> FileToSymbols;
85  /// Stores the latest ref snapshots for all active files.
86  llvm::StringMap<RefSlabAndCountReferences> FileToRefs;
87  /// Stores the latest relation snapshots for all active files.
88  llvm::StringMap<std::shared_ptr<RelationSlab>> FileToRelations;
89 };
90 
91 /// This manages symbols from files and an in-memory index on all symbols.
92 /// FIXME: Expose an interface to remove files that are closed.
93 class FileIndex : public MergedIndex {
94 public:
95  FileIndex(bool UseDex = true);
96 
97  /// Update preamble symbols of file \p Path with all declarations in \p AST
98  /// and macros in \p PP.
99  void updatePreamble(PathRef Path, ASTContext &AST,
100  std::shared_ptr<Preprocessor> PP,
101  const CanonicalIncludes &Includes);
102 
103  /// Update symbols and references from main file \p Path with
104  /// `indexMainDecls`.
105  void updateMain(PathRef Path, ParsedAST &AST);
106 
107 private:
108  bool UseDex; // FIXME: this should be always on.
109 
110  // Contains information from each file's preamble only.
111  // These are large, but update fairly infrequently (preambles are stable).
112  // Missing information:
113  // - symbol refs (these are always "from the main file")
114  // - definition locations in the main file
115  //
116  // FIXME: Because the preambles for different TUs have large overlap and
117  // FileIndex doesn't deduplicate, this uses lots of extra RAM.
118  // The biggest obstacle in fixing this: the obvious approach of partitioning
119  // by declaring file (rather than main file) fails if headers provide
120  // different symbols based on preprocessor state.
121  FileSymbols PreambleSymbols;
122  SwapIndex PreambleIndex;
123 
124  // Contains information from each file's main AST.
125  // These are updated frequently (on file change), but are relatively small.
126  // Mostly contains:
127  // - refs to symbols declared in the preamble and referenced from main
128  // - symbols declared both in the main file and the preamble
129  // (Note that symbols *only* in the main file are not indexed).
130  FileSymbols MainFileSymbols;
131  SwapIndex MainFileIndex;
132 };
133 
134 using SlabTuple = std::tuple<SymbolSlab, RefSlab, RelationSlab>;
135 
136 /// Retrieves symbols and refs of local top level decls in \p AST (i.e.
137 /// `AST.getLocalTopLevelDecls()`).
138 /// Exposed to assist in unit tests.
140 
141 /// Idex declarations from \p AST and macros from \p PP that are declared in
142 /// included headers.
143 SlabTuple indexHeaderSymbols(ASTContext &AST, std::shared_ptr<Preprocessor> PP,
144  const CanonicalIncludes &Includes);
145 
146 } // namespace clangd
147 } // namespace clang
148 
149 #endif // LLVM_CLANG_TOOLS_EXTRA_CLANGD_INDEX_FILEINDEX_H
std::tuple< SymbolSlab, RefSlab, RelationSlab > SlabTuple
Definition: FileIndex.h:134
IndexType
Select between in-memory index implementations, which have tradeoffs.
Definition: FileIndex.h:31
A container of Symbols from several source files.
Definition: FileIndex.h:59
This manages symbols from files and an in-memory index on all symbols.
Definition: FileIndex.h:93
SlabTuple indexMainDecls(ParsedAST &AST)
Retrieves symbols and refs of local top level decls in AST (i.e.
Definition: FileIndex.cpp:79
llvm::StringRef PathRef
A typedef to represent a ref to file path.
Definition: Path.h:23
DuplicateHandling
How to handle duplicated symbols across multiple files.
Definition: FileIndex.h:39
Maps a definition location onto an #include file, based on a set of filename rules.
std::string Path
A typedef to represent a file path.
Definition: Path.h:20
RelationSlab Relations
Stores and provides access to parsed AST.
Definition: ClangdUnit.h:73
===– Representation.cpp - ClangDoc Representation --------—*- C++ -*-===//
RefSlab Refs
SlabTuple indexHeaderSymbols(ASTContext &AST, std::shared_ptr< Preprocessor > PP, const CanonicalIncludes &Includes)
Idex declarations from AST and macros from PP that are declared in included headers.
Definition: FileIndex.cpp:85