clang-tools  10.0.0
Ref.cpp
Go to the documentation of this file.
1 //===--- Ref.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 "Ref.h"
10 
11 namespace clang {
12 namespace clangd {
13 
14 llvm::raw_ostream &operator<<(llvm::raw_ostream &OS, RefKind K) {
15  if (K == RefKind::Unknown)
16  return OS << "Unknown";
17  static constexpr std::array<const char *, 3> Messages = {"Decl", "Def",
18  "Ref"};
19  bool VisitedOnce = false;
20  for (unsigned I = 0; I < Messages.size(); ++I) {
21  if (static_cast<uint8_t>(K) & 1u << I) {
22  if (VisitedOnce)
23  OS << ", ";
24  OS << Messages[I];
25  VisitedOnce = true;
26  }
27  }
28  return OS;
29 }
30 
31 llvm::raw_ostream &operator<<(llvm::raw_ostream &OS, const Ref &R) {
32  return OS << R.Location << ":" << R.Kind;
33 }
34 
35 void RefSlab::Builder::insert(const SymbolID &ID, const Ref &S) {
36  auto &M = Refs[ID];
37  if (M.count(S))
38  return;
39  Ref R = S;
40  R.Location.FileURI =
41  UniqueStrings.save(R.Location.FileURI).data();
42  M.insert(std::move(R));
43 }
44 
46  // We can reuse the arena, as it only has unique strings and we need them all.
47  // Reallocate refs on the arena to reduce waste and indirections when reading.
48  std::vector<std::pair<SymbolID, llvm::ArrayRef<Ref>>> Result;
49  Result.reserve(Refs.size());
50  size_t NumRefs = 0;
51  for (auto &Sym : Refs) {
52  std::vector<Ref> SymRefs(Sym.second.begin(), Sym.second.end());
53  NumRefs += SymRefs.size();
54  Result.emplace_back(Sym.first, llvm::ArrayRef<Ref>(SymRefs).copy(Arena));
55  }
56  return RefSlab(std::move(Result), std::move(Arena), NumRefs);
57 }
58 
59 } // namespace clangd
60 } // namespace clang
An efficient structure of storing large set of symbol references in memory.
Definition: Ref.h:69
Represents a symbol occurrence in the source file.
Definition: Ref.h:52
SymbolLocation Location
The source location where the symbol is named.
Definition: Ref.h:54
RefKind
Describes the kind of a cross-reference.
Definition: Ref.h:28
===– Representation.cpp - ClangDoc Representation --------—*- C++ -*-===//
void insert(const SymbolID &ID, const Ref &S)
Adds a ref to the slab. Deep copy: Strings will be owned by the slab.
Definition: Ref.cpp:35
llvm::raw_ostream & operator<<(llvm::raw_ostream &OS, const CodeCompletion &C)
RefKind Kind
Definition: Ref.h:55
RefSlab build() &&
Consumes the builder to finalize the slab.
Definition: Ref.cpp:45