[clangd] Introduce a "Symbol" class.
Summary:
* The "Symbol" class represents a C++ symbol in the codebase, containing all the
information of a C++ symbol needed by clangd. clangd will use it in clangd's
AST/dynamic index and global/static index (code completion and code
navigation).
* The SymbolCollector (another IndexAction) will be used to recollect the
symbols when the source file is changed (for ASTIndex), or to generate
all C++ symbols for the whole project.
In the long term (when index-while-building is ready), clangd should share a
same "Symbol" structure and IndexAction with index-while-building, but
for now we want to have some stuff working in clangd.
Reviewers: ioeric, sammccall, ilya-biryukov, malaperle
Reviewed By: sammccall
Subscribers: malaperle, klimek, mgorny, cfe-commits
Differential Revision: https://reviews.llvm.org/D40897
llvm-svn: 320486
2017-12-12 23:42:10 +08:00
|
|
|
//===--- Index.cpp -----------------------------------------------*- C++-*-===//
|
|
|
|
//
|
|
|
|
// The LLVM Compiler Infrastructure
|
|
|
|
//
|
|
|
|
// This file is distributed under the University of Illinois Open Source
|
|
|
|
// License. See LICENSE.TXT for details.
|
|
|
|
//
|
|
|
|
//===----------------------------------------------------------------------===//
|
|
|
|
|
|
|
|
#include "Index.h"
|
2017-12-14 20:17:14 +08:00
|
|
|
#include "llvm/ADT/StringExtras.h"
|
2017-12-15 05:22:03 +08:00
|
|
|
#include "llvm/Support/SHA1.h"
|
2018-02-09 22:42:01 +08:00
|
|
|
#include "llvm/Support/raw_ostream.h"
|
[clangd] Introduce a "Symbol" class.
Summary:
* The "Symbol" class represents a C++ symbol in the codebase, containing all the
information of a C++ symbol needed by clangd. clangd will use it in clangd's
AST/dynamic index and global/static index (code completion and code
navigation).
* The SymbolCollector (another IndexAction) will be used to recollect the
symbols when the source file is changed (for ASTIndex), or to generate
all C++ symbols for the whole project.
In the long term (when index-while-building is ready), clangd should share a
same "Symbol" structure and IndexAction with index-while-building, but
for now we want to have some stuff working in clangd.
Reviewers: ioeric, sammccall, ilya-biryukov, malaperle
Reviewed By: sammccall
Subscribers: malaperle, klimek, mgorny, cfe-commits
Differential Revision: https://reviews.llvm.org/D40897
llvm-svn: 320486
2017-12-12 23:42:10 +08:00
|
|
|
|
|
|
|
namespace clang {
|
|
|
|
namespace clangd {
|
2017-12-24 03:38:03 +08:00
|
|
|
using namespace llvm;
|
[clangd] Introduce a "Symbol" class.
Summary:
* The "Symbol" class represents a C++ symbol in the codebase, containing all the
information of a C++ symbol needed by clangd. clangd will use it in clangd's
AST/dynamic index and global/static index (code completion and code
navigation).
* The SymbolCollector (another IndexAction) will be used to recollect the
symbols when the source file is changed (for ASTIndex), or to generate
all C++ symbols for the whole project.
In the long term (when index-while-building is ready), clangd should share a
same "Symbol" structure and IndexAction with index-while-building, but
for now we want to have some stuff working in clangd.
Reviewers: ioeric, sammccall, ilya-biryukov, malaperle
Reviewed By: sammccall
Subscribers: malaperle, klimek, mgorny, cfe-commits
Differential Revision: https://reviews.llvm.org/D40897
llvm-svn: 320486
2017-12-12 23:42:10 +08:00
|
|
|
|
2018-02-09 22:42:01 +08:00
|
|
|
raw_ostream &operator<<(raw_ostream &OS, const SymbolLocation &L) {
|
|
|
|
if (!L)
|
|
|
|
return OS << "(none)";
|
2018-04-13 16:30:39 +08:00
|
|
|
return OS << L.FileURI << "[" << L.Start.Line << ":" << L.Start.Column << "-"
|
|
|
|
<< L.End.Line << ":" << L.End.Column << ")";
|
2018-02-09 22:42:01 +08:00
|
|
|
}
|
|
|
|
|
2017-12-24 03:38:03 +08:00
|
|
|
SymbolID::SymbolID(StringRef USR)
|
|
|
|
: HashValue(SHA1::hash(arrayRefFromStringRef(USR))) {}
|
2017-12-14 20:17:14 +08:00
|
|
|
|
2017-12-24 03:38:03 +08:00
|
|
|
raw_ostream &operator<<(raw_ostream &OS, const SymbolID &ID) {
|
|
|
|
OS << toHex(toStringRef(ID.HashValue));
|
2017-12-14 20:17:14 +08:00
|
|
|
return OS;
|
[clangd] Introduce a "Symbol" class.
Summary:
* The "Symbol" class represents a C++ symbol in the codebase, containing all the
information of a C++ symbol needed by clangd. clangd will use it in clangd's
AST/dynamic index and global/static index (code completion and code
navigation).
* The SymbolCollector (another IndexAction) will be used to recollect the
symbols when the source file is changed (for ASTIndex), or to generate
all C++ symbols for the whole project.
In the long term (when index-while-building is ready), clangd should share a
same "Symbol" structure and IndexAction with index-while-building, but
for now we want to have some stuff working in clangd.
Reviewers: ioeric, sammccall, ilya-biryukov, malaperle
Reviewed By: sammccall
Subscribers: malaperle, klimek, mgorny, cfe-commits
Differential Revision: https://reviews.llvm.org/D40897
llvm-svn: 320486
2017-12-12 23:42:10 +08:00
|
|
|
}
|
|
|
|
|
2018-04-25 23:27:09 +08:00
|
|
|
std::string SymbolID::str() const {
|
|
|
|
std::string ID;
|
|
|
|
llvm::raw_string_ostream OS(ID);
|
|
|
|
OS << *this;
|
|
|
|
return OS.str();
|
|
|
|
}
|
|
|
|
|
2017-12-24 03:38:03 +08:00
|
|
|
void operator>>(StringRef Str, SymbolID &ID) {
|
2017-12-14 20:17:14 +08:00
|
|
|
std::string HexString = fromHex(Str);
|
2017-12-22 04:11:46 +08:00
|
|
|
assert(HexString.size() == ID.HashValue.size());
|
2017-12-14 20:17:14 +08:00
|
|
|
std::copy(HexString.begin(), HexString.end(), ID.HashValue.begin());
|
|
|
|
}
|
[clangd] Introduce a "Symbol" class.
Summary:
* The "Symbol" class represents a C++ symbol in the codebase, containing all the
information of a C++ symbol needed by clangd. clangd will use it in clangd's
AST/dynamic index and global/static index (code completion and code
navigation).
* The SymbolCollector (another IndexAction) will be used to recollect the
symbols when the source file is changed (for ASTIndex), or to generate
all C++ symbols for the whole project.
In the long term (when index-while-building is ready), clangd should share a
same "Symbol" structure and IndexAction with index-while-building, but
for now we want to have some stuff working in clangd.
Reviewers: ioeric, sammccall, ilya-biryukov, malaperle
Reviewed By: sammccall
Subscribers: malaperle, klimek, mgorny, cfe-commits
Differential Revision: https://reviews.llvm.org/D40897
llvm-svn: 320486
2017-12-12 23:42:10 +08:00
|
|
|
|
2018-07-05 14:20:41 +08:00
|
|
|
raw_ostream &operator<<(raw_ostream &OS, SymbolOrigin O) {
|
|
|
|
if (O == SymbolOrigin::Unknown)
|
|
|
|
return OS << "unknown";
|
|
|
|
constexpr static char Sigils[] = "ADSM4567";
|
|
|
|
for (unsigned I = 0; I < sizeof(Sigils); ++I)
|
2018-07-06 19:50:49 +08:00
|
|
|
if (static_cast<uint8_t>(O) & 1u << I)
|
2018-07-05 14:20:41 +08:00
|
|
|
OS << Sigils[I];
|
|
|
|
return OS;
|
|
|
|
}
|
|
|
|
|
2018-02-09 22:42:01 +08:00
|
|
|
raw_ostream &operator<<(raw_ostream &OS, const Symbol &S) {
|
|
|
|
return OS << S.Scope << S.Name;
|
|
|
|
}
|
|
|
|
|
2018-05-03 22:53:02 +08:00
|
|
|
double quality(const Symbol &S) {
|
|
|
|
// This avoids a sharp gradient for tail symbols, and also neatly avoids the
|
|
|
|
// question of whether 0 references means a bad symbol or missing data.
|
|
|
|
if (S.References < 3)
|
|
|
|
return 1;
|
|
|
|
return std::log(S.References);
|
|
|
|
}
|
|
|
|
|
2017-12-24 03:38:03 +08:00
|
|
|
SymbolSlab::const_iterator SymbolSlab::find(const SymbolID &ID) const {
|
|
|
|
auto It = std::lower_bound(Symbols.begin(), Symbols.end(), ID,
|
|
|
|
[](const Symbol &S, const SymbolID &I) {
|
|
|
|
return S.ID < I;
|
|
|
|
});
|
|
|
|
if (It != Symbols.end() && It->ID == ID)
|
|
|
|
return It;
|
|
|
|
return Symbols.end();
|
[clangd] Introduce a "Symbol" class.
Summary:
* The "Symbol" class represents a C++ symbol in the codebase, containing all the
information of a C++ symbol needed by clangd. clangd will use it in clangd's
AST/dynamic index and global/static index (code completion and code
navigation).
* The SymbolCollector (another IndexAction) will be used to recollect the
symbols when the source file is changed (for ASTIndex), or to generate
all C++ symbols for the whole project.
In the long term (when index-while-building is ready), clangd should share a
same "Symbol" structure and IndexAction with index-while-building, but
for now we want to have some stuff working in clangd.
Reviewers: ioeric, sammccall, ilya-biryukov, malaperle
Reviewed By: sammccall
Subscribers: malaperle, klimek, mgorny, cfe-commits
Differential Revision: https://reviews.llvm.org/D40897
llvm-svn: 320486
2017-12-12 23:42:10 +08:00
|
|
|
}
|
|
|
|
|
2017-12-24 03:38:03 +08:00
|
|
|
// Copy the underlying data of the symbol into the owned arena.
|
|
|
|
static void own(Symbol &S, DenseSet<StringRef> &Strings,
|
|
|
|
BumpPtrAllocator &Arena) {
|
|
|
|
// Intern replaces V with a reference to the same string owned by the arena.
|
|
|
|
auto Intern = [&](StringRef &V) {
|
|
|
|
auto R = Strings.insert(V);
|
|
|
|
if (R.second) { // New entry added to the table, copy the string.
|
2017-12-28 22:47:01 +08:00
|
|
|
*R.first = V.copy(Arena);
|
2017-12-24 03:38:03 +08:00
|
|
|
}
|
|
|
|
V = *R.first;
|
|
|
|
};
|
[clangd] Introduce a "Symbol" class.
Summary:
* The "Symbol" class represents a C++ symbol in the codebase, containing all the
information of a C++ symbol needed by clangd. clangd will use it in clangd's
AST/dynamic index and global/static index (code completion and code
navigation).
* The SymbolCollector (another IndexAction) will be used to recollect the
symbols when the source file is changed (for ASTIndex), or to generate
all C++ symbols for the whole project.
In the long term (when index-while-building is ready), clangd should share a
same "Symbol" structure and IndexAction with index-while-building, but
for now we want to have some stuff working in clangd.
Reviewers: ioeric, sammccall, ilya-biryukov, malaperle
Reviewed By: sammccall
Subscribers: malaperle, klimek, mgorny, cfe-commits
Differential Revision: https://reviews.llvm.org/D40897
llvm-svn: 320486
2017-12-12 23:42:10 +08:00
|
|
|
|
2017-12-24 03:38:03 +08:00
|
|
|
// We need to copy every StringRef field onto the arena.
|
|
|
|
Intern(S.Name);
|
|
|
|
Intern(S.Scope);
|
2018-02-07 00:10:35 +08:00
|
|
|
Intern(S.CanonicalDeclaration.FileURI);
|
2018-02-09 22:42:01 +08:00
|
|
|
Intern(S.Definition.FileURI);
|
2018-01-10 01:32:00 +08:00
|
|
|
|
2018-06-23 00:11:35 +08:00
|
|
|
Intern(S.Signature);
|
|
|
|
Intern(S.CompletionSnippetSuffix);
|
2018-01-10 01:32:00 +08:00
|
|
|
|
|
|
|
if (S.Detail) {
|
|
|
|
// Copy values of StringRefs into arena.
|
|
|
|
auto *Detail = Arena.Allocate<Symbol::Details>();
|
2018-01-16 04:09:09 +08:00
|
|
|
*Detail = *S.Detail;
|
2018-01-10 01:32:00 +08:00
|
|
|
// Intern the actual strings.
|
2018-01-16 04:09:09 +08:00
|
|
|
Intern(Detail->Documentation);
|
2018-06-23 00:11:35 +08:00
|
|
|
Intern(Detail->ReturnType);
|
2018-02-16 22:15:55 +08:00
|
|
|
Intern(Detail->IncludeHeader);
|
2018-01-16 04:09:09 +08:00
|
|
|
// Replace the detail pointer with our copy.
|
|
|
|
S.Detail = Detail;
|
2018-01-10 01:32:00 +08:00
|
|
|
}
|
2017-12-24 03:38:03 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
void SymbolSlab::Builder::insert(const Symbol &S) {
|
|
|
|
auto R = SymbolIndex.try_emplace(S.ID, Symbols.size());
|
|
|
|
if (R.second) {
|
|
|
|
Symbols.push_back(S);
|
|
|
|
own(Symbols.back(), Strings, Arena);
|
|
|
|
} else {
|
|
|
|
auto &Copy = Symbols[R.first->second] = S;
|
|
|
|
own(Copy, Strings, Arena);
|
|
|
|
}
|
|
|
|
}
|
2017-12-21 22:58:44 +08:00
|
|
|
|
2017-12-24 03:38:03 +08:00
|
|
|
SymbolSlab SymbolSlab::Builder::build() && {
|
|
|
|
Symbols = {Symbols.begin(), Symbols.end()}; // Force shrink-to-fit.
|
|
|
|
// Sort symbols so the slab can binary search over them.
|
|
|
|
std::sort(Symbols.begin(), Symbols.end(),
|
|
|
|
[](const Symbol &L, const Symbol &R) { return L.ID < R.ID; });
|
|
|
|
// We may have unused strings from overwritten symbols. Build a new arena.
|
|
|
|
BumpPtrAllocator NewArena;
|
|
|
|
DenseSet<StringRef> Strings;
|
|
|
|
for (auto &S : Symbols)
|
|
|
|
own(S, Strings, NewArena);
|
|
|
|
return SymbolSlab(std::move(NewArena), std::move(Symbols));
|
[clangd] Introduce a "Symbol" class.
Summary:
* The "Symbol" class represents a C++ symbol in the codebase, containing all the
information of a C++ symbol needed by clangd. clangd will use it in clangd's
AST/dynamic index and global/static index (code completion and code
navigation).
* The SymbolCollector (another IndexAction) will be used to recollect the
symbols when the source file is changed (for ASTIndex), or to generate
all C++ symbols for the whole project.
In the long term (when index-while-building is ready), clangd should share a
same "Symbol" structure and IndexAction with index-while-building, but
for now we want to have some stuff working in clangd.
Reviewers: ioeric, sammccall, ilya-biryukov, malaperle
Reviewed By: sammccall
Subscribers: malaperle, klimek, mgorny, cfe-commits
Differential Revision: https://reviews.llvm.org/D40897
llvm-svn: 320486
2017-12-12 23:42:10 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
} // namespace clangd
|
|
|
|
} // namespace clang
|