clang-tools 10.0.0

Symbol.cpp
Go to the documentation of this file.
1 //===--- Symbol.cpp ----------------------------------------------*- C++-*-===//
2 //
3 // Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions.
4 // See https://llvm.org/LICENSE.txt for license information.
5 // SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception
6 //
7 //===----------------------------------------------------------------------===//
8
9 #include "Symbol.h"
10
11 namespace clang {
12 namespace clangd {
13
14 llvm::raw_ostream &operator<<(llvm::raw_ostream &OS, Symbol::SymbolFlag F) {
15 if (F == Symbol::None)
16 return OS << "None";
17 std::string S;
18 if (F & Symbol::Deprecated)
19 S += "deprecated|";
20 if (F & Symbol::IndexedForCodeCompletion)
21 S += "completion|";
22 return OS << llvm::StringRef(S).rtrim('|');
23 }
24
25 llvm::raw_ostream &operator<<(llvm::raw_ostream &OS, const Symbol &S) {
26 return OS << S.Scope << S.Name;
27 }
28
29 float quality(const Symbol &S) {
30 // This avoids a sharp gradient for tail symbols, and also neatly avoids the
31 // question of whether 0 references means a bad symbol or missing data.
32 if (S.References < 3)
33 return 1;
34 return std::log(S.References);
35 }
36
37 SymbolSlab::const_iterator SymbolSlab::find(const SymbolID &ID) const {
38 auto It = llvm::partition_point(Symbols,
39 [&](const Symbol &S) { return S.ID < ID; });
40 if (It != Symbols.end() && It->ID == ID)
41 return It;
42 return Symbols.end();
43 }
44
45 // Copy the underlying data of the symbol into the owned arena.
46 static void own(Symbol &S, llvm::UniqueStringSaver &Strings) {
47 visitStrings(S, [&](llvm::StringRef &V) { V = Strings.save(V); });
48 }
49
50 void SymbolSlab::Builder::insert(const Symbol &S) {
51 own(Symbols[S.ID] = S, UniqueStrings);
52 }
53
54 SymbolSlab SymbolSlab::Builder::build() && {
55 // Sort symbols into vector so the slab can binary search over them.
56 std::vector<Symbol> SortedSymbols;
57 SortedSymbols.reserve(Symbols.size());
58 for (auto &Entry : Symbols)
59 SortedSymbols.push_back(std::move(Entry.second));
60 llvm::sort(SortedSymbols,
61 [](const Symbol &L, const Symbol &R) { return L.ID < R.ID; });
62 // We may have unused strings from overwritten symbols. Build a new arena.
63 llvm::BumpPtrAllocator NewArena;
64 llvm::UniqueStringSaver Strings(NewArena);
65 for (auto &S : SortedSymbols)
66 own(S, Strings);
67 return SymbolSlab(std::move(NewArena), std::move(SortedSymbols));
68 }
69
70 } // namespace clangd
71 } // namespace clang
clang::clangd::SymbolSlab
An immutable symbol container that stores a set of symbols.
Definition: Symbol.h:177
Symbol.h
clang::clangd::detail::log
void log(Logger::Level, const llvm::formatv_object_base &)
Definition: Logger.cpp:30
clang::clangd::SymbolSlab::const_iterator
std::vector< Symbol >::const_iterator const_iterator
Definition: Symbol.h:179
clang::clangd::SymbolSlab::find
const_iterator find(const SymbolID &SymID) const
Definition: Symbol.cpp:37
clang::clangd::SymbolSlab::Builder::insert
void insert(const Symbol &S)
Adds a symbol, overwriting any existing one with the same ID.
Definition: Symbol.cpp:50
clang::clangd::Symbol::Scope
llvm::StringRef Scope
The containing namespace. e.g. "" (global), "ns::" (top-level namespace).
Definition: Symbol.h:44
clang::clangd::Symbol::SymbolFlag
SymbolFlag
Definition: Symbol.h:113
clang::clangd::SymbolSlab::SymbolSlab
SymbolSlab()=default
clang::clangd::Symbol::References
unsigned References
The number of translation units that reference this symbol from their main file.
Definition: Symbol.h:59
clang::clangd::Symbol::ID
SymbolID ID
The ID of the symbol.
Definition: Symbol.h:38
clang::clangd::SymbolSlab::Builder::build
SymbolSlab build() &&
Consumes the builder to finalize the slab.
Definition: Symbol.cpp:54
clang::clangd::Symbol::IndexedForCodeCompletion
Whether or not this symbol is meant to be used for the code completion.
Definition: Symbol.h:119
Strings
std::vector< llvm::StringRef > Strings
Definition: Serialization.cpp:195
clang::clangd::own
static void own(Symbol &S, llvm::UniqueStringSaver &Strings)
Definition: Symbol.cpp:46
clang::clangd::Symbol
The class presents a C++ symbol, e.g.
Definition: Symbol.h:36
clang
===– Representation.cpp - ClangDoc Representation --------—*- C++ -*-===//
Definition: ApplyReplacements.h:27
clang::clangd::Symbol::Name
llvm::StringRef Name
The unqualified name of the symbol, e.g. "bar" (for ns::bar).
Definition: Symbol.h:42
clang::clangd::Symbol::Deprecated
Indicates if the symbol is deprecated.
Definition: Symbol.h:121
clang::clangd::operator<<
llvm::raw_ostream & operator<<(llvm::raw_ostream &OS, const CodeCompletion &C)
Definition: CodeComplete.cpp:1850
clang::clangd::Symbol::None
Definition: Symbol.h:114
Entry
Definition: Modularize.cpp:429
clang::clangd::visitStrings
void visitStrings(Symbol &S, const Callback &CB)
Invokes Callback with each StringRef& contained in the Symbol.
Definition: Symbol.h:147
clang::clangd::quality
float quality(const Symbol &S)
Computes query-independent quality score for a Symbol.
Definition: Symbol.cpp:29
clang::clangd::SymbolID
Definition: SymbolID.h:31