clang-tools 10.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/Support/StringSaver.h"
17 #include "llvm/Support/raw_ostream.h"
18 #include <cstdint>
19 #include <set>
20 #include <utility>
21
22 namespace clang {
23 namespace clangd {
24
25 /// Describes the kind of a cross-reference.
26 ///
27 /// This is a bitfield which can be combined from different kinds.
28 enum class RefKind : uint8_t {
29 Unknown = 0,
30 Declaration = static_cast<uint8_t>(index::SymbolRole::Declaration),
31 Definition = static_cast<uint8_t>(index::SymbolRole::Definition),
32 Reference = static_cast<uint8_t>(index::SymbolRole::Reference),
33 All = Declaration | Definition | Reference,
34 };
35
36 inline RefKind operator|(RefKind L, RefKind R) {
37 return static_cast<RefKind>(static_cast<uint8_t>(L) |
38 static_cast<uint8_t>(R));
39 }
40 inline RefKind &operator|=(RefKind &L, RefKind R) { return L = L | R; }
41 inline RefKind operator&(RefKind A, RefKind B) {
42 return static_cast<RefKind>(static_cast<uint8_t>(A) &
43 static_cast<uint8_t>(B));
44 }
45
46 llvm::raw_ostream &operator<<(llvm::raw_ostream &, RefKind);
47
48 /// Represents a symbol occurrence in the source file.
49 /// Despite the name, it could be a declaration/definition/reference.
50 ///
51 /// WARNING: Location does not own the underlying data - Copies are shallow.
52 struct Ref {
53 /// The source location where the symbol is named.
54 SymbolLocation Location;
55 RefKind Kind = RefKind::Unknown;
56 };
57
58 inline bool operator<(const Ref &L, const Ref &R) {
59 return std::tie(L.Location, L.Kind) < std::tie(R.Location, R.Kind);
60 }
61 inline bool operator==(const Ref &L, const Ref &R) {
62 return std::tie(L.Location, L.Kind) == std::tie(R.Location, R.Kind);
63 }
64
65 llvm::raw_ostream &operator<<(llvm::raw_ostream &, const Ref &);
66
67 /// An efficient structure of storing large set of symbol references in memory.
68 /// Filenames are deduplicated.
69 class RefSlab {
70 public:
71 // Refs are stored in order.
72 using value_type = std::pair<SymbolID, llvm::ArrayRef<Ref>>;
73 using const_iterator = std::vector<value_type>::const_iterator;
74 using iterator = const_iterator;
75
76 RefSlab() = default;
77 RefSlab(RefSlab &&Slab) = default;
78 RefSlab &operator=(RefSlab &&RHS) = default;
79
80 const_iterator begin() const { return Refs.begin(); }
81 const_iterator end() const { return Refs.end(); }
82 /// Gets the number of symbols.
83 size_t size() const { return Refs.size(); }
84 size_t numRefs() const { return NumRefs; }
85 bool empty() const { return Refs.empty(); }
86
87 size_t bytes() const {
88 return sizeof(*this) + Arena.getTotalMemory() +
89 sizeof(value_type) * Refs.capacity();
90 }
91
92 /// RefSlab::Builder is a mutable container that can 'freeze' to RefSlab.
93 class Builder {
94 public:
95 Builder() : UniqueStrings(Arena) {}
96 /// Adds a ref to the slab. Deep copy: Strings will be owned by the slab.
97 void insert(const SymbolID &ID, const Ref &S);
98 /// Consumes the builder to finalize the slab.
99 RefSlab build() &&;
100
101 private:
102 llvm::BumpPtrAllocator Arena;
103 llvm::UniqueStringSaver UniqueStrings; // Contents on the arena.
104 llvm::DenseMap<SymbolID, std::set<Ref>> Refs;
105 };
106
107 private:
108 RefSlab(std::vector<value_type> Refs, llvm::BumpPtrAllocator Arena,
109 size_t NumRefs)
110 : Arena(std::move(Arena)), Refs(std::move(Refs)), NumRefs(NumRefs) {}
111
112 llvm::BumpPtrAllocator Arena;
113 std::vector<value_type> Refs;
114 /// Number of all references.
115 size_t NumRefs = 0;
116 };
117
118 } // namespace clangd
119 } // namespace clang
120
121 #endif // LLVM_CLANG_TOOLS_EXTRA_CLANGD_INDEX_REF_H
clang::clangd::RefKind::Unknown
clang::clangd::SymbolLocation
Definition: SymbolLocation.h:19
clang::clangd::RefSlab
An efficient structure of storing large set of symbol references in memory.
Definition: Ref.h:69
clang::clangd::RefKind::Declaration
clang::clangd::operator==
bool operator==(const HoverInfo::Param &LHS, const HoverInfo::Param &RHS)
Definition: Hover.h:78
clang::clangd::Ref
Represents a symbol occurrence in the source file.
Definition: Ref.h:52
clang::clangd::RefKind::All
clang::clangd::RefSlab::numRefs
size_t numRefs() const
Definition: Ref.h:84
clang::clangd::RefSlab::Builder::Builder
Builder()
Definition: Ref.h:95
clang::clangd::operator &
DeclRelationSet operator &(DeclRelation L, DeclRelation R)
Definition: FindTarget.h:198
Kind
BindArgumentKind Kind
Definition: AvoidBindCheck.cpp:57
clang::clangd::RefSlab::begin
const_iterator begin() const
Definition: Ref.h:80
clang::clangd::RefSlab::iterator
const_iterator iterator
Definition: Ref.h:74
Arena
llvm::BumpPtrAllocator Arena
Definition: Serialization.cpp:194
clang::clangd::operator|
DeclRelationSet operator|(DeclRelation L, DeclRelation R)
Definition: FindTarget.h:195
clang::clangd::operator<
bool operator<(const Ref &L, const Ref &R)
Definition: Ref.h:58
clang::clangd::Ref::Location
SymbolLocation Location
The source location where the symbol is named.
Definition: Ref.h:54
clang::clangd::RefKind::Definition
clang::clangd::RefSlab::size
size_t size() const
Gets the number of symbols.
Definition: Ref.h:83
clang::clangd::RefKind
RefKind
Describes the kind of a cross-reference.
Definition: Ref.h:28
clang::clangd::RefSlab::Builder
RefSlab::Builder is a mutable container that can &#39;freeze&#39; to RefSlab.
Definition: Ref.h:93
clang::clangd::RefKind::Reference
clang::clangd::RefSlab::empty
bool empty() const
Definition: Ref.h:85
clang
===– Representation.cpp - ClangDoc Representation --------—*- C++ -*-===//
Definition: ApplyReplacements.h:27
clang::clangd::RefSlab::bytes
size_t bytes() const
Definition: Ref.h:87
Refs
RefSlab Refs
Definition: SymbolCollectorTests.cpp:296
clang::clangd::operator<<
llvm::raw_ostream & operator<<(llvm::raw_ostream &OS, const CodeCompletion &C)
Definition: CodeComplete.cpp:1850
clang::clangd::RefSlab::end
const_iterator end() const
Definition: Ref.h:81
clang::clangd::RefSlab::const_iterator
std::vector< value_type >::const_iterator const_iterator
Definition: Ref.h:73
clang::clangd::RefSlab::value_type
std::pair< SymbolID, llvm::ArrayRef< Ref > > value_type
Definition: Ref.h:72
clang::clangd::Ref::Kind
RefKind Kind
Definition: Ref.h:55
SymbolID.h
clang::clangd::operator|=
IncludeGraphNode::SourceFlag & operator|=(IncludeGraphNode::SourceFlag &A, IncludeGraphNode::SourceFlag B)
Definition: Headers.h:96
SymbolLocation.h
clang::clangd::SymbolID
Definition: SymbolID.h:31