clang-tools 19.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/ADT/StringExtras.h"
12#include "llvm/Support/SHA1.h"
13
14namespace clang {
15namespace clangd {
16
17SymbolID::SymbolID(llvm::StringRef USR) {
18 auto Hash = llvm::SHA1::hash(llvm::arrayRefFromStringRef(USR));
19 static_assert(sizeof(Hash) >= RawSize, "RawSize larger than SHA1");
20 memcpy(HashValue.data(), Hash.data(), RawSize);
21}
22
23llvm::StringRef SymbolID::raw() const {
24 return llvm::StringRef(reinterpret_cast<const char *>(HashValue.data()),
25 RawSize);
26}
27
28SymbolID SymbolID::fromRaw(llvm::StringRef Raw) {
30 assert(Raw.size() == RawSize);
31 memcpy(ID.HashValue.data(), Raw.data(), RawSize);
32 return ID;
33}
34
35std::string SymbolID::str() const { return llvm::toHex(raw()); }
36
37llvm::Expected<SymbolID> SymbolID::fromStr(llvm::StringRef Str) {
38 if (Str.size() != RawSize * 2)
39 return error("Bad ID length");
40 for (char C : Str)
41 if (!llvm::isHexDigit(C))
42 return error("Bad hex ID");
43 return fromRaw(llvm::fromHex(Str));
44}
45
46llvm::raw_ostream &operator<<(llvm::raw_ostream &OS, const SymbolID &ID) {
47 return OS << llvm::toHex(ID.raw());
48}
49
50} // namespace clangd
51} // namespace clang
const Criteria C
std::string USR
llvm::raw_string_ostream OS
Definition: TraceTests.cpp:160
static constexpr size_t RawSize
Definition: SymbolID.h:49
static llvm::Expected< SymbolID > fromStr(llvm::StringRef)
Definition: SymbolID.cpp:37
static SymbolID fromRaw(llvm::StringRef)
Definition: SymbolID.cpp:28
std::string str() const
Definition: SymbolID.cpp:35
llvm::StringRef raw() const
Definition: SymbolID.cpp:23
llvm::Error error(std::error_code EC, const char *Fmt, Ts &&... Vals)
Definition: Logger.h:79
llvm::raw_ostream & operator<<(llvm::raw_ostream &OS, const CodeCompletion &C)
===– Representation.cpp - ClangDoc Representation --------—*- C++ -*-===//