[docs] Add LICENSE.txt to the root of the mono-repo
[llvm-project.git] / clang-tools-extra / clangd / index / SymbolID.cpp
blobd6686d2b1084a7cab4fe294b51e80494b8f5dccd
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 //===----------------------------------------------------------------------===//
9 #include "SymbolID.h"
10 #include "support/Logger.h"
11 #include "llvm/Support/SHA1.h"
13 namespace clang {
14 namespace clangd {
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);
22 llvm::StringRef SymbolID::raw() const {
23 return llvm::StringRef(reinterpret_cast<const char *>(HashValue.data()),
24 RawSize);
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;
34 std::string SymbolID::str() const { return llvm::toHex(raw()); }
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));
45 llvm::raw_ostream &operator<<(llvm::raw_ostream &OS, const SymbolID &ID) {
46 return OS << llvm::toHex(ID.raw());
49 } // namespace clangd
50 } // namespace clang