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) {
|
2018-01-10 22:57:58 +08:00
|
|
|
SymbolCollector::Options CollectorOpts;
|
[clangd] Not collect include headers for dynamic index for now.
Summary:
The new behaviors introduced by this patch:
o When include collection is enabled, we always set IncludeHeader field in Symbol
even if it's the same as FileURI in decl.
o Disable include collection in FileIndex which is currently only used to build
dynamic index. We should revisit when we actually want to use FileIndex to global
index.
o Code-completion only uses IncludeHeader to insert headers but not FileURI in
CanonicalDeclaration. This ensures that inserted headers are always canonicalized.
Note that include insertion can still be triggered for symbols that are already
included if they are merged from dynamic index and static index, but we would
only use includes that are already canonicalized (e.g. from static index).
Reason for change:
Collecting header includes in dynamic index enables inserting includes for headers
that are not indexed but opened in the editor. Comparing to inserting includes for
symbols in global/static index, this is nice-to-have but would probably require
non-trivial amount of work to get right. For example:
o Currently it's not easy to fully support CanonicalIncludes in dynamic index, given the way
we run dynamic index.
o It's also harder to reason about the correctness of include canonicalization for dynamic index
(i.e. symbols in the current file/TU) than static index where symbols are collected
offline and sanity check is possible before shipping to production.
o We have less control/flexibility over symbol info in the dynamic index
(e.g. URIs, path normalization), which could be used to help make decision when inserting includes.
As header collection (especially canonicalization) is relatively new, and enabling
it for dynamic index would immediately affect current users with only dynamic
index support, I propose we disable it for dynamic index for now to avoid
compromising other hot features like code completion and only support it for
static index where include insertion would likely to bring more value.
Reviewers: ilya-biryukov, sammccall, hokein
Subscribers: klimek, jkorous-apple, cfe-commits
Differential Revision: https://reviews.llvm.org/D43550
llvm-svn: 325764
2018-02-22 18:14:05 +08:00
|
|
|
// FIXME(ioeric): we might also want to collect include headers. We would need
|
|
|
|
// to make sure all includes are canonicalized (with CanonicalIncludes), which
|
|
|
|
// is not trivial given the current way of collecting symbols: we only have
|
|
|
|
// AST at this point, but we also need preprocessor callbacks (e.g.
|
|
|
|
// CommentHandler for IWYU pragma) to canonicalize includes.
|
|
|
|
CollectorOpts.CollectIncludePath = false;
|
2018-03-12 22:49:09 +08:00
|
|
|
CollectorOpts.CountReferences = false;
|
2018-02-20 02:48:44 +08:00
|
|
|
|
2018-01-10 22:57:58 +08:00
|
|
|
auto Collector = std::make_shared<SymbolCollector>(std::move(CollectorOpts));
|
2018-01-10 01:32:00 +08:00
|
|
|
Collector->setPreprocessor(std::move(PP));
|
2017-12-15 20:25:02 +08:00
|
|
|
index::IndexingOptions IndexOpts;
|
2018-03-12 22:49:09 +08:00
|
|
|
// We only need declarations, because we don't count references.
|
2017-12-15 20:25:02 +08:00
|
|
|
IndexOpts.SystemSymbolFilter =
|
2018-03-12 22:49:09 +08:00
|
|
|
index::IndexingOptions::SystemSymbolFilterKind::DeclarationsOnly;
|
2017-12-15 20:25:02 +08:00
|
|
|
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};
|
|
|
|
}
|
|
|
|
|
[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
|
|
|
void FileIndex::update(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(
|
[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
|
|
|
const FuzzyFindRequest &Req,
|
2017-12-28 22:47:01 +08:00
|
|
|
llvm::function_ref<void(const Symbol &)> Callback) const {
|
[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
|
|
|
return Index.fuzzyFind(Req, Callback);
|
2017-12-15 20:25:02 +08:00
|
|
|
}
|
|
|
|
|
2017-12-14 22:50:58 +08:00
|
|
|
} // namespace clangd
|
|
|
|
} // namespace clang
|