clang-tools  9.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 const std::vector<const char *> Messages = {"Decl", "Def", "Ref"};
18  bool VisitedOnce = false;
19  for (unsigned I = 0; I < Messages.size(); ++I) {
20  if (static_cast<uint8_t>(K) & 1u << I) {
21  if (VisitedOnce)
22  OS << ", ";
23  OS << Messages[I];
24  VisitedOnce = true;
25  }
26  }
27  return OS;
28 }
29 
30 llvm::raw_ostream &operator<<(llvm::raw_ostream &OS, const Ref &R) {
31  return OS << R.Location << ":" << R.Kind;
32 }
33 
34 void RefSlab::Builder::insert(const SymbolID &ID, const Ref &S) {
35  auto &M = Refs[ID];
36  if (M.count(S))
37  return;
38  Ref R = S;
39  R.Location.FileURI =
40  UniqueStrings.save(R.Location.FileURI).data();
41  M.insert(std::move(R));
42 }
43 
45  // We can reuse the arena, as it only has unique strings and we need them all.
46  // Reallocate refs on the arena to reduce waste and indirections when reading.
47  std::vector<std::pair<SymbolID, llvm::ArrayRef<Ref>>> Result;
48  Result.reserve(Refs.size());
49  size_t NumRefs = 0;
50  for (auto &Sym : Refs) {
51  std::vector<Ref> SymRefs(Sym.second.begin(), Sym.second.end());
52  NumRefs += SymRefs.size();
53  Result.emplace_back(Sym.first, llvm::ArrayRef<Ref>(SymRefs).copy(Arena));
54  }
55  return RefSlab(std::move(Result), std::move(Arena), NumRefs);
56 }
57 
58 } // namespace clangd
59 } // 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++ -*-===//
llvm::Optional< llvm::Expected< tooling::AtomicChanges > > Result
Definition: Rename.cpp:36
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:34
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:44