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