clang-tools  11.0.0
Ref.h
Go to the documentation of this file.
1 //===--- Ref.h ---------------------------------------------------*- 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 #ifndef LLVM_CLANG_TOOLS_EXTRA_CLANGD_INDEX_REF_H
10 #define LLVM_CLANG_TOOLS_EXTRA_CLANGD_INDEX_REF_H
11 
12 #include "SymbolID.h"
13 #include "SymbolLocation.h"
14 #include "clang/Index/IndexSymbol.h"
15 #include "llvm/ADT/DenseMap.h"
16 #include "llvm/ADT/Hashing.h"
17 #include "llvm/Support/Allocator.h"
18 #include "llvm/Support/StringSaver.h"
19 #include "llvm/Support/raw_ostream.h"
20 #include <cstdint>
21 #include <set>
22 #include <utility>
23 
24 namespace clang {
25 namespace clangd {
26 
27 /// Describes the kind of a cross-reference.
28 ///
29 /// This is a bitfield which can be combined from different kinds.
30 enum class RefKind : uint8_t {
31  Unknown = 0,
32  // Points to symbol declaration. Example:
33  //
34  // class Foo;
35  // ^ Foo declaration
36  // Foo foo;
37  // ^ this does not reference Foo declaration
38  Declaration = 1 << 0,
39  // Points to symbol definition. Example:
40  //
41  // int foo();
42  // ^ references foo declaration, but not foo definition
43  // int foo() { return 42; }
44  // ^ references foo definition, but not declaration
45  // bool bar() { return true; }
46  // ^ references both definition and declaration
47  Definition = 1 << 1,
48  // Points to symbol reference. Example:
49  //
50  // int Foo = 42;
51  // int Bar = Foo + 1;
52  // ^ this is a reference to Foo
53  Reference = 1 << 2,
54  // The reference explicitly spells out declaration's name. Such references can
55  // not come from macro expansions or implicit AST nodes.
56  //
57  // class Foo { public: Foo() {} };
58  // ^ references declaration, definition and explicitly spells out name
59  // #define MACRO Foo
60  // v there is an implicit constructor call here which is not a spelled ref
61  // Foo foo;
62  // ^ this reference explicitly spells out Foo's name
63  // struct Bar {
64  // MACRO Internal;
65  // ^ this references Foo, but does not explicitly spell out its name
66  // };
67  Spelled = 1 << 3,
69 };
70 
72  return static_cast<RefKind>(static_cast<uint8_t>(L) |
73  static_cast<uint8_t>(R));
74 }
75 inline RefKind &operator|=(RefKind &L, RefKind R) { return L = L | R; }
77  return static_cast<RefKind>(static_cast<uint8_t>(A) &
78  static_cast<uint8_t>(B));
79 }
80 
81 llvm::raw_ostream &operator<<(llvm::raw_ostream &, RefKind);
82 
83 /// Represents a symbol occurrence in the source file.
84 /// Despite the name, it could be a declaration/definition/reference.
85 ///
86 /// WARNING: Location does not own the underlying data - Copies are shallow.
87 struct Ref {
88  /// The source location where the symbol is named.
91 };
92 
93 inline bool operator<(const Ref &L, const Ref &R) {
94  return std::tie(L.Location, L.Kind) < std::tie(R.Location, R.Kind);
95 }
96 inline bool operator==(const Ref &L, const Ref &R) {
97  return std::tie(L.Location, L.Kind) == std::tie(R.Location, R.Kind);
98 }
99 
100 llvm::raw_ostream &operator<<(llvm::raw_ostream &, const Ref &);
101 
102 /// An efficient structure of storing large set of symbol references in memory.
103 /// Filenames are deduplicated.
104 class RefSlab {
105 public:
106  // Refs are stored in order.
107  using value_type = std::pair<SymbolID, llvm::ArrayRef<Ref>>;
108  using const_iterator = std::vector<value_type>::const_iterator;
110 
111  RefSlab() = default;
112  RefSlab(RefSlab &&Slab) = default;
113  RefSlab &operator=(RefSlab &&RHS) = default;
114 
115  const_iterator begin() const { return Refs.begin(); }
116  const_iterator end() const { return Refs.end(); }
117  /// Gets the number of symbols.
118  size_t size() const { return Refs.size(); }
119  size_t numRefs() const { return NumRefs; }
120  bool empty() const { return Refs.empty(); }
121 
122  size_t bytes() const {
123  return sizeof(*this) + Arena.getTotalMemory() +
124  sizeof(value_type) * Refs.capacity();
125  }
126 
127  /// RefSlab::Builder is a mutable container that can 'freeze' to RefSlab.
128  class Builder {
129  public:
130  Builder() : UniqueStrings(Arena) {}
131  /// Adds a ref to the slab. Deep copy: Strings will be owned by the slab.
132  void insert(const SymbolID &ID, const Ref &S);
133  /// Consumes the builder to finalize the slab.
134  RefSlab build() &&;
135 
136  private:
137  // A ref we're storing with its symbol to consume with build().
138  // All strings are interned, so DenseMapInfo can use pointer comparisons.
139  struct Entry {
141  Ref Reference;
142  };
143  friend struct llvm::DenseMapInfo<Entry>;
144 
145  llvm::BumpPtrAllocator Arena;
146  llvm::UniqueStringSaver UniqueStrings; // Contents on the arena.
147  llvm::DenseSet<Entry> Entries;
148  };
149 
150 private:
151  RefSlab(std::vector<value_type> Refs, llvm::BumpPtrAllocator Arena,
152  size_t NumRefs)
153  : Arena(std::move(Arena)), Refs(std::move(Refs)), NumRefs(NumRefs) {}
154 
155  llvm::BumpPtrAllocator Arena;
156  std::vector<value_type> Refs;
157  /// Number of all references.
158  size_t NumRefs = 0;
159 };
160 
161 } // namespace clangd
162 } // namespace clang
163 
164 namespace llvm {
165 template <> struct DenseMapInfo<clang::clangd::RefSlab::Builder::Entry> {
166  using Entry = clang::clangd::RefSlab::Builder::Entry;
167  static inline Entry getEmptyKey() {
168  static Entry E{clang::clangd::SymbolID(""), {}};
169  return E;
170  }
171  static inline Entry getTombstoneKey() {
172  static Entry E{clang::clangd::SymbolID("TOMBSTONE"), {}};
173  return E;
174  }
175  static unsigned getHashValue(const Entry &Val) {
176  return llvm::hash_combine(
177  Val.Symbol, reinterpret_cast<uintptr_t>(Val.Reference.Location.FileURI),
178  Val.Reference.Location.Start.rep(), Val.Reference.Location.End.rep());
179  }
180  static bool isEqual(const Entry &LHS, const Entry &RHS) {
181  return std::tie(LHS.Symbol, LHS.Reference.Location.FileURI,
182  LHS.Reference.Kind) ==
183  std::tie(RHS.Symbol, RHS.Reference.Location.FileURI,
184  RHS.Reference.Kind) &&
185  LHS.Reference.Location.Start == RHS.Reference.Location.Start &&
186  LHS.Reference.Location.End == RHS.Reference.Location.End;
187  }
188 };
189 } // namespace llvm
190 
191 #endif // LLVM_CLANG_TOOLS_EXTRA_CLANGD_INDEX_REF_H
clang::clangd::Ref::Kind
RefKind Kind
Definition: Ref.h:90
llvm
Some operations such as code completion produce a set of candidates.
Definition: YAMLGenerator.cpp:28
clang::clangd::SymbolLocation
Definition: SymbolLocation.h:19
SymbolID.h
E
const Expr * E
Definition: AvoidBindCheck.cpp:88
clang::clangd::RefKind::All
clang::clangd::RefKind::Unknown
Refs
RefSlab Refs
Definition: SymbolCollectorTests.cpp:296
clang::clangd::RefKind::Declaration
SymbolLocation.h
clang::clangd::RefSlab::Builder::Builder
Builder()
Definition: Ref.h:130
clang::clangd::RefSlab::RefSlab
RefSlab()=default
clang::clangd::RefKind
RefKind
Describes the kind of a cross-reference.
Definition: Ref.h:30
clang::clangd::RefSlab::end
const_iterator end() const
Definition: Ref.h:116
clang::clangd::RefSlab::Builder
RefSlab::Builder is a mutable container that can 'freeze' to RefSlab.
Definition: Ref.h:128
llvm::DenseMapInfo< clang::clangd::RefSlab::Builder::Entry >::getEmptyKey
static Entry getEmptyKey()
Definition: Ref.h:167
clang::clangd::RefSlab
An efficient structure of storing large set of symbol references in memory.
Definition: Ref.h:104
clang::clangd::operator&
DeclRelationSet operator&(DeclRelation L, DeclRelation R)
Definition: FindTarget.h:198
llvm::DenseMapInfo< clang::clangd::RefSlab::Builder::Entry >::isEqual
static bool isEqual(const Entry &LHS, const Entry &RHS)
Definition: Ref.h:180
clang::clangd::RefSlab::value_type
std::pair< SymbolID, llvm::ArrayRef< Ref > > value_type
Definition: Ref.h:107
clang::clangd::RefSlab::numRefs
size_t numRefs() const
Definition: Ref.h:119
llvm::DenseMapInfo< clang::clangd::RefSlab::Builder::Entry >::Entry
clang::clangd::RefSlab::Builder::Entry Entry
Definition: Ref.h:166
clang::clangd::Ref::Location
SymbolLocation Location
The source location where the symbol is named.
Definition: Ref.h:89
clang::clangd::operator|=
IncludeGraphNode::SourceFlag & operator|=(IncludeGraphNode::SourceFlag &A, IncludeGraphNode::SourceFlag B)
Definition: Headers.h:100
clang::clangd::Symbol
The class presents a C++ symbol, e.g.
Definition: Symbol.h:36
clang::clangd::RefKind::Reference
clang::clangd::RefSlab::Builder::build
RefSlab build() &&
Consumes the builder to finalize the slab.
Definition: Ref.cpp:42
clang::clangd::RefSlab::Builder::insert
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:36
clang::doc::SymbolID
std::array< uint8_t, 20 > SymbolID
Definition: Representation.h:30
clang::clangd::operator<<
llvm::raw_ostream & operator<<(llvm::raw_ostream &OS, const CodeCompletion &C)
Definition: CodeComplete.cpp:1912
llvm::DenseMapInfo< clang::clangd::RefSlab::Builder::Entry >::getHashValue
static unsigned getHashValue(const Entry &Val)
Definition: Ref.h:175
Entry
Definition: Modularize.cpp:429
clang::clangd::RefSlab::const_iterator
std::vector< value_type >::const_iterator const_iterator
Definition: Ref.h:108
clang::clangd::RefSlab::empty
bool empty() const
Definition: Ref.h:120
clang::clangd::RefKind::Spelled
clang::clangd::Ref
Represents a symbol occurrence in the source file.
Definition: Ref.h:87
clang::clangd::RefSlab::begin
const_iterator begin() const
Definition: Ref.h:115
clang::clangd::RefSlab::iterator
const_iterator iterator
Definition: Ref.h:109
clang::clangd::operator|
DeclRelationSet operator|(DeclRelation L, DeclRelation R)
Definition: FindTarget.h:195
clang::clangd::RefSlab::operator=
RefSlab & operator=(RefSlab &&RHS)=default
clang
===– Representation.cpp - ClangDoc Representation --------—*- C++ -*-===//
Definition: ApplyReplacements.h:27
Arena
llvm::BumpPtrAllocator Arena
Definition: Serialization.cpp:194
clang::clangd::operator<
bool operator<(const Ref &L, const Ref &R)
Definition: Ref.h:93
clang::clangd::RefSlab::size
size_t size() const
Gets the number of symbols.
Definition: Ref.h:118
clang::clangd::operator==
bool operator==(const Inclusion &LHS, const Inclusion &RHS)
Definition: Headers.cpp:273
clang::clangd::SymbolID
Definition: SymbolID.h:31
clang::clangd::RefKind::Definition
llvm::DenseMapInfo< clang::clangd::RefSlab::Builder::Entry >::getTombstoneKey
static Entry getTombstoneKey()
Definition: Ref.h:171
clang::clangd::RefSlab::bytes
size_t bytes() const
Definition: Ref.h:122