clang-tools  10.0.0svn
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> {
69  static clang::clangd::SymbolID EmptyKey("EMPTYKEY");
70  return EmptyKey;
71  }
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
Some operations such as code completion produce a set of candidates.
static SymbolID fromRaw(llvm::StringRef)
Definition: SymbolID.cpp:26
static llvm::Expected< SymbolID > fromStr(llvm::StringRef)
Definition: SymbolID.cpp:35
static clang::clangd::SymbolID getEmptyKey()
Definition: SymbolID.h:68
static constexpr size_t RawSize
Definition: SymbolID.h:45
std::string str() const
Definition: SymbolID.cpp:33
static bool isEqual(const clang::clangd::SymbolID &LHS, const clang::clangd::SymbolID &RHS)
Definition: SymbolID.h:79
===– Representation.cpp - ClangDoc Representation --------—*- C++ -*-===//
llvm::StringRef raw() const
Definition: SymbolID.cpp:21
static clang::clangd::SymbolID getTombstoneKey()
Definition: SymbolID.h:72
bool operator<(const SymbolID &Sym) const
Definition: SymbolID.h:39
static unsigned getHashValue(const clang::clangd::SymbolID &Sym)
Definition: SymbolID.h:76
llvm::hash_code hash_value(const SymbolID &ID)
Definition: SymbolID.cpp:50
llvm::raw_ostream & operator<<(llvm::raw_ostream &OS, const CodeCompletion &C)
std::array< uint8_t, 20 > SymbolID
bool operator==(const SymbolID &Sym) const
Definition: SymbolID.h:36