clang-tools 8.0.0

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