2017-12-15 20:25:02 +08:00
|
|
|
//===--- FileIndex.h - Index for files. ---------------------------- C++-*-===//
|
2017-12-14 22:50:58 +08:00
|
|
|
//
|
|
|
|
// The LLVM Compiler Infrastructure
|
|
|
|
//
|
|
|
|
// This file is distributed under the University of Illinois Open Source
|
|
|
|
// License. See LICENSE.TXT for details.
|
|
|
|
//
|
|
|
|
//===----------------------------------------------------------------------===//
|
2017-12-15 20:25:02 +08:00
|
|
|
//
|
|
|
|
// FileIndex implements SymbolIndex for symbols from a set of files. Symbols are
|
|
|
|
// maintained at source-file granuality (e.g. with ASTs), and files can be
|
|
|
|
// updated dynamically.
|
|
|
|
//
|
|
|
|
//===---------------------------------------------------------------------===//
|
2017-12-14 22:50:58 +08:00
|
|
|
|
2017-12-15 20:25:02 +08:00
|
|
|
#ifndef LLVM_CLANG_TOOLS_EXTRA_CLANGD_INDEX_FILEINDEX_H
|
|
|
|
#define LLVM_CLANG_TOOLS_EXTRA_CLANGD_INDEX_FILEINDEX_H
|
2017-12-14 22:50:58 +08:00
|
|
|
|
2017-12-15 20:25:02 +08:00
|
|
|
#include "../ClangdUnit.h"
|
2017-12-14 22:50:58 +08:00
|
|
|
#include "Index.h"
|
2017-12-15 20:25:02 +08:00
|
|
|
#include "MemIndex.h"
|
2018-05-24 23:50:15 +08:00
|
|
|
#include "clang/Lex/Preprocessor.h"
|
2017-12-14 22:50:58 +08:00
|
|
|
|
|
|
|
namespace clang {
|
|
|
|
namespace clangd {
|
|
|
|
|
|
|
|
/// \brief A container of Symbols from several source files. It can be updated
|
|
|
|
/// at source-file granularity, replacing all symbols from one file with a new
|
|
|
|
/// set.
|
|
|
|
///
|
|
|
|
/// This implements a snapshot semantics for symbols in a file. Each update to a
|
|
|
|
/// file will create a new snapshot for all symbols in the file. Snapshots are
|
|
|
|
/// managed with shared pointers that are shared between this class and the
|
|
|
|
/// users. For each file, this class only stores a pointer pointing to the
|
|
|
|
/// newest snapshot, and an outdated snapshot is deleted by the last owner of
|
|
|
|
/// the snapshot, either this class or the symbol index.
|
|
|
|
///
|
|
|
|
/// The snapshot semantics keeps critical sections minimal since we only need
|
|
|
|
/// locking when we swap or obtain refereces to snapshots.
|
|
|
|
class FileSymbols {
|
|
|
|
public:
|
|
|
|
/// \brief Updates all symbols in a file. If \p Slab is nullptr, symbols for
|
|
|
|
/// \p Path will be removed.
|
|
|
|
void update(PathRef Path, std::unique_ptr<SymbolSlab> Slab);
|
|
|
|
|
|
|
|
// The shared_ptr keeps the symbols alive
|
|
|
|
std::shared_ptr<std::vector<const Symbol *>> allSymbols();
|
|
|
|
|
|
|
|
private:
|
|
|
|
mutable std::mutex Mutex;
|
|
|
|
|
|
|
|
/// \brief Stores the latest snapshots for all active files.
|
|
|
|
llvm::StringMap<std::shared_ptr<SymbolSlab>> FileToSlabs;
|
|
|
|
};
|
|
|
|
|
2017-12-15 20:25:02 +08:00
|
|
|
/// \brief This manages symbls from files and an in-memory index on all symbols.
|
|
|
|
class FileIndex : public SymbolIndex {
|
|
|
|
public:
|
2018-06-15 16:55:00 +08:00
|
|
|
/// If URISchemes is empty, the default schemes in SymbolCollector will be
|
|
|
|
/// used.
|
|
|
|
FileIndex(std::vector<std::string> URISchemes = {});
|
|
|
|
|
2017-12-15 20:25:02 +08:00
|
|
|
/// \brief Update symbols in \p Path with symbols in \p AST. If \p AST is
|
2018-05-24 23:50:15 +08:00
|
|
|
/// nullptr, this removes all symbols in the file.
|
|
|
|
/// If \p AST is not null, \p PP cannot be null and it should be the
|
|
|
|
/// preprocessor that was used to build \p AST.
|
|
|
|
void update(PathRef Path, ASTContext *AST, std::shared_ptr<Preprocessor> PP);
|
2017-12-15 20:25:02 +08:00
|
|
|
|
2017-12-28 22:47:01 +08:00
|
|
|
bool
|
[clangd] Pass Context implicitly using TLS.
Summary:
Instead of passing Context explicitly around, we now have a thread-local
Context object `Context::current()` which is an implicit argument to
every function.
Most manipulation of this should use the WithContextValue helper, which
augments the current Context to add a single KV pair, and restores the
old context on destruction.
Advantages are:
- less boilerplate in functions that just propagate contexts
- reading most code doesn't require understanding context at all, and
using context as values in fewer places still
- fewer options to pass the "wrong" context when it changes within a
scope (e.g. when using Span)
- contexts pass through interfaces we can't modify, such as VFS
- propagating contexts across threads was slightly tricky (e.g.
copy vs move, no move-init in lambdas), and is now encapsulated in
the threadpool
Disadvantages are all the usual TLS stuff - hidden magic, and
potential for higher memory usage on threads that don't use the
context. (In practice, it's just one pointer)
Reviewers: ilya-biryukov
Subscribers: klimek, jkorous-apple, ioeric, cfe-commits
Differential Revision: https://reviews.llvm.org/D42517
llvm-svn: 323872
2018-01-31 21:40:48 +08:00
|
|
|
fuzzyFind(const FuzzyFindRequest &Req,
|
2017-12-28 22:47:01 +08:00
|
|
|
llvm::function_ref<void(const Symbol &)> Callback) const override;
|
2017-12-15 20:25:02 +08:00
|
|
|
|
2018-03-14 17:48:05 +08:00
|
|
|
void lookup(const LookupRequest &Req,
|
|
|
|
llvm::function_ref<void(const Symbol &)> Callback) const override;
|
|
|
|
|
2017-12-15 20:25:02 +08:00
|
|
|
private:
|
|
|
|
FileSymbols FSymbols;
|
|
|
|
MemIndex Index;
|
2018-06-15 16:55:00 +08:00
|
|
|
std::vector<std::string> URISchemes;
|
2017-12-15 20:25:02 +08:00
|
|
|
};
|
|
|
|
|
2018-04-30 23:24:17 +08:00
|
|
|
/// Retrieves namespace and class level symbols in \p AST.
|
|
|
|
/// Exposed to assist in unit tests.
|
2018-06-15 16:55:00 +08:00
|
|
|
/// If URISchemes is empty, the default schemes in SymbolCollector will be used.
|
|
|
|
SymbolSlab indexAST(ASTContext &AST, std::shared_ptr<Preprocessor> PP,
|
|
|
|
llvm::ArrayRef<std::string> URISchemes = {});
|
2018-04-30 23:24:17 +08:00
|
|
|
|
2017-12-14 22:50:58 +08:00
|
|
|
} // namespace clangd
|
|
|
|
} // namespace clang
|
|
|
|
|
2017-12-15 20:25:02 +08:00
|
|
|
#endif // LLVM_CLANG_TOOLS_EXTRA_CLANGD_INDEX_FILEINDEX_H
|