clang-tools  14.0.0git
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 "support/Logger.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 error("Bad ID length");
39  for (char C : Str)
40  if (!llvm::isHexDigit(C))
41  return error("Bad hex ID");
42  return fromRaw(llvm::fromHex(Str));
43 }
44 
45 llvm::raw_ostream &operator<<(llvm::raw_ostream &OS, const SymbolID &ID) {
46  return OS << llvm::toHex(ID.raw());
47 }
48 
49 llvm::hash_code hash_value(const SymbolID &ID) {
50  // We already have a good hash, just return the first bytes.
51  static_assert(sizeof(size_t) <= SymbolID::RawSize,
52  "size_t longer than SHA1!");
53  size_t Result;
54  memcpy(&Result, ID.raw().data(), sizeof(size_t));
55  return llvm::hash_code(Result);
56 }
57 
58 } // namespace clangd
59 } // namespace clang
SymbolID.h
clang::clangd::error
llvm::Error error(std::error_code EC, const char *Fmt, Ts &&... Vals)
Definition: Logger.h:80
clang::clangd::hash_value
llvm::hash_code hash_value(const SymbolID &ID)
Definition: SymbolID.cpp:49
Logger.h
clang::clangd::SymbolID::RawSize
constexpr static size_t RawSize
Definition: SymbolID.h:49
clang::clangd::SymbolID::str
std::string str() const
Definition: SymbolID.cpp:34
clang::clangd::operator<<
llvm::raw_ostream & operator<<(llvm::raw_ostream &OS, const CodeCompletion &C)
Definition: CodeComplete.cpp:2126
ID
static char ID
Definition: Logger.cpp:74
clang::clangd::SymbolID::fromRaw
static SymbolID fromRaw(llvm::StringRef)
Definition: SymbolID.cpp:27
C
const Criteria C
Definition: FunctionCognitiveComplexityCheck.cpp:93
clang::clangd::SymbolID::fromStr
static llvm::Expected< SymbolID > fromStr(llvm::StringRef)
Definition: SymbolID.cpp:36
clang
===– Representation.cpp - ClangDoc Representation --------—*- C++ -*-===//
Definition: ApplyReplacements.h:27
OS
llvm::raw_string_ostream OS
Definition: TraceTests.cpp:163
clang::clangd::SymbolID::SymbolID
SymbolID()=default
clang::clangd::SymbolID::raw
llvm::StringRef raw() const
Definition: SymbolID.cpp:22
clang::clangd::SymbolID
Definition: SymbolID.h:32