2017-12-15 20:25:02 +08:00
|
|
|
//===--- FileIndex.cpp - Indexes 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
|
|
|
#include "FileIndex.h"
|
|
|
|
#include "SymbolCollector.h"
|
2017-12-14 22:50:58 +08:00
|
|
|
#include "clang/Index/IndexingAction.h"
|
|
|
|
|
|
|
|
namespace clang {
|
|
|
|
namespace clangd {
|
2017-12-15 20:25:02 +08:00
|
|
|
namespace {
|
|
|
|
|
|
|
|
/// Retrieves namespace and class level symbols in \p Decls.
|
|
|
|
std::unique_ptr<SymbolSlab> indexAST(ASTContext &Ctx,
|
2018-01-10 01:32:00 +08:00
|
|
|
std::shared_ptr<Preprocessor> PP,
|
2017-12-15 20:25:02 +08:00
|
|
|
llvm::ArrayRef<const Decl *> Decls) {
|
|
|
|
auto Collector = std::make_shared<SymbolCollector>();
|
2018-01-10 01:32:00 +08:00
|
|
|
Collector->setPreprocessor(std::move(PP));
|
2017-12-15 20:25:02 +08:00
|
|
|
index::IndexingOptions IndexOpts;
|
|
|
|
IndexOpts.SystemSymbolFilter =
|
|
|
|
index::IndexingOptions::SystemSymbolFilterKind::All;
|
|
|
|
IndexOpts.IndexFunctionLocals = false;
|
|
|
|
|
|
|
|
index::indexTopLevelDecls(Ctx, Decls, Collector, IndexOpts);
|
|
|
|
auto Symbols = llvm::make_unique<SymbolSlab>();
|
|
|
|
*Symbols = Collector->takeSymbols();
|
|
|
|
return Symbols;
|
|
|
|
}
|
|
|
|
|
|
|
|
} // namespace
|
2017-12-14 22:50:58 +08:00
|
|
|
|
|
|
|
void FileSymbols::update(PathRef Path, std::unique_ptr<SymbolSlab> Slab) {
|
|
|
|
std::lock_guard<std::mutex> Lock(Mutex);
|
|
|
|
if (!Slab)
|
|
|
|
FileToSlabs.erase(Path);
|
|
|
|
else
|
2017-12-28 22:47:01 +08:00
|
|
|
FileToSlabs[Path] = std::move(Slab);
|
2017-12-14 22:50:58 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
std::shared_ptr<std::vector<const Symbol *>> FileSymbols::allSymbols() {
|
|
|
|
// The snapshot manages life time of symbol slabs and provides pointers of all
|
|
|
|
// symbols in all slabs.
|
|
|
|
struct Snapshot {
|
|
|
|
std::vector<const Symbol *> Pointers;
|
|
|
|
std::vector<std::shared_ptr<SymbolSlab>> KeepAlive;
|
|
|
|
};
|
|
|
|
auto Snap = std::make_shared<Snapshot>();
|
|
|
|
{
|
|
|
|
std::lock_guard<std::mutex> Lock(Mutex);
|
|
|
|
|
|
|
|
for (const auto &FileAndSlab : FileToSlabs) {
|
|
|
|
Snap->KeepAlive.push_back(FileAndSlab.second);
|
|
|
|
for (const auto &Iter : *FileAndSlab.second)
|
2017-12-24 03:38:03 +08:00
|
|
|
Snap->Pointers.push_back(&Iter);
|
2017-12-14 22:50:58 +08:00
|
|
|
}
|
|
|
|
}
|
|
|
|
auto *Pointers = &Snap->Pointers;
|
|
|
|
// Use aliasing constructor to keep the snapshot alive along with the
|
|
|
|
// pointers.
|
|
|
|
return {std::move(Snap), Pointers};
|
|
|
|
}
|
|
|
|
|
2017-12-20 00:50:37 +08:00
|
|
|
void FileIndex::update(const Context &Ctx, PathRef Path, ParsedAST *AST) {
|
2017-12-15 20:25:02 +08:00
|
|
|
if (!AST) {
|
|
|
|
FSymbols.update(Path, nullptr);
|
|
|
|
} else {
|
2018-01-10 01:32:00 +08:00
|
|
|
auto Slab = indexAST(AST->getASTContext(), AST->getPreprocessorPtr(),
|
|
|
|
AST->getTopLevelDecls());
|
2017-12-15 20:25:02 +08:00
|
|
|
FSymbols.update(Path, std::move(Slab));
|
|
|
|
}
|
|
|
|
auto Symbols = FSymbols.allSymbols();
|
|
|
|
Index.build(std::move(Symbols));
|
|
|
|
}
|
|
|
|
|
2017-12-28 22:47:01 +08:00
|
|
|
bool FileIndex::fuzzyFind(
|
|
|
|
const Context &Ctx, const FuzzyFindRequest &Req,
|
|
|
|
llvm::function_ref<void(const Symbol &)> Callback) const {
|
|
|
|
return Index.fuzzyFind(Ctx, Req, Callback);
|
2017-12-15 20:25:02 +08:00
|
|
|
}
|
|
|
|
|
2017-12-14 22:50:58 +08:00
|
|
|
} // namespace clangd
|
|
|
|
} // namespace clang
|