2018-01-15 20:33:00 +08:00
|
|
|
//===--- Merge.h ------------------------------------------------*- C++-*-===//
|
|
|
|
//
|
|
|
|
// The LLVM Compiler Infrastructure
|
|
|
|
//
|
|
|
|
// This file is distributed under the University of Illinois Open Source
|
|
|
|
// License. See LICENSE.TXT for details.
|
|
|
|
//
|
|
|
|
//===---------------------------------------------------------------------===//
|
|
|
|
#include "Merge.h"
|
|
|
|
#include "llvm/ADT/STLExtras.h"
|
|
|
|
#include "llvm/Support/raw_ostream.h"
|
|
|
|
namespace clang {
|
|
|
|
namespace clangd {
|
|
|
|
namespace {
|
|
|
|
using namespace llvm;
|
|
|
|
|
|
|
|
class MergedIndex : public SymbolIndex {
|
|
|
|
public:
|
|
|
|
MergedIndex(const SymbolIndex *Dynamic, const SymbolIndex *Static)
|
|
|
|
: Dynamic(Dynamic), Static(Static) {}
|
|
|
|
|
|
|
|
// FIXME: Deleted symbols in dirty files are still returned (from Static).
|
|
|
|
// To identify these eliminate these, we should:
|
|
|
|
// - find the generating file from each Symbol which is Static-only
|
|
|
|
// - ask Dynamic if it has that file (needs new SymbolIndex method)
|
|
|
|
// - if so, drop the Symbol.
|
[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
|
|
|
bool fuzzyFind(const FuzzyFindRequest &Req,
|
2018-01-15 20:33:00 +08:00
|
|
|
function_ref<void(const Symbol &)> Callback) const override {
|
|
|
|
// We can't step through both sources in parallel. So:
|
|
|
|
// 1) query all dynamic symbols, slurping results into a slab
|
|
|
|
// 2) query the static symbols, for each one:
|
|
|
|
// a) if it's not in the dynamic slab, yield it directly
|
|
|
|
// b) if it's in the dynamic slab, merge it and yield the result
|
|
|
|
// 3) now yield all the dynamic symbols we haven't processed.
|
|
|
|
bool More = false; // We'll be incomplete if either source was.
|
|
|
|
SymbolSlab::Builder DynB;
|
[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
|
|
|
More |= Dynamic->fuzzyFind(Req, [&](const Symbol &S) { DynB.insert(S); });
|
2018-01-15 20:33:00 +08:00
|
|
|
SymbolSlab Dyn = std::move(DynB).build();
|
|
|
|
|
|
|
|
DenseSet<SymbolID> SeenDynamicSymbols;
|
|
|
|
Symbol::Details Scratch;
|
[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
|
|
|
More |= Static->fuzzyFind(Req, [&](const Symbol &S) {
|
2018-01-15 20:33:00 +08:00
|
|
|
auto DynS = Dyn.find(S.ID);
|
|
|
|
if (DynS == Dyn.end())
|
|
|
|
return Callback(S);
|
|
|
|
SeenDynamicSymbols.insert(S.ID);
|
|
|
|
Callback(mergeSymbol(*DynS, S, &Scratch));
|
|
|
|
});
|
|
|
|
for (const Symbol &S : Dyn)
|
|
|
|
if (!SeenDynamicSymbols.count(S.ID))
|
|
|
|
Callback(S);
|
2018-02-19 21:04:41 +08:00
|
|
|
return More;
|
2018-01-15 20:33:00 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
private:
|
|
|
|
const SymbolIndex *Dynamic, *Static;
|
|
|
|
};
|
|
|
|
}
|
|
|
|
|
|
|
|
Symbol
|
|
|
|
mergeSymbol(const Symbol &L, const Symbol &R, Symbol::Details *Scratch) {
|
|
|
|
assert(L.ID == R.ID);
|
2018-02-09 22:42:01 +08:00
|
|
|
// We prefer information from TUs that saw the definition.
|
|
|
|
// Classes: this is the def itself. Functions: hopefully the header decl.
|
|
|
|
// If both did (or both didn't), continue to prefer L over R.
|
|
|
|
bool PreferR = R.Definition && !L.Definition;
|
|
|
|
Symbol S = PreferR ? R : L; // The target symbol we're merging into.
|
|
|
|
const Symbol &O = PreferR ? L : R; // The "other" less-preferred symbol.
|
|
|
|
|
|
|
|
// For each optional field, fill it from O if missing in S.
|
|
|
|
// (It might be missing in O too, but that's a no-op).
|
|
|
|
if (!S.Definition)
|
|
|
|
S.Definition = O.Definition;
|
|
|
|
if (!S.CanonicalDeclaration)
|
|
|
|
S.CanonicalDeclaration = O.CanonicalDeclaration;
|
2018-03-12 22:49:09 +08:00
|
|
|
S.References += O.References;
|
2018-01-15 20:33:00 +08:00
|
|
|
if (S.CompletionLabel == "")
|
2018-02-09 22:42:01 +08:00
|
|
|
S.CompletionLabel = O.CompletionLabel;
|
2018-01-15 20:33:00 +08:00
|
|
|
if (S.CompletionFilterText == "")
|
2018-02-09 22:42:01 +08:00
|
|
|
S.CompletionFilterText = O.CompletionFilterText;
|
2018-01-15 20:33:00 +08:00
|
|
|
if (S.CompletionPlainInsertText == "")
|
2018-02-09 22:42:01 +08:00
|
|
|
S.CompletionPlainInsertText = O.CompletionPlainInsertText;
|
2018-01-15 20:33:00 +08:00
|
|
|
if (S.CompletionSnippetInsertText == "")
|
2018-02-09 22:42:01 +08:00
|
|
|
S.CompletionSnippetInsertText = O.CompletionSnippetInsertText;
|
2018-01-15 20:33:00 +08:00
|
|
|
|
2018-02-09 22:42:01 +08:00
|
|
|
if (O.Detail) {
|
|
|
|
if (S.Detail) {
|
|
|
|
// Copy into scratch space so we can merge.
|
|
|
|
*Scratch = *S.Detail;
|
|
|
|
if (Scratch->Documentation == "")
|
|
|
|
Scratch->Documentation = O.Detail->Documentation;
|
|
|
|
if (Scratch->CompletionDetail == "")
|
|
|
|
Scratch->CompletionDetail = O.Detail->CompletionDetail;
|
2018-02-16 22:15:55 +08:00
|
|
|
if (Scratch->IncludeHeader == "")
|
|
|
|
Scratch->IncludeHeader = O.Detail->IncludeHeader;
|
2018-02-09 22:42:01 +08:00
|
|
|
S.Detail = Scratch;
|
|
|
|
} else
|
|
|
|
S.Detail = O.Detail;
|
|
|
|
}
|
2018-01-15 20:33:00 +08:00
|
|
|
return S;
|
|
|
|
}
|
|
|
|
|
|
|
|
std::unique_ptr<SymbolIndex> mergeIndex(const SymbolIndex *Dynamic,
|
|
|
|
const SymbolIndex *Static) {
|
|
|
|
return llvm::make_unique<MergedIndex>(Dynamic, Static);
|
|
|
|
}
|
|
|
|
} // namespace clangd
|
|
|
|
} // namespace clang
|