clang-tools 10.0.0

SymbolID.h
Go to the documentation of this file.
1 //===--- SymbolID.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_SYMBOLID_H
10 #define LLVM_CLANG_TOOLS_EXTRA_CLANGD_INDEX_SYMBOLID_H
11
12 #include "llvm/ADT/DenseMap.h"
13 #include "llvm/ADT/Hashing.h"
14 #include "llvm/ADT/StringRef.h"
15 #include "llvm/Support/Error.h"
16 #include "llvm/Support/raw_ostream.h"
17 #include <array>
18 #include <string>
19
20 namespace clang {
21 namespace clangd {
22
23 // The class identifies a particular C++ symbol (class, function, method, etc).
24 //
25 // As USRs (Unified Symbol Resolution) could be large, especially for functions
26 // with long type arguments, SymbolID is using truncated SHA1(USR) values to
27 // guarantee the uniqueness of symbols while using a relatively small amount of
28 // memory (vs storing USRs directly).
29 //
30 // SymbolID can be used as key in the symbol indexes to lookup the symbol.
31 class SymbolID {
32 public:
33 SymbolID() = default;
34 explicit SymbolID(llvm::StringRef USR);
35
36 bool operator==(const SymbolID &Sym) const {
37 return HashValue == Sym.HashValue;
38 }
39 bool operator<(const SymbolID &Sym) const {
40 return HashValue < Sym.HashValue;
41 }
42
43 // The stored hash is truncated to RawSize bytes.
44 // This trades off memory against the number of symbols we can handle.
45 constexpr static size_t RawSize = 8;
46 llvm::StringRef raw() const;
47 static SymbolID fromRaw(llvm::StringRef);
48
49 // Returns a hex encoded string.
50 std::string str() const;
51 static llvm::Expected<SymbolID> fromStr(llvm::StringRef);
52
53 private:
54 std::array<uint8_t, RawSize> HashValue;
55 };
56
57 llvm::hash_code hash_value(const SymbolID &ID);
58
59 // Write SymbolID into the given stream. SymbolID is encoded as ID.str().
60 llvm::raw_ostream &operator<<(llvm::raw_ostream &OS, const SymbolID &ID);
61
62 } // namespace clangd
63 } // namespace clang
64
65 namespace llvm {
66 // Support SymbolIDs as DenseMap keys.
67 template <> struct DenseMapInfo<clang::clangd::SymbolID> {
68 static inline clang::clangd::SymbolID getEmptyKey() {
69 static clang::clangd::SymbolID EmptyKey("EMPTYKEY");
70 return EmptyKey;
71 }
72 static inline clang::clangd::SymbolID getTombstoneKey() {
73 static clang::clangd::SymbolID TombstoneKey("TOMBSTONEKEY");
74 return TombstoneKey;
75 }
76 static unsigned getHashValue(const clang::clangd::SymbolID &Sym) {
77 return hash_value(Sym);
78 }
79 static bool isEqual(const clang::clangd::SymbolID &LHS,
80 const clang::clangd::SymbolID &RHS) {
81 return LHS == RHS;
82 }
83 };
84 } // namespace llvm
85
86 #endif // LLVM_CLANG_TOOLS_EXTRA_CLANGD_INDEX_SYMBOLID_H
clang::clangd::SymbolID::SymbolID
SymbolID()=default
llvm
Some operations such as code completion produce a set of candidates.
Definition: YAMLGenerator.cpp:28
clang::clangd::SymbolID::fromRaw
static SymbolID fromRaw(llvm::StringRef)
Definition: SymbolID.cpp:26
clang::clangd::SymbolID::fromStr
static llvm::Expected< SymbolID > fromStr(llvm::StringRef)
Definition: SymbolID.cpp:35
llvm::DenseMapInfo< clang::clangd::SymbolID >::getEmptyKey
static clang::clangd::SymbolID getEmptyKey()
Definition: SymbolID.h:68
clang::clangd::SymbolID::RawSize
static constexpr size_t RawSize
Definition: SymbolID.h:45
clang::clangd::SymbolID::str
std::string str() const
Definition: SymbolID.cpp:33
llvm::DenseMapInfo< clang::clangd::SymbolID >::isEqual
static bool isEqual(const clang::clangd::SymbolID &LHS, const clang::clangd::SymbolID &RHS)
Definition: SymbolID.h:79
clang
===– Representation.cpp - ClangDoc Representation --------—*- C++ -*-===//
Definition: ApplyReplacements.h:27
clang::clangd::SymbolID::raw
llvm::StringRef raw() const
Definition: SymbolID.cpp:21
llvm::DenseMapInfo< clang::clangd::SymbolID >::getTombstoneKey
static clang::clangd::SymbolID getTombstoneKey()
Definition: SymbolID.h:72
clang::clangd::SymbolID::operator<
bool operator<(const SymbolID &Sym) const
Definition: SymbolID.h:39
llvm::DenseMapInfo< clang::clangd::SymbolID >::getHashValue
static unsigned getHashValue(const clang::clangd::SymbolID &Sym)
Definition: SymbolID.h:76
clang::clangd::hash_value
llvm::hash_code hash_value(const SymbolID &ID)
Definition: SymbolID.cpp:50
clang::clangd::operator<<
llvm::raw_ostream & operator<<(llvm::raw_ostream &OS, const CodeCompletion &C)
Definition: CodeComplete.cpp:1850
clang::doc::SymbolID
std::array< uint8_t, 20 > SymbolID
Definition: Representation.h:30
clang::clangd::SymbolID::operator==
bool operator==(const SymbolID &Sym) const
Definition: SymbolID.h:36
clang::clangd::SymbolID
Definition: SymbolID.h:31