1 //===--- Symbol.cpp ----------------------------------------------*- C++-*-===//
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
7 //===----------------------------------------------------------------------===//
14 llvm::raw_ostream
&operator<<(llvm::raw_ostream
&OS
, Symbol::SymbolFlag F
) {
15 if (F
== Symbol::None
)
18 if (F
& Symbol::Deprecated
)
20 if (F
& Symbol::IndexedForCodeCompletion
)
22 return OS
<< llvm::StringRef(S
).rtrim('|');
25 llvm::raw_ostream
&operator<<(llvm::raw_ostream
&OS
, const Symbol
&S
) {
26 return OS
<< S
.Scope
<< S
.Name
;
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.
34 return std::log(S
.References
);
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
)
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
); });
50 void SymbolSlab::Builder::insert(const Symbol
&S
) {
51 own(Symbols
[S
.ID
] = S
, UniqueStrings
);
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
)
67 return SymbolSlab(std::move(NewArena
), std::move(SortedSymbols
));
70 llvm::raw_ostream
&operator<<(llvm::raw_ostream
&OS
, const SymbolSlab
&Slab
) {
72 llvm::StringRef Sep
= "";
73 for (const auto &S
: Slab
) {