clang-tools  8.0.0
SymbolID.h
Go to the documentation of this file.
1 //===--- SymbolID.h ----------------------------------------------*- C++-*-===//
2 //
3 // The LLVM Compiler Infrastructure
4 //
5 // This file is distributed under the University of Illinois Open Source
6 // License. See LICENSE.TXT for details.
7 //
8 //===----------------------------------------------------------------------===//
9 
10 #ifndef LLVM_CLANG_TOOLS_EXTRA_CLANGD_INDEX_SYMBOLID_H
11 #define LLVM_CLANG_TOOLS_EXTRA_CLANGD_INDEX_SYMBOLID_H
12 
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 #endif // LLVM_CLANG_TOOLS_EXTRA_CLANGD_INDEX_SYMBOLID_H
static SymbolID fromRaw(llvm::StringRef)
Definition: SymbolID.cpp:27
static llvm::Expected< SymbolID > fromStr(llvm::StringRef)
Definition: SymbolID.cpp:36
static constexpr size_t RawSize
Definition: SymbolID.h:45
std::string str() const
Definition: SymbolID.cpp:34
===– Representation.cpp - ClangDoc Representation --------—*- C++ -*-===//
llvm::StringRef raw() const
Definition: SymbolID.cpp:22
bool operator<(const SymbolID &Sym) const
Definition: SymbolID.h:39
llvm::hash_code hash_value(const SymbolID &ID)
Definition: SymbolID.cpp:51
llvm::raw_ostream & operator<<(llvm::raw_ostream &OS, const CodeCompletion &C)
bool operator==(const SymbolID &Sym) const
Definition: SymbolID.h:36