clang-tools  16.0.0git
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 #include <cmath>
12 
13 namespace clang {
14 namespace clangd {
15 
16 llvm::raw_ostream &operator<<(llvm::raw_ostream &OS, Symbol::SymbolFlag F) {
17  if (F == Symbol::None)
18  return OS << "None";
19  std::string S;
20  if (F & Symbol::Deprecated)
21  S += "deprecated|";
23  S += "completion|";
24  return OS << llvm::StringRef(S).rtrim('|');
25 }
26 
27 llvm::raw_ostream &operator<<(llvm::raw_ostream &OS, const Symbol &S) {
28  return OS << S.Scope << S.Name;
29 }
30 
31 float quality(const Symbol &S) {
32  // This avoids a sharp gradient for tail symbols, and also neatly avoids the
33  // question of whether 0 references means a bad symbol or missing data.
34  if (S.References < 3)
35  return 1;
36  return std::log(S.References);
37 }
38 
40  auto It = llvm::partition_point(Symbols,
41  [&](const Symbol &S) { return S.ID < ID; });
42  if (It != Symbols.end() && It->ID == ID)
43  return It;
44  return Symbols.end();
45 }
46 
47 // Copy the underlying data of the symbol into the owned arena.
48 static void own(Symbol &S, llvm::UniqueStringSaver &Strings) {
49  visitStrings(S, [&](llvm::StringRef &V) { V = Strings.save(V); });
50 }
51 
53  own(Symbols[S.ID] = S, UniqueStrings);
54 }
55 
57  // Sort symbols into vector so the slab can binary search over them.
58  std::vector<Symbol> SortedSymbols;
59  SortedSymbols.reserve(Symbols.size());
60  for (auto &Entry : Symbols)
61  SortedSymbols.push_back(std::move(Entry.second));
62  llvm::sort(SortedSymbols,
63  [](const Symbol &L, const Symbol &R) { return L.ID < R.ID; });
64  // We may have unused strings from overwritten symbols.
65  // In practice, these are extremely small, it's not worth compacting.
66  return SymbolSlab(std::move(Arena), std::move(SortedSymbols));
67 }
68 
69 llvm::raw_ostream &operator<<(llvm::raw_ostream &OS, const SymbolSlab &Slab) {
70  OS << "{";
71  llvm::StringRef Sep = "";
72  for (const auto &S : Slab) {
73  OS << Sep << S;
74  Sep = ", ";
75  }
76  OS << "}";
77  return OS;
78 }
79 } // namespace clangd
80 } // namespace clang
clang::clangd::Symbol::IndexedForCodeCompletion
@ IndexedForCodeCompletion
Whether or not this symbol is meant to be used for the code completion.
Definition: Symbol.h:119
clang::clangd::Symbol::ID
SymbolID ID
The ID of the symbol.
Definition: Symbol.h:38
clang::clangd::Symbol::SymbolFlag
SymbolFlag
Definition: Symbol.h:113
clang::clangd::Symbol::Deprecated
@ Deprecated
Indicates if the symbol is deprecated.
Definition: Symbol.h:121
clang::clangd::SymbolSlab::Builder::build
SymbolSlab build() &&
Consumes the builder to finalize the slab.
Definition: Symbol.cpp:56
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::None
@ None
Definition: Symbol.h:114
clang::clangd::Symbol
The class presents a C++ symbol, e.g.
Definition: Symbol.h:36
clang::clangd::SymbolSlab::find
const_iterator find(const SymbolID &SymID) const
Definition: Symbol.cpp:39
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::operator<<
llvm::raw_ostream & operator<<(llvm::raw_ostream &OS, const CodeCompletion &C)
Definition: CodeComplete.cpp:2217
Symbol.h
Entry
Definition: Modularize.cpp:427
Strings
std::vector< llvm::StringRef > Strings
Definition: Serialization.cpp:214
ID
static char ID
Definition: Logger.cpp:74
clang::clangd::Symbol::Scope
llvm::StringRef Scope
The containing namespace. e.g. "" (global), "ns::" (top-level namespace).
Definition: Symbol.h:44
clang
===– Representation.cpp - ClangDoc Representation --------—*- C++ -*-===//
Definition: ApplyReplacements.h:27
OS
llvm::raw_string_ostream OS
Definition: TraceTests.cpp:160
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::SymbolSlab::const_iterator
std::vector< Symbol >::const_iterator const_iterator
Definition: Symbol.h:179
clang::clangd::SymbolSlab
An immutable symbol container that stores a set of symbols.
Definition: Symbol.h:177
clang::clangd::SymbolID
Definition: SymbolID.h:32
clang::clangd::own
static void own(Symbol &S, llvm::UniqueStringSaver &Strings)
Definition: Symbol.cpp:48
clang::clangd::detail::log
void log(Logger::Level L, const char *Fmt, Ts &&... Vals)
Definition: Logger.h:47
clang::clangd::quality
float quality(const Symbol &S)
Computes query-independent quality score for a Symbol.
Definition: Symbol.cpp:31
clang::clangd::SymbolSlab::SymbolSlab
SymbolSlab()=default
clang::clangd::SymbolSlab::Builder::insert
void insert(const Symbol &S)
Adds a symbol, overwriting any existing one with the same ID.
Definition: Symbol.cpp:52