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