clang-tools 10.0.0

SymbolID.cpp
Go to the documentation of this file.
1 //===--- SymbolID.cpp --------------------------------------------*- 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 #include "SymbolID.h"
10 #include "llvm/Support/SHA1.h"
11
12 namespace clang {
13 namespace clangd {
14
15 SymbolID::SymbolID(llvm::StringRef USR) {
16 auto Hash = llvm::SHA1::hash(llvm::arrayRefFromStringRef(USR));
17 static_assert(sizeof(Hash) >= RawSize, "RawSize larger than SHA1");
18 memcpy(HashValue.data(), Hash.data(), RawSize);
19 }
20
21 llvm::StringRef SymbolID::raw() const {
22 return llvm::StringRef(reinterpret_cast<const char *>(HashValue.data()),
23 RawSize);
24 }
25
26 SymbolID SymbolID::fromRaw(llvm::StringRef Raw) {
27 SymbolID ID;
28 assert(Raw.size() == RawSize);
29 memcpy(ID.HashValue.data(), Raw.data(), RawSize);
30 return ID;
31 }
32
33 std::string SymbolID::str() const { return llvm::toHex(raw()); }
34
35 llvm::Expected<SymbolID> SymbolID::fromStr(llvm::StringRef Str) {
36 if (Str.size() != RawSize * 2)
37 return llvm::createStringError(llvm::inconvertibleErrorCode(),
38 "Bad ID length");
39 for (char C : Str)
40 if (!llvm::isHexDigit(C))
41 return llvm::createStringError(llvm::inconvertibleErrorCode(),
42 "Bad hex ID");
43 return fromRaw(llvm::fromHex(Str));
44 }
45
46 llvm::raw_ostream &operator<<(llvm::raw_ostream &OS, const SymbolID &ID) {
47 return OS << llvm::toHex(ID.raw());
48 }
49
50 llvm::hash_code hash_value(const SymbolID &ID) {
51 // We already have a good hash, just return the first bytes.
52 static_assert(sizeof(size_t) <= SymbolID::RawSize,
53 "size_t longer than SHA1!");
54 size_t Result;
55 memcpy(&Result, ID.raw().data(), sizeof(size_t));
56 return llvm::hash_code(Result);
57 }
58
59 } // namespace clangd
60 } // namespace clang
clang::clangd::SymbolID::SymbolID
SymbolID()=default
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
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
clang
===– Representation.cpp - ClangDoc Representation --------—*- C++ -*-===//
Definition: ApplyReplacements.h:27
clang::clangd::SymbolID::raw
llvm::StringRef raw() const
Definition: SymbolID.cpp:21
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
SymbolID.h
clang::clangd::SymbolID
Definition: SymbolID.h:31